booke.c 56 KB

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