KVM: x86: CPUID emulation and tracing fixes
cover KVM: x86: CPUID emulation and tracing fixes
Two fixes related to out-of-range CPUID emulation and related cleanup on top.
I have a unit test and also manually verified a few interesting cases. I'm not planning on posting the unit test at this time because I haven't figured out how to avoid false positives, e.g. if a random in-bounds leaf just happens to match the output of a max basic leaf. It might be doable by hardcoding the cpu model?
Sean Christopherson (6):
KVM: x86: Fix tracing of CPUID.function when function is out-of-range
KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
KVM: x86: Add dedicated emulator helper for grabbing CPUID.maxphyaddr
KVM: x86: Drop return value from kvm_cpuid()
KVM: x86: Rename "found" variable in kvm_cpuid() to
"exact_entry_exists"
KVM: x86: Add requested index to the CPUID tracepoint
arch/x86/include/asm/kvm_emulate.h | 3 ++- arch/x86/kvm/cpuid.c | 26 ++++++++++++-------------- arch/x86/kvm/cpuid.h | 2 +- arch/x86/kvm/emulate.c | 10 +--------- arch/x86/kvm/trace.h | 13 ++++++++----- arch/x86/kvm/x86.c | 10 ++++++++-- 6 files changed, 32 insertions(+), 32 deletions(-)
Signature · 1 line
2.24.1
PATCH 1/6 KVM: x86: Fix tracing of CPUID.function when function is out-of-range
Rework kvm_cpuid() to query entry->function when adjusting the output values so that the original function (in the aptly named "function") is preserved for tracing. This fixes a bug where trace_kvm_cpuid() will trace the max function for a range instead of the requested function if the requested function is out-of-range and an entry for the max function exists.
--- arch/x86/kvm/cpuid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
arch/x86/kvm/cpuid.c +7 -8
@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } +/* Returns true if the requested leaf/function exists in guest CPUID. */ bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -u32 function = *eax, index = *ecx; +const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid_entry2 *max;bool found; entry =kvm_find_cpuid_entry (vcpu, function, index);
@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,*/ if (!entry && check_limit && !guest_cpuid_is_amd (vcpu) && !cpuid_function_in_range (vcpu, function)) { - max =kvm_find_cpuid_entry (vcpu,0 ,0 ); -if (max) { - function = max->eax; - entry =kvm_find_cpuid_entry (vcpu, function, index); - } + entry =kvm_find_cpuid_entry (vcpu,0 ,0 ); +if (entry) + entry =kvm_find_cpuid_entry (vcpu, entry->eax, index); }if (entry) { *eax = entry->eax; *ebx = entry->ebx; *ecx = entry->ecx; *edx = entry->edx; -if (function ==7 && index ==0 ) { + +if (entry->function ==7 && index ==0 ) {u64 data;if (!__kvm_get_msr (vcpu,MSR_IA32_TSX_CTRL , &data, true) && (data &TSX_CTRL_CPUID_CLEAR ))
Signature · 1 line
2.24.1
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
Quoted 50 lines · Sean Christopherson
Rework
kvm_cpuid()to queryentry->functionwhen adjusting the output values so that the original function (in the aptly named "function") is preserved for tracing. This fixes a bug wheretrace_kvm_cpuid()will trace the max function for a range instead of the requested function if the requested function is out-of-range and an entry for the max function exists.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Reported-by Jan Kiszka <[email protected]>Cc Jim Mattson <[email protected]>Cc Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)arch/x86/kvm/cpuid.c +5
-7@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } +/* Returns true if the requested leaf/function exists in guest CPUID. */ bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -u32 function = *eax, index = *ecx; +const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid_entry2 *max;bool found; entry =kvm_find_cpuid_entry (vcpu, function, index);@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,*/ if (!entry && check_limit && !guest_cpuid_is_amd (vcpu) && !cpuid_function_in_range (vcpu, function)) { - max =kvm_find_cpuid_entry (vcpu,0 ,0 ); -if (max) { - function = max->eax; - entry =kvm_find_cpuid_entry (vcpu, function, index); - } + entry =kvm_find_cpuid_entry (vcpu,0 ,0 ); +if (entry) + entry =kvm_find_cpuid_entry (vcpu, entry->eax, index); }if (entry) {
arch/x86/kvm/cpuid.c bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1031
Quoted 10 lines
arch/x86/kvm/cpuid.c +2
-1*eax = entry->eax; *ebx = entry->ebx; *ecx = entry->ecx; *edx = entry->edx; -if (function ==7 && index ==0 ) { + +if (entry->function ==7 && index ==0 ) {u64 data;if (!__kvm_get_msr (vcpu,MSR_IA32_TSX_CTRL , &data, true) && (data &TSX_CTRL_CPUID_CLEAR ))
What about the !entry case below this? It was impacted by the function capping so far, not it's no longer.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE Corporate Competence Center Embedded Linux
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
Quoted 54 lines · Jan Kiszka
Quoted 53 lines · Sean Christopherson
Rework
kvm_cpuid()to queryentry->functionwhen adjusting the output values so that the original function (in the aptly named "function") is preserved for tracing. This fixes a bug wheretrace_kvm_cpuid()will trace the max function for a range instead of the requested function if the requested function is out-of-range and an entry for the max function exists.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Reported-by Jan Kiszka <[email protected]>Cc Jim Mattson <[email protected]>Cc Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)arch/x86/kvm/cpuid.c +6
-8@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } +/* Returns true if the requested leaf/function exists in guest CPUID. */ bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -u32 function = *eax, index = *ecx; +const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid_entry2 *max;bool found; entry =kvm_find_cpuid_entry (vcpu, function, index);@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,*/ if (!entry && check_limit && !guest_cpuid_is_amd (vcpu) && !cpuid_function_in_range (vcpu, function)) { - max =kvm_find_cpuid_entry (vcpu,0 ,0 ); -if (max) { - function = max->eax; - entry =kvm_find_cpuid_entry (vcpu, function, index); - } + entry =kvm_find_cpuid_entry (vcpu,0 ,0 ); +if (entry) + entry =kvm_find_cpuid_entry (vcpu, entry->eax, index); }if (entry) { *eax = entry->eax; *ebx = entry->ebx; *ecx = entry->ecx; *edx = entry->edx; -if (function ==7 && index ==0 ) { +
Quoted 8 lines
arch/x86/kvm/cpuid.c bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1031
Quoted 5 lines
arch/x86/kvm/cpuid.c +1
-0+if (entry->function ==7 && index ==0 ) {u64 data;if (!__kvm_get_msr (vcpu,MSR_IA32_TSX_CTRL , &data, true) && (data &TSX_CTRL_CPUID_CLEAR ))What about the !entry case below this? It was impacted by the function capping so far, not it's no longer.
Hmm, the only way the output would be different is in a really contrived scenario where userspace doesn't provide an entry for the max basic leaf.
The !entry path can only be reached with "orig_function != function" if orig_function is out of range and there is no entry for the max basic leaf. The adjustments for 0xb/0x1f require the max basic leaf to be 0xb or 0x1f, and to take effect with !entry would require there to be a CPUID.max.1 but not a CPUID.max.0. That'd be a violation of Intel's SDM, i.e. it's bogus userspace input and IMO can be ignored.
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
Quoted 64 lines · Sean Christopherson
Quoted 54 lines · Jan Kiszka
Quoted 53 lines · Sean Christopherson
Rework
kvm_cpuid()to queryentry->functionwhen adjusting the output values so that the original function (in the aptly named "function") is preserved for tracing. This fixes a bug wheretrace_kvm_cpuid()will trace the max function for a range instead of the requested function if the requested function is out-of-range and an entry for the max function exists.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Reported-by Jan Kiszka <[email protected]>Cc Jim Mattson <[email protected]>Cc Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)arch/x86/kvm/cpuid.c +6
-8@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } +/* Returns true if the requested leaf/function exists in guest CPUID. */ bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -u32 function = *eax, index = *ecx; +const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid_entry2 *max;bool found; entry =kvm_find_cpuid_entry (vcpu, function, index);@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,*/ if (!entry && check_limit && !guest_cpuid_is_amd (vcpu) && !cpuid_function_in_range (vcpu, function)) { - max =kvm_find_cpuid_entry (vcpu,0 ,0 ); -if (max) { - function = max->eax; - entry =kvm_find_cpuid_entry (vcpu, function, index); - } + entry =kvm_find_cpuid_entry (vcpu,0 ,0 ); +if (entry) + entry =kvm_find_cpuid_entry (vcpu, entry->eax, index); }if (entry) { *eax = entry->eax; *ebx = entry->ebx; *ecx = entry->ecx; *edx = entry->edx; -if (function ==7 && index ==0 ) { +Quoted 8 lines
arch/x86/kvm/cpuid.c bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1031
Quoted 5 lines
arch/x86/kvm/cpuid.c +1
-0+if (entry->function ==7 && index ==0 ) {u64 data;if (!__kvm_get_msr (vcpu,MSR_IA32_TSX_CTRL , &data, true) && (data &TSX_CTRL_CPUID_CLEAR ))What about the !entry case below this? It was impacted by the function capping so far, not it's no longer.
Quoted 2 lines
Hmm, the only way the output would be different is in a really contrived scenario where userspace doesn't provide an entry for the max basic leaf.
I think I've seen that, a cap to 0x10, with QEMU and '-cpu host# when providing intentionally bogus values to cpuid.
Jan
Quoted 8 lines
The !entry path can only be reached with "
orig_function!= function" iforig_functionis out of range and there is no entry for the max basic leaf. The adjustments for 0xb/0x1f require the max basic leaf to be0xbor0x1f, and to take effect with !entry would require there to be a CPUID.max.1 but not a CPUID.max.0. That'd be a violation of Intel's SDM, i.e. it's bogus userspace input and IMO can be ignored.
Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE Corporate Competence Center Embedded Linux
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
In reply to Sean ChristophersonQuoted 67 lines · Sean Christopherson
Quoted 54 lines · Jan Kiszka
Quoted 53 lines · Sean Christopherson
Rework
kvm_cpuid()to queryentry->functionwhen adjusting the output values so that the original function (in the aptly named "function") is preserved for tracing. This fixes a bug wheretrace_kvm_cpuid()will trace the max function for a range instead of the requested function if the requested function is out-of-range and an entry for the max function exists.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Reported-by Jan Kiszka <[email protected]>Cc Jim Mattson <[email protected]>Cc Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)arch/x86/kvm/cpuid.c +6
-8@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } +/* Returns true if the requested leaf/function exists in guest CPUID. */ bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -u32 function = *eax, index = *ecx; +const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid_entry2 *max;bool found; entry =kvm_find_cpuid_entry (vcpu, function, index);@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,*/ if (!entry && check_limit && !guest_cpuid_is_amd (vcpu) && !cpuid_function_in_range (vcpu, function)) { - max =kvm_find_cpuid_entry (vcpu,0 ,0 ); -if (max) { - function = max->eax; - entry =kvm_find_cpuid_entry (vcpu, function, index); - } + entry =kvm_find_cpuid_entry (vcpu,0 ,0 ); +if (entry) + entry =kvm_find_cpuid_entry (vcpu, entry->eax, index); }if (entry) { *eax = entry->eax; *ebx = entry->ebx; *ecx = entry->ecx; *edx = entry->edx; -if (function ==7 && index ==0 ) { +Quoted 8 lines
arch/x86/kvm/cpuid.c bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1031
Quoted 5 lines
arch/x86/kvm/cpuid.c +1
-0+if (entry->function ==7 && index ==0 ) {u64 data;if (!__kvm_get_msr (vcpu,MSR_IA32_TSX_CTRL , &data, true) && (data &TSX_CTRL_CPUID_CLEAR ))What about the !entry case below this? It was impacted by the function capping so far, not it's no longer.
Hmm, the only way the output would be different is in a really contrived scenario where userspace doesn't provide an entry for the max basic leaf.
Quoted 8 lines
The !entry path can only be reached with "
orig_function!= function" iforig_functionis out of range and there is no entry for the max basic leaf.The adjustments for 0xb/0x1f require the max basic leaf to be
0xbor0x1f, and to take effect with !entry would require there to be a CPUID.max.1 but not a CPUID.max.0. That'd be a violation of Intel's SDM, i.e. it's bogus userspace input and IMO can be ignored.
Sorry I cannot catch you. Why it's a violation of Intel's SDM?
Supposing the max basic is 0x1f, and it queries cpuid(0x20, 0x5), it should return cpuid(0x1f, 0x5).
But based on this patch, it returns all zeros.
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
Quoted 71 lines · Xiaoyao Li
Quoted 69 lines · Sean Christopherson
Quoted 54 lines · Jan Kiszka
Quoted 53 lines · Sean Christopherson
Rework
kvm_cpuid()to queryentry->functionwhen adjusting the output values so that the original function (in the aptly named "function") is preserved for tracing. This fixes a bug wheretrace_kvm_cpuid()will trace the max function for a range instead of the requested function if the requested function is out-of-range and an entry for the max function exists.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Reported-by Jan Kiszka <[email protected]>Cc Jim Mattson <[email protected]>Cc Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)arch/x86/kvm/cpuid.c +6
-8@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } +/* Returns true if the requested leaf/function exists in guest CPUID. */ bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -u32 function = *eax, index = *ecx; +const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid_entry2 *max;bool found; entry =kvm_find_cpuid_entry (vcpu, function, index);@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,*/ if (!entry && check_limit && !guest_cpuid_is_amd (vcpu) && !cpuid_function_in_range (vcpu, function)) { - max =kvm_find_cpuid_entry (vcpu,0 ,0 ); -if (max) { - function = max->eax; - entry =kvm_find_cpuid_entry (vcpu, function, index); - } + entry =kvm_find_cpuid_entry (vcpu,0 ,0 ); +if (entry) + entry =kvm_find_cpuid_entry (vcpu, entry->eax, index); }if (entry) { *eax = entry->eax; *ebx = entry->ebx; *ecx = entry->ecx; *edx = entry->edx; -if (function ==7 && index ==0 ) { +Quoted 8 lines
arch/x86/kvm/cpuid.c bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1031
Quoted 5 lines
arch/x86/kvm/cpuid.c +1
-0+if (entry->function ==7 && index ==0 ) {u64 data;if (!__kvm_get_msr (vcpu,MSR_IA32_TSX_CTRL , &data, true) && (data &TSX_CTRL_CPUID_CLEAR ))What about the !entry case below this? It was impacted by the function capping so far, not it's no longer.
Hmm, the only way the output would be different is in a really contrived scenario where userspace doesn't provide an entry for the max basic leaf.
The !entry path can only be reached with "
orig_function!= function" iforig_functionis out of range and there is no entry for the max basic leaf.
Quoted 7 lines
Quoted 5 lines
The adjustments for 0xb/0x1f require the max basic leaf to be
0xbor0x1f, and to take effect with !entry would require there to be a CPUID.max.1 but not a CPUID.max.0. That'd be a violation of Intel's SDM, i.e. it's bogus userspace input and IMO can be ignored.Sorry I cannot catch you. Why it's a violation of Intel's SDM?
The case being discussed above would look like:
KVM CPUID Entries:
Function Index Output
0x00000000 0x00: eax=0x0000000b ebx=0x756e6547 ecx=0x6c65746e edx=0x49656e69 0x00000001 0x00: eax=0x000906ea ebx=0x03000800 ecx=0xfffa3223 edx=0x0f8bfbff 0x00000002 0x00: eax=0x00000001 ebx=0x00000000 ecx=0x0000004d edx=0x002c307d 0x00000003 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000004 0x00: eax=0x00000121 ebx=0x01c0003f ecx=0x0000003f edx=0x00000001 0x00000004 0x01: eax=0x00000122 ebx=0x01c0003f ecx=0x0000003f edx=0x00000001 0x00000004 0x02: eax=0x00000143 ebx=0x03c0003f ecx=0x00000fff edx=0x00000001 0x00000004 0x03: eax=0x00000163 ebx=0x03c0003f ecx=0x00003fff edx=0x00000006 0x00000005 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000003 edx=0x00000000 0x00000006 0x00: eax=0x00000004 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000007 0x00: eax=0x00000000 ebx=0x009c4fbb ecx=0x00000004 edx=0x84000000 0x00000008 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000009 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x0000000a 0x00: eax=0x07300402 ebx=0x00000000 ecx=0x00000000 edx=0x00000603
--> MISSING CPUID.0xB.0
0x0000000b 0x01: eax=0x00000000 ebx=0x00000001 ecx=0x00000201 edx=0x00000003
CPUID.0xB.0 does not exist, so output.ECX=0, which indicates an invalid level-type.
The SDM states (for CPUID.0xB):
If an input value n in ECX returns the invalid level-type of 0 in ECX[15:8], other input values with ECX > n also return 0 in ECX[15:8]
That means returning a valid level-type in CPUID.0xB.1 as above violates the SDM's definition of how leaf 0xB works. I'm arguing we can ignore the adjustments that would be done on output.E{C,D} for an out of range leaf because the model is bogus.
Quoted 4 lines
Supposing the max basic is
0x1f, and it queriescpuid(0x20, 0x5), it should returncpuid(0x1f, 0x5).But based on this patch, it returns all zeros.
Have you tested the patch, or is your comment based on the above discussion and/or code inspection? Honest question, because I've thoroughly tested the above scenario and it works as you describe, but now I'm worried I completely botched my testing.
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
Quoted 106 lines · Sean Christopherson
Quoted 71 lines · Xiaoyao Li
Quoted 69 lines · Sean Christopherson
Quoted 54 lines · Jan Kiszka
Quoted 53 lines · Sean Christopherson
Rework
kvm_cpuid()to queryentry->functionwhen adjusting the output values so that the original function (in the aptly named "function") is preserved for tracing. This fixes a bug wheretrace_kvm_cpuid()will trace the max function for a range instead of the requested function if the requested function is out-of-range and an entry for the max function exists.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Reported-by Jan Kiszka <[email protected]>Cc Jim Mattson <[email protected]>Cc Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)arch/x86/kvm/cpuid.c +6
-8@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } +/* Returns true if the requested leaf/function exists in guest CPUID. */ bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -u32 function = *eax, index = *ecx; +const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid_entry2 *max;bool found; entry =kvm_find_cpuid_entry (vcpu, function, index);@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,*/ if (!entry && check_limit && !guest_cpuid_is_amd (vcpu) && !cpuid_function_in_range (vcpu, function)) { - max =kvm_find_cpuid_entry (vcpu,0 ,0 ); -if (max) { - function = max->eax; - entry =kvm_find_cpuid_entry (vcpu, function, index); - } + entry =kvm_find_cpuid_entry (vcpu,0 ,0 ); +if (entry) + entry =kvm_find_cpuid_entry (vcpu, entry->eax, index); }if (entry) { *eax = entry->eax; *ebx = entry->ebx; *ecx = entry->ecx; *edx = entry->edx; -if (function ==7 && index ==0 ) { +Quoted 8 lines
arch/x86/kvm/cpuid.c bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1031
Quoted 5 lines
arch/x86/kvm/cpuid.c +1
-0+if (entry->function ==7 && index ==0 ) {u64 data;if (!__kvm_get_msr (vcpu,MSR_IA32_TSX_CTRL , &data, true) && (data &TSX_CTRL_CPUID_CLEAR ))What about the !entry case below this? It was impacted by the function capping so far, not it's no longer.
Hmm, the only way the output would be different is in a really contrived scenario where userspace doesn't provide an entry for the max basic leaf.
The !entry path can only be reached with "
orig_function!= function" iforig_functionis out of range and there is no entry for the max basic leaf.Quoted 7 lines
Quoted 5 lines
The adjustments for 0xb/0x1f require the max basic leaf to be
0xbor0x1f, and to take effect with !entry would require there to be a CPUID.max.1 but not a CPUID.max.0. That'd be a violation of Intel's SDM, i.e. it's bogus userspace input and IMO can be ignored.Sorry I cannot catch you. Why it's a violation of Intel's SDM?
The case being discussed above would look like:
KVM CPUID Entries:
Function Index Output 0x00000000 0x00: eax=0x0000000b ebx=0x756e6547 ecx=0x6c65746e edx=0x49656e69 0x00000001 0x00: eax=0x000906ea ebx=0x03000800 ecx=0xfffa3223 edx=0x0f8bfbff 0x00000002 0x00: eax=0x00000001 ebx=0x00000000 ecx=0x0000004d edx=0x002c307d 0x00000003 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000004 0x00: eax=0x00000121 ebx=0x01c0003f ecx=0x0000003f edx=0x00000001 0x00000004 0x01: eax=0x00000122 ebx=0x01c0003f ecx=0x0000003f edx=0x00000001 0x00000004 0x02: eax=0x00000143 ebx=0x03c0003f ecx=0x00000fff edx=0x00000001 0x00000004 0x03: eax=0x00000163 ebx=0x03c0003f ecx=0x00003fff edx=0x00000006 0x00000005 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000003 edx=0x00000000 0x00000006 0x00: eax=0x00000004 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000007 0x00: eax=0x00000000 ebx=0x009c4fbb ecx=0x00000004 edx=0x84000000 0x00000008 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000009 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x0000000a 0x00: eax=0x07300402 ebx=0x00000000 ecx=0x00000000 edx=0x00000603 --> MISSING CPUID.0xB.0 0x0000000b 0x01: eax=0x00000000 ebx=0x00000001 ecx=0x00000201 edx=0x00000003CPUID.0xB.0 does not exist, so output.ECX=0, which indicates an invalid level-type.
The SDM states (for CPUID.0xB):
Quoted 7 lines
If an input value n in ECX returns the invalid level-type of 0 in ECX[15:8], other input values with ECX > n also return 0 in ECX[15:8]
That means returning a valid level-type in CPUID.0xB.1 as above violates the SDM's definition of how leaf
0xBworks. I'm arguing we can ignore the adjustments that would be done on output.E{C,D} for an out of range leaf because the model is bogus.
Right.
So we'd better do something in KVM_SET_CPUID* , to avoid userspace set bogus cpuid.
Quoted 10 lines
Quoted 4 lines
Supposing the max basic is
0x1f, and it queriescpuid(0x20, 0x5), it should returncpuid(0x1f, 0x5).But based on this patch, it returns all zeros.
Have you tested the patch, or is your comment based on the above discussion and/or code inspection? Honest question, because I've thoroughly tested the above scenario and it works as you describe, but now I'm worried I completely botched my testing.
No, I didn't test.
Leaf 0xB and 0x1f are special cases when they are the maximum basic leaf, because no matter what subleaf is, there is always a non-zero E[CX,DX].
If cpuid.0 returns maximum basic leaf as 0xB/0x1F, when queried leaf is greater, it should always return a non-zero value.
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
Quoted 54 lines · Xiaoyao Li
Quoted 29 lines · Sean Christopherson
Quoted 1 line · Xiaoyao Li
Sorry I cannot catch you. Why it's a violation of Intel's SDM?
The case being discussed above would look like:
KVM CPUID Entries:
Function Index Output 0x00000000 0x00: eax=0x0000000b ebx=0x756e6547 ecx=0x6c65746e edx=0x49656e69 0x00000001 0x00: eax=0x000906ea ebx=0x03000800 ecx=0xfffa3223 edx=0x0f8bfbff 0x00000002 0x00: eax=0x00000001 ebx=0x00000000 ecx=0x0000004d edx=0x002c307d 0x00000003 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000004 0x00: eax=0x00000121 ebx=0x01c0003f ecx=0x0000003f edx=0x00000001 0x00000004 0x01: eax=0x00000122 ebx=0x01c0003f ecx=0x0000003f edx=0x00000001 0x00000004 0x02: eax=0x00000143 ebx=0x03c0003f ecx=0x00000fff edx=0x00000001 0x00000004 0x03: eax=0x00000163 ebx=0x03c0003f ecx=0x00003fff edx=0x00000006 0x00000005 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000003 edx=0x00000000 0x00000006 0x00: eax=0x00000004 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000007 0x00: eax=0x00000000 ebx=0x009c4fbb ecx=0x00000004 edx=0x84000000 0x00000008 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000009 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x0000000a 0x00: eax=0x07300402 ebx=0x00000000 ecx=0x00000000 edx=0x00000603--> MISSING CPUID.0xB.0
0x0000000b0x01: eax=0x00000000 ebx=0x00000001 ecx=0x00000201 edx=0x00000003CPUID.0xB.0 does not exist, so output.ECX=0, which indicates an invalid level-type.
The SDM states (for CPUID.0xB):
Quoted 7 lines
If an input value n in ECX returns the invalid level-type of 0 in ECX[15:8], other input values with ECX > n also return 0 in ECX[15:8]
That means returning a valid level-type in CPUID.0xB.1 as above violates the SDM's definition of how leaf
0xBworks. I'm arguing we can ignore the adjustments that would be done on output.E{C,D} for an out of range leaf because the model is bogus.Right.
So we'd better do something in KVM_SET_CPUID* , to avoid userspace set bogus cpuid.
Quoted 10 lines
Quoted 4 lines
Supposing the max basic is
0x1f, and it queriescpuid(0x20, 0x5), it should returncpuid(0x1f, 0x5).But based on this patch, it returns all zeros.
Have you tested the patch, or is your comment based on the above discussion and/or code inspection? Honest question, because I've thoroughly tested the above scenario and it works as you describe, but now I'm worried I completely botched my testing.
Quoted 7 lines
No, I didn't test.
Leaf
0xBand0x1fare special cases when they are the maximum basic leaf, because no matter what subleaf is, there is always a non-zero E[CX,DX].If cpuid.0 returns maximum basic leaf as 0xB/0x1F, when queried leaf is greater, it should always return a non-zero value.
Yes, and that's userspace's responsibility to not screw up. E.g. if userspace didn't create CPUID.0xB.0 (as above) then it's not KVM's fault for returning zeros when the guest executes CPUID.0xB.0.
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
Quoted 60 lines · Sean Christopherson
Quoted 59 lines · Xiaoyao Li
Quoted 29 lines · Sean Christopherson
Quoted 1 line · Xiaoyao Li
Sorry I cannot catch you. Why it's a violation of Intel's SDM?
The case being discussed above would look like:
KVM CPUID Entries:
Function Index Output 0x00000000 0x00: eax=0x0000000b ebx=0x756e6547 ecx=0x6c65746e edx=0x49656e69 0x00000001 0x00: eax=0x000906ea ebx=0x03000800 ecx=0xfffa3223 edx=0x0f8bfbff 0x00000002 0x00: eax=0x00000001 ebx=0x00000000 ecx=0x0000004d edx=0x002c307d 0x00000003 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000004 0x00: eax=0x00000121 ebx=0x01c0003f ecx=0x0000003f edx=0x00000001 0x00000004 0x01: eax=0x00000122 ebx=0x01c0003f ecx=0x0000003f edx=0x00000001 0x00000004 0x02: eax=0x00000143 ebx=0x03c0003f ecx=0x00000fff edx=0x00000001 0x00000004 0x03: eax=0x00000163 ebx=0x03c0003f ecx=0x00003fff edx=0x00000006 0x00000005 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000003 edx=0x00000000 0x00000006 0x00: eax=0x00000004 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000007 0x00: eax=0x00000000 ebx=0x009c4fbb ecx=0x00000004 edx=0x84000000 0x00000008 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x00000009 0x00: eax=0x00000000 ebx=0x00000000 ecx=0x00000000 edx=0x00000000 0x0000000a 0x00: eax=0x07300402 ebx=0x00000000 ecx=0x00000000 edx=0x00000603 --> MISSING CPUID.0xB.0 0x0000000b 0x01: eax=0x00000000 ebx=0x00000001 ecx=0x00000201 edx=0x00000003CPUID.0xB.0 does not exist, so output.ECX=0, which indicates an invalid level-type.
The SDM states (for CPUID.0xB):
Quoted 7 lines
If an input value n in ECX returns the invalid level-type of 0 in ECX[15:8], other input values with ECX > n also return 0 in ECX[15:8]
That means returning a valid level-type in CPUID.0xB.1 as above violates the SDM's definition of how leaf
0xBworks. I'm arguing we can ignore the adjustments that would be done on output.E{C,D} for an out of range leaf because the model is bogus.Right.
So we'd better do something in KVM_SET_CPUID* , to avoid userspace set bogus cpuid.
Quoted 10 lines
Quoted 4 lines
Supposing the max basic is
0x1f, and it queriescpuid(0x20, 0x5), it should returncpuid(0x1f, 0x5).But based on this patch, it returns all zeros.
Have you tested the patch, or is your comment based on the above discussion and/or code inspection? Honest question, because I've thoroughly tested the above scenario and it works as you describe, but now I'm worried I completely botched my testing.
No, I didn't test.
Leaf
0xBand0x1fare special cases when they are the maximum basic leaf, because no matter what subleaf is, there is always a non-zero E[CX,DX].
Quoted 7 lines
Quoted 2 lines
If cpuid.0 returns maximum basic leaf as 0xB/0x1F, when queried leaf is greater, it should always return a non-zero value.
Yes, and that's userspace's responsibility to not screw up. E.g. if userspace didn't create CPUID.0xB.0 (as above) then it's not KVM's fault for returning zeros when the guest executes CPUID.0xB.0.
But this needs userspace to create all the subleaf of 0xB/0x1F. with a correct userspace, for example, it only creates one more subleaf of 0xB/0x1F to indicate from this subleaf, no valid level-type anymore.
So when maximum basic leaf is 0x1f, cpuid(0x20, bigger than the first invalid subleaf created by userspace) returns all-zero.
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
In reply to Sean ChristophersonQuoted 37 lines · Sean Christopherson
Rework
kvm_cpuid()to queryentry->functionwhen adjusting the output values so that the original function (in the aptly named "function") is preserved for tracing. This fixes a bug wheretrace_kvm_cpuid()will trace the max function for a range instead of the requested function if the requested function is out-of-range and an entry for the max function exists.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Reported-by Jan Kiszka <[email protected]>Cc Jim Mattson <[email protected]>Cc Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)arch/x86/kvm/cpuid.c +2
-2@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } +/* Returns true if the requested leaf/function exists in guest CPUID. */ bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -u32 function = *eax, index = *ecx; +const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid_entry2 *max;bool found; entry =kvm_find_cpuid_entry (vcpu, function, index);@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,*/
arch/x86/kvm/cpuid.c bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1020
Quoted 10 lines
arch/x86/kvm/cpuid.c +3
-5if (!entry && check_limit && !guest_cpuid_is_amd (vcpu) && !cpuid_function_in_range (vcpu, function)) { - max =kvm_find_cpuid_entry (vcpu,0 ,0 ); -if (max) { - function = max->eax; - entry =kvm_find_cpuid_entry (vcpu, function, index); - } + entry =kvm_find_cpuid_entry (vcpu,0 ,0 ); +if (entry) + entry =kvm_find_cpuid_entry (vcpu, entry->eax, index);
There is a problem.
when queried leaf is out of range on Intel CPU, it returns the maximum basic leaf, and any dependence on input ECX (i.e., subleaf) value in the basic leaf is honored. As disclaimed in SDM of CPUID instruction.
The ECX should be honored if and only the leaf has a significant index.
If the leaf doesn't has a significant index, it just ignores the EDX input in bare metal.
So it should be something like:
if (!entry && check_limit && !guest_cpuid_is_amd(vcpu) &&
!cpuid_function_in_range(vcpu, function)) {
entry = kvm_find_cpuid_entry(vcpu, 0, 0);
if (entry) {
entry = kvm_find_cpuid_entry(vcpu, entry->eax, 0);
if (entry &&
entry->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX ) {
entry = kvm_find_cpuid_entry(vcpu, entry->eax,
index);
}
}
}Quoted 13 lines
arch/x86/kvm/cpuid.c +2
-1}if (entry) { *eax = entry->eax; *ebx = entry->ebx; *ecx = entry->ecx; *edx = entry->edx; -if (function ==7 && index ==0 ) { + +if (entry->function ==7 && index ==0 ) {u64 data;if (!__kvm_get_msr (vcpu,MSR_IA32_TSX_CTRL , &data, true) && (data &TSX_CTRL_CPUID_CLEAR ))
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
Quoted 49 lines · Xiaoyao Li
Quoted 35 lines · Sean Christopherson
Rework
kvm_cpuid()to queryentry->functionwhen adjusting the output values so that the original function (in the aptly named "function") is preserved for tracing. This fixes a bug wheretrace_kvm_cpuid()will trace the max function for a range instead of the requested function if the requested function is out-of-range and an entry for the max function exists.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Reported-by Jan Kiszka <[email protected]>Cc Jim Mattson <[email protected]>Cc Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)arch/x86/kvm/cpuid.c +2
-2@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } +/* Returns true if the requested leaf/function exists in guest CPUID. */ bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -u32 function = *eax, index = *ecx; +const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid_entry2 *max;bool found; entry =kvm_find_cpuid_entry (vcpu, function, index);@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,*/ arch/x86/kvm/cpuid.c bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1020
Quoted 10 lines
arch/x86/kvm/cpuid.c +3
-5if (!entry && check_limit && !guest_cpuid_is_amd (vcpu) && !cpuid_function_in_range (vcpu, function)) { - max =kvm_find_cpuid_entry (vcpu,0 ,0 ); -if (max) { - function = max->eax; - entry =kvm_find_cpuid_entry (vcpu, function, index); - } + entry =kvm_find_cpuid_entry (vcpu,0 ,0 ); +if (entry) + entry =kvm_find_cpuid_entry (vcpu, entry->eax, index);There is a problem.
Quoted 3 lines
when queried leaf is out of range on Intel CPU, it returns the maximum basic leaf, and any dependence on input ECX (i.e., subleaf) value in the basic leaf is honored. As disclaimed in SDM of CPUID instruction.
That's what the code above does.
Quoted 2 lines
The ECX should be honored if and only the leaf has a significant index. If the leaf doesn't has a significant index, it just ignores the EDX input
s/EDX/ECX
Quoted 11 lines
in bare metal.
So it should be something like:
if (!entry &&
check_limit&& !guest_cpuid_is_amd(vcpu) &&!cpuid_function_in_range(vcpu, function)) { entry = kvm_find_cpuid_entry(vcpu, 0, 0); if (entry) { entry = kvm_find_cpuid_entry(vcpu, entry->eax, 0); if (entry && entry->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX ) {
This is unnecessary IMO. The only scenario where SIGNFICANT_INDEX is 0 and cpuid_entry(entry->eax, 0) != cpuid_entry(entry->eax, index) is if userspace created a cpuid entry for index>0 with SIGNFICANT_INDEX. That's a busted model, e.g. it'd be the SDM equivalent of an Intel CPU having different output for CPUID.0x16.0 and CPUID.16.5 despite the SDM stating that the CPUID.0x16 ignores the index.
E.g. on my system with a max basic leaf of 0x16
$ cpuid -1 -r CPU: 0x00000000 0x00: eax=0x00000016 ebx=0x756e6547 ecx=0x6c65746e edx=0x49656e69 ... 0x00000016 0x00: eax=0x00000e74 ebx=0x0000125c ecx=0x00000064 edx=0x00000000
$ cpuid -1 -r -l 0x16 CPU: 0x00000016 0x00: eax=0x00000e74 ebx=0x0000125c ecx=0x00000064 edx=0x00000000 ~ $ cpuid -1 -r -l 0x16 -s 4 CPU: 0x00000016 0x04: eax=0x00000e74 ebx=0x0000125c ecx=0x00000064 edx=0x00000000 ~ $ cpuid -1 -r -l 0x16 -s 466 CPU: 0x00000016 0x1d2: eax=0x00000e74 ebx=0x0000125c ecx=0x00000064 edx=0x00000000
If it returned anything else for CPUID.0x16.0x4 then it'd be a CPU bug. Same thing here, it's a userspace bug if it creates a CPUID entry that shouldn't exist. E.g. ignoring Intel's silly "max basic leaf" behavior for the moment, if userspace created a entry for CPUID.0x0.N it would break the Linux kernel's cpu_detect(), as it doesn't initialize ECX when doing CPUID.0x0.
Quoted 20 lines
entry = kvm_find_cpuid_entry(vcpu, entry->eax, index); } } }Quoted 13 lines
arch/x86/kvm/cpuid.c +2
-1}if (entry) { *eax = entry->eax; *ebx = entry->ebx; *ecx = entry->ecx; *edx = entry->edx; -if (function ==7 && index ==0 ) { + +if (entry->function ==7 && index ==0 ) {u64 data;if (!__kvm_get_msr (vcpu,MSR_IA32_TSX_CTRL , &data, true) && (data &TSX_CTRL_CPUID_CLEAR ))
Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range
Quoted 73 lines · Sean Christopherson
Quoted 49 lines · Xiaoyao Li
Quoted 35 lines · Sean Christopherson
Rework
kvm_cpuid()to queryentry->functionwhen adjusting the output values so that the original function (in the aptly named "function") is preserved for tracing. This fixes a bug wheretrace_kvm_cpuid()will trace the max function for a range instead of the requested function if the requested function is out-of-range and an entry for the max function exists.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Reported-by Jan Kiszka <[email protected]>Cc Jim Mattson <[email protected]>Cc Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)arch/x86/kvm/cpuid.c +2
-2@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } +/* Returns true if the requested leaf/function exists in guest CPUID. */ bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -u32 function = *eax, index = *ecx; +const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid_entry2 *max;bool found; entry =kvm_find_cpuid_entry (vcpu, function, index);@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,*/ arch/x86/kvm/cpuid.c bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1020
Quoted 10 lines
arch/x86/kvm/cpuid.c +3
-5if (!entry && check_limit && !guest_cpuid_is_amd (vcpu) && !cpuid_function_in_range (vcpu, function)) { - max =kvm_find_cpuid_entry (vcpu,0 ,0 ); -if (max) { - function = max->eax; - entry =kvm_find_cpuid_entry (vcpu, function, index); - } + entry =kvm_find_cpuid_entry (vcpu,0 ,0 ); +if (entry) + entry =kvm_find_cpuid_entry (vcpu, entry->eax, index);There is a problem.
Quoted 3 lines
when queried leaf is out of range on Intel CPU, it returns the maximum basic leaf, and any dependence on input ECX (i.e., subleaf) value in the basic leaf is honored. As disclaimed in SDM of CPUID instruction.
That's what the code above does.
Quoted 2 lines
The ECX should be honored if and only the leaf has a significant index. If the leaf doesn't has a significant index, it just ignores the EDX input
s/EDX/ECX
Quoted 11 lines
in bare metal.
So it should be something like:
if (!entry &&
check_limit&& !guest_cpuid_is_amd(vcpu) &&!cpuid_function_in_range(vcpu, function)) { entry = kvm_find_cpuid_entry(vcpu, 0, 0); if (entry) { entry = kvm_find_cpuid_entry(vcpu, entry->eax, 0); if (entry && entry->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX ) {
Quoted 3 lines
This is unnecessary IMO. The only scenario where
SIGNFICANT_INDEXis 0 andcpuid_entry(entry->eax, 0)!=cpuid_entry(entry->eax, index)is if userspace created a cpuid entry for index>0 withSIGNFICANT_INDEX.
I just forgot that is_matching_cpuid_entry() has taken SIGNIFICANT_INDEX into account.
Please ignore my stupid noise.
Quoted 50 lines
a busted model, e.g. it'd be the SDM equivalent of an Intel CPU having different output for CPUID.0x16.0 and CPUID.16.5 despite the SDM stating that the CPUID.0x16 ignores the index.
E.g. on my system with a max basic leaf of
0x16$ cpuid -1 -r CPU: 0x00000000 0x00: eax=0x00000016 ebx=0x756e6547 ecx=0x6c65746e edx=0x49656e69 ... 0x00000016 0x00: eax=0x00000e74 ebx=0x0000125c ecx=0x00000064 edx=0x00000000$ cpuid -1 -r -l 0x16 CPU: 0x00000016 0x00: eax=0x00000e74 ebx=0x0000125c ecx=0x00000064 edx=0x00000000 ~ $ cpuid -1 -r -l 0x16 -s 4 CPU: 0x00000016 0x04: eax=0x00000e74 ebx=0x0000125c ecx=0x00000064 edx=0x00000000 ~ $ cpuid -1 -r -l 0x16 -s 466 CPU: 0x00000016 0x1d2: eax=0x00000e74 ebx=0x0000125c ecx=0x00000064 edx=0x00000000If it returned anything else for CPUID.0x16.0x4 then it'd be a CPU bug. Same thing here, it's a userspace bug if it creates a CPUID entry that shouldn't exist. E.g. ignoring Intel's silly "max basic leaf" behavior for the moment, if userspace created a entry for CPUID.0x0.N it would break the Linux kernel's
cpu_detect(), as it doesn't initialize ECX when doing CPUID.0x0.Quoted 20 lines
entry = kvm_find_cpuid_entry(vcpu, entry->eax, index); } } }Quoted 13 lines
arch/x86/kvm/cpuid.c +2
-1}if (entry) { *eax = entry->eax; *ebx = entry->ebx; *ecx = entry->ecx; *edx = entry->edx; -if (function ==7 && index ==0 ) { + +if (entry->function ==7 && index ==0 ) {u64 data;if (!__kvm_get_msr (vcpu,MSR_IA32_TSX_CTRL , &data, true) && (data &TSX_CTRL_CPUID_CLEAR ))
PATCH 2/6 KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
In reply to Sean ChristophersonExtend the mask in cpuid_function_in_range() for finding the "class" of the function to 0xfffffff00. While there is no official definition of what constitutes a class, e.g. arguably bits 31:16 should be the class and bits 15:0 the functions within that class, the Hypervisor logic effectively uses bits 31:8 as the class by virtue of checking for different bases in increments of 0x100, e.g. KVM advertises its CPUID functions starting at 0x40000100 when HyperV features are advertised at the default base of 0x40000000.
Masking against 0x80000000 only handles basic and extended leafs, which results in Centaur and Hypervisor range checks being performed against the basic CPUID range, e.g. if CPUID.0x40000000.EAX=0x4000000A and there is no entry for CPUID.0x40000006, then function 0x40000006 would be incorrectly reported as out of bounds.
The bad range check doesn't cause function problems for any known VMM because out-of-range semantics only come into play if the exact entry isn't found, and VMMs either support a very limited Hypervisor range, e.g. the official KVM range is 0x40000000-0x40000001 (effectively no room for undefined leafs) or explicitly defines gaps to be zero, e.g. Qemu explicitly creates zeroed entries up to the Cenatur and Hypervisor limits (the latter comes into play when providing HyperV features).
The bad behavior can be visually confirmed by dumping CPUID output in the guest when running Qemu with a stable TSC, as Qemu extends the limit of range 0x40000000 to 0x40000010 to advertise VMware's cpuid_freq, without defining zeroed entries for 0x40000002 - 0x4000000f.
--- arch/x86/kvm/cpuid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
@@ -993,7 +993,7 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function) {struct kvm_cpuid_entry2 *max; - max =kvm_find_cpuid_entry (vcpu, function &0x80000000 ,0 ); + max =kvm_find_cpuid_entry (vcpu, function &0xffffff00u ,0 );return max && function <= max->eax; }
Signature · 1 line
2.24.1
Re: [PATCH 2/6] KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
Quoted 9 lines · Sean Christopherson
Extend the mask in
cpuid_function_in_range()for finding the "class" of the function to0xfffffff00. While there is no official definition of what constitutes a class, e.g. arguably bits 31:16 should be the class and bits 15:0 the functions within that class, the Hypervisor logic effectively uses bits 31:8 as the class by virtue of checking for different bases in increments of0x100, e.g. KVM advertises its CPUID functions starting at0x40000100when HyperV features are advertised at the default base of0x40000000.
This convention deserves explicit documentation outside of the commit message.
Quoted 6 lines
Masking against
0x80000000only handles basic and extended leafs, which results in Centaur and Hypervisor range checks being performed against the basic CPUID range, e.g. if CPUID.0x40000000.EAX=0x4000000A and there is no entry for CPUID.0x40000006, then function0x40000006would be incorrectly reported as out of bounds.
Quoted 7 lines
The bad range check doesn't cause function problems for any known VMM because out-of-range semantics only come into play if the exact entry isn't found, and VMMs either support a very limited Hypervisor range, e.g. the official KVM range is 0x40000000-0x40000001 (effectively no room for undefined leafs) or explicitly defines gaps to be zero, e.g. Qemu explicitly creates zeroed entries up to the Cenatur and Hypervisor limits (the latter comes into play when providing HyperV features).
Does Centaur implement the bizarre Intel behavior for out-of-bound entries? It seems that if there are Centaur leaves defined, the CPUD semantics should be those specified by Centaur.
Quoted 17 lines
The bad behavior can be visually confirmed by dumping CPUID output in the guest when running Qemu with a stable TSC, as Qemu extends the limit of range
0x40000000to0x40000010to advertise VMware'scpuid_freq, without defining zeroed entries for0x40000002-0x4000000f.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Cc Jim Mattson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/cpuid.c +0
-0
arch/x86/kvm/cpuid.c static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function) · line 996
Quoted 5 lines
arch/x86/kvm/cpuid.c +1
-1@@ -993,7 +993,7 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function) {struct kvm_cpuid_entry2 *max; - max =kvm_find_cpuid_entry (vcpu, function &0x80000000 ,0 ); + max =kvm_find_cpuid_entry (vcpu, function &0xffffff00u ,0 );
This assumes that CPUID.(function & 0xffffff00):EAX always contains the maximum input value for the 256-entry range sharing the high 24 bits. I don't believe that convention has ever been established or documented.
Quoted 6 lines
arch/x86/kvm/cpuid.c +0
-1return max && function <= max->eax; } --2.24.1
Re: [PATCH 2/6] KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
Quoted 12 lines · Jim Mattson
Quoted 9 lines · Sean Christopherson
Extend the mask in
cpuid_function_in_range()for finding the "class" of the function to0xfffffff00. While there is no official definition of what constitutes a class, e.g. arguably bits 31:16 should be the class and bits 15:0 the functions within that class, the Hypervisor logic effectively uses bits 31:8 as the class by virtue of checking for different bases in increments of0x100, e.g. KVM advertises its CPUID functions starting at0x40000100when HyperV features are advertised at the default base of0x40000000.
Quoted 1 line
This convention deserves explicit documentation outside of the commit message.
No argument there.
Quoted 14 lines
Quoted 6 lines
Masking against
0x80000000only handles basic and extended leafs, which results in Centaur and Hypervisor range checks being performed against the basic CPUID range, e.g. if CPUID.0x40000000.EAX=0x4000000A and there is no entry for CPUID.0x40000006, then function0x40000006would be incorrectly reported as out of bounds.Quoted 7 lines
The bad range check doesn't cause function problems for any known VMM because out-of-range semantics only come into play if the exact entry isn't found, and VMMs either support a very limited Hypervisor range, e.g. the official KVM range is 0x40000000-0x40000001 (effectively no room for undefined leafs) or explicitly defines gaps to be zero, e.g. Qemu explicitly creates zeroed entries up to the Cenatur and Hypervisor limits (the latter comes into play when providing HyperV features).
Quoted 3 lines
Does Centaur implement the bizarre Intel behavior for out-of-bound entries? It seems that if there are Centaur leaves defined, the CPUD semantics should be those specified by Centaur.
Ah, right, because this code triggers on !=AMD, not ==Intel. Your guess is as good as mine, I've dug around a few times trying to track down a spec for Centaur/VIA without success.
I would say that KVM's emulation behavior should probably be all or nothing, i.e. either due Intel's silly logic for all ranges/classes or do it for none.
Quoted 20 lines
Quoted 20 lines
The bad behavior can be visually confirmed by dumping CPUID output in the guest when running Qemu with a stable TSC, as Qemu extends the limit of range
0x40000000to0x40000010to advertise VMware'scpuid_freq, without defining zeroed entries for0x40000002-0x4000000f.Fixes 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH")Cc Jim Mattson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/cpuid.c +0
-0@@ -993,7 +993,7 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function) {struct kvm_cpuid_entry2 *max;
Quoted 7 lines
arch/x86/kvm/cpuid.c static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function) · line 996
Quoted 2 lines
arch/x86/kvm/cpuid.c +1
-1- max =kvm_find_cpuid_entry (vcpu, function &0x80000000 ,0 ); + max =kvm_find_cpuid_entry (vcpu, function &0xffffff00u ,0 );This assumes that CPUID.(function & 0xffffff00):EAX always contains the maximum input value for the 256-entry range sharing the high 24 bits. I don't believe that convention has ever been established or documented.
Not sure if it's formally documented, but it's well established. The closest thing I could find to documentation is the lkml thread where what's implemented today (AFAICT) was proposed.
https://lore.kernel.org/lkml/[email protected]/
The relevant linux code in Linux (arch/x86/include/asm/processor.h), where @leaves contains the kernel's required minimum leaf to enable paravirt stuff for the hypervisor.
static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
{
uint32_t base, eax, signature[3];
for (base = 0x40000000; base < 0x40010000; base += 0x100) {
cpuid(base, &eax, &signature[0], &signature[1], &signature[2]);
if (!memcmp(sig, signature, 12) &&
(leaves == 0 || ((eax - base) >= leaves)))
return base;
}
return 0;
}Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/cpuid.c +0
-1return max && function <= max->eax; } --2.24.1
Re: [PATCH 2/6] KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
In reply to Sean ChristophersonQuoted 4 lines · Sean Christopherson
The bad behavior can be visually confirmed by dumping CPUID output in the guest when running Qemu with a stable TSC, as Qemu extends the limit of range
0x40000000to0x40000010to advertise VMware'scpuid_freq, without defining zeroed entries for0x40000002-0x4000000f.
I think it could be reasonably argued that this is a userspace bug. Clearly, when userspace explicitly supplies the results for a leaf, those results override the default CPUID values for that leaf. But I haven't seen it documented anywhere that leaves *not* explicitly supplied by userspace will override the default CPUID values, just because they happen to appear in some magic range.
Re: [PATCH 2/6] KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
Quoted 9 lines · Jim Mattson
Quoted 4 lines · Sean Christopherson
The bad behavior can be visually confirmed by dumping CPUID output in the guest when running Qemu with a stable TSC, as Qemu extends the limit of range
0x40000000to0x40000010to advertise VMware'scpuid_freq, without defining zeroed entries for0x40000002-0x4000000f.
Quoted 6 lines
I think it could be reasonably argued that this is a userspace bug. Clearly, when userspace explicitly supplies the results for a leaf, those results override the default CPUID values for that leaf. But I haven't seen it documented anywhere that leaves *not* explicitly supplied by userspace will override the default CPUID values, just because they happen to appear in some magic range.
In fact, the more I think about it, the original change is correct, at least in this regard. Your "fix" introduces undocumented and unfathomable behavior.
Re: [PATCH 2/6] KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
Quoted 17 lines · Jim Mattson
Quoted 9 lines · Jim Mattson
Quoted 4 lines · Sean Christopherson
The bad behavior can be visually confirmed by dumping CPUID output in the guest when running Qemu with a stable TSC, as Qemu extends the limit of range
0x40000000to0x40000010to advertise VMware'scpuid_freq, without defining zeroed entries for0x40000002-0x4000000f.Quoted 6 lines
I think it could be reasonably argued that this is a userspace bug. Clearly, when userspace explicitly supplies the results for a leaf, those results override the default CPUID values for that leaf. But I haven't seen it documented anywhere that leaves *not* explicitly supplied by userspace will override the default CPUID values, just because they happen to appear in some magic range.
Quoted 3 lines
In fact, the more I think about it, the original change is correct, at least in this regard. Your "fix" introduces undocumented and unfathomable behavior.
Heh, the takeaway from this is that whatever we decide on needs to be documented somewhere :-)
I wouldn't say it's unfathomable, conceptually it seems like the intent of the hypervisor range was to mimic the basic and extended ranges. The whole thing is arbitrary behavior. Of course if Intel CPUs would just return 0s on undefined leafs it would be a lot less arbitrary :-)
Anyways, I don't have a strong opinion on whether this patch stays or goes.
Re: [PATCH 2/6] KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
Unfathomable was the wrong word. I can see what you're trying to do. I just don't think it's defensible. I suspect that Intel CPU architects will be surprised and disappointed to find that the maximum effective value of CPUID.0H:EAX is now 255, and that they have to define CPUID.100H:EAX as the "maximum leaf between 100H and 1FFH" if they want to define any leaves between 100H and 1FFH.
Furthermore, AMD has only ceded 4000_0000h through 4000_00FFh to hypervisors, so kvm's use of 40000100H through 400001FFH appears to be a land grab, akin to VIA's unilateral grab of the C0000000H leaves. Admittedly, one could argue that the 40000000H leaves are not AMD's to apportion, since AMD and Intel appear to have reached a detente by splitting the available space down the middle. Intel, who seems to be the recognized authority for this range, declares the entire range from 40000000H through 4FFFFFFFH to be invalid. Make of that what you will.
In any event, no one has ever documented what's supposed to happen if you leave gaps in the 4xxxxxxxH range when defining synthesized CPUID leaves under kvm.
Quoted 32 lines · Sean Christopherson
Quoted 17 lines · Jim Mattson
Quoted 9 lines · Jim Mattson
Quoted 4 lines · Sean Christopherson
The bad behavior can be visually confirmed by dumping CPUID output in the guest when running Qemu with a stable TSC, as Qemu extends the limit of range
0x40000000to0x40000010to advertise VMware'scpuid_freq, without defining zeroed entries for0x40000002-0x4000000f.Quoted 6 lines
I think it could be reasonably argued that this is a userspace bug. Clearly, when userspace explicitly supplies the results for a leaf, those results override the default CPUID values for that leaf. But I haven't seen it documented anywhere that leaves *not* explicitly supplied by userspace will override the default CPUID values, just because they happen to appear in some magic range.
Quoted 3 lines
In fact, the more I think about it, the original change is correct, at least in this regard. Your "fix" introduces undocumented and unfathomable behavior.
Heh, the takeaway from this is that whatever we decide on needs to be documented somewhere :-)
I wouldn't say it's unfathomable, conceptually it seems like the intent of the hypervisor range was to mimic the basic and extended ranges. The whole thing is arbitrary behavior. Of course if Intel CPUs would just return 0s on undefined leafs it would be a lot less arbitrary :-)
Anyways, I don't have a strong opinion on whether this patch stays or goes.
Re: [PATCH 2/6] KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
Quoted 1 line · Jim Mattson
Unfathomable was the wrong word.
I dunno, one could argue that the behavior of Intel CPUs for CPUID is unfathomable and I was just trying to follow suit :-D
Quoted 6 lines
I can see what you're trying to do. I
just don't think it's defensible. I suspect that Intel CPU architects will be surprised and disappointed to find that the maximum effective value of CPUID.0H:EAX is now 255, and that they have to define CPUID.100H:EAX as the "maximum leaf between 100H and 1FFH" if they want to define any leaves between 100H and 1FFH.
Hmm, ya, I agree that applying a 0xffffff00 mask to all classes of CPUID ranges is straight up wrong.
Quoted 10 lines
Furthermore, AMD has only ceded 4000_0000h through 4000_00FFh to hypervisors, so kvm's use of 40000100H through 400001FFH appears to be a land grab, akin to VIA's unilateral grab of the C0000000H leaves. Admittedly, one could argue that the 40000000H leaves are not AMD's to apportion, since AMD and Intel appear to have reached a detente by splitting the available space down the middle. Intel, who seems to be the recognized authority for this range, declares the entire range from 40000000H through 4FFFFFFFH to be invalid. Make of that what you will.
Quoted 3 lines
In any event, no one has ever documented what's supposed to happen if you leave gaps in the 4xxxxxxxH range when defining synthesized CPUID leaves under kvm.
Probably stating the obvious, but for me, the least suprising thing is for such leafs to output zeros. It also feels safer, e.g. a guest that's querying hypervisor support is less likely to be led astray by all zeros than by a random feature bits being set.
What about something like this? Along with a comment and documentation...
static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)
{
struct kvm_cpuid_entry2 *max;
if (function >= 0x40000000 && function <= 0x4fffffff)
max = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0);
else
max = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
return max && function <= max->eax;
}Quoted 34 lines
Quoted 32 lines · Sean Christopherson
Quoted 17 lines · Jim Mattson
Quoted 9 lines · Jim Mattson
Quoted 4 lines · Sean Christopherson
The bad behavior can be visually confirmed by dumping CPUID output in the guest when running Qemu with a stable TSC, as Qemu extends the limit of range
0x40000000to0x40000010to advertise VMware'scpuid_freq, without defining zeroed entries for0x40000002-0x4000000f.Quoted 6 lines
I think it could be reasonably argued that this is a userspace bug. Clearly, when userspace explicitly supplies the results for a leaf, those results override the default CPUID values for that leaf. But I haven't seen it documented anywhere that leaves *not* explicitly supplied by userspace will override the default CPUID values, just because they happen to appear in some magic range.
Quoted 3 lines
In fact, the more I think about it, the original change is correct, at least in this regard. Your "fix" introduces undocumented and unfathomable behavior.
Heh, the takeaway from this is that whatever we decide on needs to be documented somewhere :-)
I wouldn't say it's unfathomable, conceptually it seems like the intent of the hypervisor range was to mimic the basic and extended ranges. The whole thing is arbitrary behavior. Of course if Intel CPUs would just return 0s on undefined leafs it would be a lot less arbitrary :-)
Anyways, I don't have a strong opinion on whether this patch stays or goes.
Re: [PATCH 2/6] KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
Quoted 39 lines · Sean Christopherson
Quoted 1 line · Jim Mattson
Unfathomable was the wrong word.
I dunno, one could argue that the behavior of Intel CPUs for CPUID is unfathomable and I was just trying to follow suit :-D
Quoted 6 lines
I can see what you're trying to do. I
just don't think it's defensible. I suspect that Intel CPU architects will be surprised and disappointed to find that the maximum effective value of CPUID.0H:EAX is now 255, and that they have to define CPUID.100H:EAX as the "maximum leaf between 100H and 1FFH" if they want to define any leaves between 100H and 1FFH.Hmm, ya, I agree that applying a
0xffffff00mask to all classes of CPUID ranges is straight up wrong.Quoted 10 lines
Furthermore, AMD has only ceded 4000_0000h through 4000_00FFh to hypervisors, so kvm's use of 40000100H through 400001FFH appears to be a land grab, akin to VIA's unilateral grab of the C0000000H leaves. Admittedly, one could argue that the 40000000H leaves are not AMD's to apportion, since AMD and Intel appear to have reached a detente by splitting the available space down the middle. Intel, who seems to be the recognized authority for this range, declares the entire range from 40000000H through 4FFFFFFFH to be invalid. Make of that what you will.
Quoted 3 lines
In any event, no one has ever documented what's supposed to happen if you leave gaps in the 4xxxxxxxH range when defining synthesized CPUID leaves under kvm.
Probably stating the obvious, but for me, the least suprising thing is for such leafs to output zeros. It also feels safer, e.g. a guest that's querying hypervisor support is less likely to be led astray by all zeros than by a random feature bits being set.
What about something like this? Along with a comment and documentation...
static bool
cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)
Quoted 9 lines
{
struct kvm_cpuid_entry2 *max;
if (function >= 0x40000000 && function <= 0x4fffffff)
max = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0);
else
max = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
return max && function <= max->eax;
}I can get behind that. The behavior of the 4xxxxxxxH leaves under kvm is arguably up to kvm (though AMD may disagree).
Paolo Bonzini
maintainerRe: [PATCH 2/6] KVM: x86: Fix CPUID range check for Centaur and Hypervisor ranges
In reply to Sean ChristophersonQuoted 10 lines · Sean Christopherson
static bool
cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function){ struct kvm_cpuid_entry2 *max; if (function >= 0x40000000 && function <= 0x4fffffff) max = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0); else max = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0); return max && function <= max->eax; }
Yes, this is a good idea (except it should be & 0xc0000000 to cover Centaur).
Paolo
PATCH 3/6 KVM: x86: Add dedicated emulator helper for grabbing CPUID.maxphyaddr
In reply to Sean ChristophersonAdd a helper to retrieve cpuid_maxphyaddr() instead of manually calculating the value in the emulator via raw CPUID output. In addition to consolidating logic, this also paves the way toward simplifying kvm_cpuid(), whose somewhat confusing return value exists purely to support the emulator's maxphyaddr calculation.
No functional change intended.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 10 +--------- arch/x86/kvm/x86.c | 6 ++++++ 3 files changed, 8 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -222,6 +222,7 @@ struct x86_emulate_ops {bool (*get_cpuid)(struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit); +int (*get_cpuid_maxphyaddr)(struct x86_emulate_ctxt *ctxt);bool (*guest_has_long_mode)(struct x86_emulate_ctxt *ctxt);bool (*guest_has_movbe)(struct x86_emulate_ctxt *ctxt);bool (*guest_has_fxsr)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/emulate.c +1 -9
@@ -4244,16 +4244,8 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt) ctxt->ops->get_msr (ctxt,MSR_EFER , &efer);if (efer &EFER_LMA ) { -u64 maxphyaddr; -u32 eax, ebx, ecx, edx; +int maxphyaddr = ctxt->ops->get_cpuid_maxphyaddr (ctxt); - eax =0x80000008 ; - ecx =0 ; -if (ctxt->ops->get_cpuid (ctxt, &eax, &ebx, &ecx, - &edx, false)) - maxphyaddr = eax &0xff ; -else - maxphyaddr =36 ; rsvd =rsvd_bits (maxphyaddr,63 );if (ctxt->ops->get_cr (ctxt,4 ) &X86_CR4_PCIDE ) rsvd &= ~X86_CR3_PCID_NOFLUSH ;
arch/x86/kvm/x86.c +6 -0
@@ -6209,6 +6209,11 @@ static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,return kvm_cpuid (emul_to_vcpu (ctxt), eax, ebx, ecx, edx, check_limit); } +static int emulator_get_cpuid_maxphyaddr (struct x86_emulate_ctxt *ctxt) +{ +return cpuid_maxphyaddr (emul_to_vcpu (ctxt)); +} +static bool emulator_guest_has_long_mode (struct x86_emulate_ctxt *ctxt) {return guest_cpuid_has (emul_to_vcpu (ctxt),X86_FEATURE_LM );
@@ -6301,6 +6306,7 @@ static const struct x86_emulate_ops emulate_ops = { .fix_hypercall = emulator_fix_hypercall, .intercept = emulator_intercept, .get_cpuid = emulator_get_cpuid, + .get_cpuid_maxphyaddr= emulator_get_cpuid_maxphyaddr, .guest_has_long_mode = emulator_guest_has_long_mode, .guest_has_movbe = emulator_guest_has_movbe, .guest_has_fxsr = emulator_guest_has_fxsr,
Signature · 1 line
2.24.1
Paolo Bonzini
maintainerRe: [PATCH 3/6] KVM: x86: Add dedicated emulator helper for grabbing CPUID.maxphyaddr
Quoted 7 lines · Sean Christopherson
Add a helper to retrieve
cpuid_maxphyaddr()instead of manually calculating the value in the emulator via raw CPUID output. In addition to consolidating logic, this also paves the way toward simplifyingkvm_cpuid(), whose somewhat confusing return value exists purely to support the emulator's maxphyaddr calculation.No functional change intended.
I don't think this is a particularly useful change. Yes, it's not intuitive but is it more than a matter of documentation (and possibly moving the check_cr_write snippet into a separate function)?
Paolo
Quoted 66 lines
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 10 +--------- arch/x86/kvm/x86.c | 6 ++++++ 3 files changed, 8 insertions(+), 9 deletions(-)arch/x86/include/asm/kvm_emulate.h +1
-0@@ -222,6 +222,7 @@ struct x86_emulate_ops {bool (*get_cpuid)(struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit); +int (*get_cpuid_maxphyaddr)(struct x86_emulate_ctxt *ctxt);bool (*guest_has_long_mode)(struct x86_emulate_ctxt *ctxt);bool (*guest_has_movbe)(struct x86_emulate_ctxt *ctxt);bool (*guest_has_fxsr)(struct x86_emulate_ctxt *ctxt);arch/x86/kvm/emulate.c +1
-9@@ -4244,16 +4244,8 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt) ctxt->ops->get_msr (ctxt,MSR_EFER , &efer);if (efer &EFER_LMA ) { -u64 maxphyaddr; -u32 eax, ebx, ecx, edx; +int maxphyaddr = ctxt->ops->get_cpuid_maxphyaddr (ctxt); - eax =0x80000008 ; - ecx =0 ; -if (ctxt->ops->get_cpuid (ctxt, &eax, &ebx, &ecx, - &edx, false)) - maxphyaddr = eax &0xff ; -else - maxphyaddr =36 ; rsvd =rsvd_bits (maxphyaddr,63 );if (ctxt->ops->get_cr (ctxt,4 ) &X86_CR4_PCIDE ) rsvd &= ~X86_CR3_PCID_NOFLUSH ;arch/x86/kvm/x86.c +6
-0@@ -6209,6 +6209,11 @@ static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,return kvm_cpuid (emul_to_vcpu (ctxt), eax, ebx, ecx, edx, check_limit); } +static int emulator_get_cpuid_maxphyaddr (struct x86_emulate_ctxt *ctxt) +{ +return cpuid_maxphyaddr (emul_to_vcpu (ctxt)); +} +static bool emulator_guest_has_long_mode (struct x86_emulate_ctxt *ctxt) {return guest_cpuid_has (emul_to_vcpu (ctxt),X86_FEATURE_LM );@@ -6301,6 +6306,7 @@ static const struct x86_emulate_ops emulate_ops = { .fix_hypercall = emulator_fix_hypercall, .intercept = emulator_intercept, .get_cpuid = emulator_get_cpuid, + .get_cpuid_maxphyaddr= emulator_get_cpuid_maxphyaddr, .guest_has_long_mode = emulator_guest_has_long_mode, .guest_has_movbe = emulator_guest_has_movbe, .guest_has_fxsr = emulator_guest_has_fxsr,
Re: [PATCH 3/6] KVM: x86: Add dedicated emulator helper for grabbing CPUID.maxphyaddr
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson
Add a helper to retrieve
cpuid_maxphyaddr()instead of manually calculating the value in the emulator via raw CPUID output. In addition to consolidating logic, this also paves the way toward simplifyingkvm_cpuid(), whose somewhat confusing return value exists purely to support the emulator's maxphyaddr calculation.
Quoted 5 lines
Quoted 1 line
No functional change intended.
I don't think this is a particularly useful change. Yes, it's not intuitive but is it more than a matter of documentation (and possibly moving the
check_cr_writesnippet into a separate function)?
Besides the non obvious return value of the current function, this approach also avoids leaving cpuid traces for querying maxphyaddr, which is also not very intuitive IMHO.
Jan
Quoted 70 lines
Paolo
Quoted 66 lines
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 10 +--------- arch/x86/kvm/x86.c | 6 ++++++ 3 files changed, 8 insertions(+), 9 deletions(-)arch/x86/include/asm/kvm_emulate.h +1
-0@@ -222,6 +222,7 @@ struct x86_emulate_ops {bool (*get_cpuid)(struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit); +int (*get_cpuid_maxphyaddr)(struct x86_emulate_ctxt *ctxt);bool (*guest_has_long_mode)(struct x86_emulate_ctxt *ctxt);bool (*guest_has_movbe)(struct x86_emulate_ctxt *ctxt);bool (*guest_has_fxsr)(struct x86_emulate_ctxt *ctxt);arch/x86/kvm/emulate.c +1
-9@@ -4244,16 +4244,8 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt) ctxt->ops->get_msr (ctxt,MSR_EFER , &efer);if (efer &EFER_LMA ) { -u64 maxphyaddr; -u32 eax, ebx, ecx, edx; +int maxphyaddr = ctxt->ops->get_cpuid_maxphyaddr (ctxt); - eax =0x80000008 ; - ecx =0 ; -if (ctxt->ops->get_cpuid (ctxt, &eax, &ebx, &ecx, - &edx, false)) - maxphyaddr = eax &0xff ; -else - maxphyaddr =36 ; rsvd =rsvd_bits (maxphyaddr,63 );if (ctxt->ops->get_cr (ctxt,4 ) &X86_CR4_PCIDE ) rsvd &= ~X86_CR3_PCID_NOFLUSH ;arch/x86/kvm/x86.c +6
-0@@ -6209,6 +6209,11 @@ static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,return kvm_cpuid (emul_to_vcpu (ctxt), eax, ebx, ecx, edx, check_limit); } +static int emulator_get_cpuid_maxphyaddr (struct x86_emulate_ctxt *ctxt) +{ +return cpuid_maxphyaddr (emul_to_vcpu (ctxt)); +} +static bool emulator_guest_has_long_mode (struct x86_emulate_ctxt *ctxt) {return guest_cpuid_has (emul_to_vcpu (ctxt),X86_FEATURE_LM );@@ -6301,6 +6306,7 @@ static const struct x86_emulate_ops emulate_ops = { .fix_hypercall = emulator_fix_hypercall, .intercept = emulator_intercept, .get_cpuid = emulator_get_cpuid, + .get_cpuid_maxphyaddr= emulator_get_cpuid_maxphyaddr, .guest_has_long_mode = emulator_guest_has_long_mode, .guest_has_movbe = emulator_guest_has_movbe, .guest_has_fxsr = emulator_guest_has_fxsr,
Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE Corporate Competence Center Embedded Linux
Paolo Bonzini
maintainerRe: [PATCH 3/6] KVM: x86: Add dedicated emulator helper for grabbing CPUID.maxphyaddr
Quoted 8 lines · Jan Kiszka
Quoted 4 lines
I don't think this is a particularly useful change. Yes, it's not intuitive but is it more than a matter of documentation (and possibly moving the
check_cr_writesnippet into a separate function)?Besides the non obvious return value of the current function, this approach also avoids leaving cpuid traces for querying maxphyaddr, which is also not very intuitive IMHO.
There are already other cases where we leave CPUID traces. We can just stop tracing if check_limit (which should be renamed to from_guest) is true; there are other internal cases which call ctxt->ops->get_cpuid, such as vendor_intel, and those should also use check_limit==true and check the return value of ctxt->ops->get_cpuid.
Paolo
Re: [PATCH 3/6] KVM: x86: Add dedicated emulator helper for grabbing CPUID.maxphyaddr
Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · Jan Kiszka
Quoted 4 lines
I don't think this is a particularly useful change. Yes, it's not intuitive but is it more than a matter of documentation (and possibly moving the
check_cr_writesnippet into a separate function)?Besides the non obvious return value of the current function, this approach also avoids leaving cpuid traces for querying maxphyaddr, which is also not very intuitive IMHO.
Quoted 5 lines
There are already other cases where we leave CPUID traces. We can just stop tracing if
check_limit(which should be renamed tofrom_guest) is true; there are other internal cases which callctxt->ops->get_cpuid, such asvendor_intel, and those should also use check_limit==true and check the return value ofctxt->ops->get_cpuid.
No, the vendor checks that use get_cpuid() shouldn't do check_limit=true, they're looking for an exact match on the vendor.
Not that it matters. @check_limit only comes into play on a vendor check if CPUID.0 doesn't exist, and @check_limit only effects the output if CPUID.0 _does_ exist. I.e. the output for CPUID.0 is unaffected by @check_limit.
Re: [PATCH 3/6] KVM: x86: Add dedicated emulator helper for grabbing CPUID.maxphyaddr
In reply to Paolo BonziniQuoted 7 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson
Add a helper to retrieve
cpuid_maxphyaddr()instead of manually calculating the value in the emulator via raw CPUID output. In addition to consolidating logic, this also paves the way toward simplifyingkvm_cpuid(), whose somewhat confusing return value exists purely to support the emulator's maxphyaddr calculation.
Quoted 5 lines
Quoted 1 line
No functional change intended.
I don't think this is a particularly useful change. Yes, it's not intuitive but is it more than a matter of documentation (and possibly moving the
check_cr_writesnippet into a separate function)?
I really don't like duplicating the maxphyaddr logic. I'm paranoid something will come along and change the "effective" maxphyaddr and we'll forget all about the emulator, e.g. SEV, TME and paravirt XO all dance around maxphyaddr.
Paolo Bonzini
maintainerRe: [PATCH 3/6] KVM: x86: Add dedicated emulator helper for grabbing CPUID.maxphyaddr
Quoted 8 lines · Sean Christopherson
Quoted 3 lines
I don't think this is a particularly useful change. Yes, it's not intuitive but is it more than a matter of documentation (and possibly moving the
check_cr_writesnippet into a separate function)?I really don't like duplicating the maxphyaddr logic. I'm paranoid something will come along and change the "effective" maxphyaddr and we'll forget all about the emulator, e.g. SEV, TME and paravirt XO all dance around maxphyaddr.
Well, I'm paranoid about breaking everything else... :)
Adding a separate emulator_get_maxphyaddr function would at least simplify grepping, since searching for 0x80000008 isn't exactly intuitive.
Paolo
PATCH 4/6 KVM: x86: Drop return value from kvm_cpuid()
In reply to Sean ChristophersonRemove the boolean return from kvm_cpuid() now that all callers ignore the return value.
No functional change intended.
--- arch/x86/include/asm/kvm_emulate.h | 2 +- arch/x86/kvm/cpuid.c | 4 +--- arch/x86/kvm/cpuid.h | 2 +- arch/x86/kvm/x86.c | 4 ++-- 4 files changed, 5 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -1
@@ -220,7 +220,7 @@ struct x86_emulate_ops {struct x86_instruction_info *info, enum x86_intercept_stage stage); -bool (*get_cpuid)(struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx, +void (*get_cpuid)(struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit);int (*get_cpuid_maxphyaddr)(struct x86_emulate_ctxt *ctxt);bool (*guest_has_long_mode)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/cpuid.c +1 -3
@@ -997,8 +997,7 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function)return max && function <= max->eax; } -/* Returns true if the requested leaf/function exists in guest CPUID. */ -bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx, +void kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) {const u32 function = *eax, index = *ecx;
@@ -1049,7 +1048,6 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, } }trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, found); -return found; }EXPORT_SYMBOL_GPL (kvm_cpuid);
arch/x86/kvm/cpuid.h +1 -1
@@ -22,7 +22,7 @@ int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,int kvm_vcpu_ioctl_get_cpuid2 (struct kvm_vcpu *vcpu,struct kvm_cpuid2 *cpuid,struct kvm_cpuid_entry2 __user *entries); -bool kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx, +void kvm_cpuid (struct kvm_vcpu *vcpu,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit);int cpuid_query_maxphyaddr (struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +2 -2
@@ -6203,10 +6203,10 @@ static int emulator_intercept(struct x86_emulate_ctxt *ctxt,return kvm_x86_ops->check_intercept (emul_to_vcpu (ctxt), info, stage); } -static bool emulator_get_cpuid (struct x86_emulate_ctxt *ctxt, +static void emulator_get_cpuid (struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool check_limit) { -return kvm_cpuid (emul_to_vcpu (ctxt), eax, ebx, ecx, edx, check_limit); +kvm_cpuid (emul_to_vcpu (ctxt), eax, ebx, ecx, edx, check_limit); }static int emulator_get_cpuid_maxphyaddr (struct x86_emulate_ctxt *ctxt)
Signature · 1 line
2.24.1
PATCH 5/6 KVM: x86: Rename "found" variable in kvm_cpuid() to "exact_entry_exists"
In reply to Sean ChristophersonRename "found" in kvm_cpuid() to "exact_entry_exists" to better convey that the intent of the tracepoint's "found/not found" output is to trace whether the output values are for the actual requested leaf or for some other (likely unrelated) leaf that was found while processing entries to emulate funky CPU behavior, e.g. the max basic leaf on Intel CPUs when the requested CPUID leaf is out of range.
--- arch/x86/kvm/cpuid.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/cpuid.c +3 -3
@@ -1002,10 +1002,10 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, {const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -bool found; +bool exact_entry_exists; entry =kvm_find_cpuid_entry (vcpu, function, index); - found = entry; + exact_entry_exists = !!entry;/* * Intel CPUID semantics treats any query for an out-of-range * leaf as if the highest basic leaf (i.e. CPUID.0H:EAX) were
@@ -1047,7 +1047,7 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, } } } -trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, found); +trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, exact_entry_exists); }EXPORT_SYMBOL_GPL (kvm_cpuid);
Signature · 1 line
2.24.1
Re: [PATCH 5/6] KVM: x86: Rename "found" variable in kvm_cpuid() to "exact_entry_exists"
Quoted 31 lines · Sean Christopherson
Rename "found" in
kvm_cpuid()to "exact_entry_exists" to better convey that the intent of the tracepoint's "found/not found" output is to trace whether the output values are for the actual requested leaf or for some other (likely unrelated) leaf that was found while processing entries to emulate funky CPU behavior, e.g. the max basic leaf on Intel CPUs when the requested CPUID leaf is out of range.Suggested-by Jan Kiszka <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)arch/x86/kvm/cpuid.c +2
-2@@ -1002,10 +1002,10 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, {const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -bool found; +bool exact_entry_exists; entry =kvm_find_cpuid_entry (vcpu, function, index); - found = entry; + exact_entry_exists = !!entry;/* * Intel CPUID semantics treats any query for an out-of-range * leaf as if the highest basic leaf (i.e. CPUID.0H:EAX) were
arch/x86/kvm/cpuid.c void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1050
Quoted 5 lines
arch/x86/kvm/cpuid.c +1
-1@@ -1047,7 +1047,7 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, } } } -trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, found); +trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, exact_entry_exists);
Actually, I think we also what to change output in the tracepoint.
Jan
Quoted 4 lines
arch/x86/kvm/cpuid.c +0
-0}EXPORT_SYMBOL_GPL (kvm_cpuid);
Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE Corporate Competence Center Embedded Linux
Re: [PATCH 5/6] KVM: x86: Rename "found" variable in kvm_cpuid() to "exact_entry_exists"
Quoted 30 lines · Jan Kiszka
Quoted 29 lines · Sean Christopherson
Rename "found" in
kvm_cpuid()to "exact_entry_exists" to better convey that the intent of the tracepoint's "found/not found" output is to trace whether the output values are for the actual requested leaf or for some other (likely unrelated) leaf that was found while processing entries to emulate funky CPU behavior, e.g. the max basic leaf on Intel CPUs when the requested CPUID leaf is out of range.Suggested-by Jan Kiszka <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)arch/x86/kvm/cpuid.c +2
-2@@ -1002,10 +1002,10 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, {const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -bool found; +bool exact_entry_exists; entry =kvm_find_cpuid_entry (vcpu, function, index); - found = entry; + exact_entry_exists = !!entry;/* * Intel CPUID semantics treats any query for an out-of-range * leaf as if the highest basic leaf (i.e. CPUID.0H:EAX) were
Quoted 8 lines
arch/x86/kvm/cpuid.c void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1050
Quoted 6 lines
arch/x86/kvm/cpuid.c +1
-1@@ -1047,7 +1047,7 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, } } } -trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, found); +trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, exact_entry_exists);Actually, I think we also what to change output in the tracepoint.
Oh, I definitely want to change it, but AIUI it's ABI and shouldn't be changed. Paolo?
Re: [PATCH 5/6] KVM: x86: Rename "found" variable in kvm_cpuid() to "exact_entry_exists"
Quoted 40 lines · Sean Christopherson
Quoted 30 lines · Jan Kiszka
Quoted 29 lines · Sean Christopherson
Rename "found" in
kvm_cpuid()to "exact_entry_exists" to better convey that the intent of the tracepoint's "found/not found" output is to trace whether the output values are for the actual requested leaf or for some other (likely unrelated) leaf that was found while processing entries to emulate funky CPU behavior, e.g. the max basic leaf on Intel CPUs when the requested CPUID leaf is out of range.Suggested-by Jan Kiszka <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)arch/x86/kvm/cpuid.c +2
-2@@ -1002,10 +1002,10 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, {const u32 function = *eax, index = *ecx;struct kvm_cpuid_entry2 *entry; -bool found; +bool exact_entry_exists; entry =kvm_find_cpuid_entry (vcpu, function, index); - found = entry; + exact_entry_exists = !!entry;/* * Intel CPUID semantics treats any query for an out-of-range * leaf as if the highest basic leaf (i.e. CPUID.0H:EAX) were Quoted 8 lines
arch/x86/kvm/cpuid.c void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 1050
Quoted 6 lines
arch/x86/kvm/cpuid.c +1
-1@@ -1047,7 +1047,7 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, } } } -trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, found); +trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, exact_entry_exists);Actually, I think we also what to change output in the tracepoint.
Quoted 3 lines
Oh, I definitely want to change it, but AIUI it's ABI and shouldn't be changed. Paolo?
This can be discovered via the format string in sysfs and handled by the consumer. But I'm not an expert in the tracing ABI.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE Corporate Competence Center Embedded Linux
PATCH 6/6 KVM: x86: Add requested index to the CPUID tracepoint
In reply to Sean ChristophersonOutput the requested index when tracing CPUID emulation; it's basically mandatory for leafs where the index is meaningful, and is helpful for verifying KVM correctness even when the index isn't meaningful, e.g. the trace for a Linux guest's hypervisor_cpuid_base() probing appears to be broken (returns all zeroes) at first glance, but is correct because the index is non-zero, i.e. the output values correspond to random index in the maximum basic leaf.
--- arch/x86/kvm/cpuid.c | 3 ++- arch/x86/kvm/trace.h | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-)
arch/x86/kvm/cpuid.c +2 -1
@@ -1047,7 +1047,8 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, } } } -trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, exact_entry_exists); +trace_kvm_cpuid (function, index, *eax, *ebx, *ecx, *edx, + exact_entry_exists); }EXPORT_SYMBOL_GPL (kvm_cpuid);
arch/x86/kvm/trace.h +8 -5
@@ -151,12 +151,14 @@ TRACE_EVENT(kvm_fast_mmio,* Tracepoint for cpuid. */ TRACE_EVENT(kvm_cpuid, - TP_PROTO(unsigned int function, unsigned long rax, unsigned long rbx, - unsigned long rcx, unsigned long rdx, bool found), - TP_ARGS(function, rax, rbx, rcx, rdx, found), + TP_PROTO(unsigned int function, unsigned int index, unsigned long rax, + unsigned long rbx, unsigned long rcx, unsigned long rdx, + bool found), + TP_ARGS(function, index, rax, rbx, rcx, rdx, found), TP_STRUCT__entry( __field( unsigned int, function ) + __field( unsigned int, index ) __field( unsigned long, rax ) __field( unsigned long, rbx ) __field( unsigned long, rcx )
@@ -166,6 +168,7 @@ TRACE_EVENT(kvm_cpuid,TP_fast_assign ( __entry->function = function; + __entry->index = index; __entry->rax = rax; __entry->rbx = rbx; __entry->rcx = rcx;
@@ -173,8 +176,8 @@ TRACE_EVENT(kvm_cpuid, __entry->found = found; ), -TP_printk ("func %x rax %lx rbx %lx rcx %lx rdx %lx, cpuid entry %s" , - __entry->function, __entry->rax, +TP_printk ("func %x idx %x rax %lx rbx %lx rcx %lx rdx %lx, cpuid entry %s" , + __entry->function, __entry->index, __entry->rax, __entry->rbx, __entry->rcx, __entry->rdx, __entry->found ?"found" :"not found" ) );
Signature · 1 line
2.24.1
Re: [PATCH 6/6] KVM: x86: Add requested index to the CPUID tracepoint
Quoted 62 lines · Sean Christopherson
Output the requested index when tracing CPUID emulation; it's basically mandatory for leafs where the index is meaningful, and is helpful for verifying KVM correctness even when the index isn't meaningful, e.g. the trace for a Linux guest's
hypervisor_cpuid_base()probing appears to be broken (returns all zeroes) at first glance, but is correct because the index is non-zero, i.e. the output values correspond to random index in the maximum basic leaf.Suggested-by Xiaoyao Li <[email protected]>Cc Jan Kiszka <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 3 ++- arch/x86/kvm/trace.h | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-)arch/x86/kvm/cpuid.c +2
-1@@ -1047,7 +1047,8 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, } } } -trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, exact_entry_exists); +trace_kvm_cpuid (function, index, *eax, *ebx, *ecx, *edx, + exact_entry_exists); }EXPORT_SYMBOL_GPL (kvm_cpuid);arch/x86/kvm/trace.h +6
-3@@ -151,12 +151,14 @@ TRACE_EVENT(kvm_fast_mmio,* Tracepoint for cpuid. */ TRACE_EVENT(kvm_cpuid, - TP_PROTO(unsigned int function, unsigned long rax, unsigned long rbx, - unsigned long rcx, unsigned long rdx, bool found), - TP_ARGS(function, rax, rbx, rcx, rdx, found), + TP_PROTO(unsigned int function, unsigned int index, unsigned long rax, + unsigned long rbx, unsigned long rcx, unsigned long rdx, + bool found), + TP_ARGS(function, index, rax, rbx, rcx, rdx, found), TP_STRUCT__entry( __field( unsigned int, function ) + __field( unsigned int, index ) __field( unsigned long, rax ) __field( unsigned long, rbx ) __field( unsigned long, rcx )@@ -166,6 +168,7 @@ TRACE_EVENT(kvm_cpuid,TP_fast_assign ( __entry->function = function; + __entry->index = index; __entry->rax = rax; __entry->rbx = rbx; __entry->rcx = rcx;
arch/x86/kvm/trace.h TRACE_EVENT(kvm_cpuid, · line 183
Quoted 10 lines
arch/x86/kvm/trace.h +2
-2@@ -173,8 +176,8 @@ TRACE_EVENT(kvm_cpuid, __entry->found = found; ), -TP_printk ("func %x rax %lx rbx %lx rcx %lx rdx %lx, cpuid entry %s" , - __entry->function, __entry->rax, +TP_printk ("func %x idx %x rax %lx rbx %lx rcx %lx rdx %lx, cpuid entry %s" , + __entry->function, __entry->index, __entry->rax, __entry->rbx, __entry->rcx, __entry->rdx, __entry->found ?"found" :"not found" ) );
What happened to this patch in your v2 round?
Jan
Re: [PATCH 6/6] KVM: x86: Add requested index to the CPUID tracepoint
Quoted 65 lines · Jan Kiszka
Quoted 64 lines · Sean Christopherson
Output the requested index when tracing CPUID emulation; it's basically mandatory for leafs where the index is meaningful, and is helpful for verifying KVM correctness even when the index isn't meaningful, e.g. the trace for a Linux guest's
hypervisor_cpuid_base()probing appears to be broken (returns all zeroes) at first glance, but is correct because the index is non-zero, i.e. the output values correspond to random index in the maximum basic leaf.Suggested-by Xiaoyao Li <[email protected]>Cc Jan Kiszka <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/cpuid.c | 3 ++- arch/x86/kvm/trace.h | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-)arch/x86/kvm/cpuid.c +2
-1@@ -1047,7 +1047,8 @@ void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, } } } -trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, exact_entry_exists); +trace_kvm_cpuid (function, index, *eax, *ebx, *ecx, *edx, + exact_entry_exists); }EXPORT_SYMBOL_GPL (kvm_cpuid);arch/x86/kvm/trace.h +6
-3@@ -151,12 +151,14 @@ TRACE_EVENT(kvm_fast_mmio,* Tracepoint for cpuid. */ TRACE_EVENT(kvm_cpuid, - TP_PROTO(unsigned int function, unsigned long rax, unsigned long rbx, - unsigned long rcx, unsigned long rdx, bool found), - TP_ARGS(function, rax, rbx, rcx, rdx, found), + TP_PROTO(unsigned int function, unsigned int index, unsigned long rax, + unsigned long rbx, unsigned long rcx, unsigned long rdx, + bool found), + TP_ARGS(function, index, rax, rbx, rcx, rdx, found), TP_STRUCT__entry( __field( unsigned int, function ) + __field( unsigned int, index ) __field( unsigned long, rax ) __field( unsigned long, rbx ) __field( unsigned long, rcx )@@ -166,6 +168,7 @@ TRACE_EVENT(kvm_cpuid,TP_fast_assign ( __entry->function = function; + __entry->index = index; __entry->rax = rax; __entry->rbx = rbx; __entry->rcx = rcx;@@ -173,8 +176,8 @@ TRACE_EVENT(kvm_cpuid, __entry->found = found; ),
Quoted 10 lines
arch/x86/kvm/trace.h TRACE_EVENT(kvm_cpuid, · line 183
Quoted 8 lines
arch/x86/kvm/trace.h +2
-2-TP_printk ("func %x rax %lx rbx %lx rcx %lx rdx %lx, cpuid entry %s" , - __entry->function, __entry->rax, +TP_printk ("func %x idx %x rax %lx rbx %lx rcx %lx rdx %lx, cpuid entry %s" , + __entry->function, __entry->index, __entry->rax, __entry->rbx, __entry->rcx, __entry->rdx, __entry->found ?"found" :"not found" ) );What happened to this patch in your v2 round?
I completely forgot about it...
Paolo Bonzini
maintainerQuoted 8 lines · Sean Christopherson
Two fixes related to out-of-range CPUID emulation and related cleanup on top.
I have a unit test and also manually verified a few interesting cases. I'm not planning on posting the unit test at this time because I haven't figured out how to avoid false positives, e.g. if a random in-bounds leaf just happens to match the output of a max basic leaf. It might be doable by hardcoding the cpu model?
It would be best suited for selftests rather than kvm-unit-tests. But I don't really see the benefit of anything more than just
arch/x86/kvm/cpuid.c +2 -1
@@ -1001,6 +1001,7 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool check_limit) {u32 function = *eax, index = *ecx; +u32 orig_function = function;struct kvm_cpuid_entry2 *entry;struct kvm_cpuid_entry2 *max;bool found;
@@ -1049,7 +1050,7 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, } } } -trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, found); +trace_kvm_cpuid (orig_function, *eax, *ebx, *ecx, *edx, found);return found; }EXPORT_SYMBOL_GPL (kvm_cpuid);
Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · Sean Christopherson
Two fixes related to out-of-range CPUID emulation and related cleanup on top.
Quoted 8 lines
Quoted 5 lines
I have a unit test and also manually verified a few interesting cases. I'm not planning on posting the unit test at this time because I haven't figured out how to avoid false positives, e.g. if a random in-bounds leaf just happens to match the output of a max basic leaf. It might be doable by hardcoding the cpu model?
It would be best suited for selftests rather than kvm-unit-tests. But I don't really see the benefit of anything more than just
Gotta save those stack bytes?
I got a bit confused by the "max" variable; I thought it would hold the max basic leaf, not CPUID.0x0. Removing it seemed easier than trying to come up with a better name :-)
Quoted 22 lines
arch/x86/kvm/cpuid.c +2
-1@@ -1001,6 +1001,7 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool check_limit) {u32 function = *eax, index = *ecx; +u32 orig_function = function;struct kvm_cpuid_entry2 *entry;struct kvm_cpuid_entry2 *max;bool found;@@ -1049,7 +1050,7 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, } } } -trace_kvm_cpuid (function, *eax, *ebx, *ecx, *edx, found); +trace_kvm_cpuid (orig_function, *eax, *ebx, *ecx, *edx, found);return found; }EXPORT_SYMBOL_GPL (kvm_cpuid);