Mention branches and keyring.
[releases.git] / x86 / kvm / vmx / evmcs.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_VMX_EVMCS_H
3 #define __KVM_X86_VMX_EVMCS_H
4
5 #include <linux/jump_label.h>
6
7 #include <asm/hyperv-tlfs.h>
8 #include <asm/mshyperv.h>
9 #include <asm/vmx.h>
10
11 #include "capabilities.h"
12 #include "vmcs.h"
13 #include "vmcs12.h"
14
15 struct vmcs_config;
16
17 DECLARE_STATIC_KEY_FALSE(enable_evmcs);
18
19 #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
20
21 #define KVM_EVMCS_VERSION 1
22
23 /*
24  * Enlightened VMCSv1 doesn't support these:
25  *
26  *      POSTED_INTR_NV                  = 0x00000002,
27  *      GUEST_INTR_STATUS               = 0x00000810,
28  *      APIC_ACCESS_ADDR                = 0x00002014,
29  *      POSTED_INTR_DESC_ADDR           = 0x00002016,
30  *      EOI_EXIT_BITMAP0                = 0x0000201c,
31  *      EOI_EXIT_BITMAP1                = 0x0000201e,
32  *      EOI_EXIT_BITMAP2                = 0x00002020,
33  *      EOI_EXIT_BITMAP3                = 0x00002022,
34  *      GUEST_PML_INDEX                 = 0x00000812,
35  *      PML_ADDRESS                     = 0x0000200e,
36  *      VM_FUNCTION_CONTROL             = 0x00002018,
37  *      EPTP_LIST_ADDRESS               = 0x00002024,
38  *      VMREAD_BITMAP                   = 0x00002026,
39  *      VMWRITE_BITMAP                  = 0x00002028,
40  *
41  *      TSC_MULTIPLIER                  = 0x00002032,
42  *      PLE_GAP                         = 0x00004020,
43  *      PLE_WINDOW                      = 0x00004022,
44  *      VMX_PREEMPTION_TIMER_VALUE      = 0x0000482E,
45  *
46  * Currently unsupported in KVM:
47  *      GUEST_IA32_RTIT_CTL             = 0x00002814,
48  */
49 #define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
50                                     PIN_BASED_VMX_PREEMPTION_TIMER)
51 #define EVMCS1_UNSUPPORTED_EXEC_CTRL (CPU_BASED_ACTIVATE_TERTIARY_CONTROLS)
52 #define EVMCS1_UNSUPPORTED_2NDEXEC                                      \
53         (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |                         \
54          SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |                      \
55          SECONDARY_EXEC_APIC_REGISTER_VIRT |                            \
56          SECONDARY_EXEC_ENABLE_PML |                                    \
57          SECONDARY_EXEC_ENABLE_VMFUNC |                                 \
58          SECONDARY_EXEC_SHADOW_VMCS |                                   \
59          SECONDARY_EXEC_TSC_SCALING |                                   \
60          SECONDARY_EXEC_PAUSE_LOOP_EXITING)
61 #define EVMCS1_UNSUPPORTED_VMEXIT_CTRL                                  \
62         (VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
63 #define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (0)
64 #define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
65
66 struct evmcs_field {
67         u16 offset;
68         u16 clean_field;
69 };
70
71 extern const struct evmcs_field vmcs_field_to_evmcs_1[];
72 extern const unsigned int nr_evmcs_1_fields;
73
74 static __always_inline int evmcs_field_offset(unsigned long field,
75                                               u16 *clean_field)
76 {
77         unsigned int index = ROL16(field, 6);
78         const struct evmcs_field *evmcs_field;
79
80         if (unlikely(index >= nr_evmcs_1_fields))
81                 return -ENOENT;
82
83         evmcs_field = &vmcs_field_to_evmcs_1[index];
84
85         /*
86          * Use offset=0 to detect holes in eVMCS. This offset belongs to
87          * 'revision_id' but this field has no encoding and is supposed to
88          * be accessed directly.
89          */
90         if (unlikely(!evmcs_field->offset))
91                 return -ENOENT;
92
93         if (clean_field)
94                 *clean_field = evmcs_field->clean_field;
95
96         return evmcs_field->offset;
97 }
98
99 static inline u64 evmcs_read_any(struct hv_enlightened_vmcs *evmcs,
100                                  unsigned long field, u16 offset)
101 {
102         /*
103          * vmcs12_read_any() doesn't care whether the supplied structure
104          * is 'struct vmcs12' or 'struct hv_enlightened_vmcs' as it takes
105          * the exact offset of the required field, use it for convenience
106          * here.
107          */
108         return vmcs12_read_any((void *)evmcs, field, offset);
109 }
110
111 #if IS_ENABLED(CONFIG_HYPERV)
112
113 static __always_inline int get_evmcs_offset(unsigned long field,
114                                             u16 *clean_field)
115 {
116         int offset = evmcs_field_offset(field, clean_field);
117
118         WARN_ONCE(offset < 0, "KVM: accessing unsupported EVMCS field %lx\n",
119                   field);
120
121         return offset;
122 }
123
124 static __always_inline void evmcs_write64(unsigned long field, u64 value)
125 {
126         u16 clean_field;
127         int offset = get_evmcs_offset(field, &clean_field);
128
129         if (offset < 0)
130                 return;
131
132         *(u64 *)((char *)current_evmcs + offset) = value;
133
134         current_evmcs->hv_clean_fields &= ~clean_field;
135 }
136
137 static inline void evmcs_write32(unsigned long field, u32 value)
138 {
139         u16 clean_field;
140         int offset = get_evmcs_offset(field, &clean_field);
141
142         if (offset < 0)
143                 return;
144
145         *(u32 *)((char *)current_evmcs + offset) = value;
146         current_evmcs->hv_clean_fields &= ~clean_field;
147 }
148
149 static inline void evmcs_write16(unsigned long field, u16 value)
150 {
151         u16 clean_field;
152         int offset = get_evmcs_offset(field, &clean_field);
153
154         if (offset < 0)
155                 return;
156
157         *(u16 *)((char *)current_evmcs + offset) = value;
158         current_evmcs->hv_clean_fields &= ~clean_field;
159 }
160
161 static inline u64 evmcs_read64(unsigned long field)
162 {
163         int offset = get_evmcs_offset(field, NULL);
164
165         if (offset < 0)
166                 return 0;
167
168         return *(u64 *)((char *)current_evmcs + offset);
169 }
170
171 static inline u32 evmcs_read32(unsigned long field)
172 {
173         int offset = get_evmcs_offset(field, NULL);
174
175         if (offset < 0)
176                 return 0;
177
178         return *(u32 *)((char *)current_evmcs + offset);
179 }
180
181 static inline u16 evmcs_read16(unsigned long field)
182 {
183         int offset = get_evmcs_offset(field, NULL);
184
185         if (offset < 0)
186                 return 0;
187
188         return *(u16 *)((char *)current_evmcs + offset);
189 }
190
191 static inline void evmcs_load(u64 phys_addr)
192 {
193         struct hv_vp_assist_page *vp_ap =
194                 hv_get_vp_assist_page(smp_processor_id());
195
196         if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall)
197                 vp_ap->nested_control.features.directhypercall = 1;
198         vp_ap->current_nested_vmcs = phys_addr;
199         vp_ap->enlighten_vmentry = 1;
200 }
201
202 #else /* !IS_ENABLED(CONFIG_HYPERV) */
203 static __always_inline void evmcs_write64(unsigned long field, u64 value) {}
204 static inline void evmcs_write32(unsigned long field, u32 value) {}
205 static inline void evmcs_write16(unsigned long field, u16 value) {}
206 static inline u64 evmcs_read64(unsigned long field) { return 0; }
207 static inline u32 evmcs_read32(unsigned long field) { return 0; }
208 static inline u16 evmcs_read16(unsigned long field) { return 0; }
209 static inline void evmcs_load(u64 phys_addr) {}
210 #endif /* IS_ENABLED(CONFIG_HYPERV) */
211
212 #define EVMPTR_INVALID (-1ULL)
213 #define EVMPTR_MAP_PENDING (-2ULL)
214
215 static inline bool evmptr_is_valid(u64 evmptr)
216 {
217         return evmptr != EVMPTR_INVALID && evmptr != EVMPTR_MAP_PENDING;
218 }
219
220 enum nested_evmptrld_status {
221         EVMPTRLD_DISABLED,
222         EVMPTRLD_SUCCEEDED,
223         EVMPTRLD_VMFAIL,
224         EVMPTRLD_ERROR,
225 };
226
227 bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa);
228 uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu);
229 int nested_enable_evmcs(struct kvm_vcpu *vcpu,
230                         uint16_t *vmcs_version);
231 void nested_evmcs_filter_control_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
232 int nested_evmcs_check_controls(struct vmcs12 *vmcs12);
233
234 #endif /* __KVM_X86_VMX_EVMCS_H */