source: mainline/kernel/generic/src/ddi/ddi.c@ dbb29de

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dbb29de was a17cced, checked in by Jakub Jermar <jakub@…>, 11 years ago

dmamem_map_anonymous() should reserve the memory.

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