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> 74d732138SIngo Molnar * 8905a36a2SIngo Molnar * entry.S contains the system-call and fault low-level handling routines. 9905a36a2SIngo Molnar * 10905a36a2SIngo Molnar * Some of this is documented in Documentation/x86/entry_64.txt 11905a36a2SIngo Molnar * 12905a36a2SIngo Molnar * A note on terminology: 13905a36a2SIngo Molnar * - iret frame: Architecture defined interrupt frame from SS to RIP 14905a36a2SIngo Molnar * at the top of the kernel process stack. 15905a36a2SIngo Molnar * 16905a36a2SIngo Molnar * Some macro usage: 174d732138SIngo Molnar * - ENTRY/END: Define functions in the symbol table. 184d732138SIngo Molnar * - TRACE_IRQ_*: Trace hardirq state for lock debugging. 194d732138SIngo Molnar * - idtentry: Define exception entry points. 20905a36a2SIngo Molnar */ 21905a36a2SIngo Molnar#include <linux/linkage.h> 22905a36a2SIngo Molnar#include <asm/segment.h> 23905a36a2SIngo Molnar#include <asm/cache.h> 24905a36a2SIngo Molnar#include <asm/errno.h> 25d36f9479SIngo Molnar#include "calling.h" 26905a36a2SIngo Molnar#include <asm/asm-offsets.h> 27905a36a2SIngo Molnar#include <asm/msr.h> 28905a36a2SIngo Molnar#include <asm/unistd.h> 29905a36a2SIngo Molnar#include <asm/thread_info.h> 30905a36a2SIngo Molnar#include <asm/hw_irq.h> 31905a36a2SIngo Molnar#include <asm/page_types.h> 32905a36a2SIngo Molnar#include <asm/irqflags.h> 33905a36a2SIngo Molnar#include <asm/paravirt.h> 34905a36a2SIngo Molnar#include <asm/percpu.h> 35905a36a2SIngo Molnar#include <asm/asm.h> 36905a36a2SIngo Molnar#include <asm/smap.h> 37905a36a2SIngo Molnar#include <asm/pgtable_types.h> 38905a36a2SIngo Molnar#include <linux/err.h> 39905a36a2SIngo Molnar 40905a36a2SIngo Molnar/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */ 41905a36a2SIngo Molnar#include <linux/elf-em.h> 42905a36a2SIngo Molnar#define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE) 43905a36a2SIngo Molnar#define __AUDIT_ARCH_64BIT 0x80000000 44905a36a2SIngo Molnar#define __AUDIT_ARCH_LE 0x40000000 45905a36a2SIngo Molnar 46905a36a2SIngo Molnar.code64 47905a36a2SIngo Molnar.section .entry.text, "ax" 48905a36a2SIngo Molnar 49905a36a2SIngo Molnar#ifdef CONFIG_PARAVIRT 50905a36a2SIngo MolnarENTRY(native_usergs_sysret64) 51905a36a2SIngo Molnar swapgs 52905a36a2SIngo Molnar sysretq 53905a36a2SIngo MolnarENDPROC(native_usergs_sysret64) 54905a36a2SIngo Molnar#endif /* CONFIG_PARAVIRT */ 55905a36a2SIngo Molnar 56905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ 57905a36a2SIngo Molnar#ifdef CONFIG_TRACE_IRQFLAGS 58905a36a2SIngo Molnar bt $9, EFLAGS(%rsp) /* interrupts off? */ 59905a36a2SIngo Molnar jnc 1f 60905a36a2SIngo Molnar TRACE_IRQS_ON 61905a36a2SIngo Molnar1: 62905a36a2SIngo Molnar#endif 63905a36a2SIngo Molnar.endm 64905a36a2SIngo Molnar 65905a36a2SIngo Molnar/* 66905a36a2SIngo Molnar * When dynamic function tracer is enabled it will add a breakpoint 67905a36a2SIngo Molnar * to all locations that it is about to modify, sync CPUs, update 68905a36a2SIngo Molnar * all the code, sync CPUs, then remove the breakpoints. In this time 69905a36a2SIngo Molnar * if lockdep is enabled, it might jump back into the debug handler 70905a36a2SIngo Molnar * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF). 71905a36a2SIngo Molnar * 72905a36a2SIngo Molnar * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to 73905a36a2SIngo Molnar * make sure the stack pointer does not get reset back to the top 74905a36a2SIngo Molnar * of the debug stack, and instead just reuses the current stack. 75905a36a2SIngo Molnar */ 76905a36a2SIngo Molnar#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS) 77905a36a2SIngo Molnar 78905a36a2SIngo Molnar.macro TRACE_IRQS_OFF_DEBUG 79905a36a2SIngo Molnar call debug_stack_set_zero 80905a36a2SIngo Molnar TRACE_IRQS_OFF 81905a36a2SIngo Molnar call debug_stack_reset 82905a36a2SIngo Molnar.endm 83905a36a2SIngo Molnar 84905a36a2SIngo Molnar.macro TRACE_IRQS_ON_DEBUG 85905a36a2SIngo Molnar call debug_stack_set_zero 86905a36a2SIngo Molnar TRACE_IRQS_ON 87905a36a2SIngo Molnar call debug_stack_reset 88905a36a2SIngo Molnar.endm 89905a36a2SIngo Molnar 90905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ_DEBUG 91905a36a2SIngo Molnar bt $9, EFLAGS(%rsp) /* interrupts off? */ 92905a36a2SIngo Molnar jnc 1f 93905a36a2SIngo Molnar TRACE_IRQS_ON_DEBUG 94905a36a2SIngo Molnar1: 95905a36a2SIngo Molnar.endm 96905a36a2SIngo Molnar 97905a36a2SIngo Molnar#else 98905a36a2SIngo Molnar# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF 99905a36a2SIngo Molnar# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON 100905a36a2SIngo Molnar# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ 101905a36a2SIngo Molnar#endif 102905a36a2SIngo Molnar 103905a36a2SIngo Molnar/* 1044d732138SIngo Molnar * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers. 105905a36a2SIngo Molnar * 106fda57b22SAndy Lutomirski * This is the only entry point used for 64-bit system calls. The 107fda57b22SAndy Lutomirski * hardware interface is reasonably well designed and the register to 108fda57b22SAndy Lutomirski * argument mapping Linux uses fits well with the registers that are 109fda57b22SAndy Lutomirski * available when SYSCALL is used. 110fda57b22SAndy Lutomirski * 111fda57b22SAndy Lutomirski * SYSCALL instructions can be found inlined in libc implementations as 112fda57b22SAndy Lutomirski * well as some other programs and libraries. There are also a handful 113fda57b22SAndy Lutomirski * of SYSCALL instructions in the vDSO used, for example, as a 114fda57b22SAndy Lutomirski * clock_gettimeofday fallback. 115fda57b22SAndy Lutomirski * 1164d732138SIngo Molnar * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11, 117905a36a2SIngo Molnar * then loads new ss, cs, and rip from previously programmed MSRs. 118905a36a2SIngo Molnar * rflags gets masked by a value from another MSR (so CLD and CLAC 119905a36a2SIngo Molnar * are not needed). SYSCALL does not save anything on the stack 120905a36a2SIngo Molnar * and does not change rsp. 121905a36a2SIngo Molnar * 122905a36a2SIngo Molnar * Registers on entry: 123905a36a2SIngo Molnar * rax system call number 124905a36a2SIngo Molnar * rcx return address 125905a36a2SIngo Molnar * r11 saved rflags (note: r11 is callee-clobbered register in C ABI) 126905a36a2SIngo Molnar * rdi arg0 127905a36a2SIngo Molnar * rsi arg1 128905a36a2SIngo Molnar * rdx arg2 129905a36a2SIngo Molnar * r10 arg3 (needs to be moved to rcx to conform to C ABI) 130905a36a2SIngo Molnar * r8 arg4 131905a36a2SIngo Molnar * r9 arg5 132905a36a2SIngo Molnar * (note: r12-r15, rbp, rbx are callee-preserved in C ABI) 133905a36a2SIngo Molnar * 134905a36a2SIngo Molnar * Only called from user space. 135905a36a2SIngo Molnar * 136905a36a2SIngo Molnar * When user can change pt_regs->foo always force IRET. That is because 137905a36a2SIngo Molnar * it deals with uncanonical addresses better. SYSRET has trouble 138905a36a2SIngo Molnar * with them due to bugs in both AMD and Intel CPUs. 139905a36a2SIngo Molnar */ 140905a36a2SIngo Molnar 141b2502b41SIngo MolnarENTRY(entry_SYSCALL_64) 142905a36a2SIngo Molnar /* 143905a36a2SIngo Molnar * Interrupts are off on entry. 144905a36a2SIngo Molnar * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON, 145905a36a2SIngo Molnar * it is too small to ever cause noticeable irq latency. 146905a36a2SIngo Molnar */ 147905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 148905a36a2SIngo Molnar /* 149905a36a2SIngo Molnar * A hypervisor implementation might want to use a label 150905a36a2SIngo Molnar * after the swapgs, so that it can do the swapgs 151905a36a2SIngo Molnar * for the guest and jump here on syscall. 152905a36a2SIngo Molnar */ 153b2502b41SIngo MolnarGLOBAL(entry_SYSCALL_64_after_swapgs) 154905a36a2SIngo Molnar 155905a36a2SIngo Molnar movq %rsp, PER_CPU_VAR(rsp_scratch) 156905a36a2SIngo Molnar movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 157905a36a2SIngo Molnar 1581e423bffSAndy Lutomirski TRACE_IRQS_OFF 1591e423bffSAndy Lutomirski 160905a36a2SIngo Molnar /* Construct struct pt_regs on stack */ 161905a36a2SIngo Molnar pushq $__USER_DS /* pt_regs->ss */ 162905a36a2SIngo Molnar pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */ 163905a36a2SIngo Molnar pushq %r11 /* pt_regs->flags */ 164905a36a2SIngo Molnar pushq $__USER_CS /* pt_regs->cs */ 165905a36a2SIngo Molnar pushq %rcx /* pt_regs->ip */ 166905a36a2SIngo Molnar pushq %rax /* pt_regs->orig_ax */ 167905a36a2SIngo Molnar pushq %rdi /* pt_regs->di */ 168905a36a2SIngo Molnar pushq %rsi /* pt_regs->si */ 169905a36a2SIngo Molnar pushq %rdx /* pt_regs->dx */ 170905a36a2SIngo Molnar pushq %rcx /* pt_regs->cx */ 171905a36a2SIngo Molnar pushq $-ENOSYS /* pt_regs->ax */ 172905a36a2SIngo Molnar pushq %r8 /* pt_regs->r8 */ 173905a36a2SIngo Molnar pushq %r9 /* pt_regs->r9 */ 174905a36a2SIngo Molnar pushq %r10 /* pt_regs->r10 */ 175905a36a2SIngo Molnar pushq %r11 /* pt_regs->r11 */ 176905a36a2SIngo Molnar sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */ 177905a36a2SIngo Molnar 1781e423bffSAndy Lutomirski /* 1791e423bffSAndy Lutomirski * If we need to do entry work or if we guess we'll need to do 1801e423bffSAndy Lutomirski * exit work, go straight to the slow path. 1811e423bffSAndy Lutomirski */ 182*15f4eae7SAndy Lutomirski movq PER_CPU_VAR(current_task), %r11 183*15f4eae7SAndy Lutomirski testl $_TIF_WORK_SYSCALL_ENTRY|_TIF_ALLWORK_MASK, TASK_TI_flags(%r11) 1841e423bffSAndy Lutomirski jnz entry_SYSCALL64_slow_path 1851e423bffSAndy Lutomirski 186b2502b41SIngo Molnarentry_SYSCALL_64_fastpath: 1871e423bffSAndy Lutomirski /* 1881e423bffSAndy Lutomirski * Easy case: enable interrupts and issue the syscall. If the syscall 1891e423bffSAndy Lutomirski * needs pt_regs, we'll call a stub that disables interrupts again 1901e423bffSAndy Lutomirski * and jumps to the slow path. 1911e423bffSAndy Lutomirski */ 1921e423bffSAndy Lutomirski TRACE_IRQS_ON 1931e423bffSAndy Lutomirski ENABLE_INTERRUPTS(CLBR_NONE) 194905a36a2SIngo Molnar#if __SYSCALL_MASK == ~0 195905a36a2SIngo Molnar cmpq $__NR_syscall_max, %rax 196905a36a2SIngo Molnar#else 197905a36a2SIngo Molnar andl $__SYSCALL_MASK, %eax 198905a36a2SIngo Molnar cmpl $__NR_syscall_max, %eax 199905a36a2SIngo Molnar#endif 200905a36a2SIngo Molnar ja 1f /* return -ENOSYS (already in pt_regs->ax) */ 201905a36a2SIngo Molnar movq %r10, %rcx 202302f5b26SAndy Lutomirski 203302f5b26SAndy Lutomirski /* 204302f5b26SAndy Lutomirski * This call instruction is handled specially in stub_ptregs_64. 205b7765086SAndy Lutomirski * It might end up jumping to the slow path. If it jumps, RAX 206b7765086SAndy Lutomirski * and all argument registers are clobbered. 207302f5b26SAndy Lutomirski */ 208905a36a2SIngo Molnar call *sys_call_table(, %rax, 8) 209302f5b26SAndy Lutomirski.Lentry_SYSCALL_64_after_fastpath_call: 210302f5b26SAndy Lutomirski 211905a36a2SIngo Molnar movq %rax, RAX(%rsp) 212905a36a2SIngo Molnar1: 2131e423bffSAndy Lutomirski 214905a36a2SIngo Molnar /* 2151e423bffSAndy Lutomirski * If we get here, then we know that pt_regs is clean for SYSRET64. 2161e423bffSAndy Lutomirski * If we see that no exit work is required (which we are required 2171e423bffSAndy Lutomirski * to check with IRQs off), then we can go straight to SYSRET64. 218905a36a2SIngo Molnar */ 219905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 2201e423bffSAndy Lutomirski TRACE_IRQS_OFF 221*15f4eae7SAndy Lutomirski movq PER_CPU_VAR(current_task), %r11 222*15f4eae7SAndy Lutomirski testl $_TIF_ALLWORK_MASK, TASK_TI_flags(%r11) 2231e423bffSAndy Lutomirski jnz 1f 224905a36a2SIngo Molnar 2251e423bffSAndy Lutomirski LOCKDEP_SYS_EXIT 2261e423bffSAndy Lutomirski TRACE_IRQS_ON /* user mode is traced as IRQs on */ 227eb2a54c3SAndy Lutomirski movq RIP(%rsp), %rcx 228eb2a54c3SAndy Lutomirski movq EFLAGS(%rsp), %r11 229eb2a54c3SAndy Lutomirski RESTORE_C_REGS_EXCEPT_RCX_R11 230905a36a2SIngo Molnar movq RSP(%rsp), %rsp 231905a36a2SIngo Molnar USERGS_SYSRET64 232905a36a2SIngo Molnar 2331e423bffSAndy Lutomirski1: 2341e423bffSAndy Lutomirski /* 2351e423bffSAndy Lutomirski * The fast path looked good when we started, but something changed 2361e423bffSAndy Lutomirski * along the way and we need to switch to the slow path. Calling 2371e423bffSAndy Lutomirski * raise(3) will trigger this, for example. IRQs are off. 2381e423bffSAndy Lutomirski */ 23929ea1b25SAndy Lutomirski TRACE_IRQS_ON 24029ea1b25SAndy Lutomirski ENABLE_INTERRUPTS(CLBR_NONE) 241905a36a2SIngo Molnar SAVE_EXTRA_REGS 24229ea1b25SAndy Lutomirski movq %rsp, %rdi 24329ea1b25SAndy Lutomirski call syscall_return_slowpath /* returns with IRQs disabled */ 2441e423bffSAndy Lutomirski jmp return_from_SYSCALL_64 2451e423bffSAndy Lutomirski 2461e423bffSAndy Lutomirskientry_SYSCALL64_slow_path: 2471e423bffSAndy Lutomirski /* IRQs are off. */ 2481e423bffSAndy Lutomirski SAVE_EXTRA_REGS 2491e423bffSAndy Lutomirski movq %rsp, %rdi 2501e423bffSAndy Lutomirski call do_syscall_64 /* returns with IRQs disabled */ 2511e423bffSAndy Lutomirski 2521e423bffSAndy Lutomirskireturn_from_SYSCALL_64: 253905a36a2SIngo Molnar RESTORE_EXTRA_REGS 25429ea1b25SAndy Lutomirski TRACE_IRQS_IRETQ /* we're about to change IF */ 255905a36a2SIngo Molnar 256905a36a2SIngo Molnar /* 257905a36a2SIngo Molnar * Try to use SYSRET instead of IRET if we're returning to 258905a36a2SIngo Molnar * a completely clean 64-bit userspace context. 259905a36a2SIngo Molnar */ 260905a36a2SIngo Molnar movq RCX(%rsp), %rcx 261905a36a2SIngo Molnar movq RIP(%rsp), %r11 262905a36a2SIngo Molnar cmpq %rcx, %r11 /* RCX == RIP */ 263905a36a2SIngo Molnar jne opportunistic_sysret_failed 264905a36a2SIngo Molnar 265905a36a2SIngo Molnar /* 266905a36a2SIngo Molnar * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP 267905a36a2SIngo Molnar * in kernel space. This essentially lets the user take over 268905a36a2SIngo Molnar * the kernel, since userspace controls RSP. 269905a36a2SIngo Molnar * 270905a36a2SIngo Molnar * If width of "canonical tail" ever becomes variable, this will need 271905a36a2SIngo Molnar * to be updated to remain correct on both old and new CPUs. 272905a36a2SIngo Molnar */ 273905a36a2SIngo Molnar .ifne __VIRTUAL_MASK_SHIFT - 47 274905a36a2SIngo Molnar .error "virtual address width changed -- SYSRET checks need update" 275905a36a2SIngo Molnar .endif 2764d732138SIngo Molnar 277905a36a2SIngo Molnar /* Change top 16 bits to be the sign-extension of 47th bit */ 278905a36a2SIngo Molnar shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 279905a36a2SIngo Molnar sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 2804d732138SIngo Molnar 281905a36a2SIngo Molnar /* If this changed %rcx, it was not canonical */ 282905a36a2SIngo Molnar cmpq %rcx, %r11 283905a36a2SIngo Molnar jne opportunistic_sysret_failed 284905a36a2SIngo Molnar 285905a36a2SIngo Molnar cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */ 286905a36a2SIngo Molnar jne opportunistic_sysret_failed 287905a36a2SIngo Molnar 288905a36a2SIngo Molnar movq R11(%rsp), %r11 289905a36a2SIngo Molnar cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */ 290905a36a2SIngo Molnar jne opportunistic_sysret_failed 291905a36a2SIngo Molnar 292905a36a2SIngo Molnar /* 2933e035305SBorislav Petkov * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot 2943e035305SBorislav Petkov * restore RF properly. If the slowpath sets it for whatever reason, we 2953e035305SBorislav Petkov * need to restore it correctly. 2963e035305SBorislav Petkov * 2973e035305SBorislav Petkov * SYSRET can restore TF, but unlike IRET, restoring TF results in a 2983e035305SBorislav Petkov * trap from userspace immediately after SYSRET. This would cause an 2993e035305SBorislav Petkov * infinite loop whenever #DB happens with register state that satisfies 3003e035305SBorislav Petkov * the opportunistic SYSRET conditions. For example, single-stepping 3013e035305SBorislav Petkov * this user code: 302905a36a2SIngo Molnar * 303905a36a2SIngo Molnar * movq $stuck_here, %rcx 304905a36a2SIngo Molnar * pushfq 305905a36a2SIngo Molnar * popq %r11 306905a36a2SIngo Molnar * stuck_here: 307905a36a2SIngo Molnar * 308905a36a2SIngo Molnar * would never get past 'stuck_here'. 309905a36a2SIngo Molnar */ 310905a36a2SIngo Molnar testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11 311905a36a2SIngo Molnar jnz opportunistic_sysret_failed 312905a36a2SIngo Molnar 313905a36a2SIngo Molnar /* nothing to check for RSP */ 314905a36a2SIngo Molnar 315905a36a2SIngo Molnar cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */ 316905a36a2SIngo Molnar jne opportunistic_sysret_failed 317905a36a2SIngo Molnar 318905a36a2SIngo Molnar /* 319905a36a2SIngo Molnar * We win! This label is here just for ease of understanding 320905a36a2SIngo Molnar * perf profiles. Nothing jumps here. 321905a36a2SIngo Molnar */ 322905a36a2SIngo Molnarsyscall_return_via_sysret: 323905a36a2SIngo Molnar /* rcx and r11 are already restored (see code above) */ 324905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RCX_R11 325905a36a2SIngo Molnar movq RSP(%rsp), %rsp 326905a36a2SIngo Molnar USERGS_SYSRET64 327905a36a2SIngo Molnar 328905a36a2SIngo Molnaropportunistic_sysret_failed: 329905a36a2SIngo Molnar SWAPGS 330905a36a2SIngo Molnar jmp restore_c_regs_and_iret 331b2502b41SIngo MolnarEND(entry_SYSCALL_64) 332905a36a2SIngo Molnar 333302f5b26SAndy LutomirskiENTRY(stub_ptregs_64) 334302f5b26SAndy Lutomirski /* 335302f5b26SAndy Lutomirski * Syscalls marked as needing ptregs land here. 336b7765086SAndy Lutomirski * If we are on the fast path, we need to save the extra regs, 337b7765086SAndy Lutomirski * which we achieve by trying again on the slow path. If we are on 338b7765086SAndy Lutomirski * the slow path, the extra regs are already saved. 339302f5b26SAndy Lutomirski * 340302f5b26SAndy Lutomirski * RAX stores a pointer to the C function implementing the syscall. 341b7765086SAndy Lutomirski * IRQs are on. 342302f5b26SAndy Lutomirski */ 343302f5b26SAndy Lutomirski cmpq $.Lentry_SYSCALL_64_after_fastpath_call, (%rsp) 344302f5b26SAndy Lutomirski jne 1f 345302f5b26SAndy Lutomirski 346b7765086SAndy Lutomirski /* 347b7765086SAndy Lutomirski * Called from fast path -- disable IRQs again, pop return address 348b7765086SAndy Lutomirski * and jump to slow path 349b7765086SAndy Lutomirski */ 350b7765086SAndy Lutomirski DISABLE_INTERRUPTS(CLBR_NONE) 351b7765086SAndy Lutomirski TRACE_IRQS_OFF 352302f5b26SAndy Lutomirski popq %rax 353b7765086SAndy Lutomirski jmp entry_SYSCALL64_slow_path 354302f5b26SAndy Lutomirski 355302f5b26SAndy Lutomirski1: 356b3830e8dSBorislav Petkov jmp *%rax /* Called from C */ 357302f5b26SAndy LutomirskiEND(stub_ptregs_64) 358302f5b26SAndy Lutomirski 359302f5b26SAndy Lutomirski.macro ptregs_stub func 360302f5b26SAndy LutomirskiENTRY(ptregs_\func) 361302f5b26SAndy Lutomirski leaq \func(%rip), %rax 362302f5b26SAndy Lutomirski jmp stub_ptregs_64 363302f5b26SAndy LutomirskiEND(ptregs_\func) 364302f5b26SAndy Lutomirski.endm 365302f5b26SAndy Lutomirski 366302f5b26SAndy Lutomirski/* Instantiate ptregs_stub for each ptregs-using syscall */ 367302f5b26SAndy Lutomirski#define __SYSCALL_64_QUAL_(sym) 368302f5b26SAndy Lutomirski#define __SYSCALL_64_QUAL_ptregs(sym) ptregs_stub sym 369302f5b26SAndy Lutomirski#define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym) 370302f5b26SAndy Lutomirski#include <asm/syscalls_64.h> 371905a36a2SIngo Molnar 372905a36a2SIngo Molnar/* 3730100301bSBrian Gerst * %rdi: prev task 3740100301bSBrian Gerst * %rsi: next task 375*15f4eae7SAndy Lutomirski * rsi: task we're switching to 3760100301bSBrian Gerst */ 3770100301bSBrian GerstENTRY(__switch_to_asm) 3780100301bSBrian Gerst /* 3790100301bSBrian Gerst * Save callee-saved registers 3800100301bSBrian Gerst * This must match the order in inactive_task_frame 3810100301bSBrian Gerst */ 3820100301bSBrian Gerst pushq %rbp 3830100301bSBrian Gerst pushq %rbx 3840100301bSBrian Gerst pushq %r12 3850100301bSBrian Gerst pushq %r13 3860100301bSBrian Gerst pushq %r14 3870100301bSBrian Gerst pushq %r15 3880100301bSBrian Gerst 3890100301bSBrian Gerst /* switch stack */ 3900100301bSBrian Gerst movq %rsp, TASK_threadsp(%rdi) 3910100301bSBrian Gerst movq TASK_threadsp(%rsi), %rsp 3920100301bSBrian Gerst 3930100301bSBrian Gerst#ifdef CONFIG_CC_STACKPROTECTOR 3940100301bSBrian Gerst movq TASK_stack_canary(%rsi), %rbx 3950100301bSBrian Gerst movq %rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset 3960100301bSBrian Gerst#endif 3970100301bSBrian Gerst 3980100301bSBrian Gerst /* restore callee-saved registers */ 3990100301bSBrian Gerst popq %r15 4000100301bSBrian Gerst popq %r14 4010100301bSBrian Gerst popq %r13 4020100301bSBrian Gerst popq %r12 4030100301bSBrian Gerst popq %rbx 4040100301bSBrian Gerst popq %rbp 4050100301bSBrian Gerst 4060100301bSBrian Gerst jmp __switch_to 4070100301bSBrian GerstEND(__switch_to_asm) 4080100301bSBrian Gerst 4090100301bSBrian Gerst/* 410905a36a2SIngo Molnar * A newly forked process directly context switches into this address. 411905a36a2SIngo Molnar * 4120100301bSBrian Gerst * rax: prev task we switched from 413616d2483SBrian Gerst * rbx: kernel thread func (NULL for user thread) 414616d2483SBrian Gerst * r12: kernel thread arg 415905a36a2SIngo Molnar */ 416905a36a2SIngo MolnarENTRY(ret_from_fork) 4170100301bSBrian Gerst movq %rax, %rdi 4184d732138SIngo Molnar call schedule_tail /* rdi: 'prev' task parameter */ 419905a36a2SIngo Molnar 420616d2483SBrian Gerst testq %rbx, %rbx /* from kernel_thread? */ 421616d2483SBrian Gerst jnz 1f /* kernel threads are uncommon */ 422905a36a2SIngo Molnar 423616d2483SBrian Gerst2: 42424d978b7SAndy Lutomirski movq %rsp, %rdi 42524d978b7SAndy Lutomirski call syscall_return_slowpath /* returns with IRQs disabled */ 42624d978b7SAndy Lutomirski TRACE_IRQS_ON /* user mode is traced as IRQS on */ 42724d978b7SAndy Lutomirski SWAPGS 42824d978b7SAndy Lutomirski jmp restore_regs_and_iret 429616d2483SBrian Gerst 430616d2483SBrian Gerst1: 431616d2483SBrian Gerst /* kernel thread */ 432616d2483SBrian Gerst movq %r12, %rdi 433616d2483SBrian Gerst call *%rbx 434616d2483SBrian Gerst /* 435616d2483SBrian Gerst * A kernel thread is allowed to return here after successfully 436616d2483SBrian Gerst * calling do_execve(). Exit to userspace to complete the execve() 437616d2483SBrian Gerst * syscall. 438616d2483SBrian Gerst */ 439616d2483SBrian Gerst movq $0, RAX(%rsp) 440616d2483SBrian Gerst jmp 2b 441905a36a2SIngo MolnarEND(ret_from_fork) 442905a36a2SIngo Molnar 443905a36a2SIngo Molnar/* 444905a36a2SIngo Molnar * Build the entry stubs with some assembler magic. 445905a36a2SIngo Molnar * We pack 1 stub into every 8-byte block. 446905a36a2SIngo Molnar */ 447905a36a2SIngo Molnar .align 8 448905a36a2SIngo MolnarENTRY(irq_entries_start) 449905a36a2SIngo Molnar vector=FIRST_EXTERNAL_VECTOR 450905a36a2SIngo Molnar .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR) 451905a36a2SIngo Molnar pushq $(~vector+0x80) /* Note: always in signed byte range */ 452905a36a2SIngo Molnar vector=vector+1 453905a36a2SIngo Molnar jmp common_interrupt 454905a36a2SIngo Molnar .align 8 455905a36a2SIngo Molnar .endr 456905a36a2SIngo MolnarEND(irq_entries_start) 457905a36a2SIngo Molnar 458905a36a2SIngo Molnar/* 459905a36a2SIngo Molnar * Interrupt entry/exit. 460905a36a2SIngo Molnar * 461905a36a2SIngo Molnar * Interrupt entry points save only callee clobbered registers in fast path. 462905a36a2SIngo Molnar * 463905a36a2SIngo Molnar * Entry runs with interrupts off. 464905a36a2SIngo Molnar */ 465905a36a2SIngo Molnar 466905a36a2SIngo Molnar/* 0(%rsp): ~(interrupt number) */ 467905a36a2SIngo Molnar .macro interrupt func 468905a36a2SIngo Molnar cld 469ff467594SAndy Lutomirski ALLOC_PT_GPREGS_ON_STACK 470ff467594SAndy Lutomirski SAVE_C_REGS 471ff467594SAndy Lutomirski SAVE_EXTRA_REGS 472905a36a2SIngo Molnar 473ff467594SAndy Lutomirski testb $3, CS(%rsp) 474905a36a2SIngo Molnar jz 1f 47502bc7768SAndy Lutomirski 47602bc7768SAndy Lutomirski /* 47702bc7768SAndy Lutomirski * IRQ from user mode. Switch to kernel gsbase and inform context 47802bc7768SAndy Lutomirski * tracking that we're in kernel mode. 47902bc7768SAndy Lutomirski */ 480905a36a2SIngo Molnar SWAPGS 481f1075053SAndy Lutomirski 482f1075053SAndy Lutomirski /* 483f1075053SAndy Lutomirski * We need to tell lockdep that IRQs are off. We can't do this until 484f1075053SAndy Lutomirski * we fix gsbase, and we should do it before enter_from_user_mode 485f1075053SAndy Lutomirski * (which can take locks). Since TRACE_IRQS_OFF idempotent, 486f1075053SAndy Lutomirski * the simplest way to handle it is to just call it twice if 487f1075053SAndy Lutomirski * we enter from user mode. There's no reason to optimize this since 488f1075053SAndy Lutomirski * TRACE_IRQS_OFF is a no-op if lockdep is off. 489f1075053SAndy Lutomirski */ 490f1075053SAndy Lutomirski TRACE_IRQS_OFF 491f1075053SAndy Lutomirski 492478dc89cSAndy Lutomirski CALL_enter_from_user_mode 49302bc7768SAndy Lutomirski 494905a36a2SIngo Molnar1: 495905a36a2SIngo Molnar /* 496905a36a2SIngo Molnar * Save previous stack pointer, optionally switch to interrupt stack. 497905a36a2SIngo Molnar * irq_count is used to check if a CPU is already on an interrupt stack 498905a36a2SIngo Molnar * or not. While this is essentially redundant with preempt_count it is 499905a36a2SIngo Molnar * a little cheaper to use a separate counter in the PDA (short of 500905a36a2SIngo Molnar * moving irq_enter into assembly, which would be too much work) 501905a36a2SIngo Molnar */ 502a586f98eSAndy Lutomirski movq %rsp, %rdi 503905a36a2SIngo Molnar incl PER_CPU_VAR(irq_count) 504905a36a2SIngo Molnar cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp 505a586f98eSAndy Lutomirski pushq %rdi 506905a36a2SIngo Molnar /* We entered an interrupt context - irqs are off: */ 507905a36a2SIngo Molnar TRACE_IRQS_OFF 508905a36a2SIngo Molnar 509a586f98eSAndy Lutomirski call \func /* rdi points to pt_regs */ 510905a36a2SIngo Molnar .endm 511905a36a2SIngo Molnar 512905a36a2SIngo Molnar /* 513905a36a2SIngo Molnar * The interrupt stubs push (~vector+0x80) onto the stack and 514905a36a2SIngo Molnar * then jump to common_interrupt. 515905a36a2SIngo Molnar */ 516905a36a2SIngo Molnar .p2align CONFIG_X86_L1_CACHE_SHIFT 517905a36a2SIngo Molnarcommon_interrupt: 518905a36a2SIngo Molnar ASM_CLAC 519905a36a2SIngo Molnar addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */ 520905a36a2SIngo Molnar interrupt do_IRQ 521905a36a2SIngo Molnar /* 0(%rsp): old RSP */ 522905a36a2SIngo Molnarret_from_intr: 523905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 524905a36a2SIngo Molnar TRACE_IRQS_OFF 525905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 526905a36a2SIngo Molnar 527905a36a2SIngo Molnar /* Restore saved previous stack */ 528ff467594SAndy Lutomirski popq %rsp 529905a36a2SIngo Molnar 530905a36a2SIngo Molnar testb $3, CS(%rsp) 531905a36a2SIngo Molnar jz retint_kernel 53202bc7768SAndy Lutomirski 533905a36a2SIngo Molnar /* Interrupt came from user space */ 53402bc7768SAndy LutomirskiGLOBAL(retint_user) 53502bc7768SAndy Lutomirski mov %rsp,%rdi 53602bc7768SAndy Lutomirski call prepare_exit_to_usermode 537905a36a2SIngo Molnar TRACE_IRQS_IRETQ 538905a36a2SIngo Molnar SWAPGS 539ff467594SAndy Lutomirski jmp restore_regs_and_iret 540905a36a2SIngo Molnar 541905a36a2SIngo Molnar/* Returning to kernel space */ 542905a36a2SIngo Molnarretint_kernel: 543905a36a2SIngo Molnar#ifdef CONFIG_PREEMPT 544905a36a2SIngo Molnar /* Interrupts are off */ 545905a36a2SIngo Molnar /* Check if we need preemption */ 5464d732138SIngo Molnar bt $9, EFLAGS(%rsp) /* were interrupts off? */ 547905a36a2SIngo Molnar jnc 1f 548905a36a2SIngo Molnar0: cmpl $0, PER_CPU_VAR(__preempt_count) 549905a36a2SIngo Molnar jnz 1f 550905a36a2SIngo Molnar call preempt_schedule_irq 551905a36a2SIngo Molnar jmp 0b 552905a36a2SIngo Molnar1: 553905a36a2SIngo Molnar#endif 554905a36a2SIngo Molnar /* 555905a36a2SIngo Molnar * The iretq could re-enable interrupts: 556905a36a2SIngo Molnar */ 557905a36a2SIngo Molnar TRACE_IRQS_IRETQ 558905a36a2SIngo Molnar 559905a36a2SIngo Molnar/* 560905a36a2SIngo Molnar * At this label, code paths which return to kernel and to user, 561905a36a2SIngo Molnar * which come from interrupts/exception and from syscalls, merge. 562905a36a2SIngo Molnar */ 563ee08c6bdSAndy LutomirskiGLOBAL(restore_regs_and_iret) 564ff467594SAndy Lutomirski RESTORE_EXTRA_REGS 565905a36a2SIngo Molnarrestore_c_regs_and_iret: 566905a36a2SIngo Molnar RESTORE_C_REGS 567905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 8 568905a36a2SIngo Molnar INTERRUPT_RETURN 569905a36a2SIngo Molnar 570905a36a2SIngo MolnarENTRY(native_iret) 571905a36a2SIngo Molnar /* 572905a36a2SIngo Molnar * Are we returning to a stack segment from the LDT? Note: in 573905a36a2SIngo Molnar * 64-bit mode SS:RSP on the exception stack is always valid. 574905a36a2SIngo Molnar */ 575905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 576905a36a2SIngo Molnar testb $4, (SS-RIP)(%rsp) 577905a36a2SIngo Molnar jnz native_irq_return_ldt 578905a36a2SIngo Molnar#endif 579905a36a2SIngo Molnar 580905a36a2SIngo Molnar.global native_irq_return_iret 581905a36a2SIngo Molnarnative_irq_return_iret: 582905a36a2SIngo Molnar /* 583905a36a2SIngo Molnar * This may fault. Non-paranoid faults on return to userspace are 584905a36a2SIngo Molnar * handled by fixup_bad_iret. These include #SS, #GP, and #NP. 585905a36a2SIngo Molnar * Double-faults due to espfix64 are handled in do_double_fault. 586905a36a2SIngo Molnar * Other faults here are fatal. 587905a36a2SIngo Molnar */ 588905a36a2SIngo Molnar iretq 589905a36a2SIngo Molnar 590905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 591905a36a2SIngo Molnarnative_irq_return_ldt: 59285063facSAndy Lutomirski /* 59385063facSAndy Lutomirski * We are running with user GSBASE. All GPRs contain their user 59485063facSAndy Lutomirski * values. We have a percpu ESPFIX stack that is eight slots 59585063facSAndy Lutomirski * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom 59685063facSAndy Lutomirski * of the ESPFIX stack. 59785063facSAndy Lutomirski * 59885063facSAndy Lutomirski * We clobber RAX and RDI in this code. We stash RDI on the 59985063facSAndy Lutomirski * normal stack and RAX on the ESPFIX stack. 60085063facSAndy Lutomirski * 60185063facSAndy Lutomirski * The ESPFIX stack layout we set up looks like this: 60285063facSAndy Lutomirski * 60385063facSAndy Lutomirski * --- top of ESPFIX stack --- 60485063facSAndy Lutomirski * SS 60585063facSAndy Lutomirski * RSP 60685063facSAndy Lutomirski * RFLAGS 60785063facSAndy Lutomirski * CS 60885063facSAndy Lutomirski * RIP <-- RSP points here when we're done 60985063facSAndy Lutomirski * RAX <-- espfix_waddr points here 61085063facSAndy Lutomirski * --- bottom of ESPFIX stack --- 61185063facSAndy Lutomirski */ 61285063facSAndy Lutomirski 61385063facSAndy Lutomirski pushq %rdi /* Stash user RDI */ 614905a36a2SIngo Molnar SWAPGS 615905a36a2SIngo Molnar movq PER_CPU_VAR(espfix_waddr), %rdi 61685063facSAndy Lutomirski movq %rax, (0*8)(%rdi) /* user RAX */ 61785063facSAndy Lutomirski movq (1*8)(%rsp), %rax /* user RIP */ 618905a36a2SIngo Molnar movq %rax, (1*8)(%rdi) 61985063facSAndy Lutomirski movq (2*8)(%rsp), %rax /* user CS */ 620905a36a2SIngo Molnar movq %rax, (2*8)(%rdi) 62185063facSAndy Lutomirski movq (3*8)(%rsp), %rax /* user RFLAGS */ 622905a36a2SIngo Molnar movq %rax, (3*8)(%rdi) 62385063facSAndy Lutomirski movq (5*8)(%rsp), %rax /* user SS */ 624905a36a2SIngo Molnar movq %rax, (5*8)(%rdi) 62585063facSAndy Lutomirski movq (4*8)(%rsp), %rax /* user RSP */ 626905a36a2SIngo Molnar movq %rax, (4*8)(%rdi) 62785063facSAndy Lutomirski /* Now RAX == RSP. */ 62885063facSAndy Lutomirski 62985063facSAndy Lutomirski andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */ 63085063facSAndy Lutomirski popq %rdi /* Restore user RDI */ 63185063facSAndy Lutomirski 63285063facSAndy Lutomirski /* 63385063facSAndy Lutomirski * espfix_stack[31:16] == 0. The page tables are set up such that 63485063facSAndy Lutomirski * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of 63585063facSAndy Lutomirski * espfix_waddr for any X. That is, there are 65536 RO aliases of 63685063facSAndy Lutomirski * the same page. Set up RSP so that RSP[31:16] contains the 63785063facSAndy Lutomirski * respective 16 bits of the /userspace/ RSP and RSP nonetheless 63885063facSAndy Lutomirski * still points to an RO alias of the ESPFIX stack. 63985063facSAndy Lutomirski */ 640905a36a2SIngo Molnar orq PER_CPU_VAR(espfix_stack), %rax 641905a36a2SIngo Molnar SWAPGS 642905a36a2SIngo Molnar movq %rax, %rsp 64385063facSAndy Lutomirski 64485063facSAndy Lutomirski /* 64585063facSAndy Lutomirski * At this point, we cannot write to the stack any more, but we can 64685063facSAndy Lutomirski * still read. 64785063facSAndy Lutomirski */ 64885063facSAndy Lutomirski popq %rax /* Restore user RAX */ 64985063facSAndy Lutomirski 65085063facSAndy Lutomirski /* 65185063facSAndy Lutomirski * RSP now points to an ordinary IRET frame, except that the page 65285063facSAndy Lutomirski * is read-only and RSP[31:16] are preloaded with the userspace 65385063facSAndy Lutomirski * values. We can now IRET back to userspace. 65485063facSAndy Lutomirski */ 655905a36a2SIngo Molnar jmp native_irq_return_iret 656905a36a2SIngo Molnar#endif 657905a36a2SIngo MolnarEND(common_interrupt) 658905a36a2SIngo Molnar 659905a36a2SIngo Molnar/* 660905a36a2SIngo Molnar * APIC interrupts. 661905a36a2SIngo Molnar */ 662905a36a2SIngo Molnar.macro apicinterrupt3 num sym do_sym 663905a36a2SIngo MolnarENTRY(\sym) 664905a36a2SIngo Molnar ASM_CLAC 665905a36a2SIngo Molnar pushq $~(\num) 666905a36a2SIngo Molnar.Lcommon_\sym: 667905a36a2SIngo Molnar interrupt \do_sym 668905a36a2SIngo Molnar jmp ret_from_intr 669905a36a2SIngo MolnarEND(\sym) 670905a36a2SIngo Molnar.endm 671905a36a2SIngo Molnar 672905a36a2SIngo Molnar#ifdef CONFIG_TRACING 673905a36a2SIngo Molnar#define trace(sym) trace_##sym 674905a36a2SIngo Molnar#define smp_trace(sym) smp_trace_##sym 675905a36a2SIngo Molnar 676905a36a2SIngo Molnar.macro trace_apicinterrupt num sym 677905a36a2SIngo Molnarapicinterrupt3 \num trace(\sym) smp_trace(\sym) 678905a36a2SIngo Molnar.endm 679905a36a2SIngo Molnar#else 680905a36a2SIngo Molnar.macro trace_apicinterrupt num sym do_sym 681905a36a2SIngo Molnar.endm 682905a36a2SIngo Molnar#endif 683905a36a2SIngo Molnar 684469f0023SAlexander Potapenko/* Make sure APIC interrupt handlers end up in the irqentry section: */ 685469f0023SAlexander Potapenko#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) 686469f0023SAlexander Potapenko# define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax" 687469f0023SAlexander Potapenko# define POP_SECTION_IRQENTRY .popsection 688469f0023SAlexander Potapenko#else 689469f0023SAlexander Potapenko# define PUSH_SECTION_IRQENTRY 690469f0023SAlexander Potapenko# define POP_SECTION_IRQENTRY 691469f0023SAlexander Potapenko#endif 692469f0023SAlexander Potapenko 693905a36a2SIngo Molnar.macro apicinterrupt num sym do_sym 694469f0023SAlexander PotapenkoPUSH_SECTION_IRQENTRY 695905a36a2SIngo Molnarapicinterrupt3 \num \sym \do_sym 696905a36a2SIngo Molnartrace_apicinterrupt \num \sym 697469f0023SAlexander PotapenkoPOP_SECTION_IRQENTRY 698905a36a2SIngo Molnar.endm 699905a36a2SIngo Molnar 700905a36a2SIngo Molnar#ifdef CONFIG_SMP 7014d732138SIngo Molnarapicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt 7024d732138SIngo Molnarapicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt 703905a36a2SIngo Molnar#endif 704905a36a2SIngo Molnar 705905a36a2SIngo Molnar#ifdef CONFIG_X86_UV 7064d732138SIngo Molnarapicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt 707905a36a2SIngo Molnar#endif 7084d732138SIngo Molnar 7094d732138SIngo Molnarapicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt 7104d732138SIngo Molnarapicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi 711905a36a2SIngo Molnar 712905a36a2SIngo Molnar#ifdef CONFIG_HAVE_KVM 7134d732138SIngo Molnarapicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi 7144d732138SIngo Molnarapicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi 715905a36a2SIngo Molnar#endif 716905a36a2SIngo Molnar 717905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE_THRESHOLD 7184d732138SIngo Molnarapicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt 719905a36a2SIngo Molnar#endif 720905a36a2SIngo Molnar 7219dda1658SIngo Molnar#ifdef CONFIG_X86_MCE_AMD 7224d732138SIngo Molnarapicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt 7239dda1658SIngo Molnar#endif 7249dda1658SIngo Molnar 725905a36a2SIngo Molnar#ifdef CONFIG_X86_THERMAL_VECTOR 7264d732138SIngo Molnarapicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt 727905a36a2SIngo Molnar#endif 728905a36a2SIngo Molnar 729905a36a2SIngo Molnar#ifdef CONFIG_SMP 7304d732138SIngo Molnarapicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt 7314d732138SIngo Molnarapicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt 7324d732138SIngo Molnarapicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt 733905a36a2SIngo Molnar#endif 734905a36a2SIngo Molnar 7354d732138SIngo Molnarapicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt 7364d732138SIngo Molnarapicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt 737905a36a2SIngo Molnar 738905a36a2SIngo Molnar#ifdef CONFIG_IRQ_WORK 7394d732138SIngo Molnarapicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt 740905a36a2SIngo Molnar#endif 741905a36a2SIngo Molnar 742905a36a2SIngo Molnar/* 743905a36a2SIngo Molnar * Exception entry points. 744905a36a2SIngo Molnar */ 745905a36a2SIngo Molnar#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8) 746905a36a2SIngo Molnar 747905a36a2SIngo Molnar.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 748905a36a2SIngo MolnarENTRY(\sym) 749905a36a2SIngo Molnar /* Sanity check */ 750905a36a2SIngo Molnar .if \shift_ist != -1 && \paranoid == 0 751905a36a2SIngo Molnar .error "using shift_ist requires paranoid=1" 752905a36a2SIngo Molnar .endif 753905a36a2SIngo Molnar 754905a36a2SIngo Molnar ASM_CLAC 755905a36a2SIngo Molnar PARAVIRT_ADJUST_EXCEPTION_FRAME 756905a36a2SIngo Molnar 757905a36a2SIngo Molnar .ifeq \has_error_code 758905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 759905a36a2SIngo Molnar .endif 760905a36a2SIngo Molnar 761905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 762905a36a2SIngo Molnar 763905a36a2SIngo Molnar .if \paranoid 764905a36a2SIngo Molnar .if \paranoid == 1 7654d732138SIngo Molnar testb $3, CS(%rsp) /* If coming from userspace, switch stacks */ 7664d732138SIngo Molnar jnz 1f 767905a36a2SIngo Molnar .endif 768905a36a2SIngo Molnar call paranoid_entry 769905a36a2SIngo Molnar .else 770905a36a2SIngo Molnar call error_entry 771905a36a2SIngo Molnar .endif 772905a36a2SIngo Molnar /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */ 773905a36a2SIngo Molnar 774905a36a2SIngo Molnar .if \paranoid 775905a36a2SIngo Molnar .if \shift_ist != -1 776905a36a2SIngo Molnar TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */ 777905a36a2SIngo Molnar .else 778905a36a2SIngo Molnar TRACE_IRQS_OFF 779905a36a2SIngo Molnar .endif 780905a36a2SIngo Molnar .endif 781905a36a2SIngo Molnar 782905a36a2SIngo Molnar movq %rsp, %rdi /* pt_regs pointer */ 783905a36a2SIngo Molnar 784905a36a2SIngo Molnar .if \has_error_code 785905a36a2SIngo Molnar movq ORIG_RAX(%rsp), %rsi /* get error code */ 786905a36a2SIngo Molnar movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 787905a36a2SIngo Molnar .else 788905a36a2SIngo Molnar xorl %esi, %esi /* no error code */ 789905a36a2SIngo Molnar .endif 790905a36a2SIngo Molnar 791905a36a2SIngo Molnar .if \shift_ist != -1 792905a36a2SIngo Molnar subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist) 793905a36a2SIngo Molnar .endif 794905a36a2SIngo Molnar 795905a36a2SIngo Molnar call \do_sym 796905a36a2SIngo Molnar 797905a36a2SIngo Molnar .if \shift_ist != -1 798905a36a2SIngo Molnar addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist) 799905a36a2SIngo Molnar .endif 800905a36a2SIngo Molnar 801905a36a2SIngo Molnar /* these procedures expect "no swapgs" flag in ebx */ 802905a36a2SIngo Molnar .if \paranoid 803905a36a2SIngo Molnar jmp paranoid_exit 804905a36a2SIngo Molnar .else 805905a36a2SIngo Molnar jmp error_exit 806905a36a2SIngo Molnar .endif 807905a36a2SIngo Molnar 808905a36a2SIngo Molnar .if \paranoid == 1 809905a36a2SIngo Molnar /* 810905a36a2SIngo Molnar * Paranoid entry from userspace. Switch stacks and treat it 811905a36a2SIngo Molnar * as a normal entry. This means that paranoid handlers 812905a36a2SIngo Molnar * run in real process context if user_mode(regs). 813905a36a2SIngo Molnar */ 814905a36a2SIngo Molnar1: 815905a36a2SIngo Molnar call error_entry 816905a36a2SIngo Molnar 817905a36a2SIngo Molnar 818905a36a2SIngo Molnar movq %rsp, %rdi /* pt_regs pointer */ 819905a36a2SIngo Molnar call sync_regs 820905a36a2SIngo Molnar movq %rax, %rsp /* switch stack */ 821905a36a2SIngo Molnar 822905a36a2SIngo Molnar movq %rsp, %rdi /* pt_regs pointer */ 823905a36a2SIngo Molnar 824905a36a2SIngo Molnar .if \has_error_code 825905a36a2SIngo Molnar movq ORIG_RAX(%rsp), %rsi /* get error code */ 826905a36a2SIngo Molnar movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 827905a36a2SIngo Molnar .else 828905a36a2SIngo Molnar xorl %esi, %esi /* no error code */ 829905a36a2SIngo Molnar .endif 830905a36a2SIngo Molnar 831905a36a2SIngo Molnar call \do_sym 832905a36a2SIngo Molnar 833905a36a2SIngo Molnar jmp error_exit /* %ebx: no swapgs flag */ 834905a36a2SIngo Molnar .endif 835905a36a2SIngo MolnarEND(\sym) 836905a36a2SIngo Molnar.endm 837905a36a2SIngo Molnar 838905a36a2SIngo Molnar#ifdef CONFIG_TRACING 839905a36a2SIngo Molnar.macro trace_idtentry sym do_sym has_error_code:req 840905a36a2SIngo Molnaridtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code 841905a36a2SIngo Molnaridtentry \sym \do_sym has_error_code=\has_error_code 842905a36a2SIngo Molnar.endm 843905a36a2SIngo Molnar#else 844905a36a2SIngo Molnar.macro trace_idtentry sym do_sym has_error_code:req 845905a36a2SIngo Molnaridtentry \sym \do_sym has_error_code=\has_error_code 846905a36a2SIngo Molnar.endm 847905a36a2SIngo Molnar#endif 848905a36a2SIngo Molnar 849905a36a2SIngo Molnaridtentry divide_error do_divide_error has_error_code=0 850905a36a2SIngo Molnaridtentry overflow do_overflow has_error_code=0 851905a36a2SIngo Molnaridtentry bounds do_bounds has_error_code=0 852905a36a2SIngo Molnaridtentry invalid_op do_invalid_op has_error_code=0 853905a36a2SIngo Molnaridtentry device_not_available do_device_not_available has_error_code=0 854905a36a2SIngo Molnaridtentry double_fault do_double_fault has_error_code=1 paranoid=2 855905a36a2SIngo Molnaridtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0 856905a36a2SIngo Molnaridtentry invalid_TSS do_invalid_TSS has_error_code=1 857905a36a2SIngo Molnaridtentry segment_not_present do_segment_not_present has_error_code=1 858905a36a2SIngo Molnaridtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0 859905a36a2SIngo Molnaridtentry coprocessor_error do_coprocessor_error has_error_code=0 860905a36a2SIngo Molnaridtentry alignment_check do_alignment_check has_error_code=1 861905a36a2SIngo Molnaridtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0 862905a36a2SIngo Molnar 863905a36a2SIngo Molnar 8644d732138SIngo Molnar /* 8654d732138SIngo Molnar * Reload gs selector with exception handling 8664d732138SIngo Molnar * edi: new selector 8674d732138SIngo Molnar */ 868905a36a2SIngo MolnarENTRY(native_load_gs_index) 869905a36a2SIngo Molnar pushfq 870905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI) 871905a36a2SIngo Molnar SWAPGS 87242c748bbSBorislav Petkov.Lgs_change: 873905a36a2SIngo Molnar movl %edi, %gs 87496e5d28aSBorislav Petkov2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE 875905a36a2SIngo Molnar SWAPGS 876905a36a2SIngo Molnar popfq 877905a36a2SIngo Molnar ret 878905a36a2SIngo MolnarEND(native_load_gs_index) 879905a36a2SIngo Molnar 88042c748bbSBorislav Petkov _ASM_EXTABLE(.Lgs_change, bad_gs) 881905a36a2SIngo Molnar .section .fixup, "ax" 882905a36a2SIngo Molnar /* running with kernelgs */ 883905a36a2SIngo Molnarbad_gs: 884905a36a2SIngo Molnar SWAPGS /* switch back to user gs */ 885b038c842SAndy Lutomirski.macro ZAP_GS 886b038c842SAndy Lutomirski /* This can't be a string because the preprocessor needs to see it. */ 887b038c842SAndy Lutomirski movl $__USER_DS, %eax 888b038c842SAndy Lutomirski movl %eax, %gs 889b038c842SAndy Lutomirski.endm 890b038c842SAndy Lutomirski ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG 891905a36a2SIngo Molnar xorl %eax, %eax 892905a36a2SIngo Molnar movl %eax, %gs 893905a36a2SIngo Molnar jmp 2b 894905a36a2SIngo Molnar .previous 895905a36a2SIngo Molnar 896905a36a2SIngo Molnar/* Call softirq on interrupt stack. Interrupts are off. */ 897905a36a2SIngo MolnarENTRY(do_softirq_own_stack) 898905a36a2SIngo Molnar pushq %rbp 899905a36a2SIngo Molnar mov %rsp, %rbp 900905a36a2SIngo Molnar incl PER_CPU_VAR(irq_count) 901905a36a2SIngo Molnar cmove PER_CPU_VAR(irq_stack_ptr), %rsp 9024d732138SIngo Molnar push %rbp /* frame pointer backlink */ 903905a36a2SIngo Molnar call __do_softirq 904905a36a2SIngo Molnar leaveq 905905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 906905a36a2SIngo Molnar ret 907905a36a2SIngo MolnarEND(do_softirq_own_stack) 908905a36a2SIngo Molnar 909905a36a2SIngo Molnar#ifdef CONFIG_XEN 910905a36a2SIngo Molnaridtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0 911905a36a2SIngo Molnar 912905a36a2SIngo Molnar/* 913905a36a2SIngo Molnar * A note on the "critical region" in our callback handler. 914905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring 915905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled 916905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before 917905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still 918905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack. 919905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd 920905a36a2SIngo Molnar * like to avoid the possibility. 921905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an 922905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current 923905a36a2SIngo Molnar * activation and restart the handler using the previous one. 924905a36a2SIngo Molnar */ 9254d732138SIngo MolnarENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */ 9264d732138SIngo Molnar 927905a36a2SIngo Molnar/* 928905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will 929905a36a2SIngo Molnar * see the correct pointer to the pt_regs 930905a36a2SIngo Molnar */ 9314d732138SIngo Molnar movq %rdi, %rsp /* we don't return, adjust the stack frame */ 932905a36a2SIngo Molnar11: incl PER_CPU_VAR(irq_count) 933905a36a2SIngo Molnar movq %rsp, %rbp 934905a36a2SIngo Molnar cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp 9354d732138SIngo Molnar pushq %rbp /* frame pointer backlink */ 936905a36a2SIngo Molnar call xen_evtchn_do_upcall 937905a36a2SIngo Molnar popq %rsp 938905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 939905a36a2SIngo Molnar#ifndef CONFIG_PREEMPT 940905a36a2SIngo Molnar call xen_maybe_preempt_hcall 941905a36a2SIngo Molnar#endif 942905a36a2SIngo Molnar jmp error_exit 943905a36a2SIngo MolnarEND(xen_do_hypervisor_callback) 944905a36a2SIngo Molnar 945905a36a2SIngo Molnar/* 946905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes. 947905a36a2SIngo Molnar * We get here for two reasons: 948905a36a2SIngo Molnar * 1. Fault while reloading DS, ES, FS or GS 949905a36a2SIngo Molnar * 2. Fault while executing IRET 950905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment 951905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others. 952905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the 953905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall 954905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks. 955905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register 956905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1. 957905a36a2SIngo Molnar */ 958905a36a2SIngo MolnarENTRY(xen_failsafe_callback) 959905a36a2SIngo Molnar movl %ds, %ecx 960905a36a2SIngo Molnar cmpw %cx, 0x10(%rsp) 961905a36a2SIngo Molnar jne 1f 962905a36a2SIngo Molnar movl %es, %ecx 963905a36a2SIngo Molnar cmpw %cx, 0x18(%rsp) 964905a36a2SIngo Molnar jne 1f 965905a36a2SIngo Molnar movl %fs, %ecx 966905a36a2SIngo Molnar cmpw %cx, 0x20(%rsp) 967905a36a2SIngo Molnar jne 1f 968905a36a2SIngo Molnar movl %gs, %ecx 969905a36a2SIngo Molnar cmpw %cx, 0x28(%rsp) 970905a36a2SIngo Molnar jne 1f 971905a36a2SIngo Molnar /* All segments match their saved values => Category 2 (Bad IRET). */ 972905a36a2SIngo Molnar movq (%rsp), %rcx 973905a36a2SIngo Molnar movq 8(%rsp), %r11 974905a36a2SIngo Molnar addq $0x30, %rsp 975905a36a2SIngo Molnar pushq $0 /* RIP */ 976905a36a2SIngo Molnar pushq %r11 977905a36a2SIngo Molnar pushq %rcx 978905a36a2SIngo Molnar jmp general_protection 979905a36a2SIngo Molnar1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */ 980905a36a2SIngo Molnar movq (%rsp), %rcx 981905a36a2SIngo Molnar movq 8(%rsp), %r11 982905a36a2SIngo Molnar addq $0x30, %rsp 983905a36a2SIngo Molnar pushq $-1 /* orig_ax = -1 => not a system call */ 984905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 985905a36a2SIngo Molnar SAVE_C_REGS 986905a36a2SIngo Molnar SAVE_EXTRA_REGS 987905a36a2SIngo Molnar jmp error_exit 988905a36a2SIngo MolnarEND(xen_failsafe_callback) 989905a36a2SIngo Molnar 990905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 991905a36a2SIngo Molnar xen_hvm_callback_vector xen_evtchn_do_upcall 992905a36a2SIngo Molnar 993905a36a2SIngo Molnar#endif /* CONFIG_XEN */ 994905a36a2SIngo Molnar 995905a36a2SIngo Molnar#if IS_ENABLED(CONFIG_HYPERV) 996905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 997905a36a2SIngo Molnar hyperv_callback_vector hyperv_vector_handler 998905a36a2SIngo Molnar#endif /* CONFIG_HYPERV */ 999905a36a2SIngo Molnar 1000905a36a2SIngo Molnaridtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK 1001905a36a2SIngo Molnaridtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK 1002905a36a2SIngo Molnaridtentry stack_segment do_stack_segment has_error_code=1 10034d732138SIngo Molnar 1004905a36a2SIngo Molnar#ifdef CONFIG_XEN 1005905a36a2SIngo Molnaridtentry xen_debug do_debug has_error_code=0 1006905a36a2SIngo Molnaridtentry xen_int3 do_int3 has_error_code=0 1007905a36a2SIngo Molnaridtentry xen_stack_segment do_stack_segment has_error_code=1 1008905a36a2SIngo Molnar#endif 10094d732138SIngo Molnar 1010905a36a2SIngo Molnaridtentry general_protection do_general_protection has_error_code=1 1011905a36a2SIngo Molnartrace_idtentry page_fault do_page_fault has_error_code=1 10124d732138SIngo Molnar 1013905a36a2SIngo Molnar#ifdef CONFIG_KVM_GUEST 1014905a36a2SIngo Molnaridtentry async_page_fault do_async_page_fault has_error_code=1 1015905a36a2SIngo Molnar#endif 10164d732138SIngo Molnar 1017905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE 1018905a36a2SIngo Molnaridtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip) 1019905a36a2SIngo Molnar#endif 1020905a36a2SIngo Molnar 1021905a36a2SIngo Molnar/* 1022905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed. 1023905a36a2SIngo Molnar * Use slow, but surefire "are we in kernel?" check. 1024905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise 1025905a36a2SIngo Molnar */ 1026905a36a2SIngo MolnarENTRY(paranoid_entry) 1027905a36a2SIngo Molnar cld 1028905a36a2SIngo Molnar SAVE_C_REGS 8 1029905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 1030905a36a2SIngo Molnar movl $1, %ebx 1031905a36a2SIngo Molnar movl $MSR_GS_BASE, %ecx 1032905a36a2SIngo Molnar rdmsr 1033905a36a2SIngo Molnar testl %edx, %edx 1034905a36a2SIngo Molnar js 1f /* negative -> in kernel */ 1035905a36a2SIngo Molnar SWAPGS 1036905a36a2SIngo Molnar xorl %ebx, %ebx 1037905a36a2SIngo Molnar1: ret 1038905a36a2SIngo MolnarEND(paranoid_entry) 1039905a36a2SIngo Molnar 1040905a36a2SIngo Molnar/* 1041905a36a2SIngo Molnar * "Paranoid" exit path from exception stack. This is invoked 1042905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came 1043905a36a2SIngo Molnar * from kernel space. 1044905a36a2SIngo Molnar * 1045905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early 1046905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would 1047905a36a2SIngo Molnar * be complicated. Fortunately, we there's no good reason 1048905a36a2SIngo Molnar * to try to handle preemption here. 10494d732138SIngo Molnar * 10504d732138SIngo Molnar * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) 1051905a36a2SIngo Molnar */ 1052905a36a2SIngo MolnarENTRY(paranoid_exit) 1053905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 1054905a36a2SIngo Molnar TRACE_IRQS_OFF_DEBUG 1055905a36a2SIngo Molnar testl %ebx, %ebx /* swapgs needed? */ 1056905a36a2SIngo Molnar jnz paranoid_exit_no_swapgs 1057905a36a2SIngo Molnar TRACE_IRQS_IRETQ 1058905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1059905a36a2SIngo Molnar jmp paranoid_exit_restore 1060905a36a2SIngo Molnarparanoid_exit_no_swapgs: 1061905a36a2SIngo Molnar TRACE_IRQS_IRETQ_DEBUG 1062905a36a2SIngo Molnarparanoid_exit_restore: 1063905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1064905a36a2SIngo Molnar RESTORE_C_REGS 1065905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 8 1066905a36a2SIngo Molnar INTERRUPT_RETURN 1067905a36a2SIngo MolnarEND(paranoid_exit) 1068905a36a2SIngo Molnar 1069905a36a2SIngo Molnar/* 1070905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed. 1071539f5113SAndy Lutomirski * Return: EBX=0: came from user mode; EBX=1: otherwise 1072905a36a2SIngo Molnar */ 1073905a36a2SIngo MolnarENTRY(error_entry) 1074905a36a2SIngo Molnar cld 1075905a36a2SIngo Molnar SAVE_C_REGS 8 1076905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 1077905a36a2SIngo Molnar xorl %ebx, %ebx 1078905a36a2SIngo Molnar testb $3, CS+8(%rsp) 1079cb6f64edSAndy Lutomirski jz .Lerror_kernelspace 1080539f5113SAndy Lutomirski 1081cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_swapgs: 1082cb6f64edSAndy Lutomirski /* 1083cb6f64edSAndy Lutomirski * We entered from user mode or we're pretending to have entered 1084cb6f64edSAndy Lutomirski * from user mode due to an IRET fault. 1085cb6f64edSAndy Lutomirski */ 1086905a36a2SIngo Molnar SWAPGS 1087539f5113SAndy Lutomirski 1088cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_after_swapgs: 1089f1075053SAndy Lutomirski /* 1090f1075053SAndy Lutomirski * We need to tell lockdep that IRQs are off. We can't do this until 1091f1075053SAndy Lutomirski * we fix gsbase, and we should do it before enter_from_user_mode 1092f1075053SAndy Lutomirski * (which can take locks). 1093f1075053SAndy Lutomirski */ 1094f1075053SAndy Lutomirski TRACE_IRQS_OFF 1095478dc89cSAndy Lutomirski CALL_enter_from_user_mode 1096f1075053SAndy Lutomirski ret 109702bc7768SAndy Lutomirski 1098cb6f64edSAndy Lutomirski.Lerror_entry_done: 1099905a36a2SIngo Molnar TRACE_IRQS_OFF 1100905a36a2SIngo Molnar ret 1101905a36a2SIngo Molnar 1102905a36a2SIngo Molnar /* 1103905a36a2SIngo Molnar * There are two places in the kernel that can potentially fault with 1104905a36a2SIngo Molnar * usergs. Handle them here. B stepping K8s sometimes report a 1105905a36a2SIngo Molnar * truncated RIP for IRET exceptions returning to compat mode. Check 1106905a36a2SIngo Molnar * for these here too. 1107905a36a2SIngo Molnar */ 1108cb6f64edSAndy Lutomirski.Lerror_kernelspace: 1109905a36a2SIngo Molnar incl %ebx 1110905a36a2SIngo Molnar leaq native_irq_return_iret(%rip), %rcx 1111905a36a2SIngo Molnar cmpq %rcx, RIP+8(%rsp) 1112cb6f64edSAndy Lutomirski je .Lerror_bad_iret 1113905a36a2SIngo Molnar movl %ecx, %eax /* zero extend */ 1114905a36a2SIngo Molnar cmpq %rax, RIP+8(%rsp) 1115cb6f64edSAndy Lutomirski je .Lbstep_iret 111642c748bbSBorislav Petkov cmpq $.Lgs_change, RIP+8(%rsp) 1117cb6f64edSAndy Lutomirski jne .Lerror_entry_done 1118539f5113SAndy Lutomirski 1119539f5113SAndy Lutomirski /* 112042c748bbSBorislav Petkov * hack: .Lgs_change can fail with user gsbase. If this happens, fix up 1121539f5113SAndy Lutomirski * gsbase and proceed. We'll fix up the exception and land in 112242c748bbSBorislav Petkov * .Lgs_change's error handler with kernel gsbase. 1123539f5113SAndy Lutomirski */ 1124cb6f64edSAndy Lutomirski jmp .Lerror_entry_from_usermode_swapgs 1125905a36a2SIngo Molnar 1126cb6f64edSAndy Lutomirski.Lbstep_iret: 1127905a36a2SIngo Molnar /* Fix truncated RIP */ 1128905a36a2SIngo Molnar movq %rcx, RIP+8(%rsp) 1129905a36a2SIngo Molnar /* fall through */ 1130905a36a2SIngo Molnar 1131cb6f64edSAndy Lutomirski.Lerror_bad_iret: 1132539f5113SAndy Lutomirski /* 1133539f5113SAndy Lutomirski * We came from an IRET to user mode, so we have user gsbase. 1134539f5113SAndy Lutomirski * Switch to kernel gsbase: 1135539f5113SAndy Lutomirski */ 1136905a36a2SIngo Molnar SWAPGS 1137539f5113SAndy Lutomirski 1138539f5113SAndy Lutomirski /* 1139539f5113SAndy Lutomirski * Pretend that the exception came from user mode: set up pt_regs 1140539f5113SAndy Lutomirski * as if we faulted immediately after IRET and clear EBX so that 1141539f5113SAndy Lutomirski * error_exit knows that we will be returning to user mode. 1142539f5113SAndy Lutomirski */ 1143905a36a2SIngo Molnar mov %rsp, %rdi 1144905a36a2SIngo Molnar call fixup_bad_iret 1145905a36a2SIngo Molnar mov %rax, %rsp 1146539f5113SAndy Lutomirski decl %ebx 1147cb6f64edSAndy Lutomirski jmp .Lerror_entry_from_usermode_after_swapgs 1148905a36a2SIngo MolnarEND(error_entry) 1149905a36a2SIngo Molnar 1150905a36a2SIngo Molnar 1151539f5113SAndy Lutomirski/* 1152539f5113SAndy Lutomirski * On entry, EBS is a "return to kernel mode" flag: 1153539f5113SAndy Lutomirski * 1: already in kernel mode, don't need SWAPGS 1154539f5113SAndy Lutomirski * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode 1155539f5113SAndy Lutomirski */ 1156905a36a2SIngo MolnarENTRY(error_exit) 1157905a36a2SIngo Molnar movl %ebx, %eax 1158905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 1159905a36a2SIngo Molnar TRACE_IRQS_OFF 1160905a36a2SIngo Molnar testl %eax, %eax 1161905a36a2SIngo Molnar jnz retint_kernel 1162905a36a2SIngo Molnar jmp retint_user 1163905a36a2SIngo MolnarEND(error_exit) 1164905a36a2SIngo Molnar 1165905a36a2SIngo Molnar/* Runs on exception stack */ 1166905a36a2SIngo MolnarENTRY(nmi) 1167fc57a7c6SAndy Lutomirski /* 1168fc57a7c6SAndy Lutomirski * Fix up the exception frame if we're on Xen. 1169fc57a7c6SAndy Lutomirski * PARAVIRT_ADJUST_EXCEPTION_FRAME is guaranteed to push at most 1170fc57a7c6SAndy Lutomirski * one value to the stack on native, so it may clobber the rdx 1171fc57a7c6SAndy Lutomirski * scratch slot, but it won't clobber any of the important 1172fc57a7c6SAndy Lutomirski * slots past it. 1173fc57a7c6SAndy Lutomirski * 1174fc57a7c6SAndy Lutomirski * Xen is a different story, because the Xen frame itself overlaps 1175fc57a7c6SAndy Lutomirski * the "NMI executing" variable. 1176fc57a7c6SAndy Lutomirski */ 1177905a36a2SIngo Molnar PARAVIRT_ADJUST_EXCEPTION_FRAME 1178fc57a7c6SAndy Lutomirski 1179905a36a2SIngo Molnar /* 1180905a36a2SIngo Molnar * We allow breakpoints in NMIs. If a breakpoint occurs, then 1181905a36a2SIngo Molnar * the iretq it performs will take us out of NMI context. 1182905a36a2SIngo Molnar * This means that we can have nested NMIs where the next 1183905a36a2SIngo Molnar * NMI is using the top of the stack of the previous NMI. We 1184905a36a2SIngo Molnar * can't let it execute because the nested NMI will corrupt the 1185905a36a2SIngo Molnar * stack of the previous NMI. NMI handlers are not re-entrant 1186905a36a2SIngo Molnar * anyway. 1187905a36a2SIngo Molnar * 1188905a36a2SIngo Molnar * To handle this case we do the following: 1189905a36a2SIngo Molnar * Check the a special location on the stack that contains 1190905a36a2SIngo Molnar * a variable that is set when NMIs are executing. 1191905a36a2SIngo Molnar * The interrupted task's stack is also checked to see if it 1192905a36a2SIngo Molnar * is an NMI stack. 1193905a36a2SIngo Molnar * If the variable is not set and the stack is not the NMI 1194905a36a2SIngo Molnar * stack then: 1195905a36a2SIngo Molnar * o Set the special variable on the stack 11960b22930eSAndy Lutomirski * o Copy the interrupt frame into an "outermost" location on the 11970b22930eSAndy Lutomirski * stack 11980b22930eSAndy Lutomirski * o Copy the interrupt frame into an "iret" location on the stack 1199905a36a2SIngo Molnar * o Continue processing the NMI 1200905a36a2SIngo Molnar * If the variable is set or the previous stack is the NMI stack: 12010b22930eSAndy Lutomirski * o Modify the "iret" location to jump to the repeat_nmi 1202905a36a2SIngo Molnar * o return back to the first NMI 1203905a36a2SIngo Molnar * 1204905a36a2SIngo Molnar * Now on exit of the first NMI, we first clear the stack variable 1205905a36a2SIngo Molnar * The NMI stack will tell any nested NMIs at that point that it is 1206905a36a2SIngo Molnar * nested. Then we pop the stack normally with iret, and if there was 1207905a36a2SIngo Molnar * a nested NMI that updated the copy interrupt stack frame, a 1208905a36a2SIngo Molnar * jump will be made to the repeat_nmi code that will handle the second 1209905a36a2SIngo Molnar * NMI. 12109b6e6a83SAndy Lutomirski * 12119b6e6a83SAndy Lutomirski * However, espfix prevents us from directly returning to userspace 12129b6e6a83SAndy Lutomirski * with a single IRET instruction. Similarly, IRET to user mode 12139b6e6a83SAndy Lutomirski * can fault. We therefore handle NMIs from user space like 12149b6e6a83SAndy Lutomirski * other IST entries. 1215905a36a2SIngo Molnar */ 1216905a36a2SIngo Molnar 1217905a36a2SIngo Molnar /* Use %rdx as our temp variable throughout */ 1218905a36a2SIngo Molnar pushq %rdx 1219905a36a2SIngo Molnar 12209b6e6a83SAndy Lutomirski testb $3, CS-RIP+8(%rsp) 12219b6e6a83SAndy Lutomirski jz .Lnmi_from_kernel 1222905a36a2SIngo Molnar 1223905a36a2SIngo Molnar /* 12249b6e6a83SAndy Lutomirski * NMI from user mode. We need to run on the thread stack, but we 12259b6e6a83SAndy Lutomirski * can't go through the normal entry paths: NMIs are masked, and 12269b6e6a83SAndy Lutomirski * we don't want to enable interrupts, because then we'll end 12279b6e6a83SAndy Lutomirski * up in an awkward situation in which IRQs are on but NMIs 12289b6e6a83SAndy Lutomirski * are off. 122983c133cfSAndy Lutomirski * 123083c133cfSAndy Lutomirski * We also must not push anything to the stack before switching 123183c133cfSAndy Lutomirski * stacks lest we corrupt the "NMI executing" variable. 12329b6e6a83SAndy Lutomirski */ 12339b6e6a83SAndy Lutomirski 123483c133cfSAndy Lutomirski SWAPGS_UNSAFE_STACK 12359b6e6a83SAndy Lutomirski cld 12369b6e6a83SAndy Lutomirski movq %rsp, %rdx 12379b6e6a83SAndy Lutomirski movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 12389b6e6a83SAndy Lutomirski pushq 5*8(%rdx) /* pt_regs->ss */ 12399b6e6a83SAndy Lutomirski pushq 4*8(%rdx) /* pt_regs->rsp */ 12409b6e6a83SAndy Lutomirski pushq 3*8(%rdx) /* pt_regs->flags */ 12419b6e6a83SAndy Lutomirski pushq 2*8(%rdx) /* pt_regs->cs */ 12429b6e6a83SAndy Lutomirski pushq 1*8(%rdx) /* pt_regs->rip */ 12439b6e6a83SAndy Lutomirski pushq $-1 /* pt_regs->orig_ax */ 12449b6e6a83SAndy Lutomirski pushq %rdi /* pt_regs->di */ 12459b6e6a83SAndy Lutomirski pushq %rsi /* pt_regs->si */ 12469b6e6a83SAndy Lutomirski pushq (%rdx) /* pt_regs->dx */ 12479b6e6a83SAndy Lutomirski pushq %rcx /* pt_regs->cx */ 12489b6e6a83SAndy Lutomirski pushq %rax /* pt_regs->ax */ 12499b6e6a83SAndy Lutomirski pushq %r8 /* pt_regs->r8 */ 12509b6e6a83SAndy Lutomirski pushq %r9 /* pt_regs->r9 */ 12519b6e6a83SAndy Lutomirski pushq %r10 /* pt_regs->r10 */ 12529b6e6a83SAndy Lutomirski pushq %r11 /* pt_regs->r11 */ 12539b6e6a83SAndy Lutomirski pushq %rbx /* pt_regs->rbx */ 12549b6e6a83SAndy Lutomirski pushq %rbp /* pt_regs->rbp */ 12559b6e6a83SAndy Lutomirski pushq %r12 /* pt_regs->r12 */ 12569b6e6a83SAndy Lutomirski pushq %r13 /* pt_regs->r13 */ 12579b6e6a83SAndy Lutomirski pushq %r14 /* pt_regs->r14 */ 12589b6e6a83SAndy Lutomirski pushq %r15 /* pt_regs->r15 */ 12599b6e6a83SAndy Lutomirski 12609b6e6a83SAndy Lutomirski /* 12619b6e6a83SAndy Lutomirski * At this point we no longer need to worry about stack damage 12629b6e6a83SAndy Lutomirski * due to nesting -- we're on the normal thread stack and we're 12639b6e6a83SAndy Lutomirski * done with the NMI stack. 12649b6e6a83SAndy Lutomirski */ 12659b6e6a83SAndy Lutomirski 12669b6e6a83SAndy Lutomirski movq %rsp, %rdi 12679b6e6a83SAndy Lutomirski movq $-1, %rsi 12689b6e6a83SAndy Lutomirski call do_nmi 12699b6e6a83SAndy Lutomirski 12709b6e6a83SAndy Lutomirski /* 12719b6e6a83SAndy Lutomirski * Return back to user mode. We must *not* do the normal exit 12729b6e6a83SAndy Lutomirski * work, because we don't want to enable interrupts. Fortunately, 12739b6e6a83SAndy Lutomirski * do_nmi doesn't modify pt_regs. 12749b6e6a83SAndy Lutomirski */ 12759b6e6a83SAndy Lutomirski SWAPGS 12769b6e6a83SAndy Lutomirski jmp restore_c_regs_and_iret 12779b6e6a83SAndy Lutomirski 12789b6e6a83SAndy Lutomirski.Lnmi_from_kernel: 12799b6e6a83SAndy Lutomirski /* 12800b22930eSAndy Lutomirski * Here's what our stack frame will look like: 12810b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12820b22930eSAndy Lutomirski * | original SS | 12830b22930eSAndy Lutomirski * | original Return RSP | 12840b22930eSAndy Lutomirski * | original RFLAGS | 12850b22930eSAndy Lutomirski * | original CS | 12860b22930eSAndy Lutomirski * | original RIP | 12870b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12880b22930eSAndy Lutomirski * | temp storage for rdx | 12890b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12900b22930eSAndy Lutomirski * | "NMI executing" variable | 12910b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12920b22930eSAndy Lutomirski * | iret SS } Copied from "outermost" frame | 12930b22930eSAndy Lutomirski * | iret Return RSP } on each loop iteration; overwritten | 12940b22930eSAndy Lutomirski * | iret RFLAGS } by a nested NMI to force another | 12950b22930eSAndy Lutomirski * | iret CS } iteration if needed. | 12960b22930eSAndy Lutomirski * | iret RIP } | 12970b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12980b22930eSAndy Lutomirski * | outermost SS } initialized in first_nmi; | 12990b22930eSAndy Lutomirski * | outermost Return RSP } will not be changed before | 13000b22930eSAndy Lutomirski * | outermost RFLAGS } NMI processing is done. | 13010b22930eSAndy Lutomirski * | outermost CS } Copied to "iret" frame on each | 13020b22930eSAndy Lutomirski * | outermost RIP } iteration. | 13030b22930eSAndy Lutomirski * +---------------------------------------------------------+ 13040b22930eSAndy Lutomirski * | pt_regs | 13050b22930eSAndy Lutomirski * +---------------------------------------------------------+ 13060b22930eSAndy Lutomirski * 13070b22930eSAndy Lutomirski * The "original" frame is used by hardware. Before re-enabling 13080b22930eSAndy Lutomirski * NMIs, we need to be done with it, and we need to leave enough 13090b22930eSAndy Lutomirski * space for the asm code here. 13100b22930eSAndy Lutomirski * 13110b22930eSAndy Lutomirski * We return by executing IRET while RSP points to the "iret" frame. 13120b22930eSAndy Lutomirski * That will either return for real or it will loop back into NMI 13130b22930eSAndy Lutomirski * processing. 13140b22930eSAndy Lutomirski * 13150b22930eSAndy Lutomirski * The "outermost" frame is copied to the "iret" frame on each 13160b22930eSAndy Lutomirski * iteration of the loop, so each iteration starts with the "iret" 13170b22930eSAndy Lutomirski * frame pointing to the final return target. 13180b22930eSAndy Lutomirski */ 13190b22930eSAndy Lutomirski 13200b22930eSAndy Lutomirski /* 13210b22930eSAndy Lutomirski * Determine whether we're a nested NMI. 13220b22930eSAndy Lutomirski * 1323a27507caSAndy Lutomirski * If we interrupted kernel code between repeat_nmi and 1324a27507caSAndy Lutomirski * end_repeat_nmi, then we are a nested NMI. We must not 1325a27507caSAndy Lutomirski * modify the "iret" frame because it's being written by 1326a27507caSAndy Lutomirski * the outer NMI. That's okay; the outer NMI handler is 1327a27507caSAndy Lutomirski * about to about to call do_nmi anyway, so we can just 1328a27507caSAndy Lutomirski * resume the outer NMI. 1329a27507caSAndy Lutomirski */ 1330a27507caSAndy Lutomirski 1331a27507caSAndy Lutomirski movq $repeat_nmi, %rdx 1332a27507caSAndy Lutomirski cmpq 8(%rsp), %rdx 1333a27507caSAndy Lutomirski ja 1f 1334a27507caSAndy Lutomirski movq $end_repeat_nmi, %rdx 1335a27507caSAndy Lutomirski cmpq 8(%rsp), %rdx 1336a27507caSAndy Lutomirski ja nested_nmi_out 1337a27507caSAndy Lutomirski1: 1338a27507caSAndy Lutomirski 1339a27507caSAndy Lutomirski /* 1340a27507caSAndy Lutomirski * Now check "NMI executing". If it's set, then we're nested. 13410b22930eSAndy Lutomirski * This will not detect if we interrupted an outer NMI just 13420b22930eSAndy Lutomirski * before IRET. 1343905a36a2SIngo Molnar */ 1344905a36a2SIngo Molnar cmpl $1, -8(%rsp) 1345905a36a2SIngo Molnar je nested_nmi 1346905a36a2SIngo Molnar 1347905a36a2SIngo Molnar /* 13480b22930eSAndy Lutomirski * Now test if the previous stack was an NMI stack. This covers 13490b22930eSAndy Lutomirski * the case where we interrupt an outer NMI after it clears 1350810bc075SAndy Lutomirski * "NMI executing" but before IRET. We need to be careful, though: 1351810bc075SAndy Lutomirski * there is one case in which RSP could point to the NMI stack 1352810bc075SAndy Lutomirski * despite there being no NMI active: naughty userspace controls 1353810bc075SAndy Lutomirski * RSP at the very beginning of the SYSCALL targets. We can 1354810bc075SAndy Lutomirski * pull a fast one on naughty userspace, though: we program 1355810bc075SAndy Lutomirski * SYSCALL to mask DF, so userspace cannot cause DF to be set 1356810bc075SAndy Lutomirski * if it controls the kernel's RSP. We set DF before we clear 1357810bc075SAndy Lutomirski * "NMI executing". 1358905a36a2SIngo Molnar */ 1359905a36a2SIngo Molnar lea 6*8(%rsp), %rdx 1360905a36a2SIngo Molnar /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */ 1361905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1362905a36a2SIngo Molnar /* If the stack pointer is above the NMI stack, this is a normal NMI */ 1363905a36a2SIngo Molnar ja first_nmi 13644d732138SIngo Molnar 1365905a36a2SIngo Molnar subq $EXCEPTION_STKSZ, %rdx 1366905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1367905a36a2SIngo Molnar /* If it is below the NMI stack, it is a normal NMI */ 1368905a36a2SIngo Molnar jb first_nmi 1369810bc075SAndy Lutomirski 1370810bc075SAndy Lutomirski /* Ah, it is within the NMI stack. */ 1371810bc075SAndy Lutomirski 1372810bc075SAndy Lutomirski testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp) 1373810bc075SAndy Lutomirski jz first_nmi /* RSP was user controlled. */ 1374810bc075SAndy Lutomirski 1375810bc075SAndy Lutomirski /* This is a nested NMI. */ 1376905a36a2SIngo Molnar 1377905a36a2SIngo Molnarnested_nmi: 1378905a36a2SIngo Molnar /* 13790b22930eSAndy Lutomirski * Modify the "iret" frame to point to repeat_nmi, forcing another 13800b22930eSAndy Lutomirski * iteration of NMI handling. 1381905a36a2SIngo Molnar */ 138223a781e9SAndy Lutomirski subq $8, %rsp 1383905a36a2SIngo Molnar leaq -10*8(%rsp), %rdx 1384905a36a2SIngo Molnar pushq $__KERNEL_DS 1385905a36a2SIngo Molnar pushq %rdx 1386905a36a2SIngo Molnar pushfq 1387905a36a2SIngo Molnar pushq $__KERNEL_CS 1388905a36a2SIngo Molnar pushq $repeat_nmi 1389905a36a2SIngo Molnar 1390905a36a2SIngo Molnar /* Put stack back */ 1391905a36a2SIngo Molnar addq $(6*8), %rsp 1392905a36a2SIngo Molnar 1393905a36a2SIngo Molnarnested_nmi_out: 1394905a36a2SIngo Molnar popq %rdx 1395905a36a2SIngo Molnar 13960b22930eSAndy Lutomirski /* We are returning to kernel mode, so this cannot result in a fault. */ 1397905a36a2SIngo Molnar INTERRUPT_RETURN 1398905a36a2SIngo Molnar 1399905a36a2SIngo Molnarfirst_nmi: 14000b22930eSAndy Lutomirski /* Restore rdx. */ 1401905a36a2SIngo Molnar movq (%rsp), %rdx 1402905a36a2SIngo Molnar 140336f1a77bSAndy Lutomirski /* Make room for "NMI executing". */ 140436f1a77bSAndy Lutomirski pushq $0 1405905a36a2SIngo Molnar 14060b22930eSAndy Lutomirski /* Leave room for the "iret" frame */ 1407905a36a2SIngo Molnar subq $(5*8), %rsp 1408905a36a2SIngo Molnar 14090b22930eSAndy Lutomirski /* Copy the "original" frame to the "outermost" frame */ 1410905a36a2SIngo Molnar .rept 5 1411905a36a2SIngo Molnar pushq 11*8(%rsp) 1412905a36a2SIngo Molnar .endr 1413905a36a2SIngo Molnar 1414905a36a2SIngo Molnar /* Everything up to here is safe from nested NMIs */ 1415905a36a2SIngo Molnar 1416a97439aaSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY 1417a97439aaSAndy Lutomirski /* 1418a97439aaSAndy Lutomirski * For ease of testing, unmask NMIs right away. Disabled by 1419a97439aaSAndy Lutomirski * default because IRET is very expensive. 1420a97439aaSAndy Lutomirski */ 1421a97439aaSAndy Lutomirski pushq $0 /* SS */ 1422a97439aaSAndy Lutomirski pushq %rsp /* RSP (minus 8 because of the previous push) */ 1423a97439aaSAndy Lutomirski addq $8, (%rsp) /* Fix up RSP */ 1424a97439aaSAndy Lutomirski pushfq /* RFLAGS */ 1425a97439aaSAndy Lutomirski pushq $__KERNEL_CS /* CS */ 1426a97439aaSAndy Lutomirski pushq $1f /* RIP */ 1427a97439aaSAndy Lutomirski INTERRUPT_RETURN /* continues at repeat_nmi below */ 1428a97439aaSAndy Lutomirski1: 1429a97439aaSAndy Lutomirski#endif 1430a97439aaSAndy Lutomirski 14310b22930eSAndy Lutomirskirepeat_nmi: 1432905a36a2SIngo Molnar /* 1433905a36a2SIngo Molnar * If there was a nested NMI, the first NMI's iret will return 1434905a36a2SIngo Molnar * here. But NMIs are still enabled and we can take another 1435905a36a2SIngo Molnar * nested NMI. The nested NMI checks the interrupted RIP to see 1436905a36a2SIngo Molnar * if it is between repeat_nmi and end_repeat_nmi, and if so 1437905a36a2SIngo Molnar * it will just return, as we are about to repeat an NMI anyway. 1438905a36a2SIngo Molnar * This makes it safe to copy to the stack frame that a nested 1439905a36a2SIngo Molnar * NMI will update. 14400b22930eSAndy Lutomirski * 14410b22930eSAndy Lutomirski * RSP is pointing to "outermost RIP". gsbase is unknown, but, if 14420b22930eSAndy Lutomirski * we're repeating an NMI, gsbase has the same value that it had on 14430b22930eSAndy Lutomirski * the first iteration. paranoid_entry will load the kernel 144436f1a77bSAndy Lutomirski * gsbase if needed before we call do_nmi. "NMI executing" 144536f1a77bSAndy Lutomirski * is zero. 1446905a36a2SIngo Molnar */ 144736f1a77bSAndy Lutomirski movq $1, 10*8(%rsp) /* Set "NMI executing". */ 1448905a36a2SIngo Molnar 14490b22930eSAndy Lutomirski /* 14500b22930eSAndy Lutomirski * Copy the "outermost" frame to the "iret" frame. NMIs that nest 14510b22930eSAndy Lutomirski * here must not modify the "iret" frame while we're writing to 14520b22930eSAndy Lutomirski * it or it will end up containing garbage. 14530b22930eSAndy Lutomirski */ 1454905a36a2SIngo Molnar addq $(10*8), %rsp 1455905a36a2SIngo Molnar .rept 5 1456905a36a2SIngo Molnar pushq -6*8(%rsp) 1457905a36a2SIngo Molnar .endr 1458905a36a2SIngo Molnar subq $(5*8), %rsp 1459905a36a2SIngo Molnarend_repeat_nmi: 1460905a36a2SIngo Molnar 1461905a36a2SIngo Molnar /* 14620b22930eSAndy Lutomirski * Everything below this point can be preempted by a nested NMI. 14630b22930eSAndy Lutomirski * If this happens, then the inner NMI will change the "iret" 14640b22930eSAndy Lutomirski * frame to point back to repeat_nmi. 1465905a36a2SIngo Molnar */ 1466905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 1467905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 1468905a36a2SIngo Molnar 1469905a36a2SIngo Molnar /* 1470905a36a2SIngo Molnar * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit 1471905a36a2SIngo Molnar * as we should not be calling schedule in NMI context. 1472905a36a2SIngo Molnar * Even with normal interrupts enabled. An NMI should not be 1473905a36a2SIngo Molnar * setting NEED_RESCHED or anything that normal interrupts and 1474905a36a2SIngo Molnar * exceptions might do. 1475905a36a2SIngo Molnar */ 1476905a36a2SIngo Molnar call paranoid_entry 1477905a36a2SIngo Molnar 1478905a36a2SIngo Molnar /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */ 1479905a36a2SIngo Molnar movq %rsp, %rdi 1480905a36a2SIngo Molnar movq $-1, %rsi 1481905a36a2SIngo Molnar call do_nmi 1482905a36a2SIngo Molnar 1483905a36a2SIngo Molnar testl %ebx, %ebx /* swapgs needed? */ 1484905a36a2SIngo Molnar jnz nmi_restore 1485905a36a2SIngo Molnarnmi_swapgs: 1486905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1487905a36a2SIngo Molnarnmi_restore: 1488905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1489905a36a2SIngo Molnar RESTORE_C_REGS 14900b22930eSAndy Lutomirski 14910b22930eSAndy Lutomirski /* Point RSP at the "iret" frame. */ 1492905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 6*8 1493905a36a2SIngo Molnar 1494810bc075SAndy Lutomirski /* 1495810bc075SAndy Lutomirski * Clear "NMI executing". Set DF first so that we can easily 1496810bc075SAndy Lutomirski * distinguish the remaining code between here and IRET from 1497810bc075SAndy Lutomirski * the SYSCALL entry and exit paths. On a native kernel, we 1498810bc075SAndy Lutomirski * could just inspect RIP, but, on paravirt kernels, 1499810bc075SAndy Lutomirski * INTERRUPT_RETURN can translate into a jump into a 1500810bc075SAndy Lutomirski * hypercall page. 1501810bc075SAndy Lutomirski */ 1502810bc075SAndy Lutomirski std 1503810bc075SAndy Lutomirski movq $0, 5*8(%rsp) /* clear "NMI executing" */ 15040b22930eSAndy Lutomirski 15050b22930eSAndy Lutomirski /* 15060b22930eSAndy Lutomirski * INTERRUPT_RETURN reads the "iret" frame and exits the NMI 15070b22930eSAndy Lutomirski * stack in a single instruction. We are returning to kernel 15080b22930eSAndy Lutomirski * mode, so this cannot result in a fault. 15090b22930eSAndy Lutomirski */ 15105ca6f70fSAndy Lutomirski INTERRUPT_RETURN 1511905a36a2SIngo MolnarEND(nmi) 1512905a36a2SIngo Molnar 1513905a36a2SIngo MolnarENTRY(ignore_sysret) 1514905a36a2SIngo Molnar mov $-ENOSYS, %eax 1515905a36a2SIngo Molnar sysret 1516905a36a2SIngo MolnarEND(ignore_sysret) 15172deb4be2SAndy Lutomirski 15182deb4be2SAndy LutomirskiENTRY(rewind_stack_do_exit) 15192deb4be2SAndy Lutomirski /* Prevent any naive code from trying to unwind to our caller. */ 15202deb4be2SAndy Lutomirski xorl %ebp, %ebp 15212deb4be2SAndy Lutomirski 15222deb4be2SAndy Lutomirski movq PER_CPU_VAR(cpu_current_top_of_stack), %rax 15232deb4be2SAndy Lutomirski leaq -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%rax), %rsp 15242deb4be2SAndy Lutomirski 15252deb4be2SAndy Lutomirski call do_exit 15262deb4be2SAndy Lutomirski1: jmp 1b 15272deb4be2SAndy LutomirskiEND(rewind_stack_do_exit) 1528