[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 |
|
---|
[e49e234] | 124 | /* Find the zone of the physical memory */
|
---|
[da1bafb] | 125 | irq_spinlock_lock(&zones.lock, true);
|
---|
[98000fb] | 126 | size_t znum = find_zone(ADDR2PFN(pf), pages, 0);
|
---|
[ae318d3] | 127 |
|
---|
[98000fb] | 128 | if (znum == (size_t) -1) {
|
---|
[d7533c7] | 129 | /*
|
---|
| 130 | * Frames not found in any zone
|
---|
| 131 | * -> assume it is a hardware device and allow mapping
|
---|
| 132 | * for privileged tasks.
|
---|
[e49e234] | 133 | */
|
---|
[da1bafb] | 134 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[d7533c7] | 135 |
|
---|
| 136 | if (!priv)
|
---|
| 137 | return EPERM;
|
---|
| 138 |
|
---|
[e49e234] | 139 | goto map;
|
---|
[ae318d3] | 140 | }
|
---|
| 141 |
|
---|
[e49e234] | 142 | if (zones.info[znum].flags & ZONE_FIRMWARE) {
|
---|
[d7533c7] | 143 | /*
|
---|
| 144 | * Frames are part of firmware
|
---|
| 145 | * -> allow mapping for privileged tasks.
|
---|
| 146 | */
|
---|
[da1bafb] | 147 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[d7533c7] | 148 |
|
---|
| 149 | if (!priv)
|
---|
| 150 | return EPERM;
|
---|
| 151 |
|
---|
[e49e234] | 152 | goto map;
|
---|
| 153 | }
|
---|
[ae318d3] | 154 |
|
---|
[e49e234] | 155 | if (zone_flags_available(zones.info[znum].flags)) {
|
---|
[da1bafb] | 156 | /*
|
---|
[d7533c7] | 157 | * Frames are part of physical memory, check
|
---|
| 158 | * if the memory region is enabled for mapping.
|
---|
[f8ddd17] | 159 | */
|
---|
[da1bafb] | 160 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[e49e234] | 161 |
|
---|
[373acb4] | 162 | mutex_lock(&parea_lock);
|
---|
[e49e234] | 163 | btree_node_t *nodep;
|
---|
| 164 | parea_t *parea = (parea_t *) btree_search(&parea_btree,
|
---|
| 165 | (btree_key_t) pf, &nodep);
|
---|
| 166 |
|
---|
[f430f58] | 167 | if ((!parea) || (parea->frames < pages)) {
|
---|
[373acb4] | 168 | mutex_unlock(&parea_lock);
|
---|
[d7533c7] | 169 | return ENOENT;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | if (!priv) {
|
---|
| 173 | if (!parea->unpriv) {
|
---|
| 174 | mutex_unlock(&parea_lock);
|
---|
| 175 | return EPERM;
|
---|
| 176 | }
|
---|
[f430f58] | 177 | }
|
---|
[e49e234] | 178 |
|
---|
[373acb4] | 179 | mutex_unlock(&parea_lock);
|
---|
[e49e234] | 180 | goto map;
|
---|
[f8ddd17] | 181 | }
|
---|
[ae318d3] | 182 |
|
---|
[da1bafb] | 183 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[e49e234] | 184 | return ENOENT;
|
---|
| 185 |
|
---|
| 186 | map:
|
---|
| 187 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp,
|
---|
| 188 | AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
|
---|
[9a8d91b] | 189 | /*
|
---|
| 190 | * The address space area could not have been created.
|
---|
| 191 | * We report it using ENOMEM.
|
---|
| 192 | */
|
---|
| 193 | return ENOMEM;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[0ee077ee] | 196 | /*
|
---|
| 197 | * Mapping is created on-demand during page fault.
|
---|
| 198 | */
|
---|
[9a8d91b] | 199 | return 0;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[f52e54da] | 202 | /** Enable range of I/O space for task.
|
---|
| 203 | *
|
---|
| 204 | * @param id Task ID of the destination task.
|
---|
| 205 | * @param ioaddr Starting I/O address.
|
---|
| 206 | * @param size Size of the enabled I/O space..
|
---|
| 207 | *
|
---|
[f8ddd17] | 208 | * @return 0 on success, EPERM if the caller lacks capabilities to use this
|
---|
[e49e234] | 209 | * syscall, ENOENT if there is no task matching the specified ID.
|
---|
| 210 | *
|
---|
[f52e54da] | 211 | */
|
---|
[7a0359b] | 212 | NO_TRACE static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr,
|
---|
| 213 | size_t size)
|
---|
[f52e54da] | 214 | {
|
---|
| 215 | /*
|
---|
| 216 | * Make sure the caller is authorised to make this syscall.
|
---|
| 217 | */
|
---|
[e49e234] | 218 | cap_t caps = cap_get(TASK);
|
---|
[f52e54da] | 219 | if (!(caps & CAP_IO_MANAGER))
|
---|
| 220 | return EPERM;
|
---|
| 221 |
|
---|
[da1bafb] | 222 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[f52e54da] | 223 |
|
---|
[e49e234] | 224 | task_t *task = task_find_by_id(id);
|
---|
[f52e54da] | 225 |
|
---|
[473d5d2] | 226 | if ((!task) || (!container_check(CONTAINER, task->container))) {
|
---|
[f52e54da] | 227 | /*
|
---|
[cfffb290] | 228 | * There is no task with the specified ID
|
---|
| 229 | * or the task belongs to a different security
|
---|
| 230 | * context.
|
---|
[f52e54da] | 231 | */
|
---|
[da1bafb] | 232 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[f52e54da] | 233 | return ENOENT;
|
---|
| 234 | }
|
---|
[e49e234] | 235 |
|
---|
[f52e54da] | 236 | /* Lock the task and release the lock protecting tasks_btree. */
|
---|
[da1bafb] | 237 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
---|
[f52e54da] | 238 |
|
---|
[e49e234] | 239 | int rc = ddi_iospace_enable_arch(task, ioaddr, size);
|
---|
| 240 |
|
---|
[da1bafb] | 241 | irq_spinlock_unlock(&task->lock, true);
|
---|
[e49e234] | 242 |
|
---|
[f52e54da] | 243 | return rc;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[5a8b2a2] | 246 | /** Wrapper for SYS_PHYSMEM_MAP syscall.
|
---|
[9a8d91b] | 247 | *
|
---|
[8da51ad] | 248 | * @param phys_base Physical base address to map
|
---|
| 249 | * @param virt_base Destination virtual address
|
---|
| 250 | * @param pages Number of pages
|
---|
| 251 | * @param flags Flags of newly mapped pages
|
---|
[9a8d91b] | 252 | *
|
---|
| 253 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
[e49e234] | 254 | *
|
---|
| 255 | */
|
---|
[96b02eb9] | 256 | sysarg_t sys_physmem_map(sysarg_t phys_base, sysarg_t virt_base,
|
---|
| 257 | sysarg_t pages, sysarg_t flags)
|
---|
[9a8d91b] | 258 | {
|
---|
[96b02eb9] | 259 | return (sysarg_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
|
---|
[f619ec11] | 260 | FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
|
---|
[98000fb] | 261 | (size_t) pages, (int) flags);
|
---|
[9a8d91b] | 262 | }
|
---|
[f52e54da] | 263 |
|
---|
| 264 | /** Wrapper for SYS_ENABLE_IOSPACE syscall.
|
---|
| 265 | *
|
---|
[abbc16e] | 266 | * @param uspace_io_arg User space address of DDI argument structure.
|
---|
[f52e54da] | 267 | *
|
---|
| 268 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
[e49e234] | 269 | *
|
---|
| 270 | */
|
---|
[96b02eb9] | 271 | sysarg_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
|
---|
[f52e54da] | 272 | {
|
---|
| 273 | ddi_ioarg_t arg;
|
---|
[e49e234] | 274 | int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
---|
[e3c762cd] | 275 | if (rc != 0)
|
---|
[96b02eb9] | 276 | return (sysarg_t) rc;
|
---|
[e49e234] | 277 |
|
---|
[96b02eb9] | 278 | return (sysarg_t) ddi_iospace_enable((task_id_t) arg.task_id,
|
---|
[f619ec11] | 279 | (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
---|
[f52e54da] | 280 | }
|
---|
[2bb8648] | 281 |
|
---|
[06e1e95] | 282 | /** @}
|
---|
[b45c443] | 283 | */
|
---|