xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Wei Liu <wei.liu2@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>,
	Wei Liu <wei.liu2@citrix.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>,
	Jan Beulich <jbeulich@suse.com>
Subject: [PATCH RFC v2 3/7] x86emul/test: factor out emul_test_{read_cr, cpuid}
Date: Fri, 9 Dec 2016 12:23:21 +0000	[thread overview]
Message-ID: <1481286205-4373-4-git-send-email-wei.liu2@citrix.com> (raw)
In-Reply-To: <1481286205-4373-1-git-send-email-wei.liu2@citrix.com>

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Wei Liu <wei.liu2@citrix.com>
---
 tools/tests/x86_emulator/test_x86_emulator.c | 59 +++++-----------------------
 tools/tests/x86_emulator/x86_emulate.c       | 39 ++++++++++++++++++
 tools/tests/x86_emulator/x86_emulate.h       | 19 +++++++++
 3 files changed, 68 insertions(+), 49 deletions(-)

diff --git a/tools/tests/x86_emulator/test_x86_emulator.c b/tools/tests/x86_emulator/test_x86_emulator.c
index 0d80bff..6099401 100644
--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -92,51 +92,33 @@ static int cmpxchg(
     return X86EMUL_OKAY;
 }
 
-static int cpuid(
-    unsigned int *eax,
-    unsigned int *ebx,
-    unsigned int *ecx,
-    unsigned int *edx,
-    struct x86_emulate_ctxt *ctxt)
-{
-    unsigned int leaf = *eax;
-
-    asm ("cpuid" : "+a" (*eax), "+c" (*ecx), "=d" (*edx), "=b" (*ebx));
-
-    /* The emulator doesn't itself use MOVBE, so we can always run the test. */
-    if ( leaf == 1 )
-        *ecx |= 1U << 22;
-
-    return X86EMUL_OKAY;
-}
-
 #define cache_line_size() ({ \
     unsigned int eax = 1, ebx, ecx = 0, edx; \
-    cpuid(&eax, &ebx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ebx, &ecx, &edx, NULL); \
     edx & (1U << 19) ? (ebx >> 5) & 0x7f8 : 0; \
 })
 
 #define cpu_has_mmx ({ \
     unsigned int eax = 1, ecx = 0, edx; \
-    cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ecx, &ecx, &edx, NULL); \
     (edx & (1U << 23)) != 0; \
 })
 
 #define cpu_has_sse ({ \
     unsigned int eax = 1, ecx = 0, edx; \
-    cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ecx, &ecx, &edx, NULL); \
     (edx & (1U << 25)) != 0; \
 })
 
 #define cpu_has_sse2 ({ \
     unsigned int eax = 1, ecx = 0, edx; \
-    cpuid(&eax, &ecx, &ecx, &edx, NULL); \
+    emul_test_cpuid(&eax, &ecx, &ecx, &edx, NULL); \
     (edx & (1U << 26)) != 0; \
 })
 
 #define cpu_has_xsave ({ \
     unsigned int eax = 1, ecx = 0; \
-    cpuid(&eax, &eax, &ecx, &eax, NULL); \
+    emul_test_cpuid(&eax, &eax, &ecx, &eax, NULL); \
     /* Intentionally checking OSXSAVE here. */ \
     (ecx & (1U << 27)) != 0; \
 })
@@ -152,7 +134,7 @@ static inline uint64_t xgetbv(uint32_t xcr)
 
 #define cpu_has_avx ({ \
     unsigned int eax = 1, ecx = 0; \
-    cpuid(&eax, &eax, &ecx, &eax, NULL); \
+    emul_test_cpuid(&eax, &eax, &ecx, &eax, NULL); \
     if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) != 6) ) \
         ecx = 0; \
     (ecx & (1U << 28)) != 0; \
@@ -160,37 +142,16 @@ static inline uint64_t xgetbv(uint32_t xcr)
 
 #define cpu_has_avx2 ({ \
     unsigned int eax = 1, ebx, ecx = 0; \
-    cpuid(&eax, &ebx, &ecx, &eax, NULL); \
+    emul_test_cpuid(&eax, &ebx, &ecx, &eax, NULL); \
     if ( !(ecx & (1U << 27)) || ((xgetbv(0) & 6) != 6) ) \
         ebx = 0; \
     else { \
         eax = 7, ecx = 0; \
-        cpuid(&eax, &ebx, &ecx, &eax, NULL); \
+        emul_test_cpuid(&eax, &ebx, &ecx, &eax, NULL); \
     } \
     (ebx & (1U << 5)) != 0; \
 })
 
