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

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

automatic kernel console lockout

  • kernel automatically relinquishes the access to the kernel console when the uspace maps the respective physical memory area
  • kernel output before uspace initialization is currently broken on Ski (no physical memory area), but this is pending further unification
  • kernel console devices are now independent (there is no system-wide "silent" variable), thus on multiple devices the kernel console and uspace output might be usable at the same time
  • 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 /*
125 * Check if the memory region is explicitly enabled
126 * for mapping by any parea structure.
127 */
128
129 mutex_lock(&parea_lock);
130 btree_node_t *nodep;
131 parea_t *parea = (parea_t *) btree_search(&parea_btree,
132 (btree_key_t) pf, &nodep);
133
134 if ((parea != NULL) && (parea->frames >= pages)) {
135 if ((!priv) && (!parea->unpriv)) {
136 mutex_unlock(&parea_lock);
137 return EPERM;
138 }
139
140 goto map;
141 }
142
143 parea = NULL;
144 mutex_unlock(&parea_lock);
145
146 /*
147 * Check if the memory region is part of physical
148 * memory generally enabled for mapping.
149 */
150
151 irq_spinlock_lock(&zones.lock, true);
152 size_t znum = find_zone(ADDR2PFN(pf), pages, 0);
153
154 if (znum == (size_t) -1) {
155 /*
156 * Frames not found in any zone
157 * -> assume it is a hardware device and allow mapping
158 * for privileged tasks.
159 */
160 irq_spinlock_unlock(&zones.lock, true);
161
162 if (!priv)
163 return EPERM;
164
165 goto map;
166 }
167
168 if (zones.info[znum].flags & ZONE_FIRMWARE) {
169 /*
170 * Frames are part of firmware
171 * -> allow mapping for privileged tasks.
172 */
173 irq_spinlock_unlock(&zones.lock, true);
174
175 if (!priv)
176 return EPERM;
177
178 goto map;
179 }
180
181 irq_spinlock_unlock(&zones.lock, true);
182 return ENOENT;
183
184map:
185 if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp,
186 AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
187 /*
188 * The address space area was not created.
189 * We report it using ENOMEM.
190 */
191
192 if (parea != NULL)
193 mutex_unlock(&parea_lock);
194
195 return ENOMEM;
196 }
197
198 /*
199 * Mapping is created on-demand during page fault.
200 */
201
202 if (parea != NULL) {
203 parea->mapped = true;
204 mutex_unlock(&parea_lock);
205 }
206
207 return EOK;
208}
209
210/** Enable range of I/O space for task.
211 *
212 * @param id Task ID of the destination task.
213 * @param ioaddr Starting I/O address.
214 * @param size Size of the enabled I/O space..
215 *
216 * @return 0 on success, EPERM if the caller lacks capabilities to use this
217 * syscall, ENOENT if there is no task matching the specified ID.
218 *
219 */
220NO_TRACE static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr,
221 size_t size)
222{
223 /*
224 * Make sure the caller is authorised to make this syscall.
225 */
226 cap_t caps = cap_get(TASK);
227 if (!(caps & CAP_IO_MANAGER))
228 return EPERM;
229
230 irq_spinlock_lock(&tasks_lock, true);
231
232 task_t *task = task_find_by_id(id);
233
234 if ((!task) || (!container_check(CONTAINER, task->container))) {
235 /*
236 * There is no task with the specified ID
237 * or the task belongs to a different security
238 * context.
239 */
240 irq_spinlock_unlock(&tasks_lock, true);
241 return ENOENT;
242 }
243
244 /* Lock the task and release the lock protecting tasks_btree. */
245 irq_spinlock_exchange(&tasks_lock, &task->lock);
246
247 int rc = ddi_iospace_enable_arch(task, ioaddr, size);
248
249 irq_spinlock_unlock(&task->lock, true);
250
251 return rc;
252}
253
254/** Wrapper for SYS_PHYSMEM_MAP syscall.
255 *
256 * @param phys_base Physical base address to map
257 * @param virt_base Destination virtual address
258 * @param pages Number of pages
259 * @param flags Flags of newly mapped pages
260 *
261 * @return 0 on success, otherwise it returns error code found in errno.h
262 *
263 */
264sysarg_t sys_physmem_map(sysarg_t phys_base, sysarg_t virt_base,
265 sysarg_t pages, sysarg_t flags)
266{
267 return (sysarg_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
268 FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
269 (size_t) pages, (int) flags);
270}
271
272/** Wrapper for SYS_ENABLE_IOSPACE syscall.
273 *
274 * @param uspace_io_arg User space address of DDI argument structure.
275 *
276 * @return 0 on success, otherwise it returns error code found in errno.h
277 *
278 */
279sysarg_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
280{
281 ddi_ioarg_t arg;
282 int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
283 if (rc != 0)
284 return (sysarg_t) rc;
285
286 return (sysarg_t) ddi_iospace_enable((task_id_t) arg.task_id,
287 (uintptr_t) arg.ioaddr, (size_t) arg.size);
288}
289
290/** @}
291 */
Note: See TracBrowser for help on using the repository browser.