1b2441318SGreg Kroah-Hartman/* SPDX-License-Identifier: GPL-2.0 */ 2905a36a2SIngo Molnar/* 3905a36a2SIngo Molnar * linux/arch/x86_64/entry.S 4905a36a2SIngo Molnar * 5905a36a2SIngo Molnar * Copyright (C) 1991, 1992 Linus Torvalds 6905a36a2SIngo Molnar * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs 7905a36a2SIngo Molnar * Copyright (C) 2000 Pavel Machek <pavel@suse.cz> 84d732138SIngo Molnar * 9905a36a2SIngo Molnar * entry.S contains the system-call and fault low-level handling routines. 10905a36a2SIngo Molnar * 11cb1aaebeSMauro Carvalho Chehab * Some of this is documented in Documentation/x86/entry_64.rst 12905a36a2SIngo Molnar * 13905a36a2SIngo Molnar * A note on terminology: 14905a36a2SIngo Molnar * - iret frame: Architecture defined interrupt frame from SS to RIP 15905a36a2SIngo Molnar * at the top of the kernel process stack. 16905a36a2SIngo Molnar * 17905a36a2SIngo Molnar * Some macro usage: 184d732138SIngo Molnar * - ENTRY/END: Define functions in the symbol table. 194d732138SIngo Molnar * - TRACE_IRQ_*: Trace hardirq state for lock debugging. 204d732138SIngo Molnar * - idtentry: Define exception entry points. 21905a36a2SIngo Molnar */ 22905a36a2SIngo Molnar#include <linux/linkage.h> 23905a36a2SIngo Molnar#include <asm/segment.h> 24905a36a2SIngo Molnar#include <asm/cache.h> 25905a36a2SIngo Molnar#include <asm/errno.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> 38784d5699SAl Viro#include <asm/export.h> 398c1f7558SJosh Poimboeuf#include <asm/frame.h> 402641f08bSDavid Woodhouse#include <asm/nospec-branch.h> 41905a36a2SIngo Molnar#include <linux/err.h> 42905a36a2SIngo Molnar 436fd166aaSPeter Zijlstra#include "calling.h" 446fd166aaSPeter Zijlstra 45905a36a2SIngo Molnar.code64 46905a36a2SIngo Molnar.section .entry.text, "ax" 47905a36a2SIngo Molnar 48905a36a2SIngo Molnar#ifdef CONFIG_PARAVIRT 49905a36a2SIngo MolnarENTRY(native_usergs_sysret64) 508c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 51905a36a2SIngo Molnar swapgs 52905a36a2SIngo Molnar sysretq 538c1f7558SJosh PoimboeufEND(native_usergs_sysret64) 54905a36a2SIngo Molnar#endif /* CONFIG_PARAVIRT */ 55905a36a2SIngo Molnar 56ca37e57bSAndy Lutomirski.macro TRACE_IRQS_FLAGS flags:req 57905a36a2SIngo Molnar#ifdef CONFIG_TRACE_IRQFLAGS 58a368d7fdSJan Beulich btl $9, \flags /* interrupts off? */ 59905a36a2SIngo Molnar jnc 1f 60905a36a2SIngo Molnar TRACE_IRQS_ON 61905a36a2SIngo Molnar1: 62905a36a2SIngo Molnar#endif 63905a36a2SIngo Molnar.endm 64905a36a2SIngo Molnar 65ca37e57bSAndy Lutomirski.macro TRACE_IRQS_IRETQ 66ca37e57bSAndy Lutomirski TRACE_IRQS_FLAGS EFLAGS(%rsp) 67ca37e57bSAndy Lutomirski.endm 68ca37e57bSAndy Lutomirski 69905a36a2SIngo Molnar/* 70905a36a2SIngo Molnar * When dynamic function tracer is enabled it will add a breakpoint 71905a36a2SIngo Molnar * to all locations that it is about to modify, sync CPUs, update 72905a36a2SIngo Molnar * all the code, sync CPUs, then remove the breakpoints. In this time 73905a36a2SIngo Molnar * if lockdep is enabled, it might jump back into the debug handler 74905a36a2SIngo Molnar * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF). 75905a36a2SIngo Molnar * 76905a36a2SIngo Molnar * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to 77905a36a2SIngo Molnar * make sure the stack pointer does not get reset back to the top 78905a36a2SIngo Molnar * of the debug stack, and instead just reuses the current stack. 79905a36a2SIngo Molnar */ 80905a36a2SIngo Molnar#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS) 81905a36a2SIngo Molnar 82905a36a2SIngo Molnar.macro TRACE_IRQS_OFF_DEBUG 83905a36a2SIngo Molnar call debug_stack_set_zero 84905a36a2SIngo Molnar TRACE_IRQS_OFF 85905a36a2SIngo Molnar call debug_stack_reset 86905a36a2SIngo Molnar.endm 87905a36a2SIngo Molnar 88905a36a2SIngo Molnar.macro TRACE_IRQS_ON_DEBUG 89905a36a2SIngo Molnar call debug_stack_set_zero 90905a36a2SIngo Molnar TRACE_IRQS_ON 91905a36a2SIngo Molnar call debug_stack_reset 92905a36a2SIngo Molnar.endm 93905a36a2SIngo Molnar 94905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ_DEBUG 956709812fSJan Beulich btl $9, EFLAGS(%rsp) /* interrupts off? */ 96905a36a2SIngo Molnar jnc 1f 97905a36a2SIngo Molnar TRACE_IRQS_ON_DEBUG 98905a36a2SIngo Molnar1: 99905a36a2SIngo Molnar.endm 100905a36a2SIngo Molnar 101905a36a2SIngo Molnar#else 102905a36a2SIngo Molnar# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF 103905a36a2SIngo Molnar# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON 104905a36a2SIngo Molnar# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ 105905a36a2SIngo Molnar#endif 106905a36a2SIngo Molnar 107905a36a2SIngo Molnar/* 1084d732138SIngo Molnar * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers. 109905a36a2SIngo Molnar * 110fda57b22SAndy Lutomirski * This is the only entry point used for 64-bit system calls. The 111fda57b22SAndy Lutomirski * hardware interface is reasonably well designed and the register to 112fda57b22SAndy Lutomirski * argument mapping Linux uses fits well with the registers that are 113fda57b22SAndy Lutomirski * available when SYSCALL is used. 114fda57b22SAndy Lutomirski * 115fda57b22SAndy Lutomirski * SYSCALL instructions can be found inlined in libc implementations as 116fda57b22SAndy Lutomirski * well as some other programs and libraries. There are also a handful 117fda57b22SAndy Lutomirski * of SYSCALL instructions in the vDSO used, for example, as a 118fda57b22SAndy Lutomirski * clock_gettimeofday fallback. 119fda57b22SAndy Lutomirski * 1204d732138SIngo Molnar * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11, 121905a36a2SIngo Molnar * then loads new ss, cs, and rip from previously programmed MSRs. 122905a36a2SIngo Molnar * rflags gets masked by a value from another MSR (so CLD and CLAC 123905a36a2SIngo Molnar * are not needed). SYSCALL does not save anything on the stack 124905a36a2SIngo Molnar * and does not change rsp. 125905a36a2SIngo Molnar * 126905a36a2SIngo Molnar * Registers on entry: 127905a36a2SIngo Molnar * rax system call number 128905a36a2SIngo Molnar * rcx return address 129905a36a2SIngo Molnar * r11 saved rflags (note: r11 is callee-clobbered register in C ABI) 130905a36a2SIngo Molnar * rdi arg0 131905a36a2SIngo Molnar * rsi arg1 132905a36a2SIngo Molnar * rdx arg2 133905a36a2SIngo Molnar * r10 arg3 (needs to be moved to rcx to conform to C ABI) 134905a36a2SIngo Molnar * r8 arg4 135905a36a2SIngo Molnar * r9 arg5 136905a36a2SIngo Molnar * (note: r12-r15, rbp, rbx are callee-preserved in C ABI) 137905a36a2SIngo Molnar * 138905a36a2SIngo Molnar * Only called from user space. 139905a36a2SIngo Molnar * 140905a36a2SIngo Molnar * When user can change pt_regs->foo always force IRET. That is because 141905a36a2SIngo Molnar * it deals with uncanonical addresses better. SYSRET has trouble 142905a36a2SIngo Molnar * with them due to bugs in both AMD and Intel CPUs. 143905a36a2SIngo Molnar */ 144905a36a2SIngo Molnar 145b2502b41SIngo MolnarENTRY(entry_SYSCALL_64) 1468c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 147905a36a2SIngo Molnar /* 148905a36a2SIngo Molnar * Interrupts are off on entry. 149905a36a2SIngo Molnar * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON, 150905a36a2SIngo Molnar * it is too small to ever cause noticeable irq latency. 151905a36a2SIngo Molnar */ 152905a36a2SIngo Molnar 1538a9949bcSAndy Lutomirski swapgs 154bf904d27SAndy Lutomirski /* tss.sp2 is scratch space. */ 15598f05b51SAndy Lutomirski movq %rsp, PER_CPU_VAR(cpu_tss_rw + TSS_sp2) 156bf904d27SAndy Lutomirski SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp 157905a36a2SIngo Molnar movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 158905a36a2SIngo Molnar 159905a36a2SIngo Molnar /* Construct struct pt_regs on stack */ 160905a36a2SIngo Molnar pushq $__USER_DS /* pt_regs->ss */ 16198f05b51SAndy Lutomirski pushq PER_CPU_VAR(cpu_tss_rw + TSS_sp2) /* pt_regs->sp */ 162905a36a2SIngo Molnar pushq %r11 /* pt_regs->flags */ 163905a36a2SIngo Molnar pushq $__USER_CS /* pt_regs->cs */ 164905a36a2SIngo Molnar pushq %rcx /* pt_regs->ip */ 1658a9949bcSAndy LutomirskiGLOBAL(entry_SYSCALL_64_after_hwframe) 166905a36a2SIngo Molnar pushq %rax /* pt_regs->orig_ax */ 16730907fd1SDominik Brodowski 16830907fd1SDominik Brodowski PUSH_AND_CLEAR_REGS rax=$-ENOSYS 169905a36a2SIngo Molnar 170548c3050SAndy Lutomirski TRACE_IRQS_OFF 171548c3050SAndy Lutomirski 1721e423bffSAndy Lutomirski /* IRQs are off. */ 173dfe64506SLinus Torvalds movq %rax, %rdi 174dfe64506SLinus Torvalds movq %rsp, %rsi 1751e423bffSAndy Lutomirski call do_syscall_64 /* returns with IRQs disabled */ 1761e423bffSAndy Lutomirski 17729ea1b25SAndy Lutomirski TRACE_IRQS_IRETQ /* we're about to change IF */ 178905a36a2SIngo Molnar 179905a36a2SIngo Molnar /* 180905a36a2SIngo Molnar * Try to use SYSRET instead of IRET if we're returning to 1818a055d7fSAndy Lutomirski * a completely clean 64-bit userspace context. If we're not, 1828a055d7fSAndy Lutomirski * go to the slow exit path. 183905a36a2SIngo Molnar */ 184905a36a2SIngo Molnar movq RCX(%rsp), %rcx 185905a36a2SIngo Molnar movq RIP(%rsp), %r11 1868a055d7fSAndy Lutomirski 1878a055d7fSAndy Lutomirski cmpq %rcx, %r11 /* SYSRET requires RCX == RIP */ 1888a055d7fSAndy Lutomirski jne swapgs_restore_regs_and_return_to_usermode 189905a36a2SIngo Molnar 190905a36a2SIngo Molnar /* 191905a36a2SIngo Molnar * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP 192905a36a2SIngo Molnar * in kernel space. This essentially lets the user take over 193905a36a2SIngo Molnar * the kernel, since userspace controls RSP. 194905a36a2SIngo Molnar * 195905a36a2SIngo Molnar * If width of "canonical tail" ever becomes variable, this will need 196905a36a2SIngo Molnar * to be updated to remain correct on both old and new CPUs. 197361b4b58SKirill A. Shutemov * 198cbe0317bSKirill A. Shutemov * Change top bits to match most significant bit (47th or 56th bit 199cbe0317bSKirill A. Shutemov * depending on paging mode) in the address. 200905a36a2SIngo Molnar */ 20109e61a77SKirill A. Shutemov#ifdef CONFIG_X86_5LEVEL 20239b95522SKirill A. Shutemov ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \ 20339b95522SKirill A. Shutemov "shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57 20409e61a77SKirill A. Shutemov#else 205905a36a2SIngo Molnar shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 206905a36a2SIngo Molnar sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 20709e61a77SKirill A. Shutemov#endif 2084d732138SIngo Molnar 209905a36a2SIngo Molnar /* If this changed %rcx, it was not canonical */ 210905a36a2SIngo Molnar cmpq %rcx, %r11 2118a055d7fSAndy Lutomirski jne swapgs_restore_regs_and_return_to_usermode 212905a36a2SIngo Molnar 213905a36a2SIngo Molnar cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */ 2148a055d7fSAndy Lutomirski jne swapgs_restore_regs_and_return_to_usermode 215905a36a2SIngo Molnar 216905a36a2SIngo Molnar movq R11(%rsp), %r11 217905a36a2SIngo Molnar cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */ 2188a055d7fSAndy Lutomirski jne swapgs_restore_regs_and_return_to_usermode 219905a36a2SIngo Molnar 220905a36a2SIngo Molnar /* 2213e035305SBorislav Petkov * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot 2223e035305SBorislav Petkov * restore RF properly. If the slowpath sets it for whatever reason, we 2233e035305SBorislav Petkov * need to restore it correctly. 2243e035305SBorislav Petkov * 2253e035305SBorislav Petkov * SYSRET can restore TF, but unlike IRET, restoring TF results in a 2263e035305SBorislav Petkov * trap from userspace immediately after SYSRET. This would cause an 2273e035305SBorislav Petkov * infinite loop whenever #DB happens with register state that satisfies 2283e035305SBorislav Petkov * the opportunistic SYSRET conditions. For example, single-stepping 2293e035305SBorislav Petkov * this user code: 230905a36a2SIngo Molnar * 231905a36a2SIngo Molnar * movq $stuck_here, %rcx 232905a36a2SIngo Molnar * pushfq 233905a36a2SIngo Molnar * popq %r11 234905a36a2SIngo Molnar * stuck_here: 235905a36a2SIngo Molnar * 236905a36a2SIngo Molnar * would never get past 'stuck_here'. 237905a36a2SIngo Molnar */ 238905a36a2SIngo Molnar testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11 2398a055d7fSAndy Lutomirski jnz swapgs_restore_regs_and_return_to_usermode 240905a36a2SIngo Molnar 241905a36a2SIngo Molnar /* nothing to check for RSP */ 242905a36a2SIngo Molnar 243905a36a2SIngo Molnar cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */ 2448a055d7fSAndy Lutomirski jne swapgs_restore_regs_and_return_to_usermode 245905a36a2SIngo Molnar 246905a36a2SIngo Molnar /* 247905a36a2SIngo Molnar * We win! This label is here just for ease of understanding 248905a36a2SIngo Molnar * perf profiles. Nothing jumps here. 249905a36a2SIngo Molnar */ 250905a36a2SIngo Molnarsyscall_return_via_sysret: 251905a36a2SIngo Molnar /* rcx and r11 are already restored (see code above) */ 2528c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 253502af0d7SDominik Brodowski POP_REGS pop_rdi=0 skip_r11rcx=1 2543e3b9293SAndy Lutomirski 2553e3b9293SAndy Lutomirski /* 2563e3b9293SAndy Lutomirski * Now all regs are restored except RSP and RDI. 2573e3b9293SAndy Lutomirski * Save old stack pointer and switch to trampoline stack. 2583e3b9293SAndy Lutomirski */ 2593e3b9293SAndy Lutomirski movq %rsp, %rdi 260c482feefSAndy Lutomirski movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp 2613e3b9293SAndy Lutomirski 2623e3b9293SAndy Lutomirski pushq RSP-RDI(%rdi) /* RSP */ 2633e3b9293SAndy Lutomirski pushq (%rdi) /* RDI */ 2643e3b9293SAndy Lutomirski 2653e3b9293SAndy Lutomirski /* 2663e3b9293SAndy Lutomirski * We are on the trampoline stack. All regs except RDI are live. 2673e3b9293SAndy Lutomirski * We can do future final exit work right here. 2683e3b9293SAndy Lutomirski */ 269afaef01cSAlexander Popov STACKLEAK_ERASE_NOCLOBBER 270afaef01cSAlexander Popov 2716fd166aaSPeter Zijlstra SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi 2723e3b9293SAndy Lutomirski 2734fbb3910SAndy Lutomirski popq %rdi 2743e3b9293SAndy Lutomirski popq %rsp 275905a36a2SIngo Molnar USERGS_SYSRET64 276b2502b41SIngo MolnarEND(entry_SYSCALL_64) 277905a36a2SIngo Molnar 278905a36a2SIngo Molnar/* 2790100301bSBrian Gerst * %rdi: prev task 2800100301bSBrian Gerst * %rsi: next task 2810100301bSBrian Gerst */ 2820100301bSBrian GerstENTRY(__switch_to_asm) 2838c1f7558SJosh Poimboeuf UNWIND_HINT_FUNC 2840100301bSBrian Gerst /* 2850100301bSBrian Gerst * Save callee-saved registers 2860100301bSBrian Gerst * This must match the order in inactive_task_frame 2870100301bSBrian Gerst */ 2880100301bSBrian Gerst pushq %rbp 2890100301bSBrian Gerst pushq %rbx 2900100301bSBrian Gerst pushq %r12 2910100301bSBrian Gerst pushq %r13 2920100301bSBrian Gerst pushq %r14 2930100301bSBrian Gerst pushq %r15 2940100301bSBrian Gerst 2950100301bSBrian Gerst /* switch stack */ 2960100301bSBrian Gerst movq %rsp, TASK_threadsp(%rdi) 2970100301bSBrian Gerst movq TASK_threadsp(%rsi), %rsp 2980100301bSBrian Gerst 299050e9baaSLinus Torvalds#ifdef CONFIG_STACKPROTECTOR 3000100301bSBrian Gerst movq TASK_stack_canary(%rsi), %rbx 301e6401c13SAndy Lutomirski movq %rbx, PER_CPU_VAR(fixed_percpu_data) + stack_canary_offset 3020100301bSBrian Gerst#endif 3030100301bSBrian Gerst 304c995efd5SDavid Woodhouse#ifdef CONFIG_RETPOLINE 305c995efd5SDavid Woodhouse /* 306c995efd5SDavid Woodhouse * When switching from a shallower to a deeper call stack 307c995efd5SDavid Woodhouse * the RSB may either underflow or use entries populated 308c995efd5SDavid Woodhouse * with userspace addresses. On CPUs where those concerns 309c995efd5SDavid Woodhouse * exist, overwrite the RSB with entries which capture 310c995efd5SDavid Woodhouse * speculative execution to prevent attack. 311c995efd5SDavid Woodhouse */ 312d1c99108SDavid Woodhouse FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW 313c995efd5SDavid Woodhouse#endif 314c995efd5SDavid Woodhouse 3150100301bSBrian Gerst /* restore callee-saved registers */ 3160100301bSBrian Gerst popq %r15 3170100301bSBrian Gerst popq %r14 3180100301bSBrian Gerst popq %r13 3190100301bSBrian Gerst popq %r12 3200100301bSBrian Gerst popq %rbx 3210100301bSBrian Gerst popq %rbp 3220100301bSBrian Gerst 3230100301bSBrian Gerst jmp __switch_to 3240100301bSBrian GerstEND(__switch_to_asm) 3250100301bSBrian Gerst 3260100301bSBrian Gerst/* 327905a36a2SIngo Molnar * A newly forked process directly context switches into this address. 328905a36a2SIngo Molnar * 3290100301bSBrian Gerst * rax: prev task we switched from 330616d2483SBrian Gerst * rbx: kernel thread func (NULL for user thread) 331616d2483SBrian Gerst * r12: kernel thread arg 332905a36a2SIngo Molnar */ 333905a36a2SIngo MolnarENTRY(ret_from_fork) 3348c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 3350100301bSBrian Gerst movq %rax, %rdi 3364d732138SIngo Molnar call schedule_tail /* rdi: 'prev' task parameter */ 337905a36a2SIngo Molnar 338616d2483SBrian Gerst testq %rbx, %rbx /* from kernel_thread? */ 339616d2483SBrian Gerst jnz 1f /* kernel threads are uncommon */ 340905a36a2SIngo Molnar 341616d2483SBrian Gerst2: 3428c1f7558SJosh Poimboeuf UNWIND_HINT_REGS 343ebd57499SJosh Poimboeuf movq %rsp, %rdi 34424d978b7SAndy Lutomirski call syscall_return_slowpath /* returns with IRQs disabled */ 34524d978b7SAndy Lutomirski TRACE_IRQS_ON /* user mode is traced as IRQS on */ 3468a055d7fSAndy Lutomirski jmp swapgs_restore_regs_and_return_to_usermode 347616d2483SBrian Gerst 348616d2483SBrian Gerst1: 349616d2483SBrian Gerst /* kernel thread */ 350d31a5802SJosh Poimboeuf UNWIND_HINT_EMPTY 351616d2483SBrian Gerst movq %r12, %rdi 3522641f08bSDavid Woodhouse CALL_NOSPEC %rbx 353616d2483SBrian Gerst /* 354616d2483SBrian Gerst * A kernel thread is allowed to return here after successfully 355616d2483SBrian Gerst * calling do_execve(). Exit to userspace to complete the execve() 356616d2483SBrian Gerst * syscall. 357616d2483SBrian Gerst */ 358616d2483SBrian Gerst movq $0, RAX(%rsp) 359616d2483SBrian Gerst jmp 2b 360905a36a2SIngo MolnarEND(ret_from_fork) 361905a36a2SIngo Molnar 362905a36a2SIngo Molnar/* 363905a36a2SIngo Molnar * Build the entry stubs with some assembler magic. 364905a36a2SIngo Molnar * We pack 1 stub into every 8-byte block. 365905a36a2SIngo Molnar */ 366905a36a2SIngo Molnar .align 8 367905a36a2SIngo MolnarENTRY(irq_entries_start) 368905a36a2SIngo Molnar vector=FIRST_EXTERNAL_VECTOR 369905a36a2SIngo Molnar .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR) 3708c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 371905a36a2SIngo Molnar pushq $(~vector+0x80) /* Note: always in signed byte range */ 372905a36a2SIngo Molnar jmp common_interrupt 373905a36a2SIngo Molnar .align 8 3748c1f7558SJosh Poimboeuf vector=vector+1 375905a36a2SIngo Molnar .endr 376905a36a2SIngo MolnarEND(irq_entries_start) 377905a36a2SIngo Molnar 378f8a8fe61SThomas Gleixner .align 8 379f8a8fe61SThomas GleixnerENTRY(spurious_entries_start) 380f8a8fe61SThomas Gleixner vector=FIRST_SYSTEM_VECTOR 381f8a8fe61SThomas Gleixner .rept (NR_VECTORS - FIRST_SYSTEM_VECTOR) 382f8a8fe61SThomas Gleixner UNWIND_HINT_IRET_REGS 383f8a8fe61SThomas Gleixner pushq $(~vector+0x80) /* Note: always in signed byte range */ 384f8a8fe61SThomas Gleixner jmp common_spurious 385f8a8fe61SThomas Gleixner .align 8 386f8a8fe61SThomas Gleixner vector=vector+1 387f8a8fe61SThomas Gleixner .endr 388f8a8fe61SThomas GleixnerEND(spurious_entries_start) 389f8a8fe61SThomas Gleixner 3901d3e53e8SAndy Lutomirski.macro DEBUG_ENTRY_ASSERT_IRQS_OFF 3911d3e53e8SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY 392e17f8234SBoris Ostrovsky pushq %rax 393e17f8234SBoris Ostrovsky SAVE_FLAGS(CLBR_RAX) 394e17f8234SBoris Ostrovsky testl $X86_EFLAGS_IF, %eax 3951d3e53e8SAndy Lutomirski jz .Lokay_\@ 3961d3e53e8SAndy Lutomirski ud2 3971d3e53e8SAndy Lutomirski.Lokay_\@: 398e17f8234SBoris Ostrovsky popq %rax 3991d3e53e8SAndy Lutomirski#endif 4001d3e53e8SAndy Lutomirski.endm 4011d3e53e8SAndy Lutomirski 4021d3e53e8SAndy Lutomirski/* 4031d3e53e8SAndy Lutomirski * Enters the IRQ stack if we're not already using it. NMI-safe. Clobbers 4041d3e53e8SAndy Lutomirski * flags and puts old RSP into old_rsp, and leaves all other GPRs alone. 4051d3e53e8SAndy Lutomirski * Requires kernel GSBASE. 4061d3e53e8SAndy Lutomirski * 4071d3e53e8SAndy Lutomirski * The invariant is that, if irq_count != -1, then the IRQ stack is in use. 4081d3e53e8SAndy Lutomirski */ 4092ba64741SDominik Brodowski.macro ENTER_IRQ_STACK regs=1 old_rsp save_ret=0 4101d3e53e8SAndy Lutomirski DEBUG_ENTRY_ASSERT_IRQS_OFF 4112ba64741SDominik Brodowski 4122ba64741SDominik Brodowski .if \save_ret 4132ba64741SDominik Brodowski /* 4142ba64741SDominik Brodowski * If save_ret is set, the original stack contains one additional 4152ba64741SDominik Brodowski * entry -- the return address. Therefore, move the address one 4162ba64741SDominik Brodowski * entry below %rsp to \old_rsp. 4172ba64741SDominik Brodowski */ 4182ba64741SDominik Brodowski leaq 8(%rsp), \old_rsp 4192ba64741SDominik Brodowski .else 4201d3e53e8SAndy Lutomirski movq %rsp, \old_rsp 4212ba64741SDominik Brodowski .endif 4228c1f7558SJosh Poimboeuf 4238c1f7558SJosh Poimboeuf .if \regs 4248c1f7558SJosh Poimboeuf UNWIND_HINT_REGS base=\old_rsp 4258c1f7558SJosh Poimboeuf .endif 4268c1f7558SJosh Poimboeuf 4271d3e53e8SAndy Lutomirski incl PER_CPU_VAR(irq_count) 42829955909SAndy Lutomirski jnz .Lirq_stack_push_old_rsp_\@ 4291d3e53e8SAndy Lutomirski 4301d3e53e8SAndy Lutomirski /* 4311d3e53e8SAndy Lutomirski * Right now, if we just incremented irq_count to zero, we've 4321d3e53e8SAndy Lutomirski * claimed the IRQ stack but we haven't switched to it yet. 4331d3e53e8SAndy Lutomirski * 4341d3e53e8SAndy Lutomirski * If anything is added that can interrupt us here without using IST, 4351d3e53e8SAndy Lutomirski * it must be *extremely* careful to limit its stack usage. This 4361d3e53e8SAndy Lutomirski * could include kprobes and a hypothetical future IST-less #DB 4371d3e53e8SAndy Lutomirski * handler. 43829955909SAndy Lutomirski * 43929955909SAndy Lutomirski * The OOPS unwinder relies on the word at the top of the IRQ 44029955909SAndy Lutomirski * stack linking back to the previous RSP for the entire time we're 44129955909SAndy Lutomirski * on the IRQ stack. For this to work reliably, we need to write 44229955909SAndy Lutomirski * it before we actually move ourselves to the IRQ stack. 4431d3e53e8SAndy Lutomirski */ 4441d3e53e8SAndy Lutomirski 445e6401c13SAndy Lutomirski movq \old_rsp, PER_CPU_VAR(irq_stack_backing_store + IRQ_STACK_SIZE - 8) 446758a2e31SThomas Gleixner movq PER_CPU_VAR(hardirq_stack_ptr), %rsp 44729955909SAndy Lutomirski 44829955909SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY 44929955909SAndy Lutomirski /* 45029955909SAndy Lutomirski * If the first movq above becomes wrong due to IRQ stack layout 45129955909SAndy Lutomirski * changes, the only way we'll notice is if we try to unwind right 45229955909SAndy Lutomirski * here. Assert that we set up the stack right to catch this type 45329955909SAndy Lutomirski * of bug quickly. 45429955909SAndy Lutomirski */ 45529955909SAndy Lutomirski cmpq -8(%rsp), \old_rsp 45629955909SAndy Lutomirski je .Lirq_stack_okay\@ 45729955909SAndy Lutomirski ud2 45829955909SAndy Lutomirski .Lirq_stack_okay\@: 45929955909SAndy Lutomirski#endif 46029955909SAndy Lutomirski 46129955909SAndy Lutomirski.Lirq_stack_push_old_rsp_\@: 4621d3e53e8SAndy Lutomirski pushq \old_rsp 4638c1f7558SJosh Poimboeuf 4648c1f7558SJosh Poimboeuf .if \regs 4658c1f7558SJosh Poimboeuf UNWIND_HINT_REGS indirect=1 4668c1f7558SJosh Poimboeuf .endif 4672ba64741SDominik Brodowski 4682ba64741SDominik Brodowski .if \save_ret 4692ba64741SDominik Brodowski /* 4702ba64741SDominik Brodowski * Push the return address to the stack. This return address can 4712ba64741SDominik Brodowski * be found at the "real" original RSP, which was offset by 8 at 4722ba64741SDominik Brodowski * the beginning of this macro. 4732ba64741SDominik Brodowski */ 4742ba64741SDominik Brodowski pushq -8(\old_rsp) 4752ba64741SDominik Brodowski .endif 4761d3e53e8SAndy Lutomirski.endm 4771d3e53e8SAndy Lutomirski 4781d3e53e8SAndy Lutomirski/* 4791d3e53e8SAndy Lutomirski * Undoes ENTER_IRQ_STACK. 4801d3e53e8SAndy Lutomirski */ 4818c1f7558SJosh Poimboeuf.macro LEAVE_IRQ_STACK regs=1 4821d3e53e8SAndy Lutomirski DEBUG_ENTRY_ASSERT_IRQS_OFF 4831d3e53e8SAndy Lutomirski /* We need to be off the IRQ stack before decrementing irq_count. */ 4841d3e53e8SAndy Lutomirski popq %rsp 4851d3e53e8SAndy Lutomirski 4868c1f7558SJosh Poimboeuf .if \regs 4878c1f7558SJosh Poimboeuf UNWIND_HINT_REGS 4888c1f7558SJosh Poimboeuf .endif 4898c1f7558SJosh Poimboeuf 4901d3e53e8SAndy Lutomirski /* 4911d3e53e8SAndy Lutomirski * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming 4921d3e53e8SAndy Lutomirski * the irq stack but we're not on it. 4931d3e53e8SAndy Lutomirski */ 4941d3e53e8SAndy Lutomirski 4951d3e53e8SAndy Lutomirski decl PER_CPU_VAR(irq_count) 4961d3e53e8SAndy Lutomirski.endm 4971d3e53e8SAndy Lutomirski 498905a36a2SIngo Molnar/* 499f3d415eaSDominik Brodowski * Interrupt entry helper function. 500905a36a2SIngo Molnar * 501f3d415eaSDominik Brodowski * Entry runs with interrupts off. Stack layout at entry: 502f3d415eaSDominik Brodowski * +----------------------------------------------------+ 503f3d415eaSDominik Brodowski * | regs->ss | 504f3d415eaSDominik Brodowski * | regs->rsp | 505f3d415eaSDominik Brodowski * | regs->eflags | 506f3d415eaSDominik Brodowski * | regs->cs | 507f3d415eaSDominik Brodowski * | regs->ip | 508f3d415eaSDominik Brodowski * +----------------------------------------------------+ 509f3d415eaSDominik Brodowski * | regs->orig_ax = ~(interrupt number) | 510f3d415eaSDominik Brodowski * +----------------------------------------------------+ 511f3d415eaSDominik Brodowski * | return address | 512f3d415eaSDominik Brodowski * +----------------------------------------------------+ 513905a36a2SIngo Molnar */ 514f3d415eaSDominik BrodowskiENTRY(interrupt_entry) 515f3d415eaSDominik Brodowski UNWIND_HINT_FUNC 516f3d415eaSDominik Brodowski ASM_CLAC 517905a36a2SIngo Molnar cld 5187f2590a1SAndy Lutomirski 519f3d415eaSDominik Brodowski testb $3, CS-ORIG_RAX+8(%rsp) 5207f2590a1SAndy Lutomirski jz 1f 5217f2590a1SAndy Lutomirski SWAPGS 522f3d415eaSDominik Brodowski 523f3d415eaSDominik Brodowski /* 524f3d415eaSDominik Brodowski * Switch to the thread stack. The IRET frame and orig_ax are 525f3d415eaSDominik Brodowski * on the stack, as well as the return address. RDI..R12 are 526f3d415eaSDominik Brodowski * not (yet) on the stack and space has not (yet) been 527f3d415eaSDominik Brodowski * allocated for them. 528f3d415eaSDominik Brodowski */ 52990a6acc4SDominik Brodowski pushq %rdi 530f3d415eaSDominik Brodowski 53190a6acc4SDominik Brodowski /* Need to switch before accessing the thread stack. */ 53290a6acc4SDominik Brodowski SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi 53390a6acc4SDominik Brodowski movq %rsp, %rdi 53490a6acc4SDominik Brodowski movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 535f3d415eaSDominik Brodowski 536f3d415eaSDominik Brodowski /* 537f3d415eaSDominik Brodowski * We have RDI, return address, and orig_ax on the stack on 538f3d415eaSDominik Brodowski * top of the IRET frame. That means offset=24 539f3d415eaSDominik Brodowski */ 540f3d415eaSDominik Brodowski UNWIND_HINT_IRET_REGS base=%rdi offset=24 54190a6acc4SDominik Brodowski 54290a6acc4SDominik Brodowski pushq 7*8(%rdi) /* regs->ss */ 54390a6acc4SDominik Brodowski pushq 6*8(%rdi) /* regs->rsp */ 54490a6acc4SDominik Brodowski pushq 5*8(%rdi) /* regs->eflags */ 54590a6acc4SDominik Brodowski pushq 4*8(%rdi) /* regs->cs */ 54690a6acc4SDominik Brodowski pushq 3*8(%rdi) /* regs->ip */ 54790a6acc4SDominik Brodowski pushq 2*8(%rdi) /* regs->orig_ax */ 54890a6acc4SDominik Brodowski pushq 8(%rdi) /* return address */ 54990a6acc4SDominik Brodowski UNWIND_HINT_FUNC 55090a6acc4SDominik Brodowski 55190a6acc4SDominik Brodowski movq (%rdi), %rdi 5527f2590a1SAndy Lutomirski1: 5537f2590a1SAndy Lutomirski 5540e34d226SDominik Brodowski PUSH_AND_CLEAR_REGS save_ret=1 5550e34d226SDominik Brodowski ENCODE_FRAME_POINTER 8 556905a36a2SIngo Molnar 5572ba64741SDominik Brodowski testb $3, CS+8(%rsp) 558905a36a2SIngo Molnar jz 1f 55902bc7768SAndy Lutomirski 56002bc7768SAndy Lutomirski /* 5617f2590a1SAndy Lutomirski * IRQ from user mode. 5627f2590a1SAndy Lutomirski * 563f1075053SAndy Lutomirski * We need to tell lockdep that IRQs are off. We can't do this until 564f1075053SAndy Lutomirski * we fix gsbase, and we should do it before enter_from_user_mode 565f3d415eaSDominik Brodowski * (which can take locks). Since TRACE_IRQS_OFF is idempotent, 566f1075053SAndy Lutomirski * the simplest way to handle it is to just call it twice if 567f1075053SAndy Lutomirski * we enter from user mode. There's no reason to optimize this since 568f1075053SAndy Lutomirski * TRACE_IRQS_OFF is a no-op if lockdep is off. 569f1075053SAndy Lutomirski */ 570f1075053SAndy Lutomirski TRACE_IRQS_OFF 571f1075053SAndy Lutomirski 572478dc89cSAndy Lutomirski CALL_enter_from_user_mode 57302bc7768SAndy Lutomirski 574905a36a2SIngo Molnar1: 5752ba64741SDominik Brodowski ENTER_IRQ_STACK old_rsp=%rdi save_ret=1 576905a36a2SIngo Molnar /* We entered an interrupt context - irqs are off: */ 577905a36a2SIngo Molnar TRACE_IRQS_OFF 578905a36a2SIngo Molnar 5792ba64741SDominik Brodowski ret 5802ba64741SDominik BrodowskiEND(interrupt_entry) 581a50480cbSAndrea Righi_ASM_NOKPROBE(interrupt_entry) 5822ba64741SDominik Brodowski 583f3d415eaSDominik Brodowski 584f3d415eaSDominik Brodowski/* Interrupt entry/exit. */ 585905a36a2SIngo Molnar 586905a36a2SIngo Molnar/* 587905a36a2SIngo Molnar * The interrupt stubs push (~vector+0x80) onto the stack and 588f8a8fe61SThomas Gleixner * then jump to common_spurious/interrupt. 589905a36a2SIngo Molnar */ 590f8a8fe61SThomas Gleixnercommon_spurious: 591f8a8fe61SThomas Gleixner addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */ 592f8a8fe61SThomas Gleixner call interrupt_entry 593f8a8fe61SThomas Gleixner UNWIND_HINT_REGS indirect=1 594f8a8fe61SThomas Gleixner call smp_spurious_interrupt /* rdi points to pt_regs */ 595f8a8fe61SThomas Gleixner jmp ret_from_intr 596f8a8fe61SThomas GleixnerEND(common_spurious) 597f8a8fe61SThomas Gleixner_ASM_NOKPROBE(common_spurious) 598f8a8fe61SThomas Gleixner 599f8a8fe61SThomas Gleixner/* common_interrupt is a hotpath. Align it */ 600905a36a2SIngo Molnar .p2align CONFIG_X86_L1_CACHE_SHIFT 601905a36a2SIngo Molnarcommon_interrupt: 602905a36a2SIngo Molnar addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */ 6033aa99fc3SDominik Brodowski call interrupt_entry 6043aa99fc3SDominik Brodowski UNWIND_HINT_REGS indirect=1 6053aa99fc3SDominik Brodowski call do_IRQ /* rdi points to pt_regs */ 606905a36a2SIngo Molnar /* 0(%rsp): old RSP */ 607905a36a2SIngo Molnarret_from_intr: 6082140a994SJan Beulich DISABLE_INTERRUPTS(CLBR_ANY) 609905a36a2SIngo Molnar TRACE_IRQS_OFF 610905a36a2SIngo Molnar 6111d3e53e8SAndy Lutomirski LEAVE_IRQ_STACK 612905a36a2SIngo Molnar 613905a36a2SIngo Molnar testb $3, CS(%rsp) 614905a36a2SIngo Molnar jz retint_kernel 61502bc7768SAndy Lutomirski 616905a36a2SIngo Molnar /* Interrupt came from user space */ 61702bc7768SAndy LutomirskiGLOBAL(retint_user) 61802bc7768SAndy Lutomirski mov %rsp,%rdi 61902bc7768SAndy Lutomirski call prepare_exit_to_usermode 620905a36a2SIngo Molnar TRACE_IRQS_IRETQ 62126c4ef9cSAndy Lutomirski 6228a055d7fSAndy LutomirskiGLOBAL(swapgs_restore_regs_and_return_to_usermode) 62326c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY 62426c4ef9cSAndy Lutomirski /* Assert that pt_regs indicates user mode. */ 6251e4c4f61SBorislav Petkov testb $3, CS(%rsp) 62626c4ef9cSAndy Lutomirski jnz 1f 62726c4ef9cSAndy Lutomirski ud2 62826c4ef9cSAndy Lutomirski1: 62926c4ef9cSAndy Lutomirski#endif 630502af0d7SDominik Brodowski POP_REGS pop_rdi=0 6313e3b9293SAndy Lutomirski 6323e3b9293SAndy Lutomirski /* 6333e3b9293SAndy Lutomirski * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS. 6343e3b9293SAndy Lutomirski * Save old stack pointer and switch to trampoline stack. 6353e3b9293SAndy Lutomirski */ 6363e3b9293SAndy Lutomirski movq %rsp, %rdi 637c482feefSAndy Lutomirski movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp 6383e3b9293SAndy Lutomirski 6393e3b9293SAndy Lutomirski /* Copy the IRET frame to the trampoline stack. */ 6403e3b9293SAndy Lutomirski pushq 6*8(%rdi) /* SS */ 6413e3b9293SAndy Lutomirski pushq 5*8(%rdi) /* RSP */ 6423e3b9293SAndy Lutomirski pushq 4*8(%rdi) /* EFLAGS */ 6433e3b9293SAndy Lutomirski pushq 3*8(%rdi) /* CS */ 6443e3b9293SAndy Lutomirski pushq 2*8(%rdi) /* RIP */ 6453e3b9293SAndy Lutomirski 6463e3b9293SAndy Lutomirski /* Push user RDI on the trampoline stack. */ 6473e3b9293SAndy Lutomirski pushq (%rdi) 6483e3b9293SAndy Lutomirski 6493e3b9293SAndy Lutomirski /* 6503e3b9293SAndy Lutomirski * We are on the trampoline stack. All regs except RDI are live. 6513e3b9293SAndy Lutomirski * We can do future final exit work right here. 6523e3b9293SAndy Lutomirski */ 653afaef01cSAlexander Popov STACKLEAK_ERASE_NOCLOBBER 6543e3b9293SAndy Lutomirski 6556fd166aaSPeter Zijlstra SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi 6568a09317bSDave Hansen 6573e3b9293SAndy Lutomirski /* Restore RDI. */ 6583e3b9293SAndy Lutomirski popq %rdi 6593e3b9293SAndy Lutomirski SWAPGS 66026c4ef9cSAndy Lutomirski INTERRUPT_RETURN 66126c4ef9cSAndy Lutomirski 662905a36a2SIngo Molnar 663905a36a2SIngo Molnar/* Returning to kernel space */ 664905a36a2SIngo Molnarretint_kernel: 665905a36a2SIngo Molnar#ifdef CONFIG_PREEMPT 666905a36a2SIngo Molnar /* Interrupts are off */ 667905a36a2SIngo Molnar /* Check if we need preemption */ 6686709812fSJan Beulich btl $9, EFLAGS(%rsp) /* were interrupts off? */ 669905a36a2SIngo Molnar jnc 1f 670b5b447b6SValentin Schneider cmpl $0, PER_CPU_VAR(__preempt_count) 671905a36a2SIngo Molnar jnz 1f 672905a36a2SIngo Molnar call preempt_schedule_irq 673905a36a2SIngo Molnar1: 674905a36a2SIngo Molnar#endif 675905a36a2SIngo Molnar /* 676905a36a2SIngo Molnar * The iretq could re-enable interrupts: 677905a36a2SIngo Molnar */ 678905a36a2SIngo Molnar TRACE_IRQS_IRETQ 679905a36a2SIngo Molnar 68026c4ef9cSAndy LutomirskiGLOBAL(restore_regs_and_return_to_kernel) 68126c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY 68226c4ef9cSAndy Lutomirski /* Assert that pt_regs indicates kernel mode. */ 6831e4c4f61SBorislav Petkov testb $3, CS(%rsp) 68426c4ef9cSAndy Lutomirski jz 1f 68526c4ef9cSAndy Lutomirski ud2 68626c4ef9cSAndy Lutomirski1: 68726c4ef9cSAndy Lutomirski#endif 688502af0d7SDominik Brodowski POP_REGS 689e872045bSAndy Lutomirski addq $8, %rsp /* skip regs->orig_ax */ 69010bcc80eSMathieu Desnoyers /* 69110bcc80eSMathieu Desnoyers * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization 69210bcc80eSMathieu Desnoyers * when returning from IPI handler. 69310bcc80eSMathieu Desnoyers */ 694905a36a2SIngo Molnar INTERRUPT_RETURN 695905a36a2SIngo Molnar 696905a36a2SIngo MolnarENTRY(native_iret) 6978c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 698905a36a2SIngo Molnar /* 699905a36a2SIngo Molnar * Are we returning to a stack segment from the LDT? Note: in 700905a36a2SIngo Molnar * 64-bit mode SS:RSP on the exception stack is always valid. 701905a36a2SIngo Molnar */ 702905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 703905a36a2SIngo Molnar testb $4, (SS-RIP)(%rsp) 704905a36a2SIngo Molnar jnz native_irq_return_ldt 705905a36a2SIngo Molnar#endif 706905a36a2SIngo Molnar 707905a36a2SIngo Molnar.global native_irq_return_iret 708905a36a2SIngo Molnarnative_irq_return_iret: 709905a36a2SIngo Molnar /* 710905a36a2SIngo Molnar * This may fault. Non-paranoid faults on return to userspace are 711905a36a2SIngo Molnar * handled by fixup_bad_iret. These include #SS, #GP, and #NP. 712905a36a2SIngo Molnar * Double-faults due to espfix64 are handled in do_double_fault. 713905a36a2SIngo Molnar * Other faults here are fatal. 714905a36a2SIngo Molnar */ 715905a36a2SIngo Molnar iretq 716905a36a2SIngo Molnar 717905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 718905a36a2SIngo Molnarnative_irq_return_ldt: 71985063facSAndy Lutomirski /* 72085063facSAndy Lutomirski * We are running with user GSBASE. All GPRs contain their user 72185063facSAndy Lutomirski * values. We have a percpu ESPFIX stack that is eight slots 72285063facSAndy Lutomirski * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom 72385063facSAndy Lutomirski * of the ESPFIX stack. 72485063facSAndy Lutomirski * 72585063facSAndy Lutomirski * We clobber RAX and RDI in this code. We stash RDI on the 72685063facSAndy Lutomirski * normal stack and RAX on the ESPFIX stack. 72785063facSAndy Lutomirski * 72885063facSAndy Lutomirski * The ESPFIX stack layout we set up looks like this: 72985063facSAndy Lutomirski * 73085063facSAndy Lutomirski * --- top of ESPFIX stack --- 73185063facSAndy Lutomirski * SS 73285063facSAndy Lutomirski * RSP 73385063facSAndy Lutomirski * RFLAGS 73485063facSAndy Lutomirski * CS 73585063facSAndy Lutomirski * RIP <-- RSP points here when we're done 73685063facSAndy Lutomirski * RAX <-- espfix_waddr points here 73785063facSAndy Lutomirski * --- bottom of ESPFIX stack --- 73885063facSAndy Lutomirski */ 73985063facSAndy Lutomirski 74085063facSAndy Lutomirski pushq %rdi /* Stash user RDI */ 7418a09317bSDave Hansen SWAPGS /* to kernel GS */ 7428a09317bSDave Hansen SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi /* to kernel CR3 */ 7438a09317bSDave Hansen 744905a36a2SIngo Molnar movq PER_CPU_VAR(espfix_waddr), %rdi 74585063facSAndy Lutomirski movq %rax, (0*8)(%rdi) /* user RAX */ 74685063facSAndy Lutomirski movq (1*8)(%rsp), %rax /* user RIP */ 747905a36a2SIngo Molnar movq %rax, (1*8)(%rdi) 74885063facSAndy Lutomirski movq (2*8)(%rsp), %rax /* user CS */ 749905a36a2SIngo Molnar movq %rax, (2*8)(%rdi) 75085063facSAndy Lutomirski movq (3*8)(%rsp), %rax /* user RFLAGS */ 751905a36a2SIngo Molnar movq %rax, (3*8)(%rdi) 75285063facSAndy Lutomirski movq (5*8)(%rsp), %rax /* user SS */ 753905a36a2SIngo Molnar movq %rax, (5*8)(%rdi) 75485063facSAndy Lutomirski movq (4*8)(%rsp), %rax /* user RSP */ 755905a36a2SIngo Molnar movq %rax, (4*8)(%rdi) 75685063facSAndy Lutomirski /* Now RAX == RSP. */ 75785063facSAndy Lutomirski 75885063facSAndy Lutomirski andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */ 75985063facSAndy Lutomirski 76085063facSAndy Lutomirski /* 76185063facSAndy Lutomirski * espfix_stack[31:16] == 0. The page tables are set up such that 76285063facSAndy Lutomirski * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of 76385063facSAndy Lutomirski * espfix_waddr for any X. That is, there are 65536 RO aliases of 76485063facSAndy Lutomirski * the same page. Set up RSP so that RSP[31:16] contains the 76585063facSAndy Lutomirski * respective 16 bits of the /userspace/ RSP and RSP nonetheless 76685063facSAndy Lutomirski * still points to an RO alias of the ESPFIX stack. 76785063facSAndy Lutomirski */ 768905a36a2SIngo Molnar orq PER_CPU_VAR(espfix_stack), %rax 7698a09317bSDave Hansen 7706fd166aaSPeter Zijlstra SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi 7718a09317bSDave Hansen SWAPGS /* to user GS */ 7728a09317bSDave Hansen popq %rdi /* Restore user RDI */ 7738a09317bSDave Hansen 774905a36a2SIngo Molnar movq %rax, %rsp 7758c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS offset=8 77685063facSAndy Lutomirski 77785063facSAndy Lutomirski /* 77885063facSAndy Lutomirski * At this point, we cannot write to the stack any more, but we can 77985063facSAndy Lutomirski * still read. 78085063facSAndy Lutomirski */ 78185063facSAndy Lutomirski popq %rax /* Restore user RAX */ 78285063facSAndy Lutomirski 78385063facSAndy Lutomirski /* 78485063facSAndy Lutomirski * RSP now points to an ordinary IRET frame, except that the page 78585063facSAndy Lutomirski * is read-only and RSP[31:16] are preloaded with the userspace 78685063facSAndy Lutomirski * values. We can now IRET back to userspace. 78785063facSAndy Lutomirski */ 788905a36a2SIngo Molnar jmp native_irq_return_iret 789905a36a2SIngo Molnar#endif 790905a36a2SIngo MolnarEND(common_interrupt) 791a50480cbSAndrea Righi_ASM_NOKPROBE(common_interrupt) 792905a36a2SIngo Molnar 793905a36a2SIngo Molnar/* 794905a36a2SIngo Molnar * APIC interrupts. 795905a36a2SIngo Molnar */ 796905a36a2SIngo Molnar.macro apicinterrupt3 num sym do_sym 797905a36a2SIngo MolnarENTRY(\sym) 7988c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 799905a36a2SIngo Molnar pushq $~(\num) 800905a36a2SIngo Molnar.Lcommon_\sym: 8013aa99fc3SDominik Brodowski call interrupt_entry 8023aa99fc3SDominik Brodowski UNWIND_HINT_REGS indirect=1 8033aa99fc3SDominik Brodowski call \do_sym /* rdi points to pt_regs */ 804905a36a2SIngo Molnar jmp ret_from_intr 805905a36a2SIngo MolnarEND(\sym) 806a50480cbSAndrea Righi_ASM_NOKPROBE(\sym) 807905a36a2SIngo Molnar.endm 808905a36a2SIngo Molnar 809469f0023SAlexander Potapenko/* Make sure APIC interrupt handlers end up in the irqentry section: */ 810469f0023SAlexander Potapenko#define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax" 811469f0023SAlexander Potapenko#define POP_SECTION_IRQENTRY .popsection 812469f0023SAlexander Potapenko 813905a36a2SIngo Molnar.macro apicinterrupt num sym do_sym 814469f0023SAlexander PotapenkoPUSH_SECTION_IRQENTRY 815905a36a2SIngo Molnarapicinterrupt3 \num \sym \do_sym 816469f0023SAlexander PotapenkoPOP_SECTION_IRQENTRY 817905a36a2SIngo Molnar.endm 818905a36a2SIngo Molnar 819905a36a2SIngo Molnar#ifdef CONFIG_SMP 8204d732138SIngo Molnarapicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt 8214d732138SIngo Molnarapicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt 822905a36a2SIngo Molnar#endif 823905a36a2SIngo Molnar 824905a36a2SIngo Molnar#ifdef CONFIG_X86_UV 8254d732138SIngo Molnarapicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt 826905a36a2SIngo Molnar#endif 8274d732138SIngo Molnar 8284d732138SIngo Molnarapicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt 8294d732138SIngo Molnarapicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi 830905a36a2SIngo Molnar 831905a36a2SIngo Molnar#ifdef CONFIG_HAVE_KVM 8324d732138SIngo Molnarapicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi 8334d732138SIngo Molnarapicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi 834210f84b0SWincy Vanapicinterrupt3 POSTED_INTR_NESTED_VECTOR kvm_posted_intr_nested_ipi smp_kvm_posted_intr_nested_ipi 835905a36a2SIngo Molnar#endif 836905a36a2SIngo Molnar 837905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE_THRESHOLD 8384d732138SIngo Molnarapicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt 839905a36a2SIngo Molnar#endif 840905a36a2SIngo Molnar 8419dda1658SIngo Molnar#ifdef CONFIG_X86_MCE_AMD 8424d732138SIngo Molnarapicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt 8439dda1658SIngo Molnar#endif 8449dda1658SIngo Molnar 845905a36a2SIngo Molnar#ifdef CONFIG_X86_THERMAL_VECTOR 8464d732138SIngo Molnarapicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt 847905a36a2SIngo Molnar#endif 848905a36a2SIngo Molnar 849905a36a2SIngo Molnar#ifdef CONFIG_SMP 8504d732138SIngo Molnarapicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt 8514d732138SIngo Molnarapicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt 8524d732138SIngo Molnarapicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt 853905a36a2SIngo Molnar#endif 854905a36a2SIngo Molnar 8554d732138SIngo Molnarapicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt 8564d732138SIngo Molnarapicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt 857905a36a2SIngo Molnar 858905a36a2SIngo Molnar#ifdef CONFIG_IRQ_WORK 8594d732138SIngo Molnarapicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt 860905a36a2SIngo Molnar#endif 861905a36a2SIngo Molnar 862905a36a2SIngo Molnar/* 863905a36a2SIngo Molnar * Exception entry points. 864905a36a2SIngo Molnar */ 8658f34c5b5SThomas Gleixner#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss_rw) + (TSS_ist + (x) * 8) 866905a36a2SIngo Molnar 867*2fd37912SPeter Zijlstra.macro idtentry_part do_sym, has_error_code:req, paranoid:req, shift_ist=-1, ist_offset=0 868*2fd37912SPeter Zijlstra 869*2fd37912SPeter Zijlstra .if \paranoid 870*2fd37912SPeter Zijlstra call paranoid_entry 871*2fd37912SPeter Zijlstra /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */ 872*2fd37912SPeter Zijlstra .else 873*2fd37912SPeter Zijlstra call error_entry 874*2fd37912SPeter Zijlstra .endif 875*2fd37912SPeter Zijlstra UNWIND_HINT_REGS 876*2fd37912SPeter Zijlstra 877*2fd37912SPeter Zijlstra .if \paranoid 878*2fd37912SPeter Zijlstra .if \shift_ist != -1 879*2fd37912SPeter Zijlstra TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */ 880*2fd37912SPeter Zijlstra .else 881*2fd37912SPeter Zijlstra TRACE_IRQS_OFF 882*2fd37912SPeter Zijlstra .endif 883*2fd37912SPeter Zijlstra .endif 884*2fd37912SPeter Zijlstra 885*2fd37912SPeter Zijlstra movq %rsp, %rdi /* pt_regs pointer */ 886*2fd37912SPeter Zijlstra 887*2fd37912SPeter Zijlstra .if \has_error_code 888*2fd37912SPeter Zijlstra movq ORIG_RAX(%rsp), %rsi /* get error code */ 889*2fd37912SPeter Zijlstra movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 890*2fd37912SPeter Zijlstra .else 891*2fd37912SPeter Zijlstra xorl %esi, %esi /* no error code */ 892*2fd37912SPeter Zijlstra .endif 893*2fd37912SPeter Zijlstra 894*2fd37912SPeter Zijlstra .if \shift_ist != -1 895*2fd37912SPeter Zijlstra subq $\ist_offset, CPU_TSS_IST(\shift_ist) 896*2fd37912SPeter Zijlstra .endif 897*2fd37912SPeter Zijlstra 898*2fd37912SPeter Zijlstra call \do_sym 899*2fd37912SPeter Zijlstra 900*2fd37912SPeter Zijlstra .if \shift_ist != -1 901*2fd37912SPeter Zijlstra addq $\ist_offset, CPU_TSS_IST(\shift_ist) 902*2fd37912SPeter Zijlstra .endif 903*2fd37912SPeter Zijlstra 904*2fd37912SPeter Zijlstra .if \paranoid 905*2fd37912SPeter Zijlstra /* this procedure expect "no swapgs" flag in ebx */ 906*2fd37912SPeter Zijlstra jmp paranoid_exit 907*2fd37912SPeter Zijlstra .else 908*2fd37912SPeter Zijlstra jmp error_exit 909*2fd37912SPeter Zijlstra .endif 910*2fd37912SPeter Zijlstra 911*2fd37912SPeter Zijlstra.endm 912*2fd37912SPeter Zijlstra 913bd7b1f7cSAndy Lutomirski/** 914bd7b1f7cSAndy Lutomirski * idtentry - Generate an IDT entry stub 915bd7b1f7cSAndy Lutomirski * @sym: Name of the generated entry point 916bd7b1f7cSAndy Lutomirski * @do_sym: C function to be called 917bd7b1f7cSAndy Lutomirski * @has_error_code: True if this IDT vector has an error code on the stack 918bd7b1f7cSAndy Lutomirski * @paranoid: non-zero means that this vector may be invoked from 919bd7b1f7cSAndy Lutomirski * kernel mode with user GSBASE and/or user CR3. 920bd7b1f7cSAndy Lutomirski * 2 is special -- see below. 921bd7b1f7cSAndy Lutomirski * @shift_ist: Set to an IST index if entries from kernel mode should 922bd7b1f7cSAndy Lutomirski * decrement the IST stack so that nested entries get a 923bd7b1f7cSAndy Lutomirski * fresh stack. (This is for #DB, which has a nasty habit 924bd7b1f7cSAndy Lutomirski * of recursing.) 925bd7b1f7cSAndy Lutomirski * 926bd7b1f7cSAndy Lutomirski * idtentry generates an IDT stub that sets up a usable kernel context, 927bd7b1f7cSAndy Lutomirski * creates struct pt_regs, and calls @do_sym. The stub has the following 928bd7b1f7cSAndy Lutomirski * special behaviors: 929bd7b1f7cSAndy Lutomirski * 930bd7b1f7cSAndy Lutomirski * On an entry from user mode, the stub switches from the trampoline or 931bd7b1f7cSAndy Lutomirski * IST stack to the normal thread stack. On an exit to user mode, the 932bd7b1f7cSAndy Lutomirski * normal exit-to-usermode path is invoked. 933bd7b1f7cSAndy Lutomirski * 934bd7b1f7cSAndy Lutomirski * On an exit to kernel mode, if @paranoid == 0, we check for preemption, 935bd7b1f7cSAndy Lutomirski * whereas we omit the preemption check if @paranoid != 0. This is purely 936bd7b1f7cSAndy Lutomirski * because the implementation is simpler this way. The kernel only needs 937bd7b1f7cSAndy Lutomirski * to check for asynchronous kernel preemption when IRQ handlers return. 938bd7b1f7cSAndy Lutomirski * 939bd7b1f7cSAndy Lutomirski * If @paranoid == 0, then the stub will handle IRET faults by pretending 940bd7b1f7cSAndy Lutomirski * that the fault came from user mode. It will handle gs_change faults by 941bd7b1f7cSAndy Lutomirski * pretending that the fault happened with kernel GSBASE. Since this handling 942bd7b1f7cSAndy Lutomirski * is omitted for @paranoid != 0, the #GP, #SS, and #NP stubs must have 943bd7b1f7cSAndy Lutomirski * @paranoid == 0. This special handling will do the wrong thing for 944bd7b1f7cSAndy Lutomirski * espfix-induced #DF on IRET, so #DF must not use @paranoid == 0. 945bd7b1f7cSAndy Lutomirski * 946bd7b1f7cSAndy Lutomirski * @paranoid == 2 is special: the stub will never switch stacks. This is for 947bd7b1f7cSAndy Lutomirski * #DF: if the thread stack is somehow unusable, we'll still get a useful OOPS. 948bd7b1f7cSAndy Lutomirski */ 949d2d8b146SLinus Torvalds.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 ist_offset=0 create_gap=0 950905a36a2SIngo MolnarENTRY(\sym) 95198990a33SJosh Poimboeuf UNWIND_HINT_IRET_REGS offset=\has_error_code*8 9528c1f7558SJosh Poimboeuf 953905a36a2SIngo Molnar /* Sanity check */ 954905a36a2SIngo Molnar .if \shift_ist != -1 && \paranoid == 0 955905a36a2SIngo Molnar .error "using shift_ist requires paranoid=1" 956905a36a2SIngo Molnar .endif 957905a36a2SIngo Molnar 958905a36a2SIngo Molnar ASM_CLAC 959905a36a2SIngo Molnar 96082c62fa0SJosh Poimboeuf .if \has_error_code == 0 961905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 962905a36a2SIngo Molnar .endif 963905a36a2SIngo Molnar 964071ccc96SAndy Lutomirski .if \paranoid == 1 9659e809d15SDominik Brodowski testb $3, CS-ORIG_RAX(%rsp) /* If coming from userspace, switch stacks */ 9667f2590a1SAndy Lutomirski jnz .Lfrom_usermode_switch_stack_\@ 967905a36a2SIngo Molnar .endif 9687f2590a1SAndy Lutomirski 9692700fefdSJosh Poimboeuf .if \create_gap == 1 9702700fefdSJosh Poimboeuf /* 9712700fefdSJosh Poimboeuf * If coming from kernel space, create a 6-word gap to allow the 9722700fefdSJosh Poimboeuf * int3 handler to emulate a call instruction. 9732700fefdSJosh Poimboeuf */ 9742700fefdSJosh Poimboeuf testb $3, CS-ORIG_RAX(%rsp) 9752700fefdSJosh Poimboeuf jnz .Lfrom_usermode_no_gap_\@ 9762700fefdSJosh Poimboeuf .rept 6 9772700fefdSJosh Poimboeuf pushq 5*8(%rsp) 9782700fefdSJosh Poimboeuf .endr 9792700fefdSJosh Poimboeuf UNWIND_HINT_IRET_REGS offset=8 9802700fefdSJosh Poimboeuf.Lfrom_usermode_no_gap_\@: 9812700fefdSJosh Poimboeuf .endif 9822700fefdSJosh Poimboeuf 983*2fd37912SPeter Zijlstra idtentry_part \do_sym, \has_error_code, \paranoid, \shift_ist, \ist_offset 984905a36a2SIngo Molnar 985071ccc96SAndy Lutomirski .if \paranoid == 1 986905a36a2SIngo Molnar /* 9877f2590a1SAndy Lutomirski * Entry from userspace. Switch stacks and treat it 988905a36a2SIngo Molnar * as a normal entry. This means that paranoid handlers 989905a36a2SIngo Molnar * run in real process context if user_mode(regs). 990905a36a2SIngo Molnar */ 9917f2590a1SAndy Lutomirski.Lfrom_usermode_switch_stack_\@: 992*2fd37912SPeter Zijlstra idtentry_part \do_sym, \has_error_code, paranoid=0 993905a36a2SIngo Molnar .endif 994905a36a2SIngo Molnar 995a50480cbSAndrea Righi_ASM_NOKPROBE(\sym) 996905a36a2SIngo MolnarEND(\sym) 997905a36a2SIngo Molnar.endm 998905a36a2SIngo Molnar 999905a36a2SIngo Molnaridtentry divide_error do_divide_error has_error_code=0 1000905a36a2SIngo Molnaridtentry overflow do_overflow has_error_code=0 1001905a36a2SIngo Molnaridtentry bounds do_bounds has_error_code=0 1002905a36a2SIngo Molnaridtentry invalid_op do_invalid_op has_error_code=0 1003905a36a2SIngo Molnaridtentry device_not_available do_device_not_available has_error_code=0 1004905a36a2SIngo Molnaridtentry double_fault do_double_fault has_error_code=1 paranoid=2 1005905a36a2SIngo Molnaridtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0 1006905a36a2SIngo Molnaridtentry invalid_TSS do_invalid_TSS has_error_code=1 1007905a36a2SIngo Molnaridtentry segment_not_present do_segment_not_present has_error_code=1 1008905a36a2SIngo Molnaridtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0 1009905a36a2SIngo Molnaridtentry coprocessor_error do_coprocessor_error has_error_code=0 1010905a36a2SIngo Molnaridtentry alignment_check do_alignment_check has_error_code=1 1011905a36a2SIngo Molnaridtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0 1012905a36a2SIngo Molnar 1013905a36a2SIngo Molnar 10144d732138SIngo Molnar /* 10154d732138SIngo Molnar * Reload gs selector with exception handling 10164d732138SIngo Molnar * edi: new selector 10174d732138SIngo Molnar */ 1018905a36a2SIngo MolnarENTRY(native_load_gs_index) 10198c1f7558SJosh Poimboeuf FRAME_BEGIN 1020905a36a2SIngo Molnar pushfq 1021905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI) 1022ca37e57bSAndy Lutomirski TRACE_IRQS_OFF 1023905a36a2SIngo Molnar SWAPGS 102442c748bbSBorislav Petkov.Lgs_change: 1025905a36a2SIngo Molnar movl %edi, %gs 102696e5d28aSBorislav Petkov2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE 1027905a36a2SIngo Molnar SWAPGS 1028ca37e57bSAndy Lutomirski TRACE_IRQS_FLAGS (%rsp) 1029905a36a2SIngo Molnar popfq 10308c1f7558SJosh Poimboeuf FRAME_END 1031905a36a2SIngo Molnar ret 10328c1f7558SJosh PoimboeufENDPROC(native_load_gs_index) 1033784d5699SAl ViroEXPORT_SYMBOL(native_load_gs_index) 1034905a36a2SIngo Molnar 103542c748bbSBorislav Petkov _ASM_EXTABLE(.Lgs_change, bad_gs) 1036905a36a2SIngo Molnar .section .fixup, "ax" 1037905a36a2SIngo Molnar /* running with kernelgs */ 1038905a36a2SIngo Molnarbad_gs: 1039905a36a2SIngo Molnar SWAPGS /* switch back to user gs */ 1040b038c842SAndy Lutomirski.macro ZAP_GS 1041b038c842SAndy Lutomirski /* This can't be a string because the preprocessor needs to see it. */ 1042b038c842SAndy Lutomirski movl $__USER_DS, %eax 1043b038c842SAndy Lutomirski movl %eax, %gs 1044b038c842SAndy Lutomirski.endm 1045b038c842SAndy Lutomirski ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG 1046905a36a2SIngo Molnar xorl %eax, %eax 1047905a36a2SIngo Molnar movl %eax, %gs 1048905a36a2SIngo Molnar jmp 2b 1049905a36a2SIngo Molnar .previous 1050905a36a2SIngo Molnar 1051905a36a2SIngo Molnar/* Call softirq on interrupt stack. Interrupts are off. */ 1052905a36a2SIngo MolnarENTRY(do_softirq_own_stack) 1053905a36a2SIngo Molnar pushq %rbp 1054905a36a2SIngo Molnar mov %rsp, %rbp 10558c1f7558SJosh Poimboeuf ENTER_IRQ_STACK regs=0 old_rsp=%r11 1056905a36a2SIngo Molnar call __do_softirq 10578c1f7558SJosh Poimboeuf LEAVE_IRQ_STACK regs=0 1058905a36a2SIngo Molnar leaveq 1059905a36a2SIngo Molnar ret 10608c1f7558SJosh PoimboeufENDPROC(do_softirq_own_stack) 1061905a36a2SIngo Molnar 106228c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV 10635878d5d6SJuergen Grossidtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0 1064905a36a2SIngo Molnar 1065905a36a2SIngo Molnar/* 1066905a36a2SIngo Molnar * A note on the "critical region" in our callback handler. 1067905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring 1068905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled 1069905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before 1070905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still 1071905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack. 1072905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd 1073905a36a2SIngo Molnar * like to avoid the possibility. 1074905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an 1075905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current 1076905a36a2SIngo Molnar * activation and restart the handler using the previous one. 1077905a36a2SIngo Molnar */ 10784d732138SIngo MolnarENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */ 10794d732138SIngo Molnar 1080905a36a2SIngo Molnar/* 1081905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will 1082905a36a2SIngo Molnar * see the correct pointer to the pt_regs 1083905a36a2SIngo Molnar */ 10848c1f7558SJosh Poimboeuf UNWIND_HINT_FUNC 10854d732138SIngo Molnar movq %rdi, %rsp /* we don't return, adjust the stack frame */ 10868c1f7558SJosh Poimboeuf UNWIND_HINT_REGS 10871d3e53e8SAndy Lutomirski 10881d3e53e8SAndy Lutomirski ENTER_IRQ_STACK old_rsp=%r10 1089905a36a2SIngo Molnar call xen_evtchn_do_upcall 10901d3e53e8SAndy Lutomirski LEAVE_IRQ_STACK 10911d3e53e8SAndy Lutomirski 1092905a36a2SIngo Molnar#ifndef CONFIG_PREEMPT 1093905a36a2SIngo Molnar call xen_maybe_preempt_hcall 1094905a36a2SIngo Molnar#endif 1095905a36a2SIngo Molnar jmp error_exit 1096905a36a2SIngo MolnarEND(xen_do_hypervisor_callback) 1097905a36a2SIngo Molnar 1098905a36a2SIngo Molnar/* 1099905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes. 1100905a36a2SIngo Molnar * We get here for two reasons: 1101905a36a2SIngo Molnar * 1. Fault while reloading DS, ES, FS or GS 1102905a36a2SIngo Molnar * 2. Fault while executing IRET 1103905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment 1104905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others. 1105905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the 1106905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall 1107905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks. 1108905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register 1109905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1. 1110905a36a2SIngo Molnar */ 1111905a36a2SIngo MolnarENTRY(xen_failsafe_callback) 11128c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 1113905a36a2SIngo Molnar movl %ds, %ecx 1114905a36a2SIngo Molnar cmpw %cx, 0x10(%rsp) 1115905a36a2SIngo Molnar jne 1f 1116905a36a2SIngo Molnar movl %es, %ecx 1117905a36a2SIngo Molnar cmpw %cx, 0x18(%rsp) 1118905a36a2SIngo Molnar jne 1f 1119905a36a2SIngo Molnar movl %fs, %ecx 1120905a36a2SIngo Molnar cmpw %cx, 0x20(%rsp) 1121905a36a2SIngo Molnar jne 1f 1122905a36a2SIngo Molnar movl %gs, %ecx 1123905a36a2SIngo Molnar cmpw %cx, 0x28(%rsp) 1124905a36a2SIngo Molnar jne 1f 1125905a36a2SIngo Molnar /* All segments match their saved values => Category 2 (Bad IRET). */ 1126905a36a2SIngo Molnar movq (%rsp), %rcx 1127905a36a2SIngo Molnar movq 8(%rsp), %r11 1128905a36a2SIngo Molnar addq $0x30, %rsp 1129905a36a2SIngo Molnar pushq $0 /* RIP */ 11308c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS offset=8 1131905a36a2SIngo Molnar jmp general_protection 1132905a36a2SIngo Molnar1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */ 1133905a36a2SIngo Molnar movq (%rsp), %rcx 1134905a36a2SIngo Molnar movq 8(%rsp), %r11 1135905a36a2SIngo Molnar addq $0x30, %rsp 11368c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 1137905a36a2SIngo Molnar pushq $-1 /* orig_ax = -1 => not a system call */ 11383f01daecSDominik Brodowski PUSH_AND_CLEAR_REGS 1139946c1911SJosh Poimboeuf ENCODE_FRAME_POINTER 1140905a36a2SIngo Molnar jmp error_exit 1141905a36a2SIngo MolnarEND(xen_failsafe_callback) 114228c11b0fSJuergen Gross#endif /* CONFIG_XEN_PV */ 1143905a36a2SIngo Molnar 114428c11b0fSJuergen Gross#ifdef CONFIG_XEN_PVHVM 1145905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 1146905a36a2SIngo Molnar xen_hvm_callback_vector xen_evtchn_do_upcall 114728c11b0fSJuergen Gross#endif 1148905a36a2SIngo Molnar 1149905a36a2SIngo Molnar 1150905a36a2SIngo Molnar#if IS_ENABLED(CONFIG_HYPERV) 1151905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 1152905a36a2SIngo Molnar hyperv_callback_vector hyperv_vector_handler 115393286261SVitaly Kuznetsov 115493286261SVitaly Kuznetsovapicinterrupt3 HYPERV_REENLIGHTENMENT_VECTOR \ 115593286261SVitaly Kuznetsov hyperv_reenlightenment_vector hyperv_reenlightenment_intr 1156248e742aSMichael Kelley 1157248e742aSMichael Kelleyapicinterrupt3 HYPERV_STIMER0_VECTOR \ 1158248e742aSMichael Kelley hv_stimer0_callback_vector hv_stimer0_vector_handler 1159905a36a2SIngo Molnar#endif /* CONFIG_HYPERV */ 1160905a36a2SIngo Molnar 1161498ad393SZhao Yakui#if IS_ENABLED(CONFIG_ACRN_GUEST) 1162498ad393SZhao Yakuiapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 1163498ad393SZhao Yakui acrn_hv_callback_vector acrn_hv_vector_handler 1164498ad393SZhao Yakui#endif 1165498ad393SZhao Yakui 11662a594d4cSThomas Gleixneridtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=IST_INDEX_DB ist_offset=DB_STACK_OFFSET 11672700fefdSJosh Poimboeufidtentry int3 do_int3 has_error_code=0 create_gap=1 1168905a36a2SIngo Molnaridtentry stack_segment do_stack_segment has_error_code=1 11694d732138SIngo Molnar 117028c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV 117143e41110SJuergen Grossidtentry xennmi do_nmi has_error_code=0 11725878d5d6SJuergen Grossidtentry xendebug do_debug has_error_code=0 11735878d5d6SJuergen Grossidtentry xenint3 do_int3 has_error_code=0 1174905a36a2SIngo Molnar#endif 11754d732138SIngo Molnar 1176905a36a2SIngo Molnaridtentry general_protection do_general_protection has_error_code=1 117711a7ffb0SThomas Gleixneridtentry page_fault do_page_fault has_error_code=1 11784d732138SIngo Molnar 1179905a36a2SIngo Molnar#ifdef CONFIG_KVM_GUEST 1180905a36a2SIngo Molnaridtentry async_page_fault do_async_page_fault has_error_code=1 1181905a36a2SIngo Molnar#endif 11824d732138SIngo Molnar 1183905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE 11846f41c34dSThomas Gleixneridtentry machine_check do_mce has_error_code=0 paranoid=1 1185905a36a2SIngo Molnar#endif 1186905a36a2SIngo Molnar 1187905a36a2SIngo Molnar/* 11889e809d15SDominik Brodowski * Save all registers in pt_regs, and switch gs if needed. 1189905a36a2SIngo Molnar * Use slow, but surefire "are we in kernel?" check. 1190905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise 1191905a36a2SIngo Molnar */ 1192905a36a2SIngo MolnarENTRY(paranoid_entry) 11938c1f7558SJosh Poimboeuf UNWIND_HINT_FUNC 1194905a36a2SIngo Molnar cld 11959e809d15SDominik Brodowski PUSH_AND_CLEAR_REGS save_ret=1 11969e809d15SDominik Brodowski ENCODE_FRAME_POINTER 8 1197905a36a2SIngo Molnar movl $1, %ebx 1198905a36a2SIngo Molnar movl $MSR_GS_BASE, %ecx 1199905a36a2SIngo Molnar rdmsr 1200905a36a2SIngo Molnar testl %edx, %edx 1201905a36a2SIngo Molnar js 1f /* negative -> in kernel */ 1202905a36a2SIngo Molnar SWAPGS 1203905a36a2SIngo Molnar xorl %ebx, %ebx 12048a09317bSDave Hansen 12058a09317bSDave Hansen1: 120616561f27SDave Hansen /* 120716561f27SDave Hansen * Always stash CR3 in %r14. This value will be restored, 1208ae852495SAndy Lutomirski * verbatim, at exit. Needed if paranoid_entry interrupted 1209ae852495SAndy Lutomirski * another entry that already switched to the user CR3 value 1210ae852495SAndy Lutomirski * but has not yet returned to userspace. 121116561f27SDave Hansen * 121216561f27SDave Hansen * This is also why CS (stashed in the "iret frame" by the 121316561f27SDave Hansen * hardware at entry) can not be used: this may be a return 1214ae852495SAndy Lutomirski * to kernel code, but with a user CR3 value. 121516561f27SDave Hansen */ 12168a09317bSDave Hansen SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14 12178a09317bSDave Hansen 12188a09317bSDave Hansen ret 1219905a36a2SIngo MolnarEND(paranoid_entry) 1220905a36a2SIngo Molnar 1221905a36a2SIngo Molnar/* 1222905a36a2SIngo Molnar * "Paranoid" exit path from exception stack. This is invoked 1223905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came 1224905a36a2SIngo Molnar * from kernel space. 1225905a36a2SIngo Molnar * 1226905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early 1227905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would 1228905a36a2SIngo Molnar * be complicated. Fortunately, we there's no good reason 1229905a36a2SIngo Molnar * to try to handle preemption here. 12304d732138SIngo Molnar * 12314d732138SIngo Molnar * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) 1232905a36a2SIngo Molnar */ 1233905a36a2SIngo MolnarENTRY(paranoid_exit) 12348c1f7558SJosh Poimboeuf UNWIND_HINT_REGS 12352140a994SJan Beulich DISABLE_INTERRUPTS(CLBR_ANY) 1236905a36a2SIngo Molnar TRACE_IRQS_OFF_DEBUG 1237905a36a2SIngo Molnar testl %ebx, %ebx /* swapgs needed? */ 1238e5317832SAndy Lutomirski jnz .Lparanoid_exit_no_swapgs 1239905a36a2SIngo Molnar TRACE_IRQS_IRETQ 124016561f27SDave Hansen /* Always restore stashed CR3 value (see paranoid_entry) */ 124121e94459SPeter Zijlstra RESTORE_CR3 scratch_reg=%rbx save_reg=%r14 1242905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1243e5317832SAndy Lutomirski jmp .Lparanoid_exit_restore 1244e5317832SAndy Lutomirski.Lparanoid_exit_no_swapgs: 1245905a36a2SIngo Molnar TRACE_IRQS_IRETQ_DEBUG 124616561f27SDave Hansen /* Always restore stashed CR3 value (see paranoid_entry) */ 1247e4865757SIngo Molnar RESTORE_CR3 scratch_reg=%rbx save_reg=%r14 1248e5317832SAndy Lutomirski.Lparanoid_exit_restore: 1249e5317832SAndy Lutomirski jmp restore_regs_and_return_to_kernel 1250905a36a2SIngo MolnarEND(paranoid_exit) 1251905a36a2SIngo Molnar 1252905a36a2SIngo Molnar/* 12539e809d15SDominik Brodowski * Save all registers in pt_regs, and switch GS if needed. 1254905a36a2SIngo Molnar */ 1255905a36a2SIngo MolnarENTRY(error_entry) 12569e809d15SDominik Brodowski UNWIND_HINT_FUNC 1257905a36a2SIngo Molnar cld 12589e809d15SDominik Brodowski PUSH_AND_CLEAR_REGS save_ret=1 12599e809d15SDominik Brodowski ENCODE_FRAME_POINTER 8 1260905a36a2SIngo Molnar testb $3, CS+8(%rsp) 1261cb6f64edSAndy Lutomirski jz .Lerror_kernelspace 1262539f5113SAndy Lutomirski 1263cb6f64edSAndy Lutomirski /* 1264cb6f64edSAndy Lutomirski * We entered from user mode or we're pretending to have entered 1265cb6f64edSAndy Lutomirski * from user mode due to an IRET fault. 1266cb6f64edSAndy Lutomirski */ 1267905a36a2SIngo Molnar SWAPGS 12688a09317bSDave Hansen /* We have user CR3. Change to kernel CR3. */ 12698a09317bSDave Hansen SWITCH_TO_KERNEL_CR3 scratch_reg=%rax 1270539f5113SAndy Lutomirski 1271cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_after_swapgs: 12727f2590a1SAndy Lutomirski /* Put us onto the real thread stack. */ 12737f2590a1SAndy Lutomirski popq %r12 /* save return addr in %12 */ 12747f2590a1SAndy Lutomirski movq %rsp, %rdi /* arg0 = pt_regs pointer */ 12757f2590a1SAndy Lutomirski call sync_regs 12767f2590a1SAndy Lutomirski movq %rax, %rsp /* switch stack */ 12777f2590a1SAndy Lutomirski ENCODE_FRAME_POINTER 12787f2590a1SAndy Lutomirski pushq %r12 12797f2590a1SAndy Lutomirski 1280f1075053SAndy Lutomirski /* 1281f1075053SAndy Lutomirski * We need to tell lockdep that IRQs are off. We can't do this until 1282f1075053SAndy Lutomirski * we fix gsbase, and we should do it before enter_from_user_mode 1283f1075053SAndy Lutomirski * (which can take locks). 1284f1075053SAndy Lutomirski */ 1285f1075053SAndy Lutomirski TRACE_IRQS_OFF 1286478dc89cSAndy Lutomirski CALL_enter_from_user_mode 1287f1075053SAndy Lutomirski ret 128802bc7768SAndy Lutomirski 1289cb6f64edSAndy Lutomirski.Lerror_entry_done: 1290905a36a2SIngo Molnar TRACE_IRQS_OFF 1291905a36a2SIngo Molnar ret 1292905a36a2SIngo Molnar 1293905a36a2SIngo Molnar /* 1294905a36a2SIngo Molnar * There are two places in the kernel that can potentially fault with 1295905a36a2SIngo Molnar * usergs. Handle them here. B stepping K8s sometimes report a 1296905a36a2SIngo Molnar * truncated RIP for IRET exceptions returning to compat mode. Check 1297905a36a2SIngo Molnar * for these here too. 1298905a36a2SIngo Molnar */ 1299cb6f64edSAndy Lutomirski.Lerror_kernelspace: 1300905a36a2SIngo Molnar leaq native_irq_return_iret(%rip), %rcx 1301905a36a2SIngo Molnar cmpq %rcx, RIP+8(%rsp) 1302cb6f64edSAndy Lutomirski je .Lerror_bad_iret 1303905a36a2SIngo Molnar movl %ecx, %eax /* zero extend */ 1304905a36a2SIngo Molnar cmpq %rax, RIP+8(%rsp) 1305cb6f64edSAndy Lutomirski je .Lbstep_iret 130642c748bbSBorislav Petkov cmpq $.Lgs_change, RIP+8(%rsp) 1307cb6f64edSAndy Lutomirski jne .Lerror_entry_done 1308539f5113SAndy Lutomirski 1309539f5113SAndy Lutomirski /* 131042c748bbSBorislav Petkov * hack: .Lgs_change can fail with user gsbase. If this happens, fix up 1311539f5113SAndy Lutomirski * gsbase and proceed. We'll fix up the exception and land in 131242c748bbSBorislav Petkov * .Lgs_change's error handler with kernel gsbase. 1313539f5113SAndy Lutomirski */ 13142fa5f04fSWanpeng Li SWAPGS 13158a09317bSDave Hansen SWITCH_TO_KERNEL_CR3 scratch_reg=%rax 13162fa5f04fSWanpeng Li jmp .Lerror_entry_done 1317905a36a2SIngo Molnar 1318cb6f64edSAndy Lutomirski.Lbstep_iret: 1319905a36a2SIngo Molnar /* Fix truncated RIP */ 1320905a36a2SIngo Molnar movq %rcx, RIP+8(%rsp) 1321905a36a2SIngo Molnar /* fall through */ 1322905a36a2SIngo Molnar 1323cb6f64edSAndy Lutomirski.Lerror_bad_iret: 1324539f5113SAndy Lutomirski /* 13258a09317bSDave Hansen * We came from an IRET to user mode, so we have user 13268a09317bSDave Hansen * gsbase and CR3. Switch to kernel gsbase and CR3: 1327539f5113SAndy Lutomirski */ 1328905a36a2SIngo Molnar SWAPGS 13298a09317bSDave Hansen SWITCH_TO_KERNEL_CR3 scratch_reg=%rax 1330539f5113SAndy Lutomirski 1331539f5113SAndy Lutomirski /* 1332539f5113SAndy Lutomirski * Pretend that the exception came from user mode: set up pt_regs 1333b3681dd5SAndy Lutomirski * as if we faulted immediately after IRET. 1334539f5113SAndy Lutomirski */ 1335905a36a2SIngo Molnar mov %rsp, %rdi 1336905a36a2SIngo Molnar call fixup_bad_iret 1337905a36a2SIngo Molnar mov %rax, %rsp 1338cb6f64edSAndy Lutomirski jmp .Lerror_entry_from_usermode_after_swapgs 1339905a36a2SIngo MolnarEND(error_entry) 1340905a36a2SIngo Molnar 1341905a36a2SIngo MolnarENTRY(error_exit) 13428c1f7558SJosh Poimboeuf UNWIND_HINT_REGS 13432140a994SJan Beulich DISABLE_INTERRUPTS(CLBR_ANY) 1344905a36a2SIngo Molnar TRACE_IRQS_OFF 1345b3681dd5SAndy Lutomirski testb $3, CS(%rsp) 1346b3681dd5SAndy Lutomirski jz retint_kernel 1347905a36a2SIngo Molnar jmp retint_user 1348905a36a2SIngo MolnarEND(error_exit) 1349905a36a2SIngo Molnar 1350929bacecSAndy Lutomirski/* 1351929bacecSAndy Lutomirski * Runs on exception stack. Xen PV does not go through this path at all, 1352929bacecSAndy Lutomirski * so we can use real assembly here. 13538a09317bSDave Hansen * 13548a09317bSDave Hansen * Registers: 13558a09317bSDave Hansen * %r14: Used to save/restore the CR3 of the interrupted context 13568a09317bSDave Hansen * when PAGE_TABLE_ISOLATION is in use. Do not clobber. 1357929bacecSAndy Lutomirski */ 1358905a36a2SIngo MolnarENTRY(nmi) 13598c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 1360929bacecSAndy Lutomirski 1361fc57a7c6SAndy Lutomirski /* 1362905a36a2SIngo Molnar * We allow breakpoints in NMIs. If a breakpoint occurs, then 1363905a36a2SIngo Molnar * the iretq it performs will take us out of NMI context. 1364905a36a2SIngo Molnar * This means that we can have nested NMIs where the next 1365905a36a2SIngo Molnar * NMI is using the top of the stack of the previous NMI. We 1366905a36a2SIngo Molnar * can't let it execute because the nested NMI will corrupt the 1367905a36a2SIngo Molnar * stack of the previous NMI. NMI handlers are not re-entrant 1368905a36a2SIngo Molnar * anyway. 1369905a36a2SIngo Molnar * 1370905a36a2SIngo Molnar * To handle this case we do the following: 1371905a36a2SIngo Molnar * Check the a special location on the stack that contains 1372905a36a2SIngo Molnar * a variable that is set when NMIs are executing. 1373905a36a2SIngo Molnar * The interrupted task's stack is also checked to see if it 1374905a36a2SIngo Molnar * is an NMI stack. 1375905a36a2SIngo Molnar * If the variable is not set and the stack is not the NMI 1376905a36a2SIngo Molnar * stack then: 1377905a36a2SIngo Molnar * o Set the special variable on the stack 13780b22930eSAndy Lutomirski * o Copy the interrupt frame into an "outermost" location on the 13790b22930eSAndy Lutomirski * stack 13800b22930eSAndy Lutomirski * o Copy the interrupt frame into an "iret" location on the stack 1381905a36a2SIngo Molnar * o Continue processing the NMI 1382905a36a2SIngo Molnar * If the variable is set or the previous stack is the NMI stack: 13830b22930eSAndy Lutomirski * o Modify the "iret" location to jump to the repeat_nmi 1384905a36a2SIngo Molnar * o return back to the first NMI 1385905a36a2SIngo Molnar * 1386905a36a2SIngo Molnar * Now on exit of the first NMI, we first clear the stack variable 1387905a36a2SIngo Molnar * The NMI stack will tell any nested NMIs at that point that it is 1388905a36a2SIngo Molnar * nested. Then we pop the stack normally with iret, and if there was 1389905a36a2SIngo Molnar * a nested NMI that updated the copy interrupt stack frame, a 1390905a36a2SIngo Molnar * jump will be made to the repeat_nmi code that will handle the second 1391905a36a2SIngo Molnar * NMI. 13929b6e6a83SAndy Lutomirski * 13939b6e6a83SAndy Lutomirski * However, espfix prevents us from directly returning to userspace 13949b6e6a83SAndy Lutomirski * with a single IRET instruction. Similarly, IRET to user mode 13959b6e6a83SAndy Lutomirski * can fault. We therefore handle NMIs from user space like 13969b6e6a83SAndy Lutomirski * other IST entries. 1397905a36a2SIngo Molnar */ 1398905a36a2SIngo Molnar 1399e93c1730SAndy Lutomirski ASM_CLAC 1400e93c1730SAndy Lutomirski 1401905a36a2SIngo Molnar /* Use %rdx as our temp variable throughout */ 1402905a36a2SIngo Molnar pushq %rdx 1403905a36a2SIngo Molnar 14049b6e6a83SAndy Lutomirski testb $3, CS-RIP+8(%rsp) 14059b6e6a83SAndy Lutomirski jz .Lnmi_from_kernel 1406905a36a2SIngo Molnar 1407905a36a2SIngo Molnar /* 14089b6e6a83SAndy Lutomirski * NMI from user mode. We need to run on the thread stack, but we 14099b6e6a83SAndy Lutomirski * can't go through the normal entry paths: NMIs are masked, and 14109b6e6a83SAndy Lutomirski * we don't want to enable interrupts, because then we'll end 14119b6e6a83SAndy Lutomirski * up in an awkward situation in which IRQs are on but NMIs 14129b6e6a83SAndy Lutomirski * are off. 141383c133cfSAndy Lutomirski * 141483c133cfSAndy Lutomirski * We also must not push anything to the stack before switching 141583c133cfSAndy Lutomirski * stacks lest we corrupt the "NMI executing" variable. 14169b6e6a83SAndy Lutomirski */ 14179b6e6a83SAndy Lutomirski 1418929bacecSAndy Lutomirski swapgs 14199b6e6a83SAndy Lutomirski cld 14208a09317bSDave Hansen SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx 14219b6e6a83SAndy Lutomirski movq %rsp, %rdx 14229b6e6a83SAndy Lutomirski movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 14238c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS base=%rdx offset=8 14249b6e6a83SAndy Lutomirski pushq 5*8(%rdx) /* pt_regs->ss */ 14259b6e6a83SAndy Lutomirski pushq 4*8(%rdx) /* pt_regs->rsp */ 14269b6e6a83SAndy Lutomirski pushq 3*8(%rdx) /* pt_regs->flags */ 14279b6e6a83SAndy Lutomirski pushq 2*8(%rdx) /* pt_regs->cs */ 14289b6e6a83SAndy Lutomirski pushq 1*8(%rdx) /* pt_regs->rip */ 14298c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 14309b6e6a83SAndy Lutomirski pushq $-1 /* pt_regs->orig_ax */ 143130907fd1SDominik Brodowski PUSH_AND_CLEAR_REGS rdx=(%rdx) 1432946c1911SJosh Poimboeuf ENCODE_FRAME_POINTER 14339b6e6a83SAndy Lutomirski 14349b6e6a83SAndy Lutomirski /* 14359b6e6a83SAndy Lutomirski * At this point we no longer need to worry about stack damage 14369b6e6a83SAndy Lutomirski * due to nesting -- we're on the normal thread stack and we're 14379b6e6a83SAndy Lutomirski * done with the NMI stack. 14389b6e6a83SAndy Lutomirski */ 14399b6e6a83SAndy Lutomirski 14409b6e6a83SAndy Lutomirski movq %rsp, %rdi 14419b6e6a83SAndy Lutomirski movq $-1, %rsi 14429b6e6a83SAndy Lutomirski call do_nmi 14439b6e6a83SAndy Lutomirski 14449b6e6a83SAndy Lutomirski /* 14459b6e6a83SAndy Lutomirski * Return back to user mode. We must *not* do the normal exit 1446946c1911SJosh Poimboeuf * work, because we don't want to enable interrupts. 14479b6e6a83SAndy Lutomirski */ 14488a055d7fSAndy Lutomirski jmp swapgs_restore_regs_and_return_to_usermode 14499b6e6a83SAndy Lutomirski 14509b6e6a83SAndy Lutomirski.Lnmi_from_kernel: 14519b6e6a83SAndy Lutomirski /* 14520b22930eSAndy Lutomirski * Here's what our stack frame will look like: 14530b22930eSAndy Lutomirski * +---------------------------------------------------------+ 14540b22930eSAndy Lutomirski * | original SS | 14550b22930eSAndy Lutomirski * | original Return RSP | 14560b22930eSAndy Lutomirski * | original RFLAGS | 14570b22930eSAndy Lutomirski * | original CS | 14580b22930eSAndy Lutomirski * | original RIP | 14590b22930eSAndy Lutomirski * +---------------------------------------------------------+ 14600b22930eSAndy Lutomirski * | temp storage for rdx | 14610b22930eSAndy Lutomirski * +---------------------------------------------------------+ 14620b22930eSAndy Lutomirski * | "NMI executing" variable | 14630b22930eSAndy Lutomirski * +---------------------------------------------------------+ 14640b22930eSAndy Lutomirski * | iret SS } Copied from "outermost" frame | 14650b22930eSAndy Lutomirski * | iret Return RSP } on each loop iteration; overwritten | 14660b22930eSAndy Lutomirski * | iret RFLAGS } by a nested NMI to force another | 14670b22930eSAndy Lutomirski * | iret CS } iteration if needed. | 14680b22930eSAndy Lutomirski * | iret RIP } | 14690b22930eSAndy Lutomirski * +---------------------------------------------------------+ 14700b22930eSAndy Lutomirski * | outermost SS } initialized in first_nmi; | 14710b22930eSAndy Lutomirski * | outermost Return RSP } will not be changed before | 14720b22930eSAndy Lutomirski * | outermost RFLAGS } NMI processing is done. | 14730b22930eSAndy Lutomirski * | outermost CS } Copied to "iret" frame on each | 14740b22930eSAndy Lutomirski * | outermost RIP } iteration. | 14750b22930eSAndy Lutomirski * +---------------------------------------------------------+ 14760b22930eSAndy Lutomirski * | pt_regs | 14770b22930eSAndy Lutomirski * +---------------------------------------------------------+ 14780b22930eSAndy Lutomirski * 14790b22930eSAndy Lutomirski * The "original" frame is used by hardware. Before re-enabling 14800b22930eSAndy Lutomirski * NMIs, we need to be done with it, and we need to leave enough 14810b22930eSAndy Lutomirski * space for the asm code here. 14820b22930eSAndy Lutomirski * 14830b22930eSAndy Lutomirski * We return by executing IRET while RSP points to the "iret" frame. 14840b22930eSAndy Lutomirski * That will either return for real or it will loop back into NMI 14850b22930eSAndy Lutomirski * processing. 14860b22930eSAndy Lutomirski * 14870b22930eSAndy Lutomirski * The "outermost" frame is copied to the "iret" frame on each 14880b22930eSAndy Lutomirski * iteration of the loop, so each iteration starts with the "iret" 14890b22930eSAndy Lutomirski * frame pointing to the final return target. 14900b22930eSAndy Lutomirski */ 14910b22930eSAndy Lutomirski 14920b22930eSAndy Lutomirski /* 14930b22930eSAndy Lutomirski * Determine whether we're a nested NMI. 14940b22930eSAndy Lutomirski * 1495a27507caSAndy Lutomirski * If we interrupted kernel code between repeat_nmi and 1496a27507caSAndy Lutomirski * end_repeat_nmi, then we are a nested NMI. We must not 1497a27507caSAndy Lutomirski * modify the "iret" frame because it's being written by 1498a27507caSAndy Lutomirski * the outer NMI. That's okay; the outer NMI handler is 1499a27507caSAndy Lutomirski * about to about to call do_nmi anyway, so we can just 1500a27507caSAndy Lutomirski * resume the outer NMI. 1501a27507caSAndy Lutomirski */ 1502a27507caSAndy Lutomirski 1503a27507caSAndy Lutomirski movq $repeat_nmi, %rdx 1504a27507caSAndy Lutomirski cmpq 8(%rsp), %rdx 1505a27507caSAndy Lutomirski ja 1f 1506a27507caSAndy Lutomirski movq $end_repeat_nmi, %rdx 1507a27507caSAndy Lutomirski cmpq 8(%rsp), %rdx 1508a27507caSAndy Lutomirski ja nested_nmi_out 1509a27507caSAndy Lutomirski1: 1510a27507caSAndy Lutomirski 1511a27507caSAndy Lutomirski /* 1512a27507caSAndy Lutomirski * Now check "NMI executing". If it's set, then we're nested. 15130b22930eSAndy Lutomirski * This will not detect if we interrupted an outer NMI just 15140b22930eSAndy Lutomirski * before IRET. 1515905a36a2SIngo Molnar */ 1516905a36a2SIngo Molnar cmpl $1, -8(%rsp) 1517905a36a2SIngo Molnar je nested_nmi 1518905a36a2SIngo Molnar 1519905a36a2SIngo Molnar /* 15200b22930eSAndy Lutomirski * Now test if the previous stack was an NMI stack. This covers 15210b22930eSAndy Lutomirski * the case where we interrupt an outer NMI after it clears 1522810bc075SAndy Lutomirski * "NMI executing" but before IRET. We need to be careful, though: 1523810bc075SAndy Lutomirski * there is one case in which RSP could point to the NMI stack 1524810bc075SAndy Lutomirski * despite there being no NMI active: naughty userspace controls 1525810bc075SAndy Lutomirski * RSP at the very beginning of the SYSCALL targets. We can 1526810bc075SAndy Lutomirski * pull a fast one on naughty userspace, though: we program 1527810bc075SAndy Lutomirski * SYSCALL to mask DF, so userspace cannot cause DF to be set 1528810bc075SAndy Lutomirski * if it controls the kernel's RSP. We set DF before we clear 1529810bc075SAndy Lutomirski * "NMI executing". 1530905a36a2SIngo Molnar */ 1531905a36a2SIngo Molnar lea 6*8(%rsp), %rdx 1532905a36a2SIngo Molnar /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */ 1533905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1534905a36a2SIngo Molnar /* If the stack pointer is above the NMI stack, this is a normal NMI */ 1535905a36a2SIngo Molnar ja first_nmi 15364d732138SIngo Molnar 1537905a36a2SIngo Molnar subq $EXCEPTION_STKSZ, %rdx 1538905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1539905a36a2SIngo Molnar /* If it is below the NMI stack, it is a normal NMI */ 1540905a36a2SIngo Molnar jb first_nmi 1541810bc075SAndy Lutomirski 1542810bc075SAndy Lutomirski /* Ah, it is within the NMI stack. */ 1543810bc075SAndy Lutomirski 1544810bc075SAndy Lutomirski testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp) 1545810bc075SAndy Lutomirski jz first_nmi /* RSP was user controlled. */ 1546810bc075SAndy Lutomirski 1547810bc075SAndy Lutomirski /* This is a nested NMI. */ 1548905a36a2SIngo Molnar 1549905a36a2SIngo Molnarnested_nmi: 1550905a36a2SIngo Molnar /* 15510b22930eSAndy Lutomirski * Modify the "iret" frame to point to repeat_nmi, forcing another 15520b22930eSAndy Lutomirski * iteration of NMI handling. 1553905a36a2SIngo Molnar */ 155423a781e9SAndy Lutomirski subq $8, %rsp 1555905a36a2SIngo Molnar leaq -10*8(%rsp), %rdx 1556905a36a2SIngo Molnar pushq $__KERNEL_DS 1557905a36a2SIngo Molnar pushq %rdx 1558905a36a2SIngo Molnar pushfq 1559905a36a2SIngo Molnar pushq $__KERNEL_CS 1560905a36a2SIngo Molnar pushq $repeat_nmi 1561905a36a2SIngo Molnar 1562905a36a2SIngo Molnar /* Put stack back */ 1563905a36a2SIngo Molnar addq $(6*8), %rsp 1564905a36a2SIngo Molnar 1565905a36a2SIngo Molnarnested_nmi_out: 1566905a36a2SIngo Molnar popq %rdx 1567905a36a2SIngo Molnar 15680b22930eSAndy Lutomirski /* We are returning to kernel mode, so this cannot result in a fault. */ 1569929bacecSAndy Lutomirski iretq 1570905a36a2SIngo Molnar 1571905a36a2SIngo Molnarfirst_nmi: 15720b22930eSAndy Lutomirski /* Restore rdx. */ 1573905a36a2SIngo Molnar movq (%rsp), %rdx 1574905a36a2SIngo Molnar 157536f1a77bSAndy Lutomirski /* Make room for "NMI executing". */ 157636f1a77bSAndy Lutomirski pushq $0 1577905a36a2SIngo Molnar 15780b22930eSAndy Lutomirski /* Leave room for the "iret" frame */ 1579905a36a2SIngo Molnar subq $(5*8), %rsp 1580905a36a2SIngo Molnar 15810b22930eSAndy Lutomirski /* Copy the "original" frame to the "outermost" frame */ 1582905a36a2SIngo Molnar .rept 5 1583905a36a2SIngo Molnar pushq 11*8(%rsp) 1584905a36a2SIngo Molnar .endr 15858c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 1586905a36a2SIngo Molnar 1587905a36a2SIngo Molnar /* Everything up to here is safe from nested NMIs */ 1588905a36a2SIngo Molnar 1589a97439aaSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY 1590a97439aaSAndy Lutomirski /* 1591a97439aaSAndy Lutomirski * For ease of testing, unmask NMIs right away. Disabled by 1592a97439aaSAndy Lutomirski * default because IRET is very expensive. 1593a97439aaSAndy Lutomirski */ 1594a97439aaSAndy Lutomirski pushq $0 /* SS */ 1595a97439aaSAndy Lutomirski pushq %rsp /* RSP (minus 8 because of the previous push) */ 1596a97439aaSAndy Lutomirski addq $8, (%rsp) /* Fix up RSP */ 1597a97439aaSAndy Lutomirski pushfq /* RFLAGS */ 1598a97439aaSAndy Lutomirski pushq $__KERNEL_CS /* CS */ 1599a97439aaSAndy Lutomirski pushq $1f /* RIP */ 1600929bacecSAndy Lutomirski iretq /* continues at repeat_nmi below */ 16018c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 1602a97439aaSAndy Lutomirski1: 1603a97439aaSAndy Lutomirski#endif 1604a97439aaSAndy Lutomirski 16050b22930eSAndy Lutomirskirepeat_nmi: 1606905a36a2SIngo Molnar /* 1607905a36a2SIngo Molnar * If there was a nested NMI, the first NMI's iret will return 1608905a36a2SIngo Molnar * here. But NMIs are still enabled and we can take another 1609905a36a2SIngo Molnar * nested NMI. The nested NMI checks the interrupted RIP to see 1610905a36a2SIngo Molnar * if it is between repeat_nmi and end_repeat_nmi, and if so 1611905a36a2SIngo Molnar * it will just return, as we are about to repeat an NMI anyway. 1612905a36a2SIngo Molnar * This makes it safe to copy to the stack frame that a nested 1613905a36a2SIngo Molnar * NMI will update. 16140b22930eSAndy Lutomirski * 16150b22930eSAndy Lutomirski * RSP is pointing to "outermost RIP". gsbase is unknown, but, if 16160b22930eSAndy Lutomirski * we're repeating an NMI, gsbase has the same value that it had on 16170b22930eSAndy Lutomirski * the first iteration. paranoid_entry will load the kernel 161836f1a77bSAndy Lutomirski * gsbase if needed before we call do_nmi. "NMI executing" 161936f1a77bSAndy Lutomirski * is zero. 1620905a36a2SIngo Molnar */ 162136f1a77bSAndy Lutomirski movq $1, 10*8(%rsp) /* Set "NMI executing". */ 1622905a36a2SIngo Molnar 16230b22930eSAndy Lutomirski /* 16240b22930eSAndy Lutomirski * Copy the "outermost" frame to the "iret" frame. NMIs that nest 16250b22930eSAndy Lutomirski * here must not modify the "iret" frame while we're writing to 16260b22930eSAndy Lutomirski * it or it will end up containing garbage. 16270b22930eSAndy Lutomirski */ 1628905a36a2SIngo Molnar addq $(10*8), %rsp 1629905a36a2SIngo Molnar .rept 5 1630905a36a2SIngo Molnar pushq -6*8(%rsp) 1631905a36a2SIngo Molnar .endr 1632905a36a2SIngo Molnar subq $(5*8), %rsp 1633905a36a2SIngo Molnarend_repeat_nmi: 1634905a36a2SIngo Molnar 1635905a36a2SIngo Molnar /* 16360b22930eSAndy Lutomirski * Everything below this point can be preempted by a nested NMI. 16370b22930eSAndy Lutomirski * If this happens, then the inner NMI will change the "iret" 16380b22930eSAndy Lutomirski * frame to point back to repeat_nmi. 1639905a36a2SIngo Molnar */ 1640905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 1641905a36a2SIngo Molnar 1642905a36a2SIngo Molnar /* 1643905a36a2SIngo Molnar * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit 1644905a36a2SIngo Molnar * as we should not be calling schedule in NMI context. 1645905a36a2SIngo Molnar * Even with normal interrupts enabled. An NMI should not be 1646905a36a2SIngo Molnar * setting NEED_RESCHED or anything that normal interrupts and 1647905a36a2SIngo Molnar * exceptions might do. 1648905a36a2SIngo Molnar */ 1649905a36a2SIngo Molnar call paranoid_entry 16508c1f7558SJosh Poimboeuf UNWIND_HINT_REGS 1651905a36a2SIngo Molnar 1652905a36a2SIngo Molnar /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */ 1653905a36a2SIngo Molnar movq %rsp, %rdi 1654905a36a2SIngo Molnar movq $-1, %rsi 1655905a36a2SIngo Molnar call do_nmi 1656905a36a2SIngo Molnar 165716561f27SDave Hansen /* Always restore stashed CR3 value (see paranoid_entry) */ 165821e94459SPeter Zijlstra RESTORE_CR3 scratch_reg=%r15 save_reg=%r14 16598a09317bSDave Hansen 1660905a36a2SIngo Molnar testl %ebx, %ebx /* swapgs needed? */ 1661905a36a2SIngo Molnar jnz nmi_restore 1662905a36a2SIngo Molnarnmi_swapgs: 1663905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1664905a36a2SIngo Molnarnmi_restore: 1665502af0d7SDominik Brodowski POP_REGS 16660b22930eSAndy Lutomirski 1667471ee483SAndy Lutomirski /* 1668471ee483SAndy Lutomirski * Skip orig_ax and the "outermost" frame to point RSP at the "iret" 1669471ee483SAndy Lutomirski * at the "iret" frame. 1670471ee483SAndy Lutomirski */ 1671471ee483SAndy Lutomirski addq $6*8, %rsp 1672905a36a2SIngo Molnar 1673810bc075SAndy Lutomirski /* 1674810bc075SAndy Lutomirski * Clear "NMI executing". Set DF first so that we can easily 1675810bc075SAndy Lutomirski * distinguish the remaining code between here and IRET from 1676929bacecSAndy Lutomirski * the SYSCALL entry and exit paths. 1677929bacecSAndy Lutomirski * 1678929bacecSAndy Lutomirski * We arguably should just inspect RIP instead, but I (Andy) wrote 1679929bacecSAndy Lutomirski * this code when I had the misapprehension that Xen PV supported 1680929bacecSAndy Lutomirski * NMIs, and Xen PV would break that approach. 1681810bc075SAndy Lutomirski */ 1682810bc075SAndy Lutomirski std 1683810bc075SAndy Lutomirski movq $0, 5*8(%rsp) /* clear "NMI executing" */ 16840b22930eSAndy Lutomirski 16850b22930eSAndy Lutomirski /* 1686929bacecSAndy Lutomirski * iretq reads the "iret" frame and exits the NMI stack in a 1687929bacecSAndy Lutomirski * single instruction. We are returning to kernel mode, so this 1688929bacecSAndy Lutomirski * cannot result in a fault. Similarly, we don't need to worry 1689929bacecSAndy Lutomirski * about espfix64 on the way back to kernel mode. 16900b22930eSAndy Lutomirski */ 1691929bacecSAndy Lutomirski iretq 1692905a36a2SIngo MolnarEND(nmi) 1693905a36a2SIngo Molnar 1694dffb3f9dSAndy Lutomirski#ifndef CONFIG_IA32_EMULATION 1695dffb3f9dSAndy Lutomirski/* 1696dffb3f9dSAndy Lutomirski * This handles SYSCALL from 32-bit code. There is no way to program 1697dffb3f9dSAndy Lutomirski * MSRs to fully disable 32-bit SYSCALL. 1698dffb3f9dSAndy Lutomirski */ 1699905a36a2SIngo MolnarENTRY(ignore_sysret) 17008c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 1701905a36a2SIngo Molnar mov $-ENOSYS, %eax 1702905a36a2SIngo Molnar sysret 1703905a36a2SIngo MolnarEND(ignore_sysret) 1704dffb3f9dSAndy Lutomirski#endif 17052deb4be2SAndy Lutomirski 17062deb4be2SAndy LutomirskiENTRY(rewind_stack_do_exit) 17078c1f7558SJosh Poimboeuf UNWIND_HINT_FUNC 17082deb4be2SAndy Lutomirski /* Prevent any naive code from trying to unwind to our caller. */ 17092deb4be2SAndy Lutomirski xorl %ebp, %ebp 17102deb4be2SAndy Lutomirski 17112deb4be2SAndy Lutomirski movq PER_CPU_VAR(cpu_current_top_of_stack), %rax 17128c1f7558SJosh Poimboeuf leaq -PTREGS_SIZE(%rax), %rsp 17138c1f7558SJosh Poimboeuf UNWIND_HINT_FUNC sp_offset=PTREGS_SIZE 17142deb4be2SAndy Lutomirski 17152deb4be2SAndy Lutomirski call do_exit 17162deb4be2SAndy LutomirskiEND(rewind_stack_do_exit) 1717