source: mainline/kernel/generic/src/ddi/ddi.c@ 98e4507

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

add magic value to THE structure for better stack/memory corruption detection
rename (security) contexts to containers (a slightly less overloaded term within SPARTAN kernel)

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