xref: /openbmc/linux/arch/x86/kvm/xen.h (revision a795cd43c5b5819b8ee94690f22caa5e2e4d8620)
123200b7aSJoao Martins // SPDX-License-Identifier: GPL-2.0
223200b7aSJoao Martins /*
323200b7aSJoao Martins  * Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
423200b7aSJoao Martins  * Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
523200b7aSJoao Martins  *
623200b7aSJoao Martins  * KVM Xen emulation
723200b7aSJoao Martins  */
823200b7aSJoao Martins 
923200b7aSJoao Martins #ifndef __ARCH_X86_KVM_XEN_H__
1023200b7aSJoao Martins #define __ARCH_X86_KVM_XEN_H__
1123200b7aSJoao Martins 
12b59b153dSPaolo Bonzini #ifdef CONFIG_KVM_XEN
137d6bbebbSDavid Woodhouse #include <linux/jump_label_ratelimit.h>
147d6bbebbSDavid Woodhouse 
157d6bbebbSDavid Woodhouse extern struct static_key_false_deferred kvm_xen_enabled;
167d6bbebbSDavid Woodhouse 
1740da8ccdSDavid Woodhouse int __kvm_xen_has_interrupt(struct kvm_vcpu *vcpu);
183e324615SDavid Woodhouse int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
193e324615SDavid Woodhouse int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
20a76b9641SJoao Martins int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
21a76b9641SJoao Martins int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
2223200b7aSJoao Martins int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data);
2378e9878cSDavid Woodhouse int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc);
24319afe68SPaolo Bonzini void kvm_xen_init_vm(struct kvm *kvm);
257d6bbebbSDavid Woodhouse void kvm_xen_destroy_vm(struct kvm *kvm);
26*a795cd43SDavid Woodhouse void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu);
2714243b38SDavid Woodhouse int kvm_xen_set_evtchn_fast(struct kvm_kernel_irq_routing_entry *e,
2814243b38SDavid Woodhouse 			    struct kvm *kvm);
2914243b38SDavid Woodhouse int kvm_xen_setup_evtchn(struct kvm *kvm,
3014243b38SDavid Woodhouse 			 struct kvm_kernel_irq_routing_entry *e,
3114243b38SDavid Woodhouse 			 const struct kvm_irq_routing_entry *ue);
3214243b38SDavid Woodhouse 
3330b5c851SDavid Woodhouse static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
3430b5c851SDavid Woodhouse {
3530b5c851SDavid Woodhouse 	return static_branch_unlikely(&kvm_xen_enabled.key) &&
3630b5c851SDavid Woodhouse 		kvm->arch.xen_hvm_config.msr;
3730b5c851SDavid Woodhouse }
3830b5c851SDavid Woodhouse 
3923200b7aSJoao Martins static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
4023200b7aSJoao Martins {
417d6bbebbSDavid Woodhouse 	return static_branch_unlikely(&kvm_xen_enabled.key) &&
427d6bbebbSDavid Woodhouse 		(kvm->arch.xen_hvm_config.flags &
437d6bbebbSDavid Woodhouse 		 KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL);
4423200b7aSJoao Martins }
4523200b7aSJoao Martins 
4640da8ccdSDavid Woodhouse static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
4740da8ccdSDavid Woodhouse {
4840da8ccdSDavid Woodhouse 	if (static_branch_unlikely(&kvm_xen_enabled.key) &&
4940da8ccdSDavid Woodhouse 	    vcpu->arch.xen.vcpu_info_set && vcpu->kvm->arch.xen.upcall_vector)
5040da8ccdSDavid Woodhouse 		return __kvm_xen_has_interrupt(vcpu);
5140da8ccdSDavid Woodhouse 
5240da8ccdSDavid Woodhouse 	return 0;
5340da8ccdSDavid Woodhouse }
54b59b153dSPaolo Bonzini #else
55b59b153dSPaolo Bonzini static inline int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
56b59b153dSPaolo Bonzini {
57b59b153dSPaolo Bonzini 	return 1;
58b59b153dSPaolo Bonzini }
59b59b153dSPaolo Bonzini 
60319afe68SPaolo Bonzini static inline void kvm_xen_init_vm(struct kvm *kvm)
61319afe68SPaolo Bonzini {
62319afe68SPaolo Bonzini }
63319afe68SPaolo Bonzini 
64b59b153dSPaolo Bonzini static inline void kvm_xen_destroy_vm(struct kvm *kvm)
65b59b153dSPaolo Bonzini {
66b59b153dSPaolo Bonzini }
67b59b153dSPaolo Bonzini 
68*a795cd43SDavid Woodhouse static inline void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
69*a795cd43SDavid Woodhouse {
70*a795cd43SDavid Woodhouse }
71*a795cd43SDavid Woodhouse 
7230b5c851SDavid Woodhouse static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
7330b5c851SDavid Woodhouse {
7430b5c851SDavid Woodhouse 	return false;
7530b5c851SDavid Woodhouse }
7630b5c851SDavid Woodhouse 
77b59b153dSPaolo Bonzini static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
78b59b153dSPaolo Bonzini {
79b59b153dSPaolo Bonzini 	return false;
80b59b153dSPaolo Bonzini }
81b59b153dSPaolo Bonzini 
82b59b153dSPaolo Bonzini static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
83b59b153dSPaolo Bonzini {
84b59b153dSPaolo Bonzini 	return 0;
85b59b153dSPaolo Bonzini }
86b59b153dSPaolo Bonzini #endif
87b59b153dSPaolo Bonzini 
88b59b153dSPaolo Bonzini int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
891ea9f2edSDavid Woodhouse 
901ea9f2edSDavid Woodhouse #include <asm/pvclock-abi.h>
911ea9f2edSDavid Woodhouse #include <asm/xen/interface.h>
9230b5c851SDavid Woodhouse #include <xen/interface/vcpu.h>
931ea9f2edSDavid Woodhouse 
9430b5c851SDavid Woodhouse void kvm_xen_update_runstate_guest(struct kvm_vcpu *vcpu, int state);
9530b5c851SDavid Woodhouse 
9630b5c851SDavid Woodhouse static inline void kvm_xen_runstate_set_running(struct kvm_vcpu *vcpu)
9730b5c851SDavid Woodhouse {
9830b5c851SDavid Woodhouse 	kvm_xen_update_runstate_guest(vcpu, RUNSTATE_running);
9930b5c851SDavid Woodhouse }
10030b5c851SDavid Woodhouse 
10130b5c851SDavid Woodhouse static inline void kvm_xen_runstate_set_preempted(struct kvm_vcpu *vcpu)
10230b5c851SDavid Woodhouse {
10330b5c851SDavid Woodhouse 	/*
10430b5c851SDavid Woodhouse 	 * If the vCPU wasn't preempted but took a normal exit for
10530b5c851SDavid Woodhouse 	 * some reason (hypercalls, I/O, etc.), that is accounted as
10630b5c851SDavid Woodhouse 	 * still RUNSTATE_running, as the VMM is still operating on
10730b5c851SDavid Woodhouse 	 * behalf of the vCPU. Only if the VMM does actually block
10830b5c851SDavid Woodhouse 	 * does it need to enter RUNSTATE_blocked.
10930b5c851SDavid Woodhouse 	 */
11030b5c851SDavid Woodhouse 	if (vcpu->preempted)
11130b5c851SDavid Woodhouse 		kvm_xen_update_runstate_guest(vcpu, RUNSTATE_runnable);
11230b5c851SDavid Woodhouse }
11330b5c851SDavid Woodhouse 
11430b5c851SDavid Woodhouse /* 32-bit compatibility definitions, also used natively in 32-bit build */
1151ea9f2edSDavid Woodhouse struct compat_arch_vcpu_info {
1161ea9f2edSDavid Woodhouse 	unsigned int cr2;
1171ea9f2edSDavid Woodhouse 	unsigned int pad[5];
1181ea9f2edSDavid Woodhouse };
1191ea9f2edSDavid Woodhouse 
1201ea9f2edSDavid Woodhouse struct compat_vcpu_info {
1211ea9f2edSDavid Woodhouse 	uint8_t evtchn_upcall_pending;
1221ea9f2edSDavid Woodhouse 	uint8_t evtchn_upcall_mask;
1237137b7aeSSean Christopherson 	uint16_t pad;
1241ea9f2edSDavid Woodhouse 	uint32_t evtchn_pending_sel;
1251ea9f2edSDavid Woodhouse 	struct compat_arch_vcpu_info arch;
1261ea9f2edSDavid Woodhouse 	struct pvclock_vcpu_time_info time;
1271ea9f2edSDavid Woodhouse }; /* 64 bytes (x86) */
1281ea9f2edSDavid Woodhouse 
1291ea9f2edSDavid Woodhouse struct compat_arch_shared_info {
1301ea9f2edSDavid Woodhouse 	unsigned int max_pfn;
1311ea9f2edSDavid Woodhouse 	unsigned int pfn_to_mfn_frame_list_list;
1321ea9f2edSDavid Woodhouse 	unsigned int nmi_reason;
1331ea9f2edSDavid Woodhouse 	unsigned int p2m_cr3;
1341ea9f2edSDavid Woodhouse 	unsigned int p2m_vaddr;
1351ea9f2edSDavid Woodhouse 	unsigned int p2m_generation;
1361ea9f2edSDavid Woodhouse 	uint32_t wc_sec_hi;
1371ea9f2edSDavid Woodhouse };
1381ea9f2edSDavid Woodhouse 
1391ea9f2edSDavid Woodhouse struct compat_shared_info {
1401ea9f2edSDavid Woodhouse 	struct compat_vcpu_info vcpu_info[MAX_VIRT_CPUS];
1411ea9f2edSDavid Woodhouse 	uint32_t evtchn_pending[32];
1421ea9f2edSDavid Woodhouse 	uint32_t evtchn_mask[32];
1431ea9f2edSDavid Woodhouse 	struct pvclock_wall_clock wc;
1441ea9f2edSDavid Woodhouse 	struct compat_arch_shared_info arch;
1451ea9f2edSDavid Woodhouse };
1461ea9f2edSDavid Woodhouse 
14714243b38SDavid Woodhouse #define COMPAT_EVTCHN_2L_NR_CHANNELS (8 *				\
14814243b38SDavid Woodhouse 				      sizeof_field(struct compat_shared_info, \
14914243b38SDavid Woodhouse 						   evtchn_pending))
15030b5c851SDavid Woodhouse struct compat_vcpu_runstate_info {
15130b5c851SDavid Woodhouse     int state;
15230b5c851SDavid Woodhouse     uint64_t state_entry_time;
15330b5c851SDavid Woodhouse     uint64_t time[4];
15430b5c851SDavid Woodhouse } __attribute__((packed));
15530b5c851SDavid Woodhouse 
15623200b7aSJoao Martins #endif /* __ARCH_X86_KVM_XEN_H__ */
157