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
RevLine 
[9a8d91b]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[9a8d91b]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 */
[b45c443]28
[06e1e95]29/** @addtogroup genericddi
[b45c443]30 * @{
31 */
[e49e234]32
[9179d0a]33/**
[b45c443]34 * @file
[e49e234]35 * @brief Device Driver Interface functions.
[9179d0a]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 */
[9a8d91b]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>
[373acb4]47#include <synch/mutex.h>
[e3c762cd]48#include <syscall/copy.h>
[e49e234]49#include <adt/btree.h>
[9a8d91b]50#include <arch.h>
51#include <align.h>
52#include <errno.h>
[7a0359b]53#include <trace.h>
[9a8d91b]54
[f8ddd17]55/** This lock protects the parea_btree. */
[373acb4]56static mutex_t parea_lock;
[f8ddd17]57
[e49e234]58/** B+tree with enabled physical memory areas. */
59static btree_t parea_btree;
[ae318d3]60
[da1bafb]61/** Initialize DDI.
62 *
63 */
[f8ddd17]64void ddi_init(void)
65{
[e49e234]66 btree_create(&parea_btree);
[373acb4]67 mutex_initialize(&parea_lock, MUTEX_PASSIVE);
[f8ddd17]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{
[373acb4]77 mutex_lock(&parea_lock);
[f8ddd17]78
79 /*
[e49e234]80 * We don't check for overlaps here as the kernel is pretty sane.
[f8ddd17]81 */
[e49e234]82 btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL);
[ae318d3]83
[373acb4]84 mutex_unlock(&parea_lock);
[f8ddd17]85}
86
[8da51ad]87/** Map piece of physical memory into virtual address space of current task.
[9a8d91b]88 *
[e49e234]89 * @param pf Physical address of the starting frame.
90 * @param vp Virtual address of the starting page.
[9a8d91b]91 * @param pages Number of pages to map.
[6212095]92 * @param flags Address space area flags for the mapping.
[9a8d91b]93 *
[f8ddd17]94 * @return 0 on success, EPERM if the caller lacks capabilities to use this
[e49e234]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 *
[9a8d91b]100 */
[7a0359b]101NO_TRACE static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, size_t pages,
[da1bafb]102 unsigned int flags)
[9a8d91b]103{
[e49e234]104 ASSERT(TASK);
[d7533c7]105
106 if ((pf % FRAME_SIZE) != 0)
107 return EBADMEM;
108
109 if ((vp % PAGE_SIZE) != 0)
110 return EBADMEM;
[9a8d91b]111
112 /*
[d7533c7]113 * Unprivileged tasks are only allowed to map pareas
114 * which are explicitly marked as such.
[9a8d91b]115 */
[d7533c7]116 bool priv =
117 ((cap_get(TASK) & CAP_MEM_MANAGER) == CAP_MEM_MANAGER);
[ae318d3]118
[e49e234]119 mem_backend_data_t backend_data;
120 backend_data.base = pf;
121 backend_data.frames = pages;
[ae318d3]122
[b366a6f4]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
[da1bafb]150 irq_spinlock_lock(&zones.lock, true);
[98000fb]151 size_t znum = find_zone(ADDR2PFN(pf), pages, 0);
[ae318d3]152
[98000fb]153 if (znum == (size_t) -1) {
[d7533c7]154 /*
155 * Frames not found in any zone
156 * -> assume it is a hardware device and allow mapping
157 * for privileged tasks.
[e49e234]158 */
[da1bafb]159 irq_spinlock_unlock(&zones.lock, true);
[d7533c7]160
161 if (!priv)
162 return EPERM;
163
[e49e234]164 goto map;
[ae318d3]165 }
166
[e49e234]167 if (zones.info[znum].flags & ZONE_FIRMWARE) {
[d7533c7]168 /*
169 * Frames are part of firmware
170 * -> allow mapping for privileged tasks.
171 */
[da1bafb]172 irq_spinlock_unlock(&zones.lock, true);
[d7533c7]173
174 if (!priv)
175 return EPERM;
176
[e49e234]177 goto map;
178 }
[ae318d3]179
[da1bafb]180 irq_spinlock_unlock(&zones.lock, true);
[e49e234]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)) {
[9a8d91b]186 /*
[b366a6f4]187 * The address space area was not created.
[9a8d91b]188 * We report it using ENOMEM.
189 */
[b366a6f4]190
191 if (parea != NULL)
192 mutex_unlock(&parea_lock);
193
[9a8d91b]194 return ENOMEM;
195 }
196
[0ee077ee]197 /*
198 * Mapping is created on-demand during page fault.
199 */
[b366a6f4]200
201 if (parea != NULL) {
202 parea->mapped = true;
203 mutex_unlock(&parea_lock);
204 }
205
206 return EOK;
[9a8d91b]207}
208
[f52e54da]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 *
[f8ddd17]215 * @return 0 on success, EPERM if the caller lacks capabilities to use this
[e49e234]216 * syscall, ENOENT if there is no task matching the specified ID.
217 *
[f52e54da]218 */
[7a0359b]219NO_TRACE static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr,
220 size_t size)
[f52e54da]221{
222 /*
223 * Make sure the caller is authorised to make this syscall.
224 */
[e49e234]225 cap_t caps = cap_get(TASK);
[f52e54da]226 if (!(caps & CAP_IO_MANAGER))
227 return EPERM;
228
[da1bafb]229 irq_spinlock_lock(&tasks_lock, true);
[f52e54da]230
[e49e234]231 task_t *task = task_find_by_id(id);
[f52e54da]232
[473d5d2]233 if ((!task) || (!container_check(CONTAINER, task->container))) {
[f52e54da]234 /*
[cfffb290]235 * There is no task with the specified ID
236 * or the task belongs to a different security
237 * context.
[f52e54da]238 */
[da1bafb]239 irq_spinlock_unlock(&tasks_lock, true);
[f52e54da]240 return ENOENT;
241 }
[e49e234]242
[f52e54da]243 /* Lock the task and release the lock protecting tasks_btree. */
[da1bafb]244 irq_spinlock_exchange(&tasks_lock, &task->lock);
[f52e54da]245
[e49e234]246 int rc = ddi_iospace_enable_arch(task, ioaddr, size);
247
[da1bafb]248 irq_spinlock_unlock(&task->lock, true);
[e49e234]249
[f52e54da]250 return rc;
251}
252
[5a8b2a2]253/** Wrapper for SYS_PHYSMEM_MAP syscall.
[9a8d91b]254 *
[8da51ad]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
[9a8d91b]259 *
260 * @return 0 on success, otherwise it returns error code found in errno.h
[e49e234]261 *
262 */
[96b02eb9]263sysarg_t sys_physmem_map(sysarg_t phys_base, sysarg_t virt_base,
264 sysarg_t pages, sysarg_t flags)
[9a8d91b]265{
[96b02eb9]266 return (sysarg_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
[f619ec11]267 FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
[98000fb]268 (size_t) pages, (int) flags);
[9a8d91b]269}
[f52e54da]270
271/** Wrapper for SYS_ENABLE_IOSPACE syscall.
272 *
[abbc16e]273 * @param uspace_io_arg User space address of DDI argument structure.
[f52e54da]274 *
275 * @return 0 on success, otherwise it returns error code found in errno.h
[e49e234]276 *
277 */
[96b02eb9]278sysarg_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
[f52e54da]279{
280 ddi_ioarg_t arg;
[e49e234]281 int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
[e3c762cd]282 if (rc != 0)
[96b02eb9]283 return (sysarg_t) rc;
[e49e234]284
[96b02eb9]285 return (sysarg_t) ddi_iospace_enable((task_id_t) arg.task_id,
[f619ec11]286 (uintptr_t) arg.ioaddr, (size_t) arg.size);
[f52e54da]287}
[2bb8648]288
[06e1e95]289/** @}
[b45c443]290 */
Note: See TracBrowser for help on using the repository browser.