Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
703 jermar 1
/*
2
 * Copyright (C) 2001-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
/*
30
 * This file contains address space manipulation functions.
31
 * Roughly speaking, this is a higher-level client of
32
 * Virtual Address Translation (VAT) subsystem.
33
 */
34
 
35
#include <mm/as.h>
727 jermar 36
#include <mm/asid.h>
703 jermar 37
#include <mm/page.h>
38
#include <mm/frame.h>
39
#include <mm/tlb.h>
40
#include <mm/heap.h>
41
#include <arch/mm/page.h>
42
#include <genarch/mm/page_pt.h>
727 jermar 43
#include <mm/asid.h>
703 jermar 44
#include <arch/mm/asid.h>
45
#include <arch/mm/as.h>
46
#include <arch/types.h>
47
#include <typedefs.h>
48
#include <synch/spinlock.h>
49
#include <config.h>
50
#include <list.h>
51
#include <panic.h>
52
#include <arch/asm.h>
53
#include <debug.h>
54
#include <memstr.h>
55
#include <arch.h>
56
#include <print.h>
57
 
58
#define KAS_START_INDEX		PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)
59
#define KAS_END_INDEX		PTL0_INDEX(KERNEL_ADDRESS_SPACE_END)
60
#define KAS_INDICES		(1+(KAS_END_INDEX-KAS_START_INDEX))
61
 
754 jermar 62
static int get_area_flags(as_area_t *a);
703 jermar 63
 
64
/** Create address space. */
65
/*
66
 * FIXME: this interface must be meaningful for all possible VAT
67
 * 	  (Virtual Address Translation) mechanisms.
68
 */
727 jermar 69
as_t *as_create(pte_t *ptl0, int flags)
703 jermar 70
{
71
	as_t *as;
72
 
73
	as = (as_t *) malloc(sizeof(as_t));
74
	if (as) {
727 jermar 75
		list_initialize(&as->as_with_asid_link);
703 jermar 76
		spinlock_initialize(&as->lock, "as_lock");
77
		list_initialize(&as->as_area_head);
78
 
727 jermar 79
		if (flags & AS_KERNEL)
80
			as->asid = ASID_KERNEL;
81
		else
82
			as->asid = ASID_INVALID;
703 jermar 83
 
84
		as->ptl0 = ptl0;
85
		if (!as->ptl0) {
86
			pte_t *src_ptl0, *dst_ptl0;
87
 
88
			src_ptl0 = (pte_t *) PA2KA((__address) GET_PTL0_ADDRESS());
89
			dst_ptl0 = (pte_t *) frame_alloc(FRAME_KA | FRAME_PANIC, ONE_FRAME, NULL);
90
 
91
//			memsetb((__address) dst_ptl0, PAGE_SIZE, 0);
92
//			memcpy((void *) &dst_ptl0[KAS_START_INDEX], (void *) &src_ptl0[KAS_START_INDEX], KAS_INDICES);
93
 
94
			memcpy((void *) dst_ptl0,(void *) src_ptl0, PAGE_SIZE);
95
 
96
			as->ptl0 = (pte_t *) KA2PA((__address) dst_ptl0);
97
		}
98
	}
99
 
100
	return as;
101
}
102
 
103
/** Create address space area of common attributes.
104
 *
105
 * The created address space area is added to the target address space.
106
 *
107
 * @param as Target address space.
108
 * @param type Type of area.
109
 * @param size Size of area in multiples of PAGE_SIZE.
110
 * @param base Base address of area.
111
 *
112
 * @return Address space area on success or NULL on failure.
113
 */
114
as_area_t *as_area_create(as_t *as, as_area_type_t type, size_t size, __address base)
115
{
116
	ipl_t ipl;
117
	as_area_t *a;
118
 
119
	if (base % PAGE_SIZE)
120
		panic("addr not aligned to a page boundary");
121
 
122
	ipl = interrupts_disable();
123
	spinlock_lock(&as->lock);
124
 
125
	/*
126
	 * TODO: test as_area which is to be created doesn't overlap with an existing one.
127
	 */
128
 
129
	a = (as_area_t *) malloc(sizeof(as_area_t));
754 jermar 130
	if (a) {	
703 jermar 131
		spinlock_initialize(&a->lock, "as_area_lock");
132
 
133
		link_initialize(&a->link);			
134
		a->type = type;
135
		a->size = size;
136
		a->base = base;
137
 
138
		list_append(&a->link, &as->as_area_head);
139
	}
140
 
141
	spinlock_unlock(&as->lock);
142
	interrupts_restore(ipl);
704 jermar 143
 
703 jermar 144
	return a;
145
}
146
 
754 jermar 147
/** Initialize mapping for one page of address space.
703 jermar 148
 *
754 jermar 149
 * This functions maps 'page' to 'frame' according
150
 * to attributes of the address space area to
151
 * wich 'page' belongs.
703 jermar 152
 *
754 jermar 153
 * @param a Target address space.
154
 * @param page Virtual page within the area.
155
 * @param frame Physical frame to which page will be mapped.
703 jermar 156
 */
