source: mainline/kernel/arch/sparc64/src/mm/tlb.c@ 7905360

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7905360 was 6c441cf8, checked in by Martin Decky <martin@…>, 18 years ago

code cleanup (mostly signed/unsigned)
allow extra compiler warnings

  • Property mode set to 100644
File size: 13.2 KB
RevLine 
[0d04024]1/*
[df4ed85]2 * Copyright (c) 2005 Jakub Jermar
[0d04024]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
[10b890b]29/** @addtogroup sparc64mm
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[0d04024]35#include <arch/mm/tlb.h>
36#include <mm/tlb.h>
[f47fd19]37#include <mm/as.h>
38#include <mm/asid.h>
[0cfc4d38]39#include <arch/mm/frame.h>
40#include <arch/mm/page.h>
41#include <arch/mm/mmu.h>
[f47fd19]42#include <arch/interrupt.h>
[e2bf639]43#include <interrupt.h>
[f47fd19]44#include <arch.h>
[0d04024]45#include <print.h>
[dbb6886]46#include <arch/types.h>
[0cfc4d38]47#include <config.h>
[49b6d32]48#include <arch/trap/trap.h>
[7bb6b06]49#include <arch/trap/exception.h>
[008029d]50#include <panic.h>
[b6fba84]51#include <arch/asm.h>
[02f441c0]52
[29b2bbf]53#ifdef CONFIG_TSB
54#include <arch/mm/tsb.h>
55#endif
56
[2057572]57static void dtlb_pte_copy(pte_t *t, index_t index, bool ro);
58static void itlb_pte_copy(pte_t *t, index_t index);
59static void do_fast_instruction_access_mmu_miss_fault(istate_t *istate,
60 const char *str);
[771cd22]61static void do_fast_data_access_mmu_miss_fault(istate_t *istate,
[2057572]62 tlb_tag_access_reg_t tag, const char *str);
[771cd22]63static void do_fast_data_access_protection_fault(istate_t *istate,
[2057572]64 tlb_tag_access_reg_t tag, const char *str);
[f47fd19]65
[b6fba84]66char *context_encoding[] = {
67 "Primary",
68 "Secondary",
69 "Nucleus",
70 "Reserved"
71};
[0d04024]72
73void tlb_arch_init(void)
74{
[c6e314a]75 /*
[c23baab]76 * Invalidate all non-locked DTLB and ITLB entries.
[c6e314a]77 */
[c23baab]78 tlb_invalidate_all();
[8cee705]79
80 /*
81 * Clear both SFSRs.
82 */
83 dtlb_sfsr_write(0);
84 itlb_sfsr_write(0);
[97f1691]85}
[b6fba84]86
[97f1691]87/** Insert privileged mapping into DMMU TLB.
88 *
89 * @param page Virtual page address.
90 * @param frame Physical frame address.
91 * @param pagesize Page size.
92 * @param locked True for permanent mappings, false otherwise.
93 * @param cacheable True if the mapping is cacheable, false otherwise.
94 */
[2057572]95void dtlb_insert_mapping(uintptr_t page, uintptr_t frame, int pagesize,
96 bool locked, bool cacheable)
[97f1691]97{
98 tlb_tag_access_reg_t tag;
99 tlb_data_t data;
100 page_address_t pg;
101 frame_address_t fr;
[b6fba84]102
[97f1691]103 pg.address = page;
104 fr.address = frame;
[02f441c0]105
106 tag.value = ASID_KERNEL;
107 tag.vpn = pg.vpn;
108
109 dtlb_tag_access_write(tag.value);
110
111 data.value = 0;
112 data.v = true;
[97f1691]113 data.size = pagesize;
[02f441c0]114 data.pfn = fr.pfn;
[97f1691]115 data.l = locked;
116 data.cp = cacheable;
[92778f2]117#ifdef CONFIG_VIRT_IDX_DCACHE
[97f1691]118 data.cv = cacheable;
[92778f2]119#endif /* CONFIG_VIRT_IDX_DCACHE */
[02f441c0]120 data.p = true;
121 data.w = true;
[d681c17]122 data.g = false;
[02f441c0]123
124 dtlb_data_in_write(data.value);
[0d04024]125}
126
[a7961271]127/** Copy PTE to TLB.
128 *
[2057572]129 * @param t Page Table Entry to be copied.
130 * @param index Zero if lower 8K-subpage, one if higher 8K-subpage.
131 * @param ro If true, the entry will be created read-only, regardless of its
132 * w field.
[a7961271]133 */
[2057572]134void dtlb_pte_copy(pte_t *t, index_t index, bool ro)
[a7961271]135{
136 tlb_tag_access_reg_t tag;
137 tlb_data_t data;
138 page_address_t pg;
139 frame_address_t fr;
140
[2057572]141 pg.address = t->page + (index << MMU_PAGE_WIDTH);
142 fr.address = t->frame + (index << MMU_PAGE_WIDTH);
[a7961271]143
144 tag.value = 0;
145 tag.context = t->as->asid;
146 tag.vpn = pg.vpn;
[2057572]147
[a7961271]148 dtlb_tag_access_write(tag.value);
[2057572]149
[a7961271]150 data.value = 0;
151 data.v = true;
152 data.size = PAGESIZE_8K;
153 data.pfn = fr.pfn;
154 data.l = false;
155 data.cp = t->c;
[92778f2]156#ifdef CONFIG_VIRT_IDX_DCACHE
[a7961271]157 data.cv = t->c;
[92778f2]158#endif /* CONFIG_VIRT_IDX_DCACHE */
[cfa70add]159 data.p = t->k; /* p like privileged */
[a7961271]160 data.w = ro ? false : t->w;
161 data.g = t->g;
[2057572]162
[a7961271]163 dtlb_data_in_write(data.value);
164}
165
[29b2bbf]166/** Copy PTE to ITLB.
167 *
[2057572]168 * @param t Page Table Entry to be copied.
169 * @param index Zero if lower 8K-subpage, one if higher 8K-subpage.
[29b2bbf]170 */
[2057572]171void itlb_pte_copy(pte_t *t, index_t index)
[f47fd19]172{
[a7961271]173 tlb_tag_access_reg_t tag;
174 tlb_data_t data;
175 page_address_t pg;
176 frame_address_t fr;
177
[2057572]178 pg.address = t->page + (index << MMU_PAGE_WIDTH);
179 fr.address = t->frame + (index << MMU_PAGE_WIDTH);
[a7961271]180
181 tag.value = 0;
182 tag.context = t->as->asid;
183 tag.vpn = pg.vpn;
184
185 itlb_tag_access_write(tag.value);
186
187 data.value = 0;
188 data.v = true;
189 data.size = PAGESIZE_8K;
190 data.pfn = fr.pfn;
191 data.l = false;
192 data.cp = t->c;
[cfa70add]193 data.p = t->k; /* p like privileged */
[a7961271]194 data.w = false;
195 data.g = t->g;
196
197 itlb_data_in_write(data.value);
[f47fd19]198}
199
[008029d]200/** ITLB miss handler. */
[36f19c0]201void fast_instruction_access_mmu_miss(unative_t unused, istate_t *istate)
[008029d]202{
[a7961271]203 uintptr_t va = ALIGN_DOWN(istate->tpc, PAGE_SIZE);
[2057572]204 index_t index = (istate->tpc >> MMU_PAGE_WIDTH) % MMU_PAGES_PER_PAGE;
[a7961271]205 pte_t *t;
206
207 page_table_lock(AS, true);
208 t = page_mapping_find(AS, va);
209 if (t && PTE_EXECUTABLE(t)) {
210 /*
211 * The mapping was found in the software page hash table.
212 * Insert it into ITLB.
213 */
214 t->a = true;
[2057572]215 itlb_pte_copy(t, index);
[29b2bbf]216#ifdef CONFIG_TSB
[2057572]217 itsb_pte_copy(t, index);
[29b2bbf]218#endif
[a7961271]219 page_table_unlock(AS, true);
220 } else {
221 /*
[771cd22]222 * Forward the page fault to the address space page fault
223 * handler.
[a7961271]224 */
225 page_table_unlock(AS, true);
226 if (as_page_fault(va, PF_ACCESS_EXEC, istate) == AS_PF_FAULT) {
[771cd22]227 do_fast_instruction_access_mmu_miss_fault(istate,
[3ee8a075]228 __func__);
[a7961271]229 }
230 }
[008029d]231}
232
[f47fd19]233/** DTLB miss handler.
234 *
[771cd22]235 * Note that some faults (e.g. kernel faults) were already resolved by the
236 * low-level, assembly language part of the fast_data_access_mmu_miss handler.
[36f19c0]237 *
238 * @param tag Content of the TLB Tag Access register as it existed when the
239 * trap happened. This is to prevent confusion created by clobbered
240 * Tag Access register during a nested DTLB miss.
241 * @param istate Interrupted state saved on the stack.
[f47fd19]242 */
[36f19c0]243void fast_data_access_mmu_miss(tlb_tag_access_reg_t tag, istate_t *istate)
[008029d]244{
[f47fd19]245 uintptr_t va;
[2057572]246 index_t index;
[f47fd19]247 pte_t *t;
[7cb53f62]248
[2057572]249 va = ALIGN_DOWN((uint64_t) tag.vpn << MMU_PAGE_WIDTH, PAGE_SIZE);
250 index = tag.vpn % MMU_PAGES_PER_PAGE;
[fd85ae5]251
[f47fd19]252 if (tag.context == ASID_KERNEL) {
253 if (!tag.vpn) {
254 /* NULL access in kernel */
[771cd22]255 do_fast_data_access_mmu_miss_fault(istate, tag,
[3ee8a075]256 __func__);
[f47fd19]257 }
[771cd22]258 do_fast_data_access_mmu_miss_fault(istate, tag, "Unexpected "
[2057572]259 "kernel page fault.");
[68656282]260 }
261
[f47fd19]262 page_table_lock(AS, true);
263 t = page_mapping_find(AS, va);
264 if (t) {
265 /*
266 * The mapping was found in the software page hash table.
267 * Insert it into DTLB.
268 */
[a7961271]269 t->a = true;
[2057572]270 dtlb_pte_copy(t, index, true);
[29b2bbf]271#ifdef CONFIG_TSB
[2057572]272 dtsb_pte_copy(t, index, true);
[29b2bbf]273#endif
[f47fd19]274 page_table_unlock(AS, true);
275 } else {
276 /*
[2057572]277 * Forward the page fault to the address space page fault
278 * handler.
[f47fd19]279 */
280 page_table_unlock(AS, true);
281 if (as_page_fault(va, PF_ACCESS_READ, istate) == AS_PF_FAULT) {
[771cd22]282 do_fast_data_access_mmu_miss_fault(istate, tag,
[3ee8a075]283 __func__);
[f47fd19]284 }
285 }
[008029d]286}
287
[36f19c0]288/** DTLB protection fault handler.
289 *
290 * @param tag Content of the TLB Tag Access register as it existed when the
291 * trap happened. This is to prevent confusion created by clobbered
292 * Tag Access register during a nested DTLB miss.
293 * @param istate Interrupted state saved on the stack.
294 */
295void fast_data_access_protection(tlb_tag_access_reg_t tag, istate_t *istate)
[008029d]296{
[e0b241f]297 uintptr_t va;
[2057572]298 index_t index;
[e0b241f]299 pte_t *t;
300
[2057572]301 va = ALIGN_DOWN((uint64_t) tag.vpn << MMU_PAGE_WIDTH, PAGE_SIZE);
302 index = tag.vpn % MMU_PAGES_PER_PAGE; /* 16K-page emulation */
[e0b241f]303
304 page_table_lock(AS, true);
305 t = page_mapping_find(AS, va);
306 if (t && PTE_WRITABLE(t)) {
307 /*
[771cd22]308 * The mapping was found in the software page hash table and is
309 * writable. Demap the old mapping and insert an updated mapping
310 * into DTLB.
[e0b241f]311 */
312 t->a = true;
313 t->d = true;
[2057572]314 dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_SECONDARY,
315 va + index * MMU_PAGE_SIZE);
316 dtlb_pte_copy(t, index, false);
[29b2bbf]317#ifdef CONFIG_TSB
[2057572]318 dtsb_pte_copy(t, index, false);
[29b2bbf]319#endif
[e0b241f]320 page_table_unlock(AS, true);
321 } else {
322 /*
[771cd22]323 * Forward the page fault to the address space page fault
324 * handler.
[e0b241f]325 */
326 page_table_unlock(AS, true);
327 if (as_page_fault(va, PF_ACCESS_WRITE, istate) == AS_PF_FAULT) {
[771cd22]328 do_fast_data_access_protection_fault(istate, tag,
[3ee8a075]329 __func__);
[e0b241f]330 }
331 }
[008029d]332}
333
[0d04024]334/** Print contents of both TLBs. */
335void tlb_print(void)
336{
337 int i;
338 tlb_data_t d;
339 tlb_tag_read_reg_t t;
340
341 printf("I-TLB contents:\n");
342 for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
343 d.value = itlb_data_access_read(i);
[c52ed6b]344 t.value = itlb_tag_read_read(i);
[8dbc18c]345
[771cd22]346 printf("%d: vpn=%#llx, context=%d, v=%d, size=%d, nfo=%d, "
[2057572]347 "ie=%d, soft2=%#x, diag=%#x, pfn=%#x, soft=%#x, l=%d, "
348 "cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n", i, t.vpn,
349 t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag,
350 d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g);
[0d04024]351 }
352
353 printf("D-TLB contents:\n");
354 for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
355 d.value = dtlb_data_access_read(i);
[c52ed6b]356 t.value = dtlb_tag_read_read(i);
[0d04024]357
[771cd22]358 printf("%d: vpn=%#llx, context=%d, v=%d, size=%d, nfo=%d, "
[2057572]359 "ie=%d, soft2=%#x, diag=%#x, pfn=%#x, soft=%#x, l=%d, "
360 "cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n", i, t.vpn,
361 t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag,
362 d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g);
[0d04024]363 }
364
365}
[dbb6886]366
[2057572]367void do_fast_instruction_access_mmu_miss_fault(istate_t *istate,
368 const char *str)
[a7961271]369{
[e2bf639]370 fault_if_from_uspace(istate, "%s\n", str);
[7bb6b06]371 dump_istate(istate);
[a7961271]372 panic("%s\n", str);
373}
374
[2057572]375void do_fast_data_access_mmu_miss_fault(istate_t *istate,
376 tlb_tag_access_reg_t tag, const char *str)
[f47fd19]377{
378 uintptr_t va;
379
[2057572]380 va = tag.vpn << MMU_PAGE_WIDTH;
[36f19c0]381 if (tag.context) {
382 fault_if_from_uspace(istate, "%s, Page=%p (ASID=%d)\n", str, va,
383 tag.context);
384 }
[7bb6b06]385 dump_istate(istate);
[f47fd19]386 printf("Faulting page: %p, ASID=%d\n", va, tag.context);
387 panic("%s\n", str);
388}
389
[2057572]390void do_fast_data_access_protection_fault(istate_t *istate,
391 tlb_tag_access_reg_t tag, const char *str)
[e0b241f]392{
393 uintptr_t va;
394
[2057572]395 va = tag.vpn << MMU_PAGE_WIDTH;
[e0b241f]396
[36f19c0]397 if (tag.context) {
398 fault_if_from_uspace(istate, "%s, Page=%p (ASID=%d)\n", str, va,
399 tag.context);
400 }
[e0b241f]401 printf("Faulting page: %p, ASID=%d\n", va, tag.context);
[7bb6b06]402 dump_istate(istate);
[e0b241f]403 panic("%s\n", str);
404}
405
[8cee705]406void dump_sfsr_and_sfar(void)
407{
408 tlb_sfsr_reg_t sfsr;
409 uintptr_t sfar;
410
411 sfsr.value = dtlb_sfsr_read();
412 sfar = dtlb_sfar_read();
413
[771cd22]414 printf("DTLB SFSR: asi=%#x, ft=%#x, e=%d, ct=%d, pr=%d, w=%d, ow=%d, "
[2057572]415 "fv=%d\n", sfsr.asi, sfsr.ft, sfsr.e, sfsr.ct, sfsr.pr, sfsr.w,
416 sfsr.ow, sfsr.fv);
[8cee705]417 printf("DTLB SFAR: address=%p\n", sfar);
418
419 dtlb_sfsr_write(0);
420}
421
[dbb6886]422/** Invalidate all unlocked ITLB and DTLB entries. */
423void tlb_invalidate_all(void)
424{
425 int i;
426 tlb_data_t d;
427 tlb_tag_read_reg_t t;
428
[8dbc18c]429 /*
430 * Walk all ITLB and DTLB entries and remove all unlocked mappings.
431 *
432 * The kernel doesn't use global mappings so any locked global mappings
433 * found must have been created by someone else. Their only purpose now
434 * is to collide with proper mappings. Invalidate immediately. It should
435 * be safe to invalidate them as late as now.
436 */
437
[dbb6886]438 for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
439 d.value = itlb_data_access_read(i);
[8dbc18c]440 if (!d.l || d.g) {
[dbb6886]441 t.value = itlb_tag_read_read(i);
442 d.v = false;
443 itlb_tag_access_write(t.value);
444 itlb_data_access_write(i, d.value);
445 }
446 }
447
448 for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
449 d.value = dtlb_data_access_read(i);
[8dbc18c]450 if (!d.l || d.g) {
[dbb6886]451 t.value = dtlb_tag_read_read(i);
452 d.v = false;
453 dtlb_tag_access_write(t.value);
454 dtlb_data_access_write(i, d.value);
455 }
456 }
457
458}
459
[771cd22]460/** Invalidate all ITLB and DTLB entries that belong to specified ASID
461 * (Context).
[dbb6886]462 *
463 * @param asid Address Space ID.
464 */
465void tlb_invalidate_asid(asid_t asid)
466{
[fd85ae5]467 tlb_context_reg_t pc_save, ctx;
[ed166f7]468
[fd85ae5]469 /* switch to nucleus because we are mapped by the primary context */
470 nucleus_enter();
471
472 ctx.v = pc_save.v = mmu_primary_context_read();
[ed166f7]473 ctx.context = asid;
[fd85ae5]474 mmu_primary_context_write(ctx.v);
475
476 itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_PRIMARY, 0);
477 dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_PRIMARY, 0);
[ed166f7]478
[fd85ae5]479 mmu_primary_context_write(pc_save.v);
[ed166f7]480
[fd85ae5]481 nucleus_leave();
[dbb6886]482}
483
[771cd22]484/** Invalidate all ITLB and DTLB entries for specified page range in specified
485 * address space.
[dbb6886]486 *
487 * @param asid Address Space ID.
[4512d7e]488 * @param page First page which to sweep out from ITLB and DTLB.
489 * @param cnt Number of ITLB and DTLB entries to invalidate.
[dbb6886]490 */
[7f1c620]491void tlb_invalidate_pages(asid_t asid, uintptr_t page, count_t cnt)
[dbb6886]492{
[6c441cf8]493 unsigned int i;
[fd85ae5]494 tlb_context_reg_t pc_save, ctx;
[ed166f7]495
[fd85ae5]496 /* switch to nucleus because we are mapped by the primary context */
497 nucleus_enter();
498
499 ctx.v = pc_save.v = mmu_primary_context_read();
[ed166f7]500 ctx.context = asid;
[fd85ae5]501 mmu_primary_context_write(ctx.v);
[4512d7e]502
[2057572]503 for (i = 0; i < cnt * MMU_PAGES_PER_PAGE; i++) {
[454f1da]504 itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_PRIMARY,
[2057572]505 page + i * MMU_PAGE_SIZE);
[454f1da]506 dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_PRIMARY,
[2057572]507 page + i * MMU_PAGE_SIZE);
[4512d7e]508 }
[ed166f7]509
[fd85ae5]510 mmu_primary_context_write(pc_save.v);
511
512 nucleus_leave();
[dbb6886]513}
[b45c443]514
[10b890b]515/** @}
[b45c443]516 */
Note: See TracBrowser for help on using the repository browser.