KVM
vmcs.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_VMX_VMCS_H
3 #define __KVM_X86_VMX_VMCS_H
4 
5 #include <linux/ktime.h>
6 #include <linux/list.h>
7 #include <linux/nospec.h>
8 
9 #include <asm/kvm.h>
10 #include <asm/vmx.h>
11 
12 #include "capabilities.h"
13 
14 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
15 
16 struct vmcs_hdr {
17  u32 revision_id:31;
18  u32 shadow_vmcs:1;
19 };
20 
21 struct vmcs {
22  struct vmcs_hdr hdr;
23  u32 abort;
24  char data[];
25 };
26 
27 DECLARE_PER_CPU(struct vmcs *, current_vmcs);
28 
29 /*
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.
33  */
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;
39  unsigned long rsp;
40 
42 #ifdef CONFIG_X86_64
43  u16 ds_sel, es_sel;
44 #endif
45 };
46 
48  u32 vm_entry;
49  u32 vm_exit;
50  u32 pin;
51  u32 exec;
54 };
55 
56 /*
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).
60  */
61 struct loaded_vmcs {
62  struct vmcs *vmcs;
63  struct vmcs *shadow_vmcs;
64  int cpu;
65  bool launched;
68  /* Support for vnmi-less CPUs */
70  ktime_t entry_time;
72  unsigned long *msr_bitmap;
73  struct list_head loaded_vmcss_on_cpu_link;
76 };
77 
78 static __always_inline bool is_intr_type(u32 intr_info, u32 type)
79 {
80  const u32 mask = INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK;
81 
82  return (intr_info & mask) == (INTR_INFO_VALID_MASK | type);
83 }
84 
85 static inline bool is_intr_type_n(u32 intr_info, u32 type, u8 vector)
86 {
87  const u32 mask = INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK |
88  INTR_INFO_VECTOR_MASK;
89 
90  return (intr_info & mask) == (INTR_INFO_VALID_MASK | type | vector);
91 }
92 
93 static inline bool is_exception_n(u32 intr_info, u8 vector)
94 {
95  return is_intr_type_n(intr_info, INTR_TYPE_HARD_EXCEPTION, vector);
96 }
97 
98 static inline bool is_debug(u32 intr_info)
99 {
100  return is_exception_n(intr_info, DB_VECTOR);
101 }
102 
103 static inline bool is_breakpoint(u32 intr_info)
104 {
105  return is_exception_n(intr_info, BP_VECTOR);
106 }
107 
108 static inline bool is_double_fault(u32 intr_info)
109 {
110  return is_exception_n(intr_info, DF_VECTOR);
111 }
112 
113 static inline bool is_page_fault(u32 intr_info)
114 {
115  return is_exception_n(intr_info, PF_VECTOR);
116 }
117 
118 static inline bool is_invalid_opcode(u32 intr_info)
119 {
120  return is_exception_n(intr_info, UD_VECTOR);
121 }
122 
123 static inline bool is_gp_fault(u32 intr_info)
124 {
125  return is_exception_n(intr_info, GP_VECTOR);
126 }
127 
128 static inline bool is_alignment_check(u32 intr_info)
129 {
130  return is_exception_n(intr_info, AC_VECTOR);
131 }
132 
133 static inline bool is_machine_check(u32 intr_info)
134 {
135  return is_exception_n(intr_info, MC_VECTOR);
136 }
137 
138 static inline bool is_nm_fault(u32 intr_info)
139 {
140  return is_exception_n(intr_info, NM_VECTOR);
141 }
142 
143 /* Undocumented: icebp/int1 */
144 static inline bool is_icebp(u32 intr_info)
145 {
146  return is_intr_type(intr_info, INTR_TYPE_PRIV_SW_EXCEPTION);
147 }
148 
149 static __always_inline bool is_nmi(u32 intr_info)
150 {
151  return is_intr_type(intr_info, INTR_TYPE_NMI_INTR);
152 }
153 
154 static inline bool is_external_intr(u32 intr_info)
155 {
156  return is_intr_type(intr_info, INTR_TYPE_EXT_INTR);
157 }
158 
159 static inline bool is_exception_with_error_code(u32 intr_info)
160 {
161  const u32 mask = INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK;
162 
163  return (intr_info & mask) == mask;
164 }
165 
171 };
172 
173 static inline int vmcs_field_width(unsigned long field)
174 {
175  if (0x1 & field) /* the *_HIGH fields are all 32 bit */
176  return VMCS_FIELD_WIDTH_U32;
177  return (field >> 13) & 0x3;
178 }
179 
180 static inline int vmcs_field_readonly(unsigned long field)
181 {
182  return (((field >> 10) & 0x3) == 1);
183 }
184 
185 #define VMCS_FIELD_INDEX_SHIFT (1)
186 #define VMCS_FIELD_INDEX_MASK GENMASK(9, 1)
187 
188 static inline unsigned int vmcs_field_index(unsigned long field)
189 {
190  return (field & VMCS_FIELD_INDEX_MASK) >> VMCS_FIELD_INDEX_SHIFT;
191 }
192 
193 #endif /* __KVM_X86_VMX_VMCS_H */
struct vmcs_host_state host_state
Definition: vmcs.h:74
ktime_t entry_time
Definition: vmcs.h:70
int cpu
Definition: vmcs.h:64
bool hv_timer_soft_disabled
Definition: vmcs.h:67
unsigned long * msr_bitmap
Definition: vmcs.h:72
s64 vnmi_blocked_time
Definition: vmcs.h:71
struct vmcs * vmcs
Definition: vmcs.h:62
bool nmi_known_unmasked
Definition: vmcs.h:66
bool launched
Definition: vmcs.h:65
int soft_vnmi_blocked
Definition: vmcs.h:69
struct vmcs * shadow_vmcs
Definition: vmcs.h:63
struct list_head loaded_vmcss_on_cpu_link
Definition: vmcs.h:73
struct vmcs_controls_shadow controls_shadow
Definition: vmcs.h:75
u32 secondary_exec
Definition: vmcs.h:52
Definition: vmcs.h:16
u32 shadow_vmcs
Definition: vmcs.h:18
u32 revision_id
Definition: vmcs.h:17
u16 fs_sel
Definition: vmcs.h:41
unsigned long cr3
Definition: vmcs.h:35
unsigned long cr4
Definition: vmcs.h:36
unsigned long fs_base
Definition: vmcs.h:38
u16 ldt_sel
Definition: vmcs.h:41
unsigned long gs_base
Definition: vmcs.h:37
u16 gs_sel
Definition: vmcs.h:41
unsigned long rsp
Definition: vmcs.h:39
Definition: vmcs.h:21
struct vmcs_hdr hdr
Definition: vmcs.h:22
char data[]
Definition: vmcs.h:24
u32 abort
Definition: vmcs.h:23
static __always_inline bool is_nmi(u32 intr_info)
Definition: vmcs.h:149
DECLARE_PER_CPU(struct vmcs *, current_vmcs)
static bool is_nm_fault(u32 intr_info)
Definition: vmcs.h:138
static bool is_gp_fault(u32 intr_info)
Definition: vmcs.h:123
static bool is_page_fault(u32 intr_info)
Definition: vmcs.h:113
#define VMCS_FIELD_INDEX_SHIFT
Definition: vmcs.h:185
#define VMCS_FIELD_INDEX_MASK
Definition: vmcs.h:186
static bool is_alignment_check(u32 intr_info)
Definition: vmcs.h:128
static bool is_exception_n(u32 intr_info, u8 vector)
Definition: vmcs.h:93
static int vmcs_field_readonly(unsigned long field)
Definition: vmcs.h:180
vmcs_field_width
Definition: vmcs.h:166
@ VMCS_FIELD_WIDTH_U32
Definition: vmcs.h:169
@ VMCS_FIELD_WIDTH_U16
Definition: vmcs.h:167
@ VMCS_FIELD_WIDTH_NATURAL_WIDTH
Definition: vmcs.h:170
@ VMCS_FIELD_WIDTH_U64
Definition: vmcs.h:168
static bool is_breakpoint(u32 intr_info)
Definition: vmcs.h:103
static bool is_debug(u32 intr_info)
Definition: vmcs.h:98
static bool is_exception_with_error_code(u32 intr_info)
Definition: vmcs.h:159
static bool is_icebp(u32 intr_info)
Definition: vmcs.h:144
static __always_inline bool is_intr_type(u32 intr_info, u32 type)
Definition: vmcs.h:78
static unsigned int vmcs_field_index(unsigned long field)
Definition: vmcs.h:188
static bool is_external_intr(u32 intr_info)
Definition: vmcs.h:154
static bool is_double_fault(u32 intr_info)
Definition: vmcs.h:108
static bool is_invalid_opcode(u32 intr_info)
Definition: vmcs.h:118
static bool is_intr_type_n(u32 intr_info, u32 type, u8 vector)
Definition: vmcs.h:85
static bool is_machine_check(u32 intr_info)
Definition: vmcs.h:133