1
2#ifndef __KVM_X86_VMX_EVMCS_H
3#define __KVM_X86_VMX_EVMCS_H
4
5#include <linux/jump_label.h>
6
7#include <asm/hyperv-tlfs.h>
8#include <asm/mshyperv.h>
9#include <asm/vmx.h>
10
11#include "capabilities.h"
12#include "vmcs.h"
13
14struct vmcs_config;
15
16DECLARE_STATIC_KEY_FALSE(enable_evmcs);
17
18#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
19
20#define KVM_EVMCS_VERSION 1
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
51 PIN_BASED_VMX_PREEMPTION_TIMER)
52#define EVMCS1_UNSUPPORTED_2NDEXEC \
53 (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \
54 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \
55 SECONDARY_EXEC_APIC_REGISTER_VIRT | \
56 SECONDARY_EXEC_ENABLE_PML | \
57 SECONDARY_EXEC_ENABLE_VMFUNC | \
58 SECONDARY_EXEC_SHADOW_VMCS | \
59 SECONDARY_EXEC_TSC_SCALING | \
60 SECONDARY_EXEC_PAUSE_LOOP_EXITING)
61#define EVMCS1_UNSUPPORTED_VMEXIT_CTRL (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
62#define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
63#define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
64
65#if IS_ENABLED(CONFIG_HYPERV)
66
67struct evmcs_field {
68 u16 offset;
69 u16 clean_field;
70};
71
72extern const struct evmcs_field vmcs_field_to_evmcs_1[];
73extern const unsigned int nr_evmcs_1_fields;
74
75#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
76
77static __always_inline int get_evmcs_offset(unsigned long field,
78 u16 *clean_field)
79{
80 unsigned int index = ROL16(field, 6);
81 const struct evmcs_field *evmcs_field;
82
83 if (unlikely(index >= nr_evmcs_1_fields)) {
84 WARN_ONCE(1, "KVM: accessing unsupported EVMCS field %lx\n",
85 field);
86 return -ENOENT;
87 }
88
89 evmcs_field = &vmcs_field_to_evmcs_1[index];
90
91 if (clean_field)
92 *clean_field = evmcs_field->clean_field;
93
94 return evmcs_field->offset;
95}
96
97#undef ROL16
98
99static inline void evmcs_write64(unsigned long field, u64 value)
100{
101 u16 clean_field;
102 int offset = get_evmcs_offset(field, &clean_field);
103
104 if (offset < 0)
105 return;
106
107 *(u64 *)((char *)current_evmcs + offset) = value;
108
109 current_evmcs->hv_clean_fields &= ~clean_field;
110}
111
112static inline void evmcs_write32(unsigned long field, u32 value)
113{
114 u16 clean_field;
115 int offset = get_evmcs_offset(field, &clean_field);
116
117 if (offset < 0)
118 return;
119
120 *(u32 *)((char *)current_evmcs + offset) = value;
121 current_evmcs->hv_clean_fields &= ~clean_field;
122}
123
124static inline void evmcs_write16(unsigned long field, u16 value)
125{
126 u16 clean_field;
127 int offset = get_evmcs_offset(field, &clean_field);
128
129 if (offset < 0)
130 return;
131
132 *(u16 *)((char *)current_evmcs + offset) = value;
133 current_evmcs->hv_clean_fields &= ~clean_field;
134}
135
136static inline u64 evmcs_read64(unsigned long field)
137{
138 int offset = get_evmcs_offset(field, NULL);
139
140 if (offset < 0)
141 return 0;
142
143 return *(u64 *)((char *)current_evmcs + offset);
144}
145
146static inline u32 evmcs_read32(unsigned long field)
147{
148 int offset = get_evmcs_offset(field, NULL);
149
150 if (offset < 0)
151 return 0;
152
153 return *(u32 *)((char *)current_evmcs + offset);
154}
155
156static inline u16 evmcs_read16(unsigned long field)
157{
158 int offset = get_evmcs_offset(field, NULL);
159
160 if (offset < 0)
161 return 0;
162
163 return *(u16 *)((char *)current_evmcs + offset);
164}
165
166static inline void evmcs_touch_msr_bitmap(void)
167{
168 if (unlikely(!current_evmcs))
169 return;
170
171 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
172 current_evmcs->hv_clean_fields &=
173 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
174}
175
176static inline void evmcs_load(u64 phys_addr)
177{
178 struct hv_vp_assist_page *vp_ap =
179 hv_get_vp_assist_page(smp_processor_id());
180
181 if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall)
182 vp_ap->nested_control.features.directhypercall = 1;
183 vp_ap->current_nested_vmcs = phys_addr;
184 vp_ap->enlighten_vmentry = 1;
185}
186
187void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf);
188#else
189static inline void evmcs_write64(unsigned long field, u64 value) {}
190static inline void evmcs_write32(unsigned long field, u32 value) {}
191static inline void evmcs_write16(unsigned long field, u16 value) {}
192static inline u64 evmcs_read64(unsigned long field) { return 0; }
193static inline u32 evmcs_read32(unsigned long field) { return 0; }
194static inline u16 evmcs_read16(unsigned long field) { return 0; }
195static inline void evmcs_load(u64 phys_addr) {}
196static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
197static inline void evmcs_touch_msr_bitmap(void) {}
198#endif
199
200bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa);
201uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu);
202int nested_enable_evmcs(struct kvm_vcpu *vcpu,
203 uint16_t *vmcs_version);
204
205#endif
206