Skip to contentSkip to conversation

x86emul: further FPU env testing relaxation for AMD-like CPUs

PATCH In review Jan Beulich 2 participants 3 messages Aug 4, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86emul: further FPU env testing relaxation for AMD-like CPUs

See the code comment that's being extended. Additionally a few more zap_fpsel() invocations are needed - whenever we stored state after there potentially having been a context switch behind our backs.

tools/tests/x86_emulator/test_x86_emulator.c +25 -0
@@ -752,6 +752,13 @@ static struct x86_emulate_ops emulops =
  * 64-bit OSes may not (be able to) properly restore the two selectors in
  * the FPU environment. Zap them so that memcmp() on two saved images will
  * work regardless of whether a context switch occurred in the middle.
+ *
+ * Additionally on AMD-like CPUs FDP/FIP/FOP may get lost across context
+ * switches, when there's no unmasked pending FP exception: With
+ * CPUID[80000008].EBX[2] clear, the fields don't get written/read by
+ * {F,}XSAVE / {F,}XRSTOR, which OSes often compensate for by invoking an
+ * insn forcing the fields to gain a deterministic value. Whereas with said
+ * bit set, zeroes will get written (and hence later restored).
  */
 static void zap_fpsel(unsigned int *env, bool is_32bit)
 {
@@ -765,6 +772,21 @@ static void zap_fpsel(unsigned int *env,
         env[2] &= ~0xffff;
         env[3] &= ~0xffff;
     }
+
+    if ( cp.x86_vendor != X86_VENDOR_AMD && cp.x86_vendor != X86_VENDOR_HYGON )
+        return;
+
+    if ( is_32bit )
+    {
+        env[3] = 0;
+        env[4] = 0;
+        env[5] = 0;
+    }
+    else
+    {
+        env[1] &= 0xffff;
+        env[2] = 0;
+    }
 }
 
 static void zap_xfpsel(unsigned int *env)
@@ -2460,6 +2482,7 @@ int main(int argc, char **argv)
         regs.edx = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
         asm volatile ( "fnstenv %0" : "=m" (res[9]) :: "memory" );
+        zap_fpsel(&res[9], true);
         if ( (rc != X86EMUL_OKAY) ||
              memcmp(res + 2, res + 9, 28) ||
              (regs.eip != (unsigned long)&instr[3]) )
@@ -2487,6 +2510,7 @@ int main(int argc, char **argv)
         res[23] = 0xaa55aa55;
         res[24] = 0xaa55aa55;
         rc = x86_emulate(&ctxt, &emulops);
+        zap_fpsel(&res[0], false);
         if ( (rc != X86EMUL_OKAY) ||
              memcmp(res, res + 25, 94) ||
              (res[23] >> 16) != 0xaa55 ||
@@ -2514,6 +2538,7 @@ int main(int argc, char **argv)
         regs.edx = (unsigned long)res;
         rc = x86_emulate(&ctxt, &emulops);
         asm volatile ( "fnsave %0" : "=m" (res[27]) :: "memory" );
+        zap_fpsel(&res[27], true);
         if ( (rc != X86EMUL_OKAY) ||
              memcmp(res, res + 27, 108) ||
              (regs.eip != (unsigned long)&instr[2]) )
Acked-by Tested-by on v1 1/1
#1 lore raw
Quoted 6 lines · Jan Beulich

See the code comment that's being extended. Additionally a few more zap_fpsel() invocations are needed - whenever we stored state after there potentially having been a context switch behind our backs.

tools/tests/x86_emulator/test_x86_emulator.c static struct x86_emulate_ops emulops = · line 757

Quoted 10 lines
tools/tests/x86_emulator/test_x86_emulator.c +3 -0
@@ -752,6 +752,13 @@ static struct x86_emulate_ops emulops =
  * 64-bit OSes may not (be able to) properly restore the two selectors in
  * the FPU environment. Zap them so that memcmp() on two saved images will
  * work regardless of whether a context switch occurred in the middle.
+ *
+ * Additionally on AMD-like CPUs FDP/FIP/FOP may get lost across context
+ * switches, when there's no unmasked pending FP exception: With

I think you want a full stop rather than a colon, and ...

tools/tests/x86_emulator/test_x86_emulator.c static struct x86_emulate_ops emulops = · line 760

Quoted 3 lines
tools/tests/x86_emulator/test_x86_emulator.c +3 -0
+ * CPUID[80000008].EBX[2] clear, the fields don't get written/read by
+ * {F,}XSAVE / {F,}XRSTOR, which OSes often compensate for by invoking an
+ * insn forcing the fields to gain a deterministic value. Whereas with said

... a comma here rather than a full stop.

Having "whereas" at the beginning of a sentence like this is weird, given that you're contrasting the behaviour of the CPUID bit.

Also, the more usual CPUID syntax would be CPUID.0x80000008.EBX[2].

~Andrew

Quoted 5 lines
tools/tests/x86_emulator/test_x86_emulator.c +1 -0
+ * bit set, zeroes will get written (and hence later restored).
  */
 static void zap_fpsel(unsigned int *env, bool is_32bit)
 {
#2 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich

See the code comment that's being extended. Additionally a few more zap_fpsel() invocations are needed - whenever we stored state after there potentially having been a context switch behind our backs.

Thanks.

Quoted 11 lines

tools/tests/x86_emulator/test_x86_emulator.c static struct x86_emulate_ops emulops = · line 757

Quoted 9 lines
tools/tests/x86_emulator/test_x86_emulator.c +3 -0
@@ -752,6 +752,13 @@ static struct x86_emulate_ops emulops =
  * 64-bit OSes may not (be able to) properly restore the two selectors in
  * the FPU environment. Zap them so that memcmp() on two saved images will
  * work regardless of whether a context switch occurred in the middle.
+ *
+ * Additionally on AMD-like CPUs FDP/FIP/FOP may get lost across context
+ * switches, when there's no unmasked pending FP exception: With

I think you want a full stop rather than a colon, and ...

I'd prefer to stick to the colon here, while ...

Quoted 10 lines

tools/tests/x86_emulator/test_x86_emulator.c static struct x86_emulate_ops emulops = · line 760

Quoted 3 lines
tools/tests/x86_emulator/test_x86_emulator.c +3 -0
+ * CPUID[80000008].EBX[2] clear, the fields don't get written/read by
+ * {F,}XSAVE / {F,}XRSTOR, which OSes often compensate for by invoking an
+ * insn forcing the fields to gain a deterministic value. Whereas with said

... a comma here rather than a full stop.

Having "whereas" at the beginning of a sentence like this is weird, given that you're contrasting the behaviour of the CPUID bit.

Also, the more usual CPUID syntax would be CPUID.0x80000008.EBX[2].

... I've adjusted these.

Jan