1905a36a2SIngo Molnar/* 2905a36a2SIngo Molnar * linux/arch/x86_64/entry.S 3905a36a2SIngo Molnar * 4905a36a2SIngo Molnar * Copyright (C) 1991, 1992 Linus Torvalds 5905a36a2SIngo Molnar * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs 6905a36a2SIngo Molnar * Copyright (C) 2000 Pavel Machek <pavel@suse.cz> 7905a36a2SIngo Molnar */ 8905a36a2SIngo Molnar 9905a36a2SIngo Molnar/* 10905a36a2SIngo Molnar * entry.S contains the system-call and fault low-level handling routines. 11905a36a2SIngo Molnar * 12905a36a2SIngo Molnar * Some of this is documented in Documentation/x86/entry_64.txt 13905a36a2SIngo Molnar * 14905a36a2SIngo Molnar * NOTE: This code handles signal-recognition, which happens every time 15905a36a2SIngo Molnar * after an interrupt and after each system call. 16905a36a2SIngo Molnar * 17905a36a2SIngo Molnar * A note on terminology: 18905a36a2SIngo Molnar * - iret frame: Architecture defined interrupt frame from SS to RIP 19905a36a2SIngo Molnar * at the top of the kernel process stack. 20905a36a2SIngo Molnar * 21905a36a2SIngo Molnar * Some macro usage: 22905a36a2SIngo Molnar * - ENTRY/END Define functions in the symbol table. 23905a36a2SIngo Molnar * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging. 24905a36a2SIngo Molnar * - idtentry - Define exception entry points. 25905a36a2SIngo Molnar */ 26905a36a2SIngo Molnar 27905a36a2SIngo Molnar#include <linux/linkage.h> 28905a36a2SIngo Molnar#include <asm/segment.h> 29905a36a2SIngo Molnar#include <asm/cache.h> 30905a36a2SIngo Molnar#include <asm/errno.h> 31d36f9479SIngo Molnar#include "calling.h" 32905a36a2SIngo Molnar#include <asm/asm-offsets.h> 33905a36a2SIngo Molnar#include <asm/msr.h> 34905a36a2SIngo Molnar#include <asm/unistd.h> 35905a36a2SIngo Molnar#include <asm/thread_info.h> 36905a36a2SIngo Molnar#include <asm/hw_irq.h> 37905a36a2SIngo Molnar#include <asm/page_types.h> 38905a36a2SIngo Molnar#include <asm/irqflags.h> 39905a36a2SIngo Molnar#include <asm/paravirt.h> 40905a36a2SIngo Molnar#include <asm/percpu.h> 41905a36a2SIngo Molnar#include <asm/asm.h> 42905a36a2SIngo Molnar#include <asm/context_tracking.h> 43905a36a2SIngo Molnar#include <asm/smap.h> 44905a36a2SIngo Molnar#include <asm/pgtable_types.h> 45905a36a2SIngo Molnar#include <linux/err.h> 46905a36a2SIngo Molnar 47905a36a2SIngo Molnar/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */ 48905a36a2SIngo Molnar#include <linux/elf-em.h> 49905a36a2SIngo Molnar#define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE) 50905a36a2SIngo Molnar#define __AUDIT_ARCH_64BIT 0x80000000 51905a36a2SIngo Molnar#define __AUDIT_ARCH_LE 0x40000000 52905a36a2SIngo Molnar 53905a36a2SIngo Molnar .code64 54905a36a2SIngo Molnar .section .entry.text, "ax" 55905a36a2SIngo Molnar 56905a36a2SIngo Molnar 57905a36a2SIngo Molnar#ifdef CONFIG_PARAVIRT 58905a36a2SIngo MolnarENTRY(native_usergs_sysret64) 59905a36a2SIngo Molnar swapgs 60905a36a2SIngo Molnar sysretq 61905a36a2SIngo MolnarENDPROC(native_usergs_sysret64) 62905a36a2SIngo Molnar#endif /* CONFIG_PARAVIRT */ 63905a36a2SIngo Molnar 64905a36a2SIngo Molnar 65905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ 66905a36a2SIngo Molnar#ifdef CONFIG_TRACE_IRQFLAGS 67905a36a2SIngo Molnar bt $9,EFLAGS(%rsp) /* interrupts off? */ 68905a36a2SIngo Molnar jnc 1f 69905a36a2SIngo Molnar TRACE_IRQS_ON 70905a36a2SIngo Molnar1: 71905a36a2SIngo Molnar#endif 72905a36a2SIngo Molnar.endm 73905a36a2SIngo Molnar 74905a36a2SIngo Molnar/* 75905a36a2SIngo Molnar * When dynamic function tracer is enabled it will add a breakpoint 76905a36a2SIngo Molnar * to all locations that it is about to modify, sync CPUs, update 77905a36a2SIngo Molnar * all the code, sync CPUs, then remove the breakpoints. In this time 78905a36a2SIngo Molnar * if lockdep is enabled, it might jump back into the debug handler 79905a36a2SIngo Molnar * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF). 80905a36a2SIngo Molnar * 81905a36a2SIngo Molnar * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to 82905a36a2SIngo Molnar * make sure the stack pointer does not get reset back to the top 83905a36a2SIngo Molnar * of the debug stack, and instead just reuses the current stack. 84905a36a2SIngo Molnar */ 85905a36a2SIngo Molnar#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS) 86905a36a2SIngo Molnar 87905a36a2SIngo Molnar.macro TRACE_IRQS_OFF_DEBUG 88905a36a2SIngo Molnar call debug_stack_set_zero 89905a36a2SIngo Molnar TRACE_IRQS_OFF 90905a36a2SIngo Molnar call debug_stack_reset 91905a36a2SIngo Molnar.endm 92905a36a2SIngo Molnar 93905a36a2SIngo Molnar.macro TRACE_IRQS_ON_DEBUG 94905a36a2SIngo Molnar call debug_stack_set_zero 95905a36a2SIngo Molnar TRACE_IRQS_ON 96905a36a2SIngo Molnar call debug_stack_reset 97905a36a2SIngo Molnar.endm 98905a36a2SIngo Molnar 99905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ_DEBUG 100905a36a2SIngo Molnar bt $9,EFLAGS(%rsp) /* interrupts off? */ 101905a36a2SIngo Molnar jnc 1f 102905a36a2SIngo Molnar TRACE_IRQS_ON_DEBUG 103905a36a2SIngo Molnar1: 104905a36a2SIngo Molnar.endm 105905a36a2SIngo Molnar 106905a36a2SIngo Molnar#else 107905a36a2SIngo Molnar# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF 108905a36a2SIngo Molnar# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON 109905a36a2SIngo Molnar# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ 110905a36a2SIngo Molnar#endif 111905a36a2SIngo Molnar 112905a36a2SIngo Molnar/* 113905a36a2SIngo Molnar * 64bit SYSCALL instruction entry. Up to 6 arguments in registers. 114905a36a2SIngo Molnar * 115905a36a2SIngo Molnar * 64bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11, 116905a36a2SIngo Molnar * then loads new ss, cs, and rip from previously programmed MSRs. 117905a36a2SIngo Molnar * rflags gets masked by a value from another MSR (so CLD and CLAC 118905a36a2SIngo Molnar * are not needed). SYSCALL does not save anything on the stack 119905a36a2SIngo Molnar * and does not change rsp. 120905a36a2SIngo Molnar * 121905a36a2SIngo Molnar * Registers on entry: 122905a36a2SIngo Molnar * rax system call number 123905a36a2SIngo Molnar * rcx return address 124905a36a2SIngo Molnar * r11 saved rflags (note: r11 is callee-clobbered register in C ABI) 125905a36a2SIngo Molnar * rdi arg0 126905a36a2SIngo Molnar * rsi arg1 127905a36a2SIngo Molnar * rdx arg2 128905a36a2SIngo Molnar * r10 arg3 (needs to be moved to rcx to conform to C ABI) 129905a36a2SIngo Molnar * r8 arg4 130905a36a2SIngo Molnar * r9 arg5 131905a36a2SIngo Molnar * (note: r12-r15,rbp,rbx are callee-preserved in C ABI) 132905a36a2SIngo Molnar * 133905a36a2SIngo Molnar * Only called from user space. 134905a36a2SIngo Molnar * 135905a36a2SIngo Molnar * When user can change pt_regs->foo always force IRET. That is because 136905a36a2SIngo Molnar * it deals with uncanonical addresses better. SYSRET has trouble 137905a36a2SIngo Molnar * with them due to bugs in both AMD and Intel CPUs. 138905a36a2SIngo Molnar */ 139905a36a2SIngo Molnar 140b2502b41SIngo MolnarENTRY(entry_SYSCALL_64) 141905a36a2SIngo Molnar /* 142905a36a2SIngo Molnar * Interrupts are off on entry. 143905a36a2SIngo Molnar * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON, 144905a36a2SIngo Molnar * it is too small to ever cause noticeable irq latency. 145905a36a2SIngo Molnar */ 146905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 147905a36a2SIngo Molnar /* 148905a36a2SIngo Molnar * A hypervisor implementation might want to use a label 149905a36a2SIngo Molnar * after the swapgs, so that it can do the swapgs 150905a36a2SIngo Molnar * for the guest and jump here on syscall. 151905a36a2SIngo Molnar */ 152b2502b41SIngo MolnarGLOBAL(entry_SYSCALL_64_after_swapgs) 153905a36a2SIngo Molnar 154905a36a2SIngo Molnar movq %rsp,PER_CPU_VAR(rsp_scratch) 155905a36a2SIngo Molnar movq PER_CPU_VAR(cpu_current_top_of_stack),%rsp 156905a36a2SIngo Molnar 157905a36a2SIngo Molnar /* Construct struct pt_regs on stack */ 158905a36a2SIngo Molnar pushq $__USER_DS /* pt_regs->ss */ 159905a36a2SIngo Molnar pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */ 160905a36a2SIngo Molnar /* 161905a36a2SIngo Molnar * Re-enable interrupts. 162905a36a2SIngo Molnar * We use 'rsp_scratch' as a scratch space, hence irq-off block above 163905a36a2SIngo Molnar * must execute atomically in the face of possible interrupt-driven 164905a36a2SIngo Molnar * task preemption. We must enable interrupts only after we're done 165905a36a2SIngo Molnar * with using rsp_scratch: 166905a36a2SIngo Molnar */ 167905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 168905a36a2SIngo Molnar pushq %r11 /* pt_regs->flags */ 169905a36a2SIngo Molnar pushq $__USER_CS /* pt_regs->cs */ 170905a36a2SIngo Molnar pushq %rcx /* pt_regs->ip */ 171905a36a2SIngo Molnar pushq %rax /* pt_regs->orig_ax */ 172905a36a2SIngo Molnar pushq %rdi /* pt_regs->di */ 173905a36a2SIngo Molnar pushq %rsi /* pt_regs->si */ 174905a36a2SIngo Molnar pushq %rdx /* pt_regs->dx */ 175905a36a2SIngo Molnar pushq %rcx /* pt_regs->cx */ 176905a36a2SIngo Molnar pushq $-ENOSYS /* pt_regs->ax */ 177905a36a2SIngo Molnar pushq %r8 /* pt_regs->r8 */ 178905a36a2SIngo Molnar pushq %r9 /* pt_regs->r9 */ 179905a36a2SIngo Molnar pushq %r10 /* pt_regs->r10 */ 180905a36a2SIngo Molnar pushq %r11 /* pt_regs->r11 */ 181905a36a2SIngo Molnar sub $(6*8),%rsp /* pt_regs->bp,bx,r12-15 not saved */ 182905a36a2SIngo Molnar 183905a36a2SIngo Molnar testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS) 184905a36a2SIngo Molnar jnz tracesys 185b2502b41SIngo Molnarentry_SYSCALL_64_fastpath: 186905a36a2SIngo Molnar#if __SYSCALL_MASK == ~0 187905a36a2SIngo Molnar cmpq $__NR_syscall_max,%rax 188905a36a2SIngo Molnar#else 189905a36a2SIngo Molnar andl $__SYSCALL_MASK,%eax 190905a36a2SIngo Molnar cmpl $__NR_syscall_max,%eax 191905a36a2SIngo Molnar#endif 192905a36a2SIngo Molnar ja 1f /* return -ENOSYS (already in pt_regs->ax) */ 193905a36a2SIngo Molnar movq %r10,%rcx 194905a36a2SIngo Molnar call *sys_call_table(,%rax,8) 195905a36a2SIngo Molnar movq %rax,RAX(%rsp) 196905a36a2SIngo Molnar1: 197905a36a2SIngo Molnar/* 198905a36a2SIngo Molnar * Syscall return path ending with SYSRET (fast path). 199905a36a2SIngo Molnar * Has incompletely filled pt_regs. 200905a36a2SIngo Molnar */ 201905a36a2SIngo Molnar LOCKDEP_SYS_EXIT 202905a36a2SIngo Molnar /* 203905a36a2SIngo Molnar * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON, 204905a36a2SIngo Molnar * it is too small to ever cause noticeable irq latency. 205905a36a2SIngo Molnar */ 206905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 207905a36a2SIngo Molnar 208905a36a2SIngo Molnar /* 209905a36a2SIngo Molnar * We must check ti flags with interrupts (or at least preemption) 210905a36a2SIngo Molnar * off because we must *never* return to userspace without 211905a36a2SIngo Molnar * processing exit work that is enqueued if we're preempted here. 212905a36a2SIngo Molnar * In particular, returning to userspace with any of the one-shot 213905a36a2SIngo Molnar * flags (TIF_NOTIFY_RESUME, TIF_USER_RETURN_NOTIFY, etc) set is 214905a36a2SIngo Molnar * very bad. 215905a36a2SIngo Molnar */ 216905a36a2SIngo Molnar testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS) 217905a36a2SIngo Molnar jnz int_ret_from_sys_call_irqs_off /* Go to the slow path */ 218905a36a2SIngo Molnar 219905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RCX_R11 220905a36a2SIngo Molnar movq RIP(%rsp),%rcx 221905a36a2SIngo Molnar movq EFLAGS(%rsp),%r11 222905a36a2SIngo Molnar movq RSP(%rsp),%rsp 223905a36a2SIngo Molnar /* 224905a36a2SIngo Molnar * 64bit SYSRET restores rip from rcx, 225905a36a2SIngo Molnar * rflags from r11 (but RF and VM bits are forced to 0), 226905a36a2SIngo Molnar * cs and ss are loaded from MSRs. 227905a36a2SIngo Molnar * Restoration of rflags re-enables interrupts. 228905a36a2SIngo Molnar * 229905a36a2SIngo Molnar * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss 230905a36a2SIngo Molnar * descriptor is not reinitialized. This means that we should 231905a36a2SIngo Molnar * avoid SYSRET with SS == NULL, which could happen if we schedule, 232905a36a2SIngo Molnar * exit the kernel, and re-enter using an interrupt vector. (All 233905a36a2SIngo Molnar * interrupt entries on x86_64 set SS to NULL.) We prevent that 234905a36a2SIngo Molnar * from happening by reloading SS in __switch_to. (Actually 235905a36a2SIngo Molnar * detecting the failure in 64-bit userspace is tricky but can be 236905a36a2SIngo Molnar * done.) 237905a36a2SIngo Molnar */ 238905a36a2SIngo Molnar USERGS_SYSRET64 239905a36a2SIngo Molnar 240905a36a2SIngo Molnar /* Do syscall entry tracing */ 241905a36a2SIngo Molnartracesys: 242905a36a2SIngo Molnar movq %rsp, %rdi 243905a36a2SIngo Molnar movl $AUDIT_ARCH_X86_64, %esi 244905a36a2SIngo Molnar call syscall_trace_enter_phase1 245905a36a2SIngo Molnar test %rax, %rax 246905a36a2SIngo Molnar jnz tracesys_phase2 /* if needed, run the slow path */ 247905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RAX /* else restore clobbered regs */ 248905a36a2SIngo Molnar movq ORIG_RAX(%rsp), %rax 249b2502b41SIngo Molnar jmp entry_SYSCALL_64_fastpath /* and return to the fast path */ 250905a36a2SIngo Molnar 251905a36a2SIngo Molnartracesys_phase2: 252905a36a2SIngo Molnar SAVE_EXTRA_REGS 253905a36a2SIngo Molnar movq %rsp, %rdi 254905a36a2SIngo Molnar movl $AUDIT_ARCH_X86_64, %esi 255905a36a2SIngo Molnar movq %rax,%rdx 256905a36a2SIngo Molnar call syscall_trace_enter_phase2 257905a36a2SIngo Molnar 258905a36a2SIngo Molnar /* 259905a36a2SIngo Molnar * Reload registers from stack in case ptrace changed them. 260905a36a2SIngo Molnar * We don't reload %rax because syscall_trace_entry_phase2() returned 261905a36a2SIngo Molnar * the value it wants us to use in the table lookup. 262905a36a2SIngo Molnar */ 263905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RAX 264905a36a2SIngo Molnar RESTORE_EXTRA_REGS 265905a36a2SIngo Molnar#if __SYSCALL_MASK == ~0 266905a36a2SIngo Molnar cmpq $__NR_syscall_max,%rax 267905a36a2SIngo Molnar#else 268905a36a2SIngo Molnar andl $__SYSCALL_MASK,%eax 269905a36a2SIngo Molnar cmpl $__NR_syscall_max,%eax 270905a36a2SIngo Molnar#endif 271905a36a2SIngo Molnar ja 1f /* return -ENOSYS (already in pt_regs->ax) */ 272905a36a2SIngo Molnar movq %r10,%rcx /* fixup for C */ 273905a36a2SIngo Molnar call *sys_call_table(,%rax,8) 274905a36a2SIngo Molnar movq %rax,RAX(%rsp) 275905a36a2SIngo Molnar1: 276905a36a2SIngo Molnar /* Use IRET because user could have changed pt_regs->foo */ 277905a36a2SIngo Molnar 278905a36a2SIngo Molnar/* 279905a36a2SIngo Molnar * Syscall return path ending with IRET. 280905a36a2SIngo Molnar * Has correct iret frame. 281905a36a2SIngo Molnar */ 282905a36a2SIngo MolnarGLOBAL(int_ret_from_sys_call) 283905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 284905a36a2SIngo Molnarint_ret_from_sys_call_irqs_off: /* jumps come here from the irqs-off SYSRET path */ 285905a36a2SIngo Molnar TRACE_IRQS_OFF 286905a36a2SIngo Molnar movl $_TIF_ALLWORK_MASK,%edi 287905a36a2SIngo Molnar /* edi: mask to check */ 288905a36a2SIngo MolnarGLOBAL(int_with_check) 289905a36a2SIngo Molnar LOCKDEP_SYS_EXIT_IRQ 290905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 291905a36a2SIngo Molnar movl TI_flags(%rcx),%edx 292905a36a2SIngo Molnar andl %edi,%edx 293905a36a2SIngo Molnar jnz int_careful 294905a36a2SIngo Molnar andl $~TS_COMPAT,TI_status(%rcx) 295905a36a2SIngo Molnar jmp syscall_return 296905a36a2SIngo Molnar 297905a36a2SIngo Molnar /* Either reschedule or signal or syscall exit tracking needed. */ 298905a36a2SIngo Molnar /* First do a reschedule test. */ 299905a36a2SIngo Molnar /* edx: work, edi: workmask */ 300905a36a2SIngo Molnarint_careful: 301905a36a2SIngo Molnar bt $TIF_NEED_RESCHED,%edx 302905a36a2SIngo Molnar jnc int_very_careful 303905a36a2SIngo Molnar TRACE_IRQS_ON 304905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 305905a36a2SIngo Molnar pushq %rdi 306905a36a2SIngo Molnar SCHEDULE_USER 307905a36a2SIngo Molnar popq %rdi 308905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 309905a36a2SIngo Molnar TRACE_IRQS_OFF 310905a36a2SIngo Molnar jmp int_with_check 311905a36a2SIngo Molnar 312905a36a2SIngo Molnar /* handle signals and tracing -- both require a full pt_regs */ 313905a36a2SIngo Molnarint_very_careful: 314905a36a2SIngo Molnar TRACE_IRQS_ON 315905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 316905a36a2SIngo Molnar SAVE_EXTRA_REGS 317905a36a2SIngo Molnar /* Check for syscall exit trace */ 318905a36a2SIngo Molnar testl $_TIF_WORK_SYSCALL_EXIT,%edx 319905a36a2SIngo Molnar jz int_signal 320905a36a2SIngo Molnar pushq %rdi 321905a36a2SIngo Molnar leaq 8(%rsp),%rdi # &ptregs -> arg1 322905a36a2SIngo Molnar call syscall_trace_leave 323905a36a2SIngo Molnar popq %rdi 324905a36a2SIngo Molnar andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi 325905a36a2SIngo Molnar jmp int_restore_rest 326905a36a2SIngo Molnar 327905a36a2SIngo Molnarint_signal: 328905a36a2SIngo Molnar testl $_TIF_DO_NOTIFY_MASK,%edx 329905a36a2SIngo Molnar jz 1f 330905a36a2SIngo Molnar movq %rsp,%rdi # &ptregs -> arg1 331905a36a2SIngo Molnar xorl %esi,%esi # oldset -> arg2 332905a36a2SIngo Molnar call do_notify_resume 333905a36a2SIngo Molnar1: movl $_TIF_WORK_MASK,%edi 334905a36a2SIngo Molnarint_restore_rest: 335905a36a2SIngo Molnar RESTORE_EXTRA_REGS 336905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 337905a36a2SIngo Molnar TRACE_IRQS_OFF 338905a36a2SIngo Molnar jmp int_with_check 339905a36a2SIngo Molnar 340905a36a2SIngo Molnarsyscall_return: 341905a36a2SIngo Molnar /* The IRETQ could re-enable interrupts: */ 342905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_ANY) 343905a36a2SIngo Molnar TRACE_IRQS_IRETQ 344905a36a2SIngo Molnar 345905a36a2SIngo Molnar /* 346905a36a2SIngo Molnar * Try to use SYSRET instead of IRET if we're returning to 347905a36a2SIngo Molnar * a completely clean 64-bit userspace context. 348905a36a2SIngo Molnar */ 349905a36a2SIngo Molnar movq RCX(%rsp),%rcx 350905a36a2SIngo Molnar movq RIP(%rsp),%r11 351905a36a2SIngo Molnar cmpq %rcx,%r11 /* RCX == RIP */ 352905a36a2SIngo Molnar jne opportunistic_sysret_failed 353905a36a2SIngo Molnar 354905a36a2SIngo Molnar /* 355905a36a2SIngo Molnar * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP 356905a36a2SIngo Molnar * in kernel space. This essentially lets the user take over 357905a36a2SIngo Molnar * the kernel, since userspace controls RSP. 358905a36a2SIngo Molnar * 359905a36a2SIngo Molnar * If width of "canonical tail" ever becomes variable, this will need 360905a36a2SIngo Molnar * to be updated to remain correct on both old and new CPUs. 361905a36a2SIngo Molnar */ 362905a36a2SIngo Molnar .ifne __VIRTUAL_MASK_SHIFT - 47 363905a36a2SIngo Molnar .error "virtual address width changed -- SYSRET checks need update" 364905a36a2SIngo Molnar .endif 365905a36a2SIngo Molnar /* Change top 16 bits to be the sign-extension of 47th bit */ 366905a36a2SIngo Molnar shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 367905a36a2SIngo Molnar sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 368905a36a2SIngo Molnar /* If this changed %rcx, it was not canonical */ 369905a36a2SIngo Molnar cmpq %rcx, %r11 370905a36a2SIngo Molnar jne opportunistic_sysret_failed 371905a36a2SIngo Molnar 372905a36a2SIngo Molnar cmpq $__USER_CS,CS(%rsp) /* CS must match SYSRET */ 373905a36a2SIngo Molnar jne opportunistic_sysret_failed 374905a36a2SIngo Molnar 375905a36a2SIngo Molnar movq R11(%rsp),%r11 376905a36a2SIngo Molnar cmpq %r11,EFLAGS(%rsp) /* R11 == RFLAGS */ 377905a36a2SIngo Molnar jne opportunistic_sysret_failed 378905a36a2SIngo Molnar 379905a36a2SIngo Molnar /* 380905a36a2SIngo Molnar * SYSRET can't restore RF. SYSRET can restore TF, but unlike IRET, 381905a36a2SIngo Molnar * restoring TF results in a trap from userspace immediately after 382905a36a2SIngo Molnar * SYSRET. This would cause an infinite loop whenever #DB happens 383905a36a2SIngo Molnar * with register state that satisfies the opportunistic SYSRET 384905a36a2SIngo Molnar * conditions. For example, single-stepping this user code: 385905a36a2SIngo Molnar * 386905a36a2SIngo Molnar * movq $stuck_here,%rcx 387905a36a2SIngo Molnar * pushfq 388905a36a2SIngo Molnar * popq %r11 389905a36a2SIngo Molnar * stuck_here: 390905a36a2SIngo Molnar * 391905a36a2SIngo Molnar * would never get past 'stuck_here'. 392905a36a2SIngo Molnar */ 393905a36a2SIngo Molnar testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11 394905a36a2SIngo Molnar jnz opportunistic_sysret_failed 395905a36a2SIngo Molnar 396905a36a2SIngo Molnar /* nothing to check for RSP */ 397905a36a2SIngo Molnar 398905a36a2SIngo Molnar cmpq $__USER_DS,SS(%rsp) /* SS must match SYSRET */ 399905a36a2SIngo Molnar jne opportunistic_sysret_failed 400905a36a2SIngo Molnar 401905a36a2SIngo Molnar /* 402905a36a2SIngo Molnar * We win! This label is here just for ease of understanding 403905a36a2SIngo Molnar * perf profiles. Nothing jumps here. 404905a36a2SIngo Molnar */ 405905a36a2SIngo Molnarsyscall_return_via_sysret: 406905a36a2SIngo Molnar /* rcx and r11 are already restored (see code above) */ 407905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RCX_R11 408905a36a2SIngo Molnar movq RSP(%rsp),%rsp 409905a36a2SIngo Molnar USERGS_SYSRET64 410905a36a2SIngo Molnar 411905a36a2SIngo Molnaropportunistic_sysret_failed: 412905a36a2SIngo Molnar SWAPGS 413905a36a2SIngo Molnar jmp restore_c_regs_and_iret 414b2502b41SIngo MolnarEND(entry_SYSCALL_64) 415905a36a2SIngo Molnar 416905a36a2SIngo Molnar 417905a36a2SIngo Molnar .macro FORK_LIKE func 418905a36a2SIngo MolnarENTRY(stub_\func) 419905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 420905a36a2SIngo Molnar jmp sys_\func 421905a36a2SIngo MolnarEND(stub_\func) 422905a36a2SIngo Molnar .endm 423905a36a2SIngo Molnar 424905a36a2SIngo Molnar FORK_LIKE clone 425905a36a2SIngo Molnar FORK_LIKE fork 426905a36a2SIngo Molnar FORK_LIKE vfork 427905a36a2SIngo Molnar 428905a36a2SIngo MolnarENTRY(stub_execve) 429905a36a2SIngo Molnar call sys_execve 430905a36a2SIngo Molnarreturn_from_execve: 431905a36a2SIngo Molnar testl %eax, %eax 432905a36a2SIngo Molnar jz 1f 433905a36a2SIngo Molnar /* exec failed, can use fast SYSRET code path in this case */ 434905a36a2SIngo Molnar ret 435905a36a2SIngo Molnar1: 436905a36a2SIngo Molnar /* must use IRET code path (pt_regs->cs may have changed) */ 437905a36a2SIngo Molnar addq $8, %rsp 438905a36a2SIngo Molnar ZERO_EXTRA_REGS 439905a36a2SIngo Molnar movq %rax,RAX(%rsp) 440905a36a2SIngo Molnar jmp int_ret_from_sys_call 441905a36a2SIngo MolnarEND(stub_execve) 442905a36a2SIngo Molnar/* 443905a36a2SIngo Molnar * Remaining execve stubs are only 7 bytes long. 444905a36a2SIngo Molnar * ENTRY() often aligns to 16 bytes, which in this case has no benefits. 445905a36a2SIngo Molnar */ 446905a36a2SIngo Molnar .align 8 447905a36a2SIngo MolnarGLOBAL(stub_execveat) 448905a36a2SIngo Molnar call sys_execveat 449905a36a2SIngo Molnar jmp return_from_execve 450905a36a2SIngo MolnarEND(stub_execveat) 451905a36a2SIngo Molnar 452905a36a2SIngo Molnar#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION) 453905a36a2SIngo Molnar .align 8 454905a36a2SIngo MolnarGLOBAL(stub_x32_execve) 455905a36a2SIngo MolnarGLOBAL(stub32_execve) 456905a36a2SIngo Molnar call compat_sys_execve 457905a36a2SIngo Molnar jmp return_from_execve 458905a36a2SIngo MolnarEND(stub32_execve) 459905a36a2SIngo MolnarEND(stub_x32_execve) 460905a36a2SIngo Molnar .align 8 461905a36a2SIngo MolnarGLOBAL(stub_x32_execveat) 462905a36a2SIngo MolnarGLOBAL(stub32_execveat) 463905a36a2SIngo Molnar call compat_sys_execveat 464905a36a2SIngo Molnar jmp return_from_execve 465905a36a2SIngo MolnarEND(stub32_execveat) 466905a36a2SIngo MolnarEND(stub_x32_execveat) 467905a36a2SIngo Molnar#endif 468905a36a2SIngo Molnar 469905a36a2SIngo Molnar/* 470905a36a2SIngo Molnar * sigreturn is special because it needs to restore all registers on return. 471905a36a2SIngo Molnar * This cannot be done with SYSRET, so use the IRET return path instead. 472905a36a2SIngo Molnar */ 473905a36a2SIngo MolnarENTRY(stub_rt_sigreturn) 474905a36a2SIngo Molnar /* 475905a36a2SIngo Molnar * SAVE_EXTRA_REGS result is not normally needed: 476905a36a2SIngo Molnar * sigreturn overwrites all pt_regs->GPREGS. 477905a36a2SIngo Molnar * But sigreturn can fail (!), and there is no easy way to detect that. 478905a36a2SIngo Molnar * To make sure RESTORE_EXTRA_REGS doesn't restore garbage on error, 479905a36a2SIngo Molnar * we SAVE_EXTRA_REGS here. 480905a36a2SIngo Molnar */ 481905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 482905a36a2SIngo Molnar call sys_rt_sigreturn 483905a36a2SIngo Molnarreturn_from_stub: 484905a36a2SIngo Molnar addq $8, %rsp 485905a36a2SIngo Molnar RESTORE_EXTRA_REGS 486905a36a2SIngo Molnar movq %rax,RAX(%rsp) 487905a36a2SIngo Molnar jmp int_ret_from_sys_call 488905a36a2SIngo MolnarEND(stub_rt_sigreturn) 489905a36a2SIngo Molnar 490905a36a2SIngo Molnar#ifdef CONFIG_X86_X32_ABI 491905a36a2SIngo MolnarENTRY(stub_x32_rt_sigreturn) 492905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 493905a36a2SIngo Molnar call sys32_x32_rt_sigreturn 494905a36a2SIngo Molnar jmp return_from_stub 495905a36a2SIngo MolnarEND(stub_x32_rt_sigreturn) 496905a36a2SIngo Molnar#endif 497905a36a2SIngo Molnar 498905a36a2SIngo Molnar/* 499905a36a2SIngo Molnar * A newly forked process directly context switches into this address. 500905a36a2SIngo Molnar * 501905a36a2SIngo Molnar * rdi: prev task we switched from 502905a36a2SIngo Molnar */ 503905a36a2SIngo MolnarENTRY(ret_from_fork) 504905a36a2SIngo Molnar 505905a36a2SIngo Molnar LOCK ; btr $TIF_FORK,TI_flags(%r8) 506905a36a2SIngo Molnar 507905a36a2SIngo Molnar pushq $0x0002 508905a36a2SIngo Molnar popfq # reset kernel eflags 509905a36a2SIngo Molnar 510905a36a2SIngo Molnar call schedule_tail # rdi: 'prev' task parameter 511905a36a2SIngo Molnar 512905a36a2SIngo Molnar RESTORE_EXTRA_REGS 513905a36a2SIngo Molnar 514905a36a2SIngo Molnar testb $3, CS(%rsp) # from kernel_thread? 515905a36a2SIngo Molnar 516905a36a2SIngo Molnar /* 517905a36a2SIngo Molnar * By the time we get here, we have no idea whether our pt_regs, 518905a36a2SIngo Molnar * ti flags, and ti status came from the 64-bit SYSCALL fast path, 519138bd56aSIngo Molnar * the slow path, or one of the 32-bit compat paths. 520905a36a2SIngo Molnar * Use IRET code path to return, since it can safely handle 521905a36a2SIngo Molnar * all of the above. 522905a36a2SIngo Molnar */ 523905a36a2SIngo Molnar jnz int_ret_from_sys_call 524905a36a2SIngo Molnar 525905a36a2SIngo Molnar /* We came from kernel_thread */ 526905a36a2SIngo Molnar /* nb: we depend on RESTORE_EXTRA_REGS above */ 527905a36a2SIngo Molnar movq %rbp, %rdi 528905a36a2SIngo Molnar call *%rbx 529905a36a2SIngo Molnar movl $0, RAX(%rsp) 530905a36a2SIngo Molnar RESTORE_EXTRA_REGS 531905a36a2SIngo Molnar jmp int_ret_from_sys_call 532905a36a2SIngo MolnarEND(ret_from_fork) 533905a36a2SIngo Molnar 534905a36a2SIngo Molnar/* 535905a36a2SIngo Molnar * Build the entry stubs with some assembler magic. 536905a36a2SIngo Molnar * We pack 1 stub into every 8-byte block. 537905a36a2SIngo Molnar */ 538905a36a2SIngo Molnar .align 8 539905a36a2SIngo MolnarENTRY(irq_entries_start) 540905a36a2SIngo Molnar vector=FIRST_EXTERNAL_VECTOR 541905a36a2SIngo Molnar .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR) 542905a36a2SIngo Molnar pushq $(~vector+0x80) /* Note: always in signed byte range */ 543905a36a2SIngo Molnar vector=vector+1 544905a36a2SIngo Molnar jmp common_interrupt 545905a36a2SIngo Molnar .align 8 546905a36a2SIngo Molnar .endr 547905a36a2SIngo MolnarEND(irq_entries_start) 548905a36a2SIngo Molnar 549905a36a2SIngo Molnar/* 550905a36a2SIngo Molnar * Interrupt entry/exit. 551905a36a2SIngo Molnar * 552905a36a2SIngo Molnar * Interrupt entry points save only callee clobbered registers in fast path. 553905a36a2SIngo Molnar * 554905a36a2SIngo Molnar * Entry runs with interrupts off. 555905a36a2SIngo Molnar */ 556905a36a2SIngo Molnar 557905a36a2SIngo Molnar/* 0(%rsp): ~(interrupt number) */ 558905a36a2SIngo Molnar .macro interrupt func 559905a36a2SIngo Molnar cld 560905a36a2SIngo Molnar /* 561905a36a2SIngo Molnar * Since nothing in interrupt handling code touches r12...r15 members 562905a36a2SIngo Molnar * of "struct pt_regs", and since interrupts can nest, we can save 563905a36a2SIngo Molnar * four stack slots and simultaneously provide 564905a36a2SIngo Molnar * an unwind-friendly stack layout by saving "truncated" pt_regs 565905a36a2SIngo Molnar * exactly up to rbp slot, without these members. 566905a36a2SIngo Molnar */ 567905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK -RBP 568905a36a2SIngo Molnar SAVE_C_REGS -RBP 569905a36a2SIngo Molnar /* this goes to 0(%rsp) for unwinder, not for saving the value: */ 570905a36a2SIngo Molnar SAVE_EXTRA_REGS_RBP -RBP 571905a36a2SIngo Molnar 572905a36a2SIngo Molnar leaq -RBP(%rsp),%rdi /* arg1 for \func (pointer to pt_regs) */ 573905a36a2SIngo Molnar 574905a36a2SIngo Molnar testb $3, CS-RBP(%rsp) 575905a36a2SIngo Molnar jz 1f 576905a36a2SIngo Molnar SWAPGS 577905a36a2SIngo Molnar1: 578905a36a2SIngo Molnar /* 579905a36a2SIngo Molnar * Save previous stack pointer, optionally switch to interrupt stack. 580905a36a2SIngo Molnar * irq_count is used to check if a CPU is already on an interrupt stack 581905a36a2SIngo Molnar * or not. While this is essentially redundant with preempt_count it is 582905a36a2SIngo Molnar * a little cheaper to use a separate counter in the PDA (short of 583905a36a2SIngo Molnar * moving irq_enter into assembly, which would be too much work) 584905a36a2SIngo Molnar */ 585905a36a2SIngo Molnar movq %rsp, %rsi 586905a36a2SIngo Molnar incl PER_CPU_VAR(irq_count) 587905a36a2SIngo Molnar cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp 588905a36a2SIngo Molnar pushq %rsi 589905a36a2SIngo Molnar /* We entered an interrupt context - irqs are off: */ 590905a36a2SIngo Molnar TRACE_IRQS_OFF 591905a36a2SIngo Molnar 592905a36a2SIngo Molnar call \func 593905a36a2SIngo Molnar .endm 594905a36a2SIngo Molnar 595905a36a2SIngo Molnar /* 596905a36a2SIngo Molnar * The interrupt stubs push (~vector+0x80) onto the stack and 597905a36a2SIngo Molnar * then jump to common_interrupt. 598905a36a2SIngo Molnar */ 599905a36a2SIngo Molnar .p2align CONFIG_X86_L1_CACHE_SHIFT 600905a36a2SIngo Molnarcommon_interrupt: 601905a36a2SIngo Molnar ASM_CLAC 602905a36a2SIngo Molnar addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */ 603905a36a2SIngo Molnar interrupt do_IRQ 604905a36a2SIngo Molnar /* 0(%rsp): old RSP */ 605905a36a2SIngo Molnarret_from_intr: 606905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 607905a36a2SIngo Molnar TRACE_IRQS_OFF 608905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 609905a36a2SIngo Molnar 610905a36a2SIngo Molnar /* Restore saved previous stack */ 611905a36a2SIngo Molnar popq %rsi 612905a36a2SIngo Molnar /* return code expects complete pt_regs - adjust rsp accordingly: */ 613905a36a2SIngo Molnar leaq -RBP(%rsi),%rsp 614905a36a2SIngo Molnar 615905a36a2SIngo Molnar testb $3, CS(%rsp) 616905a36a2SIngo Molnar jz retint_kernel 617905a36a2SIngo Molnar /* Interrupt came from user space */ 618905a36a2SIngo Molnarretint_user: 619905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 620905a36a2SIngo Molnar /* 621905a36a2SIngo Molnar * %rcx: thread info. Interrupts off. 622905a36a2SIngo Molnar */ 623905a36a2SIngo Molnarretint_with_reschedule: 624905a36a2SIngo Molnar movl $_TIF_WORK_MASK,%edi 625905a36a2SIngo Molnarretint_check: 626905a36a2SIngo Molnar LOCKDEP_SYS_EXIT_IRQ 627905a36a2SIngo Molnar movl TI_flags(%rcx),%edx 628905a36a2SIngo Molnar andl %edi,%edx 629905a36a2SIngo Molnar jnz retint_careful 630905a36a2SIngo Molnar 631905a36a2SIngo Molnarretint_swapgs: /* return to user-space */ 632905a36a2SIngo Molnar /* 633905a36a2SIngo Molnar * The iretq could re-enable interrupts: 634905a36a2SIngo Molnar */ 635905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_ANY) 636905a36a2SIngo Molnar TRACE_IRQS_IRETQ 637905a36a2SIngo Molnar 638905a36a2SIngo Molnar SWAPGS 639905a36a2SIngo Molnar jmp restore_c_regs_and_iret 640905a36a2SIngo Molnar 641905a36a2SIngo Molnar/* Returning to kernel space */ 642905a36a2SIngo Molnarretint_kernel: 643905a36a2SIngo Molnar#ifdef CONFIG_PREEMPT 644905a36a2SIngo Molnar /* Interrupts are off */ 645905a36a2SIngo Molnar /* Check if we need preemption */ 646905a36a2SIngo Molnar bt $9,EFLAGS(%rsp) /* interrupts were off? */ 647905a36a2SIngo Molnar jnc 1f 648905a36a2SIngo Molnar0: cmpl $0,PER_CPU_VAR(__preempt_count) 649905a36a2SIngo Molnar jnz 1f 650905a36a2SIngo Molnar call preempt_schedule_irq 651905a36a2SIngo Molnar jmp 0b 652905a36a2SIngo Molnar1: 653905a36a2SIngo Molnar#endif 654905a36a2SIngo Molnar /* 655905a36a2SIngo Molnar * The iretq could re-enable interrupts: 656905a36a2SIngo Molnar */ 657905a36a2SIngo Molnar TRACE_IRQS_IRETQ 658905a36a2SIngo Molnar 659905a36a2SIngo Molnar/* 660905a36a2SIngo Molnar * At this label, code paths which return to kernel and to user, 661905a36a2SIngo Molnar * which come from interrupts/exception and from syscalls, merge. 662905a36a2SIngo Molnar */ 663905a36a2SIngo Molnarrestore_c_regs_and_iret: 664905a36a2SIngo Molnar RESTORE_C_REGS 665905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 8 666905a36a2SIngo Molnar INTERRUPT_RETURN 667905a36a2SIngo Molnar 668905a36a2SIngo MolnarENTRY(native_iret) 669905a36a2SIngo Molnar /* 670905a36a2SIngo Molnar * Are we returning to a stack segment from the LDT? Note: in 671905a36a2SIngo Molnar * 64-bit mode SS:RSP on the exception stack is always valid. 672905a36a2SIngo Molnar */ 673905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 674905a36a2SIngo Molnar testb $4,(SS-RIP)(%rsp) 675905a36a2SIngo Molnar jnz native_irq_return_ldt 676905a36a2SIngo Molnar#endif 677905a36a2SIngo Molnar 678905a36a2SIngo Molnar.global native_irq_return_iret 679905a36a2SIngo Molnarnative_irq_return_iret: 680905a36a2SIngo Molnar /* 681905a36a2SIngo Molnar * This may fault. Non-paranoid faults on return to userspace are 682905a36a2SIngo Molnar * handled by fixup_bad_iret. These include #SS, #GP, and #NP. 683905a36a2SIngo Molnar * Double-faults due to espfix64 are handled in do_double_fault. 684905a36a2SIngo Molnar * Other faults here are fatal. 685905a36a2SIngo Molnar */ 686905a36a2SIngo Molnar iretq 687905a36a2SIngo Molnar 688905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 689905a36a2SIngo Molnarnative_irq_return_ldt: 690905a36a2SIngo Molnar pushq %rax 691905a36a2SIngo Molnar pushq %rdi 692905a36a2SIngo Molnar SWAPGS 693905a36a2SIngo Molnar movq PER_CPU_VAR(espfix_waddr),%rdi 694905a36a2SIngo Molnar movq %rax,(0*8)(%rdi) /* RAX */ 695905a36a2SIngo Molnar movq (2*8)(%rsp),%rax /* RIP */ 696905a36a2SIngo Molnar movq %rax,(1*8)(%rdi) 697905a36a2SIngo Molnar movq (3*8)(%rsp),%rax /* CS */ 698905a36a2SIngo Molnar movq %rax,(2*8)(%rdi) 699905a36a2SIngo Molnar movq (4*8)(%rsp),%rax /* RFLAGS */ 700905a36a2SIngo Molnar movq %rax,(3*8)(%rdi) 701905a36a2SIngo Molnar movq (6*8)(%rsp),%rax /* SS */ 702905a36a2SIngo Molnar movq %rax,(5*8)(%rdi) 703905a36a2SIngo Molnar movq (5*8)(%rsp),%rax /* RSP */ 704905a36a2SIngo Molnar movq %rax,(4*8)(%rdi) 705905a36a2SIngo Molnar andl $0xffff0000,%eax 706905a36a2SIngo Molnar popq %rdi 707905a36a2SIngo Molnar orq PER_CPU_VAR(espfix_stack),%rax 708905a36a2SIngo Molnar SWAPGS 709905a36a2SIngo Molnar movq %rax,%rsp 710905a36a2SIngo Molnar popq %rax 711905a36a2SIngo Molnar jmp native_irq_return_iret 712905a36a2SIngo Molnar#endif 713905a36a2SIngo Molnar 714905a36a2SIngo Molnar /* edi: workmask, edx: work */ 715905a36a2SIngo Molnarretint_careful: 716905a36a2SIngo Molnar bt $TIF_NEED_RESCHED,%edx 717905a36a2SIngo Molnar jnc retint_signal 718905a36a2SIngo Molnar TRACE_IRQS_ON 719905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 720905a36a2SIngo Molnar pushq %rdi 721905a36a2SIngo Molnar SCHEDULE_USER 722905a36a2SIngo Molnar popq %rdi 723905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 724905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 725905a36a2SIngo Molnar TRACE_IRQS_OFF 726905a36a2SIngo Molnar jmp retint_check 727905a36a2SIngo Molnar 728905a36a2SIngo Molnarretint_signal: 729905a36a2SIngo Molnar testl $_TIF_DO_NOTIFY_MASK,%edx 730905a36a2SIngo Molnar jz retint_swapgs 731905a36a2SIngo Molnar TRACE_IRQS_ON 732905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 733905a36a2SIngo Molnar SAVE_EXTRA_REGS 734905a36a2SIngo Molnar movq $-1,ORIG_RAX(%rsp) 735905a36a2SIngo Molnar xorl %esi,%esi # oldset 736905a36a2SIngo Molnar movq %rsp,%rdi # &pt_regs 737905a36a2SIngo Molnar call do_notify_resume 738905a36a2SIngo Molnar RESTORE_EXTRA_REGS 739905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 740905a36a2SIngo Molnar TRACE_IRQS_OFF 741905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 742905a36a2SIngo Molnar jmp retint_with_reschedule 743905a36a2SIngo Molnar 744905a36a2SIngo MolnarEND(common_interrupt) 745905a36a2SIngo Molnar 746905a36a2SIngo Molnar/* 747905a36a2SIngo Molnar * APIC interrupts. 748905a36a2SIngo Molnar */ 749905a36a2SIngo Molnar.macro apicinterrupt3 num sym do_sym 750905a36a2SIngo MolnarENTRY(\sym) 751905a36a2SIngo Molnar ASM_CLAC 752905a36a2SIngo Molnar pushq $~(\num) 753905a36a2SIngo Molnar.Lcommon_\sym: 754905a36a2SIngo Molnar interrupt \do_sym 755905a36a2SIngo Molnar jmp ret_from_intr 756905a36a2SIngo MolnarEND(\sym) 757905a36a2SIngo Molnar.endm 758905a36a2SIngo Molnar 759905a36a2SIngo Molnar#ifdef CONFIG_TRACING 760905a36a2SIngo Molnar#define trace(sym) trace_##sym 761905a36a2SIngo Molnar#define smp_trace(sym) smp_trace_##sym 762905a36a2SIngo Molnar 763905a36a2SIngo Molnar.macro trace_apicinterrupt num sym 764905a36a2SIngo Molnarapicinterrupt3 \num trace(\sym) smp_trace(\sym) 765905a36a2SIngo Molnar.endm 766905a36a2SIngo Molnar#else 767905a36a2SIngo Molnar.macro trace_apicinterrupt num sym do_sym 768905a36a2SIngo Molnar.endm 769905a36a2SIngo Molnar#endif 770905a36a2SIngo Molnar 771905a36a2SIngo Molnar.macro apicinterrupt num sym do_sym 772905a36a2SIngo Molnarapicinterrupt3 \num \sym \do_sym 773905a36a2SIngo Molnartrace_apicinterrupt \num \sym 774905a36a2SIngo Molnar.endm 775905a36a2SIngo Molnar 776905a36a2SIngo Molnar#ifdef CONFIG_SMP 777905a36a2SIngo Molnarapicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR \ 778905a36a2SIngo Molnar irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt 779905a36a2SIngo Molnarapicinterrupt3 REBOOT_VECTOR \ 780905a36a2SIngo Molnar reboot_interrupt smp_reboot_interrupt 781905a36a2SIngo Molnar#endif 782905a36a2SIngo Molnar 783905a36a2SIngo Molnar#ifdef CONFIG_X86_UV 784905a36a2SIngo Molnarapicinterrupt3 UV_BAU_MESSAGE \ 785905a36a2SIngo Molnar uv_bau_message_intr1 uv_bau_message_interrupt 786905a36a2SIngo Molnar#endif 787905a36a2SIngo Molnarapicinterrupt LOCAL_TIMER_VECTOR \ 788905a36a2SIngo Molnar apic_timer_interrupt smp_apic_timer_interrupt 789905a36a2SIngo Molnarapicinterrupt X86_PLATFORM_IPI_VECTOR \ 790905a36a2SIngo Molnar x86_platform_ipi smp_x86_platform_ipi 791905a36a2SIngo Molnar 792905a36a2SIngo Molnar#ifdef CONFIG_HAVE_KVM 793905a36a2SIngo Molnarapicinterrupt3 POSTED_INTR_VECTOR \ 794905a36a2SIngo Molnar kvm_posted_intr_ipi smp_kvm_posted_intr_ipi 795*9dda1658SIngo Molnarapicinterrupt3 POSTED_INTR_WAKEUP_VECTOR \ 796*9dda1658SIngo Molnar kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi 797905a36a2SIngo Molnar#endif 798905a36a2SIngo Molnar 799905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE_THRESHOLD 800905a36a2SIngo Molnarapicinterrupt THRESHOLD_APIC_VECTOR \ 801905a36a2SIngo Molnar threshold_interrupt smp_threshold_interrupt 802905a36a2SIngo Molnar#endif 803905a36a2SIngo Molnar 804*9dda1658SIngo Molnar#ifdef CONFIG_X86_MCE_AMD 805*9dda1658SIngo Molnarapicinterrupt DEFERRED_ERROR_VECTOR \ 806*9dda1658SIngo Molnar deferred_error_interrupt smp_deferred_error_interrupt 807*9dda1658SIngo Molnar#endif 808*9dda1658SIngo Molnar 809905a36a2SIngo Molnar#ifdef CONFIG_X86_THERMAL_VECTOR 810905a36a2SIngo Molnarapicinterrupt THERMAL_APIC_VECTOR \ 811905a36a2SIngo Molnar thermal_interrupt smp_thermal_interrupt 812905a36a2SIngo Molnar#endif 813905a36a2SIngo Molnar 814905a36a2SIngo Molnar#ifdef CONFIG_SMP 815905a36a2SIngo Molnarapicinterrupt CALL_FUNCTION_SINGLE_VECTOR \ 816905a36a2SIngo Molnar call_function_single_interrupt smp_call_function_single_interrupt 817905a36a2SIngo Molnarapicinterrupt CALL_FUNCTION_VECTOR \ 818905a36a2SIngo Molnar call_function_interrupt smp_call_function_interrupt 819905a36a2SIngo Molnarapicinterrupt RESCHEDULE_VECTOR \ 820905a36a2SIngo Molnar reschedule_interrupt smp_reschedule_interrupt 821905a36a2SIngo Molnar#endif 822905a36a2SIngo Molnar 823905a36a2SIngo Molnarapicinterrupt ERROR_APIC_VECTOR \ 824905a36a2SIngo Molnar error_interrupt smp_error_interrupt 825905a36a2SIngo Molnarapicinterrupt SPURIOUS_APIC_VECTOR \ 826905a36a2SIngo Molnar spurious_interrupt smp_spurious_interrupt 827905a36a2SIngo Molnar 828905a36a2SIngo Molnar#ifdef CONFIG_IRQ_WORK 829905a36a2SIngo Molnarapicinterrupt IRQ_WORK_VECTOR \ 830905a36a2SIngo Molnar irq_work_interrupt smp_irq_work_interrupt 831905a36a2SIngo Molnar#endif 832905a36a2SIngo Molnar 833905a36a2SIngo Molnar/* 834905a36a2SIngo Molnar * Exception entry points. 835905a36a2SIngo Molnar */ 836905a36a2SIngo Molnar#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8) 837905a36a2SIngo Molnar 838905a36a2SIngo Molnar.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 839905a36a2SIngo MolnarENTRY(\sym) 840905a36a2SIngo Molnar /* Sanity check */ 841905a36a2SIngo Molnar .if \shift_ist != -1 && \paranoid == 0 842905a36a2SIngo Molnar .error "using shift_ist requires paranoid=1" 843905a36a2SIngo Molnar .endif 844905a36a2SIngo Molnar 845905a36a2SIngo Molnar ASM_CLAC 846905a36a2SIngo Molnar PARAVIRT_ADJUST_EXCEPTION_FRAME 847905a36a2SIngo Molnar 848905a36a2SIngo Molnar .ifeq \has_error_code 849905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 850905a36a2SIngo Molnar .endif 851905a36a2SIngo Molnar 852905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 853905a36a2SIngo Molnar 854905a36a2SIngo Molnar .if \paranoid 855905a36a2SIngo Molnar .if \paranoid == 1 856905a36a2SIngo Molnar testb $3, CS(%rsp) /* If coming from userspace, switch */ 857905a36a2SIngo Molnar jnz 1f /* stacks. */ 858905a36a2SIngo Molnar .endif 859905a36a2SIngo Molnar call paranoid_entry 860905a36a2SIngo Molnar .else 861905a36a2SIngo Molnar call error_entry 862905a36a2SIngo Molnar .endif 863905a36a2SIngo Molnar /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */ 864905a36a2SIngo Molnar 865905a36a2SIngo Molnar .if \paranoid 866905a36a2SIngo Molnar .if \shift_ist != -1 867905a36a2SIngo Molnar TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */ 868905a36a2SIngo Molnar .else 869905a36a2SIngo Molnar TRACE_IRQS_OFF 870905a36a2SIngo Molnar .endif 871905a36a2SIngo Molnar .endif 872905a36a2SIngo Molnar 873905a36a2SIngo Molnar movq %rsp,%rdi /* pt_regs pointer */ 874905a36a2SIngo Molnar 875905a36a2SIngo Molnar .if \has_error_code 876905a36a2SIngo Molnar movq ORIG_RAX(%rsp),%rsi /* get error code */ 877905a36a2SIngo Molnar movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */ 878905a36a2SIngo Molnar .else 879905a36a2SIngo Molnar xorl %esi,%esi /* no error code */ 880905a36a2SIngo Molnar .endif 881905a36a2SIngo Molnar 882905a36a2SIngo Molnar .if \shift_ist != -1 883905a36a2SIngo Molnar subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist) 884905a36a2SIngo Molnar .endif 885905a36a2SIngo Molnar 886905a36a2SIngo Molnar call \do_sym 887905a36a2SIngo Molnar 888905a36a2SIngo Molnar .if \shift_ist != -1 889905a36a2SIngo Molnar addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist) 890905a36a2SIngo Molnar .endif 891905a36a2SIngo Molnar 892905a36a2SIngo Molnar /* these procedures expect "no swapgs" flag in ebx */ 893905a36a2SIngo Molnar .if \paranoid 894905a36a2SIngo Molnar jmp paranoid_exit 895905a36a2SIngo Molnar .else 896905a36a2SIngo Molnar jmp error_exit 897905a36a2SIngo Molnar .endif 898905a36a2SIngo Molnar 899905a36a2SIngo Molnar .if \paranoid == 1 900905a36a2SIngo Molnar /* 901905a36a2SIngo Molnar * Paranoid entry from userspace. Switch stacks and treat it 902905a36a2SIngo Molnar * as a normal entry. This means that paranoid handlers 903905a36a2SIngo Molnar * run in real process context if user_mode(regs). 904905a36a2SIngo Molnar */ 905905a36a2SIngo Molnar1: 906905a36a2SIngo Molnar call error_entry 907905a36a2SIngo Molnar 908905a36a2SIngo Molnar 909905a36a2SIngo Molnar movq %rsp,%rdi /* pt_regs pointer */ 910905a36a2SIngo Molnar call sync_regs 911905a36a2SIngo Molnar movq %rax,%rsp /* switch stack */ 912905a36a2SIngo Molnar 913905a36a2SIngo Molnar movq %rsp,%rdi /* pt_regs pointer */ 914905a36a2SIngo Molnar 915905a36a2SIngo Molnar .if \has_error_code 916905a36a2SIngo Molnar movq ORIG_RAX(%rsp),%rsi /* get error code */ 917905a36a2SIngo Molnar movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */ 918905a36a2SIngo Molnar .else 919905a36a2SIngo Molnar xorl %esi,%esi /* no error code */ 920905a36a2SIngo Molnar .endif 921905a36a2SIngo Molnar 922905a36a2SIngo Molnar call \do_sym 923905a36a2SIngo Molnar 924905a36a2SIngo Molnar jmp error_exit /* %ebx: no swapgs flag */ 925905a36a2SIngo Molnar .endif 926905a36a2SIngo MolnarEND(\sym) 927905a36a2SIngo Molnar.endm 928905a36a2SIngo Molnar 929905a36a2SIngo Molnar#ifdef CONFIG_TRACING 930905a36a2SIngo Molnar.macro trace_idtentry sym do_sym has_error_code:req 931905a36a2SIngo Molnaridtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code 932905a36a2SIngo Molnaridtentry \sym \do_sym has_error_code=\has_error_code 933905a36a2SIngo Molnar.endm 934905a36a2SIngo Molnar#else 935905a36a2SIngo Molnar.macro trace_idtentry sym do_sym has_error_code:req 936905a36a2SIngo Molnaridtentry \sym \do_sym has_error_code=\has_error_code 937905a36a2SIngo Molnar.endm 938905a36a2SIngo Molnar#endif 939905a36a2SIngo Molnar 940905a36a2SIngo Molnaridtentry divide_error do_divide_error has_error_code=0 941905a36a2SIngo Molnaridtentry overflow do_overflow has_error_code=0 942905a36a2SIngo Molnaridtentry bounds do_bounds has_error_code=0 943905a36a2SIngo Molnaridtentry invalid_op do_invalid_op has_error_code=0 944905a36a2SIngo Molnaridtentry device_not_available do_device_not_available has_error_code=0 945905a36a2SIngo Molnaridtentry double_fault do_double_fault has_error_code=1 paranoid=2 946905a36a2SIngo Molnaridtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0 947905a36a2SIngo Molnaridtentry invalid_TSS do_invalid_TSS has_error_code=1 948905a36a2SIngo Molnaridtentry segment_not_present do_segment_not_present has_error_code=1 949905a36a2SIngo Molnaridtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0 950905a36a2SIngo Molnaridtentry coprocessor_error do_coprocessor_error has_error_code=0 951905a36a2SIngo Molnaridtentry alignment_check do_alignment_check has_error_code=1 952905a36a2SIngo Molnaridtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0 953905a36a2SIngo Molnar 954905a36a2SIngo Molnar 955905a36a2SIngo Molnar /* Reload gs selector with exception handling */ 956905a36a2SIngo Molnar /* edi: new selector */ 957905a36a2SIngo MolnarENTRY(native_load_gs_index) 958905a36a2SIngo Molnar pushfq 959905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI) 960905a36a2SIngo Molnar SWAPGS 961905a36a2SIngo Molnargs_change: 962905a36a2SIngo Molnar movl %edi,%gs 963905a36a2SIngo Molnar2: mfence /* workaround */ 964905a36a2SIngo Molnar SWAPGS 965905a36a2SIngo Molnar popfq 966905a36a2SIngo Molnar ret 967905a36a2SIngo MolnarEND(native_load_gs_index) 968905a36a2SIngo Molnar 969905a36a2SIngo Molnar _ASM_EXTABLE(gs_change,bad_gs) 970905a36a2SIngo Molnar .section .fixup,"ax" 971905a36a2SIngo Molnar /* running with kernelgs */ 972905a36a2SIngo Molnarbad_gs: 973905a36a2SIngo Molnar SWAPGS /* switch back to user gs */ 974905a36a2SIngo Molnar xorl %eax,%eax 975905a36a2SIngo Molnar movl %eax,%gs 976905a36a2SIngo Molnar jmp 2b 977905a36a2SIngo Molnar .previous 978905a36a2SIngo Molnar 979905a36a2SIngo Molnar/* Call softirq on interrupt stack. Interrupts are off. */ 980905a36a2SIngo MolnarENTRY(do_softirq_own_stack) 981905a36a2SIngo Molnar pushq %rbp 982905a36a2SIngo Molnar mov %rsp,%rbp 983905a36a2SIngo Molnar incl PER_CPU_VAR(irq_count) 984905a36a2SIngo Molnar cmove PER_CPU_VAR(irq_stack_ptr),%rsp 985905a36a2SIngo Molnar push %rbp # backlink for old unwinder 986905a36a2SIngo Molnar call __do_softirq 987905a36a2SIngo Molnar leaveq 988905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 989905a36a2SIngo Molnar ret 990905a36a2SIngo MolnarEND(do_softirq_own_stack) 991905a36a2SIngo Molnar 992905a36a2SIngo Molnar#ifdef CONFIG_XEN 993905a36a2SIngo Molnaridtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0 994905a36a2SIngo Molnar 995905a36a2SIngo Molnar/* 996905a36a2SIngo Molnar * A note on the "critical region" in our callback handler. 997905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring 998905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled 999905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before 1000905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still 1001905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack. 1002905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd 1003905a36a2SIngo Molnar * like to avoid the possibility. 1004905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an 1005905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current 1006905a36a2SIngo Molnar * activation and restart the handler using the previous one. 1007905a36a2SIngo Molnar */ 1008905a36a2SIngo MolnarENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs) 1009905a36a2SIngo Molnar/* 1010905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will 1011905a36a2SIngo Molnar * see the correct pointer to the pt_regs 1012905a36a2SIngo Molnar */ 1013905a36a2SIngo Molnar movq %rdi, %rsp # we don't return, adjust the stack frame 1014905a36a2SIngo Molnar11: incl PER_CPU_VAR(irq_count) 1015905a36a2SIngo Molnar movq %rsp,%rbp 1016905a36a2SIngo Molnar cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp 1017905a36a2SIngo Molnar pushq %rbp # backlink for old unwinder 1018905a36a2SIngo Molnar call xen_evtchn_do_upcall 1019905a36a2SIngo Molnar popq %rsp 1020905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 1021905a36a2SIngo Molnar#ifndef CONFIG_PREEMPT 1022905a36a2SIngo Molnar call xen_maybe_preempt_hcall 1023905a36a2SIngo Molnar#endif 1024905a36a2SIngo Molnar jmp error_exit 1025905a36a2SIngo MolnarEND(xen_do_hypervisor_callback) 1026905a36a2SIngo Molnar 1027905a36a2SIngo Molnar/* 1028905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes. 1029905a36a2SIngo Molnar * We get here for two reasons: 1030905a36a2SIngo Molnar * 1. Fault while reloading DS, ES, FS or GS 1031905a36a2SIngo Molnar * 2. Fault while executing IRET 1032905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment 1033905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others. 1034905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the 1035905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall 1036905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks. 1037905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register 1038905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1. 1039905a36a2SIngo Molnar */ 1040905a36a2SIngo MolnarENTRY(xen_failsafe_callback) 1041905a36a2SIngo Molnar movl %ds,%ecx 1042905a36a2SIngo Molnar cmpw %cx,0x10(%rsp) 1043905a36a2SIngo Molnar jne 1f 1044905a36a2SIngo Molnar movl %es,%ecx 1045905a36a2SIngo Molnar cmpw %cx,0x18(%rsp) 1046905a36a2SIngo Molnar jne 1f 1047905a36a2SIngo Molnar movl %fs,%ecx 1048905a36a2SIngo Molnar cmpw %cx,0x20(%rsp) 1049905a36a2SIngo Molnar jne 1f 1050905a36a2SIngo Molnar movl %gs,%ecx 1051905a36a2SIngo Molnar cmpw %cx,0x28(%rsp) 1052905a36a2SIngo Molnar jne 1f 1053905a36a2SIngo Molnar /* All segments match their saved values => Category 2 (Bad IRET). */ 1054905a36a2SIngo Molnar movq (%rsp),%rcx 1055905a36a2SIngo Molnar movq 8(%rsp),%r11 1056905a36a2SIngo Molnar addq $0x30,%rsp 1057905a36a2SIngo Molnar pushq $0 /* RIP */ 1058905a36a2SIngo Molnar pushq %r11 1059905a36a2SIngo Molnar pushq %rcx 1060905a36a2SIngo Molnar jmp general_protection 1061905a36a2SIngo Molnar1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */ 1062905a36a2SIngo Molnar movq (%rsp),%rcx 1063905a36a2SIngo Molnar movq 8(%rsp),%r11 1064905a36a2SIngo Molnar addq $0x30,%rsp 1065905a36a2SIngo Molnar pushq $-1 /* orig_ax = -1 => not a system call */ 1066905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 1067905a36a2SIngo Molnar SAVE_C_REGS 1068905a36a2SIngo Molnar SAVE_EXTRA_REGS 1069905a36a2SIngo Molnar jmp error_exit 1070905a36a2SIngo MolnarEND(xen_failsafe_callback) 1071905a36a2SIngo Molnar 1072905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 1073905a36a2SIngo Molnar xen_hvm_callback_vector xen_evtchn_do_upcall 1074905a36a2SIngo Molnar 1075905a36a2SIngo Molnar#endif /* CONFIG_XEN */ 1076905a36a2SIngo Molnar 1077905a36a2SIngo Molnar#if IS_ENABLED(CONFIG_HYPERV) 1078905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 1079905a36a2SIngo Molnar hyperv_callback_vector hyperv_vector_handler 1080905a36a2SIngo Molnar#endif /* CONFIG_HYPERV */ 1081905a36a2SIngo Molnar 1082905a36a2SIngo Molnaridtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK 1083905a36a2SIngo Molnaridtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK 1084905a36a2SIngo Molnaridtentry stack_segment do_stack_segment has_error_code=1 1085905a36a2SIngo Molnar#ifdef CONFIG_XEN 1086905a36a2SIngo Molnaridtentry xen_debug do_debug has_error_code=0 1087905a36a2SIngo Molnaridtentry xen_int3 do_int3 has_error_code=0 1088905a36a2SIngo Molnaridtentry xen_stack_segment do_stack_segment has_error_code=1 1089905a36a2SIngo Molnar#endif 1090905a36a2SIngo Molnaridtentry general_protection do_general_protection has_error_code=1 1091905a36a2SIngo Molnartrace_idtentry page_fault do_page_fault has_error_code=1 1092905a36a2SIngo Molnar#ifdef CONFIG_KVM_GUEST 1093905a36a2SIngo Molnaridtentry async_page_fault do_async_page_fault has_error_code=1 1094905a36a2SIngo Molnar#endif 1095905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE 1096905a36a2SIngo Molnaridtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip) 1097905a36a2SIngo Molnar#endif 1098905a36a2SIngo Molnar 1099905a36a2SIngo Molnar/* 1100905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed. 1101905a36a2SIngo Molnar * Use slow, but surefire "are we in kernel?" check. 1102905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise 1103905a36a2SIngo Molnar */ 1104905a36a2SIngo MolnarENTRY(paranoid_entry) 1105905a36a2SIngo Molnar cld 1106905a36a2SIngo Molnar SAVE_C_REGS 8 1107905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 1108905a36a2SIngo Molnar movl $1,%ebx 1109905a36a2SIngo Molnar movl $MSR_GS_BASE,%ecx 1110905a36a2SIngo Molnar rdmsr 1111905a36a2SIngo Molnar testl %edx,%edx 1112905a36a2SIngo Molnar js 1f /* negative -> in kernel */ 1113905a36a2SIngo Molnar SWAPGS 1114905a36a2SIngo Molnar xorl %ebx,%ebx 1115905a36a2SIngo Molnar1: ret 1116905a36a2SIngo MolnarEND(paranoid_entry) 1117905a36a2SIngo Molnar 1118905a36a2SIngo Molnar/* 1119905a36a2SIngo Molnar * "Paranoid" exit path from exception stack. This is invoked 1120905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came 1121905a36a2SIngo Molnar * from kernel space. 1122905a36a2SIngo Molnar * 1123905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early 1124905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would 1125905a36a2SIngo Molnar * be complicated. Fortunately, we there's no good reason 1126905a36a2SIngo Molnar * to try to handle preemption here. 1127905a36a2SIngo Molnar */ 1128905a36a2SIngo Molnar/* On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) */ 1129905a36a2SIngo MolnarENTRY(paranoid_exit) 1130905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 1131905a36a2SIngo Molnar TRACE_IRQS_OFF_DEBUG 1132905a36a2SIngo Molnar testl %ebx,%ebx /* swapgs needed? */ 1133905a36a2SIngo Molnar jnz paranoid_exit_no_swapgs 1134905a36a2SIngo Molnar TRACE_IRQS_IRETQ 1135905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1136905a36a2SIngo Molnar jmp paranoid_exit_restore 1137905a36a2SIngo Molnarparanoid_exit_no_swapgs: 1138905a36a2SIngo Molnar TRACE_IRQS_IRETQ_DEBUG 1139905a36a2SIngo Molnarparanoid_exit_restore: 1140905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1141905a36a2SIngo Molnar RESTORE_C_REGS 1142905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 8 1143905a36a2SIngo Molnar INTERRUPT_RETURN 1144905a36a2SIngo MolnarEND(paranoid_exit) 1145905a36a2SIngo Molnar 1146905a36a2SIngo Molnar/* 1147905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed. 1148905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise 1149905a36a2SIngo Molnar */ 1150905a36a2SIngo MolnarENTRY(error_entry) 1151905a36a2SIngo Molnar cld 1152905a36a2SIngo Molnar SAVE_C_REGS 8 1153905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 1154905a36a2SIngo Molnar xorl %ebx,%ebx 1155905a36a2SIngo Molnar testb $3, CS+8(%rsp) 1156905a36a2SIngo Molnar jz error_kernelspace 1157905a36a2SIngo Molnarerror_swapgs: 1158905a36a2SIngo Molnar SWAPGS 1159905a36a2SIngo Molnarerror_sti: 1160905a36a2SIngo Molnar TRACE_IRQS_OFF 1161905a36a2SIngo Molnar ret 1162905a36a2SIngo Molnar 1163905a36a2SIngo Molnar /* 1164905a36a2SIngo Molnar * There are two places in the kernel that can potentially fault with 1165905a36a2SIngo Molnar * usergs. Handle them here. B stepping K8s sometimes report a 1166905a36a2SIngo Molnar * truncated RIP for IRET exceptions returning to compat mode. Check 1167905a36a2SIngo Molnar * for these here too. 1168905a36a2SIngo Molnar */ 1169905a36a2SIngo Molnarerror_kernelspace: 1170905a36a2SIngo Molnar incl %ebx 1171905a36a2SIngo Molnar leaq native_irq_return_iret(%rip),%rcx 1172905a36a2SIngo Molnar cmpq %rcx,RIP+8(%rsp) 1173905a36a2SIngo Molnar je error_bad_iret 1174905a36a2SIngo Molnar movl %ecx,%eax /* zero extend */ 1175905a36a2SIngo Molnar cmpq %rax,RIP+8(%rsp) 1176905a36a2SIngo Molnar je bstep_iret 1177905a36a2SIngo Molnar cmpq $gs_change,RIP+8(%rsp) 1178905a36a2SIngo Molnar je error_swapgs 1179905a36a2SIngo Molnar jmp error_sti 1180905a36a2SIngo Molnar 1181905a36a2SIngo Molnarbstep_iret: 1182905a36a2SIngo Molnar /* Fix truncated RIP */ 1183905a36a2SIngo Molnar movq %rcx,RIP+8(%rsp) 1184905a36a2SIngo Molnar /* fall through */ 1185905a36a2SIngo Molnar 1186905a36a2SIngo Molnarerror_bad_iret: 1187905a36a2SIngo Molnar SWAPGS 1188905a36a2SIngo Molnar mov %rsp,%rdi 1189905a36a2SIngo Molnar call fixup_bad_iret 1190905a36a2SIngo Molnar mov %rax,%rsp 1191905a36a2SIngo Molnar decl %ebx /* Return to usergs */ 1192905a36a2SIngo Molnar jmp error_sti 1193905a36a2SIngo MolnarEND(error_entry) 1194905a36a2SIngo Molnar 1195905a36a2SIngo Molnar 1196905a36a2SIngo Molnar/* On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) */ 1197905a36a2SIngo MolnarENTRY(error_exit) 1198905a36a2SIngo Molnar movl %ebx,%eax 1199905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1200905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 1201905a36a2SIngo Molnar TRACE_IRQS_OFF 1202905a36a2SIngo Molnar testl %eax,%eax 1203905a36a2SIngo Molnar jnz retint_kernel 1204905a36a2SIngo Molnar jmp retint_user 1205905a36a2SIngo MolnarEND(error_exit) 1206905a36a2SIngo Molnar 1207905a36a2SIngo Molnar/* Runs on exception stack */ 1208905a36a2SIngo MolnarENTRY(nmi) 1209905a36a2SIngo Molnar PARAVIRT_ADJUST_EXCEPTION_FRAME 1210905a36a2SIngo Molnar /* 1211905a36a2SIngo Molnar * We allow breakpoints in NMIs. If a breakpoint occurs, then 1212905a36a2SIngo Molnar * the iretq it performs will take us out of NMI context. 1213905a36a2SIngo Molnar * This means that we can have nested NMIs where the next 1214905a36a2SIngo Molnar * NMI is using the top of the stack of the previous NMI. We 1215905a36a2SIngo Molnar * can't let it execute because the nested NMI will corrupt the 1216905a36a2SIngo Molnar * stack of the previous NMI. NMI handlers are not re-entrant 1217905a36a2SIngo Molnar * anyway. 1218905a36a2SIngo Molnar * 1219905a36a2SIngo Molnar * To handle this case we do the following: 1220905a36a2SIngo Molnar * Check the a special location on the stack that contains 1221905a36a2SIngo Molnar * a variable that is set when NMIs are executing. 1222905a36a2SIngo Molnar * The interrupted task's stack is also checked to see if it 1223905a36a2SIngo Molnar * is an NMI stack. 1224905a36a2SIngo Molnar * If the variable is not set and the stack is not the NMI 1225905a36a2SIngo Molnar * stack then: 1226905a36a2SIngo Molnar * o Set the special variable on the stack 1227905a36a2SIngo Molnar * o Copy the interrupt frame into a "saved" location on the stack 1228905a36a2SIngo Molnar * o Copy the interrupt frame into a "copy" location on the stack 1229905a36a2SIngo Molnar * o Continue processing the NMI 1230905a36a2SIngo Molnar * If the variable is set or the previous stack is the NMI stack: 1231905a36a2SIngo Molnar * o Modify the "copy" location to jump to the repeate_nmi 1232905a36a2SIngo Molnar * o return back to the first NMI 1233905a36a2SIngo Molnar * 1234905a36a2SIngo Molnar * Now on exit of the first NMI, we first clear the stack variable 1235905a36a2SIngo Molnar * The NMI stack will tell any nested NMIs at that point that it is 1236905a36a2SIngo Molnar * nested. Then we pop the stack normally with iret, and if there was 1237905a36a2SIngo Molnar * a nested NMI that updated the copy interrupt stack frame, a 1238905a36a2SIngo Molnar * jump will be made to the repeat_nmi code that will handle the second 1239905a36a2SIngo Molnar * NMI. 1240905a36a2SIngo Molnar */ 1241905a36a2SIngo Molnar 1242905a36a2SIngo Molnar /* Use %rdx as our temp variable throughout */ 1243905a36a2SIngo Molnar pushq %rdx 1244905a36a2SIngo Molnar 1245905a36a2SIngo Molnar /* 1246905a36a2SIngo Molnar * If %cs was not the kernel segment, then the NMI triggered in user 1247905a36a2SIngo Molnar * space, which means it is definitely not nested. 1248905a36a2SIngo Molnar */ 1249905a36a2SIngo Molnar cmpl $__KERNEL_CS, 16(%rsp) 1250905a36a2SIngo Molnar jne first_nmi 1251905a36a2SIngo Molnar 1252905a36a2SIngo Molnar /* 1253905a36a2SIngo Molnar * Check the special variable on the stack to see if NMIs are 1254905a36a2SIngo Molnar * executing. 1255905a36a2SIngo Molnar */ 1256905a36a2SIngo Molnar cmpl $1, -8(%rsp) 1257905a36a2SIngo Molnar je nested_nmi 1258905a36a2SIngo Molnar 1259905a36a2SIngo Molnar /* 1260905a36a2SIngo Molnar * Now test if the previous stack was an NMI stack. 1261905a36a2SIngo Molnar * We need the double check. We check the NMI stack to satisfy the 1262905a36a2SIngo Molnar * race when the first NMI clears the variable before returning. 1263905a36a2SIngo Molnar * We check the variable because the first NMI could be in a 1264905a36a2SIngo Molnar * breakpoint routine using a breakpoint stack. 1265905a36a2SIngo Molnar */ 1266905a36a2SIngo Molnar lea 6*8(%rsp), %rdx 1267905a36a2SIngo Molnar /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */ 1268905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1269905a36a2SIngo Molnar /* If the stack pointer is above the NMI stack, this is a normal NMI */ 1270905a36a2SIngo Molnar ja first_nmi 1271905a36a2SIngo Molnar subq $EXCEPTION_STKSZ, %rdx 1272905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1273905a36a2SIngo Molnar /* If it is below the NMI stack, it is a normal NMI */ 1274905a36a2SIngo Molnar jb first_nmi 1275905a36a2SIngo Molnar /* Ah, it is within the NMI stack, treat it as nested */ 1276905a36a2SIngo Molnar 1277905a36a2SIngo Molnarnested_nmi: 1278905a36a2SIngo Molnar /* 1279905a36a2SIngo Molnar * Do nothing if we interrupted the fixup in repeat_nmi. 1280905a36a2SIngo Molnar * It's about to repeat the NMI handler, so we are fine 1281905a36a2SIngo Molnar * with ignoring this one. 1282905a36a2SIngo Molnar */ 1283905a36a2SIngo Molnar movq $repeat_nmi, %rdx 1284905a36a2SIngo Molnar cmpq 8(%rsp), %rdx 1285905a36a2SIngo Molnar ja 1f 1286905a36a2SIngo Molnar movq $end_repeat_nmi, %rdx 1287905a36a2SIngo Molnar cmpq 8(%rsp), %rdx 1288905a36a2SIngo Molnar ja nested_nmi_out 1289905a36a2SIngo Molnar 1290905a36a2SIngo Molnar1: 1291905a36a2SIngo Molnar /* Set up the interrupted NMIs stack to jump to repeat_nmi */ 1292905a36a2SIngo Molnar leaq -1*8(%rsp), %rdx 1293905a36a2SIngo Molnar movq %rdx, %rsp 1294905a36a2SIngo Molnar leaq -10*8(%rsp), %rdx 1295905a36a2SIngo Molnar pushq $__KERNEL_DS 1296905a36a2SIngo Molnar pushq %rdx 1297905a36a2SIngo Molnar pushfq 1298905a36a2SIngo Molnar pushq $__KERNEL_CS 1299905a36a2SIngo Molnar pushq $repeat_nmi 1300905a36a2SIngo Molnar 1301905a36a2SIngo Molnar /* Put stack back */ 1302905a36a2SIngo Molnar addq $(6*8), %rsp 1303905a36a2SIngo Molnar 1304905a36a2SIngo Molnarnested_nmi_out: 1305905a36a2SIngo Molnar popq %rdx 1306905a36a2SIngo Molnar 1307905a36a2SIngo Molnar /* No need to check faults here */ 1308905a36a2SIngo Molnar INTERRUPT_RETURN 1309905a36a2SIngo Molnar 1310905a36a2SIngo Molnarfirst_nmi: 1311905a36a2SIngo Molnar /* 1312905a36a2SIngo Molnar * Because nested NMIs will use the pushed location that we 1313905a36a2SIngo Molnar * stored in rdx, we must keep that space available. 1314905a36a2SIngo Molnar * Here's what our stack frame will look like: 1315905a36a2SIngo Molnar * +-------------------------+ 1316905a36a2SIngo Molnar * | original SS | 1317905a36a2SIngo Molnar * | original Return RSP | 1318905a36a2SIngo Molnar * | original RFLAGS | 1319905a36a2SIngo Molnar * | original CS | 1320905a36a2SIngo Molnar * | original RIP | 1321905a36a2SIngo Molnar * +-------------------------+ 1322905a36a2SIngo Molnar * | temp storage for rdx | 1323905a36a2SIngo Molnar * +-------------------------+ 1324905a36a2SIngo Molnar * | NMI executing variable | 1325905a36a2SIngo Molnar * +-------------------------+ 1326905a36a2SIngo Molnar * | copied SS | 1327905a36a2SIngo Molnar * | copied Return RSP | 1328905a36a2SIngo Molnar * | copied RFLAGS | 1329905a36a2SIngo Molnar * | copied CS | 1330905a36a2SIngo Molnar * | copied RIP | 1331905a36a2SIngo Molnar * +-------------------------+ 1332905a36a2SIngo Molnar * | Saved SS | 1333905a36a2SIngo Molnar * | Saved Return RSP | 1334905a36a2SIngo Molnar * | Saved RFLAGS | 1335905a36a2SIngo Molnar * | Saved CS | 1336905a36a2SIngo Molnar * | Saved RIP | 1337905a36a2SIngo Molnar * +-------------------------+ 1338905a36a2SIngo Molnar * | pt_regs | 1339905a36a2SIngo Molnar * +-------------------------+ 1340905a36a2SIngo Molnar * 1341905a36a2SIngo Molnar * The saved stack frame is used to fix up the copied stack frame 1342905a36a2SIngo Molnar * that a nested NMI may change to make the interrupted NMI iret jump 1343905a36a2SIngo Molnar * to the repeat_nmi. The original stack frame and the temp storage 1344905a36a2SIngo Molnar * is also used by nested NMIs and can not be trusted on exit. 1345905a36a2SIngo Molnar */ 1346905a36a2SIngo Molnar /* Do not pop rdx, nested NMIs will corrupt that part of the stack */ 1347905a36a2SIngo Molnar movq (%rsp), %rdx 1348905a36a2SIngo Molnar 1349905a36a2SIngo Molnar /* Set the NMI executing variable on the stack. */ 1350905a36a2SIngo Molnar pushq $1 1351905a36a2SIngo Molnar 1352905a36a2SIngo Molnar /* 1353905a36a2SIngo Molnar * Leave room for the "copied" frame 1354905a36a2SIngo Molnar */ 1355905a36a2SIngo Molnar subq $(5*8), %rsp 1356905a36a2SIngo Molnar 1357905a36a2SIngo Molnar /* Copy the stack frame to the Saved frame */ 1358905a36a2SIngo Molnar .rept 5 1359905a36a2SIngo Molnar pushq 11*8(%rsp) 1360905a36a2SIngo Molnar .endr 1361905a36a2SIngo Molnar 1362905a36a2SIngo Molnar /* Everything up to here is safe from nested NMIs */ 1363905a36a2SIngo Molnar 1364905a36a2SIngo Molnar /* 1365905a36a2SIngo Molnar * If there was a nested NMI, the first NMI's iret will return 1366905a36a2SIngo Molnar * here. But NMIs are still enabled and we can take another 1367905a36a2SIngo Molnar * nested NMI. The nested NMI checks the interrupted RIP to see 1368905a36a2SIngo Molnar * if it is between repeat_nmi and end_repeat_nmi, and if so 1369905a36a2SIngo Molnar * it will just return, as we are about to repeat an NMI anyway. 1370905a36a2SIngo Molnar * This makes it safe to copy to the stack frame that a nested 1371905a36a2SIngo Molnar * NMI will update. 1372905a36a2SIngo Molnar */ 1373905a36a2SIngo Molnarrepeat_nmi: 1374905a36a2SIngo Molnar /* 1375905a36a2SIngo Molnar * Update the stack variable to say we are still in NMI (the update 1376905a36a2SIngo Molnar * is benign for the non-repeat case, where 1 was pushed just above 1377905a36a2SIngo Molnar * to this very stack slot). 1378905a36a2SIngo Molnar */ 1379905a36a2SIngo Molnar movq $1, 10*8(%rsp) 1380905a36a2SIngo Molnar 1381905a36a2SIngo Molnar /* Make another copy, this one may be modified by nested NMIs */ 1382905a36a2SIngo Molnar addq $(10*8), %rsp 1383905a36a2SIngo Molnar .rept 5 1384905a36a2SIngo Molnar pushq -6*8(%rsp) 1385905a36a2SIngo Molnar .endr 1386905a36a2SIngo Molnar subq $(5*8), %rsp 1387905a36a2SIngo Molnarend_repeat_nmi: 1388905a36a2SIngo Molnar 1389905a36a2SIngo Molnar /* 1390905a36a2SIngo Molnar * Everything below this point can be preempted by a nested 1391905a36a2SIngo Molnar * NMI if the first NMI took an exception and reset our iret stack 1392905a36a2SIngo Molnar * so that we repeat another NMI. 1393905a36a2SIngo Molnar */ 1394905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 1395905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 1396905a36a2SIngo Molnar 1397905a36a2SIngo Molnar /* 1398905a36a2SIngo Molnar * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit 1399905a36a2SIngo Molnar * as we should not be calling schedule in NMI context. 1400905a36a2SIngo Molnar * Even with normal interrupts enabled. An NMI should not be 1401905a36a2SIngo Molnar * setting NEED_RESCHED or anything that normal interrupts and 1402905a36a2SIngo Molnar * exceptions might do. 1403905a36a2SIngo Molnar */ 1404905a36a2SIngo Molnar call paranoid_entry 1405905a36a2SIngo Molnar 1406905a36a2SIngo Molnar /* 1407905a36a2SIngo Molnar * Save off the CR2 register. If we take a page fault in the NMI then 1408905a36a2SIngo Molnar * it could corrupt the CR2 value. If the NMI preempts a page fault 1409905a36a2SIngo Molnar * handler before it was able to read the CR2 register, and then the 1410905a36a2SIngo Molnar * NMI itself takes a page fault, the page fault that was preempted 1411905a36a2SIngo Molnar * will read the information from the NMI page fault and not the 1412905a36a2SIngo Molnar * origin fault. Save it off and restore it if it changes. 1413905a36a2SIngo Molnar * Use the r12 callee-saved register. 1414905a36a2SIngo Molnar */ 1415905a36a2SIngo Molnar movq %cr2, %r12 1416905a36a2SIngo Molnar 1417905a36a2SIngo Molnar /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */ 1418905a36a2SIngo Molnar movq %rsp,%rdi 1419905a36a2SIngo Molnar movq $-1,%rsi 1420905a36a2SIngo Molnar call do_nmi 1421905a36a2SIngo Molnar 1422905a36a2SIngo Molnar /* Did the NMI take a page fault? Restore cr2 if it did */ 1423905a36a2SIngo Molnar movq %cr2, %rcx 1424905a36a2SIngo Molnar cmpq %rcx, %r12 1425905a36a2SIngo Molnar je 1f 1426905a36a2SIngo Molnar movq %r12, %cr2 1427905a36a2SIngo Molnar1: 1428905a36a2SIngo Molnar testl %ebx,%ebx /* swapgs needed? */ 1429905a36a2SIngo Molnar jnz nmi_restore 1430905a36a2SIngo Molnarnmi_swapgs: 1431905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1432905a36a2SIngo Molnarnmi_restore: 1433905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1434905a36a2SIngo Molnar RESTORE_C_REGS 1435905a36a2SIngo Molnar /* Pop the extra iret frame at once */ 1436905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 6*8 1437905a36a2SIngo Molnar 1438905a36a2SIngo Molnar /* Clear the NMI executing stack variable */ 1439905a36a2SIngo Molnar movq $0, 5*8(%rsp) 14405ca6f70fSAndy Lutomirski INTERRUPT_RETURN 1441905a36a2SIngo MolnarEND(nmi) 1442905a36a2SIngo Molnar 1443905a36a2SIngo MolnarENTRY(ignore_sysret) 1444905a36a2SIngo Molnar mov $-ENOSYS,%eax 1445905a36a2SIngo Molnar sysret 1446905a36a2SIngo MolnarEND(ignore_sysret) 1447905a36a2SIngo Molnar 1448