KVM
Macros | Functions | Variables
vmx_ops.h File Reference
#include <linux/nospec.h>
#include <asm/vmx.h>
#include "vmx_onhyperv.h"
#include "vmcs.h"
#include "../x86.h"
Include dependency graph for vmx_ops.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define vmx_asm1(insn, op1, error_args...)
 
#define vmx_asm2(insn, op1, op2, error_args...)
 

Functions

void vmread_error (unsigned long field)
 
void vmwrite_error (unsigned long field, unsigned long value)
 
void vmclear_error (struct vmcs *vmcs, u64 phys_addr)
 
void vmptrld_error (struct vmcs *vmcs, u64 phys_addr)
 
void invvpid_error (unsigned long ext, u16 vpid, gva_t gva)
 
void invept_error (unsigned long ext, u64 eptp, gpa_t gpa)
 
void vmread_error_trampoline2 (unsigned long field, bool fault)
 
static __always_inline void vmcs_check16 (unsigned long field)
 
static __always_inline void vmcs_check32 (unsigned long field)
 
static __always_inline void vmcs_check64 (unsigned long field)
 
static __always_inline void vmcs_checkl (unsigned long field)
 
static __always_inline unsigned long __vmcs_readl (unsigned long field)
 
static __always_inline u16 vmcs_read16 (unsigned long field)
 
static __always_inline u32 vmcs_read32 (unsigned long field)
 
static __always_inline u64 vmcs_read64 (unsigned long field)
 
static __always_inline unsigned long vmcs_readl (unsigned long field)
 
static __always_inline void __vmcs_writel (unsigned long field, unsigned long value)
 
static __always_inline void vmcs_write16 (unsigned long field, u16 value)
 
static __always_inline void vmcs_write32 (unsigned long field, u32 value)
 
static __always_inline void vmcs_write64 (unsigned long field, u64 value)
 
static __always_inline void vmcs_writel (unsigned long field, unsigned long value)
 
static __always_inline void vmcs_clear_bits (unsigned long field, u32 mask)
 
static __always_inline void vmcs_set_bits (unsigned long field, u32 mask)
 
static void vmcs_clear (struct vmcs *vmcs)
 
static void vmcs_load (struct vmcs *vmcs)
 
static void __invvpid (unsigned long ext, u16 vpid, gva_t gva)
 
static void __invept (unsigned long ext, u64 eptp, gpa_t gpa)
 
static void vpid_sync_vcpu_single (int vpid)
 
static void vpid_sync_vcpu_global (void)
 
static void vpid_sync_context (int vpid)
 
static void vpid_sync_vcpu_addr (int vpid, gva_t addr)
 
static void ept_sync_global (void)
 
static void ept_sync_context (u64 eptp)
 

Variables

unsigned long vmread_error_trampoline
 

Macro Definition Documentation

◆ vmx_asm1

#define vmx_asm1 (   insn,
  op1,
  error_args... 
)
Value:
do { \
asm goto("1: " __stringify(insn) " %0\n\t" \
".byte 0x2e\n\t" /* branch not taken hint */ \
"jna %l[error]\n\t" \
_ASM_EXTABLE(1b, %l[fault]) \
: : op1 : "cc" : error, fault); \
return; \
error: \
instrumentation_begin(); \
insn##_error(error_args); \
instrumentation_end(); \
return; \
fault: \
kvm_spurious_fault(); \
} while (0)

Definition at line 189 of file vmx_ops.h.

◆ vmx_asm2

#define vmx_asm2 (   insn,
  op1,
  op2,
  error_args... 
)
Value:
do { \
asm goto("1: " __stringify(insn) " %1, %0\n\t" \
".byte 0x2e\n\t" /* branch not taken hint */ \
"jna %l[error]\n\t" \
_ASM_EXTABLE(1b, %l[fault]) \
: : op1, op2 : "cc" : error, fault); \
return; \
error: \
instrumentation_begin(); \
insn##_error(error_args); \
instrumentation_end(); \
return; \
fault: \
kvm_spurious_fault(); \
} while (0)

