i386/hvf: Fix for UB in handling CPUID function 0xD
authorPhil Dennis-Jordan <phil@philjordan.eu>
Tue, 5 Nov 2024 15:57:57 +0000 (16:57 +0100)
committerPaolo Bonzini <pbonzini@redhat.com>
Sat, 9 Nov 2024 07:34:07 +0000 (08:34 +0100)
commite3150028fffb808452078ead055fc6a4d50a63e9
tree31973992a06a670529e4ba3f64b3666b4de21709
parent0e27f3a5d0cbc0099ba8dcd7ff78e7f80d0c4f15
i386/hvf: Fix for UB in handling CPUID function 0xD

The handling for CPUID function 0xD (supported XSAVE features) was
improved in a recent patch. Unfortunately, this appears to have
introduced undefined behaviour for cases where ecx > 30, as the result
of (1 << idx) is undefined if idx > 30.

Per Intel SDM section 13.2, the behaviour for ecx values up to and
including 62 are specified. This change therefore specifically sets
all registers returned by the CPUID instruction to 0 for 63 and higher.
Furthermore, the bit shift uses uint64_t, where behaviour for the entire
range of 2..62 is safe and correct.

Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
Link: https://lore.kernel.org/r/20241105155800.5461-3-phil@philjordan.eu
Reviewed-by: Roman Bolshakov <r.bolshakov@yadro.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
target/i386/hvf/x86_cpuid.c