booke.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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 "timing.h"
  38. #include "booke.h"
  39. #include "trace.h"
  40. unsigned long kvmppc_booke_handlers;
  41. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  42. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  43. struct kvm_stats_debugfs_item debugfs_entries[] = {
  44. { "mmio", VCPU_STAT(mmio_exits) },
  45. { "dcr", VCPU_STAT(dcr_exits) },
  46. { "sig", VCPU_STAT(signal_exits) },
  47. { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
  48. { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
  49. { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
  50. { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
  51. { "sysc", VCPU_STAT(syscall_exits) },
  52. { "isi", VCPU_STAT(isi_exits) },
  53. { "dsi", VCPU_STAT(dsi_exits) },
  54. { "inst_emu", VCPU_STAT(emulated_inst_exits) },
  55. { "dec", VCPU_STAT(dec_exits) },
  56. { "ext_intr", VCPU_STAT(ext_intr_exits) },
  57. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  58. { "doorbell", VCPU_STAT(dbell_exits) },
  59. { "guest doorbell", VCPU_STAT(gdbell_exits) },
  60. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  61. { NULL }
  62. };
  63. /* TODO: use vcpu_printf() */
  64. void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
  65. {
  66. int i;
  67. printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
  68. printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
  69. printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
  70. vcpu->arch.shared->srr1);
  71. printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
  72. for (i = 0; i < 32; i += 4) {
  73. printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
  74. kvmppc_get_gpr(vcpu, i),
  75. kvmppc_get_gpr(vcpu, i+1),
  76. kvmppc_get_gpr(vcpu, i+2),
  77. kvmppc_get_gpr(vcpu, i+3));
  78. }
  79. }
  80. #ifdef CONFIG_SPE
  81. void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
  82. {
  83. preempt_disable();
  84. enable_kernel_spe();
  85. kvmppc_save_guest_spe(vcpu);
  86. vcpu->arch.shadow_msr &= ~MSR_SPE;
  87. preempt_enable();
  88. }
  89. static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
  90. {
  91. preempt_disable();
  92. enable_kernel_spe();
  93. kvmppc_load_guest_spe(vcpu);
  94. vcpu->arch.shadow_msr |= MSR_SPE;
  95. preempt_enable();
  96. }
  97. static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
  98. {
  99. if (vcpu->arch.shared->msr & MSR_SPE) {
  100. if (!(vcpu->arch.shadow_msr & MSR_SPE))
  101. kvmppc_vcpu_enable_spe(vcpu);
  102. } else if (vcpu->arch.shadow_msr & MSR_SPE) {
  103. kvmppc_vcpu_disable_spe(vcpu);
  104. }
  105. }
  106. #else
  107. static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
  108. {
  109. }
  110. #endif
  111. /*
  112. * Helper function for "full" MSR writes. No need to call this if only
  113. * EE/CE/ME/DE/RI are changing.
  114. */
  115. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
  116. {
  117. u32 old_msr = vcpu->arch.shared->msr;
  118. #ifdef CONFIG_KVM_BOOKE_HV
  119. new_msr |= MSR_GS;
  120. #endif
  121. vcpu->arch.shared->msr = new_msr;
  122. kvmppc_mmu_msr_notify(vcpu, old_msr);
  123. kvmppc_vcpu_sync_spe(vcpu);
  124. }
  125. static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
  126. unsigned int priority)
  127. {
  128. trace_kvm_booke_queue_irqprio(vcpu, priority);
  129. set_bit(priority, &vcpu->arch.pending_exceptions);
  130. }
  131. static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
  132. ulong dear_flags, ulong esr_flags)
  133. {
  134. vcpu->arch.queued_dear = dear_flags;
  135. vcpu->arch.queued_esr = esr_flags;
  136. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
  137. }
  138. static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
  139. ulong dear_flags, ulong esr_flags)
  140. {
  141. vcpu->arch.queued_dear = dear_flags;
  142. vcpu->arch.queued_esr = esr_flags;
  143. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
  144. }
  145. static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
  146. ulong esr_flags)
  147. {
  148. vcpu->arch.queued_esr = esr_flags;
  149. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
  150. }
  151. void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
  152. {
  153. vcpu->arch.queued_esr = esr_flags;
  154. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
  155. }
  156. void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
  157. {
  158. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
  159. }
  160. int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
  161. {
  162. return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  163. }
  164. void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
  165. {
  166. clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  167. }
  168. void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
  169. struct kvm_interrupt *irq)
  170. {
  171. unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
  172. if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
  173. prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
  174. kvmppc_booke_queue_irqprio(vcpu, prio);
  175. }
  176. void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
  177. struct kvm_interrupt *irq)
  178. {
  179. clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
  180. clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
  181. }
  182. static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  183. {
  184. #ifdef CONFIG_KVM_BOOKE_HV
  185. mtspr(SPRN_GSRR0, srr0);
  186. mtspr(SPRN_GSRR1, srr1);
  187. #else
  188. vcpu->arch.shared->srr0 = srr0;
  189. vcpu->arch.shared->srr1 = srr1;
  190. #endif
  191. }
  192. static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  193. {
  194. vcpu->arch.csrr0 = srr0;
  195. vcpu->arch.csrr1 = srr1;
  196. }
  197. static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  198. {
  199. if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
  200. vcpu->arch.dsrr0 = srr0;
  201. vcpu->arch.dsrr1 = srr1;
  202. } else {
  203. set_guest_csrr(vcpu, srr0, srr1);
  204. }
  205. }
  206. static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  207. {
  208. vcpu->arch.mcsrr0 = srr0;
  209. vcpu->arch.mcsrr1 = srr1;
  210. }
  211. static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
  212. {
  213. #ifdef CONFIG_KVM_BOOKE_HV
  214. return mfspr(SPRN_GDEAR);
  215. #else
  216. return vcpu->arch.shared->dar;
  217. #endif
  218. }
  219. static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
  220. {
  221. #ifdef CONFIG_KVM_BOOKE_HV
  222. mtspr(SPRN_GDEAR, dear);
  223. #else
  224. vcpu->arch.shared->dar = dear;
  225. #endif
  226. }
  227. static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
  228. {
  229. #ifdef CONFIG_KVM_BOOKE_HV
  230. return mfspr(SPRN_GESR);
  231. #else
  232. return vcpu->arch.shared->esr;
  233. #endif
  234. }
  235. static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
  236. {
  237. #ifdef CONFIG_KVM_BOOKE_HV
  238. mtspr(SPRN_GESR, esr);
  239. #else
  240. vcpu->arch.shared->esr = esr;
  241. #endif
  242. }
  243. /* Deliver the interrupt of the corresponding priority, if possible. */
  244. static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
  245. unsigned int priority)
  246. {
  247. int allowed = 0;
  248. ulong msr_mask = 0;
  249. bool update_esr = false, update_dear = false;
  250. ulong crit_raw = vcpu->arch.shared->critical;
  251. ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
  252. bool crit;
  253. bool keep_irq = false;
  254. enum int_class int_class;
  255. /* Truncate crit indicators in 32 bit mode */
  256. if (!(vcpu->arch.shared->msr & MSR_SF)) {
  257. crit_raw &= 0xffffffff;
  258. crit_r1 &= 0xffffffff;
  259. }
  260. /* Critical section when crit == r1 */
  261. crit = (crit_raw == crit_r1);
  262. /* ... and we're in supervisor mode */
  263. crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
  264. if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
  265. priority = BOOKE_IRQPRIO_EXTERNAL;
  266. keep_irq = true;
  267. }
  268. switch (priority) {
  269. case BOOKE_IRQPRIO_DTLB_MISS:
  270. case BOOKE_IRQPRIO_DATA_STORAGE:
  271. update_dear = true;
  272. /* fall through */
  273. case BOOKE_IRQPRIO_INST_STORAGE:
  274. case BOOKE_IRQPRIO_PROGRAM:
  275. update_esr = true;
  276. /* fall through */
  277. case BOOKE_IRQPRIO_ITLB_MISS:
  278. case BOOKE_IRQPRIO_SYSCALL:
  279. case BOOKE_IRQPRIO_FP_UNAVAIL:
  280. case BOOKE_IRQPRIO_SPE_UNAVAIL:
  281. case BOOKE_IRQPRIO_SPE_FP_DATA:
  282. case BOOKE_IRQPRIO_SPE_FP_ROUND:
  283. case BOOKE_IRQPRIO_AP_UNAVAIL:
  284. case BOOKE_IRQPRIO_ALIGNMENT:
  285. allowed = 1;
  286. msr_mask = MSR_CE | MSR_ME | MSR_DE;
  287. int_class = INT_CLASS_NONCRIT;
  288. break;
  289. case BOOKE_IRQPRIO_CRITICAL:
  290. case BOOKE_IRQPRIO_DBELL_CRIT:
  291. allowed = vcpu->arch.shared->msr & MSR_CE;
  292. allowed = allowed && !crit;
  293. msr_mask = MSR_ME;
  294. int_class = INT_CLASS_CRIT;
  295. break;
  296. case BOOKE_IRQPRIO_MACHINE_CHECK:
  297. allowed = vcpu->arch.shared->msr & MSR_ME;
  298. allowed = allowed && !crit;
  299. int_class = INT_CLASS_MC;
  300. break;
  301. case BOOKE_IRQPRIO_DECREMENTER:
  302. case BOOKE_IRQPRIO_FIT:
  303. keep_irq = true;
  304. /* fall through */
  305. case BOOKE_IRQPRIO_EXTERNAL:
  306. case BOOKE_IRQPRIO_DBELL:
  307. allowed = vcpu->arch.shared->msr & MSR_EE;
  308. allowed = allowed && !crit;
  309. msr_mask = MSR_CE | MSR_ME | MSR_DE;
  310. int_class = INT_CLASS_NONCRIT;
  311. break;
  312. case BOOKE_IRQPRIO_DEBUG:
  313. allowed = vcpu->arch.shared->msr & MSR_DE;
  314. allowed = allowed && !crit;
  315. msr_mask = MSR_ME;
  316. int_class = INT_CLASS_CRIT;
  317. break;
  318. }
  319. if (allowed) {
  320. switch (int_class) {
  321. case INT_CLASS_NONCRIT:
  322. set_guest_srr(vcpu, vcpu->arch.pc,
  323. vcpu->arch.shared->msr);
  324. break;
  325. case INT_CLASS_CRIT:
  326. set_guest_csrr(vcpu, vcpu->arch.pc,
  327. vcpu->arch.shared->msr);
  328. break;
  329. case INT_CLASS_DBG:
  330. set_guest_dsrr(vcpu, vcpu->arch.pc,
  331. vcpu->arch.shared->msr);
  332. break;
  333. case INT_CLASS_MC:
  334. set_guest_mcsrr(vcpu, vcpu->arch.pc,
  335. vcpu->arch.shared->msr);
  336. break;
  337. }
  338. vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
  339. if (update_esr == true)
  340. set_guest_esr(vcpu, vcpu->arch.queued_esr);
  341. if (update_dear == true)
  342. set_guest_dear(vcpu, vcpu->arch.queued_dear);
  343. kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
  344. if (!keep_irq)
  345. clear_bit(priority, &vcpu->arch.pending_exceptions);
  346. }
  347. #ifdef CONFIG_KVM_BOOKE_HV
  348. /*
  349. * If an interrupt is pending but masked, raise a guest doorbell
  350. * so that we are notified when the guest enables the relevant
  351. * MSR bit.
  352. */
  353. if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
  354. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
  355. if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
  356. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
  357. if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
  358. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
  359. #endif
  360. return allowed;
  361. }
  362. static void update_timer_ints(struct kvm_vcpu *vcpu)
  363. {
  364. if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
  365. kvmppc_core_queue_dec(vcpu);
  366. else
  367. kvmppc_core_dequeue_dec(vcpu);
  368. }
  369. static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
  370. {
  371. unsigned long *pending = &vcpu->arch.pending_exceptions;
  372. unsigned int priority;
  373. priority = __ffs(*pending);
  374. while (priority < BOOKE_IRQPRIO_MAX) {
  375. if (kvmppc_booke_irqprio_deliver(vcpu, priority))
  376. break;
  377. priority = find_next_bit(pending,
  378. BITS_PER_BYTE * sizeof(*pending),
  379. priority + 1);
  380. }
  381. /* Tell the guest about our interrupt status */
  382. vcpu->arch.shared->int_pending = !!*pending;
  383. }
  384. /* Check pending exceptions and deliver one, if possible. */
  385. int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
  386. {
  387. int r = 0;
  388. WARN_ON_ONCE(!irqs_disabled());
  389. kvmppc_core_check_exceptions(vcpu);
  390. if (vcpu->arch.shared->msr & MSR_WE) {
  391. local_irq_enable();
  392. kvm_vcpu_block(vcpu);
  393. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  394. local_irq_disable();
  395. kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
  396. r = 1;
  397. };
  398. return r;
  399. }
  400. static void kvmppc_check_requests(struct kvm_vcpu *vcpu)
  401. {
  402. if (vcpu->requests) {
  403. trace_kvm_check_requests(vcpu);
  404. if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
  405. update_timer_ints(vcpu);
  406. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  407. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
  408. kvmppc_core_flush_tlb(vcpu);
  409. #endif
  410. }
  411. }
  412. /*
  413. * Common checks before entering the guest world. Call with interrupts
  414. * disabled.
  415. *
  416. * returns !0 if a signal is pending and check_signal is true
  417. */
  418. static int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
  419. {
  420. int r = 0;
  421. WARN_ON_ONCE(!irqs_disabled());
  422. while (true) {
  423. if (need_resched()) {
  424. local_irq_enable();
  425. cond_resched();
  426. local_irq_disable();
  427. continue;
  428. }
  429. if (signal_pending(current)) {
  430. r = 1;
  431. break;
  432. }
  433. smp_mb();
  434. if (vcpu->requests) {
  435. /* Make sure we process requests preemptable */
  436. local_irq_enable();
  437. kvmppc_check_requests(vcpu);
  438. local_irq_disable();
  439. continue;
  440. }
  441. if (kvmppc_core_prepare_to_enter(vcpu)) {
  442. /* interrupts got enabled in between, so we
  443. are back at square 1 */
  444. continue;
  445. }
  446. if (vcpu->mode == EXITING_GUEST_MODE) {
  447. r = 1;
  448. break;
  449. }
  450. /* Going into guest context! Yay! */
  451. vcpu->mode = IN_GUEST_MODE;
  452. smp_wmb();
  453. break;
  454. }
  455. return r;
  456. }
  457. int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  458. {
  459. int ret;
  460. #ifdef CONFIG_PPC_FPU
  461. unsigned int fpscr;
  462. int fpexc_mode;
  463. u64 fpr[32];
  464. #endif
  465. if (!vcpu->arch.sane) {
  466. kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  467. return -EINVAL;
  468. }
  469. local_irq_disable();
  470. if (kvmppc_prepare_to_enter(vcpu)) {
  471. kvm_run->exit_reason = KVM_EXIT_INTR;
  472. ret = -EINTR;
  473. goto out;
  474. }
  475. kvm_guest_enter();
  476. #ifdef CONFIG_PPC_FPU
  477. /* Save userspace FPU state in stack */
  478. enable_kernel_fp();
  479. memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
  480. fpscr = current->thread.fpscr.val;
  481. fpexc_mode = current->thread.fpexc_mode;
  482. /* Restore guest FPU state to thread */
  483. memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
  484. current->thread.fpscr.val = vcpu->arch.fpscr;
  485. /*
  486. * Since we can't trap on MSR_FP in GS-mode, we consider the guest
  487. * as always using the FPU. Kernel usage of FP (via
  488. * enable_kernel_fp()) in this thread must not occur while
  489. * vcpu->fpu_active is set.
  490. */
  491. vcpu->fpu_active = 1;
  492. kvmppc_load_guest_fp(vcpu);
  493. #endif
  494. ret = __kvmppc_vcpu_run(kvm_run, vcpu);
  495. #ifdef CONFIG_PPC_FPU
  496. kvmppc_save_guest_fp(vcpu);
  497. vcpu->fpu_active = 0;
  498. /* Save guest FPU state from thread */
  499. memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
  500. vcpu->arch.fpscr = current->thread.fpscr.val;
  501. /* Restore userspace FPU state from stack */
  502. memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
  503. current->thread.fpscr.val = fpscr;
  504. current->thread.fpexc_mode = fpexc_mode;
  505. #endif
  506. kvm_guest_exit();
  507. vcpu->mode = OUTSIDE_GUEST_MODE;
  508. smp_wmb();
  509. out:
  510. vcpu->mode = OUTSIDE_GUEST_MODE;
  511. smp_wmb();
  512. local_irq_enable();
  513. return ret;
  514. }
  515. static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
  516. {
  517. enum emulation_result er;
  518. er = kvmppc_emulate_instruction(run, vcpu);
  519. switch (er) {
  520. case EMULATE_DONE:
  521. /* don't overwrite subtypes, just account kvm_stats */
  522. kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
  523. /* Future optimization: only reload non-volatiles if
  524. * they were actually modified by emulation. */
  525. return RESUME_GUEST_NV;
  526. case EMULATE_DO_DCR:
  527. run->exit_reason = KVM_EXIT_DCR;
  528. return RESUME_HOST;
  529. case EMULATE_FAIL:
  530. printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
  531. __func__, vcpu->arch.pc, vcpu->arch.last_inst);
  532. /* For debugging, encode the failing instruction and
  533. * report it to userspace. */
  534. run->hw.hardware_exit_reason = ~0ULL << 32;
  535. run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
  536. kvmppc_core_queue_program(vcpu, ESR_PIL);
  537. return RESUME_HOST;
  538. default:
  539. BUG();
  540. }
  541. }
  542. static void kvmppc_fill_pt_regs(struct pt_regs *regs)
  543. {
  544. ulong r1, ip, msr, lr;
  545. asm("mr %0, 1" : "=r"(r1));
  546. asm("mflr %0" : "=r"(lr));
  547. asm("mfmsr %0" : "=r"(msr));
  548. asm("bl 1f; 1: mflr %0" : "=r"(ip));
  549. memset(regs, 0, sizeof(*regs));
  550. regs->gpr[1] = r1;
  551. regs->nip = ip;
  552. regs->msr = msr;
  553. regs->link = lr;
  554. }
  555. /*
  556. * For interrupts needed to be handled by host interrupt handlers,
  557. * corresponding host handler are called from here in similar way
  558. * (but not exact) as they are called from low level handler
  559. * (such as from arch/powerpc/kernel/head_fsl_booke.S).
  560. */
  561. static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
  562. unsigned int exit_nr)
  563. {
  564. struct pt_regs regs;
  565. switch (exit_nr) {
  566. case BOOKE_INTERRUPT_EXTERNAL:
  567. kvmppc_fill_pt_regs(&regs);
  568. do_IRQ(&regs);
  569. break;
  570. case BOOKE_INTERRUPT_DECREMENTER:
  571. kvmppc_fill_pt_regs(&regs);
  572. timer_interrupt(&regs);
  573. break;
  574. #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
  575. case BOOKE_INTERRUPT_DOORBELL:
  576. kvmppc_fill_pt_regs(&regs);
  577. doorbell_exception(&regs);
  578. break;
  579. #endif
  580. case BOOKE_INTERRUPT_MACHINE_CHECK:
  581. /* FIXME */
  582. break;
  583. case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
  584. kvmppc_fill_pt_regs(&regs);
  585. performance_monitor_exception(&regs);
  586. break;
  587. case BOOKE_INTERRUPT_WATCHDOG:
  588. kvmppc_fill_pt_regs(&regs);
  589. #ifdef CONFIG_BOOKE_WDT
  590. WatchdogException(&regs);
  591. #else
  592. unknown_exception(&regs);
  593. #endif
  594. break;
  595. case BOOKE_INTERRUPT_CRITICAL:
  596. unknown_exception(&regs);
  597. break;
  598. }
  599. }
  600. /**
  601. * kvmppc_handle_exit
  602. *
  603. * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
  604. */
  605. int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  606. unsigned int exit_nr)
  607. {
  608. int r = RESUME_HOST;
  609. /* update before a new last_exit_type is rewritten */
  610. kvmppc_update_timing_stats(vcpu);
  611. /* restart interrupts if they were meant for the host */
  612. kvmppc_restart_interrupt(vcpu, exit_nr);
  613. local_irq_enable();
  614. trace_kvm_exit(exit_nr, vcpu);
  615. run->exit_reason = KVM_EXIT_UNKNOWN;
  616. run->ready_for_interrupt_injection = 1;
  617. switch (exit_nr) {
  618. case BOOKE_INTERRUPT_MACHINE_CHECK:
  619. printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
  620. kvmppc_dump_vcpu(vcpu);
  621. /* For debugging, send invalid exit reason to user space */
  622. run->hw.hardware_exit_reason = ~1ULL << 32;
  623. run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
  624. r = RESUME_HOST;
  625. break;
  626. case BOOKE_INTERRUPT_EXTERNAL:
  627. kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
  628. r = RESUME_GUEST;
  629. break;
  630. case BOOKE_INTERRUPT_DECREMENTER:
  631. kvmppc_account_exit(vcpu, DEC_EXITS);
  632. r = RESUME_GUEST;
  633. break;
  634. case BOOKE_INTERRUPT_WATCHDOG:
  635. r = RESUME_GUEST;
  636. break;
  637. case BOOKE_INTERRUPT_DOORBELL:
  638. kvmppc_account_exit(vcpu, DBELL_EXITS);
  639. r = RESUME_GUEST;
  640. break;
  641. case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
  642. kvmppc_account_exit(vcpu, GDBELL_EXITS);
  643. /*
  644. * We are here because there is a pending guest interrupt
  645. * which could not be delivered as MSR_CE or MSR_ME was not
  646. * set. Once we break from here we will retry delivery.
  647. */
  648. r = RESUME_GUEST;
  649. break;
  650. case BOOKE_INTERRUPT_GUEST_DBELL:
  651. kvmppc_account_exit(vcpu, GDBELL_EXITS);
  652. /*
  653. * We are here because there is a pending guest interrupt
  654. * which could not be delivered as MSR_EE was not set. Once
  655. * we break from here we will retry delivery.
  656. */
  657. r = RESUME_GUEST;
  658. break;
  659. case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
  660. r = RESUME_GUEST;
  661. break;
  662. case BOOKE_INTERRUPT_HV_PRIV:
  663. r = emulation_exit(run, vcpu);
  664. break;
  665. case BOOKE_INTERRUPT_PROGRAM:
  666. if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
  667. /*
  668. * Program traps generated by user-level software must
  669. * be handled by the guest kernel.
  670. *
  671. * In GS mode, hypervisor privileged instructions trap
  672. * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
  673. * actual program interrupts, handled by the guest.
  674. */
  675. kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
  676. r = RESUME_GUEST;
  677. kvmppc_account_exit(vcpu, USR_PR_INST);
  678. break;
  679. }
  680. r = emulation_exit(run, vcpu);
  681. break;
  682. case BOOKE_INTERRUPT_FP_UNAVAIL:
  683. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
  684. kvmppc_account_exit(vcpu, FP_UNAVAIL);
  685. r = RESUME_GUEST;
  686. break;
  687. #ifdef CONFIG_SPE
  688. case BOOKE_INTERRUPT_SPE_UNAVAIL: {
  689. if (vcpu->arch.shared->msr & MSR_SPE)
  690. kvmppc_vcpu_enable_spe(vcpu);
  691. else
  692. kvmppc_booke_queue_irqprio(vcpu,
  693. BOOKE_IRQPRIO_SPE_UNAVAIL);
  694. r = RESUME_GUEST;
  695. break;
  696. }
  697. case BOOKE_INTERRUPT_SPE_FP_DATA:
  698. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
  699. r = RESUME_GUEST;
  700. break;
  701. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  702. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
  703. r = RESUME_GUEST;
  704. break;
  705. #else
  706. case BOOKE_INTERRUPT_SPE_UNAVAIL:
  707. /*
  708. * Guest wants SPE, but host kernel doesn't support it. Send
  709. * an "unimplemented operation" program check to the guest.
  710. */
  711. kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
  712. r = RESUME_GUEST;
  713. break;
  714. /*
  715. * These really should never happen without CONFIG_SPE,
  716. * as we should never enable the real MSR[SPE] in the guest.
  717. */
  718. case BOOKE_INTERRUPT_SPE_FP_DATA:
  719. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  720. printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
  721. __func__, exit_nr, vcpu->arch.pc);
  722. run->hw.hardware_exit_reason = exit_nr;
  723. r = RESUME_HOST;
  724. break;
  725. #endif
  726. case BOOKE_INTERRUPT_DATA_STORAGE:
  727. kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
  728. vcpu->arch.fault_esr);
  729. kvmppc_account_exit(vcpu, DSI_EXITS);
  730. r = RESUME_GUEST;
  731. break;
  732. case BOOKE_INTERRUPT_INST_STORAGE:
  733. kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
  734. kvmppc_account_exit(vcpu, ISI_EXITS);
  735. r = RESUME_GUEST;
  736. break;
  737. #ifdef CONFIG_KVM_BOOKE_HV
  738. case BOOKE_INTERRUPT_HV_SYSCALL:
  739. if (!(vcpu->arch.shared->msr & MSR_PR)) {
  740. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  741. } else {
  742. /*
  743. * hcall from guest userspace -- send privileged
  744. * instruction program check.
  745. */
  746. kvmppc_core_queue_program(vcpu, ESR_PPR);
  747. }
  748. r = RESUME_GUEST;
  749. break;
  750. #else
  751. case BOOKE_INTERRUPT_SYSCALL:
  752. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  753. (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
  754. /* KVM PV hypercalls */
  755. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  756. r = RESUME_GUEST;
  757. } else {
  758. /* Guest syscalls */
  759. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
  760. }
  761. kvmppc_account_exit(vcpu, SYSCALL_EXITS);
  762. r = RESUME_GUEST;
  763. break;
  764. #endif
  765. case BOOKE_INTERRUPT_DTLB_MISS: {
  766. unsigned long eaddr = vcpu->arch.fault_dear;
  767. int gtlb_index;
  768. gpa_t gpaddr;
  769. gfn_t gfn;
  770. #ifdef CONFIG_KVM_E500V2
  771. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  772. (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
  773. kvmppc_map_magic(vcpu);
  774. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  775. r = RESUME_GUEST;
  776. break;
  777. }
  778. #endif
  779. /* Check the guest TLB. */
  780. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  781. if (gtlb_index < 0) {
  782. /* The guest didn't have a mapping for it. */
  783. kvmppc_core_queue_dtlb_miss(vcpu,
  784. vcpu->arch.fault_dear,
  785. vcpu->arch.fault_esr);
  786. kvmppc_mmu_dtlb_miss(vcpu);
  787. kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
  788. r = RESUME_GUEST;
  789. break;
  790. }
  791. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  792. gfn = gpaddr >> PAGE_SHIFT;
  793. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  794. /* The guest TLB had a mapping, but the shadow TLB
  795. * didn't, and it is RAM. This could be because:
  796. * a) the entry is mapping the host kernel, or
  797. * b) the guest used a large mapping which we're faking
  798. * Either way, we need to satisfy the fault without
  799. * invoking the guest. */
  800. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  801. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  802. r = RESUME_GUEST;
  803. } else {
  804. /* Guest has mapped and accessed a page which is not
  805. * actually RAM. */
  806. vcpu->arch.paddr_accessed = gpaddr;
  807. vcpu->arch.vaddr_accessed = eaddr;
  808. r = kvmppc_emulate_mmio(run, vcpu);
  809. kvmppc_account_exit(vcpu, MMIO_EXITS);
  810. }
  811. break;
  812. }
  813. case BOOKE_INTERRUPT_ITLB_MISS: {
  814. unsigned long eaddr = vcpu->arch.pc;
  815. gpa_t gpaddr;
  816. gfn_t gfn;
  817. int gtlb_index;
  818. r = RESUME_GUEST;
  819. /* Check the guest TLB. */
  820. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  821. if (gtlb_index < 0) {
  822. /* The guest didn't have a mapping for it. */
  823. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  824. kvmppc_mmu_itlb_miss(vcpu);
  825. kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
  826. break;
  827. }
  828. kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
  829. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  830. gfn = gpaddr >> PAGE_SHIFT;
  831. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  832. /* The guest TLB had a mapping, but the shadow TLB
  833. * didn't. This could be because:
  834. * a) the entry is mapping the host kernel, or
  835. * b) the guest used a large mapping which we're faking
  836. * Either way, we need to satisfy the fault without
  837. * invoking the guest. */
  838. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  839. } else {
  840. /* Guest mapped and leaped at non-RAM! */
  841. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
  842. }
  843. break;
  844. }
  845. case BOOKE_INTERRUPT_DEBUG: {
  846. u32 dbsr;
  847. vcpu->arch.pc = mfspr(SPRN_CSRR0);
  848. /* clear IAC events in DBSR register */
  849. dbsr = mfspr(SPRN_DBSR);
  850. dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
  851. mtspr(SPRN_DBSR, dbsr);
  852. run->exit_reason = KVM_EXIT_DEBUG;
  853. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  854. r = RESUME_HOST;
  855. break;
  856. }
  857. default:
  858. printk(KERN_EMERG "exit_nr %d\n", exit_nr);
  859. BUG();
  860. }
  861. /*
  862. * To avoid clobbering exit_reason, only check for signals if we
  863. * aren't already exiting to userspace for some other reason.
  864. */
  865. if (!(r & RESUME_HOST)) {
  866. local_irq_disable();
  867. if (kvmppc_prepare_to_enter(vcpu)) {
  868. run->exit_reason = KVM_EXIT_INTR;
  869. r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
  870. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  871. }
  872. }
  873. return r;
  874. }
  875. /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
  876. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  877. {
  878. int i;
  879. int r;
  880. vcpu->arch.pc = 0;
  881. vcpu->arch.shared->pir = vcpu->vcpu_id;
  882. kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
  883. kvmppc_set_msr(vcpu, 0);
  884. #ifndef CONFIG_KVM_BOOKE_HV
  885. vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
  886. vcpu->arch.shadow_pid = 1;
  887. vcpu->arch.shared->msr = 0;
  888. #endif
  889. /* Eye-catching numbers so we know if the guest takes an interrupt
  890. * before it's programmed its own IVPR/IVORs. */
  891. vcpu->arch.ivpr = 0x55550000;
  892. for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
  893. vcpu->arch.ivor[i] = 0x7700 | i * 4;
  894. kvmppc_init_timing_stats(vcpu);
  895. r = kvmppc_core_vcpu_setup(vcpu);
  896. kvmppc_sanity_check(vcpu);
  897. return r;
  898. }
  899. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  900. {
  901. int i;
  902. regs->pc = vcpu->arch.pc;
  903. regs->cr = kvmppc_get_cr(vcpu);
  904. regs->ctr = vcpu->arch.ctr;
  905. regs->lr = vcpu->arch.lr;
  906. regs->xer = kvmppc_get_xer(vcpu);
  907. regs->msr = vcpu->arch.shared->msr;
  908. regs->srr0 = vcpu->arch.shared->srr0;
  909. regs->srr1 = vcpu->arch.shared->srr1;
  910. regs->pid = vcpu->arch.pid;
  911. regs->sprg0 = vcpu->arch.shared->sprg0;
  912. regs->sprg1 = vcpu->arch.shared->sprg1;
  913. regs->sprg2 = vcpu->arch.shared->sprg2;
  914. regs->sprg3 = vcpu->arch.shared->sprg3;
  915. regs->sprg4 = vcpu->arch.shared->sprg4;
  916. regs->sprg5 = vcpu->arch.shared->sprg5;
  917. regs->sprg6 = vcpu->arch.shared->sprg6;
  918. regs->sprg7 = vcpu->arch.shared->sprg7;
  919. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  920. regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
  921. return 0;
  922. }
  923. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  924. {
  925. int i;
  926. vcpu->arch.pc = regs->pc;
  927. kvmppc_set_cr(vcpu, regs->cr);
  928. vcpu->arch.ctr = regs->ctr;
  929. vcpu->arch.lr = regs->lr;
  930. kvmppc_set_xer(vcpu, regs->xer);
  931. kvmppc_set_msr(vcpu, regs->msr);
  932. vcpu->arch.shared->srr0 = regs->srr0;
  933. vcpu->arch.shared->srr1 = regs->srr1;
  934. kvmppc_set_pid(vcpu, regs->pid);
  935. vcpu->arch.shared->sprg0 = regs->sprg0;
  936. vcpu->arch.shared->sprg1 = regs->sprg1;
  937. vcpu->arch.shared->sprg2 = regs->sprg2;
  938. vcpu->arch.shared->sprg3 = regs->sprg3;
  939. vcpu->arch.shared->sprg4 = regs->sprg4;
  940. vcpu->arch.shared->sprg5 = regs->sprg5;
  941. vcpu->arch.shared->sprg6 = regs->sprg6;
  942. vcpu->arch.shared->sprg7 = regs->sprg7;
  943. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  944. kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
  945. return 0;
  946. }
  947. static void get_sregs_base(struct kvm_vcpu *vcpu,
  948. struct kvm_sregs *sregs)
  949. {
  950. u64 tb = get_tb();
  951. sregs->u.e.features |= KVM_SREGS_E_BASE;
  952. sregs->u.e.csrr0 = vcpu->arch.csrr0;
  953. sregs->u.e.csrr1 = vcpu->arch.csrr1;
  954. sregs->u.e.mcsr = vcpu->arch.mcsr;
  955. sregs->u.e.esr = get_guest_esr(vcpu);
  956. sregs->u.e.dear = get_guest_dear(vcpu);
  957. sregs->u.e.tsr = vcpu->arch.tsr;
  958. sregs->u.e.tcr = vcpu->arch.tcr;
  959. sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
  960. sregs->u.e.tb = tb;
  961. sregs->u.e.vrsave = vcpu->arch.vrsave;
  962. }
  963. static int set_sregs_base(struct kvm_vcpu *vcpu,
  964. struct kvm_sregs *sregs)
  965. {
  966. if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
  967. return 0;
  968. vcpu->arch.csrr0 = sregs->u.e.csrr0;
  969. vcpu->arch.csrr1 = sregs->u.e.csrr1;
  970. vcpu->arch.mcsr = sregs->u.e.mcsr;
  971. set_guest_esr(vcpu, sregs->u.e.esr);
  972. set_guest_dear(vcpu, sregs->u.e.dear);
  973. vcpu->arch.vrsave = sregs->u.e.vrsave;
  974. kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
  975. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
  976. vcpu->arch.dec = sregs->u.e.dec;
  977. kvmppc_emulate_dec(vcpu);
  978. }
  979. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
  980. vcpu->arch.tsr = sregs->u.e.tsr;
  981. update_timer_ints(vcpu);
  982. }
  983. return 0;
  984. }
  985. static void get_sregs_arch206(struct kvm_vcpu *vcpu,
  986. struct kvm_sregs *sregs)
  987. {
  988. sregs->u.e.features |= KVM_SREGS_E_ARCH206;
  989. sregs->u.e.pir = vcpu->vcpu_id;
  990. sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
  991. sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
  992. sregs->u.e.decar = vcpu->arch.decar;
  993. sregs->u.e.ivpr = vcpu->arch.ivpr;
  994. }
  995. static int set_sregs_arch206(struct kvm_vcpu *vcpu,
  996. struct kvm_sregs *sregs)
  997. {
  998. if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
  999. return 0;
  1000. if (sregs->u.e.pir != vcpu->vcpu_id)
  1001. return -EINVAL;
  1002. vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
  1003. vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
  1004. vcpu->arch.decar = sregs->u.e.decar;
  1005. vcpu->arch.ivpr = sregs->u.e.ivpr;
  1006. return 0;
  1007. }
  1008. void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  1009. {
  1010. sregs->u.e.features |= KVM_SREGS_E_IVOR;
  1011. sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
  1012. sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
  1013. sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
  1014. sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
  1015. sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
  1016. sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
  1017. sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
  1018. sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
  1019. sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
  1020. sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
  1021. sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
  1022. sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
  1023. sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
  1024. sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
  1025. sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
  1026. sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
  1027. }
  1028. int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  1029. {
  1030. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  1031. return 0;
  1032. vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
  1033. vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
  1034. vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
  1035. vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
  1036. vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
  1037. vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
  1038. vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
  1039. vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
  1040. vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
  1041. vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
  1042. vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
  1043. vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
  1044. vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
  1045. vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
  1046. vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
  1047. vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
  1048. return 0;
  1049. }
  1050. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1051. struct kvm_sregs *sregs)
  1052. {
  1053. sregs->pvr = vcpu->arch.pvr;
  1054. get_sregs_base(vcpu, sregs);
  1055. get_sregs_arch206(vcpu, sregs);
  1056. kvmppc_core_get_sregs(vcpu, sregs);
  1057. return 0;
  1058. }
  1059. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1060. struct kvm_sregs *sregs)
  1061. {
  1062. int ret;
  1063. if (vcpu->arch.pvr != sregs->pvr)
  1064. return -EINVAL;
  1065. ret = set_sregs_base(vcpu, sregs);
  1066. if (ret < 0)
  1067. return ret;
  1068. ret = set_sregs_arch206(vcpu, sregs);
  1069. if (ret < 0)
  1070. return ret;
  1071. return kvmppc_core_set_sregs(vcpu, sregs);
  1072. }
  1073. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1074. {
  1075. return -EINVAL;
  1076. }
  1077. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1078. {
  1079. return -EINVAL;
  1080. }
  1081. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  1082. {
  1083. return -ENOTSUPP;
  1084. }
  1085. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  1086. {
  1087. return -ENOTSUPP;
  1088. }
  1089. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1090. struct kvm_translation *tr)
  1091. {
  1092. int r;
  1093. r = kvmppc_core_vcpu_translate(vcpu, tr);
  1094. return r;
  1095. }
  1096. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1097. {
  1098. return -ENOTSUPP;
  1099. }
  1100. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1101. struct kvm_userspace_memory_region *mem)
  1102. {
  1103. return 0;
  1104. }
  1105. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1106. struct kvm_userspace_memory_region *mem)
  1107. {
  1108. }
  1109. void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
  1110. {
  1111. vcpu->arch.tcr = new_tcr;
  1112. update_timer_ints(vcpu);
  1113. }
  1114. void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
  1115. {
  1116. set_bits(tsr_bits, &vcpu->arch.tsr);
  1117. smp_wmb();
  1118. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  1119. kvm_vcpu_kick(vcpu);
  1120. }
  1121. void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
  1122. {
  1123. clear_bits(tsr_bits, &vcpu->arch.tsr);
  1124. update_timer_ints(vcpu);
  1125. }
  1126. void kvmppc_decrementer_func(unsigned long data)
  1127. {
  1128. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  1129. if (vcpu->arch.tcr & TCR_ARE) {
  1130. vcpu->arch.dec = vcpu->arch.decar;
  1131. kvmppc_emulate_dec(vcpu);
  1132. }
  1133. kvmppc_set_tsr_bits(vcpu, TSR_DIS);
  1134. }
  1135. void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1136. {
  1137. current->thread.kvm_vcpu = vcpu;
  1138. }
  1139. void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
  1140. {
  1141. current->thread.kvm_vcpu = NULL;
  1142. }
  1143. int __init kvmppc_booke_init(void)
  1144. {
  1145. #ifndef CONFIG_KVM_BOOKE_HV
  1146. unsigned long ivor[16];
  1147. unsigned long max_ivor = 0;
  1148. int i;
  1149. /* We install our own exception handlers by hijacking IVPR. IVPR must
  1150. * be 16-bit aligned, so we need a 64KB allocation. */
  1151. kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1152. VCPU_SIZE_ORDER);
  1153. if (!kvmppc_booke_handlers)
  1154. return -ENOMEM;
  1155. /* XXX make sure our handlers are smaller than Linux's */
  1156. /* Copy our interrupt handlers to match host IVORs. That way we don't
  1157. * have to swap the IVORs on every guest/host transition. */
  1158. ivor[0] = mfspr(SPRN_IVOR0);
  1159. ivor[1] = mfspr(SPRN_IVOR1);
  1160. ivor[2] = mfspr(SPRN_IVOR2);
  1161. ivor[3] = mfspr(SPRN_IVOR3);
  1162. ivor[4] = mfspr(SPRN_IVOR4);
  1163. ivor[5] = mfspr(SPRN_IVOR5);
  1164. ivor[6] = mfspr(SPRN_IVOR6);
  1165. ivor[7] = mfspr(SPRN_IVOR7);
  1166. ivor[8] = mfspr(SPRN_IVOR8);
  1167. ivor[9] = mfspr(SPRN_IVOR9);
  1168. ivor[10] = mfspr(SPRN_IVOR10);
  1169. ivor[11] = mfspr(SPRN_IVOR11);
  1170. ivor[12] = mfspr(SPRN_IVOR12);
  1171. ivor[13] = mfspr(SPRN_IVOR13);
  1172. ivor[14] = mfspr(SPRN_IVOR14);
  1173. ivor[15] = mfspr(SPRN_IVOR15);
  1174. for (i = 0; i < 16; i++) {
  1175. if (ivor[i] > max_ivor)
  1176. max_ivor = ivor[i];
  1177. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  1178. kvmppc_handlers_start + i * kvmppc_handler_len,
  1179. kvmppc_handler_len);
  1180. }
  1181. flush_icache_range(kvmppc_booke_handlers,
  1182. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  1183. #endif /* !BOOKE_HV */
  1184. return 0;
  1185. }
  1186. void __exit kvmppc_booke_exit(void)
  1187. {
  1188. free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
  1189. kvm_exit();
  1190. }