xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xen.org>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>,
	Jan Beulich <JBeulich@suse.com>
Subject: [PATCH 2/3] tests/x86emul: Save and restore FPU state in the emulator callbacks
Date: Tue, 6 Mar 2018 20:24:51 +0000	[thread overview]
Message-ID: <1520367892-15055-3-git-send-email-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <1520367892-15055-1-git-send-email-andrew.cooper3@citrix.com>

Currently with then native toolchain on Debian Jessie ./test_x86_emulator
yeilds:

  Testing AVX2 256bit single native execution...okay
  Testing AVX2 256bit single 64-bit code sequence...[line 933] failed!

The bug is that libc's memcpy() in read() uses %xmm8 (specifically, in
__memcpy_sse2_unaligned()), which corrupts %ymm8 behind the back of the AVX2
test code.

Switch all hooks to use "goto out" style returns, and use
emul_{save,restore}_fpu_state().

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>
---
 tools/tests/x86_emulator/test_x86_emulator.c | 94 +++++++++++++++++++++++-----
 1 file changed, 79 insertions(+), 15 deletions(-)

diff --git a/tools/tests/x86_emulator/test_x86_emulator.c b/tools/tests/x86_emulator/test_x86_emulator.c
index 625cd2a..a764d99 100644
--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -226,6 +226,10 @@ static int read(
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
+    int rc = X86EMUL_OKAY;
+
+    emul_save_fpu_state();
+
     if ( verbose )
         printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
 
@@ -236,42 +240,61 @@ static int read(
     case x86_seg_gdtr:
         /* Fake system segment type matching table index. */
         if ( (offset & 7) || (bytes > 8) )
-            return X86EMUL_UNHANDLEABLE;
+        {
+            rc = X86EMUL_UNHANDLEABLE;
+            goto out;
+        }
 #ifdef __x86_64__
         if ( !(offset & 8) )
         {
             memset(p_data, 0, bytes);
-            return X86EMUL_OKAY;
+            goto out;
         }
         value = (offset - 8) >> 4;
 #else
         value = (offset - 8) >> 3;
 #endif
         if ( value >= 0x10 )
-            return X86EMUL_UNHANDLEABLE;
+        {
+            rc = X86EMUL_UNHANDLEABLE;
+            goto out;
+        }
         value |= value << 40;
         memcpy(p_data, &value, bytes);
-        return X86EMUL_OKAY;
+        goto out;
 
     case x86_seg_ldtr:
         /* Fake user segment type matching table index. */
         if ( (offset & 7) || (bytes > 8) )
-            return X86EMUL_UNHANDLEABLE;
+        {
+            rc = X86EMUL_UNHANDLEABLE;
+            goto out;
+        }
         value = offset >> 3;
         if ( value >= 0x10 )
-            return X86EMUL_UNHANDLEABLE;
+        {
+            rc = X86EMUL_UNHANDLEABLE;
+            goto out;
+        }
         value |= (value | 0x10) << 40;
         memcpy(p_data, &value, bytes);
-        return X86EMUL_OKAY;
+        goto out;
 
     default:
         if ( !is_x86_user_segment(seg) )
-            return X86EMUL_UNHANDLEABLE;
+        {
+            rc = X86EMUL_UNHANDLEABLE;
+            goto out;
+        }
         bytes_read += bytes;
         break;
     }
     memcpy(p_data, (void *)offset, bytes);
-    return X86EMUL_OKAY;
+
+ out:
+    emul_restore_fpu_state();
+
+    return rc;
 }
 
 static int fetch(
@@ -281,10 +304,15 @@ static int fetch(
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
+    emul_save_fpu_state();
+
     if ( verbose )
         printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
 
     memcpy(p_data, (void *)offset, bytes);
+
+    emul_restore_fpu_state();
+
     return X86EMUL_OKAY;
 }
 
@@ -295,13 +323,25 @@ static int write(
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
+    int rc = X86EMUL_OKAY;
+
+    emul_save_fpu_state();
+
     if ( verbose )
         printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
 
     if ( !is_x86_user_segment(seg) )
-        return X86EMUL_UNHANDLEABLE;
+    {
+        rc = X86EMUL_UNHANDLEABLE;
+        goto out;
+    }
+
     memcpy((void *)offset, p_data, bytes);
-    return X86EMUL_OKAY;
+
+ out:
+    emul_restore_fpu_state();
+
+    return rc;
 }
 
 static int cmpxchg(
@@ -312,13 +352,25 @@ static int cmpxchg(
     unsigned int bytes,
     struct x86_emulate_ctxt *ctxt)
 {
+    int rc = X86EMUL_OKAY;
+
+    emul_save_fpu_state();
+
     if ( verbose )
         printf("** %s(%u, %p,, %u,)\n", __func__, seg, (void *)offset, bytes);
 
     if ( !is_x86_user_segment(seg) )
-        return X86EMUL_UNHANDLEABLE;
+    {
+        rc = X86EMUL_UNHANDLEABLE;
+        goto out;
+    }
+
     memcpy((void *)offset, new, bytes);
-    return X86EMUL_OKAY;
+
+ out:
+    emul_restore_fpu_state();
+
+    return rc;
 }
 
 static int read_segment(
@@ -326,11 +378,23 @@ static int read_segment(
     struct segment_register *reg,
     struct x86_emulate_ctxt *ctxt)
 {
+    int rc = X86EMUL_OKAY;
+
+    emul_save_fpu_state();
+
     if ( !is_x86_user_segment(seg) )
-        return X86EMUL_UNHANDLEABLE;
+    {
+        rc = X86EMUL_UNHANDLEABLE;
+        goto out;
+    }
+
     memset(reg, 0, sizeof(*reg));
     reg->p = 1;
-    return X86EMUL_OKAY;
+
+ out:
+    emul_restore_fpu_state();
+
+    return rc;
 }
 
 static int read_msr(
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2018-03-06 20:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-06 20:24 [PATCH 0/3] tests/x86emul: Fix register corruption in the test harness Andrew Cooper
2018-03-06 20:24 ` [PATCH 1/3] tests/x86emul: Helpers to save and restore FPU state Andrew Cooper
2018-03-09 11:21   ` Jan Beulich
2018-03-09 12:37   ` Jan Beulich
2018-03-09 13:38     ` Jan Beulich
2018-03-09 13:43       ` Andrew Cooper
2018-03-06 20:24 ` Andrew Cooper [this message]
2018-03-09 11:41   ` [PATCH 2/3] tests/x86emul: Save and restore FPU state in the emulator callbacks Jan Beulich
2018-03-09 11:45     ` Andrew Cooper
2018-03-09 11:57       ` Jan Beulich
2018-03-06 20:24 ` [PATCH 3/3] tests/x86emul: Improve the utility of verbose mode Andrew Cooper
2018-03-09 11:48   ` Jan Beulich
2018-03-06 20:37 ` [PATCH 4/3] tests/x86emul: Save and restore FPU state in the middle of emulation Andrew Cooper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1520367892-15055-3-git-send-email-andrew.cooper3@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).