tlb_low_64e.S 37 KB

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