blob: 3a461a32128b2d72e5ba40a8e7351b97bf3df059 [file] [log] [blame]
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __KVM_X86_VMX_EVMCS_H
3#define __KVM_X86_VMX_EVMCS_H
4
Sean Christopherson75edce82018-12-03 13:53:06 -08005#include <linux/jump_label.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006
Sean Christopherson75edce82018-12-03 13:53:06 -08007#include <asm/hyperv-tlfs.h>
8#include <asm/mshyperv.h>
9#include <asm/vmx.h>
10
11#include "capabilities.h"
12#include "vmcs.h"
Vitaly Kuznetsova8350232020-02-05 13:30:34 +010013#include "vmcs12.h"
Sean Christopherson75edce82018-12-03 13:53:06 -080014
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)
Vitaly Kuznetsov7a601e22022-01-12 18:01:31 +010062#define EVMCS1_UNSUPPORTED_VMEXIT_CTRL \
63 (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | \
64 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
Sean Christopherson75edce82018-12-03 13:53:06 -080065#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)
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010069
70struct evmcs_field {
71 u16 offset;
72 u16 clean_field;
73};
74
Sean Christopherson75edce82018-12-03 13:53:06 -080075extern const struct evmcs_field vmcs_field_to_evmcs_1[];
76extern const unsigned int nr_evmcs_1_fields;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010077
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010078static __always_inline int get_evmcs_offset(unsigned long field,
79 u16 *clean_field)
80{
81 unsigned int index = ROL16(field, 6);
82 const struct evmcs_field *evmcs_field;
83
Sean Christopherson75edce82018-12-03 13:53:06 -080084 if (unlikely(index >= nr_evmcs_1_fields)) {
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010085 WARN_ONCE(1, "KVM: accessing unsupported EVMCS field %lx\n",
86 field);
87 return -ENOENT;
88 }
89
90 evmcs_field = &vmcs_field_to_evmcs_1[index];
91
92 if (clean_field)
93 *clean_field = evmcs_field->clean_field;
94
95 return evmcs_field->offset;
96}
97
Peter Zijlstra010050a2021-06-24 11:41:07 +020098static __always_inline void evmcs_write64(unsigned long field, u64 value)
Sean Christopherson75edce82018-12-03 13:53:06 -080099{
100 u16 clean_field;
101 int offset = get_evmcs_offset(field, &clean_field);
102
103 if (offset < 0)
104 return;
105
106 *(u64 *)((char *)current_evmcs + offset) = value;
107
108 current_evmcs->hv_clean_fields &= ~clean_field;
109}
110
111static inline void evmcs_write32(unsigned long field, u32 value)
112{
113 u16 clean_field;
114 int offset = get_evmcs_offset(field, &clean_field);
115
116 if (offset < 0)
117 return;
118
119 *(u32 *)((char *)current_evmcs + offset) = value;
120 current_evmcs->hv_clean_fields &= ~clean_field;
121}
122
123static inline void evmcs_write16(unsigned long field, u16 value)
124{
125 u16 clean_field;
126 int offset = get_evmcs_offset(field, &clean_field);
127
128 if (offset < 0)
129 return;
130
131 *(u16 *)((char *)current_evmcs + offset) = value;
132 current_evmcs->hv_clean_fields &= ~clean_field;
133}
134
135static inline u64 evmcs_read64(unsigned long field)
136{
137 int offset = get_evmcs_offset(field, NULL);
138
139 if (offset < 0)
140 return 0;
141
142 return *(u64 *)((char *)current_evmcs + offset);
143}
144
145static inline u32 evmcs_read32(unsigned long field)
146{
147 int offset = get_evmcs_offset(field, NULL);
148
149 if (offset < 0)
150 return 0;
151
152 return *(u32 *)((char *)current_evmcs + offset);
153}
154
155static inline u16 evmcs_read16(unsigned long field)
156{
157 int offset = get_evmcs_offset(field, NULL);
158
159 if (offset < 0)
160 return 0;
161
162 return *(u16 *)((char *)current_evmcs + offset);
163}
164
165static inline void evmcs_touch_msr_bitmap(void)
166{
167 if (unlikely(!current_evmcs))
168 return;
169
170 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
171 current_evmcs->hv_clean_fields &=
172 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
173}
174
175static inline void evmcs_load(u64 phys_addr)
176{
177 struct hv_vp_assist_page *vp_ap =
178 hv_get_vp_assist_page(smp_processor_id());
179
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800180 if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall)
181 vp_ap->nested_control.features.directhypercall = 1;
Sean Christopherson75edce82018-12-03 13:53:06 -0800182 vp_ap->current_nested_vmcs = phys_addr;
183 vp_ap->enlighten_vmentry = 1;
184}
185
Vitaly Kuznetsov064eedf2020-10-14 16:33:46 +0200186__init void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf);
Sean Christopherson75edce82018-12-03 13:53:06 -0800187#else /* !IS_ENABLED(CONFIG_HYPERV) */
Peter Zijlstra010050a2021-06-24 11:41:07 +0200188static __always_inline void evmcs_write64(unsigned long field, u64 value) {}
Sean Christopherson75edce82018-12-03 13:53:06 -0800189static inline void evmcs_write32(unsigned long field, u32 value) {}
190static inline void evmcs_write16(unsigned long field, u16 value) {}
191static inline u64 evmcs_read64(unsigned long field) { return 0; }
192static inline u32 evmcs_read32(unsigned long field) { return 0; }
193static inline u16 evmcs_read16(unsigned long field) { return 0; }
194static inline void evmcs_load(u64 phys_addr) {}
Sean Christopherson75edce82018-12-03 13:53:06 -0800195static inline void evmcs_touch_msr_bitmap(void) {}
196#endif /* IS_ENABLED(CONFIG_HYPERV) */
197
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +0200198#define EVMPTR_INVALID (-1ULL)
Vitaly Kuznetsov27849962021-05-26 15:20:20 +0200199#define EVMPTR_MAP_PENDING (-2ULL)
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +0200200
201static inline bool evmptr_is_valid(u64 evmptr)
202{
Vitaly Kuznetsov27849962021-05-26 15:20:20 +0200203 return evmptr != EVMPTR_INVALID && evmptr != EVMPTR_MAP_PENDING;
Vitaly Kuznetsov1e9dfbd2021-05-26 15:20:16 +0200204}
205
Vitaly Kuznetsovb6a06532020-03-09 16:52:13 +0100206enum nested_evmptrld_status {
207 EVMPTRLD_DISABLED,
208 EVMPTRLD_SUCCEEDED,
209 EVMPTRLD_VMFAIL,
210 EVMPTRLD_ERROR,
211};
212
Vitaly Kuznetsov11e34912019-06-28 13:23:33 +0200213bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa);
Vitaly Kuznetsove2e871a2018-12-10 18:21:55 +0100214uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu);
Sean Christopherson75edce82018-12-03 13:53:06 -0800215int nested_enable_evmcs(struct kvm_vcpu *vcpu,
216 uint16_t *vmcs_version);
Vitaly Kuznetsov31de3d22020-02-05 13:30:33 +0100217void nested_evmcs_filter_control_msr(u32 msr_index, u64 *pdata);
Vitaly Kuznetsova8350232020-02-05 13:30:34 +0100218int nested_evmcs_check_controls(struct vmcs12 *vmcs12);
Sean Christopherson75edce82018-12-03 13:53:06 -0800219
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100220#endif /* __KVM_X86_VMX_EVMCS_H */