Skip to content

Commit a104e0a

Browse files
committed
Merge tag 'kvm-x86-svm-6.18' of https://github.com/kvm-x86/linux into HEAD
KVM SVM changes for 6.18 - Require a minimum GHCB version of 2 when starting SEV-SNP guests via KVM_SEV_INIT2 so that invalid GHCB versions result in immediate errors instead of latent guest failures. - Add support for Secure TSC for SEV-SNP guests, which prevents the untrusted host from tampering with the guest's TSC frequency, while still allowing the the VMM to configure the guest's TSC frequency prior to launch. - Mitigate the potential for TOCTOU bugs when accessing GHCB fields by wrapping all accesses via READ_ONCE(). - Validate the XCR0 provided by the guest (via the GHCB) to avoid tracking a bogous XCR0 value in KVM's software model. - Save an SEV guest's policy if and only if LAUNCH_START fully succeeds to avoid leaving behind stale state (thankfully not consumed in KVM). - Explicitly reject non-positive effective lengths during SNP's LAUNCH_UPDATE instead of subtly relying on guest_memfd to do the "heavy" lifting. - Reload the pre-VMRUN TSC_AUX on #VMEXIT for SEV-ES guests, not the host's desired TSC_AUX, to fix a bug where KVM could clobber a different vCPU's TSC_AUX due to hardware not matching the value cached in the user-return MSR infrastructure. - Enable AVIC by default for Zen4+ if x2AVIC (and other prereqs) is supported, and clean up the AVIC initialization code along the way.
2 parents 0f68fe4 + ca2967d commit a104e0a

File tree

12 files changed

+322
-246
lines changed

12 files changed

+322
-246
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@
444444
#define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2) /* VM Page Flush MSR is supported */
445445
#define X86_FEATURE_SEV_ES (19*32+ 3) /* "sev_es" Secure Encrypted Virtualization - Encrypted State */
446446
#define X86_FEATURE_SEV_SNP (19*32+ 4) /* "sev_snp" Secure Encrypted Virtualization - Secure Nested Paging */
447+
#define X86_FEATURE_SNP_SECURE_TSC (19*32+ 8) /* SEV-SNP Secure TSC */
447448
#define X86_FEATURE_V_TSC_AUX (19*32+ 9) /* Virtual TSC_AUX */
448449
#define X86_FEATURE_SME_COHERENT (19*32+10) /* hardware-enforced cache coherency */
449450
#define X86_FEATURE_DEBUG_SWAP (19*32+14) /* "debug_swap" SEV-ES full debug state swap support */

arch/x86/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,7 @@ int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val);
22002200
unsigned long kvm_get_dr(struct kvm_vcpu *vcpu, int dr);
22012201
unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu);
22022202
void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
2203+
int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr);
22032204
int kvm_emulate_xsetbv(struct kvm_vcpu *vcpu);
22042205

22052206
int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr);
@@ -2367,6 +2368,7 @@ int kvm_add_user_return_msr(u32 msr);
23672368
int kvm_find_user_return_msr(u32 msr);
23682369
int kvm_set_user_return_msr(unsigned index, u64 val, u64 mask);
23692370
void kvm_user_return_msr_update_cache(unsigned int index, u64 val);
2371+
u64 kvm_get_user_return_msr(unsigned int slot);
23702372

23712373
static inline bool kvm_is_supported_user_return_msr(u32 msr)
23722374
{

arch/x86/include/asm/svm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_
299299
#define SVM_SEV_FEAT_RESTRICTED_INJECTION BIT(3)
300300
#define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4)
301301
#define SVM_SEV_FEAT_DEBUG_SWAP BIT(5)
302+
#define SVM_SEV_FEAT_SECURE_TSC BIT(9)
302303

303304
#define VMCB_ALLOWED_SEV_FEATURES_VALID BIT_ULL(63)
304305

arch/x86/kvm/svm/avic.c

Lines changed: 125 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@
6464

6565
static_assert(__AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_IDX_MASK) == -1u);
6666

67+
#define AVIC_AUTO_MODE -1
68+
69+
static int avic_param_set(const char *val, const struct kernel_param *kp)
70+
{
71+
if (val && sysfs_streq(val, "auto")) {
72+
*(int *)kp->arg = AVIC_AUTO_MODE;
73+
return 0;
74+
}
75+
76+
return param_set_bint(val, kp);
77+
}
78+
79+
static const struct kernel_param_ops avic_ops = {
80+
.flags = KERNEL_PARAM_OPS_FL_NOARG,
81+
.set = avic_param_set,
82+
.get = param_get_bool,
83+
};
84+
85+
/*
86+
* Enable / disable AVIC. In "auto" mode (default behavior), AVIC is enabled
87+
* for Zen4+ CPUs with x2AVIC (and all other criteria for enablement are met).
88+
*/
89+
static int avic = AVIC_AUTO_MODE;
90+
module_param_cb(avic, &avic_ops, &avic, 0444);
91+
__MODULE_PARM_TYPE(avic, "bool");
92+
93+
module_param(enable_ipiv, bool, 0444);
94+
6795
static bool force_avic;
6896
module_param_unsafe(force_avic, bool, 0444);
6997

