source: mainline/kernel/generic/src/ddi/ddi.c@ 02667d9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 02667d9 was 63e27ef, checked in by Jiri Svoboda <jiri@…>, 8 years ago

ASSERT → assert

  • Property mode set to 100644
File size: 11.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
[63e27ef]42#include <assert.h>
[9a8d91b]43#include <ddi/ddi.h>
44#include <proc/task.h>
[719a208]45#include <security/perm.h>
[9a8d91b]46#include <mm/frame.h>
47#include <mm/as.h>
[c6ae4c2]48#include <mm/page.h>
[373acb4]49#include <synch/mutex.h>
[e3c762cd]50#include <syscall/copy.h>
[e49e234]51#include <adt/btree.h>
[9a8d91b]52#include <arch.h>
53#include <align.h>
54#include <errno.h>
[7a0359b]55#include <trace.h>
[c6ae4c2]56#include <bitops.h>
[9a8d91b]57
[f8ddd17]58/** This lock protects the parea_btree. */
[373acb4]59static mutex_t parea_lock;
[f8ddd17]60
[e49e234]61/** B+tree with enabled physical memory areas. */
62static btree_t parea_btree;
[ae318d3]63
[da1bafb]64/** Initialize DDI.
65 *
66 */
[f8ddd17]67void ddi_init(void)
68{
[e49e234]69 btree_create(&parea_btree);
[373acb4]70 mutex_initialize(&parea_lock, MUTEX_PASSIVE);
[f8ddd17]71}
72
73/** Enable piece of physical memory for mapping by physmem_map().
74 *
75 * @param parea Pointer to physical area structure.
76 *
77 */
78void ddi_parea_register(parea_t *parea)
79{
[373acb4]80 mutex_lock(&parea_lock);
[f8ddd17]81
82 /*
[e49e234]83 * We don't check for overlaps here as the kernel is pretty sane.
[f8ddd17]84 */
[e49e234]85 btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
[ae318d3]86
[373acb4]87 mutex_unlock(&parea_lock);
[f8ddd17]88}
89
[8da51ad]90/** Map piece of physical memory into virtual address space of current task.
[9a8d91b]91 *
[c6ae4c2]92 * @param phys Physical address of the starting frame.
[9a8d91b]93 * @param pages Number of pages to map.
[6212095]94 * @param flags Address space area flags for the mapping.
[fbcdeb8]95 * @param virt Virtual address of the starting page.
96 * @param bound Lowest virtual address bound.
[9a8d91b]97 *
[c6ae4c2]98 * @return EOK on success.
[719a208]99 * @return EPERM if the caller lacks permissions to use this syscall.
[fbcdeb8]100 * @return EBADMEM if phys is not page aligned.
[c6ae4c2]101 * @return ENOENT if there is no task matching the specified ID or
102 * the physical address space is not enabled for mapping.
103 * @return ENOMEM if there was a problem in creating address space area.
[e49e234]104 *
[9a8d91b]105 */
[fbcdeb8]106NO_TRACE static int physmem_map(uintptr_t phys, size_t pages,
107 unsigned int flags, uintptr_t *virt, uintptr_t bound)
[9a8d91b]108{
[63e27ef]109 assert(TASK);
[d7533c7]110
[c6ae4c2]111 if ((phys % FRAME_SIZE) != 0)
[d7533c7]112 return EBADMEM;
113
[9a8d91b]114 /*
[d7533c7]115 * Unprivileged tasks are only allowed to map pareas
116 * which are explicitly marked as such.
[9a8d91b]117 */
[d7533c7]118 bool priv =
[719a208]119 ((perm_get(TASK) & PERM_MEM_MANAGER) == PERM_MEM_MANAGER);
[ae318d3]120
[e49e234]121 mem_backend_data_t backend_data;
[c6ae4c2]122 backend_data.base = phys;
[e49e234]123 backend_data.frames = pages;
[c101dc0]124 backend_data.anonymous = false;
[ae318d3]125
[b366a6f4]126 /*
127 * Check if the memory region is explicitly enabled
128 * for mapping by any parea structure.
129 */
130
131 mutex_lock(&parea_lock);
132 btree_node_t *nodep;
133 parea_t *parea = (parea_t *) btree_search(&parea_btree,
[c6ae4c2]134 (btree_key_t) phys, &nodep);
[b366a6f4]135
136 if ((parea != NULL) && (parea->frames >= pages)) {
137 if ((!priv) && (!parea->unpriv)) {
138 mutex_unlock(&parea_lock);
139 return EPERM;
140 }
141
142 goto map;
143 }
144
145 parea = NULL;
146 mutex_unlock(&parea_lock);
147
148 /*
149 * Check if the memory region is part of physical
150 * memory generally enabled for mapping.
151 */
152
[da1bafb]153 irq_spinlock_lock(&zones.lock, true);
[c6ae4c2]154 size_t znum = find_zone(ADDR2PFN(phys), pages, 0);
[ae318d3]155
[98000fb]156 if (znum == (size_t) -1) {
[d7533c7]157 /*
158 * Frames not found in any zone
159 * -> assume it is a hardware device and allow mapping
160 * for privileged tasks.
[e49e234]161 */
[da1bafb]162 irq_spinlock_unlock(&zones.lock, true);
[d7533c7]163
164 if (!priv)
165 return EPERM;
166
[e49e234]167 goto map;
[ae318d3]168 }
169
[3164e3b]170 if (zones.info[znum].flags & (ZONE_FIRMWARE | ZONE_RESERVED)) {
[d7533c7]171 /*
[3164e3b]172 * Frames are part of firmware or reserved zone
[d7533c7]173 * -> allow mapping for privileged tasks.
174 */
[da1bafb]175 irq_spinlock_unlock(&zones.lock, true);
[d7533c7]176
177 if (!priv)
178 return EPERM;
179
[e49e234]180 goto map;
181 }
[ae318d3]182
[da1bafb]183 irq_spinlock_unlock(&zones.lock, true);
[e49e234]184 return ENOENT;
185
186map:
[fbcdeb8]187 if (!as_area_create(TASK->as, flags, FRAMES2SIZE(pages),
188 AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
[9a8d91b]189 /*
[b366a6f4]190 * The address space area was not created.
[9a8d91b]191 * We report it using ENOMEM.
192 */
[b366a6f4]193
194 if (parea != NULL)
195 mutex_unlock(&parea_lock);
196
[9a8d91b]197 return ENOMEM;
198 }
199
[0ee077ee]200 /*
201 * Mapping is created on-demand during page fault.
202 */
[b366a6f4]203
204 if (parea != NULL) {
205 parea->mapped = true;
206 mutex_unlock(&parea_lock);
207 }
208
209 return EOK;
[9a8d91b]210}
211
[fbcdeb8]212NO_TRACE static int physmem_unmap(uintptr_t virt)
213{
[63e27ef]214 assert(TASK);
[8cd680c]215
216 return as_area_destroy(TASK->as, virt);
[fbcdeb8]217}
218
219/** Wrapper for SYS_PHYSMEM_MAP syscall.
220 *
221 * @param phys Physical base address to map
222 * @param pages Number of pages
223 * @param flags Flags of newly mapped pages
224 * @param virt_ptr Destination virtual address
225 * @param bound Lowest virtual address bound.
226 *
227 * @return 0 on success, otherwise it returns error code found in errno.h
228 *
229 */
230sysarg_t sys_physmem_map(uintptr_t phys, size_t pages, unsigned int flags,
231 void *virt_ptr, uintptr_t bound)
232{
[bf9cb2f]233 uintptr_t virt;
234 int rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
235 if (rc != EOK)
236 return rc;
237
238 rc = physmem_map(ALIGN_DOWN(phys, FRAME_SIZE), pages, flags, &virt,
239 bound);
[fbcdeb8]240 if (rc != EOK)
241 return rc;
242
243 rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
244 if (rc != EOK) {
245 physmem_unmap((uintptr_t) virt);
246 return rc;
247 }
248
249 return EOK;
250}
251
252sysarg_t sys_physmem_unmap(uintptr_t virt)
253{
254 return physmem_unmap(virt);
255}
256
[f52e54da]257/** Enable range of I/O space for task.
258 *
[8cd680c]259 * @param id Task ID of the destination task.
[f52e54da]260 * @param ioaddr Starting I/O address.
[8cd680c]261 * @param size Size of the enabled I/O space.
[f52e54da]262 *
[719a208]263 * @return 0 on success, EPERM if the caller lacks permissions to use this
[e49e234]264 * syscall, ENOENT if there is no task matching the specified ID.
265 *
[f52e54da]266 */
[fbcdeb8]267NO_TRACE static int iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
[f52e54da]268{
269 /*
270 * Make sure the caller is authorised to make this syscall.
271 */
[719a208]272 perm_t perms = perm_get(TASK);
273 if (!(perms & PERM_IO_MANAGER))
[f52e54da]274 return EPERM;
275
[da1bafb]276 irq_spinlock_lock(&tasks_lock, true);
[f52e54da]277
[e49e234]278 task_t *task = task_find_by_id(id);
[f52e54da]279
[473d5d2]280 if ((!task) || (!container_check(CONTAINER, task->container))) {
[f52e54da]281 /*
[cfffb290]282 * There is no task with the specified ID
283 * or the task belongs to a different security
284 * context.
[f52e54da]285 */
[da1bafb]286 irq_spinlock_unlock(&tasks_lock, true);
[f52e54da]287 return ENOENT;
288 }
[e49e234]289
[f52e54da]290 /* Lock the task and release the lock protecting tasks_btree. */
[da1bafb]291 irq_spinlock_exchange(&tasks_lock, &task->lock);
[e49e234]292 int rc = ddi_iospace_enable_arch(task, ioaddr, size);
[da1bafb]293 irq_spinlock_unlock(&task->lock, true);
[8cd680c]294
295 return rc;
296}
297
298/** Disable range of I/O space for task.
299 *
300 * @param id Task ID of the destination task.
301 * @param ioaddr Starting I/O address.
302 * @param size Size of the enabled I/O space.
303 *
[719a208]304 * @return 0 on success, EPERM if the caller lacks permissions to use this
[8cd680c]305 * syscall, ENOENT if there is no task matching the specified ID.
306 *
307 */
308NO_TRACE static int iospace_disable(task_id_t id, uintptr_t ioaddr, size_t size)
309{
310 /*
311 * Make sure the caller is authorised to make this syscall.
312 */
[719a208]313 perm_t perms = perm_get(TASK);
314 if (!(perms & PERM_IO_MANAGER))
[8cd680c]315 return EPERM;
316
317 irq_spinlock_lock(&tasks_lock, true);
318
319 task_t *task = task_find_by_id(id);
320
321 if ((!task) || (!container_check(CONTAINER, task->container))) {
322 /*
323 * There is no task with the specified ID
324 * or the task belongs to a different security
325 * context.
326 */
327 irq_spinlock_unlock(&tasks_lock, true);
328 return ENOENT;
329 }
330
331 /* Lock the task and release the lock protecting tasks_btree. */
332 irq_spinlock_exchange(&tasks_lock, &task->lock);
333 int rc = ddi_iospace_disable_arch(task, ioaddr, size);
334 irq_spinlock_unlock(&task->lock, true);
[e49e234]335
[f52e54da]336 return rc;
337}
338
339/** Wrapper for SYS_ENABLE_IOSPACE syscall.
340 *
[abbc16e]341 * @param uspace_io_arg User space address of DDI argument structure.
[f52e54da]342 *
343 * @return 0 on success, otherwise it returns error code found in errno.h
[e49e234]344 *
345 */
[96b02eb9]346sysarg_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
[f52e54da]347{
348 ddi_ioarg_t arg;
[e49e234]349 int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
[e3c762cd]350 if (rc != 0)
[96b02eb9]351 return (sysarg_t) rc;
[e49e234]352
[fbcdeb8]353 return (sysarg_t) iospace_enable((task_id_t) arg.task_id,
[f619ec11]354 (uintptr_t) arg.ioaddr, (size_t) arg.size);
[f52e54da]355}
[2bb8648]356
[fbcdeb8]357sysarg_t sys_iospace_disable(ddi_ioarg_t *uspace_io_arg)
358{
[8cd680c]359 ddi_ioarg_t arg;
360 int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
361 if (rc != 0)
362 return (sysarg_t) rc;
363
364 return (sysarg_t) iospace_disable((task_id_t) arg.task_id,
365 (uintptr_t) arg.ioaddr, (size_t) arg.size);
[fbcdeb8]366}
367
368NO_TRACE static int dmamem_map(uintptr_t virt, size_t size, unsigned int map_flags,
[8cbf1c3]369 unsigned int flags, uintptr_t *phys)
[c6ae4c2]370{
[63e27ef]371 assert(TASK);
[c6ae4c2]372
[fbcdeb8]373 // TODO: implement locking of non-anonymous mapping
374 return page_find_mapping(virt, phys);
375}
376
[b0c2075]377NO_TRACE static int dmamem_map_anonymous(size_t size, uintptr_t constraint,
378 unsigned int map_flags, unsigned int flags, uintptr_t *phys,
379 uintptr_t *virt, uintptr_t bound)
[fbcdeb8]380{
[63e27ef]381 assert(TASK);
[fbcdeb8]382
[e2a0d76]383 size_t frames = SIZE2FRAMES(size);
[a17cced]384 *phys = frame_alloc(frames, FRAME_ATOMIC, constraint);
[8cbf1c3]385 if (*phys == 0)
[fbcdeb8]386 return ENOMEM;
387
388 mem_backend_data_t backend_data;
[8cbf1c3]389 backend_data.base = *phys;
[e2a0d76]390 backend_data.frames = frames;
[c101dc0]391 backend_data.anonymous = true;
[fbcdeb8]392
393 if (!as_area_create(TASK->as, map_flags, size,
394 AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
[a17cced]395 frame_free(*phys, frames);
[fbcdeb8]396 return ENOMEM;
[c6ae4c2]397 }
[fbcdeb8]398
399 return EOK;
[c6ae4c2]400}
401
[fbcdeb8]402NO_TRACE static int dmamem_unmap(uintptr_t virt, size_t size)
[c6ae4c2]403{
404 // TODO: implement unlocking & unmap
405 return EOK;
406}
407
[fbcdeb8]408NO_TRACE static int dmamem_unmap_anonymous(uintptr_t virt)
[c6ae4c2]409{
[c101dc0]410 return as_area_destroy(TASK->as, virt);
[fbcdeb8]411}
412
413sysarg_t sys_dmamem_map(size_t size, unsigned int map_flags, unsigned int flags,
414 void *phys_ptr, void *virt_ptr, uintptr_t bound)
415{
416 if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0) {
417 /*
418 * Non-anonymous DMA mapping
419 */
420
[8cbf1c3]421 uintptr_t phys;
[fbcdeb8]422 int rc = dmamem_map((uintptr_t) virt_ptr, size, map_flags,
423 flags, &phys);
424
425 if (rc != EOK)
426 return rc;
427
428 rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
429 if (rc != EOK) {
430 dmamem_unmap((uintptr_t) virt_ptr, size);
431 return rc;
432 }
433 } else {
434 /*
435 * Anonymous DMA mapping
436 */
437
[b0c2075]438 uintptr_t constraint;
439 int rc = copy_from_uspace(&constraint, phys_ptr,
440 sizeof(constraint));
441 if (rc != EOK)
442 return rc;
443
[bf9cb2f]444 uintptr_t virt;
445 rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
446 if (rc != EOK)
447 return rc;
448
[8cbf1c3]449 uintptr_t phys;
[b0c2075]450 rc = dmamem_map_anonymous(size, constraint, map_flags, flags,
[fbcdeb8]451 &phys, &virt, bound);
452 if (rc != EOK)
453 return rc;
454
455 rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
456 if (rc != EOK) {
457 dmamem_unmap_anonymous((uintptr_t) virt);
458 return rc;
459 }
460
461 rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
462 if (rc != EOK) {
463 dmamem_unmap_anonymous((uintptr_t) virt);
464 return rc;
465 }
[c6ae4c2]466 }
467
468 return EOK;
469}
470
471sysarg_t sys_dmamem_unmap(uintptr_t virt, size_t size, unsigned int flags)
472{
[fbcdeb8]473 if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0)
474 return dmamem_unmap(virt, size);
475 else
476 return dmamem_unmap_anonymous(virt);
[c6ae4c2]477}
478
[06e1e95]479/** @}
[b45c443]480 */
Note: See TracBrowser for help on using the repository browser.