KVM: x86: workaround invalid CPUID[0xD, 9] info on some AMD processors
PATCH 1/1 KVM: x86: workaround invalid CPUID[0xD, 9] info on some AMD processors
Some AMD processors expose the PKRU extended save state even if they do not have the related PKU feature in CPUID. Worse, when they do they report a size of 64, whereas the expected size of the PKRU extended save state is 8, therefore the esa->size == eax assertion does not hold.
The state is already ignored by KVM_GET_SUPPORTED_CPUID because it was not enabled in the host XCR0. However, QEMU kvm_cpu_xsave_init() runs before QEMU invokes arch_prctl() to enable dynamically-enabled save states such as XTILEDATA, and KVM_GET_SUPPORTED_CPUID hides save states that have yet to be enabled. Therefore, kvm_cpu_xsave_init() needs to consult the host CPUID instead of KVM_GET_SUPPORTED_CPUID, and dies with an assertion failure.
When setting up the ExtSaveArea array to match the host, ignore features that KVM does not report as supported. This will cause QEMU to skip the incorrect CPUID leaf instead of tripping the assertion.
--- target/i386/cpu.c | 4 ++-- target/i386/cpu.h | 2 ++ target/i386/kvm/kvm-cpu.c | 19 ++++++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-)
target/i386/cpu.c +2 -2
@@ -4981,8 +4981,8 @@ CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)return cpu_list; } -static uint64_t x86_cpu_get_supported_feature_word (FeatureWord w, -bool migratable_only) +uint64_t x86_cpu_get_supported_feature_word (FeatureWord w, +bool migratable_only) {FeatureWordInfo *wi = &feature_word_info[w];uint64_t r =0 ;
target/i386/cpu.h +2 -0
@@ -606,6 +606,8 @@ typedef enum FeatureWord { } FeatureWord;typedef uint64_t FeatureWordArray [FEATURE_WORDS ]; +uint64_t x86_cpu_get_supported_feature_word (FeatureWord w, +bool migratable_only);/* cpuid_features bits */ #define CPUID_FP87 (1U << 0)
target/i386/kvm/kvm-cpu.c +12 -7
@@ -99,13 +99,18 @@ static void kvm_cpu_xsave_init(void)for (i =XSTATE_SSE_BIT +1 ; i <XSAVE_STATE_AREA_COUNT ; i++) {ExtSaveArea *esa = &x86_ext_save_areas[i]; -if (esa->size) { -host_cpuid (0xd , i, &eax, &ebx, &ecx, &edx); -if (eax !=0 ) { -assert (esa->size == eax); - esa->offset = ebx; - esa->ecx = ecx; - } +if (!esa->size) { +continue ; + } +if ((x86_cpu_get_supported_feature_word (esa->feature, false) & esa->bits) + != esa->bits) { +continue ; + } +host_cpuid (0xd , i, &eax, &ebx, &ecx, &edx); +if (eax !=0 ) { +assert (esa->size == eax); + esa->offset = ebx; + esa->ecx = ecx; } } }
Signature · 1 line
2.35.1
Re: [PATCH] KVM: x86: workaround invalid CPUID[0xD,9] info on some AMD processors
Quoted 13 lines · Paolo Bonzini
Some AMD processors expose the PKRU extended save state even if they do not have the related PKU feature in CPUID. Worse, when they do they report a size of 64, whereas the expected size of the PKRU extended save state is 8, therefore the
esa->size== eax assertion does not hold.The state is already ignored by
KVM_GET_SUPPORTED_CPUIDbecause it was not enabled in the host XCR0. However, QEMUkvm_cpu_xsave_init()runs before QEMU invokesarch_prctl()to enable dynamically-enabled save states such as XTILEDATA, andKVM_GET_SUPPORTED_CPUIDhides save states that have yet to be enabled. Therefore,kvm_cpu_xsave_init()needs to consult the host CPUID instead ofKVM_GET_SUPPORTED_CPUID, and dies with an assertion failure.
Quoted 3 lines
When setting up the ExtSaveArea array to match the host, ignore features that KVM does not report as supported. This will cause QEMU to skip the incorrect CPUID leaf instead of tripping the assertion.
Quoted 2 lines
Reported-by Daniel P. Berrangé <[email protected]>
Also credit
Quoted 7 lines
Analyzed-by Yang Zhong <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- target/i386/cpu.c | 4 ++-- target/i386/cpu.h | 2 ++ target/i386/kvm/kvm-cpu.c | 19 ++++++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-)
no longer crashes on the AMD machine I have to hand.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH] KVM: x86: workaround invalid CPUID[0xD,9] info on some AMD processors
In reply to Paolo BonziniQuoted 17 lines · Paolo Bonzini
Some AMD processors expose the PKRU extended save state even if they do not have the related PKU feature in CPUID. Worse, when they do they report a size of 64, whereas the expected size of the PKRU extended save state is 8, therefore the
esa->size== eax assertion does not hold.The state is already ignored by
KVM_GET_SUPPORTED_CPUIDbecause it was not enabled in the host XCR0. However, QEMUkvm_cpu_xsave_init()runs before QEMU invokesarch_prctl()to enable dynamically-enabled save states such as XTILEDATA, andKVM_GET_SUPPORTED_CPUIDhides save states that have yet to be enabled. Therefore,kvm_cpu_xsave_init()needs to consult the host CPUID instead ofKVM_GET_SUPPORTED_CPUID, and dies with an assertion failure.When setting up the ExtSaveArea array to match the host, ignore features that KVM does not report as supported. This will cause QEMU to skip the incorrect CPUID leaf instead of tripping the assertion.
Quoted 8 lines
Reported-by Daniel P. Berrangé <[email protected]>Analyzed-by Yang Zhong <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- target/i386/cpu.c | 4 ++-- target/i386/cpu.h | 2 ++ target/i386/kvm/kvm-cpu.c | 19 ++++++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-)
Verified this patch on AMD EPYC 7402P, no crash issue now. thanks!
Yang
Quoted 17 lines · Paolo Bonzini
Some AMD processors expose the PKRU extended save state even if they do not have the related PKU feature in CPUID. Worse, when they do they report a size of 64, whereas the expected size of the PKRU extended save state is 8, therefore the
esa->size== eax assertion does not hold.The state is already ignored by
KVM_GET_SUPPORTED_CPUIDbecause it was not enabled in the host XCR0. However, QEMUkvm_cpu_xsave_init()runs before QEMU invokesarch_prctl()to enable dynamically-enabled save states such as XTILEDATA, andKVM_GET_SUPPORTED_CPUIDhides save states that have yet to be enabled. Therefore,kvm_cpu_xsave_init()needs to consult the host CPUID instead ofKVM_GET_SUPPORTED_CPUID, and dies with an assertion failure.When setting up the ExtSaveArea array to match the host, ignore features that KVM does not report as supported. This will cause QEMU to skip the incorrect CPUID leaf instead of tripping the assertion.
Quoted 8 lines
Reported-by Daniel P. Berrangé <[email protected]>Analyzed-by Yang Zhong <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- target/i386/cpu.c | 4 ++-- target/i386/cpu.h | 2 ++ target/i386/kvm/kvm-cpu.c | 19 ++++++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-)
With this patch it no longer abort()s on my Ryzen 3900X