Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1178 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2006 Jakub Jermar
1178 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
 */
1702 cejka 28
 
1888 jermar 29
/** @addtogroup genericddi
1702 cejka 30
 * @{
31
 */
1248 jermar 32
 
33
/**
1702 cejka 34
 * @file
1248 jermar 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
 */
1178 jermar 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/spinlock.h>
1288 jermar 49
#include <syscall/copy.h>
3908 decky 50
#include <adt/list.h>
1178 jermar 51
#include <arch.h>
52
#include <align.h>
53
#include <errno.h>
54
 
2015 jermar 55
/** This lock protects the parea_btree. */
56
SPINLOCK_INITIALIZE(parea_lock);
57
 
3908 decky 58
/** List with enabled physical memory areas. */
59
static LIST_INITIALIZE(parea_head);
2015 jermar 60
 
61
/** Initialize DDI. */
62
void ddi_init(void)
63
{
3940 decky 64
	hw_area();
2015 jermar 65
}
66
 
67
/** Enable piece of physical memory for mapping by physmem_map().
68
 *
69
 * @param parea Pointer to physical area structure.
70
 *
71
 * @todo This function doesn't check for overlaps. It depends on the kernel to
72
 * create disjunct physical memory areas.
73
 */
74
void ddi_parea_register(parea_t *parea)
75
{
76
	ipl_t ipl;
3908 decky 77
 
2015 jermar 78
	ipl = interrupts_disable();
79
	spinlock_lock(&parea_lock);
80
 
81
	/*
82
	 * TODO: we should really check for overlaps here.
3908 decky 83
	 * However, we should be safe because the kernel is pretty sane.
2015 jermar 84
	 */
3908 decky 85
	link_initialize(&parea->link);
86
	list_append(&parea->link, &parea_head);
87
 
2015 jermar 88
	spinlock_unlock(&parea_lock);
3908 decky 89
	interrupts_restore(ipl);
2015 jermar 90
}
91
 
1494 palkovsky 92
/** Map piece of physical memory into virtual address space of current task.
1178 jermar 93
 *
2015 jermar 94
 * @param pf Physical address of the starting frame.
95
 * @param vp Virtual address of the starting page.
1178 jermar 96
 * @param pages Number of pages to map.
1429 jermar 97
 * @param flags Address space area flags for the mapping.
1178 jermar 98
 *
2015 jermar 99
 * @return 0 on success, EPERM if the caller lacks capabilities to use this
3908 decky 100
 *  syscall, ENOENT if there is no task matching the specified ID or the
101
 *  physical address space is not enabled for mapping and ENOMEM if there
102
 *  was a problem in creating address space area.
1178 jermar 103
 */
3908 decky 104
static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, pfn_t pages, int flags)
1178 jermar 105
{
106
	ipl_t ipl;
107
	cap_t caps;
1425 jermar 108
	mem_backend_data_t backend_data;
3908 decky 109
 
1425 jermar 110
	backend_data.base = pf;
111
	backend_data.frames = pages;
1178 jermar 112
 
113
	/*
114
	 * Make sure the caller is authorised to make this syscall.
115
	 */
116
	caps = cap_get(TASK);
117
	if (!(caps & CAP_MEM_MANAGER))
118
		return EPERM;
3908 decky 119
 
1178 jermar 120
	ipl = interrupts_disable();
3908 decky 121
 
2015 jermar 122
	/*
123
	 * Check if the physical memory area is enabled for mapping.
124
	 */
125
	spinlock_lock(&parea_lock);
3908 decky 126
 
127
	bool fnd = false;
128
	link_t *cur;
129
 
130
	for (cur = parea_head.next; cur != &parea_head; cur = cur->next) {
131
		parea_t *parea = list_get_instance(cur, parea_t, link);
132
		if ((parea->pbase <= pf) && (ADDR2PFN(pf - parea->pbase) + pages <= parea->frames)) {
133
			fnd = true;
134
			break;
135
		}
136
	}
137
 
138
	spinlock_unlock(&parea_lock);
139
 
140
	if (!fnd) {
2015 jermar 141
		/*
3908 decky 142
		 * Physical memory area cannot be mapped.
2015 jermar 143
		 */
144
		interrupts_restore(ipl);
145
		return ENOENT;
146
	}
3908 decky 147
 
1494 palkovsky 148
	spinlock_lock(&TASK->lock);
1178 jermar 149
 
1494 palkovsky 150
	if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE,
1424 jermar 151
		&phys_backend, &backend_data)) {
1178 jermar 152
		/*
153
		 * The address space area could not have been created.
154
		 * We report it using ENOMEM.
155
		 */
1494 palkovsky 156
		spinlock_unlock(&TASK->lock);
1178 jermar 157
		interrupts_restore(ipl);
158
		return ENOMEM;
159
	}
