xref: /openbmc/linux/arch/x86/kvm/vmx/vmcs.h (revision 11df586d)
1cb1d474bSSean Christopherson /* SPDX-License-Identifier: GPL-2.0 */
2cb1d474bSSean Christopherson #ifndef __KVM_X86_VMX_VMCS_H
3cb1d474bSSean Christopherson #define __KVM_X86_VMX_VMCS_H
4cb1d474bSSean Christopherson 
5cb1d474bSSean Christopherson #include <linux/ktime.h>
6609363cfSSean Christopherson #include <linux/list.h>
7609363cfSSean Christopherson #include <linux/nospec.h>
8cb1d474bSSean Christopherson 
9609363cfSSean Christopherson #include <asm/kvm.h>
10cb1d474bSSean Christopherson #include <asm/vmx.h>
11cb1d474bSSean Christopherson 
12cb1d474bSSean Christopherson #include "capabilities.h"
13cb1d474bSSean Christopherson 
14c1a527a1SLike Xu #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
15c1a527a1SLike Xu 
16cb1d474bSSean Christopherson struct vmcs_hdr {
17cb1d474bSSean Christopherson 	u32 revision_id:31;
18cb1d474bSSean Christopherson 	u32 shadow_vmcs:1;
19cb1d474bSSean Christopherson };
20cb1d474bSSean Christopherson 
21cb1d474bSSean Christopherson struct vmcs {
22cb1d474bSSean Christopherson 	struct vmcs_hdr hdr;
23cb1d474bSSean Christopherson 	u32 abort;
24f4a9fdd5SGustavo A. R. Silva 	char data[];
25cb1d474bSSean Christopherson };
26cb1d474bSSean Christopherson 
2775edce8aSSean Christopherson DECLARE_PER_CPU(struct vmcs *, current_vmcs);
2875edce8aSSean Christopherson 
29cb1d474bSSean Christopherson /*
30cb1d474bSSean Christopherson  * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
31cb1d474bSSean Christopherson  * and whose values change infrequently, but are not constant.  I.e. this is
32cb1d474bSSean Christopherson  * used as a write-through cache of the corresponding VMCS fields.
33cb1d474bSSean Christopherson  */
34cb1d474bSSean Christopherson struct vmcs_host_state {
35cb1d474bSSean Christopherson 	unsigned long cr3;	/* May not match real cr3 */
36cb1d474bSSean Christopherson 	unsigned long cr4;	/* May not match real cr4 */
37cb1d474bSSean Christopherson 	unsigned long gs_base;
38cb1d474bSSean Christopherson 	unsigned long fs_base;
395a878160SSean Christopherson 	unsigned long rsp;
40cb1d474bSSean Christopherson 
41cb1d474bSSean Christopherson 	u16           fs_sel, gs_sel, ldt_sel;
42cb1d474bSSean Christopherson #ifdef CONFIG_X86_64
43cb1d474bSSean Christopherson 	u16           ds_sel, es_sel;
44cb1d474bSSean Christopherson #endif
45cb1d474bSSean Christopherson };
46cb1d474bSSean Christopherson 
4709e226cfSSean Christopherson struct vmcs_controls_shadow {
4809e226cfSSean Christopherson 	u32 vm_entry;
4909e226cfSSean Christopherson 	u32 vm_exit;
5009e226cfSSean Christopherson 	u32 pin;
5109e226cfSSean Christopherson 	u32 exec;
5209e226cfSSean Christopherson 	u32 secondary_exec;
531ad4e543SRobert Hoo 	u64 tertiary_exec;
5409e226cfSSean Christopherson };
5509e226cfSSean Christopherson 
56cb1d474bSSean Christopherson /*
57cb1d474bSSean Christopherson  * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
58cb1d474bSSean Christopherson  * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
59cb1d474bSSean Christopherson  * loaded on this CPU (so we can clear them if the CPU goes down).
60cb1d474bSSean Christopherson  */
61cb1d474bSSean Christopherson struct loaded_vmcs {
62cb1d474bSSean Christopherson 	struct vmcs *vmcs;
63cb1d474bSSean Christopherson 	struct vmcs *shadow_vmcs;
64cb1d474bSSean Christopherson 	int cpu;
65cb1d474bSSean Christopherson 	bool launched;
66cb1d474bSSean Christopherson 	bool nmi_known_unmasked;
67804939eaSSean Christopherson 	bool hv_timer_soft_disabled;
68cb1d474bSSean Christopherson 	/* Support for vnmi-less CPUs */
69cb1d474bSSean Christopherson 	int soft_vnmi_blocked;
70cb1d474bSSean Christopherson 	ktime_t entry_time;
71cb1d474bSSean Christopherson 	s64 vnmi_blocked_time;
72cb1d474bSSean Christopherson 	unsigned long *msr_bitmap;
73cb1d474bSSean Christopherson 	struct list_head loaded_vmcss_on_cpu_link;
74cb1d474bSSean Christopherson 	struct vmcs_host_state host_state;
7509e226cfSSean Christopherson 	struct vmcs_controls_shadow controls_shadow;
76cb1d474bSSean Christopherson };
77cb1d474bSSean Christopherson 
is_intr_type(u32 intr_info,u32 type)78*11df586dSSean Christopherson static __always_inline bool is_intr_type(u32 intr_info, u32 type)
7988c200d9SSean Christopherson {
8088c200d9SSean Christopherson 	const u32 mask = INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK;
8188c200d9SSean Christopherson 
8288c200d9SSean Christopherson 	return (intr_info & mask) == (INTR_INFO_VALID_MASK | type);
8388c200d9SSean Christopherson }
8488c200d9SSean Christopherson 
is_intr_type_n(u32 intr_info,u32 type,u8 vector)8588c200d9SSean Christopherson static inline bool is_intr_type_n(u32 intr_info, u32 type, u8 vector)
8688c200d9SSean Christopherson {
8788c200d9SSean Christopherson 	const u32 mask = INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK |
8888c200d9SSean Christopherson 			 INTR_INFO_VECTOR_MASK;
8988c200d9SSean Christopherson 
9088c200d9SSean Christopherson 	return (intr_info & mask) == (INTR_INFO_VALID_MASK | type | vector);
9188c200d9SSean Christopherson }
9288c200d9SSean Christopherson 
is_exception_n(u32 intr_info,u8 vector)93cb1d474bSSean Christopherson static inline bool is_exception_n(u32 intr_info, u8 vector)
94cb1d474bSSean Christopherson {
9588c200d9SSean Christopherson 	return is_intr_type_n(intr_info, INTR_TYPE_HARD_EXCEPTION, vector);
96cb1d474bSSean Christopherson }
97cb1d474bSSean Christopherson 
is_debug(u32 intr_info)98cb1d474bSSean Christopherson static inline bool is_debug(u32 intr_info)
99cb1d474bSSean Christopherson {
100cb1d474bSSean Christopherson 	return is_exception_n(intr_info, DB_VECTOR);
101cb1d474bSSean Christopherson }
102cb1d474bSSean Christopherson 
is_breakpoint(u32 intr_info)103cb1d474bSSean Christopherson static inline bool is_breakpoint(u32 intr_info)
104cb1d474bSSean Christopherson {
105cb1d474bSSean Christopherson 	return is_exception_n(intr_info, BP_VECTOR);
106cb1d474bSSean Christopherson }
107cb1d474bSSean Christopherson 
is_double_fault(u32 intr_info)1089bd1f0efSSean Christopherson static inline bool is_double_fault(u32 intr_info)
1099bd1f0efSSean Christopherson {
1109bd1f0efSSean Christopherson 	return is_exception_n(intr_info, DF_VECTOR);
1119bd1f0efSSean Christopherson }
1129bd1f0efSSean Christopherson 
is_page_fault(u32 intr_info)113cb1d474bSSean Christopherson static inline bool is_page_fault(u32 intr_info)
114cb1d474bSSean Christopherson {
115cb1d474bSSean Christopherson 	return is_exception_n(intr_info, PF_VECTOR);
116cb1d474bSSean Christopherson }
117cb1d474bSSean Christopherson 
is_invalid_opcode(u32 intr_info)118cb1d474bSSean Christopherson static inline bool is_invalid_opcode(u32 intr_info)
119cb1d474bSSean Christopherson {
120cb1d474bSSean Christopherson 	return is_exception_n(intr_info, UD_VECTOR);
121cb1d474bSSean Christopherson }
122cb1d474bSSean Christopherson 
is_gp_fault(u32 intr_info)123cb1d474bSSean Christopherson static inline bool is_gp_fault(u32 intr_info)
124cb1d474bSSean Christopherson {
125cb1d474bSSean Christopherson 	return is_exception_n(intr_info, GP_VECTOR);
126cb1d474bSSean Christopherson }
127cb1d474bSSean Christopherson 
is_alignment_check(u32 intr_info)128b33bb78aSSean Christopherson static inline bool is_alignment_check(u32 intr_info)
129b33bb78aSSean Christopherson {
130b33bb78aSSean Christopherson 	return is_exception_n(intr_info, AC_VECTOR);
131b33bb78aSSean Christopherson }
132b33bb78aSSean Christopherson 
is_machine_check(u32 intr_info)133cb1d474bSSean Christopherson static inline bool is_machine_check(u32 intr_info)
134cb1d474bSSean Christopherson {
13588c200d9SSean Christopherson 	return is_exception_n(intr_info, MC_VECTOR);
136cb1d474bSSean Christopherson }
137cb1d474bSSean Christopherson 
is_nm_fault(u32 intr_info)138ec5be88aSJing Liu static inline bool is_nm_fault(u32 intr_info)
139ec5be88aSJing Liu {
140ec5be88aSJing Liu 	return is_exception_n(intr_info, NM_VECTOR);
141ec5be88aSJing Liu }
142ec5be88aSJing Liu 
143cb1d474bSSean Christopherson /* Undocumented: icebp/int1 */
is_icebp(u32 intr_info)144cb1d474bSSean Christopherson static inline bool is_icebp(u32 intr_info)
145cb1d474bSSean Christopherson {
14688c200d9SSean Christopherson 	return is_intr_type(intr_info, INTR_TYPE_PRIV_SW_EXCEPTION);
147cb1d474bSSean Christopherson }
148cb1d474bSSean Christopherson 
is_nmi(u32 intr_info)149*11df586dSSean Christopherson static __always_inline bool is_nmi(u32 intr_info)
150cb1d474bSSean Christopherson {
15188c200d9SSean Christopherson 	return is_intr_type(intr_info, INTR_TYPE_NMI_INTR);
152cb1d474bSSean Christopherson }
153cb1d474bSSean Christopherson 
is_external_intr(u32 intr_info)15449def500SSean Christopherson static inline bool is_external_intr(u32 intr_info)
15549def500SSean Christopherson {
15688c200d9SSean Christopherson 	return is_intr_type(intr_info, INTR_TYPE_EXT_INTR);
15749def500SSean Christopherson }
15849def500SSean Christopherson 
is_exception_with_error_code(u32 intr_info)159f315f2b1SSean Christopherson static inline bool is_exception_with_error_code(u32 intr_info)
160f315f2b1SSean Christopherson {
161f315f2b1SSean Christopherson 	const u32 mask = INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK;
162f315f2b1SSean Christopherson 
163f315f2b1SSean Christopherson 	return (intr_info & mask) == mask;
164f315f2b1SSean Christopherson }
165f315f2b1SSean Christopherson 
166cb1d474bSSean Christopherson enum vmcs_field_width {
167cb1d474bSSean Christopherson 	VMCS_FIELD_WIDTH_U16 = 0,
168cb1d474bSSean Christopherson 	VMCS_FIELD_WIDTH_U64 = 1,
169cb1d474bSSean Christopherson 	VMCS_FIELD_WIDTH_U32 = 2,
170cb1d474bSSean Christopherson 	VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
171cb1d474bSSean Christopherson };
172cb1d474bSSean Christopherson 
vmcs_field_width(unsigned long field)173cb1d474bSSean Christopherson static inline int vmcs_field_width(unsigned long field)
174cb1d474bSSean Christopherson {
175cb1d474bSSean Christopherson 	if (0x1 & field)	/* the *_HIGH fields are all 32 bit */
176cb1d474bSSean Christopherson 		return VMCS_FIELD_WIDTH_U32;
177cb1d474bSSean Christopherson 	return (field >> 13) & 0x3;
178cb1d474bSSean Christopherson }
179cb1d474bSSean Christopherson 
vmcs_field_readonly(unsigned long field)180cb1d474bSSean Christopherson static inline int vmcs_field_readonly(unsigned long field)
181cb1d474bSSean Christopherson {
182cb1d474bSSean Christopherson 	return (((field >> 10) & 0x3) == 1);
183cb1d474bSSean Christopherson }
184cb1d474bSSean Christopherson 
185ba1f8245SSean Christopherson #define VMCS_FIELD_INDEX_SHIFT		(1)
186ba1f8245SSean Christopherson #define VMCS_FIELD_INDEX_MASK		GENMASK(9, 1)
187ba1f8245SSean Christopherson 
vmcs_field_index(unsigned long field)188ba1f8245SSean Christopherson static inline unsigned int vmcs_field_index(unsigned long field)
189ba1f8245SSean Christopherson {
190ba1f8245SSean Christopherson 	return (field & VMCS_FIELD_INDEX_MASK) >> VMCS_FIELD_INDEX_SHIFT;
191ba1f8245SSean Christopherson }
192ba1f8245SSean Christopherson 
193cb1d474bSSean Christopherson #endif /* __KVM_X86_VMX_VMCS_H */
194