1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
6 * Alexander Graf <agraf@suse.de>
7 * Kevin Wolf <mail@kevin-wolf.de>
10 * This file is derived from arch/powerpc/kvm/44x.c,
11 * by Hollis Blanchard <hollisb@us.ibm.com>.
14 #include <linux/kvm_host.h>
15 #include <linux/err.h>
16 #include <linux/export.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/miscdevice.h>
20 #include <linux/gfp.h>
21 #include <linux/sched.h>
22 #include <linux/vmalloc.h>
23 #include <linux/highmem.h>
26 #include <asm/cputable.h>
27 #include <asm/cacheflush.h>
28 #include <linux/uaccess.h>
30 #include <asm/kvm_ppc.h>
31 #include <asm/kvm_book3s.h>
32 #include <asm/mmu_context.h>
39 /* #define EXIT_DEBUG */
41 const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
42 KVM_GENERIC_VM_STATS(),
43 STATS_DESC_ICOUNTER(VM, num_2M_pages),
44 STATS_DESC_ICOUNTER(VM, num_1G_pages)
47 const struct kvm_stats_header kvm_vm_stats_header = {
48 .name_size = KVM_STATS_NAME_SIZE,
49 .num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
50 .id_offset = sizeof(struct kvm_stats_header),
51 .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
52 .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
53 sizeof(kvm_vm_stats_desc),
56 const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
57 KVM_GENERIC_VCPU_STATS(),
58 STATS_DESC_COUNTER(VCPU, sum_exits),
59 STATS_DESC_COUNTER(VCPU, mmio_exits),
60 STATS_DESC_COUNTER(VCPU, signal_exits),
61 STATS_DESC_COUNTER(VCPU, light_exits),
62 STATS_DESC_COUNTER(VCPU, itlb_real_miss_exits),
63 STATS_DESC_COUNTER(VCPU, itlb_virt_miss_exits),
64 STATS_DESC_COUNTER(VCPU, dtlb_real_miss_exits),
65 STATS_DESC_COUNTER(VCPU, dtlb_virt_miss_exits),
66 STATS_DESC_COUNTER(VCPU, syscall_exits),
67 STATS_DESC_COUNTER(VCPU, isi_exits),
68 STATS_DESC_COUNTER(VCPU, dsi_exits),
69 STATS_DESC_COUNTER(VCPU, emulated_inst_exits),
70 STATS_DESC_COUNTER(VCPU, dec_exits),
71 STATS_DESC_COUNTER(VCPU, ext_intr_exits),
72 STATS_DESC_COUNTER(VCPU, halt_successful_wait),
73 STATS_DESC_COUNTER(VCPU, dbell_exits),
74 STATS_DESC_COUNTER(VCPU, gdbell_exits),
75 STATS_DESC_COUNTER(VCPU, ld),
76 STATS_DESC_COUNTER(VCPU, st),
77 STATS_DESC_COUNTER(VCPU, pf_storage),
78 STATS_DESC_COUNTER(VCPU, pf_instruc),
79 STATS_DESC_COUNTER(VCPU, sp_storage),
80 STATS_DESC_COUNTER(VCPU, sp_instruc),
81 STATS_DESC_COUNTER(VCPU, queue_intr),
82 STATS_DESC_COUNTER(VCPU, ld_slow),
83 STATS_DESC_COUNTER(VCPU, st_slow),
84 STATS_DESC_COUNTER(VCPU, pthru_all),
85 STATS_DESC_COUNTER(VCPU, pthru_host),
86 STATS_DESC_COUNTER(VCPU, pthru_bad_aff)
89 const struct kvm_stats_header kvm_vcpu_stats_header = {
90 .name_size = KVM_STATS_NAME_SIZE,
91 .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
92 .id_offset = sizeof(struct kvm_stats_header),
93 .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
94 .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
95 sizeof(kvm_vcpu_stats_desc),
98 static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
99 unsigned long pending_now, unsigned long old_pending)
101 if (is_kvmppc_hv_enabled(vcpu->kvm))
104 kvmppc_set_int_pending(vcpu, 1);
105 else if (old_pending)
106 kvmppc_set_int_pending(vcpu, 0);
109 static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
115 if (is_kvmppc_hv_enabled(vcpu->kvm))
118 crit_raw = kvmppc_get_critical(vcpu);
119 crit_r1 = kvmppc_get_gpr(vcpu, 1);
121 /* Truncate crit indicators in 32 bit mode */
122 if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
123 crit_raw &= 0xffffffff;
124 crit_r1 &= 0xffffffff;
127 /* Critical section when crit == r1 */
128 crit = (crit_raw == crit_r1);
129 /* ... and we're in supervisor mode */
130 crit = crit && !(kvmppc_get_msr(vcpu) & MSR_PR);
135 void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
137 vcpu->kvm->arch.kvm_ops->inject_interrupt(vcpu, vec, flags);
140 static int kvmppc_book3s_vec2irqprio(unsigned int vec)
145 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
146 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
147 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
148 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
149 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
150 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
151 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
152 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
153 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
154 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
155 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
156 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
157 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
158 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
159 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
160 case 0xf60: prio = BOOK3S_IRQPRIO_FAC_UNAVAIL; break;
161 default: prio = BOOK3S_IRQPRIO_MAX; break;
167 void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
170 unsigned long old_pending = vcpu->arch.pending_exceptions;
172 clear_bit(kvmppc_book3s_vec2irqprio(vec),
173 &vcpu->arch.pending_exceptions);
175 kvmppc_update_int_pending(vcpu, vcpu->arch.pending_exceptions,
179 void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
181 vcpu->stat.queue_intr++;
183 set_bit(kvmppc_book3s_vec2irqprio(vec),
184 &vcpu->arch.pending_exceptions);
186 printk(KERN_INFO "Queueing interrupt %x\n", vec);
189 EXPORT_SYMBOL_GPL(kvmppc_book3s_queue_irqprio);
191 void kvmppc_core_queue_machine_check(struct kvm_vcpu *vcpu, ulong flags)
193 /* might as well deliver this straight away */
194 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_MACHINE_CHECK, flags);
196 EXPORT_SYMBOL_GPL(kvmppc_core_queue_machine_check);
198 void kvmppc_core_queue_syscall(struct kvm_vcpu *vcpu)
200 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_SYSCALL, 0);
202 EXPORT_SYMBOL(kvmppc_core_queue_syscall);
204 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
206 /* might as well deliver this straight away */
207 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_PROGRAM, flags);
209 EXPORT_SYMBOL_GPL(kvmppc_core_queue_program);
211 void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu)
213 /* might as well deliver this straight away */
214 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, 0);
217 void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu)
219 /* might as well deliver this straight away */
220 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_ALTIVEC, 0);
223 void kvmppc_core_queue_vsx_unavail(struct kvm_vcpu *vcpu)
225 /* might as well deliver this straight away */
226 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_VSX, 0);
229 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
231 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
233 EXPORT_SYMBOL_GPL(kvmppc_core_queue_dec);
235 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
237 return test_bit(BOOK3S_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
239 EXPORT_SYMBOL_GPL(kvmppc_core_pending_dec);
241 void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
243 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
245 EXPORT_SYMBOL_GPL(kvmppc_core_dequeue_dec);
247 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
248 struct kvm_interrupt *irq)
251 * This case (KVM_INTERRUPT_SET) should never actually arise for
252 * a pseries guest (because pseries guests expect their interrupt
253 * controllers to continue asserting an external interrupt request
254 * until it is acknowledged at the interrupt controller), but is
255 * included to avoid ABI breakage and potentially for other
258 * There is a subtlety here: HV KVM does not test the
259 * external_oneshot flag in the code that synthesizes
260 * external interrupts for the guest just before entering
261 * the guest. That is OK even if userspace did do a
262 * KVM_INTERRUPT_SET on a pseries guest vcpu, because the
263 * caller (kvm_vcpu_ioctl_interrupt) does a kvm_vcpu_kick()
264 * which ends up doing a smp_send_reschedule(), which will
265 * pull the guest all the way out to the host, meaning that
266 * we will call kvmppc_core_prepare_to_enter() before entering
267 * the guest again, and that will handle the external_oneshot
270 if (irq->irq == KVM_INTERRUPT_SET)
271 vcpu->arch.external_oneshot = 1;
273 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
276 void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
278 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
281 void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu, ulong dar,
284 kvmppc_set_dar(vcpu, dar);
285 kvmppc_set_dsisr(vcpu, flags);
286 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_DATA_STORAGE, 0);
288 EXPORT_SYMBOL_GPL(kvmppc_core_queue_data_storage);
290 void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, ulong flags)
292 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_INST_STORAGE, flags);
294 EXPORT_SYMBOL_GPL(kvmppc_core_queue_inst_storage);
296 static int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu,
297 unsigned int priority)
301 bool crit = kvmppc_critical_section(vcpu);
304 case BOOK3S_IRQPRIO_DECREMENTER:
305 deliver = (kvmppc_get_msr(vcpu) & MSR_EE) && !crit;
306 vec = BOOK3S_INTERRUPT_DECREMENTER;
308 case BOOK3S_IRQPRIO_EXTERNAL:
309 deliver = (kvmppc_get_msr(vcpu) & MSR_EE) && !crit;
310 vec = BOOK3S_INTERRUPT_EXTERNAL;
312 case BOOK3S_IRQPRIO_SYSTEM_RESET:
313 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
315 case BOOK3S_IRQPRIO_MACHINE_CHECK:
316 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
318 case BOOK3S_IRQPRIO_DATA_STORAGE:
319 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
321 case BOOK3S_IRQPRIO_INST_STORAGE:
322 vec = BOOK3S_INTERRUPT_INST_STORAGE;
324 case BOOK3S_IRQPRIO_DATA_SEGMENT:
325 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
327 case BOOK3S_IRQPRIO_INST_SEGMENT:
328 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
330 case BOOK3S_IRQPRIO_ALIGNMENT:
331 vec = BOOK3S_INTERRUPT_ALIGNMENT;
333 case BOOK3S_IRQPRIO_PROGRAM:
334 vec = BOOK3S_INTERRUPT_PROGRAM;
336 case BOOK3S_IRQPRIO_VSX:
337 vec = BOOK3S_INTERRUPT_VSX;
339 case BOOK3S_IRQPRIO_ALTIVEC:
340 vec = BOOK3S_INTERRUPT_ALTIVEC;
342 case BOOK3S_IRQPRIO_FP_UNAVAIL:
343 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
345 case BOOK3S_IRQPRIO_SYSCALL:
346 vec = BOOK3S_INTERRUPT_SYSCALL;
348 case BOOK3S_IRQPRIO_DEBUG:
349 vec = BOOK3S_INTERRUPT_TRACE;
351 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
352 vec = BOOK3S_INTERRUPT_PERFMON;
354 case BOOK3S_IRQPRIO_FAC_UNAVAIL:
355 vec = BOOK3S_INTERRUPT_FAC_UNAVAIL;
359 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
364 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
368 kvmppc_inject_interrupt(vcpu, vec, 0);
374 * This function determines if an irqprio should be cleared once issued.
376 static bool clear_irqprio(struct kvm_vcpu *vcpu, unsigned int priority)
379 case BOOK3S_IRQPRIO_DECREMENTER:
380 /* DEC interrupts get cleared by mtdec */
382 case BOOK3S_IRQPRIO_EXTERNAL:
384 * External interrupts get cleared by userspace
385 * except when set by the KVM_INTERRUPT ioctl with
386 * KVM_INTERRUPT_SET (not KVM_INTERRUPT_SET_LEVEL).
388 if (vcpu->arch.external_oneshot) {
389 vcpu->arch.external_oneshot = 0;
398 int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
400 unsigned long *pending = &vcpu->arch.pending_exceptions;
401 unsigned long old_pending = vcpu->arch.pending_exceptions;
402 unsigned int priority;
405 if (vcpu->arch.pending_exceptions)
406 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
408 priority = __ffs(*pending);
409 while (priority < BOOK3S_IRQPRIO_MAX) {
410 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
411 clear_irqprio(vcpu, priority)) {
412 clear_bit(priority, &vcpu->arch.pending_exceptions);
416 priority = find_next_bit(pending,
417 BITS_PER_BYTE * sizeof(*pending),
421 /* Tell the guest about our interrupt status */
422 kvmppc_update_int_pending(vcpu, *pending, old_pending);
426 EXPORT_SYMBOL_GPL(kvmppc_core_prepare_to_enter);
428 kvm_pfn_t kvmppc_gpa_to_pfn(struct kvm_vcpu *vcpu, gpa_t gpa, bool writing,
431 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM;
432 gfn_t gfn = gpa >> PAGE_SHIFT;
434 if (!(kvmppc_get_msr(vcpu) & MSR_SF))
435 mp_pa = (uint32_t)mp_pa;
437 /* Magic page override */
439 if (unlikely(mp_pa) && unlikely((gpa & KVM_PAM) == mp_pa)) {
440 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
443 pfn = (kvm_pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
444 get_page(pfn_to_page(pfn));
450 return gfn_to_pfn_prot(vcpu->kvm, gfn, writing, writable);
452 EXPORT_SYMBOL_GPL(kvmppc_gpa_to_pfn);
454 int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, enum xlate_instdata xlid,
455 enum xlate_readwrite xlrw, struct kvmppc_pte *pte)
457 bool data = (xlid == XLATE_DATA);
458 bool iswrite = (xlrw == XLATE_WRITE);
459 int relocated = (kvmppc_get_msr(vcpu) & (data ? MSR_DR : MSR_IR));
463 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data, iswrite);
466 pte->raddr = eaddr & KVM_PAM;
467 pte->vpage = VSID_REAL | eaddr >> 12;
468 pte->may_read = true;
469 pte->may_write = true;
470 pte->may_execute = true;
473 if ((kvmppc_get_msr(vcpu) & (MSR_IR | MSR_DR)) == MSR_DR &&
475 if ((vcpu->arch.hflags & BOOK3S_HFLAG_SPLIT_HACK) &&
476 ((eaddr & SPLIT_HACK_MASK) == SPLIT_HACK_OFFS))
477 pte->raddr &= ~SPLIT_HACK_MASK;
484 int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
485 enum instruction_fetch_type type, u32 *inst)
487 ulong pc = kvmppc_get_pc(vcpu);
493 r = kvmppc_ld(vcpu, &pc, sizeof(u32), inst, false);
494 if (r == EMULATE_DONE)
497 return EMULATE_AGAIN;
499 EXPORT_SYMBOL_GPL(kvmppc_load_last_inst);
501 int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
506 void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
510 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
511 struct kvm_sregs *sregs)
516 ret = vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
522 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
523 struct kvm_sregs *sregs)
528 ret = vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
534 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
538 regs->pc = kvmppc_get_pc(vcpu);
539 regs->cr = kvmppc_get_cr(vcpu);
540 regs->ctr = kvmppc_get_ctr(vcpu);
541 regs->lr = kvmppc_get_lr(vcpu);
542 regs->xer = kvmppc_get_xer(vcpu);
543 regs->msr = kvmppc_get_msr(vcpu);
544 regs->srr0 = kvmppc_get_srr0(vcpu);
545 regs->srr1 = kvmppc_get_srr1(vcpu);
546 regs->pid = vcpu->arch.pid;
547 regs->sprg0 = kvmppc_get_sprg0(vcpu);
548 regs->sprg1 = kvmppc_get_sprg1(vcpu);
549 regs->sprg2 = kvmppc_get_sprg2(vcpu);
550 regs->sprg3 = kvmppc_get_sprg3(vcpu);
551 regs->sprg4 = kvmppc_get_sprg4(vcpu);
552 regs->sprg5 = kvmppc_get_sprg5(vcpu);
553 regs->sprg6 = kvmppc_get_sprg6(vcpu);
554 regs->sprg7 = kvmppc_get_sprg7(vcpu);
556 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
557 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
562 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
566 kvmppc_set_pc(vcpu, regs->pc);
567 kvmppc_set_cr(vcpu, regs->cr);
568 kvmppc_set_ctr(vcpu, regs->ctr);
569 kvmppc_set_lr(vcpu, regs->lr);
570 kvmppc_set_xer(vcpu, regs->xer);
571 kvmppc_set_msr(vcpu, regs->msr);
572 kvmppc_set_srr0(vcpu, regs->srr0);
573 kvmppc_set_srr1(vcpu, regs->srr1);
574 kvmppc_set_sprg0(vcpu, regs->sprg0);
575 kvmppc_set_sprg1(vcpu, regs->sprg1);
576 kvmppc_set_sprg2(vcpu, regs->sprg2);
577 kvmppc_set_sprg3(vcpu, regs->sprg3);
578 kvmppc_set_sprg4(vcpu, regs->sprg4);
579 kvmppc_set_sprg5(vcpu, regs->sprg5);
580 kvmppc_set_sprg6(vcpu, regs->sprg6);
581 kvmppc_set_sprg7(vcpu, regs->sprg7);
583 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
584 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
589 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
594 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
599 int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
600 union kvmppc_one_reg *val)
605 r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, id, val);
609 case KVM_REG_PPC_DAR:
610 *val = get_reg_val(id, kvmppc_get_dar(vcpu));
612 case KVM_REG_PPC_DSISR:
613 *val = get_reg_val(id, kvmppc_get_dsisr(vcpu));
615 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
616 i = id - KVM_REG_PPC_FPR0;
617 *val = get_reg_val(id, VCPU_FPR(vcpu, i));
619 case KVM_REG_PPC_FPSCR:
620 *val = get_reg_val(id, vcpu->arch.fp.fpscr);
623 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
624 if (cpu_has_feature(CPU_FTR_VSX)) {
625 i = id - KVM_REG_PPC_VSR0;
626 val->vsxval[0] = vcpu->arch.fp.fpr[i][0];
627 val->vsxval[1] = vcpu->arch.fp.fpr[i][1];
632 #endif /* CONFIG_VSX */
633 case KVM_REG_PPC_DEBUG_INST:
634 *val = get_reg_val(id, INS_TW);
636 #ifdef CONFIG_KVM_XICS
637 case KVM_REG_PPC_ICP_STATE:
638 if (!vcpu->arch.icp && !vcpu->arch.xive_vcpu) {
643 *val = get_reg_val(id, kvmppc_xive_get_icp(vcpu));
645 *val = get_reg_val(id, kvmppc_xics_get_icp(vcpu));
647 #endif /* CONFIG_KVM_XICS */
648 #ifdef CONFIG_KVM_XIVE
649 case KVM_REG_PPC_VP_STATE:
650 if (!vcpu->arch.xive_vcpu) {
655 r = kvmppc_xive_native_get_vp(vcpu, val);
659 #endif /* CONFIG_KVM_XIVE */
660 case KVM_REG_PPC_FSCR:
661 *val = get_reg_val(id, vcpu->arch.fscr);
663 case KVM_REG_PPC_TAR:
664 *val = get_reg_val(id, vcpu->arch.tar);
666 case KVM_REG_PPC_EBBHR:
667 *val = get_reg_val(id, vcpu->arch.ebbhr);
669 case KVM_REG_PPC_EBBRR:
670 *val = get_reg_val(id, vcpu->arch.ebbrr);
672 case KVM_REG_PPC_BESCR:
673 *val = get_reg_val(id, vcpu->arch.bescr);
676 *val = get_reg_val(id, vcpu->arch.ic);
687 int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
688 union kvmppc_one_reg *val)
693 r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, id, val);
697 case KVM_REG_PPC_DAR:
698 kvmppc_set_dar(vcpu, set_reg_val(id, *val));
700 case KVM_REG_PPC_DSISR:
701 kvmppc_set_dsisr(vcpu, set_reg_val(id, *val));
703 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
704 i = id - KVM_REG_PPC_FPR0;
705 VCPU_FPR(vcpu, i) = set_reg_val(id, *val);
707 case KVM_REG_PPC_FPSCR:
708 vcpu->arch.fp.fpscr = set_reg_val(id, *val);
711 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
712 if (cpu_has_feature(CPU_FTR_VSX)) {
713 i = id - KVM_REG_PPC_VSR0;
714 vcpu->arch.fp.fpr[i][0] = val->vsxval[0];
715 vcpu->arch.fp.fpr[i][1] = val->vsxval[1];
720 #endif /* CONFIG_VSX */
721 #ifdef CONFIG_KVM_XICS
722 case KVM_REG_PPC_ICP_STATE:
723 if (!vcpu->arch.icp && !vcpu->arch.xive_vcpu) {
728 r = kvmppc_xive_set_icp(vcpu, set_reg_val(id, *val));
730 r = kvmppc_xics_set_icp(vcpu, set_reg_val(id, *val));
732 #endif /* CONFIG_KVM_XICS */
733 #ifdef CONFIG_KVM_XIVE
734 case KVM_REG_PPC_VP_STATE:
735 if (!vcpu->arch.xive_vcpu) {
740 r = kvmppc_xive_native_set_vp(vcpu, val);
744 #endif /* CONFIG_KVM_XIVE */
745 case KVM_REG_PPC_FSCR:
746 vcpu->arch.fscr = set_reg_val(id, *val);
748 case KVM_REG_PPC_TAR:
749 vcpu->arch.tar = set_reg_val(id, *val);
751 case KVM_REG_PPC_EBBHR:
752 vcpu->arch.ebbhr = set_reg_val(id, *val);
754 case KVM_REG_PPC_EBBRR:
755 vcpu->arch.ebbrr = set_reg_val(id, *val);
757 case KVM_REG_PPC_BESCR:
758 vcpu->arch.bescr = set_reg_val(id, *val);
761 vcpu->arch.ic = set_reg_val(id, *val);
772 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
774 vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
777 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
779 vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
782 void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
784 vcpu->kvm->arch.kvm_ops->set_msr(vcpu, msr);
786 EXPORT_SYMBOL_GPL(kvmppc_set_msr);
788 int kvmppc_vcpu_run(struct kvm_vcpu *vcpu)
790 return vcpu->kvm->arch.kvm_ops->vcpu_run(vcpu);
793 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
794 struct kvm_translation *tr)
799 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
800 struct kvm_guest_debug *dbg)
803 vcpu->guest_debug = dbg->control;
808 void kvmppc_decrementer_func(struct kvm_vcpu *vcpu)
810 kvmppc_core_queue_dec(vcpu);
814 int kvmppc_core_vcpu_create(struct kvm_vcpu *vcpu)
816 return vcpu->kvm->arch.kvm_ops->vcpu_create(vcpu);
819 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
821 vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
824 int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
826 return vcpu->kvm->arch.kvm_ops->check_requests(vcpu);
829 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
834 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
836 return kvm->arch.kvm_ops->get_dirty_log(kvm, log);
839 void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
841 kvm->arch.kvm_ops->free_memslot(slot);
844 void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
846 kvm->arch.kvm_ops->flush_memslot(kvm, memslot);
849 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
850 struct kvm_memory_slot *memslot,
851 const struct kvm_userspace_memory_region *mem,
852 enum kvm_mr_change change)
854 return kvm->arch.kvm_ops->prepare_memory_region(kvm, memslot, mem,
858 void kvmppc_core_commit_memory_region(struct kvm *kvm,
859 const struct kvm_userspace_memory_region *mem,
860 const struct kvm_memory_slot *old,
861 const struct kvm_memory_slot *new,
862 enum kvm_mr_change change)
864 kvm->arch.kvm_ops->commit_memory_region(kvm, mem, old, new, change);
867 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
869 return kvm->arch.kvm_ops->unmap_gfn_range(kvm, range);
872 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
874 return kvm->arch.kvm_ops->age_gfn(kvm, range);
877 bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
879 return kvm->arch.kvm_ops->test_age_gfn(kvm, range);
882 bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
884 return kvm->arch.kvm_ops->set_spte_gfn(kvm, range);
887 int kvmppc_core_init_vm(struct kvm *kvm)
891 INIT_LIST_HEAD_RCU(&kvm->arch.spapr_tce_tables);
892 INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
893 mutex_init(&kvm->arch.rtas_token_lock);
896 return kvm->arch.kvm_ops->init_vm(kvm);
899 void kvmppc_core_destroy_vm(struct kvm *kvm)
901 kvm->arch.kvm_ops->destroy_vm(kvm);
904 kvmppc_rtas_tokens_free(kvm);
905 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
908 #ifdef CONFIG_KVM_XICS
910 * Free the XIVE and XICS devices which are not directly freed by the
911 * device 'release' method
913 kfree(kvm->arch.xive_devices.native);
914 kvm->arch.xive_devices.native = NULL;
915 kfree(kvm->arch.xive_devices.xics_on_xive);
916 kvm->arch.xive_devices.xics_on_xive = NULL;
917 kfree(kvm->arch.xics_device);
918 kvm->arch.xics_device = NULL;
919 #endif /* CONFIG_KVM_XICS */
922 int kvmppc_h_logical_ci_load(struct kvm_vcpu *vcpu)
924 unsigned long size = kvmppc_get_gpr(vcpu, 4);
925 unsigned long addr = kvmppc_get_gpr(vcpu, 5);
930 if (!is_power_of_2(size) || (size > sizeof(buf)))
933 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
934 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, size, &buf);
935 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
941 kvmppc_set_gpr(vcpu, 4, *(u8 *)&buf);
945 kvmppc_set_gpr(vcpu, 4, be16_to_cpu(*(__be16 *)&buf));
949 kvmppc_set_gpr(vcpu, 4, be32_to_cpu(*(__be32 *)&buf));
953 kvmppc_set_gpr(vcpu, 4, be64_to_cpu(*(__be64 *)&buf));
962 EXPORT_SYMBOL_GPL(kvmppc_h_logical_ci_load);
964 int kvmppc_h_logical_ci_store(struct kvm_vcpu *vcpu)
966 unsigned long size = kvmppc_get_gpr(vcpu, 4);
967 unsigned long addr = kvmppc_get_gpr(vcpu, 5);
968 unsigned long val = kvmppc_get_gpr(vcpu, 6);
979 *(__be16 *)&buf = cpu_to_be16(val);
983 *(__be32 *)&buf = cpu_to_be32(val);
987 *(__be64 *)&buf = cpu_to_be64(val);
994 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
995 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, size, &buf);
996 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1002 EXPORT_SYMBOL_GPL(kvmppc_h_logical_ci_store);
1004 int kvmppc_core_check_processor_compat(void)
1007 * We always return 0 for book3s. We check
1008 * for compatibility while loading the HV
1014 int kvmppc_book3s_hcall_implemented(struct kvm *kvm, unsigned long hcall)
1016 return kvm->arch.kvm_ops->hcall_implemented(hcall);
1019 #ifdef CONFIG_KVM_XICS
1020 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1024 return kvmppc_xive_set_irq(kvm, irq_source_id, irq, level,
1027 return kvmppc_xics_set_irq(kvm, irq_source_id, irq, level,
1031 int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *irq_entry,
1032 struct kvm *kvm, int irq_source_id,
1033 int level, bool line_status)
1035 return kvm_set_irq(kvm, irq_source_id, irq_entry->gsi,
1036 level, line_status);
1038 static int kvmppc_book3s_set_irq(struct kvm_kernel_irq_routing_entry *e,
1039 struct kvm *kvm, int irq_source_id, int level,
1042 return kvm_set_irq(kvm, irq_source_id, e->gsi, level, line_status);
1045 int kvm_irq_map_gsi(struct kvm *kvm,
1046 struct kvm_kernel_irq_routing_entry *entries, int gsi)
1049 entries->type = KVM_IRQ_ROUTING_IRQCHIP;
1050 entries->set = kvmppc_book3s_set_irq;
1051 entries->irqchip.irqchip = 0;
1052 entries->irqchip.pin = gsi;
1056 int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
1061 #endif /* CONFIG_KVM_XICS */
1063 static int kvmppc_book3s_init(void)
1067 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1070 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
1071 r = kvmppc_book3s_init_pr();
1074 #ifdef CONFIG_KVM_XICS
1075 #ifdef CONFIG_KVM_XIVE
1076 if (xics_on_xive()) {
1077 kvm_register_device_ops(&kvm_xive_ops, KVM_DEV_TYPE_XICS);
1078 if (kvmppc_xive_native_supported())
1079 kvm_register_device_ops(&kvm_xive_native_ops,
1083 kvm_register_device_ops(&kvm_xics_ops, KVM_DEV_TYPE_XICS);
1088 static void kvmppc_book3s_exit(void)
1090 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
1091 kvmppc_book3s_exit_pr();
1096 module_init(kvmppc_book3s_init);
1097 module_exit(kvmppc_book3s_exit);
1099 /* On 32bit this is our one and only kernel module */
1100 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
1101 MODULE_ALIAS_MISCDEV(KVM_MINOR);
1102 MODULE_ALIAS("devname:kvm");