2
* Copyright (c) 2005 Jakub Jermar
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
29
/** @addtogroup sparc64mm
35
#include <arch/mm/tlb.h>
39
#include <arch/mm/frame.h>
40
#include <arch/mm/page.h>
41
#include <arch/mm/mmu.h>
42
#include <arch/interrupt.h>
43
#include <interrupt.h>
46
#include <arch/types.h>
48
#include <arch/trap/trap.h>
49
#include <arch/trap/exception.h>
54
#include <arch/mm/tsb.h>
57
static void dtlb_pte_copy(pte_t *, size_t, bool);
58
static void itlb_pte_copy(pte_t *, size_t);
59
static void do_fast_instruction_access_mmu_miss_fault(istate_t *, const char *);
60
static void do_fast_data_access_mmu_miss_fault(istate_t *, tlb_tag_access_reg_t,
62
static void do_fast_data_access_protection_fault(istate_t *,
63
tlb_tag_access_reg_t, const char *);
65
char *context_encoding[] = {
72
void tlb_arch_init(void)
75
* Invalidate all non-locked DTLB and ITLB entries.
86
/** Insert privileged mapping into DMMU TLB.
88
* @param page Virtual page address.
89
* @param frame Physical frame address.
90
* @param pagesize Page size.
91
* @param locked True for permanent mappings, false otherwise.
92
* @param cacheable True if the mapping is cacheable, false otherwise.
94
void dtlb_insert_mapping(uintptr_t page, uintptr_t frame, int pagesize,
95
bool locked, bool cacheable)
97
tlb_tag_access_reg_t tag;
105
tag.context = ASID_KERNEL;
108
dtlb_tag_access_write(tag.value);
112
data.size = pagesize;
116
#ifdef CONFIG_VIRT_IDX_DCACHE
118
#endif /* CONFIG_VIRT_IDX_DCACHE */
123
dtlb_data_in_write(data.value);
128
* @param t Page Table Entry to be copied.
129
* @param index Zero if lower 8K-subpage, one if higher 8K-subpage.
130
* @param ro If true, the entry will be created read-only, regardless
133
void dtlb_pte_copy(pte_t *t, size_t index, bool ro)
135
tlb_tag_access_reg_t tag;
140
pg.address = t->page + (index << MMU_PAGE_WIDTH);
141
fr.address = t->frame + (index << MMU_PAGE_WIDTH);
144
tag.context = t->as->asid;
147
dtlb_tag_access_write(tag.value);
151
data.size = PAGESIZE_8K;
155
#ifdef CONFIG_VIRT_IDX_DCACHE
157
#endif /* CONFIG_VIRT_IDX_DCACHE */
158
data.p = t->k; /* p like privileged */
159
data.w = ro ? false : t->w;
162
dtlb_data_in_write(data.value);
165
/** Copy PTE to ITLB.
167
* @param t Page Table Entry to be copied.
168
* @param index Zero if lower 8K-subpage, one if higher 8K-subpage.
170
void itlb_pte_copy(pte_t *t, size_t index)
172
tlb_tag_access_reg_t tag;
177
pg.address = t->page + (index << MMU_PAGE_WIDTH);
178
fr.address = t->frame + (index << MMU_PAGE_WIDTH);
181
tag.context = t->as->asid;
184
itlb_tag_access_write(tag.value);
188
data.size = PAGESIZE_8K;
192
data.p = t->k; /* p like privileged */
196
itlb_data_in_write(data.value);
199
/** ITLB miss handler. */
200
void fast_instruction_access_mmu_miss(unative_t unused, istate_t *istate)
202
uintptr_t page_16k = ALIGN_DOWN(istate->tpc, PAGE_SIZE);
203
size_t index = (istate->tpc >> MMU_PAGE_WIDTH) % MMU_PAGES_PER_PAGE;
206
page_table_lock(AS, true);
207
t = page_mapping_find(AS, page_16k);
208
if (t && PTE_EXECUTABLE(t)) {
210
* The mapping was found in the software page hash table.
211
* Insert it into ITLB.
214
itlb_pte_copy(t, index);
216
itsb_pte_copy(t, index);
218
page_table_unlock(AS, true);
221
* Forward the page fault to the address space page fault
224
page_table_unlock(AS, true);
225
if (as_page_fault(page_16k, PF_ACCESS_EXEC, istate) ==
227
do_fast_instruction_access_mmu_miss_fault(istate,
233
/** DTLB miss handler.
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.
238
* @param tag Content of the TLB Tag Access register as it existed
239
* when the trap happened. This is to prevent confusion
240
* created by clobbered Tag Access register during a nested
242
* @param istate Interrupted state saved on the stack.
244
void fast_data_access_mmu_miss(tlb_tag_access_reg_t tag, istate_t *istate)
251
page_8k = (uint64_t) tag.vpn << MMU_PAGE_WIDTH;
252
page_16k = ALIGN_DOWN(page_8k, PAGE_SIZE);
253
index = tag.vpn % MMU_PAGES_PER_PAGE;
255
if (tag.context == ASID_KERNEL) {
257
/* NULL access in kernel */
258
do_fast_data_access_mmu_miss_fault(istate, tag,
260
} else if (page_8k >= end_of_identity) {
262
* The kernel is accessing the I/O space.
263
* We still do identity mapping for I/O,
264
* but without caching.
266
dtlb_insert_mapping(page_8k, KA2PA(page_8k),
267
PAGESIZE_8K, false, false);
270
do_fast_data_access_mmu_miss_fault(istate, tag, "Unexpected "
271
"kernel page fault.");
274
page_table_lock(AS, true);
275
t = page_mapping_find(AS, page_16k);
278
* The mapping was found in the software page hash table.
279
* Insert it into DTLB.
282
dtlb_pte_copy(t, index, true);
284
dtsb_pte_copy(t, index, true);
286
page_table_unlock(AS, true);
289
* Forward the page fault to the address space page fault
292
page_table_unlock(AS, true);
293
if (as_page_fault(page_16k, PF_ACCESS_READ, istate) ==
295
do_fast_data_access_mmu_miss_fault(istate, tag,
301
/** DTLB protection fault handler.
303
* @param tag Content of the TLB Tag Access register as it existed
304
* when the trap happened. This is to prevent confusion
305
* created by clobbered Tag Access register during a nested
307
* @param istate Interrupted state saved on the stack.
309
void fast_data_access_protection(tlb_tag_access_reg_t tag, istate_t *istate)
315
page_16k = ALIGN_DOWN((uint64_t) tag.vpn << MMU_PAGE_WIDTH, PAGE_SIZE);
316
index = tag.vpn % MMU_PAGES_PER_PAGE; /* 16K-page emulation */
318
page_table_lock(AS, true);
319
t = page_mapping_find(AS, page_16k);
320
if (t && PTE_WRITABLE(t)) {
322
* The mapping was found in the software page hash table and is
323
* writable. Demap the old mapping and insert an updated mapping
328
dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_SECONDARY,
329
page_16k + index * MMU_PAGE_SIZE);
330
dtlb_pte_copy(t, index, false);
332
dtsb_pte_copy(t, index, false);
334
page_table_unlock(AS, true);
337
* Forward the page fault to the address space page fault
340
page_table_unlock(AS, true);
341
if (as_page_fault(page_16k, PF_ACCESS_WRITE, istate) ==
343
do_fast_data_access_protection_fault(istate, tag,
349
/** Print TLB entry (for debugging purposes).
351
* The diag field has been left out in order to make this function more generic
352
* (there is no diag field in US3 architeture).
354
* @param i TLB entry number
355
* @param t TLB entry tag
356
* @param d TLB entry data
358
static void print_tlb_entry(int i, tlb_tag_read_reg_t t, tlb_data_t d)
360
printf("%d: vpn=%#llx, context=%d, v=%d, size=%d, nfo=%d, "
361
"ie=%d, soft2=%#x, pfn=%#x, soft=%#x, l=%d, "
362
"cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n", i, t.vpn,
363
t.context, d.v, d.size, d.nfo, d.ie, d.soft2,
364
d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g);
369
/** Print contents of both TLBs. */
374
tlb_tag_read_reg_t t;
376
printf("I-TLB contents:\n");
377
for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
378
d.value = itlb_data_access_read(i);
379
t.value = itlb_tag_read_read(i);
380
print_tlb_entry(i, t, d);
383
printf("D-TLB contents:\n");
384
for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
385
d.value = dtlb_data_access_read(i);
386
t.value = dtlb_tag_read_read(i);
387
print_tlb_entry(i, t, d);
393
/** Print contents of all TLBs. */
398
tlb_tag_read_reg_t t;
400
printf("TLB_ISMALL contents:\n");
401
for (i = 0; i < tlb_ismall_size(); i++) {
402
d.value = dtlb_data_access_read(TLB_ISMALL, i);
403
t.value = dtlb_tag_read_read(TLB_ISMALL, i);
404
print_tlb_entry(i, t, d);
407
printf("TLB_IBIG contents:\n");
408
for (i = 0; i < tlb_ibig_size(); i++) {
409
d.value = dtlb_data_access_read(TLB_IBIG, i);
410
t.value = dtlb_tag_read_read(TLB_IBIG, i);
411
print_tlb_entry(i, t, d);
414
printf("TLB_DSMALL contents:\n");
415
for (i = 0; i < tlb_dsmall_size(); i++) {
416
d.value = dtlb_data_access_read(TLB_DSMALL, i);
417
t.value = dtlb_tag_read_read(TLB_DSMALL, i);
418
print_tlb_entry(i, t, d);
421
printf("TLB_DBIG_1 contents:\n");
422
for (i = 0; i < tlb_dbig_size(); i++) {
423
d.value = dtlb_data_access_read(TLB_DBIG_0, i);
424
t.value = dtlb_tag_read_read(TLB_DBIG_0, i);
425
print_tlb_entry(i, t, d);
428
printf("TLB_DBIG_2 contents:\n");
429
for (i = 0; i < tlb_dbig_size(); i++) {
430
d.value = dtlb_data_access_read(TLB_DBIG_1, i);
431
t.value = dtlb_tag_read_read(TLB_DBIG_1, i);
432
print_tlb_entry(i, t, d);
438
void do_fast_instruction_access_mmu_miss_fault(istate_t *istate,
441
fault_if_from_uspace(istate, "%s.", str);
446
void do_fast_data_access_mmu_miss_fault(istate_t *istate,
447
tlb_tag_access_reg_t tag, const char *str)
451
va = tag.vpn << MMU_PAGE_WIDTH;
453
fault_if_from_uspace(istate, "%s, Page=%p (ASID=%d).", str, va,
457
printf("Faulting page: %p, ASID=%d.\n", va, tag.context);
461
void do_fast_data_access_protection_fault(istate_t *istate,
462
tlb_tag_access_reg_t tag, const char *str)
466
va = tag.vpn << MMU_PAGE_WIDTH;
469
fault_if_from_uspace(istate, "%s, Page=%p (ASID=%d).", str, va,
472
printf("Faulting page: %p, ASID=%d\n", va, tag.context);
477
void dump_sfsr_and_sfar(void)
482
sfsr.value = dtlb_sfsr_read();
483
sfar = dtlb_sfar_read();
486
printf("DTLB SFSR: asi=%#x, ft=%#x, e=%d, ct=%d, pr=%d, w=%d, ow=%d, "
487
"fv=%d\n", sfsr.asi, sfsr.ft, sfsr.e, sfsr.ct, sfsr.pr, sfsr.w,
490
printf("DTLB SFSR: nf=%d, asi=%#x, tm=%d, ft=%#x, e=%d, ct=%d, pr=%d, "
491
"w=%d, ow=%d, fv=%d\n", sfsr.nf, sfsr.asi, sfsr.tm, sfsr.ft,
492
sfsr.e, sfsr.ct, sfsr.pr, sfsr.w, sfsr.ow, sfsr.fv);
495
printf("DTLB SFAR: address=%p\n", sfar);
501
/** Invalidate all unlocked ITLB and DTLB entries. */
502
void tlb_invalidate_all(void)
507
* Walk all ITLB and DTLB entries and remove all unlocked mappings.
509
* The kernel doesn't use global mappings so any locked global mappings
510
* found must have been created by someone else. Their only purpose now
511
* is to collide with proper mappings. Invalidate immediately. It should
512
* be safe to invalidate them as late as now.
516
tlb_tag_read_reg_t t;
518
for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
519
d.value = itlb_data_access_read(i);
521
t.value = itlb_tag_read_read(i);
523
itlb_tag_access_write(t.value);
524
itlb_data_access_write(i, d.value);
528
for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
529
d.value = dtlb_data_access_read(i);
531
t.value = dtlb_tag_read_read(i);
533
dtlb_tag_access_write(t.value);
534
dtlb_data_access_write(i, d.value);
542
/** Invalidate all unlocked ITLB and DTLB entries. */
543
void tlb_invalidate_all(void)
545
itlb_demap(TLB_DEMAP_ALL, 0, 0);
546
dtlb_demap(TLB_DEMAP_ALL, 0, 0);
551
/** Invalidate all ITLB and DTLB entries that belong to specified ASID
554
* @param asid Address Space ID.
556
void tlb_invalidate_asid(asid_t asid)
558
tlb_context_reg_t pc_save, ctx;
560
/* switch to nucleus because we are mapped by the primary context */
563
ctx.v = pc_save.v = mmu_primary_context_read();
565
mmu_primary_context_write(ctx.v);
567
itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_PRIMARY, 0);
568
dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_PRIMARY, 0);
570
mmu_primary_context_write(pc_save.v);
575
/** Invalidate all ITLB and DTLB entries for specified page range in specified
578
* @param asid Address Space ID.
579
* @param page First page which to sweep out from ITLB and DTLB.
580
* @param cnt Number of ITLB and DTLB entries to invalidate.
582
void tlb_invalidate_pages(asid_t asid, uintptr_t page, size_t cnt)
585
tlb_context_reg_t pc_save, ctx;
587
/* switch to nucleus because we are mapped by the primary context */
590
ctx.v = pc_save.v = mmu_primary_context_read();
592
mmu_primary_context_write(ctx.v);
594
for (i = 0; i < cnt * MMU_PAGES_PER_PAGE; i++) {
595
itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_PRIMARY,
596
page + i * MMU_PAGE_SIZE);
597
dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_PRIMARY,
598
page + i * MMU_PAGE_SIZE);
601
mmu_primary_context_write(pc_save.v);