tlb_low_64e.S 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. * Low level TLB miss handlers for Book3E
  3. *
  4. * Copyright (C) 2008-2009
  5. * Ben. Herrenschmidt (benh@kernel.crashing.org), IBM Corp.
  6. *
  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.
  11. */
  12. #include <asm/processor.h>
  13. #include <asm/reg.h>
  14. #include <asm/page.h>
  15. #include <asm/mmu.h>
  16. #include <asm/ppc_asm.h>
  17. #include <asm/asm-offsets.h>
  18. #include <asm/cputable.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/exception-64e.h>
  21. #include <asm/ppc-opcode.h>
  22. #include <asm/kvm_asm.h>
  23. #include <asm/kvm_booke_hv_asm.h>
  24. #ifdef CONFIG_PPC_64K_PAGES
  25. #define VPTE_PMD_SHIFT (PTE_INDEX_SIZE+1)
  26. #else
  27. #define VPTE_PMD_SHIFT (PTE_INDEX_SIZE)
  28. #endif
  29. #define VPTE_PUD_SHIFT (VPTE_PMD_SHIFT + PMD_INDEX_SIZE)
  30. #define VPTE_PGD_SHIFT (VPTE_PUD_SHIFT + PUD_INDEX_SIZE)
  31. #define VPTE_INDEX_SIZE (VPTE_PGD_SHIFT + PGD_INDEX_SIZE)
  32. /**********************************************************************
  33. * *
  34. * TLB miss handling for Book3E with a bolted linear mapping *
  35. * No virtual page table, no nested TLB misses *
  36. * *
  37. **********************************************************************/
  38. .macro tlb_prolog_bolted intnum addr
  39. mtspr SPRN_SPRG_GEN_SCRATCH,r13
  40. mfspr r13,SPRN_SPRG_PACA
  41. std r10,PACA_EXTLB+EX_TLB_R10(r13)
  42. mfcr r10
  43. std r11,PACA_EXTLB+EX_TLB_R11(r13)
  44. #ifdef CONFIG_KVM_BOOKE_HV
  45. BEGIN_FTR_SECTION
  46. mfspr r11, SPRN_SRR1
  47. END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
  48. #endif
  49. DO_KVM \intnum, SPRN_SRR1
  50. std r16,PACA_EXTLB+EX_TLB_R16(r13)
  51. mfspr r16,\addr /* get faulting address */
  52. std r14,PACA_EXTLB+EX_TLB_R14(r13)
  53. ld r14,PACAPGD(r13)
  54. std r15,PACA_EXTLB+EX_TLB_R15(r13)
  55. std r10,PACA_EXTLB+EX_TLB_CR(r13)
  56. TLB_MISS_PROLOG_STATS_BOLTED
  57. .endm
  58. .macro tlb_epilog_bolted
  59. ld r14,PACA_EXTLB+EX_TLB_CR(r13)
  60. ld r10,PACA_EXTLB+EX_TLB_R10(r13)
  61. ld r11,PACA_EXTLB+EX_TLB_R11(r13)
  62. mtcr r14
  63. ld r14,PACA_EXTLB+EX_TLB_R14(r13)
  64. ld r15,PACA_EXTLB+EX_TLB_R15(r13)
  65. TLB_MISS_RESTORE_STATS_BOLTED
  66. ld r16,PACA_EXTLB+EX_TLB_R16(r13)
  67. mfspr r13,SPRN_SPRG_GEN_SCRATCH
  68. .endm
  69. /* Data TLB miss */
  70. START_EXCEPTION(data_tlb_miss_bolted)
  71. tlb_prolog_bolted BOOKE_INTERRUPT_DTLB_MISS SPRN_DEAR
  72. /* We need _PAGE_PRESENT and _PAGE_ACCESSED set */
  73. /* We do the user/kernel test for the PID here along with the RW test
  74. */
  75. /* We pre-test some combination of permissions to avoid double
  76. * faults:
  77. *
  78. * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE
  79. * ESR_ST is 0x00800000
  80. * _PAGE_BAP_SW is 0x00000010
  81. * So the shift is >> 19. This tests for supervisor writeability.
  82. * If the page happens to be supervisor writeable and not user
  83. * writeable, we will take a new fault later, but that should be
  84. * a rare enough case.
  85. *
  86. * We also move ESR_ST in _PAGE_DIRTY position
  87. * _PAGE_DIRTY is 0x00001000 so the shift is >> 11
  88. *
  89. * MAS1 is preset for all we need except for TID that needs to
  90. * be cleared for kernel translations
  91. */
  92. mfspr r11,SPRN_ESR
  93. srdi r15,r16,60 /* get region */
  94. rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
  95. bne- dtlb_miss_fault_bolted /* Bail if fault addr is invalid */
  96. rlwinm r10,r11,32-19,27,27
  97. rlwimi r10,r11,32-16,19,19
  98. cmpwi r15,0 /* user vs kernel check */
  99. ori r10,r10,_PAGE_PRESENT
  100. oris r11,r10,_PAGE_ACCESSED@h
  101. TLB_MISS_STATS_SAVE_INFO_BOLTED
  102. bne tlb_miss_kernel_bolted
  103. tlb_miss_common_bolted:
  104. /*
  105. * This is the guts of the TLB miss handler for bolted-linear.
  106. * We are entered with:
  107. *
  108. * r16 = faulting address
  109. * r15 = crap (free to use)
  110. * r14 = page table base
  111. * r13 = PACA
  112. * r11 = PTE permission mask
  113. * r10 = crap (free to use)
  114. */
  115. rldicl r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3
  116. cmpldi cr0,r14,0
  117. clrrdi r15,r15,3
  118. beq tlb_miss_fault_bolted /* No PGDIR, bail */
  119. BEGIN_MMU_FTR_SECTION
  120. /* Set the TLB reservation and search for existing entry. Then load
  121. * the entry.
  122. */
  123. PPC_TLBSRX_DOT(0,R16)
  124. ldx r14,r14,r15 /* grab pgd entry */
  125. beq normal_tlb_miss_done /* tlb exists already, bail */
  126. MMU_FTR_SECTION_ELSE
  127. ldx r14,r14,r15 /* grab pgd entry */
  128. ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_USE_TLBRSRV)
  129. #ifndef CONFIG_PPC_64K_PAGES
  130. rldicl r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3
  131. clrrdi r15,r15,3
  132. cmpdi cr0,r14,0
  133. bge tlb_miss_fault_bolted /* Bad pgd entry or hugepage; bail */
  134. ldx r14,r14,r15 /* grab pud entry */
  135. #endif /* CONFIG_PPC_64K_PAGES */
  136. rldicl r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3
  137. clrrdi r15,r15,3
  138. cmpdi cr0,r14,0
  139. bge tlb_miss_fault_bolted
  140. ldx r14,r14,r15 /* Grab pmd entry */
  141. rldicl r15,r16,64-PAGE_SHIFT+3,64-PTE_INDEX_SIZE-3
  142. clrrdi r15,r15,3
  143. cmpdi cr0,r14,0
  144. bge tlb_miss_fault_bolted
  145. ldx r14,r14,r15 /* Grab PTE, normal (!huge) page */
  146. /* Check if required permissions are met */
  147. andc. r15,r11,r14
  148. rldicr r15,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
  149. bne- tlb_miss_fault_bolted
  150. /* Now we build the MAS:
  151. *
  152. * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
  153. * MAS 1 : Almost fully setup
  154. * - PID already updated by caller if necessary
  155. * - TSIZE need change if !base page size, not
  156. * yet implemented for now
  157. * MAS 2 : Defaults not useful, need to be redone
  158. * MAS 3+7 : Needs to be done
  159. */
  160. clrrdi r11,r16,12 /* Clear low crap in EA */
  161. clrldi r15,r15,12 /* Clear crap at the top */
  162. rlwimi r11,r14,32-19,27,31 /* Insert WIMGE */
  163. rlwimi r15,r14,32-8,22,25 /* Move in U bits */
  164. mtspr SPRN_MAS2,r11
  165. andi. r11,r14,_PAGE_DIRTY
  166. rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */
  167. /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
  168. bne 1f
  169. li r11,MAS3_SW|MAS3_UW
  170. andc r15,r15,r11
  171. 1:
  172. mtspr SPRN_MAS7_MAS3,r15
  173. tlbwe
  174. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
  175. tlb_epilog_bolted
  176. rfi
  177. itlb_miss_kernel_bolted:
  178. li r11,_PAGE_PRESENT|_PAGE_BAP_SX /* Base perm */
  179. oris r11,r11,_PAGE_ACCESSED@h
  180. tlb_miss_kernel_bolted:
  181. mfspr r10,SPRN_MAS1
  182. ld r14,PACA_KERNELPGD(r13)
  183. cmpldi cr0,r15,8 /* Check for vmalloc region */
  184. rlwinm r10,r10,0,16,1 /* Clear TID */
  185. mtspr SPRN_MAS1,r10
  186. beq+ tlb_miss_common_bolted
  187. tlb_miss_fault_bolted:
  188. /* We need to check if it was an instruction miss */
  189. andi. r10,r11,_PAGE_EXEC|_PAGE_BAP_SX
  190. bne itlb_miss_fault_bolted
  191. dtlb_miss_fault_bolted:
  192. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
  193. tlb_epilog_bolted
  194. b exc_data_storage_book3e
  195. itlb_miss_fault_bolted:
  196. TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
  197. tlb_epilog_bolted
  198. b exc_instruction_storage_book3e
  199. /* Instruction TLB miss */
  200. START_EXCEPTION(instruction_tlb_miss_bolted)
  201. tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0
  202. rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
  203. srdi r15,r16,60 /* get region */
  204. TLB_MISS_STATS_SAVE_INFO_BOLTED
  205. bne- itlb_miss_fault_bolted
  206. li r11,_PAGE_PRESENT|_PAGE_EXEC /* Base perm */
  207. /* We do the user/kernel test for the PID here along with the RW test
  208. */
  209. cmpldi cr0,r15,0 /* Check for user region */
  210. oris r11,r11,_PAGE_ACCESSED@h
  211. beq tlb_miss_common_bolted
  212. b itlb_miss_kernel_bolted
  213. /**********************************************************************
  214. * *
  215. * TLB miss handling for Book3E with TLB reservation and HES support *
  216. * *
  217. **********************************************************************/
  218. /* Data TLB miss */
  219. START_EXCEPTION(data_tlb_miss)
  220. TLB_MISS_PROLOG
  221. /* Now we handle the fault proper. We only save DEAR in normal
  222. * fault case since that's the only interesting values here.
  223. * We could probably also optimize by not saving SRR0/1 in the
  224. * linear mapping case but I'll leave that for later
  225. */
  226. mfspr r14,SPRN_ESR
  227. mfspr r16,SPRN_DEAR /* get faulting address */
  228. srdi r15,r16,60 /* get region */
  229. cmpldi cr0,r15,0xc /* linear mapping ? */
  230. TLB_MISS_STATS_SAVE_INFO
  231. beq tlb_load_linear /* yes -> go to linear map load */
  232. /* The page tables are mapped virtually linear. At this point, though,
  233. * we don't know whether we are trying to fault in a first level
  234. * virtual address or a virtual page table address. We can get that
  235. * from bit 0x1 of the region ID which we have set for a page table
  236. */
  237. andi. r10,r15,0x1
  238. bne- virt_page_table_tlb_miss
  239. std r14,EX_TLB_ESR(r12); /* save ESR */
  240. std r16,EX_TLB_DEAR(r12); /* save DEAR */
  241. /* We need _PAGE_PRESENT and _PAGE_ACCESSED set */
  242. li r11,_PAGE_PRESENT
  243. oris r11,r11,_PAGE_ACCESSED@h
  244. /* We do the user/kernel test for the PID here along with the RW test
  245. */
  246. cmpldi cr0,r15,0 /* Check for user region */
  247. /* We pre-test some combination of permissions to avoid double
  248. * faults:
  249. *
  250. * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE
  251. * ESR_ST is 0x00800000
  252. * _PAGE_BAP_SW is 0x00000010
  253. * So the shift is >> 19. This tests for supervisor writeability.
  254. * If the page happens to be supervisor writeable and not user
  255. * writeable, we will take a new fault later, but that should be
  256. * a rare enough case.
  257. *
  258. * We also move ESR_ST in _PAGE_DIRTY position
  259. * _PAGE_DIRTY is 0x00001000 so the shift is >> 11
  260. *
  261. * MAS1 is preset for all we need except for TID that needs to
  262. * be cleared for kernel translations
  263. */
  264. rlwimi r11,r14,32-19,27,27
  265. rlwimi r11,r14,32-16,19,19
  266. beq normal_tlb_miss
  267. /* XXX replace the RMW cycles with immediate loads + writes */
  268. 1: mfspr r10,SPRN_MAS1
  269. cmpldi cr0,r15,8 /* Check for vmalloc region */
  270. rlwinm r10,r10,0,16,1 /* Clear TID */
  271. mtspr SPRN_MAS1,r10
  272. beq+ normal_tlb_miss
  273. /* We got a crappy address, just fault with whatever DEAR and ESR
  274. * are here
  275. */
  276. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
  277. TLB_MISS_EPILOG_ERROR
  278. b exc_data_storage_book3e
  279. /* Instruction TLB miss */
  280. START_EXCEPTION(instruction_tlb_miss)
  281. TLB_MISS_PROLOG
  282. /* If we take a recursive fault, the second level handler may need
  283. * to know whether we are handling a data or instruction fault in
  284. * order to get to the right store fault handler. We provide that
  285. * info by writing a crazy value in ESR in our exception frame
  286. */
  287. li r14,-1 /* store to exception frame is done later */
  288. /* Now we handle the fault proper. We only save DEAR in the non
  289. * linear mapping case since we know the linear mapping case will
  290. * not re-enter. We could indeed optimize and also not save SRR0/1
  291. * in the linear mapping case but I'll leave that for later
  292. *
  293. * Faulting address is SRR0 which is already in r16
  294. */
  295. srdi r15,r16,60 /* get region */
  296. cmpldi cr0,r15,0xc /* linear mapping ? */
  297. TLB_MISS_STATS_SAVE_INFO
  298. beq tlb_load_linear /* yes -> go to linear map load */
  299. /* We do the user/kernel test for the PID here along with the RW test
  300. */
  301. li r11,_PAGE_PRESENT|_PAGE_EXEC /* Base perm */
  302. oris r11,r11,_PAGE_ACCESSED@h
  303. cmpldi cr0,r15,0 /* Check for user region */
  304. std r14,EX_TLB_ESR(r12) /* write crazy -1 to frame */
  305. beq normal_tlb_miss
  306. li r11,_PAGE_PRESENT|_PAGE_BAP_SX /* Base perm */
  307. oris r11,r11,_PAGE_ACCESSED@h
  308. /* XXX replace the RMW cycles with immediate loads + writes */
  309. mfspr r10,SPRN_MAS1
  310. cmpldi cr0,r15,8 /* Check for vmalloc region */
  311. rlwinm r10,r10,0,16,1 /* Clear TID */
  312. mtspr SPRN_MAS1,r10
  313. beq+ normal_tlb_miss
  314. /* We got a crappy address, just fault */
  315. TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
  316. TLB_MISS_EPILOG_ERROR
  317. b exc_instruction_storage_book3e
  318. /*
  319. * This is the guts of the first-level TLB miss handler for direct
  320. * misses. We are entered with:
  321. *
  322. * r16 = faulting address
  323. * r15 = region ID
  324. * r14 = crap (free to use)
  325. * r13 = PACA
  326. * r12 = TLB exception frame in PACA
  327. * r11 = PTE permission mask
  328. * r10 = crap (free to use)
  329. */
  330. normal_tlb_miss:
  331. /* So we first construct the page table address. We do that by
  332. * shifting the bottom of the address (not the region ID) by
  333. * PAGE_SHIFT-3, clearing the bottom 3 bits (get a PTE ptr) and
  334. * or'ing the fourth high bit.
  335. *
  336. * NOTE: For 64K pages, we do things slightly differently in
  337. * order to handle the weird page table format used by linux
  338. */
  339. ori r10,r15,0x1
  340. #ifdef CONFIG_PPC_64K_PAGES
  341. /* For the top bits, 16 bytes per PTE */
  342. rldicl r14,r16,64-(PAGE_SHIFT-4),PAGE_SHIFT-4+4
  343. /* Now create the bottom bits as 0 in position 0x8000 and
  344. * the rest calculated for 8 bytes per PTE
  345. */
  346. rldicl r15,r16,64-(PAGE_SHIFT-3),64-15
  347. /* Insert the bottom bits in */
  348. rlwimi r14,r15,0,16,31
  349. #else
  350. rldicl r14,r16,64-(PAGE_SHIFT-3),PAGE_SHIFT-3+4
  351. #endif
  352. sldi r15,r10,60
  353. clrrdi r14,r14,3
  354. or r10,r15,r14
  355. BEGIN_MMU_FTR_SECTION
  356. /* Set the TLB reservation and search for existing entry. Then load
  357. * the entry.
  358. */
  359. PPC_TLBSRX_DOT(0,R16)
  360. ld r14,0(r10)
  361. beq normal_tlb_miss_done
  362. MMU_FTR_SECTION_ELSE
  363. ld r14,0(r10)
  364. ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_USE_TLBRSRV)
  365. finish_normal_tlb_miss:
  366. /* Check if required permissions are met */
  367. andc. r15,r11,r14
  368. bne- normal_tlb_miss_access_fault
  369. /* Now we build the MAS:
  370. *
  371. * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
  372. * MAS 1 : Almost fully setup
  373. * - PID already updated by caller if necessary
  374. * - TSIZE need change if !base page size, not
  375. * yet implemented for now
  376. * MAS 2 : Defaults not useful, need to be redone
  377. * MAS 3+7 : Needs to be done
  378. *
  379. * TODO: mix up code below for better scheduling
  380. */
  381. clrrdi r11,r16,12 /* Clear low crap in EA */
  382. rlwimi r11,r14,32-19,27,31 /* Insert WIMGE */
  383. mtspr SPRN_MAS2,r11
  384. /* Check page size, if not standard, update MAS1 */
  385. rldicl r11,r14,64-8,64-8
  386. #ifdef CONFIG_PPC_64K_PAGES
  387. cmpldi cr0,r11,BOOK3E_PAGESZ_64K
  388. #else
  389. cmpldi cr0,r11,BOOK3E_PAGESZ_4K
  390. #endif
  391. beq- 1f
  392. mfspr r11,SPRN_MAS1
  393. rlwimi r11,r14,31,21,24
  394. rlwinm r11,r11,0,21,19
  395. mtspr SPRN_MAS1,r11
  396. 1:
  397. /* Move RPN in position */
  398. rldicr r11,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
  399. clrldi r15,r11,12 /* Clear crap at the top */
  400. rlwimi r15,r14,32-8,22,25 /* Move in U bits */
  401. rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */
  402. /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
  403. andi. r11,r14,_PAGE_DIRTY
  404. bne 1f
  405. li r11,MAS3_SW|MAS3_UW
  406. andc r15,r15,r11
  407. 1:
  408. BEGIN_MMU_FTR_SECTION
  409. srdi r16,r15,32
  410. mtspr SPRN_MAS3,r15
  411. mtspr SPRN_MAS7,r16
  412. MMU_FTR_SECTION_ELSE
  413. mtspr SPRN_MAS7_MAS3,r15
  414. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
  415. tlbwe
  416. normal_tlb_miss_done:
  417. /* We don't bother with restoring DEAR or ESR since we know we are
  418. * level 0 and just going back to userland. They are only needed
  419. * if you are going to take an access fault
  420. */
  421. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
  422. TLB_MISS_EPILOG_SUCCESS
  423. rfi
  424. normal_tlb_miss_access_fault:
  425. /* We need to check if it was an instruction miss */
  426. andi. r10,r11,_PAGE_EXEC
  427. bne 1f
  428. ld r14,EX_TLB_DEAR(r12)
  429. ld r15,EX_TLB_ESR(r12)
  430. mtspr SPRN_DEAR,r14
  431. mtspr SPRN_ESR,r15
  432. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
  433. TLB_MISS_EPILOG_ERROR
  434. b exc_data_storage_book3e
  435. 1: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
  436. TLB_MISS_EPILOG_ERROR
  437. b exc_instruction_storage_book3e
  438. /*
  439. * This is the guts of the second-level TLB miss handler for direct
  440. * misses. We are entered with:
  441. *
  442. * r16 = virtual page table faulting address
  443. * r15 = region (top 4 bits of address)
  444. * r14 = crap (free to use)
  445. * r13 = PACA
  446. * r12 = TLB exception frame in PACA
  447. * r11 = crap (free to use)
  448. * r10 = crap (free to use)
  449. *
  450. * Note that this should only ever be called as a second level handler
  451. * with the current scheme when using SW load.
  452. * That means we can always get the original fault DEAR at
  453. * EX_TLB_DEAR-EX_TLB_SIZE(r12)
  454. *
  455. * It can be re-entered by the linear mapping miss handler. However, to
  456. * avoid too much complication, it will restart the whole fault at level
  457. * 0 so we don't care too much about clobbers
  458. *
  459. * XXX That code was written back when we couldn't clobber r14. We can now,
  460. * so we could probably optimize things a bit
  461. */
  462. virt_page_table_tlb_miss:
  463. /* Are we hitting a kernel page table ? */
  464. andi. r10,r15,0x8
  465. /* The cool thing now is that r10 contains 0 for user and 8 for kernel,
  466. * and we happen to have the swapper_pg_dir at offset 8 from the user
  467. * pgdir in the PACA :-).
  468. */
  469. add r11,r10,r13
  470. /* If kernel, we need to clear MAS1 TID */
  471. beq 1f
  472. /* XXX replace the RMW cycles with immediate loads + writes */
  473. mfspr r10,SPRN_MAS1
  474. rlwinm r10,r10,0,16,1 /* Clear TID */
  475. mtspr SPRN_MAS1,r10
  476. 1:
  477. BEGIN_MMU_FTR_SECTION
  478. /* Search if we already have a TLB entry for that virtual address, and
  479. * if we do, bail out.
  480. */
  481. PPC_TLBSRX_DOT(0,R16)
  482. beq virt_page_table_tlb_miss_done
  483. END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
  484. /* Now, we need to walk the page tables. First check if we are in
  485. * range.
  486. */
  487. rldicl. r10,r16,64-(VPTE_INDEX_SIZE+3),VPTE_INDEX_SIZE+3+4
  488. bne- virt_page_table_tlb_miss_fault
  489. /* Get the PGD pointer */
  490. ld r15,PACAPGD(r11)
  491. cmpldi cr0,r15,0
  492. beq- virt_page_table_tlb_miss_fault
  493. /* Get to PGD entry */
  494. rldicl r11,r16,64-VPTE_PGD_SHIFT,64-PGD_INDEX_SIZE-3
  495. clrrdi r10,r11,3
  496. ldx r15,r10,r15
  497. cmpdi cr0,r15,0
  498. bge virt_page_table_tlb_miss_fault
  499. #ifndef CONFIG_PPC_64K_PAGES
  500. /* Get to PUD entry */
  501. rldicl r11,r16,64-VPTE_PUD_SHIFT,64-PUD_INDEX_SIZE-3
  502. clrrdi r10,r11,3
  503. ldx r15,r10,r15
  504. cmpdi cr0,r15,0
  505. bge virt_page_table_tlb_miss_fault
  506. #endif /* CONFIG_PPC_64K_PAGES */
  507. /* Get to PMD entry */
  508. rldicl r11,r16,64-VPTE_PMD_SHIFT,64-PMD_INDEX_SIZE-3
  509. clrrdi r10,r11,3
  510. ldx r15,r10,r15
  511. cmpdi cr0,r15,0
  512. bge virt_page_table_tlb_miss_fault
  513. /* Ok, we're all right, we can now create a kernel translation for
  514. * a 4K or 64K page from r16 -> r15.
  515. */
  516. /* Now we build the MAS:
  517. *
  518. * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
  519. * MAS 1 : Almost fully setup
  520. * - PID already updated by caller if necessary
  521. * - TSIZE for now is base page size always
  522. * MAS 2 : Use defaults
  523. * MAS 3+7 : Needs to be done
  524. *
  525. * So we only do MAS 2 and 3 for now...
  526. */
  527. clrldi r11,r15,4 /* remove region ID from RPN */
  528. ori r10,r11,1 /* Or-in SR */
  529. BEGIN_MMU_FTR_SECTION
  530. srdi r16,r10,32
  531. mtspr SPRN_MAS3,r10
  532. mtspr SPRN_MAS7,r16
  533. MMU_FTR_SECTION_ELSE
  534. mtspr SPRN_MAS7_MAS3,r10
  535. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
  536. tlbwe
  537. BEGIN_MMU_FTR_SECTION
  538. virt_page_table_tlb_miss_done:
  539. /* We have overriden MAS2:EPN but currently our primary TLB miss
  540. * handler will always restore it so that should not be an issue,
  541. * if we ever optimize the primary handler to not write MAS2 on
  542. * some cases, we'll have to restore MAS2:EPN here based on the
  543. * original fault's DEAR. If we do that we have to modify the
  544. * ITLB miss handler to also store SRR0 in the exception frame
  545. * as DEAR.
  546. *
  547. * However, one nasty thing we did is we cleared the reservation
  548. * (well, potentially we did). We do a trick here thus if we
  549. * are not a level 0 exception (we interrupted the TLB miss) we
  550. * offset the return address by -4 in order to replay the tlbsrx
  551. * instruction there
  552. */
  553. subf r10,r13,r12
  554. cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE
  555. bne- 1f
  556. ld r11,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
  557. addi r10,r11,-4
  558. std r10,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
  559. 1:
  560. END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
  561. /* Return to caller, normal case */
  562. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK);
  563. TLB_MISS_EPILOG_SUCCESS
  564. rfi
  565. virt_page_table_tlb_miss_fault:
  566. /* If we fault here, things are a little bit tricky. We need to call
  567. * either data or instruction store fault, and we need to retrieve
  568. * the original fault address and ESR (for data).
  569. *
  570. * The thing is, we know that in normal circumstances, this is
  571. * always called as a second level tlb miss for SW load or as a first
  572. * level TLB miss for HW load, so we should be able to peek at the
  573. * relevant information in the first exception frame in the PACA.
  574. *
  575. * However, we do need to double check that, because we may just hit
  576. * a stray kernel pointer or a userland attack trying to hit those
  577. * areas. If that is the case, we do a data fault. (We can't get here
  578. * from an instruction tlb miss anyway).
  579. *
  580. * Note also that when going to a fault, we must unwind the previous
  581. * level as well. Since we are doing that, we don't need to clear or
  582. * restore the TLB reservation neither.
  583. */
  584. subf r10,r13,r12
  585. cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE
  586. bne- virt_page_table_tlb_miss_whacko_fault
  587. /* We dig the original DEAR and ESR from slot 0 */
  588. ld r15,EX_TLB_DEAR+PACA_EXTLB(r13)
  589. ld r16,EX_TLB_ESR+PACA_EXTLB(r13)
  590. /* We check for the "special" ESR value for instruction faults */
  591. cmpdi cr0,r16,-1
  592. beq 1f
  593. mtspr SPRN_DEAR,r15
  594. mtspr SPRN_ESR,r16
  595. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT);
  596. TLB_MISS_EPILOG_ERROR
  597. b exc_data_storage_book3e
  598. 1: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT);
  599. TLB_MISS_EPILOG_ERROR
  600. b exc_instruction_storage_book3e
  601. virt_page_table_tlb_miss_whacko_fault:
  602. /* The linear fault will restart everything so ESR and DEAR will
  603. * not have been clobbered, let's just fault with what we have
  604. */
  605. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_FAULT);
  606. TLB_MISS_EPILOG_ERROR
  607. b exc_data_storage_book3e
  608. /**************************************************************
  609. * *
  610. * TLB miss handling for Book3E with hw page table support *
  611. * *
  612. **************************************************************/
  613. /* Data TLB miss */
  614. START_EXCEPTION(data_tlb_miss_htw)
  615. TLB_MISS_PROLOG
  616. /* Now we handle the fault proper. We only save DEAR in normal
  617. * fault case since that's the only interesting values here.
  618. * We could probably also optimize by not saving SRR0/1 in the
  619. * linear mapping case but I'll leave that for later
  620. */
  621. mfspr r14,SPRN_ESR
  622. mfspr r16,SPRN_DEAR /* get faulting address */
  623. srdi r11,r16,60 /* get region */
  624. cmpldi cr0,r11,0xc /* linear mapping ? */
  625. TLB_MISS_STATS_SAVE_INFO
  626. beq tlb_load_linear /* yes -> go to linear map load */
  627. /* We do the user/kernel test for the PID here along with the RW test
  628. */
  629. cmpldi cr0,r11,0 /* Check for user region */
  630. ld r15,PACAPGD(r13) /* Load user pgdir */
  631. beq htw_tlb_miss
  632. /* XXX replace the RMW cycles with immediate loads + writes */
  633. 1: mfspr r10,SPRN_MAS1
  634. cmpldi cr0,r11,8 /* Check for vmalloc region */
  635. rlwinm r10,r10,0,16,1 /* Clear TID */
  636. mtspr SPRN_MAS1,r10
  637. ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
  638. beq+ htw_tlb_miss
  639. /* We got a crappy address, just fault with whatever DEAR and ESR
  640. * are here
  641. */
  642. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
  643. TLB_MISS_EPILOG_ERROR
  644. b exc_data_storage_book3e
  645. /* Instruction TLB miss */
  646. START_EXCEPTION(instruction_tlb_miss_htw)
  647. TLB_MISS_PROLOG
  648. /* If we take a recursive fault, the second level handler may need
  649. * to know whether we are handling a data or instruction fault in
  650. * order to get to the right store fault handler. We provide that
  651. * info by keeping a crazy value for ESR in r14
  652. */
  653. li r14,-1 /* store to exception frame is done later */
  654. /* Now we handle the fault proper. We only save DEAR in the non
  655. * linear mapping case since we know the linear mapping case will
  656. * not re-enter. We could indeed optimize and also not save SRR0/1
  657. * in the linear mapping case but I'll leave that for later
  658. *
  659. * Faulting address is SRR0 which is already in r16
  660. */
  661. srdi r11,r16,60 /* get region */
  662. cmpldi cr0,r11,0xc /* linear mapping ? */
  663. TLB_MISS_STATS_SAVE_INFO
  664. beq tlb_load_linear /* yes -> go to linear map load */
  665. /* We do the user/kernel test for the PID here along with the RW test
  666. */
  667. cmpldi cr0,r11,0 /* Check for user region */
  668. ld r15,PACAPGD(r13) /* Load user pgdir */
  669. beq htw_tlb_miss
  670. /* XXX replace the RMW cycles with immediate loads + writes */
  671. 1: mfspr r10,SPRN_MAS1
  672. cmpldi cr0,r11,8 /* Check for vmalloc region */
  673. rlwinm r10,r10,0,16,1 /* Clear TID */
  674. mtspr SPRN_MAS1,r10
  675. ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
  676. beq+ htw_tlb_miss
  677. /* We got a crappy address, just fault */
  678. TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
  679. TLB_MISS_EPILOG_ERROR
  680. b exc_instruction_storage_book3e
  681. /*
  682. * This is the guts of the second-level TLB miss handler for direct
  683. * misses. We are entered with:
  684. *
  685. * r16 = virtual page table faulting address
  686. * r15 = PGD pointer
  687. * r14 = ESR
  688. * r13 = PACA
  689. * r12 = TLB exception frame in PACA
  690. * r11 = crap (free to use)
  691. * r10 = crap (free to use)
  692. *
  693. * It can be re-entered by the linear mapping miss handler. However, to
  694. * avoid too much complication, it will save/restore things for us
  695. */
  696. htw_tlb_miss:
  697. /* Search if we already have a TLB entry for that virtual address, and
  698. * if we do, bail out.
  699. *
  700. * MAS1:IND should be already set based on MAS4
  701. */
  702. PPC_TLBSRX_DOT(0,R16)
  703. beq htw_tlb_miss_done
  704. /* Now, we need to walk the page tables. First check if we are in
  705. * range.
  706. */
  707. rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
  708. bne- htw_tlb_miss_fault
  709. /* Get the PGD pointer */
  710. cmpldi cr0,r15,0
  711. beq- htw_tlb_miss_fault
  712. /* Get to PGD entry */
  713. rldicl r11,r16,64-(PGDIR_SHIFT-3),64-PGD_INDEX_SIZE-3
  714. clrrdi r10,r11,3
  715. ldx r15,r10,r15
  716. cmpdi cr0,r15,0
  717. bge htw_tlb_miss_fault
  718. #ifndef CONFIG_PPC_64K_PAGES
  719. /* Get to PUD entry */
  720. rldicl r11,r16,64-(PUD_SHIFT-3),64-PUD_INDEX_SIZE-3
  721. clrrdi r10,r11,3
  722. ldx r15,r10,r15
  723. cmpdi cr0,r15,0
  724. bge htw_tlb_miss_fault
  725. #endif /* CONFIG_PPC_64K_PAGES */
  726. /* Get to PMD entry */
  727. rldicl r11,r16,64-(PMD_SHIFT-3),64-PMD_INDEX_SIZE-3
  728. clrrdi r10,r11,3
  729. ldx r15,r10,r15
  730. cmpdi cr0,r15,0
  731. bge htw_tlb_miss_fault
  732. /* Ok, we're all right, we can now create an indirect entry for
  733. * a 1M or 256M page.
  734. *
  735. * The last trick is now that because we use "half" pages for
  736. * the HTW (1M IND is 2K and 256M IND is 32K) we need to account
  737. * for an added LSB bit to the RPN. For 64K pages, there is no
  738. * problem as we already use 32K arrays (half PTE pages), but for
  739. * 4K page we need to extract a bit from the virtual address and
  740. * insert it into the "PA52" bit of the RPN.
  741. */
  742. #ifndef CONFIG_PPC_64K_PAGES
  743. rlwimi r15,r16,32-9,20,20
  744. #endif
  745. /* Now we build the MAS:
  746. *
  747. * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
  748. * MAS 1 : Almost fully setup
  749. * - PID already updated by caller if necessary
  750. * - TSIZE for now is base ind page size always
  751. * MAS 2 : Use defaults
  752. * MAS 3+7 : Needs to be done
  753. */
  754. #ifdef CONFIG_PPC_64K_PAGES
  755. ori r10,r15,(BOOK3E_PAGESZ_64K << MAS3_SPSIZE_SHIFT)
  756. #else
  757. ori r10,r15,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
  758. #endif
  759. BEGIN_MMU_FTR_SECTION
  760. srdi r16,r10,32
  761. mtspr SPRN_MAS3,r10
  762. mtspr SPRN_MAS7,r16
  763. MMU_FTR_SECTION_ELSE
  764. mtspr SPRN_MAS7_MAS3,r10
  765. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
  766. tlbwe
  767. htw_tlb_miss_done:
  768. /* We don't bother with restoring DEAR or ESR since we know we are
  769. * level 0 and just going back to userland. They are only needed
  770. * if you are going to take an access fault
  771. */
  772. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK)
  773. TLB_MISS_EPILOG_SUCCESS
  774. rfi
  775. htw_tlb_miss_fault:
  776. /* We need to check if it was an instruction miss. We know this
  777. * though because r14 would contain -1
  778. */
  779. cmpdi cr0,r14,-1
  780. beq 1f
  781. mtspr SPRN_DEAR,r16
  782. mtspr SPRN_ESR,r14
  783. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT)
  784. TLB_MISS_EPILOG_ERROR
  785. b exc_data_storage_book3e
  786. 1: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT)
  787. TLB_MISS_EPILOG_ERROR
  788. b exc_instruction_storage_book3e
  789. /*
  790. * This is the guts of "any" level TLB miss handler for kernel linear
  791. * mapping misses. We are entered with:
  792. *
  793. *
  794. * r16 = faulting address
  795. * r15 = crap (free to use)
  796. * r14 = ESR (data) or -1 (instruction)
  797. * r13 = PACA
  798. * r12 = TLB exception frame in PACA
  799. * r11 = crap (free to use)
  800. * r10 = crap (free to use)
  801. *
  802. * In addition we know that we will not re-enter, so in theory, we could
  803. * use a simpler epilog not restoring SRR0/1 etc.. but we'll do that later.
  804. *
  805. * We also need to be careful about MAS registers here & TLB reservation,
  806. * as we know we'll have clobbered them if we interrupt the main TLB miss
  807. * handlers in which case we probably want to do a full restart at level
  808. * 0 rather than saving / restoring the MAS.
  809. *
  810. * Note: If we care about performance of that core, we can easily shuffle
  811. * a few things around
  812. */
  813. tlb_load_linear:
  814. /* For now, we assume the linear mapping is contiguous and stops at
  815. * linear_map_top. We also assume the size is a multiple of 1G, thus
  816. * we only use 1G pages for now. That might have to be changed in a
  817. * final implementation, especially when dealing with hypervisors
  818. */
  819. ld r11,PACATOC(r13)
  820. ld r11,linear_map_top@got(r11)
  821. ld r10,0(r11)
  822. cmpld cr0,r10,r16
  823. bge tlb_load_linear_fault
  824. /* MAS1 need whole new setup. */
  825. li r15,(BOOK3E_PAGESZ_1GB<<MAS1_TSIZE_SHIFT)
  826. oris r15,r15,MAS1_VALID@h /* MAS1 needs V and TSIZE */
  827. mtspr SPRN_MAS1,r15
  828. /* Already somebody there ? */
  829. PPC_TLBSRX_DOT(0,R16)
  830. beq tlb_load_linear_done
  831. /* Now we build the remaining MAS. MAS0 and 2 should be fine
  832. * with their defaults, which leaves us with MAS 3 and 7. The
  833. * mapping is linear, so we just take the address, clear the
  834. * region bits, and or in the permission bits which are currently
  835. * hard wired
  836. */
  837. clrrdi r10,r16,30 /* 1G page index */
  838. clrldi r10,r10,4 /* clear region bits */
  839. ori r10,r10,MAS3_SR|MAS3_SW|MAS3_SX
  840. BEGIN_MMU_FTR_SECTION
  841. srdi r16,r10,32
  842. mtspr SPRN_MAS3,r10
  843. mtspr SPRN_MAS7,r16
  844. MMU_FTR_SECTION_ELSE
  845. mtspr SPRN_MAS7_MAS3,r10
  846. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
  847. tlbwe
  848. tlb_load_linear_done:
  849. /* We use the "error" epilog for success as we do want to
  850. * restore to the initial faulting context, whatever it was.
  851. * We do that because we can't resume a fault within a TLB
  852. * miss handler, due to MAS and TLB reservation being clobbered.
  853. */
  854. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_LINEAR)
  855. TLB_MISS_EPILOG_ERROR
  856. rfi
  857. tlb_load_linear_fault:
  858. /* We keep the DEAR and ESR around, this shouldn't have happened */
  859. cmpdi cr0,r14,-1
  860. beq 1f
  861. TLB_MISS_EPILOG_ERROR_SPECIAL
  862. b exc_data_storage_book3e
  863. 1: TLB_MISS_EPILOG_ERROR_SPECIAL
  864. b exc_instruction_storage_book3e
  865. #ifdef CONFIG_BOOK3E_MMU_TLB_STATS
  866. .tlb_stat_inc:
  867. 1: ldarx r8,0,r9
  868. addi r8,r8,1
  869. stdcx. r8,0,r9
  870. bne- 1b
  871. blr
  872. #endif