Rev 1996 | Rev 2048 | 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 | |||
1792 | jermar | 29 | /** @addtogroup sparc64mm |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | /** @file |
||
33 | */ |
||
34 | |||
570 | jermar | 35 | #include <arch/mm/tlb.h> |
36 | #include <mm/tlb.h> |
||
1851 | jermar | 37 | #include <mm/as.h> |
38 | #include <mm/asid.h> |
||
619 | jermar | 39 | #include <arch/mm/frame.h> |
40 | #include <arch/mm/page.h> |
||
41 | #include <arch/mm/mmu.h> |
||
1851 | jermar | 42 | #include <arch/interrupt.h> |
1870 | jermar | 43 | #include <interrupt.h> |
1851 | jermar | 44 | #include <arch.h> |
570 | jermar | 45 | #include <print.h> |
617 | jermar | 46 | #include <arch/types.h> |
47 | #include <typedefs.h> |
||
619 | jermar | 48 | #include <config.h> |
630 | jermar | 49 | #include <arch/trap/trap.h> |
1880 | jermar | 50 | #include <arch/trap/exception.h> |
863 | jermar | 51 | #include <panic.h> |
873 | jermar | 52 | #include <arch/asm.h> |
894 | jermar | 53 | |
1891 | jermar | 54 | #ifdef CONFIG_TSB |
55 | #include <arch/mm/tsb.h> |
||
56 | #endif |
||
57 | |||
1852 | jermar | 58 | static void dtlb_pte_copy(pte_t *t, bool ro); |
59 | static void itlb_pte_copy(pte_t *t); |
||
60 | static void do_fast_instruction_access_mmu_miss_fault(istate_t *istate, const char *str); |
||
1865 | jermar | 61 | static void do_fast_data_access_mmu_miss_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str); |
62 | static void do_fast_data_access_protection_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str); |
||
1851 | jermar | 63 | |
873 | jermar | 64 | char *context_encoding[] = { |
65 | "Primary", |
||
66 | "Secondary", |
||
67 | "Nucleus", |
||
68 | "Reserved" |
||
69 | }; |
||
70 | |||
570 | jermar | 71 | void tlb_arch_init(void) |
72 | { |
||
1793 | jermar | 73 | /* |
1905 | jermar | 74 | * Invalidate all non-locked DTLB and ITLB entries. |
1793 | jermar | 75 | */ |
1905 | jermar | 76 | tlb_invalidate_all(); |
1946 | jermar | 77 | |
78 | /* |
||
79 | * Clear both SFSRs. |
||
80 | */ |
||
81 | dtlb_sfsr_write(0); |
||
82 | itlb_sfsr_write(0); |
||
897 | jermar | 83 | } |
873 | jermar | 84 | |
897 | jermar | 85 | /** Insert privileged mapping into DMMU TLB. |
86 | * |
||
87 | * @param page Virtual page address. |
||
88 | * @param frame Physical frame address. |
||
89 | * @param pagesize Page size. |
||
90 | * @param locked True for permanent mappings, false otherwise. |
||
91 | * @param cacheable True if the mapping is cacheable, false otherwise. |
||
92 | */ |
||
1780 | jermar | 93 | void dtlb_insert_mapping(uintptr_t page, uintptr_t frame, int pagesize, bool locked, bool cacheable) |
897 | jermar | 94 | { |
95 | tlb_tag_access_reg_t tag; |
||
96 | tlb_data_t data; |
||
97 | page_address_t pg; |
||
98 | frame_address_t fr; |
||
873 | jermar | 99 | |
897 | jermar | 100 | pg.address = page; |
101 | fr.address = frame; |
||
873 | jermar | 102 | |
894 | jermar | 103 | tag.value = ASID_KERNEL; |
104 | tag.vpn = pg.vpn; |
||
105 | |||
106 | dtlb_tag_access_write(tag.value); |
||
107 | |||
108 | data.value = 0; |
||
109 | data.v = true; |
||
897 | jermar | 110 | data.size = pagesize; |
894 | jermar | 111 | data.pfn = fr.pfn; |
897 | jermar | 112 | data.l = locked; |
113 | data.cp = cacheable; |
||
2009 | jermar | 114 | #ifdef CONFIG_VIRT_IDX_DCACHE |
897 | jermar | 115 | data.cv = cacheable; |
2009 | jermar | 116 | #endif /* CONFIG_VIRT_IDX_DCACHE */ |
894 | jermar | 117 | data.p = true; |
118 | data.w = true; |
||
1868 | jermar | 119 | data.g = false; |
894 | jermar | 120 | |
121 | dtlb_data_in_write(data.value); |
||
570 | jermar | 122 | } |
123 | |||
1852 | jermar | 124 | /** Copy PTE to TLB. |
125 | * |
||
126 | * @param t Page Table Entry to be copied. |
||
127 | * @param ro If true, the entry will be created read-only, regardless of its w field. |
||
128 | */ |
||
129 | void dtlb_pte_copy(pte_t *t, bool ro) |
||
1851 | jermar | 130 | { |
1852 | jermar | 131 | tlb_tag_access_reg_t tag; |
132 | tlb_data_t data; |
||
133 | page_address_t pg; |
||
134 | frame_address_t fr; |
||
135 | |||
136 | pg.address = t->page; |
||
137 | fr.address = t->frame; |
||
138 | |||
139 | tag.value = 0; |
||
140 | tag.context = t->as->asid; |
||
141 | tag.vpn = pg.vpn; |
||
142 | |||
143 | dtlb_tag_access_write(tag.value); |
||
144 | |||
145 | data.value = 0; |
||
146 | data.v = true; |
||
147 | data.size = PAGESIZE_8K; |
||
148 | data.pfn = fr.pfn; |
||
149 | data.l = false; |
||
150 | data.cp = t->c; |
||
2009 | jermar | 151 | #ifdef CONFIG_VIRT_IDX_DCACHE |
1852 | jermar | 152 | data.cv = t->c; |
2009 | jermar | 153 | #endif /* CONFIG_VIRT_IDX_DCACHE */ |
1864 | jermar | 154 | data.p = t->k; /* p like privileged */ |
1852 | jermar | 155 | data.w = ro ? false : t->w; |
156 | data.g = t->g; |
||
157 | |||
158 | dtlb_data_in_write(data.value); |
||
1851 | jermar | 159 | } |
160 | |||
1891 | jermar | 161 | /** Copy PTE to ITLB. |
162 | * |
||
163 | * @param t Page Table Entry to be copied. |
||
164 | */ |
||
1852 | jermar | 165 | void itlb_pte_copy(pte_t *t) |
166 | { |
||
167 | tlb_tag_access_reg_t tag; |
||
168 | tlb_data_t data; |
||
169 | page_address_t pg; |
||
170 | frame_address_t fr; |
||
171 | |||
172 | pg.address = t->page; |
||
173 | fr.address = t->frame; |
||
174 | |||
175 | tag.value = 0; |
||
176 | tag.context = t->as->asid; |
||
177 | tag.vpn = pg.vpn; |
||
178 | |||
179 | itlb_tag_access_write(tag.value); |
||
180 | |||
181 | data.value = 0; |
||
182 | data.v = true; |
||
183 | data.size = PAGESIZE_8K; |
||
184 | data.pfn = fr.pfn; |
||
185 | data.l = false; |
||
186 | data.cp = t->c; |
||
1864 | jermar | 187 | data.p = t->k; /* p like privileged */ |
1852 | jermar | 188 | data.w = false; |
189 | data.g = t->g; |
||
190 | |||
191 | itlb_data_in_write(data.value); |
||
192 | } |
||
193 | |||
863 | jermar | 194 | /** ITLB miss handler. */ |
1851 | jermar | 195 | void fast_instruction_access_mmu_miss(int n, istate_t *istate) |
863 | jermar | 196 | { |
1852 | jermar | 197 | uintptr_t va = ALIGN_DOWN(istate->tpc, PAGE_SIZE); |
198 | pte_t *t; |
||
199 | |||
200 | page_table_lock(AS, true); |
||
201 | t = page_mapping_find(AS, va); |
||
202 | if (t && PTE_EXECUTABLE(t)) { |
||
203 | /* |
||
204 | * The mapping was found in the software page hash table. |
||
205 | * Insert it into ITLB. |
||
206 | */ |
||
207 | t->a = true; |
||
208 | itlb_pte_copy(t); |
||
1891 | jermar | 209 | #ifdef CONFIG_TSB |
210 | itsb_pte_copy(t); |
||
211 | #endif |
||
1852 | jermar | 212 | page_table_unlock(AS, true); |
213 | } else { |
||
214 | /* |
||
215 | * Forward the page fault to the address space page fault handler. |
||
216 | */ |
||
217 | page_table_unlock(AS, true); |
||
218 | if (as_page_fault(va, PF_ACCESS_EXEC, istate) == AS_PF_FAULT) { |
||
219 | do_fast_instruction_access_mmu_miss_fault(istate, __FUNCTION__); |
||
220 | } |
||
221 | } |
||
863 | jermar | 222 | } |
223 | |||
1851 | jermar | 224 | /** DTLB miss handler. |
225 | * |
||
226 | * Note that some faults (e.g. kernel faults) were already resolved |
||
227 | * by the low-level, assembly language part of the fast_data_access_mmu_miss |
||
228 | * handler. |
||
229 | */ |
||
230 | void fast_data_access_mmu_miss(int n, istate_t *istate) |
||
863 | jermar | 231 | { |
877 | jermar | 232 | tlb_tag_access_reg_t tag; |
1851 | jermar | 233 | uintptr_t va; |
234 | pte_t *t; |
||
883 | jermar | 235 | |
877 | jermar | 236 | tag.value = dtlb_tag_access_read(); |
1865 | jermar | 237 | va = tag.vpn << PAGE_WIDTH; |
238 | |||
1851 | jermar | 239 | if (tag.context == ASID_KERNEL) { |
240 | if (!tag.vpn) { |
||
241 | /* NULL access in kernel */ |
||
1865 | jermar | 242 | do_fast_data_access_mmu_miss_fault(istate, tag, __FUNCTION__); |
1851 | jermar | 243 | } |
1865 | jermar | 244 | do_fast_data_access_mmu_miss_fault(istate, tag, "Unexpected kernel page fault."); |
1851 | jermar | 245 | } |
873 | jermar | 246 | |
1851 | jermar | 247 | page_table_lock(AS, true); |
248 | t = page_mapping_find(AS, va); |
||
249 | if (t) { |
||
250 | /* |
||
251 | * The mapping was found in the software page hash table. |
||
252 | * Insert it into DTLB. |
||
253 | */ |
||
1852 | jermar | 254 | t->a = true; |
255 | dtlb_pte_copy(t, true); |
||
1891 | jermar | 256 | #ifdef CONFIG_TSB |
257 | dtsb_pte_copy(t, true); |
||
258 | #endif |
||
1851 | jermar | 259 | page_table_unlock(AS, true); |
260 | } else { |
||
261 | /* |
||
262 | * Forward the page fault to the address space page fault handler. |
||
263 | */ |
||
264 | page_table_unlock(AS, true); |
||
265 | if (as_page_fault(va, PF_ACCESS_READ, istate) == AS_PF_FAULT) { |
||
1865 | jermar | 266 | do_fast_data_access_mmu_miss_fault(istate, tag, __FUNCTION__); |
1851 | jermar | 267 | } |
877 | jermar | 268 | } |
863 | jermar | 269 | } |
270 | |||
271 | /** DTLB protection fault handler. */ |
||
1851 | jermar | 272 | void fast_data_access_protection(int n, istate_t *istate) |
863 | jermar | 273 | { |
1859 | jermar | 274 | tlb_tag_access_reg_t tag; |
275 | uintptr_t va; |
||
276 | pte_t *t; |
||
277 | |||
278 | tag.value = dtlb_tag_access_read(); |
||
1865 | jermar | 279 | va = tag.vpn << PAGE_WIDTH; |
1859 | jermar | 280 | |
281 | page_table_lock(AS, true); |
||
282 | t = page_mapping_find(AS, va); |
||
283 | if (t && PTE_WRITABLE(t)) { |
||
284 | /* |
||
285 | * The mapping was found in the software page hash table and is writable. |
||
286 | * Demap the old mapping and insert an updated mapping into DTLB. |
||
287 | */ |
||
288 | t->a = true; |
||
289 | t->d = true; |
||
290 | dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_SECONDARY, va); |
||
291 | dtlb_pte_copy(t, false); |
||
1891 | jermar | 292 | #ifdef CONFIG_TSB |
293 | dtsb_pte_copy(t, false); |
||
294 | #endif |
||
1859 | jermar | 295 | page_table_unlock(AS, true); |
296 | } else { |
||
297 | /* |
||
298 | * Forward the page fault to the address space page fault handler. |
||
299 | */ |
||
300 | page_table_unlock(AS, true); |
||
301 | if (as_page_fault(va, PF_ACCESS_WRITE, istate) == AS_PF_FAULT) { |
||
1865 | jermar | 302 | do_fast_data_access_protection_fault(istate, tag, __FUNCTION__); |
1859 | jermar | 303 | } |
304 | } |
||
863 | jermar | 305 | } |
306 | |||
570 | jermar | 307 | /** Print contents of both TLBs. */ |
308 | void tlb_print(void) |
||
309 | { |
||
310 | int i; |
||
311 | tlb_data_t d; |
||
312 | tlb_tag_read_reg_t t; |
||
313 | |||
314 | printf("I-TLB contents:\n"); |
||
315 | for (i = 0; i < ITLB_ENTRY_COUNT; i++) { |
||
316 | d.value = itlb_data_access_read(i); |
||
613 | jermar | 317 | t.value = itlb_tag_read_read(i); |
570 | jermar | 318 | |
1735 | decky | 319 | printf("%d: vpn=%#llx, 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", |
617 | jermar | 320 | 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 | 321 | } |
322 | |||
323 | printf("D-TLB contents:\n"); |
||
324 | for (i = 0; i < DTLB_ENTRY_COUNT; i++) { |
||
325 | d.value = dtlb_data_access_read(i); |
||
613 | jermar | 326 | t.value = dtlb_tag_read_read(i); |
570 | jermar | 327 | |
1735 | decky | 328 | printf("%d: vpn=%#llx, 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", |
617 | jermar | 329 | 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 | 330 | } |
331 | |||
332 | } |
||
617 | jermar | 333 | |
1852 | jermar | 334 | void do_fast_instruction_access_mmu_miss_fault(istate_t *istate, const char *str) |
335 | { |
||
1870 | jermar | 336 | fault_if_from_uspace(istate, "%s\n", str); |
1880 | jermar | 337 | dump_istate(istate); |
1852 | jermar | 338 | panic("%s\n", str); |
339 | } |
||
340 | |||
1865 | jermar | 341 | void do_fast_data_access_mmu_miss_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str) |
1851 | jermar | 342 | { |
343 | uintptr_t va; |
||
344 | |||
1865 | jermar | 345 | va = tag.vpn << PAGE_WIDTH; |
1851 | jermar | 346 | |
1870 | jermar | 347 | fault_if_from_uspace(istate, "%s, Page=%p (ASID=%d)\n", str, va, tag.context); |
1880 | jermar | 348 | dump_istate(istate); |
1851 | jermar | 349 | printf("Faulting page: %p, ASID=%d\n", va, tag.context); |
350 | panic("%s\n", str); |
||
351 | } |
||
352 | |||
1865 | jermar | 353 | void do_fast_data_access_protection_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str) |
1859 | jermar | 354 | { |
355 | uintptr_t va; |
||
356 | |||
1865 | jermar | 357 | va = tag.vpn << PAGE_WIDTH; |
1859 | jermar | 358 | |
1870 | jermar | 359 | fault_if_from_uspace(istate, "%s, Page=%p (ASID=%d)\n", str, va, tag.context); |
1859 | jermar | 360 | printf("Faulting page: %p, ASID=%d\n", va, tag.context); |
1880 | jermar | 361 | dump_istate(istate); |
1859 | jermar | 362 | panic("%s\n", str); |
363 | } |
||
364 | |||
1946 | jermar | 365 | void dump_sfsr_and_sfar(void) |
366 | { |
||
367 | tlb_sfsr_reg_t sfsr; |
||
368 | uintptr_t sfar; |
||
369 | |||
370 | sfsr.value = dtlb_sfsr_read(); |
||
371 | sfar = dtlb_sfar_read(); |
||
372 | |||
373 | printf("DTLB SFSR: asi=%#x, ft=%#x, e=%d, ct=%d, pr=%d, w=%d, ow=%d, fv=%d\n", |
||
374 | sfsr.asi, sfsr.ft, sfsr.e, sfsr.ct, sfsr.pr, sfsr.w, sfsr.ow, sfsr.fv); |
||
375 | printf("DTLB SFAR: address=%p\n", sfar); |
||
376 | |||
377 | dtlb_sfsr_write(0); |
||
378 | } |
||
379 | |||
617 | jermar | 380 | /** Invalidate all unlocked ITLB and DTLB entries. */ |
381 | void tlb_invalidate_all(void) |
||
382 | { |
||
383 | int i; |
||
384 | tlb_data_t d; |
||
385 | tlb_tag_read_reg_t t; |
||
386 | |||
387 | for (i = 0; i < ITLB_ENTRY_COUNT; i++) { |
||
388 | d.value = itlb_data_access_read(i); |
||
389 | if (!d.l) { |
||
390 | t.value = itlb_tag_read_read(i); |
||
391 | d.v = false; |
||
392 | itlb_tag_access_write(t.value); |
||
393 | itlb_data_access_write(i, d.value); |
||
394 | } |
||
395 | } |
||
396 | |||
397 | for (i = 0; i < DTLB_ENTRY_COUNT; i++) { |
||
398 | d.value = dtlb_data_access_read(i); |
||
399 | if (!d.l) { |
||
400 | t.value = dtlb_tag_read_read(i); |
||
401 | d.v = false; |
||
402 | dtlb_tag_access_write(t.value); |
||
403 | dtlb_data_access_write(i, d.value); |
||
404 | } |
||
405 | } |
||
406 | |||
407 | } |
||
408 | |||
409 | /** Invalidate all ITLB and DTLB entries that belong to specified ASID (Context). |
||
410 | * |
||
411 | * @param asid Address Space ID. |
||
412 | */ |
||
413 | void tlb_invalidate_asid(asid_t asid) |
||
414 | { |
||
1865 | jermar | 415 | tlb_context_reg_t pc_save, ctx; |
1860 | jermar | 416 | |
1865 | jermar | 417 | /* switch to nucleus because we are mapped by the primary context */ |
418 | nucleus_enter(); |
||
419 | |||
420 | ctx.v = pc_save.v = mmu_primary_context_read(); |
||
1860 | jermar | 421 | ctx.context = asid; |
1865 | jermar | 422 | mmu_primary_context_write(ctx.v); |
1860 | jermar | 423 | |
1865 | jermar | 424 | itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_PRIMARY, 0); |
425 | dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_PRIMARY, 0); |
||
1860 | jermar | 426 | |
1865 | jermar | 427 | mmu_primary_context_write(pc_save.v); |
428 | |||
429 | nucleus_leave(); |
||
617 | jermar | 430 | } |
431 | |||
727 | jermar | 432 | /** Invalidate all ITLB and DTLB entries for specified page range in specified address space. |
617 | jermar | 433 | * |
434 | * @param asid Address Space ID. |
||
727 | jermar | 435 | * @param page First page which to sweep out from ITLB and DTLB. |
436 | * @param cnt Number of ITLB and DTLB entries to invalidate. |
||
617 | jermar | 437 | */ |
1780 | jermar | 438 | void tlb_invalidate_pages(asid_t asid, uintptr_t page, count_t cnt) |
617 | jermar | 439 | { |
727 | jermar | 440 | int i; |
1865 | jermar | 441 | tlb_context_reg_t pc_save, ctx; |
727 | jermar | 442 | |
1865 | jermar | 443 | /* switch to nucleus because we are mapped by the primary context */ |
444 | nucleus_enter(); |
||
445 | |||
446 | ctx.v = pc_save.v = mmu_primary_context_read(); |
||
1860 | jermar | 447 | ctx.context = asid; |
1865 | jermar | 448 | mmu_primary_context_write(ctx.v); |
1860 | jermar | 449 | |
727 | jermar | 450 | for (i = 0; i < cnt; i++) { |
1865 | jermar | 451 | itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_PRIMARY, page + i * PAGE_SIZE); |
452 | dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_PRIMARY, page + i * PAGE_SIZE); |
||
727 | jermar | 453 | } |
1860 | jermar | 454 | |
1865 | jermar | 455 | mmu_primary_context_write(pc_save.v); |
456 | |||
457 | nucleus_leave(); |
||
617 | jermar | 458 | } |
1702 | cejka | 459 | |
1792 | jermar | 460 | /** @} |
1702 | cejka | 461 | */ |