Rev 883 | Rev 895 | 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> |
894 | jermar | 45 | #include <arch/drivers/keyboard.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(); |
||
873 | jermar | 112 | |
113 | /* |
||
114 | * Quick hack: map frame buffer |
||
115 | */ |
||
883 | jermar | 116 | fr.address = FB_PHYS_ADDRESS; |
117 | pg.address = FB_VIRT_ADDRESS; |
||
873 | jermar | 118 | |
877 | jermar | 119 | tag.value = ASID_KERNEL; |
873 | jermar | 120 | tag.vpn = pg.vpn; |
121 | |||
122 | dtlb_tag_access_write(tag.value); |
||
123 | |||
124 | data.value = 0; |
||
125 | data.v = true; |
||
126 | data.size = PAGESIZE_4M; |
||
127 | data.pfn = fr.pfn; |
||
128 | data.l = true; |
||
129 | data.cp = 0; |
||
130 | data.cv = 0; |
||
131 | data.p = true; |
||
132 | data.w = true; |
||
133 | data.g = true; |
||
134 | |||
135 | dtlb_data_in_write(data.value); |
||
894 | jermar | 136 | |
137 | /* |
||
138 | * Quick hack: map keyboard |
||
139 | */ |
||
140 | fr.address = KBD_PHYS_ADDRESS; |
||
141 | pg.address = KBD_VIRT_ADDRESS; |
||
142 | |||
143 | tag.value = ASID_KERNEL; |
||
144 | tag.vpn = pg.vpn; |
||
145 | |||
146 | dtlb_tag_access_write(tag.value); |
||
147 | |||
148 | data.value = 0; |
||
149 | data.v = true; |
||
150 | data.size = PAGESIZE_8K; |
||
151 | data.pfn = fr.pfn; |
||
152 | data.l = true; |
||
153 | data.cp = 0; |
||
154 | data.cv = 0; |
||
155 | data.p = true; |
||
156 | data.w = true; |
||
157 | data.g = true; |
||
158 | |||
159 | dtlb_data_in_write(data.value); |
||
570 | jermar | 160 | } |
161 | |||
863 | jermar | 162 | /** ITLB miss handler. */ |
163 | void fast_instruction_access_mmu_miss(void) |
||
164 | { |
||
165 | panic("%s\n", __FUNCTION__); |
||
166 | } |
||
167 | |||
168 | /** DTLB miss handler. */ |
||
169 | void fast_data_access_mmu_miss(void) |
||
170 | { |
||
877 | jermar | 171 | tlb_tag_access_reg_t tag; |
172 | tlb_data_t data; |
||
173 | __address tpc; |
||
873 | jermar | 174 | char *tpc_str; |
883 | jermar | 175 | |
877 | jermar | 176 | tag.value = dtlb_tag_access_read(); |
177 | if (tag.context != ASID_KERNEL || tag.vpn == 0) { |
||
178 | tpc = tpc_read(); |
||
179 | tpc_str = get_symtab_entry(tpc); |
||
873 | jermar | 180 | |
877 | jermar | 181 | printf("Faulting page: %P, ASID=%d\n", tag.vpn * PAGE_SIZE, tag.context); |
182 | printf("TPC=%P, (%s)\n", tpc, tpc_str ? tpc_str : "?"); |
||
183 | panic("%s\n", __FUNCTION__); |
||
184 | } |
||
185 | |||
186 | /* |
||
187 | * Identity map piece of faulting kernel address space. |
||
188 | */ |
||
189 | data.value = 0; |
||
190 | data.v = true; |
||
191 | data.size = PAGESIZE_8K; |
||
192 | data.pfn = tag.vpn; |
||
193 | data.l = false; |
||
194 | data.cp = 1; |
||
195 | data.cv = 1; |
||
196 | data.p = true; |
||
197 | data.w = true; |
||
198 | data.g = true; |
||
199 | |||
200 | dtlb_data_in_write(data.value); |
||
863 | jermar | 201 | } |
202 | |||
203 | /** DTLB protection fault handler. */ |
||
204 | void fast_data_access_protection(void) |
||
205 | { |
||
206 | panic("%s\n", __FUNCTION__); |
||
207 | } |
||
208 | |||
570 | jermar | 209 | /** Print contents of both TLBs. */ |
210 | void tlb_print(void) |
||
211 | { |
||
212 | int i; |
||
213 | tlb_data_t d; |
||
214 | tlb_tag_read_reg_t t; |
||
215 | |||
216 | printf("I-TLB contents:\n"); |
||
217 | for (i = 0; i < ITLB_ENTRY_COUNT; i++) { |
||
218 | d.value = itlb_data_access_read(i); |
||
613 | jermar | 219 | t.value = itlb_tag_read_read(i); |
570 | jermar | 220 | |
617 | jermar | 221 | 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", |
222 | 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 | 223 | } |
224 | |||
225 | printf("D-TLB contents:\n"); |
||
226 | for (i = 0; i < DTLB_ENTRY_COUNT; i++) { |
||
227 | d.value = dtlb_data_access_read(i); |
||
613 | jermar | 228 | t.value = dtlb_tag_read_read(i); |
570 | jermar | 229 | |
617 | jermar | 230 | 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", |
231 | 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 | 232 | } |
233 | |||
234 | } |
||
617 | jermar | 235 | |
236 | /** Invalidate all unlocked ITLB and DTLB entries. */ |
||
237 | void tlb_invalidate_all(void) |
||
238 | { |
||
239 | int i; |
||
240 | tlb_data_t d; |
||
241 | tlb_tag_read_reg_t t; |
||
242 | |||
243 | for (i = 0; i < ITLB_ENTRY_COUNT; i++) { |
||
244 | d.value = itlb_data_access_read(i); |
||
245 | if (!d.l) { |
||
246 | t.value = itlb_tag_read_read(i); |
||
247 | d.v = false; |
||
248 | itlb_tag_access_write(t.value); |
||
249 | itlb_data_access_write(i, d.value); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | for (i = 0; i < DTLB_ENTRY_COUNT; i++) { |
||
254 | d.value = dtlb_data_access_read(i); |
||
255 | if (!d.l) { |
||
256 | t.value = dtlb_tag_read_read(i); |
||
257 | d.v = false; |
||
258 | dtlb_tag_access_write(t.value); |
||
259 | dtlb_data_access_write(i, d.value); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | } |
||
264 | |||
265 | /** Invalidate all ITLB and DTLB entries that belong to specified ASID (Context). |
||
266 | * |
||
267 | * @param asid Address Space ID. |
||
268 | */ |
||
269 | void tlb_invalidate_asid(asid_t asid) |
||
270 | { |
||
271 | /* TODO: write asid to some Context register and encode the register in second parameter below. */ |
||
272 | itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0); |
||
273 | dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0); |
||
274 | } |
||
275 | |||
727 | jermar | 276 | /** Invalidate all ITLB and DTLB entries for specified page range in specified address space. |
617 | jermar | 277 | * |
278 | * @param asid Address Space ID. |
||
727 | jermar | 279 | * @param page First page which to sweep out from ITLB and DTLB. |
280 | * @param cnt Number of ITLB and DTLB entries to invalidate. |
||
617 | jermar | 281 | */ |
727 | jermar | 282 | void tlb_invalidate_pages(asid_t asid, __address page, count_t cnt) |
617 | jermar | 283 | { |
727 | jermar | 284 | int i; |
285 | |||
286 | for (i = 0; i < cnt; i++) { |
||
287 | /* TODO: write asid to some Context register and encode the register in second parameter below. */ |
||
288 | itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE); |
||
289 | dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE); |
||
290 | } |
||
617 | jermar | 291 | } |