Subversion Repositories HelenOS-historic

Rev

Rev 1297 | Rev 1424 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1178 jermar 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
 */
1248 jermar 28
 
29
/**
30
 * @file	ddi.c
31
 * @brief	Device Driver Interface functions.
32
 *
33
 * This file contains functions that comprise the Device Driver Interface.
34
 * These are the functions for mapping physical memory and enabling I/O
35
 * space to tasks.
36
 */
1178 jermar 37
 
38
#include <ddi/ddi.h>
39
#include <ddi/ddi_arg.h>
40
#include <proc/task.h>
41
#include <security/cap.h>
42
#include <mm/frame.h>
43
#include <mm/as.h>
44
#include <synch/spinlock.h>
1288 jermar 45
#include <syscall/copy.h>
1178 jermar 46
#include <arch.h>
47
#include <align.h>
48
#include <errno.h>
49
 
50
/** Map piece of physical memory into virtual address space of specified task.
51
 *
52
 * @param id Task ID of the destination task.
53
 * @param pf Physical frame address of the starting frame.
54
 * @param vp Virtual page address of the starting page.
55
 * @param pages Number of pages to map.
56
 * @param writable If true, the mapping will be created writable.
57
 *
58
 * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall,
59
 *	   ENOENT if there is no task matching the specified ID and ENOMEM if
60
 *	   there was a problem in creating address space area.
61
 */
1227 jermar 62
static int ddi_physmem_map(task_id_t id, __address pf, __address vp, count_t pages, bool writable)
1178 jermar 63
{
64
	ipl_t ipl;
65
	cap_t caps;
66
	task_t *t;
67
	int flags;
68
	count_t i;
69
 
70
	/*
71
	 * Make sure the caller is authorised to make this syscall.
72
	 */
73
	caps = cap_get(TASK);
74
	if (!(caps & CAP_MEM_MANAGER))
75
		return EPERM;
76
 
77
	ipl = interrupts_disable();
78
	spinlock_lock(&tasks_lock);
79
 
80
	t = task_find_by_id(id);
81
 
82
	if (!t) {
83
		/*
84
		 * There is no task with the specified ID.
85
		 */
86
		spinlock_unlock(&tasks_lock);
87
		interrupts_restore(ipl);
88
		return ENOENT;
89
	}
90
 
91
	/*
92
	 * TODO: We are currently lacking support for task destroying.
93
	 * Once it is added to the kernel, we must take care to
94
	 * synchronize in a way that prevents race conditions here.
95
	 */
96
 
97
	/* Lock the task and release the lock protecting tasks_btree. */
98
	spinlock_lock(&t->lock);
99
	spinlock_unlock(&tasks_lock);
100
 
101
	flags = AS_AREA_DEVICE | AS_AREA_READ;
102
	if (writable)
103
		flags |= AS_AREA_WRITE;
1409 jermar 104
	if (!as_area_create(t->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE, NULL, NULL)) {
1178 jermar 105
		/*
106
		 * The address space area could not have been created.
107
		 * We report it using ENOMEM.
108
		 */
109
		spinlock_unlock(&t->lock);
110
		interrupts_restore(ipl);
111
		return ENOMEM;
112
	}
113
 
114
	/* Initialize page tables. */
115
	for (i = 0; i < pages; i++)
116
		as_set_mapping(t->as, vp + i * PAGE_SIZE, pf + i * FRAME_SIZE);
117
 
118
	spinlock_unlock(&t->lock);
119
	interrupts_restore(ipl);
120
	return 0;
121
}
122
 
1191 jermar 123
/** Enable range of I/O space for task.
124
 *
125
 * @param id Task ID of the destination task.
126
 * @param ioaddr Starting I/O address.
127
 * @param size Size of the enabled I/O space..
128
 *
129
 * @return 0 on success, EPERM if the caller lacks capabilities to use this syscall,
130
 *	   ENOENT if there is no task matching the specified ID.
131
 */
