tlb_low_64e.S 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*
  2. * Low level TLB miss handlers for Book3E
  3. *
  4. * Copyright (C) 2008-2009
  5. * Ben. Herrenschmidt (benh@kernel.crashing.org), IBM Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <asm/processor.h>
  13. #include <asm/reg.h>
  14. #include <asm/page.h>
  15. #include <asm/mmu.h>
  16. #include <asm/ppc_asm.h>
  17. #include <asm/asm-offsets.h>
  18. #include <asm/cputable.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/exception-64e.h>
  21. #include <asm/ppc-opcode.h>
  22. #include <asm/kvm_asm.h>
  23. #include <asm/kvm_booke_hv_asm.h>
  24. #ifdef CONFIG_PPC_64K_PAGES
  25. #define VPTE_PMD_SHIFT (PTE_INDEX_SIZE+1)
  26. #else
  27. #define VPTE_PMD_SHIFT (PTE_INDEX_SIZE)
  28. #endif
  29. #define VPTE_PUD_SHIFT (VPTE_PMD_SHIFT + PMD_INDEX_SIZE)
  30. #define VPTE_PGD_SHIFT (VPTE_PUD_SHIFT + PUD_INDEX_SIZE)
  31. #define VPTE_INDEX_SIZE (VPTE_PGD_SHIFT + PGD_INDEX_SIZE)
  32. /**********************************************************************
  33. * *
  34. * TLB miss handling for Book3E with a bolted linear mapping *
  35. * No virtual page table, no nested TLB misses *
  36. * *
  37. **********************************************************************/
  38. /*
  39. * Note that, unlike non-bolted handlers, TLB_EXFRAME is not
  40. * modified by the TLB miss handlers themselves, since the TLB miss
  41. * handler code will not itself cause a recursive TLB miss.
  42. *
  43. * TLB_EXFRAME will be modified when crit/mc/debug exceptions are
  44. * entered/exited.
  45. */
  46. .macro tlb_prolog_bolted intnum addr
  47. mtspr SPRN_SPRG_GEN_SCRATCH,r12
  48. mfspr r12,SPRN_SPRG_TLB_EXFRAME
  49. std r13,EX_TLB_R13(r12)
  50. std r10,EX_TLB_R10(r12)
  51. mfspr r13,SPRN_SPRG_PACA
  52. mfcr r10
  53. std r11,EX_TLB_R11(r12)
  54. #ifdef CONFIG_KVM_BOOKE_HV
  55. BEGIN_FTR_SECTION
  56. mfspr r11, SPRN_SRR1
  57. END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
  58. #endif
  59. DO_KVM \intnum, SPRN_SRR1
  60. std r16,EX_TLB_R16(r12)
  61. mfspr r16,\addr /* get faulting address */
  62. std r14,EX_TLB_R14(r12)
  63. ld r14,PACAPGD(r13)
  64. std r15,EX_TLB_R15(r12)
  65. std r10,EX_TLB_CR(r12)
  66. #ifdef CONFIG_PPC_FSL_BOOK3E
  67. std r7,EX_TLB_R7(r12)
  68. #endif
  69. TLB_MISS_PROLOG_STATS
  70. .endm
  71. .macro tlb_epilog_bolted
  72. ld r14,EX_TLB_CR(r12)
  73. #ifdef CONFIG_PPC_FSL_BOOK3E
  74. ld r7,EX_TLB_R7(r12)
  75. #endif
  76. ld r10,EX_TLB_R10(r12)
  77. ld r11,EX_TLB_R11(r12)
  78. ld r13,EX_TLB_R13(r12)
  79. mtcr r14
  80. ld r14,EX_TLB_R14(r12)
  81. ld r15,EX_TLB_R15(r12)
  82. TLB_MISS_RESTORE_STATS
  83. ld r16,EX_TLB_R16(r12)
  84. mfspr r12,SPRN_SPRG_GEN_SCRATCH
  85. .endm
  86. /* Data TLB miss */
  87. START_EXCEPTION(data_tlb_miss_bolted)
  88. tlb_prolog_bolted BOOKE_INTERRUPT_DTLB_MISS SPRN_DEAR
  89. /* We need _PAGE_PRESENT and _PAGE_ACCESSED set */
  90. /* We do the user/kernel test for the PID here along with the RW test
  91. */
  92. /* We pre-test some combination of permissions to avoid double
  93. * faults:
  94. *
  95. * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE
  96. * ESR_ST is 0x00800000
  97. * _PAGE_BAP_SW is 0x00000010
  98. * So the shift is >> 19. This tests for supervisor writeability.
  99. * If the page happens to be supervisor writeable and not user
  100. * writeable, we will take a new fault later, but that should be
  101. * a rare enough case.
  102. *
  103. * We also move ESR_ST in _PAGE_DIRTY position
  104. * _PAGE_DIRTY is 0x00001000 so the shift is >> 11
  105. *
  106. * MAS1 is preset for all we need except for TID that needs to
  107. * be cleared for kernel translations
  108. */
  109. mfspr r11,SPRN_ESR
  110. srdi r15,r16,60 /* get region */
  111. rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
  112. bne- dtlb_miss_fault_bolted /* Bail if fault addr is invalid */
  113. rlwinm r10,r11,32-19,27,27
  114. rlwimi r10,r11,32-16,19,19
  115. cmpwi r15,0 /* user vs kernel check */
  116. ori r10,r10,_PAGE_PRESENT
  117. oris r11,r10,_PAGE_ACCESSED@h
  118. TLB_MISS_STATS_SAVE_INFO_BOLTED
  119. bne tlb_miss_kernel_bolted
  120. tlb_miss_common_bolted:
  121. /*
  122. * This is the guts of the TLB miss handler for bolted-linear.
  123. * We are entered with:
  124. *
  125. * r16 = faulting address
  126. * r15 = crap (free to use)
  127. * r14 = page table base
  128. * r13 = PACA
  129. * r11 = PTE permission mask
  130. * r10 = crap (free to use)
  131. */
  132. rldicl r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3
  133. cmpldi cr0,r14,0
  134. clrrdi r15,r15,3
  135. beq tlb_miss_fault_bolted /* No PGDIR, bail */
  136. BEGIN_MMU_FTR_SECTION
  137. /* Set the TLB reservation and search for existing entry. Then load
  138. * the entry.
  139. */
  140. PPC_TLBSRX_DOT(0,R16)
  141. ldx r14,r14,r15 /* grab pgd entry */
  142. beq tlb_miss_done_bolted /* tlb exists already, bail */
  143. MMU_FTR_SECTION_ELSE
  144. ldx r14,r14,r15 /* grab pgd entry */
  145. ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_USE_TLBRSRV)
  146. #ifndef CONFIG_PPC_64K_PAGES
  147. rldicl r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3
  148. clrrdi r15,r15,3
  149. cmpdi cr0,r14,0
  150. bge tlb_miss_fault_bolted /* Bad pgd entry or hugepage; bail */
  151. ldx r14,r14,r15 /* grab pud entry */
  152. #endif /* CONFIG_PPC_64K_PAGES */
  153. rldicl r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3
  154. clrrdi r15,r15,3
  155. cmpdi cr0,r14,0
  156. bge tlb_miss_fault_bolted
  157. ldx r14,r14,r15 /* Grab pmd entry */
  158. rldicl r15,r16,64-PAGE_SHIFT+3,64-PTE_INDEX_SIZE-3
  159. clrrdi r15,r15,3
  160. cmpdi cr0,r14,0
  161. bge tlb_miss_fault_bolted
  162. ldx r14,r14,r15 /* Grab PTE, normal (!huge) page */
  163. /* Check if required permissions are met */
  164. andc. r15,r11,r14
  165. rldicr r15,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
  166. bne- tlb_miss_fault_bolted
  167. /* Now we build the MAS:
  168. *
  169. * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
  170. * MAS 1 : Almost fully setup
  171. * - PID already updated by caller if necessary
  172. * - TSIZE need change if !base page size, not
  173. * yet implemented for now
  174. * MAS 2 : Defaults not useful, need to be redone
  175. * MAS 3+7 : Needs to be done
  176. */
  177. clrrdi r11,r16,12 /* Clear low crap in EA */
  178. clrldi r15,r15,12 /* Clear crap at the top */
  179. rlwimi r11,r14,32-19,27,31 /* Insert WIMGE */
  180. rlwimi r15,r14,32-8,22,25 /* Move in U bits */
  181. mtspr SPRN_MAS2,r11
  182. andi. r11,r14,_PAGE_DIRTY
  183. rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */
  184. /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
  185. bne 1f
  186. li r11,MAS3_SW|MAS3_UW
  187. andc r15,r15,r11
  188. 1:
  189. mtspr SPRN_MAS7_MAS3,r15
  190. tlbwe
  191. tlb_miss_done_bolted:
  192. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
  193. tlb_epilog_bolted
  194. rfi
  195. itlb_miss_kernel_bolted:
  196. li r11,_PAGE_PRESENT|_PAGE_BAP_SX /* Base perm */
  197. oris r11,r11,_PAGE_ACCESSED@h
  198. tlb_miss_kernel_bolted:
  199. mfspr r10,SPRN_MAS1
  200. ld r14,PACA_KERNELPGD(r13)
  201. cmpldi cr0,r15,8 /* Check for vmalloc region */
  202. rlwinm r10,r10,0,16,1 /* Clear TID */
  203. mtspr SPRN_MAS1,r10
  204. beq+ tlb_miss_common_bolted
  205. tlb_miss_fault_bolted:
  206. /* We need to check if it was an instruction miss */
  207. andi. r10,r11,_PAGE_EXEC|_PAGE_BAP_SX
  208. bne itlb_miss_fault_bolted
  209. dtlb_miss_fault_bolted:
  210. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
  211. tlb_epilog_bolted
  212. b exc_data_storage_book3e
  213. itlb_miss_fault_bolted:
  214. TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
  215. tlb_epilog_bolted
  216. b exc_instruction_storage_book3e
  217. /* Instruction TLB miss */
  218. START_EXCEPTION(instruction_tlb_miss_bolted)
  219. tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0
  220. rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
  221. srdi r15,r16,60 /* get region */
  222. TLB_MISS_STATS_SAVE_INFO_BOLTED
  223. bne- itlb_miss_fault_bolted
  224. li r11,_PAGE_PRESENT|_PAGE_EXEC /* Base perm */
  225. /* We do the user/kernel test for the PID here along with the RW test
  226. */
  227. cmpldi cr0,r15,0 /* Check for user region */
  228. oris r11,r11,_PAGE_ACCESSED@h
  229. beq tlb_miss_common_bolted
  230. b itlb_miss_kernel_bolted
  231. #ifdef CONFIG_PPC_FSL_BOOK3E
  232. /*
  233. * TLB miss handling for e6500 and derivatives, using hardware tablewalk.
  234. *
  235. * Linear mapping is bolted: no virtual page table or nested TLB misses
  236. * Indirect entries in TLB1, hardware loads resulting direct entries
  237. * into TLB0
  238. * No HES or NV hint on TLB1, so we need to do software round-robin
  239. * No tlbsrx. so we need a spinlock, and we have to deal
  240. * with MAS-damage caused by tlbsx
  241. * 4K pages only
  242. */
  243. START_EXCEPTION(instruction_tlb_miss_e6500)
  244. tlb_prolog_bolted BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR0
  245. ld r11,PACA_TCD_PTR(r13)
  246. srdi. r15,r16,60 /* get region */
  247. ori r16,r16,1
  248. TLB_MISS_STATS_SAVE_INFO_BOLTED
  249. bne tlb_miss_kernel_e6500 /* user/kernel test */
  250. b tlb_miss_common_e6500
  251. START_EXCEPTION(data_tlb_miss_e6500)
  252. tlb_prolog_bolted BOOKE_INTERRUPT_DTLB_MISS SPRN_DEAR
  253. ld r11,PACA_TCD_PTR(r13)
  254. srdi. r15,r16,60 /* get region */
  255. rldicr r16,r16,0,62
  256. TLB_MISS_STATS_SAVE_INFO_BOLTED
  257. bne tlb_miss_kernel_e6500 /* user vs kernel check */
  258. /*
  259. * This is the guts of the TLB miss handler for e6500 and derivatives.
  260. * We are entered with:
  261. *
  262. * r16 = page of faulting address (low bit 0 if data, 1 if instruction)
  263. * r15 = crap (free to use)
  264. * r14 = page table base
  265. * r13 = PACA
  266. * r11 = tlb_per_core ptr
  267. * r10 = crap (free to use)
  268. * r7 = esel_next
  269. */
  270. tlb_miss_common_e6500:
  271. crmove cr2*4+2,cr0*4+2 /* cr2.eq != 0 if kernel address */
  272. BEGIN_FTR_SECTION /* CPU_FTR_SMT */
  273. /*
  274. * Search if we already have an indirect entry for that virtual
  275. * address, and if we do, bail out.
  276. *
  277. * MAS6:IND should be already set based on MAS4
  278. */
  279. lhz r10,PACAPACAINDEX(r13)
  280. addi r10,r10,1
  281. crclr cr1*4+eq /* set cr1.eq = 0 for non-recursive */
  282. 1: lbarx r15,0,r11
  283. cmpdi r15,0
  284. bne 2f
  285. stbcx. r10,0,r11
  286. bne 1b
  287. 3:
  288. .subsection 1
  289. 2: cmpd cr1,r15,r10 /* recursive lock due to mcheck/crit/etc? */
  290. beq cr1,3b /* unlock will happen if cr1.eq = 0 */
  291. 10: lbz r15,0(r11)
  292. cmpdi r15,0
  293. bne 10b
  294. b 1b
  295. .previous
  296. END_FTR_SECTION_IFSET(CPU_FTR_SMT)
  297. lbz r7,TCD_ESEL_NEXT(r11)
  298. BEGIN_FTR_SECTION /* CPU_FTR_SMT */
  299. /*
  300. * Erratum A-008139 says that we can't use tlbwe to change
  301. * an indirect entry in any way (including replacing or
  302. * invalidating) if the other thread could be in the process
  303. * of a lookup. The workaround is to invalidate the entry
  304. * with tlbilx before overwriting.
  305. */
  306. rlwinm r10,r7,16,0xff0000
  307. oris r10,r10,MAS0_TLBSEL(1)@h
  308. mtspr SPRN_MAS0,r10
  309. isync
  310. tlbre
  311. mfspr r15,SPRN_MAS1
  312. andis. r15,r15,MAS1_VALID@h
  313. beq 5f
  314. BEGIN_FTR_SECTION_NESTED(532)
  315. mfspr r10,SPRN_MAS8
  316. rlwinm r10,r10,0,0x80000fff /* tgs,tlpid -> sgs,slpid */
  317. mtspr SPRN_MAS5,r10
  318. END_FTR_SECTION_NESTED(CPU_FTR_EMB_HV,CPU_FTR_EMB_HV,532)
  319. mfspr r10,SPRN_MAS1
  320. rlwinm r15,r10,0,0x3fff0000 /* tid -> spid */
  321. rlwimi r15,r10,20,0x00000003 /* ind,ts -> sind,sas */
  322. mfspr r10,SPRN_MAS6
  323. mtspr SPRN_MAS6,r15
  324. mfspr r15,SPRN_MAS2
  325. isync
  326. tlbilxva 0,r15
  327. isync
  328. mtspr SPRN_MAS6,r10
  329. 5:
  330. BEGIN_FTR_SECTION_NESTED(532)
  331. li r10,0
  332. mtspr SPRN_MAS8,r10
  333. mtspr SPRN_MAS5,r10
  334. END_FTR_SECTION_NESTED(CPU_FTR_EMB_HV,CPU_FTR_EMB_HV,532)
  335. tlbsx 0,r16
  336. mfspr r10,SPRN_MAS1
  337. andis. r15,r10,MAS1_VALID@h
  338. bne tlb_miss_done_e6500
  339. FTR_SECTION_ELSE
  340. mfspr r10,SPRN_MAS1
  341. ALT_FTR_SECTION_END_IFSET(CPU_FTR_SMT)
  342. oris r10,r10,MAS1_VALID@h
  343. beq cr2,4f
  344. rlwinm r10,r10,0,16,1 /* Clear TID */
  345. 4: mtspr SPRN_MAS1,r10
  346. /* Now, we need to walk the page tables. First check if we are in
  347. * range.
  348. */
  349. rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
  350. bne- tlb_miss_fault_e6500
  351. rldicl r15,r16,64-PGDIR_SHIFT+3,64-PGD_INDEX_SIZE-3
  352. cmpldi cr0,r14,0
  353. clrrdi r15,r15,3
  354. beq- tlb_miss_fault_e6500 /* No PGDIR, bail */
  355. ldx r14,r14,r15 /* grab pgd entry */
  356. rldicl r15,r16,64-PUD_SHIFT+3,64-PUD_INDEX_SIZE-3
  357. clrrdi r15,r15,3
  358. cmpdi cr0,r14,0
  359. bge tlb_miss_huge_e6500 /* Bad pgd entry or hugepage; bail */
  360. ldx r14,r14,r15 /* grab pud entry */
  361. rldicl r15,r16,64-PMD_SHIFT+3,64-PMD_INDEX_SIZE-3
  362. clrrdi r15,r15,3
  363. cmpdi cr0,r14,0
  364. bge tlb_miss_huge_e6500
  365. ldx r14,r14,r15 /* Grab pmd entry */
  366. mfspr r10,SPRN_MAS0
  367. cmpdi cr0,r14,0
  368. bge tlb_miss_huge_e6500
  369. /* Now we build the MAS for a 2M indirect page:
  370. *
  371. * MAS 0 : ESEL needs to be filled by software round-robin
  372. * MAS 1 : Fully set up
  373. * - PID already updated by caller if necessary
  374. * - TSIZE for now is base ind page size always
  375. * - TID already cleared if necessary
  376. * MAS 2 : Default not 2M-aligned, need to be redone
  377. * MAS 3+7 : Needs to be done
  378. */
  379. ori r14,r14,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
  380. mtspr SPRN_MAS7_MAS3,r14
  381. clrrdi r15,r16,21 /* make EA 2M-aligned */
  382. mtspr SPRN_MAS2,r15
  383. tlb_miss_huge_done_e6500:
  384. lbz r16,TCD_ESEL_MAX(r11)
  385. lbz r14,TCD_ESEL_FIRST(r11)
  386. rlwimi r10,r7,16,0x00ff0000 /* insert esel_next into MAS0 */
  387. addi r7,r7,1 /* increment esel_next */
  388. mtspr SPRN_MAS0,r10
  389. cmpw r7,r16
  390. iseleq r7,r14,r7 /* if next == last use first */
  391. stb r7,TCD_ESEL_NEXT(r11)
  392. tlbwe
  393. tlb_miss_done_e6500:
  394. .macro tlb_unlock_e6500
  395. BEGIN_FTR_SECTION
  396. beq cr1,1f /* no unlock if lock was recursively grabbed */
  397. li r15,0
  398. isync
  399. stb r15,0(r11)
  400. 1:
  401. END_FTR_SECTION_IFSET(CPU_FTR_SMT)
  402. .endm
  403. tlb_unlock_e6500
  404. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
  405. tlb_epilog_bolted
  406. rfi
  407. tlb_miss_huge_e6500:
  408. beq tlb_miss_fault_e6500
  409. li r10,1
  410. andi. r15,r14,HUGEPD_SHIFT_MASK@l /* r15 = psize */
  411. rldimi r14,r10,63,0 /* Set PD_HUGE */
  412. xor r14,r14,r15 /* Clear size bits */
  413. ldx r14,0,r14
  414. /*
  415. * Now we build the MAS for a huge page.
  416. *
  417. * MAS 0 : ESEL needs to be filled by software round-robin
  418. * - can be handled by indirect code
  419. * MAS 1 : Need to clear IND and set TSIZE
  420. * MAS 2,3+7: Needs to be redone similar to non-tablewalk handler
  421. */
  422. subi r15,r15,10 /* Convert psize to tsize */
  423. mfspr r10,SPRN_MAS1
  424. rlwinm r10,r10,0,~MAS1_IND
  425. rlwimi r10,r15,MAS1_TSIZE_SHIFT,MAS1_TSIZE_MASK
  426. mtspr SPRN_MAS1,r10
  427. li r10,-0x400
  428. sld r15,r10,r15 /* Generate mask based on size */
  429. and r10,r16,r15
  430. rldicr r15,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
  431. rlwimi r10,r14,32-19,27,31 /* Insert WIMGE */
  432. clrldi r15,r15,PAGE_SHIFT /* Clear crap at the top */
  433. rlwimi r15,r14,32-8,22,25 /* Move in U bits */
  434. mtspr SPRN_MAS2,r10
  435. andi. r10,r14,_PAGE_DIRTY
  436. rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */
  437. /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
  438. bne 1f
  439. li r10,MAS3_SW|MAS3_UW
  440. andc r15,r15,r10
  441. 1:
  442. mtspr SPRN_MAS7_MAS3,r15
  443. mfspr r10,SPRN_MAS0
  444. b tlb_miss_huge_done_e6500
  445. tlb_miss_kernel_e6500:
  446. ld r14,PACA_KERNELPGD(r13)
  447. cmpldi cr1,r15,8 /* Check for vmalloc region */
  448. beq+ cr1,tlb_miss_common_e6500
  449. tlb_miss_fault_e6500:
  450. tlb_unlock_e6500
  451. /* We need to check if it was an instruction miss */
  452. andi. r16,r16,1
  453. bne itlb_miss_fault_e6500
  454. dtlb_miss_fault_e6500:
  455. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
  456. tlb_epilog_bolted
  457. b exc_data_storage_book3e
  458. itlb_miss_fault_e6500:
  459. TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
  460. tlb_epilog_bolted
  461. b exc_instruction_storage_book3e
  462. #endif /* CONFIG_PPC_FSL_BOOK3E */
  463. /**********************************************************************
  464. * *
  465. * TLB miss handling for Book3E with TLB reservation and HES support *
  466. * *
  467. **********************************************************************/
  468. /* Data TLB miss */
  469. START_EXCEPTION(data_tlb_miss)
  470. TLB_MISS_PROLOG
  471. /* Now we handle the fault proper. We only save DEAR in normal
  472. * fault case since that's the only interesting values here.
  473. * We could probably also optimize by not saving SRR0/1 in the
  474. * linear mapping case but I'll leave that for later
  475. */
  476. mfspr r14,SPRN_ESR
  477. mfspr r16,SPRN_DEAR /* get faulting address */
  478. srdi r15,r16,60 /* get region */
  479. cmpldi cr0,r15,0xc /* linear mapping ? */
  480. TLB_MISS_STATS_SAVE_INFO
  481. beq tlb_load_linear /* yes -> go to linear map load */
  482. /* The page tables are mapped virtually linear. At this point, though,
  483. * we don't know whether we are trying to fault in a first level
  484. * virtual address or a virtual page table address. We can get that
  485. * from bit 0x1 of the region ID which we have set for a page table
  486. */
  487. andi. r10,r15,0x1
  488. bne- virt_page_table_tlb_miss
  489. std r14,EX_TLB_ESR(r12); /* save ESR */
  490. std r16,EX_TLB_DEAR(r12); /* save DEAR */
  491. /* We need _PAGE_PRESENT and _PAGE_ACCESSED set */
  492. li r11,_PAGE_PRESENT
  493. oris r11,r11,_PAGE_ACCESSED@h
  494. /* We do the user/kernel test for the PID here along with the RW test
  495. */
  496. cmpldi cr0,r15,0 /* Check for user region */
  497. /* We pre-test some combination of permissions to avoid double
  498. * faults:
  499. *
  500. * We move the ESR:ST bit into the position of _PAGE_BAP_SW in the PTE
  501. * ESR_ST is 0x00800000
  502. * _PAGE_BAP_SW is 0x00000010
  503. * So the shift is >> 19. This tests for supervisor writeability.
  504. * If the page happens to be supervisor writeable and not user
  505. * writeable, we will take a new fault later, but that should be
  506. * a rare enough case.
  507. *
  508. * We also move ESR_ST in _PAGE_DIRTY position
  509. * _PAGE_DIRTY is 0x00001000 so the shift is >> 11
  510. *
  511. * MAS1 is preset for all we need except for TID that needs to
  512. * be cleared for kernel translations
  513. */
  514. rlwimi r11,r14,32-19,27,27
  515. rlwimi r11,r14,32-16,19,19
  516. beq normal_tlb_miss
  517. /* XXX replace the RMW cycles with immediate loads + writes */
  518. 1: mfspr r10,SPRN_MAS1
  519. cmpldi cr0,r15,8 /* Check for vmalloc region */
  520. rlwinm r10,r10,0,16,1 /* Clear TID */
  521. mtspr SPRN_MAS1,r10
  522. beq+ normal_tlb_miss
  523. /* We got a crappy address, just fault with whatever DEAR and ESR
  524. * are here
  525. */
  526. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
  527. TLB_MISS_EPILOG_ERROR
  528. b exc_data_storage_book3e
  529. /* Instruction TLB miss */
  530. START_EXCEPTION(instruction_tlb_miss)
  531. TLB_MISS_PROLOG
  532. /* If we take a recursive fault, the second level handler may need
  533. * to know whether we are handling a data or instruction fault in
  534. * order to get to the right store fault handler. We provide that
  535. * info by writing a crazy value in ESR in our exception frame
  536. */
  537. li r14,-1 /* store to exception frame is done later */
  538. /* Now we handle the fault proper. We only save DEAR in the non
  539. * linear mapping case since we know the linear mapping case will
  540. * not re-enter. We could indeed optimize and also not save SRR0/1
  541. * in the linear mapping case but I'll leave that for later
  542. *
  543. * Faulting address is SRR0 which is already in r16
  544. */
  545. srdi r15,r16,60 /* get region */
  546. cmpldi cr0,r15,0xc /* linear mapping ? */
  547. TLB_MISS_STATS_SAVE_INFO
  548. beq tlb_load_linear /* yes -> go to linear map load */
  549. /* We do the user/kernel test for the PID here along with the RW test
  550. */
  551. li r11,_PAGE_PRESENT|_PAGE_EXEC /* Base perm */
  552. oris r11,r11,_PAGE_ACCESSED@h
  553. cmpldi cr0,r15,0 /* Check for user region */
  554. std r14,EX_TLB_ESR(r12) /* write crazy -1 to frame */
  555. beq normal_tlb_miss
  556. li r11,_PAGE_PRESENT|_PAGE_BAP_SX /* Base perm */
  557. oris r11,r11,_PAGE_ACCESSED@h
  558. /* XXX replace the RMW cycles with immediate loads + writes */
  559. mfspr r10,SPRN_MAS1
  560. cmpldi cr0,r15,8 /* Check for vmalloc region */
  561. rlwinm r10,r10,0,16,1 /* Clear TID */
  562. mtspr SPRN_MAS1,r10
  563. beq+ normal_tlb_miss
  564. /* We got a crappy address, just fault */
  565. TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
  566. TLB_MISS_EPILOG_ERROR
  567. b exc_instruction_storage_book3e
  568. /*
  569. * This is the guts of the first-level TLB miss handler for direct
  570. * misses. We are entered with:
  571. *
  572. * r16 = faulting address
  573. * r15 = region ID
  574. * r14 = crap (free to use)
  575. * r13 = PACA
  576. * r12 = TLB exception frame in PACA
  577. * r11 = PTE permission mask
  578. * r10 = crap (free to use)
  579. */
  580. normal_tlb_miss:
  581. /* So we first construct the page table address. We do that by
  582. * shifting the bottom of the address (not the region ID) by
  583. * PAGE_SHIFT-3, clearing the bottom 3 bits (get a PTE ptr) and
  584. * or'ing the fourth high bit.
  585. *
  586. * NOTE: For 64K pages, we do things slightly differently in
  587. * order to handle the weird page table format used by linux
  588. */
  589. ori r10,r15,0x1
  590. #ifdef CONFIG_PPC_64K_PAGES
  591. /* For the top bits, 16 bytes per PTE */
  592. rldicl r14,r16,64-(PAGE_SHIFT-4),PAGE_SHIFT-4+4
  593. /* Now create the bottom bits as 0 in position 0x8000 and
  594. * the rest calculated for 8 bytes per PTE
  595. */
  596. rldicl r15,r16,64-(PAGE_SHIFT-3),64-15
  597. /* Insert the bottom bits in */
  598. rlwimi r14,r15,0,16,31
  599. #else
  600. rldicl r14,r16,64-(PAGE_SHIFT-3),PAGE_SHIFT-3+4
  601. #endif
  602. sldi r15,r10,60
  603. clrrdi r14,r14,3
  604. or r10,r15,r14
  605. BEGIN_MMU_FTR_SECTION
  606. /* Set the TLB reservation and search for existing entry. Then load
  607. * the entry.
  608. */
  609. PPC_TLBSRX_DOT(0,R16)
  610. ld r14,0(r10)
  611. beq normal_tlb_miss_done
  612. MMU_FTR_SECTION_ELSE
  613. ld r14,0(r10)
  614. ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_USE_TLBRSRV)
  615. finish_normal_tlb_miss:
  616. /* Check if required permissions are met */
  617. andc. r15,r11,r14
  618. bne- normal_tlb_miss_access_fault
  619. /* Now we build the MAS:
  620. *
  621. * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
  622. * MAS 1 : Almost fully setup
  623. * - PID already updated by caller if necessary
  624. * - TSIZE need change if !base page size, not
  625. * yet implemented for now
  626. * MAS 2 : Defaults not useful, need to be redone
  627. * MAS 3+7 : Needs to be done
  628. *
  629. * TODO: mix up code below for better scheduling
  630. */
  631. clrrdi r11,r16,12 /* Clear low crap in EA */
  632. rlwimi r11,r14,32-19,27,31 /* Insert WIMGE */
  633. mtspr SPRN_MAS2,r11
  634. /* Check page size, if not standard, update MAS1 */
  635. rldicl r11,r14,64-8,64-8
  636. #ifdef CONFIG_PPC_64K_PAGES
  637. cmpldi cr0,r11,BOOK3E_PAGESZ_64K
  638. #else
  639. cmpldi cr0,r11,BOOK3E_PAGESZ_4K
  640. #endif
  641. beq- 1f
  642. mfspr r11,SPRN_MAS1
  643. rlwimi r11,r14,31,21,24
  644. rlwinm r11,r11,0,21,19
  645. mtspr SPRN_MAS1,r11
  646. 1:
  647. /* Move RPN in position */
  648. rldicr r11,r14,64-(PTE_RPN_SHIFT-PAGE_SHIFT),63-PAGE_SHIFT
  649. clrldi r15,r11,12 /* Clear crap at the top */
  650. rlwimi r15,r14,32-8,22,25 /* Move in U bits */
  651. rlwimi r15,r14,32-2,26,31 /* Move in BAP bits */
  652. /* Mask out SW and UW if !DIRTY (XXX optimize this !) */
  653. andi. r11,r14,_PAGE_DIRTY
  654. bne 1f
  655. li r11,MAS3_SW|MAS3_UW
  656. andc r15,r15,r11
  657. 1:
  658. BEGIN_MMU_FTR_SECTION
  659. srdi r16,r15,32
  660. mtspr SPRN_MAS3,r15
  661. mtspr SPRN_MAS7,r16
  662. MMU_FTR_SECTION_ELSE
  663. mtspr SPRN_MAS7_MAS3,r15
  664. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
  665. tlbwe
  666. normal_tlb_miss_done:
  667. /* We don't bother with restoring DEAR or ESR since we know we are
  668. * level 0 and just going back to userland. They are only needed
  669. * if you are going to take an access fault
  670. */
  671. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_NORM_OK)
  672. TLB_MISS_EPILOG_SUCCESS
  673. rfi
  674. normal_tlb_miss_access_fault:
  675. /* We need to check if it was an instruction miss */
  676. andi. r10,r11,_PAGE_EXEC
  677. bne 1f
  678. ld r14,EX_TLB_DEAR(r12)
  679. ld r15,EX_TLB_ESR(r12)
  680. mtspr SPRN_DEAR,r14
  681. mtspr SPRN_ESR,r15
  682. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
  683. TLB_MISS_EPILOG_ERROR
  684. b exc_data_storage_book3e
  685. 1: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
  686. TLB_MISS_EPILOG_ERROR
  687. b exc_instruction_storage_book3e
  688. /*
  689. * This is the guts of the second-level TLB miss handler for direct
  690. * misses. We are entered with:
  691. *
  692. * r16 = virtual page table faulting address
  693. * r15 = region (top 4 bits of address)
  694. * r14 = crap (free to use)
  695. * r13 = PACA
  696. * r12 = TLB exception frame in PACA
  697. * r11 = crap (free to use)
  698. * r10 = crap (free to use)
  699. *
  700. * Note that this should only ever be called as a second level handler
  701. * with the current scheme when using SW load.
  702. * That means we can always get the original fault DEAR at
  703. * EX_TLB_DEAR-EX_TLB_SIZE(r12)
  704. *
  705. * It can be re-entered by the linear mapping miss handler. However, to
  706. * avoid too much complication, it will restart the whole fault at level
  707. * 0 so we don't care too much about clobbers
  708. *
  709. * XXX That code was written back when we couldn't clobber r14. We can now,
  710. * so we could probably optimize things a bit
  711. */
  712. virt_page_table_tlb_miss:
  713. /* Are we hitting a kernel page table ? */
  714. andi. r10,r15,0x8
  715. /* The cool thing now is that r10 contains 0 for user and 8 for kernel,
  716. * and we happen to have the swapper_pg_dir at offset 8 from the user
  717. * pgdir in the PACA :-).
  718. */
  719. add r11,r10,r13
  720. /* If kernel, we need to clear MAS1 TID */
  721. beq 1f
  722. /* XXX replace the RMW cycles with immediate loads + writes */
  723. mfspr r10,SPRN_MAS1
  724. rlwinm r10,r10,0,16,1 /* Clear TID */
  725. mtspr SPRN_MAS1,r10
  726. 1:
  727. BEGIN_MMU_FTR_SECTION
  728. /* Search if we already have a TLB entry for that virtual address, and
  729. * if we do, bail out.
  730. */
  731. PPC_TLBSRX_DOT(0,R16)
  732. beq virt_page_table_tlb_miss_done
  733. END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
  734. /* Now, we need to walk the page tables. First check if we are in
  735. * range.
  736. */
  737. rldicl. r10,r16,64-(VPTE_INDEX_SIZE+3),VPTE_INDEX_SIZE+3+4
  738. bne- virt_page_table_tlb_miss_fault
  739. /* Get the PGD pointer */
  740. ld r15,PACAPGD(r11)
  741. cmpldi cr0,r15,0
  742. beq- virt_page_table_tlb_miss_fault
  743. /* Get to PGD entry */
  744. rldicl r11,r16,64-VPTE_PGD_SHIFT,64-PGD_INDEX_SIZE-3
  745. clrrdi r10,r11,3
  746. ldx r15,r10,r15
  747. cmpdi cr0,r15,0
  748. bge virt_page_table_tlb_miss_fault
  749. #ifndef CONFIG_PPC_64K_PAGES
  750. /* Get to PUD entry */
  751. rldicl r11,r16,64-VPTE_PUD_SHIFT,64-PUD_INDEX_SIZE-3
  752. clrrdi r10,r11,3
  753. ldx r15,r10,r15
  754. cmpdi cr0,r15,0
  755. bge virt_page_table_tlb_miss_fault
  756. #endif /* CONFIG_PPC_64K_PAGES */
  757. /* Get to PMD entry */
  758. rldicl r11,r16,64-VPTE_PMD_SHIFT,64-PMD_INDEX_SIZE-3
  759. clrrdi r10,r11,3
  760. ldx r15,r10,r15
  761. cmpdi cr0,r15,0
  762. bge virt_page_table_tlb_miss_fault
  763. /* Ok, we're all right, we can now create a kernel translation for
  764. * a 4K or 64K page from r16 -> r15.
  765. */
  766. /* Now we build the MAS:
  767. *
  768. * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
  769. * MAS 1 : Almost fully setup
  770. * - PID already updated by caller if necessary
  771. * - TSIZE for now is base page size always
  772. * MAS 2 : Use defaults
  773. * MAS 3+7 : Needs to be done
  774. *
  775. * So we only do MAS 2 and 3 for now...
  776. */
  777. clrldi r11,r15,4 /* remove region ID from RPN */
  778. ori r10,r11,1 /* Or-in SR */
  779. BEGIN_MMU_FTR_SECTION
  780. srdi r16,r10,32
  781. mtspr SPRN_MAS3,r10
  782. mtspr SPRN_MAS7,r16
  783. MMU_FTR_SECTION_ELSE
  784. mtspr SPRN_MAS7_MAS3,r10
  785. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
  786. tlbwe
  787. BEGIN_MMU_FTR_SECTION
  788. virt_page_table_tlb_miss_done:
  789. /* We have overriden MAS2:EPN but currently our primary TLB miss
  790. * handler will always restore it so that should not be an issue,
  791. * if we ever optimize the primary handler to not write MAS2 on
  792. * some cases, we'll have to restore MAS2:EPN here based on the
  793. * original fault's DEAR. If we do that we have to modify the
  794. * ITLB miss handler to also store SRR0 in the exception frame
  795. * as DEAR.
  796. *
  797. * However, one nasty thing we did is we cleared the reservation
  798. * (well, potentially we did). We do a trick here thus if we
  799. * are not a level 0 exception (we interrupted the TLB miss) we
  800. * offset the return address by -4 in order to replay the tlbsrx
  801. * instruction there
  802. */
  803. subf r10,r13,r12
  804. cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE
  805. bne- 1f
  806. ld r11,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
  807. addi r10,r11,-4
  808. std r10,PACA_EXTLB+EX_TLB_SIZE+EX_TLB_SRR0(r13)
  809. 1:
  810. END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_TLBRSRV)
  811. /* Return to caller, normal case */
  812. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK);
  813. TLB_MISS_EPILOG_SUCCESS
  814. rfi
  815. virt_page_table_tlb_miss_fault:
  816. /* If we fault here, things are a little bit tricky. We need to call
  817. * either data or instruction store fault, and we need to retrieve
  818. * the original fault address and ESR (for data).
  819. *
  820. * The thing is, we know that in normal circumstances, this is
  821. * always called as a second level tlb miss for SW load or as a first
  822. * level TLB miss for HW load, so we should be able to peek at the
  823. * relevant information in the first exception frame in the PACA.
  824. *
  825. * However, we do need to double check that, because we may just hit
  826. * a stray kernel pointer or a userland attack trying to hit those
  827. * areas. If that is the case, we do a data fault. (We can't get here
  828. * from an instruction tlb miss anyway).
  829. *
  830. * Note also that when going to a fault, we must unwind the previous
  831. * level as well. Since we are doing that, we don't need to clear or
  832. * restore the TLB reservation neither.
  833. */
  834. subf r10,r13,r12
  835. cmpldi cr0,r10,PACA_EXTLB+EX_TLB_SIZE
  836. bne- virt_page_table_tlb_miss_whacko_fault
  837. /* We dig the original DEAR and ESR from slot 0 */
  838. ld r15,EX_TLB_DEAR+PACA_EXTLB(r13)
  839. ld r16,EX_TLB_ESR+PACA_EXTLB(r13)
  840. /* We check for the "special" ESR value for instruction faults */
  841. cmpdi cr0,r16,-1
  842. beq 1f
  843. mtspr SPRN_DEAR,r15
  844. mtspr SPRN_ESR,r16
  845. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT);
  846. TLB_MISS_EPILOG_ERROR
  847. b exc_data_storage_book3e
  848. 1: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT);
  849. TLB_MISS_EPILOG_ERROR
  850. b exc_instruction_storage_book3e
  851. virt_page_table_tlb_miss_whacko_fault:
  852. /* The linear fault will restart everything so ESR and DEAR will
  853. * not have been clobbered, let's just fault with what we have
  854. */
  855. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_FAULT);
  856. TLB_MISS_EPILOG_ERROR
  857. b exc_data_storage_book3e
  858. /**************************************************************
  859. * *
  860. * TLB miss handling for Book3E with hw page table support *
  861. * *
  862. **************************************************************/
  863. /* Data TLB miss */
  864. START_EXCEPTION(data_tlb_miss_htw)
  865. TLB_MISS_PROLOG
  866. /* Now we handle the fault proper. We only save DEAR in normal
  867. * fault case since that's the only interesting values here.
  868. * We could probably also optimize by not saving SRR0/1 in the
  869. * linear mapping case but I'll leave that for later
  870. */
  871. mfspr r14,SPRN_ESR
  872. mfspr r16,SPRN_DEAR /* get faulting address */
  873. srdi r11,r16,60 /* get region */
  874. cmpldi cr0,r11,0xc /* linear mapping ? */
  875. TLB_MISS_STATS_SAVE_INFO
  876. beq tlb_load_linear /* yes -> go to linear map load */
  877. /* We do the user/kernel test for the PID here along with the RW test
  878. */
  879. cmpldi cr0,r11,0 /* Check for user region */
  880. ld r15,PACAPGD(r13) /* Load user pgdir */
  881. beq htw_tlb_miss
  882. /* XXX replace the RMW cycles with immediate loads + writes */
  883. 1: mfspr r10,SPRN_MAS1
  884. cmpldi cr0,r11,8 /* Check for vmalloc region */
  885. rlwinm r10,r10,0,16,1 /* Clear TID */
  886. mtspr SPRN_MAS1,r10
  887. ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
  888. beq+ htw_tlb_miss
  889. /* We got a crappy address, just fault with whatever DEAR and ESR
  890. * are here
  891. */
  892. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_NORM_FAULT)
  893. TLB_MISS_EPILOG_ERROR
  894. b exc_data_storage_book3e
  895. /* Instruction TLB miss */
  896. START_EXCEPTION(instruction_tlb_miss_htw)
  897. TLB_MISS_PROLOG
  898. /* If we take a recursive fault, the second level handler may need
  899. * to know whether we are handling a data or instruction fault in
  900. * order to get to the right store fault handler. We provide that
  901. * info by keeping a crazy value for ESR in r14
  902. */
  903. li r14,-1 /* store to exception frame is done later */
  904. /* Now we handle the fault proper. We only save DEAR in the non
  905. * linear mapping case since we know the linear mapping case will
  906. * not re-enter. We could indeed optimize and also not save SRR0/1
  907. * in the linear mapping case but I'll leave that for later
  908. *
  909. * Faulting address is SRR0 which is already in r16
  910. */
  911. srdi r11,r16,60 /* get region */
  912. cmpldi cr0,r11,0xc /* linear mapping ? */
  913. TLB_MISS_STATS_SAVE_INFO
  914. beq tlb_load_linear /* yes -> go to linear map load */
  915. /* We do the user/kernel test for the PID here along with the RW test
  916. */
  917. cmpldi cr0,r11,0 /* Check for user region */
  918. ld r15,PACAPGD(r13) /* Load user pgdir */
  919. beq htw_tlb_miss
  920. /* XXX replace the RMW cycles with immediate loads + writes */
  921. 1: mfspr r10,SPRN_MAS1
  922. cmpldi cr0,r11,8 /* Check for vmalloc region */
  923. rlwinm r10,r10,0,16,1 /* Clear TID */
  924. mtspr SPRN_MAS1,r10
  925. ld r15,PACA_KERNELPGD(r13) /* Load kernel pgdir */
  926. beq+ htw_tlb_miss
  927. /* We got a crappy address, just fault */
  928. TLB_MISS_STATS_I(MMSTAT_TLB_MISS_NORM_FAULT)
  929. TLB_MISS_EPILOG_ERROR
  930. b exc_instruction_storage_book3e
  931. /*
  932. * This is the guts of the second-level TLB miss handler for direct
  933. * misses. We are entered with:
  934. *
  935. * r16 = virtual page table faulting address
  936. * r15 = PGD pointer
  937. * r14 = ESR
  938. * r13 = PACA
  939. * r12 = TLB exception frame in PACA
  940. * r11 = crap (free to use)
  941. * r10 = crap (free to use)
  942. *
  943. * It can be re-entered by the linear mapping miss handler. However, to
  944. * avoid too much complication, it will save/restore things for us
  945. */
  946. htw_tlb_miss:
  947. /* Search if we already have a TLB entry for that virtual address, and
  948. * if we do, bail out.
  949. *
  950. * MAS1:IND should be already set based on MAS4
  951. */
  952. PPC_TLBSRX_DOT(0,R16)
  953. beq htw_tlb_miss_done
  954. /* Now, we need to walk the page tables. First check if we are in
  955. * range.
  956. */
  957. rldicl. r10,r16,64-PGTABLE_EADDR_SIZE,PGTABLE_EADDR_SIZE+4
  958. bne- htw_tlb_miss_fault
  959. /* Get the PGD pointer */
  960. cmpldi cr0,r15,0
  961. beq- htw_tlb_miss_fault
  962. /* Get to PGD entry */
  963. rldicl r11,r16,64-(PGDIR_SHIFT-3),64-PGD_INDEX_SIZE-3
  964. clrrdi r10,r11,3
  965. ldx r15,r10,r15
  966. cmpdi cr0,r15,0
  967. bge htw_tlb_miss_fault
  968. #ifndef CONFIG_PPC_64K_PAGES
  969. /* Get to PUD entry */
  970. rldicl r11,r16,64-(PUD_SHIFT-3),64-PUD_INDEX_SIZE-3
  971. clrrdi r10,r11,3
  972. ldx r15,r10,r15
  973. cmpdi cr0,r15,0
  974. bge htw_tlb_miss_fault
  975. #endif /* CONFIG_PPC_64K_PAGES */
  976. /* Get to PMD entry */
  977. rldicl r11,r16,64-(PMD_SHIFT-3),64-PMD_INDEX_SIZE-3
  978. clrrdi r10,r11,3
  979. ldx r15,r10,r15
  980. cmpdi cr0,r15,0
  981. bge htw_tlb_miss_fault
  982. /* Ok, we're all right, we can now create an indirect entry for
  983. * a 1M or 256M page.
  984. *
  985. * The last trick is now that because we use "half" pages for
  986. * the HTW (1M IND is 2K and 256M IND is 32K) we need to account
  987. * for an added LSB bit to the RPN. For 64K pages, there is no
  988. * problem as we already use 32K arrays (half PTE pages), but for
  989. * 4K page we need to extract a bit from the virtual address and
  990. * insert it into the "PA52" bit of the RPN.
  991. */
  992. #ifndef CONFIG_PPC_64K_PAGES
  993. rlwimi r15,r16,32-9,20,20
  994. #endif
  995. /* Now we build the MAS:
  996. *
  997. * MAS 0 : Fully setup with defaults in MAS4 and TLBnCFG
  998. * MAS 1 : Almost fully setup
  999. * - PID already updated by caller if necessary
  1000. * - TSIZE for now is base ind page size always
  1001. * MAS 2 : Use defaults
  1002. * MAS 3+7 : Needs to be done
  1003. */
  1004. #ifdef CONFIG_PPC_64K_PAGES
  1005. ori r10,r15,(BOOK3E_PAGESZ_64K << MAS3_SPSIZE_SHIFT)
  1006. #else
  1007. ori r10,r15,(BOOK3E_PAGESZ_4K << MAS3_SPSIZE_SHIFT)
  1008. #endif
  1009. BEGIN_MMU_FTR_SECTION
  1010. srdi r16,r10,32
  1011. mtspr SPRN_MAS3,r10
  1012. mtspr SPRN_MAS7,r16
  1013. MMU_FTR_SECTION_ELSE
  1014. mtspr SPRN_MAS7_MAS3,r10
  1015. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
  1016. tlbwe
  1017. htw_tlb_miss_done:
  1018. /* We don't bother with restoring DEAR or ESR since we know we are
  1019. * level 0 and just going back to userland. They are only needed
  1020. * if you are going to take an access fault
  1021. */
  1022. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_PT_OK)
  1023. TLB_MISS_EPILOG_SUCCESS
  1024. rfi
  1025. htw_tlb_miss_fault:
  1026. /* We need to check if it was an instruction miss. We know this
  1027. * though because r14 would contain -1
  1028. */
  1029. cmpdi cr0,r14,-1
  1030. beq 1f
  1031. mtspr SPRN_DEAR,r16
  1032. mtspr SPRN_ESR,r14
  1033. TLB_MISS_STATS_D(MMSTAT_TLB_MISS_PT_FAULT)
  1034. TLB_MISS_EPILOG_ERROR
  1035. b exc_data_storage_book3e
  1036. 1: TLB_MISS_STATS_I(MMSTAT_TLB_MISS_PT_FAULT)
  1037. TLB_MISS_EPILOG_ERROR
  1038. b exc_instruction_storage_book3e
  1039. /*
  1040. * This is the guts of "any" level TLB miss handler for kernel linear
  1041. * mapping misses. We are entered with:
  1042. *
  1043. *
  1044. * r16 = faulting address
  1045. * r15 = crap (free to use)
  1046. * r14 = ESR (data) or -1 (instruction)
  1047. * r13 = PACA
  1048. * r12 = TLB exception frame in PACA
  1049. * r11 = crap (free to use)
  1050. * r10 = crap (free to use)
  1051. *
  1052. * In addition we know that we will not re-enter, so in theory, we could
  1053. * use a simpler epilog not restoring SRR0/1 etc.. but we'll do that later.
  1054. *
  1055. * We also need to be careful about MAS registers here & TLB reservation,
  1056. * as we know we'll have clobbered them if we interrupt the main TLB miss
  1057. * handlers in which case we probably want to do a full restart at level
  1058. * 0 rather than saving / restoring the MAS.
  1059. *
  1060. * Note: If we care about performance of that core, we can easily shuffle
  1061. * a few things around
  1062. */
  1063. tlb_load_linear:
  1064. /* For now, we assume the linear mapping is contiguous and stops at
  1065. * linear_map_top. We also assume the size is a multiple of 1G, thus
  1066. * we only use 1G pages for now. That might have to be changed in a
  1067. * final implementation, especially when dealing with hypervisors
  1068. */
  1069. ld r11,PACATOC(r13)
  1070. ld r11,linear_map_top@got(r11)
  1071. ld r10,0(r11)
  1072. tovirt(10,10)
  1073. cmpld cr0,r16,r10
  1074. bge tlb_load_linear_fault
  1075. /* MAS1 need whole new setup. */
  1076. li r15,(BOOK3E_PAGESZ_1GB<<MAS1_TSIZE_SHIFT)
  1077. oris r15,r15,MAS1_VALID@h /* MAS1 needs V and TSIZE */
  1078. mtspr SPRN_MAS1,r15
  1079. /* Already somebody there ? */
  1080. PPC_TLBSRX_DOT(0,R16)
  1081. beq tlb_load_linear_done
  1082. /* Now we build the remaining MAS. MAS0 and 2 should be fine
  1083. * with their defaults, which leaves us with MAS 3 and 7. The
  1084. * mapping is linear, so we just take the address, clear the
  1085. * region bits, and or in the permission bits which are currently
  1086. * hard wired
  1087. */
  1088. clrrdi r10,r16,30 /* 1G page index */
  1089. clrldi r10,r10,4 /* clear region bits */
  1090. ori r10,r10,MAS3_SR|MAS3_SW|MAS3_SX
  1091. BEGIN_MMU_FTR_SECTION
  1092. srdi r16,r10,32
  1093. mtspr SPRN_MAS3,r10
  1094. mtspr SPRN_MAS7,r16
  1095. MMU_FTR_SECTION_ELSE
  1096. mtspr SPRN_MAS7_MAS3,r10
  1097. ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_USE_PAIRED_MAS)
  1098. tlbwe
  1099. tlb_load_linear_done:
  1100. /* We use the "error" epilog for success as we do want to
  1101. * restore to the initial faulting context, whatever it was.
  1102. * We do that because we can't resume a fault within a TLB
  1103. * miss handler, due to MAS and TLB reservation being clobbered.
  1104. */
  1105. TLB_MISS_STATS_X(MMSTAT_TLB_MISS_LINEAR)
  1106. TLB_MISS_EPILOG_ERROR
  1107. rfi
  1108. tlb_load_linear_fault:
  1109. /* We keep the DEAR and ESR around, this shouldn't have happened */
  1110. cmpdi cr0,r14,-1
  1111. beq 1f
  1112. TLB_MISS_EPILOG_ERROR_SPECIAL
  1113. b exc_data_storage_book3e
  1114. 1: TLB_MISS_EPILOG_ERROR_SPECIAL
  1115. b exc_instruction_storage_book3e
  1116. #ifdef CONFIG_BOOK3E_MMU_TLB_STATS
  1117. .tlb_stat_inc:
  1118. 1: ldarx r8,0,r9
  1119. addi r8,r8,1
  1120. stdcx. r8,0,r9
  1121. bne- 1b
  1122. blr
  1123. #endif