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

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

Behaviour of the TLB invalid exception is different on 4Kc than on R4000.

  • Property mode set to 100644
File size: 9.7 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
[1dbc43f]50static pte_t *find_mapping_and_check(uintptr_t, int, istate_t *);
[8c5e6c7]51
[91befde0]52/** Initialize TLB.
[1084a784]53 *
54 * Invalidate all entries and mark wired entries.
55 */
[b00fdde]56void tlb_arch_init(void)
[ce031f0]57{
[dd14cced]58 int i;
59
[ce031f0]60 cp0_pagemask_write(TLB_PAGE_MASK_16K);
[dd14cced]61 cp0_entry_hi_write(0);
62 cp0_entry_lo0_write(0);
63 cp0_entry_lo1_write(0);
[ce031f0]64
[dd14cced]65 /* Clear and initialize TLB. */
66
67 for (i = 0; i < TLB_ENTRY_COUNT; i++) {
68 cp0_index_write(i);
69 tlbwi();
70 }
[54a7a20]71
[ce031f0]72 /*
73 * The kernel is going to make use of some wired
[1084a784]74 * entries (e.g. mapping kernel stacks in kseg3).
[ce031f0]75 */
76 cp0_wired_write(TLB_WIRED);
77}
78
[91befde0]79/** Process TLB Refill Exception.
[1084a784]80 *
[91befde0]81 * @param istate Interrupted register context.
[1084a784]82 */
[25d7709]83void tlb_refill(istate_t *istate)
[1084a784]84{
[cc205f1]85 entry_lo_t lo;
[2299914]86 entry_hi_t hi;
87 asid_t asid;
[7f1c620]88 uintptr_t badvaddr;
[1084a784]89 pte_t *pte;
[7f341820]90
[1084a784]91 badvaddr = cp0_badvaddr_read();
[2299914]92 asid = AS->asid;
[7f341820]93
[1dbc43f]94 pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate);
95 if (pte) {
96 /*
97 * Record access to PTE.
98 */
99 pte->a = 1;
[38a1a84]100
[1dbc43f]101 tlb_prepare_entry_hi(&hi, asid, badvaddr);
102 tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->d,
103 pte->cacheable, pte->pfn);
[1084a784]104
[1dbc43f]105 /*
106 * New entry is to be inserted into TLB
107 */
108 cp0_entry_hi_write(hi.value);
109 if ((badvaddr / PAGE_SIZE) % 2 == 0) {
110 cp0_entry_lo0_write(lo.value);
111 cp0_entry_lo1_write(0);
112 } else {
113 cp0_entry_lo0_write(0);
114 cp0_entry_lo1_write(lo.value);
115 }
116 cp0_pagemask_write(TLB_PAGE_MASK_16K);
117 tlbwr();
[1084a784]118 }
119}
120
[91befde0]121/** Process TLB Invalid Exception.
[38a1a84]122 *
[91befde0]123 * @param istate Interrupted register context.
[38a1a84]124 */
[25d7709]125void tlb_invalid(istate_t *istate)
[1084a784]126{
[cc205f1]127 tlb_index_t index;
[7f1c620]128 uintptr_t badvaddr;
[cc205f1]129 entry_lo_t lo;
[8c5e6c7]130 entry_hi_t hi;
[38a1a84]131 pte_t *pte;
132
133 badvaddr = cp0_badvaddr_read();
134
135 /*
136 * Locate the faulting entry in TLB.
137 */
[8c5e6c7]138 hi.value = cp0_entry_hi_read();
[edebc15c]139 tlb_prepare_entry_hi(&hi, hi.asid, badvaddr);
[8c5e6c7]140 cp0_entry_hi_write(hi.value);
[38a1a84]141 tlbp();
[cc205f1]142 index.value = cp0_index_read();
[2299914]143
[cf538e7]144#if defined(PROCESSOR_4Kc)
145 /*
146 * This can happen on a 4Kc when Status.EXL is 1 and there is a TLB miss.
147 * EXL is 1 when interrupts are disabled. The combination of a TLB miss
148 * and disabled interrupts is possible in copy_to/from_uspace().
149 */
150 if (index.p) {
151 tlb_refill(istate);
152 return;
153 }
154#endif
155
[1dbc43f]156 ASSERT(!index.p);
[38a1a84]157
[1dbc43f]158 pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate);
159 if (pte) {
160 /*
161 * Read the faulting TLB entry.
162 */
163 tlbr();
[38a1a84]164
[1dbc43f]165 /*
166 * Record access to PTE.
167 */
168 pte->a = 1;
[38a1a84]169
[1dbc43f]170 tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->d,
171 pte->cacheable, pte->pfn);
[38a1a84]172
[1dbc43f]173 /*
174 * The entry is to be updated in TLB.
175 */
176 if ((badvaddr / PAGE_SIZE) % 2 == 0)
177 cp0_entry_lo0_write(lo.value);
178 else
179 cp0_entry_lo1_write(lo.value);
180 cp0_pagemask_write(TLB_PAGE_MASK_16K);
181 tlbwi();
182 }
[1084a784]183}
184
[91befde0]185/** Process TLB Modified Exception.
[38a1a84]186 *
[91befde0]187 * @param istate Interrupted register context.
[38a1a84]188 */
[25d7709]189void tlb_modified(istate_t *istate)
[1084a784]190{
[cc205f1]191 tlb_index_t index;
[7f1c620]192 uintptr_t badvaddr;
[cc205f1]193 entry_lo_t lo;
[8c5e6c7]194 entry_hi_t hi;
[38a1a84]195 pte_t *pte;
196
197 badvaddr = cp0_badvaddr_read();
198
199 /*
200 * Locate the faulting entry in TLB.
201 */
[8c5e6c7]202 hi.value = cp0_entry_hi_read();
[edebc15c]203 tlb_prepare_entry_hi(&hi, hi.asid, badvaddr);
[8c5e6c7]204 cp0_entry_hi_write(hi.value);
[38a1a84]205 tlbp();
[cc205f1]206 index.value = cp0_index_read();
[2299914]207
[38a1a84]208 /*
209 * Fail if the entry is not in TLB.
210 */
[1dbc43f]211 ASSERT(!index.p);
[1084a784]212
[1dbc43f]213 pte = find_mapping_and_check(badvaddr, PF_ACCESS_WRITE, istate);
214 if (pte) {
215 /*
216 * Read the faulting TLB entry.
217 */
218 tlbr();
[f761f1eb]219
[1dbc43f]220 /*
221 * Record access and write to PTE.
222 */
223 pte->a = 1;
224 pte->d = 1;
[1084a784]225
[1dbc43f]226 tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->w,
227 pte->cacheable, pte->pfn);
[f761f1eb]228
[1dbc43f]229 /*
230 * The entry is to be updated in TLB.
231 */
232 if ((badvaddr / PAGE_SIZE) % 2 == 0)
233 cp0_entry_lo0_write(lo.value);
234 else
235 cp0_entry_lo1_write(lo.value);
236 cp0_pagemask_write(TLB_PAGE_MASK_16K);
237 tlbwi();
238 }
[ce031f0]239}
240
[91befde0]241/** Try to find PTE for faulting address.
[38a1a84]242 *
[91befde0]243 * @param badvaddr Faulting virtual address.
244 * @param access Access mode that caused the fault.
245 * @param istate Pointer to interrupted state.
[38a1a84]246 *
[91befde0]247 * @return PTE on success, NULL otherwise.
[38a1a84]248 */
[1dbc43f]249pte_t *find_mapping_and_check(uintptr_t badvaddr, int access, istate_t *istate)
[38a1a84]250{
[cc205f1]251 entry_hi_t hi;
[38a1a84]252 pte_t *pte;
253
[cc205f1]254 hi.value = cp0_entry_hi_read();
[38a1a84]255
[1dbc43f]256 ASSERT(hi.asid == AS->asid);
[20d50a1]257
258 /*
259 * Check if the mapping exists in page tables.
260 */
[0ff03f3]261 pte = page_mapping_find(AS, badvaddr, true);
[c867756e]262 if (pte && pte->p && (pte->w || access != PF_ACCESS_WRITE)) {
[20d50a1]263 /*
264 * Mapping found in page tables.
265 * Immediately succeed.
266 */
267 return pte;
268 }
[1dbc43f]269
270 /*
271 * Mapping not found in page tables.
272 * Resort to higher-level page fault handler.
273 */
274 if (as_page_fault(badvaddr, access, istate) == AS_PF_OK) {
275 pte = page_mapping_find(AS, badvaddr, true);
276 ASSERT(pte && pte->p);
277 ASSERT(pte->w || access != PF_ACCESS_WRITE);
278 return pte;
279 }
280
281 return NULL;
[38a1a84]282}
283
[91befde0]284void
285tlb_prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable,
286 uintptr_t pfn)
[38a1a84]287{
[8c5e6c7]288 lo->value = 0;
[38a1a84]289 lo->g = g;
290 lo->v = v;
291 lo->d = d;
[0882a9a]292 lo->c = cacheable ? PAGE_CACHEABLE_EXC_WRITE : PAGE_UNCACHED;
[38a1a84]293 lo->pfn = pfn;
[8c5e6c7]294}
295
[edebc15c]296void tlb_prepare_entry_hi(entry_hi_t *hi, asid_t asid, uintptr_t addr)
[8c5e6c7]297{
[2d01bbd]298 hi->value = ALIGN_DOWN(addr, PAGE_SIZE * 2);
[8c5e6c7]299 hi->asid = asid;
[38a1a84]300}
[b00fdde]301
[02055415]302/** Print contents of TLB. */
[b00fdde]303void tlb_print(void)
304{
[0bd4f56d]305 page_mask_t mask;
[02055415]306 entry_lo_t lo0, lo1;
[f9425006]307 entry_hi_t hi, hi_save;
[a0f6a61]308 unsigned int i;
[02055415]309
[f9425006]310 hi_save.value = cp0_entry_hi_read();
[a0f6a61]311
[ccb426c]312 printf("[nr] [asid] [vpn2] [mask] [gvdc] [pfn ]\n");
[a0f6a61]313
[02055415]314 for (i = 0; i < TLB_ENTRY_COUNT; i++) {
315 cp0_index_write(i);
316 tlbr();
317
[0bd4f56d]318 mask.value = cp0_pagemask_read();
[02055415]319 hi.value = cp0_entry_hi_read();
320 lo0.value = cp0_entry_lo0_read();
321 lo1.value = cp0_entry_lo1_read();
322
[ccb426c]323 printf("%-4u %-6u %#6x %#6x %1u%1u%1u%1u %#6x\n",
[91befde0]324 i, hi.asid, hi.vpn2, mask.mask,
325 lo0.g, lo0.v, lo0.d, lo0.c, lo0.pfn);
[ccb426c]326 printf(" %1u%1u%1u%1u %#6x\n",
[91befde0]327 lo1.g, lo1.v, lo1.d, lo1.c, lo1.pfn);
[02055415]328 }
[f9425006]329
330 cp0_entry_hi_write(hi_save.value);
[b00fdde]331}
[a98d2ec]332
[8ad925c]333/** Invalidate all not wired TLB entries. */
[a98d2ec]334void tlb_invalidate_all(void)
335{
[dd14cced]336 ipl_t ipl;
337 entry_lo_t lo0, lo1;
[f9425006]338 entry_hi_t hi_save;
[a98d2ec]339 int i;
340
[f9425006]341 hi_save.value = cp0_entry_hi_read();
[dd14cced]342 ipl = interrupts_disable();
[a98d2ec]343
[8ad925c]344 for (i = TLB_WIRED; i < TLB_ENTRY_COUNT; i++) {
[a98d2ec]345 cp0_index_write(i);
[dd14cced]346 tlbr();
347
348 lo0.value = cp0_entry_lo0_read();
349 lo1.value = cp0_entry_lo1_read();
350
351 lo0.v = 0;
352 lo1.v = 0;
353
354 cp0_entry_lo0_write(lo0.value);
355 cp0_entry_lo1_write(lo1.value);
356
[a98d2ec]357 tlbwi();
358 }
[dd14cced]359
360 interrupts_restore(ipl);
[f9425006]361 cp0_entry_hi_write(hi_save.value);
[a98d2ec]362}
363
364/** Invalidate all TLB entries belonging to specified address space.
365 *
366 * @param asid Address space identifier.
367 */
368void tlb_invalidate_asid(asid_t asid)
369{
[dd14cced]370 ipl_t ipl;
371 entry_lo_t lo0, lo1;
[f9425006]372 entry_hi_t hi, hi_save;
[a98d2ec]373 int i;
374
[dd14cced]375 ASSERT(asid != ASID_INVALID);
376
[f9425006]377 hi_save.value = cp0_entry_hi_read();
[dd14cced]378 ipl = interrupts_disable();
379
[a98d2ec]380 for (i = 0; i < TLB_ENTRY_COUNT; i++) {
381 cp0_index_write(i);
382 tlbr();
383
[dd14cced]384 hi.value = cp0_entry_hi_read();
385
[a98d2ec]386 if (hi.asid == asid) {
[dd14cced]387 lo0.value = cp0_entry_lo0_read();
388 lo1.value = cp0_entry_lo1_read();
389
390 lo0.v = 0;
391 lo1.v = 0;
392
393 cp0_entry_lo0_write(lo0.value);
394 cp0_entry_lo1_write(lo1.value);
395
[a98d2ec]396 tlbwi();
397 }
398 }
[dd14cced]399
400 interrupts_restore(ipl);
[f9425006]401 cp0_entry_hi_write(hi_save.value);
[a98d2ec]402}
403
[91befde0]404/** Invalidate TLB entries for specified page range belonging to specified
405 * address space.
[a98d2ec]406 *
[91befde0]407 * @param asid Address space identifier.
408 * @param page First page whose TLB entry is to be invalidated.
409 * @param cnt Number of entries to invalidate.
[a98d2ec]410 */
[98000fb]411void tlb_invalidate_pages(asid_t asid, uintptr_t page, size_t cnt)
[a98d2ec]412{
[6c441cf8]413 unsigned int i;
[dd14cced]414 ipl_t ipl;
415 entry_lo_t lo0, lo1;
[f9425006]416 entry_hi_t hi, hi_save;
[a98d2ec]417 tlb_index_t index;
[bd81386]418
419 if (asid == ASID_INVALID)
420 return;
[dd14cced]421
[f9425006]422 hi_save.value = cp0_entry_hi_read();
[dd14cced]423 ipl = interrupts_disable();
[a98d2ec]424
[6c441cf8]425 for (i = 0; i < cnt + 1; i += 2) {
[4512d7e]426 hi.value = 0;
[edebc15c]427 tlb_prepare_entry_hi(&hi, asid, page + i * PAGE_SIZE);
[4512d7e]428 cp0_entry_hi_write(hi.value);
[dd14cced]429
[4512d7e]430 tlbp();
431 index.value = cp0_index_read();
[a98d2ec]432
[4512d7e]433 if (!index.p) {
[91befde0]434 /*
435 * Entry was found, index register contains valid
436 * index.
437 */
[4512d7e]438 tlbr();
[dd14cced]439
[4512d7e]440 lo0.value = cp0_entry_lo0_read();
441 lo1.value = cp0_entry_lo1_read();
[dd14cced]442
[4512d7e]443 lo0.v = 0;
444 lo1.v = 0;
[dd14cced]445
[4512d7e]446 cp0_entry_lo0_write(lo0.value);
447 cp0_entry_lo1_write(lo1.value);
[dd14cced]448
[4512d7e]449 tlbwi();
450 }
[a98d2ec]451 }
[dd14cced]452
453 interrupts_restore(ipl);
[f9425006]454 cp0_entry_hi_write(hi_save.value);
[a98d2ec]455}
[b45c443]456
[a6dd361]457/** @}
[b45c443]458 */
Note: See TracBrowser for help on using the repository browser.