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