tlb_low_64e.S 23 KB

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