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 | */
|
---|
28 |
|
---|
29 | /** @addtogroup genericddi
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
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 | */
|
---|
41 |
|
---|
42 | #include <ddi/ddi.h>
|
---|
43 | #include <proc/task.h>
|
---|
44 | #include <security/cap.h>
|
---|
45 | #include <mm/frame.h>
|
---|
46 | #include <mm/as.h>
|
---|
47 | #include <mm/page.h>
|
---|
48 | #include <synch/mutex.h>
|
---|
49 | #include <syscall/copy.h>
|
---|
50 | #include <adt/btree.h>
|
---|
51 | #include <arch.h>
|
---|
52 | #include <align.h>
|
---|
53 | #include <errno.h>
|
---|
54 | #include <trace.h>
|
---|
55 | #include <bitops.h>
|
---|
56 |
|
---|
57 | /** This lock protects the parea_btree. */
|
---|
58 | static mutex_t parea_lock;
|
---|
59 |
|
---|
60 | /** B+tree with enabled physical memory areas. */
|
---|
61 | static btree_t parea_btree;
|
---|
62 |
|
---|
63 | /** Initialize DDI.
|
---|
64 | *
|
---|
65 | */
|
---|
66 | void ddi_init(void)
|
---|
67 | {
|
---|
68 | btree_create(&parea_btree);
|
---|
69 | mutex_initialize(&parea_lock, MUTEX_PASSIVE);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Enable piece of physical memory for mapping by physmem_map().
|
---|
73 | *
|
---|
74 | * @param parea Pointer to physical area structure.
|
---|
75 | *
|
---|
76 | */
|
---|
77 | void ddi_parea_register(parea_t *parea)
|
---|
78 | {
|
---|
79 | mutex_lock(&parea_lock);
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * We don't check for overlaps here as the kernel is pretty sane.
|
---|
83 | */
|
---|
84 | btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
|
---|
85 |
|
---|
86 | mutex_unlock(&parea_lock);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Map piece of physical memory into virtual address space of current task.
|
---|
90 | *
|
---|
91 | * @param phys Physical address of the starting frame.
|
---|
92 | * @param virt Virtual address of the starting page.
|
---|
93 | * @param pages Number of pages to map.
|
---|
94 | * @param flags Address space area flags for the mapping.
|
---|
95 | *
|
---|
96 | * @return EOK on success.
|
---|
97 | * @return EPERM if the caller lacks capabilities to use this syscall.
|
---|
98 | * @return EBADMEM if phys or virt is not page aligned.
|
---|
99 | * @return ENOENT if there is no task matching the specified ID or
|
---|
100 | * the physical address space is not enabled for mapping.
|
---|
101 | * @return ENOMEM if there was a problem in creating address space area.
|
---|
102 | *
|
---|
103 | */
|
---|
104 | NO_TRACE static int ddi_physmem_map(uintptr_t phys, uintptr_t virt, size_t pages,
|
---|
105 | unsigned int flags)
|
---|
106 | {
|
---|
107 | ASSERT(TASK);
|
---|
108 |
|
---|
109 | if ((phys % FRAME_SIZE) != 0)
|
---|
110 | return EBADMEM;
|
---|
111 |
|
---|
112 | if ((virt % PAGE_SIZE) != 0)
|
---|
113 | return EBADMEM;
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * Unprivileged tasks are only allowed to map pareas
|
---|
117 | * which are explicitly marked as such.
|
---|
118 | */
|
---|
119 | bool priv =
|
---|
120 | ((cap_get(TASK) & CAP_MEM_MANAGER) == CAP_MEM_MANAGER);
|
---|
121 |
|
---|
122 | mem_backend_data_t backend_data;
|
---|
123 | backend_data.base = phys;
|
---|
124 | backend_data.frames = pages;
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Check if the memory region is explicitly enabled
|
---|
128 | * for mapping by any parea structure.
|
---|
129 | */
|
---|
130 |
|
---|
131 | mutex_lock(&parea_lock);
|
---|
132 | btree_node_t *nodep;
|
---|
133 | parea_t *parea = (parea_t *) btree_search(&parea_btree,
|
---|
134 | (btree_key_t) phys, &nodep);
|
---|
135 |
|
---|
136 | if ((parea != NULL) && (parea->frames >= pages)) {
|
---|
137 | if ((!priv) && (!parea->unpriv)) {
|
---|
138 | mutex_unlock(&parea_lock);
|
---|
139 | return EPERM;
|
---|
140 | }
|
---|
141 |
|
---|
142 | goto map;
|
---|
143 | }
|
---|
144 |
|
---|
145 | parea = NULL;
|
---|
146 | mutex_unlock(&parea_lock);
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Check if the memory region is part of physical
|
---|
150 | * memory generally enabled for mapping.
|
---|
151 | */
|
---|
152 |
|
---|
153 | irq_spinlock_lock(&zones.lock, true);
|
---|
154 | size_t znum = find_zone(ADDR2PFN(phys), pages, 0);
|
---|
155 |
|
---|
156 | if (znum == (size_t) -1) {
|
---|
157 | /*
|
---|
158 | * Frames not found in any zone
|
---|
159 | * -> assume it is a hardware device and allow mapping
|
---|
160 | * for privileged tasks.
|
---|
161 | */
|
---|
162 | irq_spinlock_unlock(&zones.lock, true);
|
---|
163 |
|
---|
164 | if (!priv)
|
---|
165 | return EPERM;
|
---|
166 |
|
---|
167 | goto map;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (zones.info[znum].flags & ZONE_FIRMWARE) {
|
---|
171 | /*
|
---|
172 | * Frames are part of firmware
|
---|
173 | * -> allow mapping for privileged tasks.
|
---|
174 | */
|
---|
175 | irq_spinlock_unlock(&zones.lock, true);
|
---|
176 |
|
---|
177 | if (!priv)
|
---|
178 | return EPERM;
|
---|
179 |
|
---|
180 | goto map;
|
---|
181 | }
|
---|
182 |
|
---|
183 | irq_spinlock_unlock(&zones.lock, true);
|
---|
184 | return ENOENT;
|
---|
185 |
|
---|
186 | map:
|
---|
187 | if (!as_area_create(TASK->as, flags, FRAMES2SIZE(pages), virt,
|
---|
188 | AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
|
---|
189 | /*
|
---|
190 | * The address space area was not created.
|
---|
191 | * We report it using ENOMEM.
|
---|
192 | */
|
---|
193 |
|
---|
194 | if (parea != NULL)
|
---|
195 | mutex_unlock(&parea_lock);
|
---|
196 |
|
---|
197 | return ENOMEM;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * Mapping is created on-demand during page fault.
|
---|
202 | */
|
---|
203 |
|
---|
204 | if (parea != NULL) {
|
---|
205 | parea->mapped = true;
|
---|
206 | mutex_unlock(&parea_lock);
|
---|
207 | }
|
---|
208 |
|
---|
209 | return EOK;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** Enable range of I/O space for task.
|
---|
213 | *
|
---|
214 | * @param id Task ID of the destination task.
|
---|
215 | * @param ioaddr Starting I/O address.
|
---|
216 | * @param size Size of the enabled I/O space..
|
---|
217 | *
|
---|
218 | * @return 0 on success, EPERM if the caller lacks capabilities to use this
|
---|
219 | * syscall, ENOENT if there is no task matching the specified ID.
|
---|
220 | *
|
---|
221 | */
|
---|
222 | NO_TRACE static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr,
|
---|
223 | size_t size)
|
---|
224 | {
|
---|
225 | /*
|
---|
226 | * Make sure the caller is authorised to make this syscall.
|
---|
227 | */
|
---|
228 | cap_t caps = cap_get(TASK);
|
---|
229 | if (!(caps & CAP_IO_MANAGER))
|
---|
230 | return EPERM;
|
---|
231 |
|
---|
232 | irq_spinlock_lock(&tasks_lock, true);
|
---|
233 |
|
---|
234 | task_t *task = task_find_by_id(id);
|
---|
235 |
|
---|
236 | if ((!task) || (!container_check(CONTAINER, task->container))) {
|
---|
237 | /*
|
---|
238 | * There is no task with the specified ID
|
---|
239 | * or the task belongs to a different security
|
---|
240 | * context.
|
---|
241 | */
|
---|
242 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
243 | return ENOENT;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /* Lock the task and release the lock protecting tasks_btree. */
|
---|
247 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
---|
248 |
|
---|
249 | int rc = ddi_iospace_enable_arch(task, ioaddr, size);
|
---|
250 |
|
---|
251 | irq_spinlock_unlock(&task->lock, true);
|
---|
252 |
|
---|
253 | return rc;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /** Wrapper for SYS_PHYSMEM_MAP syscall.
|
---|
257 | *
|
---|
258 | * @param phys Physical base address to map
|
---|
259 | * @param virt Destination virtual address
|
---|
260 | * @param pages Number of pages
|
---|
261 | * @param flags Flags of newly mapped pages
|
---|
262 | *
|
---|
263 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
264 | *
|
---|
265 | */
|
---|
266 | sysarg_t sys_physmem_map(uintptr_t phys, uintptr_t virt,
|
---|
267 | size_t pages, unsigned int flags)
|
---|
268 | {
|
---|
269 | return (sysarg_t)
|
---|
270 | ddi_physmem_map(ALIGN_DOWN(phys, FRAME_SIZE),
|
---|
271 | ALIGN_DOWN(virt, PAGE_SIZE), pages, flags);
|
---|
272 | }
|
---|
273 |
|
---|
274 | /** Wrapper for SYS_ENABLE_IOSPACE syscall.
|
---|
275 | *
|
---|
276 | * @param uspace_io_arg User space address of DDI argument structure.
|
---|
277 | *
|
---|
278 | * @return 0 on success, otherwise it returns error code found in errno.h
|
---|
279 | *
|
---|
280 | */
|
---|
281 | sysarg_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
|
---|
282 | {
|
---|
283 | ddi_ioarg_t arg;
|
---|
284 | int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
|
---|
285 | if (rc != 0)
|
---|
286 | return (sysarg_t) rc;
|
---|
287 |
|
---|
288 | return (sysarg_t) ddi_iospace_enable((task_id_t) arg.task_id,
|
---|
289 | (uintptr_t) arg.ioaddr, (size_t) arg.size);
|
---|
290 | }
|
---|
291 |
|
---|
292 | NO_TRACE static int dmamem_map(uintptr_t virt, size_t size,
|
---|
293 | unsigned int map_flags, unsigned int flags, void **phys)
|
---|
294 | {
|
---|
295 | ASSERT(TASK);
|
---|
296 |
|
---|
297 | if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0) {
|
---|
298 | // TODO: implement locking of non-anonymous mapping
|
---|
299 | return page_find_mapping(virt, phys);
|
---|
300 | } else {
|
---|
301 | // TODO: implement locking
|
---|
302 |
|
---|
303 | if ((virt % PAGE_SIZE) != 0)
|
---|
304 | return EBADMEM;
|
---|
305 |
|
---|
306 | size_t pages = SIZE2FRAMES(size);
|
---|
307 | uint8_t order;
|
---|
308 |
|
---|
309 | /* We need the 2^order >= pages */
|
---|
310 | if (pages == 1)
|
---|
311 | order = 0;
|
---|
312 | else
|
---|
313 | order = fnzb(pages - 1) + 1;
|
---|
314 |
|
---|
315 | *phys = frame_alloc_noreserve(order, 0);
|
---|
316 | if (*phys == NULL)
|
---|
317 | return ENOMEM;
|
---|
318 |
|
---|
319 | mem_backend_data_t backend_data;
|
---|
320 | backend_data.base = (uintptr_t) *phys;
|
---|
321 | backend_data.frames = pages;
|
---|
322 |
|
---|
323 | if (!as_area_create(TASK->as, map_flags, size, virt,
|
---|
324 | AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
|
---|
325 | frame_free_noreserve((uintptr_t) *phys);
|
---|
326 | return ENOMEM;
|
---|
327 | }
|
---|
328 |
|
---|
329 | return EOK;
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | NO_TRACE static int dmamem_unmap(uintptr_t virt, size_t size,
|
---|
334 | unsigned int flags)
|
---|
335 | {
|
---|
336 | // TODO: implement unlocking & unmap
|
---|
337 | return EOK;
|
---|
338 | }
|
---|
339 |
|
---|
340 | sysarg_t sys_dmamem_map(uintptr_t virt, size_t size, unsigned int map_flags,
|
---|
341 | unsigned int flags, void *phys_ptr)
|
---|
342 | {
|
---|
343 | void *phys;
|
---|
344 | int rc = dmamem_map(virt, size, map_flags, flags, &phys);
|
---|
345 | if (rc != EOK)
|
---|
346 | return rc;
|
---|
347 |
|
---|
348 | rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
|
---|
349 | if (rc != EOK) {
|
---|
350 | dmamem_unmap(virt, size, flags);
|
---|
351 | return rc;
|
---|
352 | }
|
---|
353 |
|
---|
354 | return EOK;
|
---|
355 | }
|
---|
356 |
|
---|
357 | sysarg_t sys_dmamem_unmap(uintptr_t virt, size_t size, unsigned int flags)
|
---|
358 | {
|
---|
359 | return dmamem_unmap(virt, size, flags);
|
---|
360 | }
|
---|
361 |
|
---|
362 | /** @}
|
---|
363 | */
|
---|