Skip to contentSkip to conversation

x86emul: FPU handling corrections

PATCH Superseded Jan Beulich 4 participants 16 messages Mar 13, 2017 – Mar 14, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/4 x86emul: fold exit paths

Move "cannot_emulate" and make it go through the common (error) exit path.

xen/arch/x86/x86_emulate/x86_emulate.c +3 -6
@@ -7762,7 +7762,9 @@ x86_emulate(
     }
 
     default:
-        goto cannot_emulate;
+    cannot_emulate:
+        rc = X86EMUL_UNHANDLEABLE;
+        goto done;
     }
 
     if ( state->simd_size )
@@ -7906,11 +7908,6 @@ x86_emulate(
     _put_fpu();
     put_stub(stub);
     return rc;
-
- cannot_emulate:
-    _put_fpu();
-    put_stub(stub);
-    return X86EMUL_UNHANDLEABLE;
 #undef state
 }
 

x86emul: fold exit paths

Move "cannot_emulate" and make it go through the common (error) exit path.

xen/arch/x86/x86_emulate/x86_emulate.c +3 -6
@@ -7762,7 +7762,9 @@ x86_emulate(
     }
 
     default:
-        goto cannot_emulate;
+    cannot_emulate:
+        rc = X86EMUL_UNHANDLEABLE;
+        goto done;
     }
 
     if ( state->simd_size )
@@ -7906,11 +7908,6 @@ x86_emulate(
     _put_fpu();
     put_stub(stub);
     return rc;
-
- cannot_emulate:
-    _put_fpu();
-    put_stub(stub);
-    return X86EMUL_UNHANDLEABLE;
 #undef state
 }
 

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw

PATCH 2/4 x86emul: centralize put_fpu() invocations

In reply to Jan Beulich

