[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 <ddi/ddi_arg.h>
|
---|
| 44 | #include <proc/task.h>
|
---|
| 45 | #include <security/cap.h>
|
---|
| 46 | #include <mm/frame.h>
|
---|
| 47 | #include <mm/as.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>
|
---|
[9a8d91b] | 55 |
|
---|
[f8ddd17] | 56 | /** This lock protects the parea_btree. */
|
---|
[373acb4] | 57 | static mutex_t parea_lock;
|
---|
[f8ddd17] | 58 |
|
---|
[e49e234] | 59 | /** B+tree with enabled physical memory areas. */
|
---|
| 60 | static btree_t parea_btree;
|
---|
[ae318d3] | 61 |
|
---|
[da1bafb] | 62 | /** Initialize DDI.
|
---|
| 63 | *
|
---|
| 64 | */
|
---|
[f8ddd17] | 65 | void ddi_init(void)
|
---|
| 66 | {
|
---|
[e49e234] | 67 | btree_create(&parea_btree);
|
---|
[373acb4] | 68 | mutex_initialize(&parea_lock, MUTEX_PASSIVE);
|
---|
[f8ddd17] | 69 | }
|
---|
| 70 |
|
---|
| 71 | /** Enable piece of physical memory for mapping by physmem_map().
|
---|
| 72 | *
|
---|
| 73 | * @param parea Pointer to physical area structure.
|
---|
| 74 | *
|
---|
| 75 | */
|
---|
| 76 | void ddi_parea_register(parea_t *parea)
|
---|
| 77 | {
|
---|
[373acb4] | 78 | mutex_lock(&parea_lock);
|
---|
[f8ddd17] | 79 |
|
---|
| 80 | /*
|
---|
[e49e234] | 81 | * We don't check for overlaps here as the kernel is pretty sane.
|
---|
[f8ddd17] | 82 | */
|
---|
[e49e234] | 83 | btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
|
---|
[ae318d3] | 84 |
|
---|
[373acb4] | 85 | mutex_unlock(&parea_lock);
|
---|
[f8ddd17] | 86 | }
|
---|
| 87 |
|
---|
[8da51ad] | 88 | /** Map piece of physical memory into virtual address space of current task.
|
---|
[9a8d91b] | 89 | *
|
---|
[e49e234] | 90 | * @param pf Physical address of the starting frame.
|
---|
| 91 | * @param vp Virtual address of the starting page.
|
---|
[9a8d91b] | 92 | * @param pages Number of pages to map.
|
---|
[6212095] | 93 | * @param flags Address space area flags for the mapping.
|
---|
[9a8d91b] | 94 | *
|
---|
[f8ddd17] | 95 | * @return 0 on success, EPERM if the caller lacks capabilities to use this
|
---|
[e49e234] | 96 | * syscall, EBADMEM if pf or vf is not page aligned, ENOENT if there
|
---|
| 97 | * is no task matching the specified ID or the physical address space
|
---|
| 98 | * is not enabled for mapping and ENOMEM if there was a problem in
|
---|
| 99 | * creating address space area.
|
---|
| 100 | *
|
---|
[9a8d91b] | 101 | */
|
---|
[7a0359b] | 102 | NO_TRACE static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, size_t pages,
|
---|
[da1bafb] | 103 | unsigned int flags)
|
---|
[9a8d91b] | 104 | {
|
---|
[e49e234] | 105 | ASSERT(TASK);
|
---|
[d7533c7] | 106 |
|
---|
| 107 | if ((pf % FRAME_SIZE) != 0)
|
---|
| 108 | return EBADMEM;
|
---|
| 109 |
|
---|
| 110 | if ((vp % PAGE_SIZE) != 0)
|
---|
| 111 | return EBADMEM;
|
---|
[9a8d91b] | 112 |
|
---|
| 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;
|
---|
| 121 | backend_data.base = pf;
|
---|
| 122 | backend_data.frames = pages;
|
---|
[ae318d3] | 123 |
|
---|
[b366a6f4] | 124 | /*
|
---|
| 125 | * Check if the memory region is explicitly enabled
|
---|
| 126 | * for mapping by any parea structure.
|
---|
| 127 | */
|
---|
| 128 |
|
---|
| 129 | mutex_lock(&parea_lock);
|
---|
| 130 | btree_node_t *nodep;
|
---|
| 131 | parea_t *parea = (parea_t *) btree_search(&parea_btree,
|
---|
| 132 | (btree_key_t) pf, &nodep);
|
---|
| 133 |
|
---|
| 134 | if ((parea != NULL) && (parea->frames >= pages)) {
|
---|
| 135 | if ((!priv) && (!parea->unpriv)) {
|
---|
| 136 | mutex_unlock(&parea_lock);
|
---|
| 137 | return EPERM;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | goto map;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | parea = NULL;
|
---|
| 144 | mutex_unlock(&parea_lock);
|
---|
| 145 |
|
---|
| 146 | /*
|
---|
| 147 | * Check if the memory region is part of physical
|
---|
| 148 | * memory generally enabled for mapping.
|
---|
| 149 | */
|
---|
| 150 |
|
---|
[da1bafb] | 151 | irq_spinlock_lock(&zones.lock, true);
|
---|
[98000fb] | 152 | size_t znum = find_zone(ADDR2PFN(pf), pages, 0);
|
---|
[ae318d3] | 153 |
|
---|
[98000fb] | 154 | if (znum == (size_t) -1) {
|
---|
[d7533c7] | 155 | /*
|
---|
| 156 | * Frames not found in any zone
|
---|
| 157 | * -> assume it is a hardware device and allow mapping
|
---|
| 158 | * for privileged tasks.
|
---|
[e49e234] | 159 | */
|
---|
[da1bafb] | 160 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[d7533c7] | 161 |
|
---|
| 162 | if (!priv)
|
---|
| 163 | return EPERM;
|
---|
| 164 |
|
---|
[e49e234] | 165 | goto map;
|
---|
[ae318d3] | 166 | }
|
---|
| 167 |
|
---|
[e49e234] | 168 | if (zones.info[znum].flags & ZONE_FIRMWARE) {
|
---|
[d7533c7] | 169 | /*
|
---|
| 170 | * Frames are part of firmware
|
---|
| 171 | * -> allow mapping for privileged tasks.
|
---|
| 172 | */
|
---|
[da1bafb] | 173 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[d7533c7] | 174 |
|
---|
| 175 | if (!priv)
|
---|
| 176 | return EPERM;
|
---|
| 177 |
|
---|
[e49e234] | 178 | goto map;
|
---|
| 179 | }
|
---|
[ae318d3] | 180 |
|
---|
[da1bafb] | 181 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[e49e234] | 182 | return ENOENT;
|
---|
| 183 |
|
---|
| 184 | map:
|
---|
| 185 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp,
|
---|
| 186 | AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
|
---|
[9a8d91b] | 187 | /*
|
---|
[b366a6f4] | 188 | * The address space area was not created.
|
---|
[9a8d91b] | 189 | * We report it using ENOMEM.
|
---|
| 190 | */
|
---|
[b366a6f4] | 191 |
|
---|
| 192 | if (parea != NULL)
|
---|
| 193 | mutex_unlock(&parea_lock);
|
---|
| 194 |
|
---|
[9a8d91b] | 195 | return ENOMEM;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[0ee077ee] | 198 | /*
|
---|
| 199 | * Mapping is created on-demand during page fault.
|
---|
| 200 | */
|
---|
[b366a6f4] | 201 |
|
---|
| 202 | if (parea != NULL) {
|
---|
| 203 | parea->mapped = true;
|
---|
| 204 | mutex_unlock(&parea_lock);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | return EOK;
|
---|
[9a8d91b] | 208 | }
|
---|
| 209 |
|
---|
[f52e54da] | 210 | /** Enable range of I/O space for task.
|
---|
| 211 | *
|
---|
| 212 | * @param id Task ID of the destination task.
|
---|
| 213 | * @param ioaddr Starting I/O address.
|
---|
| 214 | * @param size Size of the enabled I/O space..
|
---|
| 215 | *
|
---|
[f8ddd17] | 216 | * @return 0 on success, EPERM if the caller lacks capabilities to use this
|
---|
[e49e234] | 217 | * syscall, ENOENT if there is no task matching the specified ID.
|
---|
| 218 | *
|
---|
[f52e54da] | 219 | */
|
---|
[7a0359b] | 220 | NO_TRACE static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr,
|
---|
| 221 | size_t size)
|
---|
[f52e54da] | 222 | {
|
---|
| 223 | /*
|
---|
| 224 | * Make sure the caller is authorised to make this syscall.
|
---|
| 225 | */
|
---|
[e49e234] | 226 | cap_t caps = cap_get(TASK);
|
---|
[f52e54da] | 227 | if (!(caps & CAP_IO_MANAGER))
|
---|
| 228 | return EPERM;
|
---|
| 229 |
|
---|
[da1bafb] | 230 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[f52e54da] | 231 |
|
---|
[e49e234] | 232 | task_t *task = task_find_by_id(id);
|
---|
[f52e54da] | 233 |
|
---|
[473d5d2] | 234 | if ((!task) || (!container_check(CONTAINER, task->container))) {
|
---|
[f52e54da] | 235 | /*
|
---|
[cfffb290] | 236 | * There is no task with the specified ID
|
---|
| 237 | * or the task belongs to a different security
|
---|
| 238 | * context.
|
---|
[f52e54da] | 239 | */
|
---|
[da1bafb] | 240 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[f52e54da] | 241 | return ENOENT;
|
---|
| 242 | }
|
---|
[e49e234] | 243 |
|
---|
[f52e54da] | 244 | /* Lock the task and release the lock protecting tasks_btree. */
|
---|
[da1bafb] | 245 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
---|
[f52e54da] | 246 |
|
---|
[e49e234] | 247 | int rc = ddi_iospace_enable_arch(task, ioaddr, size);
|
---|
| 248 |
|
---|
[da1bafb] | 249 | irq_spinlock_unlock(&task->lock, true);
|
---|
[e49e234] | 250 |
|
---|
[f52e54da] | 251 | return rc;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[5a8b2a2] | 254 | /** Wrapper for SYS_PHYSMEM_MAP syscall.
|
---|
[9a8d91b] | 255 | *
|
---|
[8da51ad] | 256 | * @param phys_base Physical base address to map
|
---|
| 257 | * @param virt_base Destination virtual address
|
---|
| 258 | * @param pages Number of pages
|
---|
| 259 | * @param flags Flags of newly mapped pages
|
---|
[9a8d91b] | 260 | *
|
---|
| 261 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
[e49e234] | 262 | *
|
---|
| 263 | */
|
---|
[96b02eb9] | 264 | sysarg_t sys_physmem_map(sysarg_t phys_base, sysarg_t virt_base,
|
---|
| 265 | sysarg_t pages, sysarg_t flags)
|
---|
[9a8d91b] | 266 | {
|
---|
[96b02eb9] | 267 | return (sysarg_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
|
---|
[f619ec11] | 268 | FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
|
---|
[98000fb] | 269 | (size_t) pages, (int) flags);
|
---|
[9a8d91b] | 270 | }
|
---|
[f52e54da] | 271 |
|
---|
| 272 | /** Wrapper for SYS_ENABLE_IOSPACE syscall.
|
---|
| 273 | *
|
---|
[abbc16e] | 274 | * @param uspace_io_arg User space address of DDI argument structure.
|
---|
[f52e54da] | 275 | *
|
---|
| 276 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
[e49e234] | 277 | *
|
---|
| 278 | */
|
---|
[96b02eb9] | 279 | sysarg_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
|
---|
[f52e54da] | 280 | {
|
---|
| 281 | ddi_ioarg_t arg;
|
---|
[e49e234] | 282 | int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
---|
[e3c762cd] | 283 | if (rc != 0)
|
---|
[96b02eb9] | 284 | return (sysarg_t) rc;
|
---|
[e49e234] | 285 |
|
---|
[96b02eb9] | 286 | return (sysarg_t) ddi_iospace_enable((task_id_t) arg.task_id,
|
---|
[f619ec11] | 287 | (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
---|
[f52e54da] | 288 | }
|
---|
[2bb8648] | 289 |
|
---|
[06e1e95] | 290 | /** @}
|
---|
[b45c443] | 291 | */
|
---|