book3s_hv_rmhandlers.S 72 KB

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