GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / 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
14 struct vmcs_config;
15
16 DECLARE_STATIC_KEY_FALSE(enable_evmcs);
17
18 #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
19
20 #define KVM_EVMCS_VERSION 1
21
22 /*
23  * Enlightened VMCSv1 doesn't support these:
24  *
25  *      POSTED_INTR_NV                  = 0x00000002,
26  *      GUEST_INTR_STATUS               = 0x00000810,
27  *      APIC_ACCESS_ADDR                = 0x00002014,
28  *      POSTED_INTR_DESC_ADDR           = 0x00002016,
29  *      EOI_EXIT_BITMAP0                = 0x0000201c,
30  *      EOI_EXIT_BITMAP1                = 0x0000201e,
31  *      EOI_EXIT_BITMAP2                = 0x00002020,
32  *      EOI_EXIT_BITMAP3                = 0x00002022,
33  *      GUEST_PML_INDEX                 = 0x00000812,
34  *      PML_ADDRESS                     = 0x0000200e,
35  *      VM_FUNCTION_CONTROL             = 0x00002018,
36  *      EPTP_LIST_ADDRESS               = 0x00002024,
37  *      VMREAD_BITMAP                   = 0x00002026,
38  *      VMWRITE_BITMAP                  = 0x00002028,
39  *
40  *      TSC_MULTIPLIER                  = 0x00002032,
41  *      PLE_GAP                         = 0x00004020,
42  *      PLE_WINDOW                      = 0x00004022,
43  *      VMX_PREEMPTION_TIMER_VALUE      = 0x0000482E,
44  *      GUEST_IA32_PERF_GLOBAL_CTRL     = 0x00002808,
45  *      HOST_IA32_PERF_GLOBAL_CTRL      = 0x00002c04,
46  *
47  * Currently unsupported in KVM:
48  *      GUEST_IA32_RTIT_CTL             = 0x00002814,
49  */
50 #define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
51                                     PIN_BASED_VMX_PREEMPTION_TIMER)
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_LOAD_IA32_PERF_GLOBAL_CTRL |                           \
63          VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
64 #define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
65 #define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
66
67 #if IS_ENABLED(CONFIG_HYPERV)
68
69 struct evmcs_field {
70         u16 offset;
71         u16 clean_field;
72 };
73
74 extern const struct evmcs_field vmcs_field_to_evmcs_1[];
75 extern const unsigned int nr_evmcs_1_fields;
76
77 #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
78
79 static __always_inline int get_evmcs_offset(unsigned long field,
80                                             u16 *clean_field)
81 {
82         unsigned int index = ROL16(field, 6);
83         const struct evmcs_field *evmcs_field;
84
85         if (unlikely(index >= nr_evmcs_1_fields)) {
86                 WARN_ONCE(1, "KVM: accessing unsupported EVMCS field %lx\n",
87                           field);
88                 return -ENOENT;
89         }
90
91         evmcs_field = &vmcs_field_to_evmcs_1[index];
92
93         if (clean_field)
94                 *clean_field = evmcs_field->clean_field;
95
96         return evmcs_field->offset;
97 }
98
99 #undef ROL16
100
101 static inline void evmcs_write64(unsigned long field, u64 value)
102 {
103         u16 clean_field;
104         int offset = get_evmcs_offset(field, &clean_field);
105
106         if (offset < 0)
107                 return;
108
109         *(u64 *)((char *)current_evmcs + offset) = value;
110
111         current_evmcs->hv_clean_fields &= ~clean_field;
112 }
113
114 static inline void evmcs_write32(unsigned long field, u32 value)
115 {
116         u16 clean_field;
117         int offset = get_evmcs_offset(field, &clean_field);
118
119         if (offset < 0)
120                 return;
121
122         *(u32 *)((char *)current_evmcs + offset) = value;
123         current_evmcs->hv_clean_fields &= ~clean_field;
124 }
125
126 static inline void evmcs_write16(unsigned long field, u16 value)
127 {
128         u16 clean_field;
129         int offset = get_evmcs_offset(field, &clean_field);
130
131         if (offset < 0)
132                 return;
133
134         *(u16 *)((char *)current_evmcs + offset) = value;
135         current_evmcs->hv_clean_fields &= ~clean_field;
136 }
137
138 static inline u64 evmcs_read64(unsigned long field)
139 {
140         int offset = get_evmcs_offset(field, NULL);
141
142         if (offset < 0)
143                 return 0;
144
145         return *(u64 *)((char *)current_evmcs + offset);
146 }
147
148 static inline u32 evmcs_read32(unsigned long field)
149 {
150         int offset = get_evmcs_offset(field, NULL);
151
152         if (offset < 0)
153                 return 0;
154
155         return *(u32 *)((char *)current_evmcs + offset);
156 }
157
158 static inline u16 evmcs_read16(unsigned long field)
159 {
160         int offset = get_evmcs_offset(field, NULL);
161
162         if (offset < 0)
163                 return 0;
164
165         return *(u16 *)((char *)current_evmcs + offset);
166 }
167
168 static inline void evmcs_touch_msr_bitmap(void)
169 {
170         if (unlikely(!current_evmcs))
171                 return;
172
173         if (current_evmcs->hv_enlightenments_control.msr_bitmap)
174                 current_evmcs->hv_clean_fields &=
175                         ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
176 }
177
178 static inline void evmcs_load(u64 phys_addr)
179 {
180         struct hv_vp_assist_page *vp_ap =
181                 hv_get_vp_assist_page(smp_processor_id());
182
183         if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall)
184                 vp_ap->nested_control.features.directhypercall = 1;
185         vp_ap->current_nested_vmcs = phys_addr;
186         vp_ap->enlighten_vmentry = 1;
187 }
188
189 void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf);
190 #else /* !IS_ENABLED(CONFIG_HYPERV) */
191 static inline void evmcs_write64(unsigned long field, u64 value) {}
192 static inline void evmcs_write32(unsigned long field, u32 value) {}
193 static inline void evmcs_write16(unsigned long field, u16 value) {}
194 static inline u64 evmcs_read64(unsigned long field) { return 0; }
195 static inline u32 evmcs_read32(unsigned long field) { return 0; }
196 static inline u16 evmcs_read16(unsigned long field) { return 0; }
197 static inline void evmcs_load(u64 phys_addr) {}
198 static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
199 static inline void evmcs_touch_msr_bitmap(void) {}
200 #endif /* IS_ENABLED(CONFIG_HYPERV) */
201
202 bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa);
203 uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu);
204 int nested_enable_evmcs(struct kvm_vcpu *vcpu,
205                         uint16_t *vmcs_version);
206
207 #endif /* __KVM_X86_VMX_EVMCS_H */