[9a8d91b] | 1 | /*
|
---|
| 2 | * Copyright (C) 2006 Jakub Jermar
|
---|
| 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>
|
---|
[9a8d91b] | 50 | #include <arch.h>
|
---|
| 51 | #include <align.h>
|
---|
| 52 | #include <errno.h>
|
---|
| 53 |
|
---|
[8da51ad] | 54 | /** Map piece of physical memory into virtual address space of current task.
|
---|
[9a8d91b] | 55 | *
|
---|
| 56 | * @param pf Physical frame address of the starting frame.
|
---|
| 57 | * @param vp Virtual page address of the starting page.
|
---|
| 58 | * @param pages Number of pages to map.
|
---|
[6212095] | 59 | * @param flags Address space area flags for the mapping.
|
---|
[9a8d91b] | 60 | *
|
---|
| 61 | * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall,
|
---|
| 62 | * ENOENT if there is no task matching the specified ID and ENOMEM if
|
---|
| 63 | * there was a problem in creating address space area.
|
---|
| 64 | */
|
---|
[7f1c620] | 65 | static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, count_t pages, int flags)
|
---|
[9a8d91b] | 66 | {
|
---|
| 67 | ipl_t ipl;
|
---|
| 68 | cap_t caps;
|
---|
[127c957b] | 69 | mem_backend_data_t backend_data;
|
---|
| 70 |
|
---|
| 71 | backend_data.base = pf;
|
---|
| 72 | backend_data.frames = pages;
|
---|
[9a8d91b] | 73 |
|
---|
| 74 | /*
|
---|
| 75 | * Make sure the caller is authorised to make this syscall.
|
---|
| 76 | */
|
---|
| 77 | caps = cap_get(TASK);
|
---|
| 78 | if (!(caps & CAP_MEM_MANAGER))
|
---|
| 79 | return EPERM;
|
---|
| 80 |
|
---|
[8da51ad] | 81 | ipl = interrupts_disable();
|
---|
| 82 | spinlock_lock(&TASK->lock);
|
---|
[9a8d91b] | 83 |
|
---|
[8da51ad] | 84 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE,
|
---|
[0ee077ee] | 85 | &phys_backend, &backend_data)) {
|
---|
[9a8d91b] | 86 | /*
|
---|
| 87 | * The address space area could not have been created.
|
---|
| 88 | * We report it using ENOMEM.
|
---|
| 89 | */
|
---|
[8da51ad] | 90 | spinlock_unlock(&TASK->lock);
|
---|
[9a8d91b] | 91 | interrupts_restore(ipl);
|
---|
| 92 | return ENOMEM;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[0ee077ee] | 95 | /*
|
---|
| 96 | * Mapping is created on-demand during page fault.
|
---|
| 97 | */
|
---|
| 98 |
|
---|
[8da51ad] | 99 | spinlock_unlock(&TASK->lock);
|
---|
[9a8d91b] | 100 | interrupts_restore(ipl);
|
---|
| 101 | return 0;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[f52e54da] | 104 | /** Enable range of I/O space for task.
|
---|
| 105 | *
|
---|
| 106 | * @param id Task ID of the destination task.
|
---|
| 107 | * @param ioaddr Starting I/O address.
|
---|
| 108 | * @param size Size of the enabled I/O space..
|
---|
| 109 | *
|
---|
| 110 | * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall,
|
---|
| 111 | * ENOENT if there is no task matching the specified ID.
|
---|
| 112 | */
|
---|
[7f1c620] | 113 | static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
|
---|
[f52e54da] | 114 | {
|
---|
| 115 | ipl_t ipl;
|
---|
| 116 | cap_t caps;
|
---|
| 117 | task_t *t;
|
---|
| 118 | int rc;
|
---|
| 119 |
|
---|
| 120 | /*
|
---|
| 121 | * Make sure the caller is authorised to make this syscall.
|
---|
| 122 | */
|
---|
| 123 | caps = cap_get(TASK);
|
---|
| 124 | if (!(caps & CAP_IO_MANAGER))
|
---|
| 125 | return EPERM;
|
---|
| 126 |
|
---|
| 127 | ipl = interrupts_disable();
|
---|
| 128 | spinlock_lock(&tasks_lock);
|
---|
| 129 |
|
---|
| 130 | t = task_find_by_id(id);
|
---|
| 131 |
|
---|
[cfffb290] | 132 | if ((!t) || (!context_check(CONTEXT, t->context))) {
|
---|
[f52e54da] | 133 | /*
|
---|
[cfffb290] | 134 | * There is no task with the specified ID
|
---|
| 135 | * or the task belongs to a different security
|
---|
| 136 | * context.
|
---|
[f52e54da] | 137 | */
|
---|
| 138 | spinlock_unlock(&tasks_lock);
|
---|
| 139 | interrupts_restore(ipl);
|
---|
| 140 | return ENOENT;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /* Lock the task and release the lock protecting tasks_btree. */
|
---|
| 144 | spinlock_lock(&t->lock);
|
---|
| 145 | spinlock_unlock(&tasks_lock);
|
---|
| 146 |
|
---|
[24f3874] | 147 | rc = ddi_iospace_enable_arch(t, ioaddr, size);
|
---|
[f52e54da] | 148 |
|
---|
| 149 | spinlock_unlock(&t->lock);
|
---|
| 150 | interrupts_restore(ipl);
|
---|
| 151 | return rc;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[5a8b2a2] | 154 | /** Wrapper for SYS_PHYSMEM_MAP syscall.
|
---|
[9a8d91b] | 155 | *
|
---|
[8da51ad] | 156 | * @param phys_base Physical base address to map
|
---|
| 157 | * @param virt_base Destination virtual address
|
---|
| 158 | * @param pages Number of pages
|
---|
| 159 | * @param flags Flags of newly mapped pages
|
---|
[9a8d91b] | 160 | *
|
---|
| 161 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
| 162 | */
|
---|
[7f1c620] | 163 | unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, unative_t pages,
|
---|
| 164 | unative_t flags)
|
---|
[9a8d91b] | 165 | {
|
---|
[7f1c620] | 166 | return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base, FRAME_SIZE),
|
---|
| 167 | ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE), (count_t) pages,
|
---|
[8da51ad] | 168 | (int) flags);
|
---|
[9a8d91b] | 169 | }
|
---|
[f52e54da] | 170 |
|
---|
| 171 | /** Wrapper for SYS_ENABLE_IOSPACE syscall.
|
---|
| 172 | *
|
---|
[abbc16e] | 173 | * @param uspace_io_arg User space address of DDI argument structure.
|
---|
[f52e54da] | 174 | *
|
---|
| 175 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
| 176 | */
|
---|
[7f1c620] | 177 | unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
|
---|
[f52e54da] | 178 | {
|
---|
| 179 | ddi_ioarg_t arg;
|
---|
[e3c762cd] | 180 | int rc;
|
---|
[f52e54da] | 181 |
|
---|
[e3c762cd] | 182 | rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
---|
| 183 | if (rc != 0)
|
---|
[7f1c620] | 184 | return (unative_t) rc;
|
---|
[e3c762cd] | 185 |
|
---|
[7f1c620] | 186 | return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id, (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
---|
[f52e54da] | 187 | }
|
---|
[2bb8648] | 188 |
|
---|
| 189 | /** Disable or enable preemption.
|
---|
| 190 | *
|
---|
| 191 | * @param enable If non-zero, the preemption counter will be decremented, leading to potential
|
---|
| 192 | * enabling of preemption. Otherwise the preemption counter will be incremented,
|
---|
| 193 | * preventing preemption from occurring.
|
---|
| 194 | *
|
---|
| 195 | * @return Zero on success or EPERM if callers capabilities are not sufficient.
|
---|
| 196 | */
|
---|
[7f1c620] | 197 | unative_t sys_preempt_control(int enable)
|
---|
[2bb8648] | 198 | {
|
---|
| 199 | if (! cap_get(TASK) & CAP_PREEMPT_CONTROL)
|
---|
| 200 | return EPERM;
|
---|
| 201 | if (enable)
|
---|
| 202 | preemption_enable();
|
---|
| 203 | else
|
---|
| 204 | preemption_disable();
|
---|
| 205 | return 0;
|
---|
| 206 | }
|
---|
[b45c443] | 207 |
|
---|
[06e1e95] | 208 | /** @}
|
---|
[b45c443] | 209 | */
|
---|