2 * Low level TLB miss handlers for Book3E
4 * Copyright (C) 2008-2009
5 * Ben. Herrenschmidt (benh@kernel.crashing.org), IBM Corp.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <asm/processor.h>
17 #include <asm/ppc_asm.h>
18 #include <asm/asm-offsets.h>
19 #include <asm/cputable.h>
20 #include <asm/pgtable.h>
21 #include <asm/exception-64e.h>
22 #include <asm/ppc-opcode.h>
23 #include <asm/kvm_asm.h>
24 #include <asm/kvm_booke_hv_asm.h>
26 #ifdef CONFIG_PPC_64K_PAGES
27 #define VPTE_PMD_SHIFT (PTE_INDEX_SIZE+1)
29 #define VPTE_PMD_SHIFT (PTE_INDEX_SIZE)
31 #define VPTE_PUD_SHIFT (VPTE_PMD_SHIFT + PMD_INDEX_SIZE)
32 #define VPTE_PGD_SHIFT (VPTE_PUD_SHIFT + PUD_INDEX_SIZE)
33 #define VPTE_INDEX_SIZE (VPTE_PGD_SHIFT + PGD_INDEX_SIZE)
35 /**********************************************************************
37 * TLB miss handling for Book3E with a bolted linear mapping *
38 * No virtual page table, no nested TLB misses *
40 **********************************************************************/
43 * Note that, unlike non-bolted handlers, TLB_EXFRAME is not
44 * modified by the TLB miss handlers themselves, since the TLB miss
45 * handler code will not itself cause a recursive TLB miss.
47 * TLB_EXFRAME will be modified when crit/mc/debug exceptions are
50 .macro tlb_prolog_bolted intnum addr
51 mtspr SPRN_SPRG_GEN_SCRATCH,r12
52 mfspr r12,SPRN_SPRG_TLB_EXFRAME
53 std r13,EX_TLB_R13(r12)
54 std r10,EX_TLB_R10(r12)
55 mfspr r13,SPRN_SPRG_PACA
58 std r11,EX_TLB_R11(r12)
59 #ifdef CONFIG_KVM_BOOKE_HV
62 END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
64 DO_KVM \intnum, SPRN_SRR1
65 std r16,EX_TLB_R16(r12)
66 mfspr r16,\addr /* get faulting address */
67 std r14,EX_TLB_R14(r12)
69 std r15,EX_TLB_R15(r12)
70 std r10,EX_TLB_CR(r12)
74 .macro tlb_epilog_bolted
76 ld r10,EX_TLB_R10(r12)
77 ld r11,EX_TLB_R11(r12)
78 ld r13,EX_TLB_R13(r12)
80 ld r14,EX_TLB_R14(r12)
81 ld r15,EX_TLB_R15(r12)
82 TLB_MISS_RESTORE_STATS
83 ld r16,EX_TLB_R16(r12)
84 mfspr r12,SPRN_SPRG_GEN_SCRATCH
88 START_EXCEPTION(data_tlb_miss_bolted)
89 tlb_prolog_bolted BOOKE_INTERRUPT_DTLB_MISS SPRN_DEAR
91 /* We need _PAGE_PRESENT and _PAGE_ACCESSED set */
93 /* We do the user/kernel test for the PID here along with the RW test
95 /* We pre-test some combination of permissions to avoid double
98 * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE
99 * ESR_ST is 0x00800000
100 * _PAGE_BAP_SW is 0x00000010
101 * So the shift is >> 19. This tests for supervisor writeability.
102 * If the page happens to be supervisor writeable and not user
103 * writeable, we will take a new fault later, but that should be
104 * a rare enough case.
106 * We also move ESR_ST in _PAGE_DIRTY position
107 * _PAGE_DIRTY is 0x00001000 so the shift is >> 11
109 * MAS1 is preset for all we need except for TID that needs to
110 * be cleared for kernel translations
115 srdi r15,r16,60 /* get region */
116 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
117 bne- dtlb_miss_fault_bolted /* Bail if fault addr is invalid */
119 rlwinm r10,r11,32-19,27,27
120 rlwimi r10,r11,32-16,19,19
121 cmpwi r15,0 /* user vs kernel check */
122 ori r10,r10,_PAGE_PRESENT
123 oris r11,r10,_PAGE_ACCESSED@h
125 TLB_MISS_STATS_SAVE_INFO_BOLTED
126 bne tlb_miss_kernel_bolted
128 tlb_miss_common_bolted:
130 * This is the guts of the TLB miss handler for bolted-linear.
131 * We are entered with:
133 * r16 = faulting address
134 * r15 = crap (free to use)
135 * r14 = page table base
137 * r11 = PTE permission mask
138 * r10 = crap (free to use)
140 rldicl r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3
143 beq tlb_miss_fault_bolted /* No PGDIR, bail */
145 BEGIN_MMU_FTR_SECTION
146 /* Set the TLB reservation and search for existing entry. Then load
149 PPC_TLBSRX_DOT(0,R16)
150 ldx r14,r14,r15 /* grab pgd entry */
151 beq tlb_miss_done_bolted /* tlb exists already, bail */
153 ldx r14,r14,r15 /* grab pgd entry */
154 ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_USE_TLBRSRV)
156 #ifndef CONFIG_PPC_64K_PAGES
157 rldicl r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3
160 bge tlb_miss_fault_bolted /* Bad pgd entry or hugepage; bail */
161 ldx r14,r14,r15 /* grab pud entry */
162 #endif /* CONFIG_PPC_64K_PAGES */
164 rldicl r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3
167 bge tlb_miss_fault_bolted
168 ldx r14,r14,r15 /* Grab pmd entry */
170 rldicl r15,r16,64-PAGE_SHIFT+3,64-PTE_INDEX_SIZE-3
173 bge tlb_miss_fault_bolted
174 ldx r14,r14,r15 /* Grab PTE, normal (!huge) page */
176 /* Check if required permissions are met */
178 rldicr r15,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
179 bne- tlb_miss_fault_bolted
181 /* Now we build the MAS:
183 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
184 * MAS 1 : Almost fully setup
185 * - PID already updated by caller if necessary
186 * - TSIZE need change if !base page size, not
187 * yet implemented for now
188 * MAS 2 : Defaults not useful, need to be redone
189 * MAS 3+7 : Needs to be done
191 clrrdi r11,r16,12 /* Clear low crap in EA */
192 clrldi r15,r15,12 /* Clear crap at the top */
193 rlwimi r11,r14,32-19,27,31 /* Insert WIMGE */
194 rlwimi r15,r14,32-8,22,25 /* Move in U bits */
196 andi. r11,r14,_PAGE_DIRTY
197 rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */
199 /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
201 li r11,MAS3_SW|MAS3_UW
204 mtspr SPRN_MAS7_MAS3,r15
207 tlb_miss_done_bolted:
208 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
212 itlb_miss_kernel_bolted:
213 li r11,_PAGE_PRESENT|_PAGE_BAP_SX /* Base perm */
214 oris r11,r11,_PAGE_ACCESSED@h
215 tlb_miss_kernel_bolted:
217 ld r14,PACA_KERNELPGD(r13)
218 cmpldi cr0,r15,8 /* Check for vmalloc region */
219 rlwinm r10,r10,0,16,1 /* Clear TID */
221 beq+ tlb_miss_common_bolted
223 tlb_miss_fault_bolted:
224 /* We need to check if it was an instruction miss */
225 andi. r10,r11,_PAGE_EXEC|_PAGE_BAP_SX
226 bne itlb_miss_fault_bolted
227 dtlb_miss_fault_bolted:
228 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
230 b exc_data_storage_book3e
231 itlb_miss_fault_bolted:
232 TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
234 b exc_instruction_storage_book3e
236 /* Instruction TLB miss */
237 START_EXCEPTION(instruction_tlb_miss_bolted)
238 tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0
240 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
241 srdi r15,r16,60 /* get region */
242 TLB_MISS_STATS_SAVE_INFO_BOLTED
243 bne- itlb_miss_fault_bolted
245 li r11,_PAGE_PRESENT|_PAGE_EXEC /* Base perm */
247 /* We do the user/kernel test for the PID here along with the RW test
250 cmpldi cr0,r15,0 /* Check for user region */
251 oris r11,r11,_PAGE_ACCESSED@h
252 beq tlb_miss_common_bolted
253 b itlb_miss_kernel_bolted
255 #ifdef CONFIG_PPC_FSL_BOOK3E
257 * TLB miss handling for e6500 and derivatives, using hardware tablewalk.
259 * Linear mapping is bolted: no virtual page table or nested TLB misses
260 * Indirect entries in TLB1, hardware loads resulting direct entries
262 * No HES or NV hint on TLB1, so we need to do software round-robin
263 * No tlbsrx. so we need a spinlock, and we have to deal
264 * with MAS-damage caused by tlbsx
268 START_EXCEPTION(instruction_tlb_miss_e6500)
269 tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0
271 ld r11,PACA_TCD_PTR(r13)
272 srdi. r15,r16,60 /* get region */
275 TLB_MISS_STATS_SAVE_INFO_BOLTED
276 bne tlb_miss_kernel_e6500 /* user/kernel test */
278 b tlb_miss_common_e6500
280 START_EXCEPTION(data_tlb_miss_e6500)
281 tlb_prolog_bolted BOOKE_INTERRUPT_DTLB_MISS SPRN_DEAR
283 ld r11,PACA_TCD_PTR(r13)
284 srdi. r15,r16,60 /* get region */
287 TLB_MISS_STATS_SAVE_INFO_BOLTED
288 bne tlb_miss_kernel_e6500 /* user vs kernel check */
291 * This is the guts of the TLB miss handler for e6500 and derivatives.
292 * We are entered with:
294 * r16 = page of faulting address (low bit 0 if data, 1 if instruction)
295 * r15 = crap (free to use)
296 * r14 = page table base
298 * r11 = tlb_per_core ptr
299 * r10 = crap (free to use)
301 tlb_miss_common_e6500:
304 * Search if we already have an indirect entry for that virtual
305 * address, and if we do, bail out.
307 * MAS6:IND should be already set based on MAS4
310 lhz r10,PACAPACAINDEX(r13)
312 cmpdi cr1,r15,1 /* set cr1.eq = 0 for non-recursive */
319 2: cmpd cr1,r15,r10 /* recursive lock due to mcheck/crit/etc? */
320 beq cr1,3b /* unlock will happen if cr1.eq = 0 */
335 andis. r10,r10,MAS1_VALID@h
336 bne tlb_miss_done_e6500
337 END_FTR_SECTION_IFSET(CPU_FTR_SMT)
339 /* Now, we need to walk the page tables. First check if we are in
342 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
343 bne- tlb_miss_fault_e6500
345 rldicl r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3
348 beq- tlb_miss_fault_e6500 /* No PGDIR, bail */
349 ldx r14,r14,r15 /* grab pgd entry */
351 rldicl r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3
354 bge tlb_miss_fault_e6500 /* Bad pgd entry or hugepage; bail */
355 ldx r14,r14,r15 /* grab pud entry */
357 rldicl r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3
360 bge tlb_miss_fault_e6500
361 ldx r14,r14,r15 /* Grab pmd entry */
365 bge tlb_miss_fault_e6500
367 /* Now we build the MAS for a 2M indirect page:
369 * MAS 0 : ESEL needs to be filled by software round-robin
370 * MAS 1 : Fully set up
371 * - PID already updated by caller if necessary
372 * - TSIZE for now is base ind page size always
373 * - TID already cleared if necessary
374 * MAS 2 : Default not 2M-aligned, need to be redone
375 * MAS 3+7 : Needs to be done
378 ori r14,r14,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
379 mtspr SPRN_MAS7_MAS3,r14
381 clrrdi r15,r16,21 /* make EA 2M-aligned */
384 lbz r15,TCD_ESEL_NEXT(r11)
385 lbz r16,TCD_ESEL_MAX(r11)
386 lbz r14,TCD_ESEL_FIRST(r11)
387 rlwimi r10,r15,16,0x00ff0000 /* insert esel_next into MAS0 */
388 addi r15,r15,1 /* increment esel_next */
391 iseleq r15,r14,r15 /* if next == last use first */
392 stb r15,TCD_ESEL_NEXT(r11)
397 .macro tlb_unlock_e6500
399 beq cr1,1f /* no unlock if lock was recursively grabbed */
404 END_FTR_SECTION_IFSET(CPU_FTR_SMT)
408 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
412 tlb_miss_kernel_e6500:
414 ld r14,PACA_KERNELPGD(r13)
415 cmpldi cr0,r15,8 /* Check for vmalloc region */
416 rlwinm r10,r10,0,16,1 /* Clear TID */
418 beq+ tlb_miss_common_e6500
420 tlb_miss_fault_e6500:
422 /* We need to check if it was an instruction miss */
424 bne itlb_miss_fault_e6500
425 dtlb_miss_fault_e6500:
426 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
428 b exc_data_storage_book3e
429 itlb_miss_fault_e6500:
430 TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
432 b exc_instruction_storage_book3e
433 #endif /* CONFIG_PPC_FSL_BOOK3E */
435 /**********************************************************************
437 * TLB miss handling for Book3E with TLB reservation and HES support *
439 **********************************************************************/
443 START_EXCEPTION(data_tlb_miss)
446 /* Now we handle the fault proper. We only save DEAR in normal
447 * fault case since that's the only interesting values here.
448 * We could probably also optimize by not saving SRR0/1 in the
449 * linear mapping case but I'll leave that for later
452 mfspr r16,SPRN_DEAR /* get faulting address */
453 srdi r15,r16,60 /* get region */
454 cmpldi cr0,r15,0xc /* linear mapping ? */
455 TLB_MISS_STATS_SAVE_INFO
456 beq tlb_load_linear /* yes -> go to linear map load */
458 /* The page tables are mapped virtually linear. At this point, though,
459 * we don't know whether we are trying to fault in a first level
460 * virtual address or a virtual page table address. We can get that
461 * from bit 0x1 of the region ID which we have set for a page table
464 bne- virt_page_table_tlb_miss
466 std r14,EX_TLB_ESR(r12); /* save ESR */
467 std r16,EX_TLB_DEAR(r12); /* save DEAR */
469 /* We need _PAGE_PRESENT and _PAGE_ACCESSED set */
471 oris r11,r11,_PAGE_ACCESSED@h
473 /* We do the user/kernel test for the PID here along with the RW test
475 cmpldi cr0,r15,0 /* Check for user region */
477 /* We pre-test some combination of permissions to avoid double
480 * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE
481 * ESR_ST is 0x00800000
482 * _PAGE_BAP_SW is 0x00000010
483 * So the shift is >> 19. This tests for supervisor writeability.
484 * If the page happens to be supervisor writeable and not user
485 * writeable, we will take a new fault later, but that should be
486 * a rare enough case.
488 * We also move ESR_ST in _PAGE_DIRTY position
489 * _PAGE_DIRTY is 0x00001000 so the shift is >> 11
491 * MAS1 is preset for all we need except for TID that needs to
492 * be cleared for kernel translations
494 rlwimi r11,r14,32-19,27,27
495 rlwimi r11,r14,32-16,19,19
497 /* XXX replace the RMW cycles with immediate loads + writes */
498 1: mfspr r10,SPRN_MAS1
499 cmpldi cr0,r15,8 /* Check for vmalloc region */
500 rlwinm r10,r10,0,16,1 /* Clear TID */
504 /* We got a crappy address, just fault with whatever DEAR and ESR
507 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
508 TLB_MISS_EPILOG_ERROR
509 b exc_data_storage_book3e
511 /* Instruction TLB miss */
512 START_EXCEPTION(instruction_tlb_miss)
515 /* If we take a recursive fault, the second level handler may need
516 * to know whether we are handling a data or instruction fault in
517 * order to get to the right store fault handler. We provide that
518 * info by writing a crazy value in ESR in our exception frame
520 li r14,-1 /* store to exception frame is done later */
522 /* Now we handle the fault proper. We only save DEAR in the non
523 * linear mapping case since we know the linear mapping case will
524 * not re-enter. We could indeed optimize and also not save SRR0/1
525 * in the linear mapping case but I'll leave that for later
527 * Faulting address is SRR0 which is already in r16
529 srdi r15,r16,60 /* get region */
530 cmpldi cr0,r15,0xc /* linear mapping ? */
531 TLB_MISS_STATS_SAVE_INFO
532 beq tlb_load_linear /* yes -> go to linear map load */
534 /* We do the user/kernel test for the PID here along with the RW test
536 li r11,_PAGE_PRESENT|_PAGE_EXEC /* Base perm */
537 oris r11,r11,_PAGE_ACCESSED@h
539 cmpldi cr0,r15,0 /* Check for user region */
540 std r14,EX_TLB_ESR(r12) /* write crazy -1 to frame */
543 li r11,_PAGE_PRESENT|_PAGE_BAP_SX /* Base perm */
544 oris r11,r11,_PAGE_ACCESSED@h
545 /* XXX replace the RMW cycles with immediate loads + writes */
547 cmpldi cr0,r15,8 /* Check for vmalloc region */
548 rlwinm r10,r10,0,16,1 /* Clear TID */
552 /* We got a crappy address, just fault */
553 TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
554 TLB_MISS_EPILOG_ERROR
555 b exc_instruction_storage_book3e
558 * This is the guts of the first-level TLB miss handler for direct
559 * misses. We are entered with:
561 * r16 = faulting address
563 * r14 = crap (free to use)
565 * r12 = TLB exception frame in PACA
566 * r11 = PTE permission mask
567 * r10 = crap (free to use)
570 /* So we first construct the page table address. We do that by
571 * shifting the bottom of the address (not the region ID) by
572 * PAGE_SHIFT-3, clearing the bottom 3 bits (get a PTE ptr) and
573 * or'ing the fourth high bit.
575 * NOTE: For 64K pages, we do things slightly differently in
576 * order to handle the weird page table format used by linux
579 #ifdef CONFIG_PPC_64K_PAGES
580 /* For the top bits, 16 bytes per PTE */
581 rldicl r14,r16,64-(PAGE_SHIFT-4),PAGE_SHIFT-4+4
582 /* Now create the bottom bits as 0 in position 0x8000 and
583 * the rest calculated for 8 bytes per PTE
585 rldicl r15,r16,64-(PAGE_SHIFT-3),64-15
586 /* Insert the bottom bits in */
587 rlwimi r14,r15,0,16,31
589 rldicl r14,r16,64-(PAGE_SHIFT-3),PAGE_SHIFT-3+4
595 BEGIN_MMU_FTR_SECTION
596 /* Set the TLB reservation and search for existing entry. Then load
599 PPC_TLBSRX_DOT(0,R16)
601 beq normal_tlb_miss_done
604 ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_USE_TLBRSRV)
606 finish_normal_tlb_miss:
607 /* Check if required permissions are met */
609 bne- normal_tlb_miss_access_fault
611 /* Now we build the MAS:
613 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
614 * MAS 1 : Almost fully setup
615 * - PID already updated by caller if necessary
616 * - TSIZE need change if !base page size, not
617 * yet implemented for now
618 * MAS 2 : Defaults not useful, need to be redone
619 * MAS 3+7 : Needs to be done
621 * TODO: mix up code below for better scheduling
623 clrrdi r11,r16,12 /* Clear low crap in EA */
624 rlwimi r11,r14,32-19,27,31 /* Insert WIMGE */
627 /* Check page size, if not standard, update MAS1 */
628 rldicl r11,r14,64-8,64-8
629 #ifdef CONFIG_PPC_64K_PAGES
630 cmpldi cr0,r11,BOOK3E_PAGESZ_64K
632 cmpldi cr0,r11,BOOK3E_PAGESZ_4K
636 rlwimi r11,r14,31,21,24
637 rlwinm r11,r11,0,21,19
640 /* Move RPN in position */
641 rldicr r11,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
642 clrldi r15,r11,12 /* Clear crap at the top */
643 rlwimi r15,r14,32-8,22,25 /* Move in U bits */
644 rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */
646 /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
647 andi. r11,r14,_PAGE_DIRTY
649 li r11,MAS3_SW|MAS3_UW
652 BEGIN_MMU_FTR_SECTION
657 mtspr SPRN_MAS7_MAS3,r15
658 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
662 normal_tlb_miss_done:
663 /* We don't bother with restoring DEAR or ESR since we know we are
664 * level 0 and just going back to userland. They are only needed
665 * if you are going to take an access fault
667 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
668 TLB_MISS_EPILOG_SUCCESS
671 normal_tlb_miss_access_fault:
672 /* We need to check if it was an instruction miss */
673 andi. r10,r11,_PAGE_EXEC
675 ld r14,EX_TLB_DEAR(r12)
676 ld r15,EX_TLB_ESR(r12)
679 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
680 TLB_MISS_EPILOG_ERROR
681 b exc_data_storage_book3e
682 1: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
683 TLB_MISS_EPILOG_ERROR
684 b exc_instruction_storage_book3e
688 * This is the guts of the second-level TLB miss handler for direct
689 * misses. We are entered with:
691 * r16 = virtual page table faulting address
692 * r15 = region (top 4 bits of address)
693 * r14 = crap (free to use)
695 * r12 = TLB exception frame in PACA
696 * r11 = crap (free to use)
697 * r10 = crap (free to use)
699 * Note that this should only ever be called as a second level handler
700 * with the current scheme when using SW load.
701 * That means we can always get the original fault DEAR at
702 * EX_TLB_DEAR-EX_TLB_SIZE(r12)
704 * It can be re-entered by the linear mapping miss handler. However, to
705 * avoid too much complication, it will restart the whole fault at level
706 * 0 so we don't care too much about clobbers
708 * XXX That code was written back when we couldn't clobber r14. We can now,
709 * so we could probably optimize things a bit
711 virt_page_table_tlb_miss:
712 /* Are we hitting a kernel page table ? */
715 /* The cool thing now is that r10 contains 0 for user and 8 for kernel,
716 * and we happen to have the swapper_pg_dir at offset 8 from the user
717 * pgdir in the PACA :-).
721 /* If kernel, we need to clear MAS1 TID */
723 /* XXX replace the RMW cycles with immediate loads + writes */
725 rlwinm r10,r10,0,16,1 /* Clear TID */
728 BEGIN_MMU_FTR_SECTION
729 /* Search if we already have a TLB entry for that virtual address, and
730 * if we do, bail out.
732 PPC_TLBSRX_DOT(0,R16)
733 beq virt_page_table_tlb_miss_done
734 END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
736 /* Now, we need to walk the page tables. First check if we are in
739 rldicl. r10,r16,64-(VPTE_INDEX_SIZE+3),VPTE_INDEX_SIZE+3+4
740 bne- virt_page_table_tlb_miss_fault
742 /* Get the PGD pointer */
745 beq- virt_page_table_tlb_miss_fault
747 /* Get to PGD entry */
748 rldicl r11,r16,64-VPTE_PGD_SHIFT,64-PGD_INDEX_SIZE-3
752 bge virt_page_table_tlb_miss_fault
754 #ifndef CONFIG_PPC_64K_PAGES
755 /* Get to PUD entry */
756 rldicl r11,r16,64-VPTE_PUD_SHIFT,64-PUD_INDEX_SIZE-3
760 bge virt_page_table_tlb_miss_fault
761 #endif /* CONFIG_PPC_64K_PAGES */
763 /* Get to PMD entry */
764 rldicl r11,r16,64-VPTE_PMD_SHIFT,64-PMD_INDEX_SIZE-3
768 bge virt_page_table_tlb_miss_fault
770 /* Ok, we're all right, we can now create a kernel translation for
771 * a 4K or 64K page from r16 -> r15.
773 /* Now we build the MAS:
775 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
776 * MAS 1 : Almost fully setup
777 * - PID already updated by caller if necessary
778 * - TSIZE for now is base page size always
779 * MAS 2 : Use defaults
780 * MAS 3+7 : Needs to be done
782 * So we only do MAS 2 and 3 for now...
784 clrldi r11,r15,4 /* remove region ID from RPN */
785 ori r10,r11,1 /* Or-in SR */
787 BEGIN_MMU_FTR_SECTION
792 mtspr SPRN_MAS7_MAS3,r10
793 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
797 BEGIN_MMU_FTR_SECTION
798 virt_page_table_tlb_miss_done:
800 /* We have overriden MAS2:EPN but currently our primary TLB miss
801 * handler will always restore it so that should not be an issue,
802 * if we ever optimize the primary handler to not write MAS2 on
803 * some cases, we'll have to restore MAS2:EPN here based on the
804 * original fault's DEAR. If we do that we have to modify the
805 * ITLB miss handler to also store SRR0 in the exception frame
808 * However, one nasty thing we did is we cleared the reservation
809 * (well, potentially we did). We do a trick here thus if we
810 * are not a level 0 exception (we interrupted the TLB miss) we
811 * offset the return address by -4 in order to replay the tlbsrx
815 cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE
817 ld r11,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
819 std r10,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
821 END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
822 /* Return to caller, normal case */
823 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK);
824 TLB_MISS_EPILOG_SUCCESS
827 virt_page_table_tlb_miss_fault:
828 /* If we fault here, things are a little bit tricky. We need to call
829 * either data or instruction store fault, and we need to retrieve
830 * the original fault address and ESR (for data).
832 * The thing is, we know that in normal circumstances, this is
833 * always called as a second level tlb miss for SW load or as a first
834 * level TLB miss for HW load, so we should be able to peek at the
835 * relevant information in the first exception frame in the PACA.
837 * However, we do need to double check that, because we may just hit
838 * a stray kernel pointer or a userland attack trying to hit those
839 * areas. If that is the case, we do a data fault. (We can't get here
840 * from an instruction tlb miss anyway).
842 * Note also that when going to a fault, we must unwind the previous
843 * level as well. Since we are doing that, we don't need to clear or
844 * restore the TLB reservation neither.
847 cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE
848 bne- virt_page_table_tlb_miss_whacko_fault
850 /* We dig the original DEAR and ESR from slot 0 */
851 ld r15,EX_TLB_DEAR+PACA_EXTLB(r13)
852 ld r16,EX_TLB_ESR+PACA_EXTLB(r13)
854 /* We check for the "special" ESR value for instruction faults */
859 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT);
860 TLB_MISS_EPILOG_ERROR
861 b exc_data_storage_book3e
862 1: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT);
863 TLB_MISS_EPILOG_ERROR
864 b exc_instruction_storage_book3e
866 virt_page_table_tlb_miss_whacko_fault:
867 /* The linear fault will restart everything so ESR and DEAR will
868 * not have been clobbered, let's just fault with what we have
870 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_FAULT);
871 TLB_MISS_EPILOG_ERROR
872 b exc_data_storage_book3e
875 /**************************************************************
877 * TLB miss handling for Book3E with hw page table support *
879 **************************************************************/
883 START_EXCEPTION(data_tlb_miss_htw)
886 /* Now we handle the fault proper. We only save DEAR in normal
887 * fault case since that's the only interesting values here.
888 * We could probably also optimize by not saving SRR0/1 in the
889 * linear mapping case but I'll leave that for later
892 mfspr r16,SPRN_DEAR /* get faulting address */
893 srdi r11,r16,60 /* get region */
894 cmpldi cr0,r11,0xc /* linear mapping ? */
895 TLB_MISS_STATS_SAVE_INFO
896 beq tlb_load_linear /* yes -> go to linear map load */
898 /* We do the user/kernel test for the PID here along with the RW test
900 cmpldi cr0,r11,0 /* Check for user region */
901 ld r15,PACAPGD(r13) /* Load user pgdir */
904 /* XXX replace the RMW cycles with immediate loads + writes */
905 1: mfspr r10,SPRN_MAS1
906 cmpldi cr0,r11,8 /* Check for vmalloc region */
907 rlwinm r10,r10,0,16,1 /* Clear TID */
909 ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
912 /* We got a crappy address, just fault with whatever DEAR and ESR
915 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
916 TLB_MISS_EPILOG_ERROR
917 b exc_data_storage_book3e
919 /* Instruction TLB miss */
920 START_EXCEPTION(instruction_tlb_miss_htw)
923 /* If we take a recursive fault, the second level handler may need
924 * to know whether we are handling a data or instruction fault in
925 * order to get to the right store fault handler. We provide that
926 * info by keeping a crazy value for ESR in r14
928 li r14,-1 /* store to exception frame is done later */
930 /* Now we handle the fault proper. We only save DEAR in the non
931 * linear mapping case since we know the linear mapping case will
932 * not re-enter. We could indeed optimize and also not save SRR0/1
933 * in the linear mapping case but I'll leave that for later
935 * Faulting address is SRR0 which is already in r16
937 srdi r11,r16,60 /* get region */
938 cmpldi cr0,r11,0xc /* linear mapping ? */
939 TLB_MISS_STATS_SAVE_INFO
940 beq tlb_load_linear /* yes -> go to linear map load */
942 /* We do the user/kernel test for the PID here along with the RW test
944 cmpldi cr0,r11,0 /* Check for user region */
945 ld r15,PACAPGD(r13) /* Load user pgdir */
948 /* XXX replace the RMW cycles with immediate loads + writes */
949 1: mfspr r10,SPRN_MAS1
950 cmpldi cr0,r11,8 /* Check for vmalloc region */
951 rlwinm r10,r10,0,16,1 /* Clear TID */
953 ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
956 /* We got a crappy address, just fault */
957 TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
958 TLB_MISS_EPILOG_ERROR
959 b exc_instruction_storage_book3e
963 * This is the guts of the second-level TLB miss handler for direct
964 * misses. We are entered with:
966 * r16 = virtual page table faulting address
970 * r12 = TLB exception frame in PACA
971 * r11 = crap (free to use)
972 * r10 = crap (free to use)
974 * It can be re-entered by the linear mapping miss handler. However, to
975 * avoid too much complication, it will save/restore things for us
978 /* Search if we already have a TLB entry for that virtual address, and
979 * if we do, bail out.
981 * MAS1:IND should be already set based on MAS4
983 PPC_TLBSRX_DOT(0,R16)
984 beq htw_tlb_miss_done
986 /* Now, we need to walk the page tables. First check if we are in
989 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
990 bne- htw_tlb_miss_fault
992 /* Get the PGD pointer */
994 beq- htw_tlb_miss_fault
996 /* Get to PGD entry */
997 rldicl r11,r16,64-(PGDIR_SHIFT-3),64-PGD_INDEX_SIZE-3
1001 bge htw_tlb_miss_fault
1003 #ifndef CONFIG_PPC_64K_PAGES
1004 /* Get to PUD entry */
1005 rldicl r11,r16,64-(PUD_SHIFT-3),64-PUD_INDEX_SIZE-3
1009 bge htw_tlb_miss_fault
1010 #endif /* CONFIG_PPC_64K_PAGES */
1012 /* Get to PMD entry */
1013 rldicl r11,r16,64-(PMD_SHIFT-3),64-PMD_INDEX_SIZE-3
1017 bge htw_tlb_miss_fault
1019 /* Ok, we're all right, we can now create an indirect entry for
1020 * a 1M or 256M page.
1022 * The last trick is now that because we use "half" pages for
1023 * the HTW (1M IND is 2K and 256M IND is 32K) we need to account
1024 * for an added LSB bit to the RPN. For 64K pages, there is no
1025 * problem as we already use 32K arrays (half PTE pages), but for
1026 * 4K page we need to extract a bit from the virtual address and
1027 * insert it into the "PA52" bit of the RPN.
1029 #ifndef CONFIG_PPC_64K_PAGES
1030 rlwimi r15,r16,32-9,20,20
1032 /* Now we build the MAS:
1034 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
1035 * MAS 1 : Almost fully setup
1036 * - PID already updated by caller if necessary
1037 * - TSIZE for now is base ind page size always
1038 * MAS 2 : Use defaults
1039 * MAS 3+7 : Needs to be done
1041 #ifdef CONFIG_PPC_64K_PAGES
1042 ori r10,r15,(BOOK3E_PAGESZ_64K << MAS3_SPSIZE_SHIFT)
1044 ori r10,r15,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
1047 BEGIN_MMU_FTR_SECTION
1051 MMU_FTR_SECTION_ELSE
1052 mtspr SPRN_MAS7_MAS3,r10
1053 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
1058 /* We don't bother with restoring DEAR or ESR since we know we are
1059 * level 0 and just going back to userland. They are only needed
1060 * if you are going to take an access fault
1062 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK)
1063 TLB_MISS_EPILOG_SUCCESS
1067 /* We need to check if it was an instruction miss. We know this
1068 * though because r14 would contain -1
1074 TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT)
1075 TLB_MISS_EPILOG_ERROR
1076 b exc_data_storage_book3e
1077 1: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT)
1078 TLB_MISS_EPILOG_ERROR
1079 b exc_instruction_storage_book3e
1082 * This is the guts of "any" level TLB miss handler for kernel linear
1083 * mapping misses. We are entered with:
1086 * r16 = faulting address
1087 * r15 = crap (free to use)
1088 * r14 = ESR (data) or -1 (instruction)
1090 * r12 = TLB exception frame in PACA
1091 * r11 = crap (free to use)
1092 * r10 = crap (free to use)
1094 * In addition we know that we will not re-enter, so in theory, we could
1095 * use a simpler epilog not restoring SRR0/1 etc.. but we'll do that later.
1097 * We also need to be careful about MAS registers here & TLB reservation,
1098 * as we know we'll have clobbered them if we interrupt the main TLB miss
1099 * handlers in which case we probably want to do a full restart at level
1100 * 0 rather than saving / restoring the MAS.
1102 * Note: If we care about performance of that core, we can easily shuffle
1103 * a few things around
1106 /* For now, we assume the linear mapping is contiguous and stops at
1107 * linear_map_top. We also assume the size is a multiple of 1G, thus
1108 * we only use 1G pages for now. That might have to be changed in a
1109 * final implementation, especially when dealing with hypervisors
1112 ld r11,linear_map_top@got(r11)
1116 bge tlb_load_linear_fault
1118 /* MAS1 need whole new setup. */
1119 li r15,(BOOK3E_PAGESZ_1GB<<MAS1_TSIZE_SHIFT)
1120 oris r15,r15,MAS1_VALID@h /* MAS1 needs V and TSIZE */
1123 /* Already somebody there ? */
1124 PPC_TLBSRX_DOT(0,R16)
1125 beq tlb_load_linear_done
1127 /* Now we build the remaining MAS. MAS0 and 2 should be fine
1128 * with their defaults, which leaves us with MAS 3 and 7. The
1129 * mapping is linear, so we just take the address, clear the
1130 * region bits, and or in the permission bits which are currently
1133 clrrdi r10,r16,30 /* 1G page index */
1134 clrldi r10,r10,4 /* clear region bits */
1135 ori r10,r10,MAS3_SR|MAS3_SW|MAS3_SX
1137 BEGIN_MMU_FTR_SECTION
1141 MMU_FTR_SECTION_ELSE
1142 mtspr SPRN_MAS7_MAS3,r10
1143 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
1147 tlb_load_linear_done:
1148 /* We use the "error" epilog for success as we do want to
1149 * restore to the initial faulting context, whatever it was.
1150 * We do that because we can't resume a fault within a TLB
1151 * miss handler, due to MAS and TLB reservation being clobbered.
1153 TLB_MISS_STATS_X(MMSTAT_TLB_MISS_LINEAR)
1154 TLB_MISS_EPILOG_ERROR
1157 tlb_load_linear_fault:
1158 /* We keep the DEAR and ESR around, this shouldn't have happened */
1161 TLB_MISS_EPILOG_ERROR_SPECIAL
1162 b exc_data_storage_book3e
1163 1: TLB_MISS_EPILOG_ERROR_SPECIAL
1164 b exc_instruction_storage_book3e
1167 #ifdef CONFIG_BOOK3E_MMU_TLB_STATS