tlb_low_64e.S 29 KB

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