Skip to contentSkip to conversation

i386/cpu: Two minor fixes for x86_cpu_enable_xsave_components()

PATCH Applied Xiaoyao Li 4 participants 8 messages Jan 15, 2024 – Jan 25, 2024

lore
Share & tools
lore

Xiaoyao Li

author
#0 lore raw

cover i386/cpu: Two minor fixes for x86_cpu_enable_xsave_components()

The two bugs were introduced when xsaves feature was added by commit 301e90675c3f ("target/i386: Enable support for XSAVES based features").

Xiaoyao Li (2):
i386/cpu: Clear FEAT_XSAVE_XSS_LO/HI leafs when CPUID_EXT_XSAVE is not
available
i386/cpu: Mask with XCR0/XSS mask for FEAT_XSAVE_XCR0_HI and
FEAT_XSAVE_XSS_HI leafs

 target/i386/cpu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 i386/cpu: Clear FEAT_XSAVE_XSS_LO/HI leafs when CPUID_EXT_XSAVE is not available

Leaf FEAT_XSAVE_XSS_LO and FEAT_XSAVE_XSS_HI also need to be cleared when CPUID_EXT_XSAVE is not set.

Fixes 301e90675c3f ("target/i386: Enable support for XSAVES based features")
---
 target/i386/cpu.c | 2 ++
 1 file changed, 2 insertions(+)
target/i386/cpu.c +2 -0
index 2524881ce245..b445e2957c4f 100644
@@ -6926,6 +6926,8 @@ static void x86_cpu_enable_xsave_components(X86CPU *cpu)
     if (!(env->features[FEAT_1_ECX] & CPUID_EXT_XSAVE)) {
         env->features[FEAT_XSAVE_XCR0_LO] = 0;
         env->features[FEAT_XSAVE_XCR0_HI] = 0;
+        env->features[FEAT_XSAVE_XSS_LO] = 0;
+        env->features[FEAT_XSAVE_XSS_HI] = 0;
         return;
     }
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] i386/cpu: Clear FEAT_XSAVE_XSS_LO/HI leafs when CPUID_EXT_XSAVE is not available

Quoted 14 lines · Xiaoyao

Leaf FEAT_XSAVE_XSS_LO and FEAT_XSAVE_XSS_HI also need to be cleared when CPUID_EXT_XSAVE is not set.

Fixes 301e90675c3f ("target/i386: Enable support for XSAVES based features")
---
  target/i386/cpu.c | 2 ++
  1 file changed, 2 insertions(+)
target/i386/cpu.c +0 -0
index 2524881ce245..b445e2957c4f 100644

target/i386/cpu.c static void x86_cpu_enable_xsave_components(X86CPU *cpu) · line 6932

Quoted 7 lines
target/i386/cpu.c +2 -0
@@ -6926,6 +6926,8 @@ static void x86_cpu_enable_xsave_components(X86CPU *cpu)
      if (!(env->features[FEAT_1_ECX] & CPUID_EXT_XSAVE)) {
          env->features[FEAT_XSAVE_XCR0_LO] = 0;
          env->features[FEAT_XSAVE_XCR0_HI] = 0;
+        env->features[FEAT_XSAVE_XSS_LO] = 0;
+        env->features[FEAT_XSAVE_XSS_HI] = 0;
          return;
      }

Thanks for fixing this!

Quoted 1 line
author
#3 lore raw

PATCH 2/2 i386/cpu: Mask with XCR0/XSS mask for FEAT_XSAVE_XCR0_HI and FEAT_XSAVE_XSS_HI leafs

In reply to Xiaoyao Li

The value of FEAT_XSAVE_XCR0_HI leaf and FEAT_XSAVE_XSS_HI leaf also need to be masked by XCR0 and XSS mask respectively, to make it logically correct.

Fixes 301e90675c3f ("target/i386: Enable support for XSAVES based features")
---
 target/i386/cpu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/i386/cpu.c +2 -2
index b445e2957c4f..a5c08944a483 100644
@@ -6946,9 +6946,9 @@ static void x86_cpu_enable_xsave_components(X86CPU *cpu)
     }
 
     env->features[FEAT_XSAVE_XCR0_LO] = mask & CPUID_XSTATE_XCR0_MASK;
-    env->features[FEAT_XSAVE_XCR0_HI] = mask >> 32;
+    env->features[FEAT_XSAVE_XCR0_HI] = (mask & CPUID_XSTATE_XCR0_MASK) >> 32;
     env->features[FEAT_XSAVE_XSS_LO] = mask & CPUID_XSTATE_XSS_MASK;
