1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright © 2019 Oracle and/or its affiliates. All rights reserved. 4 * Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 5 * 6 * KVM Xen emulation 7 */ 8 9 #ifndef __ARCH_X86_KVM_XEN_H__ 10 #define __ARCH_X86_KVM_XEN_H__ 11 12 #include <asm/xen/cpuid.h> 13 #include <asm/xen/hypervisor.h> 14 15 #ifdef CONFIG_KVM_XEN 16 #include <linux/jump_label_ratelimit.h> 17 18 extern struct static_key_false_deferred kvm_xen_enabled; 19 20 int __kvm_xen_has_interrupt(struct kvm_vcpu *vcpu); 21 void kvm_xen_inject_pending_events(struct kvm_vcpu *vcpu); 22 void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *vcpu); 23 int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data); 24 int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data); 25 int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data); 26 int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data); 27 int kvm_xen_hvm_evtchn_send(struct kvm *kvm, struct kvm_irq_routing_xen_evtchn *evt); 28 int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data); 29 int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc); 30 void kvm_xen_init_vm(struct kvm *kvm); 31 void kvm_xen_destroy_vm(struct kvm *kvm); 32 void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu); 33 void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu); 34 int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, 35 struct kvm *kvm); 36 int kvm_xen_setup_evtchn(struct kvm *kvm, 37 struct kvm_kernel_irq_routing_entry *e, 38 const struct kvm_irq_routing_entry *ue); 39 40 static inline void kvm_xen_sw_enable_lapic(struct kvm_vcpu *vcpu) 41 { 42 /* 43 * The local APIC is being enabled. If the per-vCPU upcall vector is 44 * set and the vCPU's evtchn_upcall_pending flag is set, inject the 45 * interrupt. 46 */ 47 if (static_branch_unlikely(&kvm_xen_enabled.key) && 48 vcpu->arch.xen.vcpu_info_cache.active && 49 vcpu->arch.xen.upcall_vector && __kvm_xen_has_interrupt(vcpu)) 50 kvm_xen_inject_vcpu_vector(vcpu); 51 } 52 53 static inline bool kvm_xen_msr_enabled(struct kvm *kvm) 54 { 55 return static_branch_unlikely(&kvm_xen_enabled.key) && 56 kvm->arch.xen.hvm_config.msr; 57 } 58 59 static inline bool kvm_xen_is_hypercall_page_msr(struct kvm *kvm, u32 msr) 60 { 61 if (!static_branch_unlikely(&kvm_xen_enabled.key)) 62 return false; 63 64 return msr && msr == kvm->arch.xen.hvm_config.msr; 65 } 66 67 static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm) 68 { 69 return static_branch_unlikely(&kvm_xen_enabled.key) && 70 (kvm->arch.xen.hvm_config.flags & 71 KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL); 72 } 73 74 static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu) 75 { 76 if (static_branch_unlikely(&kvm_xen_enabled.key) && 77 vcpu->arch.xen.vcpu_info_cache.active && 78 vcpu->kvm->arch.xen.upcall_vector) 79 return __kvm_xen_has_interrupt(vcpu); 80 81 return 0; 82 } 83 84 static inline bool kvm_xen_has_pending_events(struct kvm_vcpu *vcpu) 85 { 86 return static_branch_unlikely(&kvm_xen_enabled.key) && 87 vcpu->arch.xen.evtchn_pending_sel; 88 } 89 90 static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu) 91 { 92 return !!vcpu->arch.xen.timer_virq; 93 } 94 95 static inline int kvm_xen_has_pending_timer(struct kvm_vcpu *vcpu) 96 { 97 if (kvm_xen_hypercall_enabled(vcpu->kvm) && kvm_xen_timer_enabled(vcpu)) 98 return atomic_read(&vcpu->arch.xen.timer_pending); 99 100 return 0; 101 } 102 103 void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu); 104 #else 105 static inline int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data) 106 { 107 return 1; 108 } 109 110 static inline void kvm_xen_init_vm(struct kvm *kvm) 111 { 112 } 113 114 static inline void kvm_xen_destroy_vm(struct kvm *kvm) 115 { 116 } 117 118 static inline void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu) 119 { 120 } 121 122 static inline void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu) 123 { 124 } 125 126 static inline void kvm_xen_sw_enable_lapic(struct kvm_vcpu *vcpu) 127 { 128 } 129 130 static inline bool kvm_xen_msr_enabled(struct kvm *kvm) 131 { 132 return false; 133 } 134 135 static inline bool kvm_xen_is_hypercall_page_msr(struct kvm *kvm, u32 msr) 136 { 137 return false; 138 } 139 140 static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm) 141 { 142 return false; 143 } 144 145 static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu) 146 { 147 return 0; 148 } 149 150 static inline void kvm_xen_inject_pending_events(struct kvm_vcpu *vcpu) 151 { 152 } 153 154 static inline bool kvm_xen_has_pending_events(struct kvm_vcpu *vcpu) 155 { 156 return false; 157 } 158 159 static inline int kvm_xen_has_pending_timer(struct kvm_vcpu *vcpu) 160 { 161 return 0; 162 } 163 164 static inline void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu) 165 { 166 } 167 168 static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu) 169 { 170 return false; 171 } 172 #endif 173 174 int kvm_xen_hypercall(struct kvm_vcpu *vcpu); 175 176 #include <asm/pvclock-abi.h> 177 #include <asm/xen/interface.h> 178 #include <xen/interface/vcpu.h> 179 180 void kvm_xen_update_runstate(struct kvm_vcpu *vcpu, int state); 181 182 static inline void kvm_xen_runstate_set_running(struct kvm_vcpu *vcpu) 183 { 184 kvm_xen_update_runstate(vcpu, RUNSTATE_running); 185 } 186 187 static inline void kvm_xen_runstate_set_preempted(struct kvm_vcpu *vcpu) 188 { 189 /* 190 * If the vCPU wasn't preempted but took a normal exit for 191 * some reason (hypercalls, I/O, etc.), that is accounted as 192 * still RUNSTATE_running, as the VMM is still operating on 193 * behalf of the vCPU. Only if the VMM does actually block 194 * does it need to enter RUNSTATE_blocked. 195 */ 196 if (WARN_ON_ONCE(!vcpu->preempted)) 197 return; 198 199 kvm_xen_update_runstate(vcpu, RUNSTATE_runnable); 200 } 201 202 /* 32-bit compatibility definitions, also used natively in 32-bit build */ 203 struct compat_arch_vcpu_info { 204 unsigned int cr2; 205 unsigned int pad[5]; 206 }; 207 208 struct compat_vcpu_info { 209 uint8_t evtchn_upcall_pending; 210 uint8_t evtchn_upcall_mask; 211 uint16_t pad; 212 uint32_t evtchn_pending_sel; 213 struct compat_arch_vcpu_info arch; 214 struct pvclock_vcpu_time_info time; 215 }; /* 64 bytes (x86) */ 216 217 struct compat_arch_shared_info { 218 unsigned int max_pfn; 219 unsigned int pfn_to_mfn_frame_list_list; 220 unsigned int nmi_reason; 221 unsigned int p2m_cr3; 222 unsigned int p2m_vaddr; 223 unsigned int p2m_generation; 224 uint32_t wc_sec_hi; 225 }; 226 227 struct compat_shared_info { 228 struct compat_vcpu_info vcpu_info[MAX_VIRT_CPUS]; 229 uint32_t evtchn_pending[32]; 230 uint32_t evtchn_mask[32]; 231 struct pvclock_wall_clock wc; 232 struct compat_arch_shared_info arch; 233 }; 234 235 #define COMPAT_EVTCHN_2L_NR_CHANNELS (8 * \ 236 sizeof_field(struct compat_shared_info, \ 237 evtchn_pending)) 238 struct compat_vcpu_runstate_info { 239 int state; 240 uint64_t state_entry_time; 241 uint64_t time[4]; 242 } __attribute__((packed)); 243 244 struct compat_sched_poll { 245 /* This is actually a guest virtual address which points to ports. */ 246 uint32_t ports; 247 unsigned int nr_ports; 248 uint64_t timeout; 249 }; 250 251 #endif /* __ARCH_X86_KVM_XEN_H__ */ 252