booke.c 26 KB

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