x86/traps: Get rid of exception handlers' second argument error code
Subsystems (4)
RFC PATCH 1/1 x86/traps: Get rid of exception handlers' second argument error code
The IDT event delivery of X86_TRAP_DF, X86_TRAP_TS, X86_TRAP_NP, X86_TRAP_SS, X86_TRAP_GP, X86_TRAP_AC and X86_TRAP_CP pushes an error code into the orig_ax member of the pt_regs structure, and the error code is passed as the second argument of their C-handlers, although the pt_regs structure is already passed as the first argument.
The asm entry code of such faults does the following
movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
to set the orig_ax member to -1 just before calling the C-handler.
In addition, the IRQ entry code uses the second error code argument as its IRQ vector, as the IRQ asm entry code pushes its IRQ vector into the orig_ax member.
The commit d99015b1abbad ("x86: move entry_64.S register saving out of the macros") introduced the changes to set orig_ax to -1, but I can't see why it's required. Our tests on x86_64 and x86_32 seem fine if orig_ax is left unchanged instead of set to -1.
It's probably cleaner and simpler to remove the second argument from such C-handlers while leave orig_ax unchanged to pass the error code inside the first argument. This patch allows FRED to use IDT exception and IRQ handlers with no or minimal changes.
The DECLARE_IDTENTRY_*_ERRORCODE macros still need to be kept to emit the ASM stubs, which do NOT need to push -1 as an error code placement. As a result, their corresponding DEFINE_IDTENTRY_*_ERRORCODE macros are kept, although they simply mapped to their no _ERRORCODE suffix versions.
I haven't checked Xen implications with this change, i.e., does Xen hypervisor need to adjust how it passes arguments to PV guests?
--- arch/x86/entry/common.c | 2 +- arch/x86/entry/entry_32.S | 7 -- arch/x86/entry/entry_64.S | 22 ++----- arch/x86/include/asm/idtentry.h | 108 ++++++++++--------------------- arch/x86/include/asm/irq_stack.h | 43 ++---------- arch/x86/kernel/apic/apic.c | 2 +- arch/x86/kernel/doublefault_32.c | 2 +- arch/x86/kernel/irq.c | 1 + arch/x86/kernel/sev.c | 10 +-- arch/x86/kernel/traps.c | 14 ++-- arch/x86/mm/fault.c | 6 +- arch/x86/xen/enlighten_pv.c | 2 +- 12 files changed, 69 insertions(+), 150 deletions(-)
arch/x86/entry/common.c +1 -1
@@ -305,7 +305,7 @@ __visible noinstr void xen_pv_evtchn_do_upcall(struct pt_regs *regs)bool inhcall;instrumentation_begin (); -run_sysvec_on_irqstack_cond (__xen_pv_evtchn_do_upcall, regs); +run_irq_on_irqstack_cond (__xen_pv_evtchn_do_upcall, regs); inhcall =get_and_clear_inhcall ();if (inhcall && !WARN_ON_ONCE (state.exit_rcu)) {
arch/x86/entry/entry_32.S +0 -7
@@ -642,8 +642,6 @@ SYM_CODE_START_LOCAL(asm_\cfunc) SAVE_ALL switch_stacks=1 ENCODE_FRAME_POINTER movl %esp, %eax - movl PT_ORIG_EAX(%esp), %edx /* get the vector from stack */ - movl $-1, PT_ORIG_EAX(%esp) /* no syscall to restart */ call \cfunc jmp handle_exception_return SYM_CODE_END(asm_\cfunc)
@@ -1036,11 +1034,6 @@ SYM_CODE_START_LOCAL_NOALIGN(handle_exception) ENCODE_FRAME_POINTER movl PT_GS(%esp), %edi # get the function address - - /* fixup orig %eax */ - movl PT_ORIG_EAX(%esp), %edx # get the error code - movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart - movl %esp, %eax # pt_regs pointer CALL_NOSPEC edi
arch/x86/entry/entry_64.S +4 -18
@@ -337,9 +337,8 @@ SYM_CODE_END(xen_error_entry) /** * idtentry_body - Macro to emit code calling the C function * @cfunc: C function to be called - * @has_error_code: Hardware pushed error code on stack */ -.macro idtentry_body cfunc has_error_code:req +.macro idtentry_body cfunc /* * Call error_entry() and switch to the task stack if from userspace.
@@ -356,12 +355,6 @@ SYM_CODE_END(xen_error_entry) UNWIND_HINT_REGS movq %rsp, %rdi /* pt_regs pointer into 1st argument*/ - - .if \has_error_code == 1 - movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/ - movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ - .endif - call \cfunc /* For some configurations \cfunc ends up being a noreturn. */
@@ -412,7 +405,7 @@ SYM_CODE_START(\asmsym) .Lfrom_usermode_no_gap_\@: .endif - idtentry_body \cfunc \has_error_code + idtentry_body \cfunc _ASM_NOKPROBE(\asmsym) SYM_CODE_END(\asmsym)
@@ -485,7 +478,7 @@ SYM_CODE_START(\asmsym) /* Switch to the regular task stack and use the noist entry point */ .Lfrom_usermode_switch_stack_\@: - idtentry_body noist_\cfunc, has_error_code=0 + idtentry_body noist_\cfunc _ASM_NOKPROBE(\asmsym) SYM_CODE_END(\asmsym)
@@ -548,12 +541,7 @@ SYM_CODE_START(\asmsym) ENCODE_FRAME_POINTER UNWIND_HINT_REGS - /* Update pt_regs */ - movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/ - movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ - movq %rsp, %rdi /* pt_regs pointer */ - call kernel_\cfunc /*
@@ -565,7 +553,7 @@ SYM_CODE_START(\asmsym) /* Switch to the regular task stack */ .Lfrom_usermode_switch_stack_\@: - idtentry_body user_\cfunc, has_error_code=1 + idtentry_body user_\cfunc _ASM_NOKPROBE(\asmsym) SYM_CODE_END(\asmsym)
@@ -589,8 +577,6 @@ SYM_CODE_START(\asmsym) UNWIND_HINT_REGS movq %rsp, %rdi /* pt_regs pointer into first argument */ - movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/ - movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ call \cfunc /* For some configurations \cfunc ends up being a noreturn. */
arch/x86/include/asm/idtentry.h +34 -74
249 lines not on this page · raw message
arch/x86/include/asm/irq_stack.h +7 -36
77 lines not on this page · raw message
arch/x86/kernel/apic/apic.c +1 -1
@@ -2230,7 +2230,7 @@ static noinline void handle_spurious_interrupt(u8 vector)*/ DEFINE_IDTENTRY_IRQ (spurious_interrupt) { -handle_spurious_interrupt (vector); +handle_spurious_interrupt ((u32 )(u8 )regs->orig_ax); }DEFINE_IDTENTRY_SYSVEC (sysvec_spurious_apic_interrupt)
arch/x86/kernel/doublefault_32.c +1 -1
@@ -70,7 +70,7 @@ asmlinkage noinstr void __noreturn doublefault_shim(void) regs.cx =TSS (cx); regs.bx =TSS (bx); -exc_double_fault (®s,0 , cr2); +exc_double_fault (®s, cr2);/* * x86_32 does not save the original CR3 anywhere on a task switch.
arch/x86/kernel/irq.c +1 -0
@@ -246,6 +246,7 @@ static __always_inline void handle_irq(struct irq_desc *desc,*/ DEFINE_IDTENTRY_IRQ (common_interrupt) { +u32 vector = (u32 )(u8 )regs->orig_ax;struct pt_regs *old_regs =set_irq_regs (regs);struct irq_desc *desc;
arch/x86/kernel/sev.c +6 -4
@@ -1773,24 +1773,23 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt, static __always_inline void vc_forward_exception(struct es_em_ctxt *ctxt) { -long error_code = ctxt->fi.error_code;int trapnr = ctxt->fi.vector; ctxt->regs->orig_ax = ctxt->fi.error_code;switch (trapnr) {case X86_TRAP_GP : -exc_general_protection (ctxt->regs, error_code); +exc_general_protection (ctxt->regs);break ;case X86_TRAP_UD :exc_invalid_op (ctxt->regs);break ;case X86_TRAP_PF :write_cr2 (ctxt->fi.cr2); -exc_page_fault (ctxt->regs, error_code); +exc_page_fault (ctxt->regs);break ;case X86_TRAP_AC : -exc_alignment_check (ctxt->regs, error_code); +exc_alignment_check (ctxt->regs);break ;default :pr_emerg ("Unsupported exception in #VC instruction emulation - can't continue\n" );
@@ -1885,6 +1884,7 @@ static __always_inline bool vc_is_db(unsigned long error_code)*/ DEFINE_IDTENTRY_VC_KERNEL (exc_vmm_communication) { +unsigned long error_code = regs->orig_ax;irqentry_state_t irq_state;/*
@@ -1937,6 +1937,8 @@ DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)*/ DEFINE_IDTENTRY_VC_USER (exc_vmm_communication) { +unsigned long error_code = regs->orig_ax; +/* * Handle #DB before calling into !noinstr code to avoid recursive #DB. */
arch/x86/kernel/traps.c +10 -4
@@ -233,6 +233,8 @@ enum cp_error_code {DEFINE_IDTENTRY_ERRORCODE (exc_control_protection) { +unsigned long error_code = regs->orig_ax; +if (!cpu_feature_enabled (X86_FEATURE_IBT )) {pr_err ("Unexpected #CP\n" );BUG ();
@@ -360,24 +362,25 @@ DEFINE_IDTENTRY(exc_coproc_segment_overrun)DEFINE_IDTENTRY_ERRORCODE (exc_invalid_tss) { -do_error_trap (regs, error_code,"invalid TSS" ,X86_TRAP_TS ,SIGSEGV , +do_error_trap (regs, regs->orig_ax,"invalid TSS" ,X86_TRAP_TS ,SIGSEGV ,0 ,NULL ); }DEFINE_IDTENTRY_ERRORCODE (exc_segment_not_present) { -do_error_trap (regs, error_code,"segment not present" ,X86_TRAP_NP , +do_error_trap (regs, regs->orig_ax,"segment not present" ,X86_TRAP_NP ,SIGBUS ,0 ,NULL ); }DEFINE_IDTENTRY_ERRORCODE (exc_stack_segment) { -do_error_trap (regs, error_code,"stack segment" ,X86_TRAP_SS ,SIGBUS , +do_error_trap (regs, regs->orig_ax,"stack segment" ,X86_TRAP_SS ,SIGBUS ,0 ,NULL ); }DEFINE_IDTENTRY_ERRORCODE (exc_alignment_check) { +unsigned long error_code = regs->orig_ax;char *str ="alignment check" ;if (notify_die (DIE_TRAP , str, regs, error_code,X86_TRAP_AC ,SIGBUS ) ==NOTIFY_STOP )
@@ -436,6 +439,7 @@ __visible void __noreturn handle_stack_overflow(struct pt_regs *regs,*/ DEFINE_IDTENTRY_DF (exc_double_fault) { +unsigned long error_code = regs->orig_ax;static const char str[] ="double fault" ;struct task_struct *tsk = current;
@@ -728,6 +732,7 @@ static void gp_user_force_sig_segv(struct pt_regs *regs, int trapnr,DEFINE_IDTENTRY_ERRORCODE (exc_general_protection) { +unsigned long error_code = regs->orig_ax;char desc[sizeof (GPFSTR ) +50 +2 *sizeof (unsigned long ) +1 ] =GPFSTR ;enum kernel_gp_hint hint =GP_NO_HINT ;unsigned long gp_addr;
@@ -1254,7 +1259,8 @@ DEFINE_IDTENTRY(exc_simd_coprocessor_error)if (IS_ENABLED (CONFIG_X86_INVD_BUG )) {/* AMD 486 bug: INVD in CPL 0 raises #XF instead of #GP */ if (!static_cpu_has (X86_FEATURE_XMM )) { -__exc_general_protection (regs,0 ); + regs->orig_ax =0 ; +__exc_general_protection (regs);return ; } }
arch/x86/mm/fault.c +3 -3
@@ -1471,9 +1471,9 @@ trace_page_fault_entries(struct pt_regs *regs, unsigned long error_code, } static __always_inline void -handle_page_fault (struct pt_regs *regs,unsigned long error_code, -unsigned long address) +handle_page_fault (struct pt_regs *regs,unsigned long address) { +unsigned long error_code = regs->orig_ax;trace_page_fault_entries (regs, error_code, address);if (unlikely (kmmio_fault (regs, address)))
@@ -1539,7 +1539,7 @@ DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault) state =irqentry_enter (regs);instrumentation_begin (); -handle_page_fault (regs, error_code, address); +handle_page_fault (regs, address);instrumentation_end ();irqentry_exit (regs, state);
arch/x86/xen/enlighten_pv.c +1 -1
@@ -614,7 +614,7 @@ DEFINE_IDTENTRY_RAW(xenpv_exc_nmi)DEFINE_IDTENTRY_RAW_ERRORCODE (xenpv_exc_double_fault) {/* On Xen PV, DF doesn't use IST. The C part is the same as native. */ -exc_double_fault (regs, error_code); +exc_double_fault (regs); }DEFINE_IDTENTRY_RAW (xenpv_exc_debug)
Signature · 1 line
2.34.1
Peter Zijlstra
maintainerQuoted 13 lines · Xin Li
The IDT event delivery of
X86_TRAP_DF,X86_TRAP_TS,X86_TRAP_NP,X86_TRAP_SS,X86_TRAP_GP,X86_TRAP_ACandX86_TRAP_CPpushes an error code into theorig_axmember of thept_regsstructure, and the error code is passed as the second argument of their C-handlers, although thept_regsstructure is already passed as the first argument.The asm entry code of such faults does the following
movq
ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
movq $-1,ORIG_RAX(%rsp)/* no syscall to restart */to set the
orig_axmember to -1 just before calling the C-handler.
Quoted 8 lines
In addition, the IRQ entry code uses the second error code argument as its IRQ vector, as the IRQ asm entry code pushes its IRQ vector into the
orig_axmember.The commit
d99015b1abbad("x86: moveentry_64.Sregister saving out of the macros") introduced the changes to setorig_axto -1, but I can't see why it's required. Our tests onx86_64andx86_32seem fine iforig_axis left unchanged instead of set to -1.
That means that SYSCALL_NUM(regs) get to be garbage; or something like that.
Quoted 17 lines
Quoted 17 lines
The IDT event delivery of
X86_TRAP_DF,X86_TRAP_TS,X86_TRAP_NP,X86_TRAP_SS,X86_TRAP_GP,X86_TRAP_ACandX86_TRAP_CPpushes an error code into theorig_axmember of thept_regsstructure, and the error code is passed as the second argument of their C-handlers, although thept_regsstructure is already passed as the first argument.The asm entry code of such faults does the following
movq
ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
movq $-1,ORIG_RAX(%rsp)/* no syscall to restart */to set the
orig_axmember to -1 just before calling the C-handler.In addition, the IRQ entry code uses the second error code argument as its IRQ vector, as the IRQ asm entry code pushes its IRQ vector into the
orig_axmember.
Quoted 6 lines
Quoted 4 lines
The commit
d99015b1abbad("x86: moveentry_64.Sregister saving out of the macros") introduced the changes to setorig_axto -1, but I can't see why it's required. Our tests onx86_64andx86_32seem fine iforig_axis left unchanged instead of set to -1.That means that
SYSCALL_NUM(regs)get to be garbage; or something like that.
I find SYSCALL_NUM(regs) in tools/testing/selftests/seccomp/seccomp_bpf.c, but nothing obvious to me.
I think it's clear that once exceptions and IRQs are handled, the original context will be fully recovered in a normal case.
Is it related to preemption after such a event?
I must have missed something; can you please elaborate it?
Peter Zijlstra
maintainerQuoted 27 lines · On Fri, Aug 04, 2023 at 05:35:11PM +0000, Li, Xin3 wrote:
Quoted 17 lines
Quoted 17 lines
The IDT event delivery of
X86_TRAP_DF,X86_TRAP_TS,X86_TRAP_NP,X86_TRAP_SS,X86_TRAP_GP,X86_TRAP_ACandX86_TRAP_CPpushes an error code into theorig_axmember of thept_regsstructure, and the error code is passed as the second argument of their C-handlers, although thept_regsstructure is already passed as the first argument.The asm entry code of such faults does the following
movq
ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
movq $-1,ORIG_RAX(%rsp)/* no syscall to restart */to set the
orig_axmember to -1 just before calling the C-handler.In addition, the IRQ entry code uses the second error code argument as its IRQ vector, as the IRQ asm entry code pushes its IRQ vector into the
orig_axmember.Quoted 6 lines
Quoted 4 lines
The commit
d99015b1abbad("x86: moveentry_64.Sregister saving out of the macros") introduced the changes to setorig_axto -1, but I can't see why it's required. Our tests onx86_64andx86_32seem fine iforig_axis left unchanged instead of set to -1.That means that
SYSCALL_NUM(regs)get to be garbage; or something like that.I find
SYSCALL_NUM(regs)intools/testing/selftests/seccomp/seccomp_bpf.c, but nothing obvious to me.
Quoted 6 lines
I think it's clear that once exceptions and IRQs are handled, the original context will be fully recovered in a normal case.
Is it related to preemption after such a event?
I must have missed something; can you please elaborate it?
arch/x86/include/asm/syscall.h
syscall_get_nr() syscall_rollback()
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · On Fri, Aug 04, 2023 at 05:35:11PM +0000, Li, Xin3 wrote:
Quoted 6 lines
Quoted 4 lines
The commit
d99015b1abbad("x86: moveentry_64.Sregister saving out of the macros") introduced the changes to setorig_axto -1, but I can't see why it's required. Our tests onx86_64andx86_32seem fine iforig_axis left unchanged instead of set to -1.That means that
SYSCALL_NUM(regs)get to be garbage; or something like that.I find
SYSCALL_NUM(regs)intools/testing/selftests/seccomp/seccomp_bpf.c, but nothing obvious to me.I think it's clear that once exceptions and IRQs are handled, the original context will be fully recovered in a normal case.
Quoted 7 lines
Quoted 3 lines
Is it related to preemption after such a event?
I must have missed something; can you please elaborate it?
arch/x86/include/asm/syscall.h
syscall_get_nr()syscall_rollback()
Specifically it breaks signal handling. See arch_do_signal_or_restart() and handle_signal().
So, no. This cannot be changed nilly willy especially not because orig_ax is part of the UABI.
Even if it would work, this patch is completely unreviewable. There are smarter ways to make such a change in digestable chunks.
I also looked at the latest FRED series and that's broken vs. orig_ax in the same way.
Aside of it the series is not applying to any tree I'm aware of. It fails because its based on a tree which has IRQ_MOVE_CLEANUP_VECTOR removed, but that's nowhere in mainline or tip. This is not the way it works, really.
Back to the problem at hand. I'm absolutely not understanding why this all is so complicated.
FRED pushs the error code or 0 (in case there is no error code) right into orig_ax. That's where the ERET[US] frame starts. ERT[US] do not care at all about the error code location, they skip it by adding 8 to RSP.
So the obvious thing to do is:
fred_entry_from_xxxx(struct pt_regs *regs)
{
unsigned long error_code = regs->orig_ax;
regs->orig_ax = -1;
dispatch_1st_level(regs->type, regs, error_code);
}Now the dispatching code in your series looks nice and shiny with all those tables, but in practice it's not so shiny at all.
For one this code forces indirect calls even in places where there are no indirect calls required.
But what's worse it causes you to make everything uniform one way or the other and adding all this dispatch_table_##func muck, which is fully duplicated code for absolutely zero reason. That's error prone and a maintainence mess.
The vast majority of exceptions, interrupts whatever is not special at all neither on FRED nor on IDT. The ones which need special treatment are those which store additional information in the stack frame and these are the exception - not the rule. And those require special treatment anyway whether you make the rest look uniform or not.
IOW, your approach of making everything uniform is completely wrong. There is a reason why some system vectors are not using DEFINE_IDTENTRY_SYSVEC(). Particularly the RESCHEDULE_VECTOR. This has been performance optimized with a lot of effort and no, we are not going to sacrifice that just because it makes it sooo simple for FRED.
This is not about simplicity. This is about sane and maintainable code which preserves the carefully optimized code paths which are hotpath no matter whether there is IDT or FRED.
I'm going to reply on your last failed resend attempt after creating a sane mail thread out of the mess you created (including the insanely large and inappropriate cc list).
Thanks,
tglx
Quoted 2 lines · Xin Li
I haven't checked Xen implications with this change, i.e., does Xen hypervisor need to adjust how it passes arguments to PV guests?
This is an internal detail of how Linux handles data on it's stacks, isn't it?
The Xen code in Linux will need adjustment to match, but this is not a hypervisor ABI (which obviously can't and won't change).
~Andrew
Juergen Gross
maintainerQuoted 9 lines · Andrew Cooper
Quoted 2 lines · Xin Li
I haven't checked Xen implications with this change, i.e., does Xen hypervisor need to adjust how it passes arguments to PV guests?
This is an internal detail of how Linux handles data on it's stacks, isn't it?
The Xen code in Linux will need adjustment to match, but this is not a hypervisor ABI (which obviously can't and won't change).
The only adaption of Xen code will need to happen for the only use of DEFINE_IDTENTRY_RAW_ERRORCODE() there, and this you have covered in your patch already.
Juergen
Quoted 7 lines
Quoted 7 lines · Xin Li
Quoted 2 lines
I haven't checked Xen implications with this change, i.e., does Xen hypervisor need to adjust how it passes arguments to PV guests?
This is an internal detail of how Linux handles data on it's stacks, isn't it?
Quoted 6 lines
Quoted 2 lines
The Xen code in Linux will need adjustment to match, but this is not a hypervisor ABI (which obviously can't and won't change).
The only adaption of Xen code will need to happen for the only use of
DEFINE_IDTENTRY_RAW_ERRORCODE()there, and this you have covered in your patch already.
Thanks for checking it for me.
Quoted 5 lines · Xin Li
Quoted 2 lines
I haven't checked Xen implications with this change, i.e., does Xen hypervisor need to adjust how it passes arguments to PV guests?
This is an internal detail of how Linux handles data on it's stacks, isn't it?
Yes, it is completely internal to Linux.
Quoted 2 lines
The Xen code in Linux will need adjustment to match, but this is not a hypervisor ABI (which obviously can't and won't change).
You're right! Thanks for confirming!