tlb_low_64e.S 23 KB

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