1227 jermar 132
static int ddi_iospace_enable(task_id_t id, __address ioaddr, size_t size)
1191 jermar 133
{
134
	ipl_t ipl;
135
	cap_t caps;
136
	task_t *t;
137
	int rc;
138
 
139
	/*
140
	 * Make sure the caller is authorised to make this syscall.
141
	 */
142
	caps = cap_get(TASK);
143
	if (!(caps & CAP_IO_MANAGER))
144
		return EPERM;
145
 
146
	ipl = interrupts_disable();
147
	spinlock_lock(&tasks_lock);
148
 
149
	t = task_find_by_id(id);
150
 
151
	if (!t) {
152
		/*
153
		 * There is no task with the specified ID.
154
		 */
155
		spinlock_unlock(&tasks_lock);
156
		interrupts_restore(ipl);
157
		return ENOENT;
158
	}
159
 
160
	/*
161
	 * TODO: We are currently lacking support for task destroying.
162
	 * Once it is added to the kernel, we must take care to
163
	 * synchronize in a way that prevents race conditions here.
164
	 */
165
 
166
	/* Lock the task and release the lock protecting tasks_btree. */
167
	spinlock_lock(&t->lock);
168
	spinlock_unlock(&tasks_lock);
169
 
1227 jermar 170
	rc = ddi_iospace_enable_arch(t, ioaddr, size);
1191 jermar 171
 
172
	spinlock_unlock(&t->lock);
173
	interrupts_restore(ipl);
174
	return rc;
175
}
176
 
1178 jermar 177
/** Wrapper for SYS_MAP_PHYSMEM syscall.
178
 *
1191 jermar 179
 * @param User space address of memory DDI argument structure.
1178 jermar 180
 *
181
 * @return 0 on success, otherwise it returns error code found in errno.h
182
 */ 
1227 jermar 183
__native sys_physmem_map(ddi_memarg_t *uspace_mem_arg)
1178 jermar 184
{
1191 jermar 185
	ddi_memarg_t arg;
1288 jermar 186
	int rc;
1178 jermar 187
 
1288 jermar 188
	rc = copy_from_uspace(&arg, uspace_mem_arg, sizeof(ddi_memarg_t));
189
	if (rc != 0)
190
		return (__native) rc;
191
 
1227 jermar 192
	return (__native) ddi_physmem_map((task_id_t) arg.task_id, ALIGN_DOWN((__address) arg.phys_base, FRAME_SIZE),
1178 jermar 193
					  ALIGN_DOWN((__address) arg.virt_base, PAGE_SIZE), (count_t) arg.pages,
194
					  (bool) arg.writable);
195
}
1191 jermar 196
 
197
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
198
 *
199
 * @param User space address of DDI argument structure.
200
 *
201
 * @return 0 on success, otherwise it returns error code found in errno.h
202
 */ 
1227 jermar 203
__native sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
1191 jermar 204
{
205
	ddi_ioarg_t arg;
1288 jermar 206
	int rc;
1191 jermar 207
 
1288 jermar 208
	rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
209
	if (rc != 0)
210
		return (__native) rc;
211
 
1227 jermar 212
	return (__native) ddi_iospace_enable((task_id_t) arg.task_id, (__address) arg.ioaddr, (size_t) arg.size);
1191 jermar 213
}
1297 jermar 214
 
215
/** Disable or enable preemption.
216
 *
217
 * @param enable If non-zero, the preemption counter will be decremented, leading to potential
218
 * 		 enabling of preemption. Otherwise the preemption counter will be incremented,
219
 *		 preventing preemption from occurring.
220
 *
221
 * @return Zero on success or EPERM if callers capabilities are not sufficient.
222
 */ 
223
__native sys_preempt_control(int enable)
224
{
225
        if (! cap_get(TASK) & CAP_PREEMPT_CONTROL)
226
                return EPERM;
227
        if (enable)
228
                preemption_enable();
229
        else
230
                preemption_disable();
231
        return 0;
232
}