blob: 5c0ff80b85c07467b9794e050b98829eceb52782 [file] [log] [blame]
Sean Christophersoncb1d4742018-12-03 13:53:04 -08001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __KVM_X86_VMX_VMCS_H
3#define __KVM_X86_VMX_VMCS_H
4
Sean Christophersoncb1d4742018-12-03 13:53:04 -08005#include <linux/ktime.h>
Sean Christopherson609363c2018-12-03 13:53:05 -08006#include <linux/list.h>
7#include <linux/nospec.h>
Sean Christophersoncb1d4742018-12-03 13:53:04 -08008
Sean Christopherson609363c2018-12-03 13:53:05 -08009#include <asm/kvm.h>
Sean Christophersoncb1d4742018-12-03 13:53:04 -080010#include <asm/vmx.h>
11
12#include "capabilities.h"
13
14struct vmcs_hdr {
15 u32 revision_id:31;
16 u32 shadow_vmcs:1;
17};
18
19struct vmcs {
20 struct vmcs_hdr hdr;
21 u32 abort;
Gustavo A. R. Silvaf4a9fdd2020-05-07 13:56:18 -050022 char data[];
Sean Christophersoncb1d4742018-12-03 13:53:04 -080023};
24
Sean Christopherson75edce82018-12-03 13:53:06 -080025DECLARE_PER_CPU(struct vmcs *, current_vmcs);
26
Sean Christophersoncb1d4742018-12-03 13:53:04 -080027/*
28 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
29 * and whose values change infrequently, but are not constant. I.e. this is
30 * used as a write-through cache of the corresponding VMCS fields.
31 */
32struct vmcs_host_state {
33 unsigned long cr3; /* May not match real cr3 */
34 unsigned long cr4; /* May not match real cr4 */
35 unsigned long gs_base;
36 unsigned long fs_base;
Sean Christopherson5a878162019-01-25 07:41:02 -080037 unsigned long rsp;
Sean Christophersoncb1d4742018-12-03 13:53:04 -080038
39 u16 fs_sel, gs_sel, ldt_sel;
40#ifdef CONFIG_X86_64
41 u16 ds_sel, es_sel;
42#endif
43};
44
Sean Christopherson09e226c2019-05-07 12:17:58 -070045struct vmcs_controls_shadow {
46 u32 vm_entry;
47 u32 vm_exit;
48 u32 pin;
49 u32 exec;
50 u32 secondary_exec;
51};
52
Sean Christophersoncb1d4742018-12-03 13:53:04 -080053/*
54 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
55 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
56 * loaded on this CPU (so we can clear them if the CPU goes down).
57 */
58struct loaded_vmcs {
59 struct vmcs *vmcs;
60 struct vmcs *shadow_vmcs;
61 int cpu;
62 bool launched;
63 bool nmi_known_unmasked;
Sean Christopherson804939e2019-05-07 12:18:05 -070064 bool hv_timer_soft_disabled;
Sean Christophersoncb1d4742018-12-03 13:53:04 -080065 /* Support for vnmi-less CPUs */
66 int soft_vnmi_blocked;
67 ktime_t entry_time;
68 s64 vnmi_blocked_time;
69 unsigned long *msr_bitmap;
70 struct list_head loaded_vmcss_on_cpu_link;
71 struct vmcs_host_state host_state;
Sean Christopherson09e226c2019-05-07 12:17:58 -070072 struct vmcs_controls_shadow controls_shadow;
Sean Christophersoncb1d4742018-12-03 13:53:04 -080073};
74
75static inline bool is_exception_n(u32 intr_info, u8 vector)
76{
77 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
78 INTR_INFO_VALID_MASK)) ==
79 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
80}
81
82static inline bool is_debug(u32 intr_info)
83{
84 return is_exception_n(intr_info, DB_VECTOR);
85}
86
87static inline bool is_breakpoint(u32 intr_info)
88{
89 return is_exception_n(intr_info, BP_VECTOR);
90}
91
92static inline bool is_page_fault(u32 intr_info)
93{
94 return is_exception_n(intr_info, PF_VECTOR);
95}
96
97static inline bool is_invalid_opcode(u32 intr_info)
98{
99 return is_exception_n(intr_info, UD_VECTOR);
100}
101
102static inline bool is_gp_fault(u32 intr_info)
103{
104 return is_exception_n(intr_info, GP_VECTOR);
105}
106
107static inline bool is_machine_check(u32 intr_info)
108{
109 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
110 INTR_INFO_VALID_MASK)) ==
111 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
112}
113
114/* Undocumented: icebp/int1 */
115static inline bool is_icebp(u32 intr_info)
116{
117 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
118 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
119}
120
121static inline bool is_nmi(u32 intr_info)
122{
123 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
124 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
125}
126
Sean Christopherson49def502019-04-19 22:50:56 -0700127static inline bool is_external_intr(u32 intr_info)
128{
129 return (intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
130 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR);
131}
132
Sean Christophersoncb1d4742018-12-03 13:53:04 -0800133enum vmcs_field_width {
134 VMCS_FIELD_WIDTH_U16 = 0,
135 VMCS_FIELD_WIDTH_U64 = 1,
136 VMCS_FIELD_WIDTH_U32 = 2,
137 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
138};
139
140static inline int vmcs_field_width(unsigned long field)
141{
142 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
143 return VMCS_FIELD_WIDTH_U32;
144 return (field >> 13) & 0x3;
145}
146
147static inline int vmcs_field_readonly(unsigned long field)
148{
149 return (((field >> 10) & 0x3) == 1);
150}
151
152#endif /* __KVM_X86_VMX_VMCS_H */