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

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

Reflect assumptions about lock and interrupt state in functions themselves.

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