blob: 011929a63823084e21e2dc5575e1567bbc478ee7 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* 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"
Olivier Deprez157378f2022-04-04 15:47:50 +020013#include "vmcs12.h"
David Brazdil0f672f62019-12-10 10:32:29 +000014
15struct vmcs_config;
16
17DECLARE_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 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
46 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
47 *
48 * Currently unsupported in KVM:
49 * GUEST_IA32_RTIT_CTL = 0x00002814,
50 */
51#define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
52 PIN_BASED_VMX_PREEMPTION_TIMER)
53#define EVMCS1_UNSUPPORTED_2NDEXEC \
54 (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \
55 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \
56 SECONDARY_EXEC_APIC_REGISTER_VIRT | \
57 SECONDARY_EXEC_ENABLE_PML | \
58 SECONDARY_EXEC_ENABLE_VMFUNC | \
59 SECONDARY_EXEC_SHADOW_VMCS | \
60 SECONDARY_EXEC_TSC_SCALING | \
61 SECONDARY_EXEC_PAUSE_LOOP_EXITING)
Olivier Deprez157378f2022-04-04 15:47:50 +020062#define EVMCS1_UNSUPPORTED_VMEXIT_CTRL \
63 (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | \
64 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
David Brazdil0f672f62019-12-10 10:32:29 +000065#define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
66#define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
67
68#if IS_ENABLED(CONFIG_HYPERV)
69
70struct evmcs_field {
71 u16 offset;
72 u16 clean_field;
73};
74
75extern const struct evmcs_field vmcs_field_to_evmcs_1[];
76extern const unsigned int nr_evmcs_1_fields;
77
78#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
79
80static __always_inline int get_evmcs_offset(unsigned long field,
81 u16 *clean_field)
82{
83 unsigned int index = ROL16(field, 6);
84 const struct evmcs_field *evmcs_field;
85
86 if (unlikely(index >= nr_evmcs_1_fields)) {
87 WARN_ONCE(1, "KVM: accessing unsupported EVMCS field %lx\n",
88 field);
89 return -ENOENT;
90 }
91
92 evmcs_field = &vmcs_field_to_evmcs_1[index];
93
94 if (clean_field)
95 *clean_field = evmcs_field->clean_field;
96
97 return evmcs_field->offset;
98}
99
100#undef ROL16
101
102static inline void evmcs_write64(unsigned long field, u64 value)
103{
104 u16 clean_field;
105 int offset = get_evmcs_offset(field, &clean_field);
106
107 if (offset < 0)
108 return;
109
110 *(u64 *)((char *)current_evmcs + offset) = value;
111
112 current_evmcs->hv_clean_fields &= ~clean_field;
113}
114
115static inline void evmcs_write32(unsigned long field, u32 value)
116{
117 u16 clean_field;
118 int offset = get_evmcs_offset(field, &clean_field);
119
120 if (offset < 0)
121 return;
122
123 *(u32 *)((char *)current_evmcs + offset) = value;
124 current_evmcs->hv_clean_fields &= ~clean_field;
125}
126
127static inline void evmcs_write16(unsigned long field, u16 value)
128{
129 u16 clean_field;
130 int offset = get_evmcs_offset(field, &clean_field);
131
132 if (offset < 0)
133 return;
134
135 *(u16 *)((char *)current_evmcs + offset) = value;
136 current_evmcs->hv_clean_fields &= ~clean_field;
137}
138
139static inline u64 evmcs_read64(unsigned long field)
140{
141 int offset = get_evmcs_offset(field, NULL);
142
143 if (offset < 0)
144 return 0;
145
146 return *(u64 *)((char *)current_evmcs + offset);
147}
148
149static inline u32 evmcs_read32(unsigned long field)
150{
151 int offset = get_evmcs_offset(field, NULL);
152
153 if (offset < 0)
154 return 0;
155
156 return *(u32 *)((char *)current_evmcs + offset);
157}
158
159static inline u16 evmcs_read16(unsigned long field)
160{
161 int offset = get_evmcs_offset(field, NULL);
162
163 if (offset < 0)
164 return 0;
165
166 return *(u16 *)((char *)current_evmcs + offset);
167}
168
169static inline void evmcs_touch_msr_bitmap(void)
170{
171 if (unlikely(!current_evmcs))
172 return;
173
174 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
175 current_evmcs->hv_clean_fields &=
176 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
177}
178
179static inline void evmcs_load(u64 phys_addr)
180{
181 struct hv_vp_assist_page *vp_ap =
182 hv_get_vp_assist_page(smp_processor_id());
183
184 if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall)
185 vp_ap->nested_control.features.directhypercall = 1;
186 vp_ap->current_nested_vmcs = phys_addr;
187 vp_ap->enlighten_vmentry = 1;
188}
189
Olivier Deprez157378f2022-04-04 15:47:50 +0200190__init void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf);
David Brazdil0f672f62019-12-10 10:32:29 +0000191#else /* !IS_ENABLED(CONFIG_HYPERV) */
192static inline void evmcs_write64(unsigned long field, u64 value) {}
193static inline void evmcs_write32(unsigned long field, u32 value) {}
194static inline void evmcs_write16(unsigned long field, u16 value) {}
195static inline u64 evmcs_read64(unsigned long field) { return 0; }
196static inline u32 evmcs_read32(unsigned long field) { return 0; }
197static inline u16 evmcs_read16(unsigned long field) { return 0; }
198static inline void evmcs_load(u64 phys_addr) {}
David Brazdil0f672f62019-12-10 10:32:29 +0000199static inline void evmcs_touch_msr_bitmap(void) {}
200#endif /* IS_ENABLED(CONFIG_HYPERV) */
201
Olivier Deprez157378f2022-04-04 15:47:50 +0200202enum nested_evmptrld_status {
203 EVMPTRLD_DISABLED,
204 EVMPTRLD_SUCCEEDED,
205 EVMPTRLD_VMFAIL,
206 EVMPTRLD_ERROR,
207};
208
David Brazdil0f672f62019-12-10 10:32:29 +0000209bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa);
210uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu);
211int nested_enable_evmcs(struct kvm_vcpu *vcpu,
212 uint16_t *vmcs_version);
Olivier Deprez157378f2022-04-04 15:47:50 +0200213void nested_evmcs_filter_control_msr(u32 msr_index, u64 *pdata);
214int nested_evmcs_check_controls(struct vmcs12 *vmcs12);
David Brazdil0f672f62019-12-10 10:32:29 +0000215
216#endif /* __KVM_X86_VMX_EVMCS_H */