754 jermar 157
void as_set_mapping(as_t *as, __address page, __address frame)
703 jermar 158
{
754 jermar 159
	as_area_t *a, *area = NULL;
160
	link_t *cur;
703 jermar 161
	ipl_t ipl;
162
 
163
	ipl = interrupts_disable();
754 jermar 164
	spinlock_lock(&as->lock);
703 jermar 165
 
754 jermar 166
	/*
167
	 * First, try locate an area.
168
	 */
169
	for (cur = as->as_area_head.next; cur != &as->as_area_head; cur = cur->next) {
170
		a = list_get_instance(cur, as_area_t, link);
171
		spinlock_lock(&a->lock);
172
 
173
		if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
174
			area = a;
175
			break;
176
		}
177
 
178
		spinlock_unlock(&a->lock);
179
	}
718 decky 180
 
754 jermar 181
	if (!area) {
182
		panic("page not part of any as_area\n");
183
	}
184
 
185
	/*
186
	 * Note: area->lock is held.
187
	 */
188
 
189
	page_mapping_insert(page, as->asid, frame, get_area_flags(area), (__address) as->ptl0);
190
 
191
	spinlock_unlock(&area->lock);
192
	spinlock_unlock(&as->lock);
703 jermar 193
	interrupts_restore(ipl);
194
}
195
 
196
/** Handle page fault within the current address space.
197
 *
198
 * This is the high-level page fault handler.
199
 * Interrupts are assumed disabled.
200
 *
201
 * @param page Faulting page.
202
 *
704 jermar 203
 * @return 0 on page fault, 1 on success.
703 jermar 204
 */
205
int as_page_fault(__address page)
206
{
207
	link_t *cur;
208
	as_area_t *a, *area = NULL;
209
	__address frame;
210
 
211
	ASSERT(AS);
212
	spinlock_lock(&AS->lock);
213
 
214
	/*
215
	 * Search this areas of this address space for presence of 'page'.
216
	 */
217
	for (cur = AS->as_area_head.next; cur != &AS->as_area_head; cur = cur->next) {
218
		a = list_get_instance(cur, as_area_t, link);
219
		spinlock_lock(&a->lock);
220
 
221
		if ((page >= a->base) && (page < a->base + a->size * PAGE_SIZE)) {
222
 
223
			/*
224
			 * We found the area containing 'page'.
225
			 * TODO: access checking
226
			 */
227
			area = a;
228
			break;
229
		}
230
 
231
		spinlock_unlock(&a->lock);
232
	}
233
 
234
	if (!area) {
235
		/*
236
		 * No area contained mapping for 'page'.
237
		 * Signal page fault to low-level handler.
238
		 */
239
		spinlock_unlock(&AS->lock);
240
		return 0;
241
	}
242
 
243
	/*
244
	 * Note: area->lock is held.
245
	 */
246
 
247
	/*
754 jermar 248
	 * In general, there can be several reasons that
249
	 * can have caused this fault.
250
	 *
251
	 * - non-existent mapping: the area is a scratch
252
	 *   area (e.g. stack) and so far has not been
253
	 *   allocated a frame for the faulting page
254
	 *
255
	 * - non-present mapping: another possibility,
256
	 *   currently not implemented, would be frame
257
	 *   reuse; when this becomes a possibility,
258
	 *   do not forget to distinguish between
259
	 *   the different causes
703 jermar 260
	 */
754 jermar 261
	frame = frame_alloc(0, ONE_FRAME, NULL);
262
	memsetb(PA2KA(frame), FRAME_SIZE, 0);
703 jermar 263
 
264
	/*
265
	 * Map 'page' to 'frame'.
266
	 * Note that TLB shootdown is not attempted as only new information is being
267
	 * inserted into page tables.
268
	 */
754 jermar 269
	page_mapping_insert(page, AS->asid, frame, get_area_flags(area), (__address) AS->ptl0);
703 jermar 270
 
271
	spinlock_unlock(&area->lock);
272
	spinlock_unlock(&AS->lock);
273
 
274
	return 1;
275
}
276
 
277
/** Install address space on CPU.
278
 *
279
 * @param as Address space.
280
 */
281
void as_install(as_t *as)
282
{
283
	ipl_t ipl;
284
 
727 jermar 285
	asid_install(as);
286
 
703 jermar 287
	ipl = interrupts_disable();
288
	spinlock_lock(&as->lock);
289
	ASSERT(as->ptl0);
290
	SET_PTL0_ADDRESS(as->ptl0);
291
	spinlock_unlock(&as->lock);
292
	interrupts_restore(ipl);
293
 
294
	/*
295
	 * Perform architecture-specific steps.
727 jermar 296
	 * (e.g. write ASID to hardware register etc.)
703 jermar 297
	 */
298
	as_install_arch(as);
299
 
300
	AS = as;
301
}
754 jermar 302
 
303
/** Compute flags for virtual address translation subsytem.
304
 *
305
 * The address space area must be locked.
306
 * Interrupts must be disabled.
307
 *
308
 * @param a Address space area.
309
 *
310
 * @return Flags to be used in page_mapping_insert().
311
 */
312
int get_area_flags(as_area_t *a)
313
{
314
	int flags;
315
 
316
	switch (a->type) {
317
		case AS_AREA_TEXT:
318
			flags = PAGE_EXEC | PAGE_READ | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
319
			break;
320
		case AS_AREA_DATA:
321
		case AS_AREA_STACK:
322
			flags = PAGE_READ | PAGE_WRITE | PAGE_USER | PAGE_PRESENT | PAGE_CACHEABLE;
323
			break;
324
		default:
325
			panic("unexpected as_area_type_t %d", a->type);
326
	}
327
 
328
	return flags;
329
}