[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v9 12/13] test_x86_emulator.c: Add tests for #GP usage



Signed-off-by: Don Slutz <dslutz@xxxxxxxxxxx>
---
 tools/tests/x86_emulator/test_x86_emulator.c | 155 +++++++++++++++++++++++++++
 1 file changed, 155 insertions(+)

diff --git a/tools/tests/x86_emulator/test_x86_emulator.c 
b/tools/tests/x86_emulator/test_x86_emulator.c
index 02a9f0a..985c80e 100644
--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -61,6 +61,16 @@ static int write(
     return X86EMUL_OKAY;
 }
 
+static int write_gp(
+    unsigned int seg,
+    unsigned long offset,
+    void *p_data,
+    unsigned int bytes,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_EXCEPTION;
+}
+
 static int cmpxchg(
     unsigned int seg,
     unsigned long offset,
@@ -73,6 +83,17 @@ static int cmpxchg(
     return X86EMUL_OKAY;
 }
 
+static int cmpxchg_gp(
+    unsigned int seg,
+    unsigned long offset,
+    void *old,
+    void *new,
+    unsigned int bytes,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_EXCEPTION;
+}
+
 static int cpuid(
     unsigned int *eax,
     unsigned int *ebx,
@@ -156,6 +177,51 @@ int get_fpu(
     return X86EMUL_OKAY;
 }
 
+static int read_io(
+    unsigned int port,
+    unsigned int bytes,
+    unsigned long *val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    *val = 0xffffffff;
+    return X86EMUL_OKAY;
+}
+
+static int write_io(
+    unsigned int port,
+    unsigned int bytes,
+    unsigned long val,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_OKAY;
+}
+
+static int vmport_check(
+       unsigned int first_port,
+       struct x86_emulate_ctxt *ctxt)
+{
+    if ( first_port == 0x5658 )
+        return 1;
+    else
+        return 0;
+}
+
+static int read_segment(
+    enum x86_segment seg,
+    struct segment_register *reg,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_EXCEPTION;
+}
+
+static int inject_hw_exception(
+    uint8_t vector,
+    int32_t error_code,
+    struct x86_emulate_ctxt *ctxt)
+{
+    return X86EMUL_EXCEPTION;
+}
+
 static struct x86_emulate_ops emulops = {
     .read       = read,
     .insn_fetch = fetch,
@@ -165,6 +231,18 @@ static struct x86_emulate_ops emulops = {
     .get_fpu    = get_fpu,
 };
 
+static struct x86_emulate_ops emulops_gp = {
+    .read       = read,
+    .insn_fetch = fetch,
+    .write      = write_gp,
+    .cmpxchg    = cmpxchg_gp,
+    .read_io    = read_io,
+    .write_io   = write_io,
+    .read_segment = read_segment,
+    .inject_hw_exception = inject_hw_exception,
+    .vmport_check = vmport_check,
+};
+
 int main(int argc, char **argv)
 {
     struct x86_emulate_ctxt ctxt;
@@ -930,6 +1008,83 @@ int main(int argc, char **argv)
         goto fail;
     printf("okay\n");
 
+    for ( j = 0; j <= 1; j++ )
+    {
+        regs.eflags = 0x20002;
+        regs.edx    = 0x5658 + j;
+        printf("Testing %s dx=%x ...       ", "in (%dx),%eax", (int)regs.edx);
+        instr[0] = 0xed; /* in (%dx),%eax or in (%dx),%ax */
+        regs.eip    = (unsigned long)&instr[0];
+        regs.eax    = 0x12345678;
+        rc = x86_emulate(&ctxt, &emulops_gp);
+        if ( rc != X86EMUL_OKAY )
+        {
+            if ( j == 0 )
+                goto fail;
+        }
+        else if ( j == 1 )
+            goto fail;
+        if ( regs.eip != (unsigned long)&instr[1 - j] )
+            goto fail;
+        if ( j == 0 && regs.eax == 0x12345678 )
+            goto fail;
+        printf("okay\n");
+
+        printf("Testing %s  dx=%x ...       ", "in (%dx),%al", (int)regs.edx);
+        instr[0] = 0xec; /* in (%dx),%al */
+        regs.eip    = (unsigned long)&instr[0];
+        regs.eax    = 0x12345678;
+        rc = x86_emulate(&ctxt, &emulops_gp);
+        if ( rc != X86EMUL_OKAY )
+        {
+            if ( j == 0 )
+                goto fail;
+        }
+        else if ( j == 1 )
+            goto fail;
+        if ( regs.eip != (unsigned long)&instr[1 - j] )
+            goto fail;
+        if ( j == 0 && regs.eax == 0x12345678 )
+            goto fail;
+        printf("okay\n");
+
+        printf("Testing %s dx=%x ...      ", "out %eax,(%dx)", (int)regs.edx);
+        instr[0] = 0xef; /* out %eax,(%dx) or out %ax,(%dx) */
+        regs.eip    = (unsigned long)&instr[0];
+        regs.eax    = 0x12345678;
+        rc = x86_emulate(&ctxt, &emulops_gp);
+        if ( rc != X86EMUL_OKAY )
+        {
+            if ( j == 0 )
+                goto fail;
+        }
+        else if ( j == 1 )
+            goto fail;
+        if ( regs.eip != (unsigned long)&instr[1 - j] )
+            goto fail;
+        if ( regs.eax != 0x12345678 )
+            goto fail;
+        printf("okay\n");
+
+        printf("Testing %s  dx=%x ...      ", "out %al,(%dx)", (int)regs.edx);
+        instr[0] = 0xee; /* out %al,(%dx) */
+        regs.eip    = (unsigned long)&instr[0];
+        regs.eax    = 0x12345678;
+        rc = x86_emulate(&ctxt, &emulops_gp);
+        if ( rc != X86EMUL_OKAY )
+        {
+            if ( j == 0 )
+                goto fail;
+        }
+        else if ( j == 1 )
+            goto fail;
+        if ( regs.eip != (unsigned long)&instr[1 - j] )
+            goto fail;
+        if ( regs.eax != 0x12345678 )
+            goto fail;
+        printf("okay\n");
+    }
+
     return 0;
 
  fail:
-- 
1.8.4


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


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.