booke.c 56 KB

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