2 * Copyright (c) 2006 Jakub Jermar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup genericddi
35 * @brief Device Driver Interface functions.
37 * This file contains functions that comprise the Device Driver Interface.
38 * These are the functions for mapping physical memory and enabling I/O
43 #include <proc/task.h>
44 #include <security/perm.h>
48 #include <synch/mutex.h>
49 #include <syscall/copy.h>
50 #include <adt/btree.h>
57 /** This lock protects the parea_btree. */
58 static mutex_t parea_lock
;
60 /** B+tree with enabled physical memory areas. */
61 static btree_t parea_btree
;
68 btree_create(&parea_btree
);
69 mutex_initialize(&parea_lock
, MUTEX_PASSIVE
);
72 /** Enable piece of physical memory for mapping by physmem_map().
74 * @param parea Pointer to physical area structure.
77 void ddi_parea_register(parea_t
*parea
)
79 mutex_lock(&parea_lock
);
82 * We don't check for overlaps here as the kernel is pretty sane.
84 btree_insert(&parea_btree
, (btree_key_t
) parea
->pbase
, parea
, NULL
);
86 mutex_unlock(&parea_lock
);
89 /** Map piece of physical memory into virtual address space of current task.
91 * @param phys Physical address of the starting frame.
92 * @param pages Number of pages to map.
93 * @param flags Address space area flags for the mapping.
94 * @param virt Virtual address of the starting page.
95 * @param bound Lowest virtual address bound.
97 * @return EOK on success.
98 * @return EPERM if the caller lacks permissions to use this syscall.
99 * @return EBADMEM if phys is not page aligned.
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.
105 NO_TRACE
static int physmem_map(uintptr_t phys
, size_t pages
,
106 unsigned int flags
, uintptr_t *virt
, uintptr_t bound
)
110 if ((phys
% FRAME_SIZE
) != 0)
114 * Unprivileged tasks are only allowed to map pareas
115 * which are explicitly marked as such.
118 ((perm_get(TASK
) & PERM_MEM_MANAGER
) == PERM_MEM_MANAGER
);
120 mem_backend_data_t backend_data
;
121 backend_data
.base
= phys
;
122 backend_data
.frames
= pages
;
123 backend_data
.anonymous
= false;
126 * Check if the memory region is explicitly enabled
127 * for mapping by any parea structure.
130 mutex_lock(&parea_lock
);
132 parea_t
*parea
= (parea_t
*) btree_search(&parea_btree
,
133 (btree_key_t
) phys
, &nodep
);
135 if ((parea
!= NULL
) && (parea
->frames
>= pages
)) {
136 if ((!priv
) && (!parea
->unpriv
)) {
137 mutex_unlock(&parea_lock
);
145 mutex_unlock(&parea_lock
);
148 * Check if the memory region is part of physical
149 * memory generally enabled for mapping.
152 irq_spinlock_lock(&zones
.lock
, true);
153 size_t znum
= find_zone(ADDR2PFN(phys
), pages
, 0);
155 if (znum
== (size_t) -1) {
157 * Frames not found in any zone
158 * -> assume it is a hardware device and allow mapping
159 * for privileged tasks.
161 irq_spinlock_unlock(&zones
.lock
, true);
169 if (zones
.info
[znum
].flags
& (ZONE_FIRMWARE
| ZONE_RESERVED
)) {
171 * Frames are part of firmware or reserved zone
172 * -> allow mapping for privileged tasks.
174 irq_spinlock_unlock(&zones
.lock
, true);
182 irq_spinlock_unlock(&zones
.lock
, true);
186 if (!as_area_create(TASK
->as
, flags
, FRAMES2SIZE(pages
),
187 AS_AREA_ATTR_NONE
, &phys_backend
, &backend_data
, virt
, bound
)) {
189 * The address space area was not created.
190 * We report it using ENOMEM.
194 mutex_unlock(&parea_lock
);
200 * Mapping is created on-demand during page fault.
204 parea
->mapped
= true;
205 mutex_unlock(&parea_lock
);
211 NO_TRACE
static int physmem_unmap(uintptr_t virt
)
215 return as_area_destroy(TASK
->as
, virt
);
218 /** Wrapper for SYS_PHYSMEM_MAP syscall.
220 * @param phys Physical base address to map
221 * @param pages Number of pages
222 * @param flags Flags of newly mapped pages
223 * @param virt_ptr Destination virtual address
224 * @param bound Lowest virtual address bound.
226 * @return 0 on success, otherwise it returns error code found in errno.h
229 sysarg_t
sys_physmem_map(uintptr_t phys
, size_t pages
, unsigned int flags
,
230 void *virt_ptr
, uintptr_t bound
)
233 int rc
= copy_from_uspace(&virt
, virt_ptr
, sizeof(virt
));
237 rc
= physmem_map(ALIGN_DOWN(phys
, FRAME_SIZE
), pages
, flags
, &virt
,
242 rc
= copy_to_uspace(virt_ptr
, &virt
, sizeof(virt
));
244 physmem_unmap((uintptr_t) virt
);
251 sysarg_t
sys_physmem_unmap(uintptr_t virt
)
253 return physmem_unmap(virt
);
256 /** Enable range of I/O space for task.
258 * @param id Task ID of the destination task.
259 * @param ioaddr Starting I/O address.
260 * @param size Size of the enabled I/O space.
262 * @return 0 on success, EPERM if the caller lacks permissions to use this
263 * syscall, ENOENT if there is no task matching the specified ID.
266 NO_TRACE
static int iospace_enable(task_id_t id
, uintptr_t ioaddr
, size_t size
)
269 * Make sure the caller is authorised to make this syscall.
271 perm_t perms
= perm_get(TASK
);
272 if (!(perms
& PERM_IO_MANAGER
))
275 irq_spinlock_lock(&tasks_lock
, true);
277 task_t
*task
= task_find_by_id(id
);
279 if ((!task
) || (!container_check(CONTAINER
, task
->container
))) {
281 * There is no task with the specified ID
282 * or the task belongs to a different security
285 irq_spinlock_unlock(&tasks_lock
, true);
289 /* Lock the task and release the lock protecting tasks_btree. */
290 irq_spinlock_exchange(&tasks_lock
, &task
->lock
);
291 int rc
= ddi_iospace_enable_arch(task
, ioaddr
, size
);
292 irq_spinlock_unlock(&task
->lock
, true);
297 /** Disable range of I/O space for task.
299 * @param id Task ID of the destination task.
300 * @param ioaddr Starting I/O address.
301 * @param size Size of the enabled I/O space.
303 * @return 0 on success, EPERM if the caller lacks permissions to use this
304 * syscall, ENOENT if there is no task matching the specified ID.
307 NO_TRACE
static int iospace_disable(task_id_t id
, uintptr_t ioaddr
, size_t size
)
310 * Make sure the caller is authorised to make this syscall.
312 perm_t perms
= perm_get(TASK
);
313 if (!(perms
& PERM_IO_MANAGER
))
316 irq_spinlock_lock(&tasks_lock
, true);
318 task_t
*task
= task_find_by_id(id
);
320 if ((!task
) || (!container_check(CONTAINER
, task
->container
))) {
322 * There is no task with the specified ID
323 * or the task belongs to a different security
326 irq_spinlock_unlock(&tasks_lock
, true);
330 /* Lock the task and release the lock protecting tasks_btree. */
331 irq_spinlock_exchange(&tasks_lock
, &task
->lock
);
332 int rc
= ddi_iospace_disable_arch(task
, ioaddr
, size
);
333 irq_spinlock_unlock(&task
->lock
, true);
338 /** Wrapper for SYS_ENABLE_IOSPACE syscall.
340 * @param uspace_io_arg User space address of DDI argument structure.
342 * @return 0 on success, otherwise it returns error code found in errno.h
345 sysarg_t
sys_iospace_enable(ddi_ioarg_t
*uspace_io_arg
)
348 int rc
= copy_from_uspace(&arg
, uspace_io_arg
, sizeof(ddi_ioarg_t
));
350 return (sysarg_t
) rc
;
352 return (sysarg_t
) iospace_enable((task_id_t
) arg
.task_id
,
353 (uintptr_t) arg
.ioaddr
, (size_t) arg
.size
);
356 sysarg_t
sys_iospace_disable(ddi_ioarg_t
*uspace_io_arg
)
359 int rc
= copy_from_uspace(&arg
, uspace_io_arg
, sizeof(ddi_ioarg_t
));
361 return (sysarg_t
) rc
;
363 return (sysarg_t
) iospace_disable((task_id_t
) arg
.task_id
,
364 (uintptr_t) arg
.ioaddr
, (size_t) arg
.size
);
367 NO_TRACE
static int dmamem_map(uintptr_t virt
, size_t size
, unsigned int map_flags
,
368 unsigned int flags
, uintptr_t *phys
)
372 // TODO: implement locking of non-anonymous mapping
373 return page_find_mapping(virt
, phys
);
376 NO_TRACE
static int dmamem_map_anonymous(size_t size
, uintptr_t constraint
,
377 unsigned int map_flags
, unsigned int flags
, uintptr_t *phys
,
378 uintptr_t *virt
, uintptr_t bound
)
382 size_t frames
= SIZE2FRAMES(size
);
383 *phys
= frame_alloc(frames
, FRAME_ATOMIC
, constraint
);
387 mem_backend_data_t backend_data
;
388 backend_data
.base
= *phys
;
389 backend_data
.frames
= frames
;
390 backend_data
.anonymous
= true;
392 if (!as_area_create(TASK
->as
, map_flags
, size
,
393 AS_AREA_ATTR_NONE
, &phys_backend
, &backend_data
, virt
, bound
)) {
394 frame_free(*phys
, frames
);
401 NO_TRACE
static int dmamem_unmap(uintptr_t virt
, size_t size
)
403 // TODO: implement unlocking & unmap
407 NO_TRACE
static int dmamem_unmap_anonymous(uintptr_t virt
)
409 return as_area_destroy(TASK
->as
, virt
);
412 sysarg_t
sys_dmamem_map(size_t size
, unsigned int map_flags
, unsigned int flags
,
413 void *phys_ptr
, void *virt_ptr
, uintptr_t bound
)
415 if ((flags
& DMAMEM_FLAGS_ANONYMOUS
) == 0) {
417 * Non-anonymous DMA mapping
421 int rc
= dmamem_map((uintptr_t) virt_ptr
, size
, map_flags
,
427 rc
= copy_to_uspace(phys_ptr
, &phys
, sizeof(phys
));
429 dmamem_unmap((uintptr_t) virt_ptr
, size
);
434 * Anonymous DMA mapping
437 uintptr_t constraint
;
438 int rc
= copy_from_uspace(&constraint
, phys_ptr
,
444 rc
= copy_from_uspace(&virt
, virt_ptr
, sizeof(virt
));
449 rc
= dmamem_map_anonymous(size
, constraint
, map_flags
, flags
,
450 &phys
, &virt
, bound
);
454 rc
= copy_to_uspace(phys_ptr
, &phys
, sizeof(phys
));
456 dmamem_unmap_anonymous((uintptr_t) virt
);
460 rc
= copy_to_uspace(virt_ptr
, &virt
, sizeof(virt
));
462 dmamem_unmap_anonymous((uintptr_t) virt
);
470 sysarg_t
sys_dmamem_unmap(uintptr_t virt
, size_t size
, unsigned int flags
)
472 if ((flags
& DMAMEM_FLAGS_ANONYMOUS
) == 0)
473 return dmamem_unmap(virt
, size
);
475 return dmamem_unmap_anonymous(virt
);