source: mainline/kernel/arch/mips32/src/mm/tlb.c@ 2bdf8313

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2bdf8313 was 2bdf8313, checked in by Jakub Jermar <jakub@…>, 14 years ago

Do not lock page tables on mips32 during page fault.

  • Property mode set to 100644
File size: 11.9 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2003-2004 Jakub Jermar
[f761f1eb]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
[7f341820]29/** @addtogroup mips32mm
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[f761f1eb]35#include <arch/mm/tlb.h>
[4512d7e]36#include <mm/asid.h>
[f761f1eb]37#include <mm/tlb.h>
[1084a784]38#include <mm/page.h>
[20d50a1]39#include <mm/as.h>
[f761f1eb]40#include <arch/cp0.h>
41#include <panic.h>
42#include <arch.h>
[7f341820]43#include <synch/mutex.h>
[1084a784]44#include <print.h>
[cc205f1]45#include <debug.h>
[2d01bbd]46#include <align.h>
[874621f]47#include <interrupt.h>
[e2b762ec]48#include <symtab.h>
49
[91befde0]50static void tlb_refill_fail(istate_t *);
51static void tlb_invalid_fail(istate_t *);
52static void tlb_modified_fail(istate_t *);
[1084a784]53
[91befde0]54static pte_t *find_mapping_and_check(uintptr_t, int, istate_t *, int *);
[8c5e6c7]55
[91befde0]56/** Initialize TLB.
[1084a784]57 *
58 * Invalidate all entries and mark wired entries.
59 */
[b00fdde]60void tlb_arch_init(void)
[ce031f0]61{
[dd14cced]62 int i;
63
[ce031f0]64 cp0_pagemask_write(TLB_PAGE_MASK_16K);
[dd14cced]65 cp0_entry_hi_write(0);
66 cp0_entry_lo0_write(0);
67 cp0_entry_lo1_write(0);
[ce031f0]68
[dd14cced]69 /* Clear and initialize TLB. */
70
71 for (i = 0; i < TLB_ENTRY_COUNT; i++) {
72 cp0_index_write(i);
73 tlbwi();
74 }
[a98d2ec]75
[ce031f0]76 /*
77 * The kernel is going to make use of some wired
[1084a784]78 * entries (e.g. mapping kernel stacks in kseg3).
[ce031f0]79 */
80 cp0_wired_write(TLB_WIRED);
81}
82
[91befde0]83/** Process TLB Refill Exception.
[1084a784]84 *
[91befde0]85 * @param istate Interrupted register context.
[1084a784]86 */
[25d7709]87void tlb_refill(istate_t *istate)
[1084a784]88{
[cc205f1]89 entry_lo_t lo;
[2299914]90 entry_hi_t hi;
91 asid_t asid;
[7f1c620]92 uintptr_t badvaddr;
[1084a784]93 pte_t *pte;
[e3c762cd]94 int pfrc;
[7f341820]95
[1084a784]96 badvaddr = cp0_badvaddr_read();
[7f341820]97
98 mutex_lock(&AS->lock);
[2299914]99 asid = AS->asid;
[7f341820]100 mutex_unlock(&AS->lock);
101
[567807b1]102 pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate, &pfrc);
[e3c762cd]103 if (!pte) {
104 switch (pfrc) {
105 case AS_PF_FAULT:
106 goto fail;
107 break;
108 case AS_PF_DEFER:
109 /*
110 * The page fault came during copy_from_uspace()
111 * or copy_to_uspace().
112 */
113 return;
114 default:
[f651e80]115 panic("Unexpected pfrc (%d).", pfrc);
[e3c762cd]116 }
117 }
[38a1a84]118
[1084a784]119 /*
[38a1a84]120 * Record access to PTE.
[1084a784]121 */
[38a1a84]122 pte->a = 1;
123
[edebc15c]124 tlb_prepare_entry_hi(&hi, asid, badvaddr);
[91befde0]125 tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable,
126 pte->pfn);
[1084a784]127
128 /*
129 * New entry is to be inserted into TLB
130 */
[8c5e6c7]131 cp0_entry_hi_write(hi.value);
[91befde0]132 if ((badvaddr / PAGE_SIZE) % 2 == 0) {
[cc205f1]133 cp0_entry_lo0_write(lo.value);
[1084a784]134 cp0_entry_lo1_write(0);
135 }
136 else {
137 cp0_entry_lo0_write(0);
[cc205f1]138 cp0_entry_lo1_write(lo.value);
[1084a784]139 }
[0bd4f56d]140 cp0_pagemask_write(TLB_PAGE_MASK_16K);
[1084a784]141 tlbwr();
142
143 return;
144
145fail:
[25d7709]146 tlb_refill_fail(istate);
[1084a784]147}
148
[91befde0]149/** Process TLB Invalid Exception.
[38a1a84]150 *
[91befde0]151 * @param istate Interrupted register context.
[38a1a84]152 */
[25d7709]153void tlb_invalid(istate_t *istate)
[1084a784]154{
[cc205f1]155 tlb_index_t index;
[7f1c620]156 uintptr_t badvaddr;
[cc205f1]157 entry_lo_t lo;
[8c5e6c7]158 entry_hi_t hi;
[38a1a84]159 pte_t *pte;
[e3c762cd]160 int pfrc;
[38a1a84]161
162 badvaddr = cp0_badvaddr_read();
163
164 /*
165 * Locate the faulting entry in TLB.
166 */
[8c5e6c7]167 hi.value = cp0_entry_hi_read();
[edebc15c]168 tlb_prepare_entry_hi(&hi, hi.asid, badvaddr);
[8c5e6c7]169 cp0_entry_hi_write(hi.value);
[38a1a84]170 tlbp();
[cc205f1]171 index.value = cp0_index_read();
[2299914]172
[38a1a84]173 /*
174 * Fail if the entry is not in TLB.
175 */
[cc205f1]176 if (index.p) {
177 printf("TLB entry not found.\n");
[38a1a84]178 goto fail;
[cc205f1]179 }
[38a1a84]180
[567807b1]181 pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate, &pfrc);
[e3c762cd]182 if (!pte) {
183 switch (pfrc) {
184 case AS_PF_FAULT:
185 goto fail;
186 break;
187 case AS_PF_DEFER:
188 /*
189 * The page fault came during copy_from_uspace()
190 * or copy_to_uspace().
191 */
192 return;
193 default:
[f651e80]194 panic("Unexpected pfrc (%d).", pfrc);
[e3c762cd]195 }
196 }
[38a1a84]197
198 /*
199 * Read the faulting TLB entry.
200 */
201 tlbr();
202
203 /*
204 * Record access to PTE.
205 */
206 pte->a = 1;
207
[91befde0]208 tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->d, pte->cacheable,
209 pte->pfn);
[38a1a84]210
211 /*
212 * The entry is to be updated in TLB.
213 */
[91befde0]214 if ((badvaddr / PAGE_SIZE) % 2 == 0)
[cc205f1]215 cp0_entry_lo0_write(lo.value);
[38a1a84]216 else
[cc205f1]217 cp0_entry_lo1_write(lo.value);
[0bd4f56d]218 cp0_pagemask_write(TLB_PAGE_MASK_16K);
[38a1a84]219 tlbwi();
220
221 return;
222
223fail:
[25d7709]224 tlb_invalid_fail(istate);
[1084a784]225}
226
[91befde0]227/** Process TLB Modified Exception.
[38a1a84]228 *
[91befde0]229 * @param istate Interrupted register context.
[38a1a84]230 */
[25d7709]231void tlb_modified(istate_t *istate)
[1084a784]232{
[cc205f1]233 tlb_index_t index;
[7f1c620]234 uintptr_t badvaddr;
[cc205f1]235 entry_lo_t lo;
[8c5e6c7]236 entry_hi_t hi;
[38a1a84]237 pte_t *pte;
[e3c762cd]238 int pfrc;
[38a1a84]239
240 badvaddr = cp0_badvaddr_read();
241
242 /*
243 * Locate the faulting entry in TLB.
244 */
[8c5e6c7]245 hi.value = cp0_entry_hi_read();
[edebc15c]246 tlb_prepare_entry_hi(&hi, hi.asid, badvaddr);
[8c5e6c7]247 cp0_entry_hi_write(hi.value);
[38a1a84]248 tlbp();
[cc205f1]249 index.value = cp0_index_read();
[2299914]250
[38a1a84]251 /*
252 * Fail if the entry is not in TLB.
253 */
[cc205f1]254 if (index.p) {
255 printf("TLB entry not found.\n");
[38a1a84]256 goto fail;
[cc205f1]257 }
[38a1a84]258
[567807b1]259 pte = find_mapping_and_check(badvaddr, PF_ACCESS_WRITE, istate, &pfrc);
[e3c762cd]260 if (!pte) {
261 switch (pfrc) {
262 case AS_PF_FAULT:
263 goto fail;
264 break;
265 case AS_PF_DEFER:
266 /*
267 * The page fault came during copy_from_uspace()
268 * or copy_to_uspace().
269 */
270 return;
271 default:
[f651e80]272 panic("Unexpected pfrc (%d).", pfrc);
[e3c762cd]273 }
274 }
[38a1a84]275
276 /*
277 * Read the faulting TLB entry.
278 */
279 tlbr();
280
281 /*
282 * Record access and write to PTE.
283 */
284 pte->a = 1;
[0882a9a]285 pte->d = 1;
[38a1a84]286
[91befde0]287 tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->w, pte->cacheable,
288 pte->pfn);
[38a1a84]289
290 /*
291 * The entry is to be updated in TLB.
292 */
[91befde0]293 if ((badvaddr / PAGE_SIZE) % 2 == 0)
[cc205f1]294 cp0_entry_lo0_write(lo.value);
[38a1a84]295 else
[cc205f1]296 cp0_entry_lo1_write(lo.value);
[0bd4f56d]297 cp0_pagemask_write(TLB_PAGE_MASK_16K);
[38a1a84]298 tlbwi();
299
300 return;
301
302fail:
[25d7709]303 tlb_modified_fail(istate);
[1084a784]304}
305
[25d7709]306void tlb_refill_fail(istate_t *istate)
[f761f1eb]307{
[ac11ac7]308 uintptr_t va = cp0_badvaddr_read();
[e16e0d59]309
[7e752b2]310 fault_if_from_uspace(istate, "TLB Refill Exception on %p.",
311 (void *) va);
[c15b374]312 panic_memtrap(istate, PF_ACCESS_UNKNOWN, va, "TLB Refill Exception.");
[f761f1eb]313}
314
[1084a784]315
[25d7709]316void tlb_invalid_fail(istate_t *istate)
[f761f1eb]317{
[ac11ac7]318 uintptr_t va = cp0_badvaddr_read();
[a000878c]319
[7e752b2]320 fault_if_from_uspace(istate, "TLB Invalid Exception on %p.",
321 (void *) va);
[c15b374]322 panic_memtrap(istate, PF_ACCESS_UNKNOWN, va, "TLB Invalid Exception.");
[f761f1eb]323}
324
[25d7709]325void tlb_modified_fail(istate_t *istate)
[ce031f0]326{
[ac11ac7]327 uintptr_t va = cp0_badvaddr_read();
[a000878c]328
[7e752b2]329 fault_if_from_uspace(istate, "TLB Modified Exception on %p.",
330 (void *) va);
[ac11ac7]331 panic_memtrap(istate, PF_ACCESS_WRITE, va, "TLB Modified Exception.");
[ce031f0]332}
333
[91befde0]334/** Try to find PTE for faulting address.
[38a1a84]335 *
[91befde0]336 * @param badvaddr Faulting virtual address.
337 * @param access Access mode that caused the fault.
338 * @param istate Pointer to interrupted state.
339 * @param pfrc Pointer to variable where as_page_fault() return code
340 * will be stored.
[38a1a84]341 *
[91befde0]342 * @return PTE on success, NULL otherwise.
[38a1a84]343 */
[91befde0]344pte_t *
345find_mapping_and_check(uintptr_t badvaddr, int access, istate_t *istate,
346 int *pfrc)
[38a1a84]347{
[cc205f1]348 entry_hi_t hi;
[38a1a84]349 pte_t *pte;
350
[cc205f1]351 hi.value = cp0_entry_hi_read();
[38a1a84]352
353 /*
354 * Handler cannot succeed if the ASIDs don't match.
355 */
[20d50a1]356 if (hi.asid != AS->asid) {
357 printf("EntryHi.asid=%d, AS->asid=%d\n", hi.asid, AS->asid);
[38a1a84]358 return NULL;
[cc205f1]359 }
[20d50a1]360
361 /*
362 * Check if the mapping exists in page tables.
363 */
[0ff03f3]364 pte = page_mapping_find(AS, badvaddr, true);
[c867756e]365 if (pte && pte->p && (pte->w || access != PF_ACCESS_WRITE)) {
[20d50a1]366 /*
367 * Mapping found in page tables.
368 * Immediately succeed.
369 */
370 return pte;
371 } else {
[e3c762cd]372 int rc;
373
[20d50a1]374 /*
375 * Mapping not found in page tables.
376 * Resort to higher-level page fault handler.
377 */
[567807b1]378 switch (rc = as_page_fault(badvaddr, access, istate)) {
[e3c762cd]379 case AS_PF_OK:
[20d50a1]380 /*
381 * The higher-level page fault handler succeeded,
382 * The mapping ought to be in place.
383 */
[0ff03f3]384 pte = page_mapping_find(AS, badvaddr, true);
[0882a9a]385 ASSERT(pte && pte->p);
[c867756e]386 ASSERT(pte->w || access != PF_ACCESS_WRITE);
[20d50a1]387 return pte;
[e3c762cd]388 break;
389 case AS_PF_DEFER:
390 *pfrc = AS_PF_DEFER;
391 return NULL;
392 break;
393 case AS_PF_FAULT:
394 *pfrc = AS_PF_FAULT;
[2299914]395 return NULL;
[e3c762cd]396 break;
397 default:
[f651e80]398 panic("Unexpected rc (%d).", rc);
[20d50a1]399 }
[2299914]400
[20d50a1]401 }
[38a1a84]402}
403
[91befde0]404void
405tlb_prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable,
406 uintptr_t pfn)
[38a1a84]407{
[8c5e6c7]408 lo->value = 0;
[38a1a84]409 lo->g = g;
410 lo->v = v;
411 lo->d = d;
[0882a9a]412 lo->c = cacheable ? PAGE_CACHEABLE_EXC_WRITE : PAGE_UNCACHED;
[38a1a84]413 lo->pfn = pfn;
[8c5e6c7]414}
415
[edebc15c]416void tlb_prepare_entry_hi(entry_hi_t *hi, asid_t asid, uintptr_t addr)
[8c5e6c7]417{
[2d01bbd]418 hi->value = ALIGN_DOWN(addr, PAGE_SIZE * 2);
[8c5e6c7]419 hi->asid = asid;
[38a1a84]420}
[b00fdde]421
[02055415]422/** Print contents of TLB. */
[b00fdde]423void tlb_print(void)
424{
[0bd4f56d]425 page_mask_t mask;
[02055415]426 entry_lo_t lo0, lo1;
[f9425006]427 entry_hi_t hi, hi_save;
[a0f6a61]428 unsigned int i;
[02055415]429
[f9425006]430 hi_save.value = cp0_entry_hi_read();
[a0f6a61]431
[ccb426c]432 printf("[nr] [asid] [vpn2] [mask] [gvdc] [pfn ]\n");
[a0f6a61]433
[02055415]434 for (i = 0; i < TLB_ENTRY_COUNT; i++) {
435 cp0_index_write(i);
436 tlbr();
437
[0bd4f56d]438 mask.value = cp0_pagemask_read();
[02055415]439 hi.value = cp0_entry_hi_read();
440 lo0.value = cp0_entry_lo0_read();
441 lo1.value = cp0_entry_lo1_read();
442
[ccb426c]443 printf("%-4u %-6u %#6x %#6x %1u%1u%1u%1u %#6x\n",
[91befde0]444 i, hi.asid, hi.vpn2, mask.mask,
445 lo0.g, lo0.v, lo0.d, lo0.c, lo0.pfn);
[ccb426c]446 printf(" %1u%1u%1u%1u %#6x\n",
[91befde0]447 lo1.g, lo1.v, lo1.d, lo1.c, lo1.pfn);
[02055415]448 }
[f9425006]449
450 cp0_entry_hi_write(hi_save.value);
[b00fdde]451}
[a98d2ec]452
[8ad925c]453/** Invalidate all not wired TLB entries. */
[a98d2ec]454void tlb_invalidate_all(void)
455{
[dd14cced]456 ipl_t ipl;
457 entry_lo_t lo0, lo1;
[f9425006]458 entry_hi_t hi_save;
[a98d2ec]459 int i;
460
[f9425006]461 hi_save.value = cp0_entry_hi_read();
[dd14cced]462 ipl = interrupts_disable();
[a98d2ec]463
[8ad925c]464 for (i = TLB_WIRED; i < TLB_ENTRY_COUNT; i++) {
[a98d2ec]465 cp0_index_write(i);
[dd14cced]466 tlbr();
467
468 lo0.value = cp0_entry_lo0_read();
469 lo1.value = cp0_entry_lo1_read();
470
471 lo0.v = 0;
472 lo1.v = 0;
473
474 cp0_entry_lo0_write(lo0.value);
475 cp0_entry_lo1_write(lo1.value);
476
[a98d2ec]477 tlbwi();
478 }
[dd14cced]479
480 interrupts_restore(ipl);
[f9425006]481 cp0_entry_hi_write(hi_save.value);
[a98d2ec]482}
483
484/** Invalidate all TLB entries belonging to specified address space.
485 *
486 * @param asid Address space identifier.
487 */
488void tlb_invalidate_asid(asid_t asid)
489{
[dd14cced]490 ipl_t ipl;
491 entry_lo_t lo0, lo1;
[f9425006]492 entry_hi_t hi, hi_save;
[a98d2ec]493 int i;
494
[dd14cced]495 ASSERT(asid != ASID_INVALID);
496
[f9425006]497 hi_save.value = cp0_entry_hi_read();
[dd14cced]498 ipl = interrupts_disable();
499
[a98d2ec]500 for (i = 0; i < TLB_ENTRY_COUNT; i++) {
501 cp0_index_write(i);
502 tlbr();
503
[dd14cced]504 hi.value = cp0_entry_hi_read();
505
[a98d2ec]506 if (hi.asid == asid) {
[dd14cced]507 lo0.value = cp0_entry_lo0_read();
508 lo1.value = cp0_entry_lo1_read();
509
510 lo0.v = 0;
511 lo1.v = 0;
512
513 cp0_entry_lo0_write(lo0.value);
514 cp0_entry_lo1_write(lo1.value);
515
[a98d2ec]516 tlbwi();
517 }
518 }
[dd14cced]519
520 interrupts_restore(ipl);
[f9425006]521 cp0_entry_hi_write(hi_save.value);
[a98d2ec]522}
523
[91befde0]524/** Invalidate TLB entries for specified page range belonging to specified
525 * address space.
[a98d2ec]526 *
[91befde0]527 * @param asid Address space identifier.
528 * @param page First page whose TLB entry is to be invalidated.
529 * @param cnt Number of entries to invalidate.
[a98d2ec]530 */
[98000fb]531void tlb_invalidate_pages(asid_t asid, uintptr_t page, size_t cnt)
[a98d2ec]532{
[6c441cf8]533 unsigned int i;
[dd14cced]534 ipl_t ipl;
535 entry_lo_t lo0, lo1;
[f9425006]536 entry_hi_t hi, hi_save;
[a98d2ec]537 tlb_index_t index;
[bd81386]538
539 if (asid == ASID_INVALID)
540 return;
[dd14cced]541
[f9425006]542 hi_save.value = cp0_entry_hi_read();
[dd14cced]543 ipl = interrupts_disable();
[a98d2ec]544
[6c441cf8]545 for (i = 0; i < cnt + 1; i += 2) {
[4512d7e]546 hi.value = 0;
[edebc15c]547 tlb_prepare_entry_hi(&hi, asid, page + i * PAGE_SIZE);
[4512d7e]548 cp0_entry_hi_write(hi.value);
[dd14cced]549
[4512d7e]550 tlbp();
551 index.value = cp0_index_read();
[a98d2ec]552
[4512d7e]553 if (!index.p) {
[91befde0]554 /*
555 * Entry was found, index register contains valid
556 * index.
557 */
[4512d7e]558 tlbr();
[dd14cced]559
[4512d7e]560 lo0.value = cp0_entry_lo0_read();
561 lo1.value = cp0_entry_lo1_read();
[dd14cced]562
[4512d7e]563 lo0.v = 0;
564 lo1.v = 0;
[dd14cced]565
[4512d7e]566 cp0_entry_lo0_write(lo0.value);
567 cp0_entry_lo1_write(lo1.value);
[dd14cced]568
[4512d7e]569 tlbwi();
570 }
[a98d2ec]571 }
[dd14cced]572
573 interrupts_restore(ipl);
[f9425006]574 cp0_entry_hi_write(hi_save.value);
[a98d2ec]575}
[b45c443]576
[a6dd361]577/** @}
[b45c443]578 */
Note: See TracBrowser for help on using the repository browser.