xref: /openbmc/linux/arch/x86/kvm/vmx/vmx.h (revision 0db00e5d86dc793aab9722ad3728d99166eb7d96)
18373d25dSSean Christopherson /* SPDX-License-Identifier: GPL-2.0 */
28373d25dSSean Christopherson #ifndef __KVM_X86_VMX_H
38373d25dSSean Christopherson #define __KVM_X86_VMX_H
48373d25dSSean Christopherson 
58373d25dSSean Christopherson #include <linux/kvm_host.h>
68373d25dSSean Christopherson 
78373d25dSSean Christopherson #include <asm/kvm.h>
8f99e3dafSChao Peng #include <asm/intel_pt.h>
97de8e5b6SSean Christopherson #include <asm/perf_event.h>
10*65b2514eSJacob Pan #include <asm/posted_intr.h>
118373d25dSSean Christopherson 
128373d25dSSean Christopherson #include "capabilities.h"
1307853adcSJosh Poimboeuf #include "../kvm_cache_regs.h"
148373d25dSSean Christopherson #include "vmcs.h"
155a085326SSean Christopherson #include "vmx_ops.h"
1607853adcSJosh Poimboeuf #include "../cpuid.h"
17bb066506SJosh Poimboeuf #include "run_flags.h"
188373d25dSSean Christopherson 
198373d25dSSean Christopherson #define MSR_TYPE_R	1
208373d25dSSean Christopherson #define MSR_TYPE_W	2
218373d25dSSean Christopherson #define MSR_TYPE_RW	3
228373d25dSSean Christopherson 
238373d25dSSean Christopherson #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
248373d25dSSean Christopherson 
257d73710dSJim Mattson #ifdef CONFIG_X86_64
26eb3db1b1SSean Christopherson #define MAX_NR_USER_RETURN_MSRS	7
277d73710dSJim Mattson #else
28eb3db1b1SSean Christopherson #define MAX_NR_USER_RETURN_MSRS	4
297d73710dSJim Mattson #endif
307d73710dSJim Mattson 
31ce833b23SSean Christopherson #define MAX_NR_LOADSTORE_MSRS	8
328373d25dSSean Christopherson 
338373d25dSSean Christopherson struct vmx_msrs {
348373d25dSSean Christopherson 	unsigned int		nr;
35ce833b23SSean Christopherson 	struct vmx_msr_entry	val[MAX_NR_LOADSTORE_MSRS];
368373d25dSSean Christopherson };
378373d25dSSean Christopherson 
38eb3db1b1SSean Christopherson struct vmx_uret_msr {
39ee9d22e0SSean Christopherson 	bool load_into_hardware;
408373d25dSSean Christopherson 	u64 data;
418373d25dSSean Christopherson 	u64 mask;
428373d25dSSean Christopherson };
438373d25dSSean Christopherson 
448373d25dSSean Christopherson enum segment_cache_field {
458373d25dSSean Christopherson 	SEG_FIELD_SEL = 0,
468373d25dSSean Christopherson 	SEG_FIELD_BASE = 1,
478373d25dSSean Christopherson 	SEG_FIELD_LIMIT = 2,
488373d25dSSean Christopherson 	SEG_FIELD_AR = 3,
498373d25dSSean Christopherson 
508373d25dSSean Christopherson 	SEG_FIELD_NR = 4
518373d25dSSean Christopherson };
528373d25dSSean Christopherson 
532ef444f1SChao Peng #define RTIT_ADDR_RANGE		4
542ef444f1SChao Peng 
552ef444f1SChao Peng struct pt_ctx {
562ef444f1SChao Peng 	u64 ctl;
572ef444f1SChao Peng 	u64 status;
582ef444f1SChao Peng 	u64 output_base;
592ef444f1SChao Peng 	u64 output_mask;
602ef444f1SChao Peng 	u64 cr3_match;
612ef444f1SChao Peng 	u64 addr_a[RTIT_ADDR_RANGE];
622ef444f1SChao Peng 	u64 addr_b[RTIT_ADDR_RANGE];
632ef444f1SChao Peng };
642ef444f1SChao Peng 
652ef444f1SChao Peng struct pt_desc {
662ef444f1SChao Peng 	u64 ctl_bitmask;
67f4d3a902SXiaoyao Li 	u32 num_address_ranges;
682ef444f1SChao Peng 	u32 caps[PT_CPUID_REGS_NUM * PT_CPUID_LEAVES];
692ef444f1SChao Peng 	struct pt_ctx host;
702ef444f1SChao Peng 	struct pt_ctx guest;
712ef444f1SChao Peng };
728373d25dSSean Christopherson 
738e533240SSean Christopherson union vmx_exit_reason {
748e533240SSean Christopherson 	struct {
758e533240SSean Christopherson 		u32	basic			: 16;
768e533240SSean Christopherson 		u32	reserved16		: 1;
778e533240SSean Christopherson 		u32	reserved17		: 1;
788e533240SSean Christopherson 		u32	reserved18		: 1;
798e533240SSean Christopherson 		u32	reserved19		: 1;
808e533240SSean Christopherson 		u32	reserved20		: 1;
818e533240SSean Christopherson 		u32	reserved21		: 1;
828e533240SSean Christopherson 		u32	reserved22		: 1;
838e533240SSean Christopherson 		u32	reserved23		: 1;
848e533240SSean Christopherson 		u32	reserved24		: 1;
858e533240SSean Christopherson 		u32	reserved25		: 1;
86fe6b6bc8SChenyi Qiang 		u32	bus_lock_detected	: 1;
878e533240SSean Christopherson 		u32	enclave_mode		: 1;
888e533240SSean Christopherson 		u32	smi_pending_mtf		: 1;
898e533240SSean Christopherson 		u32	smi_from_vmx_root	: 1;
908e533240SSean Christopherson 		u32	reserved30		: 1;
918e533240SSean Christopherson 		u32	failed_vmentry		: 1;
928e533240SSean Christopherson 	};
938e533240SSean Christopherson 	u32 full;
948e533240SSean Christopherson };
958e533240SSean Christopherson 
969c9520ceSPaolo Bonzini struct lbr_desc {
979c9520ceSPaolo Bonzini 	/* Basic info about guest LBR records. */
989c9520ceSPaolo Bonzini 	struct x86_pmu_lbr records;
998e12911bSLike Xu 
1008e12911bSLike Xu 	/*
1018e12911bSLike Xu 	 * Emulate LBR feature via passthrough LBR registers when the
1028e12911bSLike Xu 	 * per-vcpu guest LBR event is scheduled on the current pcpu.
1038e12911bSLike Xu 	 *
1048e12911bSLike Xu 	 * The records may be inaccurate if the host reclaims the LBR.
1058e12911bSLike Xu 	 */
1068e12911bSLike Xu 	struct perf_event *event;
1079254beaaSLike Xu 
1089254beaaSLike Xu 	/* True if LBRs are marked as not intercepted in the MSR bitmap */
1099254beaaSLike Xu 	bool msr_passthrough;
1109c9520ceSPaolo Bonzini };
1119c9520ceSPaolo Bonzini 
1128373d25dSSean Christopherson /*
1138373d25dSSean Christopherson  * The nested_vmx structure is part of vcpu_vmx, and holds information we need
1148373d25dSSean Christopherson  * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
1158373d25dSSean Christopherson  */
1168373d25dSSean Christopherson struct nested_vmx {
1178373d25dSSean Christopherson 	/* Has the level1 guest done vmxon? */
1188373d25dSSean Christopherson 	bool vmxon;
1198373d25dSSean Christopherson 	gpa_t vmxon_ptr;
1208373d25dSSean Christopherson 	bool pml_full;
1218373d25dSSean Christopherson 
1228373d25dSSean Christopherson 	/* The guest-physical address of the current VMCS L1 keeps for L2 */
1238373d25dSSean Christopherson 	gpa_t current_vmptr;
1248373d25dSSean Christopherson 	/*
1258373d25dSSean Christopherson 	 * Cache of the guest's VMCS, existing outside of guest memory.
1268373d25dSSean Christopherson 	 * Loaded from guest memory during VMPTRLD. Flushed to guest
1278373d25dSSean Christopherson 	 * memory during VMCLEAR and VMPTRLD.
1288373d25dSSean Christopherson 	 */
1298373d25dSSean Christopherson 	struct vmcs12 *cached_vmcs12;
1308373d25dSSean Christopherson 	/*
1318373d25dSSean Christopherson 	 * Cache of the guest's shadow VMCS, existing outside of guest
1328373d25dSSean Christopherson 	 * memory. Loaded from guest memory during VM entry. Flushed
1338373d25dSSean Christopherson 	 * to guest memory during VM exit.
1348373d25dSSean Christopherson 	 */
1358373d25dSSean Christopherson 	struct vmcs12 *cached_shadow_vmcs12;
1367952d769SSean Christopherson 
1378373d25dSSean Christopherson 	/*
138297d597aSDavid Woodhouse 	 * GPA to HVA cache for accessing vmcs12->vmcs_link_pointer
139297d597aSDavid Woodhouse 	 */
140297d597aSDavid Woodhouse 	struct gfn_to_hva_cache shadow_vmcs12_cache;
141297d597aSDavid Woodhouse 
142297d597aSDavid Woodhouse 	/*
143cee66664SDavid Woodhouse 	 * GPA to HVA cache for VMCS12
144cee66664SDavid Woodhouse 	 */
145cee66664SDavid Woodhouse 	struct gfn_to_hva_cache vmcs12_cache;
146cee66664SDavid Woodhouse 
147cee66664SDavid Woodhouse 	/*
1488373d25dSSean Christopherson 	 * Indicates if the shadow vmcs or enlightened vmcs must be updated
1498373d25dSSean Christopherson 	 * with the data held by struct vmcs12.
1508373d25dSSean Christopherson 	 */
1513731905eSSean Christopherson 	bool need_vmcs12_to_shadow_sync;
1528373d25dSSean Christopherson 	bool dirty_vmcs12;
1538373d25dSSean Christopherson 
1548373d25dSSean Christopherson 	/*
155ed2a4800SVitaly Kuznetsov 	 * Indicates whether MSR bitmap for L2 needs to be rebuilt due to
156ed2a4800SVitaly Kuznetsov 	 * changes in MSR bitmap for L1 or switching to a different L2. Note,
157ed2a4800SVitaly Kuznetsov 	 * this flag can only be used reliably in conjunction with a paravirt L1
158ed2a4800SVitaly Kuznetsov 	 * which informs L0 whether any changes to MSR bitmap for L2 were done
159ed2a4800SVitaly Kuznetsov 	 * on its side.
160ed2a4800SVitaly Kuznetsov 	 */
161ed2a4800SVitaly Kuznetsov 	bool force_msr_bitmap_recalc;
162ed2a4800SVitaly Kuznetsov 
163ed2a4800SVitaly Kuznetsov 	/*
1647952d769SSean Christopherson 	 * Indicates lazily loaded guest state has not yet been decached from
1657952d769SSean Christopherson 	 * vmcs02.
1667952d769SSean Christopherson 	 */
1677952d769SSean Christopherson 	bool need_sync_vmcs02_to_vmcs12_rare;
1687952d769SSean Christopherson 
1697952d769SSean Christopherson 	/*
1708373d25dSSean Christopherson 	 * vmcs02 has been initialized, i.e. state that is constant for
1718373d25dSSean Christopherson 	 * vmcs02 has been written to the backing VMCS.  Initialization
1728373d25dSSean Christopherson 	 * is delayed until L1 actually attempts to run a nested VM.
1738373d25dSSean Christopherson 	 */
1748373d25dSSean Christopherson 	bool vmcs02_initialized;
1758373d25dSSean Christopherson 
1768373d25dSSean Christopherson 	bool change_vmcs01_virtual_apic_mode;
1771196cb97SSean Christopherson 	bool reload_vmcs01_apic_access_page;
178a85863c2SMakarand Sonare 	bool update_vmcs01_cpu_dirty_logging;
1797c69661eSSean Christopherson 	bool update_vmcs01_apicv_status;
1808373d25dSSean Christopherson 
1818373d25dSSean Christopherson 	/*
1828373d25dSSean Christopherson 	 * Enlightened VMCS has been enabled. It does not mean that L1 has to
1838373d25dSSean Christopherson 	 * use it. However, VMX features available to L1 will be limited based
1848373d25dSSean Christopherson 	 * on what the enlightened VMCS supports.
1858373d25dSSean Christopherson 	 */
1868373d25dSSean Christopherson 	bool enlightened_vmcs_enabled;
1878373d25dSSean Christopherson 
1888373d25dSSean Christopherson 	/* L2 must run next, and mustn't decide to exit to L1. */
1898373d25dSSean Christopherson 	bool nested_run_pending;
1908373d25dSSean Christopherson 
1915ef8acbdSOliver Upton 	/* Pending MTF VM-exit into L1.  */
1925ef8acbdSOliver Upton 	bool mtf_pending;
1935ef8acbdSOliver Upton 
1948373d25dSSean Christopherson 	struct loaded_vmcs vmcs02;
1958373d25dSSean Christopherson 
1968373d25dSSean Christopherson 	/*
1978373d25dSSean Christopherson 	 * Guest pages referred to in the vmcs02 with host-physical
1988373d25dSSean Christopherson 	 * pointers, so we must keep them pinned while L2 runs.
1998373d25dSSean Christopherson 	 */
200fe1911aaSSean Christopherson 	struct kvm_host_map apic_access_page_map;
20196c66e87SKarimAllah Ahmed 	struct kvm_host_map virtual_apic_map;
2023278e049SKarimAllah Ahmed 	struct kvm_host_map pi_desc_map;
20331f0b6c4SKarimAllah Ahmed 
20431f0b6c4SKarimAllah Ahmed 	struct kvm_host_map msr_bitmap_map;
20531f0b6c4SKarimAllah Ahmed 
2068373d25dSSean Christopherson 	struct pi_desc *pi_desc;
2078373d25dSSean Christopherson 	bool pi_pending;
2088373d25dSSean Christopherson 	u16 posted_intr_nv;
2098373d25dSSean Christopherson 
2108373d25dSSean Christopherson 	struct hrtimer preemption_timer;
211850448f3SPeter Shier 	u64 preemption_timer_deadline;
212850448f3SPeter Shier 	bool has_preemption_timer_deadline;
2138373d25dSSean Christopherson 	bool preemption_timer_expired;
2148373d25dSSean Christopherson 
2155d76b1f8SSean Christopherson 	/*
2165d76b1f8SSean Christopherson 	 * Used to snapshot MSRs that are conditionally loaded on VM-Enter in
2175d76b1f8SSean Christopherson 	 * order to propagate the guest's pre-VM-Enter value into vmcs02.  For
2185d76b1f8SSean Christopherson 	 * emulation of VMLAUNCH/VMRESUME, the snapshot will be of L1's value.
2195d76b1f8SSean Christopherson 	 * For KVM_SET_NESTED_STATE, the snapshot is of L2's value, _if_
2205d76b1f8SSean Christopherson 	 * userspace restores MSRs before nested state.  If userspace restores
2215d76b1f8SSean Christopherson 	 * MSRs after nested state, the snapshot holds garbage, but KVM can't
2225d76b1f8SSean Christopherson 	 * detect that, and the garbage value in vmcs02 will be overwritten by
2235d76b1f8SSean Christopherson 	 * MSR restoration in any case.
2245d76b1f8SSean Christopherson 	 */
2255d76b1f8SSean Christopherson 	u64 pre_vmenter_debugctl;
2265d76b1f8SSean Christopherson 	u64 pre_vmenter_bndcfgs;
2278373d25dSSean Christopherson 
22802d496cfSLiran Alon 	/* to migrate it to L1 if L2 writes to L1's CR8 directly */
22902d496cfSLiran Alon 	int l1_tpr_threshold;
23002d496cfSLiran Alon 
2318373d25dSSean Christopherson 	u16 vpid02;
2328373d25dSSean Christopherson 	u16 last_vpid;
2338373d25dSSean Christopherson 
2348373d25dSSean Christopherson 	struct nested_vmx_msrs msrs;
2358373d25dSSean Christopherson 
2368373d25dSSean Christopherson 	/* SMM related state */
2378373d25dSSean Christopherson 	struct {
2388373d25dSSean Christopherson 		/* in VMX operation on SMM entry? */
2398373d25dSSean Christopherson 		bool vmxon;
2408373d25dSSean Christopherson 		/* in guest mode on SMM entry? */
2418373d25dSSean Christopherson 		bool guest_mode;
2428373d25dSSean Christopherson 	} smm;
2438373d25dSSean Christopherson 
2448373d25dSSean Christopherson 	gpa_t hv_evmcs_vmptr;
245dee9c049SKarimAllah Ahmed 	struct kvm_host_map hv_evmcs_map;
2468373d25dSSean Christopherson 	struct hv_enlightened_vmcs *hv_evmcs;
2478373d25dSSean Christopherson };
2488373d25dSSean Christopherson 
2498373d25dSSean Christopherson struct vcpu_vmx {
2508373d25dSSean Christopherson 	struct kvm_vcpu       vcpu;
2518373d25dSSean Christopherson 	u8                    fail;
25284ec8d2dSSean Christopherson 	u8		      x2apic_msr_bitmap_mode;
253b464f57eSPaolo Bonzini 
254b464f57eSPaolo Bonzini 	/*
255b464f57eSPaolo Bonzini 	 * If true, host state has been stored in vmx->loaded_vmcs for
256b464f57eSPaolo Bonzini 	 * the CPU registers that only need to be switched when transitioning
257b464f57eSPaolo Bonzini 	 * to/from the kernel, and the registers have been loaded with guest
258b464f57eSPaolo Bonzini 	 * values.  If false, host state is loaded in the CPU registers
259b464f57eSPaolo Bonzini 	 * and vmx->loaded_vmcs->host_state is invalid.
260b464f57eSPaolo Bonzini 	 */
261b464f57eSPaolo Bonzini 	bool		      guest_state_loaded;
262b464f57eSPaolo Bonzini 
2635addc235SSean Christopherson 	unsigned long         exit_qualification;
2648373d25dSSean Christopherson 	u32                   exit_intr_info;
2658373d25dSSean Christopherson 	u32                   idt_vectoring_info;
2668373d25dSSean Christopherson 	ulong                 rflags;
26770f932ecSSean Christopherson 
268b6194b94SSean Christopherson 	/*
269b6194b94SSean Christopherson 	 * User return MSRs are always emulated when enabled in the guest, but
270b6194b94SSean Christopherson 	 * only loaded into hardware when necessary, e.g. SYSCALL #UDs outside
271b6194b94SSean Christopherson 	 * of 64-bit mode or if EFER.SCE=1, thus the SYSCALL MSRs don't need to
272b6194b94SSean Christopherson 	 * be loaded into hardware if those conditions aren't met.
273b6194b94SSean Christopherson 	 */
274eb3db1b1SSean Christopherson 	struct vmx_uret_msr   guest_uret_msrs[MAX_NR_USER_RETURN_MSRS];
275658ece84SSean Christopherson 	bool                  guest_uret_msrs_loaded;
2768373d25dSSean Christopherson #ifdef CONFIG_X86_64
2778373d25dSSean Christopherson 	u64		      msr_host_kernel_gs_base;
2788373d25dSSean Christopherson 	u64		      msr_guest_kernel_gs_base;
2798373d25dSSean Christopherson #endif
2808373d25dSSean Christopherson 
2818373d25dSSean Christopherson 	u64		      spec_ctrl;
2826e3ba4abSTao Xu 	u32		      msr_ia32_umwait_control;
2838373d25dSSean Christopherson 
2848373d25dSSean Christopherson 	/*
2858373d25dSSean Christopherson 	 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
2868373d25dSSean Christopherson 	 * non-nested (L1) guest, it always points to vmcs01. For a nested
287b464f57eSPaolo Bonzini 	 * guest (L2), it points to a different VMCS.
2888373d25dSSean Christopherson 	 */
2898373d25dSSean Christopherson 	struct loaded_vmcs    vmcs01;
2908373d25dSSean Christopherson 	struct loaded_vmcs   *loaded_vmcs;
291c9afc58cSSean Christopherson 
2928373d25dSSean Christopherson 	struct msr_autoload {
2938373d25dSSean Christopherson 		struct vmx_msrs guest;
2948373d25dSSean Christopherson 		struct vmx_msrs host;
2958373d25dSSean Christopherson 	} msr_autoload;
2968373d25dSSean Christopherson 
297662f1d1dSAaron Lewis 	struct msr_autostore {
298662f1d1dSAaron Lewis 		struct vmx_msrs guest;
299662f1d1dSAaron Lewis 	} msr_autostore;
300662f1d1dSAaron Lewis 
3018373d25dSSean Christopherson 	struct {
3028373d25dSSean Christopherson 		int vm86_active;
3038373d25dSSean Christopherson 		ulong save_rflags;
3048373d25dSSean Christopherson 		struct kvm_segment segs[8];
3058373d25dSSean Christopherson 	} rmode;
3068373d25dSSean Christopherson 	struct {
3078373d25dSSean Christopherson 		u32 bitmask; /* 4 bits per segment (1 bit per field) */
3088373d25dSSean Christopherson 		struct kvm_save_segment {
3098373d25dSSean Christopherson 			u16 selector;
3108373d25dSSean Christopherson 			unsigned long base;
3118373d25dSSean Christopherson 			u32 limit;
3128373d25dSSean Christopherson 			u32 ar;
3138373d25dSSean Christopherson 		} seg[8];
3148373d25dSSean Christopherson 	} segment_cache;
3158373d25dSSean Christopherson 	int vpid;
3168373d25dSSean Christopherson 	bool emulation_required;
3178373d25dSSean Christopherson 
3188e533240SSean Christopherson 	union vmx_exit_reason exit_reason;
3198373d25dSSean Christopherson 
3208373d25dSSean Christopherson 	/* Posted interrupt descriptor */
3218373d25dSSean Christopherson 	struct pi_desc pi_desc;
3228373d25dSSean Christopherson 
32312a8eee5SSean Christopherson 	/* Used if this vCPU is waiting for PI notification wakeup. */
32412a8eee5SSean Christopherson 	struct list_head pi_wakeup_list;
32512a8eee5SSean Christopherson 
3268373d25dSSean Christopherson 	/* Support for a guest hypervisor (nested VMX) */
3278373d25dSSean Christopherson 	struct nested_vmx nested;
3288373d25dSSean Christopherson 
3298373d25dSSean Christopherson 	/* Dynamic PLE window. */
330c5c5d6faSPeter Xu 	unsigned int ple_window;
3318373d25dSSean Christopherson 	bool ple_window_dirty;
3328373d25dSSean Christopherson 
3338373d25dSSean Christopherson 	bool req_immediate_exit;
3348373d25dSSean Christopherson 
3358373d25dSSean Christopherson 	/* Support for PML */
3368373d25dSSean Christopherson #define PML_ENTITY_NUM		512
3378373d25dSSean Christopherson 	struct page *pml_pg;
3388373d25dSSean Christopherson 
3398373d25dSSean Christopherson 	/* apic deadline value in host tsc */
3408373d25dSSean Christopherson 	u64 hv_deadline_tsc;
3418373d25dSSean Christopherson 
3428373d25dSSean Christopherson 	unsigned long host_debugctlmsr;
3438373d25dSSean Christopherson 
3448373d25dSSean Christopherson 	/*
3458373d25dSSean Christopherson 	 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
34632ad73dbSSean Christopherson 	 * msr_ia32_feature_control. FEAT_CTL_LOCKED is always included
3478373d25dSSean Christopherson 	 * in msr_ia32_feature_control_valid_bits.
3488373d25dSSean Christopherson 	 */
3498373d25dSSean Christopherson 	u64 msr_ia32_feature_control;
3508373d25dSSean Christopherson 	u64 msr_ia32_feature_control_valid_bits;
3518f102445SSean Christopherson 	/* SGX Launch Control public key hash */
3528f102445SSean Christopherson 	u64 msr_ia32_sgxlepubkeyhash[4];
353027bbb88SPawan Gupta 	u64 msr_ia32_mcu_opt_ctrl;
354027bbb88SPawan Gupta 	bool disable_fb_clear;
3558f102445SSean Christopherson 
3562ef444f1SChao Peng 	struct pt_desc pt_desc;
3579c9520ceSPaolo Bonzini 	struct lbr_desc lbr_desc;
3583eb90017SAlexander Graf 
3593eb90017SAlexander Graf 	/* Save desired MSR intercept (read: pass-through) state */
360da3db168SSean Christopherson #define MAX_POSSIBLE_PASSTHROUGH_MSRS	16
3613eb90017SAlexander Graf 	struct {
3623eb90017SAlexander Graf 		DECLARE_BITMAP(read, MAX_POSSIBLE_PASSTHROUGH_MSRS);
3633eb90017SAlexander Graf 		DECLARE_BITMAP(write, MAX_POSSIBLE_PASSTHROUGH_MSRS);
3643eb90017SAlexander Graf 	} shadow_msr_intercept;
3658373d25dSSean Christopherson };
3668373d25dSSean Christopherson 
3678373d25dSSean Christopherson struct kvm_vmx {
3688373d25dSSean Christopherson 	struct kvm kvm;
3698373d25dSSean Christopherson 
3708373d25dSSean Christopherson 	unsigned int tss_addr;
3718373d25dSSean Christopherson 	bool ept_identity_pagetable_done;
3728373d25dSSean Christopherson 	gpa_t ept_identity_map_addr;
373d588bb9bSChao Gao 	/* Posted Interrupt Descriptor (PID) table for IPI virtualization */
374d588bb9bSChao Gao 	u64 *pid_table;
3758373d25dSSean Christopherson };
3768373d25dSSean Christopherson 
3775c911befSSean Christopherson void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
3785c911befSSean Christopherson 			struct loaded_vmcs *buddy);
37997b7ead3SSean Christopherson int allocate_vpid(void);
38097b7ead3SSean Christopherson void free_vpid(int vpid);
38197b7ead3SSean Christopherson void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
38297b7ead3SSean Christopherson void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
383bca06b85SSean Christopherson void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
38413b964a2SSean Christopherson 			unsigned long fs_base, unsigned long gs_base);
38597b7ead3SSean Christopherson int vmx_get_cpl(struct kvm_vcpu *vcpu);
386dbab610aSMaxim Levitsky bool vmx_emulation_required(struct kvm_vcpu *vcpu);
38797b7ead3SSean Christopherson unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);
38897b7ead3SSean Christopherson void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
38997b7ead3SSean Christopherson u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu);
39097b7ead3SSean Christopherson void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask);
39172f211ecSMaxim Levitsky int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer);
39297b7ead3SSean Christopherson void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
393c2fe3cd4SSean Christopherson void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
39497b7ead3SSean Christopherson void set_cr4_guest_host_mask(struct vcpu_vmx *vmx);
39597b7ead3SSean Christopherson void ept_save_pdptrs(struct kvm_vcpu *vcpu);
39697b7ead3SSean Christopherson void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
397816be9e9SSean Christopherson void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
398e83bc09cSSean Christopherson u64 construct_eptp(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level);
3992ba4493aSSean Christopherson 
400b33bb78aSSean Christopherson bool vmx_guest_inject_ac(struct kvm_vcpu *vcpu);
401b6a7cc35SJason Baron void vmx_update_exception_bitmap(struct kvm_vcpu *vcpu);
4021b660b6bSSean Christopherson bool vmx_nmi_blocked(struct kvm_vcpu *vcpu);
403e06f46fdSSean Christopherson bool __vmx_interrupt_blocked(struct kvm_vcpu *vcpu);
4041b660b6bSSean Christopherson bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu);
40597b7ead3SSean Christopherson bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
40697b7ead3SSean Christopherson void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
40797b7ead3SSean Christopherson void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu);
408d85a8034SSean Christopherson struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr);
409476c9bd8SAaron Lewis void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu);
4104d259965SYi Wang void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp);
411fc02735bSJosh Poimboeuf void vmx_spec_ctrl_restore_host(struct vcpu_vmx *vmx, unsigned int flags);
412bb066506SJosh Poimboeuf unsigned int __vmx_vcpu_run_flags(struct vcpu_vmx *vmx);
413bb066506SJosh Poimboeuf bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs,
414bb066506SJosh Poimboeuf 		    unsigned int flags);
415a128a934SSean Christopherson int vmx_find_loadstore_msr_slot(struct vmx_msrs *m, u32 msr);
41643fea4e4SPeter Shier void vmx_ept_load_pdptrs(struct kvm_vcpu *vcpu);
417e23f6d49SSean Christopherson 
418e23f6d49SSean Christopherson void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
419e23f6d49SSean Christopherson void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
420e23f6d49SSean Christopherson 
421307a94c7SIlias Stamatis u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
422307a94c7SIlias Stamatis u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
423307a94c7SIlias Stamatis 
vmx_set_intercept_for_msr(struct kvm_vcpu * vcpu,u32 msr,int type,bool value)424e23f6d49SSean Christopherson static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
425e23f6d49SSean Christopherson 					     int type, bool value)
426e23f6d49SSean Christopherson {
427e23f6d49SSean Christopherson 	if (value)
428e23f6d49SSean Christopherson 		vmx_enable_intercept_for_msr(vcpu, msr, type);
429e23f6d49SSean Christopherson 	else
430e23f6d49SSean Christopherson 		vmx_disable_intercept_for_msr(vcpu, msr, type);
431e23f6d49SSean Christopherson }
432e23f6d49SSean Christopherson 
433a85863c2SMakarand Sonare void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu);
43497b7ead3SSean Christopherson 
4350cacb80bSSean Christopherson /*
4360cacb80bSSean Christopherson  * Note, early Intel manuals have the write-low and read-high bitmap offsets
4370cacb80bSSean Christopherson  * the wrong way round.  The bitmaps control MSRs 0x00000000-0x00001fff and
4380cacb80bSSean Christopherson  * 0xc0000000-0xc0001fff.  The former (low) uses bytes 0-0x3ff for reads and
4390cacb80bSSean Christopherson  * 0x800-0xbff for writes.  The latter (high) uses 0x400-0x7ff for reads and
4400cacb80bSSean Christopherson  * 0xc00-0xfff for writes.  MSRs not covered by either of the ranges always
4410cacb80bSSean Christopherson  * VM-Exit.
4420cacb80bSSean Christopherson  */
4430cacb80bSSean Christopherson #define __BUILD_VMX_MSR_BITMAP_HELPER(rtype, action, bitop, access, base)      \
4440cacb80bSSean Christopherson static inline rtype vmx_##action##_msr_bitmap_##access(unsigned long *bitmap,  \
4450cacb80bSSean Christopherson 						       u32 msr)		       \
4460cacb80bSSean Christopherson {									       \
4470cacb80bSSean Christopherson 	int f = sizeof(unsigned long);					       \
4480cacb80bSSean Christopherson 									       \
4490cacb80bSSean Christopherson 	if (msr <= 0x1fff)						       \
4500cacb80bSSean Christopherson 		return bitop##_bit(msr, bitmap + base / f);		       \
4510cacb80bSSean Christopherson 	else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff))		       \
4520cacb80bSSean Christopherson 		return bitop##_bit(msr & 0x1fff, bitmap + (base + 0x400) / f); \
4530cacb80bSSean Christopherson 	return (rtype)true;						       \
45467f4b996SSean Christopherson }
4550cacb80bSSean Christopherson #define BUILD_VMX_MSR_BITMAP_HELPERS(ret_type, action, bitop)		       \
4560cacb80bSSean Christopherson 	__BUILD_VMX_MSR_BITMAP_HELPER(ret_type, action, bitop, read,  0x0)     \
4570cacb80bSSean Christopherson 	__BUILD_VMX_MSR_BITMAP_HELPER(ret_type, action, bitop, write, 0x800)
45867f4b996SSean Christopherson 
BUILD_VMX_MSR_BITMAP_HELPERS(bool,test,test)4590cacb80bSSean Christopherson BUILD_VMX_MSR_BITMAP_HELPERS(bool, test, test)
4600cacb80bSSean Christopherson BUILD_VMX_MSR_BITMAP_HELPERS(void, clear, __clear)
4610cacb80bSSean Christopherson BUILD_VMX_MSR_BITMAP_HELPERS(void, set, __set)
46267f4b996SSean Christopherson 
46389b0c9f5SSean Christopherson static inline u8 vmx_get_rvi(void)
46489b0c9f5SSean Christopherson {
46589b0c9f5SSean Christopherson 	return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
46689b0c9f5SSean Christopherson }
46789b0c9f5SSean Christopherson 
468ee087b4dSVitaly Kuznetsov #define __KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS				\
469ee087b4dSVitaly Kuznetsov 	(VM_ENTRY_LOAD_DEBUG_CONTROLS)
470ee087b4dSVitaly Kuznetsov #ifdef CONFIG_X86_64
471ee087b4dSVitaly Kuznetsov 	#define KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS			\
472ee087b4dSVitaly Kuznetsov 		(__KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS |			\
473ee087b4dSVitaly Kuznetsov 		 VM_ENTRY_IA32E_MODE)
474ee087b4dSVitaly Kuznetsov #else
475ee087b4dSVitaly Kuznetsov 	#define KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS			\
476ee087b4dSVitaly Kuznetsov 		__KVM_REQUIRED_VMX_VM_ENTRY_CONTROLS
477ee087b4dSVitaly Kuznetsov #endif
478ee087b4dSVitaly Kuznetsov #define KVM_OPTIONAL_VMX_VM_ENTRY_CONTROLS				\
479ee087b4dSVitaly Kuznetsov 	(VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |				\
480ee087b4dSVitaly Kuznetsov 	 VM_ENTRY_LOAD_IA32_PAT |					\
481ee087b4dSVitaly Kuznetsov 	 VM_ENTRY_LOAD_IA32_EFER |					\
482ee087b4dSVitaly Kuznetsov 	 VM_ENTRY_LOAD_BNDCFGS |					\
483ee087b4dSVitaly Kuznetsov 	 VM_ENTRY_PT_CONCEAL_PIP |					\
484ee087b4dSVitaly Kuznetsov 	 VM_ENTRY_LOAD_IA32_RTIT_CTL)
485ee087b4dSVitaly Kuznetsov 
486ee087b4dSVitaly Kuznetsov #define __KVM_REQUIRED_VMX_VM_EXIT_CONTROLS				\
487ee087b4dSVitaly Kuznetsov 	(VM_EXIT_SAVE_DEBUG_CONTROLS |					\
488ee087b4dSVitaly Kuznetsov 	 VM_EXIT_ACK_INTR_ON_EXIT)
489ee087b4dSVitaly Kuznetsov #ifdef CONFIG_X86_64
490ee087b4dSVitaly Kuznetsov 	#define KVM_REQUIRED_VMX_VM_EXIT_CONTROLS			\
491ee087b4dSVitaly Kuznetsov 		(__KVM_REQUIRED_VMX_VM_EXIT_CONTROLS |			\
492ee087b4dSVitaly Kuznetsov 		 VM_EXIT_HOST_ADDR_SPACE_SIZE)
493ee087b4dSVitaly Kuznetsov #else
494ee087b4dSVitaly Kuznetsov 	#define KVM_REQUIRED_VMX_VM_EXIT_CONTROLS			\
495ee087b4dSVitaly Kuznetsov 		__KVM_REQUIRED_VMX_VM_EXIT_CONTROLS
496ee087b4dSVitaly Kuznetsov #endif
497ee087b4dSVitaly Kuznetsov #define KVM_OPTIONAL_VMX_VM_EXIT_CONTROLS				\
498ee087b4dSVitaly Kuznetsov 	      (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |			\
499f16e4742SVitaly Kuznetsov 	       VM_EXIT_SAVE_IA32_PAT |					\
500ee087b4dSVitaly Kuznetsov 	       VM_EXIT_LOAD_IA32_PAT |					\
501f16e4742SVitaly Kuznetsov 	       VM_EXIT_SAVE_IA32_EFER |					\
502f16e4742SVitaly Kuznetsov 	       VM_EXIT_SAVE_VMX_PREEMPTION_TIMER |			\
503ee087b4dSVitaly Kuznetsov 	       VM_EXIT_LOAD_IA32_EFER |					\
504ee087b4dSVitaly Kuznetsov 	       VM_EXIT_CLEAR_BNDCFGS |					\
505ee087b4dSVitaly Kuznetsov 	       VM_EXIT_PT_CONCEAL_PIP |					\
506ee087b4dSVitaly Kuznetsov 	       VM_EXIT_CLEAR_IA32_RTIT_CTL)
507ee087b4dSVitaly Kuznetsov 
508ee087b4dSVitaly Kuznetsov #define KVM_REQUIRED_VMX_PIN_BASED_VM_EXEC_CONTROL			\
509ee087b4dSVitaly Kuznetsov 	(PIN_BASED_EXT_INTR_MASK |					\
510ee087b4dSVitaly Kuznetsov 	 PIN_BASED_NMI_EXITING)
511ee087b4dSVitaly Kuznetsov #define KVM_OPTIONAL_VMX_PIN_BASED_VM_EXEC_CONTROL			\
512ee087b4dSVitaly Kuznetsov 	(PIN_BASED_VIRTUAL_NMIS |					\
513ee087b4dSVitaly Kuznetsov 	 PIN_BASED_POSTED_INTR |					\
514ee087b4dSVitaly Kuznetsov 	 PIN_BASED_VMX_PREEMPTION_TIMER)
515ee087b4dSVitaly Kuznetsov 
516ee087b4dSVitaly Kuznetsov #define __KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL			\
517ee087b4dSVitaly Kuznetsov 	(CPU_BASED_HLT_EXITING |					\
518ee087b4dSVitaly Kuznetsov 	 CPU_BASED_CR3_LOAD_EXITING |					\
519ee087b4dSVitaly Kuznetsov 	 CPU_BASED_CR3_STORE_EXITING |					\
520ee087b4dSVitaly Kuznetsov 	 CPU_BASED_UNCOND_IO_EXITING |					\
521ee087b4dSVitaly Kuznetsov 	 CPU_BASED_MOV_DR_EXITING |					\
522ee087b4dSVitaly Kuznetsov 	 CPU_BASED_USE_TSC_OFFSETTING |					\
523ee087b4dSVitaly Kuznetsov 	 CPU_BASED_MWAIT_EXITING |					\
524ee087b4dSVitaly Kuznetsov 	 CPU_BASED_MONITOR_EXITING |					\
525ee087b4dSVitaly Kuznetsov 	 CPU_BASED_INVLPG_EXITING |					\
526ee087b4dSVitaly Kuznetsov 	 CPU_BASED_RDPMC_EXITING |					\
527ee087b4dSVitaly Kuznetsov 	 CPU_BASED_INTR_WINDOW_EXITING)
528ee087b4dSVitaly Kuznetsov 
529ee087b4dSVitaly Kuznetsov #ifdef CONFIG_X86_64
530ee087b4dSVitaly Kuznetsov 	#define KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL		\
531ee087b4dSVitaly Kuznetsov 		(__KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL |		\
532ee087b4dSVitaly Kuznetsov 		 CPU_BASED_CR8_LOAD_EXITING |				\
533ee087b4dSVitaly Kuznetsov 		 CPU_BASED_CR8_STORE_EXITING)
534ee087b4dSVitaly Kuznetsov #else
535ee087b4dSVitaly Kuznetsov 	#define KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL		\
536ee087b4dSVitaly Kuznetsov 		__KVM_REQUIRED_VMX_CPU_BASED_VM_EXEC_CONTROL
537ee087b4dSVitaly Kuznetsov #endif
538ee087b4dSVitaly Kuznetsov 
539ee087b4dSVitaly Kuznetsov #define KVM_OPTIONAL_VMX_CPU_BASED_VM_EXEC_CONTROL			\
540a83bea73SVitaly Kuznetsov 	(CPU_BASED_RDTSC_EXITING |					\
541a83bea73SVitaly Kuznetsov 	 CPU_BASED_TPR_SHADOW |						\
542a83bea73SVitaly Kuznetsov 	 CPU_BASED_USE_IO_BITMAPS |					\
543a83bea73SVitaly Kuznetsov 	 CPU_BASED_MONITOR_TRAP_FLAG |					\
544ee087b4dSVitaly Kuznetsov 	 CPU_BASED_USE_MSR_BITMAPS |					\
545ee087b4dSVitaly Kuznetsov 	 CPU_BASED_NMI_WINDOW_EXITING |					\
546a83bea73SVitaly Kuznetsov 	 CPU_BASED_PAUSE_EXITING |					\
547ee087b4dSVitaly Kuznetsov 	 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS |			\
548ee087b4dSVitaly Kuznetsov 	 CPU_BASED_ACTIVATE_TERTIARY_CONTROLS)
549ee087b4dSVitaly Kuznetsov 
550ee087b4dSVitaly Kuznetsov #define KVM_REQUIRED_VMX_SECONDARY_VM_EXEC_CONTROL 0
551ee087b4dSVitaly Kuznetsov #define KVM_OPTIONAL_VMX_SECONDARY_VM_EXEC_CONTROL			\
552ee087b4dSVitaly Kuznetsov 	(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |			\
553ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |			\
554ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_WBINVD_EXITING |				\
555ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_ENABLE_VPID |					\
556ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_ENABLE_EPT |					\
557ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_UNRESTRICTED_GUEST |				\
558ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_PAUSE_LOOP_EXITING |				\
559ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_DESC |						\
560ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_ENABLE_RDTSCP |					\
561ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_ENABLE_INVPCID |				\
562ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_APIC_REGISTER_VIRT |				\
563ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |				\
564ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_SHADOW_VMCS |					\
565662f6815SSean Christopherson 	 SECONDARY_EXEC_ENABLE_XSAVES |					\
566ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_RDSEED_EXITING |				\
567ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_RDRAND_EXITING |				\
568ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_ENABLE_PML |					\
569ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_TSC_SCALING |					\
570ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |				\
571ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_PT_USE_GPA |					\
572ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_PT_CONCEAL_VMX |				\
573ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_ENABLE_VMFUNC |					\
574ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_BUS_LOCK_DETECTION |				\
575ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_NOTIFY_VM_EXITING |				\
576ee087b4dSVitaly Kuznetsov 	 SECONDARY_EXEC_ENCLS_EXITING)
577ee087b4dSVitaly Kuznetsov 
578ee087b4dSVitaly Kuznetsov #define KVM_REQUIRED_VMX_TERTIARY_VM_EXEC_CONTROL 0
579ee087b4dSVitaly Kuznetsov #define KVM_OPTIONAL_VMX_TERTIARY_VM_EXEC_CONTROL			\
580ee087b4dSVitaly Kuznetsov 	(TERTIARY_EXEC_IPI_VIRT)
581ee087b4dSVitaly Kuznetsov 
582ed3905baSRobert Hoo #define BUILD_CONTROLS_SHADOW(lname, uname, bits)						\
583ed3905baSRobert Hoo static inline void lname##_controls_set(struct vcpu_vmx *vmx, u##bits val)			\
58470f932ecSSean Christopherson {												\
58509e226cfSSean Christopherson 	if (vmx->loaded_vmcs->controls_shadow.lname != val) {					\
586ed3905baSRobert Hoo 		vmcs_write##bits(uname, val);							\
58709e226cfSSean Christopherson 		vmx->loaded_vmcs->controls_shadow.lname = val;					\
58809e226cfSSean Christopherson 	}											\
58970f932ecSSean Christopherson }												\
590ed3905baSRobert Hoo static inline u##bits __##lname##_controls_get(struct loaded_vmcs *vmcs)			\
591389ab252SSean Christopherson {												\
592389ab252SSean Christopherson 	return vmcs->controls_shadow.lname;							\
593389ab252SSean Christopherson }												\
594ed3905baSRobert Hoo static inline u##bits lname##_controls_get(struct vcpu_vmx *vmx)				\
59570f932ecSSean Christopherson {												\
596389ab252SSean Christopherson 	return __##lname##_controls_get(vmx->loaded_vmcs);					\
59770f932ecSSean Christopherson }												\
598ee087b4dSVitaly Kuznetsov static __always_inline void lname##_controls_setbit(struct vcpu_vmx *vmx, u##bits val)		\
59970f932ecSSean Christopherson {												\
600ee087b4dSVitaly Kuznetsov 	BUILD_BUG_ON(!(val & (KVM_REQUIRED_VMX_##uname | KVM_OPTIONAL_VMX_##uname)));		\
60170f932ecSSean Christopherson 	lname##_controls_set(vmx, lname##_controls_get(vmx) | val);				\
60270f932ecSSean Christopherson }												\
603ee087b4dSVitaly Kuznetsov static __always_inline void lname##_controls_clearbit(struct vcpu_vmx *vmx, u##bits val)	\
60470f932ecSSean Christopherson {												\
605ee087b4dSVitaly Kuznetsov 	BUILD_BUG_ON(!(val & (KVM_REQUIRED_VMX_##uname | KVM_OPTIONAL_VMX_##uname)));		\
60670f932ecSSean Christopherson 	lname##_controls_set(vmx, lname##_controls_get(vmx) & ~val);				\
60789b0c9f5SSean Christopherson }
608ed3905baSRobert Hoo BUILD_CONTROLS_SHADOW(vm_entry, VM_ENTRY_CONTROLS, 32)
609ed3905baSRobert Hoo BUILD_CONTROLS_SHADOW(vm_exit, VM_EXIT_CONTROLS, 32)
610ed3905baSRobert Hoo BUILD_CONTROLS_SHADOW(pin, PIN_BASED_VM_EXEC_CONTROL, 32)
611ed3905baSRobert Hoo BUILD_CONTROLS_SHADOW(exec, CPU_BASED_VM_EXEC_CONTROL, 32)
612ed3905baSRobert Hoo BUILD_CONTROLS_SHADOW(secondary_exec, SECONDARY_VM_EXEC_CONTROL, 32)
6131ad4e543SRobert Hoo BUILD_CONTROLS_SHADOW(tertiary_exec, TERTIARY_VM_EXEC_CONTROL, 64)
61489b0c9f5SSean Christopherson 
61541e68b69SPaolo Bonzini /*
61641e68b69SPaolo Bonzini  * VMX_REGS_LAZY_LOAD_SET - The set of registers that will be updated in the
61741e68b69SPaolo Bonzini  * cache on demand.  Other registers not listed here are synced to
61841e68b69SPaolo Bonzini  * the cache immediately after VM-Exit.
61941e68b69SPaolo Bonzini  */
62041e68b69SPaolo Bonzini #define VMX_REGS_LAZY_LOAD_SET	((1 << VCPU_REGS_RIP) |         \
62141e68b69SPaolo Bonzini 				(1 << VCPU_REGS_RSP) |          \
62241e68b69SPaolo Bonzini 				(1 << VCPU_EXREG_RFLAGS) |      \
62341e68b69SPaolo Bonzini 				(1 << VCPU_EXREG_PDPTR) |       \
62441e68b69SPaolo Bonzini 				(1 << VCPU_EXREG_SEGMENTS) |    \
62541e68b69SPaolo Bonzini 				(1 << VCPU_EXREG_CR0) |         \
62641e68b69SPaolo Bonzini 				(1 << VCPU_EXREG_CR3) |         \
62741e68b69SPaolo Bonzini 				(1 << VCPU_EXREG_CR4) |         \
62841e68b69SPaolo Bonzini 				(1 << VCPU_EXREG_EXIT_INFO_1) | \
62941e68b69SPaolo Bonzini 				(1 << VCPU_EXREG_EXIT_INFO_2))
630e5d03de5SSean Christopherson 
vmx_l1_guest_owned_cr0_bits(void)631fb509f76SMathias Krause static inline unsigned long vmx_l1_guest_owned_cr0_bits(void)
632fb509f76SMathias Krause {
633fb509f76SMathias Krause 	unsigned long bits = KVM_POSSIBLE_CR0_GUEST_BITS;
634fb509f76SMathias Krause 
635fb509f76SMathias Krause 	/*
636fb509f76SMathias Krause 	 * CR0.WP needs to be intercepted when KVM is shadowing legacy paging
637fb509f76SMathias Krause 	 * in order to construct shadow PTEs with the correct protections.
638fb509f76SMathias Krause 	 * Note!  CR0.WP technically can be passed through to the guest if
639fb509f76SMathias Krause 	 * paging is disabled, but checking CR0.PG would generate a cyclical
640fb509f76SMathias Krause 	 * dependency of sorts due to forcing the caller to ensure CR0 holds
641fb509f76SMathias Krause 	 * the correct value prior to determining which CR0 bits can be owned
642fb509f76SMathias Krause 	 * by L1.  Keep it simple and limit the optimization to EPT.
643fb509f76SMathias Krause 	 */
644fb509f76SMathias Krause 	if (!enable_ept)
645fb509f76SMathias Krause 		bits &= ~X86_CR0_WP;
646fb509f76SMathias Krause 	return bits;
647fb509f76SMathias Krause }
648fb509f76SMathias Krause 
to_kvm_vmx(struct kvm * kvm)649432727f1SSean Christopherson static __always_inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
6508373d25dSSean Christopherson {
6518373d25dSSean Christopherson 	return container_of(kvm, struct kvm_vmx, kvm);
6528373d25dSSean Christopherson }
6538373d25dSSean Christopherson 
to_vmx(struct kvm_vcpu * vcpu)654432727f1SSean Christopherson static __always_inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
6558373d25dSSean Christopherson {
6568373d25dSSean Christopherson 	return container_of(vcpu, struct vcpu_vmx, vcpu);
6578373d25dSSean Christopherson }
6588373d25dSSean Christopherson 
vcpu_to_lbr_desc(struct kvm_vcpu * vcpu)6597de8e5b6SSean Christopherson static inline struct lbr_desc *vcpu_to_lbr_desc(struct kvm_vcpu *vcpu)
6607de8e5b6SSean Christopherson {
6617de8e5b6SSean Christopherson 	return &to_vmx(vcpu)->lbr_desc;
6627de8e5b6SSean Christopherson }
6637de8e5b6SSean Christopherson 
vcpu_to_lbr_records(struct kvm_vcpu * vcpu)6647de8e5b6SSean Christopherson static inline struct x86_pmu_lbr *vcpu_to_lbr_records(struct kvm_vcpu *vcpu)
6657de8e5b6SSean Christopherson {
6667de8e5b6SSean Christopherson 	return &vcpu_to_lbr_desc(vcpu)->records;
6677de8e5b6SSean Christopherson }
6687de8e5b6SSean Christopherson 
intel_pmu_lbr_is_enabled(struct kvm_vcpu * vcpu)6696348aafaSSean Christopherson static inline bool intel_pmu_lbr_is_enabled(struct kvm_vcpu *vcpu)
6706348aafaSSean Christopherson {
6716348aafaSSean Christopherson 	return !!vcpu_to_lbr_records(vcpu)->nr;
6726348aafaSSean Christopherson }
6737de8e5b6SSean Christopherson 
6746348aafaSSean Christopherson void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu);
6757de8e5b6SSean Christopherson int intel_pmu_create_guest_lbr_event(struct kvm_vcpu *vcpu);
6767de8e5b6SSean Christopherson void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu);
6777de8e5b6SSean Christopherson 
vmx_get_exit_qual(struct kvm_vcpu * vcpu)678fc9465beSSean Christopherson static __always_inline unsigned long vmx_get_exit_qual(struct kvm_vcpu *vcpu)
6795addc235SSean Christopherson {
6805addc235SSean Christopherson 	struct vcpu_vmx *vmx = to_vmx(vcpu);
6815addc235SSean Christopherson 
682fc9465beSSean Christopherson 	if (!kvm_register_test_and_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_1))
6835addc235SSean Christopherson 		vmx->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
684fc9465beSSean Christopherson 
6855addc235SSean Christopherson 	return vmx->exit_qualification;
6865addc235SSean Christopherson }
6875addc235SSean Christopherson 
vmx_get_intr_info(struct kvm_vcpu * vcpu)688fc9465beSSean Christopherson static __always_inline u32 vmx_get_intr_info(struct kvm_vcpu *vcpu)
68987915858SSean Christopherson {
69087915858SSean Christopherson 	struct vcpu_vmx *vmx = to_vmx(vcpu);
69187915858SSean Christopherson 
692fc9465beSSean Christopherson 	if (!kvm_register_test_and_mark_available(vcpu, VCPU_EXREG_EXIT_INFO_2))
69387915858SSean Christopherson 		vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
694fc9465beSSean Christopherson 
69587915858SSean Christopherson 	return vmx->exit_intr_info;
69687915858SSean Christopherson }
69787915858SSean Christopherson 
69841836839SBen Gardon struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags);
69989b0c9f5SSean Christopherson void free_vmcs(struct vmcs *vmcs);
70089b0c9f5SSean Christopherson int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
70189b0c9f5SSean Christopherson void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
70289b0c9f5SSean Christopherson void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);
70389b0c9f5SSean Christopherson 
alloc_vmcs(bool shadow)70489b0c9f5SSean Christopherson static inline struct vmcs *alloc_vmcs(bool shadow)
70589b0c9f5SSean Christopherson {
70641836839SBen Gardon 	return alloc_vmcs_cpu(shadow, raw_smp_processor_id(),
70741836839SBen Gardon 			      GFP_KERNEL_ACCOUNT);
70889b0c9f5SSean Christopherson }
70989b0c9f5SSean Christopherson 
vmx_has_waitpkg(struct vcpu_vmx * vmx)7106e3ba4abSTao Xu static inline bool vmx_has_waitpkg(struct vcpu_vmx *vmx)
7116e3ba4abSTao Xu {
7127b9cae02SSean Christopherson 	return secondary_exec_controls_get(vmx) &
7136e3ba4abSTao Xu 		SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
7146e3ba4abSTao Xu }
7156e3ba4abSTao Xu 
vmx_need_pf_intercept(struct kvm_vcpu * vcpu)716a0c13434SPaolo Bonzini static inline bool vmx_need_pf_intercept(struct kvm_vcpu *vcpu)
717a0c13434SPaolo Bonzini {
718b96e6506SMohammed Gamal 	if (!enable_ept)
719b96e6506SMohammed Gamal 		return true;
720b96e6506SMohammed Gamal 
721b96e6506SMohammed Gamal 	return allow_smaller_maxphyaddr && cpuid_maxphyaddr(vcpu) < boot_cpu_data.x86_phys_bits;
722a0c13434SPaolo Bonzini }
723a0c13434SPaolo Bonzini 
is_unrestricted_guest(struct kvm_vcpu * vcpu)724bddd82d1SKrish Sadhukhan static inline bool is_unrestricted_guest(struct kvm_vcpu *vcpu)
725bddd82d1SKrish Sadhukhan {
726bddd82d1SKrish Sadhukhan 	return enable_unrestricted_guest && (!is_guest_mode(vcpu) ||
727bddd82d1SKrish Sadhukhan 	    (secondary_exec_controls_get(to_vmx(vcpu)) &
728bddd82d1SKrish Sadhukhan 	    SECONDARY_EXEC_UNRESTRICTED_GUEST));
729bddd82d1SKrish Sadhukhan }
730bddd82d1SKrish Sadhukhan 
7312ba4493aSSean Christopherson bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu);
vmx_guest_state_valid(struct kvm_vcpu * vcpu)7322ba4493aSSean Christopherson static inline bool vmx_guest_state_valid(struct kvm_vcpu *vcpu)
7332ba4493aSSean Christopherson {
7342ba4493aSSean Christopherson 	return is_unrestricted_guest(vcpu) || __vmx_guest_state_valid(vcpu);
7352ba4493aSSean Christopherson }
7362ba4493aSSean Christopherson 
7370702a3cbSDavid Edmondson void dump_vmcs(struct kvm_vcpu *vcpu);
73869090810SPaolo Bonzini 
vmx_get_instr_info_reg2(u32 vmx_instr_info)739329bd56cSVipin Sharma static inline int vmx_get_instr_info_reg2(u32 vmx_instr_info)
740329bd56cSVipin Sharma {
741329bd56cSVipin Sharma 	return (vmx_instr_info >> 28) & 0xf;
742329bd56cSVipin Sharma }
743329bd56cSVipin Sharma 
vmx_can_use_ipiv(struct kvm_vcpu * vcpu)744d588bb9bSChao Gao static inline bool vmx_can_use_ipiv(struct kvm_vcpu *vcpu)
745d588bb9bSChao Gao {
746d588bb9bSChao Gao 	return  lapic_in_kernel(vcpu) && enable_ipiv;
747d588bb9bSChao Gao }
748d588bb9bSChao Gao 
guest_cpuid_has_evmcs(struct kvm_vcpu * vcpu)74985ab071aSSean Christopherson static inline bool guest_cpuid_has_evmcs(struct kvm_vcpu *vcpu)
75085ab071aSSean Christopherson {
75185ab071aSSean Christopherson 	/*
75285ab071aSSean Christopherson 	 * eVMCS is exposed to the guest if Hyper-V is enabled in CPUID and
75385ab071aSSean Christopherson 	 * eVMCS has been explicitly enabled by userspace.
75485ab071aSSean Christopherson 	 */
75585ab071aSSean Christopherson 	return vcpu->arch.hyperv_enabled &&
75685ab071aSSean Christopherson 	       to_vmx(vcpu)->nested.enlightened_vmcs_enabled;
75785ab071aSSean Christopherson }
75885ab071aSSean Christopherson 
7598373d25dSSean Christopherson #endif /* __KVM_X86_VMX_H */
760