-static int read_cr(
-    unsigned int reg,
-    unsigned long *val,
-    struct x86_emulate_ctxt *ctxt)
-{
-    /* Fake just enough state for the emulator's _get_fpu() to be happy. */
-    switch ( reg )
-    {
-    case 0:
-        *val = 0x00000001; /* PE */
-        return X86EMUL_OKAY;
-
-    case 4:
-        /* OSFXSR, OSXMMEXCPT, and maybe OSXSAVE */
-        *val = 0x00000600 | (cpu_has_xsave ? 0x00040000 : 0);
-        return X86EMUL_OKAY;
-    }
-
-    return X86EMUL_UNHANDLEABLE;
-}
-
 int get_fpu(
     void (*exception_callback)(void *, struct cpu_user_regs *),
     void *exception_callback_arg,
@@ -221,8 +182,8 @@ static struct x86_emulate_ops emulops = {
     .insn_fetch = fetch,
     .write      = write,
     .cmpxchg    = cmpxchg,
-    .cpuid      = cpuid,
-    .read_cr    = read_cr,
+    .cpuid      = emul_test_cpuid,
+    .read_cr    = emul_test_read_cr,
     .get_fpu    = get_fpu,
 };
 
diff --git a/tools/tests/x86_emulator/x86_emulate.c b/tools/tests/x86_emulator/x86_emulate.c
index 78e6380..b4bd61c 100644
--- a/tools/tests/x86_emulator/x86_emulate.c
+++ b/tools/tests/x86_emulator/x86_emulate.c
@@ -40,4 +40,43 @@ bool emul_test_make_stack_executable(void)
     return stack_exec;
 }
 
+int emul_test_cpuid(
+    unsigned int *eax,
+    unsigned int *ebx,
+    unsigned int *ecx,
+    unsigned int *edx,
+    struct x86_emulate_ctxt *ctxt)
+{
+    unsigned int leaf = *eax;
+
+    asm ("cpuid" : "+a" (*eax), "+c" (*ecx), "=d" (*edx), "=b" (*ebx));
+
+    /* The emulator doesn't itself use MOVBE, so we can always run the test. */
+    if ( leaf == 1 )
+        *ecx |= 1U << 22;
+
+    return X86EMUL_OKAY;
+}
+
+int emul_test_read_cr(
+    unsigned int reg,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    /* Fake just enough state for the emulator's _get_fpu() to be happy. */
+    switch ( reg )
+    {
+    case 0:
+        *val = 0x00000001; /* PE */
+        return X86EMUL_OKAY;
+
+    case 4:
+        /* OSFXSR, OSXMMEXCPT, and maybe OSXSAVE */
+        *val = 0x00000600 | (cpu_has_xsave ? 0x00040000 : 0);
+        return X86EMUL_OKAY;
+    }
+
+    return X86EMUL_UNHANDLEABLE;
+}
+
 #include "x86_emulate/x86_emulate.c"
diff --git a/tools/tests/x86_emulator/x86_emulate.h b/tools/tests/x86_emulator/x86_emulate.h
index a9b874c..b213f2d 100644
--- a/tools/tests/x86_emulator/x86_emulate.h
+++ b/tools/tests/x86_emulator/x86_emulate.h
@@ -37,3 +37,22 @@
 bool emul_test_make_stack_executable(void);
 
 #include "x86_emulate/x86_emulate.h"
+
+#define cpu_has_xsave ({	   \
+    unsigned int eax = 1, ecx = 0; \
+    emul_test_cpuid(&eax, &eax, &ecx, &eax, NULL); \
+    /* Intentionally checking OSXSAVE here. */ \
+    (ecx & (1U << 27)) != 0; \
+})
+
+int emul_test_cpuid(
+    unsigned int *eax,
+    unsigned int *ebx,
+    unsigned int *ecx,
+    unsigned int *edx,
+    struct x86_emulate_ctxt *ctxt);
+
+int emul_test_read_cr(
+    unsigned int reg,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt);
-- 
2.1.4


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

  parent reply	other threads:[~2016-12-09 12:23 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-09 12:23 [PATCH RFC v2 0/7] Fuzzing targets for oss-fuzz Wei Liu
2016-12-09 12:23 ` [PATCH RFC v2 1/7] tools/fuzz: introduce libelf target Wei Liu
2016-12-09 12:23 ` [PATCH RFC v2 2/7] x86emul/test: factor out emul_test_make_stack_executable Wei Liu
2016-12-09 13:10   ` Jan Beulich
2016-12-09 12:23 ` Wei Liu [this message]
2016-12-09 13:13   ` [PATCH RFC v2 3/7] x86emul/test: factor out emul_test_{read_cr, cpuid} Jan Beulich
2016-12-09 13:35     ` Wei Liu
2016-12-09 12:23 ` [PATCH RFC v2 4/7] x86emul/test: remove unused macros Wei Liu
2016-12-09 13:15   ` Jan Beulich
2016-12-09 13:35     ` Wei Liu
2016-12-09 12:23 ` [PATCH RFC v2 5/7] tools/fuzz: introduce x86 instruction emulator target Wei Liu
2016-12-09 13:22   ` Jan Beulich
2016-12-09 12:23 ` [PATCH RFC v2 6/7] tools: hook up fuzz directory Wei Liu
2016-12-09 12:23 ` [PATCH RFC v2 7/7] tools/fuzz: add README Wei Liu
2016-12-09 13:07 ` [PATCH RFC v2 0/7] Fuzzing targets for oss-fuzz Jan Beulich
2016-12-09 13:09   ` Wei Liu
2016-12-09 13:25     ` Jan Beulich

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=1481286205-4373-4-git-send-email-wei.liu2@citrix.com \
    --to=wei.liu2@citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=jbeulich@suse.com \
    --cc=xen-devel@lists.xenproject.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).