-    env->features[FEAT_XSAVE_XSS_HI] = mask >> 32;
+    env->features[FEAT_XSAVE_XSS_HI] = (mask & CPUID_XSTATE_XSS_MASK) >> 32;
 }
 
 /***** Steps involved on loading and filtering CPUID data
Signature · 1 line
2.34.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] i386/cpu: Mask with XCR0/XSS mask for FEAT_XSAVE_XCR0_HI and FEAT_XSAVE_XSS_HI leafs

Quoted 15 lines · Xiaoyao

The value of FEAT_XSAVE_XCR0_HI leaf and FEAT_XSAVE_XSS_HI leaf also need to be masked by XCR0 and XSS mask respectively, to make it logically correct.

Fixes 301e90675c3f ("target/i386: Enable support for XSAVES based features")
---
  target/i386/cpu.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
target/i386/cpu.c +0 -0
index b445e2957c4f..a5c08944a483 100644

target/i386/cpu.c static void x86_cpu_enable_xsave_components(X86CPU *cpu) · line 6952

Quoted 9 lines
target/i386/cpu.c +2 -2
@@ -6946,9 +6946,9 @@ static void x86_cpu_enable_xsave_components(X86CPU *cpu)
      }
 
      env->features[FEAT_XSAVE_XCR0_LO] = mask & CPUID_XSTATE_XCR0_MASK;
-    env->features[FEAT_XSAVE_XCR0_HI] = mask >> 32;
+    env->features[FEAT_XSAVE_XCR0_HI] = (mask & CPUID_XSTATE_XCR0_MASK) >> 32;
      env->features[FEAT_XSAVE_XSS_LO] = mask & CPUID_XSTATE_XSS_MASK;
-    env->features[FEAT_XSAVE_XSS_HI] = mask >> 32;
+    env->features[FEAT_XSAVE_XSS_HI] = (mask & CPUID_XSTATE_XSS_MASK) >> 32;
  }

Thanks for fixing this!

Quoted 2 lines
target/i386/cpu.c +0 -0
  /***** Steps involved on loading and filtering CPUID data
#5 lore raw
In reply to Xiaoyao Li

Hi Xiaoyao,

Quoted 8 lines · Xiaoyao Li

Date: Mon, 15 Jan 2024 04:13:23 -0500
From: Xiaoyao Li <[email protected]>
Subject: [PATCH 0/2] i386/cpu: Two minor fixes for
x86_cpu_enable_xsave_components()
X-Mailer: git-send-email 2.34.1

The two bugs were introduced when xsaves feature was added by commit 301e90675c3f ("target/i386: Enable support for XSAVES based features").

Could you please provide more details about reproducing these two bugs? If I'm able, I'd be glad to help you to test and verify them.

Regards,
Zhao

Quoted 14 lines

Xiaoyao Li (2):
i386/cpu: Clear FEAT_XSAVE_XSS_LO/HI leafs when CPUID_EXT_XSAVE is not
available
i386/cpu: Mask with XCR0/XSS mask for FEAT_XSAVE_XCR0_HI and
FEAT_XSAVE_XSS_HI leafs

 target/i386/cpu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
author
#6 lore raw
Quoted 9 lines · Zhao Liu

Hi Xiaoyao,

Quoted 6 lines · Xiaoyao Li

Date: Mon, 15 Jan 2024 04:13:23 -0500
From: Xiaoyao Li <[email protected]>
Subject: [PATCH 0/2] i386/cpu: Two minor fixes for
x86_cpu_enable_xsave_components()
X-Mailer: git-send-email 2.34.1

Quoted 5 lines
Quoted 2 lines

The two bugs were introduced when xsaves feature was added by commit 301e90675c3f ("target/i386: Enable support for XSAVES based features").

Could you please provide more details about reproducing these two bugs? If I'm able, I'd be glad to help you to test and verify them.

There are potential bugs and currently we don't have test step to trigger it. Because for patch 1, KVM doesn't support arch-lbr virtualization yet, which is the first user in QEMU of xss. Once KVM merges the arch-lbr series, using "-cpu xxx,+arch-lbr,-xsave" can expose arch-lbr to guest, which violates the architectural behavior of xfeatures.

For patch2, current code just happens to work correctly because there is not xfeature in upper 32-bit get defined yet. But I think make the code logically correct is important and we shouldn't depend on the happened-to-work code.

Quoted 17 lines

Regards,
Zhao

Quoted 14 lines

Xiaoyao Li (2):
i386/cpu: Clear FEAT_XSAVE_XSS_LO/HI leafs when CPUID_EXT_XSAVE is not
available
i386/cpu: Mask with XCR0/XSS mask for FEAT_XSAVE_XCR0_HI and
FEAT_XSAVE_XSS_HI leafs

  target/i386/cpu.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1