18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#include <linux/errno.h>
48c2ecf20Sopenharmony_ci#include <linux/smp.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include "../hyperv.h"
78c2ecf20Sopenharmony_ci#include "../cpuid.h"
88c2ecf20Sopenharmony_ci#include "evmcs.h"
98c2ecf20Sopenharmony_ci#include "vmcs.h"
108c2ecf20Sopenharmony_ci#include "vmx.h"
118c2ecf20Sopenharmony_ci#include "trace.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciDEFINE_STATIC_KEY_FALSE(enable_evmcs);
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_HYPERV)
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
188c2ecf20Sopenharmony_ci#define EVMCS1_OFFSET(x) offsetof(struct hv_enlightened_vmcs, x)
198c2ecf20Sopenharmony_ci#define EVMCS1_FIELD(number, name, clean_field)[ROL16(number, 6)] = \
208c2ecf20Sopenharmony_ci		{EVMCS1_OFFSET(name), clean_field}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciconst struct evmcs_field vmcs_field_to_evmcs_1[] = {
238c2ecf20Sopenharmony_ci	/* 64 bit rw */
248c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_RIP, guest_rip,
258c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
268c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_RSP, guest_rsp,
278c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC),
288c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_RFLAGS, guest_rflags,
298c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC),
308c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_IA32_PAT, host_ia32_pat,
318c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
328c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_IA32_EFER, host_ia32_efer,
338c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
348c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_CR0, host_cr0,
358c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
368c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_CR3, host_cr3,
378c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
388c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_CR4, host_cr4,
398c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
408c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp,
418c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
428c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip,
438c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
448c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_RIP, host_rip,
458c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
468c2ecf20Sopenharmony_ci	EVMCS1_FIELD(IO_BITMAP_A, io_bitmap_a,
478c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP),
488c2ecf20Sopenharmony_ci	EVMCS1_FIELD(IO_BITMAP_B, io_bitmap_b,
498c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP),
508c2ecf20Sopenharmony_ci	EVMCS1_FIELD(MSR_BITMAP, msr_bitmap,
518c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP),
528c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_ES_BASE, guest_es_base,
538c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
548c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_CS_BASE, guest_cs_base,
558c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
568c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_SS_BASE, guest_ss_base,
578c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
588c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_DS_BASE, guest_ds_base,
598c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
608c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_FS_BASE, guest_fs_base,
618c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
628c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_GS_BASE, guest_gs_base,
638c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
648c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_LDTR_BASE, guest_ldtr_base,
658c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
668c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_TR_BASE, guest_tr_base,
678c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
688c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_GDTR_BASE, guest_gdtr_base,
698c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
708c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_IDTR_BASE, guest_idtr_base,
718c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
728c2ecf20Sopenharmony_ci	EVMCS1_FIELD(TSC_OFFSET, tsc_offset,
738c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2),
748c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr,
758c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2),
768c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VMCS_LINK_POINTER, vmcs_link_pointer,
778c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
788c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl,
798c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
808c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_IA32_PAT, guest_ia32_pat,
818c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
828c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_IA32_EFER, guest_ia32_efer,
838c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
848c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_PDPTR0, guest_pdptr0,
858c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
868c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_PDPTR1, guest_pdptr1,
878c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
888c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_PDPTR2, guest_pdptr2,
898c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
908c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_PDPTR3, guest_pdptr3,
918c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
928c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions,
938c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
948c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp,
958c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
968c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip,
978c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
988c2ecf20Sopenharmony_ci	EVMCS1_FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask,
998c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
1008c2ecf20Sopenharmony_ci	EVMCS1_FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask,
1018c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
1028c2ecf20Sopenharmony_ci	EVMCS1_FIELD(CR0_READ_SHADOW, cr0_read_shadow,
1038c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
1048c2ecf20Sopenharmony_ci	EVMCS1_FIELD(CR4_READ_SHADOW, cr4_read_shadow,
1058c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
1068c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_CR0, guest_cr0,
1078c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
1088c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_CR3, guest_cr3,
1098c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
1108c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_CR4, guest_cr4,
1118c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
1128c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_DR7, guest_dr7,
1138c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR),
1148c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_FS_BASE, host_fs_base,
1158c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
1168c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_GS_BASE, host_gs_base,
1178c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
1188c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_TR_BASE, host_tr_base,
1198c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
1208c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_GDTR_BASE, host_gdtr_base,
1218c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
1228c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_IDTR_BASE, host_idtr_base,
1238c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
1248c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_RSP, host_rsp,
1258c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER),
1268c2ecf20Sopenharmony_ci	EVMCS1_FIELD(EPT_POINTER, ept_pointer,
1278c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT),
1288c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_BNDCFGS, guest_bndcfgs,
1298c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
1308c2ecf20Sopenharmony_ci	EVMCS1_FIELD(XSS_EXIT_BITMAP, xss_exit_bitmap,
1318c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2),
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* 64 bit read only */
1348c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_PHYSICAL_ADDRESS, guest_physical_address,
1358c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
1368c2ecf20Sopenharmony_ci	EVMCS1_FIELD(EXIT_QUALIFICATION, exit_qualification,
1378c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
1388c2ecf20Sopenharmony_ci	/*
1398c2ecf20Sopenharmony_ci	 * Not defined in KVM:
1408c2ecf20Sopenharmony_ci	 *
1418c2ecf20Sopenharmony_ci	 * EVMCS1_FIELD(0x00006402, exit_io_instruction_ecx,
1428c2ecf20Sopenharmony_ci	 *		HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE);
1438c2ecf20Sopenharmony_ci	 * EVMCS1_FIELD(0x00006404, exit_io_instruction_esi,
1448c2ecf20Sopenharmony_ci	 *		HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE);
1458c2ecf20Sopenharmony_ci	 * EVMCS1_FIELD(0x00006406, exit_io_instruction_esi,
1468c2ecf20Sopenharmony_ci	 *		HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE);
1478c2ecf20Sopenharmony_ci	 * EVMCS1_FIELD(0x00006408, exit_io_instruction_eip,
1488c2ecf20Sopenharmony_ci	 *		HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE);
1498c2ecf20Sopenharmony_ci	 */
1508c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address,
1518c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	/*
1548c2ecf20Sopenharmony_ci	 * No mask defined in the spec as Hyper-V doesn't currently support
1558c2ecf20Sopenharmony_ci	 * these. Future proof by resetting the whole clean field mask on
1568c2ecf20Sopenharmony_ci	 * access.
1578c2ecf20Sopenharmony_ci	 */
1588c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr,
1598c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
1608c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr,
1618c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
1628c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr,
1638c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	/* 32 bit rw */
1668c2ecf20Sopenharmony_ci	EVMCS1_FIELD(TPR_THRESHOLD, tpr_threshold,
1678c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
1688c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info,
1698c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC),
1708c2ecf20Sopenharmony_ci	EVMCS1_FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control,
1718c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC),
1728c2ecf20Sopenharmony_ci	EVMCS1_FIELD(EXCEPTION_BITMAP, exception_bitmap,
1738c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN),
1748c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_ENTRY_CONTROLS, vm_entry_controls,
1758c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY),
1768c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field,
1778c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT),
1788c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE,
1798c2ecf20Sopenharmony_ci		     vm_entry_exception_error_code,
1808c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT),
1818c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len,
1828c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT),
1838c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs,
1848c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
1858c2ecf20Sopenharmony_ci	EVMCS1_FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control,
1868c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1),
1878c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_EXIT_CONTROLS, vm_exit_controls,
1888c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1),
1898c2ecf20Sopenharmony_ci	EVMCS1_FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control,
1908c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1),
1918c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_ES_LIMIT, guest_es_limit,
1928c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
1938c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_CS_LIMIT, guest_cs_limit,
1948c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
1958c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_SS_LIMIT, guest_ss_limit,
1968c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
1978c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_DS_LIMIT, guest_ds_limit,
1988c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
1998c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_FS_LIMIT, guest_fs_limit,
2008c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2018c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_GS_LIMIT, guest_gs_limit,
2028c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2038c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit,
2048c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2058c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_TR_LIMIT, guest_tr_limit,
2068c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2078c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit,
2088c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2098c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit,
2108c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2118c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes,
2128c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2138c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes,
2148c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2158c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes,
2168c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2178c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes,
2188c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2198c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes,
2208c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2218c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes,
2228c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2238c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes,
2248c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2258c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes,
2268c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2278c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_ACTIVITY_STATE, guest_activity_state,
2288c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
2298c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs,
2308c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1),
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/* 32 bit read only */
2338c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error,
2348c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
2358c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_EXIT_REASON, vm_exit_reason,
2368c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
2378c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info,
2388c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
2398c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code,
2408c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
2418c2ecf20Sopenharmony_ci	EVMCS1_FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field,
2428c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
2438c2ecf20Sopenharmony_ci	EVMCS1_FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code,
2448c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
2458c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len,
2468c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
2478c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info,
2488c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE),
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* No mask defined in the spec (not used) */
2518c2ecf20Sopenharmony_ci	EVMCS1_FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask,
2528c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
2538c2ecf20Sopenharmony_ci	EVMCS1_FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match,
2548c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
2558c2ecf20Sopenharmony_ci	EVMCS1_FIELD(CR3_TARGET_COUNT, cr3_target_count,
2568c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
2578c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count,
2588c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
2598c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count,
2608c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
2618c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count,
2628c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL),
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	/* 16 bit rw */
2658c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_ES_SELECTOR, host_es_selector,
2668c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
2678c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_CS_SELECTOR, host_cs_selector,
2688c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
2698c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_SS_SELECTOR, host_ss_selector,
2708c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
2718c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_DS_SELECTOR, host_ds_selector,
2728c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
2738c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_FS_SELECTOR, host_fs_selector,
2748c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
2758c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_GS_SELECTOR, host_gs_selector,
2768c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
2778c2ecf20Sopenharmony_ci	EVMCS1_FIELD(HOST_TR_SELECTOR, host_tr_selector,
2788c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1),
2798c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_ES_SELECTOR, guest_es_selector,
2808c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2818c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_CS_SELECTOR, guest_cs_selector,
2828c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2838c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_SS_SELECTOR, guest_ss_selector,
2848c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2858c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_DS_SELECTOR, guest_ds_selector,
2868c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2878c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_FS_SELECTOR, guest_fs_selector,
2888c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2898c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_GS_SELECTOR, guest_gs_selector,
2908c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2918c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector,
2928c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2938c2ecf20Sopenharmony_ci	EVMCS1_FIELD(GUEST_TR_SELECTOR, guest_tr_selector,
2948c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2),
2958c2ecf20Sopenharmony_ci	EVMCS1_FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id,
2968c2ecf20Sopenharmony_ci		     HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT),
2978c2ecf20Sopenharmony_ci};
2988c2ecf20Sopenharmony_ciconst unsigned int nr_evmcs_1_fields = ARRAY_SIZE(vmcs_field_to_evmcs_1);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci__init void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL;
3038c2ecf20Sopenharmony_ci	vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
3068c2ecf20Sopenharmony_ci	vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci#endif
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cibool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	struct hv_vp_assist_page assist_page;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	*evmcs_gpa = -1ull;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page)))
3178c2ecf20Sopenharmony_ci		return false;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	if (unlikely(!assist_page.enlighten_vmentry))
3208c2ecf20Sopenharmony_ci		return false;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	*evmcs_gpa = assist_page.current_nested_vmcs;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	return true;
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ciuint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	struct vcpu_vmx *vmx = to_vmx(vcpu);
3308c2ecf20Sopenharmony_ci	/*
3318c2ecf20Sopenharmony_ci	 * vmcs_version represents the range of supported Enlightened VMCS
3328c2ecf20Sopenharmony_ci	 * versions: lower 8 bits is the minimal version, higher 8 bits is the
3338c2ecf20Sopenharmony_ci	 * maximum supported version. KVM supports versions from 1 to
3348c2ecf20Sopenharmony_ci	 * KVM_EVMCS_VERSION.
3358c2ecf20Sopenharmony_ci	 */
3368c2ecf20Sopenharmony_ci	if (kvm_cpu_cap_get(X86_FEATURE_VMX) &&
3378c2ecf20Sopenharmony_ci	    vmx->nested.enlightened_vmcs_enabled)
3388c2ecf20Sopenharmony_ci		return (KVM_EVMCS_VERSION << 8) | 1;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	return 0;
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_civoid nested_evmcs_filter_control_msr(u32 msr_index, u64 *pdata)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	u32 ctl_low = (u32)*pdata;
3468c2ecf20Sopenharmony_ci	u32 ctl_high = (u32)(*pdata >> 32);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	/*
3498c2ecf20Sopenharmony_ci	 * Hyper-V 2016 and 2019 try using these features even when eVMCS
3508c2ecf20Sopenharmony_ci	 * is enabled but there are no corresponding fields.
3518c2ecf20Sopenharmony_ci	 */
3528c2ecf20Sopenharmony_ci	switch (msr_index) {
3538c2ecf20Sopenharmony_ci	case MSR_IA32_VMX_EXIT_CTLS:
3548c2ecf20Sopenharmony_ci	case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3558c2ecf20Sopenharmony_ci		ctl_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
3568c2ecf20Sopenharmony_ci		break;
3578c2ecf20Sopenharmony_ci	case MSR_IA32_VMX_ENTRY_CTLS:
3588c2ecf20Sopenharmony_ci	case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3598c2ecf20Sopenharmony_ci		ctl_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
3608c2ecf20Sopenharmony_ci		break;
3618c2ecf20Sopenharmony_ci	case MSR_IA32_VMX_PROCBASED_CTLS2:
3628c2ecf20Sopenharmony_ci		ctl_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
3638c2ecf20Sopenharmony_ci		break;
3648c2ecf20Sopenharmony_ci	case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3658c2ecf20Sopenharmony_ci	case MSR_IA32_VMX_PINBASED_CTLS:
3668c2ecf20Sopenharmony_ci		ctl_high &= ~EVMCS1_UNSUPPORTED_PINCTRL;
3678c2ecf20Sopenharmony_ci		break;
3688c2ecf20Sopenharmony_ci	case MSR_IA32_VMX_VMFUNC:
3698c2ecf20Sopenharmony_ci		ctl_low &= ~EVMCS1_UNSUPPORTED_VMFUNC;
3708c2ecf20Sopenharmony_ci		break;
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	*pdata = ctl_low | ((u64)ctl_high << 32);
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ciint nested_evmcs_check_controls(struct vmcs12 *vmcs12)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	int ret = 0;
3798c2ecf20Sopenharmony_ci	u32 unsupp_ctl;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	unsupp_ctl = vmcs12->pin_based_vm_exec_control &
3828c2ecf20Sopenharmony_ci		EVMCS1_UNSUPPORTED_PINCTRL;
3838c2ecf20Sopenharmony_ci	if (unsupp_ctl) {
3848c2ecf20Sopenharmony_ci		trace_kvm_nested_vmenter_failed(
3858c2ecf20Sopenharmony_ci			"eVMCS: unsupported pin-based VM-execution controls",
3868c2ecf20Sopenharmony_ci			unsupp_ctl);
3878c2ecf20Sopenharmony_ci		ret = -EINVAL;
3888c2ecf20Sopenharmony_ci	}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	unsupp_ctl = vmcs12->secondary_vm_exec_control &
3918c2ecf20Sopenharmony_ci		EVMCS1_UNSUPPORTED_2NDEXEC;
3928c2ecf20Sopenharmony_ci	if (unsupp_ctl) {
3938c2ecf20Sopenharmony_ci		trace_kvm_nested_vmenter_failed(
3948c2ecf20Sopenharmony_ci			"eVMCS: unsupported secondary VM-execution controls",
3958c2ecf20Sopenharmony_ci			unsupp_ctl);
3968c2ecf20Sopenharmony_ci		ret = -EINVAL;
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	unsupp_ctl = vmcs12->vm_exit_controls &
4008c2ecf20Sopenharmony_ci		EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
4018c2ecf20Sopenharmony_ci	if (unsupp_ctl) {
4028c2ecf20Sopenharmony_ci		trace_kvm_nested_vmenter_failed(
4038c2ecf20Sopenharmony_ci			"eVMCS: unsupported VM-exit controls",
4048c2ecf20Sopenharmony_ci			unsupp_ctl);
4058c2ecf20Sopenharmony_ci		ret = -EINVAL;
4068c2ecf20Sopenharmony_ci	}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	unsupp_ctl = vmcs12->vm_entry_controls &
4098c2ecf20Sopenharmony_ci		EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
4108c2ecf20Sopenharmony_ci	if (unsupp_ctl) {
4118c2ecf20Sopenharmony_ci		trace_kvm_nested_vmenter_failed(
4128c2ecf20Sopenharmony_ci			"eVMCS: unsupported VM-entry controls",
4138c2ecf20Sopenharmony_ci			unsupp_ctl);
4148c2ecf20Sopenharmony_ci		ret = -EINVAL;
4158c2ecf20Sopenharmony_ci	}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	unsupp_ctl = vmcs12->vm_function_control & EVMCS1_UNSUPPORTED_VMFUNC;
4188c2ecf20Sopenharmony_ci	if (unsupp_ctl) {
4198c2ecf20Sopenharmony_ci		trace_kvm_nested_vmenter_failed(
4208c2ecf20Sopenharmony_ci			"eVMCS: unsupported VM-function controls",
4218c2ecf20Sopenharmony_ci			unsupp_ctl);
4228c2ecf20Sopenharmony_ci		ret = -EINVAL;
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	return ret;
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ciint nested_enable_evmcs(struct kvm_vcpu *vcpu,
4298c2ecf20Sopenharmony_ci			uint16_t *vmcs_version)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	struct vcpu_vmx *vmx = to_vmx(vcpu);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	vmx->nested.enlightened_vmcs_enabled = true;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	if (vmcs_version)
4368c2ecf20Sopenharmony_ci		*vmcs_version = nested_get_evmcs_version(vcpu);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	return 0;
4398c2ecf20Sopenharmony_ci}
440