x86/fred: Fix SYSCALL handling
cover x86/fred: Fix SYSCALL handling
Andrew Cooper (2):
x86/fred: Fix FRED name in comments
x86/pv: Provide better SYSCALL backwards compatibility in FRED mode
xen/arch/x86/include/asm/x86-defns.h | 2 +- xen/arch/x86/traps.c | 2 ++ xen/arch/x86/x86_64/entry-fred.S | 12 +++++++++++- xen/include/public/arch-x86/cpufeatureset.h | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-)
Signature · 1 line
2.39.5
PATCH 1/2 x86/fred: Fix FRED name in comments
The patch introducing these constants predate the feature being renamed.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
--- xen/arch/x86/include/asm/x86-defns.h | 2 +- xen/include/public/arch-x86/cpufeatureset.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/include/asm/x86-defns.h +1 -1
@@ -82,7 +82,7 @@#define X86_CR4_PKE 0x00400000/* enable PKE */ #define X86_CR4_CET 0x00800000/* Control-flow Enforcement Technology */ #define X86_CR4_PKS 0x01000000/* Protection Key Supervisor */ -#define X86_CR4_FRED (_AC(1, ULL) << 32)/* Fast Return and Event Delivery */ +#define X86_CR4_FRED (_AC(1, ULL) << 32)/* Flexible Return and Event Delivery */ #define X86_CR8_VALID_MASK 0xf
xen/include/public/arch-x86/cpufeatureset.h +1 -1
@@ -310,7 +310,7 @@ XEN_CPUFEATURE(ARCH_PERF_MON, 10*32+8) /* Architectural Perfmon */ XEN_CPUFEATURE(FZRM, 10*32+10) /*A Fast Zero-length REP MOVSB */ XEN_CPUFEATURE(FSRS, 10*32+11) /*A Fast Short REP STOSB */ XEN_CPUFEATURE(FSRCS, 10*32+12) /*A Fast Short REP CMPSB/SCASB */ -XEN_CPUFEATURE(FRED, 10*32+17) /* Fast Return and Event Delivery */ +XEN_CPUFEATURE(FRED, 10*32+17) /* Flexible Return and Event Delivery */ XEN_CPUFEATURE(LKGS, 10*32+18) /* Load Kernel GS instruction */ XEN_CPUFEATURE(WRMSRNS, 10*32+19) /*S WRMSR Non-Serialising */XEN_CPUFEATURE (NMI_SRC ,10 *32 +20 )/* NMI-Source Reporting */
Signature · 1 line
2.39.5
Re: [PATCH 1/2] x86/fred: Fix FRED name in comments
Quoted 3 lines · Andrew Cooper
The patch introducing these constants predate the feature being renamed.
Signed-off-by Andrew Cooper <[email protected]>
PATCH 2/2 x86/pv: Provide better SYSCALL backwards compatibility in FRED mode
In reply to Andrew CooperIn FRED mode, the SYSCALL instruction does not modify %rcx/%r11. Software using SYSCALL spills %rcx/%r11 around the invocation, which is why FRED not doing this goes largely unnoticed.
However, consider the following migration scenario:
* VM suspends. Hypercall, so SYSCALL, %rcx/%r11 left unmodified
* VM moves to a non-FRED system
* Xen resumes the VM with a real SYSRET instruction
Instead of resuming at the instruction following the SYSCALL instruction, the VM is resumed at whatever dead value was in %rcx.
In FRED mode, manually adjust %rcx/%r11 when SYSCALL is and SYSRET would have been used.
Regarding the choice of instructions in eretu_exit_to_guest(), a branch would be a context dependent 50/50 split (i.e. increased chance of mispredict), and only saves one instruction. The CMOVs read the same cacheline that ERETU is about to process, so are as close to free as we can reasonably get.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
Slightly RFC. I'm still still completing the testing for this.
--- xen/arch/x86/traps.c | 2 ++ xen/arch/x86/x86_64/entry-fred.S | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-)
xen/arch/x86/traps.c +2 -0
@@ -2405,6 +2405,8 @@ void asmlinkage entry_from_pv(struct cpu_user_regs *regs) regs->ssx = l ?FLAT_KERNEL_SS :FLAT_USER_SS32 ; regs->csx = l ?FLAT_KERNEL_CS64 :FLAT_USER_CS32 ; + regs->rcx = regs->rip; + regs->r11 = regs->rflags;if (guest_kernel_mode (curr, regs) )pv_hypercall (regs);
xen/arch/x86/x86_64/entry-fred.S +11 -1
@@ -4,6 +4,7 @@ #include <asm/asm_defns.h> #include <asm/page.h> +#include <asm/processor.h> .section .text.entry, "ax", @progbits
@@ -26,7 +27,16 @@ FUNC(entry_FRED_R3, 4096) END(entry_FRED_R3) FUNC(eretu_exit_to_guest) - POP_GPRS + /* + * PV guests aren't aware of FRED. If Xen in IDT mode would have used + * a SYSRET instruction, preserve the legacy behaviour for %rcx/%r11 + */ + testb $TRAP_syscall >> 8, UREGS_entry_vector + 1(%rsp) + + POP_GPRS /* Preserves flags */ + + cmovnz EFRAME_rip(%rsp), %rcx + cmovnz EFRAME_eflags(%rsp), %r11 /* * Exceptions here are handled by redirecting either to
Signature · 1 line
2.39.5
Re: [PATCH 2/2] x86/pv: Provide better SYSCALL backwards compatibility in FRED mode
Quoted 4 lines · Andrew Cooper
In FRED mode, the SYSCALL instruction does not modify %rcx/%r11. Software using SYSCALL spills %rcx/%r11 around the invocation, which is why FRED not doing this goes largely unnoticed.
Quoted 8 lines
However, consider the following migration scenario:
* VM suspends. Hypercall, so SYSCALL, %rcx/%r11 left unmodified
* VM moves to a non-FRED system
* Xen resumes the VM with a real SYSRET instructionInstead of resuming at the instruction following the SYSCALL instruction, the VM is resumed at whatever dead value was in %rcx.
Would it? In restore_all_guest we load %r11 and %rcx from the stack frame's EFLAGS and RIP fields. If we didn't, various other things wouldn't work either.
xen/arch/x86/traps.c void asmlinkage entry_from_pv(struct cpu_user_regs *regs) · line 2409
Quoted 8 lines
xen/arch/x86/traps.c +2
-0@@ -2405,6 +2405,8 @@ void asmlinkage entry_from_pv(struct cpu_user_regs *regs) regs->ssx = l ?FLAT_KERNEL_SS :FLAT_USER_SS32 ; regs->csx = l ?FLAT_KERNEL_CS64 :FLAT_USER_CS32 ; + regs->rcx = regs->rip; + regs->r11 = regs->rflags;
Don't you also need to set TRAP_syscall here, for the new code in eretu_exit_to_guest to actually make a difference? (There actually is a paragraph about this in the comment out of context above, which then may also want adjusting.)
Further a question as to limiting overhead: Doing this on every SYSCALL entry ...
Quoted 5 lines
xen/arch/x86/traps.c +0
-1@@ -26,7 +27,16 @@ FUNC(entry_FRED_R3, 4096)END (entry_FRED_R3)FUNC (eretu_exit_to_guest) - POP_GPRS
xen/arch/x86/traps.c FUNC(entry_FRED_R3, 4096) · line 39
Quoted 10 lines
xen/arch/x86/traps.c +10
-0+/* +* PV guests aren't aware of FRED. If Xen in IDT mode would have used +* a SYSRET instruction, preserve the legacy behaviour for %rcx/%r11 +*/ + testb $TRAP_syscall >> 8, UREGS_entry_vector + 1(%rsp) + +POP_GPRS /* Preserves flags */ + + cmovnz EFRAME_rip(%rsp), %rcx + cmovnz EFRAME_eflags(%rsp), %r11
... and every exit-to-guest isn't very nice when concern is about just the specific case of migrating FRED -> non-FRED. Couldn't we instead make the adjustment when generating the save record for the register state of the vCPU?
Jan
Re: [PATCH 2/2] x86/pv: Provide better SYSCALL backwards compatibility in FRED mode
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
In FRED mode, the SYSCALL instruction does not modify %rcx/%r11. Software using SYSCALL spills %rcx/%r11 around the invocation, which is why FRED not doing this goes largely unnoticed.
However, consider the following migration scenario:
* VM suspends. Hypercall, so SYSCALL, %rcx/%r11 left unmodified
* VM moves to a non-FRED system
* Xen resumes the VM with a real SYSRET instruction
Quoted 5 lines
Quoted 2 lines
Instead of resuming at the instruction following the SYSCALL instruction, the VM is resumed at whatever dead value was in %rcx.
Would it? In
restore_all_guestwe load %r11 and %rcx from the stack frame's EFLAGS and RIP fields. If we didn't, various other things wouldn't work either.
Hmm. I suppose so. regs->rip/eflags is always going to be reconstructed properly for the records in the transmitted stream.
What will be wrong is the %rcx/%r11 put onto the guest stack.
Quoted 11 lines
xen/arch/x86/traps.c void asmlinkage entry_from_pv(struct cpu_user_regs *regs) · line 2409
Quoted 9 lines
xen/arch/x86/traps.c +2
-0@@ -2405,6 +2405,8 @@ void asmlinkage entry_from_pv(struct cpu_user_regs *regs) regs->ssx = l ?FLAT_KERNEL_SS :FLAT_USER_SS32 ; regs->csx = l ?FLAT_KERNEL_CS64 :FLAT_USER_CS32 ; + regs->rcx = regs->rip; + regs->r11 = regs->rflags;Don't you also need to set
TRAP_syscallhere, for the new code ineretu_exit_to_guestto actually make a difference?
It is create_bounce_frame() which sets up TRAP_syscall.
Quoted 10 lines
(There actually is
a paragraph about this in the comment out of context above, which then may also want adjusting.)Further a question as to limiting overhead: Doing this on every SYSCALL entry ...
Quoted 3 lines
xen/arch/x86/traps.c +0
-0@@ -26,7 +27,16 @@ FUNC(entry_FRED_R3, 4096)END (entry_FRED_R3)
Quoted 16 lines
xen/arch/x86/traps.c FUNC(entry_FRED_R3, 4096) · line 39
Quoted 12 lines
xen/arch/x86/traps.c +10
-1FUNC (eretu_exit_to_guest) -POP_GPRS +/* +* PV guests aren't aware of FRED. If Xen in IDT mode would have used +* a SYSRET instruction, preserve the legacy behaviour for %rcx/%r11 +*/ + testb $TRAP_syscall >> 8, UREGS_entry_vector + 1(%rsp) + +POP_GPRS /* Preserves flags */ + + cmovnz EFRAME_rip(%rsp), %rcx + cmovnz EFRAME_eflags(%rsp), %r11... and every exit-to-guest isn't very nice when concern is about just the specific case of migrating FRED -> non-FRED. Couldn't we instead make the adjustment when generating the save record for the register state of the vCPU?
Ignoring migration for a moment, there are two further cases where things go wrong. Consider a VM which logically does this:
// user mode
SYSCALL
mov %rcx, dbg_syscall_was_here
// kernel modeentry_SYSCALL:
... setup stack
mov %rcx, UREGS_rip(%rsp)
Both of these positions under FRED have unexpected content in %rcx/%r11.
In userspace it is common to spill %rcx/%r11 and restore them around SYSCALL, but that's not an ABI. This is addressed by the hunk in entry_from_pv().
For kernel, the only reason CALLBACKTYPE_syscall functions in the slightest in staging right now is because Xen gives the guest an IRET frame and Linux doesn't need to reconstruct UREGS_rip/eflags manually.
In this case, it's baked into the PV64 ABI that "you will be entered by SYSRET, so you must pick up the interrupted %rcx/%r11 off the stack", and it strictly only applies to kernel code, and more than that, the Xen specific parts.
If this were the only problem case, we could make an argument to say that it would be a compatible change in the PV64 ABI, except we still get into problems when the guest kernel is using HYPERCALL_iret in SYSRET mode.
Linux is dealing with this problem by adjusting their unit test which spots it to skip this test when FRED is active. I'm not convinced this is the best move.
~Andrew
Re: [PATCH 2/2] x86/pv: Provide better SYSCALL backwards compatibility in FRED mode
Quoted 14 lines · Andrew Cooper
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Andrew Cooper
In FRED mode, the SYSCALL instruction does not modify %rcx/%r11. Software using SYSCALL spills %rcx/%r11 around the invocation, which is why FRED not doing this goes largely unnoticed.
However, consider the following migration scenario:
* VM suspends. Hypercall, so SYSCALL, %rcx/%r11 left unmodified
* VM moves to a non-FRED system
* Xen resumes the VM with a real SYSRET instructionInstead of resuming at the instruction following the SYSCALL instruction, the VM is resumed at whatever dead value was in %rcx.
Quoted 8 lines
Quoted 3 lines
Would it? In
restore_all_guestwe load %r11 and %rcx from the stack frame's EFLAGS and RIP fields. If we didn't, various other things wouldn't work either.Hmm. I suppose so. regs->rip/eflags is always going to be reconstructed properly for the records in the transmitted stream.
What will be wrong is the %rcx/%r11 put onto the guest stack.
Okay, this is addressed by ...
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/traps.c void asmlinkage entry_from_pv(struct cpu_user_regs *regs) · line 2409
Quoted 8 lines
xen/arch/x86/traps.c +2
-0@@ -2405,6 +2405,8 @@ void asmlinkage entry_from_pv(struct cpu_user_regs *regs) regs->ssx = l ?FLAT_KERNEL_SS :FLAT_USER_SS32 ; regs->csx = l ?FLAT_KERNEL_CS64 :FLAT_USER_CS32 ; + regs->rcx = regs->rip; + regs->r11 = regs->rflags;
... this change.
Quoted 4 lines
Quoted 2 lines
Don't you also need to set
TRAP_syscallhere, for the new code ineretu_exit_to_guestto actually make a difference?It is
create_bounce_frame()which sets upTRAP_syscall.
Hmm, right. I was misled by {l,c}star_enter and sysenter_entry setting the flag explicitly. That looks to be necessary only for the pv_hypercall() path out of lstar_enter; everything else goes through create_bounce_frame().
Quoted 40 lines
Quoted 10 lines
(There actually is
a paragraph about this in the comment out of context above, which then may also want adjusting.)Further a question as to limiting overhead: Doing this on every SYSCALL entry ...
Quoted 3 lines
xen/arch/x86/traps.c +0
-0@@ -26,7 +27,16 @@ FUNC(entry_FRED_R3, 4096)END (entry_FRED_R3)Quoted 16 lines
xen/arch/x86/traps.c FUNC(entry_FRED_R3, 4096) · line 39
Quoted 12 lines
xen/arch/x86/traps.c +10
-1FUNC (eretu_exit_to_guest) -POP_GPRS +/* +* PV guests aren't aware of FRED. If Xen in IDT mode would have used +* a SYSRET instruction, preserve the legacy behaviour for %rcx/%r11 +*/ + testb $TRAP_syscall >> 8, UREGS_entry_vector + 1(%rsp) + +POP_GPRS /* Preserves flags */ + + cmovnz EFRAME_rip(%rsp), %rcx + cmovnz EFRAME_eflags(%rsp), %r11... and every exit-to-guest isn't very nice when concern is about just the specific case of migrating FRED -> non-FRED. Couldn't we instead make the adjustment when generating the save record for the register state of the vCPU?
Ignoring migration for a moment, there are two further cases where things go wrong. Consider a VM which logically does this:
// user mode
SYSCALL
mov %rcx,dbg_syscall_was_here// kernel mode
entry_SYSCALL:
... setup stack
mov %rcx,UREGS_rip(%rsp)
Quoted 5 lines
Both of these positions under FRED have unexpected content in %rcx/%r11.
In userspace it is common to spill %rcx/%r11 and restore them around SYSCALL, but that's not an ABI. This is addressed by the hunk in
entry_from_pv().
Right, and the eretu_exit_to_guest change mirrors what we do ahead of SYSRET (or its conversion to IRET). While there are cases (for both paths) where RESTORE_ALL / POP_GPRS would already have restored the intended values, there are enough other cases where the value would have changed.
Overall, however, I think that the patch description would want altering, to cover more aspects (as discussed).
Jan
Re: [PATCH 2/2] x86/pv: Provide better SYSCALL backwards compatibility in FRED mode
Quoted 8 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper
Quoted 2 lines · Jan Beulich
Don't you also need to set
TRAP_syscallhere, for the new code ineretu_exit_to_guestto actually make a difference?It is
create_bounce_frame()which sets upTRAP_syscall.Hmm, right. I was misled by {l,c}star_enter and
sysenter_entrysetting the flag explicitly. That looks to be necessary only for thepv_hypercall()path out oflstar_enter; everything else goes throughcreate_bounce_frame().
Oh, that's a can of worms in and of itself.
The hypercall page preserves %rcx/%r11 around the SYSCALL instruction, but that's not captured in
https://xenbits.xen.org/docs/latest/guest-guide/x86/hypercall-abi.html
Given that we're now explicitly supporting the use of hypercalls without the hypercall page for CFI hardening reasons, I guess I need to fix that.
~Andrew