1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 3 #ifndef __ARCH_X86_KVM_VMX_ONHYPERV_H__ 4 #define __ARCH_X86_KVM_VMX_ONHYPERV_H__ 5 6 #include <asm/hyperv-tlfs.h> 7 8 #include <linux/jump_label.h> 9 10 #include "capabilities.h" 11 #include "hyperv.h" 12 #include "vmcs12.h" 13 14 #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs)) 15 16 #if IS_ENABLED(CONFIG_HYPERV) 17 18 DECLARE_STATIC_KEY_FALSE(__kvm_is_using_evmcs); 19 20 static __always_inline bool kvm_is_using_evmcs(void) 21 { 22 return static_branch_unlikely(&__kvm_is_using_evmcs); 23 } 24 25 static __always_inline int get_evmcs_offset(unsigned long field, 26 u16 *clean_field) 27 { 28 int offset = evmcs_field_offset(field, clean_field); 29 30 WARN_ONCE(offset < 0, "accessing unsupported EVMCS field %lx\n", field); 31 return offset; 32 } 33 34 static __always_inline void evmcs_write64(unsigned long field, u64 value) 35 { 36 u16 clean_field; 37 int offset = get_evmcs_offset(field, &clean_field); 38 39 if (offset < 0) 40 return; 41 42 *(u64 *)((char *)current_evmcs + offset) = value; 43 44 current_evmcs->hv_clean_fields &= ~clean_field; 45 } 46 47 static __always_inline void evmcs_write32(unsigned long field, u32 value) 48 { 49 u16 clean_field; 50 int offset = get_evmcs_offset(field, &clean_field); 51 52 if (offset < 0) 53 return; 54 55 *(u32 *)((char *)current_evmcs + offset) = value; 56 current_evmcs->hv_clean_fields &= ~clean_field; 57 } 58 59 static __always_inline void evmcs_write16(unsigned long field, u16 value) 60 { 61 u16 clean_field; 62 int offset = get_evmcs_offset(field, &clean_field); 63 64 if (offset < 0) 65 return; 66 67 *(u16 *)((char *)current_evmcs + offset) = value; 68 current_evmcs->hv_clean_fields &= ~clean_field; 69 } 70 71 static __always_inline u64 evmcs_read64(unsigned long field) 72 { 73 int offset = get_evmcs_offset(field, NULL); 74 75 if (offset < 0) 76 return 0; 77 78 return *(u64 *)((char *)current_evmcs + offset); 79 } 80 81 static __always_inline u32 evmcs_read32(unsigned long field) 82 { 83 int offset = get_evmcs_offset(field, NULL); 84 85 if (offset < 0) 86 return 0; 87 88 return *(u32 *)((char *)current_evmcs + offset); 89 } 90 91 static __always_inline u16 evmcs_read16(unsigned long field) 92 { 93 int offset = get_evmcs_offset(field, NULL); 94 95 if (offset < 0) 96 return 0; 97 98 return *(u16 *)((char *)current_evmcs + offset); 99 } 100 101 static inline void evmcs_load(u64 phys_addr) 102 { 103 struct hv_vp_assist_page *vp_ap = 104 hv_get_vp_assist_page(smp_processor_id()); 105 106 if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall) 107 vp_ap->nested_control.features.directhypercall = 1; 108 vp_ap->current_nested_vmcs = phys_addr; 109 vp_ap->enlighten_vmentry = 1; 110 } 111 112 void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf); 113 #else /* !IS_ENABLED(CONFIG_HYPERV) */ 114 static __always_inline bool kvm_is_using_evmcs(void) { return false; } 115 static __always_inline void evmcs_write64(unsigned long field, u64 value) {} 116 static __always_inline void evmcs_write32(unsigned long field, u32 value) {} 117 static __always_inline void evmcs_write16(unsigned long field, u16 value) {} 118 static __always_inline u64 evmcs_read64(unsigned long field) { return 0; } 119 static __always_inline u32 evmcs_read32(unsigned long field) { return 0; } 120 static __always_inline u16 evmcs_read16(unsigned long field) { return 0; } 121 static inline void evmcs_load(u64 phys_addr) {} 122 #endif /* IS_ENABLED(CONFIG_HYPERV) */ 123 124 #endif /* __ARCH_X86_KVM_VMX_ONHYPERV_H__ */ 125