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