source: mainline/kernel/generic/src/ddi/ddi.c@ c0699467

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c0699467 was c0699467, checked in by Martin Decky <martin@…>, 14 years ago

do not provide general access to kernel headers from uspace, only allow specific headers to be accessed or shared
externalize headers which serve as kernel/uspace API/ABI into a special tree

  • Property mode set to 100644
File size: 7.5 KB
Line 
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 <synch/mutex.h>
48#include <syscall/copy.h>
49#include <adt/btree.h>
50#include <arch.h>
51#include <align.h>
52#include <errno.h>
53#include <trace.h>
54
55/** This lock protects the parea_btree. */
56static mutex_t parea_lock;
57
58/** B+tree with enabled physical memory areas. */
59static btree_t parea_btree;
60
61/** Initialize DDI.
62 *
63 */
64void ddi_init(void)
65{
66 btree_create(&parea_btree);
67 mutex_initialize(&parea_lock, MUTEX_PASSIVE);
68}
69
70/** Enable piece of physical memory for mapping by physmem_map().
71 *
72 * @param parea Pointer to physical area structure.
73 *
74 */
75void ddi_parea_register(parea_t *parea)
76{
77 mutex_lock(&parea_lock);
78
79 /*
80 * We don't check for overlaps here as the kernel is pretty sane.
81 */
82 btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
83
84 mutex_unlock(&parea_lock);
85}
86
87/** Map piece of physical memory into virtual address space of current task.
88 *
89 * @param pf Physical address of the starting frame.
90 * @param vp Virtual address of the starting page.
91 * @param pages Number of pages to map.
92 * @param flags Address space area flags for the mapping.
93 *
94 * @return 0 on success, EPERM if the caller lacks capabilities to use this
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 *
100 */
101NO_TRACE static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, size_t pages,
102 unsigned int flags)
103{
104 ASSERT(TASK);
105
106 if ((pf % FRAME_SIZE) != 0)
107 return EBADMEM;
108
109 if ((vp % PAGE_SIZE) != 0)
110 return EBADMEM;
111
112 /*
113 * Unprivileged tasks are only allowed to map pareas
114 * which are explicitly marked as such.
115 */
116 bool priv =
117 ((cap_get(TASK) & CAP_MEM_MANAGER) == CAP_MEM_MANAGER);
118
119 mem_backend_data_t backend_data;
120 backend_data.base = pf;
121 backend_data.frames = pages;
122
123 /*
124 * Check if the memory region is explicitly enabled
125 * for mapping by any parea structure.
126 */
127
128 mutex_lock(&parea_lock);
129 btree_node_t *nodep;
130 parea_t *parea = (parea_t *) btree_search(&parea_btree,
131 (btree_key_t) pf, &nodep);
132
133 if ((parea != NULL) && (parea->frames >= pages)) {
134 if ((!priv) && (!parea->unpriv)) {
135 mutex_unlock(&parea_lock);
136 return EPERM;
137 }
138
139 goto map;
140 }
141
142 parea = NULL;
143 mutex_unlock(&parea_lock);
144
145 /*
146 * Check if the memory region is part of physical
147 * memory generally enabled for mapping.
148 */
149
150 irq_spinlock_lock(&zones.lock, true);
151 size_t znum = find_zone(ADDR2PFN(pf), pages, 0);
152
153 if (znum == (size_t) -1) {
154 /*
155 * Frames not found in any zone
156 * -> assume it is a hardware device and allow mapping
157 * for privileged tasks.
158 */
159 irq_spinlock_unlock(&zones.lock, true);
160
161 if (!priv)
162 return EPERM;
163
164 goto map;
165 }
166
167 if (zones.info[znum].flags & ZONE_FIRMWARE) {
168 /*
169 * Frames are part of firmware
170 * -> allow mapping for privileged tasks.
171 */
172 irq_spinlock_unlock(&zones.lock, true);
173
174 if (!priv)
175 return EPERM;
176
177 goto map;
178 }
179
180 irq_spinlock_unlock(&zones.lock, true);
181 return ENOENT;
182
183map:
184 if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp,
185 AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
186 /*
187 * The address space area was not created.
188 * We report it using ENOMEM.
189 */
190
191 if (parea != NULL)
192 mutex_unlock(&parea_lock);
193
194 return ENOMEM;
195 }
196
197 /*
198 * Mapping is created on-demand during page fault.
199 */
200
201 if (parea != NULL) {
202 parea->mapped = true;
203 mutex_unlock(&parea_lock);
204 }
205
206 return EOK;
207}
208
209/** Enable range of I/O space for task.
210 *
211 * @param id Task ID of the destination task.
212 * @param ioaddr Starting I/O address.
213 * @param size Size of the enabled I/O space..
214 *
215 * @return 0 on success, EPERM if the caller lacks capabilities to use this
216 * syscall, ENOENT if there is no task matching the specified ID.
217 *
218 */
219NO_TRACE static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr,
220 size_t size)
221{
222 /*
223 * Make sure the caller is authorised to make this syscall.
224 */
225 cap_t caps = cap_get(TASK);
226 if (!(caps & CAP_IO_MANAGER))
227 return EPERM;
228
229 irq_spinlock_lock(&tasks_lock, true);
230
231 task_t *task = task_find_by_id(id);
232
233 if ((!task) || (!container_check(CONTAINER, task->container))) {
234 /*
235 * There is no task with the specified ID
236 * or the task belongs to a different security
237 * context.
238 */
239 irq_spinlock_unlock(&tasks_lock, true);
240 return ENOENT;
241 }
242
243 /* Lock the task and release the lock protecting tasks_btree. */
244 irq_spinlock_exchange(&tasks_lock, &task->lock);
245
246 int rc = ddi_iospace_enable_arch(task, ioaddr, size);
247
248 irq_spinlock_unlock(&task->lock, true);
249
250 return rc;
251}
252
253/** Wrapper for SYS_PHYSMEM_MAP syscall.
254 *
255 * @param phys_base Physical base address to map
256 * @param virt_base Destination virtual address
257 * @param pages Number of pages
258 * @param flags Flags of newly mapped pages
259 *
260 * @return 0 on success, otherwise it returns error code found in errno.h
261 *
262 */
263sysarg_t sys_physmem_map(sysarg_t phys_base, sysarg_t virt_base,
264 sysarg_t pages, sysarg_t flags)
265{
266 return (sysarg_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
267 FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
268 (size_t) pages, (int) flags);
269}
270
271/** Wrapper for SYS_ENABLE_IOSPACE syscall.
272 *
273 * @param uspace_io_arg User space address of DDI argument structure.
274 *
275 * @return 0 on success, otherwise it returns error code found in errno.h
276 *
277 */
278sysarg_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
279{
280 ddi_ioarg_t arg;
281 int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
282 if (rc != 0)
283 return (sysarg_t) rc;
284
285 return (sysarg_t) ddi_iospace_enable((task_id_t) arg.task_id,
286 (uintptr_t) arg.ioaddr, (size_t) arg.size);
287}
288
289/** @}
290 */
Note: See TracBrowser for help on using the repository browser.