source: mainline/kernel/generic/src/ddi/ddi.c@ 86018c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86018c1 was 5418217, checked in by Martin Decky <martin@…>, 16 years ago

add parentheses for better clarity

  • Property mode set to 100644
File size: 7.9 KB
RevLine 
[9a8d91b]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[9a8d91b]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
[b45c443]28
[06e1e95]29/** @addtogroup genericddi
[b45c443]30 * @{
31 */
[e49e234]32
[9179d0a]33/**
[b45c443]34 * @file
[e49e234]35 * @brief Device Driver Interface functions.
[9179d0a]36 *
37 * This file contains functions that comprise the Device Driver Interface.
38 * These are the functions for mapping physical memory and enabling I/O
39 * space to tasks.
40 */
[9a8d91b]41
42#include <ddi/ddi.h>
43#include <ddi/ddi_arg.h>
44#include <proc/task.h>
45#include <security/cap.h>
46#include <mm/frame.h>
47#include <mm/as.h>
48#include <synch/spinlock.h>
[e3c762cd]49#include <syscall/copy.h>
[e49e234]50#include <adt/btree.h>
[9a8d91b]51#include <arch.h>
52#include <align.h>
53#include <errno.h>
54
[f8ddd17]55/** This lock protects the parea_btree. */
56SPINLOCK_INITIALIZE(parea_lock);
57
[e49e234]58/** B+tree with enabled physical memory areas. */
59static btree_t parea_btree;
[ae318d3]60
[f8ddd17]61/** Initialize DDI. */
62void ddi_init(void)
63{
[e49e234]64 btree_create(&parea_btree);
[f8ddd17]65}
66
67/** Enable piece of physical memory for mapping by physmem_map().
68 *
69 * @param parea Pointer to physical area structure.
70 *
71 */
72void ddi_parea_register(parea_t *parea)
73{
[e49e234]74 ipl_t ipl = interrupts_disable();
[f8ddd17]75 spinlock_lock(&parea_lock);
76
77 /*
[e49e234]78 * We don't check for overlaps here as the kernel is pretty sane.
[f8ddd17]79 */
[e49e234]80 btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
[ae318d3]81
[f8ddd17]82 spinlock_unlock(&parea_lock);
[ae318d3]83 interrupts_restore(ipl);
[f8ddd17]84}
85
[8da51ad]86/** Map piece of physical memory into virtual address space of current task.
[9a8d91b]87 *
[e49e234]88 * @param pf Physical address of the starting frame.
89 * @param vp Virtual address of the starting page.
[9a8d91b]90 * @param pages Number of pages to map.
[6212095]91 * @param flags Address space area flags for the mapping.
[9a8d91b]92 *
[f8ddd17]93 * @return 0 on success, EPERM if the caller lacks capabilities to use this
[e49e234]94 * syscall, EBADMEM if pf or vf is not page aligned, ENOENT if there
95 * is no task matching the specified ID or the physical address space
96 * is not enabled for mapping and ENOMEM if there was a problem in
97 * creating address space area.
98 *
[9a8d91b]99 */
[98000fb]100static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, size_t pages, int flags)
[9a8d91b]101{
[e49e234]102 ASSERT(TASK);
103 ASSERT((pf % FRAME_SIZE) == 0);
104 ASSERT((vp % PAGE_SIZE) == 0);
[9a8d91b]105
106 /*
107 * Make sure the caller is authorised to make this syscall.
108 */
[e49e234]109 cap_t caps = cap_get(TASK);
[9a8d91b]110 if (!(caps & CAP_MEM_MANAGER))
111 return EPERM;
[ae318d3]112
[e49e234]113 mem_backend_data_t backend_data;
114 backend_data.base = pf;
115 backend_data.frames = pages;
[ae318d3]116
[e49e234]117 ipl_t ipl = interrupts_disable();
[ae318d3]118
[e49e234]119 /* Find the zone of the physical memory */
120 spinlock_lock(&zones.lock);
[98000fb]121 size_t znum = find_zone(ADDR2PFN(pf), pages, 0);
[ae318d3]122
[98000fb]123 if (znum == (size_t) -1) {
[e49e234]124 /* Frames not found in any zones
125 * -> assume it is hardware device and allow mapping
126 */
127 spinlock_unlock(&zones.lock);
128 goto map;
[ae318d3]129 }
130
[e49e234]131 if (zones.info[znum].flags & ZONE_FIRMWARE) {
132 /* Frames are part of firmware */
133 spinlock_unlock(&zones.lock);
134 goto map;
135 }
[ae318d3]136
[e49e234]137 if (zone_flags_available(zones.info[znum].flags)) {
138 /* Frames are part of physical memory, check if the memory
139 * region is enabled for mapping.
[f8ddd17]140 */
[e49e234]141 spinlock_unlock(&zones.lock);
142
143 spinlock_lock(&parea_lock);
144 btree_node_t *nodep;
145 parea_t *parea = (parea_t *) btree_search(&parea_btree,
146 (btree_key_t) pf, &nodep);
147
148 if ((!parea) || (parea->frames < pages))
149 goto err;
150
151 spinlock_unlock(&parea_lock);
152 goto map;
[f8ddd17]153 }
[ae318d3]154
[e49e234]155err:
156 spinlock_unlock(&zones.lock);
157 interrupts_restore(ipl);
158 return ENOENT;
159
160map:
[8da51ad]161 spinlock_lock(&TASK->lock);
[9a8d91b]162
[e49e234]163 if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp,
164 AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
[9a8d91b]165 /*
166 * The address space area could not have been created.
167 * We report it using ENOMEM.
168 */
[8da51ad]169 spinlock_unlock(&TASK->lock);
[9a8d91b]170 interrupts_restore(ipl);
171 return ENOMEM;
172 }
173
[0ee077ee]174 /*
175 * Mapping is created on-demand during page fault.
176 */
177
[8da51ad]178 spinlock_unlock(&TASK->lock);
[9a8d91b]179 interrupts_restore(ipl);
180 return 0;
181}
182
[f52e54da]183/** Enable range of I/O space for task.
184 *
185 * @param id Task ID of the destination task.
186 * @param ioaddr Starting I/O address.
187 * @param size Size of the enabled I/O space..
188 *
[f8ddd17]189 * @return 0 on success, EPERM if the caller lacks capabilities to use this
[e49e234]190 * syscall, ENOENT if there is no task matching the specified ID.
191 *
[f52e54da]192 */
[7f1c620]193static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
[f52e54da]194{
195 /*
196 * Make sure the caller is authorised to make this syscall.
197 */
[e49e234]198 cap_t caps = cap_get(TASK);
[f52e54da]199 if (!(caps & CAP_IO_MANAGER))
200 return EPERM;
201
[e49e234]202 ipl_t ipl = interrupts_disable();
[f52e54da]203 spinlock_lock(&tasks_lock);
204
[e49e234]205 task_t *task = task_find_by_id(id);
[f52e54da]206
[e49e234]207 if ((!task) || (!context_check(CONTEXT, task->context))) {
[f52e54da]208 /*
[cfffb290]209 * There is no task with the specified ID
210 * or the task belongs to a different security
211 * context.
[f52e54da]212 */
213 spinlock_unlock(&tasks_lock);
214 interrupts_restore(ipl);
215 return ENOENT;
216 }
[e49e234]217
[f52e54da]218 /* Lock the task and release the lock protecting tasks_btree. */
[e49e234]219 spinlock_lock(&task->lock);
[f52e54da]220 spinlock_unlock(&tasks_lock);
221
[e49e234]222 int rc = ddi_iospace_enable_arch(task, ioaddr, size);
223
224 spinlock_unlock(&task->lock);
[f52e54da]225 interrupts_restore(ipl);
[e49e234]226
[f52e54da]227 return rc;
228}
229
[5a8b2a2]230/** Wrapper for SYS_PHYSMEM_MAP syscall.
[9a8d91b]231 *
[8da51ad]232 * @param phys_base Physical base address to map
233 * @param virt_base Destination virtual address
234 * @param pages Number of pages
235 * @param flags Flags of newly mapped pages
[9a8d91b]236 *
237 * @return 0 on success, otherwise it returns error code found in errno.h
[e49e234]238 *
239 */
[f619ec11]240unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base,
241 unative_t pages, unative_t flags)
[9a8d91b]242{
[f8ddd17]243 return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
[f619ec11]244 FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
[98000fb]245 (size_t) pages, (int) flags);
[9a8d91b]246}
[f52e54da]247
248/** Wrapper for SYS_ENABLE_IOSPACE syscall.
249 *
[abbc16e]250 * @param uspace_io_arg User space address of DDI argument structure.
[f52e54da]251 *
252 * @return 0 on success, otherwise it returns error code found in errno.h
[e49e234]253 *
254 */
[7f1c620]255unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
[f52e54da]256{
257 ddi_ioarg_t arg;
[e49e234]258 int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
[e3c762cd]259 if (rc != 0)
[7f1c620]260 return (unative_t) rc;
[e49e234]261
[f8ddd17]262 return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id,
[f619ec11]263 (uintptr_t) arg.ioaddr, (size_t) arg.size);
[f52e54da]264}
[2bb8648]265
266/** Disable or enable preemption.
267 *
[f8ddd17]268 * @param enable If non-zero, the preemption counter will be decremented,
[e49e234]269 * leading to potential enabling of preemption. Otherwise
270 * the preemption counter will be incremented, preventing
271 * preemption from occurring.
[2bb8648]272 *
273 * @return Zero on success or EPERM if callers capabilities are not sufficient.
[e49e234]274 *
275 */
[7f1c620]276unative_t sys_preempt_control(int enable)
[2bb8648]277{
[5418217]278 if (!(cap_get(TASK) & CAP_PREEMPT_CONTROL))
[ae318d3]279 return EPERM;
[e49e234]280
[ae318d3]281 if (enable)
282 preemption_enable();
283 else
284 preemption_disable();
[e49e234]285
[ae318d3]286 return 0;
[2bb8648]287}
[b45c443]288
[06e1e95]289/** @}
[b45c443]290 */
Note: See TracBrowser for help on using the repository browser.