booke.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. if (!vcpu->arch.sane) {
  276. kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  277. return -EINVAL;
  278. }
  279. local_irq_disable();
  280. kvm_guest_enter();
  281. ret = __kvmppc_vcpu_run(kvm_run, vcpu);
  282. kvm_guest_exit();
  283. local_irq_enable();
  284. return ret;
  285. }
  286. /**
  287. * kvmppc_handle_exit
  288. *
  289. * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
  290. */
  291. int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  292. unsigned int exit_nr)
  293. {
  294. enum emulation_result er;
  295. int r = RESUME_HOST;
  296. /* update before a new last_exit_type is rewritten */
  297. kvmppc_update_timing_stats(vcpu);
  298. local_irq_enable();
  299. run->exit_reason = KVM_EXIT_UNKNOWN;
  300. run->ready_for_interrupt_injection = 1;
  301. switch (exit_nr) {
  302. case BOOKE_INTERRUPT_MACHINE_CHECK:
  303. printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
  304. kvmppc_dump_vcpu(vcpu);
  305. r = RESUME_HOST;
  306. break;
  307. case BOOKE_INTERRUPT_EXTERNAL:
  308. kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
  309. if (need_resched())
  310. cond_resched();
  311. r = RESUME_GUEST;
  312. break;
  313. case BOOKE_INTERRUPT_DECREMENTER:
  314. /* Since we switched IVPR back to the host's value, the host
  315. * handled this interrupt the moment we enabled interrupts.
  316. * Now we just offer it a chance to reschedule the guest. */
  317. kvmppc_account_exit(vcpu, DEC_EXITS);
  318. if (need_resched())
  319. cond_resched();
  320. r = RESUME_GUEST;
  321. break;
  322. case BOOKE_INTERRUPT_PROGRAM:
  323. if (vcpu->arch.shared->msr & MSR_PR) {
  324. /* Program traps generated by user-level software must be handled
  325. * by the guest kernel. */
  326. kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
  327. r = RESUME_GUEST;
  328. kvmppc_account_exit(vcpu, USR_PR_INST);
  329. break;
  330. }
  331. er = kvmppc_emulate_instruction(run, vcpu);
  332. switch (er) {
  333. case EMULATE_DONE:
  334. /* don't overwrite subtypes, just account kvm_stats */
  335. kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
  336. /* Future optimization: only reload non-volatiles if
  337. * they were actually modified by emulation. */
  338. r = RESUME_GUEST_NV;
  339. break;
  340. case EMULATE_DO_DCR:
  341. run->exit_reason = KVM_EXIT_DCR;
  342. r = RESUME_HOST;
  343. break;
  344. case EMULATE_FAIL:
  345. /* XXX Deliver Program interrupt to guest. */
  346. printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
  347. __func__, vcpu->arch.pc, vcpu->arch.last_inst);
  348. /* For debugging, encode the failing instruction and
  349. * report it to userspace. */
  350. run->hw.hardware_exit_reason = ~0ULL << 32;
  351. run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
  352. r = RESUME_HOST;
  353. break;
  354. default:
  355. BUG();
  356. }
  357. break;
  358. case BOOKE_INTERRUPT_FP_UNAVAIL:
  359. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
  360. kvmppc_account_exit(vcpu, FP_UNAVAIL);
  361. r = RESUME_GUEST;
  362. break;
  363. #ifdef CONFIG_SPE
  364. case BOOKE_INTERRUPT_SPE_UNAVAIL: {
  365. if (vcpu->arch.shared->msr & MSR_SPE)
  366. kvmppc_vcpu_enable_spe(vcpu);
  367. else
  368. kvmppc_booke_queue_irqprio(vcpu,
  369. BOOKE_IRQPRIO_SPE_UNAVAIL);
  370. r = RESUME_GUEST;
  371. break;
  372. }
  373. case BOOKE_INTERRUPT_SPE_FP_DATA:
  374. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
  375. r = RESUME_GUEST;
  376. break;
  377. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  378. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
  379. r = RESUME_GUEST;
  380. break;
  381. #else
  382. case BOOKE_INTERRUPT_SPE_UNAVAIL:
  383. /*
  384. * Guest wants SPE, but host kernel doesn't support it. Send
  385. * an "unimplemented operation" program check to the guest.
  386. */
  387. kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
  388. r = RESUME_GUEST;
  389. break;
  390. /*
  391. * These really should never happen without CONFIG_SPE,
  392. * as we should never enable the real MSR[SPE] in the guest.
  393. */
  394. case BOOKE_INTERRUPT_SPE_FP_DATA:
  395. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  396. printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
  397. __func__, exit_nr, vcpu->arch.pc);
  398. run->hw.hardware_exit_reason = exit_nr;
  399. r = RESUME_HOST;
  400. break;
  401. #endif
  402. case BOOKE_INTERRUPT_DATA_STORAGE:
  403. kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
  404. vcpu->arch.fault_esr);
  405. kvmppc_account_exit(vcpu, DSI_EXITS);
  406. r = RESUME_GUEST;
  407. break;
  408. case BOOKE_INTERRUPT_INST_STORAGE:
  409. kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
  410. kvmppc_account_exit(vcpu, ISI_EXITS);
  411. r = RESUME_GUEST;
  412. break;
  413. case BOOKE_INTERRUPT_SYSCALL:
  414. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  415. (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
  416. /* KVM PV hypercalls */
  417. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  418. r = RESUME_GUEST;
  419. } else {
  420. /* Guest syscalls */
  421. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
  422. }
  423. kvmppc_account_exit(vcpu, SYSCALL_EXITS);
  424. r = RESUME_GUEST;
  425. break;
  426. case BOOKE_INTERRUPT_DTLB_MISS: {
  427. unsigned long eaddr = vcpu->arch.fault_dear;
  428. int gtlb_index;
  429. gpa_t gpaddr;
  430. gfn_t gfn;
  431. #ifdef CONFIG_KVM_E500
  432. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  433. (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
  434. kvmppc_map_magic(vcpu);
  435. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  436. r = RESUME_GUEST;
  437. break;
  438. }
  439. #endif
  440. /* Check the guest TLB. */
  441. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  442. if (gtlb_index < 0) {
  443. /* The guest didn't have a mapping for it. */
  444. kvmppc_core_queue_dtlb_miss(vcpu,
  445. vcpu->arch.fault_dear,
  446. vcpu->arch.fault_esr);
  447. kvmppc_mmu_dtlb_miss(vcpu);
  448. kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
  449. r = RESUME_GUEST;
  450. break;
  451. }
  452. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  453. gfn = gpaddr >> PAGE_SHIFT;
  454. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  455. /* The guest TLB had a mapping, but the shadow TLB
  456. * didn't, and it is RAM. This could be because:
  457. * a) the entry is mapping the host kernel, or
  458. * b) the guest used a large mapping which we're faking
  459. * Either way, we need to satisfy the fault without
  460. * invoking the guest. */
  461. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  462. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  463. r = RESUME_GUEST;
  464. } else {
  465. /* Guest has mapped and accessed a page which is not
  466. * actually RAM. */
  467. vcpu->arch.paddr_accessed = gpaddr;
  468. r = kvmppc_emulate_mmio(run, vcpu);
  469. kvmppc_account_exit(vcpu, MMIO_EXITS);
  470. }
  471. break;
  472. }
  473. case BOOKE_INTERRUPT_ITLB_MISS: {
  474. unsigned long eaddr = vcpu->arch.pc;
  475. gpa_t gpaddr;
  476. gfn_t gfn;
  477. int gtlb_index;
  478. r = RESUME_GUEST;
  479. /* Check the guest TLB. */
  480. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  481. if (gtlb_index < 0) {
  482. /* The guest didn't have a mapping for it. */
  483. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  484. kvmppc_mmu_itlb_miss(vcpu);
  485. kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
  486. break;
  487. }
  488. kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
  489. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  490. gfn = gpaddr >> PAGE_SHIFT;
  491. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  492. /* The guest TLB had a mapping, but the shadow TLB
  493. * didn't. This could be because:
  494. * a) the entry is mapping the host kernel, or
  495. * b) the guest used a large mapping which we're faking
  496. * Either way, we need to satisfy the fault without
  497. * invoking the guest. */
  498. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  499. } else {
  500. /* Guest mapped and leaped at non-RAM! */
  501. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
  502. }
  503. break;
  504. }
  505. case BOOKE_INTERRUPT_DEBUG: {
  506. u32 dbsr;
  507. vcpu->arch.pc = mfspr(SPRN_CSRR0);
  508. /* clear IAC events in DBSR register */
  509. dbsr = mfspr(SPRN_DBSR);
  510. dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
  511. mtspr(SPRN_DBSR, dbsr);
  512. run->exit_reason = KVM_EXIT_DEBUG;
  513. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  514. r = RESUME_HOST;
  515. break;
  516. }
  517. default:
  518. printk(KERN_EMERG "exit_nr %d\n", exit_nr);
  519. BUG();
  520. }
  521. local_irq_disable();
  522. kvmppc_core_deliver_interrupts(vcpu);
  523. if (!(r & RESUME_HOST)) {
  524. /* To avoid clobbering exit_reason, only check for signals if
  525. * we aren't already exiting to userspace for some other
  526. * reason. */
  527. if (signal_pending(current)) {
  528. run->exit_reason = KVM_EXIT_INTR;
  529. r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
  530. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  531. }
  532. }
  533. return r;
  534. }
  535. /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
  536. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  537. {
  538. int i;
  539. int r;
  540. vcpu->arch.pc = 0;
  541. vcpu->arch.shared->msr = 0;
  542. vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
  543. kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
  544. vcpu->arch.shadow_pid = 1;
  545. /* Eye-catching numbers so we know if the guest takes an interrupt
  546. * before it's programmed its own IVPR/IVORs. */
  547. vcpu->arch.ivpr = 0x55550000;
  548. for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
  549. vcpu->arch.ivor[i] = 0x7700 | i * 4;
  550. kvmppc_init_timing_stats(vcpu);
  551. r = kvmppc_core_vcpu_setup(vcpu);
  552. kvmppc_sanity_check(vcpu);
  553. return r;
  554. }
  555. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  556. {
  557. int i;
  558. regs->pc = vcpu->arch.pc;
  559. regs->cr = kvmppc_get_cr(vcpu);
  560. regs->ctr = vcpu->arch.ctr;
  561. regs->lr = vcpu->arch.lr;
  562. regs->xer = kvmppc_get_xer(vcpu);
  563. regs->msr = vcpu->arch.shared->msr;
  564. regs->srr0 = vcpu->arch.shared->srr0;
  565. regs->srr1 = vcpu->arch.shared->srr1;
  566. regs->pid = vcpu->arch.pid;
  567. regs->sprg0 = vcpu->arch.shared->sprg0;
  568. regs->sprg1 = vcpu->arch.shared->sprg1;
  569. regs->sprg2 = vcpu->arch.shared->sprg2;
  570. regs->sprg3 = vcpu->arch.shared->sprg3;
  571. regs->sprg4 = vcpu->arch.sprg4;
  572. regs->sprg5 = vcpu->arch.sprg5;
  573. regs->sprg6 = vcpu->arch.sprg6;
  574. regs->sprg7 = vcpu->arch.sprg7;
  575. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  576. regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
  577. return 0;
  578. }
  579. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  580. {
  581. int i;
  582. vcpu->arch.pc = regs->pc;
  583. kvmppc_set_cr(vcpu, regs->cr);
  584. vcpu->arch.ctr = regs->ctr;
  585. vcpu->arch.lr = regs->lr;
  586. kvmppc_set_xer(vcpu, regs->xer);
  587. kvmppc_set_msr(vcpu, regs->msr);
  588. vcpu->arch.shared->srr0 = regs->srr0;
  589. vcpu->arch.shared->srr1 = regs->srr1;
  590. kvmppc_set_pid(vcpu, regs->pid);
  591. vcpu->arch.shared->sprg0 = regs->sprg0;
  592. vcpu->arch.shared->sprg1 = regs->sprg1;
  593. vcpu->arch.shared->sprg2 = regs->sprg2;
  594. vcpu->arch.shared->sprg3 = regs->sprg3;
  595. vcpu->arch.sprg4 = regs->sprg4;
  596. vcpu->arch.sprg5 = regs->sprg5;
  597. vcpu->arch.sprg6 = regs->sprg6;
  598. vcpu->arch.sprg7 = regs->sprg7;
  599. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  600. kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
  601. return 0;
  602. }
  603. static void get_sregs_base(struct kvm_vcpu *vcpu,
  604. struct kvm_sregs *sregs)
  605. {
  606. u64 tb = get_tb();
  607. sregs->u.e.features |= KVM_SREGS_E_BASE;
  608. sregs->u.e.csrr0 = vcpu->arch.csrr0;
  609. sregs->u.e.csrr1 = vcpu->arch.csrr1;
  610. sregs->u.e.mcsr = vcpu->arch.mcsr;
  611. sregs->u.e.esr = vcpu->arch.esr;
  612. sregs->u.e.dear = vcpu->arch.shared->dar;
  613. sregs->u.e.tsr = vcpu->arch.tsr;
  614. sregs->u.e.tcr = vcpu->arch.tcr;
  615. sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
  616. sregs->u.e.tb = tb;
  617. sregs->u.e.vrsave = vcpu->arch.vrsave;
  618. }
  619. static int set_sregs_base(struct kvm_vcpu *vcpu,
  620. struct kvm_sregs *sregs)
  621. {
  622. if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
  623. return 0;
  624. vcpu->arch.csrr0 = sregs->u.e.csrr0;
  625. vcpu->arch.csrr1 = sregs->u.e.csrr1;
  626. vcpu->arch.mcsr = sregs->u.e.mcsr;
  627. vcpu->arch.esr = sregs->u.e.esr;
  628. vcpu->arch.shared->dar = sregs->u.e.dear;
  629. vcpu->arch.vrsave = sregs->u.e.vrsave;
  630. vcpu->arch.tcr = sregs->u.e.tcr;
  631. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC)
  632. vcpu->arch.dec = sregs->u.e.dec;
  633. kvmppc_emulate_dec(vcpu);
  634. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
  635. /*
  636. * FIXME: existing KVM timer handling is incomplete.
  637. * TSR cannot be read by the guest, and its value in
  638. * vcpu->arch is always zero. For now, just handle
  639. * the case where the caller is trying to inject a
  640. * decrementer interrupt.
  641. */
  642. if ((sregs->u.e.tsr & TSR_DIS) &&
  643. (vcpu->arch.tcr & TCR_DIE))
  644. kvmppc_core_queue_dec(vcpu);
  645. }
  646. return 0;
  647. }
  648. static void get_sregs_arch206(struct kvm_vcpu *vcpu,
  649. struct kvm_sregs *sregs)
  650. {
  651. sregs->u.e.features |= KVM_SREGS_E_ARCH206;
  652. sregs->u.e.pir = 0;
  653. sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
  654. sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
  655. sregs->u.e.decar = vcpu->arch.decar;
  656. sregs->u.e.ivpr = vcpu->arch.ivpr;
  657. }
  658. static int set_sregs_arch206(struct kvm_vcpu *vcpu,
  659. struct kvm_sregs *sregs)
  660. {
  661. if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
  662. return 0;
  663. if (sregs->u.e.pir != 0)
  664. return -EINVAL;
  665. vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
  666. vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
  667. vcpu->arch.decar = sregs->u.e.decar;
  668. vcpu->arch.ivpr = sregs->u.e.ivpr;
  669. return 0;
  670. }
  671. void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  672. {
  673. sregs->u.e.features |= KVM_SREGS_E_IVOR;
  674. sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
  675. sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
  676. sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
  677. sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
  678. sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
  679. sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
  680. sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
  681. sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
  682. sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
  683. sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
  684. sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
  685. sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
  686. sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
  687. sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
  688. sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
  689. sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
  690. }
  691. int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  692. {
  693. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  694. return 0;
  695. vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
  696. vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
  697. vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
  698. vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
  699. vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
  700. vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
  701. vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
  702. vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
  703. vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
  704. vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
  705. vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
  706. vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
  707. vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
  708. vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
  709. vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
  710. vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
  711. return 0;
  712. }
  713. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  714. struct kvm_sregs *sregs)
  715. {
  716. sregs->pvr = vcpu->arch.pvr;
  717. get_sregs_base(vcpu, sregs);
  718. get_sregs_arch206(vcpu, sregs);
  719. kvmppc_core_get_sregs(vcpu, sregs);
  720. return 0;
  721. }
  722. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  723. struct kvm_sregs *sregs)
  724. {
  725. int ret;
  726. if (vcpu->arch.pvr != sregs->pvr)
  727. return -EINVAL;
  728. ret = set_sregs_base(vcpu, sregs);
  729. if (ret < 0)
  730. return ret;
  731. ret = set_sregs_arch206(vcpu, sregs);
  732. if (ret < 0)
  733. return ret;
  734. return kvmppc_core_set_sregs(vcpu, sregs);
  735. }
  736. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  737. {
  738. return -ENOTSUPP;
  739. }
  740. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  741. {
  742. return -ENOTSUPP;
  743. }
  744. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  745. struct kvm_translation *tr)
  746. {
  747. int r;
  748. r = kvmppc_core_vcpu_translate(vcpu, tr);
  749. return r;
  750. }
  751. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  752. {
  753. return -ENOTSUPP;
  754. }
  755. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  756. struct kvm_userspace_memory_region *mem)
  757. {
  758. return 0;
  759. }
  760. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  761. struct kvm_userspace_memory_region *mem)
  762. {
  763. }
  764. int kvmppc_core_init_vm(struct kvm *kvm)
  765. {
  766. return 0;
  767. }
  768. void kvmppc_core_destroy_vm(struct kvm *kvm)
  769. {
  770. }
  771. int __init kvmppc_booke_init(void)
  772. {
  773. unsigned long ivor[16];
  774. unsigned long max_ivor = 0;
  775. int i;
  776. /* We install our own exception handlers by hijacking IVPR. IVPR must
  777. * be 16-bit aligned, so we need a 64KB allocation. */
  778. kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  779. VCPU_SIZE_ORDER);
  780. if (!kvmppc_booke_handlers)
  781. return -ENOMEM;
  782. /* XXX make sure our handlers are smaller than Linux's */
  783. /* Copy our interrupt handlers to match host IVORs. That way we don't
  784. * have to swap the IVORs on every guest/host transition. */
  785. ivor[0] = mfspr(SPRN_IVOR0);
  786. ivor[1] = mfspr(SPRN_IVOR1);
  787. ivor[2] = mfspr(SPRN_IVOR2);
  788. ivor[3] = mfspr(SPRN_IVOR3);
  789. ivor[4] = mfspr(SPRN_IVOR4);
  790. ivor[5] = mfspr(SPRN_IVOR5);
  791. ivor[6] = mfspr(SPRN_IVOR6);
  792. ivor[7] = mfspr(SPRN_IVOR7);
  793. ivor[8] = mfspr(SPRN_IVOR8);
  794. ivor[9] = mfspr(SPRN_IVOR9);
  795. ivor[10] = mfspr(SPRN_IVOR10);
  796. ivor[11] = mfspr(SPRN_IVOR11);
  797. ivor[12] = mfspr(SPRN_IVOR12);
  798. ivor[13] = mfspr(SPRN_IVOR13);
  799. ivor[14] = mfspr(SPRN_IVOR14);
  800. ivor[15] = mfspr(SPRN_IVOR15);
  801. for (i = 0; i < 16; i++) {
  802. if (ivor[i] > max_ivor)
  803. max_ivor = ivor[i];
  804. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  805. kvmppc_handlers_start + i * kvmppc_handler_len,
  806. kvmppc_handler_len);
  807. }
  808. flush_icache_range(kvmppc_booke_handlers,
  809. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  810. return 0;
  811. }
  812. void __exit kvmppc_booke_exit(void)
  813. {
  814. free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
  815. kvm_exit();
  816. }