booke.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/kvm_host.h>
  23. #include <linux/gfp.h>
  24. #include <linux/module.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/fs.h>
  27. #include <asm/cputable.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/kvm_ppc.h>
  30. #include "timing.h"
  31. #include <asm/cacheflush.h>
  32. #include "booke.h"
  33. unsigned long kvmppc_booke_handlers;
  34. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  35. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  36. struct kvm_stats_debugfs_item debugfs_entries[] = {
  37. { "mmio", VCPU_STAT(mmio_exits) },
  38. { "dcr", VCPU_STAT(dcr_exits) },
  39. { "sig", VCPU_STAT(signal_exits) },
  40. { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
  41. { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
  42. { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
  43. { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
  44. { "sysc", VCPU_STAT(syscall_exits) },
  45. { "isi", VCPU_STAT(isi_exits) },
  46. { "dsi", VCPU_STAT(dsi_exits) },
  47. { "inst_emu", VCPU_STAT(emulated_inst_exits) },
  48. { "dec", VCPU_STAT(dec_exits) },
  49. { "ext_intr", VCPU_STAT(ext_intr_exits) },
  50. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  51. { NULL }
  52. };
  53. /* TODO: use vcpu_printf() */
  54. void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
  55. {
  56. int i;
  57. printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
  58. printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
  59. printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
  60. vcpu->arch.shared->srr1);
  61. printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
  62. for (i = 0; i < 32; i += 4) {
  63. printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
  64. kvmppc_get_gpr(vcpu, i),
  65. kvmppc_get_gpr(vcpu, i+1),
  66. kvmppc_get_gpr(vcpu, i+2),
  67. kvmppc_get_gpr(vcpu, i+3));
  68. }
  69. }
  70. static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
  71. unsigned int priority)
  72. {
  73. set_bit(priority, &vcpu->arch.pending_exceptions);
  74. }
  75. static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
  76. ulong dear_flags, ulong esr_flags)
  77. {
  78. vcpu->arch.queued_dear = dear_flags;
  79. vcpu->arch.queued_esr = esr_flags;
  80. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
  81. }
  82. static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
  83. ulong dear_flags, ulong esr_flags)
  84. {
  85. vcpu->arch.queued_dear = dear_flags;
  86. vcpu->arch.queued_esr = esr_flags;
  87. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
  88. }
  89. static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
  90. ulong esr_flags)
  91. {
  92. vcpu->arch.queued_esr = esr_flags;
  93. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
  94. }
  95. void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
  96. {
  97. vcpu->arch.queued_esr = esr_flags;
  98. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
  99. }
  100. void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
  101. {
  102. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
  103. }
  104. int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
  105. {
  106. return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  107. }
  108. void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
  109. {
  110. clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  111. }
  112. void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
  113. struct kvm_interrupt *irq)
  114. {
  115. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_EXTERNAL);
  116. }
  117. void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
  118. struct kvm_interrupt *irq)
  119. {
  120. clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
  121. }
  122. /* Deliver the interrupt of the corresponding priority, if possible. */
  123. static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
  124. unsigned int priority)
  125. {
  126. int allowed = 0;
  127. ulong uninitialized_var(msr_mask);
  128. bool update_esr = false, update_dear = false;
  129. switch (priority) {
  130. case BOOKE_IRQPRIO_DTLB_MISS:
  131. case BOOKE_IRQPRIO_DATA_STORAGE:
  132. update_dear = true;
  133. /* fall through */
  134. case BOOKE_IRQPRIO_INST_STORAGE:
  135. case BOOKE_IRQPRIO_PROGRAM:
  136. update_esr = true;
  137. /* fall through */
  138. case BOOKE_IRQPRIO_ITLB_MISS:
  139. case BOOKE_IRQPRIO_SYSCALL:
  140. case BOOKE_IRQPRIO_FP_UNAVAIL:
  141. case BOOKE_IRQPRIO_SPE_UNAVAIL:
  142. case BOOKE_IRQPRIO_SPE_FP_DATA:
  143. case BOOKE_IRQPRIO_SPE_FP_ROUND:
  144. case BOOKE_IRQPRIO_AP_UNAVAIL:
  145. case BOOKE_IRQPRIO_ALIGNMENT:
  146. allowed = 1;
  147. msr_mask = MSR_CE|MSR_ME|MSR_DE;
  148. break;
  149. case BOOKE_IRQPRIO_CRITICAL:
  150. case BOOKE_IRQPRIO_WATCHDOG:
  151. allowed = vcpu->arch.shared->msr & MSR_CE;
  152. msr_mask = MSR_ME;
  153. break;
  154. case BOOKE_IRQPRIO_MACHINE_CHECK:
  155. allowed = vcpu->arch.shared->msr & MSR_ME;
  156. msr_mask = 0;
  157. break;
  158. case BOOKE_IRQPRIO_EXTERNAL:
  159. case BOOKE_IRQPRIO_DECREMENTER:
  160. case BOOKE_IRQPRIO_FIT:
  161. allowed = vcpu->arch.shared->msr & MSR_EE;
  162. msr_mask = MSR_CE|MSR_ME|MSR_DE;
  163. break;
  164. case BOOKE_IRQPRIO_DEBUG:
  165. allowed = vcpu->arch.shared->msr & MSR_DE;
  166. msr_mask = MSR_ME;
  167. break;
  168. }
  169. if (allowed) {
  170. vcpu->arch.shared->srr0 = vcpu->arch.pc;
  171. vcpu->arch.shared->srr1 = vcpu->arch.shared->msr;
  172. vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
  173. if (update_esr == true)
  174. vcpu->arch.esr = vcpu->arch.queued_esr;
  175. if (update_dear == true)
  176. vcpu->arch.shared->dar = vcpu->arch.queued_dear;
  177. kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
  178. clear_bit(priority, &vcpu->arch.pending_exceptions);
  179. }
  180. return allowed;
  181. }
  182. /* Check pending exceptions and deliver one, if possible. */
  183. void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
  184. {
  185. unsigned long *pending = &vcpu->arch.pending_exceptions;
  186. unsigned int priority;
  187. priority = __ffs(*pending);
  188. while (priority <= BOOKE_IRQPRIO_MAX) {
  189. if (kvmppc_booke_irqprio_deliver(vcpu, priority))
  190. break;
  191. priority = find_next_bit(pending,
  192. BITS_PER_BYTE * sizeof(*pending),
  193. priority + 1);
  194. }
  195. }
  196. /**
  197. * kvmppc_handle_exit
  198. *
  199. * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
  200. */
  201. int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  202. unsigned int exit_nr)
  203. {
  204. enum emulation_result er;
  205. int r = RESUME_HOST;
  206. /* update before a new last_exit_type is rewritten */
  207. kvmppc_update_timing_stats(vcpu);
  208. local_irq_enable();
  209. run->exit_reason = KVM_EXIT_UNKNOWN;
  210. run->ready_for_interrupt_injection = 1;
  211. switch (exit_nr) {
  212. case BOOKE_INTERRUPT_MACHINE_CHECK:
  213. printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
  214. kvmppc_dump_vcpu(vcpu);
  215. r = RESUME_HOST;
  216. break;
  217. case BOOKE_INTERRUPT_EXTERNAL:
  218. kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
  219. if (need_resched())
  220. cond_resched();
  221. r = RESUME_GUEST;
  222. break;
  223. case BOOKE_INTERRUPT_DECREMENTER:
  224. /* Since we switched IVPR back to the host's value, the host
  225. * handled this interrupt the moment we enabled interrupts.
  226. * Now we just offer it a chance to reschedule the guest. */
  227. kvmppc_account_exit(vcpu, DEC_EXITS);
  228. if (need_resched())
  229. cond_resched();
  230. r = RESUME_GUEST;
  231. break;
  232. case BOOKE_INTERRUPT_PROGRAM:
  233. if (vcpu->arch.shared->msr & MSR_PR) {
  234. /* Program traps generated by user-level software must be handled
  235. * by the guest kernel. */
  236. kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
  237. r = RESUME_GUEST;
  238. kvmppc_account_exit(vcpu, USR_PR_INST);
  239. break;
  240. }
  241. er = kvmppc_emulate_instruction(run, vcpu);
  242. switch (er) {
  243. case EMULATE_DONE:
  244. /* don't overwrite subtypes, just account kvm_stats */
  245. kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
  246. /* Future optimization: only reload non-volatiles if
  247. * they were actually modified by emulation. */
  248. r = RESUME_GUEST_NV;
  249. break;
  250. case EMULATE_DO_DCR:
  251. run->exit_reason = KVM_EXIT_DCR;
  252. r = RESUME_HOST;
  253. break;
  254. case EMULATE_FAIL:
  255. /* XXX Deliver Program interrupt to guest. */
  256. printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
  257. __func__, vcpu->arch.pc, vcpu->arch.last_inst);
  258. /* For debugging, encode the failing instruction and
  259. * report it to userspace. */
  260. run->hw.hardware_exit_reason = ~0ULL << 32;
  261. run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
  262. r = RESUME_HOST;
  263. break;
  264. default:
  265. BUG();
  266. }
  267. break;
  268. case BOOKE_INTERRUPT_FP_UNAVAIL:
  269. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
  270. kvmppc_account_exit(vcpu, FP_UNAVAIL);
  271. r = RESUME_GUEST;
  272. break;
  273. case BOOKE_INTERRUPT_SPE_UNAVAIL:
  274. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_UNAVAIL);
  275. r = RESUME_GUEST;
  276. break;
  277. case BOOKE_INTERRUPT_SPE_FP_DATA:
  278. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
  279. r = RESUME_GUEST;
  280. break;
  281. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  282. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
  283. r = RESUME_GUEST;
  284. break;
  285. case BOOKE_INTERRUPT_DATA_STORAGE:
  286. kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
  287. vcpu->arch.fault_esr);
  288. kvmppc_account_exit(vcpu, DSI_EXITS);
  289. r = RESUME_GUEST;
  290. break;
  291. case BOOKE_INTERRUPT_INST_STORAGE:
  292. kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
  293. kvmppc_account_exit(vcpu, ISI_EXITS);
  294. r = RESUME_GUEST;
  295. break;
  296. case BOOKE_INTERRUPT_SYSCALL:
  297. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
  298. kvmppc_account_exit(vcpu, SYSCALL_EXITS);
  299. r = RESUME_GUEST;
  300. break;
  301. case BOOKE_INTERRUPT_DTLB_MISS: {
  302. unsigned long eaddr = vcpu->arch.fault_dear;
  303. int gtlb_index;
  304. gpa_t gpaddr;
  305. gfn_t gfn;
  306. /* Check the guest TLB. */
  307. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  308. if (gtlb_index < 0) {
  309. /* The guest didn't have a mapping for it. */
  310. kvmppc_core_queue_dtlb_miss(vcpu,
  311. vcpu->arch.fault_dear,
  312. vcpu->arch.fault_esr);
  313. kvmppc_mmu_dtlb_miss(vcpu);
  314. kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
  315. r = RESUME_GUEST;
  316. break;
  317. }
  318. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  319. gfn = gpaddr >> PAGE_SHIFT;
  320. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  321. /* The guest TLB had a mapping, but the shadow TLB
  322. * didn't, and it is RAM. This could be because:
  323. * a) the entry is mapping the host kernel, or
  324. * b) the guest used a large mapping which we're faking
  325. * Either way, we need to satisfy the fault without
  326. * invoking the guest. */
  327. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  328. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  329. r = RESUME_GUEST;
  330. } else {
  331. /* Guest has mapped and accessed a page which is not
  332. * actually RAM. */
  333. vcpu->arch.paddr_accessed = gpaddr;
  334. r = kvmppc_emulate_mmio(run, vcpu);
  335. kvmppc_account_exit(vcpu, MMIO_EXITS);
  336. }
  337. break;
  338. }
  339. case BOOKE_INTERRUPT_ITLB_MISS: {
  340. unsigned long eaddr = vcpu->arch.pc;
  341. gpa_t gpaddr;
  342. gfn_t gfn;
  343. int gtlb_index;
  344. r = RESUME_GUEST;
  345. /* Check the guest TLB. */
  346. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  347. if (gtlb_index < 0) {
  348. /* The guest didn't have a mapping for it. */
  349. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  350. kvmppc_mmu_itlb_miss(vcpu);
  351. kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
  352. break;
  353. }
  354. kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
  355. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  356. gfn = gpaddr >> PAGE_SHIFT;
  357. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  358. /* The guest TLB had a mapping, but the shadow TLB
  359. * didn't. This could be because:
  360. * a) the entry is mapping the host kernel, or
  361. * b) the guest used a large mapping which we're faking
  362. * Either way, we need to satisfy the fault without
  363. * invoking the guest. */
  364. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  365. } else {
  366. /* Guest mapped and leaped at non-RAM! */
  367. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
  368. }
  369. break;
  370. }
  371. case BOOKE_INTERRUPT_DEBUG: {
  372. u32 dbsr;
  373. vcpu->arch.pc = mfspr(SPRN_CSRR0);
  374. /* clear IAC events in DBSR register */
  375. dbsr = mfspr(SPRN_DBSR);
  376. dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
  377. mtspr(SPRN_DBSR, dbsr);
  378. run->exit_reason = KVM_EXIT_DEBUG;
  379. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  380. r = RESUME_HOST;
  381. break;
  382. }
  383. default:
  384. printk(KERN_EMERG "exit_nr %d\n", exit_nr);
  385. BUG();
  386. }
  387. local_irq_disable();
  388. kvmppc_core_deliver_interrupts(vcpu);
  389. if (!(r & RESUME_HOST)) {
  390. /* To avoid clobbering exit_reason, only check for signals if
  391. * we aren't already exiting to userspace for some other
  392. * reason. */
  393. if (signal_pending(current)) {
  394. run->exit_reason = KVM_EXIT_INTR;
  395. r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
  396. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  397. }
  398. }
  399. return r;
  400. }
  401. /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
  402. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  403. {
  404. vcpu->arch.pc = 0;
  405. vcpu->arch.shared->msr = 0;
  406. kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
  407. vcpu->arch.shadow_pid = 1;
  408. /* Eye-catching number so we know if the guest takes an interrupt
  409. * before it's programmed its own IVPR. */
  410. vcpu->arch.ivpr = 0x55550000;
  411. kvmppc_init_timing_stats(vcpu);
  412. return kvmppc_core_vcpu_setup(vcpu);
  413. }
  414. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  415. {
  416. int i;
  417. regs->pc = vcpu->arch.pc;
  418. regs->cr = kvmppc_get_cr(vcpu);
  419. regs->ctr = vcpu->arch.ctr;
  420. regs->lr = vcpu->arch.lr;
  421. regs->xer = kvmppc_get_xer(vcpu);
  422. regs->msr = vcpu->arch.shared->msr;
  423. regs->srr0 = vcpu->arch.shared->srr0;
  424. regs->srr1 = vcpu->arch.shared->srr1;
  425. regs->pid = vcpu->arch.pid;
  426. regs->sprg0 = vcpu->arch.sprg0;
  427. regs->sprg1 = vcpu->arch.sprg1;
  428. regs->sprg2 = vcpu->arch.sprg2;
  429. regs->sprg3 = vcpu->arch.sprg3;
  430. regs->sprg5 = vcpu->arch.sprg4;
  431. regs->sprg6 = vcpu->arch.sprg5;
  432. regs->sprg7 = vcpu->arch.sprg6;
  433. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  434. regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
  435. return 0;
  436. }
  437. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  438. {
  439. int i;
  440. vcpu->arch.pc = regs->pc;
  441. kvmppc_set_cr(vcpu, regs->cr);
  442. vcpu->arch.ctr = regs->ctr;
  443. vcpu->arch.lr = regs->lr;
  444. kvmppc_set_xer(vcpu, regs->xer);
  445. kvmppc_set_msr(vcpu, regs->msr);
  446. vcpu->arch.shared->srr0 = regs->srr0;
  447. vcpu->arch.shared->srr1 = regs->srr1;
  448. vcpu->arch.sprg0 = regs->sprg0;
  449. vcpu->arch.sprg1 = regs->sprg1;
  450. vcpu->arch.sprg2 = regs->sprg2;
  451. vcpu->arch.sprg3 = regs->sprg3;
  452. vcpu->arch.sprg5 = regs->sprg4;
  453. vcpu->arch.sprg6 = regs->sprg5;
  454. vcpu->arch.sprg7 = regs->sprg6;
  455. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  456. kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
  457. return 0;
  458. }
  459. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  460. struct kvm_sregs *sregs)
  461. {
  462. return -ENOTSUPP;
  463. }
  464. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  465. struct kvm_sregs *sregs)
  466. {
  467. return -ENOTSUPP;
  468. }
  469. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  470. {
  471. return -ENOTSUPP;
  472. }
  473. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  474. {
  475. return -ENOTSUPP;
  476. }
  477. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  478. struct kvm_translation *tr)
  479. {
  480. int r;
  481. r = kvmppc_core_vcpu_translate(vcpu, tr);
  482. return r;
  483. }
  484. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  485. {
  486. return -ENOTSUPP;
  487. }
  488. int __init kvmppc_booke_init(void)
  489. {
  490. unsigned long ivor[16];
  491. unsigned long max_ivor = 0;
  492. int i;
  493. /* We install our own exception handlers by hijacking IVPR. IVPR must
  494. * be 16-bit aligned, so we need a 64KB allocation. */
  495. kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  496. VCPU_SIZE_ORDER);
  497. if (!kvmppc_booke_handlers)
  498. return -ENOMEM;
  499. /* XXX make sure our handlers are smaller than Linux's */
  500. /* Copy our interrupt handlers to match host IVORs. That way we don't
  501. * have to swap the IVORs on every guest/host transition. */
  502. ivor[0] = mfspr(SPRN_IVOR0);
  503. ivor[1] = mfspr(SPRN_IVOR1);
  504. ivor[2] = mfspr(SPRN_IVOR2);
  505. ivor[3] = mfspr(SPRN_IVOR3);
  506. ivor[4] = mfspr(SPRN_IVOR4);
  507. ivor[5] = mfspr(SPRN_IVOR5);
  508. ivor[6] = mfspr(SPRN_IVOR6);
  509. ivor[7] = mfspr(SPRN_IVOR7);
  510. ivor[8] = mfspr(SPRN_IVOR8);
  511. ivor[9] = mfspr(SPRN_IVOR9);
  512. ivor[10] = mfspr(SPRN_IVOR10);
  513. ivor[11] = mfspr(SPRN_IVOR11);
  514. ivor[12] = mfspr(SPRN_IVOR12);
  515. ivor[13] = mfspr(SPRN_IVOR13);
  516. ivor[14] = mfspr(SPRN_IVOR14);
  517. ivor[15] = mfspr(SPRN_IVOR15);
  518. for (i = 0; i < 16; i++) {
  519. if (ivor[i] > max_ivor)
  520. max_ivor = ivor[i];
  521. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  522. kvmppc_handlers_start + i * kvmppc_handler_len,
  523. kvmppc_handler_len);
  524. }
  525. flush_icache_range(kvmppc_booke_handlers,
  526. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  527. return 0;
  528. }
  529. void __exit kvmppc_booke_exit(void)
  530. {
  531. free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
  532. kvm_exit();
  533. }