1/* SPDX-License-Identifier: GPL-2.0-or-later */ 2/* 3 * Low level TLB miss handlers for Book3E 4 * 5 * Copyright (C) 2008-2009 6 * Ben. Herrenschmidt (benh@kernel.crashing.org), IBM Corp. 7 */ 8 9#include <linux/pgtable.h> 10#include <asm/processor.h> 11#include <asm/reg.h> 12#include <asm/page.h> 13#include <asm/mmu.h> 14#include <asm/ppc_asm.h> 15#include <asm/asm-offsets.h> 16#include <asm/cputable.h> 17#include <asm/exception-64e.h> 18#include <asm/ppc-opcode.h> 19#include <asm/kvm_asm.h> 20#include <asm/kvm_booke_hv_asm.h> 21#include <asm/feature-fixups.h> 22 23#define VPTE_PMD_SHIFT (PTE_INDEX_SIZE) 24#define VPTE_PUD_SHIFT (VPTE_PMD_SHIFT + PMD_INDEX_SIZE) 25#define VPTE_PGD_SHIFT (VPTE_PUD_SHIFT + PUD_INDEX_SIZE) 26#define VPTE_INDEX_SIZE (VPTE_PGD_SHIFT + PGD_INDEX_SIZE) 27 28/********************************************************************** 29 * * 30 * TLB miss handling for Book3E with a bolted linear mapping * 31 * No virtual page table, no nested TLB misses * 32 * * 33 **********************************************************************/ 34 35/* 36 * Note that, unlike non-bolted handlers, TLB_EXFRAME is not 37 * modified by the TLB miss handlers themselves, since the TLB miss 38 * handler code will not itself cause a recursive TLB miss. 39 * 40 * TLB_EXFRAME will be modified when crit/mc/debug exceptions are 41 * entered/exited. 42 */ 43.macro tlb_prolog_bolted intnum addr 44 mtspr SPRN_SPRG_GEN_SCRATCH,r12 45 mfspr r12,SPRN_SPRG_TLB_EXFRAME 46 std r13,EX_TLB_R13(r12) 47 std r10,EX_TLB_R10(r12) 48 mfspr r13,SPRN_SPRG_PACA 49 50 mfcr r10 51 std r11,EX_TLB_R11(r12) 52#ifdef CONFIG_KVM_BOOKE_HV 53BEGIN_FTR_SECTION 54 mfspr r11, SPRN_SRR1 55END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV) 56#endif 57 DO_KVM \intnum, SPRN_SRR1 58 std r16,EX_TLB_R16(r12) 59 mfspr r16,\addr /* get faulting address */ 60 std r14,EX_TLB_R14(r12) 61 ld r14,PACAPGD(r13) 62 std r15,EX_TLB_R15(r12) 63 std r10,EX_TLB_CR(r12) 64#ifdef CONFIG_PPC_FSL_BOOK3E 65START_BTB_FLUSH_SECTION 66 mfspr r11, SPRN_SRR1 67 andi. r10,r11,MSR_PR 68 beq 1f 69 BTB_FLUSH(r10) 701: 71END_BTB_FLUSH_SECTION 72 std r7,EX_TLB_R7(r12) 73#endif 74.endm 75 76.macro tlb_epilog_bolted 77 ld r14,EX_TLB_CR(r12) 78#ifdef CONFIG_PPC_FSL_BOOK3E 79 ld r7,EX_TLB_R7(r12) 80#endif 81 ld r10,EX_TLB_R10(r12) 82 ld r11,EX_TLB_R11(r12) 83 ld r13,EX_TLB_R13(r12) 84 mtcr r14 85 ld r14,EX_TLB_R14(r12) 86 ld r15,EX_TLB_R15(r12) 87 ld r16,EX_TLB_R16(r12) 88 mfspr r12,SPRN_SPRG_GEN_SCRATCH 89.endm 90 91/* Data TLB miss */ 92 START_EXCEPTION(data_tlb_miss_bolted) 93 tlb_prolog_bolted BOOKE_INTERRUPT_DTLB_MISS SPRN_DEAR 94 95 /* We need _PAGE_PRESENT and _PAGE_ACCESSED set */ 96 97 /* We do the user/kernel test for the PID here along with the RW test 98 */ 99 /* We pre-test some combination of permissions to avoid double 100 * faults: 101 * 102 * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE 103 * ESR_ST is 0x00800000 104 * _PAGE_BAP_SW is 0x00000010 105 * So the shift is >> 19. This tests for supervisor writeability. 106 * If the page happens to be supervisor writeable and not user 107 * writeable, we will take a new fault later, but that should be 108 * a rare enough case. 109 * 110 * We also move ESR_ST in _PAGE_DIRTY position 111 * _PAGE_DIRTY is 0x00001000 so the shift is >> 11 112 * 113 * MAS1 is preset for all we need except for TID that needs to 114 * be cleared for kernel translations 115 */ 116 117 mfspr r11,SPRN_ESR 118 119 srdi r15,r16,60 /* get region */ 120 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4 121 bne- dtlb_miss_fault_bolted /* Bail if fault addr is invalid */ 122 123 rlwinm r10,r11,32-19,27,27 124 rlwimi r10,r11,32-16,19,19 125 cmpwi r15,0 /* user vs kernel check */ 126 ori r10,r10,_PAGE_PRESENT 127 oris r11,r10,_PAGE_ACCESSED@h 128 129 bne tlb_miss_kernel_bolted 130 131tlb_miss_user_bolted: 132#ifdef CONFIG_PPC_KUAP 133 mfspr r10,SPRN_MAS1 134 rlwinm. r10,r10,0,0x3fff0000 135 beq- tlb_miss_fault_bolted /* KUAP fault */ 136#endif 137 138tlb_miss_common_bolted: 139/* 140 * This is the guts of the TLB miss handler for bolted-linear. 141 * We are entered with: 142 * 143 * r16 = faulting address 144 * r15 = crap (free to use) 145 * r14 = page table base 146 * r13 = PACA 147 * r11 = PTE permission mask 148 * r10 = crap (free to use) 149 */ 150 rldicl r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3 151 cmpldi cr0,r14,0 152 clrrdi r15,r15,3 153 beq tlb_miss_fault_bolted /* No PGDIR, bail */ 154 155 ldx r14,r14,r15 /* grab pgd entry */ 156 157 rldicl r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3 158 clrrdi r15,r15,3 159 cmpdi cr0,r14,0 160 bge tlb_miss_fault_bolted /* Bad pgd entry or hugepage; bail */ 161 ldx r14,r14,r15 /* grab pud entry */ 162 163 rldicl r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3 164 clrrdi r15,r15,3 165 cmpdi cr0,r14,0 166 bge tlb_miss_fault_bolted 167 ldx r14,r14,r15 /* Grab pmd entry */ 168 169 rldicl r15,r16,64-PAGE_SHIFT+3,64-PTE_INDEX_SIZE-3 170 clrrdi r15,r15,3 171 cmpdi cr0,r14,0 172 bge tlb_miss_fault_bolted 173 ldx r14,r14,r15 /* Grab PTE, normal (!huge) page */ 174 175 /* Check if required permissions are met */ 176 andc. r15,r11,r14 177 rldicr r15,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT 178 bne- tlb_miss_fault_bolted 179 180 /* Now we build the MAS: 181 * 182 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG 183 * MAS 1 : Almost fully setup 184 * - PID already updated by caller if necessary 185 * - TSIZE need change if !base page size, not 186 * yet implemented for now 187 * MAS 2 : Defaults not useful, need to be redone 188 * MAS 3+7 : Needs to be done 189 */ 190 clrrdi r11,r16,12 /* Clear low crap in EA */ 191 clrldi r15,r15,12 /* Clear crap at the top */ 192 rlwimi r11,r14,32-19,27,31 /* Insert WIMGE */ 193 rlwimi r15,r14,32-8,22,25 /* Move in U bits */ 194 mtspr SPRN_MAS2,r11 195 andi. r11,r14,_PAGE_DIRTY 196 rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */ 197 198 /* Mask out SW and UW if !DIRTY (XXX optimize this !) */ 199 bne 1f 200 li r11,MAS3_SW|MAS3_UW 201 andc r15,r15,r11 2021: 203 mtspr SPRN_MAS7_MAS3,r15 204 tlbwe 205 206tlb_miss_done_bolted: 207 tlb_epilog_bolted 208 rfi 209 210itlb_miss_kernel_bolted: 211 li r11,_PAGE_PRESENT|_PAGE_BAP_SX /* Base perm */ 212 oris r11,r11,_PAGE_ACCESSED@h 213tlb_miss_kernel_bolted: 214 mfspr r10,SPRN_MAS1 215 ld r14,PACA_KERNELPGD(r13) 216 srdi r15,r16,44 /* get kernel region */ 217 andi. r15,r15,1 /* Check for vmalloc region */ 218 rlwinm r10,r10,0,16,1 /* Clear TID */ 219 mtspr SPRN_MAS1,r10 220 bne+ tlb_miss_common_bolted 221 222tlb_miss_fault_bolted: 223 /* We need to check if it was an instruction miss */ 224 andi. r10,r11,_PAGE_BAP_UX|_PAGE_BAP_SX 225 bne itlb_miss_fault_bolted 226dtlb_miss_fault_bolted: 227 tlb_epilog_bolted 228 b exc_data_storage_book3e 229itlb_miss_fault_bolted: 230 tlb_epilog_bolted 231 b exc_instruction_storage_book3e 232 233/* Instruction TLB miss */ 234 START_EXCEPTION(instruction_tlb_miss_bolted) 235 tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0 236 237 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4 238 srdi r15,r16,60 /* get region */ 239 bne- itlb_miss_fault_bolted 240 241 li r11,_PAGE_PRESENT|_PAGE_BAP_UX /* Base perm */ 242 243 /* We do the user/kernel test for the PID here along with the RW test 244 */ 245 246 cmpldi cr0,r15,0 /* Check for user region */ 247 oris r11,r11,_PAGE_ACCESSED@h 248 beq tlb_miss_user_bolted 249 b itlb_miss_kernel_bolted 250 251#ifdef CONFIG_PPC_FSL_BOOK3E 252/* 253 * TLB miss handling for e6500 and derivatives, using hardware tablewalk. 254 * 255 * Linear mapping is bolted: no virtual page table or nested TLB misses 256 * Indirect entries in TLB1, hardware loads resulting direct entries 257 * into TLB0 258 * No HES or NV hint on TLB1, so we need to do software round-robin 259 * No tlbsrx. so we need a spinlock, and we have to deal 260 * with MAS-damage caused by tlbsx 261 * 4K pages only 262 */ 263 264 START_EXCEPTION(instruction_tlb_miss_e6500) 265 tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0 266 267 ld r11,PACA_TCD_PTR(r13) 268 srdi. r15,r16,60 /* get region */ 269 ori r16,r16,1 270 271 bne tlb_miss_kernel_e6500 /* user/kernel test */ 272 273 b tlb_miss_common_e6500 274 275 START_EXCEPTION(data_tlb_miss_e6500) 276 tlb_prolog_bolted BOOKE_INTERRUPT_DTLB_MISS SPRN_DEAR 277 278 ld r11,PACA_TCD_PTR(r13) 279 srdi. r15,r16,60 /* get region */ 280 rldicr r16,r16,0,62 281 282 bne tlb_miss_kernel_e6500 /* user vs kernel check */ 283 284/* 285 * This is the guts of the TLB miss handler for e6500 and derivatives. 286 * We are entered with: 287 * 288 * r16 = page of faulting address (low bit 0 if data, 1 if instruction) 289 * r15 = crap (free to use) 290 * r14 = page table base 291 * r13 = PACA 292 * r11 = tlb_per_core ptr 293 * r10 = crap (free to use) 294 * r7 = esel_next 295 */ 296tlb_miss_common_e6500: 297 crmove cr2*4+2,cr0*4+2 /* cr2.eq != 0 if kernel address */ 298 299BEGIN_FTR_SECTION /* CPU_FTR_SMT */ 300 /* 301 * Search if we already have an indirect entry for that virtual 302 * address, and if we do, bail out. 303 * 304 * MAS6:IND should be already set based on MAS4 305 */ 306 lhz r10,PACAPACAINDEX(r13) 307 addi r10,r10,1 308 crclr cr1*4+eq /* set cr1.eq = 0 for non-recursive */ 3091: lbarx r15,0,r11 310 cmpdi r15,0 311 bne 2f 312 stbcx. r10,0,r11 313 bne 1b 3143: 315 .subsection 1 3162: cmpd cr1,r15,r10 /* recursive lock due to mcheck/crit/etc? */ 317 beq cr1,3b /* unlock will happen if cr1.eq = 0 */ 31810: lbz r15,0(r11) 319 cmpdi r15,0 320 bne 10b 321 b 1b 322 .previous 323END_FTR_SECTION_IFSET(CPU_FTR_SMT) 324 325 lbz r7,TCD_ESEL_NEXT(r11) 326 327BEGIN_FTR_SECTION /* CPU_FTR_SMT */ 328 /* 329 * Erratum A-008139 says that we can't use tlbwe to change 330 * an indirect entry in any way (including replacing or 331 * invalidating) if the other thread could be in the process 332 * of a lookup. The workaround is to invalidate the entry 333 * with tlbilx before overwriting. 334 */ 335 336 rlwinm r10,r7,16,0xff0000 337 oris r10,r10,MAS0_TLBSEL(1)@h 338 mtspr SPRN_MAS0,r10 339 isync 340 tlbre 341 mfspr r15,SPRN_MAS1 342 andis. r15,r15,MAS1_VALID@h 343 beq 5f 344 345BEGIN_FTR_SECTION_NESTED(532) 346 mfspr r10,SPRN_MAS8 347 rlwinm r10,r10,0,0x80000fff /* tgs,tlpid -> sgs,slpid */ 348 mtspr SPRN_MAS5,r10 349END_FTR_SECTION_NESTED(CPU_FTR_EMB_HV,CPU_FTR_EMB_HV,532) 350 351 mfspr r10,SPRN_MAS1 352 rlwinm r15,r10,0,0x3fff0000 /* tid -> spid */ 353 rlwimi r15,r10,20,0x00000003 /* ind,ts -> sind,sas */ 354 mfspr r10,SPRN_MAS6 355 mtspr SPRN_MAS6,r15 356 357 mfspr r15,SPRN_MAS2 358 isync 359 tlbilxva 0,r15 360 isync 361 362 mtspr SPRN_MAS6,r10 363 3645: 365BEGIN_FTR_SECTION_NESTED(532) 366 li r10,0 367 mtspr SPRN_MAS8,r10 368 mtspr SPRN_MAS5,r10 369END_FTR_SECTION_NESTED(CPU_FTR_EMB_HV,CPU_FTR_EMB_HV,532) 370 371 tlbsx 0,r16 372 mfspr r10,SPRN_MAS1 373 andis. r15,r10,MAS1_VALID@h 374 bne tlb_miss_done_e6500 375FTR_SECTION_ELSE 376 mfspr r10,SPRN_MAS1 377ALT_FTR_SECTION_END_IFSET(CPU_FTR_SMT) 378 379 oris r10,r10,MAS1_VALID@h 380 beq cr2,4f 381 rlwinm r10,r10,0,16,1 /* Clear TID */ 3824: mtspr SPRN_MAS1,r10 383 384 /* Now, we need to walk the page tables. First check if we are in 385 * range. 386 */ 387 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4 388 bne- tlb_miss_fault_e6500 389 390 rldicl r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3 391 cmpldi cr0,r14,0 392 clrrdi r15,r15,3 393 beq- tlb_miss_fault_e6500 /* No PGDIR, bail */ 394 ldx r14,r14,r15 /* grab pgd entry */ 395 396 rldicl r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3 397 clrrdi r15,r15,3 398 cmpdi cr0,r14,0 399 bge tlb_miss_huge_e6500 /* Bad pgd entry or hugepage; bail */ 400 ldx r14,r14,r15 /* grab pud entry */ 401 402 rldicl r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3 403 clrrdi r15,r15,3 404 cmpdi cr0,r14,0 405 bge tlb_miss_huge_e6500 406 ldx r14,r14,r15 /* Grab pmd entry */ 407 408 mfspr r10,SPRN_MAS0 409 cmpdi cr0,r14,0 410 bge tlb_miss_huge_e6500 411 412 /* Now we build the MAS for a 2M indirect page: 413 * 414 * MAS 0 : ESEL needs to be filled by software round-robin 415 * MAS 1 : Fully set up 416 * - PID already updated by caller if necessary 417 * - TSIZE for now is base ind page size always 418 * - TID already cleared if necessary 419 * MAS 2 : Default not 2M-aligned, need to be redone 420 * MAS 3+7 : Needs to be done 421 */ 422 423 ori r14,r14,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT) 424 mtspr SPRN_MAS7_MAS3,r14 425 426 clrrdi r15,r16,21 /* make EA 2M-aligned */ 427 mtspr SPRN_MAS2,r15 428 429tlb_miss_huge_done_e6500: 430 lbz r16,TCD_ESEL_MAX(r11) 431 lbz r14,TCD_ESEL_FIRST(r11) 432 rlwimi r10,r7,16,0x00ff0000 /* insert esel_next into MAS0 */ 433 addi r7,r7,1 /* increment esel_next */ 434 mtspr SPRN_MAS0,r10 435 cmpw r7,r16 436 iseleq r7,r14,r7 /* if next == last use first */ 437 stb r7,TCD_ESEL_NEXT(r11) 438 439 tlbwe 440 441tlb_miss_done_e6500: 442 .macro tlb_unlock_e6500 443BEGIN_FTR_SECTION 444 beq cr1,1f /* no unlock if lock was recursively grabbed */ 445 li r15,0 446 isync 447 stb r15,0(r11) 4481: 449END_FTR_SECTION_IFSET(CPU_FTR_SMT) 450 .endm 451 452 tlb_unlock_e6500 453 tlb_epilog_bolted 454 rfi 455 456tlb_miss_huge_e6500: 457 beq tlb_miss_fault_e6500 458 li r10,1 459 andi. r15,r14,HUGEPD_SHIFT_MASK@l /* r15 = psize */ 460 rldimi r14,r10,63,0 /* Set PD_HUGE */ 461 xor r14,r14,r15 /* Clear size bits */ 462 ldx r14,0,r14 463 464 /* 465 * Now we build the MAS for a huge page. 466 * 467 * MAS 0 : ESEL needs to be filled by software round-robin 468 * - can be handled by indirect code 469 * MAS 1 : Need to clear IND and set TSIZE 470 * MAS 2,3+7: Needs to be redone similar to non-tablewalk handler 471 */ 472 473 subi r15,r15,10 /* Convert psize to tsize */ 474 mfspr r10,SPRN_MAS1 475 rlwinm r10,r10,0,~MAS1_IND 476 rlwimi r10,r15,MAS1_TSIZE_SHIFT,MAS1_TSIZE_MASK 477 mtspr SPRN_MAS1,r10 478 479 li r10,-0x400 480 sld r15,r10,r15 /* Generate mask based on size */ 481 and r10,r16,r15 482 rldicr r15,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT 483 rlwimi r10,r14,32-19,27,31 /* Insert WIMGE */ 484 clrldi r15,r15,PAGE_SHIFT /* Clear crap at the top */ 485 rlwimi r15,r14,32-8,22,25 /* Move in U bits */ 486 mtspr SPRN_MAS2,r10 487 andi. r10,r14,_PAGE_DIRTY 488 rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */ 489 490 /* Mask out SW and UW if !DIRTY (XXX optimize this !) */ 491 bne 1f 492 li r10,MAS3_SW|MAS3_UW 493 andc r15,r15,r10 4941: 495 mtspr SPRN_MAS7_MAS3,r15 496 497 mfspr r10,SPRN_MAS0 498 b tlb_miss_huge_done_e6500 499 500tlb_miss_kernel_e6500: 501 ld r14,PACA_KERNELPGD(r13) 502 srdi r15,r16,44 /* get kernel region */ 503 xoris r15,r15,0xc /* Check for vmalloc region */ 504 cmplwi cr1,r15,1 505 beq+ cr1,tlb_miss_common_e6500 506 507tlb_miss_fault_e6500: 508 tlb_unlock_e6500 509 /* We need to check if it was an instruction miss */ 510 andi. r16,r16,1 511 bne itlb_miss_fault_e6500 512dtlb_miss_fault_e6500: 513 tlb_epilog_bolted 514 b exc_data_storage_book3e 515itlb_miss_fault_e6500: 516 tlb_epilog_bolted 517 b exc_instruction_storage_book3e 518#endif /* CONFIG_PPC_FSL_BOOK3E */ 519 520/********************************************************************** 521 * * 522 * TLB miss handling for Book3E with TLB reservation and HES support * 523 * * 524 **********************************************************************/ 525 526 527/* Data TLB miss */ 528 START_EXCEPTION(data_tlb_miss) 529 TLB_MISS_PROLOG 530 531 /* Now we handle the fault proper. We only save DEAR in normal 532 * fault case since that's the only interesting values here. 533 * We could probably also optimize by not saving SRR0/1 in the 534 * linear mapping case but I'll leave that for later 535 */ 536 mfspr r14,SPRN_ESR 537 mfspr r16,SPRN_DEAR /* get faulting address */ 538 srdi r15,r16,44 /* get region */ 539 xoris r15,r15,0xc 540 cmpldi cr0,r15,0 /* linear mapping ? */ 541 beq tlb_load_linear /* yes -> go to linear map load */ 542 cmpldi cr1,r15,1 /* vmalloc mapping ? */ 543 544 /* The page tables are mapped virtually linear. At this point, though, 545 * we don't know whether we are trying to fault in a first level 546 * virtual address or a virtual page table address. We can get that 547 * from bit 0x1 of the region ID which we have set for a page table 548 */ 549 andis. r10,r15,0x1 550 bne- virt_page_table_tlb_miss 551 552 std r14,EX_TLB_ESR(r12); /* save ESR */ 553 std r16,EX_TLB_DEAR(r12); /* save DEAR */ 554 555 /* We need _PAGE_PRESENT and _PAGE_ACCESSED set */ 556 li r11,_PAGE_PRESENT 557 oris r11,r11,_PAGE_ACCESSED@h 558 559 /* We do the user/kernel test for the PID here along with the RW test 560 */ 561 srdi. r15,r16,60 /* Check for user region */ 562 563 /* We pre-test some combination of permissions to avoid double 564 * faults: 565 * 566 * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE 567 * ESR_ST is 0x00800000 568 * _PAGE_BAP_SW is 0x00000010 569 * So the shift is >> 19. This tests for supervisor writeability. 570 * If the page happens to be supervisor writeable and not user 571 * writeable, we will take a new fault later, but that should be 572 * a rare enough case. 573 * 574 * We also move ESR_ST in _PAGE_DIRTY position 575 * _PAGE_DIRTY is 0x00001000 so the shift is >> 11 576 * 577 * MAS1 is preset for all we need except for TID that needs to 578 * be cleared for kernel translations 579 */ 580 rlwimi r11,r14,32-19,27,27 581 rlwimi r11,r14,32-16,19,19 582 beq normal_tlb_miss_user 583 /* XXX replace the RMW cycles with immediate loads + writes */ 5841: mfspr r10,SPRN_MAS1 585 rlwinm r10,r10,0,16,1 /* Clear TID */ 586 mtspr SPRN_MAS1,r10 587 beq+ cr1,normal_tlb_miss 588 589 /* We got a crappy address, just fault with whatever DEAR and ESR 590 * are here 591 */ 592 TLB_MISS_EPILOG_ERROR 593 b exc_data_storage_book3e 594 595/* Instruction TLB miss */ 596 START_EXCEPTION(instruction_tlb_miss) 597 TLB_MISS_PROLOG 598 599 /* If we take a recursive fault, the second level handler may need 600 * to know whether we are handling a data or instruction fault in 601 * order to get to the right store fault handler. We provide that 602 * info by writing a crazy value in ESR in our exception frame 603 */ 604 li r14,-1 /* store to exception frame is done later */ 605 606 /* Now we handle the fault proper. We only save DEAR in the non 607 * linear mapping case since we know the linear mapping case will 608 * not re-enter. We could indeed optimize and also not save SRR0/1 609 * in the linear mapping case but I'll leave that for later 610 * 611 * Faulting address is SRR0 which is already in r16 612 */ 613 srdi r15,r16,44 /* get region */ 614 xoris r15,r15,0xc 615 cmpldi cr0,r15,0 /* linear mapping ? */ 616 beq tlb_load_linear /* yes -> go to linear map load */ 617 cmpldi cr1,r15,1 /* vmalloc mapping ? */ 618 619 /* We do the user/kernel test for the PID here along with the RW test 620 */ 621 li r11,_PAGE_PRESENT|_PAGE_BAP_UX /* Base perm */ 622 oris r11,r11,_PAGE_ACCESSED@h 623 624 srdi. r15,r16,60 /* Check for user region */ 625 std r14,EX_TLB_ESR(r12) /* write crazy -1 to frame */ 626 beq normal_tlb_miss_user 627 628 li r11,_PAGE_PRESENT|_PAGE_BAP_SX /* Base perm */ 629 oris r11,r11,_PAGE_ACCESSED@h 630 /* XXX replace the RMW cycles with immediate loads + writes */ 631 mfspr r10,SPRN_MAS1 632 rlwinm r10,r10,0,16,1 /* Clear TID */ 633 mtspr SPRN_MAS1,r10 634 beq+ cr1,normal_tlb_miss 635 636 /* We got a crappy address, just fault */ 637 TLB_MISS_EPILOG_ERROR 638 b exc_instruction_storage_book3e 639 640/* 641 * This is the guts of the first-level TLB miss handler for direct 642 * misses. We are entered with: 643 * 644 * r16 = faulting address 645 * r15 = region ID 646 * r14 = crap (free to use) 647 * r13 = PACA 648 * r12 = TLB exception frame in PACA 649 * r11 = PTE permission mask 650 * r10 = crap (free to use) 651 */ 652normal_tlb_miss_user: 653#ifdef CONFIG_PPC_KUAP 654 mfspr r14,SPRN_MAS1 655 rlwinm. r14,r14,0,0x3fff0000 656 beq- normal_tlb_miss_access_fault /* KUAP fault */ 657#endif 658normal_tlb_miss: 659 /* So we first construct the page table address. We do that by 660 * shifting the bottom of the address (not the region ID) by 661 * PAGE_SHIFT-3, clearing the bottom 3 bits (get a PTE ptr) and 662 * or'ing the fourth high bit. 663 * 664 * NOTE: For 64K pages, we do things slightly differently in 665 * order to handle the weird page table format used by linux 666 */ 667 srdi r15,r16,44 668 oris r10,r15,0x1 669 rldicl r14,r16,64-(PAGE_SHIFT-3),PAGE_SHIFT-3+4 670 sldi r15,r10,44 671 clrrdi r14,r14,19 672 or r10,r15,r14 673 674 ld r14,0(r10) 675 676finish_normal_tlb_miss: 677 /* Check if required permissions are met */ 678 andc. r15,r11,r14 679 bne- normal_tlb_miss_access_fault 680 681 /* Now we build the MAS: 682 * 683 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG 684 * MAS 1 : Almost fully setup 685 * - PID already updated by caller if necessary 686 * - TSIZE need change if !base page size, not 687 * yet implemented for now 688 * MAS 2 : Defaults not useful, need to be redone 689 * MAS 3+7 : Needs to be done 690 * 691 * TODO: mix up code below for better scheduling 692 */ 693 clrrdi r10,r16,12 /* Clear low crap in EA */ 694 rlwimi r10,r14,32-19,27,31 /* Insert WIMGE */ 695 mtspr SPRN_MAS2,r10 696 697 /* Check page size, if not standard, update MAS1 */ 698 rldicl r10,r14,64-8,64-8 699 cmpldi cr0,r10,BOOK3E_PAGESZ_4K 700 beq- 1f 701 mfspr r11,SPRN_MAS1 702 rlwimi r11,r14,31,21,24 703 rlwinm r11,r11,0,21,19 704 mtspr SPRN_MAS1,r11 7051: 706 /* Move RPN in position */ 707 rldicr r11,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT 708 clrldi r15,r11,12 /* Clear crap at the top */ 709 rlwimi r15,r14,32-8,22,25 /* Move in U bits */ 710 rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */ 711 712 /* Mask out SW and UW if !DIRTY (XXX optimize this !) */ 713 andi. r11,r14,_PAGE_DIRTY 714 bne 1f 715 li r11,MAS3_SW|MAS3_UW 716 andc r15,r15,r11 7171: 718 srdi r16,r15,32 719 mtspr SPRN_MAS3,r15 720 mtspr SPRN_MAS7,r16 721 722 tlbwe 723 724normal_tlb_miss_done: 725 /* We don't bother with restoring DEAR or ESR since we know we are 726 * level 0 and just going back to userland. They are only needed 727 * if you are going to take an access fault 728 */ 729 TLB_MISS_EPILOG_SUCCESS 730 rfi 731 732normal_tlb_miss_access_fault: 733 /* We need to check if it was an instruction miss */ 734 andi. r10,r11,_PAGE_BAP_UX 735 bne 1f 736 ld r14,EX_TLB_DEAR(r12) 737 ld r15,EX_TLB_ESR(r12) 738 mtspr SPRN_DEAR,r14 739 mtspr SPRN_ESR,r15 740 TLB_MISS_EPILOG_ERROR 741 b exc_data_storage_book3e 7421: TLB_MISS_EPILOG_ERROR 743 b exc_instruction_storage_book3e 744 745 746/* 747 * This is the guts of the second-level TLB miss handler for direct 748 * misses. We are entered with: 749 * 750 * r16 = virtual page table faulting address 751 * r15 = region (top 4 bits of address) 752 * r14 = crap (free to use) 753 * r13 = PACA 754 * r12 = TLB exception frame in PACA 755 * r11 = crap (free to use) 756 * r10 = crap (free to use) 757 * 758 * Note that this should only ever be called as a second level handler 759 * with the current scheme when using SW load. 760 * That means we can always get the original fault DEAR at 761 * EX_TLB_DEAR-EX_TLB_SIZE(r12) 762 * 763 * It can be re-entered by the linear mapping miss handler. However, to 764 * avoid too much complication, it will restart the whole fault at level 765 * 0 so we don't care too much about clobbers 766 * 767 * XXX That code was written back when we couldn't clobber r14. We can now, 768 * so we could probably optimize things a bit 769 */ 770virt_page_table_tlb_miss: 771 /* Are we hitting a kernel page table ? */ 772 srdi r15,r16,60 773 andi. r10,r15,0x8 774 775 /* The cool thing now is that r10 contains 0 for user and 8 for kernel, 776 * and we happen to have the swapper_pg_dir at offset 8 from the user 777 * pgdir in the PACA :-). 778 */ 779 add r11,r10,r13 780 781 /* If kernel, we need to clear MAS1 TID */ 782 beq 1f 783 /* XXX replace the RMW cycles with immediate loads + writes */ 784 mfspr r10,SPRN_MAS1 785 rlwinm r10,r10,0,16,1 /* Clear TID */ 786 mtspr SPRN_MAS1,r10 787#ifdef CONFIG_PPC_KUAP 788 b 2f 7891: 790 mfspr r10,SPRN_MAS1 791 rlwinm. r10,r10,0,0x3fff0000 792 beq- virt_page_table_tlb_miss_fault /* KUAP fault */ 7932: 794#else 7951: 796#endif 797 798 /* Now, we need to walk the page tables. First check if we are in 799 * range. 800 */ 801 rldicl r10,r16,64-(VPTE_INDEX_SIZE+3),VPTE_INDEX_SIZE+3+4 802 cmpldi r10,0x80 803 bne- virt_page_table_tlb_miss_fault 804 805 /* Get the PGD pointer */ 806 ld r15,PACAPGD(r11) 807 cmpldi cr0,r15,0 808 beq- virt_page_table_tlb_miss_fault 809 810 /* Get to PGD entry */ 811 rldicl r11,r16,64-VPTE_PGD_SHIFT,64-PGD_INDEX_SIZE-3 812 clrrdi r10,r11,3 813 ldx r15,r10,r15 814 cmpdi cr0,r15,0 815 bge virt_page_table_tlb_miss_fault 816 817 /* Get to PUD entry */ 818 rldicl r11,r16,64-VPTE_PUD_SHIFT,64-PUD_INDEX_SIZE-3 819 clrrdi r10,r11,3 820 ldx r15,r10,r15 821 cmpdi cr0,r15,0 822 bge virt_page_table_tlb_miss_fault 823 824 /* Get to PMD entry */ 825 rldicl r11,r16,64-VPTE_PMD_SHIFT,64-PMD_INDEX_SIZE-3 826 clrrdi r10,r11,3 827 ldx r15,r10,r15 828 cmpdi cr0,r15,0 829 bge virt_page_table_tlb_miss_fault 830 831 /* Ok, we're all right, we can now create a kernel translation for 832 * a 4K or 64K page from r16 -> r15. 833 */ 834 /* Now we build the MAS: 835 * 836 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG 837 * MAS 1 : Almost fully setup 838 * - PID already updated by caller if necessary 839 * - TSIZE for now is base page size always 840 * MAS 2 : Use defaults 841 * MAS 3+7 : Needs to be done 842 * 843 * So we only do MAS 2 and 3 for now... 844 */ 845 clrldi r11,r15,4 /* remove region ID from RPN */ 846 ori r10,r11,1 /* Or-in SR */ 847 848 srdi r16,r10,32 849 mtspr SPRN_MAS3,r10 850 mtspr SPRN_MAS7,r16 851 852 tlbwe 853 854 /* Return to caller, normal case */ 855 TLB_MISS_EPILOG_SUCCESS 856 rfi 857 858virt_page_table_tlb_miss_fault: 859 /* If we fault here, things are a little bit tricky. We need to call 860 * either data or instruction store fault, and we need to retrieve 861 * the original fault address and ESR (for data). 862 * 863 * The thing is, we know that in normal circumstances, this is 864 * always called as a second level tlb miss for SW load or as a first 865 * level TLB miss for HW load, so we should be able to peek at the 866 * relevant information in the first exception frame in the PACA. 867 * 868 * However, we do need to double check that, because we may just hit 869 * a stray kernel pointer or a userland attack trying to hit those 870 * areas. If that is the case, we do a data fault. (We can't get here 871 * from an instruction tlb miss anyway). 872 * 873 * Note also that when going to a fault, we must unwind the previous 874 * level as well. Since we are doing that, we don't need to clear or 875 * restore the TLB reservation neither. 876 */ 877 subf r10,r13,r12 878 cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE 879 bne- virt_page_table_tlb_miss_whacko_fault 880 881 /* We dig the original DEAR and ESR from slot 0 */ 882 ld r15,EX_TLB_DEAR+PACA_EXTLB(r13) 883 ld r16,EX_TLB_ESR+PACA_EXTLB(r13) 884 885 /* We check for the "special" ESR value for instruction faults */ 886 cmpdi cr0,r16,-1 887 beq 1f 888 mtspr SPRN_DEAR,r15 889 mtspr SPRN_ESR,r16 890 TLB_MISS_EPILOG_ERROR 891 b exc_data_storage_book3e 8921: TLB_MISS_EPILOG_ERROR 893 b exc_instruction_storage_book3e 894 895virt_page_table_tlb_miss_whacko_fault: 896 /* The linear fault will restart everything so ESR and DEAR will 897 * not have been clobbered, let's just fault with what we have 898 */ 899 TLB_MISS_EPILOG_ERROR 900 b exc_data_storage_book3e 901 902 903/************************************************************** 904 * * 905 * TLB miss handling for Book3E with hw page table support * 906 * * 907 **************************************************************/ 908 909 910/* Data TLB miss */ 911 START_EXCEPTION(data_tlb_miss_htw) 912 TLB_MISS_PROLOG 913 914 /* Now we handle the fault proper. We only save DEAR in normal 915 * fault case since that's the only interesting values here. 916 * We could probably also optimize by not saving SRR0/1 in the 917 * linear mapping case but I'll leave that for later 918 */ 919 mfspr r14,SPRN_ESR 920 mfspr r16,SPRN_DEAR /* get faulting address */ 921 srdi r11,r16,44 /* get region */ 922 xoris r11,r11,0xc 923 cmpldi cr0,r11,0 /* linear mapping ? */ 924 beq tlb_load_linear /* yes -> go to linear map load */ 925 cmpldi cr1,r11,1 /* vmalloc mapping ? */ 926 927 /* We do the user/kernel test for the PID here along with the RW test 928 */ 929 srdi. r11,r16,60 /* Check for user region */ 930 ld r15,PACAPGD(r13) /* Load user pgdir */ 931 beq htw_tlb_miss 932 933 /* XXX replace the RMW cycles with immediate loads + writes */ 9341: mfspr r10,SPRN_MAS1 935 rlwinm r10,r10,0,16,1 /* Clear TID */ 936 mtspr SPRN_MAS1,r10 937 ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */ 938 beq+ cr1,htw_tlb_miss 939 940 /* We got a crappy address, just fault with whatever DEAR and ESR 941 * are here 942 */ 943 TLB_MISS_EPILOG_ERROR 944 b exc_data_storage_book3e 945 946/* Instruction TLB miss */ 947 START_EXCEPTION(instruction_tlb_miss_htw) 948 TLB_MISS_PROLOG 949 950 /* If we take a recursive fault, the second level handler may need 951 * to know whether we are handling a data or instruction fault in 952 * order to get to the right store fault handler. We provide that 953 * info by keeping a crazy value for ESR in r14 954 */ 955 li r14,-1 /* store to exception frame is done later */ 956 957 /* Now we handle the fault proper. We only save DEAR in the non 958 * linear mapping case since we know the linear mapping case will 959 * not re-enter. We could indeed optimize and also not save SRR0/1 960 * in the linear mapping case but I'll leave that for later 961 * 962 * Faulting address is SRR0 which is already in r16 963 */ 964 srdi r11,r16,44 /* get region */ 965 xoris r11,r11,0xc 966 cmpldi cr0,r11,0 /* linear mapping ? */ 967 beq tlb_load_linear /* yes -> go to linear map load */ 968 cmpldi cr1,r11,1 /* vmalloc mapping ? */ 969 970 /* We do the user/kernel test for the PID here along with the RW test 971 */ 972 srdi. r11,r16,60 /* Check for user region */ 973 ld r15,PACAPGD(r13) /* Load user pgdir */ 974 beq htw_tlb_miss 975 976 /* XXX replace the RMW cycles with immediate loads + writes */ 9771: mfspr r10,SPRN_MAS1 978 rlwinm r10,r10,0,16,1 /* Clear TID */ 979 mtspr SPRN_MAS1,r10 980 ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */ 981 beq+ htw_tlb_miss 982 983 /* We got a crappy address, just fault */ 984 TLB_MISS_EPILOG_ERROR 985 b exc_instruction_storage_book3e 986 987 988/* 989 * This is the guts of the second-level TLB miss handler for direct 990 * misses. We are entered with: 991 * 992 * r16 = virtual page table faulting address 993 * r15 = PGD pointer 994 * r14 = ESR 995 * r13 = PACA 996 * r12 = TLB exception frame in PACA 997 * r11 = crap (free to use) 998 * r10 = crap (free to use) 999 * 1000 * It can be re-entered by the linear mapping miss handler. However, to 1001 * avoid too much complication, it will save/restore things for us 1002 */ 1003htw_tlb_miss: 1004#ifdef CONFIG_PPC_KUAP 1005 mfspr r10,SPRN_MAS1 1006 rlwinm. r10,r10,0,0x3fff0000 1007 beq- htw_tlb_miss_fault /* KUAP fault */ 1008#endif 1009 /* Search if we already have a TLB entry for that virtual address, and 1010 * if we do, bail out. 1011 * 1012 * MAS1:IND should be already set based on MAS4 1013 */ 1014 PPC_TLBSRX_DOT(0,R16) 1015 beq htw_tlb_miss_done 1016 1017 /* Now, we need to walk the page tables. First check if we are in 1018 * range. 1019 */ 1020 rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4 1021 bne- htw_tlb_miss_fault 1022 1023 /* Get the PGD pointer */ 1024 cmpldi cr0,r15,0 1025 beq- htw_tlb_miss_fault 1026 1027 /* Get to PGD entry */ 1028 rldicl r11,r16,64-(PGDIR_SHIFT-3),64-PGD_INDEX_SIZE-3 1029 clrrdi r10,r11,3 1030 ldx r15,r10,r15 1031 cmpdi cr0,r15,0 1032 bge htw_tlb_miss_fault 1033 1034 /* Get to PUD entry */ 1035 rldicl r11,r16,64-(PUD_SHIFT-3),64-PUD_INDEX_SIZE-3 1036 clrrdi r10,r11,3 1037 ldx r15,r10,r15 1038 cmpdi cr0,r15,0 1039 bge htw_tlb_miss_fault 1040 1041 /* Get to PMD entry */ 1042 rldicl r11,r16,64-(PMD_SHIFT-3),64-PMD_INDEX_SIZE-3 1043 clrrdi r10,r11,3 1044 ldx r15,r10,r15 1045 cmpdi cr0,r15,0 1046 bge htw_tlb_miss_fault 1047 1048 /* Ok, we're all right, we can now create an indirect entry for 1049 * a 1M or 256M page. 1050 * 1051 * The last trick is now that because we use "half" pages for 1052 * the HTW (1M IND is 2K and 256M IND is 32K) we need to account 1053 * for an added LSB bit to the RPN. For 64K pages, there is no 1054 * problem as we already use 32K arrays (half PTE pages), but for 1055 * 4K page we need to extract a bit from the virtual address and 1056 * insert it into the "PA52" bit of the RPN. 1057 */ 1058 rlwimi r15,r16,32-9,20,20 1059 /* Now we build the MAS: 1060 * 1061 * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG 1062 * MAS 1 : Almost fully setup 1063 * - PID already updated by caller if necessary 1064 * - TSIZE for now is base ind page size always 1065 * MAS 2 : Use defaults 1066 * MAS 3+7 : Needs to be done 1067 */ 1068 ori r10,r15,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT) 1069 1070 srdi r16,r10,32 1071 mtspr SPRN_MAS3,r10 1072 mtspr SPRN_MAS7,r16 1073 1074 tlbwe 1075 1076htw_tlb_miss_done: 1077 /* We don't bother with restoring DEAR or ESR since we know we are 1078 * level 0 and just going back to userland. They are only needed 1079 * if you are going to take an access fault 1080 */ 1081 TLB_MISS_EPILOG_SUCCESS 1082 rfi 1083 1084htw_tlb_miss_fault: 1085 /* We need to check if it was an instruction miss. We know this 1086 * though because r14 would contain -1 1087 */ 1088 cmpdi cr0,r14,-1 1089 beq 1f 1090 mtspr SPRN_DEAR,r16 1091 mtspr SPRN_ESR,r14 1092 TLB_MISS_EPILOG_ERROR 1093 b exc_data_storage_book3e 10941: TLB_MISS_EPILOG_ERROR 1095 b exc_instruction_storage_book3e 1096 1097/* 1098 * This is the guts of "any" level TLB miss handler for kernel linear 1099 * mapping misses. We are entered with: 1100 * 1101 * 1102 * r16 = faulting address 1103 * r15 = crap (free to use) 1104 * r14 = ESR (data) or -1 (instruction) 1105 * r13 = PACA 1106 * r12 = TLB exception frame in PACA 1107 * r11 = crap (free to use) 1108 * r10 = crap (free to use) 1109 * 1110 * In addition we know that we will not re-enter, so in theory, we could 1111 * use a simpler epilog not restoring SRR0/1 etc.. but we'll do that later. 1112 * 1113 * We also need to be careful about MAS registers here & TLB reservation, 1114 * as we know we'll have clobbered them if we interrupt the main TLB miss 1115 * handlers in which case we probably want to do a full restart at level 1116 * 0 rather than saving / restoring the MAS. 1117 * 1118 * Note: If we care about performance of that core, we can easily shuffle 1119 * a few things around 1120 */ 1121tlb_load_linear: 1122 /* For now, we assume the linear mapping is contiguous and stops at 1123 * linear_map_top. We also assume the size is a multiple of 1G, thus 1124 * we only use 1G pages for now. That might have to be changed in a 1125 * final implementation, especially when dealing with hypervisors 1126 */ 1127 ld r11,PACATOC(r13) 1128 ld r11,linear_map_top@got(r11) 1129 ld r10,0(r11) 1130 tovirt(10,10) 1131 cmpld cr0,r16,r10 1132 bge tlb_load_linear_fault 1133 1134 /* MAS1 need whole new setup. */ 1135 li r15,(BOOK3E_PAGESZ_1GB<<MAS1_TSIZE_SHIFT) 1136 oris r15,r15,MAS1_VALID@h /* MAS1 needs V and TSIZE */ 1137 mtspr SPRN_MAS1,r15 1138 1139 /* Already somebody there ? */ 1140 PPC_TLBSRX_DOT(0,R16) 1141 beq tlb_load_linear_done 1142 1143 /* Now we build the remaining MAS. MAS0 and 2 should be fine 1144 * with their defaults, which leaves us with MAS 3 and 7. The 1145 * mapping is linear, so we just take the address, clear the 1146 * region bits, and or in the permission bits which are currently 1147 * hard wired 1148 */ 1149 clrrdi r10,r16,30 /* 1G page index */ 1150 clrldi r10,r10,4 /* clear region bits */ 1151 ori r10,r10,MAS3_SR|MAS3_SW|MAS3_SX 1152 1153 srdi r16,r10,32 1154 mtspr SPRN_MAS3,r10 1155 mtspr SPRN_MAS7,r16 1156 1157 tlbwe 1158 1159tlb_load_linear_done: 1160 /* We use the "error" epilog for success as we do want to 1161 * restore to the initial faulting context, whatever it was. 1162 * We do that because we can't resume a fault within a TLB 1163 * miss handler, due to MAS and TLB reservation being clobbered. 1164 */ 1165 TLB_MISS_EPILOG_ERROR 1166 rfi 1167 1168tlb_load_linear_fault: 1169 /* We keep the DEAR and ESR around, this shouldn't have happened */ 1170 cmpdi cr0,r14,-1 1171 beq 1f 1172 TLB_MISS_EPILOG_ERROR_SPECIAL 1173 b exc_data_storage_book3e 11741: TLB_MISS_EPILOG_ERROR_SPECIAL 1175 b exc_instruction_storage_book3e 1176