• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/errno.h>
4 #include <linux/smp.h>
5 
6 #include "../hyperv.h"
7 #include "../cpuid.h"
8 #include "evmcs.h"
9 #include "vmcs.h"
10 #include "vmx.h"
11 #include "trace.h"
12 
13 DEFINE_STATIC_KEY_FALSE(enable_evmcs);
14 
15 #if IS_ENABLED(CONFIG_HYPERV)
16 
17 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
18 #define EVMCS1_OFFSET(x) offsetof(struct hv_enlightened_vmcs, x)
19 #define EVMCS1_FIELD(number, name, clean_field)[ROL16(number, 6)] = \
20 		{EVMCS1_OFFSET(name), clean_field}
21 
22 const struct evmcs_field vmcs_field_to_evmcs_1[] = {
23 	/* 64 bit rw */
24 	EVMCS1_FIELD(GUEST_RIP, guest_rip,
25 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
26 	EVMCS1_FIELD(GUEST_RSP, guest_rsp,
27 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC),
28 	EVMCS1_FIELD(GUEST_RFLAGS, guest_rflags,
29 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC),
30 	EVMCS1_FIELD(HOST_IA32_PAT, host_ia32_pat,
31 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
32 	EVMCS1_FIELD(HOST_IA32_EFER, host_ia32_efer,
33 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
34 	EVMCS1_FIELD(HOST_CR0, host_cr0,
35 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
36 	EVMCS1_FIELD(HOST_CR3, host_cr3,
37 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
38 	EVMCS1_FIELD(HOST_CR4, host_cr4,
39 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
40 	EVMCS1_FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp,
41 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
42 	EVMCS1_FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip,
43 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
44 	EVMCS1_FIELD(HOST_RIP, host_rip,
45 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
46 	EVMCS1_FIELD(IO_BITMAP_A, io_bitmap_a,
47 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP),
48 	EVMCS1_FIELD(IO_BITMAP_B, io_bitmap_b,
49 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP),
50 	EVMCS1_FIELD(MSR_BITMAP, msr_bitmap,
51 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP),
52 	EVMCS1_FIELD(GUEST_ES_BASE, guest_es_base,
53 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
54 	EVMCS1_FIELD(GUEST_CS_BASE, guest_cs_base,
55 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
56 	EVMCS1_FIELD(GUEST_SS_BASE, guest_ss_base,
57 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
58 	EVMCS1_FIELD(GUEST_DS_BASE, guest_ds_base,
59 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
60 	EVMCS1_FIELD(GUEST_FS_BASE, guest_fs_base,
61 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
62 	EVMCS1_FIELD(GUEST_GS_BASE, guest_gs_base,
63 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
64 	EVMCS1_FIELD(GUEST_LDTR_BASE, guest_ldtr_base,
65 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
66 	EVMCS1_FIELD(GUEST_TR_BASE, guest_tr_base,
67 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
68 	EVMCS1_FIELD(GUEST_GDTR_BASE, guest_gdtr_base,
69 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
70 	EVMCS1_FIELD(GUEST_IDTR_BASE, guest_idtr_base,
71 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
72 	EVMCS1_FIELD(TSC_OFFSET, tsc_offset,
73 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2),
74 	EVMCS1_FIELD(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr,
75 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2),
76 	EVMCS1_FIELD(VMCS_LINK_POINTER, vmcs_link_pointer,
77 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
78 	EVMCS1_FIELD(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl,
79 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
80 	EVMCS1_FIELD(GUEST_IA32_PAT, guest_ia32_pat,
81 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
82 	EVMCS1_FIELD(GUEST_IA32_EFER, guest_ia32_efer,
83 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
84 	EVMCS1_FIELD(GUEST_PDPTR0, guest_pdptr0,
85 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
86 	EVMCS1_FIELD(GUEST_PDPTR1, guest_pdptr1,
87 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
88 	EVMCS1_FIELD(GUEST_PDPTR2, guest_pdptr2,
89 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
90 	EVMCS1_FIELD(GUEST_PDPTR3, guest_pdptr3,
91 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
92 	EVMCS1_FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions,
93 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
94 	EVMCS1_FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp,
95 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
96 	EVMCS1_FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip,
97 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
98 	EVMCS1_FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask,
99 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
100 	EVMCS1_FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask,
101 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
102 	EVMCS1_FIELD(CR0_READ_SHADOW, cr0_read_shadow,
103 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
104 	EVMCS1_FIELD(CR4_READ_SHADOW, cr4_read_shadow,
105 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
106 	EVMCS1_FIELD(GUEST_CR0, guest_cr0,
107 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
108 	EVMCS1_FIELD(GUEST_CR3, guest_cr3,
109 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
110 	EVMCS1_FIELD(GUEST_CR4, guest_cr4,
111 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
112 	EVMCS1_FIELD(GUEST_DR7, guest_dr7,
113 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
114 	EVMCS1_FIELD(HOST_FS_BASE, host_fs_base,
115 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
116 	EVMCS1_FIELD(HOST_GS_BASE, host_gs_base,
117 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
118 	EVMCS1_FIELD(HOST_TR_BASE, host_tr_base,
119 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
120 	EVMCS1_FIELD(HOST_GDTR_BASE, host_gdtr_base,
121 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
122 	EVMCS1_FIELD(HOST_IDTR_BASE, host_idtr_base,
123 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
124 	EVMCS1_FIELD(HOST_RSP, host_rsp,
125 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
126 	EVMCS1_FIELD(EPT_POINTER, ept_pointer,
127 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT),
128 	EVMCS1_FIELD(GUEST_BNDCFGS, guest_bndcfgs,
129 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
130 	EVMCS1_FIELD(XSS_EXIT_BITMAP, xss_exit_bitmap,
131 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2),
132 
133 	/* 64 bit read only */
134 	EVMCS1_FIELD(GUEST_PHYSICAL_ADDRESS, guest_physical_address,
135 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
136 	EVMCS1_FIELD(EXIT_QUALIFICATION, exit_qualification,
137 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
138 	/*
139 	 * Not defined in KVM:
140 	 *
141 	 * EVMCS1_FIELD(0x00006402, exit_io_instruction_ecx,
142 	 *		HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE);
143 	 * EVMCS1_FIELD(0x00006404, exit_io_instruction_esi,
144 	 *		HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE);
145 	 * EVMCS1_FIELD(0x00006406, exit_io_instruction_esi,
146 	 *		HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE);
147 	 * EVMCS1_FIELD(0x00006408, exit_io_instruction_eip,
148 	 *		HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE);
149 	 */
150 	EVMCS1_FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address,
151 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
152 
153 	/*
154 	 * No mask defined in the spec as Hyper-V doesn't currently support
155 	 * these. Future proof by resetting the whole clean field mask on
156 	 * access.
157 	 */
158 	EVMCS1_FIELD(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr,
159 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
160 	EVMCS1_FIELD(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr,
161 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
162 	EVMCS1_FIELD(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr,
163 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
164 
165 	/* 32 bit rw */
166 	EVMCS1_FIELD(TPR_THRESHOLD, tpr_threshold,
167 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
168 	EVMCS1_FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info,
169 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC),
170 	EVMCS1_FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control,
171 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC),
172 	EVMCS1_FIELD(EXCEPTION_BITMAP, exception_bitmap,
173 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN),
174 	EVMCS1_FIELD(VM_ENTRY_CONTROLS, vm_entry_controls,
175 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY),
176 	EVMCS1_FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field,
177 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT),
178 	EVMCS1_FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE,
179 		     vm_entry_exception_error_code,
180 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT),
181 	EVMCS1_FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len,
182 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT),
183 	EVMCS1_FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs,
184 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
185 	EVMCS1_FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control,
186 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1),
187 	EVMCS1_FIELD(VM_EXIT_CONTROLS, vm_exit_controls,
188 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1),
189 	EVMCS1_FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control,
190 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1),
191 	EVMCS1_FIELD(GUEST_ES_LIMIT, guest_es_limit,
192 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
193 	EVMCS1_FIELD(GUEST_CS_LIMIT, guest_cs_limit,
194 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
195 	EVMCS1_FIELD(GUEST_SS_LIMIT, guest_ss_limit,
196 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
197 	EVMCS1_FIELD(GUEST_DS_LIMIT, guest_ds_limit,
198 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
199 	EVMCS1_FIELD(GUEST_FS_LIMIT, guest_fs_limit,
200 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
201 	EVMCS1_FIELD(GUEST_GS_LIMIT, guest_gs_limit,
202 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
203 	EVMCS1_FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit,
204 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
205 	EVMCS1_FIELD(GUEST_TR_LIMIT, guest_tr_limit,
206 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
207 	EVMCS1_FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit,
208 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
209 	EVMCS1_FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit,
210 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
211 	EVMCS1_FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes,
212 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
213 	EVMCS1_FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes,
214 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
215 	EVMCS1_FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes,
216 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
217 	EVMCS1_FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes,
218 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
219 	EVMCS1_FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes,
220 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
221 	EVMCS1_FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes,
222 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
223 	EVMCS1_FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes,
224 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
225 	EVMCS1_FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes,
226 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
227 	EVMCS1_FIELD(GUEST_ACTIVITY_STATE, guest_activity_state,
228 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
229 	EVMCS1_FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs,
230 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
231 
232 	/* 32 bit read only */
233 	EVMCS1_FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error,
234 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
235 	EVMCS1_FIELD(VM_EXIT_REASON, vm_exit_reason,
236 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
237 	EVMCS1_FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info,
238 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
239 	EVMCS1_FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code,
240 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
241 	EVMCS1_FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field,
242 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
243 	EVMCS1_FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code,
244 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
245 	EVMCS1_FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len,
246 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
247 	EVMCS1_FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info,
248 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
249 
250 	/* No mask defined in the spec (not used) */
251 	EVMCS1_FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask,
252 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
253 	EVMCS1_FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match,
254 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
255 	EVMCS1_FIELD(CR3_TARGET_COUNT, cr3_target_count,
256 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
257 	EVMCS1_FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count,
258 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
259 	EVMCS1_FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count,
260 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
261 	EVMCS1_FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count,
262 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
263 
264 	/* 16 bit rw */
265 	EVMCS1_FIELD(HOST_ES_SELECTOR, host_es_selector,
266 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
267 	EVMCS1_FIELD(HOST_CS_SELECTOR, host_cs_selector,
268 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
269 	EVMCS1_FIELD(HOST_SS_SELECTOR, host_ss_selector,
270 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
271 	EVMCS1_FIELD(HOST_DS_SELECTOR, host_ds_selector,
272 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
273 	EVMCS1_FIELD(HOST_FS_SELECTOR, host_fs_selector,
274 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
275 	EVMCS1_FIELD(HOST_GS_SELECTOR, host_gs_selector,
276 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
277 	EVMCS1_FIELD(HOST_TR_SELECTOR, host_tr_selector,
278 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
279 	EVMCS1_FIELD(GUEST_ES_SELECTOR, guest_es_selector,
280 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
281 	EVMCS1_FIELD(GUEST_CS_SELECTOR, guest_cs_selector,
282 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
283 	EVMCS1_FIELD(GUEST_SS_SELECTOR, guest_ss_selector,
284 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
285 	EVMCS1_FIELD(GUEST_DS_SELECTOR, guest_ds_selector,
286 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
287 	EVMCS1_FIELD(GUEST_FS_SELECTOR, guest_fs_selector,
288 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
289 	EVMCS1_FIELD(GUEST_GS_SELECTOR, guest_gs_selector,
290 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
291 	EVMCS1_FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector,
292 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
293 	EVMCS1_FIELD(GUEST_TR_SELECTOR, guest_tr_selector,
294 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
295 	EVMCS1_FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id,
296 		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT),
297 };
298 const unsigned int nr_evmcs_1_fields = ARRAY_SIZE(vmcs_field_to_evmcs_1);
299 
evmcs_sanitize_exec_ctrls(struct vmcs_config * vmcs_conf)300 __init void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
301 {
302 	vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL;
303 	vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
304 
305 	vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
306 	vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
307 }
308 #endif
309 
nested_enlightened_vmentry(struct kvm_vcpu * vcpu,u64 * evmcs_gpa)310 bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa)
311 {
312 	struct hv_vp_assist_page assist_page;
313 
314 	*evmcs_gpa = -1ull;
315 
316 	if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page)))
317 		return false;
318 
319 	if (unlikely(!assist_page.enlighten_vmentry))
320 		return false;
321 
322 	*evmcs_gpa = assist_page.current_nested_vmcs;
323 
324 	return true;
325 }
326 
nested_get_evmcs_version(struct kvm_vcpu * vcpu)327 uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu)
328 {
329 	struct vcpu_vmx *vmx = to_vmx(vcpu);
330 	/*
331 	 * vmcs_version represents the range of supported Enlightened VMCS
332 	 * versions: lower 8 bits is the minimal version, higher 8 bits is the
333 	 * maximum supported version. KVM supports versions from 1 to
334 	 * KVM_EVMCS_VERSION.
335 	 */
336 	if (kvm_cpu_cap_get(X86_FEATURE_VMX) &&
337 	    vmx->nested.enlightened_vmcs_enabled)
338 		return (KVM_EVMCS_VERSION << 8) | 1;
339 
340 	return 0;
341 }
342 
nested_evmcs_filter_control_msr(u32 msr_index,u64 * pdata)343 void nested_evmcs_filter_control_msr(u32 msr_index, u64 *pdata)
344 {
345 	u32 ctl_low = (u32)*pdata;
346 	u32 ctl_high = (u32)(*pdata >> 32);
347 
348 	/*
349 	 * Hyper-V 2016 and 2019 try using these features even when eVMCS
350 	 * is enabled but there are no corresponding fields.
351 	 */
352 	switch (msr_index) {
353 	case MSR_IA32_VMX_EXIT_CTLS:
354 	case MSR_IA32_VMX_TRUE_EXIT_CTLS:
355 		ctl_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
356 		break;
357 	case MSR_IA32_VMX_ENTRY_CTLS:
358 	case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
359 		ctl_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
360 		break;
361 	case MSR_IA32_VMX_PROCBASED_CTLS2:
362 		ctl_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
363 		break;
364 	case MSR_IA32_VMX_PINBASED_CTLS:
365 		ctl_high &= ~EVMCS1_UNSUPPORTED_PINCTRL;
366 		break;
367 	case MSR_IA32_VMX_VMFUNC:
368 		ctl_low &= ~EVMCS1_UNSUPPORTED_VMFUNC;
369 		break;
370 	}
371 
372 	*pdata = ctl_low | ((u64)ctl_high << 32);
373 }
374 
nested_evmcs_check_controls(struct vmcs12 * vmcs12)375 int nested_evmcs_check_controls(struct vmcs12 *vmcs12)
376 {
377 	int ret = 0;
378 	u32 unsupp_ctl;
379 
380 	unsupp_ctl = vmcs12->pin_based_vm_exec_control &
381 		EVMCS1_UNSUPPORTED_PINCTRL;
382 	if (unsupp_ctl) {
383 		trace_kvm_nested_vmenter_failed(
384 			"eVMCS: unsupported pin-based VM-execution controls",
385 			unsupp_ctl);
386 		ret = -EINVAL;
387 	}
388 
389 	unsupp_ctl = vmcs12->secondary_vm_exec_control &
390 		EVMCS1_UNSUPPORTED_2NDEXEC;
391 	if (unsupp_ctl) {
392 		trace_kvm_nested_vmenter_failed(
393 			"eVMCS: unsupported secondary VM-execution controls",
394 			unsupp_ctl);
395 		ret = -EINVAL;
396 	}
397 
398 	unsupp_ctl = vmcs12->vm_exit_controls &
399 		EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
400 	if (unsupp_ctl) {
401 		trace_kvm_nested_vmenter_failed(
402 			"eVMCS: unsupported VM-exit controls",
403 			unsupp_ctl);
404 		ret = -EINVAL;
405 	}
406 
407 	unsupp_ctl = vmcs12->vm_entry_controls &
408 		EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
409 	if (unsupp_ctl) {
410 		trace_kvm_nested_vmenter_failed(
411 			"eVMCS: unsupported VM-entry controls",
412 			unsupp_ctl);
413 		ret = -EINVAL;
414 	}
415 
416 	unsupp_ctl = vmcs12->vm_function_control & EVMCS1_UNSUPPORTED_VMFUNC;
417 	if (unsupp_ctl) {
418 		trace_kvm_nested_vmenter_failed(
419 			"eVMCS: unsupported VM-function controls",
420 			unsupp_ctl);
421 		ret = -EINVAL;
422 	}
423 
424 	return ret;
425 }
426 
nested_enable_evmcs(struct kvm_vcpu * vcpu,uint16_t * vmcs_version)427 int nested_enable_evmcs(struct kvm_vcpu *vcpu,
428 			uint16_t *vmcs_version)
429 {
430 	struct vcpu_vmx *vmx = to_vmx(vcpu);
431 
432 	vmx->nested.enlightened_vmcs_enabled = true;
433 
434 	if (vmcs_version)
435 		*vmcs_version = nested_get_evmcs_version(vcpu);
436 
437 	return 0;
438 }
439