[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] | 59 | static mutex_t parea_lock;
|
---|
[f8ddd17] | 60 |
|
---|
[e49e234] | 61 | /** B+tree with enabled physical memory areas. */
|
---|
| 62 | static btree_t parea_btree;
|
---|
[ae318d3] | 63 |
|
---|
[da1bafb] | 64 | /** Initialize DDI.
|
---|
| 65 | *
|
---|
| 66 | */
|
---|
[f8ddd17] | 67 | void 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 | */
|
---|
| 78 | void 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 | */
|
---|
[b7fd2a0] | 106 | NO_TRACE static errno_t physmem_map(uintptr_t phys, size_t pages,
|
---|
[fbcdeb8] | 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 |
|
---|
| 186 | map:
|
---|
[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 |
|
---|
[b7fd2a0] | 212 | NO_TRACE static errno_t physmem_unmap(uintptr_t virt)
|
---|
[fbcdeb8] | 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 | */
|
---|
[b7fd2a0] | 230 | sys_errno_t sys_physmem_map(uintptr_t phys, size_t pages, unsigned int flags,
|
---|
[fbcdeb8] | 231 | void *virt_ptr, uintptr_t bound)
|
---|
| 232 | {
|
---|
[bf9cb2f] | 233 | uintptr_t virt;
|
---|
[b7fd2a0] | 234 | errno_t rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
|
---|
[bf9cb2f] | 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 |
|
---|
[b7fd2a0] | 252 | sys_errno_t sys_physmem_unmap(uintptr_t virt)
|
---|
[fbcdeb8] | 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 | */
|
---|
[b7fd2a0] | 267 | NO_TRACE static errno_t 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);
|
---|
[b7fd2a0] | 292 | errno_t 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 | */
|
---|
[b7fd2a0] | 308 | NO_TRACE static errno_t iospace_disable(task_id_t id, uintptr_t ioaddr, size_t size)
|
---|
[8cd680c] | 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);
|
---|
[b7fd2a0] | 333 | errno_t rc = ddi_iospace_disable_arch(task, ioaddr, size);
|
---|
[8cd680c] | 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 | */
|
---|
[b7fd2a0] | 346 | sys_errno_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
|
---|
[f52e54da] | 347 | {
|
---|
| 348 | ddi_ioarg_t arg;
|
---|
[b7fd2a0] | 349 | errno_t rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
---|
[a53ed3a] | 350 | if (rc != EOK)
|
---|
[b7fd2a0] | 351 | return (sys_errno_t) rc;
|
---|
[e49e234] | 352 |
|
---|
[b7fd2a0] | 353 | return (sys_errno_t) iospace_enable((task_id_t) arg.task_id,
|
---|
[f619ec11] | 354 | (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
---|
[f52e54da] | 355 | }
|
---|
[2bb8648] | 356 |
|
---|
[b7fd2a0] | 357 | sys_errno_t sys_iospace_disable(ddi_ioarg_t *uspace_io_arg)
|
---|
[fbcdeb8] | 358 | {
|
---|
[8cd680c] | 359 | ddi_ioarg_t arg;
|
---|
[b7fd2a0] | 360 | errno_t rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
---|
[a53ed3a] | 361 | if (rc != EOK)
|
---|
[b7fd2a0] | 362 | return (sys_errno_t) rc;
|
---|
[8cd680c] | 363 |
|
---|
[b7fd2a0] | 364 | return (sys_errno_t) iospace_disable((task_id_t) arg.task_id,
|
---|
[8cd680c] | 365 | (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
---|
[fbcdeb8] | 366 | }
|
---|
| 367 |
|
---|
[b7fd2a0] | 368 | NO_TRACE static errno_t 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 |
|
---|
[b7fd2a0] | 377 | NO_TRACE static errno_t dmamem_map_anonymous(size_t size, uintptr_t constraint,
|
---|
[b0c2075] | 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);
|
---|
[14741a0] | 384 | if (frames == 0)
|
---|
| 385 | return EINVAL;
|
---|
| 386 |
|
---|
[a17cced] | 387 | *phys = frame_alloc(frames, FRAME_ATOMIC, constraint);
|
---|
[8cbf1c3] | 388 | if (*phys == 0)
|
---|
[fbcdeb8] | 389 | return ENOMEM;
|
---|
| 390 |
|
---|
| 391 | mem_backend_data_t backend_data;
|
---|
[8cbf1c3] | 392 | backend_data.base = *phys;
|
---|
[e2a0d76] | 393 | backend_data.frames = frames;
|
---|
[c101dc0] | 394 | backend_data.anonymous = true;
|
---|
[fbcdeb8] | 395 |
|
---|
| 396 | if (!as_area_create(TASK->as, map_flags, size,
|
---|
| 397 | AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
|
---|
[a17cced] | 398 | frame_free(*phys, frames);
|
---|
[fbcdeb8] | 399 | return ENOMEM;
|
---|
[c6ae4c2] | 400 | }
|
---|
[fbcdeb8] | 401 |
|
---|
| 402 | return EOK;
|
---|
[c6ae4c2] | 403 | }
|
---|
| 404 |
|
---|
[b7fd2a0] | 405 | NO_TRACE static errno_t dmamem_unmap(uintptr_t virt, size_t size)
|
---|
[c6ae4c2] | 406 | {
|
---|
| 407 | // TODO: implement unlocking & unmap
|
---|
| 408 | return EOK;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
[b7fd2a0] | 411 | NO_TRACE static errno_t dmamem_unmap_anonymous(uintptr_t virt)
|
---|
[c6ae4c2] | 412 | {
|
---|
[c101dc0] | 413 | return as_area_destroy(TASK->as, virt);
|
---|
[fbcdeb8] | 414 | }
|
---|
| 415 |
|
---|
[b7fd2a0] | 416 | sys_errno_t sys_dmamem_map(size_t size, unsigned int map_flags, unsigned int flags,
|
---|
[fbcdeb8] | 417 | void *phys_ptr, void *virt_ptr, uintptr_t bound)
|
---|
| 418 | {
|
---|
| 419 | if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0) {
|
---|
| 420 | /*
|
---|
| 421 | * Non-anonymous DMA mapping
|
---|
| 422 | */
|
---|
| 423 |
|
---|
[8cbf1c3] | 424 | uintptr_t phys;
|
---|
[b7fd2a0] | 425 | errno_t rc = dmamem_map((uintptr_t) virt_ptr, size, map_flags,
|
---|
[fbcdeb8] | 426 | flags, &phys);
|
---|
| 427 |
|
---|
| 428 | if (rc != EOK)
|
---|
| 429 | return rc;
|
---|
| 430 |
|
---|
| 431 | rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
|
---|
| 432 | if (rc != EOK) {
|
---|
| 433 | dmamem_unmap((uintptr_t) virt_ptr, size);
|
---|
| 434 | return rc;
|
---|
| 435 | }
|
---|
| 436 | } else {
|
---|
| 437 | /*
|
---|
| 438 | * Anonymous DMA mapping
|
---|
| 439 | */
|
---|
| 440 |
|
---|
[b0c2075] | 441 | uintptr_t constraint;
|
---|
[b7fd2a0] | 442 | errno_t rc = copy_from_uspace(&constraint, phys_ptr,
|
---|
[b0c2075] | 443 | sizeof(constraint));
|
---|
| 444 | if (rc != EOK)
|
---|
| 445 | return rc;
|
---|
| 446 |
|
---|
[bf9cb2f] | 447 | uintptr_t virt;
|
---|
| 448 | rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
|
---|
| 449 | if (rc != EOK)
|
---|
| 450 | return rc;
|
---|
| 451 |
|
---|
[8cbf1c3] | 452 | uintptr_t phys;
|
---|
[b0c2075] | 453 | rc = dmamem_map_anonymous(size, constraint, map_flags, flags,
|
---|
[fbcdeb8] | 454 | &phys, &virt, bound);
|
---|
| 455 | if (rc != EOK)
|
---|
| 456 | return rc;
|
---|
| 457 |
|
---|
| 458 | rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
|
---|
| 459 | if (rc != EOK) {
|
---|
| 460 | dmamem_unmap_anonymous((uintptr_t) virt);
|
---|
| 461 | return rc;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
|
---|
| 465 | if (rc != EOK) {
|
---|
| 466 | dmamem_unmap_anonymous((uintptr_t) virt);
|
---|
| 467 | return rc;
|
---|
| 468 | }
|
---|
[c6ae4c2] | 469 | }
|
---|
| 470 |
|
---|
| 471 | return EOK;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[b7fd2a0] | 474 | sys_errno_t sys_dmamem_unmap(uintptr_t virt, size_t size, unsigned int flags)
|
---|
[c6ae4c2] | 475 | {
|
---|
[fbcdeb8] | 476 | if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0)
|
---|
| 477 | return dmamem_unmap(virt, size);
|
---|
| 478 | else
|
---|
| 479 | return dmamem_unmap_anonymous(virt);
|
---|
[c6ae4c2] | 480 | }
|
---|
| 481 |
|
---|
[06e1e95] | 482 | /** @}
|
---|
[b45c443] | 483 | */
|
---|