@@ -77,7 +105,58 @@ static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
77105
static u32 next_vm_id = 0;
78106
static bool next_vm_id_wrapped = 0;
79107
static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
80-
bool x2avic_enabled;
108+
static bool x2avic_enabled;
109+
110+
111+
static void avic_set_x2apic_msr_interception(struct vcpu_svm *svm,
112+
bool intercept)
113+
{
114+
static const u32 x2avic_passthrough_msrs[] = {
115+
X2APIC_MSR(APIC_ID),
116+
X2APIC_MSR(APIC_LVR),
117+
X2APIC_MSR(APIC_TASKPRI),
118+
X2APIC_MSR(APIC_ARBPRI),
119+
X2APIC_MSR(APIC_PROCPRI),
120+
X2APIC_MSR(APIC_EOI),
121+
X2APIC_MSR(APIC_RRR),
122+
X2APIC_MSR(APIC_LDR),
123+
X2APIC_MSR(APIC_DFR),
124+
X2APIC_MSR(APIC_SPIV),
125+
X2APIC_MSR(APIC_ISR),
126+
X2APIC_MSR(APIC_TMR),
127+
X2APIC_MSR(APIC_IRR),
128+
X2APIC_MSR(APIC_ESR),
129+
X2APIC_MSR(APIC_ICR),
130+
X2APIC_MSR(APIC_ICR2),
131+
132+
/*
133+
* Note! Always intercept LVTT, as TSC-deadline timer mode
134+
* isn't virtualized by hardware, and the CPU will generate a
135+
* #GP instead of a #VMEXIT.
136+
*/
137+
X2APIC_MSR(APIC_LVTTHMR),
138+
X2APIC_MSR(APIC_LVTPC),
139+
X2APIC_MSR(APIC_LVT0),
140+
X2APIC_MSR(APIC_LVT1),
141+
X2APIC_MSR(APIC_LVTERR),
142+
X2APIC_MSR(APIC_TMICT),
143+
X2APIC_MSR(APIC_TMCCT),
144+
X2APIC_MSR(APIC_TDCR),
145+
};
146+
int i;
147+
148+
if (intercept == svm->x2avic_msrs_intercepted)
149+
return;
150+
151+
if (!x2avic_enabled)
152+
return;
153+
154+
for (i = 0; i < ARRAY_SIZE(x2avic_passthrough_msrs); i++)
155+
svm_set_intercept_for_msr(&svm->vcpu, x2avic_passthrough_msrs[i],
156+
MSR_TYPE_RW, intercept);
157+
158+
svm->x2avic_msrs_intercepted = intercept;
159+
}
81160

82161
static void avic_activate_vmcb(struct vcpu_svm *svm)
83162
{
@@ -99,7 +178,7 @@ static void avic_activate_vmcb(struct vcpu_svm *svm)
99178
vmcb->control.int_ctl |= X2APIC_MODE_MASK;
100179
vmcb->control.avic_physical_id |= X2AVIC_MAX_PHYSICAL_ID;
101180
/* Disabling MSR intercept for x2APIC registers */
102-
svm_set_x2apic_msr_interception(svm, false);
181+
avic_set_x2apic_msr_interception(svm, false);
103182
} else {
104183
/*
105184
* Flush the TLB, the guest may have inserted a non-APIC
@@ -110,7 +189,7 @@ static void avic_activate_vmcb(struct vcpu_svm *svm)
110189
/* For xAVIC and hybrid-xAVIC modes */
111190
vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID;
112191
/* Enabling MSR intercept for x2APIC registers */
113-
svm_set_x2apic_msr_interception(svm, true);
192+
avic_set_x2apic_msr_interception(svm, true);
114193
}
115194
}
116195

@@ -130,7 +209,7 @@ static void avic_deactivate_vmcb(struct vcpu_svm *svm)
130209
return;
131210

132211
/* Enabling MSR intercept for x2APIC registers */
133-
svm_set_x2apic_msr_interception(svm, true);
212+
avic_set_x2apic_msr_interception(svm, true);
134213
}
135214

