Skip to contentSkip to conversation

x86/ioemul: Account for ioemul_handle_quirk() in stub length check

PATCH In review Andrew Cooper 2 participants 4 messages Jan 9, 2018 – Jan 10, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/ioemul: Account for ioemul_handle_quirk() in stub length check

The opcode potentially written into ctxt->io_emul_stub[] in the case that ioemul_handle_quirk() is overriding the default logic isnt accounted for in the build-time check that the stubs are large enough.

Introduce IOEMUL_QUIRK_STUB_BYTES and use for both the main and quirk stub cases. As a slim optimisation, avoid writing out the default stub when we know we are going to overwrite it.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/ioport_emulate.c  |  2 ++
 xen/arch/x86/pv/emul-priv-op.c | 27 ++++++++++++++++-----------
 xen/include/asm-x86/io.h       |  1 +
 3 files changed, 19 insertions(+), 11 deletions(-)
xen/arch/x86/ioport_emulate.c +2 -0
index 58d2b53..1f6f794 100644
@@ -35,6 +35,8 @@ static void ioemul_handle_proliant_quirk(
     io_emul_stub[8] = 0x9d;
     /*    ret */
     io_emul_stub[9] = 0xc3;
+
+    BUILD_BUG_ON(IOEMUL_QUIRK_STUB_BYTES < 10);
 }
 
 static int __init proliant_quirk(struct dmi_system_id *d)
xen/arch/x86/pv/emul-priv-op.c +16 -11
index 1041a4c..4087cf2 100644
@@ -89,19 +89,24 @@ static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode,
     /* callq *%rcx */
     ctxt->io_emul_stub[10] = 0xff;
     ctxt->io_emul_stub[11] = 0xd1;
-    /* data16 or nop */
-    ctxt->io_emul_stub[12] = (bytes != 2) ? 0x90 : 0x66;
-    /* <io-access opcode> */
-    ctxt->io_emul_stub[13] = opcode;
-    /* imm8 or nop */
-    ctxt->io_emul_stub[14] = !(opcode & 8) ? port : 0x90;
-    /* ret (jumps to guest_to_host_gpr_switch) */
-    ctxt->io_emul_stub[15] = 0xc3;
-    BUILD_BUG_ON(STUB_BUF_SIZE / 2 < 16);
-
-    if ( ioemul_handle_quirk )
+
+    if ( likely(!ioemul_handle_quirk) )
+    {
+        /* data16 or nop */
+        ctxt->io_emul_stub[12] = (bytes != 2) ? 0x90 : 0x66;
+        /* <io-access opcode> */
+        ctxt->io_emul_stub[13] = opcode;
+        /* imm8 or nop */
+        ctxt->io_emul_stub[14] = !(opcode & 8) ? port : 0x90;
+        /* ret (jumps to guest_to_host_gpr_switch) */
+        ctxt->io_emul_stub[15] = 0xc3;
+    }
+    else
         ioemul_handle_quirk(opcode, &ctxt->io_emul_stub[12], ctxt->ctxt.regs);
 
+    BUILD_BUG_ON(STUB_BUF_SIZE / 2 < MAX(16, /* Regular stubs */
+                                         12 + IOEMUL_QUIRK_STUB_BYTES));
+
     /* Handy function-typed pointer to the stub. */
     return (void *)(this_cpu(stubs.addr) + STUB_BUF_SIZE / 2);
 }
xen/include/asm-x86/io.h +1 -0
index b156f48..e6bb20c 100644
@@ -51,6 +51,7 @@ __OUT(l,,int)
 extern void (*pv_post_outb_hook)(unsigned int port, u8 value);
 
 /* Function pointer used to handle platform specific I/O port emulation. */
+#define IOEMUL_QUIRK_STUB_BYTES 10
 extern void (*ioemul_handle_quirk)(
     u8 opcode, char *io_emul_stub, struct cpu_user_regs *regs);
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 09.01.18 at 17:47, <[email protected]> wrote:

