source: mainline/kernel/generic/src/ddi/ddi.c@ 9110ff0

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

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
  • Property mode set to 100644
File size: 7.7 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
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 */
101static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, size_t pages,
102 unsigned int flags)
103{
104 ASSERT(TASK);
105 ASSERT((pf % FRAME_SIZE) == 0);
106 ASSERT((vp % PAGE_SIZE) == 0);
107
108 /*
109 * Make sure the caller is authorised to make this syscall.
110 */
111 cap_t caps = cap_get(TASK);
112 if (!(caps & CAP_MEM_MANAGER))
113 return EPERM;
114
115 mem_backend_data_t backend_data;
116 backend_data.base = pf;
117 backend_data.frames = pages;
118
119 /* Find the zone of the physical memory */
120 irq_spinlock_lock(&zones.lock, true);
121 size_t znum = find_zone(ADDR2PFN(pf), pages, 0);
122
123 if (znum == (size_t) -1) {
124 /* Frames not found in any zones
125 * -> assume it is hardware device and allow mapping
126 */
127 irq_spinlock_unlock(&zones.lock, true);
128 goto map;
129 }
130
131 if (zones.info[znum].flags & ZONE_FIRMWARE) {
132 /* Frames are part of firmware */
133 irq_spinlock_unlock(&zones.lock, true);
134 goto map;
135 }
136
137 if (zone_flags_available(zones.info[znum].flags)) {
138 /*
139 * Frames are part of physical memory, check if the memory
140 * region is enabled for mapping.
141 */
142 irq_spinlock_unlock(&zones.lock, true);
143
144 mutex_lock(&parea_lock);
145 btree_node_t *nodep;
146 parea_t *parea = (parea_t *) btree_search(&parea_btree,
147 (btree_key_t) pf, &nodep);
148
149 if ((!parea) || (parea->frames < pages)) {
150 mutex_unlock(&parea_lock);
151 goto err;
152 }
153
154 mutex_unlock(&parea_lock);
155 goto map;
156 }
157
158 irq_spinlock_unlock(&zones.lock, true);
159
160err:
161 return ENOENT;
162
163map:
164 if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp,
165 AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) {
166 /*
167 * The address space area could not have been created.
168 * We report it using ENOMEM.
169 */
170 return ENOMEM;
171 }
172
173 /*
174 * Mapping is created on-demand during page fault.
175 */
176 return 0;
177}
178
179/** Enable range of I/O space for task.
180 *
181 * @param id Task ID of the destination task.
182 * @param ioaddr Starting I/O address.
183 * @param size Size of the enabled I/O space..
184 *
185 * @return 0 on success, EPERM if the caller lacks capabilities to use this
186 * syscall, ENOENT if there is no task matching the specified ID.
187 *
188 */
189static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
190{
191 /*
192 * Make sure the caller is authorised to make this syscall.
193 */
194 cap_t caps = cap_get(TASK);
195 if (!(caps & CAP_IO_MANAGER))
196 return EPERM;
197
198 irq_spinlock_lock(&tasks_lock, true);
199
200 task_t *task = task_find_by_id(id);
201
202 if ((!task) || (!context_check(CONTEXT, task->context))) {
203 /*
204 * There is no task with the specified ID
205 * or the task belongs to a different security
206 * context.
207 */
208 irq_spinlock_unlock(&tasks_lock, true);
209 return ENOENT;
210 }
211
212 /* Lock the task and release the lock protecting tasks_btree. */
213 irq_spinlock_exchange(&tasks_lock, &task->lock);
214
215 int rc = ddi_iospace_enable_arch(task, ioaddr, size);
216
217 irq_spinlock_unlock(&task->lock, true);
218
219 return rc;
220}
221
222/** Wrapper for SYS_PHYSMEM_MAP syscall.
223 *
224 * @param phys_base Physical base address to map
225 * @param virt_base Destination virtual address
226 * @param pages Number of pages
227 * @param flags Flags of newly mapped pages
228 *
229 * @return 0 on success, otherwise it returns error code found in errno.h
230 *
231 */
232unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base,
233 unative_t pages, unative_t flags)
234{
235 return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
236 FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
237 (size_t) pages, (int) flags);
238}
239
240/** Wrapper for SYS_ENABLE_IOSPACE syscall.
241 *
242 * @param uspace_io_arg User space address of DDI argument structure.
243 *
244 * @return 0 on success, otherwise it returns error code found in errno.h
245 *
246 */
247unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
248{
249 ddi_ioarg_t arg;
250 int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
251 if (rc != 0)
252 return (unative_t) rc;
253
254 return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id,
255 (uintptr_t) arg.ioaddr, (size_t) arg.size);
256}
257
258/** Disable or enable preemption.
259 *
260 * @param enable If non-zero, the preemption counter will be decremented,
261 * leading to potential enabling of preemption. Otherwise
262 * the preemption counter will be incremented, preventing
263 * preemption from occurring.
264 *
265 * @return Zero on success or EPERM if callers capabilities are not sufficient.
266 *
267 */
268unative_t sys_preempt_control(int enable)
269{
270 if (!(cap_get(TASK) & CAP_PREEMPT_CONTROL))
271 return EPERM;
272
273 if (enable)
274 preemption_enable();
275 else
276 preemption_disable();
277
278 return 0;
279}
280
281/** @}
282 */
Note: See TracBrowser for help on using the repository browser.