booke.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. */
  21. #include <linux/errno.h>
  22. #include <linux/err.h>
  23. #include <linux/kvm_host.h>
  24. #include <linux/gfp.h>
  25. #include <linux/module.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/fs.h>
  28. #include <asm/cputable.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/kvm_ppc.h>
  31. #include "timing.h"
  32. #include <asm/cacheflush.h>
  33. #include "booke.h"
  34. unsigned long kvmppc_booke_handlers;
  35. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  36. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  37. struct kvm_stats_debugfs_item debugfs_entries[] = {
  38. { "mmio", VCPU_STAT(mmio_exits) },
  39. { "dcr", VCPU_STAT(dcr_exits) },
  40. { "sig", VCPU_STAT(signal_exits) },
  41. { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
  42. { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
  43. { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
  44. { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
  45. { "sysc", VCPU_STAT(syscall_exits) },
  46. { "isi", VCPU_STAT(isi_exits) },
  47. { "dsi", VCPU_STAT(dsi_exits) },
  48. { "inst_emu", VCPU_STAT(emulated_inst_exits) },
  49. { "dec", VCPU_STAT(dec_exits) },
  50. { "ext_intr", VCPU_STAT(ext_intr_exits) },
  51. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  52. { NULL }
  53. };
  54. /* TODO: use vcpu_printf() */
  55. void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
  56. {
  57. int i;
  58. printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
  59. printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
  60. printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
  61. vcpu->arch.shared->srr1);
  62. printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
  63. for (i = 0; i < 32; i += 4) {
  64. printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
  65. kvmppc_get_gpr(vcpu, i),
  66. kvmppc_get_gpr(vcpu, i+1),
  67. kvmppc_get_gpr(vcpu, i+2),
  68. kvmppc_get_gpr(vcpu, i+3));
  69. }
  70. }
  71. #ifdef CONFIG_SPE
  72. void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
  73. {
  74. preempt_disable();
  75. enable_kernel_spe();
  76. kvmppc_save_guest_spe(vcpu);
  77. vcpu->arch.shadow_msr &= ~MSR_SPE;
  78. preempt_enable();
  79. }
  80. static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
  81. {
  82. preempt_disable();
  83. enable_kernel_spe();
  84. kvmppc_load_guest_spe(vcpu);
  85. vcpu->arch.shadow_msr |= MSR_SPE;
  86. preempt_enable();
  87. }
  88. static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
  89. {
  90. if (vcpu->arch.shared->msr & MSR_SPE) {
  91. if (!(vcpu->arch.shadow_msr & MSR_SPE))
  92. kvmppc_vcpu_enable_spe(vcpu);
  93. } else if (vcpu->arch.shadow_msr & MSR_SPE) {
  94. kvmppc_vcpu_disable_spe(vcpu);
  95. }
  96. }
  97. #else
  98. static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
  99. {
  100. }
  101. #endif
  102. /*
  103. * Helper function for "full" MSR writes. No need to call this if only
  104. * EE/CE/ME/DE/RI are changing.
  105. */
  106. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
  107. {
  108. u32 old_msr = vcpu->arch.shared->msr;
  109. vcpu->arch.shared->msr = new_msr;
  110. kvmppc_mmu_msr_notify(vcpu, old_msr);
  111. if (vcpu->arch.shared->msr & MSR_WE) {
  112. kvm_vcpu_block(vcpu);
  113. kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
  114. };
  115. kvmppc_vcpu_sync_spe(vcpu);
  116. }
  117. static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
  118. unsigned int priority)
  119. {
  120. set_bit(priority, &vcpu->arch.pending_exceptions);
  121. }
  122. static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
  123. ulong dear_flags, ulong esr_flags)
  124. {
  125. vcpu->arch.queued_dear = dear_flags;
  126. vcpu->arch.queued_esr = esr_flags;
  127. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
  128. }
  129. static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
  130. ulong dear_flags, ulong esr_flags)
  131. {
  132. vcpu->arch.queued_dear = dear_flags;
  133. vcpu->arch.queued_esr = esr_flags;
  134. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
  135. }
  136. static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
  137. ulong esr_flags)
  138. {
  139. vcpu->arch.queued_esr = esr_flags;
  140. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
  141. }
  142. void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
  143. {
  144. vcpu->arch.queued_esr = esr_flags;
  145. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
  146. }
  147. void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
  148. {
  149. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
  150. }
  151. int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
  152. {
  153. return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  154. }
  155. void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
  156. {
  157. clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  158. }
  159. void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
  160. struct kvm_interrupt *irq)
  161. {
  162. unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
  163. if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
  164. prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
  165. kvmppc_booke_queue_irqprio(vcpu, prio);
  166. }
  167. void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
  168. struct kvm_interrupt *irq)
  169. {
  170. clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
  171. clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
  172. }
  173. /* Deliver the interrupt of the corresponding priority, if possible. */
  174. static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
  175. unsigned int priority)
  176. {
  177. int allowed = 0;
  178. ulong uninitialized_var(msr_mask);
  179. bool update_esr = false, update_dear = false;
  180. ulong crit_raw = vcpu->arch.shared->critical;
  181. ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
  182. bool crit;
  183. bool keep_irq = false;
  184. /* Truncate crit indicators in 32 bit mode */
  185. if (!(vcpu->arch.shared->msr & MSR_SF)) {
  186. crit_raw &= 0xffffffff;
  187. crit_r1 &= 0xffffffff;
  188. }
  189. /* Critical section when crit == r1 */
  190. crit = (crit_raw == crit_r1);
  191. /* ... and we're in supervisor mode */
  192. crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
  193. if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
  194. priority = BOOKE_IRQPRIO_EXTERNAL;
  195. keep_irq = true;
  196. }
  197. switch (priority) {
  198. case BOOKE_IRQPRIO_DTLB_MISS:
  199. case BOOKE_IRQPRIO_DATA_STORAGE:
  200. update_dear = true;
  201. /* fall through */
  202. case BOOKE_IRQPRIO_INST_STORAGE:
  203. case BOOKE_IRQPRIO_PROGRAM:
  204. update_esr = true;
  205. /* fall through */
  206. case BOOKE_IRQPRIO_ITLB_MISS:
  207. case BOOKE_IRQPRIO_SYSCALL:
  208. case BOOKE_IRQPRIO_FP_UNAVAIL:
  209. case BOOKE_IRQPRIO_SPE_UNAVAIL:
  210. case BOOKE_IRQPRIO_SPE_FP_DATA:
  211. case BOOKE_IRQPRIO_SPE_FP_ROUND:
  212. case BOOKE_IRQPRIO_AP_UNAVAIL:
  213. case BOOKE_IRQPRIO_ALIGNMENT:
  214. allowed = 1;
  215. msr_mask = MSR_CE|MSR_ME|MSR_DE;
  216. break;
  217. case BOOKE_IRQPRIO_CRITICAL:
  218. case BOOKE_IRQPRIO_WATCHDOG:
  219. allowed = vcpu->arch.shared->msr & MSR_CE;
  220. msr_mask = MSR_ME;
  221. break;
  222. case BOOKE_IRQPRIO_MACHINE_CHECK:
  223. allowed = vcpu->arch.shared->msr & MSR_ME;
  224. msr_mask = 0;
  225. break;
  226. case BOOKE_IRQPRIO_EXTERNAL:
  227. case BOOKE_IRQPRIO_DECREMENTER:
  228. case BOOKE_IRQPRIO_FIT:
  229. allowed = vcpu->arch.shared->msr & MSR_EE;
  230. allowed = allowed && !crit;
  231. msr_mask = MSR_CE|MSR_ME|MSR_DE;
  232. break;
  233. case BOOKE_IRQPRIO_DEBUG:
  234. allowed = vcpu->arch.shared->msr & MSR_DE;
  235. msr_mask = MSR_ME;
  236. break;
  237. }
  238. if (allowed) {
  239. vcpu->arch.shared->srr0 = vcpu->arch.pc;
  240. vcpu->arch.shared->srr1 = vcpu->arch.shared->msr;
  241. vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
  242. if (update_esr == true)
  243. vcpu->arch.esr = vcpu->arch.queued_esr;
  244. if (update_dear == true)
  245. vcpu->arch.shared->dar = vcpu->arch.queued_dear;
  246. kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
  247. if (!keep_irq)
  248. clear_bit(priority, &vcpu->arch.pending_exceptions);
  249. }
  250. return allowed;
  251. }
  252. /* Check pending exceptions and deliver one, if possible. */
  253. void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
  254. {
  255. unsigned long *pending = &vcpu->arch.pending_exceptions;
  256. unsigned long old_pending = vcpu->arch.pending_exceptions;
  257. unsigned int priority;
  258. priority = __ffs(*pending);
  259. while (priority <= BOOKE_IRQPRIO_MAX) {
  260. if (kvmppc_booke_irqprio_deliver(vcpu, priority))
  261. break;
  262. priority = find_next_bit(pending,
  263. BITS_PER_BYTE * sizeof(*pending),
  264. priority + 1);
  265. }
  266. /* Tell the guest about our interrupt status */
  267. if (*pending)
  268. vcpu->arch.shared->int_pending = 1;
  269. else if (old_pending)
  270. vcpu->arch.shared->int_pending = 0;
  271. }
  272. int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  273. {
  274. int ret;
  275. local_irq_disable();
  276. kvm_guest_enter();
  277. ret = __kvmppc_vcpu_run(kvm_run, vcpu);
  278. kvm_guest_exit();
  279. local_irq_enable();
  280. return ret;
  281. }
  282. /**
  283. * kvmppc_handle_exit
  284. *
  285. * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
  286. */
  287. int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  288. unsigned int exit_nr)
  289. {
  290. enum emulation_result er;
  291. int r = RESUME_HOST;
  292. /* update before a new last_exit_type is rewritten */
  293. kvmppc_update_timing_stats(vcpu);
  294. local_irq_enable();
  295. run->exit_reason = KVM_EXIT_UNKNOWN;
  296. run->ready_for_interrupt_injection = 1;
  297. switch (exit_nr) {
  298. case BOOKE_INTERRUPT_MACHINE_CHECK:
  299. printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
  300. kvmppc_dump_vcpu(vcpu);
  301. r = RESUME_HOST;
  302. break;
  303. case BOOKE_INTERRUPT_EXTERNAL:
  304. kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
  305. if (need_resched())
  306. cond_resched();
  307. r = RESUME_GUEST;
  308. break;
  309. case BOOKE_INTERRUPT_DECREMENTER:
  310. /* Since we switched IVPR back to the host's value, the host
  311. * handled this interrupt the moment we enabled interrupts.
  312. * Now we just offer it a chance to reschedule the guest. */
  313. kvmppc_account_exit(vcpu, DEC_EXITS);
  314. if (need_resched())
  315. cond_resched();
  316. r = RESUME_GUEST;
  317. break;
  318. case BOOKE_INTERRUPT_PROGRAM:
  319. if (vcpu->arch.shared->msr & MSR_PR) {
  320. /* Program traps generated by user-level software must be handled
  321. * by the guest kernel. */
  322. kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
  323. r = RESUME_GUEST;
  324. kvmppc_account_exit(vcpu, USR_PR_INST);
  325. break;
  326. }
  327. er = kvmppc_emulate_instruction(run, vcpu);
  328. switch (er) {
  329. case EMULATE_DONE:
  330. /* don't overwrite subtypes, just account kvm_stats */
  331. kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
  332. /* Future optimization: only reload non-volatiles if
  333. * they were actually modified by emulation. */
  334. r = RESUME_GUEST_NV;
  335. break;
  336. case EMULATE_DO_DCR:
  337. run->exit_reason = KVM_EXIT_DCR;
  338. r = RESUME_HOST;
  339. break;
  340. case EMULATE_FAIL:
  341. /* XXX Deliver Program interrupt to guest. */
  342. printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
  343. __func__, vcpu->arch.pc, vcpu->arch.last_inst);
  344. /* For debugging, encode the failing instruction and
  345. * report it to userspace. */
  346. run->hw.hardware_exit_reason = ~0ULL << 32;
  347. run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
  348. r = RESUME_HOST;
  349. break;
  350. default:
  351. BUG();
  352. }
  353. break;
  354. case BOOKE_INTERRUPT_FP_UNAVAIL:
  355. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
  356. kvmppc_account_exit(vcpu, FP_UNAVAIL);
  357. r = RESUME_GUEST;
  358. break;
  359. #ifdef CONFIG_SPE
  360. case BOOKE_INTERRUPT_SPE_UNAVAIL: {
  361. if (vcpu->arch.shared->msr & MSR_SPE)
  362. kvmppc_vcpu_enable_spe(vcpu);
  363. else
  364. kvmppc_booke_queue_irqprio(vcpu,
  365. BOOKE_IRQPRIO_SPE_UNAVAIL);
  366. r = RESUME_GUEST;
  367. break;
  368. }
  369. case BOOKE_INTERRUPT_SPE_FP_DATA:
  370. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
  371. r = RESUME_GUEST;
  372. break;
  373. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  374. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
  375. r = RESUME_GUEST;
  376. break;
  377. #else
  378. case BOOKE_INTERRUPT_SPE_UNAVAIL:
  379. /*
  380. * Guest wants SPE, but host kernel doesn't support it. Send
  381. * an "unimplemented operation" program check to the guest.
  382. */
  383. kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
  384. r = RESUME_GUEST;
  385. break;
  386. /*
  387. * These really should never happen without CONFIG_SPE,
  388. * as we should never enable the real MSR[SPE] in the guest.
  389. */
  390. case BOOKE_INTERRUPT_SPE_FP_DATA:
  391. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  392. printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
  393. __func__, exit_nr, vcpu->arch.pc);
  394. run->hw.hardware_exit_reason = exit_nr;
  395. r = RESUME_HOST;
  396. break;
  397. #endif
  398. case BOOKE_INTERRUPT_DATA_STORAGE:
  399. kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
  400. vcpu->arch.fault_esr);
  401. kvmppc_account_exit(vcpu, DSI_EXITS);
  402. r = RESUME_GUEST;
  403. break;
  404. case BOOKE_INTERRUPT_INST_STORAGE:
  405. kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
  406. kvmppc_account_exit(vcpu, ISI_EXITS);
  407. r = RESUME_GUEST;
  408. break;
  409. case BOOKE_INTERRUPT_SYSCALL:
  410. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  411. (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
  412. /* KVM PV hypercalls */
  413. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  414. r = RESUME_GUEST;
  415. } else {
  416. /* Guest syscalls */
  417. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
  418. }
  419. kvmppc_account_exit(vcpu, SYSCALL_EXITS);
  420. r = RESUME_GUEST;
  421. break;
  422. case BOOKE_INTERRUPT_DTLB_MISS: {
  423. unsigned long eaddr = vcpu->arch.fault_dear;
  424. int gtlb_index;
  425. gpa_t gpaddr;
  426. gfn_t gfn;
  427. #ifdef CONFIG_KVM_E500
  428. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  429. (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
  430. kvmppc_map_magic(vcpu);
  431. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  432. r = RESUME_GUEST;
  433. break;
  434. }
  435. #endif
  436. /* Check the guest TLB. */
  437. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  438. if (gtlb_index < 0) {
  439. /* The guest didn't have a mapping for it. */
  440. kvmppc_core_queue_dtlb_miss(vcpu,
  441. vcpu->arch.fault_dear,
  442. vcpu->arch.fault_esr);
  443. kvmppc_mmu_dtlb_miss(vcpu);
  444. kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
  445. r = RESUME_GUEST;
  446. break;
  447. }
  448. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  449. gfn = gpaddr >> PAGE_SHIFT;
  450. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  451. /* The guest TLB had a mapping, but the shadow TLB
  452. * didn't, and it is RAM. This could be because:
  453. * a) the entry is mapping the host kernel, or
  454. * b) the guest used a large mapping which we're faking
  455. * Either way, we need to satisfy the fault without
  456. * invoking the guest. */
  457. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  458. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  459. r = RESUME_GUEST;
  460. } else {
  461. /* Guest has mapped and accessed a page which is not
  462. * actually RAM. */
  463. vcpu->arch.paddr_accessed = gpaddr;
  464. r = kvmppc_emulate_mmio(run, vcpu);
  465. kvmppc_account_exit(vcpu, MMIO_EXITS);
  466. }
  467. break;
  468. }
  469. case BOOKE_INTERRUPT_ITLB_MISS: {
  470. unsigned long eaddr = vcpu->arch.pc;
  471. gpa_t gpaddr;
  472. gfn_t gfn;
  473. int gtlb_index;
  474. r = RESUME_GUEST;
  475. /* Check the guest TLB. */
  476. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  477. if (gtlb_index < 0) {
  478. /* The guest didn't have a mapping for it. */
  479. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  480. kvmppc_mmu_itlb_miss(vcpu);
  481. kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
  482. break;
  483. }
  484. kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
  485. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  486. gfn = gpaddr >> PAGE_SHIFT;
  487. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  488. /* The guest TLB had a mapping, but the shadow TLB
  489. * didn't. This could be because:
  490. * a) the entry is mapping the host kernel, or
  491. * b) the guest used a large mapping which we're faking
  492. * Either way, we need to satisfy the fault without
  493. * invoking the guest. */
  494. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  495. } else {
  496. /* Guest mapped and leaped at non-RAM! */
  497. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
  498. }
  499. break;
  500. }
  501. case BOOKE_INTERRUPT_DEBUG: {
  502. u32 dbsr;
  503. vcpu->arch.pc = mfspr(SPRN_CSRR0);
  504. /* clear IAC events in DBSR register */
  505. dbsr = mfspr(SPRN_DBSR);
  506. dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
  507. mtspr(SPRN_DBSR, dbsr);
  508. run->exit_reason = KVM_EXIT_DEBUG;
  509. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  510. r = RESUME_HOST;
  511. break;
  512. }
  513. default:
  514. printk(KERN_EMERG "exit_nr %d\n", exit_nr);
  515. BUG();
  516. }
  517. local_irq_disable();
  518. kvmppc_core_deliver_interrupts(vcpu);
  519. if (!(r & RESUME_HOST)) {
  520. /* To avoid clobbering exit_reason, only check for signals if
  521. * we aren't already exiting to userspace for some other
  522. * reason. */
  523. if (signal_pending(current)) {
  524. run->exit_reason = KVM_EXIT_INTR;
  525. r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
  526. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  527. }
  528. }
  529. return r;
  530. }
  531. /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
  532. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  533. {
  534. int i;
  535. vcpu->arch.pc = 0;
  536. vcpu->arch.shared->msr = 0;
  537. vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
  538. kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
  539. vcpu->arch.shadow_pid = 1;
  540. /* Eye-catching numbers so we know if the guest takes an interrupt
  541. * before it's programmed its own IVPR/IVORs. */
  542. vcpu->arch.ivpr = 0x55550000;
  543. for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
  544. vcpu->arch.ivor[i] = 0x7700 | i * 4;
  545. kvmppc_init_timing_stats(vcpu);
  546. return kvmppc_core_vcpu_setup(vcpu);
  547. }
  548. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  549. {
  550. int i;
  551. regs->pc = vcpu->arch.pc;
  552. regs->cr = kvmppc_get_cr(vcpu);
  553. regs->ctr = vcpu->arch.ctr;
  554. regs->lr = vcpu->arch.lr;
  555. regs->xer = kvmppc_get_xer(vcpu);
  556. regs->msr = vcpu->arch.shared->msr;
  557. regs->srr0 = vcpu->arch.shared->srr0;
  558. regs->srr1 = vcpu->arch.shared->srr1;
  559. regs->pid = vcpu->arch.pid;
  560. regs->sprg0 = vcpu->arch.shared->sprg0;
  561. regs->sprg1 = vcpu->arch.shared->sprg1;
  562. regs->sprg2 = vcpu->arch.shared->sprg2;
  563. regs->sprg3 = vcpu->arch.shared->sprg3;
  564. regs->sprg4 = vcpu->arch.sprg4;
  565. regs->sprg5 = vcpu->arch.sprg5;
  566. regs->sprg6 = vcpu->arch.sprg6;
  567. regs->sprg7 = vcpu->arch.sprg7;
  568. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  569. regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
  570. return 0;
  571. }
  572. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  573. {
  574. int i;
  575. vcpu->arch.pc = regs->pc;
  576. kvmppc_set_cr(vcpu, regs->cr);
  577. vcpu->arch.ctr = regs->ctr;
  578. vcpu->arch.lr = regs->lr;
  579. kvmppc_set_xer(vcpu, regs->xer);
  580. kvmppc_set_msr(vcpu, regs->msr);
  581. vcpu->arch.shared->srr0 = regs->srr0;
  582. vcpu->arch.shared->srr1 = regs->srr1;
  583. kvmppc_set_pid(vcpu, regs->pid);
  584. vcpu->arch.shared->sprg0 = regs->sprg0;
  585. vcpu->arch.shared->sprg1 = regs->sprg1;
  586. vcpu->arch.shared->sprg2 = regs->sprg2;
  587. vcpu->arch.shared->sprg3 = regs->sprg3;
  588. vcpu->arch.sprg4 = regs->sprg4;
  589. vcpu->arch.sprg5 = regs->sprg5;
  590. vcpu->arch.sprg6 = regs->sprg6;
  591. vcpu->arch.sprg7 = regs->sprg7;
  592. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  593. kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
  594. return 0;
  595. }
  596. static void get_sregs_base(struct kvm_vcpu *vcpu,
  597. struct kvm_sregs *sregs)
  598. {
  599. u64 tb = get_tb();
  600. sregs->u.e.features |= KVM_SREGS_E_BASE;
  601. sregs->u.e.csrr0 = vcpu->arch.csrr0;
  602. sregs->u.e.csrr1 = vcpu->arch.csrr1;
  603. sregs->u.e.mcsr = vcpu->arch.mcsr;
  604. sregs->u.e.esr = vcpu->arch.esr;
  605. sregs->u.e.dear = vcpu->arch.shared->dar;
  606. sregs->u.e.tsr = vcpu->arch.tsr;
  607. sregs->u.e.tcr = vcpu->arch.tcr;
  608. sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
  609. sregs->u.e.tb = tb;
  610. sregs->u.e.vrsave = vcpu->arch.vrsave;
  611. }
  612. static int set_sregs_base(struct kvm_vcpu *vcpu,
  613. struct kvm_sregs *sregs)
  614. {
  615. if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
  616. return 0;
  617. vcpu->arch.csrr0 = sregs->u.e.csrr0;
  618. vcpu->arch.csrr1 = sregs->u.e.csrr1;
  619. vcpu->arch.mcsr = sregs->u.e.mcsr;
  620. vcpu->arch.esr = sregs->u.e.esr;
  621. vcpu->arch.shared->dar = sregs->u.e.dear;
  622. vcpu->arch.vrsave = sregs->u.e.vrsave;
  623. vcpu->arch.tcr = sregs->u.e.tcr;
  624. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC)
  625. vcpu->arch.dec = sregs->u.e.dec;
  626. kvmppc_emulate_dec(vcpu);
  627. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
  628. /*
  629. * FIXME: existing KVM timer handling is incomplete.
  630. * TSR cannot be read by the guest, and its value in
  631. * vcpu->arch is always zero. For now, just handle
  632. * the case where the caller is trying to inject a
  633. * decrementer interrupt.
  634. */
  635. if ((sregs->u.e.tsr & TSR_DIS) &&
  636. (vcpu->arch.tcr & TCR_DIE))
  637. kvmppc_core_queue_dec(vcpu);
  638. }
  639. return 0;
  640. }
  641. static void get_sregs_arch206(struct kvm_vcpu *vcpu,
  642. struct kvm_sregs *sregs)
  643. {
  644. sregs->u.e.features |= KVM_SREGS_E_ARCH206;
  645. sregs->u.e.pir = 0;
  646. sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
  647. sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
  648. sregs->u.e.decar = vcpu->arch.decar;
  649. sregs->u.e.ivpr = vcpu->arch.ivpr;
  650. }
  651. static int set_sregs_arch206(struct kvm_vcpu *vcpu,
  652. struct kvm_sregs *sregs)
  653. {
  654. if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
  655. return 0;
  656. if (sregs->u.e.pir != 0)
  657. return -EINVAL;
  658. vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
  659. vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
  660. vcpu->arch.decar = sregs->u.e.decar;
  661. vcpu->arch.ivpr = sregs->u.e.ivpr;
  662. return 0;
  663. }
  664. void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  665. {
  666. sregs->u.e.features |= KVM_SREGS_E_IVOR;
  667. sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
  668. sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
  669. sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
  670. sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
  671. sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
  672. sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
  673. sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
  674. sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
  675. sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
  676. sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
  677. sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
  678. sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
  679. sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
  680. sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
  681. sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
  682. sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
  683. }
  684. int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  685. {
  686. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  687. return 0;
  688. vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
  689. vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
  690. vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
  691. vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
  692. vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
  693. vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
  694. vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
  695. vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
  696. vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
  697. vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
  698. vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
  699. vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
  700. vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
  701. vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
  702. vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
  703. vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
  704. return 0;
  705. }
  706. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  707. struct kvm_sregs *sregs)
  708. {
  709. sregs->pvr = vcpu->arch.pvr;
  710. get_sregs_base(vcpu, sregs);
  711. get_sregs_arch206(vcpu, sregs);
  712. kvmppc_core_get_sregs(vcpu, sregs);
  713. return 0;
  714. }
  715. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  716. struct kvm_sregs *sregs)
  717. {
  718. int ret;
  719. if (vcpu->arch.pvr != sregs->pvr)
  720. return -EINVAL;
  721. ret = set_sregs_base(vcpu, sregs);
  722. if (ret < 0)
  723. return ret;
  724. ret = set_sregs_arch206(vcpu, sregs);
  725. if (ret < 0)
  726. return ret;
  727. return kvmppc_core_set_sregs(vcpu, sregs);
  728. }
  729. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  730. {
  731. return -ENOTSUPP;
  732. }
  733. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  734. {
  735. return -ENOTSUPP;
  736. }
  737. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  738. struct kvm_translation *tr)
  739. {
  740. int r;
  741. r = kvmppc_core_vcpu_translate(vcpu, tr);
  742. return r;
  743. }
  744. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  745. {
  746. return -ENOTSUPP;
  747. }
  748. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  749. struct kvm_userspace_memory_region *mem)
  750. {
  751. return 0;
  752. }
  753. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  754. struct kvm_userspace_memory_region *mem)
  755. {
  756. }
  757. int kvmppc_core_init_vm(struct kvm *kvm)
  758. {
  759. return 0;
  760. }
  761. void kvmppc_core_destroy_vm(struct kvm *kvm)
  762. {
  763. }
  764. int __init kvmppc_booke_init(void)
  765. {
  766. unsigned long ivor[16];
  767. unsigned long max_ivor = 0;
  768. int i;
  769. /* We install our own exception handlers by hijacking IVPR. IVPR must
  770. * be 16-bit aligned, so we need a 64KB allocation. */
  771. kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  772. VCPU_SIZE_ORDER);
  773. if (!kvmppc_booke_handlers)
  774. return -ENOMEM;
  775. /* XXX make sure our handlers are smaller than Linux's */
  776. /* Copy our interrupt handlers to match host IVORs. That way we don't
  777. * have to swap the IVORs on every guest/host transition. */
  778. ivor[0] = mfspr(SPRN_IVOR0);
  779. ivor[1] = mfspr(SPRN_IVOR1);
  780. ivor[2] = mfspr(SPRN_IVOR2);
  781. ivor[3] = mfspr(SPRN_IVOR3);
  782. ivor[4] = mfspr(SPRN_IVOR4);
  783. ivor[5] = mfspr(SPRN_IVOR5);
  784. ivor[6] = mfspr(SPRN_IVOR6);
  785. ivor[7] = mfspr(SPRN_IVOR7);
  786. ivor[8] = mfspr(SPRN_IVOR8);
  787. ivor[9] = mfspr(SPRN_IVOR9);
  788. ivor[10] = mfspr(SPRN_IVOR10);
  789. ivor[11] = mfspr(SPRN_IVOR11);
  790. ivor[12] = mfspr(SPRN_IVOR12);
  791. ivor[13] = mfspr(SPRN_IVOR13);
  792. ivor[14] = mfspr(SPRN_IVOR14);
  793. ivor[15] = mfspr(SPRN_IVOR15);
  794. for (i = 0; i < 16; i++) {
  795. if (ivor[i] > max_ivor)
  796. max_ivor = ivor[i];
  797. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  798. kvmppc_handlers_start + i * kvmppc_handler_len,
  799. kvmppc_handler_len);
  800. }
  801. flush_icache_range(kvmppc_booke_handlers,
  802. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  803. return 0;
  804. }
  805. void __exit kvmppc_booke_exit(void)
  806. {
  807. free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
  808. kvm_exit();
  809. }