book3s_hv_rmhandlers.S 84 KB

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