book3s_hv_rmhandlers.S 63 KB

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