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 genericddi
|
---|
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/page.h>
|
---|
49 | #include <synch/mutex.h>
|
---|
50 | #include <syscall/copy.h>
|
---|
51 | #include <adt/btree.h>
|
---|
52 | #include <arch.h>
|
---|
53 | #include <align.h>
|
---|
54 | #include <errno.h>
|
---|
55 | #include <trace.h>
|
---|
56 | #include <bitops.h>
|
---|
57 |
|
---|
58 | /** This lock protects the parea_btree. */
|
---|
59 | static mutex_t parea_lock;
|
---|
60 |
|
---|
61 | /** B+tree with enabled physical memory areas. */
|
---|
62 | static btree_t parea_btree;
|
---|
63 |
|
---|
64 | /** Initialize DDI.
|
---|
65 | *
|
---|
66 | */
|
---|
67 | void ddi_init(void)
|
---|
68 | {
|
---|
69 | btree_create(&parea_btree);
|
---|
70 | mutex_initialize(&parea_lock, MUTEX_PASSIVE);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /** Enable piece of physical memory for mapping by physmem_map().
|
---|
74 | *
|
---|
75 | * @param parea Pointer to physical area structure.
|
---|
76 | *
|
---|
77 | */
|
---|
78 | void ddi_parea_register(parea_t *parea)
|
---|
79 | {
|
---|
80 | mutex_lock(&parea_lock);
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * We don't check for overlaps here as the kernel is pretty sane.
|
---|
84 | */
|
---|
85 | btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
|
---|
86 |
|
---|
87 | mutex_unlock(&parea_lock);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Map piece of physical memory into virtual address space of current task.
|
---|
91 | *
|
---|
92 | * @param phys Physical address of the starting frame.
|
---|
93 | * @param pages Number of pages to map.
|
---|
94 | * @param flags Address space area flags for the mapping.
|
---|
95 | * @param virt Virtual address of the starting page.
|
---|
96 | * @param bound Lowest virtual address bound.
|
---|
97 | *
|
---|
98 | * @return EOK on success.
|
---|
99 | * @return EPERM if the caller lacks permissions to use this syscall.
|
---|
100 | * @return EBADMEM if phys is not page aligned.
|
---|
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.
|
---|
104 | *
|
---|
105 | */
|
---|
106 | NO_TRACE static int physmem_map(uintptr_t phys, size_t pages,
|
---|
107 | unsigned int flags, uintptr_t *virt, uintptr_t bound)
|
---|
108 | {
|
---|
109 | assert(TASK);
|
---|
110 |
|
---|
111 | if ((phys % FRAME_SIZE) != 0)
|
---|
112 | return EBADMEM;
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Unprivileged tasks are only allowed to map pareas
|
---|
116 | * which are explicitly marked as such.
|
---|
117 | */
|
---|
118 | bool priv =
|
---|
119 | ((perm_get(TASK) & PERM_MEM_MANAGER) == PERM_MEM_MANAGER);
|
---|
120 |
|
---|
121 | mem_backend_data_t backend_data;
|
---|
122 | backend_data.base = phys;
|
---|
123 | backend_data.frames = pages;
|
---|
124 | backend_data.anonymous = false;
|
---|
125 |
|
---|
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,
|
---|
134 | (btree_key_t) phys, &nodep);
|
---|
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 |
|
---|
153 | irq_spinlock_lock(&zones.lock, true);
|
---|
154 | size_t znum = find_zone(ADDR2PFN(phys), pages, 0);
|
---|
155 |
|
---|
156 | if (znum == (size_t) -1) {
|
---|
157 | /*
|
---|
158 | * Frames not found in any zone
|
---|
159 | * -> assume it is a hardware device and allow mapping
|
---|
160 | * for privileged tasks.
|
---|
161 | */
|
---|
162 | irq_spinlock_unlock(&zones.lock, true);
|
---|
163 |
|
---|
164 | if (!priv)
|
---|
165 | return EPERM;
|
---|
166 |
|
---|
167 | goto map;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (zones.info[znum].flags & (ZONE_FIRMWARE | ZONE_RESERVED)) {
|
---|
171 | /*
|
---|
172 | * Frames are part of firmware or reserved zone
|
---|
173 | * -> allow mapping for privileged tasks.
|
---|
174 | */
|
---|
175 | irq_spinlock_unlock(&zones.lock, true);
|
---|
176 |
|
---|
177 | if (!priv)
|
---|
178 | return EPERM;
|
---|
179 |
|
---|
180 | goto map;
|
---|
181 | }
|
---|
182 |
|
---|
183 | irq_spinlock_unlock(&zones.lock, true);
|
---|
184 | return ENOENT;
|
---|
185 |
|
---|
186 | map:
|
---|
187 | if (!as_area_create(TASK->as, flags, FRAMES2SIZE(pages),
|
---|
188 | AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
|
---|
189 | /*
|
---|
190 | * The address space area was not created.
|
---|
191 | * We report it using ENOMEM.
|
---|
192 | */
|
---|
193 |
|
---|
194 | if (parea != NULL)
|
---|
195 | mutex_unlock(&parea_lock);
|
---|
196 |
|
---|
197 | return ENOMEM;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * Mapping is created on-demand during page fault.
|
---|
202 | */
|
---|
203 |
|
---|
204 | if (parea != NULL) {
|
---|
205 | parea->mapped = true;
|
---|
206 | mutex_unlock(&parea_lock);
|
---|
207 | }
|
---|
208 |
|
---|
209 | return EOK;
|
---|
210 | }
|
---|
211 |
|
---|
212 | NO_TRACE static int physmem_unmap(uintptr_t virt)
|
---|
213 | {
|
---|
214 | assert(TASK);
|
---|
215 |
|
---|
216 | return as_area_destroy(TASK->as, virt);
|
---|
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 | */
|
---|
230 | sysarg_t sys_physmem_map(uintptr_t phys, size_t pages, unsigned int flags,
|
---|
231 | void *virt_ptr, uintptr_t bound)
|
---|
232 | {
|
---|
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);
|
---|
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 |
|
---|
252 | sysarg_t sys_physmem_unmap(uintptr_t virt)
|
---|
253 | {
|
---|
254 | return physmem_unmap(virt);
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** Enable range of I/O space for task.
|
---|
258 | *
|
---|
259 | * @param id Task ID of the destination task.
|
---|
260 | * @param ioaddr Starting I/O address.
|
---|
261 | * @param size Size of the enabled I/O space.
|
---|
262 | *
|
---|
263 | * @return 0 on success, EPERM if the caller lacks permissions to use this
|
---|
264 | * syscall, ENOENT if there is no task matching the specified ID.
|
---|
265 | *
|
---|
266 | */
|
---|
267 | NO_TRACE static int iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
|
---|
268 | {
|
---|
269 | /*
|
---|
270 | * Make sure the caller is authorised to make this syscall.
|
---|
271 | */
|
---|
272 | perm_t perms = perm_get(TASK);
|
---|
273 | if (!(perms & PERM_IO_MANAGER))
|
---|
274 | return EPERM;
|
---|
275 |
|
---|
276 | irq_spinlock_lock(&tasks_lock, true);
|
---|
277 |
|
---|
278 | task_t *task = task_find_by_id(id);
|
---|
279 |
|
---|
280 | if ((!task) || (!container_check(CONTAINER, task->container))) {
|
---|
281 | /*
|
---|
282 | * There is no task with the specified ID
|
---|
283 | * or the task belongs to a different security
|
---|
284 | * context.
|
---|
285 | */
|
---|
286 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
287 | return ENOENT;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /* Lock the task and release the lock protecting tasks_btree. */
|
---|
291 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
---|
292 | int rc = ddi_iospace_enable_arch(task, ioaddr, size);
|
---|
293 | irq_spinlock_unlock(&task->lock, true);
|
---|
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 | *
|
---|
304 | * @return 0 on success, EPERM if the caller lacks permissions to use this
|
---|
305 | * syscall, ENOENT if there is no task matching the specified ID.
|
---|
306 | *
|
---|
307 | */
|
---|
308 | NO_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 | */
|
---|
313 | perm_t perms = perm_get(TASK);
|
---|
314 | if (!(perms & PERM_IO_MANAGER))
|
---|
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);
|
---|
335 |
|
---|
336 | return rc;
|
---|
337 | }
|
---|
338 |
|
---|
339 | /** Wrapper for SYS_ENABLE_IOSPACE syscall.
|
---|
340 | *
|
---|
341 | * @param uspace_io_arg User space address of DDI argument structure.
|
---|
342 | *
|
---|
343 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
344 | *
|
---|
345 | */
|
---|
346 | sysarg_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
|
---|
347 | {
|
---|
348 | ddi_ioarg_t arg;
|
---|
349 | int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
---|
350 | if (rc != 0)
|
---|
351 | return (sysarg_t) rc;
|
---|
352 |
|
---|
353 | return (sysarg_t) iospace_enable((task_id_t) arg.task_id,
|
---|
354 | (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
---|
355 | }
|
---|
356 |
|
---|
357 | sysarg_t sys_iospace_disable(ddi_ioarg_t *uspace_io_arg)
|
---|
358 | {
|
---|
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);
|
---|
366 | }
|
---|
367 |
|
---|
368 | NO_TRACE static int dmamem_map(uintptr_t virt, size_t size, unsigned int map_flags,
|
---|
369 | unsigned int flags, uintptr_t *phys)
|
---|
370 | {
|
---|
371 | assert(TASK);
|
---|
372 |
|
---|
373 | // TODO: implement locking of non-anonymous mapping
|
---|
374 | return page_find_mapping(virt, phys);
|
---|
375 | }
|
---|
376 |
|
---|
377 | NO_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)
|
---|
380 | {
|
---|
381 | assert(TASK);
|
---|
382 |
|
---|
383 | size_t frames = SIZE2FRAMES(size);
|
---|
384 | *phys = frame_alloc(frames, FRAME_ATOMIC, constraint);
|
---|
385 | if (*phys == 0)
|
---|
386 | return ENOMEM;
|
---|
387 |
|
---|
388 | mem_backend_data_t backend_data;
|
---|
389 | backend_data.base = *phys;
|
---|
390 | backend_data.frames = frames;
|
---|
391 | backend_data.anonymous = true;
|
---|
392 |
|
---|
393 | if (!as_area_create(TASK->as, map_flags, size,
|
---|
394 | AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
|
---|
395 | frame_free(*phys, frames);
|
---|
396 | return ENOMEM;
|
---|
397 | }
|
---|
398 |
|
---|
399 | return EOK;
|
---|
400 | }
|
---|
401 |
|
---|
402 | NO_TRACE static int dmamem_unmap(uintptr_t virt, size_t size)
|
---|
403 | {
|
---|
404 | // TODO: implement unlocking & unmap
|
---|
405 | return EOK;
|
---|
406 | }
|
---|
407 |
|
---|
408 | NO_TRACE static int dmamem_unmap_anonymous(uintptr_t virt)
|
---|
409 | {
|
---|
410 | return as_area_destroy(TASK->as, virt);
|
---|
411 | }
|
---|
412 |
|
---|
413 | sysarg_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 |
|
---|
421 | uintptr_t phys;
|
---|
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 |
|
---|
438 | uintptr_t constraint;
|
---|
439 | int rc = copy_from_uspace(&constraint, phys_ptr,
|
---|
440 | sizeof(constraint));
|
---|
441 | if (rc != EOK)
|
---|
442 | return rc;
|
---|
443 |
|
---|
444 | uintptr_t virt;
|
---|
445 | rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
|
---|
446 | if (rc != EOK)
|
---|
447 | return rc;
|
---|
448 |
|
---|
449 | uintptr_t phys;
|
---|
450 | rc = dmamem_map_anonymous(size, constraint, map_flags, flags,
|
---|
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 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | return EOK;
|
---|
469 | }
|
---|
470 |
|
---|
471 | sysarg_t sys_dmamem_unmap(uintptr_t virt, size_t size, unsigned int flags)
|
---|
472 | {
|
---|
473 | if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0)
|
---|
474 | return dmamem_unmap(virt, size);
|
---|
475 | else
|
---|
476 | return dmamem_unmap_anonymous(virt);
|
---|
477 | }
|
---|
478 |
|
---|
479 | /** @}
|
---|
480 | */
|
---|