source: mainline/kernel/generic/src/ddi/ddi.c@ 3f5b4e0d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3f5b4e0d was 3f5b4e0d, checked in by Jakub Jermar <jakub@…>, 15 years ago

Disable sys_interrupts_enable() because it is not portable.

  • Property mode set to 100644
File size: 7.8 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 ASSERT((pf % FRAME_SIZE) == 0);
107 ASSERT((vp % PAGE_SIZE) == 0);
108
109 /*
110 * Make sure the caller is authorised to make this syscall.
111 */
112 cap_t caps = cap_get(TASK);
113 if (!(caps & CAP_MEM_MANAGER))
114 return EPERM;
115
116 mem_backend_data_t backend_data;
117 backend_data.base = pf;
118 backend_data.frames = pages;
119
120 /* Find the zone of the physical memory */
121 irq_spinlock_lock(&zones.lock, true);
122 size_t znum = find_zone(ADDR2PFN(pf), pages, 0);
123
124 if (znum == (size_t) -1) {
125 /* Frames not found in any zones
126 * -> assume it is hardware device and allow mapping
127 */
128 irq_spinlock_unlock(&zones.lock, true);
129 goto map;
130 }
131
132 if (zones.info[znum].flags & ZONE_FIRMWARE) {
133 /* Frames are part of firmware */
134 irq_spinlock_unlock(&zones.lock, true);
135 goto map;
136 }
137
138 if (zone_flags_available(zones.info[znum].flags)) {
139 /*
140 * Frames are part of physical memory, check if the memory
141 * region is enabled for mapping.
142 */
143 irq_spinlock_unlock(&zones.lock, true);
144
145 mutex_lock(&parea_lock);
146 btree_node_t *nodep;
147 parea_t *parea = (parea_t *) btree_search(&parea_btree,
148 (btree_key_t) pf, &nodep);
149
150 if ((!parea) || (parea->frames < pages)) {
151 mutex_unlock(&parea_lock);
152 goto err;
153 }
154
155 mutex_unlock(&parea_lock);
156 goto map;
157 }
158
159 irq_spinlock_unlock(&zones.lock, true);
160
161err:
162 return ENOENT;
163
164map:
165 if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp,
166 AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
167 /*
168 * The address space area could not have been created.
169 * We report it using ENOMEM.
170 */
171 return ENOMEM;
172 }
173
174 /*
175 * Mapping is created on-demand during page fault.
176 */
177 return 0;
178}
179
180/** Enable range of I/O space for task.
181 *
182 * @param id Task ID of the destination task.
183 * @param ioaddr Starting I/O address.
184 * @param size Size of the enabled I/O space..
185 *
186 * @return 0 on success, EPERM if the caller lacks capabilities to use this
187 * syscall, ENOENT if there is no task matching the specified ID.
188 *
189 */
190NO_TRACE static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr,
191 size_t size)
192{
193 /*
194 * Make sure the caller is authorised to make this syscall.
195 */
196 cap_t caps = cap_get(TASK);
197 if (!(caps & CAP_IO_MANAGER))
198 return EPERM;
199
200 irq_spinlock_lock(&tasks_lock, true);
201
202 task_t *task = task_find_by_id(id);
203
204 if ((!task) || (!context_check(CONTEXT, task->context))) {
205 /*
206 * There is no task with the specified ID
207 * or the task belongs to a different security
208 * context.
209 */
210 irq_spinlock_unlock(&tasks_lock, true);
211 return ENOENT;
212 }
213
214 /* Lock the task and release the lock protecting tasks_btree. */
215 irq_spinlock_exchange(&tasks_lock, &task->lock);
216
217 int rc = ddi_iospace_enable_arch(task, ioaddr, size);
218
219 irq_spinlock_unlock(&task->lock, true);
220
221 return rc;
222}
223
224/** Wrapper for SYS_PHYSMEM_MAP syscall.
225 *
226 * @param phys_base Physical base address to map
227 * @param virt_base Destination virtual address
228 * @param pages Number of pages
229 * @param flags Flags of newly mapped pages
230 *
231 * @return 0 on success, otherwise it returns error code found in errno.h
232 *
233 */
234unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base,
235 unative_t pages, unative_t flags)
236{
237 return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
238 FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
239 (size_t) pages, (int) flags);
240}
241
242/** Wrapper for SYS_ENABLE_IOSPACE syscall.
243 *
244 * @param uspace_io_arg User space address of DDI argument structure.
245 *
246 * @return 0 on success, otherwise it returns error code found in errno.h
247 *
248 */
249unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
250{
251 ddi_ioarg_t arg;
252 int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
253 if (rc != 0)
254 return (unative_t) rc;
255
256 return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id,
257 (uintptr_t) arg.ioaddr, (size_t) arg.size);
258}
259
260/** Disable or enable specified interrupts.
261 *
262 * @param irq the interrupt to be enabled/disabled.
263 * @param enable if true enable the interrupt, disable otherwise.
264 *
265 * @retutn Zero on success, error code otherwise.
266 */
267unative_t sys_interrupt_enable(int irq, int enable)
268{
269/* FIXME: this needs to be generic code, or better not be in kernel at all. */
270#if 0
271 cap_t task_cap = cap_get(TASK);
272 if (!(task_cap & CAP_IRQ_REG))
273 return EPERM;
274
275 if (irq < 0 || irq > 16) {
276 return EINVAL;
277 }
278
279 uint16_t irq_mask = (uint16_t)(1 << irq);
280 if (enable) {
281 trap_virtual_enable_irqs(irq_mask);
282 } else {
283 trap_virtual_disable_irqs(irq_mask);
284 }
285
286#endif
287 return 0;
288}
289
290/** @}
291 */
Note: See TracBrowser for help on using the repository browser.