x86/ioemul: Rewrite stub generation to be shadow stack compatible
PATCH v2 1/1 x86/ioemul: Rewrite stub generation to be shadow stack compatible
The logic is completely undocumented and almost impossible to follow. It actually uses return oriented programming. Rewrite it to conform to more normal call mechanics, and leave a big comment explaining thing. As well as the code being easier to follow, it will execute faster as it isn't fighting the branch predictor.
Move the ioemul_handle_quirk() function pointer from traps.c to ioport_emulate.c. There is no reason for it to be in neither of the two translation units which use it. Alter the behaviour to return the number of bytes written into the stub.
Introduce a new nocall annotation using __attribute__((error)) to prohibit calls being made.
--
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
v2:
* Add ELF metadata for {load,save}_guest_gprs()
* Make ioemul_handle_quirk() __read_mostly
* Add new nocall tag
--- xen/arch/x86/ioport_emulate.c | 11 ++--- xen/arch/x86/pv/emul-priv-op.c | 92 +++++++++++++++++++++++++++++++----------- xen/arch/x86/pv/gpr_switch.S | 44 ++++++++------------ xen/arch/x86/traps.c | 3 -- xen/include/asm-x86/io.h | 3 +- xen/include/xen/compiler.h | 2 + 6 files changed, 95 insertions(+), 60 deletions(-)
xen/arch/x86/ioport_emulate.c +6 -5
@@ -8,7 +8,10 @@#include <xen/sched.h> #include <xen/dmi.h> -static bool ioemul_handle_proliant_quirk ( +unsigned int __read_mostly (*ioemul_handle_quirk)( +uint8_t opcode,char *io_emul_stub,struct cpu_user_regs *regs); + +static unsigned int ioemul_handle_proliant_quirk (u8 opcode,char *io_emul_stub,struct cpu_user_regs *regs) {static const char stub[] = {
@@ -19,18 +22,16 @@ static bool ioemul_handle_proliant_quirk(0xa8 ,0x80 ,/* test $0x80, %al */ 0x75 ,0xfb ,/* jnz 1b */ 0x9d, /* popf */ - 0xc3, /* ret */ };uint16_t port = regs->dx;uint8_t value = regs->al;if ( (opcode !=0xee ) || (port !=0xcd4 ) || !(value &0x80 ) ) -return false; +return 0 ;memcpy (io_emul_stub, stub,sizeof (stub)); -BUILD_BUG_ON (IOEMUL_QUIRK_STUB_BYTES <sizeof (stub)); -return true; +return sizeof (stub); }/* This table is the set of system-specific I/O emulation hooks. */
xen/arch/x86/pv/emul-priv-op.c +69 -23
120 lines not on this page · raw message
xen/arch/x86/pv/gpr_switch.S +17 -27
76 lines not on this page · raw message
xen/arch/x86/traps.c +0 -3
@@ -117,9 +117,6 @@ idt_entry_t *idt_tables[NR_CPUS] __read_mostly;*/ DEFINE_PER_CPU_PAGE_ALIGNED(struct tss_page, tss_page); -bool (*ioemul_handle_quirk)( -u8 opcode,char *io_emul_stub,struct cpu_user_regs *regs); -static int debug_stack_lines =20 ;integer_param ("debug_stack_lines" , debug_stack_lines);
xen/include/asm-x86/io.h +1 -2
@@ -49,8 +49,7 @@ __OUT(w,"w",short) __OUT(l,,int) /* Function pointer used to handle platform specific I/O port emulation. */ -#define IOEMUL_QUIRK_STUB_BYTES 10 -extern bool (*ioemul_handle_quirk)( +extern unsigned int (*ioemul_handle_quirk)(u8 opcode,char *io_emul_stub,struct cpu_user_regs *regs);#endif
xen/include/xen/compiler.h +2 -0
@@ -20,6 +20,8 @@#define __weak __attribute__((__weak__)) +#define nocall __attribute__((error("Nonstandard ABI"))) +#if (!defined(__clang__) && (__GNUC__ ==4 ) && (__GNUC_MINOR__ <5 ))#define unreachable () do {} while (1)#else
Signature · 1 line
2.11.0
xen/arch/x86/ioport_emulate.c line 11
Quoted 8 lines · Andrew Cooper
xen/arch/x86/ioport_emulate.c +1
-1@@ -8,7 +8,10 @@#include <xen/sched.h> #include <xen/dmi.h> -static bool ioemul_handle_proliant_quirk( +unsigned int __read_mostly (*ioemul_handle_quirk)(
I guess the more correct (and long term supported) placement is
unsigned int (*__read_mostly ioemul_handle_quirk)(
as you mean to modify the variable, not its type.
Quoted 104 lines
xen/arch/x86/pv/emul-priv-op.c +60
-21@@ -47,51 +47,97 @@ struct priv_op_ctxt {unsigned int bpmatch; }; -/* I/O emulation support. Helper routines for, and type of, the stack stub. */ -void host_to_guest_gpr_switch (struct cpu_user_regs *); -unsigned long guest_to_host_gpr_switch (unsigned long ); +/* I/O emulation helpers. Use non-standard calling conventions. */ +void nocall load_guest_gprs(struct cpu_user_regs *); +void nocall save_guest_gprs(void);typedef void io_emul_stub_t (struct cpu_user_regs *);static io_emul_stub_t *io_emul_stub_setup (struct priv_op_ctxt *ctxt,u8 opcode,unsigned int port,unsigned int bytes) { +/* +* Construct a stub for IN/OUT emulation. +* +* Some platform drivers communicate with the SMM handler using GPRs as a +* mailbox. Therefore, we must perform the emulation with the hardware +* domain's registers in view. +* +* We write a stub of the following form, using the guest load/save +* helpers (non-standard ABI), and one of several possible stubs +* performing the real I/O. +*/ +static const char prologue[] = { +0x53 ,/* push %rbx */ +0x55 ,/* push %rbp */ +0x41 ,0x54 ,/* push %r12 */ +0x41 ,0x55 ,/* push %r13 */ +0x41 ,0x56 ,/* push %r14 */ +0x41 ,0x57 ,/* push %r15 */ +0x57 ,/* push %rdi (param for save_guest_gprs) */ + };/* call load_guest_gprs */ +/* <I/O stub> */ +/* call save_guest_gprs */ +static const char epilogue[] = { +0x5f ,/* pop %rdi */ +0x41 ,0x5f ,/* pop %r15 */ +0x41 ,0x5e ,/* pop %r14 */ +0x41 ,0x5d ,/* pop %r13 */ +0x41 ,0x5c ,/* pop %r12 */ +0x5d ,/* pop %rbp */ +0x5b ,/* pop %rbx */ +0xc3 ,/* ret */ + }; +struct stubs *this_stubs = &this_cpu (stubs);unsigned long stub_va = this_stubs->addr +STUB_BUF_SIZE /2 ; -long disp; -bool use_quirk_stub = false; +unsigned int quirk_bytes =0 ; +char *p; + +/* Helpers - Read outer scope but only modify p. */ +#define APPEND_BUFF (b) ({ memcpy(p, b, sizeof(b)); p += sizeof(b); }) +#define APPEND_CALL (f) \ + ({ \ + long disp = (long)(f) - (stub_va + p - ctxt->io_emul_stub + 5); \ + BUG_ON((int32_t)disp != disp); \ + *p++ = 0xe8; \ + *(int32_t *)p = disp; p += 4; \ + })if ( !ctxt->io_emul_stub ) ctxt->io_emul_stub =map_domain_page (_mfn (this_stubs->mfn)) + (stub_va & ~PAGE_MASK ); -/* call host_to_guest_gpr_switch */ - ctxt->io_emul_stub[0 ] =0xe8 ; - disp = (long )host_to_guest_gpr_switch - (stub_va +5 ); -BUG_ON ((int32_t )disp != disp); - *(int32_t *)&ctxt->io_emul_stub[1 ] = disp; + p = ctxt->io_emul_stub; + +APPEND_BUFF (prologue); +APPEND_CALL (load_guest_gprs); +/* Some platforms might need to quirk the stub for specific inputs. */ if (unlikely (ioemul_handle_quirk) ) - use_quirk_stub =ioemul_handle_quirk (opcode, &ctxt->io_emul_stub[5 ], - ctxt->ctxt.regs); + { + quirk_bytes =ioemul_handle_quirk (opcode, p, ctxt->ctxt.regs); + p += quirk_bytes; + } -if ( !use_quirk_stub ) +/* Default I/O stub. */ +if (likely (!quirk_bytes) ) { -/* data16 or nop */ - ctxt->io_emul_stub[5 ] = (bytes !=2 ) ?0x90 :0x66 ; -/* <io-access opcode> */ - ctxt->io_emul_stub[6 ] = opcode; -/* imm8 or nop */ - ctxt->io_emul_stub[7 ] = !(opcode &8 ) ? port :0x90 ; -/* ret (jumps to guest_to_host_gpr_switch) */ - ctxt->io_emul_stub[8 ] =0xc3 ; + *p++ = (bytes !=2 ) ?0x90 :0x66 ;/* data16 or nop */
xen/arch/x86/pv/emul-priv-op.c struct priv_op_ctxt { · line 134
Quoted 10 lines
xen/arch/x86/pv/emul-priv-op.c +6
-2+ *p++ = opcode;/* <opcode> */ + *p++ = !(opcode &8 ) ? port :0x90 ;/* imm8 or nop */ } -BUILD_BUG_ON (STUB_BUF_SIZE /2 <MAX (9 ,/* Default emul stub */ -5 +IOEMUL_QUIRK_STUB_BYTES )); +APPEND_CALL (save_guest_gprs); +APPEND_BUFF (epilogue); + +BUG_ON (STUB_BUF_SIZE /2 < (p - ctxt->io_emul_stub));
I continue to view this as a dis-improvement, because a bug here may go unnoticed for a very long time, until someone runs Xen on one of the affected Proliants _and_ invokes an action triggering the quirk code. I don't see any better alternative than what I think I had already described. Hence preferably with the remark further up addressed
Jan