160
 
1424 jermar 161
	/*
162
	 * Mapping is created on-demand during page fault.
163
	 */
164
 
1494 palkovsky 165
	spinlock_unlock(&TASK->lock);
1178 jermar 166
	interrupts_restore(ipl);
167
	return 0;
168
}
169
 
1191 jermar 170
/** Enable range of I/O space for task.
171
 *
172
 * @param id Task ID of the destination task.
173
 * @param ioaddr Starting I/O address.
174
 * @param size Size of the enabled I/O space..
175
 *
2015 jermar 176
 * @return 0 on success, EPERM if the caller lacks capabilities to use this
177
 * 	syscall, ENOENT if there is no task matching the specified ID.
1191 jermar 178
 */
1780 jermar 179
static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
1191 jermar 180
{
181
	ipl_t ipl;
182
	cap_t caps;
183
	task_t *t;
184
	int rc;
185
 
186
	/*
187
	 * Make sure the caller is authorised to make this syscall.
188
	 */
189
	caps = cap_get(TASK);
190
	if (!(caps & CAP_IO_MANAGER))
191
		return EPERM;
192
 
193
	ipl = interrupts_disable();
194
	spinlock_lock(&tasks_lock);
195
 
196
	t = task_find_by_id(id);
197
 
1839 decky 198
	if ((!t) || (!context_check(CONTEXT, t->context))) {
1191 jermar 199
		/*
1839 decky 200
		 * There is no task with the specified ID
201
		 * or the task belongs to a different security
202
		 * context.
1191 jermar 203
		 */
204
		spinlock_unlock(&tasks_lock);
205
		interrupts_restore(ipl);
206
		return ENOENT;
207
	}
208
 
209
	/* Lock the task and release the lock protecting tasks_btree. */
210
	spinlock_lock(&t->lock);
211
	spinlock_unlock(&tasks_lock);
212
 
1227 jermar 213
	rc = ddi_iospace_enable_arch(t, ioaddr, size);
1191 jermar 214
 
215
	spinlock_unlock(&t->lock);
216
	interrupts_restore(ipl);
217
	return rc;
218
}
219
 
2012 jermar 220
/** Wrapper for SYS_PHYSMEM_MAP syscall.
1178 jermar 221
 *
1494 palkovsky 222
 * @param phys_base Physical base address to map
223
 * @param virt_base Destination virtual address
224
 * @param pages Number of pages
225
 * @param flags Flags of newly mapped pages
1178 jermar 226
 *
227
 * @return 0 on success, otherwise it returns error code found in errno.h
228
 */ 
2107 jermar 229
unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base,
230
    unative_t pages, unative_t flags)
1178 jermar 231
{
2015 jermar 232
	return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
2107 jermar 233
	    FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
3908 decky 234
	    (pfn_t) pages, (int) flags);
1178 jermar 235
}
1191 jermar 236
 
237
/** Wrapper for SYS_ENABLE_IOSPACE syscall.
238
 *
1708 jermar 239
 * @param uspace_io_arg User space address of DDI argument structure.
1191 jermar 240
 *
241
 * @return 0 on success, otherwise it returns error code found in errno.h
242
 */ 
1780 jermar 243
unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg)
1191 jermar 244
{
245
	ddi_ioarg_t arg;
1288 jermar 246
	int rc;
1191 jermar 247
 
1288 jermar 248
	rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
249
	if (rc != 0)
1780 jermar 250
		return (unative_t) rc;
1288 jermar 251
 
2015 jermar 252
	return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id,
2107 jermar 253
	    (uintptr_t) arg.ioaddr, (size_t) arg.size);
1191 jermar 254
}
1297 jermar 255
 
256
/** Disable or enable preemption.
257
 *
2015 jermar 258
 * @param enable If non-zero, the preemption counter will be decremented,
259
 * 	leading to potential enabling of preemption. Otherwise the preemption
260
 * 	counter will be incremented, preventing preemption from occurring.
1297 jermar 261
 *
262
 * @return Zero on success or EPERM if callers capabilities are not sufficient.
263
 */ 
1780 jermar 264
unative_t sys_preempt_control(int enable)
1297 jermar 265
{
3908 decky 266
	if (!cap_get(TASK) & CAP_PREEMPT_CONTROL)
267
		return EPERM;
268
	if (enable)
269
		preemption_enable();
270
	else
271
		preemption_disable();
272
	return 0;
1297 jermar 273
}
1702 cejka 274
 
1888 jermar 275
/** @}
1702 cejka 276
 */