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.
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.
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.
15 * Copyright IBM Corp. 2007
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/kvm_host.h>
24 #include <linux/vmalloc.h>
25 #include <linux/hrtimer.h>
26 #include <linux/sched/signal.h>
28 #include <linux/slab.h>
29 #include <linux/file.h>
30 #include <linux/module.h>
31 #include <linux/irqbypass.h>
32 #include <linux/kvm_irqfd.h>
33 #include <asm/cputable.h>
34 #include <linux/uaccess.h>
35 #include <asm/kvm_ppc.h>
36 #include <asm/cputhreads.h>
37 #include <asm/irqflags.h>
38 #include <asm/iommu.h>
39 #include <asm/switch_to.h>
41 #ifdef CONFIG_PPC_PSERIES
42 #include <asm/hvcall.h>
43 #include <asm/plpar_wrappers.h>
48 #include "../mm/mmu_decl.h"
50 #define CREATE_TRACE_POINTS
53 struct kvmppc_ops
*kvmppc_hv_ops
;
54 EXPORT_SYMBOL_GPL(kvmppc_hv_ops
);
55 struct kvmppc_ops
*kvmppc_pr_ops
;
56 EXPORT_SYMBOL_GPL(kvmppc_pr_ops
);
59 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*v
)
61 return !!(v
->arch
.pending_exceptions
) || kvm_request_pending(v
);
64 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu
*vcpu
)
69 int kvm_arch_vcpu_should_kick(struct kvm_vcpu
*vcpu
)
75 * Common checks before entering the guest world. Call with interrupts
80 * == 1 if we're ready to go into guest state
81 * <= 0 if we need to go back to the host with return value
83 int kvmppc_prepare_to_enter(struct kvm_vcpu
*vcpu
)
87 WARN_ON(irqs_disabled());
98 if (signal_pending(current
)) {
99 kvmppc_account_exit(vcpu
, SIGNAL_EXITS
);
100 vcpu
->run
->exit_reason
= KVM_EXIT_INTR
;
105 vcpu
->mode
= IN_GUEST_MODE
;
108 * Reading vcpu->requests must happen after setting vcpu->mode,
109 * so we don't miss a request because the requester sees
110 * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
111 * before next entering the guest (and thus doesn't IPI).
112 * This also orders the write to mode from any reads
113 * to the page tables done while the VCPU is running.
114 * Please see the comment in kvm_flush_remote_tlbs.
118 if (kvm_request_pending(vcpu
)) {
119 /* Make sure we process requests preemptable */
121 trace_kvm_check_requests(vcpu
);
122 r
= kvmppc_core_check_requests(vcpu
);
129 if (kvmppc_core_prepare_to_enter(vcpu
)) {
130 /* interrupts got enabled in between, so we
131 are back at square 1 */
135 guest_enter_irqoff();
143 EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter
);
145 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
146 static void kvmppc_swab_shared(struct kvm_vcpu
*vcpu
)
148 struct kvm_vcpu_arch_shared
*shared
= vcpu
->arch
.shared
;
151 shared
->sprg0
= swab64(shared
->sprg0
);
152 shared
->sprg1
= swab64(shared
->sprg1
);
153 shared
->sprg2
= swab64(shared
->sprg2
);
154 shared
->sprg3
= swab64(shared
->sprg3
);
155 shared
->srr0
= swab64(shared
->srr0
);
156 shared
->srr1
= swab64(shared
->srr1
);
157 shared
->dar
= swab64(shared
->dar
);
158 shared
->msr
= swab64(shared
->msr
);
159 shared
->dsisr
= swab32(shared
->dsisr
);
160 shared
->int_pending
= swab32(shared
->int_pending
);
161 for (i
= 0; i
< ARRAY_SIZE(shared
->sr
); i
++)
162 shared
->sr
[i
] = swab32(shared
->sr
[i
]);
166 int kvmppc_kvm_pv(struct kvm_vcpu
*vcpu
)
168 int nr
= kvmppc_get_gpr(vcpu
, 11);
170 unsigned long __maybe_unused param1
= kvmppc_get_gpr(vcpu
, 3);
171 unsigned long __maybe_unused param2
= kvmppc_get_gpr(vcpu
, 4);
172 unsigned long __maybe_unused param3
= kvmppc_get_gpr(vcpu
, 5);
173 unsigned long __maybe_unused param4
= kvmppc_get_gpr(vcpu
, 6);
174 unsigned long r2
= 0;
176 if (!(kvmppc_get_msr(vcpu
) & MSR_SF
)) {
178 param1
&= 0xffffffff;
179 param2
&= 0xffffffff;
180 param3
&= 0xffffffff;
181 param4
&= 0xffffffff;
185 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE
):
187 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
188 /* Book3S can be little endian, find it out here */
189 int shared_big_endian
= true;
190 if (vcpu
->arch
.intr_msr
& MSR_LE
)
191 shared_big_endian
= false;
192 if (shared_big_endian
!= vcpu
->arch
.shared_big_endian
)
193 kvmppc_swab_shared(vcpu
);
194 vcpu
->arch
.shared_big_endian
= shared_big_endian
;
197 if (!(param2
& MAGIC_PAGE_FLAG_NOT_MAPPED_NX
)) {
199 * Older versions of the Linux magic page code had
200 * a bug where they would map their trampoline code
201 * NX. If that's the case, remove !PR NX capability.
203 vcpu
->arch
.disable_kernel_nx
= true;
204 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
207 vcpu
->arch
.magic_page_pa
= param1
& ~0xfffULL
;
208 vcpu
->arch
.magic_page_ea
= param2
& ~0xfffULL
;
210 #ifdef CONFIG_PPC_64K_PAGES
212 * Make sure our 4k magic page is in the same window of a 64k
213 * page within the guest and within the host's page.
215 if ((vcpu
->arch
.magic_page_pa
& 0xf000) !=
216 ((ulong
)vcpu
->arch
.shared
& 0xf000)) {
217 void *old_shared
= vcpu
->arch
.shared
;
218 ulong shared
= (ulong
)vcpu
->arch
.shared
;
222 shared
|= vcpu
->arch
.magic_page_pa
& 0xf000;
223 new_shared
= (void*)shared
;
224 memcpy(new_shared
, old_shared
, 0x1000);
225 vcpu
->arch
.shared
= new_shared
;
229 r2
= KVM_MAGIC_FEAT_SR
| KVM_MAGIC_FEAT_MAS0_TO_SPRG7
;
234 case KVM_HCALL_TOKEN(KVM_HC_FEATURES
):
236 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
237 r2
|= (1 << KVM_FEATURE_MAGIC_PAGE
);
240 /* Second return value is in r4 */
242 case EV_HCALL_TOKEN(EV_IDLE
):
244 kvm_vcpu_block(vcpu
);
245 kvm_clear_request(KVM_REQ_UNHALT
, vcpu
);
248 r
= EV_UNIMPLEMENTED
;
252 kvmppc_set_gpr(vcpu
, 4, r2
);
256 EXPORT_SYMBOL_GPL(kvmppc_kvm_pv
);
258 int kvmppc_sanity_check(struct kvm_vcpu
*vcpu
)
262 /* We have to know what CPU to virtualize */
266 /* PAPR only works with book3s_64 */
267 if ((vcpu
->arch
.cpu_type
!= KVM_CPU_3S_64
) && vcpu
->arch
.papr_enabled
)
270 /* HV KVM can only do PAPR mode for now */
271 if (!vcpu
->arch
.papr_enabled
&& is_kvmppc_hv_enabled(vcpu
->kvm
))
274 #ifdef CONFIG_KVM_BOOKE_HV
275 if (!cpu_has_feature(CPU_FTR_EMB_HV
))
283 return r
? 0 : -EINVAL
;
285 EXPORT_SYMBOL_GPL(kvmppc_sanity_check
);
287 int kvmppc_emulate_mmio(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
289 enum emulation_result er
;
292 er
= kvmppc_emulate_loadstore(vcpu
);
295 /* Future optimization: only reload non-volatiles if they were
296 * actually modified. */
302 case EMULATE_DO_MMIO
:
303 run
->exit_reason
= KVM_EXIT_MMIO
;
304 /* We must reload nonvolatiles because "update" load/store
305 * instructions modify register state. */
306 /* Future optimization: only reload non-volatiles if they were
307 * actually modified. */
314 kvmppc_get_last_inst(vcpu
, INST_GENERIC
, &last_inst
);
315 /* XXX Deliver Program interrupt to guest. */
316 pr_emerg("%s: emulation failed (%08x)\n", __func__
, last_inst
);
327 EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio
);
329 int kvmppc_st(struct kvm_vcpu
*vcpu
, ulong
*eaddr
, int size
, void *ptr
,
332 ulong mp_pa
= vcpu
->arch
.magic_page_pa
& KVM_PAM
& PAGE_MASK
;
333 struct kvmppc_pte pte
;
338 r
= kvmppc_xlate(vcpu
, *eaddr
, data
? XLATE_DATA
: XLATE_INST
,
348 /* Magic page override */
349 if (kvmppc_supports_magic_page(vcpu
) && mp_pa
&&
350 ((pte
.raddr
& KVM_PAM
& PAGE_MASK
) == mp_pa
) &&
351 !(kvmppc_get_msr(vcpu
) & MSR_PR
)) {
352 void *magic
= vcpu
->arch
.shared
;
353 magic
+= pte
.eaddr
& 0xfff;
354 memcpy(magic
, ptr
, size
);
358 if (kvm_write_guest(vcpu
->kvm
, pte
.raddr
, ptr
, size
))
359 return EMULATE_DO_MMIO
;
363 EXPORT_SYMBOL_GPL(kvmppc_st
);
365 int kvmppc_ld(struct kvm_vcpu
*vcpu
, ulong
*eaddr
, int size
, void *ptr
,
368 ulong mp_pa
= vcpu
->arch
.magic_page_pa
& KVM_PAM
& PAGE_MASK
;
369 struct kvmppc_pte pte
;
374 rc
= kvmppc_xlate(vcpu
, *eaddr
, data
? XLATE_DATA
: XLATE_INST
,
384 if (!data
&& !pte
.may_execute
)
387 /* Magic page override */
388 if (kvmppc_supports_magic_page(vcpu
) && mp_pa
&&
389 ((pte
.raddr
& KVM_PAM
& PAGE_MASK
) == mp_pa
) &&
390 !(kvmppc_get_msr(vcpu
) & MSR_PR
)) {
391 void *magic
= vcpu
->arch
.shared
;
392 magic
+= pte
.eaddr
& 0xfff;
393 memcpy(ptr
, magic
, size
);
397 if (kvm_read_guest(vcpu
->kvm
, pte
.raddr
, ptr
, size
))
398 return EMULATE_DO_MMIO
;
402 EXPORT_SYMBOL_GPL(kvmppc_ld
);
404 int kvm_arch_hardware_enable(void)
409 int kvm_arch_hardware_setup(void)
414 void kvm_arch_check_processor_compat(void *rtn
)
416 *(int *)rtn
= kvmppc_core_check_processor_compat();
419 int kvm_arch_init_vm(struct kvm
*kvm
, unsigned long type
)
421 struct kvmppc_ops
*kvm_ops
= NULL
;
423 * if we have both HV and PR enabled, default is HV
427 kvm_ops
= kvmppc_hv_ops
;
429 kvm_ops
= kvmppc_pr_ops
;
432 } else if (type
== KVM_VM_PPC_HV
) {
435 kvm_ops
= kvmppc_hv_ops
;
436 } else if (type
== KVM_VM_PPC_PR
) {
439 kvm_ops
= kvmppc_pr_ops
;
443 if (kvm_ops
->owner
&& !try_module_get(kvm_ops
->owner
))
446 kvm
->arch
.kvm_ops
= kvm_ops
;
447 return kvmppc_core_init_vm(kvm
);
452 bool kvm_arch_has_vcpu_debugfs(void)
457 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu
*vcpu
)
462 void kvm_arch_destroy_vm(struct kvm
*kvm
)
465 struct kvm_vcpu
*vcpu
;
467 #ifdef CONFIG_KVM_XICS
469 * We call kick_all_cpus_sync() to ensure that all
470 * CPUs have executed any pending IPIs before we
471 * continue and free VCPUs structures below.
473 if (is_kvmppc_hv_enabled(kvm
))
474 kick_all_cpus_sync();
477 kvm_for_each_vcpu(i
, vcpu
, kvm
)
478 kvm_arch_vcpu_free(vcpu
);
480 mutex_lock(&kvm
->lock
);
481 for (i
= 0; i
< atomic_read(&kvm
->online_vcpus
); i
++)
482 kvm
->vcpus
[i
] = NULL
;
484 atomic_set(&kvm
->online_vcpus
, 0);
486 kvmppc_core_destroy_vm(kvm
);
488 mutex_unlock(&kvm
->lock
);
490 /* drop the module reference */
491 module_put(kvm
->arch
.kvm_ops
->owner
);
494 int kvm_vm_ioctl_check_extension(struct kvm
*kvm
, long ext
)
497 /* Assume we're using HV mode when the HV module is loaded */
498 int hv_enabled
= kvmppc_hv_ops
? 1 : 0;
502 * Hooray - we know which VM type we're running on. Depend on
503 * that rather than the guess above.
505 hv_enabled
= is_kvmppc_hv_enabled(kvm
);
510 case KVM_CAP_PPC_BOOKE_SREGS
:
511 case KVM_CAP_PPC_BOOKE_WATCHDOG
:
512 case KVM_CAP_PPC_EPR
:
514 case KVM_CAP_PPC_SEGSTATE
:
515 case KVM_CAP_PPC_HIOR
:
516 case KVM_CAP_PPC_PAPR
:
518 case KVM_CAP_PPC_UNSET_IRQ
:
519 case KVM_CAP_PPC_IRQ_LEVEL
:
520 case KVM_CAP_ENABLE_CAP
:
521 case KVM_CAP_ENABLE_CAP_VM
:
522 case KVM_CAP_ONE_REG
:
523 case KVM_CAP_IOEVENTFD
:
524 case KVM_CAP_DEVICE_CTRL
:
525 case KVM_CAP_IMMEDIATE_EXIT
:
528 case KVM_CAP_PPC_PAIRED_SINGLES
:
529 case KVM_CAP_PPC_OSI
:
530 case KVM_CAP_PPC_GET_PVINFO
:
531 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
534 /* We support this only for PR */
537 #ifdef CONFIG_KVM_MPIC
538 case KVM_CAP_IRQ_MPIC
:
543 #ifdef CONFIG_PPC_BOOK3S_64
544 case KVM_CAP_SPAPR_TCE
:
545 case KVM_CAP_SPAPR_TCE_64
:
548 case KVM_CAP_SPAPR_TCE_VFIO
:
549 r
= !!cpu_has_feature(CPU_FTR_HVMODE
);
551 case KVM_CAP_PPC_RTAS
:
552 case KVM_CAP_PPC_FIXUP_HCALL
:
553 case KVM_CAP_PPC_ENABLE_HCALL
:
554 #ifdef CONFIG_KVM_XICS
555 case KVM_CAP_IRQ_XICS
:
557 case KVM_CAP_PPC_GET_CPU_CHAR
:
561 case KVM_CAP_PPC_ALLOC_HTAB
:
564 #endif /* CONFIG_PPC_BOOK3S_64 */
565 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
566 case KVM_CAP_PPC_SMT
:
569 if (kvm
->arch
.emul_smt_mode
> 1)
570 r
= kvm
->arch
.emul_smt_mode
;
572 r
= kvm
->arch
.smt_mode
;
573 } else if (hv_enabled
) {
574 if (cpu_has_feature(CPU_FTR_ARCH_300
))
577 r
= threads_per_subcore
;
580 case KVM_CAP_PPC_SMT_POSSIBLE
:
583 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
584 r
= ((threads_per_subcore
<< 1) - 1);
586 /* P9 can emulate dbells, so allow any mode */
590 case KVM_CAP_PPC_RMA
:
593 case KVM_CAP_PPC_HWRNG
:
594 r
= kvmppc_hwrng_present();
596 case KVM_CAP_PPC_MMU_RADIX
:
597 r
= !!(hv_enabled
&& radix_enabled());
599 case KVM_CAP_PPC_MMU_HASH_V3
:
600 r
= !!(hv_enabled
&& cpu_has_feature(CPU_FTR_ARCH_300
) &&
601 cpu_has_feature(CPU_FTR_HVMODE
));
603 case KVM_CAP_PPC_NESTED_HV
:
604 r
= !!(hv_enabled
&& kvmppc_hv_ops
->enable_nested
&&
605 !kvmppc_hv_ops
->enable_nested(NULL
));
608 case KVM_CAP_SYNC_MMU
:
609 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
611 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
617 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
618 case KVM_CAP_PPC_HTAB_FD
:
622 case KVM_CAP_NR_VCPUS
:
624 * Recommending a number of CPUs is somewhat arbitrary; we
625 * return the number of present CPUs for -HV (since a host
626 * will have secondary threads "offline"), and for other KVM
627 * implementations just count online CPUs.
630 r
= num_present_cpus();
632 r
= num_online_cpus();
634 case KVM_CAP_NR_MEMSLOTS
:
635 r
= KVM_USER_MEM_SLOTS
;
637 case KVM_CAP_MAX_VCPUS
:
640 #ifdef CONFIG_PPC_BOOK3S_64
641 case KVM_CAP_PPC_GET_SMMU_INFO
:
644 case KVM_CAP_SPAPR_MULTITCE
:
647 case KVM_CAP_SPAPR_RESIZE_HPT
:
651 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
652 case KVM_CAP_PPC_FWNMI
:
656 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
657 case KVM_CAP_PPC_HTM
:
658 r
= !!(cur_cpu_spec
->cpu_user_features2
& PPC_FEATURE2_HTM
) ||
659 (hv_enabled
&& cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST
));
670 long kvm_arch_dev_ioctl(struct file
*filp
,
671 unsigned int ioctl
, unsigned long arg
)
676 void kvm_arch_free_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*free
,
677 struct kvm_memory_slot
*dont
)
679 kvmppc_core_free_memslot(kvm
, free
, dont
);
682 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
683 unsigned long npages
)
685 return kvmppc_core_create_memslot(kvm
, slot
, npages
);
688 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
689 struct kvm_memory_slot
*memslot
,
690 const struct kvm_userspace_memory_region
*mem
,
691 enum kvm_mr_change change
)
693 return kvmppc_core_prepare_memory_region(kvm
, memslot
, mem
);
696 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
697 const struct kvm_userspace_memory_region
*mem
,
698 const struct kvm_memory_slot
*old
,
699 const struct kvm_memory_slot
*new,
700 enum kvm_mr_change change
)
702 kvmppc_core_commit_memory_region(kvm
, mem
, old
, new);
705 void kvm_arch_flush_shadow_memslot(struct kvm
*kvm
,
706 struct kvm_memory_slot
*slot
)
708 kvmppc_core_flush_memslot(kvm
, slot
);
711 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
, unsigned int id
)
713 struct kvm_vcpu
*vcpu
;
714 vcpu
= kvmppc_core_vcpu_create(kvm
, id
);
716 vcpu
->arch
.wqp
= &vcpu
->wq
;
717 kvmppc_create_vcpu_debugfs(vcpu
, id
);
722 void kvm_arch_vcpu_postcreate(struct kvm_vcpu
*vcpu
)
726 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
728 /* Make sure we're not using the vcpu anymore */
729 hrtimer_cancel(&vcpu
->arch
.dec_timer
);
731 kvmppc_remove_vcpu_debugfs(vcpu
);
733 switch (vcpu
->arch
.irq_type
) {
734 case KVMPPC_IRQ_MPIC
:
735 kvmppc_mpic_disconnect_vcpu(vcpu
->arch
.mpic
, vcpu
);
737 case KVMPPC_IRQ_XICS
:
739 kvmppc_xive_cleanup_vcpu(vcpu
);
741 kvmppc_xics_free_icp(vcpu
);
745 kvmppc_core_vcpu_free(vcpu
);
748 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
750 kvm_arch_vcpu_free(vcpu
);
753 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
755 return kvmppc_core_pending_dec(vcpu
);
758 static enum hrtimer_restart
kvmppc_decrementer_wakeup(struct hrtimer
*timer
)
760 struct kvm_vcpu
*vcpu
;
762 vcpu
= container_of(timer
, struct kvm_vcpu
, arch
.dec_timer
);
763 kvmppc_decrementer_func(vcpu
);
765 return HRTIMER_NORESTART
;
768 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
772 hrtimer_init(&vcpu
->arch
.dec_timer
, CLOCK_REALTIME
, HRTIMER_MODE_ABS
);
773 vcpu
->arch
.dec_timer
.function
= kvmppc_decrementer_wakeup
;
774 vcpu
->arch
.dec_expires
= get_tb();
776 #ifdef CONFIG_KVM_EXIT_TIMING
777 mutex_init(&vcpu
->arch
.exit_timing_lock
);
779 ret
= kvmppc_subarch_vcpu_init(vcpu
);
783 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
785 kvmppc_mmu_destroy(vcpu
);
786 kvmppc_subarch_vcpu_uninit(vcpu
);
789 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
793 * vrsave (formerly usprg0) isn't used by Linux, but may
794 * be used by the guest.
796 * On non-booke this is associated with Altivec and
797 * is handled by code in book3s.c.
799 mtspr(SPRN_VRSAVE
, vcpu
->arch
.vrsave
);
801 kvmppc_core_vcpu_load(vcpu
, cpu
);
804 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
806 kvmppc_core_vcpu_put(vcpu
);
808 vcpu
->arch
.vrsave
= mfspr(SPRN_VRSAVE
);
813 * irq_bypass_add_producer and irq_bypass_del_producer are only
814 * useful if the architecture supports PCI passthrough.
815 * irq_bypass_stop and irq_bypass_start are not needed and so
816 * kvm_ops are not defined for them.
818 bool kvm_arch_has_irq_bypass(void)
820 return ((kvmppc_hv_ops
&& kvmppc_hv_ops
->irq_bypass_add_producer
) ||
821 (kvmppc_pr_ops
&& kvmppc_pr_ops
->irq_bypass_add_producer
));
824 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer
*cons
,
825 struct irq_bypass_producer
*prod
)
827 struct kvm_kernel_irqfd
*irqfd
=
828 container_of(cons
, struct kvm_kernel_irqfd
, consumer
);
829 struct kvm
*kvm
= irqfd
->kvm
;
831 if (kvm
->arch
.kvm_ops
->irq_bypass_add_producer
)
832 return kvm
->arch
.kvm_ops
->irq_bypass_add_producer(cons
, prod
);
837 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer
*cons
,
838 struct irq_bypass_producer
*prod
)
840 struct kvm_kernel_irqfd
*irqfd
=
841 container_of(cons
, struct kvm_kernel_irqfd
, consumer
);
842 struct kvm
*kvm
= irqfd
->kvm
;
844 if (kvm
->arch
.kvm_ops
->irq_bypass_del_producer
)
845 kvm
->arch
.kvm_ops
->irq_bypass_del_producer(cons
, prod
);
849 static inline int kvmppc_get_vsr_dword_offset(int index
)
853 if ((index
!= 0) && (index
!= 1))
865 static inline int kvmppc_get_vsr_word_offset(int index
)
869 if ((index
> 3) || (index
< 0))
880 static inline void kvmppc_set_vsr_dword(struct kvm_vcpu
*vcpu
,
883 union kvmppc_one_reg val
;
884 int offset
= kvmppc_get_vsr_dword_offset(vcpu
->arch
.mmio_vsx_offset
);
885 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
891 val
.vval
= VCPU_VSX_VR(vcpu
, index
- 32);
892 val
.vsxval
[offset
] = gpr
;
893 VCPU_VSX_VR(vcpu
, index
- 32) = val
.vval
;
895 VCPU_VSX_FPR(vcpu
, index
, offset
) = gpr
;
899 static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu
*vcpu
,
902 union kvmppc_one_reg val
;
903 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
906 val
.vval
= VCPU_VSX_VR(vcpu
, index
- 32);
909 VCPU_VSX_VR(vcpu
, index
- 32) = val
.vval
;
911 VCPU_VSX_FPR(vcpu
, index
, 0) = gpr
;
912 VCPU_VSX_FPR(vcpu
, index
, 1) = gpr
;
916 static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu
*vcpu
,
919 union kvmppc_one_reg val
;
920 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
923 val
.vsx32val
[0] = gpr
;
924 val
.vsx32val
[1] = gpr
;
925 val
.vsx32val
[2] = gpr
;
926 val
.vsx32val
[3] = gpr
;
927 VCPU_VSX_VR(vcpu
, index
- 32) = val
.vval
;
929 val
.vsx32val
[0] = gpr
;
930 val
.vsx32val
[1] = gpr
;
931 VCPU_VSX_FPR(vcpu
, index
, 0) = val
.vsxval
[0];
932 VCPU_VSX_FPR(vcpu
, index
, 1) = val
.vsxval
[0];
936 static inline void kvmppc_set_vsr_word(struct kvm_vcpu
*vcpu
,
939 union kvmppc_one_reg val
;
940 int offset
= kvmppc_get_vsr_word_offset(vcpu
->arch
.mmio_vsx_offset
);
941 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
942 int dword_offset
, word_offset
;
948 val
.vval
= VCPU_VSX_VR(vcpu
, index
- 32);
949 val
.vsx32val
[offset
] = gpr32
;
950 VCPU_VSX_VR(vcpu
, index
- 32) = val
.vval
;
952 dword_offset
= offset
/ 2;
953 word_offset
= offset
% 2;
954 val
.vsxval
[0] = VCPU_VSX_FPR(vcpu
, index
, dword_offset
);
955 val
.vsx32val
[word_offset
] = gpr32
;
956 VCPU_VSX_FPR(vcpu
, index
, dword_offset
) = val
.vsxval
[0];
959 #endif /* CONFIG_VSX */
961 #ifdef CONFIG_ALTIVEC
962 static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu
*vcpu
,
963 int index
, int element_size
)
966 int elts
= sizeof(vector128
)/element_size
;
968 if ((index
< 0) || (index
>= elts
))
971 if (kvmppc_need_byteswap(vcpu
))
972 offset
= elts
- index
- 1;
979 static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu
*vcpu
,
982 return kvmppc_get_vmx_offset_generic(vcpu
, index
, 8);
985 static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu
*vcpu
,
988 return kvmppc_get_vmx_offset_generic(vcpu
, index
, 4);
991 static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu
*vcpu
,
994 return kvmppc_get_vmx_offset_generic(vcpu
, index
, 2);
997 static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu
*vcpu
,
1000 return kvmppc_get_vmx_offset_generic(vcpu
, index
, 1);
1004 static inline void kvmppc_set_vmx_dword(struct kvm_vcpu
*vcpu
,
1007 union kvmppc_one_reg val
;
1008 int offset
= kvmppc_get_vmx_dword_offset(vcpu
,
1009 vcpu
->arch
.mmio_vmx_offset
);
1010 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
1015 val
.vval
= VCPU_VSX_VR(vcpu
, index
);
1016 val
.vsxval
[offset
] = gpr
;
1017 VCPU_VSX_VR(vcpu
, index
) = val
.vval
;
1020 static inline void kvmppc_set_vmx_word(struct kvm_vcpu
*vcpu
,
1023 union kvmppc_one_reg val
;
1024 int offset
= kvmppc_get_vmx_word_offset(vcpu
,
1025 vcpu
->arch
.mmio_vmx_offset
);
1026 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
1031 val
.vval
= VCPU_VSX_VR(vcpu
, index
);
1032 val
.vsx32val
[offset
] = gpr32
;
1033 VCPU_VSX_VR(vcpu
, index
) = val
.vval
;
1036 static inline void kvmppc_set_vmx_hword(struct kvm_vcpu
*vcpu
,
1039 union kvmppc_one_reg val
;
1040 int offset
= kvmppc_get_vmx_hword_offset(vcpu
,
1041 vcpu
->arch
.mmio_vmx_offset
);
1042 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
1047 val
.vval
= VCPU_VSX_VR(vcpu
, index
);
1048 val
.vsx16val
[offset
] = gpr16
;
1049 VCPU_VSX_VR(vcpu
, index
) = val
.vval
;
1052 static inline void kvmppc_set_vmx_byte(struct kvm_vcpu
*vcpu
,
1055 union kvmppc_one_reg val
;
1056 int offset
= kvmppc_get_vmx_byte_offset(vcpu
,
1057 vcpu
->arch
.mmio_vmx_offset
);
1058 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
1063 val
.vval
= VCPU_VSX_VR(vcpu
, index
);
1064 val
.vsx8val
[offset
] = gpr8
;
1065 VCPU_VSX_VR(vcpu
, index
) = val
.vval
;
1067 #endif /* CONFIG_ALTIVEC */
1069 #ifdef CONFIG_PPC_FPU
1070 static inline u64
sp_to_dp(u32 fprs
)
1076 asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd
) : "m" (fprs
)
1082 static inline u32
dp_to_sp(u64 fprd
)
1088 asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs
) : "m" (fprd
)
1095 #define sp_to_dp(x) (x)
1096 #define dp_to_sp(x) (x)
1097 #endif /* CONFIG_PPC_FPU */
1099 static void kvmppc_complete_mmio_load(struct kvm_vcpu
*vcpu
,
1100 struct kvm_run
*run
)
1102 u64
uninitialized_var(gpr
);
1104 if (run
->mmio
.len
> sizeof(gpr
)) {
1105 printk(KERN_ERR
"bad MMIO length: %d\n", run
->mmio
.len
);
1109 if (!vcpu
->arch
.mmio_host_swabbed
) {
1110 switch (run
->mmio
.len
) {
1111 case 8: gpr
= *(u64
*)run
->mmio
.data
; break;
1112 case 4: gpr
= *(u32
*)run
->mmio
.data
; break;
1113 case 2: gpr
= *(u16
*)run
->mmio
.data
; break;
1114 case 1: gpr
= *(u8
*)run
->mmio
.data
; break;
1117 switch (run
->mmio
.len
) {
1118 case 8: gpr
= swab64(*(u64
*)run
->mmio
.data
); break;
1119 case 4: gpr
= swab32(*(u32
*)run
->mmio
.data
); break;
1120 case 2: gpr
= swab16(*(u16
*)run
->mmio
.data
); break;
1121 case 1: gpr
= *(u8
*)run
->mmio
.data
; break;
1125 /* conversion between single and double precision */
1126 if ((vcpu
->arch
.mmio_sp64_extend
) && (run
->mmio
.len
== 4))
1127 gpr
= sp_to_dp(gpr
);
1129 if (vcpu
->arch
.mmio_sign_extend
) {
1130 switch (run
->mmio
.len
) {
1133 gpr
= (s64
)(s32
)gpr
;
1137 gpr
= (s64
)(s16
)gpr
;
1145 switch (vcpu
->arch
.io_gpr
& KVM_MMIO_REG_EXT_MASK
) {
1146 case KVM_MMIO_REG_GPR
:
1147 kvmppc_set_gpr(vcpu
, vcpu
->arch
.io_gpr
, gpr
);
1149 case KVM_MMIO_REG_FPR
:
1150 if (vcpu
->kvm
->arch
.kvm_ops
->giveup_ext
)
1151 vcpu
->kvm
->arch
.kvm_ops
->giveup_ext(vcpu
, MSR_FP
);
1153 VCPU_FPR(vcpu
, vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
) = gpr
;
1155 #ifdef CONFIG_PPC_BOOK3S
1156 case KVM_MMIO_REG_QPR
:
1157 vcpu
->arch
.qpr
[vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
] = gpr
;
1159 case KVM_MMIO_REG_FQPR
:
1160 VCPU_FPR(vcpu
, vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
) = gpr
;
1161 vcpu
->arch
.qpr
[vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
] = gpr
;
1165 case KVM_MMIO_REG_VSX
:
1166 if (vcpu
->kvm
->arch
.kvm_ops
->giveup_ext
)
1167 vcpu
->kvm
->arch
.kvm_ops
->giveup_ext(vcpu
, MSR_VSX
);
1169 if (vcpu
->arch
.mmio_copy_type
== KVMPPC_VSX_COPY_DWORD
)
1170 kvmppc_set_vsr_dword(vcpu
, gpr
);
1171 else if (vcpu
->arch
.mmio_copy_type
== KVMPPC_VSX_COPY_WORD
)
1172 kvmppc_set_vsr_word(vcpu
, gpr
);
1173 else if (vcpu
->arch
.mmio_copy_type
==
1174 KVMPPC_VSX_COPY_DWORD_LOAD_DUMP
)
1175 kvmppc_set_vsr_dword_dump(vcpu
, gpr
);
1176 else if (vcpu
->arch
.mmio_copy_type
==
1177 KVMPPC_VSX_COPY_WORD_LOAD_DUMP
)
1178 kvmppc_set_vsr_word_dump(vcpu
, gpr
);
1181 #ifdef CONFIG_ALTIVEC
1182 case KVM_MMIO_REG_VMX
:
1183 if (vcpu
->kvm
->arch
.kvm_ops
->giveup_ext
)
1184 vcpu
->kvm
->arch
.kvm_ops
->giveup_ext(vcpu
, MSR_VEC
);
1186 if (vcpu
->arch
.mmio_copy_type
== KVMPPC_VMX_COPY_DWORD
)
1187 kvmppc_set_vmx_dword(vcpu
, gpr
);
1188 else if (vcpu
->arch
.mmio_copy_type
== KVMPPC_VMX_COPY_WORD
)
1189 kvmppc_set_vmx_word(vcpu
, gpr
);
1190 else if (vcpu
->arch
.mmio_copy_type
==
1191 KVMPPC_VMX_COPY_HWORD
)
1192 kvmppc_set_vmx_hword(vcpu
, gpr
);
1193 else if (vcpu
->arch
.mmio_copy_type
==
1194 KVMPPC_VMX_COPY_BYTE
)
1195 kvmppc_set_vmx_byte(vcpu
, gpr
);
1203 static int __kvmppc_handle_load(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1204 unsigned int rt
, unsigned int bytes
,
1205 int is_default_endian
, int sign_extend
)
1210 /* Pity C doesn't have a logical XOR operator */
1211 if (kvmppc_need_byteswap(vcpu
)) {
1212 host_swabbed
= is_default_endian
;
1214 host_swabbed
= !is_default_endian
;
1217 if (bytes
> sizeof(run
->mmio
.data
)) {
1218 printk(KERN_ERR
"%s: bad MMIO length: %d\n", __func__
,
1222 run
->mmio
.phys_addr
= vcpu
->arch
.paddr_accessed
;
1223 run
->mmio
.len
= bytes
;
1224 run
->mmio
.is_write
= 0;
1226 vcpu
->arch
.io_gpr
= rt
;
1227 vcpu
->arch
.mmio_host_swabbed
= host_swabbed
;
1228 vcpu
->mmio_needed
= 1;
1229 vcpu
->mmio_is_write
= 0;
1230 vcpu
->arch
.mmio_sign_extend
= sign_extend
;
1232 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1234 ret
= kvm_io_bus_read(vcpu
, KVM_MMIO_BUS
, run
->mmio
.phys_addr
,
1235 bytes
, &run
->mmio
.data
);
1237 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
1240 kvmppc_complete_mmio_load(vcpu
, run
);
1241 vcpu
->mmio_needed
= 0;
1242 return EMULATE_DONE
;
1245 return EMULATE_DO_MMIO
;
1248 int kvmppc_handle_load(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1249 unsigned int rt
, unsigned int bytes
,
1250 int is_default_endian
)
1252 return __kvmppc_handle_load(run
, vcpu
, rt
, bytes
, is_default_endian
, 0);
1254 EXPORT_SYMBOL_GPL(kvmppc_handle_load
);
1256 /* Same as above, but sign extends */
1257 int kvmppc_handle_loads(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1258 unsigned int rt
, unsigned int bytes
,
1259 int is_default_endian
)
1261 return __kvmppc_handle_load(run
, vcpu
, rt
, bytes
, is_default_endian
, 1);
1265 int kvmppc_handle_vsx_load(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1266 unsigned int rt
, unsigned int bytes
,
1267 int is_default_endian
, int mmio_sign_extend
)
1269 enum emulation_result emulated
= EMULATE_DONE
;
1271 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1272 if (vcpu
->arch
.mmio_vsx_copy_nums
> 4)
1273 return EMULATE_FAIL
;
1275 while (vcpu
->arch
.mmio_vsx_copy_nums
) {
1276 emulated
= __kvmppc_handle_load(run
, vcpu
, rt
, bytes
,
1277 is_default_endian
, mmio_sign_extend
);
1279 if (emulated
!= EMULATE_DONE
)
1282 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1284 vcpu
->arch
.mmio_vsx_copy_nums
--;
1285 vcpu
->arch
.mmio_vsx_offset
++;
1289 #endif /* CONFIG_VSX */
1291 int kvmppc_handle_store(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1292 u64 val
, unsigned int bytes
, int is_default_endian
)
1294 void *data
= run
->mmio
.data
;
1298 /* Pity C doesn't have a logical XOR operator */
1299 if (kvmppc_need_byteswap(vcpu
)) {
1300 host_swabbed
= is_default_endian
;
1302 host_swabbed
= !is_default_endian
;
1305 if (bytes
> sizeof(run
->mmio
.data
)) {
1306 printk(KERN_ERR
"%s: bad MMIO length: %d\n", __func__
,
1310 run
->mmio
.phys_addr
= vcpu
->arch
.paddr_accessed
;
1311 run
->mmio
.len
= bytes
;
1312 run
->mmio
.is_write
= 1;
1313 vcpu
->mmio_needed
= 1;
1314 vcpu
->mmio_is_write
= 1;
1316 if ((vcpu
->arch
.mmio_sp64_extend
) && (bytes
== 4))
1317 val
= dp_to_sp(val
);
1319 /* Store the value at the lowest bytes in 'data'. */
1320 if (!host_swabbed
) {
1322 case 8: *(u64
*)data
= val
; break;
1323 case 4: *(u32
*)data
= val
; break;
1324 case 2: *(u16
*)data
= val
; break;
1325 case 1: *(u8
*)data
= val
; break;
1329 case 8: *(u64
*)data
= swab64(val
); break;
1330 case 4: *(u32
*)data
= swab32(val
); break;
1331 case 2: *(u16
*)data
= swab16(val
); break;
1332 case 1: *(u8
*)data
= val
; break;
1336 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1338 ret
= kvm_io_bus_write(vcpu
, KVM_MMIO_BUS
, run
->mmio
.phys_addr
,
1339 bytes
, &run
->mmio
.data
);
1341 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
1344 vcpu
->mmio_needed
= 0;
1345 return EMULATE_DONE
;
1348 return EMULATE_DO_MMIO
;
1350 EXPORT_SYMBOL_GPL(kvmppc_handle_store
);
1353 static inline int kvmppc_get_vsr_data(struct kvm_vcpu
*vcpu
, int rs
, u64
*val
)
1355 u32 dword_offset
, word_offset
;
1356 union kvmppc_one_reg reg
;
1358 int copy_type
= vcpu
->arch
.mmio_copy_type
;
1361 switch (copy_type
) {
1362 case KVMPPC_VSX_COPY_DWORD
:
1364 kvmppc_get_vsr_dword_offset(vcpu
->arch
.mmio_vsx_offset
);
1366 if (vsx_offset
== -1) {
1372 *val
= VCPU_VSX_FPR(vcpu
, rs
, vsx_offset
);
1374 reg
.vval
= VCPU_VSX_VR(vcpu
, rs
- 32);
1375 *val
= reg
.vsxval
[vsx_offset
];
1379 case KVMPPC_VSX_COPY_WORD
:
1381 kvmppc_get_vsr_word_offset(vcpu
->arch
.mmio_vsx_offset
);
1383 if (vsx_offset
== -1) {
1389 dword_offset
= vsx_offset
/ 2;
1390 word_offset
= vsx_offset
% 2;
1391 reg
.vsxval
[0] = VCPU_VSX_FPR(vcpu
, rs
, dword_offset
);
1392 *val
= reg
.vsx32val
[word_offset
];
1394 reg
.vval
= VCPU_VSX_VR(vcpu
, rs
- 32);
1395 *val
= reg
.vsx32val
[vsx_offset
];
1407 int kvmppc_handle_vsx_store(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1408 int rs
, unsigned int bytes
, int is_default_endian
)
1411 enum emulation_result emulated
= EMULATE_DONE
;
1413 vcpu
->arch
.io_gpr
= rs
;
1415 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1416 if (vcpu
->arch
.mmio_vsx_copy_nums
> 4)
1417 return EMULATE_FAIL
;
1419 while (vcpu
->arch
.mmio_vsx_copy_nums
) {
1420 if (kvmppc_get_vsr_data(vcpu
, rs
, &val
) == -1)
1421 return EMULATE_FAIL
;
1423 emulated
= kvmppc_handle_store(run
, vcpu
,
1424 val
, bytes
, is_default_endian
);
1426 if (emulated
!= EMULATE_DONE
)
1429 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1431 vcpu
->arch
.mmio_vsx_copy_nums
--;
1432 vcpu
->arch
.mmio_vsx_offset
++;
1438 static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu
*vcpu
,
1439 struct kvm_run
*run
)
1441 enum emulation_result emulated
= EMULATE_FAIL
;
1444 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1446 if (!vcpu
->mmio_is_write
) {
1447 emulated
= kvmppc_handle_vsx_load(run
, vcpu
, vcpu
->arch
.io_gpr
,
1448 run
->mmio
.len
, 1, vcpu
->arch
.mmio_sign_extend
);
1450 emulated
= kvmppc_handle_vsx_store(run
, vcpu
,
1451 vcpu
->arch
.io_gpr
, run
->mmio
.len
, 1);
1455 case EMULATE_DO_MMIO
:
1456 run
->exit_reason
= KVM_EXIT_MMIO
;
1460 pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
1461 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1462 run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
1471 #endif /* CONFIG_VSX */
1473 #ifdef CONFIG_ALTIVEC
1474 int kvmppc_handle_vmx_load(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1475 unsigned int rt
, unsigned int bytes
, int is_default_endian
)
1477 enum emulation_result emulated
= EMULATE_DONE
;
1479 if (vcpu
->arch
.mmio_vsx_copy_nums
> 2)
1480 return EMULATE_FAIL
;
1482 while (vcpu
->arch
.mmio_vmx_copy_nums
) {
1483 emulated
= __kvmppc_handle_load(run
, vcpu
, rt
, bytes
,
1484 is_default_endian
, 0);
1486 if (emulated
!= EMULATE_DONE
)
1489 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1490 vcpu
->arch
.mmio_vmx_copy_nums
--;
1491 vcpu
->arch
.mmio_vmx_offset
++;
1497 int kvmppc_get_vmx_dword(struct kvm_vcpu
*vcpu
, int index
, u64
*val
)
1499 union kvmppc_one_reg reg
;
1504 kvmppc_get_vmx_dword_offset(vcpu
, vcpu
->arch
.mmio_vmx_offset
);
1506 if (vmx_offset
== -1)
1509 reg
.vval
= VCPU_VSX_VR(vcpu
, index
);
1510 *val
= reg
.vsxval
[vmx_offset
];
1515 int kvmppc_get_vmx_word(struct kvm_vcpu
*vcpu
, int index
, u64
*val
)
1517 union kvmppc_one_reg reg
;
1522 kvmppc_get_vmx_word_offset(vcpu
, vcpu
->arch
.mmio_vmx_offset
);
1524 if (vmx_offset
== -1)
1527 reg
.vval
= VCPU_VSX_VR(vcpu
, index
);
1528 *val
= reg
.vsx32val
[vmx_offset
];
1533 int kvmppc_get_vmx_hword(struct kvm_vcpu
*vcpu
, int index
, u64
*val
)
1535 union kvmppc_one_reg reg
;
1540 kvmppc_get_vmx_hword_offset(vcpu
, vcpu
->arch
.mmio_vmx_offset
);
1542 if (vmx_offset
== -1)
1545 reg
.vval
= VCPU_VSX_VR(vcpu
, index
);
1546 *val
= reg
.vsx16val
[vmx_offset
];
1551 int kvmppc_get_vmx_byte(struct kvm_vcpu
*vcpu
, int index
, u64
*val
)
1553 union kvmppc_one_reg reg
;
1558 kvmppc_get_vmx_byte_offset(vcpu
, vcpu
->arch
.mmio_vmx_offset
);
1560 if (vmx_offset
== -1)
1563 reg
.vval
= VCPU_VSX_VR(vcpu
, index
);
1564 *val
= reg
.vsx8val
[vmx_offset
];
1569 int kvmppc_handle_vmx_store(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1570 unsigned int rs
, unsigned int bytes
, int is_default_endian
)
1573 unsigned int index
= rs
& KVM_MMIO_REG_MASK
;
1574 enum emulation_result emulated
= EMULATE_DONE
;
1576 if (vcpu
->arch
.mmio_vsx_copy_nums
> 2)
1577 return EMULATE_FAIL
;
1579 vcpu
->arch
.io_gpr
= rs
;
1581 while (vcpu
->arch
.mmio_vmx_copy_nums
) {
1582 switch (vcpu
->arch
.mmio_copy_type
) {
1583 case KVMPPC_VMX_COPY_DWORD
:
1584 if (kvmppc_get_vmx_dword(vcpu
, index
, &val
) == -1)
1585 return EMULATE_FAIL
;
1588 case KVMPPC_VMX_COPY_WORD
:
1589 if (kvmppc_get_vmx_word(vcpu
, index
, &val
) == -1)
1590 return EMULATE_FAIL
;
1592 case KVMPPC_VMX_COPY_HWORD
:
1593 if (kvmppc_get_vmx_hword(vcpu
, index
, &val
) == -1)
1594 return EMULATE_FAIL
;
1596 case KVMPPC_VMX_COPY_BYTE
:
1597 if (kvmppc_get_vmx_byte(vcpu
, index
, &val
) == -1)
1598 return EMULATE_FAIL
;
1601 return EMULATE_FAIL
;
1604 emulated
= kvmppc_handle_store(run
, vcpu
, val
, bytes
,
1606 if (emulated
!= EMULATE_DONE
)
1609 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1610 vcpu
->arch
.mmio_vmx_copy_nums
--;
1611 vcpu
->arch
.mmio_vmx_offset
++;
1617 static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu
*vcpu
,
1618 struct kvm_run
*run
)
1620 enum emulation_result emulated
= EMULATE_FAIL
;
1623 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1625 if (!vcpu
->mmio_is_write
) {
1626 emulated
= kvmppc_handle_vmx_load(run
, vcpu
,
1627 vcpu
->arch
.io_gpr
, run
->mmio
.len
, 1);
1629 emulated
= kvmppc_handle_vmx_store(run
, vcpu
,
1630 vcpu
->arch
.io_gpr
, run
->mmio
.len
, 1);
1634 case EMULATE_DO_MMIO
:
1635 run
->exit_reason
= KVM_EXIT_MMIO
;
1639 pr_info("KVM: MMIO emulation failed (VMX repeat)\n");
1640 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1641 run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
1650 #endif /* CONFIG_ALTIVEC */
1652 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu
*vcpu
, struct kvm_one_reg
*reg
)
1655 union kvmppc_one_reg val
;
1658 size
= one_reg_size(reg
->id
);
1659 if (size
> sizeof(val
))
1662 r
= kvmppc_get_one_reg(vcpu
, reg
->id
, &val
);
1666 #ifdef CONFIG_ALTIVEC
1667 case KVM_REG_PPC_VR0
... KVM_REG_PPC_VR31
:
1668 if (!cpu_has_feature(CPU_FTR_ALTIVEC
)) {
1672 val
.vval
= vcpu
->arch
.vr
.vr
[reg
->id
- KVM_REG_PPC_VR0
];
1674 case KVM_REG_PPC_VSCR
:
1675 if (!cpu_has_feature(CPU_FTR_ALTIVEC
)) {
1679 val
= get_reg_val(reg
->id
, vcpu
->arch
.vr
.vscr
.u
[3]);
1681 case KVM_REG_PPC_VRSAVE
:
1682 val
= get_reg_val(reg
->id
, vcpu
->arch
.vrsave
);
1684 #endif /* CONFIG_ALTIVEC */
1694 if (copy_to_user((char __user
*)(unsigned long)reg
->addr
, &val
, size
))
1700 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu
*vcpu
, struct kvm_one_reg
*reg
)
1703 union kvmppc_one_reg val
;
1706 size
= one_reg_size(reg
->id
);
1707 if (size
> sizeof(val
))
1710 if (copy_from_user(&val
, (char __user
*)(unsigned long)reg
->addr
, size
))
1713 r
= kvmppc_set_one_reg(vcpu
, reg
->id
, &val
);
1717 #ifdef CONFIG_ALTIVEC
1718 case KVM_REG_PPC_VR0
... KVM_REG_PPC_VR31
:
1719 if (!cpu_has_feature(CPU_FTR_ALTIVEC
)) {
1723 vcpu
->arch
.vr
.vr
[reg
->id
- KVM_REG_PPC_VR0
] = val
.vval
;
1725 case KVM_REG_PPC_VSCR
:
1726 if (!cpu_has_feature(CPU_FTR_ALTIVEC
)) {
1730 vcpu
->arch
.vr
.vscr
.u
[3] = set_reg_val(reg
->id
, val
);
1732 case KVM_REG_PPC_VRSAVE
:
1733 if (!cpu_has_feature(CPU_FTR_ALTIVEC
)) {
1737 vcpu
->arch
.vrsave
= set_reg_val(reg
->id
, val
);
1739 #endif /* CONFIG_ALTIVEC */
1749 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1755 if (vcpu
->mmio_needed
) {
1756 vcpu
->mmio_needed
= 0;
1757 if (!vcpu
->mmio_is_write
)
1758 kvmppc_complete_mmio_load(vcpu
, run
);
1760 if (vcpu
->arch
.mmio_vsx_copy_nums
> 0) {
1761 vcpu
->arch
.mmio_vsx_copy_nums
--;
1762 vcpu
->arch
.mmio_vsx_offset
++;
1765 if (vcpu
->arch
.mmio_vsx_copy_nums
> 0) {
1766 r
= kvmppc_emulate_mmio_vsx_loadstore(vcpu
, run
);
1767 if (r
== RESUME_HOST
) {
1768 vcpu
->mmio_needed
= 1;
1773 #ifdef CONFIG_ALTIVEC
1774 if (vcpu
->arch
.mmio_vmx_copy_nums
> 0) {
1775 vcpu
->arch
.mmio_vmx_copy_nums
--;
1776 vcpu
->arch
.mmio_vmx_offset
++;
1779 if (vcpu
->arch
.mmio_vmx_copy_nums
> 0) {
1780 r
= kvmppc_emulate_mmio_vmx_loadstore(vcpu
, run
);
1781 if (r
== RESUME_HOST
) {
1782 vcpu
->mmio_needed
= 1;
1787 } else if (vcpu
->arch
.osi_needed
) {
1788 u64
*gprs
= run
->osi
.gprs
;
1791 for (i
= 0; i
< 32; i
++)
1792 kvmppc_set_gpr(vcpu
, i
, gprs
[i
]);
1793 vcpu
->arch
.osi_needed
= 0;
1794 } else if (vcpu
->arch
.hcall_needed
) {
1797 kvmppc_set_gpr(vcpu
, 3, run
->papr_hcall
.ret
);
1798 for (i
= 0; i
< 9; ++i
)
1799 kvmppc_set_gpr(vcpu
, 4 + i
, run
->papr_hcall
.args
[i
]);
1800 vcpu
->arch
.hcall_needed
= 0;
1802 } else if (vcpu
->arch
.epr_needed
) {
1803 kvmppc_set_epr(vcpu
, run
->epr
.epr
);
1804 vcpu
->arch
.epr_needed
= 0;
1808 kvm_sigset_activate(vcpu
);
1810 if (run
->immediate_exit
)
1813 r
= kvmppc_vcpu_run(run
, vcpu
);
1815 kvm_sigset_deactivate(vcpu
);
1817 #ifdef CONFIG_ALTIVEC
1824 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
, struct kvm_interrupt
*irq
)
1826 if (irq
->irq
== KVM_INTERRUPT_UNSET
) {
1827 kvmppc_core_dequeue_external(vcpu
);
1831 kvmppc_core_queue_external(vcpu
, irq
);
1833 kvm_vcpu_kick(vcpu
);
1838 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu
*vcpu
,
1839 struct kvm_enable_cap
*cap
)
1847 case KVM_CAP_PPC_OSI
:
1849 vcpu
->arch
.osi_enabled
= true;
1851 case KVM_CAP_PPC_PAPR
:
1853 vcpu
->arch
.papr_enabled
= true;
1855 case KVM_CAP_PPC_EPR
:
1858 vcpu
->arch
.epr_flags
|= KVMPPC_EPR_USER
;
1860 vcpu
->arch
.epr_flags
&= ~KVMPPC_EPR_USER
;
1863 case KVM_CAP_PPC_BOOKE_WATCHDOG
:
1865 vcpu
->arch
.watchdog_enabled
= true;
1868 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1869 case KVM_CAP_SW_TLB
: {
1870 struct kvm_config_tlb cfg
;
1871 void __user
*user_ptr
= (void __user
*)(uintptr_t)cap
->args
[0];
1874 if (copy_from_user(&cfg
, user_ptr
, sizeof(cfg
)))
1877 r
= kvm_vcpu_ioctl_config_tlb(vcpu
, &cfg
);
1881 #ifdef CONFIG_KVM_MPIC
1882 case KVM_CAP_IRQ_MPIC
: {
1884 struct kvm_device
*dev
;
1887 f
= fdget(cap
->args
[0]);
1892 dev
= kvm_device_from_filp(f
.file
);
1894 r
= kvmppc_mpic_connect_vcpu(dev
, vcpu
, cap
->args
[1]);
1900 #ifdef CONFIG_KVM_XICS
1901 case KVM_CAP_IRQ_XICS
: {
1903 struct kvm_device
*dev
;
1906 f
= fdget(cap
->args
[0]);
1911 dev
= kvm_device_from_filp(f
.file
);
1914 r
= kvmppc_xive_connect_vcpu(dev
, vcpu
, cap
->args
[1]);
1916 r
= kvmppc_xics_connect_vcpu(dev
, vcpu
, cap
->args
[1]);
1922 #endif /* CONFIG_KVM_XICS */
1923 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1924 case KVM_CAP_PPC_FWNMI
:
1926 if (!is_kvmppc_hv_enabled(vcpu
->kvm
))
1929 vcpu
->kvm
->arch
.fwnmi_enabled
= true;
1931 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1938 r
= kvmppc_sanity_check(vcpu
);
1943 bool kvm_arch_intc_initialized(struct kvm
*kvm
)
1945 #ifdef CONFIG_KVM_MPIC
1949 #ifdef CONFIG_KVM_XICS
1950 if (kvm
->arch
.xics
|| kvm
->arch
.xive
)
1956 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
1957 struct kvm_mp_state
*mp_state
)
1962 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
1963 struct kvm_mp_state
*mp_state
)
1968 long kvm_arch_vcpu_async_ioctl(struct file
*filp
,
1969 unsigned int ioctl
, unsigned long arg
)
1971 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1972 void __user
*argp
= (void __user
*)arg
;
1974 if (ioctl
== KVM_INTERRUPT
) {
1975 struct kvm_interrupt irq
;
1976 if (copy_from_user(&irq
, argp
, sizeof(irq
)))
1978 return kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1980 return -ENOIOCTLCMD
;
1983 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1984 unsigned int ioctl
, unsigned long arg
)
1986 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1987 void __user
*argp
= (void __user
*)arg
;
1991 case KVM_ENABLE_CAP
:
1993 struct kvm_enable_cap cap
;
1996 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
1998 r
= kvm_vcpu_ioctl_enable_cap(vcpu
, &cap
);
2003 case KVM_SET_ONE_REG
:
2004 case KVM_GET_ONE_REG
:
2006 struct kvm_one_reg reg
;
2008 if (copy_from_user(®
, argp
, sizeof(reg
)))
2010 if (ioctl
== KVM_SET_ONE_REG
)
2011 r
= kvm_vcpu_ioctl_set_one_reg(vcpu
, ®
);
2013 r
= kvm_vcpu_ioctl_get_one_reg(vcpu
, ®
);
2017 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
2018 case KVM_DIRTY_TLB
: {
2019 struct kvm_dirty_tlb dirty
;
2022 if (copy_from_user(&dirty
, argp
, sizeof(dirty
)))
2024 r
= kvm_vcpu_ioctl_dirty_tlb(vcpu
, &dirty
);
2037 vm_fault_t
kvm_arch_vcpu_fault(struct kvm_vcpu
*vcpu
, struct vm_fault
*vmf
)
2039 return VM_FAULT_SIGBUS
;
2042 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo
*pvinfo
)
2044 u32 inst_nop
= 0x60000000;
2045 #ifdef CONFIG_KVM_BOOKE_HV
2046 u32 inst_sc1
= 0x44000022;
2047 pvinfo
->hcall
[0] = cpu_to_be32(inst_sc1
);
2048 pvinfo
->hcall
[1] = cpu_to_be32(inst_nop
);
2049 pvinfo
->hcall
[2] = cpu_to_be32(inst_nop
);
2050 pvinfo
->hcall
[3] = cpu_to_be32(inst_nop
);
2052 u32 inst_lis
= 0x3c000000;
2053 u32 inst_ori
= 0x60000000;
2054 u32 inst_sc
= 0x44000002;
2055 u32 inst_imm_mask
= 0xffff;
2058 * The hypercall to get into KVM from within guest context is as
2061 * lis r0, r0, KVM_SC_MAGIC_R0@h
2062 * ori r0, KVM_SC_MAGIC_R0@l
2066 pvinfo
->hcall
[0] = cpu_to_be32(inst_lis
| ((KVM_SC_MAGIC_R0
>> 16) & inst_imm_mask
));
2067 pvinfo
->hcall
[1] = cpu_to_be32(inst_ori
| (KVM_SC_MAGIC_R0
& inst_imm_mask
));
2068 pvinfo
->hcall
[2] = cpu_to_be32(inst_sc
);
2069 pvinfo
->hcall
[3] = cpu_to_be32(inst_nop
);
2072 pvinfo
->flags
= KVM_PPC_PVINFO_FLAGS_EV_IDLE
;
2077 int kvm_vm_ioctl_irq_line(struct kvm
*kvm
, struct kvm_irq_level
*irq_event
,
2080 if (!irqchip_in_kernel(kvm
))
2083 irq_event
->status
= kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
,
2084 irq_event
->irq
, irq_event
->level
,
2090 static int kvm_vm_ioctl_enable_cap(struct kvm
*kvm
,
2091 struct kvm_enable_cap
*cap
)
2099 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
2100 case KVM_CAP_PPC_ENABLE_HCALL
: {
2101 unsigned long hcall
= cap
->args
[0];
2104 if (hcall
> MAX_HCALL_OPCODE
|| (hcall
& 3) ||
2107 if (!kvmppc_book3s_hcall_implemented(kvm
, hcall
))
2110 set_bit(hcall
/ 4, kvm
->arch
.enabled_hcalls
);
2112 clear_bit(hcall
/ 4, kvm
->arch
.enabled_hcalls
);
2116 case KVM_CAP_PPC_SMT
: {
2117 unsigned long mode
= cap
->args
[0];
2118 unsigned long flags
= cap
->args
[1];
2121 if (kvm
->arch
.kvm_ops
->set_smt_mode
)
2122 r
= kvm
->arch
.kvm_ops
->set_smt_mode(kvm
, mode
, flags
);
2126 case KVM_CAP_PPC_NESTED_HV
:
2128 if (!is_kvmppc_hv_enabled(kvm
) ||
2129 !kvm
->arch
.kvm_ops
->enable_nested
)
2131 r
= kvm
->arch
.kvm_ops
->enable_nested(kvm
);
2142 #ifdef CONFIG_PPC_BOOK3S_64
2144 * These functions check whether the underlying hardware is safe
2145 * against attacks based on observing the effects of speculatively
2146 * executed instructions, and whether it supplies instructions for
2147 * use in workarounds. The information comes from firmware, either
2148 * via the device tree on powernv platforms or from an hcall on
2149 * pseries platforms.
2151 #ifdef CONFIG_PPC_PSERIES
2152 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char
*cp
)
2154 struct h_cpu_char_result c
;
2157 if (!machine_is(pseries
))
2160 rc
= plpar_get_cpu_characteristics(&c
);
2161 if (rc
== H_SUCCESS
) {
2162 cp
->character
= c
.character
;
2163 cp
->behaviour
= c
.behaviour
;
2164 cp
->character_mask
= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31
|
2165 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED
|
2166 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30
|
2167 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2
|
2168 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV
|
2169 KVM_PPC_CPU_CHAR_BR_HINT_HONOURED
|
2170 KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF
|
2171 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS
;
2172 cp
->behaviour_mask
= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY
|
2173 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR
|
2174 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR
;
2179 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char
*cp
)
2185 static inline bool have_fw_feat(struct device_node
*fw_features
,
2186 const char *state
, const char *name
)
2188 struct device_node
*np
;
2191 np
= of_get_child_by_name(fw_features
, name
);
2193 r
= of_property_read_bool(np
, state
);
2199 static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char
*cp
)
2201 struct device_node
*np
, *fw_features
;
2204 memset(cp
, 0, sizeof(*cp
));
2205 r
= pseries_get_cpu_char(cp
);
2209 np
= of_find_node_by_name(NULL
, "ibm,opal");
2211 fw_features
= of_get_child_by_name(np
, "fw-features");
2215 if (have_fw_feat(fw_features
, "enabled",
2216 "inst-spec-barrier-ori31,31,0"))
2217 cp
->character
|= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31
;
2218 if (have_fw_feat(fw_features
, "enabled",
2219 "fw-bcctrl-serialized"))
2220 cp
->character
|= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED
;
2221 if (have_fw_feat(fw_features
, "enabled",
2222 "inst-l1d-flush-ori30,30,0"))
2223 cp
->character
|= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30
;
2224 if (have_fw_feat(fw_features
, "enabled",
2225 "inst-l1d-flush-trig2"))
2226 cp
->character
|= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2
;
2227 if (have_fw_feat(fw_features
, "enabled",
2228 "fw-l1d-thread-split"))
2229 cp
->character
|= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV
;
2230 if (have_fw_feat(fw_features
, "enabled",
2231 "fw-count-cache-disabled"))
2232 cp
->character
|= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS
;
2233 cp
->character_mask
= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31
|
2234 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED
|
2235 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30
|
2236 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2
|
2237 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV
|
2238 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS
;
2240 if (have_fw_feat(fw_features
, "enabled",
2241 "speculation-policy-favor-security"))
2242 cp
->behaviour
|= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY
;
2243 if (!have_fw_feat(fw_features
, "disabled",
2244 "needs-l1d-flush-msr-pr-0-to-1"))
2245 cp
->behaviour
|= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR
;
2246 if (!have_fw_feat(fw_features
, "disabled",
2247 "needs-spec-barrier-for-bound-checks"))
2248 cp
->behaviour
|= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR
;
2249 cp
->behaviour_mask
= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY
|
2250 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR
|
2251 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR
;
2253 of_node_put(fw_features
);
2260 long kvm_arch_vm_ioctl(struct file
*filp
,
2261 unsigned int ioctl
, unsigned long arg
)
2263 struct kvm
*kvm __maybe_unused
= filp
->private_data
;
2264 void __user
*argp
= (void __user
*)arg
;
2268 case KVM_PPC_GET_PVINFO
: {
2269 struct kvm_ppc_pvinfo pvinfo
;
2270 memset(&pvinfo
, 0, sizeof(pvinfo
));
2271 r
= kvm_vm_ioctl_get_pvinfo(&pvinfo
);
2272 if (copy_to_user(argp
, &pvinfo
, sizeof(pvinfo
))) {
2279 case KVM_ENABLE_CAP
:
2281 struct kvm_enable_cap cap
;
2283 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
2285 r
= kvm_vm_ioctl_enable_cap(kvm
, &cap
);
2288 #ifdef CONFIG_SPAPR_TCE_IOMMU
2289 case KVM_CREATE_SPAPR_TCE_64
: {
2290 struct kvm_create_spapr_tce_64 create_tce_64
;
2293 if (copy_from_user(&create_tce_64
, argp
, sizeof(create_tce_64
)))
2295 if (create_tce_64
.flags
) {
2299 r
= kvm_vm_ioctl_create_spapr_tce(kvm
, &create_tce_64
);
2302 case KVM_CREATE_SPAPR_TCE
: {
2303 struct kvm_create_spapr_tce create_tce
;
2304 struct kvm_create_spapr_tce_64 create_tce_64
;
2307 if (copy_from_user(&create_tce
, argp
, sizeof(create_tce
)))
2310 create_tce_64
.liobn
= create_tce
.liobn
;
2311 create_tce_64
.page_shift
= IOMMU_PAGE_SHIFT_4K
;
2312 create_tce_64
.offset
= 0;
2313 create_tce_64
.size
= create_tce
.window_size
>>
2314 IOMMU_PAGE_SHIFT_4K
;
2315 create_tce_64
.flags
= 0;
2316 r
= kvm_vm_ioctl_create_spapr_tce(kvm
, &create_tce_64
);
2320 #ifdef CONFIG_PPC_BOOK3S_64
2321 case KVM_PPC_GET_SMMU_INFO
: {
2322 struct kvm_ppc_smmu_info info
;
2323 struct kvm
*kvm
= filp
->private_data
;
2325 memset(&info
, 0, sizeof(info
));
2326 r
= kvm
->arch
.kvm_ops
->get_smmu_info(kvm
, &info
);
2327 if (r
>= 0 && copy_to_user(argp
, &info
, sizeof(info
)))
2331 case KVM_PPC_RTAS_DEFINE_TOKEN
: {
2332 struct kvm
*kvm
= filp
->private_data
;
2334 r
= kvm_vm_ioctl_rtas_define_token(kvm
, argp
);
2337 case KVM_PPC_CONFIGURE_V3_MMU
: {
2338 struct kvm
*kvm
= filp
->private_data
;
2339 struct kvm_ppc_mmuv3_cfg cfg
;
2342 if (!kvm
->arch
.kvm_ops
->configure_mmu
)
2345 if (copy_from_user(&cfg
, argp
, sizeof(cfg
)))
2347 r
= kvm
->arch
.kvm_ops
->configure_mmu(kvm
, &cfg
);
2350 case KVM_PPC_GET_RMMU_INFO
: {
2351 struct kvm
*kvm
= filp
->private_data
;
2352 struct kvm_ppc_rmmu_info info
;
2355 if (!kvm
->arch
.kvm_ops
->get_rmmu_info
)
2357 r
= kvm
->arch
.kvm_ops
->get_rmmu_info(kvm
, &info
);
2358 if (r
>= 0 && copy_to_user(argp
, &info
, sizeof(info
)))
2362 case KVM_PPC_GET_CPU_CHAR
: {
2363 struct kvm_ppc_cpu_char cpuchar
;
2365 r
= kvmppc_get_cpu_char(&cpuchar
);
2366 if (r
>= 0 && copy_to_user(argp
, &cpuchar
, sizeof(cpuchar
)))
2371 struct kvm
*kvm
= filp
->private_data
;
2372 r
= kvm
->arch
.kvm_ops
->arch_vm_ioctl(filp
, ioctl
, arg
);
2374 #else /* CONFIG_PPC_BOOK3S_64 */
2383 static unsigned long lpid_inuse
[BITS_TO_LONGS(KVMPPC_NR_LPIDS
)];
2384 static unsigned long nr_lpids
;
2386 long kvmppc_alloc_lpid(void)
2391 lpid
= find_first_zero_bit(lpid_inuse
, KVMPPC_NR_LPIDS
);
2392 if (lpid
>= nr_lpids
) {
2393 pr_err("%s: No LPIDs free\n", __func__
);
2396 } while (test_and_set_bit(lpid
, lpid_inuse
));
2400 EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid
);
2402 void kvmppc_claim_lpid(long lpid
)
2404 set_bit(lpid
, lpid_inuse
);
2406 EXPORT_SYMBOL_GPL(kvmppc_claim_lpid
);
2408 void kvmppc_free_lpid(long lpid
)
2410 clear_bit(lpid
, lpid_inuse
);
2412 EXPORT_SYMBOL_GPL(kvmppc_free_lpid
);
2414 void kvmppc_init_lpid(unsigned long nr_lpids_param
)
2416 nr_lpids
= min_t(unsigned long, KVMPPC_NR_LPIDS
, nr_lpids_param
);
2417 memset(lpid_inuse
, 0, sizeof(lpid_inuse
));
2419 EXPORT_SYMBOL_GPL(kvmppc_init_lpid
);
2421 int kvm_arch_init(void *opaque
)
2426 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr
);