Definition at line 206 of file vmx_ops.h.

Function Documentation

◆ __invept()

static void __invept ( unsigned long  ext,
u64  eptp,
gpa_t  gpa 
)
inlinestatic

Definition at line 315 of file vmx_ops.h.

316 {
317  struct {
318  u64 eptp, gpa;
319  } operand = {eptp, gpa};
320 
321  vmx_asm2(invept, "r"(ext), "m"(operand), ext, eptp, gpa);
322 }
#define vmx_asm2(insn, op1, op2, error_args...)
Definition: vmx_ops.h:206
Here is the caller graph for this function:

◆ __invvpid()

static void __invvpid ( unsigned long  ext,
u16  vpid,
gva_t  gva 
)
inlinestatic

Definition at line 304 of file vmx_ops.h.

305 {
306  struct {
307  u64 vpid : 16;
308  u64 rsvd : 48;
309  u64 gva;
310  } operand = { vpid, 0, gva };
311 
312  vmx_asm2(invvpid, "r"(ext), "m"(operand), ext, vpid, gva);
313 }
u32 rsvd[6]
Definition: posted_intr.h:18
Here is the caller graph for this function:

◆ __vmcs_readl()

static __always_inline unsigned long __vmcs_readl ( unsigned long  field)
static

Definition at line 91 of file vmx_ops.h.

92 {
93  unsigned long value;
94 
95 #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
96 
97  asm_goto_output("1: vmread %[field], %[output]\n\t"
98  "jna %l[do_fail]\n\t"
99 
100  _ASM_EXTABLE(1b, %l[do_exception])
101 
102  : [output] "=r" (value)
103  : [field] "r" (field)
104  : "cc"
105  : do_fail, do_exception);
106 
107  return value;
108 
109 do_fail:
110  instrumentation_begin();
111  vmread_error(field);
112  instrumentation_end();
113  return 0;
114 
115 do_exception:
117  return 0;
118 
119 #else /* !CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
120 
121  asm volatile("1: vmread %2, %1\n\t"
122  ".byte 0x3e\n\t" /* branch taken hint */
123  "ja 3f\n\t"
124 
125  /*
126  * VMREAD failed. Push '0' for @fault, push the failing
127  * @field, and bounce through the trampoline to preserve
128  * volatile registers.
129  */
130  "xorl %k1, %k1\n\t"
131  "2:\n\t"
132  "push %1\n\t"
133  "push %2\n\t"
134  "call vmread_error_trampoline\n\t"
135 
136  /*
137  * Unwind the stack. Note, the trampoline zeros out the
138  * memory for @fault so that the result is '0' on error.
139  */
140  "pop %2\n\t"
141  "pop %1\n\t"
142  "3:\n\t"
143 
144  /* VMREAD faulted. As above, except push '1' for @fault. */
145  _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_ONE_REG, %1)
146 
147  : ASM_CALL_CONSTRAINT, "=&r"(value) : "r"(field) : "cc");
148  return value;
149 
150 #endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
151 }
void vmread_error(unsigned long field)
Definition: vmx.c:434
noinstr void kvm_spurious_fault(void)
Definition: x86.c:513
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __vmcs_writel()

static __always_inline void __vmcs_writel ( unsigned long  field,
unsigned long  value 
)
static

Definition at line 223 of file vmx_ops.h.

224 {
225  vmx_asm2(vmwrite, "r"(field), "rm"(value), field, value);
226 }
Here is the caller graph for this function:

◆ ept_sync_context()

static void ept_sync_context ( u64  eptp)
inlinestatic

Definition at line 361 of file vmx_ops.h.

362 {
364  __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
365  else
366  ept_sync_global();
367 }
static bool cpu_has_vmx_invept_context(void)
Definition: capabilities.h:338
static void ept_sync_global(void)
Definition: vmx_ops.h:356
static void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
Definition: vmx_ops.h:315
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ept_sync_global()

static void ept_sync_global ( void  )
inlinestatic

Definition at line 356 of file vmx_ops.h.