The opcode potentially written into ctxt->io_emul_stub[] in the case that ioemul_handle_quirk() is overriding the default logic isnt accounted for in the build-time check that the stubs are large enough.

Introduce IOEMUL_QUIRK_STUB_BYTES and use for both the main and quirk stub cases. As a slim optimisation, avoid writing out the default stub when we know we are going to overwrite it.

with one suggestion:

Quoted 23 lines
xen/arch/x86/pv/emul-priv-op.c +6 -11
@@ -89,19 +89,24 @@ static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode,
     /* callq *%rcx */
     ctxt->io_emul_stub[10] = 0xff;
     ctxt->io_emul_stub[11] = 0xd1;
-    /* data16 or nop */
-    ctxt->io_emul_stub[12] = (bytes != 2) ? 0x90 : 0x66;
-    /* <io-access opcode> */
-    ctxt->io_emul_stub[13] = opcode;
-    /* imm8 or nop */
-    ctxt->io_emul_stub[14] = !(opcode & 8) ? port : 0x90;
-    /* ret (jumps to guest_to_host_gpr_switch) */
-    ctxt->io_emul_stub[15] = 0xc3;
-    BUILD_BUG_ON(STUB_BUF_SIZE / 2 < 16);
-
-    if ( ioemul_handle_quirk )
+
+    if ( likely(!ioemul_handle_quirk) )
+    {
+        /* data16 or nop */
+        ctxt->io_emul_stub[12] = (bytes != 2) ? 0x90 : 0x66;
+        /* <io-access opcode> */

xen/arch/x86/pv/emul-priv-op.c static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode, · line 107

Quoted 10 lines
xen/arch/x86/pv/emul-priv-op.c +8 -0
+        ctxt->io_emul_stub[13] = opcode;
+        /* imm8 or nop */
+        ctxt->io_emul_stub[14] = !(opcode & 8) ? port : 0x90;
+        /* ret (jumps to guest_to_host_gpr_switch) */
+        ctxt->io_emul_stub[15] = 0xc3;
+    }
+    else
         ioemul_handle_quirk(opcode, &ctxt->io_emul_stub[12], ctxt->ctxt.regs);
 
