book3s_hv_rmhandlers.S 87 KB

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