..., splitting parts of it into check_*() macros. This is in preparation of making ->put_fpu() do further adjustments to register state. (Some of the check_xmm() invocations could be avoided, as in some of the cases no insns handled there can actually raise #XM, but I think we're better off keeping them to avoid later additions of further insn patterns rendering the lack of the check a bug.)

xen/arch/x86/x86_emulate/x86_emulate.c +41 -33
@@ -937,6 +937,7 @@ do {
 
 struct fpu_insn_ctxt {
     uint8_t insn_bytes;
+    uint8_t type;
     int8_t exn_raised;
 };
 
@@ -956,8 +957,6 @@ static int _get_fpu(
 {
     int rc;
 
-    fic->exn_raised = -1;
-
     fail_if(!ops->get_fpu);
     rc = ops->get_fpu(fpu_handle_exception, fic, type, ctxt);
 
@@ -965,6 +964,8 @@ static int _get_fpu(
     {
         unsigned long cr0;
 
+        fic->type = type;
+
         fail_if(!ops->read_cr);
         if ( type >= X86EMUL_FPU_xmm )
         {
@@ -1006,22 +1007,31 @@ do {
     rc = _get_fpu(_type, _fic, ctxt, ops);                      \
     if ( rc ) goto done;                                        \
 } while (0)
-#define _put_fpu()                                              \
+
+#define check_fpu(_fic)                                         \
 do {                                                            \
-    if ( ops->put_fpu != NULL )                                 \
-        (ops->put_fpu)(ctxt);                                   \
+    generate_exception_if((_fic)->exn_raised >= 0,              \
+                          (_fic)->exn_raised);                  \
 } while (0)
-#define put_fpu(_fic)                                           \
+
+#define check_xmm(_fic)                                         \
 do {                                                            \
-    _put_fpu();                                                 \
     if ( (_fic)->exn_raised == EXC_XM && ops->read_cr &&        \
          ops->read_cr(4, &cr4, ctxt) == X86EMUL_OKAY &&         \
          !(cr4 & X86_CR4_OSXMMEXCPT) )				\
         (_fic)->exn_raised = EXC_UD;                            \
-    generate_exception_if((_fic)->exn_raised >= 0,              \
-                          (_fic)->exn_raised);                  \
+    check_fpu(_fic);                                            \
 } while (0)
 
+static void put_fpu(
+    struct fpu_insn_ctxt *fic,
+    struct x86_emulate_ctxt *ctxt,
+    const struct x86_emulate_ops *ops)
+{
+    if ( fic->type != X86EMUL_FPU_none && ops->put_fpu )
+        ops->put_fpu(ctxt);
+}
+
 static inline bool fpu_check_write(void)
 {
     uint16_t fsw;
@@ -3015,7 +3025,7 @@ x86_emulate(
     struct operand dst = { .reg = PTR_POISON };
     unsigned long cr4;
     enum x86_swint_type swint_type;
-    struct fpu_insn_ctxt fic;
+    struct fpu_insn_ctxt fic = { .type = X86EMUL_FPU_none, .exn_raised = -1 };
     struct x86_emulate_stub stub = {};
     DECLARE_ALIGNED(mmval_t, mmval);
 
@@ -3708,7 +3718,7 @@ x86_emulate(
         host_and_vcpu_must_have(fpu);
         get_fpu(X86EMUL_FPU_wait, &fic);
         asm volatile ( "fwait" ::: "memory" );
-        put_fpu(&fic);
+        check_fpu(&fic);
         break;
 
     case 0x9c: /* pushf */
@@ -4153,7 +4163,7 @@ x86_emulate(
                 break;
             }
         }
-        put_fpu(&fic);
+        check_fpu(&fic);
         break;
 
     case 0xd9: /* FPU 0xd9 */
@@ -4242,7 +4252,7 @@ x86_emulate(
             if ( dst.type == OP_MEM && dst.bytes == 4 && !fpu_check_write() )
                 dst.type = OP_NONE;
         }
-        put_fpu(&fic);
+        check_fpu(&fic);
         break;
 
     case 0xda: /* FPU 0xda */
@@ -4293,7 +4303,7 @@ x86_emulate(
                 break;
             }
         }
-        put_fpu(&fic);
+        check_fpu(&fic);
         break;
 
     case 0xdb: /* FPU 0xdb */
@@ -4365,7 +4375,7 @@ x86_emulate(
             if ( dst.type == OP_MEM && !fpu_check_write() )
                 dst.type = OP_NONE;
         }
-        put_fpu(&fic);
+        check_fpu(&fic);
         break;
 
     case 0xdc: /* FPU 0xdc */
@@ -4416,7 +4426,7 @@ x86_emulate(
                 break;
             }
         }
-        put_fpu(&fic);
+        check_fpu(&fic);
         break;
 
     case 0xdd: /* FPU 0xdd */
@@ -4475,7 +4485,7 @@ x86_emulate(
             if ( dst.type == OP_MEM && dst.bytes == 8 && !fpu_check_write() )
                 dst.type = OP_NONE;
         }
-        put_fpu(&fic);
+        check_fpu(&fic);
         break;
 
     case 0xde: /* FPU 0xde */
@@ -4523,7 +4533,7 @@ x86_emulate(
                 break;
             }
         }
-        put_fpu(&fic);
+        check_fpu(&fic);
         break;
 
     case 0xdf: /* FPU 0xdf */
@@ -4605,7 +4615,7 @@ x86_emulate(
             if ( dst.type == OP_MEM && !fpu_check_write() )
                 dst.type = OP_NONE;
         }
-        put_fpu(&fic);
+        check_fpu(&fic);
         break;
 
     case 0xe0 ... 0xe2: /* loop{,z,nz} */ {
@@ -5667,7 +5677,7 @@ x86_emulate(
                             : "c" (mmvalp), "m" (*mmvalp));
 
         put_stub(stub);
-        put_fpu(&fic);
+        check_xmm(&fic);
 
         state->simd_size = simd_none;
         break;
@@ -5719,7 +5729,7 @@ x86_emulate(
                       [mask] "i" (EFLAGS_MASK));
 
         put_stub(stub);
-        put_fpu(&fic);
+        check_xmm(&fic);
 
         ASSERT(!state->simd_size);
         break;
@@ -5903,7 +5913,7 @@ x86_emulate(
         invoke_stub("", "", "=a" (dst.val) : [dummy] "i" (0));
 
         put_stub(stub);
-        put_fpu(&fic);
+        check_xmm(&fic);
 
         ASSERT(!state->simd_size);
         dst.bytes = 4;
@@ -6111,7 +6121,7 @@ x86_emulate(
         dst.val = src.val;
 
         put_stub(stub);
-        put_fpu(&fic);
+        check_xmm(&fic);
 
         ASSERT(!state->simd_size);
         break;
@@ -6253,7 +6263,7 @@ x86_emulate(
         invoke_stub("", "", [dummy_out] "=g" (dummy) : [dummy_in] "i" (0) );
 
         put_stub(stub);
-        put_fpu(&fic);
+        check_xmm(&fic);
 
         ASSERT(!state->simd_size);
         break;
@@ -6323,8 +6333,6 @@ x86_emulate(
                     asm volatile ( ".byte 0xc5,0xc1,0xeb,0xff" );
                 }
 
-                put_fpu(&fic);
-
                 ASSERT(!state->simd_size);
                 break;
             }
@@ -7070,10 +7078,7 @@ x86_emulate(
 
         put_stub(stub);
         if ( !ea.val )
-        {
-            put_fpu(&fic);
             goto complete_insn;
-        }
 
         opc = init_prefixes(stub);
         opc[0] = b;
@@ -7221,7 +7226,7 @@ x86_emulate(
         emulate_stub("+m" (*mmvalp), "a" (mmvalp));
 
         put_stub(stub);
-        put_fpu(&fic);
+        check_xmm(&fic);
 
         state->simd_size = simd_none;
         dst.type = OP_NONE;
@@ -7525,7 +7530,7 @@ x86_emulate(
         invoke_stub("", "", "=m" (dst.val) : "a" (&dst.val));
 
         put_stub(stub);
-        put_fpu(&fic);
+        check_xmm(&fic);
 
         ASSERT(!state->simd_size);
         dst.bytes = dst.type == OP_REG || b == 0x17 ? 4 : 1 << (b & 3);
@@ -7841,7 +7846,7 @@ x86_emulate(
             invoke_stub("", "", "+m" (*mmvalp) : "D" (mmvalp));
 
         put_stub(stub);
-        put_fpu(&fic);
+        check_xmm(&fic);
     }
 
     switch ( dst.type )
@@ -7883,6 +7888,8 @@ x86_emulate(
     }
 
  complete_insn: /* Commit shadow register state. */
+    put_fpu(&fic, ctxt, ops);
+
     /* Zero the upper 32 bits of %rip if not in 64-bit mode. */
     if ( !mode_64bit() )
         _regs.r(ip) = _regs.eip;
@@ -7905,7 +7912,8 @@ x86_emulate(
     ctxt->regs->eflags &= ~X86_EFLAGS_RF;
 
  done:
-    _put_fpu();
+    if ( rc != X86EMUL_OKAY )
+        put_fpu(&fic, ctxt, ops);
     put_stub(stub);
     return rc;
 #undef state
xen/arch/x86/x86_emulate/x86_emulate.h +3 -1
@@ -161,7 +161,9 @@ enum x86_emulate_fpu_type {
     X86EMUL_FPU_wait, /* WAIT/FWAIT instruction */
     X86EMUL_FPU_mmx, /* MMX instruction set (%mm0-%mm7) */
     X86EMUL_FPU_xmm, /* SSE instruction set (%xmm0-%xmm7/15) */
-    X86EMUL_FPU_ymm  /* AVX/XOP instruction set (%ymm0-%ymm7/15) */
+    X86EMUL_FPU_ymm, /* AVX/XOP instruction set (%ymm0-%ymm7/15) */
+    /* This sentinel will never be passed to ->get_fpu(). */
+    X86EMUL_FPU_none
 };
 
 struct cpuid_leaf

x86emul: centralize put_fpu() invocations

..., splitting parts of it into check_*() macros. This is in preparation of making ->put_fpu() do further adjustments to register state. (Some of the check_xmm() invocations could be avoided, as in some of the cases no insns handled there can actually raise #XM, but I think we're better off keeping them to avoid later additions of further insn patterns rendering the lack of the check a bug.)

xen/arch/x86/x86_emulate/x86_emulate.c +41 -33

240 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.h +3 -1
@@ -161,7 +161,9 @@ enum x86_emulate_fpu_type {
     X86EMUL_FPU_wait, /* WAIT/FWAIT instruction */
     X86EMUL_FPU_mmx, /* MMX instruction set (%mm0-%mm7) */
     X86EMUL_FPU_xmm, /* SSE instruction set (%xmm0-%xmm7/15) */
-    X86EMUL_FPU_ymm  /* AVX/XOP instruction set (%ymm0-%ymm7/15) */
+    X86EMUL_FPU_ymm, /* AVX/XOP instruction set (%ymm0-%ymm7/15) */
+    /* This sentinel will never be passed to ->get_fpu(). */
+    X86EMUL_FPU_none
 };
 
 struct cpuid_leaf

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw

Re: [PATCH 2/4] x86emul: centralize put_fpu() invocations

Quoted 53 lines · Jan Beulich

..., splitting parts of it into check_*() macros. This is in preparation of making ->put_fpu() do further adjustments to register state. (Some of the check_xmm() invocations could be avoided, as in some of the cases no insns handled there can actually raise #XM, but I think we're better off keeping them to avoid later additions of further insn patterns rendering the lack of the check a bug.)

xen/arch/x86/x86_emulate/x86_emulate.c +9 -6
@@ -937,6 +937,7 @@ do {
 
 struct fpu_insn_ctxt {
     uint8_t insn_bytes;
+    uint8_t type;
     int8_t exn_raised;
 };
 
@@ -956,8 +957,6 @@ static int _get_fpu(
 {
     int rc;
 
-    fic->exn_raised = -1;
-
     fail_if(!ops->get_fpu);
     rc = ops->get_fpu(fpu_handle_exception, fic, type, ctxt);
 
@@ -965,6 +964,8 @@ static int _get_fpu(
     {
         unsigned long cr0;
 
+        fic->type = type;
+
         fail_if(!ops->read_cr);
         if ( type >= X86EMUL_FPU_xmm )
         {
@@ -1006,22 +1007,31 @@ do {
     rc = _get_fpu(_type, _fic, ctxt, ops);                      \
     if ( rc ) goto done;                                        \
 } while (0)
-#define _put_fpu()                                              \
+
+#define check_fpu(_fic)                                         \
 do {                                                            \
-    if ( ops->put_fpu != NULL )                                 \
-        (ops->put_fpu)(ctxt);                                   \
+    generate_exception_if((_fic)->exn_raised >= 0,              \
+                          (_fic)->exn_raised);                  \
 } while (0)
-#define put_fpu(_fic)                                           \
+
+#define check_xmm(_fic)                                         \

xen/arch/x86/x86_emulate/x86_emulate.c do { · line 1024

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -3
 do {                                                            \
-    _put_fpu();                                                 \
     if ( (_fic)->exn_raised == EXC_XM && ops->read_cr &&        \
          ops->read_cr(4, &cr4, ctxt) == X86EMUL_OKAY &&         \
          !(cr4 & X86_CR4_OSXMMEXCPT) )				\
         (_fic)->exn_raised = EXC_UD;                            \
-    generate_exception_if((_fic)->exn_raised >= 0,              \
-                          (_fic)->exn_raised);                  \
+    check_fpu(_fic);                                            \
 } while (0)

These two check macros seem like they should have an _exn() suffix to make it clearer what they are actually doing.

Quoted 14 lines
xen/arch/x86/x86_emulate/x86_emulate.c +9 -0
+static void put_fpu(
+    struct fpu_insn_ctxt *fic,
+    struct x86_emulate_ctxt *ctxt,
+    const struct x86_emulate_ops *ops)
+{
+    if ( fic->type != X86EMUL_FPU_none && ops->put_fpu )
+        ops->put_fpu(ctxt);
+}
+
 static inline bool fpu_check_write(void)
 {
     uint16_t fsw;

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 7916

Quoted 6 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -1
@@ -7905,7 +7912,8 @@ x86_emulate(
     ctxt->regs->eflags &= ~X86_EFLAGS_RF;
 
  done:
-    _put_fpu();
+    if ( rc != X86EMUL_OKAY )
+        put_fpu(&fic, ctxt, ops);

Unless you assert at complete_insn that rc == X86EMUL_OKAY, you still might call put_fpu() twice.

You can avoid the conditional here by setting fic->type to X86EMUL_FPU_none at the end of put_fpu(), which looks like it is compatible with your later modifications.

xen/arch/x86/x86_emulate/x86_emulate.h enum x86_emulate_fpu_type { · line 165

Quoted 12 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
     put_stub(stub);
     return rc;
 #undef state
xen/arch/x86/x86_emulate/x86_emulate.h +2 -1
@@ -161,7 +161,9 @@ enum x86_emulate_fpu_type {
     X86EMUL_FPU_wait, /* WAIT/FWAIT instruction */
     X86EMUL_FPU_mmx, /* MMX instruction set (%mm0-%mm7) */
     X86EMUL_FPU_xmm, /* SSE instruction set (%xmm0-%xmm7/15) */
-    X86EMUL_FPU_ymm  /* AVX/XOP instruction set (%ymm0-%ymm7/15) */
+    X86EMUL_FPU_ymm, /* AVX/XOP instruction set (%ymm0-%ymm7/15) */
+    /* This sentinel will never be passed to ->get_fpu(). */

Don't you mean ->put_fpu() here? Either way, can we assert this fact?

~Andrew

xen/arch/x86/x86_emulate/x86_emulate.h enum x86_emulate_fpu_type { · line 169

Quoted 6 lines
xen/arch/x86/x86_emulate/x86_emulate.h +1 -0
+    X86EMUL_FPU_none
 };
 
 struct cpuid_leaf

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw

Re: [PATCH 2/4] x86emul: centralize put_fpu() invocations

Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 13.03.17 at 12:55, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +6 -5
@@ -1006,22 +1007,31 @@ do {
     rc = _get_fpu(_type, _fic, ctxt, ops);                      \
     if ( rc ) goto done;                                        \
 } while (0)
-#define _put_fpu()                                              \
+
+#define check_fpu(_fic)                                         \
 do {                                                            \
-    if ( ops->put_fpu != NULL )                                 \
-        (ops->put_fpu)(ctxt);                                   \
+    generate_exception_if((_fic)->exn_raised >= 0,              \
+                          (_fic)->exn_raised);                  \
 } while (0)
-#define put_fpu(_fic)                                           \
+
+#define check_xmm(_fic)                                         \
 do {                                                            \
-    _put_fpu();                                                 \
     if ( (_fic)->exn_raised == EXC_XM && ops->read_cr &&        \
          ops->read_cr(4, &cr4, ctxt) == X86EMUL_OKAY &&         \
          !(cr4 & X86_CR4_OSXMMEXCPT) )				\
Quoted 8 lines

xen/arch/x86/x86_emulate/x86_emulate.c do { · line 1024

Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -2
         (_fic)->exn_raised = EXC_UD;                            \
-    generate_exception_if((_fic)->exn_raised >= 0,              \
-                          (_fic)->exn_raised);                  \
+    check_fpu(_fic);                                            \
 } while (0)

These two check macros seem like they should have an _exn() suffix to make it clearer what they are actually doing.

Will do.

Quoted 10 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 7916

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -1
@@ -7905,7 +7912,8 @@ x86_emulate(
     ctxt->regs->eflags &= ~X86_EFLAGS_RF;
 
  done:
-    _put_fpu();
+    if ( rc != X86EMUL_OKAY )
+        put_fpu(&fic, ctxt, ops);

Unless you assert at complete_insn that rc == X86EMUL_OKAY, you still might call put_fpu() twice.

Well, reaching complete_insn with rc != X86EMUL_OKAY is possible, and hence can't be asserted. See the X86EMUL_DONE handling there. One could assert right before the done label, but then again reaching complete_insn with any other X86EMUL_* result would have been a bug even before this series. But anyway, I guess I'll rather do ...

Quoted 3 lines

You can avoid the conditional here by setting fic->type to X86EMUL_FPU_none at the end of put_fpu(), which looks like it is compatible with your later modifications.

... this.

Quoted 11 lines

xen/arch/x86/x86_emulate/x86_emulate.h enum x86_emulate_fpu_type { · line 165

Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.h +2 -1
@@ -161,7 +161,9 @@ enum x86_emulate_fpu_type {
     X86EMUL_FPU_wait, /* WAIT/FWAIT instruction */
     X86EMUL_FPU_mmx, /* MMX instruction set (%mm0-%mm7) */
     X86EMUL_FPU_xmm, /* SSE instruction set (%xmm0-%xmm7/15) */
-    X86EMUL_FPU_ymm  /* AVX/XOP instruction set (%ymm0-%ymm7/15) */
+    X86EMUL_FPU_ymm, /* AVX/XOP instruction set (%ymm0-%ymm7/15) */
+    /* This sentinel will never be passed to ->get_fpu(). */

Don't you mean ->put_fpu() here?

Definitely not, as patch 3 would be violating this then immediately.

Quoted 1 line

Either way, can we assert this fact?

I did consider this, but decided against because of the scalability aspect (every get_fpu() implementation would then have to have this). But then again, perhaps you'd be fine with the assertion being put in _get_fpu(), right next to the call of the hook?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#6 lore raw

PATCH 3/4 x86emul: correct handling of FPU insns faulting on memory write

In reply to Jan Beulich

When an FPU instruction with a memory destination fails during the memory write, it should not affect FPU register state. Due to the way we emulate FPU (and SIMD) instructions, we can only guarantee this by
- backing out changes to the FPU register state in such a case or
- doing a descriptor read and/or page walk up front, perhaps with the stubs accessing the actual memory location then.
The latter would require a significant change in how the emulator does its guest memory accessing, so for now the former variant is being chosen.

---
Note that the state save overhead (unless state hadn't been loaded at all before, which should only be possible if a guest is fiddling with the instruction stream under emulation) is taken for every FPU insn hitting the emulator. We could reduce this to just the ones writing to memory, but that would involve quite a few further changes and resulting code where even more code paths need to match up with one another.

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +1 -0
@@ -433,6 +433,7 @@ static struct x86_emulate_ops fuzz_emulo
     SET(wbinvd),
     SET(invlpg),
     .get_fpu    = emul_test_get_fpu,
+    .put_fpu    = emul_test_put_fpu,
     .cpuid      = emul_test_cpuid,
 };
 #undef SET
tools/tests/x86_emulator/test_x86_emulator.c +1 -0
@@ -293,6 +293,7 @@ static struct x86_emulate_ops emulops =
     .read_cr    = emul_test_read_cr,
     .read_msr   = read_msr,
     .get_fpu    = emul_test_get_fpu,
+    .put_fpu    = emul_test_put_fpu,
 };
 
 int main(int argc, char **argv)
tools/tests/x86_emulator/x86_emulate.c +7 -0
@@ -138,4 +138,11 @@ int emul_test_get_fpu(
     return X86EMUL_OKAY;
 }
 
+void emul_test_put_fpu(
+    struct x86_emulate_ctxt *ctxt,
+    enum x86_emulate_fpu_type backout)
+{
+    /* TBD */
+}
+
 #include "x86_emulate/x86_emulate.c"
tools/tests/x86_emulator/x86_emulate.h +4 -0
@@ -178,3 +178,7 @@ int emul_test_get_fpu(
     void *exception_callback_arg,
     enum x86_emulate_fpu_type type,
     struct x86_emulate_ctxt *ctxt);
+
+void emul_test_put_fpu(
+    struct x86_emulate_ctxt *ctxt,
+    enum x86_emulate_fpu_type backout);
xen/arch/x86/hvm/emulate.c +45 -1

67 lines not on this page · raw message

xen/arch/x86/hvm/svm/svm.c +1 -0
@@ -2268,6 +2268,7 @@ static struct hvm_function_table __initd
     .update_guest_cr      = svm_update_guest_cr,
     .update_guest_efer    = svm_update_guest_efer,
     .update_guest_vendor  = svm_update_guest_vendor,
+    .fpu_leave            = svm_fpu_leave,
     .set_guest_pat        = svm_set_guest_pat,
     .get_guest_pat        = svm_get_guest_pat,
     .set_tsc_offset       = svm_set_tsc_offset,
xen/arch/x86/hvm/vmx/vmx.c +1 -0
@@ -2128,6 +2128,7 @@ static struct hvm_function_table __initd
     .update_guest_cr      = vmx_update_guest_cr,
     .update_guest_efer    = vmx_update_guest_efer,
     .update_guest_vendor  = vmx_update_guest_vendor,
+    .fpu_leave            = vmx_fpu_leave,
     .set_guest_pat        = vmx_set_guest_pat,
     .get_guest_pat        = vmx_get_guest_pat,
     .set_tsc_offset       = vmx_set_tsc_offset,
xen/arch/x86/x86_emulate/x86_emulate.c +9 -5
@@ -964,6 +964,7 @@ static int _get_fpu(
     {
         unsigned long cr0;
 
+        fail_if(type == X86EMUL_FPU_fpu && !ops->put_fpu);
         fic->type = type;
 
         fail_if(!ops->read_cr);
@@ -1025,11 +1026,14 @@ do {
 
 static void put_fpu(
     struct fpu_insn_ctxt *fic,
+    bool failed_late,
     struct x86_emulate_ctxt *ctxt,
     const struct x86_emulate_ops *ops)
 {
-    if ( fic->type != X86EMUL_FPU_none && ops->put_fpu )
-        ops->put_fpu(ctxt);
+    if ( unlikely(failed_late) && fic->type == X86EMUL_FPU_fpu )
+        ops->put_fpu(ctxt, X86EMUL_FPU_fpu);
+    else if ( fic->type != X86EMUL_FPU_none && ops->put_fpu )
+        ops->put_fpu(ctxt, X86EMUL_FPU_none);
 }
 
 static inline bool fpu_check_write(void)
@@ -3714,9 +3718,9 @@ x86_emulate(
         break;
 
     case 0x9b:  /* wait/fwait */
-        fic.insn_bytes = 1;
         host_and_vcpu_must_have(fpu);
         get_fpu(X86EMUL_FPU_wait, &fic);
+        fic.insn_bytes = 1;
         asm volatile ( "fwait" ::: "memory" );
         check_fpu(&fic);
         break;
@@ -7888,7 +7892,7 @@ x86_emulate(
     }
 
  complete_insn: /* Commit shadow register state. */
-    put_fpu(&fic, ctxt, ops);
+    put_fpu(&fic, false, ctxt, ops);
 
     /* Zero the upper 32 bits of %rip if not in 64-bit mode. */
     if ( !mode_64bit() )
@@ -7913,7 +7917,7 @@ x86_emulate(
 
  done:
     if ( rc != X86EMUL_OKAY )
-        put_fpu(&fic, ctxt, ops);
+        put_fpu(&fic, fic.insn_bytes > 0 && dst.type == OP_MEM, ctxt, ops);
     put_stub(stub);
     return rc;
 #undef state
xen/arch/x86/x86_emulate/x86_emulate.h +4 -1
@@ -437,9 +437,12 @@ struct x86_emulate_ops
      * put_fpu: Relinquish the FPU. Unhook from FPU/SIMD exception handlers.
      *  The handler, if installed, must be prepared to get called without
      *  the get_fpu one having got called before!
+     * @backout: Undo updates to the specified register file (can, besides
+     *           X86EMUL_FPU_none, only be X86EMUL_FPU_fpu at present);
      */
     void (*put_fpu)(
-        struct x86_emulate_ctxt *ctxt);
+        struct x86_emulate_ctxt *ctxt,
+        enum x86_emulate_fpu_type backout);
 
     /* invlpg: Invalidate paging structures which map addressed byte. */
     int (*invlpg)(
xen/include/asm-x86/hvm/hvm.h +2 -0
@@ -140,6 +140,8 @@ struct hvm_function_table {
 
     void (*update_guest_vendor)(struct vcpu *v);
 
+    void (*fpu_leave)(struct vcpu *v);
+
     int  (*get_guest_pat)(struct vcpu *v, u64 *);
     int  (*set_guest_pat)(struct vcpu *v, u64);
 

x86emul: correct handling of FPU insns faulting on memory write

When an FPU instruction with a memory destination fails during the memory write, it should not affect FPU register state. Due to the way we emulate FPU (and SIMD) instructions, we can only guarantee this by
- backing out changes to the FPU register state in such a case or
- doing a descriptor read and/or page walk up front, perhaps with the stubs accessing the actual memory location then.
The latter would require a significant change in how the emulator does its guest memory accessing, so for now the former variant is being chosen.

---
Note that the state save overhead (unless state hadn't been loaded at all before, which should only be possible if a guest is fiddling with the instruction stream under emulation) is taken for every FPU insn hitting the emulator. We could reduce this to just the ones writing to memory, but that would involve quite a few further changes and resulting code where even more code paths need to match up with one another.

tools/fuzz/x86_instruction_emulator/x86-insn-emulator-fuzzer.c +1 -0
@@ -433,6 +433,7 @@ static struct x86_emulate_ops fuzz_emulo
     SET(wbinvd),
     SET(invlpg),
     .get_fpu    = emul_test_get_fpu,
+    .put_fpu    = emul_test_put_fpu,
     .cpuid      = emul_test_cpuid,
 };
 #undef SET
tools/tests/x86_emulator/test_x86_emulator.c +1 -0
@@ -293,6 +293,7 @@ static struct x86_emulate_ops emulops =
     .read_cr    = emul_test_read_cr,
     .read_msr   = read_msr,
     .get_fpu    = emul_test_get_fpu,
+    .put_fpu    = emul_test_put_fpu,
 };
 
 int main(int argc, char **argv)
tools/tests/x86_emulator/x86_emulate.c +7 -0
@@ -138,4 +138,11 @@ int emul_test_get_fpu(
     return X86EMUL_OKAY;
 }
 
+void emul_test_put_fpu(
+    struct x86_emulate_ctxt *ctxt,
+    enum x86_emulate_fpu_type backout)
+{
+    /* TBD */
+}
+
 #include "x86_emulate/x86_emulate.c"
tools/tests/x86_emulator/x86_emulate.h +4 -0
@@ -178,3 +178,7 @@ int emul_test_get_fpu(
     void *exception_callback_arg,
     enum x86_emulate_fpu_type type,
     struct x86_emulate_ctxt *ctxt);
+
+void emul_test_put_fpu(
+    struct x86_emulate_ctxt *ctxt,
+    enum x86_emulate_fpu_type backout);
xen/arch/x86/hvm/emulate.c +45 -1

67 lines not on this page · raw message

xen/arch/x86/hvm/svm/svm.c +1 -0
@@ -2268,6 +2268,7 @@ static struct hvm_function_table __initd
     .update_guest_cr      = svm_update_guest_cr,
     .update_guest_efer    = svm_update_guest_efer,
     .update_guest_vendor  = svm_update_guest_vendor,
+    .fpu_leave            = svm_fpu_leave,
     .set_guest_pat        = svm_set_guest_pat,
     .get_guest_pat        = svm_get_guest_pat,
     .set_tsc_offset       = svm_set_tsc_offset,
xen/arch/x86/hvm/vmx/vmx.c +1 -0
@@ -2128,6 +2128,7 @@ static struct hvm_function_table __initd
     .update_guest_cr      = vmx_update_guest_cr,
     .update_guest_efer    = vmx_update_guest_efer,
     .update_guest_vendor  = vmx_update_guest_vendor,
+    .fpu_leave            = vmx_fpu_leave,
     .set_guest_pat        = vmx_set_guest_pat,
     .get_guest_pat        = vmx_get_guest_pat,
     .set_tsc_offset       = vmx_set_tsc_offset,
xen/arch/x86/x86_emulate/x86_emulate.c +9 -5
@@ -964,6 +964,7 @@ static int _get_fpu(
     {
         unsigned long cr0;
 
+        fail_if(type == X86EMUL_FPU_fpu && !ops->put_fpu);
         fic->type = type;
 
         fail_if(!ops->read_cr);
@@ -1025,11 +1026,14 @@ do {
 
 static void put_fpu(
     struct fpu_insn_ctxt *fic,
+    bool failed_late,
     struct x86_emulate_ctxt *ctxt,
     const struct x86_emulate_ops *ops)
 {
-    if ( fic->type != X86EMUL_FPU_none && ops->put_fpu )
-        ops->put_fpu(ctxt);
+    if ( unlikely(failed_late) && fic->type == X86EMUL_FPU_fpu )
+        ops->put_fpu(ctxt, X86EMUL_FPU_fpu);
+    else if ( fic->type != X86EMUL_FPU_none && ops->put_fpu )
+        ops->put_fpu(ctxt, X86EMUL_FPU_none);
 }
 
 static inline bool fpu_check_write(void)
@@ -3714,9 +3718,9 @@ x86_emulate(
         break;
 
     case 0x9b:  /* wait/fwait */
-        fic.insn_bytes = 1;
         host_and_vcpu_must_have(fpu);
         get_fpu(X86EMUL_FPU_wait, &fic);
+        fic.insn_bytes = 1;
         asm volatile ( "fwait" ::: "memory" );
         check_fpu(&fic);
         break;
@@ -7888,7 +7892,7 @@ x86_emulate(
     }
 
  complete_insn: /* Commit shadow register state. */
-    put_fpu(&fic, ctxt, ops);
+    put_fpu(&fic, false, ctxt, ops);
 
     /* Zero the upper 32 bits of %rip if not in 64-bit mode. */
     if ( !mode_64bit() )
@@ -7913,7 +7917,7 @@ x86_emulate(
 
  done:
     if ( rc != X86EMUL_OKAY )
-        put_fpu(&fic, ctxt, ops);
+        put_fpu(&fic, fic.insn_bytes > 0 && dst.type == OP_MEM, ctxt, ops);
     put_stub(stub);
     return rc;
 #undef state
xen/arch/x86/x86_emulate/x86_emulate.h +4 -1
@@ -437,9 +437,12 @@ struct x86_emulate_ops
      * put_fpu: Relinquish the FPU. Unhook from FPU/SIMD exception handlers.
      *  The handler, if installed, must be prepared to get called without
      *  the get_fpu one having got called before!
+     * @backout: Undo updates to the specified register file (can, besides
+     *           X86EMUL_FPU_none, only be X86EMUL_FPU_fpu at present);
      */
     void (*put_fpu)(
-        struct x86_emulate_ctxt *ctxt);
+        struct x86_emulate_ctxt *ctxt,
+        enum x86_emulate_fpu_type backout);
 
     /* invlpg: Invalidate paging structures which map addressed byte. */
     int (*invlpg)(
xen/include/asm-x86/hvm/hvm.h +2 -0
@@ -140,6 +140,8 @@ struct hvm_function_table {
 
     void (*update_guest_vendor)(struct vcpu *v);
 
+    void (*fpu_leave)(struct vcpu *v);
+
     int  (*get_guest_pat)(struct vcpu *v, u64 *);
     int  (*set_guest_pat)(struct vcpu *v, u64);
 

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 3/4
#7 lore raw

Re: [PATCH 3/4] x86emul: correct handling of FPU insns faulting on memory write

Quoted 11 lines · Jan Beulich

When an FPU instruction with a memory destination fails during the memory write, it should not affect FPU register state. Due to the way we emulate FPU (and SIMD) instructions, we can only guarantee this by
- backing out changes to the FPU register state in such a case or
- doing a descriptor read and/or page walk up front, perhaps with the stubs accessing the actual memory location then.
The latter would require a significant change in how the emulator does its guest memory accessing, so for now the former variant is being chosen.

Quoted 8 lines

---
Note that the state save overhead (unless state hadn't been loaded at all before, which should only be possible if a guest is fiddling with the instruction stream under emulation) is taken for every FPU insn hitting the emulator. We could reduce this to just the ones writing to memory, but that would involve quite a few further changes and resulting code where even more code paths need to match up with one another.

An FPU save isn't the slow part of hitting emulation. I don't expect this will be a meaningful overhead.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] x86emul: correct handling of FPU insns faulting on memory write

In reply to Jan Beulich
Quoted 12 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Monday, March 13, 2017 7:05 PM

When an FPU instruction with a memory destination fails during the memory write, it should not affect FPU register state. Due to the way we emulate FPU (and SIMD) instructions, we can only guarantee this by
- backing out changes to the FPU register state in such a case or
- doing a descriptor read and/or page walk up front, perhaps with the stubs accessing the actual memory location then.
The latter would require a significant change in how the emulator does its guest memory accessing, so for now the former variant is being chosen.

Quoted 1 line

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#9 lore raw

PATCH 4/4 x86emul: correct FPU code/data pointers and opcode handling

In reply to Jan Beulich

Prevent leaking the hypervisor ones (stored by hardware during stub execution), at once making sure the guest sees correct values there. This piggybacks on the backout logic used to deal with write faults of FPU insns.

Deliberately ignore the NO_FPU_SEL feature here: Honoring it would merely mean extra code with no benefit (once we XRSTOR state, the selector values will simply be lost anyway).

tools/tests/x86_emulator/x86_emulate.c +2 -1
@@ -140,7 +140,8 @@ int emul_test_get_fpu(
 
 void emul_test_put_fpu(
     struct x86_emulate_ctxt *ctxt,
-    enum x86_emulate_fpu_type backout)
+    enum x86_emulate_fpu_type backout,
+    const struct x86_emul_fpu_aux *aux)
 {
     /* TBD */
 }
tools/tests/x86_emulator/x86_emulate.h +2 -1
@@ -181,4 +181,5 @@ int emul_test_get_fpu(
 
 void emul_test_put_fpu(
     struct x86_emulate_ctxt *ctxt,
-    enum x86_emulate_fpu_type backout);
+    enum x86_emulate_fpu_type backout,
+    const struct x86_emul_fpu_aux *aux);
xen/arch/x86/hvm/emulate.c +62 -1

74 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.c +97 -51

219 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.h +10 -1
@@ -135,6 +135,13 @@ struct __attribute__((__packed__)) segme
     uint64_t   base;
 };
 
+struct x86_emul_fpu_aux {
+    unsigned long ip, dp;
+    uint16_t cs, ds;
+    unsigned int op:11;
+    unsigned int dval:1;
+};
+
 /*
  * Return codes from state-accessor functions and from x86_emulate().
  */
@@ -439,10 +446,12 @@ struct x86_emulate_ops
      *  the get_fpu one having got called before!
      * @backout: Undo updates to the specified register file (can, besides
      *           X86EMUL_FPU_none, only be X86EMUL_FPU_fpu at present);
+     * @aux: Packaged up FIP/FDP/FOP values to load into FPU.
      */
     void (*put_fpu)(
         struct x86_emulate_ctxt *ctxt,
-        enum x86_emulate_fpu_type backout);
+        enum x86_emulate_fpu_type backout,
+        const struct x86_emul_fpu_aux *aux);
 
     /* invlpg: Invalidate paging structures which map addressed byte. */
     int (*invlpg)(

x86emul: correct FPU code/data pointers and opcode handling

Prevent leaking the hypervisor ones (stored by hardware during stub execution), at once making sure the guest sees correct values there. This piggybacks on the backout logic used to deal with write faults of FPU insns.

Deliberately ignore the NO_FPU_SEL feature here: Honoring it would merely mean extra code with no benefit (once we XRSTOR state, the selector values will simply be lost anyway).

tools/tests/x86_emulator/x86_emulate.c +2 -1
@@ -140,7 +140,8 @@ int emul_test_get_fpu(
 
 void emul_test_put_fpu(
     struct x86_emulate_ctxt *ctxt,
-    enum x86_emulate_fpu_type backout)
+    enum x86_emulate_fpu_type backout,
+    const struct x86_emul_fpu_aux *aux)
 {
     /* TBD */
 }
tools/tests/x86_emulator/x86_emulate.h +2 -1
@@ -181,4 +181,5 @@ int emul_test_get_fpu(
 
 void emul_test_put_fpu(
     struct x86_emulate_ctxt *ctxt,
-    enum x86_emulate_fpu_type backout);
+    enum x86_emulate_fpu_type backout,
+    const struct x86_emul_fpu_aux *aux);
xen/arch/x86/hvm/emulate.c +62 -1

74 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.c +97 -51

219 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.h +10 -1
@@ -135,6 +135,13 @@ struct __attribute__((__packed__)) segme
     uint64_t   base;
 };
 
+struct x86_emul_fpu_aux {
+    unsigned long ip, dp;
+    uint16_t cs, ds;
+    unsigned int op:11;
+    unsigned int dval:1;
+};
+
 /*
  * Return codes from state-accessor functions and from x86_emulate().
  */
@@ -439,10 +446,12 @@ struct x86_emulate_ops
      *  the get_fpu one having got called before!
      * @backout: Undo updates to the specified register file (can, besides
      *           X86EMUL_FPU_none, only be X86EMUL_FPU_fpu at present);
+     * @aux: Packaged up FIP/FDP/FOP values to load into FPU.
      */
     void (*put_fpu)(
         struct x86_emulate_ctxt *ctxt,
-        enum x86_emulate_fpu_type backout);
+        enum x86_emulate_fpu_type backout,
+        const struct x86_emul_fpu_aux *aux);
 
     /* invlpg: Invalidate paging structures which map addressed byte. */
     int (*invlpg)(

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 4/4
#10 lore raw

Re: [PATCH 4/4] x86emul: correct FPU code/data pointers and opcode handling

Quoted 14 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 13 March 2017 11:06
To: xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 4/4] x86emul: correct FPU code/data pointers and opcode handling

Prevent leaking the hypervisor ones (stored by hardware during stub execution), at once making sure the guest sees correct values there. This piggybacks on the backout logic used to deal with write faults of FPU insns.

Quoted 6 lines

Deliberately ignore the NO_FPU_SEL feature here: Honoring it would merely mean extra code with no benefit (once we XRSTOR state, the selector values will simply be lost anyway).

emulate.c parts...

Quoted 351 lines
tools/tests/x86_emulator/x86_emulate.c +2 -1
@@ -140,7 +140,8 @@ int emul_test_get_fpu(
 
 void emul_test_put_fpu(
     struct x86_emulate_ctxt *ctxt,
-    enum x86_emulate_fpu_type backout)
+    enum x86_emulate_fpu_type backout,
+    const struct x86_emul_fpu_aux *aux)
 {
     /* TBD */
 }
tools/tests/x86_emulator/x86_emulate.h +2 -1
@@ -181,4 +181,5 @@ int emul_test_get_fpu(
 
 void emul_test_put_fpu(
     struct x86_emulate_ctxt *ctxt,
-    enum x86_emulate_fpu_type backout);
+    enum x86_emulate_fpu_type backout,
+    const struct x86_emul_fpu_aux *aux);
xen/arch/x86/hvm/emulate.c +62 -1
@@ -1658,12 +1658,73 @@ static int hvmemul_get_fpu(
 
 static void hvmemul_put_fpu(
     struct x86_emulate_ctxt *ctxt,
-    enum x86_emulate_fpu_type backout)
+    enum x86_emulate_fpu_type backout,
+    const struct x86_emul_fpu_aux *aux)
 {
     struct vcpu *curr = current;
 
     curr->arch.hvm_vcpu.fpu_exception_callback = NULL;
 
+    if ( aux )
+    {
+        typeof(curr->arch.xsave_area->fpu_sse) *fpu_ctxt = curr-
 >arch.fpu_ctxt;
+        bool dval = aux->dval;
+        int mode = hvm_guest_x86_mode(curr);
+
+        ASSERT(backout == X86EMUL_FPU_none);
+        /*
+         * Latch current register state so that we can replace FIP/FDP/FOP
+         * (which have values resulting from our own invocation of the FPU
+         * instruction during emulation).
+         * NB: See also the comment in hvmemul_get_fpu(); we don't need to
+         * set ->fpu_dirtied here as it is going to be cleared below, and
+         * we also don't need to reload FCW as we're forcing full state to
+         * be reloaded anyway.
+         */
+        save_fpu_enable();
+
+        if ( boot_cpu_has(X86_FEATURE_FDP_EXCP_ONLY) &&
+             !(fpu_ctxt->fsw & ~fpu_ctxt->fcw & 0x003f) )
+            dval = false;
+
+        switch ( mode )
+        {
+        case 8:
+            fpu_ctxt->fip.addr = aux->ip;
+            if ( dval )
+                fpu_ctxt->fdp.addr = aux->dp;
+            fpu_ctxt->x[FPU_WORD_SIZE_OFFSET] = 8;
+            break;
+
+        case 4: case 2:
+            fpu_ctxt->fip.offs = aux->ip;
+            fpu_ctxt->fip.sel  = aux->cs;
+            if ( dval )
+            {
+                fpu_ctxt->fdp.offs = aux->dp;
+                fpu_ctxt->fdp.sel  = aux->ds;
+            }
+            fpu_ctxt->x[FPU_WORD_SIZE_OFFSET] = mode;
+            break;
+
+        case 0: case 1:
+            fpu_ctxt->fip.addr = aux->ip | (aux->cs << 4);
+            if ( dval )
+                fpu_ctxt->fdp.addr = aux->dp | (aux->ds << 4);
+            fpu_ctxt->x[FPU_WORD_SIZE_OFFSET] = 2;
+            break;
+
+        default:
+            ASSERT_UNREACHABLE();
+            return;
+        }
+
+        fpu_ctxt->fop = aux->op;
+
+        /* Re-use backout code below. */
+        backout = X86EMUL_FPU_fpu;
+    }
+
     if ( backout == X86EMUL_FPU_fpu )
     {
         /*
xen/arch/x86/x86_emulate/x86_emulate.c +97 -51
@@ -536,6 +536,55 @@ struct operand {
         unsigned long    off;
     } mem;
 };
+
+struct x86_emulate_state {
+    unsigned int op_bytes, ad_bytes;
+
+    enum {
+        ext_none = vex_none,
+        ext_0f   = vex_0f,
+        ext_0f38 = vex_0f38,
+        ext_0f3a = vex_0f3a,
+        /*
+         * For XOP use values such that the respective instruction field
+         * can be used without adjustment.
+         */
+        ext_8f08 = 8,
+        ext_8f09,
+        ext_8f0a,
+    } ext;
+    uint8_t modrm, modrm_mod, modrm_reg, modrm_rm;
+    uint8_t rex_prefix;
+    bool lock_prefix;
+    bool not_64bit; /* Instruction not available in 64bit. */
+    bool fpu_ctrl;  /* Instruction is an FPU control one. */
+    opcode_desc_t desc;
+    union vex vex;
+    union evex evex;
+    enum simd_opsize simd_size;
+
+    /*
+     * Data operand effective address (usually computed from ModRM).
+     * Default is a memory operand relative to segment DS.
+     */
+    struct operand ea;
+
+    /* Immediate operand values, if any. Use otherwise unused fields. */
+#define imm1 ea.val
+#define imm2 ea.orig_val
+
+    unsigned long ip;
+    struct cpu_user_regs *regs;
+
+#ifndef NDEBUG
+    /*
+     * Track caller of x86_decode_insn() to spot missing as well as
+     * premature calls to x86_emulate_free_state().
+     */
+    void *caller;
+#endif
+};
+
 #ifdef __x86_64__
 #define PTR_POISON ((void *)0x8086000000008086UL) /* non-canonical */
 #else
@@ -1027,13 +1076,48 @@ do {
 static void put_fpu(
     struct fpu_insn_ctxt *fic,
     bool failed_late,
+    const struct x86_emulate_state *state,
     struct x86_emulate_ctxt *ctxt,
     const struct x86_emulate_ops *ops)
 {
     if ( unlikely(failed_late) && fic->type == X86EMUL_FPU_fpu )
-        ops->put_fpu(ctxt, X86EMUL_FPU_fpu);
+        ops->put_fpu(ctxt, X86EMUL_FPU_fpu, NULL);
+    else if ( unlikely(fic->type == X86EMUL_FPU_fpu) && !state->fpu_ctrl )
+    {
+        struct x86_emul_fpu_aux aux = {
+            .ip = ctxt->regs->r(ip),
+            .cs = ctxt->regs->cs,
+            .op = ((ctxt->opcode & 7) << 8) | state->modrm,
+        };
+        struct segment_register sreg;
+
+        if ( ops->read_segment &&
+             ops->read_segment(x86_seg_cs, &sreg, ctxt) == X86EMUL_OKAY )
+            aux.cs = sreg.sel;
+        if ( state->ea.type == OP_MEM )
+        {
+            aux.dp = state->ea.mem.off;
+            if ( ops->read_segment &&
+                 ops->read_segment(state->ea.mem.seg, &sreg,
+                                   ctxt) == X86EMUL_OKAY )
+                aux.ds = sreg.sel;
+            else
+                switch ( state->ea.mem.seg )
+                {
+                case x86_seg_cs: aux.ds = ctxt->regs->cs; break;
+                case x86_seg_ds: aux.ds = ctxt->regs->ds; break;
+                case x86_seg_es: aux.ds = ctxt->regs->es; break;
+                case x86_seg_fs: aux.ds = ctxt->regs->fs; break;
+                case x86_seg_gs: aux.ds = ctxt->regs->gs; break;
+                case x86_seg_ss: aux.ds = ctxt->regs->ss; break;
+                default:         ASSERT_UNREACHABLE();    break;
+                }
+            aux.dval = true;
+        }
+        ops->put_fpu(ctxt, X86EMUL_FPU_none, &aux);
+    }
     else if ( fic->type != X86EMUL_FPU_none && ops->put_fpu )
-        ops->put_fpu(ctxt, X86EMUL_FPU_none);
+        ops->put_fpu(ctxt, X86EMUL_FPU_none, NULL);
 }
 
 static inline bool fpu_check_write(void)
@@ -2086,53 +2170,6 @@ int x86emul_unhandleable_rw(
     return X86EMUL_UNHANDLEABLE;
 }
 
-struct x86_emulate_state {
-    unsigned int op_bytes, ad_bytes;
-
-    enum {
-        ext_none = vex_none,
-        ext_0f   = vex_0f,
-        ext_0f38 = vex_0f38,
-        ext_0f3a = vex_0f3a,
-        /*
-         * For XOP use values such that the respective instruction field
-         * can be used without adjustment.
-         */
-        ext_8f08 = 8,
-        ext_8f09,
-        ext_8f0a,
-    } ext;
-    uint8_t modrm, modrm_mod, modrm_reg, modrm_rm;
-    uint8_t rex_prefix;
-    bool lock_prefix;
-    bool not_64bit; /* Instruction not available in 64bit. */
-    opcode_desc_t desc;
-    union vex vex;
-    union evex evex;
-    enum simd_opsize simd_size;
-
-    /*
-     * Data operand effective address (usually computed from ModRM).
-     * Default is a memory operand relative to segment DS.
-     */
-    struct operand ea;
-
-    /* Immediate operand values, if any. Use otherwise unused fields. */
-#define imm1 ea.val
-#define imm2 ea.orig_val
-
-    unsigned long ip;
-    struct cpu_user_regs *regs;
-
-#ifndef NDEBUG
-    /*
-     * Track caller of x86_decode_insn() to spot missing as well as
-     * premature calls to x86_emulate_free_state().
-     */
-    void *caller;
-#endif
-};
-
 /* Helper definitions. */
 #define op_bytes (state->op_bytes)
 #define ad_bytes (state->ad_bytes)
@@ -4231,8 +4268,10 @@ x86_emulate(
                 dst.bytes = 4;
                 break;
             case 4: /* fldenv - TODO */
+                state->fpu_ctrl = true;
                 goto cannot_emulate;
             case 5: /* fldcw m2byte */
+                state->fpu_ctrl = true;
                 if ( (rc = ops->read(ea.mem.seg, ea.mem.off, &src.val,
                                      2, ctxt)) != X86EMUL_OKAY )
                     goto done;
@@ -4240,8 +4279,10 @@ x86_emulate(
                 dst.type = OP_NONE;
                 break;
             case 6: /* fnstenv - TODO */
+                state->fpu_ctrl = true;
                 goto cannot_emulate;
             case 7: /* fnstcw m2byte */
+                state->fpu_ctrl = true;
                 emulate_fpu_insn_memdst("fnstcw", dst.val);
                 dst.bytes = 2;
                 break;
@@ -4330,6 +4371,7 @@ x86_emulate(
         case 0xe3: /* fninit */
         case 0xe4: /* fnsetpm - 287 only, ignored by 387 */
         /* case 0xe5: frstpm - 287 only, #UD on 387 */
+            state->fpu_ctrl = true;
             emulate_fpu_insn_stub(0xdb, modrm);
             break;
         default:
@@ -4473,8 +4515,10 @@ x86_emulate(
                 break;
             case 4: /* frstor - TODO */
             case 6: /* fnsave - TODO */
+                state->fpu_ctrl = true;
                 goto cannot_emulate;
             case 7: /* fnstsw m2byte */
+                state->fpu_ctrl = true;
                 emulate_fpu_insn_memdst("fnstsw", dst.val);
                 dst.bytes = 2;
                 break;
@@ -4547,6 +4591,7 @@ x86_emulate(
         {
         case 0xe0:
             /* fnstsw %ax */
+            state->fpu_ctrl = true;
             dst.bytes = 2;
             dst.type = OP_REG;
             dst.reg = (void *)&_regs.ax;
@@ -7892,7 +7937,7 @@ x86_emulate(
     }
 
  complete_insn: /* Commit shadow register state. */
-    put_fpu(&fic, false, ctxt, ops);
+    put_fpu(&fic, false, state, ctxt, ops);
 
     /* Zero the upper 32 bits of %rip if not in 64-bit mode. */
     if ( !mode_64bit() )
@@ -7917,7 +7962,8 @@ x86_emulate(
 
  done:
     if ( rc != X86EMUL_OKAY )
-        put_fpu(&fic, fic.insn_bytes > 0 && dst.type == OP_MEM, ctxt, ops);
+        put_fpu(&fic, fic.insn_bytes > 0 && dst.type == OP_MEM,
+                state, ctxt, ops);
     put_stub(stub);
     return rc;
 #undef state
xen/arch/x86/x86_emulate/x86_emulate.h +7 -0
@@ -135,6 +135,13 @@ struct __attribute__((__packed__)) segme
     uint64_t   base;
 };
 
+struct x86_emul_fpu_aux {
+    unsigned long ip, dp;
+    uint16_t cs, ds;
+    unsigned int op:11;
+    unsigned int dval:1;
+};
+
 /*
  * Return codes from state-accessor functions and from x86_emulate().
  */
@@ -439,10 +446,12 @@ struct x86_emulate_ops
      *  the get_fpu one having got called before!
      * @backout: Undo updates to the specified register file (can, besides
      *           X86EMUL_FPU_none, only be X86EMUL_FPU_fpu at present);

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_emulate_ops · line 457

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +3 -1
+     * @aux: Packaged up FIP/FDP/FOP values to load into FPU.
      */
     void (*put_fpu)(
         struct x86_emulate_ctxt *ctxt,
-        enum x86_emulate_fpu_type backout);
+        enum x86_emulate_fpu_type backout,
+        const struct x86_emul_fpu_aux *aux);
 
     /* invlpg: Invalidate paging structures which map addressed byte. */
     int (*invlpg)(

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#11 lore raw

Re: [PATCH 4/4] x86emul: correct FPU code/data pointers and opcode handling

In reply to Jan Beulich
Quoted 12 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +3 -1
@@ -1027,13 +1076,48 @@ do {
 static void put_fpu(
     struct fpu_insn_ctxt *fic,
     bool failed_late,
+    const struct x86_emulate_state *state,
     struct x86_emulate_ctxt *ctxt,
     const struct x86_emulate_ops *ops)
 {
     if ( unlikely(failed_late) && fic->type == X86EMUL_FPU_fpu )
-        ops->put_fpu(ctxt, X86EMUL_FPU_fpu);
+        ops->put_fpu(ctxt, X86EMUL_FPU_fpu, NULL);
+    else if ( unlikely(fic->type == X86EMUL_FPU_fpu) && !state->fpu_ctrl )

xen/arch/x86/x86_emulate/x86_emulate.c do { · line 1095

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +10 -0
+    {
+        struct x86_emul_fpu_aux aux = {
+            .ip = ctxt->regs->r(ip),
+            .cs = ctxt->regs->cs,
+            .op = ((ctxt->opcode & 7) << 8) | state->modrm,
+        };
+        struct segment_register sreg;
+
+        if ( ops->read_segment &&
+             ops->read_segment(x86_seg_cs, &sreg, ctxt) == X86EMUL_OKAY )

Why are the read_segment hooks optional here?

In the case that we are hitting FPU instructions for emulation, we can reasonably require read/write_segment hooks.

In particular, regs->%sreg are only valid for PV guests.

Quoted 30 lines
xen/arch/x86/x86_emulate/x86_emulate.c +24 -1
+            aux.cs = sreg.sel;
+        if ( state->ea.type == OP_MEM )
+        {
+            aux.dp = state->ea.mem.off;
+            if ( ops->read_segment &&
+                 ops->read_segment(state->ea.mem.seg, &sreg,
+                                   ctxt) == X86EMUL_OKAY )
+                aux.ds = sreg.sel;
+            else
+                switch ( state->ea.mem.seg )
+                {
+                case x86_seg_cs: aux.ds = ctxt->regs->cs; break;
+                case x86_seg_ds: aux.ds = ctxt->regs->ds; break;
+                case x86_seg_es: aux.ds = ctxt->regs->es; break;
+                case x86_seg_fs: aux.ds = ctxt->regs->fs; break;
+                case x86_seg_gs: aux.ds = ctxt->regs->gs; break;
+                case x86_seg_ss: aux.ds = ctxt->regs->ss; break;
+                default:         ASSERT_UNREACHABLE();    break;
+                }
+            aux.dval = true;
+        }
+        ops->put_fpu(ctxt, X86EMUL_FPU_none, &aux);
+    }
     else if ( fic->type != X86EMUL_FPU_none && ops->put_fpu )
-        ops->put_fpu(ctxt, X86EMUL_FPU_none);
+        ops->put_fpu(ctxt, X86EMUL_FPU_none, NULL);
 }
 
 static inline bool fpu_check_write(void)

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 4271

Quoted 4 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -0
@@ -4231,8 +4268,10 @@ x86_emulate(
                 dst.bytes = 4;
                 break;
             case 4: /* fldenv - TODO */
+                state->fpu_ctrl = true;

Arguably, state->fpu_ctrl is a decode property rather than an emulation property. It is the kind of information which we might plausibly want an x86_insn_*() accessor for.

~Andrew

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 4277

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -0
                 goto cannot_emulate;
             case 5: /* fldcw m2byte */
+                state->fpu_ctrl = true;
                 if ( (rc = ops->read(ea.mem.seg, ea.mem.off, &src.val,
                                      2, ctxt)) != X86EMUL_OKAY )
                     goto done;

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#12 lore raw

Re: [PATCH 4/4] x86emul: correct FPU code/data pointers and opcode handling

Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 14.03.17 at 11:56, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +3 -1
@@ -1027,13 +1076,48 @@ do {
 static void put_fpu(
     struct fpu_insn_ctxt *fic,
     bool failed_late,
+    const struct x86_emulate_state *state,
     struct x86_emulate_ctxt *ctxt,
     const struct x86_emulate_ops *ops)
 {
     if ( unlikely(failed_late) && fic->type == X86EMUL_FPU_fpu )
-        ops->put_fpu(ctxt, X86EMUL_FPU_fpu);
+        ops->put_fpu(ctxt, X86EMUL_FPU_fpu, NULL);
+    else if ( unlikely(fic->type == X86EMUL_FPU_fpu) && !state->fpu_ctrl )

xen/arch/x86/x86_emulate/x86_emulate.c do { · line 1095

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +10 -0
+    {
+        struct x86_emul_fpu_aux aux = {
+            .ip = ctxt->regs->r(ip),
+            .cs = ctxt->regs->cs,
+            .op = ((ctxt->opcode & 7) << 8) | state->modrm,
+        };
+        struct segment_register sreg;
+
+        if ( ops->read_segment &&
+             ops->read_segment(x86_seg_cs, &sreg, ctxt) == X86EMUL_OKAY )

Why are the read_segment hooks optional here?

Quoted 4 lines

In the case that we are hitting FPU instructions for emulation, we can reasonably require read/write_segment hooks.

In particular, regs->%sreg are only valid for PV guests.

That's the precise reason I made their availability optional here:
A caller caring only about PV guests has no need to set these, as long as for what might be emulated only selector values are of interest. And it is clear that we can't distinguish guest types here.

Quoted 9 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 4271

Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -0
@@ -4231,8 +4268,10 @@ x86_emulate(
                 dst.bytes = 4;
                 break;
             case 4: /* fldenv - TODO */
+                state->fpu_ctrl = true;

Arguably, state->fpu_ctrl is a decode property rather than an emulation property. It is the kind of information which we might plausibly want an x86_insn_*() accessor for.

If we had a consumer for this, I'd agree, but adding an accessor without user and moving the changes here to the decode phase (increasing code churn) seems counterproductive to me.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#13 lore raw

PATCH 1/1 add FPU/SIMD register state test

In reply to Jan Beulich

Add tests to verify that
- FPU insns leave correct (guest) values in FIP/FDP/FOP/FCS/FDS,
- FPU insns writing memory don't update FPU register state when the write faults (at the example of FISTP),
- VCVTPS2PH (once implemented in the emulator) doesn't update MXCSR if its write faults (VCVTPS2PH currently is the only SIMD insn writing to memory and updating register state).

Note that the AMD-specific code in fpu_fxrstor() and xrstor() causes the FISTP part of this test to always fail. I don't really have any idea what to do about this (other than perhaps skipping the test on AMD altogether).

Note further that the FCS part of 64-bit variant of the FSTP test also always fails on AMD, since, other than on Intel, {F,}XRSTOR with REX.W set do not appear to clear FCS/FDS (I can't find any statement either way in the PM).

---
TBD: It is not clear to me how to deal with the native execution
failure of VCVTPS2PH on at least some Intel models.

include/arch/x86/cpuid.h +1 -0
@@ -77,6 +77,7 @@ static inline bool cpu_has(unsigned int
 #define cpu_has_pcid            cpu_has(X86_FEATURE_PCID)
 #define cpu_has_xsave           cpu_has(X86_FEATURE_XSAVE)
 #define cpu_has_avx             cpu_has(X86_FEATURE_AVX)
+#define cpu_has_f16c            cpu_has(X86_FEATURE_F16C)
 
 #define cpu_has_syscall         cpu_has(X86_FEATURE_SYSCALL)
 #define cpu_has_nx              cpu_has(X86_FEATURE_NX)
tests/fpu-state/Makefile new file +9 -0
@@ -0,0 +1,9 @@
+include $(ROOT)/build/common.mk
+
+NAME      := fpu-state
+CATEGORY  := functional
+TEST-ENVS := hvm64 hvm32pse
+
+obj-perenv += main.o
+
+include $(ROOT)/build/gen.mk
tests/fpu-state/main.c new file +184 -0

184 lines not on this page · raw message

add FPU/SIMD register state test

Add tests to verify that
- FPU insns leave correct (guest) values in FIP/FDP/FOP/FCS/FDS,
- FPU insns writing memory don't update FPU register state when the write faults (at the example of FISTP),
- VCVTPS2PH (once implemented in the emulator) doesn't update MXCSR if its write faults (VCVTPS2PH currently is the only SIMD insn writing to memory and updating register state).

Note that the AMD-specific code in fpu_fxrstor() and xrstor() causes the FISTP part of this test to always fail. I don't really have any idea what to do about this (other than perhaps skipping the test on AMD altogether).

Note further that the FCS part of 64-bit variant of the FSTP test also always fails on AMD, since, other than on Intel, {F,}XRSTOR with REX.W set do not appear to clear FCS/FDS (I can't find any statement either way in the PM).

---
TBD: It is not clear to me how to deal with the native execution
failure of VCVTPS2PH on at least some Intel models.

include/arch/x86/cpuid.h +1 -0
@@ -77,6 +77,7 @@ static inline bool cpu_has(unsigned int
 #define cpu_has_pcid            cpu_has(X86_FEATURE_PCID)
 #define cpu_has_xsave           cpu_has(X86_FEATURE_XSAVE)
 #define cpu_has_avx             cpu_has(X86_FEATURE_AVX)
+#define cpu_has_f16c            cpu_has(X86_FEATURE_F16C)
 
 #define cpu_has_syscall         cpu_has(X86_FEATURE_SYSCALL)
 #define cpu_has_nx              cpu_has(X86_FEATURE_NX)
tests/fpu-state/Makefile new file +9 -0
@@ -0,0 +1,9 @@
+include $(ROOT)/build/common.mk
+
+NAME      := fpu-state
+CATEGORY  := functional
+TEST-ENVS := hvm64 hvm32pse
+
+obj-perenv += main.o
+
+include $(ROOT)/build/gen.mk
tests/fpu-state/main.c new file +184 -0

184 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#14 lore raw

Re: [PATCH][XTF] add FPU/SIMD register state test

Quoted 18 lines · Jan Beulich

Add tests to verify that
- FPU insns leave correct (guest) values in FIP/FDP/FOP/FCS/FDS,
- FPU insns writing memory don't update FPU register state when the write faults (at the example of FISTP),
- VCVTPS2PH (once implemented in the emulator) doesn't update MXCSR if its write faults (VCVTPS2PH currently is the only SIMD insn writing to memory and updating register state).

Note that the AMD-specific code in fpu_fxrstor() and xrstor() causes the FISTP part of this test to always fail. I don't really have any idea what to do about this (other than perhaps skipping the test on AMD altogether).

Note further that the FCS part of 64-bit variant of the FSTP test also always fails on AMD, since, other than on Intel, {F,}XRSTOR with REX.W set do not appear to clear FCS/FDS (I can't find any statement either way in the PM).

Quoted 4 lines

---
TBD: It is not clear to me how to deal with the native execution
failure of VCVTPS2PH on at least some Intel models.

As a general comment, tests dealing with quirks like this should tolerate real hardware behaviour, without causing the test to fail. There is nothing useful from having tests failing in cases we cant/wont do anything to fix.

As for the Intel behaviour, Is this documented, or does it have an erratum reference?

For the AMD behaviour, some more though is going to be required.

Quoted 37 lines
include/arch/x86/cpuid.h +1 -0
@@ -77,6 +77,7 @@ static inline bool cpu_has(unsigned int
 #define cpu_has_pcid            cpu_has(X86_FEATURE_PCID)
 #define cpu_has_xsave           cpu_has(X86_FEATURE_XSAVE)
 #define cpu_has_avx             cpu_has(X86_FEATURE_AVX)
+#define cpu_has_f16c            cpu_has(X86_FEATURE_F16C)
 
 #define cpu_has_syscall         cpu_has(X86_FEATURE_SYSCALL)
 #define cpu_has_nx              cpu_has(X86_FEATURE_NX)
tests/fpu-state/Makefile new file +9 -0
@@ -0,0 +1,9 @@
+include $(ROOT)/build/common.mk
+
+NAME      := fpu-state
+CATEGORY  := functional
+TEST-ENVS := hvm64 hvm32pse
+
+obj-perenv += main.o
+
+include $(ROOT)/build/gen.mk
tests/fpu-state/main.c new file +11 -0
@@ -0,0 +1,184 @@
+/**
+ * @file tests/fpu-state/main.c
+ * @ref test-fpu-state - Emulation of FPU state
+ *
+ * @page test-fpu-state FPU State Emulation
+ *
+ * FPU code/data pointers and opcode must not be the ones resulting
+ * from the stub execution in the hypervisor.
+ *
+ * FPU and SIMD instructions faulting during memory write must not
+ * update the respective register files.

tests/fpu-state/main.c line 21

Quoted 10 lines
tests/fpu-state/main.c new file +10 -0
+ *
+ * @see tests/fpu-state/main.c
+ */
+#include <xtf.h>
+
+#include <arch/x86/exinfo.h>
+
+const char test_title[] = "FPU State";
+
+struct x87_env {

Can this include 32bit in the name. The 16bit layout is different.

In fact, if you could put it in arch/x86/x87.h, that would be even more helpful.

Quoted 11 lines
tests/fpu-state/main.c new file +11 -0
+    uint16_t cw, :16;
+    uint16_t sw, :16;
+    uint16_t tw, :16;
+    uint32_t ip;
+    uint16_t cs;
+    uint16_t op:11, :5;
+    uint32_t dp;
+    uint16_t ds, :16;
+};
+
+void probe_fstp(bool force)

tests/fpu-state/main.c line 42

Quoted 10 lines
tests/fpu-state/main.c new file +10 -0
+{
+    const uint8_t *fstp_offs;
+    uint32_t flt;
+    struct x87_env fenv;
+
+    fenv.cw = 0x35f; /* unmask PE */
+    asm volatile ( "fninit;"
+                   "fldcw %[cw];"
+                   "fldpi;"
+                   "mov $1f, %[offs];"

You can have the compiler do all of this by using a named label, and using

extern char fstp_offs[] asm("$label");

~Andrew

Quoted 13 lines
tests/fpu-state/main.c new file +12 -0
+                   "test %[fep], %[fep];"
+                   "jz 1f;"
+                   _ASM_XEN_FEP
+                   "1: fstps %[data]; 2:"
+                   : [offs] "=&g" (fstp_offs), [data] "=m" (flt)
+                   : [cw] "m" (fenv.cw), [fep] "q" (force) );
+
+    asm ( "fnstenv %0" : "=m" (fenv) );
+    if ( fenv.ip != (unsigned long)fstp_offs )
+        xtf_failure("Fail: FIP wrong (%08x)\n", fenv.ip);
+    if ( fenv.cs && fenv.cs != __KERN_CS )
+        xtf_failure("Fail: FCS wrong (%04x)\n", fenv.cs);

tests/fpu-state/main.c line 64

Quoted 10 lines
tests/fpu-state/main.c new file +10 -0
+    if ( fenv.dp != (unsigned long)&flt )
+        xtf_failure("Fail: FDP wrong (%08x)\n", fenv.dp);
+    if ( fenv.ds && fenv.ds != __KERN_DS )
+        xtf_failure("Fail: FDS wrong (%04x)\n", fenv.ds);
+    /* Skip possible opcode prefixes before checking the opcode. */
+    while ( (fstp_offs[0] & ~7) != 0xd8 )
+        ++fstp_offs;
+    if ( fenv.op && fenv.op != (((fstp_offs[0] & 7) << 8) | fstp_offs[1]) )
+        xtf_failure("Fail: FOP wrong (%03x)\n", fenv.op);
+}

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#15 lore raw

Re: [PATCH][XTF] add FPU/SIMD register state test

Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 14.03.17 at 12:36, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich

Add tests to verify that
- FPU insns leave correct (guest) values in FIP/FDP/FOP/FCS/FDS,
- FPU insns writing memory don't update FPU register state when the write faults (at the example of FISTP),
- VCVTPS2PH (once implemented in the emulator) doesn't update MXCSR if its write faults (VCVTPS2PH currently is the only SIMD insn writing to memory and updating register state).

Note that the AMD-specific code in fpu_fxrstor() and xrstor() causes the FISTP part of this test to always fail. I don't really have any idea what to do about this (other than perhaps skipping the test on AMD altogether).

Note further that the FCS part of 64-bit variant of the FSTP test also always fails on AMD, since, other than on Intel, {F,}XRSTOR with REX.W set do not appear to clear FCS/FDS (I can't find any statement either way in the PM).

Quoted 4 lines

---
TBD: It is not clear to me how to deal with the native execution
failure of VCVTPS2PH on at least some Intel models.

Quoted 7 lines

As a general comment, tests dealing with quirks like this should tolerate real hardware behaviour, without causing the test to fail. There is nothing useful from having tests failing in cases we cant/wont do anything to fix.

As for the Intel behaviour, Is this documented, or does it have an erratum reference?

Neither - I am waiting for them to confirm whether this is an erratum, or whether (for some strange reason) this is intended behavior.

Quoted 1 line

For the AMD behaviour, some more though is going to be required.

One option I've been considering was to dynamically select between xtf_failure() and xtf_warning() for the problematic checks.

Quoted 14 lines

tests/fpu-state/main.c line 42

Quoted 11 lines
tests/fpu-state/main.c new file +11 -0
+void probe_fstp(bool force)
+{
+    const uint8_t *fstp_offs;
+    uint32_t flt;
+    struct x87_env fenv;
+
+    fenv.cw = 0x35f; /* unmask PE */
+    asm volatile ( "fninit;"
+                   "fldcw %[cw];"
+                   "fldpi;"
+                   "mov $1f, %[offs];"

You can have the compiler do all of this by using a named label, and using

Quoted 1 line

extern char fstp_offs[] asm("$label");

Is there any benefit to this? To me this looks uglier than what I have now.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel