| [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 |
|
|---|
| [da1bafb] | 61 | /** Initialize DDI.
|
|---|
| 62 | *
|
|---|
| 63 | */
|
|---|
| [f8ddd17] | 64 | void ddi_init(void)
|
|---|
| 65 | {
|
|---|
| [e49e234] | 66 | btree_create(&parea_btree);
|
|---|
| [373acb4] | 67 | mutex_initialize(&parea_lock, MUTEX_PASSIVE);
|
|---|
| [f8ddd17] | 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /** Enable piece of physical memory for mapping by physmem_map().
|
|---|
| 71 | *
|
|---|
| 72 | * @param parea Pointer to physical area structure.
|
|---|
| 73 | *
|
|---|
| 74 | */
|
|---|
| 75 | void ddi_parea_register(parea_t *parea)
|
|---|
| 76 | {
|
|---|
| [373acb4] | 77 | mutex_lock(&parea_lock);
|
|---|
| [f8ddd17] | 78 |
|
|---|
| 79 | /*
|
|---|
| [e49e234] | 80 | * We don't check for overlaps here as the kernel is pretty sane.
|
|---|
| [f8ddd17] | 81 | */
|
|---|
| [e49e234] | 82 | btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
|
|---|
| [ae318d3] | 83 |
|
|---|
| [373acb4] | 84 | mutex_unlock(&parea_lock);
|
|---|
| [f8ddd17] | 85 | }
|
|---|
| 86 |
|
|---|
| [8da51ad] | 87 | /** Map piece of physical memory into virtual address space of current task.
|
|---|
| [9a8d91b] | 88 | *
|
|---|
| [e49e234] | 89 | * @param pf Physical address of the starting frame.
|
|---|
| 90 | * @param vp Virtual address of the starting page.
|
|---|
| [9a8d91b] | 91 | * @param pages Number of pages to map.
|
|---|
| [6212095] | 92 | * @param flags Address space area flags for the mapping.
|
|---|
| [9a8d91b] | 93 | *
|
|---|
| [f8ddd17] | 94 | * @return 0 on success, EPERM if the caller lacks capabilities to use this
|
|---|
| [e49e234] | 95 | * syscall, EBADMEM if pf or vf is not page aligned, ENOENT if there
|
|---|
| 96 | * is no task matching the specified ID or the physical address space
|
|---|
| 97 | * is not enabled for mapping and ENOMEM if there was a problem in
|
|---|
| 98 | * creating address space area.
|
|---|
| 99 | *
|
|---|
| [9a8d91b] | 100 | */
|
|---|
| [da1bafb] | 101 | static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, size_t pages,
|
|---|
| 102 | unsigned int flags)
|
|---|
| [9a8d91b] | 103 | {
|
|---|
| [e49e234] | 104 | ASSERT(TASK);
|
|---|
| 105 | ASSERT((pf % FRAME_SIZE) == 0);
|
|---|
| 106 | ASSERT((vp % PAGE_SIZE) == 0);
|
|---|
| [9a8d91b] | 107 |
|
|---|
| 108 | /*
|
|---|
| 109 | * Make sure the caller is authorised to make this syscall.
|
|---|
| 110 | */
|
|---|
| [e49e234] | 111 | cap_t caps = cap_get(TASK);
|
|---|
| [9a8d91b] | 112 | if (!(caps & CAP_MEM_MANAGER))
|
|---|
| 113 | return EPERM;
|
|---|
| [ae318d3] | 114 |
|
|---|
| [e49e234] | 115 | mem_backend_data_t backend_data;
|
|---|
| 116 | backend_data.base = pf;
|
|---|
| 117 | backend_data.frames = pages;
|
|---|
| [ae318d3] | 118 |
|
|---|
| [e49e234] | 119 | /* Find the zone of the physical memory */
|
|---|
| [da1bafb] | 120 | irq_spinlock_lock(&zones.lock, true);
|
|---|
| [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 | */
|
|---|
| [da1bafb] | 127 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| [e49e234] | 128 | goto map;
|
|---|
| [ae318d3] | 129 | }
|
|---|
| 130 |
|
|---|
| [e49e234] | 131 | if (zones.info[znum].flags & ZONE_FIRMWARE) {
|
|---|
| 132 | /* Frames are part of firmware */
|
|---|
| [da1bafb] | 133 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| [e49e234] | 134 | goto map;
|
|---|
| 135 | }
|
|---|
| [ae318d3] | 136 |
|
|---|
| [e49e234] | 137 | if (zone_flags_available(zones.info[znum].flags)) {
|
|---|
| [da1bafb] | 138 | /*
|
|---|
| 139 | * Frames are part of physical memory, check if the memory
|
|---|
| [e49e234] | 140 | * region is enabled for mapping.
|
|---|
| [f8ddd17] | 141 | */
|
|---|
| [da1bafb] | 142 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| [e49e234] | 143 |
|
|---|
| [373acb4] | 144 | mutex_lock(&parea_lock);
|
|---|
| [e49e234] | 145 | btree_node_t *nodep;
|
|---|
| 146 | parea_t *parea = (parea_t *) btree_search(&parea_btree,
|
|---|
| 147 | (btree_key_t) pf, &nodep);
|
|---|
| 148 |
|
|---|
| [f430f58] | 149 | if ((!parea) || (parea->frames < pages)) {
|
|---|
| [373acb4] | 150 | mutex_unlock(&parea_lock);
|
|---|
| [e49e234] | 151 | goto err;
|
|---|
| [f430f58] | 152 | }
|
|---|
| [e49e234] | 153 |
|
|---|
| [373acb4] | 154 | mutex_unlock(&parea_lock);
|
|---|
| [e49e234] | 155 | goto map;
|
|---|
| [f8ddd17] | 156 | }
|
|---|
| [ae318d3] | 157 |
|
|---|
| [da1bafb] | 158 | irq_spinlock_unlock(&zones.lock, true);
|
|---|
| 159 |
|
|---|
| [f430f58] | 160 | err:
|
|---|
| [e49e234] | 161 | return ENOENT;
|
|---|
| 162 |
|
|---|
| 163 | map:
|
|---|
| 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 |
|
|---|
| [da1bafb] | 198 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| [f52e54da] | 199 |
|
|---|
| [e49e234] | 200 | task_t *task = task_find_by_id(id);
|
|---|
| [f52e54da] | 201 |
|
|---|
| [e49e234] | 202 | if ((!task) || (!context_check(CONTEXT, task->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 | */
|
|---|
| [da1bafb] | 208 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| [f52e54da] | 209 | return ENOENT;
|
|---|
| 210 | }
|
|---|
| [e49e234] | 211 |
|
|---|
| [f52e54da] | 212 | /* Lock the task and release the lock protecting tasks_btree. */
|
|---|
| [da1bafb] | 213 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
|---|
| [f52e54da] | 214 |
|
|---|
| [e49e234] | 215 | int rc = ddi_iospace_enable_arch(task, ioaddr, size);
|
|---|
| 216 |
|
|---|
| [da1bafb] | 217 | irq_spinlock_unlock(&task->lock, true);
|
|---|
| [e49e234] | 218 |
|
|---|
| [f52e54da] | 219 | return rc;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| [5a8b2a2] | 222 | /** Wrapper for SYS_PHYSMEM_MAP syscall.
|
|---|
| [9a8d91b] | 223 | *
|
|---|
| [8da51ad] | 224 | * @param phys_base Physical base address to map
|
|---|
| 225 | * @param virt_base Destination virtual address
|
|---|
| 226 | * @param pages Number of pages
|
|---|
| 227 | * @param flags Flags of newly mapped pages
|
|---|
| [9a8d91b] | 228 | *
|
|---|
| 229 | * @return 0 on success, otherwise it returns error code found in errno.h
|
|---|
| [e49e234] | 230 | *
|
|---|
| 231 | */
|
|---|
| [f619ec11] | 232 | unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base,
|
|---|
| 233 | unative_t pages, unative_t flags)
|
|---|
| [9a8d91b] | 234 | {
|
|---|
| [f8ddd17] | 235 | return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
|
|---|
| [f619ec11] | 236 | FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
|
|---|
| [98000fb] | 237 | (size_t) pages, (int) flags);
|
|---|
| [9a8d91b] | 238 | }
|
|---|
| [f52e54da] | 239 |
|
|---|
| 240 | /** Wrapper for SYS_ENABLE_IOSPACE syscall.
|
|---|
| 241 | *
|
|---|
| [abbc16e] | 242 | * @param uspace_io_arg User space address of DDI argument structure.
|
|---|
| [f52e54da] | 243 | *
|
|---|
| 244 | * @return 0 on success, otherwise it returns error code found in errno.h
|
|---|
| [e49e234] | 245 | *
|
|---|
| 246 | */
|
|---|
| [7f1c620] | 247 | unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
|
|---|
| [f52e54da] | 248 | {
|
|---|
| 249 | ddi_ioarg_t arg;
|
|---|
| [e49e234] | 250 | int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
|---|
| [e3c762cd] | 251 | if (rc != 0)
|
|---|
| [7f1c620] | 252 | return (unative_t) rc;
|
|---|
| [e49e234] | 253 |
|
|---|
| [f8ddd17] | 254 | return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id,
|
|---|
| [f619ec11] | 255 | (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
|---|
| [f52e54da] | 256 | }
|
|---|
| [2bb8648] | 257 |
|
|---|
| 258 | /** Disable or enable preemption.
|
|---|
| 259 | *
|
|---|
| [f8ddd17] | 260 | * @param enable If non-zero, the preemption counter will be decremented,
|
|---|
| [e49e234] | 261 | * leading to potential enabling of preemption. Otherwise
|
|---|
| 262 | * the preemption counter will be incremented, preventing
|
|---|
| 263 | * preemption from occurring.
|
|---|
| [2bb8648] | 264 | *
|
|---|
| 265 | * @return Zero on success or EPERM if callers capabilities are not sufficient.
|
|---|
| [e49e234] | 266 | *
|
|---|
| 267 | */
|
|---|
| [7f1c620] | 268 | unative_t sys_preempt_control(int enable)
|
|---|
| [2bb8648] | 269 | {
|
|---|
| [5418217] | 270 | if (!(cap_get(TASK) & CAP_PREEMPT_CONTROL))
|
|---|
| [ae318d3] | 271 | return EPERM;
|
|---|
| [e49e234] | 272 |
|
|---|
| [ae318d3] | 273 | if (enable)
|
|---|
| 274 | preemption_enable();
|
|---|
| 275 | else
|
|---|
| 276 | preemption_disable();
|
|---|
| [e49e234] | 277 |
|
|---|
| [ae318d3] | 278 | return 0;
|
|---|
| [2bb8648] | 279 | }
|
|---|
| [b45c443] | 280 |
|
|---|
| [06e1e95] | 281 | /** @}
|
|---|
| [b45c443] | 282 | */
|
|---|