357 {
358  __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
359 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ invept_error()

void invept_error ( unsigned long  ext,
u64  eptp,
gpa_t  gpa 
)

Definition at line 476 of file vmx.c.

477 {
478  vmx_insn_failed("invept failed: ext=0x%lx eptp=%llx gpa=0x%llx\n",
479  ext, eptp, gpa);
480 }
#define vmx_insn_failed(fmt...)
Definition: vmx.c:428

◆ invvpid_error()

void invvpid_error ( unsigned long  ext,
u16  vpid,
gva_t  gva 
)

Definition at line 470 of file vmx.c.

471 {
472  vmx_insn_failed("invvpid failed: ext=0x%lx vpid=%u gva=0x%lx\n",
473  ext, vpid, gva);
474 }

◆ vmclear_error()

void vmclear_error ( struct vmcs vmcs,
u64  phys_addr 
)

Definition at line 458 of file vmx.c.

459 {
460  vmx_insn_failed("vmclear failed: %p/%llx err=%u\n",
461  vmcs, phys_addr, vmcs_read32(VM_INSTRUCTION_ERROR));
462 }
Definition: vmcs.h:21
static __always_inline u32 vmcs_read32(unsigned long field)
Definition: vmx_ops.h:161
Here is the call graph for this function:

◆ vmcs_check16()

static __always_inline void vmcs_check16 ( unsigned long  field)
static

Definition at line 43 of file vmx_ops.h.

44 {
45  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
46  "16-bit accessor invalid for 64-bit field");
47  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
48  "16-bit accessor invalid for 64-bit high field");
49  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
50  "16-bit accessor invalid for 32-bit high field");
51  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
52  "16-bit accessor invalid for natural width field");
53 }
Here is the caller graph for this function:

◆ vmcs_check32()

static __always_inline void vmcs_check32 ( unsigned long  field)
static

Definition at line 55 of file vmx_ops.h.

56 {
57  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
58  "32-bit accessor invalid for 16-bit field");
59  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
60  "32-bit accessor invalid for 64-bit field");
61  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
62  "32-bit accessor invalid for 64-bit high field");
63  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
64  "32-bit accessor invalid for natural width field");
65 }
Here is the caller graph for this function:

◆ vmcs_check64()

static __always_inline void vmcs_check64 ( unsigned long  field)
static

Definition at line 67 of file vmx_ops.h.

68 {
69  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
70  "64-bit accessor invalid for 16-bit field");
71  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
72  "64-bit accessor invalid for 64-bit high field");
73  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
74  "64-bit accessor invalid for 32-bit field");
75  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
76  "64-bit accessor invalid for natural width field");
77 }
Here is the caller graph for this function:

◆ vmcs_checkl()

static __always_inline void vmcs_checkl ( unsigned long  field)
static

Definition at line 79 of file vmx_ops.h.

80 {
81  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
82  "Natural width accessor invalid for 16-bit field");
83  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
84  "Natural width accessor invalid for 64-bit field");
85  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
86  "Natural width accessor invalid for 64-bit high field");
87  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
88  "Natural width accessor invalid for 32-bit field");
89 }
Here is the caller graph for this function:

◆ vmcs_clear()

static void vmcs_clear ( struct vmcs vmcs)
inlinestatic

Definition at line 287 of file vmx_ops.h.

288 {
289  u64 phys_addr = __pa(vmcs);
290 
291  vmx_asm1(vmclear, "m"(phys_addr), vmcs, phys_addr);
292 }
#define vmx_asm1(insn, op1, error_args...)
Definition: vmx_ops.h:189
Here is the caller graph for this function:

◆ vmcs_clear_bits()

static __always_inline void vmcs_clear_bits ( unsigned long  field,
u32  mask 
)
static

Definition at line 267 of file vmx_ops.h.