+    BUILD_BUG_ON(STUB_BUF_SIZE / 2 < MAX(16, /* Regular stubs */

Now that we have it available globally, would you mind using MAX_INST_LEN + 1 here instead of the literal 16?

Jan

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

#2 lore raw
Quoted 45 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 09.01.18 at 17:47, <[email protected]> wrote:

The opcode potentially written into ctxt->io_emul_stub[] in the case that ioemul_handle_quirk() is overriding the default logic isnt accounted for in the build-time check that the stubs are large enough.

Introduce IOEMUL_QUIRK_STUB_BYTES and use for both the main and quirk stub cases. As a slim optimisation, avoid writing out the default stub when we know we are going to overwrite it.

with one suggestion:

Quoted 32 lines
xen/arch/x86/pv/emul-priv-op.c +13 -11
@@ -89,19 +89,24 @@ static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode,
     /* callq *%rcx */
     ctxt->io_emul_stub[10] = 0xff;
     ctxt->io_emul_stub[11] = 0xd1;
-    /* data16 or nop */
-    ctxt->io_emul_stub[12] = (bytes != 2) ? 0x90 : 0x66;
-    /* <io-access opcode> */
-    ctxt->io_emul_stub[13] = opcode;
-    /* imm8 or nop */
-    ctxt->io_emul_stub[14] = !(opcode & 8) ? port : 0x90;
-    /* ret (jumps to guest_to_host_gpr_switch) */
-    ctxt->io_emul_stub[15] = 0xc3;
-    BUILD_BUG_ON(STUB_BUF_SIZE / 2 < 16);
-
-    if ( ioemul_handle_quirk )
+
+    if ( likely(!ioemul_handle_quirk) )
+    {
+        /* data16 or nop */
+        ctxt->io_emul_stub[12] = (bytes != 2) ? 0x90 : 0x66;
+        /* <io-access opcode> */
+        ctxt->io_emul_stub[13] = opcode;
+        /* imm8 or nop */
+        ctxt->io_emul_stub[14] = !(opcode & 8) ? port : 0x90;
+        /* ret (jumps to guest_to_host_gpr_switch) */
+        ctxt->io_emul_stub[15] = 0xc3;
+    }
+    else
         ioemul_handle_quirk(opcode, &ctxt->io_emul_stub[12], ctxt->ctxt.regs);
 
Quoted 3 lines

xen/arch/x86/pv/emul-priv-op.c static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode, · line 107

Quoted 1 line
xen/arch/x86/pv/emul-priv-op.c +1 -0
+    BUILD_BUG_ON(STUB_BUF_SIZE / 2 < MAX(16, /* Regular stubs */

Now that we have it available globally, would you mind using MAX_INST_LEN + 1 here instead of the literal 16?

This raw 16 is because we write 16 bytes into ctxt->io_emul_stub[], and this jumps to 19 with the INDIRECT_THUNK work, not because it is related to MAX_INST_LEN.

~Andrew

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

#3 lore raw
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 10.01.18 at 10:56, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 09.01.18 at 17:47, <[email protected]> wrote:
xen/arch/x86/pv/emul-priv-op.c +13 -11
@@ -89,19 +89,24 @@ static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode,
     /* callq *%rcx */
     ctxt->io_emul_stub[10] = 0xff;
     ctxt->io_emul_stub[11] = 0xd1;
-    /* data16 or nop */
-    ctxt->io_emul_stub[12] = (bytes != 2) ? 0x90 : 0x66;
-    /* <io-access opcode> */
-    ctxt->io_emul_stub[13] = opcode;
-    /* imm8 or nop */
-    ctxt->io_emul_stub[14] = !(opcode & 8) ? port : 0x90;
-    /* ret (jumps to guest_to_host_gpr_switch) */
-    ctxt->io_emul_stub[15] = 0xc3;
-    BUILD_BUG_ON(STUB_BUF_SIZE / 2 < 16);
-
-    if ( ioemul_handle_quirk )
+
+    if ( likely(!ioemul_handle_quirk) )
+    {
+        /* data16 or nop */
+        ctxt->io_emul_stub[12] = (bytes != 2) ? 0x90 : 0x66;
+        /* <io-access opcode> */
+        ctxt->io_emul_stub[13] = opcode;
+        /* imm8 or nop */
+        ctxt->io_emul_stub[14] = !(opcode & 8) ? port : 0x90;
+        /* ret (jumps to guest_to_host_gpr_switch) */
+        ctxt->io_emul_stub[15] = 0xc3;
+    }
+    else
         ioemul_handle_quirk(opcode, &ctxt->io_emul_stub[12], ctxt->ctxt.regs);
 
Quoted 7 lines
Quoted 3 lines

xen/arch/x86/pv/emul-priv-op.c static io_emul_stub_t *io_emul_stub_setup(struct priv_op_ctxt *ctxt, u8 opcode, · line 107

Quoted 1 line
xen/arch/x86/pv/emul-priv-op.c +1 -0
+    BUILD_BUG_ON(STUB_BUF_SIZE / 2 < MAX(16, /* Regular stubs */

Now that we have it available globally, would you mind using MAX_INST_LEN + 1 here instead of the literal 16?

This raw 16 is because we write 16 bytes into ctxt->io_emul_stub[], and this jumps to 19 with the INDIRECT_THUNK work, not because it is related to MAX_INST_LEN.

I don't follow - as the comment says, the 16 refers to regular stubs, which are there to allow for maximum insn length plus RET.

Jan

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