[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);
|
---|
| 106 | ASSERT((pf % FRAME_SIZE) == 0);
|
---|
| 107 | ASSERT((vp % PAGE_SIZE) == 0);
|
---|
[9a8d91b] | 108 |
|
---|
| 109 | /*
|
---|
| 110 | * Make sure the caller is authorised to make this syscall.
|
---|
| 111 | */
|
---|
[e49e234] | 112 | cap_t caps = cap_get(TASK);
|
---|
[9a8d91b] | 113 | if (!(caps & CAP_MEM_MANAGER))
|
---|
| 114 | return EPERM;
|
---|
[ae318d3] | 115 |
|
---|
[e49e234] | 116 | mem_backend_data_t backend_data;
|
---|
| 117 | backend_data.base = pf;
|
---|
| 118 | backend_data.frames = pages;
|
---|
[ae318d3] | 119 |
|
---|
[e49e234] | 120 | /* Find the zone of the physical memory */
|
---|
[da1bafb] | 121 | irq_spinlock_lock(&zones.lock, true);
|
---|
[98000fb] | 122 | size_t znum = find_zone(ADDR2PFN(pf), pages, 0);
|
---|
[ae318d3] | 123 |
|
---|
[98000fb] | 124 | if (znum == (size_t) -1) {
|
---|
[e49e234] | 125 | /* Frames not found in any zones
|
---|
| 126 | * -> assume it is hardware device and allow mapping
|
---|
| 127 | */
|
---|
[da1bafb] | 128 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[e49e234] | 129 | goto map;
|
---|
[ae318d3] | 130 | }
|
---|
| 131 |
|
---|
[e49e234] | 132 | if (zones.info[znum].flags & ZONE_FIRMWARE) {
|
---|
| 133 | /* Frames are part of firmware */
|
---|
[da1bafb] | 134 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[e49e234] | 135 | goto map;
|
---|
| 136 | }
|
---|
[ae318d3] | 137 |
|
---|
[e49e234] | 138 | if (zone_flags_available(zones.info[znum].flags)) {
|
---|
[da1bafb] | 139 | /*
|
---|
| 140 | * Frames are part of physical memory, check if the memory
|
---|
[e49e234] | 141 | * region is enabled for mapping.
|
---|
[f8ddd17] | 142 | */
|
---|
[da1bafb] | 143 | irq_spinlock_unlock(&zones.lock, true);
|
---|
[e49e234] | 144 |
|
---|
[373acb4] | 145 | mutex_lock(&parea_lock);
|
---|
[e49e234] | 146 | btree_node_t *nodep;
|
---|
| 147 | parea_t *parea = (parea_t *) btree_search(&parea_btree,
|
---|
| 148 | (btree_key_t) pf, &nodep);
|
---|
| 149 |
|
---|
[f430f58] | 150 | if ((!parea) || (parea->frames < pages)) {
|
---|
[373acb4] | 151 | mutex_unlock(&parea_lock);
|
---|
[e49e234] | 152 | goto err;
|
---|
[f430f58] | 153 | }
|
---|
[e49e234] | 154 |
|
---|
[373acb4] | 155 | mutex_unlock(&parea_lock);
|
---|
[e49e234] | 156 | goto map;
|
---|
[f8ddd17] | 157 | }
|
---|
[ae318d3] | 158 |
|
---|
[da1bafb] | 159 | irq_spinlock_unlock(&zones.lock, true);
|
---|
| 160 |
|
---|
[f430f58] | 161 | err:
|
---|
[e49e234] | 162 | return ENOENT;
|
---|
| 163 |
|
---|
| 164 | map:
|
---|
| 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 | */
|
---|
| 171 | return ENOMEM;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[0ee077ee] | 174 | /*
|
---|
| 175 | * Mapping is created on-demand during page fault.
|
---|
| 176 | */
|
---|
[9a8d91b] | 177 | return 0;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[f52e54da] | 180 | /** Enable range of I/O space for task.
|
---|
| 181 | *
|
---|
| 182 | * @param id Task ID of the destination task.
|
---|
| 183 | * @param ioaddr Starting I/O address.
|
---|
| 184 | * @param size Size of the enabled I/O space..
|
---|
| 185 | *
|
---|
[f8ddd17] | 186 | * @return 0 on success, EPERM if the caller lacks capabilities to use this
|
---|
[e49e234] | 187 | * syscall, ENOENT if there is no task matching the specified ID.
|
---|
| 188 | *
|
---|
[f52e54da] | 189 | */
|
---|
[7a0359b] | 190 | NO_TRACE static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr,
|
---|
| 191 | size_t size)
|
---|
[f52e54da] | 192 | {
|
---|
| 193 | /*
|
---|
| 194 | * Make sure the caller is authorised to make this syscall.
|
---|
| 195 | */
|
---|
[e49e234] | 196 | cap_t caps = cap_get(TASK);
|
---|
[f52e54da] | 197 | if (!(caps & CAP_IO_MANAGER))
|
---|
| 198 | return EPERM;
|
---|
| 199 |
|
---|
[da1bafb] | 200 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[f52e54da] | 201 |
|
---|
[e49e234] | 202 | task_t *task = task_find_by_id(id);
|
---|
[f52e54da] | 203 |
|
---|
[e49e234] | 204 | if ((!task) || (!context_check(CONTEXT, task->context))) {
|
---|
[f52e54da] | 205 | /*
|
---|
[cfffb290] | 206 | * There is no task with the specified ID
|
---|
| 207 | * or the task belongs to a different security
|
---|
| 208 | * context.
|
---|
[f52e54da] | 209 | */
|
---|
[da1bafb] | 210 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[f52e54da] | 211 | return ENOENT;
|
---|
| 212 | }
|
---|
[e49e234] | 213 |
|
---|
[f52e54da] | 214 | /* Lock the task and release the lock protecting tasks_btree. */
|
---|
[da1bafb] | 215 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
---|
[f52e54da] | 216 |
|
---|
[e49e234] | 217 | int rc = ddi_iospace_enable_arch(task, ioaddr, size);
|
---|
| 218 |
|
---|
[da1bafb] | 219 | irq_spinlock_unlock(&task->lock, true);
|
---|
[e49e234] | 220 |
|
---|
[f52e54da] | 221 | return rc;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[5a8b2a2] | 224 | /** Wrapper for SYS_PHYSMEM_MAP syscall.
|
---|
[9a8d91b] | 225 | *
|
---|
[8da51ad] | 226 | * @param phys_base Physical base address to map
|
---|
| 227 | * @param virt_base Destination virtual address
|
---|
| 228 | * @param pages Number of pages
|
---|
| 229 | * @param flags Flags of newly mapped pages
|
---|
[9a8d91b] | 230 | *
|
---|
| 231 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
[e49e234] | 232 | *
|
---|
| 233 | */
|
---|
[f619ec11] | 234 | unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base,
|
---|
| 235 | unative_t pages, unative_t flags)
|
---|
[9a8d91b] | 236 | {
|
---|
[f8ddd17] | 237 | return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
|
---|
[f619ec11] | 238 | FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
|
---|
[98000fb] | 239 | (size_t) pages, (int) flags);
|
---|
[9a8d91b] | 240 | }
|
---|
[f52e54da] | 241 |
|
---|
| 242 | /** Wrapper for SYS_ENABLE_IOSPACE syscall.
|
---|
| 243 | *
|
---|
[abbc16e] | 244 | * @param uspace_io_arg User space address of DDI argument structure.
|
---|
[f52e54da] | 245 | *
|
---|
| 246 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
[e49e234] | 247 | *
|
---|
| 248 | */
|
---|
[7f1c620] | 249 | unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
|
---|
[f52e54da] | 250 | {
|
---|
| 251 | ddi_ioarg_t arg;
|
---|
[e49e234] | 252 | int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
---|
[e3c762cd] | 253 | if (rc != 0)
|
---|
[7f1c620] | 254 | return (unative_t) rc;
|
---|
[e49e234] | 255 |
|
---|
[f8ddd17] | 256 | return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id,
|
---|
[f619ec11] | 257 | (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
---|
[f52e54da] | 258 | }
|
---|
[2bb8648] | 259 |
|
---|
[953bc1ef] | 260 | /** Disable or enable specified interrupts.
|
---|
| 261 | *
|
---|
| 262 | * @param irq the interrupt to be enabled/disabled.
|
---|
| 263 | * @param enable if true enable the interrupt, disable otherwise.
|
---|
| 264 | *
|
---|
| 265 | * @retutn Zero on success, error code otherwise.
|
---|
| 266 | */
|
---|
| 267 | unative_t sys_interrupt_enable(int irq, int enable)
|
---|
[3f5b4e0d] | 268 | {
|
---|
| 269 | /* FIXME: this needs to be generic code, or better not be in kernel at all. */
|
---|
| 270 | #if 0
|
---|
[953bc1ef] | 271 | cap_t task_cap = cap_get(TASK);
|
---|
[04cb68f2] | 272 | if (!(task_cap & CAP_IRQ_REG))
|
---|
[953bc1ef] | 273 | return EPERM;
|
---|
| 274 |
|
---|
| 275 | if (irq < 0 || irq > 16) {
|
---|
| 276 | return EINVAL;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | uint16_t irq_mask = (uint16_t)(1 << irq);
|
---|
| 280 | if (enable) {
|
---|
| 281 | trap_virtual_enable_irqs(irq_mask);
|
---|
| 282 | } else {
|
---|
| 283 | trap_virtual_disable_irqs(irq_mask);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[3f5b4e0d] | 286 | #endif
|
---|
| 287 | return 0;
|
---|
[953bc1ef] | 288 | }
|
---|
| 289 |
|
---|
[06e1e95] | 290 | /** @}
|
---|
[b45c443] | 291 | */
|
---|