@@ -560,6 +560,13 @@ END(irq_entries_start)
560560/* 0(%rsp): ~(interrupt number) */
561561 .macro interrupt func
562562 cld
563+
564+ testb $3 , CS -ORIG_RAX(%rsp )
565+ jz 1f
566+ SWAPGS
567+ call switch_to_thread_stack
568+ 1:
569+
563570 ALLOC_PT_GPREGS_ON_STACK
564571 SAVE_C_REGS
565572 SAVE_EXTRA_REGS
@@ -569,12 +576,8 @@ END(irq_entries_start)
569576 jz 1f
570577
571578 /*
572- * IRQ from user mode. Switch to kernel gsbase and inform context
573- * tracking that we're in kernel mode.
574- */
575- SWAPGS
576-
577- /*
579+ * IRQ from user mode.
580+ *
578581 * We need to tell lockdep that IRQs are off. We can't do this until
579582 * we fix gsbase, and we should do it before enter_from_user_mode
580583 * (which can take locks). Since TRACE_IRQS_OFF idempotent,
@@ -828,6 +831,32 @@ apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
828831 */
829832#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1 ) * 8 )
830833
834+ /*
835+ * Switch to the thread stack. This is called with the IRET frame and
836+ * orig_ax on the stack. (That is, RDI..R12 are not on the stack and
837+ * space has not been allocated for them.)
838+ */
839+ ENTRY(switch_to_thread_stack)
840+ UNWIND_HINT_FUNC
841+
842+ pushq %rdi
843+ movq %rsp , %rdi
844+ movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
845+ UNWIND_HINT sp_offset =16 sp_reg = ORC_REG_DI
846+
847+ pushq 7*8 (%rdi ) /* regs->ss */
848+ pushq 6*8 (%rdi ) /* regs->rsp */
849+ pushq 5*8 (%rdi ) /* regs->eflags */
850+ pushq 4*8 (%rdi ) /* regs->cs */
851+ pushq 3*8 (%rdi ) /* regs->ip */
852+ pushq 2*8 (%rdi ) /* regs->orig_ax */
853+ pushq 8 (%rdi ) /* return address */
854+ UNWIND_HINT_FUNC
855+
856+ movq (%rdi ), %rdi
857+ ret
858+ END(switch_to_thread_stack)
859+
831860.macro idtentry sym do_sym has_error_code: req paranoid =0 shift_ist =-1
832861ENTRY(\sym)
833862 UNWIND_HINT_IRET_REGS offset = \has_error_code*8
@@ -845,11 +874,12 @@ ENTRY(\sym)
845874
846875 ALLOC_PT_GPREGS_ON_STACK
847876
848- .if \paranoid
849- .if \paranoid == 1
877+ .if \paranoid < 2
850878 testb $3 , CS (%rsp ) /* If coming from userspace, switch stacks */
851- jnz 1f
879+ jnz .Lfrom_usermode_switch_stack_\@
852880 .endif
881+
882+ .if \paranoid
853883 call paranoid_entry
854884 .else
855885 call error_entry
@@ -891,20 +921,15 @@ ENTRY(\sym)
891921 jmp error_exit
892922 .endif
893923
894- .if \paranoid == 1
924+ .if \paranoid < 2
895925 /*
896- * Paranoid entry from userspace. Switch stacks and treat it
926+ * Entry from userspace. Switch stacks and treat it
897927 * as a normal entry. This means that paranoid handlers
898928 * run in real process context if user_mode(regs).
899929 */
900- 1 :
930+ .Lfrom_usermode_switch_stack_\@ :
901931 call error_entry
902932
903-
904- movq %rsp , %rdi /* pt_regs pointer */
905- call sync_regs
906- movq %rax , %rsp /* switch stack */
907-
908933 movq %rsp , %rdi /* pt_regs pointer */
909934
910935 .if \has_error_code
@@ -1165,6 +1190,14 @@ ENTRY(error_entry)
11651190 SWAPGS
11661191
11671192.Lerror_entry_from_usermode_after_swapgs:
1193+ /* Put us onto the real thread stack. */
1194+ popq %r12 /* save return addr in %12 */
1195+ movq %rsp , %rdi /* arg0 = pt_regs pointer */
1196+ call sync_regs
1197+ movq %rax , %rsp /* switch stack */
1198+ ENCODE_FRAME_POINTER
1199+ pushq %r12
1200+
11681201 /*
11691202 * We need to tell lockdep that IRQs are off. We can't do this until
11701203 * we fix gsbase, and we should do it before enter_from_user_mode
0 commit comments