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: 186dcc5627SJiri Slaby * - SYM_FUNC_START/END:Define functions in the symbol table. 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> 25905a36a2SIngo Molnar#include <asm/asm-offsets.h> 26905a36a2SIngo Molnar#include <asm/msr.h> 27905a36a2SIngo Molnar#include <asm/unistd.h> 28905a36a2SIngo Molnar#include <asm/thread_info.h> 29905a36a2SIngo Molnar#include <asm/hw_irq.h> 30905a36a2SIngo Molnar#include <asm/page_types.h> 31905a36a2SIngo Molnar#include <asm/irqflags.h> 32905a36a2SIngo Molnar#include <asm/paravirt.h> 33905a36a2SIngo Molnar#include <asm/percpu.h> 34905a36a2SIngo Molnar#include <asm/asm.h> 35905a36a2SIngo Molnar#include <asm/smap.h> 36905a36a2SIngo Molnar#include <asm/pgtable_types.h> 37784d5699SAl Viro#include <asm/export.h> 388c1f7558SJosh Poimboeuf#include <asm/frame.h> 39cfa82a00SThomas Gleixner#include <asm/trapnr.h> 402641f08bSDavid Woodhouse#include <asm/nospec-branch.h> 41c82965f9SChang S. Bae#include <asm/fsgsbase.h> 42905a36a2SIngo Molnar#include <linux/err.h> 43905a36a2SIngo Molnar 446fd166aaSPeter Zijlstra#include "calling.h" 456fd166aaSPeter Zijlstra 46905a36a2SIngo Molnar.code64 47905a36a2SIngo Molnar.section .entry.text, "ax" 48905a36a2SIngo Molnar 49905a36a2SIngo Molnar#ifdef CONFIG_PARAVIRT 50bc7b11c0SJiri SlabySYM_CODE_START(native_usergs_sysret64) 518c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 52905a36a2SIngo Molnar swapgs 53905a36a2SIngo Molnar sysretq 54bc7b11c0SJiri SlabySYM_CODE_END(native_usergs_sysret64) 55905a36a2SIngo Molnar#endif /* CONFIG_PARAVIRT */ 56905a36a2SIngo Molnar 57905a36a2SIngo Molnar/* 584d732138SIngo Molnar * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers. 59905a36a2SIngo Molnar * 60fda57b22SAndy Lutomirski * This is the only entry point used for 64-bit system calls. The 61fda57b22SAndy Lutomirski * hardware interface is reasonably well designed and the register to 62fda57b22SAndy Lutomirski * argument mapping Linux uses fits well with the registers that are 63fda57b22SAndy Lutomirski * available when SYSCALL is used. 64fda57b22SAndy Lutomirski * 65fda57b22SAndy Lutomirski * SYSCALL instructions can be found inlined in libc implementations as 66fda57b22SAndy Lutomirski * well as some other programs and libraries. There are also a handful 67fda57b22SAndy Lutomirski * of SYSCALL instructions in the vDSO used, for example, as a 68fda57b22SAndy Lutomirski * clock_gettimeofday fallback. 69fda57b22SAndy Lutomirski * 704d732138SIngo Molnar * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11, 71905a36a2SIngo Molnar * then loads new ss, cs, and rip from previously programmed MSRs. 72905a36a2SIngo Molnar * rflags gets masked by a value from another MSR (so CLD and CLAC 73905a36a2SIngo Molnar * are not needed). SYSCALL does not save anything on the stack 74905a36a2SIngo Molnar * and does not change rsp. 75905a36a2SIngo Molnar * 76905a36a2SIngo Molnar * Registers on entry: 77905a36a2SIngo Molnar * rax system call number 78905a36a2SIngo Molnar * rcx return address 79905a36a2SIngo Molnar * r11 saved rflags (note: r11 is callee-clobbered register in C ABI) 80905a36a2SIngo Molnar * rdi arg0 81905a36a2SIngo Molnar * rsi arg1 82905a36a2SIngo Molnar * rdx arg2 83905a36a2SIngo Molnar * r10 arg3 (needs to be moved to rcx to conform to C ABI) 84905a36a2SIngo Molnar * r8 arg4 85905a36a2SIngo Molnar * r9 arg5 86905a36a2SIngo Molnar * (note: r12-r15, rbp, rbx are callee-preserved in C ABI) 87905a36a2SIngo Molnar * 88905a36a2SIngo Molnar * Only called from user space. 89905a36a2SIngo Molnar * 90905a36a2SIngo Molnar * When user can change pt_regs->foo always force IRET. That is because 91905a36a2SIngo Molnar * it deals with uncanonical addresses better. SYSRET has trouble 92905a36a2SIngo Molnar * with them due to bugs in both AMD and Intel CPUs. 93905a36a2SIngo Molnar */ 94905a36a2SIngo Molnar 95bc7b11c0SJiri SlabySYM_CODE_START(entry_SYSCALL_64) 968c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 97905a36a2SIngo Molnar 988a9949bcSAndy Lutomirski swapgs 99bf904d27SAndy Lutomirski /* tss.sp2 is scratch space. */ 10098f05b51SAndy Lutomirski movq %rsp, PER_CPU_VAR(cpu_tss_rw + TSS_sp2) 101bf904d27SAndy Lutomirski SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp 102905a36a2SIngo Molnar movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 103905a36a2SIngo Molnar 104*a13644f3SJoerg RoedelSYM_INNER_LABEL(entry_SYSCALL_64_safe_stack, SYM_L_GLOBAL) 105*a13644f3SJoerg Roedel 106905a36a2SIngo Molnar /* Construct struct pt_regs on stack */ 107905a36a2SIngo Molnar pushq $__USER_DS /* pt_regs->ss */ 10898f05b51SAndy Lutomirski pushq PER_CPU_VAR(cpu_tss_rw + TSS_sp2) /* pt_regs->sp */ 109905a36a2SIngo Molnar pushq %r11 /* pt_regs->flags */ 110905a36a2SIngo Molnar pushq $__USER_CS /* pt_regs->cs */ 111905a36a2SIngo Molnar pushq %rcx /* pt_regs->ip */ 11226ba4e57SJiri SlabySYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL) 113905a36a2SIngo Molnar pushq %rax /* pt_regs->orig_ax */ 11430907fd1SDominik Brodowski 11530907fd1SDominik Brodowski PUSH_AND_CLEAR_REGS rax=$-ENOSYS 116905a36a2SIngo Molnar 1171e423bffSAndy Lutomirski /* IRQs are off. */ 118dfe64506SLinus Torvalds movq %rax, %rdi 119dfe64506SLinus Torvalds movq %rsp, %rsi 1201e423bffSAndy Lutomirski call do_syscall_64 /* returns with IRQs disabled */ 1211e423bffSAndy Lutomirski 122905a36a2SIngo Molnar /* 123905a36a2SIngo Molnar * Try to use SYSRET instead of IRET if we're returning to 1248a055d7fSAndy Lutomirski * a completely clean 64-bit userspace context. If we're not, 1258a055d7fSAndy Lutomirski * go to the slow exit path. 126905a36a2SIngo Molnar */ 127905a36a2SIngo Molnar movq RCX(%rsp), %rcx 128905a36a2SIngo Molnar movq RIP(%rsp), %r11 1298a055d7fSAndy Lutomirski 1308a055d7fSAndy Lutomirski cmpq %rcx, %r11 /* SYSRET requires RCX == RIP */ 1318a055d7fSAndy Lutomirski jne swapgs_restore_regs_and_return_to_usermode 132905a36a2SIngo Molnar 133905a36a2SIngo Molnar /* 134905a36a2SIngo Molnar * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP 135905a36a2SIngo Molnar * in kernel space. This essentially lets the user take over 136905a36a2SIngo Molnar * the kernel, since userspace controls RSP. 137905a36a2SIngo Molnar * 138905a36a2SIngo Molnar * If width of "canonical tail" ever becomes variable, this will need 139905a36a2SIngo Molnar * to be updated to remain correct on both old and new CPUs. 140361b4b58SKirill A. Shutemov * 141cbe0317bSKirill A. Shutemov * Change top bits to match most significant bit (47th or 56th bit 142cbe0317bSKirill A. Shutemov * depending on paging mode) in the address. 143905a36a2SIngo Molnar */ 14409e61a77SKirill A. Shutemov#ifdef CONFIG_X86_5LEVEL 14539b95522SKirill A. Shutemov ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \ 14639b95522SKirill A. Shutemov "shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57 14709e61a77SKirill A. Shutemov#else 148905a36a2SIngo Molnar shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 149905a36a2SIngo Molnar sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 15009e61a77SKirill A. Shutemov#endif 1514d732138SIngo Molnar 152905a36a2SIngo Molnar /* If this changed %rcx, it was not canonical */ 153905a36a2SIngo Molnar cmpq %rcx, %r11 1548a055d7fSAndy Lutomirski jne swapgs_restore_regs_and_return_to_usermode 155905a36a2SIngo Molnar 156905a36a2SIngo Molnar cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */ 1578a055d7fSAndy Lutomirski jne swapgs_restore_regs_and_return_to_usermode 158905a36a2SIngo Molnar 159905a36a2SIngo Molnar movq R11(%rsp), %r11 160905a36a2SIngo Molnar cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */ 1618a055d7fSAndy Lutomirski jne swapgs_restore_regs_and_return_to_usermode 162905a36a2SIngo Molnar 163905a36a2SIngo Molnar /* 1643e035305SBorislav Petkov * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot 1653e035305SBorislav Petkov * restore RF properly. If the slowpath sets it for whatever reason, we 1663e035305SBorislav Petkov * need to restore it correctly. 1673e035305SBorislav Petkov * 1683e035305SBorislav Petkov * SYSRET can restore TF, but unlike IRET, restoring TF results in a 1693e035305SBorislav Petkov * trap from userspace immediately after SYSRET. This would cause an 1703e035305SBorislav Petkov * infinite loop whenever #DB happens with register state that satisfies 1713e035305SBorislav Petkov * the opportunistic SYSRET conditions. For example, single-stepping 1723e035305SBorislav Petkov * this user code: 173905a36a2SIngo Molnar * 174905a36a2SIngo Molnar * movq $stuck_here, %rcx 175905a36a2SIngo Molnar * pushfq 176905a36a2SIngo Molnar * popq %r11 177905a36a2SIngo Molnar * stuck_here: 178905a36a2SIngo Molnar * 179905a36a2SIngo Molnar * would never get past 'stuck_here'. 180905a36a2SIngo Molnar */ 181905a36a2SIngo Molnar testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11 1828a055d7fSAndy Lutomirski jnz swapgs_restore_regs_and_return_to_usermode 183905a36a2SIngo Molnar 184905a36a2SIngo Molnar /* nothing to check for RSP */ 185905a36a2SIngo Molnar 186905a36a2SIngo Molnar cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */ 1878a055d7fSAndy Lutomirski jne swapgs_restore_regs_and_return_to_usermode 188905a36a2SIngo Molnar 189905a36a2SIngo Molnar /* 190905a36a2SIngo Molnar * We win! This label is here just for ease of understanding 191905a36a2SIngo Molnar * perf profiles. Nothing jumps here. 192905a36a2SIngo Molnar */ 193905a36a2SIngo Molnarsyscall_return_via_sysret: 194905a36a2SIngo Molnar /* rcx and r11 are already restored (see code above) */ 195502af0d7SDominik Brodowski POP_REGS pop_rdi=0 skip_r11rcx=1 1963e3b9293SAndy Lutomirski 1973e3b9293SAndy Lutomirski /* 1983e3b9293SAndy Lutomirski * Now all regs are restored except RSP and RDI. 1993e3b9293SAndy Lutomirski * Save old stack pointer and switch to trampoline stack. 2003e3b9293SAndy Lutomirski */ 2013e3b9293SAndy Lutomirski movq %rsp, %rdi 202c482feefSAndy Lutomirski movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp 2031fb14363SJosh Poimboeuf UNWIND_HINT_EMPTY 2043e3b9293SAndy Lutomirski 2053e3b9293SAndy Lutomirski pushq RSP-RDI(%rdi) /* RSP */ 2063e3b9293SAndy Lutomirski pushq (%rdi) /* RDI */ 2073e3b9293SAndy Lutomirski 2083e3b9293SAndy Lutomirski /* 2093e3b9293SAndy Lutomirski * We are on the trampoline stack. All regs except RDI are live. 2103e3b9293SAndy Lutomirski * We can do future final exit work right here. 2113e3b9293SAndy Lutomirski */ 212afaef01cSAlexander Popov STACKLEAK_ERASE_NOCLOBBER 213afaef01cSAlexander Popov 2146fd166aaSPeter Zijlstra SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi 2153e3b9293SAndy Lutomirski 2164fbb3910SAndy Lutomirski popq %rdi 2173e3b9293SAndy Lutomirski popq %rsp 218905a36a2SIngo Molnar USERGS_SYSRET64 219bc7b11c0SJiri SlabySYM_CODE_END(entry_SYSCALL_64) 220905a36a2SIngo Molnar 221905a36a2SIngo Molnar/* 2220100301bSBrian Gerst * %rdi: prev task 2230100301bSBrian Gerst * %rsi: next task 2240100301bSBrian Gerst */ 225b9f6976bSThomas Gleixner.pushsection .text, "ax" 22696c64806SJosh PoimboeufSYM_FUNC_START(__switch_to_asm) 2270100301bSBrian Gerst /* 2280100301bSBrian Gerst * Save callee-saved registers 2290100301bSBrian Gerst * This must match the order in inactive_task_frame 2300100301bSBrian Gerst */ 2310100301bSBrian Gerst pushq %rbp 2320100301bSBrian Gerst pushq %rbx 2330100301bSBrian Gerst pushq %r12 2340100301bSBrian Gerst pushq %r13 2350100301bSBrian Gerst pushq %r14 2360100301bSBrian Gerst pushq %r15 2370100301bSBrian Gerst 2380100301bSBrian Gerst /* switch stack */ 2390100301bSBrian Gerst movq %rsp, TASK_threadsp(%rdi) 2400100301bSBrian Gerst movq TASK_threadsp(%rsi), %rsp 2410100301bSBrian Gerst 242050e9baaSLinus Torvalds#ifdef CONFIG_STACKPROTECTOR 2430100301bSBrian Gerst movq TASK_stack_canary(%rsi), %rbx 244e6401c13SAndy Lutomirski movq %rbx, PER_CPU_VAR(fixed_percpu_data) + stack_canary_offset 2450100301bSBrian Gerst#endif 2460100301bSBrian Gerst 247c995efd5SDavid Woodhouse#ifdef CONFIG_RETPOLINE 248c995efd5SDavid Woodhouse /* 249c995efd5SDavid Woodhouse * When switching from a shallower to a deeper call stack 250c995efd5SDavid Woodhouse * the RSB may either underflow or use entries populated 251c995efd5SDavid Woodhouse * with userspace addresses. On CPUs where those concerns 252c995efd5SDavid Woodhouse * exist, overwrite the RSB with entries which capture 253c995efd5SDavid Woodhouse * speculative execution to prevent attack. 254c995efd5SDavid Woodhouse */ 255d1c99108SDavid Woodhouse FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW 256c995efd5SDavid Woodhouse#endif 257c995efd5SDavid Woodhouse 2580100301bSBrian Gerst /* restore callee-saved registers */ 2590100301bSBrian Gerst popq %r15 2600100301bSBrian Gerst popq %r14 2610100301bSBrian Gerst popq %r13 2620100301bSBrian Gerst popq %r12 2630100301bSBrian Gerst popq %rbx 2640100301bSBrian Gerst popq %rbp 2650100301bSBrian Gerst 2660100301bSBrian Gerst jmp __switch_to 26796c64806SJosh PoimboeufSYM_FUNC_END(__switch_to_asm) 268b9f6976bSThomas Gleixner.popsection 2690100301bSBrian Gerst 2700100301bSBrian Gerst/* 271905a36a2SIngo Molnar * A newly forked process directly context switches into this address. 272905a36a2SIngo Molnar * 2730100301bSBrian Gerst * rax: prev task we switched from 274616d2483SBrian Gerst * rbx: kernel thread func (NULL for user thread) 275616d2483SBrian Gerst * r12: kernel thread arg 276905a36a2SIngo Molnar */ 277b9f6976bSThomas Gleixner.pushsection .text, "ax" 278bc7b11c0SJiri SlabySYM_CODE_START(ret_from_fork) 2798c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 2800100301bSBrian Gerst movq %rax, %rdi 2814d732138SIngo Molnar call schedule_tail /* rdi: 'prev' task parameter */ 282905a36a2SIngo Molnar 283616d2483SBrian Gerst testq %rbx, %rbx /* from kernel_thread? */ 284616d2483SBrian Gerst jnz 1f /* kernel threads are uncommon */ 285905a36a2SIngo Molnar 286616d2483SBrian Gerst2: 2878c1f7558SJosh Poimboeuf UNWIND_HINT_REGS 288ebd57499SJosh Poimboeuf movq %rsp, %rdi 289167fd210SThomas Gleixner call syscall_exit_to_user_mode /* returns with IRQs disabled */ 2908a055d7fSAndy Lutomirski jmp swapgs_restore_regs_and_return_to_usermode 291616d2483SBrian Gerst 292616d2483SBrian Gerst1: 293616d2483SBrian Gerst /* kernel thread */ 294d31a5802SJosh Poimboeuf UNWIND_HINT_EMPTY 295616d2483SBrian Gerst movq %r12, %rdi 29634fdce69SPeter Zijlstra CALL_NOSPEC rbx 297616d2483SBrian Gerst /* 298616d2483SBrian Gerst * A kernel thread is allowed to return here after successfully 299be619f7fSEric W. Biederman * calling kernel_execve(). Exit to userspace to complete the execve() 300616d2483SBrian Gerst * syscall. 301616d2483SBrian Gerst */ 302616d2483SBrian Gerst movq $0, RAX(%rsp) 303616d2483SBrian Gerst jmp 2b 304bc7b11c0SJiri SlabySYM_CODE_END(ret_from_fork) 305b9f6976bSThomas Gleixner.popsection 306905a36a2SIngo Molnar 3071d3e53e8SAndy Lutomirski.macro DEBUG_ENTRY_ASSERT_IRQS_OFF 3081d3e53e8SAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY 309e17f8234SBoris Ostrovsky pushq %rax 310e17f8234SBoris Ostrovsky SAVE_FLAGS(CLBR_RAX) 311e17f8234SBoris Ostrovsky testl $X86_EFLAGS_IF, %eax 3121d3e53e8SAndy Lutomirski jz .Lokay_\@ 3131d3e53e8SAndy Lutomirski ud2 3141d3e53e8SAndy Lutomirski.Lokay_\@: 315e17f8234SBoris Ostrovsky popq %rax 3161d3e53e8SAndy Lutomirski#endif 3171d3e53e8SAndy Lutomirski.endm 3181d3e53e8SAndy Lutomirski 319cfa82a00SThomas Gleixner/** 320cfa82a00SThomas Gleixner * idtentry_body - Macro to emit code calling the C function 321cfa82a00SThomas Gleixner * @cfunc: C function to be called 322cfa82a00SThomas Gleixner * @has_error_code: Hardware pushed error code on stack 323cfa82a00SThomas Gleixner */ 324e2dcb5f1SThomas Gleixner.macro idtentry_body cfunc has_error_code:req 325cfa82a00SThomas Gleixner 326cfa82a00SThomas Gleixner call error_entry 327cfa82a00SThomas Gleixner UNWIND_HINT_REGS 328cfa82a00SThomas Gleixner 329cfa82a00SThomas Gleixner movq %rsp, %rdi /* pt_regs pointer into 1st argument*/ 330cfa82a00SThomas Gleixner 331cfa82a00SThomas Gleixner .if \has_error_code == 1 332cfa82a00SThomas Gleixner movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/ 333cfa82a00SThomas Gleixner movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 334cfa82a00SThomas Gleixner .endif 335cfa82a00SThomas Gleixner 336cfa82a00SThomas Gleixner call \cfunc 337cfa82a00SThomas Gleixner 338424c7d0aSThomas Gleixner jmp error_return 339cfa82a00SThomas Gleixner.endm 340cfa82a00SThomas Gleixner 341cfa82a00SThomas Gleixner/** 342cfa82a00SThomas Gleixner * idtentry - Macro to generate entry stubs for simple IDT entries 343cfa82a00SThomas Gleixner * @vector: Vector number 344cfa82a00SThomas Gleixner * @asmsym: ASM symbol for the entry point 345cfa82a00SThomas Gleixner * @cfunc: C function to be called 346cfa82a00SThomas Gleixner * @has_error_code: Hardware pushed error code on stack 347cfa82a00SThomas Gleixner * 348cfa82a00SThomas Gleixner * The macro emits code to set up the kernel context for straight forward 349cfa82a00SThomas Gleixner * and simple IDT entries. No IST stack, no paranoid entry checks. 350cfa82a00SThomas Gleixner */ 351e2dcb5f1SThomas Gleixner.macro idtentry vector asmsym cfunc has_error_code:req 352cfa82a00SThomas GleixnerSYM_CODE_START(\asmsym) 353cfa82a00SThomas Gleixner UNWIND_HINT_IRET_REGS offset=\has_error_code*8 354cfa82a00SThomas Gleixner ASM_CLAC 355cfa82a00SThomas Gleixner 356cfa82a00SThomas Gleixner .if \has_error_code == 0 357cfa82a00SThomas Gleixner pushq $-1 /* ORIG_RAX: no syscall to restart */ 358cfa82a00SThomas Gleixner .endif 359cfa82a00SThomas Gleixner 360cfa82a00SThomas Gleixner .if \vector == X86_TRAP_BP 361cfa82a00SThomas Gleixner /* 362cfa82a00SThomas Gleixner * If coming from kernel space, create a 6-word gap to allow the 363cfa82a00SThomas Gleixner * int3 handler to emulate a call instruction. 364cfa82a00SThomas Gleixner */ 365cfa82a00SThomas Gleixner testb $3, CS-ORIG_RAX(%rsp) 366cfa82a00SThomas Gleixner jnz .Lfrom_usermode_no_gap_\@ 367cfa82a00SThomas Gleixner .rept 6 368cfa82a00SThomas Gleixner pushq 5*8(%rsp) 369cfa82a00SThomas Gleixner .endr 370cfa82a00SThomas Gleixner UNWIND_HINT_IRET_REGS offset=8 371cfa82a00SThomas Gleixner.Lfrom_usermode_no_gap_\@: 372cfa82a00SThomas Gleixner .endif 373cfa82a00SThomas Gleixner 374e2dcb5f1SThomas Gleixner idtentry_body \cfunc \has_error_code 375cfa82a00SThomas Gleixner 376cfa82a00SThomas Gleixner_ASM_NOKPROBE(\asmsym) 377cfa82a00SThomas GleixnerSYM_CODE_END(\asmsym) 378cfa82a00SThomas Gleixner.endm 379cfa82a00SThomas Gleixner 380cfa82a00SThomas Gleixner/* 3810bf7c314SThomas Gleixner * Interrupt entry/exit. 3820bf7c314SThomas Gleixner * 3830bf7c314SThomas Gleixner + The interrupt stubs push (vector) onto the stack, which is the error_code 3840bf7c314SThomas Gleixner * position of idtentry exceptions, and jump to one of the two idtentry points 3850bf7c314SThomas Gleixner * (common/spurious). 3860bf7c314SThomas Gleixner * 3870bf7c314SThomas Gleixner * common_interrupt is a hotpath, align it to a cache line 3880bf7c314SThomas Gleixner */ 3890bf7c314SThomas Gleixner.macro idtentry_irq vector cfunc 3900bf7c314SThomas Gleixner .p2align CONFIG_X86_L1_CACHE_SHIFT 3910bf7c314SThomas Gleixner idtentry \vector asm_\cfunc \cfunc has_error_code=1 3920bf7c314SThomas Gleixner.endm 3930bf7c314SThomas Gleixner 3940bf7c314SThomas Gleixner/* 3956368558cSThomas Gleixner * System vectors which invoke their handlers directly and are not 3966368558cSThomas Gleixner * going through the regular common device interrupt handling code. 3976368558cSThomas Gleixner */ 3986368558cSThomas Gleixner.macro idtentry_sysvec vector cfunc 3996368558cSThomas Gleixner idtentry \vector asm_\cfunc \cfunc has_error_code=0 4006368558cSThomas Gleixner.endm 4016368558cSThomas Gleixner 402cfa82a00SThomas Gleixner/** 403cfa82a00SThomas Gleixner * idtentry_mce_db - Macro to generate entry stubs for #MC and #DB 404cfa82a00SThomas Gleixner * @vector: Vector number 405cfa82a00SThomas Gleixner * @asmsym: ASM symbol for the entry point 406cfa82a00SThomas Gleixner * @cfunc: C function to be called 407cfa82a00SThomas Gleixner * 408cfa82a00SThomas Gleixner * The macro emits code to set up the kernel context for #MC and #DB 409cfa82a00SThomas Gleixner * 410cfa82a00SThomas Gleixner * If the entry comes from user space it uses the normal entry path 411cfa82a00SThomas Gleixner * including the return to user space work and preemption checks on 412cfa82a00SThomas Gleixner * exit. 413cfa82a00SThomas Gleixner * 414cfa82a00SThomas Gleixner * If hits in kernel mode then it needs to go through the paranoid 415cfa82a00SThomas Gleixner * entry as the exception can hit any random state. No preemption 416cfa82a00SThomas Gleixner * check on exit to keep the paranoid path simple. 417cfa82a00SThomas Gleixner */ 418cfa82a00SThomas Gleixner.macro idtentry_mce_db vector asmsym cfunc 419cfa82a00SThomas GleixnerSYM_CODE_START(\asmsym) 420cfa82a00SThomas Gleixner UNWIND_HINT_IRET_REGS 421cfa82a00SThomas Gleixner ASM_CLAC 422cfa82a00SThomas Gleixner 423cfa82a00SThomas Gleixner pushq $-1 /* ORIG_RAX: no syscall to restart */ 424cfa82a00SThomas Gleixner 425cfa82a00SThomas Gleixner /* 426cfa82a00SThomas Gleixner * If the entry is from userspace, switch stacks and treat it as 427cfa82a00SThomas Gleixner * a normal entry. 428cfa82a00SThomas Gleixner */ 429cfa82a00SThomas Gleixner testb $3, CS-ORIG_RAX(%rsp) 430cfa82a00SThomas Gleixner jnz .Lfrom_usermode_switch_stack_\@ 431cfa82a00SThomas Gleixner 432c82965f9SChang S. Bae /* paranoid_entry returns GS information for paranoid_exit in EBX. */ 433cfa82a00SThomas Gleixner call paranoid_entry 434cfa82a00SThomas Gleixner 435cfa82a00SThomas Gleixner UNWIND_HINT_REGS 436cfa82a00SThomas Gleixner 437cfa82a00SThomas Gleixner movq %rsp, %rdi /* pt_regs pointer */ 438cfa82a00SThomas Gleixner 439cfa82a00SThomas Gleixner call \cfunc 440cfa82a00SThomas Gleixner 441cfa82a00SThomas Gleixner jmp paranoid_exit 442cfa82a00SThomas Gleixner 443cfa82a00SThomas Gleixner /* Switch to the regular task stack and use the noist entry point */ 444cfa82a00SThomas Gleixner.Lfrom_usermode_switch_stack_\@: 445e2dcb5f1SThomas Gleixner idtentry_body noist_\cfunc, has_error_code=0 446cfa82a00SThomas Gleixner 447cfa82a00SThomas Gleixner_ASM_NOKPROBE(\asmsym) 448cfa82a00SThomas GleixnerSYM_CODE_END(\asmsym) 449cfa82a00SThomas Gleixner.endm 450cfa82a00SThomas Gleixner 451*a13644f3SJoerg Roedel#ifdef CONFIG_AMD_MEM_ENCRYPT 452*a13644f3SJoerg Roedel/** 453*a13644f3SJoerg Roedel * idtentry_vc - Macro to generate entry stub for #VC 454*a13644f3SJoerg Roedel * @vector: Vector number 455*a13644f3SJoerg Roedel * @asmsym: ASM symbol for the entry point 456*a13644f3SJoerg Roedel * @cfunc: C function to be called 457*a13644f3SJoerg Roedel * 458*a13644f3SJoerg Roedel * The macro emits code to set up the kernel context for #VC. The #VC handler 459*a13644f3SJoerg Roedel * runs on an IST stack and needs to be able to cause nested #VC exceptions. 460*a13644f3SJoerg Roedel * 461*a13644f3SJoerg Roedel * To make this work the #VC entry code tries its best to pretend it doesn't use 462*a13644f3SJoerg Roedel * an IST stack by switching to the task stack if coming from user-space (which 463*a13644f3SJoerg Roedel * includes early SYSCALL entry path) or back to the stack in the IRET frame if 464*a13644f3SJoerg Roedel * entered from kernel-mode. 465*a13644f3SJoerg Roedel * 466*a13644f3SJoerg Roedel * If entered from kernel-mode the return stack is validated first, and if it is 467*a13644f3SJoerg Roedel * not safe to use (e.g. because it points to the entry stack) the #VC handler 468*a13644f3SJoerg Roedel * will switch to a fall-back stack (VC2) and call a special handler function. 469*a13644f3SJoerg Roedel * 470*a13644f3SJoerg Roedel * The macro is only used for one vector, but it is planned to be extended in 471*a13644f3SJoerg Roedel * the future for the #HV exception. 472*a13644f3SJoerg Roedel */ 473*a13644f3SJoerg Roedel.macro idtentry_vc vector asmsym cfunc 474*a13644f3SJoerg RoedelSYM_CODE_START(\asmsym) 475*a13644f3SJoerg Roedel UNWIND_HINT_IRET_REGS 476*a13644f3SJoerg Roedel ASM_CLAC 477*a13644f3SJoerg Roedel 478*a13644f3SJoerg Roedel /* 479*a13644f3SJoerg Roedel * If the entry is from userspace, switch stacks and treat it as 480*a13644f3SJoerg Roedel * a normal entry. 481*a13644f3SJoerg Roedel */ 482*a13644f3SJoerg Roedel testb $3, CS-ORIG_RAX(%rsp) 483*a13644f3SJoerg Roedel jnz .Lfrom_usermode_switch_stack_\@ 484*a13644f3SJoerg Roedel 485*a13644f3SJoerg Roedel /* 486*a13644f3SJoerg Roedel * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX. 487*a13644f3SJoerg Roedel * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS 488*a13644f3SJoerg Roedel */ 489*a13644f3SJoerg Roedel call paranoid_entry 490*a13644f3SJoerg Roedel 491*a13644f3SJoerg Roedel UNWIND_HINT_REGS 492*a13644f3SJoerg Roedel 493*a13644f3SJoerg Roedel /* 494*a13644f3SJoerg Roedel * Switch off the IST stack to make it free for nested exceptions. The 495*a13644f3SJoerg Roedel * vc_switch_off_ist() function will switch back to the interrupted 496*a13644f3SJoerg Roedel * stack if it is safe to do so. If not it switches to the VC fall-back 497*a13644f3SJoerg Roedel * stack. 498*a13644f3SJoerg Roedel */ 499*a13644f3SJoerg Roedel movq %rsp, %rdi /* pt_regs pointer */ 500*a13644f3SJoerg Roedel call vc_switch_off_ist 501*a13644f3SJoerg Roedel movq %rax, %rsp /* Switch to new stack */ 502*a13644f3SJoerg Roedel 503*a13644f3SJoerg Roedel UNWIND_HINT_REGS 504*a13644f3SJoerg Roedel 505*a13644f3SJoerg Roedel /* Update pt_regs */ 506*a13644f3SJoerg Roedel movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/ 507*a13644f3SJoerg Roedel movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 508*a13644f3SJoerg Roedel 509*a13644f3SJoerg Roedel movq %rsp, %rdi /* pt_regs pointer */ 510*a13644f3SJoerg Roedel 511*a13644f3SJoerg Roedel call \cfunc 512*a13644f3SJoerg Roedel 513*a13644f3SJoerg Roedel /* 514*a13644f3SJoerg Roedel * No need to switch back to the IST stack. The current stack is either 515*a13644f3SJoerg Roedel * identical to the stack in the IRET frame or the VC fall-back stack, 516*a13644f3SJoerg Roedel * so it is definitly mapped even with PTI enabled. 517*a13644f3SJoerg Roedel */ 518*a13644f3SJoerg Roedel jmp paranoid_exit 519*a13644f3SJoerg Roedel 520*a13644f3SJoerg Roedel /* Switch to the regular task stack */ 521*a13644f3SJoerg Roedel.Lfrom_usermode_switch_stack_\@: 522*a13644f3SJoerg Roedel idtentry_body safe_stack_\cfunc, has_error_code=1 523*a13644f3SJoerg Roedel 524*a13644f3SJoerg Roedel_ASM_NOKPROBE(\asmsym) 525*a13644f3SJoerg RoedelSYM_CODE_END(\asmsym) 526*a13644f3SJoerg Roedel.endm 527*a13644f3SJoerg Roedel#endif 528*a13644f3SJoerg Roedel 529cfa82a00SThomas Gleixner/* 530cfa82a00SThomas Gleixner * Double fault entry. Straight paranoid. No checks from which context 531cfa82a00SThomas Gleixner * this comes because for the espfix induced #DF this would do the wrong 532cfa82a00SThomas Gleixner * thing. 533cfa82a00SThomas Gleixner */ 534cfa82a00SThomas Gleixner.macro idtentry_df vector asmsym cfunc 535cfa82a00SThomas GleixnerSYM_CODE_START(\asmsym) 536cfa82a00SThomas Gleixner UNWIND_HINT_IRET_REGS offset=8 537cfa82a00SThomas Gleixner ASM_CLAC 538cfa82a00SThomas Gleixner 539c82965f9SChang S. Bae /* paranoid_entry returns GS information for paranoid_exit in EBX. */ 540cfa82a00SThomas Gleixner call paranoid_entry 541cfa82a00SThomas Gleixner UNWIND_HINT_REGS 542cfa82a00SThomas Gleixner 543cfa82a00SThomas Gleixner movq %rsp, %rdi /* pt_regs pointer into first argument */ 544cfa82a00SThomas Gleixner movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/ 545cfa82a00SThomas Gleixner movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 546cfa82a00SThomas Gleixner call \cfunc 547cfa82a00SThomas Gleixner 548cfa82a00SThomas Gleixner jmp paranoid_exit 549cfa82a00SThomas Gleixner 550cfa82a00SThomas Gleixner_ASM_NOKPROBE(\asmsym) 551cfa82a00SThomas GleixnerSYM_CODE_END(\asmsym) 552cfa82a00SThomas Gleixner.endm 553cfa82a00SThomas Gleixner 554905a36a2SIngo Molnar/* 55553aaf262SThomas Gleixner * Include the defines which emit the idt entries which are shared 556f0178fc0SThomas Gleixner * shared between 32 and 64 bit and emit the __irqentry_text_* markers 557f0178fc0SThomas Gleixner * so the stacktrace boundary checks work. 55853aaf262SThomas Gleixner */ 559f0178fc0SThomas Gleixner .align 16 560f0178fc0SThomas Gleixner .globl __irqentry_text_start 561f0178fc0SThomas Gleixner__irqentry_text_start: 562f0178fc0SThomas Gleixner 56353aaf262SThomas Gleixner#include <asm/idtentry.h> 56453aaf262SThomas Gleixner 565f0178fc0SThomas Gleixner .align 16 566f0178fc0SThomas Gleixner .globl __irqentry_text_end 567f0178fc0SThomas Gleixner__irqentry_text_end: 568f0178fc0SThomas Gleixner 569fa5e5c40SThomas GleixnerSYM_CODE_START_LOCAL(common_interrupt_return) 57026ba4e57SJiri SlabySYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL) 57126c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY 57226c4ef9cSAndy Lutomirski /* Assert that pt_regs indicates user mode. */ 5731e4c4f61SBorislav Petkov testb $3, CS(%rsp) 57426c4ef9cSAndy Lutomirski jnz 1f 57526c4ef9cSAndy Lutomirski ud2 57626c4ef9cSAndy Lutomirski1: 57726c4ef9cSAndy Lutomirski#endif 578502af0d7SDominik Brodowski POP_REGS pop_rdi=0 5793e3b9293SAndy Lutomirski 5803e3b9293SAndy Lutomirski /* 5813e3b9293SAndy Lutomirski * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS. 5823e3b9293SAndy Lutomirski * Save old stack pointer and switch to trampoline stack. 5833e3b9293SAndy Lutomirski */ 5843e3b9293SAndy Lutomirski movq %rsp, %rdi 585c482feefSAndy Lutomirski movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp 5861fb14363SJosh Poimboeuf UNWIND_HINT_EMPTY 5873e3b9293SAndy Lutomirski 5883e3b9293SAndy Lutomirski /* Copy the IRET frame to the trampoline stack. */ 5893e3b9293SAndy Lutomirski pushq 6*8(%rdi) /* SS */ 5903e3b9293SAndy Lutomirski pushq 5*8(%rdi) /* RSP */ 5913e3b9293SAndy Lutomirski pushq 4*8(%rdi) /* EFLAGS */ 5923e3b9293SAndy Lutomirski pushq 3*8(%rdi) /* CS */ 5933e3b9293SAndy Lutomirski pushq 2*8(%rdi) /* RIP */ 5943e3b9293SAndy Lutomirski 5953e3b9293SAndy Lutomirski /* Push user RDI on the trampoline stack. */ 5963e3b9293SAndy Lutomirski pushq (%rdi) 5973e3b9293SAndy Lutomirski 5983e3b9293SAndy Lutomirski /* 5993e3b9293SAndy Lutomirski * We are on the trampoline stack. All regs except RDI are live. 6003e3b9293SAndy Lutomirski * We can do future final exit work right here. 6013e3b9293SAndy Lutomirski */ 602afaef01cSAlexander Popov STACKLEAK_ERASE_NOCLOBBER 6033e3b9293SAndy Lutomirski 6046fd166aaSPeter Zijlstra SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi 6058a09317bSDave Hansen 6063e3b9293SAndy Lutomirski /* Restore RDI. */ 6073e3b9293SAndy Lutomirski popq %rdi 6083e3b9293SAndy Lutomirski SWAPGS 60926c4ef9cSAndy Lutomirski INTERRUPT_RETURN 61026c4ef9cSAndy Lutomirski 611905a36a2SIngo Molnar 61226ba4e57SJiri SlabySYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL) 61326c4ef9cSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY 61426c4ef9cSAndy Lutomirski /* Assert that pt_regs indicates kernel mode. */ 6151e4c4f61SBorislav Petkov testb $3, CS(%rsp) 61626c4ef9cSAndy Lutomirski jz 1f 61726c4ef9cSAndy Lutomirski ud2 61826c4ef9cSAndy Lutomirski1: 61926c4ef9cSAndy Lutomirski#endif 620502af0d7SDominik Brodowski POP_REGS 621e872045bSAndy Lutomirski addq $8, %rsp /* skip regs->orig_ax */ 62210bcc80eSMathieu Desnoyers /* 62310bcc80eSMathieu Desnoyers * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization 62410bcc80eSMathieu Desnoyers * when returning from IPI handler. 62510bcc80eSMathieu Desnoyers */ 626905a36a2SIngo Molnar INTERRUPT_RETURN 627905a36a2SIngo Molnar 628cc66936eSJiri SlabySYM_INNER_LABEL_ALIGN(native_iret, SYM_L_GLOBAL) 6298c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 630905a36a2SIngo Molnar /* 631905a36a2SIngo Molnar * Are we returning to a stack segment from the LDT? Note: in 632905a36a2SIngo Molnar * 64-bit mode SS:RSP on the exception stack is always valid. 633905a36a2SIngo Molnar */ 634905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 635905a36a2SIngo Molnar testb $4, (SS-RIP)(%rsp) 636905a36a2SIngo Molnar jnz native_irq_return_ldt 637905a36a2SIngo Molnar#endif 638905a36a2SIngo Molnar 639cc66936eSJiri SlabySYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL) 640905a36a2SIngo Molnar /* 641905a36a2SIngo Molnar * This may fault. Non-paranoid faults on return to userspace are 642905a36a2SIngo Molnar * handled by fixup_bad_iret. These include #SS, #GP, and #NP. 643c29c775aSThomas Gleixner * Double-faults due to espfix64 are handled in exc_double_fault. 644905a36a2SIngo Molnar * Other faults here are fatal. 645905a36a2SIngo Molnar */ 646905a36a2SIngo Molnar iretq 647905a36a2SIngo Molnar 648905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 649905a36a2SIngo Molnarnative_irq_return_ldt: 65085063facSAndy Lutomirski /* 65185063facSAndy Lutomirski * We are running with user GSBASE. All GPRs contain their user 65285063facSAndy Lutomirski * values. We have a percpu ESPFIX stack that is eight slots 65385063facSAndy Lutomirski * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom 65485063facSAndy Lutomirski * of the ESPFIX stack. 65585063facSAndy Lutomirski * 65685063facSAndy Lutomirski * We clobber RAX and RDI in this code. We stash RDI on the 65785063facSAndy Lutomirski * normal stack and RAX on the ESPFIX stack. 65885063facSAndy Lutomirski * 65985063facSAndy Lutomirski * The ESPFIX stack layout we set up looks like this: 66085063facSAndy Lutomirski * 66185063facSAndy Lutomirski * --- top of ESPFIX stack --- 66285063facSAndy Lutomirski * SS 66385063facSAndy Lutomirski * RSP 66485063facSAndy Lutomirski * RFLAGS 66585063facSAndy Lutomirski * CS 66685063facSAndy Lutomirski * RIP <-- RSP points here when we're done 66785063facSAndy Lutomirski * RAX <-- espfix_waddr points here 66885063facSAndy Lutomirski * --- bottom of ESPFIX stack --- 66985063facSAndy Lutomirski */ 67085063facSAndy Lutomirski 67185063facSAndy Lutomirski pushq %rdi /* Stash user RDI */ 6728a09317bSDave Hansen SWAPGS /* to kernel GS */ 6738a09317bSDave Hansen SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi /* to kernel CR3 */ 6748a09317bSDave Hansen 675905a36a2SIngo Molnar movq PER_CPU_VAR(espfix_waddr), %rdi 67685063facSAndy Lutomirski movq %rax, (0*8)(%rdi) /* user RAX */ 67785063facSAndy Lutomirski movq (1*8)(%rsp), %rax /* user RIP */ 678905a36a2SIngo Molnar movq %rax, (1*8)(%rdi) 67985063facSAndy Lutomirski movq (2*8)(%rsp), %rax /* user CS */ 680905a36a2SIngo Molnar movq %rax, (2*8)(%rdi) 68185063facSAndy Lutomirski movq (3*8)(%rsp), %rax /* user RFLAGS */ 682905a36a2SIngo Molnar movq %rax, (3*8)(%rdi) 68385063facSAndy Lutomirski movq (5*8)(%rsp), %rax /* user SS */ 684905a36a2SIngo Molnar movq %rax, (5*8)(%rdi) 68585063facSAndy Lutomirski movq (4*8)(%rsp), %rax /* user RSP */ 686905a36a2SIngo Molnar movq %rax, (4*8)(%rdi) 68785063facSAndy Lutomirski /* Now RAX == RSP. */ 68885063facSAndy Lutomirski 68985063facSAndy Lutomirski andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */ 69085063facSAndy Lutomirski 69185063facSAndy Lutomirski /* 69285063facSAndy Lutomirski * espfix_stack[31:16] == 0. The page tables are set up such that 69385063facSAndy Lutomirski * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of 69485063facSAndy Lutomirski * espfix_waddr for any X. That is, there are 65536 RO aliases of 69585063facSAndy Lutomirski * the same page. Set up RSP so that RSP[31:16] contains the 69685063facSAndy Lutomirski * respective 16 bits of the /userspace/ RSP and RSP nonetheless 69785063facSAndy Lutomirski * still points to an RO alias of the ESPFIX stack. 69885063facSAndy Lutomirski */ 699905a36a2SIngo Molnar orq PER_CPU_VAR(espfix_stack), %rax 7008a09317bSDave Hansen 7016fd166aaSPeter Zijlstra SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi 7028a09317bSDave Hansen SWAPGS /* to user GS */ 7038a09317bSDave Hansen popq %rdi /* Restore user RDI */ 7048a09317bSDave Hansen 705905a36a2SIngo Molnar movq %rax, %rsp 7068c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS offset=8 70785063facSAndy Lutomirski 70885063facSAndy Lutomirski /* 70985063facSAndy Lutomirski * At this point, we cannot write to the stack any more, but we can 71085063facSAndy Lutomirski * still read. 71185063facSAndy Lutomirski */ 71285063facSAndy Lutomirski popq %rax /* Restore user RAX */ 71385063facSAndy Lutomirski 71485063facSAndy Lutomirski /* 71585063facSAndy Lutomirski * RSP now points to an ordinary IRET frame, except that the page 71685063facSAndy Lutomirski * is read-only and RSP[31:16] are preloaded with the userspace 71785063facSAndy Lutomirski * values. We can now IRET back to userspace. 71885063facSAndy Lutomirski */ 719905a36a2SIngo Molnar jmp native_irq_return_iret 720905a36a2SIngo Molnar#endif 721fa5e5c40SThomas GleixnerSYM_CODE_END(common_interrupt_return) 722fa5e5c40SThomas Gleixner_ASM_NOKPROBE(common_interrupt_return) 723905a36a2SIngo Molnar 724905a36a2SIngo Molnar/* 7254d732138SIngo Molnar * Reload gs selector with exception handling 7264d732138SIngo Molnar * edi: new selector 727b9f6976bSThomas Gleixner * 728b9f6976bSThomas Gleixner * Is in entry.text as it shouldn't be instrumented. 7294d732138SIngo Molnar */ 730410367e3SThomas GleixnerSYM_FUNC_START(asm_load_gs_index) 7318c1f7558SJosh Poimboeuf FRAME_BEGIN 732c9317202SThomas Gleixner swapgs 73342c748bbSBorislav Petkov.Lgs_change: 734905a36a2SIngo Molnar movl %edi, %gs 73596e5d28aSBorislav Petkov2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE 736c9317202SThomas Gleixner swapgs 7378c1f7558SJosh Poimboeuf FRAME_END 738905a36a2SIngo Molnar ret 739410367e3SThomas GleixnerSYM_FUNC_END(asm_load_gs_index) 740410367e3SThomas GleixnerEXPORT_SYMBOL(asm_load_gs_index) 741905a36a2SIngo Molnar 74298ededb6SJiri Slaby _ASM_EXTABLE(.Lgs_change, .Lbad_gs) 743905a36a2SIngo Molnar .section .fixup, "ax" 744905a36a2SIngo Molnar /* running with kernelgs */ 745ef77e688SJiri SlabySYM_CODE_START_LOCAL_NOALIGN(.Lbad_gs) 746c9317202SThomas Gleixner swapgs /* switch back to user gs */ 747b038c842SAndy Lutomirski.macro ZAP_GS 748b038c842SAndy Lutomirski /* This can't be a string because the preprocessor needs to see it. */ 749b038c842SAndy Lutomirski movl $__USER_DS, %eax 750b038c842SAndy Lutomirski movl %eax, %gs 751b038c842SAndy Lutomirski.endm 752b038c842SAndy Lutomirski ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG 753905a36a2SIngo Molnar xorl %eax, %eax 754905a36a2SIngo Molnar movl %eax, %gs 755905a36a2SIngo Molnar jmp 2b 756ef77e688SJiri SlabySYM_CODE_END(.Lbad_gs) 757905a36a2SIngo Molnar .previous 758905a36a2SIngo Molnar 759931b9414SThomas Gleixner/* 760931b9414SThomas Gleixner * rdi: New stack pointer points to the top word of the stack 761931b9414SThomas Gleixner * rsi: Function pointer 762931b9414SThomas Gleixner * rdx: Function argument (can be NULL if none) 763931b9414SThomas Gleixner */ 764931b9414SThomas GleixnerSYM_FUNC_START(asm_call_on_stack) 765931b9414SThomas Gleixner /* 766931b9414SThomas Gleixner * Save the frame pointer unconditionally. This allows the ORC 767931b9414SThomas Gleixner * unwinder to handle the stack switch. 768931b9414SThomas Gleixner */ 769931b9414SThomas Gleixner pushq %rbp 770931b9414SThomas Gleixner mov %rsp, %rbp 771931b9414SThomas Gleixner 772931b9414SThomas Gleixner /* 773931b9414SThomas Gleixner * The unwinder relies on the word at the top of the new stack 774931b9414SThomas Gleixner * page linking back to the previous RSP. 775931b9414SThomas Gleixner */ 776931b9414SThomas Gleixner mov %rsp, (%rdi) 777931b9414SThomas Gleixner mov %rdi, %rsp 778931b9414SThomas Gleixner /* Move the argument to the right place */ 779931b9414SThomas Gleixner mov %rdx, %rdi 780931b9414SThomas Gleixner 781931b9414SThomas Gleixner1: 782931b9414SThomas Gleixner .pushsection .discard.instr_begin 783931b9414SThomas Gleixner .long 1b - . 784931b9414SThomas Gleixner .popsection 785931b9414SThomas Gleixner 786931b9414SThomas Gleixner CALL_NOSPEC rsi 787931b9414SThomas Gleixner 788931b9414SThomas Gleixner2: 789931b9414SThomas Gleixner .pushsection .discard.instr_end 790931b9414SThomas Gleixner .long 2b - . 791931b9414SThomas Gleixner .popsection 792931b9414SThomas Gleixner 793931b9414SThomas Gleixner /* Restore the previous stack pointer from RBP. */ 794931b9414SThomas Gleixner leaveq 795931b9414SThomas Gleixner ret 796931b9414SThomas GleixnerSYM_FUNC_END(asm_call_on_stack) 797931b9414SThomas Gleixner 79828c11b0fSJuergen Gross#ifdef CONFIG_XEN_PV 799905a36a2SIngo Molnar/* 800905a36a2SIngo Molnar * A note on the "critical region" in our callback handler. 801905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring 802905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled 803905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before 804905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still 805905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack. 806905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd 807905a36a2SIngo Molnar * like to avoid the possibility. 808905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an 809905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current 810905a36a2SIngo Molnar * activation and restart the handler using the previous one. 8112f6474e4SThomas Gleixner * 8122f6474e4SThomas Gleixner * C calling convention: exc_xen_hypervisor_callback(struct *pt_regs) 813905a36a2SIngo Molnar */ 8142f6474e4SThomas GleixnerSYM_CODE_START_LOCAL(exc_xen_hypervisor_callback) 8154d732138SIngo Molnar 816905a36a2SIngo Molnar/* 817905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will 818905a36a2SIngo Molnar * see the correct pointer to the pt_regs 819905a36a2SIngo Molnar */ 8208c1f7558SJosh Poimboeuf UNWIND_HINT_FUNC 8214d732138SIngo Molnar movq %rdi, %rsp /* we don't return, adjust the stack frame */ 8228c1f7558SJosh Poimboeuf UNWIND_HINT_REGS 8231d3e53e8SAndy Lutomirski 8242f6474e4SThomas Gleixner call xen_pv_evtchn_do_upcall 8251d3e53e8SAndy Lutomirski 8262f6474e4SThomas Gleixner jmp error_return 8272f6474e4SThomas GleixnerSYM_CODE_END(exc_xen_hypervisor_callback) 828905a36a2SIngo Molnar 829905a36a2SIngo Molnar/* 830905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes. 831905a36a2SIngo Molnar * We get here for two reasons: 832905a36a2SIngo Molnar * 1. Fault while reloading DS, ES, FS or GS 833905a36a2SIngo Molnar * 2. Fault while executing IRET 834905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment 835905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others. 836905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the 837905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall 838905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks. 839905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register 840905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1. 841905a36a2SIngo Molnar */ 842bc7b11c0SJiri SlabySYM_CODE_START(xen_failsafe_callback) 8438c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 844905a36a2SIngo Molnar movl %ds, %ecx 845905a36a2SIngo Molnar cmpw %cx, 0x10(%rsp) 846905a36a2SIngo Molnar jne 1f 847905a36a2SIngo Molnar movl %es, %ecx 848905a36a2SIngo Molnar cmpw %cx, 0x18(%rsp) 849905a36a2SIngo Molnar jne 1f 850905a36a2SIngo Molnar movl %fs, %ecx 851905a36a2SIngo Molnar cmpw %cx, 0x20(%rsp) 852905a36a2SIngo Molnar jne 1f 853905a36a2SIngo Molnar movl %gs, %ecx 854905a36a2SIngo Molnar cmpw %cx, 0x28(%rsp) 855905a36a2SIngo Molnar jne 1f 856905a36a2SIngo Molnar /* All segments match their saved values => Category 2 (Bad IRET). */ 857905a36a2SIngo Molnar movq (%rsp), %rcx 858905a36a2SIngo Molnar movq 8(%rsp), %r11 859905a36a2SIngo Molnar addq $0x30, %rsp 860905a36a2SIngo Molnar pushq $0 /* RIP */ 8618c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS offset=8 862be4c11afSThomas Gleixner jmp asm_exc_general_protection 863905a36a2SIngo Molnar1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */ 864905a36a2SIngo Molnar movq (%rsp), %rcx 865905a36a2SIngo Molnar movq 8(%rsp), %r11 866905a36a2SIngo Molnar addq $0x30, %rsp 8678c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 868905a36a2SIngo Molnar pushq $-1 /* orig_ax = -1 => not a system call */ 8693f01daecSDominik Brodowski PUSH_AND_CLEAR_REGS 870946c1911SJosh Poimboeuf ENCODE_FRAME_POINTER 871e88d9741SThomas Gleixner jmp error_return 872bc7b11c0SJiri SlabySYM_CODE_END(xen_failsafe_callback) 87328c11b0fSJuergen Gross#endif /* CONFIG_XEN_PV */ 874905a36a2SIngo Molnar 875905a36a2SIngo Molnar/* 876c82965f9SChang S. Bae * Save all registers in pt_regs. Return GSBASE related information 877c82965f9SChang S. Bae * in EBX depending on the availability of the FSGSBASE instructions: 878c82965f9SChang S. Bae * 879c82965f9SChang S. Bae * FSGSBASE R/EBX 880c82965f9SChang S. Bae * N 0 -> SWAPGS on exit 881c82965f9SChang S. Bae * 1 -> no SWAPGS on exit 882c82965f9SChang S. Bae * 883c82965f9SChang S. Bae * Y GSBASE value at entry, must be restored in paranoid_exit 884905a36a2SIngo Molnar */ 885ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_entry) 8868c1f7558SJosh Poimboeuf UNWIND_HINT_FUNC 887905a36a2SIngo Molnar cld 8889e809d15SDominik Brodowski PUSH_AND_CLEAR_REGS save_ret=1 8899e809d15SDominik Brodowski ENCODE_FRAME_POINTER 8 8908a09317bSDave Hansen 89116561f27SDave Hansen /* 89216561f27SDave Hansen * Always stash CR3 in %r14. This value will be restored, 893ae852495SAndy Lutomirski * verbatim, at exit. Needed if paranoid_entry interrupted 894ae852495SAndy Lutomirski * another entry that already switched to the user CR3 value 895ae852495SAndy Lutomirski * but has not yet returned to userspace. 89616561f27SDave Hansen * 89716561f27SDave Hansen * This is also why CS (stashed in the "iret frame" by the 89816561f27SDave Hansen * hardware at entry) can not be used: this may be a return 899ae852495SAndy Lutomirski * to kernel code, but with a user CR3 value. 90096b23714SChang S. Bae * 90196b23714SChang S. Bae * Switching CR3 does not depend on kernel GSBASE so it can 90296b23714SChang S. Bae * be done before switching to the kernel GSBASE. This is 90396b23714SChang S. Bae * required for FSGSBASE because the kernel GSBASE has to 90496b23714SChang S. Bae * be retrieved from a kernel internal table. 90516561f27SDave Hansen */ 9068a09317bSDave Hansen SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14 9078a09317bSDave Hansen 90818ec54fdSJosh Poimboeuf /* 909c82965f9SChang S. Bae * Handling GSBASE depends on the availability of FSGSBASE. 910c82965f9SChang S. Bae * 911c82965f9SChang S. Bae * Without FSGSBASE the kernel enforces that negative GSBASE 912c82965f9SChang S. Bae * values indicate kernel GSBASE. With FSGSBASE no assumptions 913c82965f9SChang S. Bae * can be made about the GSBASE value when entering from user 914c82965f9SChang S. Bae * space. 915c82965f9SChang S. Bae */ 916c82965f9SChang S. Bae ALTERNATIVE "jmp .Lparanoid_entry_checkgs", "", X86_FEATURE_FSGSBASE 917c82965f9SChang S. Bae 918c82965f9SChang S. Bae /* 919c82965f9SChang S. Bae * Read the current GSBASE and store it in %rbx unconditionally, 920c82965f9SChang S. Bae * retrieve and set the current CPUs kernel GSBASE. The stored value 921c82965f9SChang S. Bae * has to be restored in paranoid_exit unconditionally. 922c82965f9SChang S. Bae * 923c82965f9SChang S. Bae * The MSR write ensures that no subsequent load is based on a 924c82965f9SChang S. Bae * mispredicted GSBASE. No extra FENCE required. 925c82965f9SChang S. Bae */ 926c82965f9SChang S. Bae SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx 927c82965f9SChang S. Bae ret 928c82965f9SChang S. Bae 929c82965f9SChang S. Bae.Lparanoid_entry_checkgs: 93096b23714SChang S. Bae /* EBX = 1 -> kernel GSBASE active, no restore required */ 93196b23714SChang S. Bae movl $1, %ebx 93296b23714SChang S. Bae /* 93396b23714SChang S. Bae * The kernel-enforced convention is a negative GSBASE indicates 93496b23714SChang S. Bae * a kernel value. No SWAPGS needed on entry and exit. 93596b23714SChang S. Bae */ 93696b23714SChang S. Bae movl $MSR_GS_BASE, %ecx 93796b23714SChang S. Bae rdmsr 93896b23714SChang S. Bae testl %edx, %edx 93996b23714SChang S. Bae jns .Lparanoid_entry_swapgs 94096b23714SChang S. Bae ret 94196b23714SChang S. Bae 94296b23714SChang S. Bae.Lparanoid_entry_swapgs: 94396b23714SChang S. Bae SWAPGS 94496b23714SChang S. Bae 94518ec54fdSJosh Poimboeuf /* 94618ec54fdSJosh Poimboeuf * The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an 94718ec54fdSJosh Poimboeuf * unconditional CR3 write, even in the PTI case. So do an lfence 94818ec54fdSJosh Poimboeuf * to prevent GS speculation, regardless of whether PTI is enabled. 94918ec54fdSJosh Poimboeuf */ 95018ec54fdSJosh Poimboeuf FENCE_SWAPGS_KERNEL_ENTRY 95118ec54fdSJosh Poimboeuf 95296b23714SChang S. Bae /* EBX = 0 -> SWAPGS required on exit */ 95396b23714SChang S. Bae xorl %ebx, %ebx 9548a09317bSDave Hansen ret 955ef1e0315SJiri SlabySYM_CODE_END(paranoid_entry) 956905a36a2SIngo Molnar 957905a36a2SIngo Molnar/* 958905a36a2SIngo Molnar * "Paranoid" exit path from exception stack. This is invoked 959905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came 960905a36a2SIngo Molnar * from kernel space. 961905a36a2SIngo Molnar * 962905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early 963905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would 964c82965f9SChang S. Bae * be complicated. Fortunately, there's no good reason to try 965c82965f9SChang S. Bae * to handle preemption here. 9664d732138SIngo Molnar * 967c82965f9SChang S. Bae * R/EBX contains the GSBASE related information depending on the 968c82965f9SChang S. Bae * availability of the FSGSBASE instructions: 969c82965f9SChang S. Bae * 970c82965f9SChang S. Bae * FSGSBASE R/EBX 971c82965f9SChang S. Bae * N 0 -> SWAPGS on exit 972c82965f9SChang S. Bae * 1 -> no SWAPGS on exit 973c82965f9SChang S. Bae * 974c82965f9SChang S. Bae * Y User space GSBASE, must be restored unconditionally 975905a36a2SIngo Molnar */ 976ef1e0315SJiri SlabySYM_CODE_START_LOCAL(paranoid_exit) 9778c1f7558SJosh Poimboeuf UNWIND_HINT_REGS 978c82965f9SChang S. Bae /* 979c82965f9SChang S. Bae * The order of operations is important. RESTORE_CR3 requires 980c82965f9SChang S. Bae * kernel GSBASE. 981c82965f9SChang S. Bae * 982c82965f9SChang S. Bae * NB to anyone to try to optimize this code: this code does 983c82965f9SChang S. Bae * not execute at all for exceptions from user mode. Those 984c82965f9SChang S. Bae * exceptions go through error_exit instead. 985c82965f9SChang S. Bae */ 986c82965f9SChang S. Bae RESTORE_CR3 scratch_reg=%rax save_reg=%r14 987c82965f9SChang S. Bae 988c82965f9SChang S. Bae /* Handle the three GSBASE cases */ 989c82965f9SChang S. Bae ALTERNATIVE "jmp .Lparanoid_exit_checkgs", "", X86_FEATURE_FSGSBASE 990c82965f9SChang S. Bae 991c82965f9SChang S. Bae /* With FSGSBASE enabled, unconditionally restore GSBASE */ 992c82965f9SChang S. Bae wrgsbase %rbx 99345c08383SThomas Gleixner jmp restore_regs_and_return_to_kernel 994c82965f9SChang S. Bae 995c82965f9SChang S. Bae.Lparanoid_exit_checkgs: 996c82965f9SChang S. Bae /* On non-FSGSBASE systems, conditionally do SWAPGS */ 997c82965f9SChang S. Bae testl %ebx, %ebx 998c82965f9SChang S. Bae jnz restore_regs_and_return_to_kernel 999c82965f9SChang S. Bae 1000c82965f9SChang S. Bae /* We are returning to a context with user GSBASE */ 1001c82965f9SChang S. Bae SWAPGS_UNSAFE_STACK 1002e5317832SAndy Lutomirski jmp restore_regs_and_return_to_kernel 1003ef1e0315SJiri SlabySYM_CODE_END(paranoid_exit) 1004905a36a2SIngo Molnar 1005905a36a2SIngo Molnar/* 10069e809d15SDominik Brodowski * Save all registers in pt_regs, and switch GS if needed. 1007905a36a2SIngo Molnar */ 1008ef1e0315SJiri SlabySYM_CODE_START_LOCAL(error_entry) 10099e809d15SDominik Brodowski UNWIND_HINT_FUNC 1010905a36a2SIngo Molnar cld 10119e809d15SDominik Brodowski PUSH_AND_CLEAR_REGS save_ret=1 10129e809d15SDominik Brodowski ENCODE_FRAME_POINTER 8 1013905a36a2SIngo Molnar testb $3, CS+8(%rsp) 1014cb6f64edSAndy Lutomirski jz .Lerror_kernelspace 1015539f5113SAndy Lutomirski 1016cb6f64edSAndy Lutomirski /* 1017cb6f64edSAndy Lutomirski * We entered from user mode or we're pretending to have entered 1018cb6f64edSAndy Lutomirski * from user mode due to an IRET fault. 1019cb6f64edSAndy Lutomirski */ 1020905a36a2SIngo Molnar SWAPGS 102118ec54fdSJosh Poimboeuf FENCE_SWAPGS_USER_ENTRY 10228a09317bSDave Hansen /* We have user CR3. Change to kernel CR3. */ 10238a09317bSDave Hansen SWITCH_TO_KERNEL_CR3 scratch_reg=%rax 1024539f5113SAndy Lutomirski 1025cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_after_swapgs: 10267f2590a1SAndy Lutomirski /* Put us onto the real thread stack. */ 10277f2590a1SAndy Lutomirski popq %r12 /* save return addr in %12 */ 10287f2590a1SAndy Lutomirski movq %rsp, %rdi /* arg0 = pt_regs pointer */ 10297f2590a1SAndy Lutomirski call sync_regs 10307f2590a1SAndy Lutomirski movq %rax, %rsp /* switch stack */ 10317f2590a1SAndy Lutomirski ENCODE_FRAME_POINTER 10327f2590a1SAndy Lutomirski pushq %r12 1033f1075053SAndy Lutomirski ret 103402bc7768SAndy Lutomirski 103518ec54fdSJosh Poimboeuf.Lerror_entry_done_lfence: 103618ec54fdSJosh Poimboeuf FENCE_SWAPGS_KERNEL_ENTRY 1037cb6f64edSAndy Lutomirski.Lerror_entry_done: 1038905a36a2SIngo Molnar ret 1039905a36a2SIngo Molnar 1040905a36a2SIngo Molnar /* 1041905a36a2SIngo Molnar * There are two places in the kernel that can potentially fault with 1042905a36a2SIngo Molnar * usergs. Handle them here. B stepping K8s sometimes report a 1043905a36a2SIngo Molnar * truncated RIP for IRET exceptions returning to compat mode. Check 1044905a36a2SIngo Molnar * for these here too. 1045905a36a2SIngo Molnar */ 1046cb6f64edSAndy Lutomirski.Lerror_kernelspace: 1047905a36a2SIngo Molnar leaq native_irq_return_iret(%rip), %rcx 1048905a36a2SIngo Molnar cmpq %rcx, RIP+8(%rsp) 1049cb6f64edSAndy Lutomirski je .Lerror_bad_iret 1050905a36a2SIngo Molnar movl %ecx, %eax /* zero extend */ 1051905a36a2SIngo Molnar cmpq %rax, RIP+8(%rsp) 1052cb6f64edSAndy Lutomirski je .Lbstep_iret 105342c748bbSBorislav Petkov cmpq $.Lgs_change, RIP+8(%rsp) 105418ec54fdSJosh Poimboeuf jne .Lerror_entry_done_lfence 1055539f5113SAndy Lutomirski 1056539f5113SAndy Lutomirski /* 105742c748bbSBorislav Petkov * hack: .Lgs_change can fail with user gsbase. If this happens, fix up 1058539f5113SAndy Lutomirski * gsbase and proceed. We'll fix up the exception and land in 105942c748bbSBorislav Petkov * .Lgs_change's error handler with kernel gsbase. 1060539f5113SAndy Lutomirski */ 10612fa5f04fSWanpeng Li SWAPGS 106218ec54fdSJosh Poimboeuf FENCE_SWAPGS_USER_ENTRY 10632fa5f04fSWanpeng Li jmp .Lerror_entry_done 1064905a36a2SIngo Molnar 1065cb6f64edSAndy Lutomirski.Lbstep_iret: 1066905a36a2SIngo Molnar /* Fix truncated RIP */ 1067905a36a2SIngo Molnar movq %rcx, RIP+8(%rsp) 1068905a36a2SIngo Molnar /* fall through */ 1069905a36a2SIngo Molnar 1070cb6f64edSAndy Lutomirski.Lerror_bad_iret: 1071539f5113SAndy Lutomirski /* 10728a09317bSDave Hansen * We came from an IRET to user mode, so we have user 10738a09317bSDave Hansen * gsbase and CR3. Switch to kernel gsbase and CR3: 1074539f5113SAndy Lutomirski */ 1075905a36a2SIngo Molnar SWAPGS 107618ec54fdSJosh Poimboeuf FENCE_SWAPGS_USER_ENTRY 10778a09317bSDave Hansen SWITCH_TO_KERNEL_CR3 scratch_reg=%rax 1078539f5113SAndy Lutomirski 1079539f5113SAndy Lutomirski /* 1080539f5113SAndy Lutomirski * Pretend that the exception came from user mode: set up pt_regs 1081b3681dd5SAndy Lutomirski * as if we faulted immediately after IRET. 1082539f5113SAndy Lutomirski */ 1083905a36a2SIngo Molnar mov %rsp, %rdi 1084905a36a2SIngo Molnar call fixup_bad_iret 1085905a36a2SIngo Molnar mov %rax, %rsp 1086cb6f64edSAndy Lutomirski jmp .Lerror_entry_from_usermode_after_swapgs 1087ef1e0315SJiri SlabySYM_CODE_END(error_entry) 1088905a36a2SIngo Molnar 1089424c7d0aSThomas GleixnerSYM_CODE_START_LOCAL(error_return) 1090424c7d0aSThomas Gleixner UNWIND_HINT_REGS 1091424c7d0aSThomas Gleixner DEBUG_ENTRY_ASSERT_IRQS_OFF 1092424c7d0aSThomas Gleixner testb $3, CS(%rsp) 1093424c7d0aSThomas Gleixner jz restore_regs_and_return_to_kernel 1094424c7d0aSThomas Gleixner jmp swapgs_restore_regs_and_return_to_usermode 1095424c7d0aSThomas GleixnerSYM_CODE_END(error_return) 1096424c7d0aSThomas Gleixner 1097929bacecSAndy Lutomirski/* 1098929bacecSAndy Lutomirski * Runs on exception stack. Xen PV does not go through this path at all, 1099929bacecSAndy Lutomirski * so we can use real assembly here. 11008a09317bSDave Hansen * 11018a09317bSDave Hansen * Registers: 11028a09317bSDave Hansen * %r14: Used to save/restore the CR3 of the interrupted context 11038a09317bSDave Hansen * when PAGE_TABLE_ISOLATION is in use. Do not clobber. 1104929bacecSAndy Lutomirski */ 11056271fef0SThomas GleixnerSYM_CODE_START(asm_exc_nmi) 11068c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 1107929bacecSAndy Lutomirski 1108fc57a7c6SAndy Lutomirski /* 1109905a36a2SIngo Molnar * We allow breakpoints in NMIs. If a breakpoint occurs, then 1110905a36a2SIngo Molnar * the iretq it performs will take us out of NMI context. 1111905a36a2SIngo Molnar * This means that we can have nested NMIs where the next 1112905a36a2SIngo Molnar * NMI is using the top of the stack of the previous NMI. We 1113905a36a2SIngo Molnar * can't let it execute because the nested NMI will corrupt the 1114905a36a2SIngo Molnar * stack of the previous NMI. NMI handlers are not re-entrant 1115905a36a2SIngo Molnar * anyway. 1116905a36a2SIngo Molnar * 1117905a36a2SIngo Molnar * To handle this case we do the following: 1118905a36a2SIngo Molnar * Check the a special location on the stack that contains 1119905a36a2SIngo Molnar * a variable that is set when NMIs are executing. 1120905a36a2SIngo Molnar * The interrupted task's stack is also checked to see if it 1121905a36a2SIngo Molnar * is an NMI stack. 1122905a36a2SIngo Molnar * If the variable is not set and the stack is not the NMI 1123905a36a2SIngo Molnar * stack then: 1124905a36a2SIngo Molnar * o Set the special variable on the stack 11250b22930eSAndy Lutomirski * o Copy the interrupt frame into an "outermost" location on the 11260b22930eSAndy Lutomirski * stack 11270b22930eSAndy Lutomirski * o Copy the interrupt frame into an "iret" location on the stack 1128905a36a2SIngo Molnar * o Continue processing the NMI 1129905a36a2SIngo Molnar * If the variable is set or the previous stack is the NMI stack: 11300b22930eSAndy Lutomirski * o Modify the "iret" location to jump to the repeat_nmi 1131905a36a2SIngo Molnar * o return back to the first NMI 1132905a36a2SIngo Molnar * 1133905a36a2SIngo Molnar * Now on exit of the first NMI, we first clear the stack variable 1134905a36a2SIngo Molnar * The NMI stack will tell any nested NMIs at that point that it is 1135905a36a2SIngo Molnar * nested. Then we pop the stack normally with iret, and if there was 1136905a36a2SIngo Molnar * a nested NMI that updated the copy interrupt stack frame, a 1137905a36a2SIngo Molnar * jump will be made to the repeat_nmi code that will handle the second 1138905a36a2SIngo Molnar * NMI. 11399b6e6a83SAndy Lutomirski * 11409b6e6a83SAndy Lutomirski * However, espfix prevents us from directly returning to userspace 11419b6e6a83SAndy Lutomirski * with a single IRET instruction. Similarly, IRET to user mode 11429b6e6a83SAndy Lutomirski * can fault. We therefore handle NMIs from user space like 11439b6e6a83SAndy Lutomirski * other IST entries. 1144905a36a2SIngo Molnar */ 1145905a36a2SIngo Molnar 1146e93c1730SAndy Lutomirski ASM_CLAC 1147e93c1730SAndy Lutomirski 1148905a36a2SIngo Molnar /* Use %rdx as our temp variable throughout */ 1149905a36a2SIngo Molnar pushq %rdx 1150905a36a2SIngo Molnar 11519b6e6a83SAndy Lutomirski testb $3, CS-RIP+8(%rsp) 11529b6e6a83SAndy Lutomirski jz .Lnmi_from_kernel 1153905a36a2SIngo Molnar 1154905a36a2SIngo Molnar /* 11559b6e6a83SAndy Lutomirski * NMI from user mode. We need to run on the thread stack, but we 11569b6e6a83SAndy Lutomirski * can't go through the normal entry paths: NMIs are masked, and 11579b6e6a83SAndy Lutomirski * we don't want to enable interrupts, because then we'll end 11589b6e6a83SAndy Lutomirski * up in an awkward situation in which IRQs are on but NMIs 11599b6e6a83SAndy Lutomirski * are off. 116083c133cfSAndy Lutomirski * 116183c133cfSAndy Lutomirski * We also must not push anything to the stack before switching 116283c133cfSAndy Lutomirski * stacks lest we corrupt the "NMI executing" variable. 11639b6e6a83SAndy Lutomirski */ 11649b6e6a83SAndy Lutomirski 1165929bacecSAndy Lutomirski swapgs 11669b6e6a83SAndy Lutomirski cld 116718ec54fdSJosh Poimboeuf FENCE_SWAPGS_USER_ENTRY 11688a09317bSDave Hansen SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx 11699b6e6a83SAndy Lutomirski movq %rsp, %rdx 11709b6e6a83SAndy Lutomirski movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 11718c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS base=%rdx offset=8 11729b6e6a83SAndy Lutomirski pushq 5*8(%rdx) /* pt_regs->ss */ 11739b6e6a83SAndy Lutomirski pushq 4*8(%rdx) /* pt_regs->rsp */ 11749b6e6a83SAndy Lutomirski pushq 3*8(%rdx) /* pt_regs->flags */ 11759b6e6a83SAndy Lutomirski pushq 2*8(%rdx) /* pt_regs->cs */ 11769b6e6a83SAndy Lutomirski pushq 1*8(%rdx) /* pt_regs->rip */ 11778c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 11789b6e6a83SAndy Lutomirski pushq $-1 /* pt_regs->orig_ax */ 117930907fd1SDominik Brodowski PUSH_AND_CLEAR_REGS rdx=(%rdx) 1180946c1911SJosh Poimboeuf ENCODE_FRAME_POINTER 11819b6e6a83SAndy Lutomirski 11829b6e6a83SAndy Lutomirski /* 11839b6e6a83SAndy Lutomirski * At this point we no longer need to worry about stack damage 11849b6e6a83SAndy Lutomirski * due to nesting -- we're on the normal thread stack and we're 11859b6e6a83SAndy Lutomirski * done with the NMI stack. 11869b6e6a83SAndy Lutomirski */ 11879b6e6a83SAndy Lutomirski 11889b6e6a83SAndy Lutomirski movq %rsp, %rdi 11899b6e6a83SAndy Lutomirski movq $-1, %rsi 11906271fef0SThomas Gleixner call exc_nmi 11919b6e6a83SAndy Lutomirski 11929b6e6a83SAndy Lutomirski /* 11939b6e6a83SAndy Lutomirski * Return back to user mode. We must *not* do the normal exit 1194946c1911SJosh Poimboeuf * work, because we don't want to enable interrupts. 11959b6e6a83SAndy Lutomirski */ 11968a055d7fSAndy Lutomirski jmp swapgs_restore_regs_and_return_to_usermode 11979b6e6a83SAndy Lutomirski 11989b6e6a83SAndy Lutomirski.Lnmi_from_kernel: 11999b6e6a83SAndy Lutomirski /* 12000b22930eSAndy Lutomirski * Here's what our stack frame will look like: 12010b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12020b22930eSAndy Lutomirski * | original SS | 12030b22930eSAndy Lutomirski * | original Return RSP | 12040b22930eSAndy Lutomirski * | original RFLAGS | 12050b22930eSAndy Lutomirski * | original CS | 12060b22930eSAndy Lutomirski * | original RIP | 12070b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12080b22930eSAndy Lutomirski * | temp storage for rdx | 12090b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12100b22930eSAndy Lutomirski * | "NMI executing" variable | 12110b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12120b22930eSAndy Lutomirski * | iret SS } Copied from "outermost" frame | 12130b22930eSAndy Lutomirski * | iret Return RSP } on each loop iteration; overwritten | 12140b22930eSAndy Lutomirski * | iret RFLAGS } by a nested NMI to force another | 12150b22930eSAndy Lutomirski * | iret CS } iteration if needed. | 12160b22930eSAndy Lutomirski * | iret RIP } | 12170b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12180b22930eSAndy Lutomirski * | outermost SS } initialized in first_nmi; | 12190b22930eSAndy Lutomirski * | outermost Return RSP } will not be changed before | 12200b22930eSAndy Lutomirski * | outermost RFLAGS } NMI processing is done. | 12210b22930eSAndy Lutomirski * | outermost CS } Copied to "iret" frame on each | 12220b22930eSAndy Lutomirski * | outermost RIP } iteration. | 12230b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12240b22930eSAndy Lutomirski * | pt_regs | 12250b22930eSAndy Lutomirski * +---------------------------------------------------------+ 12260b22930eSAndy Lutomirski * 12270b22930eSAndy Lutomirski * The "original" frame is used by hardware. Before re-enabling 12280b22930eSAndy Lutomirski * NMIs, we need to be done with it, and we need to leave enough 12290b22930eSAndy Lutomirski * space for the asm code here. 12300b22930eSAndy Lutomirski * 12310b22930eSAndy Lutomirski * We return by executing IRET while RSP points to the "iret" frame. 12320b22930eSAndy Lutomirski * That will either return for real or it will loop back into NMI 12330b22930eSAndy Lutomirski * processing. 12340b22930eSAndy Lutomirski * 12350b22930eSAndy Lutomirski * The "outermost" frame is copied to the "iret" frame on each 12360b22930eSAndy Lutomirski * iteration of the loop, so each iteration starts with the "iret" 12370b22930eSAndy Lutomirski * frame pointing to the final return target. 12380b22930eSAndy Lutomirski */ 12390b22930eSAndy Lutomirski 12400b22930eSAndy Lutomirski /* 12410b22930eSAndy Lutomirski * Determine whether we're a nested NMI. 12420b22930eSAndy Lutomirski * 1243a27507caSAndy Lutomirski * If we interrupted kernel code between repeat_nmi and 1244a27507caSAndy Lutomirski * end_repeat_nmi, then we are a nested NMI. We must not 1245a27507caSAndy Lutomirski * modify the "iret" frame because it's being written by 1246a27507caSAndy Lutomirski * the outer NMI. That's okay; the outer NMI handler is 12476271fef0SThomas Gleixner * about to about to call exc_nmi() anyway, so we can just 1248a27507caSAndy Lutomirski * resume the outer NMI. 1249a27507caSAndy Lutomirski */ 1250a27507caSAndy Lutomirski 1251a27507caSAndy Lutomirski movq $repeat_nmi, %rdx 1252a27507caSAndy Lutomirski cmpq 8(%rsp), %rdx 1253a27507caSAndy Lutomirski ja 1f 1254a27507caSAndy Lutomirski movq $end_repeat_nmi, %rdx 1255a27507caSAndy Lutomirski cmpq 8(%rsp), %rdx 1256a27507caSAndy Lutomirski ja nested_nmi_out 1257a27507caSAndy Lutomirski1: 1258a27507caSAndy Lutomirski 1259a27507caSAndy Lutomirski /* 1260a27507caSAndy Lutomirski * Now check "NMI executing". If it's set, then we're nested. 12610b22930eSAndy Lutomirski * This will not detect if we interrupted an outer NMI just 12620b22930eSAndy Lutomirski * before IRET. 1263905a36a2SIngo Molnar */ 1264905a36a2SIngo Molnar cmpl $1, -8(%rsp) 1265905a36a2SIngo Molnar je nested_nmi 1266905a36a2SIngo Molnar 1267905a36a2SIngo Molnar /* 12680b22930eSAndy Lutomirski * Now test if the previous stack was an NMI stack. This covers 12690b22930eSAndy Lutomirski * the case where we interrupt an outer NMI after it clears 1270810bc075SAndy Lutomirski * "NMI executing" but before IRET. We need to be careful, though: 1271810bc075SAndy Lutomirski * there is one case in which RSP could point to the NMI stack 1272810bc075SAndy Lutomirski * despite there being no NMI active: naughty userspace controls 1273810bc075SAndy Lutomirski * RSP at the very beginning of the SYSCALL targets. We can 1274810bc075SAndy Lutomirski * pull a fast one on naughty userspace, though: we program 1275810bc075SAndy Lutomirski * SYSCALL to mask DF, so userspace cannot cause DF to be set 1276810bc075SAndy Lutomirski * if it controls the kernel's RSP. We set DF before we clear 1277810bc075SAndy Lutomirski * "NMI executing". 1278905a36a2SIngo Molnar */ 1279905a36a2SIngo Molnar lea 6*8(%rsp), %rdx 1280905a36a2SIngo Molnar /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */ 1281905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1282905a36a2SIngo Molnar /* If the stack pointer is above the NMI stack, this is a normal NMI */ 1283905a36a2SIngo Molnar ja first_nmi 12844d732138SIngo Molnar 1285905a36a2SIngo Molnar subq $EXCEPTION_STKSZ, %rdx 1286905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1287905a36a2SIngo Molnar /* If it is below the NMI stack, it is a normal NMI */ 1288905a36a2SIngo Molnar jb first_nmi 1289810bc075SAndy Lutomirski 1290810bc075SAndy Lutomirski /* Ah, it is within the NMI stack. */ 1291810bc075SAndy Lutomirski 1292810bc075SAndy Lutomirski testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp) 1293810bc075SAndy Lutomirski jz first_nmi /* RSP was user controlled. */ 1294810bc075SAndy Lutomirski 1295810bc075SAndy Lutomirski /* This is a nested NMI. */ 1296905a36a2SIngo Molnar 1297905a36a2SIngo Molnarnested_nmi: 1298905a36a2SIngo Molnar /* 12990b22930eSAndy Lutomirski * Modify the "iret" frame to point to repeat_nmi, forcing another 13000b22930eSAndy Lutomirski * iteration of NMI handling. 1301905a36a2SIngo Molnar */ 130223a781e9SAndy Lutomirski subq $8, %rsp 1303905a36a2SIngo Molnar leaq -10*8(%rsp), %rdx 1304905a36a2SIngo Molnar pushq $__KERNEL_DS 1305905a36a2SIngo Molnar pushq %rdx 1306905a36a2SIngo Molnar pushfq 1307905a36a2SIngo Molnar pushq $__KERNEL_CS 1308905a36a2SIngo Molnar pushq $repeat_nmi 1309905a36a2SIngo Molnar 1310905a36a2SIngo Molnar /* Put stack back */ 1311905a36a2SIngo Molnar addq $(6*8), %rsp 1312905a36a2SIngo Molnar 1313905a36a2SIngo Molnarnested_nmi_out: 1314905a36a2SIngo Molnar popq %rdx 1315905a36a2SIngo Molnar 13160b22930eSAndy Lutomirski /* We are returning to kernel mode, so this cannot result in a fault. */ 1317929bacecSAndy Lutomirski iretq 1318905a36a2SIngo Molnar 1319905a36a2SIngo Molnarfirst_nmi: 13200b22930eSAndy Lutomirski /* Restore rdx. */ 1321905a36a2SIngo Molnar movq (%rsp), %rdx 1322905a36a2SIngo Molnar 132336f1a77bSAndy Lutomirski /* Make room for "NMI executing". */ 132436f1a77bSAndy Lutomirski pushq $0 1325905a36a2SIngo Molnar 13260b22930eSAndy Lutomirski /* Leave room for the "iret" frame */ 1327905a36a2SIngo Molnar subq $(5*8), %rsp 1328905a36a2SIngo Molnar 13290b22930eSAndy Lutomirski /* Copy the "original" frame to the "outermost" frame */ 1330905a36a2SIngo Molnar .rept 5 1331905a36a2SIngo Molnar pushq 11*8(%rsp) 1332905a36a2SIngo Molnar .endr 13338c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 1334905a36a2SIngo Molnar 1335905a36a2SIngo Molnar /* Everything up to here is safe from nested NMIs */ 1336905a36a2SIngo Molnar 1337a97439aaSAndy Lutomirski#ifdef CONFIG_DEBUG_ENTRY 1338a97439aaSAndy Lutomirski /* 1339a97439aaSAndy Lutomirski * For ease of testing, unmask NMIs right away. Disabled by 1340a97439aaSAndy Lutomirski * default because IRET is very expensive. 1341a97439aaSAndy Lutomirski */ 1342a97439aaSAndy Lutomirski pushq $0 /* SS */ 1343a97439aaSAndy Lutomirski pushq %rsp /* RSP (minus 8 because of the previous push) */ 1344a97439aaSAndy Lutomirski addq $8, (%rsp) /* Fix up RSP */ 1345a97439aaSAndy Lutomirski pushfq /* RFLAGS */ 1346a97439aaSAndy Lutomirski pushq $__KERNEL_CS /* CS */ 1347a97439aaSAndy Lutomirski pushq $1f /* RIP */ 1348929bacecSAndy Lutomirski iretq /* continues at repeat_nmi below */ 13498c1f7558SJosh Poimboeuf UNWIND_HINT_IRET_REGS 1350a97439aaSAndy Lutomirski1: 1351a97439aaSAndy Lutomirski#endif 1352a97439aaSAndy Lutomirski 13530b22930eSAndy Lutomirskirepeat_nmi: 1354905a36a2SIngo Molnar /* 1355905a36a2SIngo Molnar * If there was a nested NMI, the first NMI's iret will return 1356905a36a2SIngo Molnar * here. But NMIs are still enabled and we can take another 1357905a36a2SIngo Molnar * nested NMI. The nested NMI checks the interrupted RIP to see 1358905a36a2SIngo Molnar * if it is between repeat_nmi and end_repeat_nmi, and if so 1359905a36a2SIngo Molnar * it will just return, as we are about to repeat an NMI anyway. 1360905a36a2SIngo Molnar * This makes it safe to copy to the stack frame that a nested 1361905a36a2SIngo Molnar * NMI will update. 13620b22930eSAndy Lutomirski * 13630b22930eSAndy Lutomirski * RSP is pointing to "outermost RIP". gsbase is unknown, but, if 13640b22930eSAndy Lutomirski * we're repeating an NMI, gsbase has the same value that it had on 13650b22930eSAndy Lutomirski * the first iteration. paranoid_entry will load the kernel 13666271fef0SThomas Gleixner * gsbase if needed before we call exc_nmi(). "NMI executing" 136736f1a77bSAndy Lutomirski * is zero. 1368905a36a2SIngo Molnar */ 136936f1a77bSAndy Lutomirski movq $1, 10*8(%rsp) /* Set "NMI executing". */ 1370905a36a2SIngo Molnar 13710b22930eSAndy Lutomirski /* 13720b22930eSAndy Lutomirski * Copy the "outermost" frame to the "iret" frame. NMIs that nest 13730b22930eSAndy Lutomirski * here must not modify the "iret" frame while we're writing to 13740b22930eSAndy Lutomirski * it or it will end up containing garbage. 13750b22930eSAndy Lutomirski */ 1376905a36a2SIngo Molnar addq $(10*8), %rsp 1377905a36a2SIngo Molnar .rept 5 1378905a36a2SIngo Molnar pushq -6*8(%rsp) 1379905a36a2SIngo Molnar .endr 1380905a36a2SIngo Molnar subq $(5*8), %rsp 1381905a36a2SIngo Molnarend_repeat_nmi: 1382905a36a2SIngo Molnar 1383905a36a2SIngo Molnar /* 13840b22930eSAndy Lutomirski * Everything below this point can be preempted by a nested NMI. 13850b22930eSAndy Lutomirski * If this happens, then the inner NMI will change the "iret" 13860b22930eSAndy Lutomirski * frame to point back to repeat_nmi. 1387905a36a2SIngo Molnar */ 1388905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 1389905a36a2SIngo Molnar 1390905a36a2SIngo Molnar /* 1391905a36a2SIngo Molnar * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit 1392905a36a2SIngo Molnar * as we should not be calling schedule in NMI context. 1393905a36a2SIngo Molnar * Even with normal interrupts enabled. An NMI should not be 1394905a36a2SIngo Molnar * setting NEED_RESCHED or anything that normal interrupts and 1395905a36a2SIngo Molnar * exceptions might do. 1396905a36a2SIngo Molnar */ 1397905a36a2SIngo Molnar call paranoid_entry 13988c1f7558SJosh Poimboeuf UNWIND_HINT_REGS 1399905a36a2SIngo Molnar 1400905a36a2SIngo Molnar movq %rsp, %rdi 1401905a36a2SIngo Molnar movq $-1, %rsi 14026271fef0SThomas Gleixner call exc_nmi 1403905a36a2SIngo Molnar 140416561f27SDave Hansen /* Always restore stashed CR3 value (see paranoid_entry) */ 140521e94459SPeter Zijlstra RESTORE_CR3 scratch_reg=%r15 save_reg=%r14 14068a09317bSDave Hansen 1407c82965f9SChang S. Bae /* 1408c82965f9SChang S. Bae * The above invocation of paranoid_entry stored the GSBASE 1409c82965f9SChang S. Bae * related information in R/EBX depending on the availability 1410c82965f9SChang S. Bae * of FSGSBASE. 1411c82965f9SChang S. Bae * 1412c82965f9SChang S. Bae * If FSGSBASE is enabled, restore the saved GSBASE value 1413c82965f9SChang S. Bae * unconditionally, otherwise take the conditional SWAPGS path. 1414c82965f9SChang S. Bae */ 1415c82965f9SChang S. Bae ALTERNATIVE "jmp nmi_no_fsgsbase", "", X86_FEATURE_FSGSBASE 1416c82965f9SChang S. Bae 1417c82965f9SChang S. Bae wrgsbase %rbx 1418c82965f9SChang S. Bae jmp nmi_restore 1419c82965f9SChang S. Bae 1420c82965f9SChang S. Baenmi_no_fsgsbase: 1421c82965f9SChang S. Bae /* EBX == 0 -> invoke SWAPGS */ 1422c82965f9SChang S. Bae testl %ebx, %ebx 1423905a36a2SIngo Molnar jnz nmi_restore 1424c82965f9SChang S. Bae 1425905a36a2SIngo Molnarnmi_swapgs: 1426905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1427c82965f9SChang S. Bae 1428905a36a2SIngo Molnarnmi_restore: 1429502af0d7SDominik Brodowski POP_REGS 14300b22930eSAndy Lutomirski 1431471ee483SAndy Lutomirski /* 1432471ee483SAndy Lutomirski * Skip orig_ax and the "outermost" frame to point RSP at the "iret" 1433471ee483SAndy Lutomirski * at the "iret" frame. 1434471ee483SAndy Lutomirski */ 1435471ee483SAndy Lutomirski addq $6*8, %rsp 1436905a36a2SIngo Molnar 1437810bc075SAndy Lutomirski /* 1438810bc075SAndy Lutomirski * Clear "NMI executing". Set DF first so that we can easily 1439810bc075SAndy Lutomirski * distinguish the remaining code between here and IRET from 1440929bacecSAndy Lutomirski * the SYSCALL entry and exit paths. 1441929bacecSAndy Lutomirski * 1442929bacecSAndy Lutomirski * We arguably should just inspect RIP instead, but I (Andy) wrote 1443929bacecSAndy Lutomirski * this code when I had the misapprehension that Xen PV supported 1444929bacecSAndy Lutomirski * NMIs, and Xen PV would break that approach. 1445810bc075SAndy Lutomirski */ 1446810bc075SAndy Lutomirski std 1447810bc075SAndy Lutomirski movq $0, 5*8(%rsp) /* clear "NMI executing" */ 14480b22930eSAndy Lutomirski 14490b22930eSAndy Lutomirski /* 1450929bacecSAndy Lutomirski * iretq reads the "iret" frame and exits the NMI stack in a 1451929bacecSAndy Lutomirski * single instruction. We are returning to kernel mode, so this 1452929bacecSAndy Lutomirski * cannot result in a fault. Similarly, we don't need to worry 1453929bacecSAndy Lutomirski * about espfix64 on the way back to kernel mode. 14540b22930eSAndy Lutomirski */ 1455929bacecSAndy Lutomirski iretq 14566271fef0SThomas GleixnerSYM_CODE_END(asm_exc_nmi) 1457905a36a2SIngo Molnar 1458dffb3f9dSAndy Lutomirski#ifndef CONFIG_IA32_EMULATION 1459dffb3f9dSAndy Lutomirski/* 1460dffb3f9dSAndy Lutomirski * This handles SYSCALL from 32-bit code. There is no way to program 1461dffb3f9dSAndy Lutomirski * MSRs to fully disable 32-bit SYSCALL. 1462dffb3f9dSAndy Lutomirski */ 1463bc7b11c0SJiri SlabySYM_CODE_START(ignore_sysret) 14648c1f7558SJosh Poimboeuf UNWIND_HINT_EMPTY 1465905a36a2SIngo Molnar mov $-ENOSYS, %eax 1466b2b1d94cSJan Beulich sysretl 1467bc7b11c0SJiri SlabySYM_CODE_END(ignore_sysret) 1468dffb3f9dSAndy Lutomirski#endif 14692deb4be2SAndy Lutomirski 1470b9f6976bSThomas Gleixner.pushsection .text, "ax" 1471bc7b11c0SJiri SlabySYM_CODE_START(rewind_stack_do_exit) 14728c1f7558SJosh Poimboeuf UNWIND_HINT_FUNC 14732deb4be2SAndy Lutomirski /* Prevent any naive code from trying to unwind to our caller. */ 14742deb4be2SAndy Lutomirski xorl %ebp, %ebp 14752deb4be2SAndy Lutomirski 14762deb4be2SAndy Lutomirski movq PER_CPU_VAR(cpu_current_top_of_stack), %rax 14778c1f7558SJosh Poimboeuf leaq -PTREGS_SIZE(%rax), %rsp 1478f977df7bSJann Horn UNWIND_HINT_REGS 14792deb4be2SAndy Lutomirski 14802deb4be2SAndy Lutomirski call do_exit 1481bc7b11c0SJiri SlabySYM_CODE_END(rewind_stack_do_exit) 1482b9f6976bSThomas Gleixner.popsection 1483