[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 | */
|
---|
[9179d0a] | 32 |
|
---|
| 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[9179d0a] | 35 | * @brief Device Driver Interface functions.
|
---|
| 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>
|
---|
[ae318d3] | 50 | #include <adt/list.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 |
|
---|
[ae318d3] | 58 | /** List with enabled physical memory areas. */
|
---|
| 59 | static LIST_INITIALIZE(parea_head);
|
---|
| 60 |
|
---|
| 61 | /** Physical memory area for devices. */
|
---|
| 62 | static parea_t dev_area;
|
---|
[f8ddd17] | 63 |
|
---|
| 64 | /** Initialize DDI. */
|
---|
| 65 | void ddi_init(void)
|
---|
| 66 | {
|
---|
[ae318d3] | 67 | hw_area(&dev_area.pbase, &dev_area.frames);
|
---|
| 68 | ddi_parea_register(&dev_area);
|
---|
[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 | * @todo This function doesn't check for overlaps. It depends on the kernel to
|
---|
| 76 | * create disjunct physical memory areas.
|
---|
| 77 | */
|
---|
| 78 | void ddi_parea_register(parea_t *parea)
|
---|
| 79 | {
|
---|
| 80 | ipl_t ipl;
|
---|
[ae318d3] | 81 |
|
---|
[f8ddd17] | 82 | ipl = interrupts_disable();
|
---|
| 83 | spinlock_lock(&parea_lock);
|
---|
| 84 |
|
---|
| 85 | /*
|
---|
| 86 | * TODO: we should really check for overlaps here.
|
---|
[ae318d3] | 87 | * However, we should be safe because the kernel is pretty sane.
|
---|
[f8ddd17] | 88 | */
|
---|
[ae318d3] | 89 | link_initialize(&parea->link);
|
---|
| 90 | list_append(&parea->link, &parea_head);
|
---|
| 91 |
|
---|
[f8ddd17] | 92 | spinlock_unlock(&parea_lock);
|
---|
[ae318d3] | 93 | interrupts_restore(ipl);
|
---|
[f8ddd17] | 94 | }
|
---|
| 95 |
|
---|
[8da51ad] | 96 | /** Map piece of physical memory into virtual address space of current task.
|
---|
[9a8d91b] | 97 | *
|
---|
[f8ddd17] | 98 | * @param pf Physical address of the starting frame.
|
---|
| 99 | * @param vp Virtual address of the starting page.
|
---|
[9a8d91b] | 100 | * @param pages Number of pages to map.
|
---|
[6212095] | 101 | * @param flags Address space area flags for the mapping.
|
---|
[9a8d91b] | 102 | *
|
---|
[f8ddd17] | 103 | * @return 0 on success, EPERM if the caller lacks capabilities to use this
|
---|
[ae318d3] | 104 | * syscall, ENOENT if there is no task matching the specified ID or the
|
---|
| 105 | * physical address space is not enabled for mapping and ENOMEM if there
|
---|
| 106 | * was a problem in creating address space area.
|
---|
[9a8d91b] | 107 | */
|
---|
[ae318d3] | 108 | static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, pfn_t pages, int flags)
|
---|
[9a8d91b] | 109 | {
|
---|
| 110 | ipl_t ipl;
|
---|
| 111 | cap_t caps;
|
---|
[127c957b] | 112 | mem_backend_data_t backend_data;
|
---|
[ae318d3] | 113 |
|
---|
[127c957b] | 114 | backend_data.base = pf;
|
---|
| 115 | backend_data.frames = pages;
|
---|
[9a8d91b] | 116 |
|
---|
| 117 | /*
|
---|
| 118 | * Make sure the caller is authorised to make this syscall.
|
---|
| 119 | */
|
---|
| 120 | caps = cap_get(TASK);
|
---|
| 121 | if (!(caps & CAP_MEM_MANAGER))
|
---|
| 122 | return EPERM;
|
---|
[ae318d3] | 123 |
|
---|
[8da51ad] | 124 | ipl = interrupts_disable();
|
---|
[ae318d3] | 125 |
|
---|
[f8ddd17] | 126 | /*
|
---|
| 127 | * Check if the physical memory area is enabled for mapping.
|
---|
| 128 | */
|
---|
| 129 | spinlock_lock(&parea_lock);
|
---|
[ae318d3] | 130 |
|
---|
| 131 | bool fnd = false;
|
---|
| 132 | link_t *cur;
|
---|
| 133 |
|
---|
| 134 | for (cur = parea_head.next; cur != &parea_head; cur = cur->next) {
|
---|
| 135 | parea_t *parea = list_get_instance(cur, parea_t, link);
|
---|
| 136 | if ((parea->pbase <= pf) && (ADDR2PFN(pf - parea->pbase) + pages <= parea->frames)) {
|
---|
| 137 | fnd = true;
|
---|
| 138 | break;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | spinlock_unlock(&parea_lock);
|
---|
| 143 |
|
---|
| 144 | if (!fnd) {
|
---|
[f8ddd17] | 145 | /*
|
---|
[ae318d3] | 146 | * Physical memory area cannot be mapped.
|
---|
[f8ddd17] | 147 | */
|
---|
| 148 | interrupts_restore(ipl);
|
---|
| 149 | return ENOENT;
|
---|
| 150 | }
|
---|
[ae318d3] | 151 |
|
---|
[8da51ad] | 152 | spinlock_lock(&TASK->lock);
|
---|
[9a8d91b] | 153 |
|
---|
[8da51ad] | 154 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE,
|
---|
[0ee077ee] | 155 | &phys_backend, &backend_data)) {
|
---|
[9a8d91b] | 156 | /*
|
---|
| 157 | * The address space area could not have been created.
|
---|
| 158 | * We report it using ENOMEM.
|
---|
| 159 | */
|
---|
[8da51ad] | 160 | spinlock_unlock(&TASK->lock);
|
---|
[9a8d91b] | 161 | interrupts_restore(ipl);
|
---|
| 162 | return ENOMEM;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[0ee077ee] | 165 | /*
|
---|
| 166 | * Mapping is created on-demand during page fault.
|
---|
| 167 | */
|
---|
| 168 |
|
---|
[8da51ad] | 169 | spinlock_unlock(&TASK->lock);
|
---|
[9a8d91b] | 170 | interrupts_restore(ipl);
|
---|
| 171 | return 0;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[f52e54da] | 174 | /** Enable range of I/O space for task.
|
---|
| 175 | *
|
---|
| 176 | * @param id Task ID of the destination task.
|
---|
| 177 | * @param ioaddr Starting I/O address.
|
---|
| 178 | * @param size Size of the enabled I/O space..
|
---|
| 179 | *
|
---|
[f8ddd17] | 180 | * @return 0 on success, EPERM if the caller lacks capabilities to use this
|
---|
| 181 | * syscall, ENOENT if there is no task matching the specified ID.
|
---|
[f52e54da] | 182 | */
|
---|
[7f1c620] | 183 | static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
|
---|
[f52e54da] | 184 | {
|
---|
| 185 | ipl_t ipl;
|
---|
| 186 | cap_t caps;
|
---|
| 187 | task_t *t;
|
---|
| 188 | int rc;
|
---|
| 189 |
|
---|
| 190 | /*
|
---|
| 191 | * Make sure the caller is authorised to make this syscall.
|
---|
| 192 | */
|
---|
| 193 | caps = cap_get(TASK);
|
---|
| 194 | if (!(caps & CAP_IO_MANAGER))
|
---|
| 195 | return EPERM;
|
---|
| 196 |
|
---|
| 197 | ipl = interrupts_disable();
|
---|
| 198 | spinlock_lock(&tasks_lock);
|
---|
| 199 |
|
---|
| 200 | t = task_find_by_id(id);
|
---|
| 201 |
|
---|
[cfffb290] | 202 | if ((!t) || (!context_check(CONTEXT, t->context))) {
|
---|
[f52e54da] | 203 | /*
|
---|
[cfffb290] | 204 | * There is no task with the specified ID
|
---|
| 205 | * or the task belongs to a different security
|
---|
| 206 | * context.
|
---|
[f52e54da] | 207 | */
|
---|
| 208 | spinlock_unlock(&tasks_lock);
|
---|
| 209 | interrupts_restore(ipl);
|
---|
| 210 | return ENOENT;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | /* Lock the task and release the lock protecting tasks_btree. */
|
---|
| 214 | spinlock_lock(&t->lock);
|
---|
| 215 | spinlock_unlock(&tasks_lock);
|
---|
| 216 |
|
---|
[24f3874] | 217 | rc = ddi_iospace_enable_arch(t, ioaddr, size);
|
---|
[f52e54da] | 218 |
|
---|
| 219 | spinlock_unlock(&t->lock);
|
---|
| 220 | interrupts_restore(ipl);
|
---|
| 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
|
---|
| 232 | */
|
---|
[f619ec11] | 233 | unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base,
|
---|
| 234 | unative_t pages, unative_t flags)
|
---|
[9a8d91b] | 235 | {
|
---|
[f8ddd17] | 236 | return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
|
---|
[f619ec11] | 237 | FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
|
---|
[ae318d3] | 238 | (pfn_t) pages, (int) flags);
|
---|
[9a8d91b] | 239 | }
|
---|
[f52e54da] | 240 |
|
---|
| 241 | /** Wrapper for SYS_ENABLE_IOSPACE syscall.
|
---|
| 242 | *
|
---|
[abbc16e] | 243 | * @param uspace_io_arg User space address of DDI argument structure.
|
---|
[f52e54da] | 244 | *
|
---|
| 245 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
| 246 | */
|
---|
[7f1c620] | 247 | unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
|
---|
[f52e54da] | 248 | {
|
---|
| 249 | ddi_ioarg_t arg;
|
---|
[e3c762cd] | 250 | int rc;
|
---|
[f52e54da] | 251 |
|
---|
[e3c762cd] | 252 | rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
---|
| 253 | if (rc != 0)
|
---|
[7f1c620] | 254 | return (unative_t) rc;
|
---|
[e3c762cd] | 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 |
|
---|
| 260 | /** Disable or enable preemption.
|
---|
| 261 | *
|
---|
[f8ddd17] | 262 | * @param enable If non-zero, the preemption counter will be decremented,
|
---|
| 263 | * leading to potential enabling of preemption. Otherwise the preemption
|
---|
| 264 | * counter will be incremented, preventing preemption from occurring.
|
---|
[2bb8648] | 265 | *
|
---|
| 266 | * @return Zero on success or EPERM if callers capabilities are not sufficient.
|
---|
| 267 | */
|
---|
[7f1c620] | 268 | unative_t sys_preempt_control(int enable)
|
---|
[2bb8648] | 269 | {
|
---|
[ae318d3] | 270 | if (!cap_get(TASK) & CAP_PREEMPT_CONTROL)
|
---|
| 271 | return EPERM;
|
---|
| 272 | if (enable)
|
---|
| 273 | preemption_enable();
|
---|
| 274 | else
|
---|
| 275 | preemption_disable();
|
---|
| 276 | return 0;
|
---|
[2bb8648] | 277 | }
|
---|
[b45c443] | 278 |
|
---|
[06e1e95] | 279 | /** @}
|
---|
[b45c443] | 280 | */
|
---|