136215
/* Note:
@@ -1090,23 +1169,27 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)
10901169
avic_vcpu_load(vcpu, vcpu->cpu);
10911170
}
10921171

1093-
/*
1094-
* Note:
1095-
* - The module param avic enable both xAPIC and x2APIC mode.
1096-
* - Hypervisor can support both xAVIC and x2AVIC in the same guest.
1097-
* - The mode can be switched at run-time.
1098-
*/
1099-
bool avic_hardware_setup(void)
1172+
static bool __init avic_want_avic_enabled(void)
11001173
{
1101-
if (!npt_enabled)
1174+
/*
1175+
* In "auto" mode, enable AVIC by default for Zen4+ if x2AVIC is
1176+
* supported (to avoid enabling partial support by default, and because
1177+
* x2AVIC should be supported by all Zen4+ CPUs). Explicitly check for
1178+
* family 0x19 and later (Zen5+), as the kernel's synthetic ZenX flags
1179+
* aren't inclusive of previous generations, i.e. the kernel will set
1180+
* at most one ZenX feature flag.
1181+
*/
1182+
if (avic == AVIC_AUTO_MODE)
1183+
avic = boot_cpu_has(X86_FEATURE_X2AVIC) &&
1184+
(boot_cpu_data.x86 > 0x19 || cpu_feature_enabled(X86_FEATURE_ZEN4));
1185+
1186+
if (!avic || !npt_enabled)
11021187
return false;
11031188

11041189
/* AVIC is a prerequisite for x2AVIC. */
11051190
if (!boot_cpu_has(X86_FEATURE_AVIC) && !force_avic) {
1106-
if (boot_cpu_has(X86_FEATURE_X2AVIC)) {
1107-
pr_warn(FW_BUG "Cannot support x2AVIC due to AVIC is disabled");
1108-
pr_warn(FW_BUG "Try enable AVIC using force_avic option");
1109-
}
1191+
if (boot_cpu_has(X86_FEATURE_X2AVIC))
1192+
pr_warn(FW_BUG "Cannot enable x2AVIC, AVIC is unsupported\n");
11101193
return false;
11111194
}
11121195

@@ -1116,21 +1199,37 @@ bool avic_hardware_setup(void)
11161199
return false;
11171200
}
11181201

1119-
if (boot_cpu_has(X86_FEATURE_AVIC)) {
1120-
pr_info("AVIC enabled\n");
1121-
} else if (force_avic) {
1122-
/*
1123-
* Some older systems does not advertise AVIC support.
1124-
* See Revision Guide for specific AMD processor for more detail.
1125-
*/
1126-
pr_warn("AVIC is not supported in CPUID but force enabled");
1127-
pr_warn("Your system might crash and burn");
1128-
}
1202+
/*
1203+
* Print a scary message if AVIC is force enabled to make it abundantly
1204+
* clear that ignoring CPUID could have repercussions. See Revision
1205+
* Guide for specific AMD processor for more details.
1206+
*/
1207+
if (!boot_cpu_has(X86_FEATURE_AVIC))
1208+
pr_warn("AVIC unsupported in CPUID but force enabled, your system might crash and burn\n");
1209+
1210+
return true;
1211+
}
1212+
1213+
/*
1214+
* Note:
1215+
* - The module param avic enable both xAPIC and x2APIC mode.
1216+
* - Hypervisor can support both xAVIC and x2AVIC in the same guest.
1217+
* - The mode can be switched at run-time.
1218+
*/
1219+
bool __init avic_hardware_setup(void)
1220+
{
1221+
avic = avic_want_avic_enabled();
1222+
if (!avic)
1223+
return false;
1224+
1225+
pr_info("AVIC enabled\n");
11291226

11301227
/* AVIC is a prerequisite for x2AVIC. */
11311228
x2avic_enabled = boot_cpu_has(X86_FEATURE_X2AVIC);
11321229
if (x2avic_enabled)
11331230
pr_info("x2AVIC enabled\n");
1231+
else
1232+
svm_x86_ops.allow_apicv_in_x2apic_without_x2apic_virtualization = true;
11341233

11351234
/*
11361235
* Disable IPI virtualization for AMD Family 17h CPUs (Zen1 and Zen2)

arch/x86/kvm/svm/nested.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,17 +1798,15 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
17981798
if (kvm_state->size < sizeof(*kvm_state) + KVM_STATE_NESTED_SVM_VMCB_SIZE)
17991799
return -EINVAL;
18001800

1801-
ret = -ENOMEM;
1802-
ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1803-
save = kzalloc(sizeof(*save), GFP_KERNEL);
1804-
if (!ctl || !save)
1805-
goto out_free;
1806-
1807-
ret = -EFAULT;
1808-
if (copy_from_user(ctl, &user_vmcb->control, sizeof(*ctl)))
1809-
goto out_free;
1810-
if (copy_from_user(save, &user_vmcb->save, sizeof(*save)))
1811-
goto out_free;
1801+
ctl = memdup_user(&user_vmcb->control, sizeof(*ctl));
1802+
if (IS_ERR(ctl))
1803+
return PTR_ERR(ctl);
1804+
1805+
save = memdup_user(&user_vmcb->save, sizeof(*save));
1806+
if (IS_ERR(save)) {
1807+
kfree(ctl);
1808+
return PTR_ERR(save);
1809+
}
18121810

18131811
ret = -EINVAL;
18141812
__nested_copy_vmcb_control_to_cache(vcpu, &ctl_cached, ctl);

0 commit comments

Comments
 (0)