booke.c 55 KB

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