Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
570 jermar 1
/*
2
 * Copyright (C) 2005 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
#include <arch/mm/tlb.h>
30
#include <mm/tlb.h>
619 jermar 31
#include <arch/mm/frame.h>
32
#include <arch/mm/page.h>
33
#include <arch/mm/mmu.h>
877 jermar 34
#include <mm/asid.h>
570 jermar 35
#include <print.h>
617 jermar 36
#include <arch/types.h>
37
#include <typedefs.h>
619 jermar 38
#include <config.h>
630 jermar 39
#include <arch/trap/trap.h>
863 jermar 40
#include <panic.h>
873 jermar 41
#include <arch/asm.h>
42
#include <symtab.h>
894 jermar 43
 
883 jermar 44
#include <arch/drivers/fb.h>
895 jermar 45
#include <arch/drivers/i8042.h>
570 jermar 46
 
873 jermar 47
char *context_encoding[] = {
48
	"Primary",
49
	"Secondary",
50
	"Nucleus",
51
	"Reserved"
52
};
53
 
619 jermar 54
/** Initialize ITLB and DTLB.
55
 *
56
 * The goal of this function is to disable MMU
57
 * so that both TLBs can be purged and new
58
 * kernel 4M locked entry can be installed.
59
 * After TLB is initialized, MMU is enabled
60
 * again.
627 jermar 61
 *
62
 * Switching MMU off imposes the requirement for
63
 * the kernel to run in identity mapped environment.
619 jermar 64
 */
570 jermar 65
void tlb_arch_init(void)
66
{
619 jermar 67
	tlb_tag_access_reg_t tag;
68
	tlb_data_t data;
69
	frame_address_t fr;
70
	page_address_t pg;
71
 
72
	fr.address = config.base;
73
	pg.address = config.base;
646 jermar 74
 
619 jermar 75
	immu_disable();
76
	dmmu_disable();
77
 
78
	/*
846 jermar 79
	 * We do identity mapping of 4M-page at 4M.
619 jermar 80
	 */
877 jermar 81
	tag.value = ASID_KERNEL;
619 jermar 82
	tag.vpn = pg.vpn;
83
 
84
	itlb_tag_access_write(tag.value);
85
	dtlb_tag_access_write(tag.value);
86
 
87
	data.value = 0;
88
	data.v = true;
89
	data.size = PAGESIZE_4M;
90
	data.pfn = fr.pfn;
91
	data.l = true;
92
	data.cp = 1;
93
	data.cv = 1;
94
	data.p = true;
95
	data.w = true;
96
	data.g = true;
97
 
98
	itlb_data_in_write(data.value);
99
	dtlb_data_in_write(data.value);
100
 
627 jermar 101
	/*
102
	 * Register window traps can occur before MMU is enabled again.
103
	 * This ensures that any such traps will be handled from 
104
	 * kernel identity mapped trap handler.
105
	 */
106
	trap_switch_trap_table();
107
 
619 jermar 108
	tlb_invalidate_all();
109
 
110
	dmmu_enable();
111
	immu_enable();
897 jermar 112
}
873 jermar 113
 
897 jermar 114
/** Insert privileged mapping into DMMU TLB.
115
 *
116
 * @param page Virtual page address.
117
 * @param frame Physical frame address.
118
 * @param pagesize Page size.
119
 * @param locked True for permanent mappings, false otherwise.
120
 * @param cacheable True if the mapping is cacheable, false otherwise.
121
 */
122
void dtlb_insert_mapping(__address page, __address frame, int pagesize, bool locked, bool cacheable)
123
{
124
	tlb_tag_access_reg_t tag;
125
	tlb_data_t data;
126
	page_address_t pg;
127
	frame_address_t fr;
873 jermar 128
 
897 jermar 129
	pg.address = page;
130
	fr.address = frame;
873 jermar 131
 
894 jermar 132
	tag.value = ASID_KERNEL;
133
	tag.vpn = pg.vpn;
134
 
135
	dtlb_tag_access_write(tag.value);
136
 
137
	data.value = 0;
138
	data.v = true;
897 jermar 139
	data.size = pagesize;
894 jermar 140
	data.pfn = fr.pfn;
897 jermar 141
	data.l = locked;
142
	data.cp = cacheable;
143
	data.cv = cacheable;
894 jermar 144
	data.p = true;
145
	data.w = true;
146
	data.g = true;
147
 
148
	dtlb_data_in_write(data.value);
570 jermar 149
}
150
 
