tlb_low_64e.S 23 KB

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