x86/ioemul: Rewrite stub generation
PATCH 1/1 x86/ioemul: Rewrite stub generation
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.
Access the addresses of the host/guest helpers with extern const char arrays. Nothing good will come of C thinking they are regular functions.
--
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/ioport_emulate.c | 11 ++--- xen/arch/x86/pv/emul-priv-op.c | 91 +++++++++++++++++++++++++++++++----------- xen/arch/x86/pv/gpr_switch.S | 37 +++++------------ xen/arch/x86/traps.c | 3 -- xen/include/asm-x86/io.h | 3 +- 5 files changed, 85 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 (*ioemul_handle_quirk)( +u8 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 +68 -23
119 lines not on this page · raw message
xen/arch/x86/pv/gpr_switch.S +10 -27
@@ -9,59 +9,42 @@ #include <asm/asm_defns.h> -ENTRY(host_to_guest_gpr_switch) - movq (%rsp), %rcx - movq %rdi, (%rsp) +/* Load guest GPRs. Parameter in %rdi, clobbers all registers. */ +ENTRY(load_guest_gprs) movq UREGS_rdx(%rdi), %rdx - pushq %rbx movq UREGS_rax(%rdi), %rax movq UREGS_rbx(%rdi), %rbx - pushq %rbp movq UREGS_rsi(%rdi), %rsi movq UREGS_rbp(%rdi), %rbp - pushq %r12 - movq UREGS_r8(%rdi), %r8 + movq UREGS_r8 (%rdi), %r8 movq UREGS_r12(%rdi), %r12 - pushq %r13 - movq UREGS_r9(%rdi), %r9 + movq UREGS_r9 (%rdi), %r9 movq UREGS_r13(%rdi), %r13 - pushq %r14 movq UREGS_r10(%rdi), %r10 movq UREGS_r14(%rdi), %r14 - pushq %r15 movq UREGS_r11(%rdi), %r11 movq UREGS_r15(%rdi), %r15 - pushq %rcx /* dummy push, filled by guest_to_host_gpr_switch pointer */ - pushq %rcx - leaq guest_to_host_gpr_switch(%rip),%rcx - movq %rcx,8(%rsp) movq UREGS_rcx(%rdi), %rcx movq UREGS_rdi(%rdi), %rdi ret -ENTRY(guest_to_host_gpr_switch) +/* Save guest GPRs. Parameter on the stack above the return address. */ +ENTRY(save_guest_gprs) pushq %rdi - movq 7*8(%rsp), %rdi + movq 2*8(%rsp), %rdi movq %rax, UREGS_rax(%rdi) - popq UREGS_rdi(%rdi) + popq UREGS_rdi(%rdi) movq %r15, UREGS_r15(%rdi) movq %r11, UREGS_r11(%rdi) - popq %r15 movq %r14, UREGS_r14(%rdi) movq %r10, UREGS_r10(%rdi) - popq %r14 movq %r13, UREGS_r13(%rdi) - movq %r9, UREGS_r9(%rdi) - popq %r13 + movq %r9, UREGS_r9 (%rdi) movq %r12, UREGS_r12(%rdi) - movq %r8, UREGS_r8(%rdi) - popq %r12 + movq %r8, UREGS_r8 (%rdi) movq %rbp, UREGS_rbp(%rdi) movq %rsi, UREGS_rsi(%rdi) - popq %rbp movq %rbx, UREGS_rbx(%rdi) movq %rdx, UREGS_rdx(%rdi) - popq %rbx movq %rcx, UREGS_rcx(%rdi) - popq %rcx ret
xen/arch/x86/traps.c +0 -3
@@ -116,9 +116,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
Signature · 1 line
2.11.0
Quoted 8 lines · Andrew Cooper
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 fromtraps.ctoioport_emulate.c.
Seeing as the newest quirk was added in 2008, I wonder if such quirks are still relevant?
Maybe they are also used by newer boxes, I really have no idea.
Quoted 25 lines
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.
Access the addresses of the host/guest helpers with extern const char arrays. Nothing good will come of C thinking they are regular functions.
Signed-off-by Andrew Cooper <[email protected]>--
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>--- xen/arch/x86/ioport_emulate.c | 11 ++--- xen/arch/x86/pv/emul-priv-op.c | 91 +++++++++++++++++++++++++++++++----------- xen/arch/x86/pv/gpr_switch.S | 37 +++++------------ xen/arch/x86/traps.c | 3 -- xen/include/asm-x86/io.h | 3 +- 5 files changed, 85 insertions(+), 60 deletions(-)xen/arch/x86/ioport_emulate.c +0
-0
xen/arch/x86/ioport_emulate.c line 12
Quoted 6 lines
xen/arch/x86/ioport_emulate.c +2
-1@@ -8,7 +8,10 @@#include <xen/sched.h> #include <xen/dmi.h> -static bool ioemul_handle_proliant_quirk( +unsigned int (*ioemul_handle_quirk)( +u8 opcode,char *io_emul_stub,struct cpu_user_regs *regs);
uint8_t for opcode and also I think regs can be made const?
(at least looking at the only implementation from ioemul_handle_proliant_quirk). I'm not familiar with this area however.
Quoted 81 lines
xen/arch/x86/ioport_emulate.c +4
-4+ +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 +35
-4@@ -54,51 +54,96 @@ 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. */ +extern const char load_guest_gprs[], save_guest_gprs[];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 (abnormal calling conventions), 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;
xen/arch/x86/pv/emul-priv-op.c struct priv_op_ctxt { · line 108
Quoted 10 lines
xen/arch/x86/pv/emul-priv-op.c +9
-1-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); \
I'm not sure I get the point of using signed integers instead of unsigned ones, AFAICT you just want to check that the displacement is < 4GB so that a relative call can be used?
Thanks, Roger.
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Andrew Cooper
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.
Quoted 6 lines
Quoted 2 lines
Move the
ioemul_handle_quirk()function pointer fromtraps.ctoioport_emulate.c.Seeing as the newest quirk was added in 2008, I wonder if such quirks are still relevant?
Maybe they are also used by newer boxes, I really have no idea.
Its not something which I'd consider altering in this patch anyway.
Quoted 87 lines
Quoted 87 lines
xen/arch/x86/ioport_emulate.c +4
-4+ +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 +39
-5@@ -54,51 +54,96 @@ 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. */ +extern const char load_guest_gprs[], save_guest_gprs[];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 (abnormal calling conventions), 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. */
Quoted 8 lines
xen/arch/x86/pv/emul-priv-op.c struct priv_op_ctxt { · line 108
Quoted 5 lines
xen/arch/x86/pv/emul-priv-op.c +5
-0+#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); \I'm not sure I get the point of using signed integers instead of unsigned ones, AFAICT you just want to check that the displacement is < 4GB so that a relative call can be used?
Displacements are +/- 2G, not <4G.
Using unsigned here would be buggy.
~Andrew
Roger Pau Monné
Reviewed-byQuoted 12 lines · Andrew Cooper
Quoted 10 lines · Roger Pau Monné
xen/arch/x86/pv/emul-priv-op.c struct priv_op_ctxt { · line 108
Quoted 6 lines · Andrew Cooper
xen/arch/x86/pv/emul-priv-op.c +6
-0+/* 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); \I'm not sure I get the point of using signed integers instead of unsigned ones, AFAICT you just want to check that the displacement is < 4GB so that a relative call can be used?
Quoted 3 lines
Displacements are +/- 2G, not <4G.
Using unsigned here would be buggy.
Right, sorry for the noise:
With the minor nits pointed above in the ioemul_handle_quirk.
Thanks, Roger.
Quoted 6 lines · Andrew Cooper
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.
Quoted 7 lines
Move the
ioemul_handle_quirk()function pointer fromtraps.ctoioport_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.Access the addresses of the host/guest helpers with extern const char arrays. Nothing good will come of C thinking they are regular functions.
I agree with the C aspect, but imo the assembly routines should, with the changes you make, be marked as being ordinary functions. A reasonable linker would then warn about the C file wanting an STT_OBJECT while the assembly file provides an STT_FUNC. I'd therefore prefer, along with marking the functions as such, to have them also declared as functions in C.
xen/arch/x86/ioport_emulate.c line 12
Quoted 9 lines
xen/arch/x86/ioport_emulate.c +2
-1@@ -8,7 +8,10 @@#include <xen/sched.h> #include <xen/dmi.h> -static bool ioemul_handle_proliant_quirk( +unsigned int (*ioemul_handle_quirk)( +u8 opcode,char *io_emul_stub,struct cpu_user_regs *regs);
Would you mind adding __read_mostly on this occasion?
Quoted 5 lines
xen/arch/x86/ioport_emulate.c +0
-1@@ -19,18 +22,16 @@ static bool ioemul_handle_proliant_quirk(0xa8 ,0x80 ,/* test $0x80, %al */ 0x75 ,0xfb ,/* jnz 1b */ 0x9d, /* popf */ - 0xc3, /* ret */
xen/arch/x86/ioport_emulate.c static bool ioemul_handle_proliant_quirk( · line 31
Quoted 10 lines
xen/arch/x86/ioport_emulate.c +1
-2};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));
So you treat a build failure for a runtime crash. I can see the advantages of the new approach, but the original got away with less stub space. If our L1_CACHE_SHIFT wasn't hard coded to 7 just to cover some unusual CPUs, your new approach would, if I got the counting and calculations right, not work (with a value resulting in a 64-byte cache line size). Introducing a Kconfig option for this should imo not come with a need to re-work the logic here again. Therefore I'd like us to think about a way to make the space needed not exceed 32 bytes.
One option might be to arrange for some or all of R12-R15 to not need spilling. And of course there then still ought to be a BUILD_BUG_ON() identifying ahead of any testing that yet smaller cache line sizes would indeed require re-work here.
Jan
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper
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 fromtraps.ctoioport_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.
Quoted 4 lines
Quoted 2 lines
Access the addresses of the host/guest helpers with extern const char arrays. Nothing good will come of C thinking they are regular functions.
I agree with the C aspect, but imo the assembly routines should, with the changes you make, be marked as being ordinary functions.
Despite the changes, they are still very much not ordinary functions, and cannot be used by C.
I have no objection to marking them as STT_FUNCTION (as this doesn't mean C function), but...
Quoted 4 lines
A reasonable linker would then warn about the C file wanting an
STT_OBJECTwhile the assembly file provides anSTT_FUNC. I'd therefore prefer, along with marking the functions as such, to have them also declared as functions in C.
... there is literally nothing safe which C can do with them other than manipulate their address.
Writing it like this is specifically prevents something from compiling which will explode at runtime, is someone is naive enough to try using the function from C.
Quoted 20 lines
xen/arch/x86/ioport_emulate.c line 12
Quoted 9 lines
xen/arch/x86/ioport_emulate.c +2
-1@@ -8,7 +8,10 @@#include <xen/sched.h> #include <xen/dmi.h> -static bool ioemul_handle_proliant_quirk( +unsigned int (*ioemul_handle_quirk)( +u8 opcode,char *io_emul_stub,struct cpu_user_regs *regs);Would you mind adding
__read_mostlyon this occasion?Quoted 9 lines
xen/arch/x86/ioport_emulate.c +0
-1@@ -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;
Quoted 7 lines
xen/arch/x86/ioport_emulate.c static bool ioemul_handle_proliant_quirk( · line 31
Quoted 6 lines
xen/arch/x86/ioport_emulate.c +1
-2if ( (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));So you treat a build failure for a runtime crash.
I presume you mean s/treat/trade/ here, and the answer is no, not really.
There is nothing which actually forced a connection between the build time checks and runtime behaviour, so it was only a facade of safety, not real safety.
Quoted 3 lines
I can see the
advantages of the new approach, but the original got away with less stub space.
Stub space doesn't matter, so long as it fits. In particular, ...
Quoted 4 lines
If our
L1_CACHE_SHIFTwasn't hard coded to 7
just to cover some unusual CPUs, your new approach would, if I got the counting and calculations right, not work (with a value resulting in a 64-byte cache line size).
... the SYSCALL stubs use 64 bytes so Xen cannot function with a shift less than 7.
Quoted 4 lines
Introducing a
Kconfig
option for this should imo not come with a need to re-work the logic here again. Therefore I'd like us to think about a way to make the space needed not exceed 32 bytes.
And why would we ever want an option like that? (I know how you're going to answer this, so I'm going to pre-emptively point out that there are hundreds of kilobytes of easier-to-shrink per-cpu data structures than this one).
Honestly, this suggestion is a total waste of time and effort. It is an enormous amount of complexity to micro-optimise a problem which doesn't exist in the first place.
The stubs are already 128 bytes per CPU and cannot be made smaller. Attempting to turn this particular stub into <32 has no benefit (the stubs don't actually get smaller), and major costs.
~Andrew
Quoted 29 lines · Andrew Cooper
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper
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 fromtraps.ctoioport_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.Quoted 4 lines
Quoted 2 lines
Access the addresses of the host/guest helpers with extern const char arrays. Nothing good will come of C thinking they are regular functions.
I agree with the C aspect, but imo the assembly routines should, with the changes you make, be marked as being ordinary functions.
Despite the changes, they are still very much not ordinary functions, and cannot be used by C.
I have no objection to marking them as
STT_FUNCTION(as this doesn't mean C function), but...Quoted 4 lines
A reasonable linker would then warn about the C file wanting an
STT_OBJECTwhile the assembly file provides anSTT_FUNC. I'd therefore prefer, along with marking the functions as such, to have them also declared as functions in C.
Quoted 6 lines
... there is literally nothing safe which C can do with them other than manipulate their address.
Writing it like this is specifically prevents something from compiling which will explode at runtime, is someone is naive enough to try using the function from C.
Besides being certain that such an attempt, if it made it into a submitted patch in the first place, would be caught by review, I don't see you addressing my main counter argument. Preventing a declared function to be called can be had by other means, e.g. __attribute__((__warning__())).
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/ioport_emulate.c +0
-1@@ -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;Quoted 7 lines
xen/arch/x86/ioport_emulate.c static bool ioemul_handle_proliant_quirk( · line 31
Quoted 6 lines
xen/arch/x86/ioport_emulate.c +1
-2if ( (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));So you treat a build failure for a runtime crash.
Quoted 5 lines
I presume you mean s/treat/trade/ here, and the answer is no, not really.
There is nothing which actually forced a connection between the build time checks and runtime behaviour, so it was only a facade of safety, not real safety.
I'm not following, I'm afraid: The above together with
BUILD_BUG_ON(STUB_BUF_SIZE / 2 < MAX(9, /* Default emul stub */
5 + IOEMUL_QUIRK_STUB_BYTES));
(where the literal numbers live next to what they describe) did very well provide some level of guarding.
Quoted 6 lines
Quoted 3 lines
I can see the
advantages of the new approach, but the original got away with less stub space.Stub space doesn't matter, so long as it fits. In particular, ...
Quoted 7 lines
Quoted 4 lines
If our
L1_CACHE_SHIFTwasn't hard coded to 7
just to cover some unusual CPUs, your new approach would, if I got the counting and calculations right, not work (with a value resulting in a 64-byte cache line size).... the SYSCALL stubs use 64 bytes so Xen cannot function with a shift less than 7.
Oh, my fault - I read the STUB_BUF_SHIFT expression as min() when it really is max(). So yes, we can rely on there being 64 bytes. (Everything further down therefore becomes moot afaict.)
Quoted 9 lines
Quoted 4 lines
Introducing a
Kconfig
option for this should imo not come with a need to re-work the logic here again. Therefore I'd like us to think about a way to make the space needed not exceed 32 bytes.And why would we ever want an option like that? (I know how you're going to answer this, so I'm going to pre-emptively point out that there are hundreds of kilobytes of easier-to-shrink per-cpu data structures than this one).
Not sure what per-CPU data structures you talk about. I wasn't thinking of space savings in particular, but rather about getting our setting in sync with actual hardware. Its value is, afaics, used in a far more relevant way by xmalloc() and friends.
Jan
Quoted 10 lines
Honestly, this suggestion is a total waste of time and effort. It is an enormous amount of complexity to micro-optimise a problem which doesn't exist in the first place.
The stubs are already 128 bytes per CPU and cannot be made smaller. Attempting to turn this particular stub into <32 has no benefit (the stubs don't actually get smaller), and major costs.
~Andrew