x86/entry: Post-XSA-453 cleanup, part 1
cover x86/entry: Post-XSA-453 cleanup, part 1
The start of making Xen's entry code conform better to the recommendations in the vendor Optimisation Guides.
Part 1 (this series) swaps RESTORE_ALL for POP_GPRs. Part 2 is the SAVE_ALL side of things which is more complicated because of flags handling.
Andrew Cooper (2):
x86/entry: Introduce POP_GPRS
x86/entry: Delete RESTORE_ALL
xen/arch/x86/hvm/svm/entry.S | 17 +------- xen/arch/x86/hvm/vmx/entry.S | 16 +------- xen/arch/x86/include/asm/asm_defns.h | 61 ++++++++-------------------- xen/arch/x86/x86_64/asm-offsets.c | 1 + xen/arch/x86/x86_64/compat/entry.S | 18 +++++++- xen/arch/x86/x86_64/entry.S | 12 +++--- 6 files changed, 45 insertions(+), 80 deletions(-)
base-commit: 03cf7ca23e0e876075954c558485b267b7d02406
Signature · 1 line
2.30.2
PATCH 1/2 x86/entry: Introduce POP_GPRS
The macro named RESTORE_ALL has several problems. It adjusts the stack pointer despite this not being clear to the caller. It also goes against recommendations in the optimisation guides because of trying to do too many things at once. (i.e. there's a reason why compilers don't emit code looking like this.)
Introduce a new POP_GPRS macro which only POPs GPRs. Use it for the HVM paths which are already using POPs.
Also use it for restore_all_{xen,guest}(). This saves most of a cacheline worth of code from two fastpaths:
add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-99 (-99)
Function old new delta restore_all_guest 378 330 -48 restore_all_xen 165 114 -51
but it also avoids having an explicit modification to the stack pointer between %rsp-relative accesses, which avoids stalls in the stack-engine optimisations in some microarchitectures.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
--- xen/arch/x86/hvm/svm/entry.S | 17 ++--------------- xen/arch/x86/hvm/vmx/entry.S | 16 +--------------- xen/arch/x86/include/asm/asm_defns.h | 23 +++++++++++++++++++++++ xen/arch/x86/x86_64/asm-offsets.c | 1 + xen/arch/x86/x86_64/entry.S | 12 ++++++------ 5 files changed, 33 insertions(+), 36 deletions(-)
xen/arch/x86/hvm/svm/entry.S +2 -15
@@ -74,22 +74,9 @@ __UNLIKELY_END(nsvm_hap) ALTERNATIVE "", svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM ALTERNATIVE "", DO_SPEC_CTRL_DIV, X86_FEATURE_SC_DIV - pop %r15 - pop %r14 - pop %r13 - pop %r12 - pop %rbp mov VCPU_svm_vmcb_pa(%rbx),%rax - pop %rbx - pop %r11 - pop %r10 - pop %r9 - pop %r8 - pop %rcx /* Skip %rax: restored by VMRUN. */ - pop %rcx - pop %rdx - pop %rsi - pop %rdi + + POP_GPRS rax=%rcx /* Skip %rax. It's restored by VMRUN. */ sti vmrun
xen/arch/x86/hvm/vmx/entry.S +1 -15
@@ -115,21 +115,7 @@ UNLIKELY_END(realmode) and $SCF_verw, %eax or %eax, %ecx - pop %r15 - pop %r14 - pop %r13 - pop %r12 - pop %rbp - pop %rbx - pop %r11 - pop %r10 - pop %r9 - pop %r8 - pop %rax - pop %rcx - pop %rdx - pop %rsi - pop %rdi + POP_GPRS jpe .L_skip_verw /* VERW clobbers ZF, but preserves all others, including SF. */
xen/arch/x86/include/asm/asm_defns.h +23 -0
@@ -314,6 +314,29 @@ static always_inline void stac(void) subq $-(UREGS_error_code-UREGS_r15+\adj), %rsp .endm +/* +* POP GPRs from a UREGS_* frame on the stack. Does not modify flags. +* +* @rax: Alternative destination for the %rax value on the stack. +*/ +.macro POP_GPRS rax=%rax + pop %r15 + pop %r14 + pop %r13 + pop %r12 + pop %rbp + pop %rbx + pop %r11 + pop %r10 + pop %r9 + pop %r8 + pop \rax + pop %rcx + pop %rdx + pop %rsi + pop %rdi +.endm +#ifdef CONFIG_PV32 #define CR4_PV32_RESTORE \ ALTERNATIVE_2 "", \
xen/arch/x86/x86_64/asm-offsets.c +1 -0
@@ -59,6 +59,7 @@ void __dummy__(void)DEFINE (sym, offsetof(struct cpu_user_regs , mem) - \ offsetof(struct cpu_user_regs, error_code) __VA_ARGS__) +OFFSET_EF (EFRAME_error_code, error_code);OFFSET_EF (EFRAME_entry_vector, entry_vector);OFFSET_EF (EFRAME_rip, rip);OFFSET_EF (EFRAME_cs, cs);
xen/arch/x86/x86_64/entry.S +6 -6
@@ -186,7 +186,8 @@ FUNC_LOCAL(restore_all_guest) /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */ SPEC_CTRL_EXIT_TO_PV /* Req: a=spec_ctrl %rsp=regs/cpuinfo, Clob: cd */ - RESTORE_ALL + POP_GPRS + BUILD_BUG_ON(TRAP_syscall & 0xff) testb $TRAP_syscall >> 8, EFRAME_entry_vector + 1(%rsp) jz iret_exit_to_guest
@@ -696,20 +697,19 @@ UNLIKELY_END(exit_cr3) /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */ SPEC_CTRL_EXIT_TO_XEN /* Req: %r12=ist_exit %r14=end %rsp=regs, Clob: abcd */ - RESTORE_ALL adj=8 + POP_GPRS /* * When the CPU pushed this exception frame, it zero-extended eflags. * For an IST exit, SPEC_CTRL_EXIT_TO_XEN stashed shadow copies of * spec_ctrl_flags and ver_sel above eflags, as we can't use any GPRs, * and we're at a random place on the stack, not in a CPUFINFO block. - * - * Account for ev/ec having already been popped off the stack. */ SPEC_CTRL_COND_VERW \ - scf=STK_REL(EFRAME_shadow_scf, EFRAME_rip), \ - sel=STK_REL(EFRAME_shadow_sel, EFRAME_rip) + scf=STK_REL(EFRAME_shadow_scf, EFRAME_error_code), \ + sel=STK_REL(EFRAME_shadow_sel, EFRAME_error_code) + add $8, %rsp iretq END(restore_all_xen)
Signature · 1 line
2.30.2
Re: [PATCH 1/2] x86/entry: Introduce POP_GPRS
Quoted 5 lines · Andrew Cooper
The macro named
RESTORE_ALLhas several problems. It adjusts the stack pointer despite this not being clear to the caller. It also goes against recommendations in the optimisation guides because of trying to do too many things at once. (i.e. there's a reason why compilers don't emit code looking like this.)
Not anymore; I'm sure they used to over a certain period of time, which is why 4d246723a85a ("x86: use MOV instead of PUSH/POP when saving/restoring register state") was created in the first place (and which you now say was a mistake, or at least has become a mistake in the over 10 years since then).
Quoted 6 lines
Introduce a new
POP_GPRSmacro which only POPs GPRs. Use it for the HVM paths which are already using POPs.Also use it for restore_all_{xen,guest}(). This saves most of a cacheline worth of code from two fastpaths:
Quoted 8 lines
add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-99 (-99)
Function old new delta restore_all_guest 378 330 -48 restore_all_xen 165 114 -51but it also avoids having an explicit modification to the stack pointer between %rsp-relative accesses, which avoids stalls in the stack-engine optimisations in some microarchitectures.
Is there such a rule? All I was able to find (and even that only with quite a bit of effort, because the section it's in wouldn't have had me think of a stack pointer rule being there) is
"Assembly/Compiler Coding Rule 22. (M impact, M generality) Avoid
putting explicit references to ESP in a sequence of stack operations (POP, PUSH, CALL, RET)."
I actually wonder whether %rsp really is special in the way you indicate - other registers, when used for memory accesses and being updated ought to have a similar stall issue?
Quoted 14 lines
xen/arch/x86/hvm/svm/entry.S +0
-7@@ -74,22 +74,9 @@ __UNLIKELY_END(nsvm_hap) ALTERNATIVE "", svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM ALTERNATIVE "", DO_SPEC_CTRL_DIV, X86_FEATURE_SC_DIV - pop %r15 - pop %r14 - pop %r13 - pop %r12 - pop %rbp mov VCPU_svm_vmcb_pa(%rbx),%rax - pop %rbx - pop %r11
xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 79
Quoted 10 lines
xen/arch/x86/hvm/svm/entry.S +2
-8- pop %r10 - pop %r9 - pop %r8 - pop %rcx /* Skip %rax: restored by VMRUN. */ - pop %rcx - pop %rdx - pop %rsi - pop %rdi + + POP_GPRS rax=%rcx /* Skip %rax. It's restored by VMRUN. */
In light of you having asked my to try and decouple ABI and internal stack frame layout, I'm wary of encoding a dependency on the ordering of registers in the frame at a use site like this one. Imo the argument ought to merely indicate "skip %rax", with the macro taking care of how that skipping is actually carried out.
Quoted 13 lines
xen/arch/x86/hvm/svm/entry.S +1
-2@@ -696,20 +697,19 @@ UNLIKELY_END(exit_cr3) /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */ SPEC_CTRL_EXIT_TO_XEN /* Req: %r12=ist_exit %r14=end %rsp=regs, Clob: abcd */ - RESTORE_ALL adj=8 + POP_GPRS /* * When the CPU pushed this exception frame, it zero-extended eflags. * For an IST exit, SPEC_CTRL_EXIT_TO_XEN stashed shadow copies of * spec_ctrl_flags and ver_sel above eflags, as we can't use any GPRs, * and we're at a random place on the stack, not in a CPUFINFO block. - *
xen/arch/x86/hvm/svm/entry.S UNLIKELY_END(exit_cr3) · line 713
Quoted 10 lines
xen/arch/x86/hvm/svm/entry.S +3
-3- * Account for ev/ec having already been popped off the stack. */ SPEC_CTRL_COND_VERW \ - scf=STK_REL(EFRAME_shadow_scf, EFRAME_rip), \ - sel=STK_REL(EFRAME_shadow_sel, EFRAME_rip) + scf=STK_REL(EFRAME_shadow_scf, EFRAME_error_code), \ + sel=STK_REL(EFRAME_shadow_sel, EFRAME_error_code) + add $8, %rsp iretq
How is this ADD different from the RESTORE_ALL one, in particular in light of the ORM rule quoted above (which surely extends to IRET as well)? It ought to be possible to avoid, by having POP_GPRS (optionally) move the %r15 value into the error code slot first thing (i.e. before %rsp starts being updated), and then having "pop %r15" last.
Jan
PATCH 2/2 x86/entry: Delete RESTORE_ALL
In reply to Andrew CooperThere is only a single user now, and it's the one odd case. Inline and simplify it to just the compat case.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
In principle we want to delay the %rsp adjustment until after VERW, but that turns disp8's into disp32's, making the overall code size larger. This path is only for 32bit PV guests anyway, which are well on their way to fully obsolete.
--- xen/arch/x86/include/asm/asm_defns.h | 48 ---------------------------- xen/arch/x86/x86_64/compat/entry.S | 18 ++++++++++- 2 files changed, 17 insertions(+), 49 deletions(-)
xen/arch/x86/include/asm/asm_defns.h +0 -48
@@ -266,54 +266,6 @@ static always_inline void stac(void) xor %r15d, %r15d .endm -#define LOAD_ONE_REG (reg, compat) \ -.if !(compat); \ - movq UREGS_r##reg(%rsp),%r##reg; \ -.else; \ - movl UREGS_r##reg(%rsp),%e##reg; \ -.endif - -/* -* Restore all previously saved registers. -* -* @adj: extra stack pointer adjustment to be folded into the adjustment done -* anyway at the end of the macro -* @compat: R8-R15 don't need reloading, but they are clobbered for added -* safety against information leaks. -*/ -.macro RESTORE_ALL adj=0, compat=0 -.if !\compat - movq UREGS_r15(%rsp), %r15 - movq UREGS_r14(%rsp), %r14 - movq UREGS_r13(%rsp), %r13 - movq UREGS_r12(%rsp), %r12 -.else - xor %r15d, %r15d - xor %r14d, %r14d - xor %r13d, %r13d - xor %r12d, %r12d -.endif - LOAD_ONE_REG(bp, \compat) - LOAD_ONE_REG(bx, \compat) -.if !\compat - movq UREGS_r11(%rsp),%r11 - movq UREGS_r10(%rsp),%r10 - movq UREGS_r9(%rsp),%r9 - movq UREGS_r8(%rsp),%r8 -.else - xor %r11d, %r11d - xor %r10d, %r10d - xor %r9d, %r9d - xor %r8d, %r8d -.endif - LOAD_ONE_REG(ax, \compat) - LOAD_ONE_REG(cx, \compat) - LOAD_ONE_REG(dx, \compat) - LOAD_ONE_REG(si, \compat) - LOAD_ONE_REG(di, \compat) - subq $-(UREGS_error_code-UREGS_r15+\adj), %rsp -.endm -/* * POP GPRs from a UREGS_* frame on the stack. Does not modify flags. *
xen/arch/x86/x86_64/compat/entry.S +17 -1
@@ -158,7 +158,23 @@ FUNC(compat_restore_all_guest) /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */ SPEC_CTRL_EXIT_TO_PV /* Req: a=spec_ctrl %rsp=regs/cpuinfo, Clob: cd */ - RESTORE_ALL adj=8, compat=1 + /* Opencoded POP_GPRS, restoring only the 32bit registers. */ + xor %r15d, %r15d + xor %r14d, %r14d + xor %r13d, %r13d + xor %r12d, %r12d + mov UREGS_rbp(%rsp), %ebp + mov UREGS_rbx(%rsp), %ebx + xor %r11d, %r11d + xor %r10d, %r10d + xor %r9d, %r9d + xor %r8d, %r8d + mov UREGS_rax(%rsp), %eax + mov UREGS_rcx(%rsp), %ecx + mov UREGS_rdx(%rsp), %edx + mov UREGS_rsi(%rsp), %esi + mov UREGS_rdi(%rsp), %edi + sub $-(UREGS_rip-UREGS_r15), %rsp /* Account for ev/ec having already been popped off the stack. */ SPEC_CTRL_COND_VERW \
Signature · 1 line
2.30.2
Re: [PATCH 2/2] x86/entry: Delete RESTORE_ALL
Quoted 6 lines · Andrew Cooper
There is only a single user now, and it's the one odd case. Inline and simplify it to just the compat case.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
Quoted 2 lines
In principle we want to delay the %rsp adjustment until after VERW, but that turns disp8's into disp32's, making the overall code size larger.
While beneficial for SPEC_CTRL_COND_VERW, that might then harm the IRETQ. So aiui neither placement is ideal, and hence the Disp<N> consideration is the determining factor either way.
Jan