268 {
269  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
270  "vmcs_clear_bits does not support 64-bit fields");
271  if (kvm_is_using_evmcs())
272  return evmcs_write32(field, evmcs_read32(field) & ~mask);
273 
274  __vmcs_writel(field, __vmcs_readl(field) & ~mask);
275 }
static __always_inline void evmcs_write32(unsigned long field, u32 value)
Definition: vmx_onhyperv.h:117
static __always_inline bool kvm_is_using_evmcs(void)
Definition: vmx_onhyperv.h:115
static __always_inline u32 evmcs_read32(unsigned long field)
Definition: vmx_onhyperv.h:120
static __always_inline unsigned long __vmcs_readl(unsigned long field)
Definition: vmx_ops.h:91
static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Definition: vmx_ops.h:223
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmcs_load()

static void vmcs_load ( struct vmcs vmcs)
inlinestatic

Definition at line 294 of file vmx_ops.h.

295 {
296  u64 phys_addr = __pa(vmcs);
297 
298  if (kvm_is_using_evmcs())
299  return evmcs_load(phys_addr);
300 
301  vmx_asm1(vmptrld, "m"(phys_addr), vmcs, phys_addr);
302 }
static void evmcs_load(u64 phys_addr)
Definition: vmx_onhyperv.h:122
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmcs_read16()

static __always_inline u16 vmcs_read16 ( unsigned long  field)
static

Definition at line 153 of file vmx_ops.h.

154 {
155  vmcs_check16(field);
156  if (kvm_is_using_evmcs())
157  return evmcs_read16(field);
158  return __vmcs_readl(field);
159 }
static __always_inline u16 evmcs_read16(unsigned long field)
Definition: vmx_onhyperv.h:121
static __always_inline void vmcs_check16(unsigned long field)
Definition: vmx_ops.h:43
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmcs_read32()

static __always_inline u32 vmcs_read32 ( unsigned long  field)
static

Definition at line 161 of file vmx_ops.h.

162 {
163  vmcs_check32(field);
164  if (kvm_is_using_evmcs())
165  return evmcs_read32(field);
166  return __vmcs_readl(field);
167 }
static __always_inline void vmcs_check32(unsigned long field)
Definition: vmx_ops.h:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmcs_read64()

static __always_inline u64 vmcs_read64 ( unsigned long  field)
static

Definition at line 169 of file vmx_ops.h.

170 {
171  vmcs_check64(field);
172  if (kvm_is_using_evmcs())
173  return evmcs_read64(field);
174 #ifdef CONFIG_X86_64
175  return __vmcs_readl(field);
176 #else
177  return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
178 #endif
179 }
static __always_inline u64 evmcs_read64(unsigned long field)
Definition: vmx_onhyperv.h:119
static __always_inline void vmcs_check64(unsigned long field)
Definition: vmx_ops.h:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmcs_readl()

static __always_inline unsigned long vmcs_readl ( unsigned long  field)
static

Definition at line 181 of file vmx_ops.h.

182 {
183  vmcs_checkl(field);
184  if (kvm_is_using_evmcs())
185  return evmcs_read64(field);
186  return __vmcs_readl(field);
187 }
static __always_inline void vmcs_checkl(unsigned long field)
Definition: vmx_ops.h:79
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmcs_set_bits()

static __always_inline void vmcs_set_bits ( unsigned long  field,
u32  mask 
)
static

Definition at line 277 of file vmx_ops.h.

278 {
279  BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
280  "vmcs_set_bits does not support 64-bit fields");
281  if (kvm_is_using_evmcs())
282  return evmcs_write32(field, evmcs_read32(field) | mask);
283 
284  __vmcs_writel(field, __vmcs_readl(field) | mask);
285 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmcs_write16()

static __always_inline void vmcs_write16 ( unsigned long  field,
u16  value 
)
static

Definition at line 228 of file vmx_ops.h.

229 {
230  vmcs_check16(field);
231  if (kvm_is_using_evmcs())
232  return evmcs_write16(field, value);
233 
234  __vmcs_writel(field, value);
235 }
static __always_inline void evmcs_write16(unsigned long field, u16 value)
Definition: vmx_onhyperv.h:118
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmcs_write32()

static __always_inline void vmcs_write32 ( unsigned long  field,
u32  value 
)
static

Definition at line 237 of file vmx_ops.h.