863 jermar 151
/** ITLB miss handler. */
152
void fast_instruction_access_mmu_miss(void)
153
{
154
	panic("%s\n", __FUNCTION__);
155
}
156
 
157
/** DTLB miss handler. */
158
void fast_data_access_mmu_miss(void)
159
{
877 jermar 160
	tlb_tag_access_reg_t tag;
161
	__address tpc;
873 jermar 162
	char *tpc_str;
883 jermar 163
 
877 jermar 164
	tag.value = dtlb_tag_access_read();
165
	if (tag.context != ASID_KERNEL || tag.vpn == 0) {
166
		tpc = tpc_read();
167
		tpc_str = get_symtab_entry(tpc);
873 jermar 168
 
877 jermar 169
		printf("Faulting page: %P, ASID=%d\n", tag.vpn * PAGE_SIZE, tag.context);
170
		printf("TPC=%P, (%s)\n", tpc, tpc_str ? tpc_str : "?");
171
		panic("%s\n", __FUNCTION__);
172
	}
173
 
174
	/*
175
	 * Identity map piece of faulting kernel address space.
176
	 */
897 jermar 177
	dtlb_insert_mapping(tag.vpn * PAGE_SIZE, tag.vpn * FRAME_SIZE, PAGESIZE_8K, false, true);
863 jermar 178
}
179
 
180
/** DTLB protection fault handler. */
181
void fast_data_access_protection(void)
182
{
183
	panic("%s\n", __FUNCTION__);
184
}
185
 
570 jermar 186
/** Print contents of both TLBs. */
187
void tlb_print(void)
188
{
189
	int i;
190
	tlb_data_t d;
191
	tlb_tag_read_reg_t t;
192
 
193
	printf("I-TLB contents:\n");
194
	for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
195
		d.value = itlb_data_access_read(i);
613 jermar 196
		t.value = itlb_tag_read_read(i);
570 jermar 197
 
617 jermar 198
		printf("%d: vpn=%Q, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%X, diag=%X, pfn=%X, soft=%X, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n",
199
			i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g);
570 jermar 200
	}
201
 
202
	printf("D-TLB contents:\n");
203
	for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
204
		d.value = dtlb_data_access_read(i);
613 jermar 205
		t.value = dtlb_tag_read_read(i);
570 jermar 206
 
617 jermar 207
		printf("%d: vpn=%Q, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%X, diag=%X, pfn=%X, soft=%X, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n",
208
			i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g);
570 jermar 209
	}
210
 
211
}
617 jermar 212
 
213
/** Invalidate all unlocked ITLB and DTLB entries. */
214
void tlb_invalidate_all(void)
215
{
216
	int i;
217
	tlb_data_t d;
218
	tlb_tag_read_reg_t t;
219
 
220
	for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
221
		d.value = itlb_data_access_read(i);
222
		if (!d.l) {
223
			t.value = itlb_tag_read_read(i);
224
			d.v = false;
225
			itlb_tag_access_write(t.value);
226
			itlb_data_access_write(i, d.value);
227
		}
228
	}
229
 
230
	for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
231
		d.value = dtlb_data_access_read(i);
232
		if (!d.l) {
233
			t.value = dtlb_tag_read_read(i);
234
			d.v = false;
235
			dtlb_tag_access_write(t.value);
236
			dtlb_data_access_write(i, d.value);
237
		}
238
	}
239
 
240
}
241
 
242
/** Invalidate all ITLB and DTLB entries that belong to specified ASID (Context).
243
 *
244
 * @param asid Address Space ID.
245
 */
246
void tlb_invalidate_asid(asid_t asid)
247
{
248
	/* TODO: write asid to some Context register and encode the register in second parameter below. */
249
	itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0);
250
	dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0);
251
}
252
 
727 jermar 253
/** Invalidate all ITLB and DTLB entries for specified page range in specified address space.
617 jermar 254
 *
255
 * @param asid Address Space ID.
727 jermar 256
 * @param page First page which to sweep out from ITLB and DTLB.
257
 * @param cnt Number of ITLB and DTLB entries to invalidate.
617 jermar 258
 */
727 jermar 259
void tlb_invalidate_pages(asid_t asid, __address page, count_t cnt)
617 jermar 260
{
727 jermar 261
	int i;
262
 
263
	for (i = 0; i < cnt; i++) {
264
		/* TODO: write asid to some Context register and encode the register in second parameter below. */
265
		itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE);
266
		dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE);
267
	}
617 jermar 268
}