booke.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright IBM Corp. 2007
  5. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  6. *
  7. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  8. * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
  9. * Scott Wood <scottwood@freescale.com>
  10. * Varun Sethi <varun.sethi@freescale.com>
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/err.h>
  14. #include <linux/kvm_host.h>
  15. #include <linux/gfp.h>
  16. #include <linux/module.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/fs.h>
  19. #include <asm/cputable.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/interrupt.h>
  22. #include <asm/kvm_ppc.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/dbell.h>
  25. #include <asm/hw_irq.h>
  26. #include <asm/irq.h>
  27. #include <asm/time.h>
  28. #include "timing.h"
  29. #include "booke.h"
  30. #define CREATE_TRACE_POINTS
  31. #include "trace_booke.h"
  32. unsigned long kvmppc_booke_handlers;
  33. const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
  34. KVM_GENERIC_VM_STATS(),
  35. STATS_DESC_ICOUNTER(VM, num_2M_pages),
  36. STATS_DESC_ICOUNTER(VM, num_1G_pages)
  37. };
  38. const struct kvm_stats_header kvm_vm_stats_header = {
  39. .name_size = KVM_STATS_NAME_SIZE,
  40. .num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
  41. .id_offset = sizeof(struct kvm_stats_header),
  42. .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
  43. .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
  44. sizeof(kvm_vm_stats_desc),
  45. };
  46. const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
  47. KVM_GENERIC_VCPU_STATS(),
  48. STATS_DESC_COUNTER(VCPU, sum_exits),
  49. STATS_DESC_COUNTER(VCPU, mmio_exits),
  50. STATS_DESC_COUNTER(VCPU, signal_exits),
  51. STATS_DESC_COUNTER(VCPU, light_exits),
  52. STATS_DESC_COUNTER(VCPU, itlb_real_miss_exits),
  53. STATS_DESC_COUNTER(VCPU, itlb_virt_miss_exits),
  54. STATS_DESC_COUNTER(VCPU, dtlb_real_miss_exits),
  55. STATS_DESC_COUNTER(VCPU, dtlb_virt_miss_exits),
  56. STATS_DESC_COUNTER(VCPU, syscall_exits),
  57. STATS_DESC_COUNTER(VCPU, isi_exits),
  58. STATS_DESC_COUNTER(VCPU, dsi_exits),
  59. STATS_DESC_COUNTER(VCPU, emulated_inst_exits),
  60. STATS_DESC_COUNTER(VCPU, dec_exits),
  61. STATS_DESC_COUNTER(VCPU, ext_intr_exits),
  62. STATS_DESC_COUNTER(VCPU, halt_successful_wait),
  63. STATS_DESC_COUNTER(VCPU, dbell_exits),
  64. STATS_DESC_COUNTER(VCPU, gdbell_exits),
  65. STATS_DESC_COUNTER(VCPU, ld),
  66. STATS_DESC_COUNTER(VCPU, st),
  67. STATS_DESC_COUNTER(VCPU, pthru_all),
  68. STATS_DESC_COUNTER(VCPU, pthru_host),
  69. STATS_DESC_COUNTER(VCPU, pthru_bad_aff)
  70. };
  71. const struct kvm_stats_header kvm_vcpu_stats_header = {
  72. .name_size = KVM_STATS_NAME_SIZE,
  73. .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
  74. .id_offset = sizeof(struct kvm_stats_header),
  75. .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
  76. .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
  77. sizeof(kvm_vcpu_stats_desc),
  78. };
  79. /* TODO: use vcpu_printf() */
  80. void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
  81. {
  82. int i;
  83. printk("pc: %08lx msr: %08llx\n", vcpu->arch.regs.nip,
  84. vcpu->arch.shared->msr);
  85. printk("lr: %08lx ctr: %08lx\n", vcpu->arch.regs.link,
  86. vcpu->arch.regs.ctr);
  87. printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
  88. vcpu->arch.shared->srr1);
  89. printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
  90. for (i = 0; i < 32; i += 4) {
  91. printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
  92. kvmppc_get_gpr(vcpu, i),
  93. kvmppc_get_gpr(vcpu, i+1),
  94. kvmppc_get_gpr(vcpu, i+2),
  95. kvmppc_get_gpr(vcpu, i+3));
  96. }
  97. }
  98. #ifdef CONFIG_SPE
  99. void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
  100. {
  101. preempt_disable();
  102. enable_kernel_spe();
  103. kvmppc_save_guest_spe(vcpu);
  104. disable_kernel_spe();
  105. vcpu->arch.shadow_msr &= ~MSR_SPE;
  106. preempt_enable();
  107. }
  108. static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
  109. {
  110. preempt_disable();
  111. enable_kernel_spe();
  112. kvmppc_load_guest_spe(vcpu);
  113. disable_kernel_spe();
  114. vcpu->arch.shadow_msr |= MSR_SPE;
  115. preempt_enable();
  116. }
  117. static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
  118. {
  119. if (vcpu->arch.shared->msr & MSR_SPE) {
  120. if (!(vcpu->arch.shadow_msr & MSR_SPE))
  121. kvmppc_vcpu_enable_spe(vcpu);
  122. } else if (vcpu->arch.shadow_msr & MSR_SPE) {
  123. kvmppc_vcpu_disable_spe(vcpu);
  124. }
  125. }
  126. #else
  127. static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
  128. {
  129. }
  130. #endif
  131. /*
  132. * Load up guest vcpu FP state if it's needed.
  133. * It also set the MSR_FP in thread so that host know
  134. * we're holding FPU, and then host can help to save
  135. * guest vcpu FP state if other threads require to use FPU.
  136. * This simulates an FP unavailable fault.
  137. *
  138. * It requires to be called with preemption disabled.
  139. */
  140. static inline void kvmppc_load_guest_fp(struct kvm_vcpu *vcpu)
  141. {
  142. #ifdef CONFIG_PPC_FPU
  143. if (!(current->thread.regs->msr & MSR_FP)) {
  144. enable_kernel_fp();
  145. load_fp_state(&vcpu->arch.fp);
  146. disable_kernel_fp();
  147. current->thread.fp_save_area = &vcpu->arch.fp;
  148. current->thread.regs->msr |= MSR_FP;
  149. }
  150. #endif
  151. }
  152. /*
  153. * Save guest vcpu FP state into thread.
  154. * It requires to be called with preemption disabled.
  155. */
  156. static inline void kvmppc_save_guest_fp(struct kvm_vcpu *vcpu)
  157. {
  158. #ifdef CONFIG_PPC_FPU
  159. if (current->thread.regs->msr & MSR_FP)
  160. giveup_fpu(current);
  161. current->thread.fp_save_area = NULL;
  162. #endif
  163. }
  164. static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
  165. {
  166. #if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
  167. /* We always treat the FP bit as enabled from the host
  168. perspective, so only need to adjust the shadow MSR */
  169. vcpu->arch.shadow_msr &= ~MSR_FP;
  170. vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
  171. #endif
  172. }
  173. /*
  174. * Simulate AltiVec unavailable fault to load guest state
  175. * from thread to AltiVec unit.
  176. * It requires to be called with preemption disabled.
  177. */
  178. static inline void kvmppc_load_guest_altivec(struct kvm_vcpu *vcpu)
  179. {
  180. #ifdef CONFIG_ALTIVEC
  181. if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
  182. if (!(current->thread.regs->msr & MSR_VEC)) {
  183. enable_kernel_altivec();
  184. load_vr_state(&vcpu->arch.vr);
  185. disable_kernel_altivec();
  186. current->thread.vr_save_area = &vcpu->arch.vr;
  187. current->thread.regs->msr |= MSR_VEC;
  188. }
  189. }
  190. #endif
  191. }
  192. /*
  193. * Save guest vcpu AltiVec state into thread.
  194. * It requires to be called with preemption disabled.
  195. */
  196. static inline void kvmppc_save_guest_altivec(struct kvm_vcpu *vcpu)
  197. {
  198. #ifdef CONFIG_ALTIVEC
  199. if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
  200. if (current->thread.regs->msr & MSR_VEC)
  201. giveup_altivec(current);
  202. current->thread.vr_save_area = NULL;
  203. }
  204. #endif
  205. }
  206. static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu)
  207. {
  208. /* Synchronize guest's desire to get debug interrupts into shadow MSR */
  209. #ifndef CONFIG_KVM_BOOKE_HV
  210. vcpu->arch.shadow_msr &= ~MSR_DE;
  211. vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE;
  212. #endif
  213. /* Force enable debug interrupts when user space wants to debug */
  214. if (vcpu->guest_debug) {
  215. #ifdef CONFIG_KVM_BOOKE_HV
  216. /*
  217. * Since there is no shadow MSR, sync MSR_DE into the guest
  218. * visible MSR.
  219. */
  220. vcpu->arch.shared->msr |= MSR_DE;
  221. #else
  222. vcpu->arch.shadow_msr |= MSR_DE;
  223. vcpu->arch.shared->msr &= ~MSR_DE;
  224. #endif
  225. }
  226. }
  227. /*
  228. * Helper function for "full" MSR writes. No need to call this if only
  229. * EE/CE/ME/DE/RI are changing.
  230. */
  231. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
  232. {
  233. u32 old_msr = vcpu->arch.shared->msr;
  234. #ifdef CONFIG_KVM_BOOKE_HV
  235. new_msr |= MSR_GS;
  236. #endif
  237. vcpu->arch.shared->msr = new_msr;
  238. kvmppc_mmu_msr_notify(vcpu, old_msr);
  239. kvmppc_vcpu_sync_spe(vcpu);
  240. kvmppc_vcpu_sync_fpu(vcpu);
  241. kvmppc_vcpu_sync_debug(vcpu);
  242. }
  243. static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
  244. unsigned int priority)
  245. {
  246. trace_kvm_booke_queue_irqprio(vcpu, priority);
  247. set_bit(priority, &vcpu->arch.pending_exceptions);
  248. }
  249. void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
  250. ulong dear_flags, ulong esr_flags)
  251. {
  252. vcpu->arch.queued_dear = dear_flags;
  253. vcpu->arch.queued_esr = esr_flags;
  254. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
  255. }
  256. void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu, ulong srr1_flags,
  257. ulong dear_flags, ulong esr_flags)
  258. {
  259. WARN_ON_ONCE(srr1_flags);
  260. vcpu->arch.queued_dear = dear_flags;
  261. vcpu->arch.queued_esr = esr_flags;
  262. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
  263. }
  264. void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu)
  265. {
  266. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  267. }
  268. void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, ulong esr_flags)
  269. {
  270. vcpu->arch.queued_esr = esr_flags;
  271. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
  272. }
  273. static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
  274. ulong esr_flags)
  275. {
  276. vcpu->arch.queued_dear = dear_flags;
  277. vcpu->arch.queued_esr = esr_flags;
  278. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
  279. }
  280. void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
  281. {
  282. vcpu->arch.queued_esr = esr_flags;
  283. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
  284. }
  285. void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu, ulong srr1_flags)
  286. {
  287. WARN_ON_ONCE(srr1_flags);
  288. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
  289. }
  290. #ifdef CONFIG_ALTIVEC
  291. void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu, ulong srr1_flags)
  292. {
  293. WARN_ON_ONCE(srr1_flags);
  294. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_UNAVAIL);
  295. }
  296. #endif
  297. void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
  298. {
  299. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
  300. }
  301. int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
  302. {
  303. return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  304. }
  305. void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
  306. {
  307. clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  308. }
  309. void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
  310. struct kvm_interrupt *irq)
  311. {
  312. unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
  313. if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
  314. prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
  315. kvmppc_booke_queue_irqprio(vcpu, prio);
  316. }
  317. void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
  318. {
  319. clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
  320. clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
  321. }
  322. static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
  323. {
  324. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
  325. }
  326. static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
  327. {
  328. clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
  329. }
  330. void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu)
  331. {
  332. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DEBUG);
  333. }
  334. void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu)
  335. {
  336. clear_bit(BOOKE_IRQPRIO_DEBUG, &vcpu->arch.pending_exceptions);
  337. }
  338. static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  339. {
  340. kvmppc_set_srr0(vcpu, srr0);
  341. kvmppc_set_srr1(vcpu, srr1);
  342. }
  343. static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  344. {
  345. vcpu->arch.csrr0 = srr0;
  346. vcpu->arch.csrr1 = srr1;
  347. }
  348. static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  349. {
  350. if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
  351. vcpu->arch.dsrr0 = srr0;
  352. vcpu->arch.dsrr1 = srr1;
  353. } else {
  354. set_guest_csrr(vcpu, srr0, srr1);
  355. }
  356. }
  357. static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  358. {
  359. vcpu->arch.mcsrr0 = srr0;
  360. vcpu->arch.mcsrr1 = srr1;
  361. }
  362. /* Deliver the interrupt of the corresponding priority, if possible. */
  363. static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
  364. unsigned int priority)
  365. {
  366. int allowed = 0;
  367. ulong msr_mask = 0;
  368. bool update_esr = false, update_dear = false, update_epr = false;
  369. ulong crit_raw = vcpu->arch.shared->critical;
  370. ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
  371. bool crit;
  372. bool keep_irq = false;
  373. enum int_class int_class;
  374. ulong new_msr = vcpu->arch.shared->msr;
  375. /* Truncate crit indicators in 32 bit mode */
  376. if (!(vcpu->arch.shared->msr & MSR_SF)) {
  377. crit_raw &= 0xffffffff;
  378. crit_r1 &= 0xffffffff;
  379. }
  380. /* Critical section when crit == r1 */
  381. crit = (crit_raw == crit_r1);
  382. /* ... and we're in supervisor mode */
  383. crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
  384. if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
  385. priority = BOOKE_IRQPRIO_EXTERNAL;
  386. keep_irq = true;
  387. }
  388. if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
  389. update_epr = true;
  390. switch (priority) {
  391. case BOOKE_IRQPRIO_DTLB_MISS:
  392. case BOOKE_IRQPRIO_DATA_STORAGE:
  393. case BOOKE_IRQPRIO_ALIGNMENT:
  394. update_dear = true;
  395. fallthrough;
  396. case BOOKE_IRQPRIO_INST_STORAGE:
  397. case BOOKE_IRQPRIO_PROGRAM:
  398. update_esr = true;
  399. fallthrough;
  400. case BOOKE_IRQPRIO_ITLB_MISS:
  401. case BOOKE_IRQPRIO_SYSCALL:
  402. case BOOKE_IRQPRIO_FP_UNAVAIL:
  403. #ifdef CONFIG_SPE_POSSIBLE
  404. case BOOKE_IRQPRIO_SPE_UNAVAIL:
  405. case BOOKE_IRQPRIO_SPE_FP_DATA:
  406. case BOOKE_IRQPRIO_SPE_FP_ROUND:
  407. #endif
  408. #ifdef CONFIG_ALTIVEC
  409. case BOOKE_IRQPRIO_ALTIVEC_UNAVAIL:
  410. case BOOKE_IRQPRIO_ALTIVEC_ASSIST:
  411. #endif
  412. case BOOKE_IRQPRIO_AP_UNAVAIL:
  413. allowed = 1;
  414. msr_mask = MSR_CE | MSR_ME | MSR_DE;
  415. int_class = INT_CLASS_NONCRIT;
  416. break;
  417. case BOOKE_IRQPRIO_WATCHDOG:
  418. case BOOKE_IRQPRIO_CRITICAL:
  419. case BOOKE_IRQPRIO_DBELL_CRIT:
  420. allowed = vcpu->arch.shared->msr & MSR_CE;
  421. allowed = allowed && !crit;
  422. msr_mask = MSR_ME;
  423. int_class = INT_CLASS_CRIT;
  424. break;
  425. case BOOKE_IRQPRIO_MACHINE_CHECK:
  426. allowed = vcpu->arch.shared->msr & MSR_ME;
  427. allowed = allowed && !crit;
  428. int_class = INT_CLASS_MC;
  429. break;
  430. case BOOKE_IRQPRIO_DECREMENTER:
  431. case BOOKE_IRQPRIO_FIT:
  432. keep_irq = true;
  433. fallthrough;
  434. case BOOKE_IRQPRIO_EXTERNAL:
  435. case BOOKE_IRQPRIO_DBELL:
  436. allowed = vcpu->arch.shared->msr & MSR_EE;
  437. allowed = allowed && !crit;
  438. msr_mask = MSR_CE | MSR_ME | MSR_DE;
  439. int_class = INT_CLASS_NONCRIT;
  440. break;
  441. case BOOKE_IRQPRIO_DEBUG:
  442. allowed = vcpu->arch.shared->msr & MSR_DE;
  443. allowed = allowed && !crit;
  444. msr_mask = MSR_ME;
  445. if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
  446. int_class = INT_CLASS_DBG;
  447. else
  448. int_class = INT_CLASS_CRIT;
  449. break;
  450. }
  451. if (allowed) {
  452. switch (int_class) {
  453. case INT_CLASS_NONCRIT:
  454. set_guest_srr(vcpu, vcpu->arch.regs.nip,
  455. vcpu->arch.shared->msr);
  456. break;
  457. case INT_CLASS_CRIT:
  458. set_guest_csrr(vcpu, vcpu->arch.regs.nip,
  459. vcpu->arch.shared->msr);
  460. break;
  461. case INT_CLASS_DBG:
  462. set_guest_dsrr(vcpu, vcpu->arch.regs.nip,
  463. vcpu->arch.shared->msr);
  464. break;
  465. case INT_CLASS_MC:
  466. set_guest_mcsrr(vcpu, vcpu->arch.regs.nip,
  467. vcpu->arch.shared->msr);
  468. break;
  469. }
  470. vcpu->arch.regs.nip = vcpu->arch.ivpr |
  471. vcpu->arch.ivor[priority];
  472. if (update_esr)
  473. kvmppc_set_esr(vcpu, vcpu->arch.queued_esr);
  474. if (update_dear)
  475. kvmppc_set_dar(vcpu, vcpu->arch.queued_dear);
  476. if (update_epr) {
  477. if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
  478. kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
  479. else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
  480. BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
  481. kvmppc_mpic_set_epr(vcpu);
  482. }
  483. }
  484. new_msr &= msr_mask;
  485. #if defined(CONFIG_64BIT)
  486. if (vcpu->arch.epcr & SPRN_EPCR_ICM)
  487. new_msr |= MSR_CM;
  488. #endif
  489. kvmppc_set_msr(vcpu, new_msr);
  490. if (!keep_irq)
  491. clear_bit(priority, &vcpu->arch.pending_exceptions);
  492. }
  493. #ifdef CONFIG_KVM_BOOKE_HV
  494. /*
  495. * If an interrupt is pending but masked, raise a guest doorbell
  496. * so that we are notified when the guest enables the relevant
  497. * MSR bit.
  498. */
  499. if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
  500. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
  501. if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
  502. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
  503. if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
  504. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
  505. #endif
  506. return allowed;
  507. }
  508. /*
  509. * Return the number of jiffies until the next timeout. If the timeout is
  510. * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
  511. * because the larger value can break the timer APIs.
  512. */
  513. static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
  514. {
  515. u64 tb, wdt_tb, wdt_ticks = 0;
  516. u64 nr_jiffies = 0;
  517. u32 period = TCR_GET_WP(vcpu->arch.tcr);
  518. wdt_tb = 1ULL << (63 - period);
  519. tb = get_tb();
  520. /*
  521. * The watchdog timeout will hapeen when TB bit corresponding
  522. * to watchdog will toggle from 0 to 1.
  523. */
  524. if (tb & wdt_tb)
  525. wdt_ticks = wdt_tb;
  526. wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
  527. /* Convert timebase ticks to jiffies */
  528. nr_jiffies = wdt_ticks;
  529. if (do_div(nr_jiffies, tb_ticks_per_jiffy))
  530. nr_jiffies++;
  531. return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
  532. }
  533. static void arm_next_watchdog(struct kvm_vcpu *vcpu)
  534. {
  535. unsigned long nr_jiffies;
  536. unsigned long flags;
  537. /*
  538. * If TSR_ENW and TSR_WIS are not set then no need to exit to
  539. * userspace, so clear the KVM_REQ_WATCHDOG request.
  540. */
  541. if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
  542. kvm_clear_request(KVM_REQ_WATCHDOG, vcpu);
  543. spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
  544. nr_jiffies = watchdog_next_timeout(vcpu);
  545. /*
  546. * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
  547. * then do not run the watchdog timer as this can break timer APIs.
  548. */
  549. if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
  550. mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
  551. else
  552. del_timer(&vcpu->arch.wdt_timer);
  553. spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
  554. }
  555. static void kvmppc_watchdog_func(struct timer_list *t)
  556. {
  557. struct kvm_vcpu *vcpu = from_timer(vcpu, t, arch.wdt_timer);
  558. u32 tsr, new_tsr;
  559. int final;
  560. do {
  561. new_tsr = tsr = vcpu->arch.tsr;
  562. final = 0;
  563. /* Time out event */
  564. if (tsr & TSR_ENW) {
  565. if (tsr & TSR_WIS)
  566. final = 1;
  567. else
  568. new_tsr = tsr | TSR_WIS;
  569. } else {
  570. new_tsr = tsr | TSR_ENW;
  571. }
  572. } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
  573. if (new_tsr & TSR_WIS) {
  574. smp_wmb();
  575. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  576. kvm_vcpu_kick(vcpu);
  577. }
  578. /*
  579. * If this is final watchdog expiry and some action is required
  580. * then exit to userspace.
  581. */
  582. if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
  583. vcpu->arch.watchdog_enabled) {
  584. smp_wmb();
  585. kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
  586. kvm_vcpu_kick(vcpu);
  587. }
  588. /*
  589. * Stop running the watchdog timer after final expiration to
  590. * prevent the host from being flooded with timers if the
  591. * guest sets a short period.
  592. * Timers will resume when TSR/TCR is updated next time.
  593. */
  594. if (!final)
  595. arm_next_watchdog(vcpu);
  596. }
  597. static void update_timer_ints(struct kvm_vcpu *vcpu)
  598. {
  599. if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
  600. kvmppc_core_queue_dec(vcpu);
  601. else
  602. kvmppc_core_dequeue_dec(vcpu);
  603. if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
  604. kvmppc_core_queue_watchdog(vcpu);
  605. else
  606. kvmppc_core_dequeue_watchdog(vcpu);
  607. }
  608. static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
  609. {
  610. unsigned long *pending = &vcpu->arch.pending_exceptions;
  611. unsigned int priority;
  612. priority = __ffs(*pending);
  613. while (priority < BOOKE_IRQPRIO_MAX) {
  614. if (kvmppc_booke_irqprio_deliver(vcpu, priority))
  615. break;
  616. priority = find_next_bit(pending,
  617. BITS_PER_BYTE * sizeof(*pending),
  618. priority + 1);
  619. }
  620. /* Tell the guest about our interrupt status */
  621. vcpu->arch.shared->int_pending = !!*pending;
  622. }
  623. /* Check pending exceptions and deliver one, if possible. */
  624. int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
  625. {
  626. int r = 0;
  627. WARN_ON_ONCE(!irqs_disabled());
  628. kvmppc_core_check_exceptions(vcpu);
  629. if (kvm_request_pending(vcpu)) {
  630. /* Exception delivery raised request; start over */
  631. return 1;
  632. }
  633. if (vcpu->arch.shared->msr & MSR_WE) {
  634. local_irq_enable();
  635. kvm_vcpu_halt(vcpu);
  636. hard_irq_disable();
  637. kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
  638. r = 1;
  639. }
  640. return r;
  641. }
  642. int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
  643. {
  644. int r = 1; /* Indicate we want to get back into the guest */
  645. if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
  646. update_timer_ints(vcpu);
  647. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  648. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
  649. kvmppc_core_flush_tlb(vcpu);
  650. #endif
  651. if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
  652. vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
  653. r = 0;
  654. }
  655. if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
  656. vcpu->run->epr.epr = 0;
  657. vcpu->arch.epr_needed = true;
  658. vcpu->run->exit_reason = KVM_EXIT_EPR;
  659. r = 0;
  660. }
  661. return r;
  662. }
  663. int kvmppc_vcpu_run(struct kvm_vcpu *vcpu)
  664. {
  665. int ret, s;
  666. struct debug_reg debug;
  667. if (!vcpu->arch.sane) {
  668. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  669. return -EINVAL;
  670. }
  671. s = kvmppc_prepare_to_enter(vcpu);
  672. if (s <= 0) {
  673. ret = s;
  674. goto out;
  675. }
  676. /* interrupts now hard-disabled */
  677. #ifdef CONFIG_PPC_FPU
  678. /* Save userspace FPU state in stack */
  679. enable_kernel_fp();
  680. /*
  681. * Since we can't trap on MSR_FP in GS-mode, we consider the guest
  682. * as always using the FPU.
  683. */
  684. kvmppc_load_guest_fp(vcpu);
  685. #endif
  686. #ifdef CONFIG_ALTIVEC
  687. /* Save userspace AltiVec state in stack */
  688. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  689. enable_kernel_altivec();
  690. /*
  691. * Since we can't trap on MSR_VEC in GS-mode, we consider the guest
  692. * as always using the AltiVec.
  693. */
  694. kvmppc_load_guest_altivec(vcpu);
  695. #endif
  696. /* Switch to guest debug context */
  697. debug = vcpu->arch.dbg_reg;
  698. switch_booke_debug_regs(&debug);
  699. debug = current->thread.debug;
  700. current->thread.debug = vcpu->arch.dbg_reg;
  701. vcpu->arch.pgdir = vcpu->kvm->mm->pgd;
  702. kvmppc_fix_ee_before_entry();
  703. ret = __kvmppc_vcpu_run(vcpu);
  704. /* No need for guest_exit. It's done in handle_exit.
  705. We also get here with interrupts enabled. */
  706. /* Switch back to user space debug context */
  707. switch_booke_debug_regs(&debug);
  708. current->thread.debug = debug;
  709. #ifdef CONFIG_PPC_FPU
  710. kvmppc_save_guest_fp(vcpu);
  711. #endif
  712. #ifdef CONFIG_ALTIVEC
  713. kvmppc_save_guest_altivec(vcpu);
  714. #endif
  715. out:
  716. vcpu->mode = OUTSIDE_GUEST_MODE;
  717. return ret;
  718. }
  719. static int emulation_exit(struct kvm_vcpu *vcpu)
  720. {
  721. enum emulation_result er;
  722. er = kvmppc_emulate_instruction(vcpu);
  723. switch (er) {
  724. case EMULATE_DONE:
  725. /* don't overwrite subtypes, just account kvm_stats */
  726. kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
  727. /* Future optimization: only reload non-volatiles if
  728. * they were actually modified by emulation. */
  729. return RESUME_GUEST_NV;
  730. case EMULATE_AGAIN:
  731. return RESUME_GUEST;
  732. case EMULATE_FAIL:
  733. printk(KERN_CRIT "%s: emulation at %lx failed (%08lx)\n",
  734. __func__, vcpu->arch.regs.nip, vcpu->arch.last_inst);
  735. /* For debugging, encode the failing instruction and
  736. * report it to userspace. */
  737. vcpu->run->hw.hardware_exit_reason = ~0ULL << 32;
  738. vcpu->run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
  739. kvmppc_core_queue_program(vcpu, ESR_PIL);
  740. return RESUME_HOST;
  741. case EMULATE_EXIT_USER:
  742. return RESUME_HOST;
  743. default:
  744. BUG();
  745. }
  746. }
  747. static int kvmppc_handle_debug(struct kvm_vcpu *vcpu)
  748. {
  749. struct kvm_run *run = vcpu->run;
  750. struct debug_reg *dbg_reg = &(vcpu->arch.dbg_reg);
  751. u32 dbsr = vcpu->arch.dbsr;
  752. if (vcpu->guest_debug == 0) {
  753. /*
  754. * Debug resources belong to Guest.
  755. * Imprecise debug event is not injected
  756. */
  757. if (dbsr & DBSR_IDE) {
  758. dbsr &= ~DBSR_IDE;
  759. if (!dbsr)
  760. return RESUME_GUEST;
  761. }
  762. if (dbsr && (vcpu->arch.shared->msr & MSR_DE) &&
  763. (vcpu->arch.dbg_reg.dbcr0 & DBCR0_IDM))
  764. kvmppc_core_queue_debug(vcpu);
  765. /* Inject a program interrupt if trap debug is not allowed */
  766. if ((dbsr & DBSR_TIE) && !(vcpu->arch.shared->msr & MSR_DE))
  767. kvmppc_core_queue_program(vcpu, ESR_PTR);
  768. return RESUME_GUEST;
  769. }
  770. /*
  771. * Debug resource owned by userspace.
  772. * Clear guest dbsr (vcpu->arch.dbsr)
  773. */
  774. vcpu->arch.dbsr = 0;
  775. run->debug.arch.status = 0;
  776. run->debug.arch.address = vcpu->arch.regs.nip;
  777. if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
  778. run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
  779. } else {
  780. if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
  781. run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
  782. else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
  783. run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
  784. if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
  785. run->debug.arch.address = dbg_reg->dac1;
  786. else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
  787. run->debug.arch.address = dbg_reg->dac2;
  788. }
  789. return RESUME_HOST;
  790. }
  791. static void kvmppc_fill_pt_regs(struct pt_regs *regs)
  792. {
  793. ulong r1, msr, lr;
  794. asm("mr %0, 1" : "=r"(r1));
  795. asm("mflr %0" : "=r"(lr));
  796. asm("mfmsr %0" : "=r"(msr));
  797. memset(regs, 0, sizeof(*regs));
  798. regs->gpr[1] = r1;
  799. regs->nip = _THIS_IP_;
  800. regs->msr = msr;
  801. regs->link = lr;
  802. }
  803. /*
  804. * For interrupts needed to be handled by host interrupt handlers,
  805. * corresponding host handler are called from here in similar way
  806. * (but not exact) as they are called from low level handler
  807. * (such as from arch/powerpc/kernel/head_fsl_booke.S).
  808. */
  809. static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
  810. unsigned int exit_nr)
  811. {
  812. struct pt_regs regs;
  813. switch (exit_nr) {
  814. case BOOKE_INTERRUPT_EXTERNAL:
  815. kvmppc_fill_pt_regs(&regs);
  816. do_IRQ(&regs);
  817. break;
  818. case BOOKE_INTERRUPT_DECREMENTER:
  819. kvmppc_fill_pt_regs(&regs);
  820. timer_interrupt(&regs);
  821. break;
  822. #if defined(CONFIG_PPC_DOORBELL)
  823. case BOOKE_INTERRUPT_DOORBELL:
  824. kvmppc_fill_pt_regs(&regs);
  825. doorbell_exception(&regs);
  826. break;
  827. #endif
  828. case BOOKE_INTERRUPT_MACHINE_CHECK:
  829. /* FIXME */
  830. break;
  831. case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
  832. kvmppc_fill_pt_regs(&regs);
  833. performance_monitor_exception(&regs);
  834. break;
  835. case BOOKE_INTERRUPT_WATCHDOG:
  836. kvmppc_fill_pt_regs(&regs);
  837. #ifdef CONFIG_BOOKE_WDT
  838. WatchdogException(&regs);
  839. #else
  840. unknown_exception(&regs);
  841. #endif
  842. break;
  843. case BOOKE_INTERRUPT_CRITICAL:
  844. kvmppc_fill_pt_regs(&regs);
  845. unknown_exception(&regs);
  846. break;
  847. case BOOKE_INTERRUPT_DEBUG:
  848. /* Save DBSR before preemption is enabled */
  849. vcpu->arch.dbsr = mfspr(SPRN_DBSR);
  850. kvmppc_clear_dbsr();
  851. break;
  852. }
  853. }
  854. static int kvmppc_resume_inst_load(struct kvm_vcpu *vcpu,
  855. enum emulation_result emulated, u32 last_inst)
  856. {
  857. switch (emulated) {
  858. case EMULATE_AGAIN:
  859. return RESUME_GUEST;
  860. case EMULATE_FAIL:
  861. pr_debug("%s: load instruction from guest address %lx failed\n",
  862. __func__, vcpu->arch.regs.nip);
  863. /* For debugging, encode the failing instruction and
  864. * report it to userspace. */
  865. vcpu->run->hw.hardware_exit_reason = ~0ULL << 32;
  866. vcpu->run->hw.hardware_exit_reason |= last_inst;
  867. kvmppc_core_queue_program(vcpu, ESR_PIL);
  868. return RESUME_HOST;
  869. default:
  870. BUG();
  871. }
  872. }
  873. /*
  874. * kvmppc_handle_exit
  875. *
  876. * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
  877. */
  878. int kvmppc_handle_exit(struct kvm_vcpu *vcpu, unsigned int exit_nr)
  879. {
  880. struct kvm_run *run = vcpu->run;
  881. int r = RESUME_HOST;
  882. int s;
  883. int idx;
  884. u32 last_inst = KVM_INST_FETCH_FAILED;
  885. ppc_inst_t pinst;
  886. enum emulation_result emulated = EMULATE_DONE;
  887. /* Fix irq state (pairs with kvmppc_fix_ee_before_entry()) */
  888. kvmppc_fix_ee_after_exit();
  889. /* update before a new last_exit_type is rewritten */
  890. kvmppc_update_timing_stats(vcpu);
  891. /* restart interrupts if they were meant for the host */
  892. kvmppc_restart_interrupt(vcpu, exit_nr);
  893. /*
  894. * get last instruction before being preempted
  895. * TODO: for e6500 check also BOOKE_INTERRUPT_LRAT_ERROR & ESR_DATA
  896. */
  897. switch (exit_nr) {
  898. case BOOKE_INTERRUPT_DATA_STORAGE:
  899. case BOOKE_INTERRUPT_DTLB_MISS:
  900. case BOOKE_INTERRUPT_HV_PRIV:
  901. emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst);
  902. last_inst = ppc_inst_val(pinst);
  903. break;
  904. case BOOKE_INTERRUPT_PROGRAM:
  905. /* SW breakpoints arrive as illegal instructions on HV */
  906. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
  907. emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst);
  908. last_inst = ppc_inst_val(pinst);
  909. }
  910. break;
  911. default:
  912. break;
  913. }
  914. trace_kvm_exit(exit_nr, vcpu);
  915. context_tracking_guest_exit();
  916. if (!vtime_accounting_enabled_this_cpu()) {
  917. local_irq_enable();
  918. /*
  919. * Service IRQs here before vtime_account_guest_exit() so any
  920. * ticks that occurred while running the guest are accounted to
  921. * the guest. If vtime accounting is enabled, accounting uses
  922. * TB rather than ticks, so it can be done without enabling
  923. * interrupts here, which has the problem that it accounts
  924. * interrupt processing overhead to the host.
  925. */
  926. local_irq_disable();
  927. }
  928. vtime_account_guest_exit();
  929. local_irq_enable();
  930. run->exit_reason = KVM_EXIT_UNKNOWN;
  931. run->ready_for_interrupt_injection = 1;
  932. if (emulated != EMULATE_DONE) {
  933. r = kvmppc_resume_inst_load(vcpu, emulated, last_inst);
  934. goto out;
  935. }
  936. switch (exit_nr) {
  937. case BOOKE_INTERRUPT_MACHINE_CHECK:
  938. printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
  939. kvmppc_dump_vcpu(vcpu);
  940. /* For debugging, send invalid exit reason to user space */
  941. run->hw.hardware_exit_reason = ~1ULL << 32;
  942. run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
  943. r = RESUME_HOST;
  944. break;
  945. case BOOKE_INTERRUPT_EXTERNAL:
  946. kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
  947. r = RESUME_GUEST;
  948. break;
  949. case BOOKE_INTERRUPT_DECREMENTER:
  950. kvmppc_account_exit(vcpu, DEC_EXITS);
  951. r = RESUME_GUEST;
  952. break;
  953. case BOOKE_INTERRUPT_WATCHDOG:
  954. r = RESUME_GUEST;
  955. break;
  956. case BOOKE_INTERRUPT_DOORBELL:
  957. kvmppc_account_exit(vcpu, DBELL_EXITS);
  958. r = RESUME_GUEST;
  959. break;
  960. case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
  961. kvmppc_account_exit(vcpu, GDBELL_EXITS);
  962. /*
  963. * We are here because there is a pending guest interrupt
  964. * which could not be delivered as MSR_CE or MSR_ME was not
  965. * set. Once we break from here we will retry delivery.
  966. */
  967. r = RESUME_GUEST;
  968. break;
  969. case BOOKE_INTERRUPT_GUEST_DBELL:
  970. kvmppc_account_exit(vcpu, GDBELL_EXITS);
  971. /*
  972. * We are here because there is a pending guest interrupt
  973. * which could not be delivered as MSR_EE was not set. Once
  974. * we break from here we will retry delivery.
  975. */
  976. r = RESUME_GUEST;
  977. break;
  978. case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
  979. r = RESUME_GUEST;
  980. break;
  981. case BOOKE_INTERRUPT_HV_PRIV:
  982. r = emulation_exit(vcpu);
  983. break;
  984. case BOOKE_INTERRUPT_PROGRAM:
  985. if ((vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) &&
  986. (last_inst == KVMPPC_INST_SW_BREAKPOINT)) {
  987. /*
  988. * We are here because of an SW breakpoint instr,
  989. * so lets return to host to handle.
  990. */
  991. r = kvmppc_handle_debug(vcpu);
  992. run->exit_reason = KVM_EXIT_DEBUG;
  993. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  994. break;
  995. }
  996. if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
  997. /*
  998. * Program traps generated by user-level software must
  999. * be handled by the guest kernel.
  1000. *
  1001. * In GS mode, hypervisor privileged instructions trap
  1002. * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
  1003. * actual program interrupts, handled by the guest.
  1004. */
  1005. kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
  1006. r = RESUME_GUEST;
  1007. kvmppc_account_exit(vcpu, USR_PR_INST);
  1008. break;
  1009. }
  1010. r = emulation_exit(vcpu);
  1011. break;
  1012. case BOOKE_INTERRUPT_FP_UNAVAIL:
  1013. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
  1014. kvmppc_account_exit(vcpu, FP_UNAVAIL);
  1015. r = RESUME_GUEST;
  1016. break;
  1017. #ifdef CONFIG_SPE
  1018. case BOOKE_INTERRUPT_SPE_UNAVAIL: {
  1019. if (vcpu->arch.shared->msr & MSR_SPE)
  1020. kvmppc_vcpu_enable_spe(vcpu);
  1021. else
  1022. kvmppc_booke_queue_irqprio(vcpu,
  1023. BOOKE_IRQPRIO_SPE_UNAVAIL);
  1024. r = RESUME_GUEST;
  1025. break;
  1026. }
  1027. case BOOKE_INTERRUPT_SPE_FP_DATA:
  1028. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
  1029. r = RESUME_GUEST;
  1030. break;
  1031. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  1032. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
  1033. r = RESUME_GUEST;
  1034. break;
  1035. #elif defined(CONFIG_SPE_POSSIBLE)
  1036. case BOOKE_INTERRUPT_SPE_UNAVAIL:
  1037. /*
  1038. * Guest wants SPE, but host kernel doesn't support it. Send
  1039. * an "unimplemented operation" program check to the guest.
  1040. */
  1041. kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
  1042. r = RESUME_GUEST;
  1043. break;
  1044. /*
  1045. * These really should never happen without CONFIG_SPE,
  1046. * as we should never enable the real MSR[SPE] in the guest.
  1047. */
  1048. case BOOKE_INTERRUPT_SPE_FP_DATA:
  1049. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  1050. printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
  1051. __func__, exit_nr, vcpu->arch.regs.nip);
  1052. run->hw.hardware_exit_reason = exit_nr;
  1053. r = RESUME_HOST;
  1054. break;
  1055. #endif /* CONFIG_SPE_POSSIBLE */
  1056. /*
  1057. * On cores with Vector category, KVM is loaded only if CONFIG_ALTIVEC,
  1058. * see kvmppc_e500mc_check_processor_compat().
  1059. */
  1060. #ifdef CONFIG_ALTIVEC
  1061. case BOOKE_INTERRUPT_ALTIVEC_UNAVAIL:
  1062. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_UNAVAIL);
  1063. r = RESUME_GUEST;
  1064. break;
  1065. case BOOKE_INTERRUPT_ALTIVEC_ASSIST:
  1066. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_ASSIST);
  1067. r = RESUME_GUEST;
  1068. break;
  1069. #endif
  1070. case BOOKE_INTERRUPT_DATA_STORAGE:
  1071. kvmppc_core_queue_data_storage(vcpu, 0, vcpu->arch.fault_dear,
  1072. vcpu->arch.fault_esr);
  1073. kvmppc_account_exit(vcpu, DSI_EXITS);
  1074. r = RESUME_GUEST;
  1075. break;
  1076. case BOOKE_INTERRUPT_INST_STORAGE:
  1077. kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
  1078. kvmppc_account_exit(vcpu, ISI_EXITS);
  1079. r = RESUME_GUEST;
  1080. break;
  1081. case BOOKE_INTERRUPT_ALIGNMENT:
  1082. kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
  1083. vcpu->arch.fault_esr);
  1084. r = RESUME_GUEST;
  1085. break;
  1086. #ifdef CONFIG_KVM_BOOKE_HV
  1087. case BOOKE_INTERRUPT_HV_SYSCALL:
  1088. if (!(vcpu->arch.shared->msr & MSR_PR)) {
  1089. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  1090. } else {
  1091. /*
  1092. * hcall from guest userspace -- send privileged
  1093. * instruction program check.
  1094. */
  1095. kvmppc_core_queue_program(vcpu, ESR_PPR);
  1096. }
  1097. r = RESUME_GUEST;
  1098. break;
  1099. #else
  1100. case BOOKE_INTERRUPT_SYSCALL:
  1101. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  1102. (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
  1103. /* KVM PV hypercalls */
  1104. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  1105. r = RESUME_GUEST;
  1106. } else {
  1107. /* Guest syscalls */
  1108. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
  1109. }
  1110. kvmppc_account_exit(vcpu, SYSCALL_EXITS);
  1111. r = RESUME_GUEST;
  1112. break;
  1113. #endif
  1114. case BOOKE_INTERRUPT_DTLB_MISS: {
  1115. unsigned long eaddr = vcpu->arch.fault_dear;
  1116. int gtlb_index;
  1117. gpa_t gpaddr;
  1118. gfn_t gfn;
  1119. #ifdef CONFIG_KVM_E500V2
  1120. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  1121. (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
  1122. kvmppc_map_magic(vcpu);
  1123. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  1124. r = RESUME_GUEST;
  1125. break;
  1126. }
  1127. #endif
  1128. /* Check the guest TLB. */
  1129. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  1130. if (gtlb_index < 0) {
  1131. /* The guest didn't have a mapping for it. */
  1132. kvmppc_core_queue_dtlb_miss(vcpu,
  1133. vcpu->arch.fault_dear,
  1134. vcpu->arch.fault_esr);
  1135. kvmppc_mmu_dtlb_miss(vcpu);
  1136. kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
  1137. r = RESUME_GUEST;
  1138. break;
  1139. }
  1140. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1141. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  1142. gfn = gpaddr >> PAGE_SHIFT;
  1143. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  1144. /* The guest TLB had a mapping, but the shadow TLB
  1145. * didn't, and it is RAM. This could be because:
  1146. * a) the entry is mapping the host kernel, or
  1147. * b) the guest used a large mapping which we're faking
  1148. * Either way, we need to satisfy the fault without
  1149. * invoking the guest. */
  1150. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  1151. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  1152. r = RESUME_GUEST;
  1153. } else {
  1154. /* Guest has mapped and accessed a page which is not
  1155. * actually RAM. */
  1156. vcpu->arch.paddr_accessed = gpaddr;
  1157. vcpu->arch.vaddr_accessed = eaddr;
  1158. r = kvmppc_emulate_mmio(vcpu);
  1159. kvmppc_account_exit(vcpu, MMIO_EXITS);
  1160. }
  1161. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1162. break;
  1163. }
  1164. case BOOKE_INTERRUPT_ITLB_MISS: {
  1165. unsigned long eaddr = vcpu->arch.regs.nip;
  1166. gpa_t gpaddr;
  1167. gfn_t gfn;
  1168. int gtlb_index;
  1169. r = RESUME_GUEST;
  1170. /* Check the guest TLB. */
  1171. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  1172. if (gtlb_index < 0) {
  1173. /* The guest didn't have a mapping for it. */
  1174. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  1175. kvmppc_mmu_itlb_miss(vcpu);
  1176. kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
  1177. break;
  1178. }
  1179. kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
  1180. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1181. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  1182. gfn = gpaddr >> PAGE_SHIFT;
  1183. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  1184. /* The guest TLB had a mapping, but the shadow TLB
  1185. * didn't. This could be because:
  1186. * a) the entry is mapping the host kernel, or
  1187. * b) the guest used a large mapping which we're faking
  1188. * Either way, we need to satisfy the fault without
  1189. * invoking the guest. */
  1190. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  1191. } else {
  1192. /* Guest mapped and leaped at non-RAM! */
  1193. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
  1194. }
  1195. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1196. break;
  1197. }
  1198. case BOOKE_INTERRUPT_DEBUG: {
  1199. r = kvmppc_handle_debug(vcpu);
  1200. if (r == RESUME_HOST)
  1201. run->exit_reason = KVM_EXIT_DEBUG;
  1202. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  1203. break;
  1204. }
  1205. default:
  1206. printk(KERN_EMERG "exit_nr %d\n", exit_nr);
  1207. BUG();
  1208. }
  1209. out:
  1210. /*
  1211. * To avoid clobbering exit_reason, only check for signals if we
  1212. * aren't already exiting to userspace for some other reason.
  1213. */
  1214. if (!(r & RESUME_HOST)) {
  1215. s = kvmppc_prepare_to_enter(vcpu);
  1216. if (s <= 0)
  1217. r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
  1218. else {
  1219. /* interrupts now hard-disabled */
  1220. kvmppc_fix_ee_before_entry();
  1221. kvmppc_load_guest_fp(vcpu);
  1222. kvmppc_load_guest_altivec(vcpu);
  1223. }
  1224. }
  1225. return r;
  1226. }
  1227. static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
  1228. {
  1229. u32 old_tsr = vcpu->arch.tsr;
  1230. vcpu->arch.tsr = new_tsr;
  1231. if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
  1232. arm_next_watchdog(vcpu);
  1233. update_timer_ints(vcpu);
  1234. }
  1235. int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
  1236. {
  1237. /* setup watchdog timer once */
  1238. spin_lock_init(&vcpu->arch.wdt_lock);
  1239. timer_setup(&vcpu->arch.wdt_timer, kvmppc_watchdog_func, 0);
  1240. /*
  1241. * Clear DBSR.MRR to avoid guest debug interrupt as
  1242. * this is of host interest
  1243. */
  1244. mtspr(SPRN_DBSR, DBSR_MRR);
  1245. return 0;
  1246. }
  1247. void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
  1248. {
  1249. del_timer_sync(&vcpu->arch.wdt_timer);
  1250. }
  1251. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  1252. {
  1253. int i;
  1254. vcpu_load(vcpu);
  1255. regs->pc = vcpu->arch.regs.nip;
  1256. regs->cr = kvmppc_get_cr(vcpu);
  1257. regs->ctr = vcpu->arch.regs.ctr;
  1258. regs->lr = vcpu->arch.regs.link;
  1259. regs->xer = kvmppc_get_xer(vcpu);
  1260. regs->msr = vcpu->arch.shared->msr;
  1261. regs->srr0 = kvmppc_get_srr0(vcpu);
  1262. regs->srr1 = kvmppc_get_srr1(vcpu);
  1263. regs->pid = vcpu->arch.pid;
  1264. regs->sprg0 = kvmppc_get_sprg0(vcpu);
  1265. regs->sprg1 = kvmppc_get_sprg1(vcpu);
  1266. regs->sprg2 = kvmppc_get_sprg2(vcpu);
  1267. regs->sprg3 = kvmppc_get_sprg3(vcpu);
  1268. regs->sprg4 = kvmppc_get_sprg4(vcpu);
  1269. regs->sprg5 = kvmppc_get_sprg5(vcpu);
  1270. regs->sprg6 = kvmppc_get_sprg6(vcpu);
  1271. regs->sprg7 = kvmppc_get_sprg7(vcpu);
  1272. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  1273. regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
  1274. vcpu_put(vcpu);
  1275. return 0;
  1276. }
  1277. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  1278. {
  1279. int i;
  1280. vcpu_load(vcpu);
  1281. vcpu->arch.regs.nip = regs->pc;
  1282. kvmppc_set_cr(vcpu, regs->cr);
  1283. vcpu->arch.regs.ctr = regs->ctr;
  1284. vcpu->arch.regs.link = regs->lr;
  1285. kvmppc_set_xer(vcpu, regs->xer);
  1286. kvmppc_set_msr(vcpu, regs->msr);
  1287. kvmppc_set_srr0(vcpu, regs->srr0);
  1288. kvmppc_set_srr1(vcpu, regs->srr1);
  1289. kvmppc_set_pid(vcpu, regs->pid);
  1290. kvmppc_set_sprg0(vcpu, regs->sprg0);
  1291. kvmppc_set_sprg1(vcpu, regs->sprg1);
  1292. kvmppc_set_sprg2(vcpu, regs->sprg2);
  1293. kvmppc_set_sprg3(vcpu, regs->sprg3);
  1294. kvmppc_set_sprg4(vcpu, regs->sprg4);
  1295. kvmppc_set_sprg5(vcpu, regs->sprg5);
  1296. kvmppc_set_sprg6(vcpu, regs->sprg6);
  1297. kvmppc_set_sprg7(vcpu, regs->sprg7);
  1298. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  1299. kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
  1300. vcpu_put(vcpu);
  1301. return 0;
  1302. }
  1303. static void get_sregs_base(struct kvm_vcpu *vcpu,
  1304. struct kvm_sregs *sregs)
  1305. {
  1306. u64 tb = get_tb();
  1307. sregs->u.e.features |= KVM_SREGS_E_BASE;
  1308. sregs->u.e.csrr0 = vcpu->arch.csrr0;
  1309. sregs->u.e.csrr1 = vcpu->arch.csrr1;
  1310. sregs->u.e.mcsr = vcpu->arch.mcsr;
  1311. sregs->u.e.esr = kvmppc_get_esr(vcpu);
  1312. sregs->u.e.dear = kvmppc_get_dar(vcpu);
  1313. sregs->u.e.tsr = vcpu->arch.tsr;
  1314. sregs->u.e.tcr = vcpu->arch.tcr;
  1315. sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
  1316. sregs->u.e.tb = tb;
  1317. sregs->u.e.vrsave = vcpu->arch.vrsave;
  1318. }
  1319. static int set_sregs_base(struct kvm_vcpu *vcpu,
  1320. struct kvm_sregs *sregs)
  1321. {
  1322. if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
  1323. return 0;
  1324. vcpu->arch.csrr0 = sregs->u.e.csrr0;
  1325. vcpu->arch.csrr1 = sregs->u.e.csrr1;
  1326. vcpu->arch.mcsr = sregs->u.e.mcsr;
  1327. kvmppc_set_esr(vcpu, sregs->u.e.esr);
  1328. kvmppc_set_dar(vcpu, sregs->u.e.dear);
  1329. vcpu->arch.vrsave = sregs->u.e.vrsave;
  1330. kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
  1331. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
  1332. vcpu->arch.dec = sregs->u.e.dec;
  1333. kvmppc_emulate_dec(vcpu);
  1334. }
  1335. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
  1336. kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
  1337. return 0;
  1338. }
  1339. static void get_sregs_arch206(struct kvm_vcpu *vcpu,
  1340. struct kvm_sregs *sregs)
  1341. {
  1342. sregs->u.e.features |= KVM_SREGS_E_ARCH206;
  1343. sregs->u.e.pir = vcpu->vcpu_id;
  1344. sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
  1345. sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
  1346. sregs->u.e.decar = vcpu->arch.decar;
  1347. sregs->u.e.ivpr = vcpu->arch.ivpr;
  1348. }
  1349. static int set_sregs_arch206(struct kvm_vcpu *vcpu,
  1350. struct kvm_sregs *sregs)
  1351. {
  1352. if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
  1353. return 0;
  1354. if (sregs->u.e.pir != vcpu->vcpu_id)
  1355. return -EINVAL;
  1356. vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
  1357. vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
  1358. vcpu->arch.decar = sregs->u.e.decar;
  1359. vcpu->arch.ivpr = sregs->u.e.ivpr;
  1360. return 0;
  1361. }
  1362. int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  1363. {
  1364. sregs->u.e.features |= KVM_SREGS_E_IVOR;
  1365. sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
  1366. sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
  1367. sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
  1368. sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
  1369. sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
  1370. sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
  1371. sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
  1372. sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
  1373. sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
  1374. sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
  1375. sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
  1376. sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
  1377. sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
  1378. sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
  1379. sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
  1380. sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
  1381. return 0;
  1382. }
  1383. int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  1384. {
  1385. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  1386. return 0;
  1387. vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
  1388. vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
  1389. vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
  1390. vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
  1391. vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
  1392. vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
  1393. vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
  1394. vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
  1395. vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
  1396. vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
  1397. vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
  1398. vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
  1399. vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
  1400. vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
  1401. vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
  1402. vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
  1403. return 0;
  1404. }
  1405. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1406. struct kvm_sregs *sregs)
  1407. {
  1408. int ret;
  1409. vcpu_load(vcpu);
  1410. sregs->pvr = vcpu->arch.pvr;
  1411. get_sregs_base(vcpu, sregs);
  1412. get_sregs_arch206(vcpu, sregs);
  1413. ret = vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
  1414. vcpu_put(vcpu);
  1415. return ret;
  1416. }
  1417. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1418. struct kvm_sregs *sregs)
  1419. {
  1420. int ret = -EINVAL;
  1421. vcpu_load(vcpu);
  1422. if (vcpu->arch.pvr != sregs->pvr)
  1423. goto out;
  1424. ret = set_sregs_base(vcpu, sregs);
  1425. if (ret < 0)
  1426. goto out;
  1427. ret = set_sregs_arch206(vcpu, sregs);
  1428. if (ret < 0)
  1429. goto out;
  1430. ret = vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
  1431. out:
  1432. vcpu_put(vcpu);
  1433. return ret;
  1434. }
  1435. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
  1436. union kvmppc_one_reg *val)
  1437. {
  1438. int r = 0;
  1439. switch (id) {
  1440. case KVM_REG_PPC_IAC1:
  1441. *val = get_reg_val(id, vcpu->arch.dbg_reg.iac1);
  1442. break;
  1443. case KVM_REG_PPC_IAC2:
  1444. *val = get_reg_val(id, vcpu->arch.dbg_reg.iac2);
  1445. break;
  1446. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  1447. case KVM_REG_PPC_IAC3:
  1448. *val = get_reg_val(id, vcpu->arch.dbg_reg.iac3);
  1449. break;
  1450. case KVM_REG_PPC_IAC4:
  1451. *val = get_reg_val(id, vcpu->arch.dbg_reg.iac4);
  1452. break;
  1453. #endif
  1454. case KVM_REG_PPC_DAC1:
  1455. *val = get_reg_val(id, vcpu->arch.dbg_reg.dac1);
  1456. break;
  1457. case KVM_REG_PPC_DAC2:
  1458. *val = get_reg_val(id, vcpu->arch.dbg_reg.dac2);
  1459. break;
  1460. case KVM_REG_PPC_EPR: {
  1461. u32 epr = kvmppc_get_epr(vcpu);
  1462. *val = get_reg_val(id, epr);
  1463. break;
  1464. }
  1465. #if defined(CONFIG_64BIT)
  1466. case KVM_REG_PPC_EPCR:
  1467. *val = get_reg_val(id, vcpu->arch.epcr);
  1468. break;
  1469. #endif
  1470. case KVM_REG_PPC_TCR:
  1471. *val = get_reg_val(id, vcpu->arch.tcr);
  1472. break;
  1473. case KVM_REG_PPC_TSR:
  1474. *val = get_reg_val(id, vcpu->arch.tsr);
  1475. break;
  1476. case KVM_REG_PPC_DEBUG_INST:
  1477. *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
  1478. break;
  1479. case KVM_REG_PPC_VRSAVE:
  1480. *val = get_reg_val(id, vcpu->arch.vrsave);
  1481. break;
  1482. default:
  1483. r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, id, val);
  1484. break;
  1485. }
  1486. return r;
  1487. }
  1488. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
  1489. union kvmppc_one_reg *val)
  1490. {
  1491. int r = 0;
  1492. switch (id) {
  1493. case KVM_REG_PPC_IAC1:
  1494. vcpu->arch.dbg_reg.iac1 = set_reg_val(id, *val);
  1495. break;
  1496. case KVM_REG_PPC_IAC2:
  1497. vcpu->arch.dbg_reg.iac2 = set_reg_val(id, *val);
  1498. break;
  1499. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  1500. case KVM_REG_PPC_IAC3:
  1501. vcpu->arch.dbg_reg.iac3 = set_reg_val(id, *val);
  1502. break;
  1503. case KVM_REG_PPC_IAC4:
  1504. vcpu->arch.dbg_reg.iac4 = set_reg_val(id, *val);
  1505. break;
  1506. #endif
  1507. case KVM_REG_PPC_DAC1:
  1508. vcpu->arch.dbg_reg.dac1 = set_reg_val(id, *val);
  1509. break;
  1510. case KVM_REG_PPC_DAC2:
  1511. vcpu->arch.dbg_reg.dac2 = set_reg_val(id, *val);
  1512. break;
  1513. case KVM_REG_PPC_EPR: {
  1514. u32 new_epr = set_reg_val(id, *val);
  1515. kvmppc_set_epr(vcpu, new_epr);
  1516. break;
  1517. }
  1518. #if defined(CONFIG_64BIT)
  1519. case KVM_REG_PPC_EPCR: {
  1520. u32 new_epcr = set_reg_val(id, *val);
  1521. kvmppc_set_epcr(vcpu, new_epcr);
  1522. break;
  1523. }
  1524. #endif
  1525. case KVM_REG_PPC_OR_TSR: {
  1526. u32 tsr_bits = set_reg_val(id, *val);
  1527. kvmppc_set_tsr_bits(vcpu, tsr_bits);
  1528. break;
  1529. }
  1530. case KVM_REG_PPC_CLEAR_TSR: {
  1531. u32 tsr_bits = set_reg_val(id, *val);
  1532. kvmppc_clr_tsr_bits(vcpu, tsr_bits);
  1533. break;
  1534. }
  1535. case KVM_REG_PPC_TSR: {
  1536. u32 tsr = set_reg_val(id, *val);
  1537. kvmppc_set_tsr(vcpu, tsr);
  1538. break;
  1539. }
  1540. case KVM_REG_PPC_TCR: {
  1541. u32 tcr = set_reg_val(id, *val);
  1542. kvmppc_set_tcr(vcpu, tcr);
  1543. break;
  1544. }
  1545. case KVM_REG_PPC_VRSAVE:
  1546. vcpu->arch.vrsave = set_reg_val(id, *val);
  1547. break;
  1548. default:
  1549. r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, id, val);
  1550. break;
  1551. }
  1552. return r;
  1553. }
  1554. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  1555. {
  1556. return -EOPNOTSUPP;
  1557. }
  1558. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  1559. {
  1560. return -EOPNOTSUPP;
  1561. }
  1562. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1563. struct kvm_translation *tr)
  1564. {
  1565. int r;
  1566. vcpu_load(vcpu);
  1567. r = kvmppc_core_vcpu_translate(vcpu, tr);
  1568. vcpu_put(vcpu);
  1569. return r;
  1570. }
  1571. void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  1572. {
  1573. }
  1574. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1575. {
  1576. return -EOPNOTSUPP;
  1577. }
  1578. void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
  1579. {
  1580. }
  1581. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1582. const struct kvm_memory_slot *old,
  1583. struct kvm_memory_slot *new,
  1584. enum kvm_mr_change change)
  1585. {
  1586. return 0;
  1587. }
  1588. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1589. struct kvm_memory_slot *old,
  1590. const struct kvm_memory_slot *new,
  1591. enum kvm_mr_change change)
  1592. {
  1593. }
  1594. void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
  1595. {
  1596. }
  1597. void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
  1598. {
  1599. #if defined(CONFIG_64BIT)
  1600. vcpu->arch.epcr = new_epcr;
  1601. #ifdef CONFIG_KVM_BOOKE_HV
  1602. vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
  1603. if (vcpu->arch.epcr & SPRN_EPCR_ICM)
  1604. vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
  1605. #endif
  1606. #endif
  1607. }
  1608. void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
  1609. {
  1610. vcpu->arch.tcr = new_tcr;
  1611. arm_next_watchdog(vcpu);
  1612. update_timer_ints(vcpu);
  1613. }
  1614. void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
  1615. {
  1616. set_bits(tsr_bits, &vcpu->arch.tsr);
  1617. smp_wmb();
  1618. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  1619. kvm_vcpu_kick(vcpu);
  1620. }
  1621. void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
  1622. {
  1623. clear_bits(tsr_bits, &vcpu->arch.tsr);
  1624. /*
  1625. * We may have stopped the watchdog due to
  1626. * being stuck on final expiration.
  1627. */
  1628. if (tsr_bits & (TSR_ENW | TSR_WIS))
  1629. arm_next_watchdog(vcpu);
  1630. update_timer_ints(vcpu);
  1631. }
  1632. void kvmppc_decrementer_func(struct kvm_vcpu *vcpu)
  1633. {
  1634. if (vcpu->arch.tcr & TCR_ARE) {
  1635. vcpu->arch.dec = vcpu->arch.decar;
  1636. kvmppc_emulate_dec(vcpu);
  1637. }
  1638. kvmppc_set_tsr_bits(vcpu, TSR_DIS);
  1639. }
  1640. static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
  1641. uint64_t addr, int index)
  1642. {
  1643. switch (index) {
  1644. case 0:
  1645. dbg_reg->dbcr0 |= DBCR0_IAC1;
  1646. dbg_reg->iac1 = addr;
  1647. break;
  1648. case 1:
  1649. dbg_reg->dbcr0 |= DBCR0_IAC2;
  1650. dbg_reg->iac2 = addr;
  1651. break;
  1652. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  1653. case 2:
  1654. dbg_reg->dbcr0 |= DBCR0_IAC3;
  1655. dbg_reg->iac3 = addr;
  1656. break;
  1657. case 3:
  1658. dbg_reg->dbcr0 |= DBCR0_IAC4;
  1659. dbg_reg->iac4 = addr;
  1660. break;
  1661. #endif
  1662. default:
  1663. return -EINVAL;
  1664. }
  1665. dbg_reg->dbcr0 |= DBCR0_IDM;
  1666. return 0;
  1667. }
  1668. static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
  1669. int type, int index)
  1670. {
  1671. switch (index) {
  1672. case 0:
  1673. if (type & KVMPPC_DEBUG_WATCH_READ)
  1674. dbg_reg->dbcr0 |= DBCR0_DAC1R;
  1675. if (type & KVMPPC_DEBUG_WATCH_WRITE)
  1676. dbg_reg->dbcr0 |= DBCR0_DAC1W;
  1677. dbg_reg->dac1 = addr;
  1678. break;
  1679. case 1:
  1680. if (type & KVMPPC_DEBUG_WATCH_READ)
  1681. dbg_reg->dbcr0 |= DBCR0_DAC2R;
  1682. if (type & KVMPPC_DEBUG_WATCH_WRITE)
  1683. dbg_reg->dbcr0 |= DBCR0_DAC2W;
  1684. dbg_reg->dac2 = addr;
  1685. break;
  1686. default:
  1687. return -EINVAL;
  1688. }
  1689. dbg_reg->dbcr0 |= DBCR0_IDM;
  1690. return 0;
  1691. }
  1692. static void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap,
  1693. bool set)
  1694. {
  1695. /* XXX: Add similar MSR protection for BookE-PR */
  1696. #ifdef CONFIG_KVM_BOOKE_HV
  1697. BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
  1698. if (set) {
  1699. if (prot_bitmap & MSR_UCLE)
  1700. vcpu->arch.shadow_msrp |= MSRP_UCLEP;
  1701. if (prot_bitmap & MSR_DE)
  1702. vcpu->arch.shadow_msrp |= MSRP_DEP;
  1703. if (prot_bitmap & MSR_PMM)
  1704. vcpu->arch.shadow_msrp |= MSRP_PMMP;
  1705. } else {
  1706. if (prot_bitmap & MSR_UCLE)
  1707. vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
  1708. if (prot_bitmap & MSR_DE)
  1709. vcpu->arch.shadow_msrp &= ~MSRP_DEP;
  1710. if (prot_bitmap & MSR_PMM)
  1711. vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
  1712. }
  1713. #endif
  1714. }
  1715. int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, enum xlate_instdata xlid,
  1716. enum xlate_readwrite xlrw, struct kvmppc_pte *pte)
  1717. {
  1718. int gtlb_index;
  1719. gpa_t gpaddr;
  1720. #ifdef CONFIG_KVM_E500V2
  1721. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  1722. (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
  1723. pte->eaddr = eaddr;
  1724. pte->raddr = (vcpu->arch.magic_page_pa & PAGE_MASK) |
  1725. (eaddr & ~PAGE_MASK);
  1726. pte->vpage = eaddr >> PAGE_SHIFT;
  1727. pte->may_read = true;
  1728. pte->may_write = true;
  1729. pte->may_execute = true;
  1730. return 0;
  1731. }
  1732. #endif
  1733. /* Check the guest TLB. */
  1734. switch (xlid) {
  1735. case XLATE_INST:
  1736. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  1737. break;
  1738. case XLATE_DATA:
  1739. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  1740. break;
  1741. default:
  1742. BUG();
  1743. }
  1744. /* Do we have a TLB entry at all? */
  1745. if (gtlb_index < 0)
  1746. return -ENOENT;
  1747. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  1748. pte->eaddr = eaddr;
  1749. pte->raddr = (gpaddr & PAGE_MASK) | (eaddr & ~PAGE_MASK);
  1750. pte->vpage = eaddr >> PAGE_SHIFT;
  1751. /* XXX read permissions from the guest TLB */
  1752. pte->may_read = true;
  1753. pte->may_write = true;
  1754. pte->may_execute = true;
  1755. return 0;
  1756. }
  1757. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  1758. struct kvm_guest_debug *dbg)
  1759. {
  1760. struct debug_reg *dbg_reg;
  1761. int n, b = 0, w = 0;
  1762. int ret = 0;
  1763. vcpu_load(vcpu);
  1764. if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
  1765. vcpu->arch.dbg_reg.dbcr0 = 0;
  1766. vcpu->guest_debug = 0;
  1767. kvm_guest_protect_msr(vcpu, MSR_DE, false);
  1768. goto out;
  1769. }
  1770. kvm_guest_protect_msr(vcpu, MSR_DE, true);
  1771. vcpu->guest_debug = dbg->control;
  1772. vcpu->arch.dbg_reg.dbcr0 = 0;
  1773. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  1774. vcpu->arch.dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  1775. /* Code below handles only HW breakpoints */
  1776. dbg_reg = &(vcpu->arch.dbg_reg);
  1777. #ifdef CONFIG_KVM_BOOKE_HV
  1778. /*
  1779. * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
  1780. * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
  1781. */
  1782. dbg_reg->dbcr1 = 0;
  1783. dbg_reg->dbcr2 = 0;
  1784. #else
  1785. /*
  1786. * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
  1787. * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
  1788. * is set.
  1789. */
  1790. dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
  1791. DBCR1_IAC4US;
  1792. dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
  1793. #endif
  1794. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
  1795. goto out;
  1796. ret = -EINVAL;
  1797. for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
  1798. uint64_t addr = dbg->arch.bp[n].addr;
  1799. uint32_t type = dbg->arch.bp[n].type;
  1800. if (type == KVMPPC_DEBUG_NONE)
  1801. continue;
  1802. if (type & ~(KVMPPC_DEBUG_WATCH_READ |
  1803. KVMPPC_DEBUG_WATCH_WRITE |
  1804. KVMPPC_DEBUG_BREAKPOINT))
  1805. goto out;
  1806. if (type & KVMPPC_DEBUG_BREAKPOINT) {
  1807. /* Setting H/W breakpoint */
  1808. if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
  1809. goto out;
  1810. } else {
  1811. /* Setting H/W watchpoint */
  1812. if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
  1813. type, w++))
  1814. goto out;
  1815. }
  1816. }
  1817. ret = 0;
  1818. out:
  1819. vcpu_put(vcpu);
  1820. return ret;
  1821. }
  1822. void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1823. {
  1824. vcpu->cpu = smp_processor_id();
  1825. current->thread.kvm_vcpu = vcpu;
  1826. }
  1827. void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
  1828. {
  1829. current->thread.kvm_vcpu = NULL;
  1830. vcpu->cpu = -1;
  1831. /* Clear pending debug event in DBSR */
  1832. kvmppc_clear_dbsr();
  1833. }
  1834. int kvmppc_core_init_vm(struct kvm *kvm)
  1835. {
  1836. return kvm->arch.kvm_ops->init_vm(kvm);
  1837. }
  1838. int kvmppc_core_vcpu_create(struct kvm_vcpu *vcpu)
  1839. {
  1840. int i;
  1841. int r;
  1842. r = vcpu->kvm->arch.kvm_ops->vcpu_create(vcpu);
  1843. if (r)
  1844. return r;
  1845. /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
  1846. vcpu->arch.regs.nip = 0;
  1847. vcpu->arch.shared->pir = vcpu->vcpu_id;
  1848. kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
  1849. kvmppc_set_msr(vcpu, 0);
  1850. #ifndef CONFIG_KVM_BOOKE_HV
  1851. vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
  1852. vcpu->arch.shadow_pid = 1;
  1853. vcpu->arch.shared->msr = 0;
  1854. #endif
  1855. /* Eye-catching numbers so we know if the guest takes an interrupt
  1856. * before it's programmed its own IVPR/IVORs. */
  1857. vcpu->arch.ivpr = 0x55550000;
  1858. for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
  1859. vcpu->arch.ivor[i] = 0x7700 | i * 4;
  1860. kvmppc_init_timing_stats(vcpu);
  1861. r = kvmppc_core_vcpu_setup(vcpu);
  1862. if (r)
  1863. vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
  1864. kvmppc_sanity_check(vcpu);
  1865. return r;
  1866. }
  1867. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  1868. {
  1869. vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
  1870. }
  1871. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1872. {
  1873. kvm->arch.kvm_ops->destroy_vm(kvm);
  1874. }
  1875. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1876. {
  1877. vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
  1878. }
  1879. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  1880. {
  1881. vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
  1882. }
  1883. int __init kvmppc_booke_init(void)
  1884. {
  1885. #ifndef CONFIG_KVM_BOOKE_HV
  1886. unsigned long ivor[16];
  1887. unsigned long *handler = kvmppc_booke_handler_addr;
  1888. unsigned long max_ivor = 0;
  1889. unsigned long handler_len;
  1890. int i;
  1891. /* We install our own exception handlers by hijacking IVPR. IVPR must
  1892. * be 16-bit aligned, so we need a 64KB allocation. */
  1893. kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1894. VCPU_SIZE_ORDER);
  1895. if (!kvmppc_booke_handlers)
  1896. return -ENOMEM;
  1897. /* XXX make sure our handlers are smaller than Linux's */
  1898. /* Copy our interrupt handlers to match host IVORs. That way we don't
  1899. * have to swap the IVORs on every guest/host transition. */
  1900. ivor[0] = mfspr(SPRN_IVOR0);
  1901. ivor[1] = mfspr(SPRN_IVOR1);
  1902. ivor[2] = mfspr(SPRN_IVOR2);
  1903. ivor[3] = mfspr(SPRN_IVOR3);
  1904. ivor[4] = mfspr(SPRN_IVOR4);
  1905. ivor[5] = mfspr(SPRN_IVOR5);
  1906. ivor[6] = mfspr(SPRN_IVOR6);
  1907. ivor[7] = mfspr(SPRN_IVOR7);
  1908. ivor[8] = mfspr(SPRN_IVOR8);
  1909. ivor[9] = mfspr(SPRN_IVOR9);
  1910. ivor[10] = mfspr(SPRN_IVOR10);
  1911. ivor[11] = mfspr(SPRN_IVOR11);
  1912. ivor[12] = mfspr(SPRN_IVOR12);
  1913. ivor[13] = mfspr(SPRN_IVOR13);
  1914. ivor[14] = mfspr(SPRN_IVOR14);
  1915. ivor[15] = mfspr(SPRN_IVOR15);
  1916. for (i = 0; i < 16; i++) {
  1917. if (ivor[i] > max_ivor)
  1918. max_ivor = i;
  1919. handler_len = handler[i + 1] - handler[i];
  1920. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  1921. (void *)handler[i], handler_len);
  1922. }
  1923. handler_len = handler[max_ivor + 1] - handler[max_ivor];
  1924. flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
  1925. ivor[max_ivor] + handler_len);
  1926. #endif /* !BOOKE_HV */
  1927. return 0;
  1928. }
  1929. void __exit kvmppc_booke_exit(void)
  1930. {
  1931. free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
  1932. kvm_exit();
  1933. }