238 {
239  vmcs_check32(field);
240  if (kvm_is_using_evmcs())
241  return evmcs_write32(field, value);
242 
243  __vmcs_writel(field, value);
244 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmcs_write64()

static __always_inline void vmcs_write64 ( unsigned long  field,
u64  value 
)
static

Definition at line 246 of file vmx_ops.h.

247 {
248  vmcs_check64(field);
249  if (kvm_is_using_evmcs())
250  return evmcs_write64(field, value);
251 
252  __vmcs_writel(field, value);
253 #ifndef CONFIG_X86_64
254  __vmcs_writel(field+1, value >> 32);
255 #endif
256 }
static __always_inline void evmcs_write64(unsigned long field, u64 value)
Definition: vmx_onhyperv.h:116
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmcs_writel()

static __always_inline void vmcs_writel ( unsigned long  field,
unsigned long  value 
)
static

Definition at line 258 of file vmx_ops.h.

259 {
260  vmcs_checkl(field);
261  if (kvm_is_using_evmcs())
262  return evmcs_write64(field, value);
263 
264  __vmcs_writel(field, value);
265 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmptrld_error()

void vmptrld_error ( struct vmcs vmcs,
u64  phys_addr 
)

Definition at line 464 of file vmx.c.

465 {
466  vmx_insn_failed("vmptrld failed: %p/%llx err=%u\n",
467  vmcs, phys_addr, vmcs_read32(VM_INSTRUCTION_ERROR));
468 }
Here is the call graph for this function:

◆ vmread_error()

void vmread_error ( unsigned long  field)

Definition at line 434 of file vmx.c.

435 {
436  vmx_insn_failed("vmread failed: field=%lx\n", field);
437 }
Here is the caller graph for this function:

◆ vmread_error_trampoline2()

void vmread_error_trampoline2 ( unsigned long  field,
bool  fault 
)

Definition at line 440 of file vmx.c.

441 {
442  if (fault) {
444  } else {
445  instrumentation_begin();
446  vmread_error(field);
447  instrumentation_end();
448  }
449 }
noinline void vmread_error(unsigned long field)
Definition: vmx.c:434
Here is the call graph for this function:

◆ vmwrite_error()

void vmwrite_error ( unsigned long  field,
unsigned long  value 
)

Definition at line 452 of file vmx.c.

453 {
454  vmx_insn_failed("vmwrite failed: field=%lx val=%lx err=%u\n",
455  field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
456 }
Here is the call graph for this function:

◆ vpid_sync_context()

static void vpid_sync_context ( int  vpid)
inlinestatic

Definition at line 337 of file vmx_ops.h.

338 {
340  vpid_sync_vcpu_single(vpid);
341  else if (vpid != 0)
343 }
static bool cpu_has_vmx_invvpid_single(void)
Definition: capabilities.h:358
static void vpid_sync_vcpu_global(void)
Definition: vmx_ops.h:332
static void vpid_sync_vcpu_single(int vpid)
Definition: vmx_ops.h:324
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vpid_sync_vcpu_addr()

static void vpid_sync_vcpu_addr ( int  vpid,
gva_t  addr 
)
inlinestatic

Definition at line 345 of file vmx_ops.h.

346 {
347  if (vpid == 0)
348  return;
349 
351  __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
352  else
353  vpid_sync_context(vpid);
354 }
static bool cpu_has_vmx_invvpid_individual_addr(void)
Definition: capabilities.h:353
static void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
Definition: vmx_ops.h:304
static void vpid_sync_context(int vpid)
Definition: vmx_ops.h:337
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vpid_sync_vcpu_global()

static void vpid_sync_vcpu_global ( void  )
inlinestatic

Definition at line 332 of file vmx_ops.h.

333 {
334  __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
335 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vpid_sync_vcpu_single()

static void vpid_sync_vcpu_single ( int  vpid)
inlinestatic

Definition at line 324 of file vmx_ops.h.

325 {
326  if (vpid == 0)
327  return;
328 
329  __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
330 }
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ vmread_error_trampoline

unsigned long vmread_error_trampoline
extern