Rev 3343 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1178 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2006 Jakub Jermar |
1178 | 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 | */ |
||
1702 | cejka | 28 | |
1888 | jermar | 29 | /** @addtogroup genericddi |
1702 | cejka | 30 | * @{ |
31 | */ |
||
1248 | jermar | 32 | |
33 | /** |
||
1702 | cejka | 34 | * @file |
1248 | jermar | 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 | */ |
||
1178 | jermar | 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> |
||
1288 | jermar | 49 | #include <syscall/copy.h> |
2015 | jermar | 50 | #include <adt/btree.h> |
1178 | jermar | 51 | #include <arch.h> |
52 | #include <align.h> |
||
53 | #include <errno.h> |
||
3862 | rimsky | 54 | #include <arch/asm.h> |
1178 | jermar | 55 | |
2015 | jermar | 56 | /** This lock protects the parea_btree. */ |
57 | SPINLOCK_INITIALIZE(parea_lock); |
||
58 | |||
59 | /** B+tree with enabled physical memory areas. */ |
||
60 | static btree_t parea_btree; |
||
61 | |||
62 | /** Initialize DDI. */ |
||
63 | void ddi_init(void) |
||
64 | { |
||
65 | btree_create(&parea_btree); |
||
66 | } |
||
67 | |||
68 | /** Enable piece of physical memory for mapping by physmem_map(). |
||
69 | * |
||
70 | * @param parea Pointer to physical area structure. |
||
71 | * |
||
72 | * @todo This function doesn't check for overlaps. It depends on the kernel to |
||
73 | * create disjunct physical memory areas. |
||
74 | */ |
||
75 | void ddi_parea_register(parea_t *parea) |
||
76 | { |
||
77 | ipl_t ipl; |
||
78 | |||
79 | ipl = interrupts_disable(); |
||
80 | spinlock_lock(&parea_lock); |
||
81 | |||
82 | /* |
||
83 | * TODO: we should really check for overlaps here. |
||
84 | * However, we should be safe because the kernel is pretty sane and |
||
85 | * memory of different devices doesn't overlap. |
||
86 | */ |
||
87 | btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL); |
||
88 | |||
89 | spinlock_unlock(&parea_lock); |
||
90 | interrupts_restore(ipl); |
||
91 | } |
||
92 | |||
1494 | palkovsky | 93 | /** Map piece of physical memory into virtual address space of current task. |
1178 | jermar | 94 | * |
2015 | jermar | 95 | * @param pf Physical address of the starting frame. |
96 | * @param vp Virtual address of the starting page. |
||
1178 | jermar | 97 | * @param pages Number of pages to map. |
1429 | jermar | 98 | * @param flags Address space area flags for the mapping. |
1178 | jermar | 99 | * |
2015 | jermar | 100 | * @return 0 on success, EPERM if the caller lacks capabilities to use this |
101 | * syscall, ENOENT if there is no task matching the specified ID or the |
||
102 | * physical address space is not enabled for mapping and ENOMEM if there |
||
2141 | jermar | 103 | * was a problem in creating address space area. |
1178 | jermar | 104 | */ |
1780 | jermar | 105 | static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, count_t pages, int flags) |
1178 | jermar | 106 | { |
107 | ipl_t ipl; |
||
108 | cap_t caps; |
||
1425 | jermar | 109 | mem_backend_data_t backend_data; |
110 | |||
111 | backend_data.base = pf; |
||
112 | backend_data.frames = pages; |
||
1178 | jermar | 113 | |
114 | /* |
||
115 | * Make sure the caller is authorised to make this syscall. |
||
116 | */ |
||
117 | caps = cap_get(TASK); |
||
118 | if (!(caps & CAP_MEM_MANAGER)) |
||
119 | return EPERM; |
||
1494 | palkovsky | 120 | |
1178 | jermar | 121 | ipl = interrupts_disable(); |
2015 | jermar | 122 | |
123 | /* |
||
124 | * Check if the physical memory area is enabled for mapping. |
||
125 | * If the architecture supports virtually indexed caches, intercept |
||
126 | * attempts to create an illegal address alias. |
||
127 | */ |
||
128 | spinlock_lock(&parea_lock); |
||
129 | parea_t *parea; |
||
130 | btree_node_t *nodep; |
||
2116 | decky | 131 | parea = (parea_t *) btree_search(&parea_btree, (btree_key_t) pf, &nodep); |
2015 | jermar | 132 | if (!parea || parea->frames < pages || ((flags & AS_AREA_CACHEABLE) && |
2107 | jermar | 133 | !parea->cacheable) || (!(flags & AS_AREA_CACHEABLE) && |
134 | parea->cacheable)) { |
||
2015 | jermar | 135 | /* |
136 | * This physical memory area cannot be mapped. |
||
137 | */ |
||
138 | spinlock_unlock(&parea_lock); |
||
139 | interrupts_restore(ipl); |
||
140 | return ENOENT; |
||
141 | } |
||
142 | spinlock_unlock(&parea_lock); |
||
143 | |||
1494 | palkovsky | 144 | spinlock_lock(&TASK->lock); |
1178 | jermar | 145 | |
1494 | palkovsky | 146 | if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE, |
1424 | jermar | 147 | &phys_backend, &backend_data)) { |
1178 | jermar | 148 | /* |
149 | * The address space area could not have been created. |
||
150 | * We report it using ENOMEM. |
||
151 | */ |
||
1494 | palkovsky | 152 | spinlock_unlock(&TASK->lock); |
1178 | jermar | 153 | interrupts_restore(ipl); |
154 | return ENOMEM; |
||
155 | } |
||
156 | |||
1424 | jermar | 157 | /* |
158 | * Mapping is created on-demand during page fault. |
||
159 | */ |
||
160 | |||
1494 | palkovsky | 161 | spinlock_unlock(&TASK->lock); |
1178 | jermar | 162 | interrupts_restore(ipl); |
163 | return 0; |
||
164 | } |
||
165 | |||
1191 | jermar | 166 | /** Enable range of I/O space for task. |
167 | * |
||
168 | * @param id Task ID of the destination task. |
||
169 | * @param ioaddr Starting I/O address. |
||
170 | * @param size Size of the enabled I/O space.. |
||
171 | * |
||
2015 | jermar | 172 | * @return 0 on success, EPERM if the caller lacks capabilities to use this |
173 | * syscall, ENOENT if there is no task matching the specified ID. |
||
1191 | jermar | 174 | */ |
1780 | jermar | 175 | static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size) |
1191 | jermar | 176 | { |
177 | ipl_t ipl; |
||
178 | cap_t caps; |
||
179 | task_t *t; |
||
180 | int rc; |
||
181 | |||
182 | /* |
||
183 | * Make sure the caller is authorised to make this syscall. |
||
184 | */ |
||
185 | caps = cap_get(TASK); |
||
186 | if (!(caps & CAP_IO_MANAGER)) |
||
187 | return EPERM; |
||
188 | |||
189 | ipl = interrupts_disable(); |
||
190 | spinlock_lock(&tasks_lock); |
||
191 | |||
192 | t = task_find_by_id(id); |
||
193 | |||
1839 | decky | 194 | if ((!t) || (!context_check(CONTEXT, t->context))) { |
1191 | jermar | 195 | /* |
1839 | decky | 196 | * There is no task with the specified ID |
197 | * or the task belongs to a different security |
||
198 | * context. |
||
1191 | jermar | 199 | */ |
200 | spinlock_unlock(&tasks_lock); |
||
201 | interrupts_restore(ipl); |
||
202 | return ENOENT; |
||
203 | } |
||
204 | |||
205 | /* Lock the task and release the lock protecting tasks_btree. */ |
||
206 | spinlock_lock(&t->lock); |
||
207 | spinlock_unlock(&tasks_lock); |
||
208 | |||
1227 | jermar | 209 | rc = ddi_iospace_enable_arch(t, ioaddr, size); |
1191 | jermar | 210 | |
211 | spinlock_unlock(&t->lock); |
||
212 | interrupts_restore(ipl); |
||
213 | return rc; |
||
214 | } |
||
215 | |||
2012 | jermar | 216 | /** Wrapper for SYS_PHYSMEM_MAP syscall. |
1178 | jermar | 217 | * |
1494 | palkovsky | 218 | * @param phys_base Physical base address to map |
219 | * @param virt_base Destination virtual address |
||
220 | * @param pages Number of pages |
||
221 | * @param flags Flags of newly mapped pages |
||
1178 | jermar | 222 | * |
223 | * @return 0 on success, otherwise it returns error code found in errno.h |
||
224 | */ |
||
2107 | jermar | 225 | unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, |
226 | unative_t pages, unative_t flags) |
||
1178 | jermar | 227 | { |
2015 | jermar | 228 | return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base, |
2107 | jermar | 229 | FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE), |
230 | (count_t) pages, (int) flags); |
||
1178 | jermar | 231 | } |
1191 | jermar | 232 | |
233 | /** Wrapper for SYS_ENABLE_IOSPACE syscall. |
||
234 | * |
||
1708 | jermar | 235 | * @param uspace_io_arg User space address of DDI argument structure. |
1191 | jermar | 236 | * |
237 | * @return 0 on success, otherwise it returns error code found in errno.h |
||
238 | */ |
||
1780 | jermar | 239 | unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg) |
1191 | jermar | 240 | { |
241 | ddi_ioarg_t arg; |
||
1288 | jermar | 242 | int rc; |
1191 | jermar | 243 | |
1288 | jermar | 244 | rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t)); |
245 | if (rc != 0) |
||
1780 | jermar | 246 | return (unative_t) rc; |
1288 | jermar | 247 | |
2015 | jermar | 248 | return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id, |
2107 | jermar | 249 | (uintptr_t) arg.ioaddr, (size_t) arg.size); |
1191 | jermar | 250 | } |
1297 | jermar | 251 | |
252 | /** Disable or enable preemption. |
||
253 | * |
||
2015 | jermar | 254 | * @param enable If non-zero, the preemption counter will be decremented, |
255 | * leading to potential enabling of preemption. Otherwise the preemption |
||
256 | * counter will be incremented, preventing preemption from occurring. |
||
1297 | jermar | 257 | * |
258 | * @return Zero on success or EPERM if callers capabilities are not sufficient. |
||
259 | */ |
||
1780 | jermar | 260 | unative_t sys_preempt_control(int enable) |
1297 | jermar | 261 | { |
2107 | jermar | 262 | if (!cap_get(TASK) & CAP_PREEMPT_CONTROL) |
1297 | jermar | 263 | return EPERM; |
264 | if (enable) |
||
265 | preemption_enable(); |
||
266 | else |
||
267 | preemption_disable(); |
||
268 | return 0; |
||
269 | } |
||
1702 | cejka | 270 | |
1888 | jermar | 271 | /** @} |
1702 | cejka | 272 | */ |