1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_VMX_VMCS_H
3 #define __KVM_X86_VMX_VMCS_H
5 #include <linux/ktime.h>
6 #include <linux/list.h>
7 #include <linux/nospec.h>
12 #include "capabilities.h"
14 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
27 DECLARE_PER_CPU(struct vmcs *, current_vmcs);
30 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
31 * and whose values change infrequently, but are not constant. I.e. this is
32 * used as a write-through cache of the corresponding VMCS fields.
34 struct vmcs_host_state {
35 unsigned long cr3; /* May not match real cr3 */
36 unsigned long cr4; /* May not match real cr4 */
37 unsigned long gs_base;
38 unsigned long fs_base;
41 u16 fs_sel, gs_sel, ldt_sel;
47 struct vmcs_controls_shadow {
57 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
58 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
59 * loaded on this CPU (so we can clear them if the CPU goes down).
63 struct vmcs *shadow_vmcs;
66 bool nmi_known_unmasked;
67 bool hv_timer_soft_disabled;
68 /* Support for vnmi-less CPUs */
69 int soft_vnmi_blocked;
71 s64 vnmi_blocked_time;
72 unsigned long *msr_bitmap;
73 struct list_head loaded_vmcss_on_cpu_link;
74 struct vmcs_host_state host_state;
75 struct vmcs_controls_shadow controls_shadow;
78 static __always_inline bool is_intr_type(u32 intr_info, u32 type)
80 const u32 mask = INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK;
82 return (intr_info & mask) == (INTR_INFO_VALID_MASK | type);
85 static inline bool is_intr_type_n(u32 intr_info, u32 type, u8 vector)
87 const u32 mask = INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK |
88 INTR_INFO_VECTOR_MASK;
90 return (intr_info & mask) == (INTR_INFO_VALID_MASK | type | vector);
93 static inline bool is_exception_n(u32 intr_info, u8 vector)
95 return is_intr_type_n(intr_info, INTR_TYPE_HARD_EXCEPTION, vector);
98 static inline bool is_debug(u32 intr_info)
100 return is_exception_n(intr_info, DB_VECTOR);
103 static inline bool is_breakpoint(u32 intr_info)
105 return is_exception_n(intr_info, BP_VECTOR);
108 static inline bool is_double_fault(u32 intr_info)
110 return is_exception_n(intr_info, DF_VECTOR);
113 static inline bool is_page_fault(u32 intr_info)
115 return is_exception_n(intr_info, PF_VECTOR);
118 static inline bool is_invalid_opcode(u32 intr_info)
120 return is_exception_n(intr_info, UD_VECTOR);
123 static inline bool is_gp_fault(u32 intr_info)
125 return is_exception_n(intr_info, GP_VECTOR);
128 static inline bool is_alignment_check(u32 intr_info)
130 return is_exception_n(intr_info, AC_VECTOR);
133 static inline bool is_machine_check(u32 intr_info)
135 return is_exception_n(intr_info, MC_VECTOR);
138 static inline bool is_nm_fault(u32 intr_info)
140 return is_exception_n(intr_info, NM_VECTOR);
143 /* Undocumented: icebp/int1 */
144 static inline bool is_icebp(u32 intr_info)
146 return is_intr_type(intr_info, INTR_TYPE_PRIV_SW_EXCEPTION);
149 static __always_inline bool is_nmi(u32 intr_info)
151 return is_intr_type(intr_info, INTR_TYPE_NMI_INTR);
154 static inline bool is_external_intr(u32 intr_info)
156 return is_intr_type(intr_info, INTR_TYPE_EXT_INTR);
159 static inline bool is_exception_with_error_code(u32 intr_info)
161 const u32 mask = INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK;
163 return (intr_info & mask) == mask;
166 enum vmcs_field_width {
167 VMCS_FIELD_WIDTH_U16 = 0,
168 VMCS_FIELD_WIDTH_U64 = 1,
169 VMCS_FIELD_WIDTH_U32 = 2,
170 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
173 static inline int vmcs_field_width(unsigned long field)
175 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
176 return VMCS_FIELD_WIDTH_U32;
177 return (field >> 13) & 0x3;
180 static inline int vmcs_field_readonly(unsigned long field)
182 return (((field >> 10) & 0x3) == 1);
185 #define VMCS_FIELD_INDEX_SHIFT (1)
186 #define VMCS_FIELD_INDEX_MASK GENMASK(9, 1)
188 static inline unsigned int vmcs_field_index(unsigned long field)
190 return (field & VMCS_FIELD_INDEX_MASK) >> VMCS_FIELD_INDEX_SHIFT;
193 #endif /* __KVM_X86_VMX_VMCS_H */