Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
0004  * Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
0005  *
0006  * KVM Xen emulation
0007  */
0008 
0009 #ifndef __ARCH_X86_KVM_XEN_H__
0010 #define __ARCH_X86_KVM_XEN_H__
0011 
0012 #ifdef CONFIG_KVM_XEN
0013 #include <linux/jump_label_ratelimit.h>
0014 
0015 extern struct static_key_false_deferred kvm_xen_enabled;
0016 
0017 int __kvm_xen_has_interrupt(struct kvm_vcpu *vcpu);
0018 void kvm_xen_inject_pending_events(struct kvm_vcpu *vcpu);
0019 int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
0020 int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
0021 int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
0022 int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
0023 int kvm_xen_hvm_evtchn_send(struct kvm *kvm, struct kvm_irq_routing_xen_evtchn *evt);
0024 int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data);
0025 int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc);
0026 void kvm_xen_init_vm(struct kvm *kvm);
0027 void kvm_xen_destroy_vm(struct kvm *kvm);
0028 void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu);
0029 void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu);
0030 int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe,
0031                 struct kvm *kvm);
0032 int kvm_xen_setup_evtchn(struct kvm *kvm,
0033              struct kvm_kernel_irq_routing_entry *e,
0034              const struct kvm_irq_routing_entry *ue);
0035 
0036 static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
0037 {
0038     return static_branch_unlikely(&kvm_xen_enabled.key) &&
0039         kvm->arch.xen_hvm_config.msr;
0040 }
0041 
0042 static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
0043 {
0044     return static_branch_unlikely(&kvm_xen_enabled.key) &&
0045         (kvm->arch.xen_hvm_config.flags &
0046          KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL);
0047 }
0048 
0049 static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
0050 {
0051     if (static_branch_unlikely(&kvm_xen_enabled.key) &&
0052         vcpu->arch.xen.vcpu_info_cache.active &&
0053         vcpu->kvm->arch.xen.upcall_vector)
0054         return __kvm_xen_has_interrupt(vcpu);
0055 
0056     return 0;
0057 }
0058 
0059 static inline bool kvm_xen_has_pending_events(struct kvm_vcpu *vcpu)
0060 {
0061     return static_branch_unlikely(&kvm_xen_enabled.key) &&
0062         vcpu->arch.xen.evtchn_pending_sel;
0063 }
0064 
0065 static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu)
0066 {
0067     return !!vcpu->arch.xen.timer_virq;
0068 }
0069 
0070 static inline int kvm_xen_has_pending_timer(struct kvm_vcpu *vcpu)
0071 {
0072     if (kvm_xen_hypercall_enabled(vcpu->kvm) && kvm_xen_timer_enabled(vcpu))
0073         return atomic_read(&vcpu->arch.xen.timer_pending);
0074 
0075     return 0;
0076 }
0077 
0078 void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu);
0079 #else
0080 static inline int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
0081 {
0082     return 1;
0083 }
0084 
0085 static inline void kvm_xen_init_vm(struct kvm *kvm)
0086 {
0087 }
0088 
0089 static inline void kvm_xen_destroy_vm(struct kvm *kvm)
0090 {
0091 }
0092 
0093 static inline void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu)
0094 {
0095 }
0096 
0097 static inline void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
0098 {
0099 }
0100 
0101 static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
0102 {
0103     return false;
0104 }
0105 
0106 static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
0107 {
0108     return false;
0109 }
0110 
0111 static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
0112 {
0113     return 0;
0114 }
0115 
0116 static inline void kvm_xen_inject_pending_events(struct kvm_vcpu *vcpu)
0117 {
0118 }
0119 
0120 static inline bool kvm_xen_has_pending_events(struct kvm_vcpu *vcpu)
0121 {
0122     return false;
0123 }
0124 
0125 static inline int kvm_xen_has_pending_timer(struct kvm_vcpu *vcpu)
0126 {
0127     return 0;
0128 }
0129 
0130 static inline void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu)
0131 {
0132 }
0133 
0134 static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu)
0135 {
0136     return false;
0137 }
0138 #endif
0139 
0140 int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
0141 
0142 #include <asm/pvclock-abi.h>
0143 #include <asm/xen/interface.h>
0144 #include <xen/interface/vcpu.h>
0145 
0146 void kvm_xen_update_runstate_guest(struct kvm_vcpu *vcpu, int state);
0147 
0148 static inline void kvm_xen_runstate_set_running(struct kvm_vcpu *vcpu)
0149 {
0150     kvm_xen_update_runstate_guest(vcpu, RUNSTATE_running);
0151 }
0152 
0153 static inline void kvm_xen_runstate_set_preempted(struct kvm_vcpu *vcpu)
0154 {
0155     /*
0156      * If the vCPU wasn't preempted but took a normal exit for
0157      * some reason (hypercalls, I/O, etc.), that is accounted as
0158      * still RUNSTATE_running, as the VMM is still operating on
0159      * behalf of the vCPU. Only if the VMM does actually block
0160      * does it need to enter RUNSTATE_blocked.
0161      */
0162     if (WARN_ON_ONCE(!vcpu->preempted))
0163         return;
0164 
0165     kvm_xen_update_runstate_guest(vcpu, RUNSTATE_runnable);
0166 }
0167 
0168 /* 32-bit compatibility definitions, also used natively in 32-bit build */
0169 struct compat_arch_vcpu_info {
0170     unsigned int cr2;
0171     unsigned int pad[5];
0172 };
0173 
0174 struct compat_vcpu_info {
0175     uint8_t evtchn_upcall_pending;
0176     uint8_t evtchn_upcall_mask;
0177     uint16_t pad;
0178     uint32_t evtchn_pending_sel;
0179     struct compat_arch_vcpu_info arch;
0180     struct pvclock_vcpu_time_info time;
0181 }; /* 64 bytes (x86) */
0182 
0183 struct compat_arch_shared_info {
0184     unsigned int max_pfn;
0185     unsigned int pfn_to_mfn_frame_list_list;
0186     unsigned int nmi_reason;
0187     unsigned int p2m_cr3;
0188     unsigned int p2m_vaddr;
0189     unsigned int p2m_generation;
0190     uint32_t wc_sec_hi;
0191 };
0192 
0193 struct compat_shared_info {
0194     struct compat_vcpu_info vcpu_info[MAX_VIRT_CPUS];
0195     uint32_t evtchn_pending[32];
0196     uint32_t evtchn_mask[32];
0197     struct pvclock_wall_clock wc;
0198     struct compat_arch_shared_info arch;
0199 };
0200 
0201 #define COMPAT_EVTCHN_2L_NR_CHANNELS (8 *               \
0202                       sizeof_field(struct compat_shared_info, \
0203                            evtchn_pending))
0204 struct compat_vcpu_runstate_info {
0205     int state;
0206     uint64_t state_entry_time;
0207     uint64_t time[4];
0208 } __attribute__((packed));
0209 
0210 #endif /* __ARCH_X86_KVM_XEN_H__ */