booke.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  298. (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
  299. /* KVM PV hypercalls */
  300. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  301. r = RESUME_GUEST;
  302. } else {
  303. /* Guest syscalls */
  304. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
  305. }
  306. kvmppc_account_exit(vcpu, SYSCALL_EXITS);
  307. r = RESUME_GUEST;
  308. break;
  309. case BOOKE_INTERRUPT_DTLB_MISS: {
  310. unsigned long eaddr = vcpu->arch.fault_dear;
  311. int gtlb_index;
  312. gpa_t gpaddr;
  313. gfn_t gfn;
  314. /* Check the guest TLB. */
  315. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  316. if (gtlb_index < 0) {
  317. /* The guest didn't have a mapping for it. */
  318. kvmppc_core_queue_dtlb_miss(vcpu,
  319. vcpu->arch.fault_dear,
  320. vcpu->arch.fault_esr);
  321. kvmppc_mmu_dtlb_miss(vcpu);
  322. kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
  323. r = RESUME_GUEST;
  324. break;
  325. }
  326. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  327. gfn = gpaddr >> PAGE_SHIFT;
  328. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  329. /* The guest TLB had a mapping, but the shadow TLB
  330. * didn't, and it is RAM. This could be because:
  331. * a) the entry is mapping the host kernel, or
  332. * b) the guest used a large mapping which we're faking
  333. * Either way, we need to satisfy the fault without
  334. * invoking the guest. */
  335. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  336. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  337. r = RESUME_GUEST;
  338. } else {
  339. /* Guest has mapped and accessed a page which is not
  340. * actually RAM. */
  341. vcpu->arch.paddr_accessed = gpaddr;
  342. r = kvmppc_emulate_mmio(run, vcpu);
  343. kvmppc_account_exit(vcpu, MMIO_EXITS);
  344. }
  345. break;
  346. }
  347. case BOOKE_INTERRUPT_ITLB_MISS: {
  348. unsigned long eaddr = vcpu->arch.pc;
  349. gpa_t gpaddr;
  350. gfn_t gfn;
  351. int gtlb_index;
  352. r = RESUME_GUEST;
  353. /* Check the guest TLB. */
  354. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  355. if (gtlb_index < 0) {
  356. /* The guest didn't have a mapping for it. */
  357. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  358. kvmppc_mmu_itlb_miss(vcpu);
  359. kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
  360. break;
  361. }
  362. kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
  363. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  364. gfn = gpaddr >> PAGE_SHIFT;
  365. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  366. /* The guest TLB had a mapping, but the shadow TLB
  367. * didn't. This could be because:
  368. * a) the entry is mapping the host kernel, or
  369. * b) the guest used a large mapping which we're faking
  370. * Either way, we need to satisfy the fault without
  371. * invoking the guest. */
  372. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  373. } else {
  374. /* Guest mapped and leaped at non-RAM! */
  375. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
  376. }
  377. break;
  378. }
  379. case BOOKE_INTERRUPT_DEBUG: {
  380. u32 dbsr;
  381. vcpu->arch.pc = mfspr(SPRN_CSRR0);
  382. /* clear IAC events in DBSR register */
  383. dbsr = mfspr(SPRN_DBSR);
  384. dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
  385. mtspr(SPRN_DBSR, dbsr);
  386. run->exit_reason = KVM_EXIT_DEBUG;
  387. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  388. r = RESUME_HOST;
  389. break;
  390. }
  391. default:
  392. printk(KERN_EMERG "exit_nr %d\n", exit_nr);
  393. BUG();
  394. }
  395. local_irq_disable();
  396. kvmppc_core_deliver_interrupts(vcpu);
  397. if (!(r & RESUME_HOST)) {
  398. /* To avoid clobbering exit_reason, only check for signals if
  399. * we aren't already exiting to userspace for some other
  400. * reason. */
  401. if (signal_pending(current)) {
  402. run->exit_reason = KVM_EXIT_INTR;
  403. r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
  404. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  405. }
  406. }
  407. return r;
  408. }
  409. /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
  410. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  411. {
  412. vcpu->arch.pc = 0;
  413. vcpu->arch.shared->msr = 0;
  414. kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
  415. vcpu->arch.shadow_pid = 1;
  416. /* Eye-catching number so we know if the guest takes an interrupt
  417. * before it's programmed its own IVPR. */
  418. vcpu->arch.ivpr = 0x55550000;
  419. kvmppc_init_timing_stats(vcpu);
  420. return kvmppc_core_vcpu_setup(vcpu);
  421. }
  422. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  423. {
  424. int i;
  425. regs->pc = vcpu->arch.pc;
  426. regs->cr = kvmppc_get_cr(vcpu);
  427. regs->ctr = vcpu->arch.ctr;
  428. regs->lr = vcpu->arch.lr;
  429. regs->xer = kvmppc_get_xer(vcpu);
  430. regs->msr = vcpu->arch.shared->msr;
  431. regs->srr0 = vcpu->arch.shared->srr0;
  432. regs->srr1 = vcpu->arch.shared->srr1;
  433. regs->pid = vcpu->arch.pid;
  434. regs->sprg0 = vcpu->arch.shared->sprg0;
  435. regs->sprg1 = vcpu->arch.shared->sprg1;
  436. regs->sprg2 = vcpu->arch.shared->sprg2;
  437. regs->sprg3 = vcpu->arch.shared->sprg3;
  438. regs->sprg5 = vcpu->arch.sprg4;
  439. regs->sprg6 = vcpu->arch.sprg5;
  440. regs->sprg7 = vcpu->arch.sprg6;
  441. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  442. regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
  443. return 0;
  444. }
  445. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  446. {
  447. int i;
  448. vcpu->arch.pc = regs->pc;
  449. kvmppc_set_cr(vcpu, regs->cr);
  450. vcpu->arch.ctr = regs->ctr;
  451. vcpu->arch.lr = regs->lr;
  452. kvmppc_set_xer(vcpu, regs->xer);
  453. kvmppc_set_msr(vcpu, regs->msr);
  454. vcpu->arch.shared->srr0 = regs->srr0;
  455. vcpu->arch.shared->srr1 = regs->srr1;
  456. vcpu->arch.shared->sprg0 = regs->sprg0;
  457. vcpu->arch.shared->sprg1 = regs->sprg1;
  458. vcpu->arch.shared->sprg2 = regs->sprg2;
  459. vcpu->arch.shared->sprg3 = regs->sprg3;
  460. vcpu->arch.sprg5 = regs->sprg4;
  461. vcpu->arch.sprg6 = regs->sprg5;
  462. vcpu->arch.sprg7 = regs->sprg6;
  463. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  464. kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
  465. return 0;
  466. }
  467. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  468. struct kvm_sregs *sregs)
  469. {
  470. return -ENOTSUPP;
  471. }
  472. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  473. struct kvm_sregs *sregs)
  474. {
  475. return -ENOTSUPP;
  476. }
  477. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  478. {
  479. return -ENOTSUPP;
  480. }
  481. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  482. {
  483. return -ENOTSUPP;
  484. }
  485. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  486. struct kvm_translation *tr)
  487. {
  488. int r;
  489. r = kvmppc_core_vcpu_translate(vcpu, tr);
  490. return r;
  491. }
  492. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  493. {
  494. return -ENOTSUPP;
  495. }
  496. int __init kvmppc_booke_init(void)
  497. {
  498. unsigned long ivor[16];
  499. unsigned long max_ivor = 0;
  500. int i;
  501. /* We install our own exception handlers by hijacking IVPR. IVPR must
  502. * be 16-bit aligned, so we need a 64KB allocation. */
  503. kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  504. VCPU_SIZE_ORDER);
  505. if (!kvmppc_booke_handlers)
  506. return -ENOMEM;
  507. /* XXX make sure our handlers are smaller than Linux's */
  508. /* Copy our interrupt handlers to match host IVORs. That way we don't
  509. * have to swap the IVORs on every guest/host transition. */
  510. ivor[0] = mfspr(SPRN_IVOR0);
  511. ivor[1] = mfspr(SPRN_IVOR1);
  512. ivor[2] = mfspr(SPRN_IVOR2);
  513. ivor[3] = mfspr(SPRN_IVOR3);
  514. ivor[4] = mfspr(SPRN_IVOR4);
  515. ivor[5] = mfspr(SPRN_IVOR5);
  516. ivor[6] = mfspr(SPRN_IVOR6);
  517. ivor[7] = mfspr(SPRN_IVOR7);
  518. ivor[8] = mfspr(SPRN_IVOR8);
  519. ivor[9] = mfspr(SPRN_IVOR9);
  520. ivor[10] = mfspr(SPRN_IVOR10);
  521. ivor[11] = mfspr(SPRN_IVOR11);
  522. ivor[12] = mfspr(SPRN_IVOR12);
  523. ivor[13] = mfspr(SPRN_IVOR13);
  524. ivor[14] = mfspr(SPRN_IVOR14);
  525. ivor[15] = mfspr(SPRN_IVOR15);
  526. for (i = 0; i < 16; i++) {
  527. if (ivor[i] > max_ivor)
  528. max_ivor = ivor[i];
  529. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  530. kvmppc_handlers_start + i * kvmppc_handler_len,
  531. kvmppc_handler_len);
  532. }
  533. flush_icache_range(kvmppc_booke_handlers,
  534. kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
  535. return 0;
  536. }
  537. void __exit kvmppc_booke_exit(void)
  538. {
  539. free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
  540. kvm_exit();
  541. }