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

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

Unify user page fault handling in as_page_fault().

  • Remove lots of architecture-dependent boilerplate code.
  • Property mode set to 100644
File size: 9.4 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
[1dbc43f]144 ASSERT(!index.p);
[38a1a84]145
[1dbc43f]146 pte = find_mapping_and_check(badvaddr, PF_ACCESS_READ, istate);
147 if (pte) {
148 /*
149 * Read the faulting TLB entry.
150 */
151 tlbr();
[38a1a84]152
[1dbc43f]153 /*
154 * Record access to PTE.
155 */
156 pte->a = 1;
[38a1a84]157
[1dbc43f]158 tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->d,
159 pte->cacheable, pte->pfn);
[38a1a84]160
[1dbc43f]161 /*
162 * The entry is to be updated in TLB.
163 */
164 if ((badvaddr / PAGE_SIZE) % 2 == 0)
165 cp0_entry_lo0_write(lo.value);
166 else
167 cp0_entry_lo1_write(lo.value);
168 cp0_pagemask_write(TLB_PAGE_MASK_16K);
169 tlbwi();
170 }
[1084a784]171}
172
[91befde0]173/** Process TLB Modified Exception.
[38a1a84]174 *
[91befde0]175 * @param istate Interrupted register context.
[38a1a84]176 */
[25d7709]177void tlb_modified(istate_t *istate)
[1084a784]178{
[cc205f1]179 tlb_index_t index;
[7f1c620]180 uintptr_t badvaddr;
[cc205f1]181 entry_lo_t lo;
[8c5e6c7]182 entry_hi_t hi;
[38a1a84]183 pte_t *pte;
184
185 badvaddr = cp0_badvaddr_read();
186
187 /*
188 * Locate the faulting entry in TLB.
189 */
[8c5e6c7]190 hi.value = cp0_entry_hi_read();
[edebc15c]191 tlb_prepare_entry_hi(&hi, hi.asid, badvaddr);
[8c5e6c7]192 cp0_entry_hi_write(hi.value);
[38a1a84]193 tlbp();
[cc205f1]194 index.value = cp0_index_read();
[2299914]195
[38a1a84]196 /*
197 * Fail if the entry is not in TLB.
198 */
[1dbc43f]199 ASSERT(!index.p);
[1084a784]200
[1dbc43f]201 pte = find_mapping_and_check(badvaddr, PF_ACCESS_WRITE, istate);
202 if (pte) {
203 /*
204 * Read the faulting TLB entry.
205 */
206 tlbr();
[f761f1eb]207
[1dbc43f]208 /*
209 * Record access and write to PTE.
210 */
211 pte->a = 1;
212 pte->d = 1;
[1084a784]213
[1dbc43f]214 tlb_prepare_entry_lo(&lo, pte->g, pte->p, pte->w,
215 pte->cacheable, pte->pfn);
[f761f1eb]216
[1dbc43f]217 /*
218 * The entry is to be updated in TLB.
219 */
220 if ((badvaddr / PAGE_SIZE) % 2 == 0)
221 cp0_entry_lo0_write(lo.value);
222 else
223 cp0_entry_lo1_write(lo.value);
224 cp0_pagemask_write(TLB_PAGE_MASK_16K);
225 tlbwi();
226 }
[ce031f0]227}
228
[91befde0]229/** Try to find PTE for faulting address.
[38a1a84]230 *
[91befde0]231 * @param badvaddr Faulting virtual address.
232 * @param access Access mode that caused the fault.
233 * @param istate Pointer to interrupted state.
[38a1a84]234 *
[91befde0]235 * @return PTE on success, NULL otherwise.
[38a1a84]236 */
[1dbc43f]237pte_t *find_mapping_and_check(uintptr_t badvaddr, int access, istate_t *istate)
[38a1a84]238{
[cc205f1]239 entry_hi_t hi;
[38a1a84]240 pte_t *pte;
241
[cc205f1]242 hi.value = cp0_entry_hi_read();
[38a1a84]243
[1dbc43f]244 ASSERT(hi.asid == AS->asid);
[20d50a1]245
246 /*
247 * Check if the mapping exists in page tables.
248 */
[0ff03f3]249 pte = page_mapping_find(AS, badvaddr, true);
[c867756e]250 if (pte && pte->p && (pte->w || access != PF_ACCESS_WRITE)) {
[20d50a1]251 /*
252 * Mapping found in page tables.
253 * Immediately succeed.
254 */
255 return pte;
256 }
[1dbc43f]257
258 /*
259 * Mapping not found in page tables.
260 * Resort to higher-level page fault handler.
261 */
262 if (as_page_fault(badvaddr, access, istate) == AS_PF_OK) {
263 pte = page_mapping_find(AS, badvaddr, true);
264 ASSERT(pte && pte->p);
265 ASSERT(pte->w || access != PF_ACCESS_WRITE);
266 return pte;
267 }
268
269 return NULL;
[38a1a84]270}
271
[91befde0]272void
273tlb_prepare_entry_lo(entry_lo_t *lo, bool g, bool v, bool d, bool cacheable,
274 uintptr_t pfn)
[38a1a84]275{
[8c5e6c7]276 lo->value = 0;
[38a1a84]277 lo->g = g;
278 lo->v = v;
279 lo->d = d;
[0882a9a]280 lo->c = cacheable ? PAGE_CACHEABLE_EXC_WRITE : PAGE_UNCACHED;
[38a1a84]281 lo->pfn = pfn;
[8c5e6c7]282}
283
[edebc15c]284void tlb_prepare_entry_hi(entry_hi_t *hi, asid_t asid, uintptr_t addr)
[8c5e6c7]285{
[2d01bbd]286 hi->value = ALIGN_DOWN(addr, PAGE_SIZE * 2);
[8c5e6c7]287 hi->asid = asid;
[38a1a84]288}
[b00fdde]289
[02055415]290/** Print contents of TLB. */
[b00fdde]291void tlb_print(void)
292{
[0bd4f56d]293 page_mask_t mask;
[02055415]294 entry_lo_t lo0, lo1;
[f9425006]295 entry_hi_t hi, hi_save;
[a0f6a61]296 unsigned int i;
[02055415]297
[f9425006]298 hi_save.value = cp0_entry_hi_read();
[a0f6a61]299
[ccb426c]300 printf("[nr] [asid] [vpn2] [mask] [gvdc] [pfn ]\n");
[a0f6a61]301
[02055415]302 for (i = 0; i < TLB_ENTRY_COUNT; i++) {
303 cp0_index_write(i);
304 tlbr();
305
[0bd4f56d]306 mask.value = cp0_pagemask_read();
[02055415]307 hi.value = cp0_entry_hi_read();
308 lo0.value = cp0_entry_lo0_read();
309 lo1.value = cp0_entry_lo1_read();
310
[ccb426c]311 printf("%-4u %-6u %#6x %#6x %1u%1u%1u%1u %#6x\n",
[91befde0]312 i, hi.asid, hi.vpn2, mask.mask,
313 lo0.g, lo0.v, lo0.d, lo0.c, lo0.pfn);
[ccb426c]314 printf(" %1u%1u%1u%1u %#6x\n",
[91befde0]315 lo1.g, lo1.v, lo1.d, lo1.c, lo1.pfn);
[02055415]316 }
[f9425006]317
318 cp0_entry_hi_write(hi_save.value);
[b00fdde]319}
[a98d2ec]320
[8ad925c]321/** Invalidate all not wired TLB entries. */
[a98d2ec]322void tlb_invalidate_all(void)
323{
[dd14cced]324 ipl_t ipl;
325 entry_lo_t lo0, lo1;
[f9425006]326 entry_hi_t hi_save;
[a98d2ec]327 int i;
328
[f9425006]329 hi_save.value = cp0_entry_hi_read();
[dd14cced]330 ipl = interrupts_disable();
[a98d2ec]331
[8ad925c]332 for (i = TLB_WIRED; i < TLB_ENTRY_COUNT; i++) {
[a98d2ec]333 cp0_index_write(i);
[dd14cced]334 tlbr();
335
336 lo0.value = cp0_entry_lo0_read();
337 lo1.value = cp0_entry_lo1_read();
338
339 lo0.v = 0;
340 lo1.v = 0;
341
342 cp0_entry_lo0_write(lo0.value);
343 cp0_entry_lo1_write(lo1.value);
344
[a98d2ec]345 tlbwi();
346 }
[dd14cced]347
348 interrupts_restore(ipl);
[f9425006]349 cp0_entry_hi_write(hi_save.value);
[a98d2ec]350}
351
352/** Invalidate all TLB entries belonging to specified address space.
353 *
354 * @param asid Address space identifier.
355 */
356void tlb_invalidate_asid(asid_t asid)
357{
[dd14cced]358 ipl_t ipl;
359 entry_lo_t lo0, lo1;
[f9425006]360 entry_hi_t hi, hi_save;
[a98d2ec]361 int i;
362
[dd14cced]363 ASSERT(asid != ASID_INVALID);
364
[f9425006]365 hi_save.value = cp0_entry_hi_read();
[dd14cced]366 ipl = interrupts_disable();
367
[a98d2ec]368 for (i = 0; i < TLB_ENTRY_COUNT; i++) {
369 cp0_index_write(i);
370 tlbr();
371
[dd14cced]372 hi.value = cp0_entry_hi_read();
373
[a98d2ec]374 if (hi.asid == asid) {
[dd14cced]375 lo0.value = cp0_entry_lo0_read();
376 lo1.value = cp0_entry_lo1_read();
377
378 lo0.v = 0;
379 lo1.v = 0;
380
381 cp0_entry_lo0_write(lo0.value);
382 cp0_entry_lo1_write(lo1.value);
383
[a98d2ec]384 tlbwi();
385 }
386 }
[dd14cced]387
388 interrupts_restore(ipl);
[f9425006]389 cp0_entry_hi_write(hi_save.value);
[a98d2ec]390}
391
[91befde0]392/** Invalidate TLB entries for specified page range belonging to specified
393 * address space.
[a98d2ec]394 *
[91befde0]395 * @param asid Address space identifier.
396 * @param page First page whose TLB entry is to be invalidated.
397 * @param cnt Number of entries to invalidate.
[a98d2ec]398 */
[98000fb]399void tlb_invalidate_pages(asid_t asid, uintptr_t page, size_t cnt)
[a98d2ec]400{
[6c441cf8]401 unsigned int i;
[dd14cced]402 ipl_t ipl;
403 entry_lo_t lo0, lo1;
[f9425006]404 entry_hi_t hi, hi_save;
[a98d2ec]405 tlb_index_t index;
[bd81386]406
407 if (asid == ASID_INVALID)
408 return;
[dd14cced]409
[f9425006]410 hi_save.value = cp0_entry_hi_read();
[dd14cced]411 ipl = interrupts_disable();
[a98d2ec]412
[6c441cf8]413 for (i = 0; i < cnt + 1; i += 2) {
[4512d7e]414 hi.value = 0;
[edebc15c]415 tlb_prepare_entry_hi(&hi, asid, page + i * PAGE_SIZE);
[4512d7e]416 cp0_entry_hi_write(hi.value);
[dd14cced]417
[4512d7e]418 tlbp();
419 index.value = cp0_index_read();
[a98d2ec]420
[4512d7e]421 if (!index.p) {
[91befde0]422 /*
423 * Entry was found, index register contains valid
424 * index.
425 */
[4512d7e]426 tlbr();
[dd14cced]427
[4512d7e]428 lo0.value = cp0_entry_lo0_read();
429 lo1.value = cp0_entry_lo1_read();
[dd14cced]430
[4512d7e]431 lo0.v = 0;
432 lo1.v = 0;
[dd14cced]433
[4512d7e]434 cp0_entry_lo0_write(lo0.value);
435 cp0_entry_lo1_write(lo1.value);
[dd14cced]436
[4512d7e]437 tlbwi();
438 }
[a98d2ec]439 }
[dd14cced]440
441 interrupts_restore(ipl);
[f9425006]442 cp0_entry_hi_write(hi_save.value);
[a98d2ec]443}
[b45c443]444
[a6dd361]445/** @}
[b45c443]446 */
Note: See TracBrowser for help on using the repository browser.