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

Last change on this file was 597fa24, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Enable static initialization of kernel synchronization primitives

  • Property mode set to 100644
File size: 13.6 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
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 */
28
29/** @addtogroup kernel_generic_ddi
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Device Driver Interface functions.
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 */
41
42#include <assert.h>
43#include <ddi/ddi.h>
44#include <proc/task.h>
45#include <security/perm.h>
46#include <mm/frame.h>
47#include <mm/as.h>
48#include <mm/km.h>
49#include <mm/page.h>
50#include <synch/mutex.h>
51#include <syscall/copy.h>
52#include <adt/odict.h>
53#include <arch.h>
54#include <align.h>
55#include <errno.h>
56#include <memw.h>
57#include <trace.h>
58#include <bitops.h>
59#include <arch/asm.h>
60
61/** This lock protects the @c pareas ordered dictionary. */
62static MUTEX_INITIALIZE(pareas_lock, MUTEX_PASSIVE);
63
64/** Ordered dictionary of enabled physical memory areas by base address. */
65static odict_t pareas;
66
67static void *pareas_getkey(odlink_t *);
68static int pareas_cmp(void *, void *);
69
70/** Initialize DDI.
71 *
72 */
73void ddi_init(void)
74{
75 odict_initialize(&pareas, pareas_getkey, pareas_cmp);
76}
77
78/** Initialize physical area structure.
79 *
80 * This should always be called first on the parea structure before
81 * filling in fields and calling ddi_parea_register.
82 *
83 * @param parea Pointer to physical area structure.
84 *
85 */
86void ddi_parea_init(parea_t *parea)
87{
88 memset(parea, 0, sizeof(parea_t));
89}
90
91/** Enable piece of physical memory for mapping by physmem_map().
92 *
93 * @param parea Pointer to physical area structure.
94 *
95 */
96void ddi_parea_register(parea_t *parea)
97{
98 mutex_lock(&pareas_lock);
99
100 /*
101 * We don't check for overlaps here as the kernel is pretty sane.
102 */
103 odict_insert(&parea->lpareas, &pareas, NULL);
104
105 mutex_unlock(&pareas_lock);
106}
107
108/** Norify physical area has been unmapped.
109 *
110 * @param parea Physical area
111 */
112void ddi_parea_unmap_notify(parea_t *parea)
113{
114 parea->mapped = false;
115 if (parea->mapped_changed != NULL)
116 parea->mapped_changed(parea->arg);
117}
118
119/** Map piece of physical memory into virtual address space of current task.
120 *
121 * @param phys Physical address of the starting frame.
122 * @param pages Number of pages to map.
123 * @param flags Address space area flags for the mapping.
124 * @param virt Virtual address of the starting page.
125 * @param bound Lowest virtual address bound.
126 *
127 * @return EOK on success.
128 * @return EPERM if the caller lacks permissions to use this syscall.
129 * @return EBADMEM if phys is not page aligned.
130 * @return ENOENT if there is no task matching the specified ID or
131 * the physical address space is not enabled for mapping.
132 * @return ENOMEM if there was a problem in creating address space area.
133 *
134 */
135_NO_TRACE static errno_t physmem_map(uintptr_t phys, size_t pages,
136 unsigned int flags, uintptr_t *virt, uintptr_t bound)
137{
138 assert(TASK);
139
140 if ((phys % FRAME_SIZE) != 0)
141 return EBADMEM;
142
143 /*
144 * Unprivileged tasks are only allowed to map pareas
145 * which are explicitly marked as such.
146 */
147 bool priv =
148 ((perm_get(TASK) & PERM_MEM_MANAGER) == PERM_MEM_MANAGER);
149
150 mem_backend_data_t backend_data;
151 backend_data.base = phys;
152 backend_data.frames = pages;
153 backend_data.anonymous = false;
154
155 /*
156 * Check if the memory region is explicitly enabled
157 * for mapping by any parea structure.
158 */
159
160 mutex_lock(&pareas_lock);
161 odlink_t *odlink = odict_find_eq(&pareas, &phys, NULL);
162 parea_t *parea = odlink != NULL ?
163 odict_get_instance(odlink, parea_t, lpareas) : NULL;
164
165 if ((parea != NULL) && (parea->frames >= pages)) {
166 if ((!priv) && (!parea->unpriv)) {
167 mutex_unlock(&pareas_lock);
168 return EPERM;
169 }
170
171 goto map;
172 }
173
174 parea = NULL;
175 mutex_unlock(&pareas_lock);
176
177 /*
178 * Check if the memory region is part of physical
179 * memory generally enabled for mapping.
180 */
181
182 irq_spinlock_lock(&zones.lock, true);
183 size_t znum = find_zone(ADDR2PFN(phys), pages, 0);
184
185 if (znum == (size_t) -1) {
186 /*
187 * Frames not found in any zone
188 * -> assume it is a hardware device and allow mapping
189 * for privileged tasks.
190 */
191 irq_spinlock_unlock(&zones.lock, true);
192
193 if (!priv)
194 return EPERM;
195
196 goto map;
197 }
198
199 if (zones.info[znum].flags & (ZONE_FIRMWARE | ZONE_RESERVED)) {
200 /*
201 * Frames are part of firmware or reserved zone
202 * -> allow mapping for privileged tasks.
203 */
204 irq_spinlock_unlock(&zones.lock, true);
205
206 if (!priv)
207 return EPERM;
208
209 goto map;
210 }
211
212 irq_spinlock_unlock(&zones.lock, true);
213 return ENOENT;
214
215map:
216 backend_data.parea = parea;
217
218 if (!as_area_create(TASK->as, flags, FRAMES2SIZE(pages),
219 AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
220 /*
221 * The address space area was not created.
222 * We report it using ENOMEM.
223 */
224
225 if (parea != NULL)
226 mutex_unlock(&pareas_lock);
227
228 return ENOMEM;
229 }
230
231 /*
232 * Mapping is created on-demand during page fault.
233 */
234
235 if (parea != NULL) {
236 parea->mapped = true;
237 mutex_unlock(&pareas_lock);
238 }
239
240 return EOK;
241}
242
243_NO_TRACE static errno_t physmem_unmap(uintptr_t virt)
244{
245 assert(TASK);
246
247 return as_area_destroy(TASK->as, virt);
248}
249
250/** Wrapper for SYS_PHYSMEM_MAP syscall.
251 *
252 * @param phys Physical base address to map
253 * @param pages Number of pages
254 * @param flags Flags of newly mapped pages
255 * @param virt_ptr Destination virtual address
256 * @param bound Lowest virtual address bound.
257 *
258 * @return 0 on success, otherwise it returns error code found in errno.h
259 *
260 */
261sys_errno_t sys_physmem_map(uintptr_t phys, size_t pages, unsigned int flags,
262 uspace_ptr_uintptr_t virt_ptr, uintptr_t bound)
263{
264 uintptr_t virt;
265 errno_t rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
266 if (rc != EOK)
267 return rc;
268
269 rc = physmem_map(ALIGN_DOWN(phys, FRAME_SIZE), pages, flags, &virt,
270 bound);
271 if (rc != EOK)
272 return rc;
273
274 rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
275 if (rc != EOK) {
276 physmem_unmap(virt);
277 return rc;
278 }
279
280 return EOK;
281}
282
283sys_errno_t sys_physmem_unmap(uintptr_t virt)
284{
285 return physmem_unmap(virt);
286}
287
288/** Get key function for the @c pareas ordered dictionary.
289 *
290 * @param odlink Link
291 * @return Pointer to base address cast as 'void *'
292 */
293static void *pareas_getkey(odlink_t *odlink)
294{
295 parea_t *parea = odict_get_instance(odlink, parea_t, lpareas);
296 return (void *) &parea->pbase;
297}
298
299/** Key comparison function for the @c pareas ordered dictionary.
300 *
301 * @param a Pointer to parea A base
302 * @param b Pointer to parea B base
303 * @return -1, 0, 1 iff base of A is less than, equal to, greater than B
304 */
305static int pareas_cmp(void *a, void *b)
306{
307 uintptr_t pa = *(uintptr_t *)a;
308 uintptr_t pb = *(uintptr_t *)b;
309
310 if (pa < pb)
311 return -1;
312 else if (pa == pb)
313 return 0;
314 else
315 return +1;
316}
317
318/** Enable range of I/O space for task.
319 *
320 * @param id Task ID of the destination task.
321 * @param ioaddr Starting I/O address.
322 * @param size Size of the enabled I/O space.
323 *
324 * @return 0 on success, EPERM if the caller lacks permissions to use this
325 * syscall, ENOENT if there is no task matching the specified ID.
326 *
327 */
328_NO_TRACE static errno_t iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
329{
330 /*
331 * Make sure the caller is authorised to make this syscall.
332 */
333 perm_t perms = perm_get(TASK);
334 if (!(perms & PERM_IO_MANAGER))
335 return EPERM;
336
337 task_t *task = task_find_by_id(id);
338
339 if (!task)
340 return ENOENT;
341
342 errno_t rc = ENOENT;
343
344 irq_spinlock_lock(&task->lock, true);
345
346 /* Check that the task belongs to the correct security context. */
347 if (container_check(CONTAINER, task->container))
348 rc = ddi_iospace_enable_arch(task, ioaddr, size);
349
350 irq_spinlock_unlock(&task->lock, true);
351 task_release(task);
352 return rc;
353}
354
355/** Disable range of I/O space for task.
356 *
357 * @param id Task ID of the destination task.
358 * @param ioaddr Starting I/O address.
359 * @param size Size of the enabled I/O space.
360 *
361 * @return 0 on success, EPERM if the caller lacks permissions to use this
362 * syscall, ENOENT if there is no task matching the specified ID.
363 *
364 */
365_NO_TRACE static errno_t iospace_disable(task_id_t id, uintptr_t ioaddr, size_t size)
366{
367 /*
368 * Make sure the caller is authorised to make this syscall.
369 */
370 perm_t perms = perm_get(TASK);
371 if (!(perms & PERM_IO_MANAGER))
372 return EPERM;
373
374 task_t *task = task_find_by_id(id);
375
376 if (!task)
377 return ENOENT;
378
379 errno_t rc = ENOENT;
380
381 irq_spinlock_lock(&task->lock, true);
382
383 /* Check that the task belongs to the correct security context. */
384 if (container_check(CONTAINER, task->container))
385 rc = ddi_iospace_disable_arch(task, ioaddr, size);
386
387 irq_spinlock_unlock(&task->lock, true);
388 task_release(task);
389 return rc;
390}
391
392/** Wrapper for SYS_ENABLE_IOSPACE syscall.
393 *
394 * @param uspace_io_arg User space address of DDI argument structure.
395 *
396 * @return 0 on success, otherwise it returns error code found in errno.h
397 *
398 */
399sys_errno_t sys_iospace_enable(uspace_ptr_ddi_ioarg_t uspace_io_arg)
400{
401 ddi_ioarg_t arg;
402 errno_t rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
403 if (rc != EOK)
404 return (sys_errno_t) rc;
405
406 return (sys_errno_t) iospace_enable((task_id_t) arg.task_id,
407 (uintptr_t) arg.ioaddr, (size_t) arg.size);
408}
409
410sys_errno_t sys_iospace_disable(uspace_ptr_ddi_ioarg_t uspace_io_arg)
411{
412 ddi_ioarg_t arg;
413 errno_t rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
414 if (rc != EOK)
415 return (sys_errno_t) rc;
416
417 return (sys_errno_t) iospace_disable((task_id_t) arg.task_id,
418 (uintptr_t) arg.ioaddr, (size_t) arg.size);
419}
420
421_NO_TRACE static errno_t dmamem_map(uintptr_t virt, size_t size, unsigned int map_flags,
422 unsigned int flags, uintptr_t *phys)
423{
424 assert(TASK);
425
426 // TODO: implement locking of non-anonymous mapping
427 return page_find_mapping(virt, phys);
428}
429
430_NO_TRACE static errno_t dmamem_map_anonymous(size_t size, uintptr_t constraint,
431 unsigned int map_flags, unsigned int flags, uintptr_t *phys,
432 uintptr_t *virt, uintptr_t bound)
433{
434 assert(TASK);
435
436 size_t frames = SIZE2FRAMES(size);
437 if (frames == 0)
438 return EINVAL;
439
440 // FIXME: probably need to ensure that the memory is suitable for DMA
441 *phys = frame_alloc(frames, FRAME_ATOMIC, constraint);
442 if (*phys == 0)
443 return ENOMEM;
444
445 mem_backend_data_t backend_data;
446 backend_data.base = *phys;
447 backend_data.frames = frames;
448 backend_data.anonymous = true;
449 backend_data.parea = NULL;
450
451 if (!as_area_create(TASK->as, map_flags, size,
452 AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
453 frame_free(*phys, frames);
454 return ENOMEM;
455 }
456
457 return EOK;
458}
459
460_NO_TRACE static errno_t dmamem_unmap(uintptr_t virt, size_t size)
461{
462 // TODO: implement unlocking & unmap
463 return EOK;
464}
465
466_NO_TRACE static errno_t dmamem_unmap_anonymous(uintptr_t virt)
467{
468 return as_area_destroy(TASK->as, virt);
469}
470
471sys_errno_t sys_dmamem_map(size_t size, unsigned int map_flags, unsigned int flags,
472 uspace_ptr_uintptr_t phys_ptr, uspace_ptr_uintptr_t virt_ptr, uintptr_t bound)
473{
474 if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0) {
475 /*
476 * Non-anonymous DMA mapping
477 */
478
479 uintptr_t phys;
480 errno_t rc = dmamem_map(virt_ptr, size, map_flags,
481 flags, &phys);
482
483 if (rc != EOK)
484 return rc;
485
486 rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
487 if (rc != EOK) {
488 dmamem_unmap(virt_ptr, size);
489 return rc;
490 }
491 } else {
492 /*
493 * Anonymous DMA mapping
494 */
495
496 uintptr_t constraint;
497 errno_t rc = copy_from_uspace(&constraint, phys_ptr,
498 sizeof(constraint));
499 if (rc != EOK)
500 return rc;
501
502 uintptr_t virt;
503 rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
504 if (rc != EOK)
505 return rc;
506
507 uintptr_t phys;
508 rc = dmamem_map_anonymous(size, constraint, map_flags, flags,
509 &phys, &virt, bound);
510 if (rc != EOK)
511 return rc;
512
513 rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
514 if (rc != EOK) {
515 dmamem_unmap_anonymous(virt);
516 return rc;
517 }
518
519 rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
520 if (rc != EOK) {
521 dmamem_unmap_anonymous(virt);
522 return rc;
523 }
524 }
525
526 return EOK;
527}
528
529sys_errno_t sys_dmamem_unmap(uintptr_t virt, size_t size, unsigned int flags)
530{
531 if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0)
532 return dmamem_unmap(virt, size);
533 else
534 return dmamem_unmap_anonymous(virt);
535}
536void *pio_map(void *phys, size_t size)
537{
538#ifdef IO_SPACE_BOUNDARY
539 if (phys < IO_SPACE_BOUNDARY)
540 return phys;
541#endif
542 return (void *) km_map((uintptr_t) phys, size, KM_NATURAL_ALIGNMENT,
543 PAGE_READ | PAGE_WRITE | PAGE_NOT_CACHEABLE);
544}
545
546void pio_unmap(void *phys, void *virt, size_t size)
547{
548#ifdef IO_SPACE_BOUNDARY
549 if (phys < IO_SPACE_BOUNDARY)
550 return;
551#endif
552 km_unmap((uintptr_t) virt, size);
553}
554
555/** @}
556 */
Note: See TracBrowser for help on using the repository browser.