book3s_hv_rmhandlers.S 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  12. *
  13. * Derived from book3s_rmhandlers.S and other files, which are:
  14. *
  15. * Copyright SUSE Linux Products GmbH 2009
  16. *
  17. * Authors: Alexander Graf <agraf@suse.de>
  18. */
  19. #include <asm/ppc_asm.h>
  20. #include <asm/kvm_asm.h>
  21. #include <asm/reg.h>
  22. #include <asm/mmu.h>
  23. #include <asm/page.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/hvcall.h>
  26. #include <asm/asm-offsets.h>
  27. #include <asm/exception-64s.h>
  28. #include <asm/kvm_book3s_asm.h>
  29. #include <asm/mmu-hash64.h>
  30. #include <asm/tm.h>
  31. #define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
  32. /* Values in HSTATE_NAPPING(r13) */
  33. #define NAPPING_CEDE 1
  34. #define NAPPING_NOVCPU 2
  35. /*
  36. * Call kvmppc_hv_entry in real mode.
  37. * Must be called with interrupts hard-disabled.
  38. *
  39. * Input Registers:
  40. *
  41. * LR = return address to continue at after eventually re-enabling MMU
  42. */
  43. _GLOBAL_TOC(kvmppc_hv_entry_trampoline)
  44. mflr r0
  45. std r0, PPC_LR_STKOFF(r1)
  46. stdu r1, -112(r1)
  47. mfmsr r10
  48. LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
  49. li r0,MSR_RI
  50. andc r0,r10,r0
  51. li r6,MSR_IR | MSR_DR
  52. andc r6,r10,r6
  53. mtmsrd r0,1 /* clear RI in MSR */
  54. mtsrr0 r5
  55. mtsrr1 r6
  56. RFI
  57. kvmppc_call_hv_entry:
  58. ld r4, HSTATE_KVM_VCPU(r13)
  59. bl kvmppc_hv_entry
  60. /* Back from guest - restore host state and return to caller */
  61. BEGIN_FTR_SECTION
  62. /* Restore host DABR and DABRX */
  63. ld r5,HSTATE_DABR(r13)
  64. li r6,7
  65. mtspr SPRN_DABR,r5
  66. mtspr SPRN_DABRX,r6
  67. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
  68. /* Restore SPRG3 */
  69. ld r3,PACA_SPRG_VDSO(r13)
  70. mtspr SPRN_SPRG_VDSO_WRITE,r3
  71. /* Reload the host's PMU registers */
  72. ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
  73. lbz r4, LPPACA_PMCINUSE(r3)
  74. cmpwi r4, 0
  75. beq 23f /* skip if not */
  76. BEGIN_FTR_SECTION
  77. ld r3, HSTATE_MMCR0(r13)
  78. andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
  79. cmpwi r4, MMCR0_PMAO
  80. beql kvmppc_fix_pmao
  81. END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
  82. lwz r3, HSTATE_PMC1(r13)
  83. lwz r4, HSTATE_PMC2(r13)
  84. lwz r5, HSTATE_PMC3(r13)
  85. lwz r6, HSTATE_PMC4(r13)
  86. lwz r8, HSTATE_PMC5(r13)
  87. lwz r9, HSTATE_PMC6(r13)
  88. mtspr SPRN_PMC1, r3
  89. mtspr SPRN_PMC2, r4
  90. mtspr SPRN_PMC3, r5
  91. mtspr SPRN_PMC4, r6
  92. mtspr SPRN_PMC5, r8
  93. mtspr SPRN_PMC6, r9
  94. ld r3, HSTATE_MMCR0(r13)
  95. ld r4, HSTATE_MMCR1(r13)
  96. ld r5, HSTATE_MMCRA(r13)
  97. ld r6, HSTATE_SIAR(r13)
  98. ld r7, HSTATE_SDAR(r13)
  99. mtspr SPRN_MMCR1, r4
  100. mtspr SPRN_MMCRA, r5
  101. mtspr SPRN_SIAR, r6
  102. mtspr SPRN_SDAR, r7
  103. BEGIN_FTR_SECTION
  104. ld r8, HSTATE_MMCR2(r13)
  105. ld r9, HSTATE_SIER(r13)
  106. mtspr SPRN_MMCR2, r8
  107. mtspr SPRN_SIER, r9
  108. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  109. mtspr SPRN_MMCR0, r3
  110. isync
  111. 23:
  112. /*
  113. * Reload DEC. HDEC interrupts were disabled when
  114. * we reloaded the host's LPCR value.
  115. */
  116. ld r3, HSTATE_DECEXP(r13)
  117. mftb r4
  118. subf r4, r4, r3
  119. mtspr SPRN_DEC, r4
  120. /* hwthread_req may have got set by cede or no vcpu, so clear it */
  121. li r0, 0
  122. stb r0, HSTATE_HWTHREAD_REQ(r13)
  123. /*
  124. * For external and machine check interrupts, we need
  125. * to call the Linux handler to process the interrupt.
  126. * We do that by jumping to absolute address 0x500 for
  127. * external interrupts, or the machine_check_fwnmi label
  128. * for machine checks (since firmware might have patched
  129. * the vector area at 0x200). The [h]rfid at the end of the
  130. * handler will return to the book3s_hv_interrupts.S code.
  131. * For other interrupts we do the rfid to get back
  132. * to the book3s_hv_interrupts.S code here.
  133. */
  134. ld r8, 112+PPC_LR_STKOFF(r1)
  135. addi r1, r1, 112
  136. ld r7, HSTATE_HOST_MSR(r13)
  137. cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
  138. cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
  139. beq 11f
  140. cmpwi cr2, r12, BOOK3S_INTERRUPT_HMI
  141. beq cr2, 14f /* HMI check */
  142. /* RFI into the highmem handler, or branch to interrupt handler */
  143. mfmsr r6
  144. li r0, MSR_RI
  145. andc r6, r6, r0
  146. mtmsrd r6, 1 /* Clear RI in MSR */
  147. mtsrr0 r8
  148. mtsrr1 r7
  149. beq cr1, 13f /* machine check */
  150. RFI
  151. /* On POWER7, we have external interrupts set to use HSRR0/1 */
  152. 11: mtspr SPRN_HSRR0, r8
  153. mtspr SPRN_HSRR1, r7
  154. ba 0x500
  155. 13: b machine_check_fwnmi
  156. 14: mtspr SPRN_HSRR0, r8
  157. mtspr SPRN_HSRR1, r7
  158. b hmi_exception_after_realmode
  159. kvmppc_primary_no_guest:
  160. /* We handle this much like a ceded vcpu */
  161. /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
  162. mfspr r3, SPRN_HDEC
  163. mtspr SPRN_DEC, r3
  164. /*
  165. * Make sure the primary has finished the MMU switch.
  166. * We should never get here on a secondary thread, but
  167. * check it for robustness' sake.
  168. */
  169. ld r5, HSTATE_KVM_VCORE(r13)
  170. 65: lbz r0, VCORE_IN_GUEST(r5)
  171. cmpwi r0, 0
  172. beq 65b
  173. /* Set LPCR. */
  174. ld r8,VCORE_LPCR(r5)
  175. mtspr SPRN_LPCR,r8
  176. isync
  177. /* set our bit in napping_threads */
  178. ld r5, HSTATE_KVM_VCORE(r13)
  179. lbz r7, HSTATE_PTID(r13)
  180. li r0, 1
  181. sld r0, r0, r7
  182. addi r6, r5, VCORE_NAPPING_THREADS
  183. 1: lwarx r3, 0, r6
  184. or r3, r3, r0
  185. stwcx. r3, 0, r6
  186. bne 1b
  187. /* order napping_threads update vs testing entry_exit_map */
  188. isync
  189. li r12, 0
  190. lwz r7, VCORE_ENTRY_EXIT(r5)
  191. cmpwi r7, 0x100
  192. bge kvm_novcpu_exit /* another thread already exiting */
  193. li r3, NAPPING_NOVCPU
  194. stb r3, HSTATE_NAPPING(r13)
  195. li r3, 0 /* Don't wake on privileged (OS) doorbell */
  196. b kvm_do_nap
  197. kvm_novcpu_wakeup:
  198. ld r1, HSTATE_HOST_R1(r13)
  199. ld r5, HSTATE_KVM_VCORE(r13)
  200. li r0, 0
  201. stb r0, HSTATE_NAPPING(r13)
  202. /* check the wake reason */
  203. bl kvmppc_check_wake_reason
  204. /* see if any other thread is already exiting */
  205. lwz r0, VCORE_ENTRY_EXIT(r5)
  206. cmpwi r0, 0x100
  207. bge kvm_novcpu_exit
  208. /* clear our bit in napping_threads */
  209. lbz r7, HSTATE_PTID(r13)
  210. li r0, 1
  211. sld r0, r0, r7
  212. addi r6, r5, VCORE_NAPPING_THREADS
  213. 4: lwarx r7, 0, r6
  214. andc r7, r7, r0
  215. stwcx. r7, 0, r6
  216. bne 4b
  217. /* See if the wake reason means we need to exit */
  218. cmpdi r3, 0
  219. bge kvm_novcpu_exit
  220. /* See if our timeslice has expired (HDEC is negative) */
  221. mfspr r0, SPRN_HDEC
  222. li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
  223. cmpwi r0, 0
  224. blt kvm_novcpu_exit
  225. /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
  226. ld r4, HSTATE_KVM_VCPU(r13)
  227. cmpdi r4, 0
  228. beq kvmppc_primary_no_guest
  229. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  230. addi r3, r4, VCPU_TB_RMENTRY
  231. bl kvmhv_start_timing
  232. #endif
  233. b kvmppc_got_guest
  234. kvm_novcpu_exit:
  235. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  236. ld r4, HSTATE_KVM_VCPU(r13)
  237. cmpdi r4, 0
  238. beq 13f
  239. addi r3, r4, VCPU_TB_RMEXIT
  240. bl kvmhv_accumulate_time
  241. #endif
  242. 13: mr r3, r12
  243. stw r12, 112-4(r1)
  244. bl kvmhv_commence_exit
  245. nop
  246. lwz r12, 112-4(r1)
  247. b kvmhv_switch_to_host
  248. /*
  249. * We come in here when wakened from nap mode.
  250. * Relocation is off and most register values are lost.
  251. * r13 points to the PACA.
  252. */
  253. .globl kvm_start_guest
  254. kvm_start_guest:
  255. /* Set runlatch bit the minute you wake up from nap */
  256. mfspr r0, SPRN_CTRLF
  257. ori r0, r0, 1
  258. mtspr SPRN_CTRLT, r0
  259. ld r2,PACATOC(r13)
  260. li r0,KVM_HWTHREAD_IN_KVM
  261. stb r0,HSTATE_HWTHREAD_STATE(r13)
  262. /* NV GPR values from power7_idle() will no longer be valid */
  263. li r0,1
  264. stb r0,PACA_NAPSTATELOST(r13)
  265. /* were we napping due to cede? */
  266. lbz r0,HSTATE_NAPPING(r13)
  267. cmpwi r0,NAPPING_CEDE
  268. beq kvm_end_cede
  269. cmpwi r0,NAPPING_NOVCPU
  270. beq kvm_novcpu_wakeup
  271. ld r1,PACAEMERGSP(r13)
  272. subi r1,r1,STACK_FRAME_OVERHEAD
  273. /*
  274. * We weren't napping due to cede, so this must be a secondary
  275. * thread being woken up to run a guest, or being woken up due
  276. * to a stray IPI. (Or due to some machine check or hypervisor
  277. * maintenance interrupt while the core is in KVM.)
  278. */
  279. /* Check the wake reason in SRR1 to see why we got here */
  280. bl kvmppc_check_wake_reason
  281. cmpdi r3, 0
  282. bge kvm_no_guest
  283. /* get vcore pointer, NULL if we have nothing to run */
  284. ld r5,HSTATE_KVM_VCORE(r13)
  285. cmpdi r5,0
  286. /* if we have no vcore to run, go back to sleep */
  287. beq kvm_no_guest
  288. kvm_secondary_got_guest:
  289. /* Set HSTATE_DSCR(r13) to something sensible */
  290. ld r6, PACA_DSCR_DEFAULT(r13)
  291. std r6, HSTATE_DSCR(r13)
  292. /* On thread 0 of a subcore, set HDEC to max */
  293. lbz r4, HSTATE_PTID(r13)
  294. cmpwi r4, 0
  295. bne 63f
  296. lis r6, 0x7fff
  297. ori r6, r6, 0xffff
  298. mtspr SPRN_HDEC, r6
  299. /* and set per-LPAR registers, if doing dynamic micro-threading */
  300. ld r6, HSTATE_SPLIT_MODE(r13)
  301. cmpdi r6, 0
  302. beq 63f
  303. ld r0, KVM_SPLIT_RPR(r6)
  304. mtspr SPRN_RPR, r0
  305. ld r0, KVM_SPLIT_PMMAR(r6)
  306. mtspr SPRN_PMMAR, r0
  307. ld r0, KVM_SPLIT_LDBAR(r6)
  308. mtspr SPRN_LDBAR, r0
  309. isync
  310. 63:
  311. /* Order load of vcpu after load of vcore */
  312. lwsync
  313. ld r4, HSTATE_KVM_VCPU(r13)
  314. bl kvmppc_hv_entry
  315. /* Back from the guest, go back to nap */
  316. /* Clear our vcpu and vcore pointers so we don't come back in early */
  317. li r0, 0
  318. std r0, HSTATE_KVM_VCPU(r13)
  319. /*
  320. * Once we clear HSTATE_KVM_VCORE(r13), the code in
  321. * kvmppc_run_core() is going to assume that all our vcpu
  322. * state is visible in memory. This lwsync makes sure
  323. * that that is true.
  324. */
  325. lwsync
  326. std r0, HSTATE_KVM_VCORE(r13)
  327. /*
  328. * At this point we have finished executing in the guest.
  329. * We need to wait for hwthread_req to become zero, since
  330. * we may not turn on the MMU while hwthread_req is non-zero.
  331. * While waiting we also need to check if we get given a vcpu to run.
  332. */
  333. kvm_no_guest:
  334. lbz r3, HSTATE_HWTHREAD_REQ(r13)
  335. cmpwi r3, 0
  336. bne 53f
  337. HMT_MEDIUM
  338. li r0, KVM_HWTHREAD_IN_KERNEL
  339. stb r0, HSTATE_HWTHREAD_STATE(r13)
  340. /* need to recheck hwthread_req after a barrier, to avoid race */
  341. sync
  342. lbz r3, HSTATE_HWTHREAD_REQ(r13)
  343. cmpwi r3, 0
  344. bne 54f
  345. /*
  346. * We jump to power7_wakeup_loss, which will return to the caller
  347. * of power7_nap in the powernv cpu offline loop. The value we
  348. * put in r3 becomes the return value for power7_nap.
  349. */
  350. li r3, LPCR_PECE0
  351. mfspr r4, SPRN_LPCR
  352. rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
  353. mtspr SPRN_LPCR, r4
  354. li r3, 0
  355. b power7_wakeup_loss
  356. 53: HMT_LOW
  357. ld r5, HSTATE_KVM_VCORE(r13)
  358. cmpdi r5, 0
  359. bne 60f
  360. ld r3, HSTATE_SPLIT_MODE(r13)
  361. cmpdi r3, 0
  362. beq kvm_no_guest
  363. lbz r0, KVM_SPLIT_DO_NAP(r3)
  364. cmpwi r0, 0
  365. beq kvm_no_guest
  366. HMT_MEDIUM
  367. b kvm_unsplit_nap
  368. 60: HMT_MEDIUM
  369. b kvm_secondary_got_guest
  370. 54: li r0, KVM_HWTHREAD_IN_KVM
  371. stb r0, HSTATE_HWTHREAD_STATE(r13)
  372. b kvm_no_guest
  373. /*
  374. * Here the primary thread is trying to return the core to
  375. * whole-core mode, so we need to nap.
  376. */
  377. kvm_unsplit_nap:
  378. /*
  379. * Ensure that secondary doesn't nap when it has
  380. * its vcore pointer set.
  381. */
  382. sync /* matches smp_mb() before setting split_info.do_nap */
  383. ld r0, HSTATE_KVM_VCORE(r13)
  384. cmpdi r0, 0
  385. bne kvm_no_guest
  386. /* clear any pending message */
  387. BEGIN_FTR_SECTION
  388. lis r6, (PPC_DBELL_SERVER << (63-36))@h
  389. PPC_MSGCLR(6)
  390. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  391. /* Set kvm_split_mode.napped[tid] = 1 */
  392. ld r3, HSTATE_SPLIT_MODE(r13)
  393. li r0, 1
  394. lhz r4, PACAPACAINDEX(r13)
  395. clrldi r4, r4, 61 /* micro-threading => P8 => 8 threads/core */
  396. addi r4, r4, KVM_SPLIT_NAPPED
  397. stbx r0, r3, r4
  398. /* Check the do_nap flag again after setting napped[] */
  399. sync
  400. lbz r0, KVM_SPLIT_DO_NAP(r3)
  401. cmpwi r0, 0
  402. beq 57f
  403. li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
  404. mfspr r4, SPRN_LPCR
  405. rlwimi r4, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
  406. mtspr SPRN_LPCR, r4
  407. isync
  408. std r0, HSTATE_SCRATCH0(r13)
  409. ptesync
  410. ld r0, HSTATE_SCRATCH0(r13)
  411. 1: cmpd r0, r0
  412. bne 1b
  413. nap
  414. b .
  415. 57: li r0, 0
  416. stbx r0, r3, r4
  417. b kvm_no_guest
  418. /******************************************************************************
  419. * *
  420. * Entry code *
  421. * *
  422. *****************************************************************************/
  423. .global kvmppc_hv_entry
  424. kvmppc_hv_entry:
  425. /* Required state:
  426. *
  427. * R4 = vcpu pointer (or NULL)
  428. * MSR = ~IR|DR
  429. * R13 = PACA
  430. * R1 = host R1
  431. * R2 = TOC
  432. * all other volatile GPRS = free
  433. */
  434. mflr r0
  435. std r0, PPC_LR_STKOFF(r1)
  436. stdu r1, -112(r1)
  437. /* Save R1 in the PACA */
  438. std r1, HSTATE_HOST_R1(r13)
  439. li r6, KVM_GUEST_MODE_HOST_HV
  440. stb r6, HSTATE_IN_GUEST(r13)
  441. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  442. /* Store initial timestamp */
  443. cmpdi r4, 0
  444. beq 1f
  445. addi r3, r4, VCPU_TB_RMENTRY
  446. bl kvmhv_start_timing
  447. 1:
  448. #endif
  449. /* Clear out SLB */
  450. li r6,0
  451. slbmte r6,r6
  452. slbia
  453. ptesync
  454. /*
  455. * POWER7/POWER8 host -> guest partition switch code.
  456. * We don't have to lock against concurrent tlbies,
  457. * but we do have to coordinate across hardware threads.
  458. */
  459. /* Set bit in entry map iff exit map is zero. */
  460. ld r5, HSTATE_KVM_VCORE(r13)
  461. li r7, 1
  462. lbz r6, HSTATE_PTID(r13)
  463. sld r7, r7, r6
  464. addi r9, r5, VCORE_ENTRY_EXIT
  465. 21: lwarx r3, 0, r9
  466. cmpwi r3, 0x100 /* any threads starting to exit? */
  467. bge secondary_too_late /* if so we're too late to the party */
  468. or r3, r3, r7
  469. stwcx. r3, 0, r9
  470. bne 21b
  471. /* Primary thread switches to guest partition. */
  472. ld r9,VCORE_KVM(r5) /* pointer to struct kvm */
  473. cmpwi r6,0
  474. bne 10f
  475. ld r6,KVM_SDR1(r9)
  476. lwz r7,KVM_LPID(r9)
  477. li r0,LPID_RSVD /* switch to reserved LPID */
  478. mtspr SPRN_LPID,r0
  479. ptesync
  480. mtspr SPRN_SDR1,r6 /* switch to partition page table */
  481. mtspr SPRN_LPID,r7
  482. isync
  483. /* See if we need to flush the TLB */
  484. lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
  485. clrldi r7,r6,64-6 /* extract bit number (6 bits) */
  486. srdi r6,r6,6 /* doubleword number */
  487. sldi r6,r6,3 /* address offset */
  488. add r6,r6,r9
  489. addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
  490. li r0,1
  491. sld r0,r0,r7
  492. ld r7,0(r6)
  493. and. r7,r7,r0
  494. beq 22f
  495. 23: ldarx r7,0,r6 /* if set, clear the bit */
  496. andc r7,r7,r0
  497. stdcx. r7,0,r6
  498. bne 23b
  499. /* Flush the TLB of any entries for this LPID */
  500. /* use arch 2.07S as a proxy for POWER8 */
  501. BEGIN_FTR_SECTION
  502. li r6,512 /* POWER8 has 512 sets */
  503. FTR_SECTION_ELSE
  504. li r6,128 /* POWER7 has 128 sets */
  505. ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
  506. mtctr r6
  507. li r7,0x800 /* IS field = 0b10 */
  508. ptesync
  509. 28: tlbiel r7
  510. addi r7,r7,0x1000
  511. bdnz 28b
  512. ptesync
  513. /* Add timebase offset onto timebase */
  514. 22: ld r8,VCORE_TB_OFFSET(r5)
  515. cmpdi r8,0
  516. beq 37f
  517. mftb r6 /* current host timebase */
  518. add r8,r8,r6
  519. mtspr SPRN_TBU40,r8 /* update upper 40 bits */
  520. mftb r7 /* check if lower 24 bits overflowed */
  521. clrldi r6,r6,40
  522. clrldi r7,r7,40
  523. cmpld r7,r6
  524. bge 37f
  525. addis r8,r8,0x100 /* if so, increment upper 40 bits */
  526. mtspr SPRN_TBU40,r8
  527. /* Load guest PCR value to select appropriate compat mode */
  528. 37: ld r7, VCORE_PCR(r5)
  529. cmpdi r7, 0
  530. beq 38f
  531. mtspr SPRN_PCR, r7
  532. 38:
  533. BEGIN_FTR_SECTION
  534. /* DPDES is shared between threads */
  535. ld r8, VCORE_DPDES(r5)
  536. mtspr SPRN_DPDES, r8
  537. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  538. li r0,1
  539. stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
  540. /* Do we have a guest vcpu to run? */
  541. 10: cmpdi r4, 0
  542. beq kvmppc_primary_no_guest
  543. kvmppc_got_guest:
  544. /* Load up guest SLB entries */
  545. lwz r5,VCPU_SLB_MAX(r4)
  546. cmpwi r5,0
  547. beq 9f
  548. mtctr r5
  549. addi r6,r4,VCPU_SLB
  550. 1: ld r8,VCPU_SLB_E(r6)
  551. ld r9,VCPU_SLB_V(r6)
  552. slbmte r9,r8
  553. addi r6,r6,VCPU_SLB_SIZE
  554. bdnz 1b
  555. 9:
  556. /* Increment yield count if they have a VPA */
  557. ld r3, VCPU_VPA(r4)
  558. cmpdi r3, 0
  559. beq 25f
  560. li r6, LPPACA_YIELDCOUNT
  561. LWZX_BE r5, r3, r6
  562. addi r5, r5, 1
  563. STWX_BE r5, r3, r6
  564. li r6, 1
  565. stb r6, VCPU_VPA_DIRTY(r4)
  566. 25:
  567. /* Save purr/spurr */
  568. mfspr r5,SPRN_PURR
  569. mfspr r6,SPRN_SPURR
  570. std r5,HSTATE_PURR(r13)
  571. std r6,HSTATE_SPURR(r13)
  572. ld r7,VCPU_PURR(r4)
  573. ld r8,VCPU_SPURR(r4)
  574. mtspr SPRN_PURR,r7
  575. mtspr SPRN_SPURR,r8
  576. BEGIN_FTR_SECTION
  577. /* Set partition DABR */
  578. /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
  579. lwz r5,VCPU_DABRX(r4)
  580. ld r6,VCPU_DABR(r4)
  581. mtspr SPRN_DABRX,r5
  582. mtspr SPRN_DABR,r6
  583. isync
  584. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
  585. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  586. BEGIN_FTR_SECTION
  587. b skip_tm
  588. END_FTR_SECTION_IFCLR(CPU_FTR_TM)
  589. /* Turn on TM/FP/VSX/VMX so we can restore them. */
  590. mfmsr r5
  591. li r6, MSR_TM >> 32
  592. sldi r6, r6, 32
  593. or r5, r5, r6
  594. ori r5, r5, MSR_FP
  595. oris r5, r5, (MSR_VEC | MSR_VSX)@h
  596. mtmsrd r5
  597. /*
  598. * The user may change these outside of a transaction, so they must
  599. * always be context switched.
  600. */
  601. ld r5, VCPU_TFHAR(r4)
  602. ld r6, VCPU_TFIAR(r4)
  603. ld r7, VCPU_TEXASR(r4)
  604. mtspr SPRN_TFHAR, r5
  605. mtspr SPRN_TFIAR, r6
  606. mtspr SPRN_TEXASR, r7
  607. ld r5, VCPU_MSR(r4)
  608. rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
  609. beq skip_tm /* TM not active in guest */
  610. /* Make sure the failure summary is set, otherwise we'll program check
  611. * when we trechkpt. It's possible that this might have been not set
  612. * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
  613. * host.
  614. */
  615. oris r7, r7, (TEXASR_FS)@h
  616. mtspr SPRN_TEXASR, r7
  617. /*
  618. * We need to load up the checkpointed state for the guest.
  619. * We need to do this early as it will blow away any GPRs, VSRs and
  620. * some SPRs.
  621. */
  622. mr r31, r4
  623. addi r3, r31, VCPU_FPRS_TM
  624. bl load_fp_state
  625. addi r3, r31, VCPU_VRS_TM
  626. bl load_vr_state
  627. mr r4, r31
  628. lwz r7, VCPU_VRSAVE_TM(r4)
  629. mtspr SPRN_VRSAVE, r7
  630. ld r5, VCPU_LR_TM(r4)
  631. lwz r6, VCPU_CR_TM(r4)
  632. ld r7, VCPU_CTR_TM(r4)
  633. ld r8, VCPU_AMR_TM(r4)
  634. ld r9, VCPU_TAR_TM(r4)
  635. mtlr r5
  636. mtcr r6
  637. mtctr r7
  638. mtspr SPRN_AMR, r8
  639. mtspr SPRN_TAR, r9
  640. /*
  641. * Load up PPR and DSCR values but don't put them in the actual SPRs
  642. * till the last moment to avoid running with userspace PPR and DSCR for
  643. * too long.
  644. */
  645. ld r29, VCPU_DSCR_TM(r4)
  646. ld r30, VCPU_PPR_TM(r4)
  647. std r2, PACATMSCRATCH(r13) /* Save TOC */
  648. /* Clear the MSR RI since r1, r13 are all going to be foobar. */
  649. li r5, 0
  650. mtmsrd r5, 1
  651. /* Load GPRs r0-r28 */
  652. reg = 0
  653. .rept 29
  654. ld reg, VCPU_GPRS_TM(reg)(r31)
  655. reg = reg + 1
  656. .endr
  657. mtspr SPRN_DSCR, r29
  658. mtspr SPRN_PPR, r30
  659. /* Load final GPRs */
  660. ld 29, VCPU_GPRS_TM(29)(r31)
  661. ld 30, VCPU_GPRS_TM(30)(r31)
  662. ld 31, VCPU_GPRS_TM(31)(r31)
  663. /* TM checkpointed state is now setup. All GPRs are now volatile. */
  664. TRECHKPT
  665. /* Now let's get back the state we need. */
  666. HMT_MEDIUM
  667. GET_PACA(r13)
  668. ld r29, HSTATE_DSCR(r13)
  669. mtspr SPRN_DSCR, r29
  670. ld r4, HSTATE_KVM_VCPU(r13)
  671. ld r1, HSTATE_HOST_R1(r13)
  672. ld r2, PACATMSCRATCH(r13)
  673. /* Set the MSR RI since we have our registers back. */
  674. li r5, MSR_RI
  675. mtmsrd r5, 1
  676. skip_tm:
  677. #endif
  678. /* Load guest PMU registers */
  679. /* R4 is live here (vcpu pointer) */
  680. li r3, 1
  681. sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
  682. mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
  683. isync
  684. BEGIN_FTR_SECTION
  685. ld r3, VCPU_MMCR(r4)
  686. andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
  687. cmpwi r5, MMCR0_PMAO
  688. beql kvmppc_fix_pmao
  689. END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
  690. lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
  691. lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
  692. lwz r6, VCPU_PMC + 8(r4)
  693. lwz r7, VCPU_PMC + 12(r4)
  694. lwz r8, VCPU_PMC + 16(r4)
  695. lwz r9, VCPU_PMC + 20(r4)
  696. mtspr SPRN_PMC1, r3
  697. mtspr SPRN_PMC2, r5
  698. mtspr SPRN_PMC3, r6
  699. mtspr SPRN_PMC4, r7
  700. mtspr SPRN_PMC5, r8
  701. mtspr SPRN_PMC6, r9
  702. ld r3, VCPU_MMCR(r4)
  703. ld r5, VCPU_MMCR + 8(r4)
  704. ld r6, VCPU_MMCR + 16(r4)
  705. ld r7, VCPU_SIAR(r4)
  706. ld r8, VCPU_SDAR(r4)
  707. mtspr SPRN_MMCR1, r5
  708. mtspr SPRN_MMCRA, r6
  709. mtspr SPRN_SIAR, r7
  710. mtspr SPRN_SDAR, r8
  711. BEGIN_FTR_SECTION
  712. ld r5, VCPU_MMCR + 24(r4)
  713. ld r6, VCPU_SIER(r4)
  714. lwz r7, VCPU_PMC + 24(r4)
  715. lwz r8, VCPU_PMC + 28(r4)
  716. ld r9, VCPU_MMCR + 32(r4)
  717. mtspr SPRN_MMCR2, r5
  718. mtspr SPRN_SIER, r6
  719. mtspr SPRN_SPMC1, r7
  720. mtspr SPRN_SPMC2, r8
  721. mtspr SPRN_MMCRS, r9
  722. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  723. mtspr SPRN_MMCR0, r3
  724. isync
  725. /* Load up FP, VMX and VSX registers */
  726. bl kvmppc_load_fp
  727. ld r14, VCPU_GPR(R14)(r4)
  728. ld r15, VCPU_GPR(R15)(r4)
  729. ld r16, VCPU_GPR(R16)(r4)
  730. ld r17, VCPU_GPR(R17)(r4)
  731. ld r18, VCPU_GPR(R18)(r4)
  732. ld r19, VCPU_GPR(R19)(r4)
  733. ld r20, VCPU_GPR(R20)(r4)
  734. ld r21, VCPU_GPR(R21)(r4)
  735. ld r22, VCPU_GPR(R22)(r4)
  736. ld r23, VCPU_GPR(R23)(r4)
  737. ld r24, VCPU_GPR(R24)(r4)
  738. ld r25, VCPU_GPR(R25)(r4)
  739. ld r26, VCPU_GPR(R26)(r4)
  740. ld r27, VCPU_GPR(R27)(r4)
  741. ld r28, VCPU_GPR(R28)(r4)
  742. ld r29, VCPU_GPR(R29)(r4)
  743. ld r30, VCPU_GPR(R30)(r4)
  744. ld r31, VCPU_GPR(R31)(r4)
  745. /* Switch DSCR to guest value */
  746. ld r5, VCPU_DSCR(r4)
  747. mtspr SPRN_DSCR, r5
  748. BEGIN_FTR_SECTION
  749. /* Skip next section on POWER7 */
  750. b 8f
  751. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
  752. /* Turn on TM so we can access TFHAR/TFIAR/TEXASR */
  753. mfmsr r8
  754. li r0, 1
  755. rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
  756. mtmsrd r8
  757. /* Load up POWER8-specific registers */
  758. ld r5, VCPU_IAMR(r4)
  759. lwz r6, VCPU_PSPB(r4)
  760. ld r7, VCPU_FSCR(r4)
  761. mtspr SPRN_IAMR, r5
  762. mtspr SPRN_PSPB, r6
  763. mtspr SPRN_FSCR, r7
  764. ld r5, VCPU_DAWR(r4)
  765. ld r6, VCPU_DAWRX(r4)
  766. ld r7, VCPU_CIABR(r4)
  767. ld r8, VCPU_TAR(r4)
  768. mtspr SPRN_DAWR, r5
  769. mtspr SPRN_DAWRX, r6
  770. mtspr SPRN_CIABR, r7
  771. mtspr SPRN_TAR, r8
  772. ld r5, VCPU_IC(r4)
  773. ld r6, VCPU_VTB(r4)
  774. mtspr SPRN_IC, r5
  775. mtspr SPRN_VTB, r6
  776. ld r8, VCPU_EBBHR(r4)
  777. mtspr SPRN_EBBHR, r8
  778. ld r5, VCPU_EBBRR(r4)
  779. ld r6, VCPU_BESCR(r4)
  780. ld r7, VCPU_CSIGR(r4)
  781. ld r8, VCPU_TACR(r4)
  782. mtspr SPRN_EBBRR, r5
  783. mtspr SPRN_BESCR, r6
  784. mtspr SPRN_CSIGR, r7
  785. mtspr SPRN_TACR, r8
  786. ld r5, VCPU_TCSCR(r4)
  787. ld r6, VCPU_ACOP(r4)
  788. lwz r7, VCPU_GUEST_PID(r4)
  789. ld r8, VCPU_WORT(r4)
  790. mtspr SPRN_TCSCR, r5
  791. mtspr SPRN_ACOP, r6
  792. mtspr SPRN_PID, r7
  793. mtspr SPRN_WORT, r8
  794. 8:
  795. /*
  796. * Set the decrementer to the guest decrementer.
  797. */
  798. ld r8,VCPU_DEC_EXPIRES(r4)
  799. /* r8 is a host timebase value here, convert to guest TB */
  800. ld r5,HSTATE_KVM_VCORE(r13)
  801. ld r6,VCORE_TB_OFFSET(r5)
  802. add r8,r8,r6
  803. mftb r7
  804. subf r3,r7,r8
  805. mtspr SPRN_DEC,r3
  806. stw r3,VCPU_DEC(r4)
  807. ld r5, VCPU_SPRG0(r4)
  808. ld r6, VCPU_SPRG1(r4)
  809. ld r7, VCPU_SPRG2(r4)
  810. ld r8, VCPU_SPRG3(r4)
  811. mtspr SPRN_SPRG0, r5
  812. mtspr SPRN_SPRG1, r6
  813. mtspr SPRN_SPRG2, r7
  814. mtspr SPRN_SPRG3, r8
  815. /* Load up DAR and DSISR */
  816. ld r5, VCPU_DAR(r4)
  817. lwz r6, VCPU_DSISR(r4)
  818. mtspr SPRN_DAR, r5
  819. mtspr SPRN_DSISR, r6
  820. /* Restore AMR and UAMOR, set AMOR to all 1s */
  821. ld r5,VCPU_AMR(r4)
  822. ld r6,VCPU_UAMOR(r4)
  823. li r7,-1
  824. mtspr SPRN_AMR,r5
  825. mtspr SPRN_UAMOR,r6
  826. mtspr SPRN_AMOR,r7
  827. /* Restore state of CTRL run bit; assume 1 on entry */
  828. lwz r5,VCPU_CTRL(r4)
  829. andi. r5,r5,1
  830. bne 4f
  831. mfspr r6,SPRN_CTRLF
  832. clrrdi r6,r6,1
  833. mtspr SPRN_CTRLT,r6
  834. 4:
  835. /* Secondary threads wait for primary to have done partition switch */
  836. ld r5, HSTATE_KVM_VCORE(r13)
  837. lbz r6, HSTATE_PTID(r13)
  838. cmpwi r6, 0
  839. beq 21f
  840. lbz r0, VCORE_IN_GUEST(r5)
  841. cmpwi r0, 0
  842. bne 21f
  843. HMT_LOW
  844. 20: lwz r3, VCORE_ENTRY_EXIT(r5)
  845. cmpwi r3, 0x100
  846. bge no_switch_exit
  847. lbz r0, VCORE_IN_GUEST(r5)
  848. cmpwi r0, 0
  849. beq 20b
  850. HMT_MEDIUM
  851. 21:
  852. /* Set LPCR. */
  853. ld r8,VCORE_LPCR(r5)
  854. mtspr SPRN_LPCR,r8
  855. isync
  856. /* Check if HDEC expires soon */
  857. mfspr r3, SPRN_HDEC
  858. cmpwi r3, 512 /* 1 microsecond */
  859. blt hdec_soon
  860. ld r6, VCPU_CTR(r4)
  861. ld r7, VCPU_XER(r4)
  862. mtctr r6
  863. mtxer r7
  864. kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
  865. ld r10, VCPU_PC(r4)
  866. ld r11, VCPU_MSR(r4)
  867. ld r6, VCPU_SRR0(r4)
  868. ld r7, VCPU_SRR1(r4)
  869. mtspr SPRN_SRR0, r6
  870. mtspr SPRN_SRR1, r7
  871. deliver_guest_interrupt:
  872. /* r11 = vcpu->arch.msr & ~MSR_HV */
  873. rldicl r11, r11, 63 - MSR_HV_LG, 1
  874. rotldi r11, r11, 1 + MSR_HV_LG
  875. ori r11, r11, MSR_ME
  876. /* Check if we can deliver an external or decrementer interrupt now */
  877. ld r0, VCPU_PENDING_EXC(r4)
  878. rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
  879. cmpdi cr1, r0, 0
  880. andi. r8, r11, MSR_EE
  881. mfspr r8, SPRN_LPCR
  882. /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
  883. rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
  884. mtspr SPRN_LPCR, r8
  885. isync
  886. beq 5f
  887. li r0, BOOK3S_INTERRUPT_EXTERNAL
  888. bne cr1, 12f
  889. mfspr r0, SPRN_DEC
  890. cmpwi r0, 0
  891. li r0, BOOK3S_INTERRUPT_DECREMENTER
  892. bge 5f
  893. 12: mtspr SPRN_SRR0, r10
  894. mr r10,r0
  895. mtspr SPRN_SRR1, r11
  896. mr r9, r4
  897. bl kvmppc_msr_interrupt
  898. 5:
  899. /*
  900. * Required state:
  901. * R4 = vcpu
  902. * R10: value for HSRR0
  903. * R11: value for HSRR1
  904. * R13 = PACA
  905. */
  906. fast_guest_return:
  907. li r0,0
  908. stb r0,VCPU_CEDED(r4) /* cancel cede */
  909. mtspr SPRN_HSRR0,r10
  910. mtspr SPRN_HSRR1,r11
  911. /* Activate guest mode, so faults get handled by KVM */
  912. li r9, KVM_GUEST_MODE_GUEST_HV
  913. stb r9, HSTATE_IN_GUEST(r13)
  914. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  915. /* Accumulate timing */
  916. addi r3, r4, VCPU_TB_GUEST
  917. bl kvmhv_accumulate_time
  918. #endif
  919. /* Enter guest */
  920. BEGIN_FTR_SECTION
  921. ld r5, VCPU_CFAR(r4)
  922. mtspr SPRN_CFAR, r5
  923. END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
  924. BEGIN_FTR_SECTION
  925. ld r0, VCPU_PPR(r4)
  926. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  927. ld r5, VCPU_LR(r4)
  928. lwz r6, VCPU_CR(r4)
  929. mtlr r5
  930. mtcr r6
  931. ld r1, VCPU_GPR(R1)(r4)
  932. ld r2, VCPU_GPR(R2)(r4)
  933. ld r3, VCPU_GPR(R3)(r4)
  934. ld r5, VCPU_GPR(R5)(r4)
  935. ld r6, VCPU_GPR(R6)(r4)
  936. ld r7, VCPU_GPR(R7)(r4)
  937. ld r8, VCPU_GPR(R8)(r4)
  938. ld r9, VCPU_GPR(R9)(r4)
  939. ld r10, VCPU_GPR(R10)(r4)
  940. ld r11, VCPU_GPR(R11)(r4)
  941. ld r12, VCPU_GPR(R12)(r4)
  942. ld r13, VCPU_GPR(R13)(r4)
  943. BEGIN_FTR_SECTION
  944. mtspr SPRN_PPR, r0
  945. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  946. ld r0, VCPU_GPR(R0)(r4)
  947. ld r4, VCPU_GPR(R4)(r4)
  948. hrfid
  949. b .
  950. secondary_too_late:
  951. li r12, 0
  952. cmpdi r4, 0
  953. beq 11f
  954. stw r12, VCPU_TRAP(r4)
  955. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  956. addi r3, r4, VCPU_TB_RMEXIT
  957. bl kvmhv_accumulate_time
  958. #endif
  959. 11: b kvmhv_switch_to_host
  960. no_switch_exit:
  961. HMT_MEDIUM
  962. li r12, 0
  963. b 12f
  964. hdec_soon:
  965. li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
  966. 12: stw r12, VCPU_TRAP(r4)
  967. mr r9, r4
  968. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  969. addi r3, r4, VCPU_TB_RMEXIT
  970. bl kvmhv_accumulate_time
  971. #endif
  972. b guest_exit_cont
  973. /******************************************************************************
  974. * *
  975. * Exit code *
  976. * *
  977. *****************************************************************************/
  978. /*
  979. * We come here from the first-level interrupt handlers.
  980. */
  981. .globl kvmppc_interrupt_hv
  982. kvmppc_interrupt_hv:
  983. /*
  984. * Register contents:
  985. * R12 = interrupt vector
  986. * R13 = PACA
  987. * guest CR, R12 saved in shadow VCPU SCRATCH1/0
  988. * guest R13 saved in SPRN_SCRATCH0
  989. */
  990. std r9, HSTATE_SCRATCH2(r13)
  991. lbz r9, HSTATE_IN_GUEST(r13)
  992. cmpwi r9, KVM_GUEST_MODE_HOST_HV
  993. beq kvmppc_bad_host_intr
  994. #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
  995. cmpwi r9, KVM_GUEST_MODE_GUEST
  996. ld r9, HSTATE_SCRATCH2(r13)
  997. beq kvmppc_interrupt_pr
  998. #endif
  999. /* We're now back in the host but in guest MMU context */
  1000. li r9, KVM_GUEST_MODE_HOST_HV
  1001. stb r9, HSTATE_IN_GUEST(r13)
  1002. ld r9, HSTATE_KVM_VCPU(r13)
  1003. /* Save registers */
  1004. std r0, VCPU_GPR(R0)(r9)
  1005. std r1, VCPU_GPR(R1)(r9)
  1006. std r2, VCPU_GPR(R2)(r9)
  1007. std r3, VCPU_GPR(R3)(r9)
  1008. std r4, VCPU_GPR(R4)(r9)
  1009. std r5, VCPU_GPR(R5)(r9)
  1010. std r6, VCPU_GPR(R6)(r9)
  1011. std r7, VCPU_GPR(R7)(r9)
  1012. std r8, VCPU_GPR(R8)(r9)
  1013. ld r0, HSTATE_SCRATCH2(r13)
  1014. std r0, VCPU_GPR(R9)(r9)
  1015. std r10, VCPU_GPR(R10)(r9)
  1016. std r11, VCPU_GPR(R11)(r9)
  1017. ld r3, HSTATE_SCRATCH0(r13)
  1018. lwz r4, HSTATE_SCRATCH1(r13)
  1019. std r3, VCPU_GPR(R12)(r9)
  1020. stw r4, VCPU_CR(r9)
  1021. BEGIN_FTR_SECTION
  1022. ld r3, HSTATE_CFAR(r13)
  1023. std r3, VCPU_CFAR(r9)
  1024. END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
  1025. BEGIN_FTR_SECTION
  1026. ld r4, HSTATE_PPR(r13)
  1027. std r4, VCPU_PPR(r9)
  1028. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  1029. /* Restore R1/R2 so we can handle faults */
  1030. ld r1, HSTATE_HOST_R1(r13)
  1031. ld r2, PACATOC(r13)
  1032. mfspr r10, SPRN_SRR0
  1033. mfspr r11, SPRN_SRR1
  1034. std r10, VCPU_SRR0(r9)
  1035. std r11, VCPU_SRR1(r9)
  1036. andi. r0, r12, 2 /* need to read HSRR0/1? */
  1037. beq 1f
  1038. mfspr r10, SPRN_HSRR0
  1039. mfspr r11, SPRN_HSRR1
  1040. clrrdi r12, r12, 2
  1041. 1: std r10, VCPU_PC(r9)
  1042. std r11, VCPU_MSR(r9)
  1043. GET_SCRATCH0(r3)
  1044. mflr r4
  1045. std r3, VCPU_GPR(R13)(r9)
  1046. std r4, VCPU_LR(r9)
  1047. stw r12,VCPU_TRAP(r9)
  1048. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1049. addi r3, r9, VCPU_TB_RMINTR
  1050. mr r4, r9
  1051. bl kvmhv_accumulate_time
  1052. ld r5, VCPU_GPR(R5)(r9)
  1053. ld r6, VCPU_GPR(R6)(r9)
  1054. ld r7, VCPU_GPR(R7)(r9)
  1055. ld r8, VCPU_GPR(R8)(r9)
  1056. #endif
  1057. /* Save HEIR (HV emulation assist reg) in emul_inst
  1058. if this is an HEI (HV emulation interrupt, e40) */
  1059. li r3,KVM_INST_FETCH_FAILED
  1060. stw r3,VCPU_LAST_INST(r9)
  1061. cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
  1062. bne 11f
  1063. mfspr r3,SPRN_HEIR
  1064. 11: stw r3,VCPU_HEIR(r9)
  1065. /* these are volatile across C function calls */
  1066. mfctr r3
  1067. mfxer r4
  1068. std r3, VCPU_CTR(r9)
  1069. std r4, VCPU_XER(r9)
  1070. /* If this is a page table miss then see if it's theirs or ours */
  1071. cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
  1072. beq kvmppc_hdsi
  1073. cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
  1074. beq kvmppc_hisi
  1075. /* See if this is a leftover HDEC interrupt */
  1076. cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
  1077. bne 2f
  1078. mfspr r3,SPRN_HDEC
  1079. cmpwi r3,0
  1080. mr r4,r9
  1081. bge fast_guest_return
  1082. 2:
  1083. /* See if this is an hcall we can handle in real mode */
  1084. cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
  1085. beq hcall_try_real_mode
  1086. /* Hypervisor doorbell - exit only if host IPI flag set */
  1087. cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
  1088. bne 3f
  1089. lbz r0, HSTATE_HOST_IPI(r13)
  1090. cmpwi r0, 0
  1091. beq 4f
  1092. b guest_exit_cont
  1093. 3:
  1094. /* External interrupt ? */
  1095. cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
  1096. bne+ guest_exit_cont
  1097. /* External interrupt, first check for host_ipi. If this is
  1098. * set, we know the host wants us out so let's do it now
  1099. */
  1100. bl kvmppc_read_intr
  1101. cmpdi r3, 0
  1102. bgt guest_exit_cont
  1103. /* Check if any CPU is heading out to the host, if so head out too */
  1104. 4: ld r5, HSTATE_KVM_VCORE(r13)
  1105. lwz r0, VCORE_ENTRY_EXIT(r5)
  1106. cmpwi r0, 0x100
  1107. mr r4, r9
  1108. blt deliver_guest_interrupt
  1109. guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
  1110. /* Save more register state */
  1111. mfdar r6
  1112. mfdsisr r7
  1113. std r6, VCPU_DAR(r9)
  1114. stw r7, VCPU_DSISR(r9)
  1115. /* don't overwrite fault_dar/fault_dsisr if HDSI */
  1116. cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
  1117. beq mc_cont
  1118. std r6, VCPU_FAULT_DAR(r9)
  1119. stw r7, VCPU_FAULT_DSISR(r9)
  1120. /* See if it is a machine check */
  1121. cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
  1122. beq machine_check_realmode
  1123. mc_cont:
  1124. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1125. addi r3, r9, VCPU_TB_RMEXIT
  1126. mr r4, r9
  1127. bl kvmhv_accumulate_time
  1128. #endif
  1129. mr r3, r12
  1130. /* Increment exit count, poke other threads to exit */
  1131. bl kvmhv_commence_exit
  1132. nop
  1133. ld r9, HSTATE_KVM_VCPU(r13)
  1134. lwz r12, VCPU_TRAP(r9)
  1135. /* Stop others sending VCPU interrupts to this physical CPU */
  1136. li r0, -1
  1137. stw r0, VCPU_CPU(r9)
  1138. stw r0, VCPU_THREAD_CPU(r9)
  1139. /* Save guest CTRL register, set runlatch to 1 */
  1140. mfspr r6,SPRN_CTRLF
  1141. stw r6,VCPU_CTRL(r9)
  1142. andi. r0,r6,1
  1143. bne 4f
  1144. ori r6,r6,1
  1145. mtspr SPRN_CTRLT,r6
  1146. 4:
  1147. /* Read the guest SLB and save it away */
  1148. lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
  1149. mtctr r0
  1150. li r6,0
  1151. addi r7,r9,VCPU_SLB
  1152. li r5,0
  1153. 1: slbmfee r8,r6
  1154. andis. r0,r8,SLB_ESID_V@h
  1155. beq 2f
  1156. add r8,r8,r6 /* put index in */
  1157. slbmfev r3,r6
  1158. std r8,VCPU_SLB_E(r7)
  1159. std r3,VCPU_SLB_V(r7)
  1160. addi r7,r7,VCPU_SLB_SIZE
  1161. addi r5,r5,1
  1162. 2: addi r6,r6,1
  1163. bdnz 1b
  1164. stw r5,VCPU_SLB_MAX(r9)
  1165. /*
  1166. * Save the guest PURR/SPURR
  1167. */
  1168. mfspr r5,SPRN_PURR
  1169. mfspr r6,SPRN_SPURR
  1170. ld r7,VCPU_PURR(r9)
  1171. ld r8,VCPU_SPURR(r9)
  1172. std r5,VCPU_PURR(r9)
  1173. std r6,VCPU_SPURR(r9)
  1174. subf r5,r7,r5
  1175. subf r6,r8,r6
  1176. /*
  1177. * Restore host PURR/SPURR and add guest times
  1178. * so that the time in the guest gets accounted.
  1179. */
  1180. ld r3,HSTATE_PURR(r13)
  1181. ld r4,HSTATE_SPURR(r13)
  1182. add r3,r3,r5
  1183. add r4,r4,r6
  1184. mtspr SPRN_PURR,r3
  1185. mtspr SPRN_SPURR,r4
  1186. /* Save DEC */
  1187. mfspr r5,SPRN_DEC
  1188. mftb r6
  1189. extsw r5,r5
  1190. add r5,r5,r6
  1191. /* r5 is a guest timebase value here, convert to host TB */
  1192. ld r3,HSTATE_KVM_VCORE(r13)
  1193. ld r4,VCORE_TB_OFFSET(r3)
  1194. subf r5,r4,r5
  1195. std r5,VCPU_DEC_EXPIRES(r9)
  1196. BEGIN_FTR_SECTION
  1197. b 8f
  1198. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
  1199. /* Save POWER8-specific registers */
  1200. mfspr r5, SPRN_IAMR
  1201. mfspr r6, SPRN_PSPB
  1202. mfspr r7, SPRN_FSCR
  1203. std r5, VCPU_IAMR(r9)
  1204. stw r6, VCPU_PSPB(r9)
  1205. std r7, VCPU_FSCR(r9)
  1206. mfspr r5, SPRN_IC
  1207. mfspr r6, SPRN_VTB
  1208. mfspr r7, SPRN_TAR
  1209. std r5, VCPU_IC(r9)
  1210. std r6, VCPU_VTB(r9)
  1211. std r7, VCPU_TAR(r9)
  1212. mfspr r8, SPRN_EBBHR
  1213. std r8, VCPU_EBBHR(r9)
  1214. mfspr r5, SPRN_EBBRR
  1215. mfspr r6, SPRN_BESCR
  1216. mfspr r7, SPRN_CSIGR
  1217. mfspr r8, SPRN_TACR
  1218. std r5, VCPU_EBBRR(r9)
  1219. std r6, VCPU_BESCR(r9)
  1220. std r7, VCPU_CSIGR(r9)
  1221. std r8, VCPU_TACR(r9)
  1222. mfspr r5, SPRN_TCSCR
  1223. mfspr r6, SPRN_ACOP
  1224. mfspr r7, SPRN_PID
  1225. mfspr r8, SPRN_WORT
  1226. std r5, VCPU_TCSCR(r9)
  1227. std r6, VCPU_ACOP(r9)
  1228. stw r7, VCPU_GUEST_PID(r9)
  1229. std r8, VCPU_WORT(r9)
  1230. 8:
  1231. /* Save and reset AMR and UAMOR before turning on the MMU */
  1232. mfspr r5,SPRN_AMR
  1233. mfspr r6,SPRN_UAMOR
  1234. std r5,VCPU_AMR(r9)
  1235. std r6,VCPU_UAMOR(r9)
  1236. li r6,0
  1237. mtspr SPRN_AMR,r6
  1238. /* Switch DSCR back to host value */
  1239. mfspr r8, SPRN_DSCR
  1240. ld r7, HSTATE_DSCR(r13)
  1241. std r8, VCPU_DSCR(r9)
  1242. mtspr SPRN_DSCR, r7
  1243. /* Save non-volatile GPRs */
  1244. std r14, VCPU_GPR(R14)(r9)
  1245. std r15, VCPU_GPR(R15)(r9)
  1246. std r16, VCPU_GPR(R16)(r9)
  1247. std r17, VCPU_GPR(R17)(r9)
  1248. std r18, VCPU_GPR(R18)(r9)
  1249. std r19, VCPU_GPR(R19)(r9)
  1250. std r20, VCPU_GPR(R20)(r9)
  1251. std r21, VCPU_GPR(R21)(r9)
  1252. std r22, VCPU_GPR(R22)(r9)
  1253. std r23, VCPU_GPR(R23)(r9)
  1254. std r24, VCPU_GPR(R24)(r9)
  1255. std r25, VCPU_GPR(R25)(r9)
  1256. std r26, VCPU_GPR(R26)(r9)
  1257. std r27, VCPU_GPR(R27)(r9)
  1258. std r28, VCPU_GPR(R28)(r9)
  1259. std r29, VCPU_GPR(R29)(r9)
  1260. std r30, VCPU_GPR(R30)(r9)
  1261. std r31, VCPU_GPR(R31)(r9)
  1262. /* Save SPRGs */
  1263. mfspr r3, SPRN_SPRG0
  1264. mfspr r4, SPRN_SPRG1
  1265. mfspr r5, SPRN_SPRG2
  1266. mfspr r6, SPRN_SPRG3
  1267. std r3, VCPU_SPRG0(r9)
  1268. std r4, VCPU_SPRG1(r9)
  1269. std r5, VCPU_SPRG2(r9)
  1270. std r6, VCPU_SPRG3(r9)
  1271. /* save FP state */
  1272. mr r3, r9
  1273. bl kvmppc_save_fp
  1274. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1275. BEGIN_FTR_SECTION
  1276. b 2f
  1277. END_FTR_SECTION_IFCLR(CPU_FTR_TM)
  1278. /* Turn on TM. */
  1279. mfmsr r8
  1280. li r0, 1
  1281. rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
  1282. mtmsrd r8
  1283. ld r5, VCPU_MSR(r9)
  1284. rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
  1285. beq 1f /* TM not active in guest. */
  1286. li r3, TM_CAUSE_KVM_RESCHED
  1287. /* Clear the MSR RI since r1, r13 are all going to be foobar. */
  1288. li r5, 0
  1289. mtmsrd r5, 1
  1290. /* All GPRs are volatile at this point. */
  1291. TRECLAIM(R3)
  1292. /* Temporarily store r13 and r9 so we have some regs to play with */
  1293. SET_SCRATCH0(r13)
  1294. GET_PACA(r13)
  1295. std r9, PACATMSCRATCH(r13)
  1296. ld r9, HSTATE_KVM_VCPU(r13)
  1297. /* Get a few more GPRs free. */
  1298. std r29, VCPU_GPRS_TM(29)(r9)
  1299. std r30, VCPU_GPRS_TM(30)(r9)
  1300. std r31, VCPU_GPRS_TM(31)(r9)
  1301. /* Save away PPR and DSCR soon so don't run with user values. */
  1302. mfspr r31, SPRN_PPR
  1303. HMT_MEDIUM
  1304. mfspr r30, SPRN_DSCR
  1305. ld r29, HSTATE_DSCR(r13)
  1306. mtspr SPRN_DSCR, r29
  1307. /* Save all but r9, r13 & r29-r31 */
  1308. reg = 0
  1309. .rept 29
  1310. .if (reg != 9) && (reg != 13)
  1311. std reg, VCPU_GPRS_TM(reg)(r9)
  1312. .endif
  1313. reg = reg + 1
  1314. .endr
  1315. /* ... now save r13 */
  1316. GET_SCRATCH0(r4)
  1317. std r4, VCPU_GPRS_TM(13)(r9)
  1318. /* ... and save r9 */
  1319. ld r4, PACATMSCRATCH(r13)
  1320. std r4, VCPU_GPRS_TM(9)(r9)
  1321. /* Reload stack pointer and TOC. */
  1322. ld r1, HSTATE_HOST_R1(r13)
  1323. ld r2, PACATOC(r13)
  1324. /* Set MSR RI now we have r1 and r13 back. */
  1325. li r5, MSR_RI
  1326. mtmsrd r5, 1
  1327. /* Save away checkpinted SPRs. */
  1328. std r31, VCPU_PPR_TM(r9)
  1329. std r30, VCPU_DSCR_TM(r9)
  1330. mflr r5
  1331. mfcr r6
  1332. mfctr r7
  1333. mfspr r8, SPRN_AMR
  1334. mfspr r10, SPRN_TAR
  1335. std r5, VCPU_LR_TM(r9)
  1336. stw r6, VCPU_CR_TM(r9)
  1337. std r7, VCPU_CTR_TM(r9)
  1338. std r8, VCPU_AMR_TM(r9)
  1339. std r10, VCPU_TAR_TM(r9)
  1340. /* Restore r12 as trap number. */
  1341. lwz r12, VCPU_TRAP(r9)
  1342. /* Save FP/VSX. */
  1343. addi r3, r9, VCPU_FPRS_TM
  1344. bl store_fp_state
  1345. addi r3, r9, VCPU_VRS_TM
  1346. bl store_vr_state
  1347. mfspr r6, SPRN_VRSAVE
  1348. stw r6, VCPU_VRSAVE_TM(r9)
  1349. 1:
  1350. /*
  1351. * We need to save these SPRs after the treclaim so that the software
  1352. * error code is recorded correctly in the TEXASR. Also the user may
  1353. * change these outside of a transaction, so they must always be
  1354. * context switched.
  1355. */
  1356. mfspr r5, SPRN_TFHAR
  1357. mfspr r6, SPRN_TFIAR
  1358. mfspr r7, SPRN_TEXASR
  1359. std r5, VCPU_TFHAR(r9)
  1360. std r6, VCPU_TFIAR(r9)
  1361. std r7, VCPU_TEXASR(r9)
  1362. 2:
  1363. #endif
  1364. /* Increment yield count if they have a VPA */
  1365. ld r8, VCPU_VPA(r9) /* do they have a VPA? */
  1366. cmpdi r8, 0
  1367. beq 25f
  1368. li r4, LPPACA_YIELDCOUNT
  1369. LWZX_BE r3, r8, r4
  1370. addi r3, r3, 1
  1371. STWX_BE r3, r8, r4
  1372. li r3, 1
  1373. stb r3, VCPU_VPA_DIRTY(r9)
  1374. 25:
  1375. /* Save PMU registers if requested */
  1376. /* r8 and cr0.eq are live here */
  1377. BEGIN_FTR_SECTION
  1378. /*
  1379. * POWER8 seems to have a hardware bug where setting
  1380. * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
  1381. * when some counters are already negative doesn't seem
  1382. * to cause a performance monitor alert (and hence interrupt).
  1383. * The effect of this is that when saving the PMU state,
  1384. * if there is no PMU alert pending when we read MMCR0
  1385. * before freezing the counters, but one becomes pending
  1386. * before we read the counters, we lose it.
  1387. * To work around this, we need a way to freeze the counters
  1388. * before reading MMCR0. Normally, freezing the counters
  1389. * is done by writing MMCR0 (to set MMCR0[FC]) which
  1390. * unavoidably writes MMCR0[PMA0] as well. On POWER8,
  1391. * we can also freeze the counters using MMCR2, by writing
  1392. * 1s to all the counter freeze condition bits (there are
  1393. * 9 bits each for 6 counters).
  1394. */
  1395. li r3, -1 /* set all freeze bits */
  1396. clrrdi r3, r3, 10
  1397. mfspr r10, SPRN_MMCR2
  1398. mtspr SPRN_MMCR2, r3
  1399. isync
  1400. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  1401. li r3, 1
  1402. sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
  1403. mfspr r4, SPRN_MMCR0 /* save MMCR0 */
  1404. mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
  1405. mfspr r6, SPRN_MMCRA
  1406. /* Clear MMCRA in order to disable SDAR updates */
  1407. li r7, 0
  1408. mtspr SPRN_MMCRA, r7
  1409. isync
  1410. beq 21f /* if no VPA, save PMU stuff anyway */
  1411. lbz r7, LPPACA_PMCINUSE(r8)
  1412. cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
  1413. bne 21f
  1414. std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
  1415. b 22f
  1416. 21: mfspr r5, SPRN_MMCR1
  1417. mfspr r7, SPRN_SIAR
  1418. mfspr r8, SPRN_SDAR
  1419. std r4, VCPU_MMCR(r9)
  1420. std r5, VCPU_MMCR + 8(r9)
  1421. std r6, VCPU_MMCR + 16(r9)
  1422. BEGIN_FTR_SECTION
  1423. std r10, VCPU_MMCR + 24(r9)
  1424. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  1425. std r7, VCPU_SIAR(r9)
  1426. std r8, VCPU_SDAR(r9)
  1427. mfspr r3, SPRN_PMC1
  1428. mfspr r4, SPRN_PMC2
  1429. mfspr r5, SPRN_PMC3
  1430. mfspr r6, SPRN_PMC4
  1431. mfspr r7, SPRN_PMC5
  1432. mfspr r8, SPRN_PMC6
  1433. stw r3, VCPU_PMC(r9)
  1434. stw r4, VCPU_PMC + 4(r9)
  1435. stw r5, VCPU_PMC + 8(r9)
  1436. stw r6, VCPU_PMC + 12(r9)
  1437. stw r7, VCPU_PMC + 16(r9)
  1438. stw r8, VCPU_PMC + 20(r9)
  1439. BEGIN_FTR_SECTION
  1440. mfspr r5, SPRN_SIER
  1441. mfspr r6, SPRN_SPMC1
  1442. mfspr r7, SPRN_SPMC2
  1443. mfspr r8, SPRN_MMCRS
  1444. std r5, VCPU_SIER(r9)
  1445. stw r6, VCPU_PMC + 24(r9)
  1446. stw r7, VCPU_PMC + 28(r9)
  1447. std r8, VCPU_MMCR + 32(r9)
  1448. lis r4, 0x8000
  1449. mtspr SPRN_MMCRS, r4
  1450. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  1451. 22:
  1452. /* Clear out SLB */
  1453. li r5,0
  1454. slbmte r5,r5
  1455. slbia
  1456. ptesync
  1457. /*
  1458. * POWER7/POWER8 guest -> host partition switch code.
  1459. * We don't have to lock against tlbies but we do
  1460. * have to coordinate the hardware threads.
  1461. */
  1462. kvmhv_switch_to_host:
  1463. /* Secondary threads wait for primary to do partition switch */
  1464. ld r5,HSTATE_KVM_VCORE(r13)
  1465. ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
  1466. lbz r3,HSTATE_PTID(r13)
  1467. cmpwi r3,0
  1468. beq 15f
  1469. HMT_LOW
  1470. 13: lbz r3,VCORE_IN_GUEST(r5)
  1471. cmpwi r3,0
  1472. bne 13b
  1473. HMT_MEDIUM
  1474. b 16f
  1475. /* Primary thread waits for all the secondaries to exit guest */
  1476. 15: lwz r3,VCORE_ENTRY_EXIT(r5)
  1477. rlwinm r0,r3,32-8,0xff
  1478. clrldi r3,r3,56
  1479. cmpw r3,r0
  1480. bne 15b
  1481. isync
  1482. /* Did we actually switch to the guest at all? */
  1483. lbz r6, VCORE_IN_GUEST(r5)
  1484. cmpwi r6, 0
  1485. beq 19f
  1486. /* Primary thread switches back to host partition */
  1487. ld r6,KVM_HOST_SDR1(r4)
  1488. lwz r7,KVM_HOST_LPID(r4)
  1489. li r8,LPID_RSVD /* switch to reserved LPID */
  1490. mtspr SPRN_LPID,r8
  1491. ptesync
  1492. mtspr SPRN_SDR1,r6 /* switch to partition page table */
  1493. mtspr SPRN_LPID,r7
  1494. isync
  1495. BEGIN_FTR_SECTION
  1496. /* DPDES is shared between threads */
  1497. mfspr r7, SPRN_DPDES
  1498. std r7, VCORE_DPDES(r5)
  1499. /* clear DPDES so we don't get guest doorbells in the host */
  1500. li r8, 0
  1501. mtspr SPRN_DPDES, r8
  1502. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  1503. /* Subtract timebase offset from timebase */
  1504. ld r8,VCORE_TB_OFFSET(r5)
  1505. cmpdi r8,0
  1506. beq 17f
  1507. mftb r6 /* current guest timebase */
  1508. subf r8,r8,r6
  1509. mtspr SPRN_TBU40,r8 /* update upper 40 bits */
  1510. mftb r7 /* check if lower 24 bits overflowed */
  1511. clrldi r6,r6,40
  1512. clrldi r7,r7,40
  1513. cmpld r7,r6
  1514. bge 17f
  1515. addis r8,r8,0x100 /* if so, increment upper 40 bits */
  1516. mtspr SPRN_TBU40,r8
  1517. /* Reset PCR */
  1518. 17: ld r0, VCORE_PCR(r5)
  1519. cmpdi r0, 0
  1520. beq 18f
  1521. li r0, 0
  1522. mtspr SPRN_PCR, r0
  1523. 18:
  1524. /* Signal secondary CPUs to continue */
  1525. stb r0,VCORE_IN_GUEST(r5)
  1526. 19: lis r8,0x7fff /* MAX_INT@h */
  1527. mtspr SPRN_HDEC,r8
  1528. 16: ld r8,KVM_HOST_LPCR(r4)
  1529. mtspr SPRN_LPCR,r8
  1530. isync
  1531. /* load host SLB entries */
  1532. ld r8,PACA_SLBSHADOWPTR(r13)
  1533. .rept SLB_NUM_BOLTED
  1534. li r3, SLBSHADOW_SAVEAREA
  1535. LDX_BE r5, r8, r3
  1536. addi r3, r3, 8
  1537. LDX_BE r6, r8, r3
  1538. andis. r7,r5,SLB_ESID_V@h
  1539. beq 1f
  1540. slbmte r6,r5
  1541. 1: addi r8,r8,16
  1542. .endr
  1543. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1544. /* Finish timing, if we have a vcpu */
  1545. ld r4, HSTATE_KVM_VCPU(r13)
  1546. cmpdi r4, 0
  1547. li r3, 0
  1548. beq 2f
  1549. bl kvmhv_accumulate_time
  1550. 2:
  1551. #endif
  1552. /* Unset guest mode */
  1553. li r0, KVM_GUEST_MODE_NONE
  1554. stb r0, HSTATE_IN_GUEST(r13)
  1555. ld r0, 112+PPC_LR_STKOFF(r1)
  1556. addi r1, r1, 112
  1557. mtlr r0
  1558. blr
  1559. /*
  1560. * Check whether an HDSI is an HPTE not found fault or something else.
  1561. * If it is an HPTE not found fault that is due to the guest accessing
  1562. * a page that they have mapped but which we have paged out, then
  1563. * we continue on with the guest exit path. In all other cases,
  1564. * reflect the HDSI to the guest as a DSI.
  1565. */
  1566. kvmppc_hdsi:
  1567. mfspr r4, SPRN_HDAR
  1568. mfspr r6, SPRN_HDSISR
  1569. /* HPTE not found fault or protection fault? */
  1570. andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
  1571. beq 1f /* if not, send it to the guest */
  1572. andi. r0, r11, MSR_DR /* data relocation enabled? */
  1573. beq 3f
  1574. clrrdi r0, r4, 28
  1575. PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
  1576. bne 1f /* if no SLB entry found */
  1577. 4: std r4, VCPU_FAULT_DAR(r9)
  1578. stw r6, VCPU_FAULT_DSISR(r9)
  1579. /* Search the hash table. */
  1580. mr r3, r9 /* vcpu pointer */
  1581. li r7, 1 /* data fault */
  1582. bl kvmppc_hpte_hv_fault
  1583. ld r9, HSTATE_KVM_VCPU(r13)
  1584. ld r10, VCPU_PC(r9)
  1585. ld r11, VCPU_MSR(r9)
  1586. li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
  1587. cmpdi r3, 0 /* retry the instruction */
  1588. beq 6f
  1589. cmpdi r3, -1 /* handle in kernel mode */
  1590. beq guest_exit_cont
  1591. cmpdi r3, -2 /* MMIO emulation; need instr word */
  1592. beq 2f
  1593. /* Synthesize a DSI for the guest */
  1594. ld r4, VCPU_FAULT_DAR(r9)
  1595. mr r6, r3
  1596. 1: mtspr SPRN_DAR, r4
  1597. mtspr SPRN_DSISR, r6
  1598. mtspr SPRN_SRR0, r10
  1599. mtspr SPRN_SRR1, r11
  1600. li r10, BOOK3S_INTERRUPT_DATA_STORAGE
  1601. bl kvmppc_msr_interrupt
  1602. fast_interrupt_c_return:
  1603. 6: ld r7, VCPU_CTR(r9)
  1604. ld r8, VCPU_XER(r9)
  1605. mtctr r7
  1606. mtxer r8
  1607. mr r4, r9
  1608. b fast_guest_return
  1609. 3: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
  1610. ld r5, KVM_VRMA_SLB_V(r5)
  1611. b 4b
  1612. /* If this is for emulated MMIO, load the instruction word */
  1613. 2: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
  1614. /* Set guest mode to 'jump over instruction' so if lwz faults
  1615. * we'll just continue at the next IP. */
  1616. li r0, KVM_GUEST_MODE_SKIP
  1617. stb r0, HSTATE_IN_GUEST(r13)
  1618. /* Do the access with MSR:DR enabled */
  1619. mfmsr r3
  1620. ori r4, r3, MSR_DR /* Enable paging for data */
  1621. mtmsrd r4
  1622. lwz r8, 0(r10)
  1623. mtmsrd r3
  1624. /* Store the result */
  1625. stw r8, VCPU_LAST_INST(r9)
  1626. /* Unset guest mode. */
  1627. li r0, KVM_GUEST_MODE_HOST_HV
  1628. stb r0, HSTATE_IN_GUEST(r13)
  1629. b guest_exit_cont
  1630. /*
  1631. * Similarly for an HISI, reflect it to the guest as an ISI unless
  1632. * it is an HPTE not found fault for a page that we have paged out.
  1633. */
  1634. kvmppc_hisi:
  1635. andis. r0, r11, SRR1_ISI_NOPT@h
  1636. beq 1f
  1637. andi. r0, r11, MSR_IR /* instruction relocation enabled? */
  1638. beq 3f
  1639. clrrdi r0, r10, 28
  1640. PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
  1641. bne 1f /* if no SLB entry found */
  1642. 4:
  1643. /* Search the hash table. */
  1644. mr r3, r9 /* vcpu pointer */
  1645. mr r4, r10
  1646. mr r6, r11
  1647. li r7, 0 /* instruction fault */
  1648. bl kvmppc_hpte_hv_fault
  1649. ld r9, HSTATE_KVM_VCPU(r13)
  1650. ld r10, VCPU_PC(r9)
  1651. ld r11, VCPU_MSR(r9)
  1652. li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
  1653. cmpdi r3, 0 /* retry the instruction */
  1654. beq fast_interrupt_c_return
  1655. cmpdi r3, -1 /* handle in kernel mode */
  1656. beq guest_exit_cont
  1657. /* Synthesize an ISI for the guest */
  1658. mr r11, r3
  1659. 1: mtspr SPRN_SRR0, r10
  1660. mtspr SPRN_SRR1, r11
  1661. li r10, BOOK3S_INTERRUPT_INST_STORAGE
  1662. bl kvmppc_msr_interrupt
  1663. b fast_interrupt_c_return
  1664. 3: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
  1665. ld r5, KVM_VRMA_SLB_V(r6)
  1666. b 4b
  1667. /*
  1668. * Try to handle an hcall in real mode.
  1669. * Returns to the guest if we handle it, or continues on up to
  1670. * the kernel if we can't (i.e. if we don't have a handler for
  1671. * it, or if the handler returns H_TOO_HARD).
  1672. *
  1673. * r5 - r8 contain hcall args,
  1674. * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
  1675. */
  1676. hcall_try_real_mode:
  1677. ld r3,VCPU_GPR(R3)(r9)
  1678. andi. r0,r11,MSR_PR
  1679. /* sc 1 from userspace - reflect to guest syscall */
  1680. bne sc_1_fast_return
  1681. clrrdi r3,r3,2
  1682. cmpldi r3,hcall_real_table_end - hcall_real_table
  1683. bge guest_exit_cont
  1684. /* See if this hcall is enabled for in-kernel handling */
  1685. ld r4, VCPU_KVM(r9)
  1686. srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
  1687. sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
  1688. add r4, r4, r0
  1689. ld r0, KVM_ENABLED_HCALLS(r4)
  1690. rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
  1691. srd r0, r0, r4
  1692. andi. r0, r0, 1
  1693. beq guest_exit_cont
  1694. /* Get pointer to handler, if any, and call it */
  1695. LOAD_REG_ADDR(r4, hcall_real_table)
  1696. lwax r3,r3,r4
  1697. cmpwi r3,0
  1698. beq guest_exit_cont
  1699. add r12,r3,r4
  1700. mtctr r12
  1701. mr r3,r9 /* get vcpu pointer */
  1702. ld r4,VCPU_GPR(R4)(r9)
  1703. bctrl
  1704. cmpdi r3,H_TOO_HARD
  1705. beq hcall_real_fallback
  1706. ld r4,HSTATE_KVM_VCPU(r13)
  1707. std r3,VCPU_GPR(R3)(r4)
  1708. ld r10,VCPU_PC(r4)
  1709. ld r11,VCPU_MSR(r4)
  1710. b fast_guest_return
  1711. sc_1_fast_return:
  1712. mtspr SPRN_SRR0,r10
  1713. mtspr SPRN_SRR1,r11
  1714. li r10, BOOK3S_INTERRUPT_SYSCALL
  1715. bl kvmppc_msr_interrupt
  1716. mr r4,r9
  1717. b fast_guest_return
  1718. /* We've attempted a real mode hcall, but it's punted it back
  1719. * to userspace. We need to restore some clobbered volatiles
  1720. * before resuming the pass-it-to-qemu path */
  1721. hcall_real_fallback:
  1722. li r12,BOOK3S_INTERRUPT_SYSCALL
  1723. ld r9, HSTATE_KVM_VCPU(r13)
  1724. b guest_exit_cont
  1725. .globl hcall_real_table
  1726. hcall_real_table:
  1727. .long 0 /* 0 - unused */
  1728. .long DOTSYM(kvmppc_h_remove) - hcall_real_table
  1729. .long DOTSYM(kvmppc_h_enter) - hcall_real_table
  1730. .long DOTSYM(kvmppc_h_read) - hcall_real_table
  1731. .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
  1732. .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
  1733. .long DOTSYM(kvmppc_h_protect) - hcall_real_table
  1734. .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
  1735. .long DOTSYM(kvmppc_h_put_tce) - hcall_real_table
  1736. .long 0 /* 0x24 - H_SET_SPRG0 */
  1737. .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
  1738. .long 0 /* 0x2c */
  1739. .long 0 /* 0x30 */
  1740. .long 0 /* 0x34 */
  1741. .long 0 /* 0x38 */
  1742. .long 0 /* 0x3c */
  1743. .long 0 /* 0x40 */
  1744. .long 0 /* 0x44 */
  1745. .long 0 /* 0x48 */
  1746. .long 0 /* 0x4c */
  1747. .long 0 /* 0x50 */
  1748. .long 0 /* 0x54 */
  1749. .long 0 /* 0x58 */
  1750. .long 0 /* 0x5c */
  1751. .long 0 /* 0x60 */
  1752. #ifdef CONFIG_KVM_XICS
  1753. .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
  1754. .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
  1755. .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
  1756. .long 0 /* 0x70 - H_IPOLL */
  1757. .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
  1758. #else
  1759. .long 0 /* 0x64 - H_EOI */
  1760. .long 0 /* 0x68 - H_CPPR */
  1761. .long 0 /* 0x6c - H_IPI */
  1762. .long 0 /* 0x70 - H_IPOLL */
  1763. .long 0 /* 0x74 - H_XIRR */
  1764. #endif
  1765. .long 0 /* 0x78 */
  1766. .long 0 /* 0x7c */
  1767. .long 0 /* 0x80 */
  1768. .long 0 /* 0x84 */
  1769. .long 0 /* 0x88 */
  1770. .long 0 /* 0x8c */
  1771. .long 0 /* 0x90 */
  1772. .long 0 /* 0x94 */
  1773. .long 0 /* 0x98 */
  1774. .long 0 /* 0x9c */
  1775. .long 0 /* 0xa0 */
  1776. .long 0 /* 0xa4 */
  1777. .long 0 /* 0xa8 */
  1778. .long 0 /* 0xac */
  1779. .long 0 /* 0xb0 */
  1780. .long 0 /* 0xb4 */
  1781. .long 0 /* 0xb8 */
  1782. .long 0 /* 0xbc */
  1783. .long 0 /* 0xc0 */
  1784. .long 0 /* 0xc4 */
  1785. .long 0 /* 0xc8 */
  1786. .long 0 /* 0xcc */
  1787. .long 0 /* 0xd0 */
  1788. .long 0 /* 0xd4 */
  1789. .long 0 /* 0xd8 */
  1790. .long 0 /* 0xdc */
  1791. .long DOTSYM(kvmppc_h_cede) - hcall_real_table
  1792. .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
  1793. .long 0 /* 0xe8 */
  1794. .long 0 /* 0xec */
  1795. .long 0 /* 0xf0 */
  1796. .long 0 /* 0xf4 */
  1797. .long 0 /* 0xf8 */
  1798. .long 0 /* 0xfc */
  1799. .long 0 /* 0x100 */
  1800. .long 0 /* 0x104 */
  1801. .long 0 /* 0x108 */
  1802. .long 0 /* 0x10c */
  1803. .long 0 /* 0x110 */
  1804. .long 0 /* 0x114 */
  1805. .long 0 /* 0x118 */
  1806. .long 0 /* 0x11c */
  1807. .long 0 /* 0x120 */
  1808. .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
  1809. .long 0 /* 0x128 */
  1810. .long 0 /* 0x12c */
  1811. .long 0 /* 0x130 */
  1812. .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
  1813. .long 0 /* 0x138 */
  1814. .long 0 /* 0x13c */
  1815. .long 0 /* 0x140 */
  1816. .long 0 /* 0x144 */
  1817. .long 0 /* 0x148 */
  1818. .long 0 /* 0x14c */
  1819. .long 0 /* 0x150 */
  1820. .long 0 /* 0x154 */
  1821. .long 0 /* 0x158 */
  1822. .long 0 /* 0x15c */
  1823. .long 0 /* 0x160 */
  1824. .long 0 /* 0x164 */
  1825. .long 0 /* 0x168 */
  1826. .long 0 /* 0x16c */
  1827. .long 0 /* 0x170 */
  1828. .long 0 /* 0x174 */
  1829. .long 0 /* 0x178 */
  1830. .long 0 /* 0x17c */
  1831. .long 0 /* 0x180 */
  1832. .long 0 /* 0x184 */
  1833. .long 0 /* 0x188 */
  1834. .long 0 /* 0x18c */
  1835. .long 0 /* 0x190 */
  1836. .long 0 /* 0x194 */
  1837. .long 0 /* 0x198 */
  1838. .long 0 /* 0x19c */
  1839. .long 0 /* 0x1a0 */
  1840. .long 0 /* 0x1a4 */
  1841. .long 0 /* 0x1a8 */
  1842. .long 0 /* 0x1ac */
  1843. .long 0 /* 0x1b0 */
  1844. .long 0 /* 0x1b4 */
  1845. .long 0 /* 0x1b8 */
  1846. .long 0 /* 0x1bc */
  1847. .long 0 /* 0x1c0 */
  1848. .long 0 /* 0x1c4 */
  1849. .long 0 /* 0x1c8 */
  1850. .long 0 /* 0x1cc */
  1851. .long 0 /* 0x1d0 */
  1852. .long 0 /* 0x1d4 */
  1853. .long 0 /* 0x1d8 */
  1854. .long 0 /* 0x1dc */
  1855. .long 0 /* 0x1e0 */
  1856. .long 0 /* 0x1e4 */
  1857. .long 0 /* 0x1e8 */
  1858. .long 0 /* 0x1ec */
  1859. .long 0 /* 0x1f0 */
  1860. .long 0 /* 0x1f4 */
  1861. .long 0 /* 0x1f8 */
  1862. .long 0 /* 0x1fc */
  1863. .long 0 /* 0x200 */
  1864. .long 0 /* 0x204 */
  1865. .long 0 /* 0x208 */
  1866. .long 0 /* 0x20c */
  1867. .long 0 /* 0x210 */
  1868. .long 0 /* 0x214 */
  1869. .long 0 /* 0x218 */
  1870. .long 0 /* 0x21c */
  1871. .long 0 /* 0x220 */
  1872. .long 0 /* 0x224 */
  1873. .long 0 /* 0x228 */
  1874. .long 0 /* 0x22c */
  1875. .long 0 /* 0x230 */
  1876. .long 0 /* 0x234 */
  1877. .long 0 /* 0x238 */
  1878. .long 0 /* 0x23c */
  1879. .long 0 /* 0x240 */
  1880. .long 0 /* 0x244 */
  1881. .long 0 /* 0x248 */
  1882. .long 0 /* 0x24c */
  1883. .long 0 /* 0x250 */
  1884. .long 0 /* 0x254 */
  1885. .long 0 /* 0x258 */
  1886. .long 0 /* 0x25c */
  1887. .long 0 /* 0x260 */
  1888. .long 0 /* 0x264 */
  1889. .long 0 /* 0x268 */
  1890. .long 0 /* 0x26c */
  1891. .long 0 /* 0x270 */
  1892. .long 0 /* 0x274 */
  1893. .long 0 /* 0x278 */
  1894. .long 0 /* 0x27c */
  1895. .long 0 /* 0x280 */
  1896. .long 0 /* 0x284 */
  1897. .long 0 /* 0x288 */
  1898. .long 0 /* 0x28c */
  1899. .long 0 /* 0x290 */
  1900. .long 0 /* 0x294 */
  1901. .long 0 /* 0x298 */
  1902. .long 0 /* 0x29c */
  1903. .long 0 /* 0x2a0 */
  1904. .long 0 /* 0x2a4 */
  1905. .long 0 /* 0x2a8 */
  1906. .long 0 /* 0x2ac */
  1907. .long 0 /* 0x2b0 */
  1908. .long 0 /* 0x2b4 */
  1909. .long 0 /* 0x2b8 */
  1910. .long 0 /* 0x2bc */
  1911. .long 0 /* 0x2c0 */
  1912. .long 0 /* 0x2c4 */
  1913. .long 0 /* 0x2c8 */
  1914. .long 0 /* 0x2cc */
  1915. .long 0 /* 0x2d0 */
  1916. .long 0 /* 0x2d4 */
  1917. .long 0 /* 0x2d8 */
  1918. .long 0 /* 0x2dc */
  1919. .long 0 /* 0x2e0 */
  1920. .long 0 /* 0x2e4 */
  1921. .long 0 /* 0x2e8 */
  1922. .long 0 /* 0x2ec */
  1923. .long 0 /* 0x2f0 */
  1924. .long 0 /* 0x2f4 */
  1925. .long 0 /* 0x2f8 */
  1926. .long 0 /* 0x2fc */
  1927. .long DOTSYM(kvmppc_h_random) - hcall_real_table
  1928. .globl hcall_real_table_end
  1929. hcall_real_table_end:
  1930. _GLOBAL(kvmppc_h_set_xdabr)
  1931. andi. r0, r5, DABRX_USER | DABRX_KERNEL
  1932. beq 6f
  1933. li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
  1934. andc. r0, r5, r0
  1935. beq 3f
  1936. 6: li r3, H_PARAMETER
  1937. blr
  1938. _GLOBAL(kvmppc_h_set_dabr)
  1939. li r5, DABRX_USER | DABRX_KERNEL
  1940. 3:
  1941. BEGIN_FTR_SECTION
  1942. b 2f
  1943. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  1944. std r4,VCPU_DABR(r3)
  1945. stw r5, VCPU_DABRX(r3)
  1946. mtspr SPRN_DABRX, r5
  1947. /* Work around P7 bug where DABR can get corrupted on mtspr */
  1948. 1: mtspr SPRN_DABR,r4
  1949. mfspr r5, SPRN_DABR
  1950. cmpd r4, r5
  1951. bne 1b
  1952. isync
  1953. li r3,0
  1954. blr
  1955. /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
  1956. 2: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
  1957. rlwimi r5, r4, 1, DAWRX_WT
  1958. clrrdi r4, r4, 3
  1959. std r4, VCPU_DAWR(r3)
  1960. std r5, VCPU_DAWRX(r3)
  1961. mtspr SPRN_DAWR, r4
  1962. mtspr SPRN_DAWRX, r5
  1963. li r3, 0
  1964. blr
  1965. _GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
  1966. ori r11,r11,MSR_EE
  1967. std r11,VCPU_MSR(r3)
  1968. li r0,1
  1969. stb r0,VCPU_CEDED(r3)
  1970. sync /* order setting ceded vs. testing prodded */
  1971. lbz r5,VCPU_PRODDED(r3)
  1972. cmpwi r5,0
  1973. bne kvm_cede_prodded
  1974. li r12,0 /* set trap to 0 to say hcall is handled */
  1975. stw r12,VCPU_TRAP(r3)
  1976. li r0,H_SUCCESS
  1977. std r0,VCPU_GPR(R3)(r3)
  1978. /*
  1979. * Set our bit in the bitmask of napping threads unless all the
  1980. * other threads are already napping, in which case we send this
  1981. * up to the host.
  1982. */
  1983. ld r5,HSTATE_KVM_VCORE(r13)
  1984. lbz r6,HSTATE_PTID(r13)
  1985. lwz r8,VCORE_ENTRY_EXIT(r5)
  1986. clrldi r8,r8,56
  1987. li r0,1
  1988. sld r0,r0,r6
  1989. addi r6,r5,VCORE_NAPPING_THREADS
  1990. 31: lwarx r4,0,r6
  1991. or r4,r4,r0
  1992. cmpw r4,r8
  1993. beq kvm_cede_exit
  1994. stwcx. r4,0,r6
  1995. bne 31b
  1996. /* order napping_threads update vs testing entry_exit_map */
  1997. isync
  1998. li r0,NAPPING_CEDE
  1999. stb r0,HSTATE_NAPPING(r13)
  2000. lwz r7,VCORE_ENTRY_EXIT(r5)
  2001. cmpwi r7,0x100
  2002. bge 33f /* another thread already exiting */
  2003. /*
  2004. * Although not specifically required by the architecture, POWER7
  2005. * preserves the following registers in nap mode, even if an SMT mode
  2006. * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
  2007. * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
  2008. */
  2009. /* Save non-volatile GPRs */
  2010. std r14, VCPU_GPR(R14)(r3)
  2011. std r15, VCPU_GPR(R15)(r3)
  2012. std r16, VCPU_GPR(R16)(r3)
  2013. std r17, VCPU_GPR(R17)(r3)
  2014. std r18, VCPU_GPR(R18)(r3)
  2015. std r19, VCPU_GPR(R19)(r3)
  2016. std r20, VCPU_GPR(R20)(r3)
  2017. std r21, VCPU_GPR(R21)(r3)
  2018. std r22, VCPU_GPR(R22)(r3)
  2019. std r23, VCPU_GPR(R23)(r3)
  2020. std r24, VCPU_GPR(R24)(r3)
  2021. std r25, VCPU_GPR(R25)(r3)
  2022. std r26, VCPU_GPR(R26)(r3)
  2023. std r27, VCPU_GPR(R27)(r3)
  2024. std r28, VCPU_GPR(R28)(r3)
  2025. std r29, VCPU_GPR(R29)(r3)
  2026. std r30, VCPU_GPR(R30)(r3)
  2027. std r31, VCPU_GPR(R31)(r3)
  2028. /* save FP state */
  2029. bl kvmppc_save_fp
  2030. /*
  2031. * Set DEC to the smaller of DEC and HDEC, so that we wake
  2032. * no later than the end of our timeslice (HDEC interrupts
  2033. * don't wake us from nap).
  2034. */
  2035. mfspr r3, SPRN_DEC
  2036. mfspr r4, SPRN_HDEC
  2037. mftb r5
  2038. cmpw r3, r4
  2039. ble 67f
  2040. mtspr SPRN_DEC, r4
  2041. 67:
  2042. /* save expiry time of guest decrementer */
  2043. extsw r3, r3
  2044. add r3, r3, r5
  2045. ld r4, HSTATE_KVM_VCPU(r13)
  2046. ld r5, HSTATE_KVM_VCORE(r13)
  2047. ld r6, VCORE_TB_OFFSET(r5)
  2048. subf r3, r6, r3 /* convert to host TB value */
  2049. std r3, VCPU_DEC_EXPIRES(r4)
  2050. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  2051. ld r4, HSTATE_KVM_VCPU(r13)
  2052. addi r3, r4, VCPU_TB_CEDE
  2053. bl kvmhv_accumulate_time
  2054. #endif
  2055. lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
  2056. /*
  2057. * Take a nap until a decrementer or external or doobell interrupt
  2058. * occurs, with PECE1 and PECE0 set in LPCR.
  2059. * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
  2060. * Also clear the runlatch bit before napping.
  2061. */
  2062. kvm_do_nap:
  2063. mfspr r0, SPRN_CTRLF
  2064. clrrdi r0, r0, 1
  2065. mtspr SPRN_CTRLT, r0
  2066. li r0,1
  2067. stb r0,HSTATE_HWTHREAD_REQ(r13)
  2068. mfspr r5,SPRN_LPCR
  2069. ori r5,r5,LPCR_PECE0 | LPCR_PECE1
  2070. BEGIN_FTR_SECTION
  2071. ori r5, r5, LPCR_PECEDH
  2072. rlwimi r5, r3, 0, LPCR_PECEDP
  2073. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  2074. mtspr SPRN_LPCR,r5
  2075. isync
  2076. li r0, 0
  2077. std r0, HSTATE_SCRATCH0(r13)
  2078. ptesync
  2079. ld r0, HSTATE_SCRATCH0(r13)
  2080. 1: cmpd r0, r0
  2081. bne 1b
  2082. nap
  2083. b .
  2084. 33: mr r4, r3
  2085. li r3, 0
  2086. li r12, 0
  2087. b 34f
  2088. kvm_end_cede:
  2089. /* get vcpu pointer */
  2090. ld r4, HSTATE_KVM_VCPU(r13)
  2091. /* Woken by external or decrementer interrupt */
  2092. ld r1, HSTATE_HOST_R1(r13)
  2093. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  2094. addi r3, r4, VCPU_TB_RMINTR
  2095. bl kvmhv_accumulate_time
  2096. #endif
  2097. /* load up FP state */
  2098. bl kvmppc_load_fp
  2099. /* Restore guest decrementer */
  2100. ld r3, VCPU_DEC_EXPIRES(r4)
  2101. ld r5, HSTATE_KVM_VCORE(r13)
  2102. ld r6, VCORE_TB_OFFSET(r5)
  2103. add r3, r3, r6 /* convert host TB to guest TB value */
  2104. mftb r7
  2105. subf r3, r7, r3
  2106. mtspr SPRN_DEC, r3
  2107. /* Load NV GPRS */
  2108. ld r14, VCPU_GPR(R14)(r4)
  2109. ld r15, VCPU_GPR(R15)(r4)
  2110. ld r16, VCPU_GPR(R16)(r4)
  2111. ld r17, VCPU_GPR(R17)(r4)
  2112. ld r18, VCPU_GPR(R18)(r4)
  2113. ld r19, VCPU_GPR(R19)(r4)
  2114. ld r20, VCPU_GPR(R20)(r4)
  2115. ld r21, VCPU_GPR(R21)(r4)
  2116. ld r22, VCPU_GPR(R22)(r4)
  2117. ld r23, VCPU_GPR(R23)(r4)
  2118. ld r24, VCPU_GPR(R24)(r4)
  2119. ld r25, VCPU_GPR(R25)(r4)
  2120. ld r26, VCPU_GPR(R26)(r4)
  2121. ld r27, VCPU_GPR(R27)(r4)
  2122. ld r28, VCPU_GPR(R28)(r4)
  2123. ld r29, VCPU_GPR(R29)(r4)
  2124. ld r30, VCPU_GPR(R30)(r4)
  2125. ld r31, VCPU_GPR(R31)(r4)
  2126. /* Check the wake reason in SRR1 to see why we got here */
  2127. bl kvmppc_check_wake_reason
  2128. /* clear our bit in vcore->napping_threads */
  2129. 34: ld r5,HSTATE_KVM_VCORE(r13)
  2130. lbz r7,HSTATE_PTID(r13)
  2131. li r0,1
  2132. sld r0,r0,r7
  2133. addi r6,r5,VCORE_NAPPING_THREADS
  2134. 32: lwarx r7,0,r6
  2135. andc r7,r7,r0
  2136. stwcx. r7,0,r6
  2137. bne 32b
  2138. li r0,0
  2139. stb r0,HSTATE_NAPPING(r13)
  2140. /* See if the wake reason means we need to exit */
  2141. stw r12, VCPU_TRAP(r4)
  2142. mr r9, r4
  2143. cmpdi r3, 0
  2144. bgt guest_exit_cont
  2145. /* see if any other thread is already exiting */
  2146. lwz r0,VCORE_ENTRY_EXIT(r5)
  2147. cmpwi r0,0x100
  2148. bge guest_exit_cont
  2149. b kvmppc_cede_reentry /* if not go back to guest */
  2150. /* cede when already previously prodded case */
  2151. kvm_cede_prodded:
  2152. li r0,0
  2153. stb r0,VCPU_PRODDED(r3)
  2154. sync /* order testing prodded vs. clearing ceded */
  2155. stb r0,VCPU_CEDED(r3)
  2156. li r3,H_SUCCESS
  2157. blr
  2158. /* we've ceded but we want to give control to the host */
  2159. kvm_cede_exit:
  2160. ld r9, HSTATE_KVM_VCPU(r13)
  2161. b guest_exit_cont
  2162. /* Try to handle a machine check in real mode */
  2163. machine_check_realmode:
  2164. mr r3, r9 /* get vcpu pointer */
  2165. bl kvmppc_realmode_machine_check
  2166. nop
  2167. cmpdi r3, 0 /* Did we handle MCE ? */
  2168. ld r9, HSTATE_KVM_VCPU(r13)
  2169. li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
  2170. /*
  2171. * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
  2172. * machine check interrupt (set HSRR0 to 0x200). And for handled
  2173. * errors (no-fatal), just go back to guest execution with current
  2174. * HSRR0 instead of exiting guest. This new approach will inject
  2175. * machine check to guest for fatal error causing guest to crash.
  2176. *
  2177. * The old code used to return to host for unhandled errors which
  2178. * was causing guest to hang with soft lockups inside guest and
  2179. * makes it difficult to recover guest instance.
  2180. */
  2181. ld r10, VCPU_PC(r9)
  2182. ld r11, VCPU_MSR(r9)
  2183. bne 2f /* Continue guest execution. */
  2184. /* If not, deliver a machine check. SRR0/1 are already set */
  2185. li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
  2186. ld r11, VCPU_MSR(r9)
  2187. bl kvmppc_msr_interrupt
  2188. 2: b fast_interrupt_c_return
  2189. /*
  2190. * Check the reason we woke from nap, and take appropriate action.
  2191. * Returns (in r3):
  2192. * 0 if nothing needs to be done
  2193. * 1 if something happened that needs to be handled by the host
  2194. * -1 if there was a guest wakeup (IPI or msgsnd)
  2195. *
  2196. * Also sets r12 to the interrupt vector for any interrupt that needs
  2197. * to be handled now by the host (0x500 for external interrupt), or zero.
  2198. * Modifies r0, r6, r7, r8.
  2199. */
  2200. kvmppc_check_wake_reason:
  2201. mfspr r6, SPRN_SRR1
  2202. BEGIN_FTR_SECTION
  2203. rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
  2204. FTR_SECTION_ELSE
  2205. rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
  2206. ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
  2207. cmpwi r6, 8 /* was it an external interrupt? */
  2208. li r12, BOOK3S_INTERRUPT_EXTERNAL
  2209. beq kvmppc_read_intr /* if so, see what it was */
  2210. li r3, 0
  2211. li r12, 0
  2212. cmpwi r6, 6 /* was it the decrementer? */
  2213. beq 0f
  2214. BEGIN_FTR_SECTION
  2215. cmpwi r6, 5 /* privileged doorbell? */
  2216. beq 0f
  2217. cmpwi r6, 3 /* hypervisor doorbell? */
  2218. beq 3f
  2219. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  2220. li r3, 1 /* anything else, return 1 */
  2221. 0: blr
  2222. /* hypervisor doorbell */
  2223. 3: li r12, BOOK3S_INTERRUPT_H_DOORBELL
  2224. /* see if it's a host IPI */
  2225. li r3, 1
  2226. lbz r0, HSTATE_HOST_IPI(r13)
  2227. cmpwi r0, 0
  2228. bnelr
  2229. /* if not, clear it and return -1 */
  2230. lis r6, (PPC_DBELL_SERVER << (63-36))@h
  2231. PPC_MSGCLR(6)
  2232. li r3, -1
  2233. blr
  2234. /*
  2235. * Determine what sort of external interrupt is pending (if any).
  2236. * Returns:
  2237. * 0 if no interrupt is pending
  2238. * 1 if an interrupt is pending that needs to be handled by the host
  2239. * -1 if there was a guest wakeup IPI (which has now been cleared)
  2240. * Modifies r0, r6, r7, r8, returns value in r3.
  2241. */
  2242. kvmppc_read_intr:
  2243. /* see if a host IPI is pending */
  2244. li r3, 1
  2245. lbz r0, HSTATE_HOST_IPI(r13)
  2246. cmpwi r0, 0
  2247. bne 1f
  2248. /* Now read the interrupt from the ICP */
  2249. ld r6, HSTATE_XICS_PHYS(r13)
  2250. li r7, XICS_XIRR
  2251. cmpdi r6, 0
  2252. beq- 1f
  2253. lwzcix r0, r6, r7
  2254. /*
  2255. * Save XIRR for later. Since we get in in reverse endian on LE
  2256. * systems, save it byte reversed and fetch it back in host endian.
  2257. */
  2258. li r3, HSTATE_SAVED_XIRR
  2259. STWX_BE r0, r3, r13
  2260. #ifdef __LITTLE_ENDIAN__
  2261. lwz r3, HSTATE_SAVED_XIRR(r13)
  2262. #else
  2263. mr r3, r0
  2264. #endif
  2265. rlwinm. r3, r3, 0, 0xffffff
  2266. sync
  2267. beq 1f /* if nothing pending in the ICP */
  2268. /* We found something in the ICP...
  2269. *
  2270. * If it's not an IPI, stash it in the PACA and return to
  2271. * the host, we don't (yet) handle directing real external
  2272. * interrupts directly to the guest
  2273. */
  2274. cmpwi r3, XICS_IPI /* if there is, is it an IPI? */
  2275. bne 42f
  2276. /* It's an IPI, clear the MFRR and EOI it */
  2277. li r3, 0xff
  2278. li r8, XICS_MFRR
  2279. stbcix r3, r6, r8 /* clear the IPI */
  2280. stwcix r0, r6, r7 /* EOI it */
  2281. sync
  2282. /* We need to re-check host IPI now in case it got set in the
  2283. * meantime. If it's clear, we bounce the interrupt to the
  2284. * guest
  2285. */
  2286. lbz r0, HSTATE_HOST_IPI(r13)
  2287. cmpwi r0, 0
  2288. bne- 43f
  2289. /* OK, it's an IPI for us */
  2290. li r12, 0
  2291. li r3, -1
  2292. 1: blr
  2293. 42: /* It's not an IPI and it's for the host. We saved a copy of XIRR in
  2294. * the PACA earlier, it will be picked up by the host ICP driver
  2295. */
  2296. li r3, 1
  2297. b 1b
  2298. 43: /* We raced with the host, we need to resend that IPI, bummer */
  2299. li r0, IPI_PRIORITY
  2300. stbcix r0, r6, r8 /* set the IPI */
  2301. sync
  2302. li r3, 1
  2303. b 1b
  2304. /*
  2305. * Save away FP, VMX and VSX registers.
  2306. * r3 = vcpu pointer
  2307. * N.B. r30 and r31 are volatile across this function,
  2308. * thus it is not callable from C.
  2309. */
  2310. kvmppc_save_fp:
  2311. mflr r30
  2312. mr r31,r3
  2313. mfmsr r5
  2314. ori r8,r5,MSR_FP
  2315. #ifdef CONFIG_ALTIVEC
  2316. BEGIN_FTR_SECTION
  2317. oris r8,r8,MSR_VEC@h
  2318. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  2319. #endif
  2320. #ifdef CONFIG_VSX
  2321. BEGIN_FTR_SECTION
  2322. oris r8,r8,MSR_VSX@h
  2323. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  2324. #endif
  2325. mtmsrd r8
  2326. addi r3,r3,VCPU_FPRS
  2327. bl store_fp_state
  2328. #ifdef CONFIG_ALTIVEC
  2329. BEGIN_FTR_SECTION
  2330. addi r3,r31,VCPU_VRS
  2331. bl store_vr_state
  2332. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  2333. #endif
  2334. mfspr r6,SPRN_VRSAVE
  2335. stw r6,VCPU_VRSAVE(r31)
  2336. mtlr r30
  2337. blr
  2338. /*
  2339. * Load up FP, VMX and VSX registers
  2340. * r4 = vcpu pointer
  2341. * N.B. r30 and r31 are volatile across this function,
  2342. * thus it is not callable from C.
  2343. */
  2344. kvmppc_load_fp:
  2345. mflr r30
  2346. mr r31,r4
  2347. mfmsr r9
  2348. ori r8,r9,MSR_FP
  2349. #ifdef CONFIG_ALTIVEC
  2350. BEGIN_FTR_SECTION
  2351. oris r8,r8,MSR_VEC@h
  2352. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  2353. #endif
  2354. #ifdef CONFIG_VSX
  2355. BEGIN_FTR_SECTION
  2356. oris r8,r8,MSR_VSX@h
  2357. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  2358. #endif
  2359. mtmsrd r8
  2360. addi r3,r4,VCPU_FPRS
  2361. bl load_fp_state
  2362. #ifdef CONFIG_ALTIVEC
  2363. BEGIN_FTR_SECTION
  2364. addi r3,r31,VCPU_VRS
  2365. bl load_vr_state
  2366. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  2367. #endif
  2368. lwz r7,VCPU_VRSAVE(r31)
  2369. mtspr SPRN_VRSAVE,r7
  2370. mtlr r30
  2371. mr r4,r31
  2372. blr
  2373. /*
  2374. * We come here if we get any exception or interrupt while we are
  2375. * executing host real mode code while in guest MMU context.
  2376. * For now just spin, but we should do something better.
  2377. */
  2378. kvmppc_bad_host_intr:
  2379. b .
  2380. /*
  2381. * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
  2382. * from VCPU_INTR_MSR and is modified based on the required TM state changes.
  2383. * r11 has the guest MSR value (in/out)
  2384. * r9 has a vcpu pointer (in)
  2385. * r0 is used as a scratch register
  2386. */
  2387. kvmppc_msr_interrupt:
  2388. rldicl r0, r11, 64 - MSR_TS_S_LG, 62
  2389. cmpwi r0, 2 /* Check if we are in transactional state.. */
  2390. ld r11, VCPU_INTR_MSR(r9)
  2391. bne 1f
  2392. /* ... if transactional, change to suspended */
  2393. li r0, 1
  2394. 1: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
  2395. blr
  2396. /*
  2397. * This works around a hardware bug on POWER8E processors, where
  2398. * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
  2399. * performance monitor interrupt. Instead, when we need to have
  2400. * an interrupt pending, we have to arrange for a counter to overflow.
  2401. */
  2402. kvmppc_fix_pmao:
  2403. li r3, 0
  2404. mtspr SPRN_MMCR2, r3
  2405. lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
  2406. ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
  2407. mtspr SPRN_MMCR0, r3
  2408. lis r3, 0x7fff
  2409. ori r3, r3, 0xffff
  2410. mtspr SPRN_PMC6, r3
  2411. isync
  2412. blr
  2413. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  2414. /*
  2415. * Start timing an activity
  2416. * r3 = pointer to time accumulation struct, r4 = vcpu
  2417. */
  2418. kvmhv_start_timing:
  2419. ld r5, HSTATE_KVM_VCORE(r13)
  2420. lbz r6, VCORE_IN_GUEST(r5)
  2421. cmpwi r6, 0
  2422. beq 5f /* if in guest, need to */
  2423. ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
  2424. 5: mftb r5
  2425. subf r5, r6, r5
  2426. std r3, VCPU_CUR_ACTIVITY(r4)
  2427. std r5, VCPU_ACTIVITY_START(r4)
  2428. blr
  2429. /*
  2430. * Accumulate time to one activity and start another.
  2431. * r3 = pointer to new time accumulation struct, r4 = vcpu
  2432. */
  2433. kvmhv_accumulate_time:
  2434. ld r5, HSTATE_KVM_VCORE(r13)
  2435. lbz r8, VCORE_IN_GUEST(r5)
  2436. cmpwi r8, 0
  2437. beq 4f /* if in guest, need to */
  2438. ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
  2439. 4: ld r5, VCPU_CUR_ACTIVITY(r4)
  2440. ld r6, VCPU_ACTIVITY_START(r4)
  2441. std r3, VCPU_CUR_ACTIVITY(r4)
  2442. mftb r7
  2443. subf r7, r8, r7
  2444. std r7, VCPU_ACTIVITY_START(r4)
  2445. cmpdi r5, 0
  2446. beqlr
  2447. subf r3, r6, r7
  2448. ld r8, TAS_SEQCOUNT(r5)
  2449. cmpdi r8, 0
  2450. addi r8, r8, 1
  2451. std r8, TAS_SEQCOUNT(r5)
  2452. lwsync
  2453. ld r7, TAS_TOTAL(r5)
  2454. add r7, r7, r3
  2455. std r7, TAS_TOTAL(r5)
  2456. ld r6, TAS_MIN(r5)
  2457. ld r7, TAS_MAX(r5)
  2458. beq 3f
  2459. cmpd r3, r6
  2460. bge 1f
  2461. 3: std r3, TAS_MIN(r5)
  2462. 1: cmpd r3, r7
  2463. ble 2f
  2464. std r3, TAS_MAX(r5)
  2465. 2: lwsync
  2466. addi r8, r8, 1
  2467. std r8, TAS_SEQCOUNT(r5)
  2468. blr
  2469. #endif