1*4882a593Smuzhiyun/* SPDX-License-Identifier: GPL-2.0 */ 2*4882a593Smuzhiyun/* 3*4882a593Smuzhiyun * linux/arch/x86_64/entry.S 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * Copyright (C) 1991, 1992 Linus Torvalds 6*4882a593Smuzhiyun * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs 7*4882a593Smuzhiyun * Copyright (C) 2000 Pavel Machek <pavel@suse.cz> 8*4882a593Smuzhiyun * 9*4882a593Smuzhiyun * entry.S contains the system-call and fault low-level handling routines. 10*4882a593Smuzhiyun * 11*4882a593Smuzhiyun * Some of this is documented in Documentation/x86/entry_64.rst 12*4882a593Smuzhiyun * 13*4882a593Smuzhiyun * A note on terminology: 14*4882a593Smuzhiyun * - iret frame: Architecture defined interrupt frame from SS to RIP 15*4882a593Smuzhiyun * at the top of the kernel process stack. 16*4882a593Smuzhiyun * 17*4882a593Smuzhiyun * Some macro usage: 18*4882a593Smuzhiyun * - SYM_FUNC_START/END:Define functions in the symbol table. 19*4882a593Smuzhiyun * - idtentry: Define exception entry points. 20*4882a593Smuzhiyun */ 21*4882a593Smuzhiyun#include <linux/linkage.h> 22*4882a593Smuzhiyun#include <asm/segment.h> 23*4882a593Smuzhiyun#include <asm/cache.h> 24*4882a593Smuzhiyun#include <asm/errno.h> 25*4882a593Smuzhiyun#include <asm/asm-offsets.h> 26*4882a593Smuzhiyun#include <asm/msr.h> 27*4882a593Smuzhiyun#include <asm/unistd.h> 28*4882a593Smuzhiyun#include <asm/thread_info.h> 29*4882a593Smuzhiyun#include <asm/hw_irq.h> 30*4882a593Smuzhiyun#include <asm/page_types.h> 31*4882a593Smuzhiyun#include <asm/irqflags.h> 32*4882a593Smuzhiyun#include <asm/paravirt.h> 33*4882a593Smuzhiyun#include <asm/percpu.h> 34*4882a593Smuzhiyun#include <asm/asm.h> 35*4882a593Smuzhiyun#include <asm/smap.h> 36*4882a593Smuzhiyun#include <asm/pgtable_types.h> 37*4882a593Smuzhiyun#include <asm/export.h> 38*4882a593Smuzhiyun#include <asm/frame.h> 39*4882a593Smuzhiyun#include <asm/trapnr.h> 40*4882a593Smuzhiyun#include <asm/nospec-branch.h> 41*4882a593Smuzhiyun#include <asm/fsgsbase.h> 42*4882a593Smuzhiyun#include <linux/err.h> 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun#include "calling.h" 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun.code64 47*4882a593Smuzhiyun.section .entry.text, "ax" 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun#ifdef CONFIG_PARAVIRT_XXL 50*4882a593SmuzhiyunSYM_CODE_START(native_usergs_sysret64) 51*4882a593Smuzhiyun UNWIND_HINT_EMPTY 52*4882a593Smuzhiyun swapgs 53*4882a593Smuzhiyun sysretq 54*4882a593SmuzhiyunSYM_CODE_END(native_usergs_sysret64) 55*4882a593Smuzhiyun#endif /* CONFIG_PARAVIRT_XXL */ 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun/* 58*4882a593Smuzhiyun * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers. 59*4882a593Smuzhiyun * 60*4882a593Smuzhiyun * This is the only entry point used for 64-bit system calls. The 61*4882a593Smuzhiyun * hardware interface is reasonably well designed and the register to 62*4882a593Smuzhiyun * argument mapping Linux uses fits well with the registers that are 63*4882a593Smuzhiyun * available when SYSCALL is used. 64*4882a593Smuzhiyun * 65*4882a593Smuzhiyun * SYSCALL instructions can be found inlined in libc implementations as 66*4882a593Smuzhiyun * well as some other programs and libraries. There are also a handful 67*4882a593Smuzhiyun * of SYSCALL instructions in the vDSO used, for example, as a 68*4882a593Smuzhiyun * clock_gettimeofday fallback. 69*4882a593Smuzhiyun * 70*4882a593Smuzhiyun * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11, 71*4882a593Smuzhiyun * then loads new ss, cs, and rip from previously programmed MSRs. 72*4882a593Smuzhiyun * rflags gets masked by a value from another MSR (so CLD and CLAC 73*4882a593Smuzhiyun * are not needed). SYSCALL does not save anything on the stack 74*4882a593Smuzhiyun * and does not change rsp. 75*4882a593Smuzhiyun * 76*4882a593Smuzhiyun * Registers on entry: 77*4882a593Smuzhiyun * rax system call number 78*4882a593Smuzhiyun * rcx return address 79*4882a593Smuzhiyun * r11 saved rflags (note: r11 is callee-clobbered register in C ABI) 80*4882a593Smuzhiyun * rdi arg0 81*4882a593Smuzhiyun * rsi arg1 82*4882a593Smuzhiyun * rdx arg2 83*4882a593Smuzhiyun * r10 arg3 (needs to be moved to rcx to conform to C ABI) 84*4882a593Smuzhiyun * r8 arg4 85*4882a593Smuzhiyun * r9 arg5 86*4882a593Smuzhiyun * (note: r12-r15, rbp, rbx are callee-preserved in C ABI) 87*4882a593Smuzhiyun * 88*4882a593Smuzhiyun * Only called from user space. 89*4882a593Smuzhiyun * 90*4882a593Smuzhiyun * When user can change pt_regs->foo always force IRET. That is because 91*4882a593Smuzhiyun * it deals with uncanonical addresses better. SYSRET has trouble 92*4882a593Smuzhiyun * with them due to bugs in both AMD and Intel CPUs. 93*4882a593Smuzhiyun */ 94*4882a593Smuzhiyun 95*4882a593SmuzhiyunSYM_CODE_START(entry_SYSCALL_64) 96*4882a593Smuzhiyun UNWIND_HINT_ENTRY 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun swapgs 99*4882a593Smuzhiyun /* tss.sp2 is scratch space. */ 100*4882a593Smuzhiyun movq %rsp, PER_CPU_VAR(cpu_tss_rw + TSS_sp2) 101*4882a593Smuzhiyun SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp 102*4882a593Smuzhiyun movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 103*4882a593Smuzhiyun 104*4882a593SmuzhiyunSYM_INNER_LABEL(entry_SYSCALL_64_safe_stack, SYM_L_GLOBAL) 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun /* Construct struct pt_regs on stack */ 107*4882a593Smuzhiyun pushq $__USER_DS /* pt_regs->ss */ 108*4882a593Smuzhiyun pushq PER_CPU_VAR(cpu_tss_rw + TSS_sp2) /* pt_regs->sp */ 109*4882a593Smuzhiyun pushq %r11 /* pt_regs->flags */ 110*4882a593Smuzhiyun pushq $__USER_CS /* pt_regs->cs */ 111*4882a593Smuzhiyun pushq %rcx /* pt_regs->ip */ 112*4882a593SmuzhiyunSYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL) 113*4882a593Smuzhiyun pushq %rax /* pt_regs->orig_ax */ 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun PUSH_AND_CLEAR_REGS rax=$-ENOSYS 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun /* IRQs are off. */ 118*4882a593Smuzhiyun movq %rax, %rdi 119*4882a593Smuzhiyun movq %rsp, %rsi 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun /* clobbers %rax, make sure it is after saving the syscall nr */ 122*4882a593Smuzhiyun IBRS_ENTER 123*4882a593Smuzhiyun UNTRAIN_RET 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun call do_syscall_64 /* returns with IRQs disabled */ 126*4882a593Smuzhiyun 127*4882a593Smuzhiyun /* 128*4882a593Smuzhiyun * Try to use SYSRET instead of IRET if we're returning to 129*4882a593Smuzhiyun * a completely clean 64-bit userspace context. If we're not, 130*4882a593Smuzhiyun * go to the slow exit path. 131*4882a593Smuzhiyun */ 132*4882a593Smuzhiyun movq RCX(%rsp), %rcx 133*4882a593Smuzhiyun movq RIP(%rsp), %r11 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun cmpq %rcx, %r11 /* SYSRET requires RCX == RIP */ 136*4882a593Smuzhiyun jne swapgs_restore_regs_and_return_to_usermode 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun /* 139*4882a593Smuzhiyun * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP 140*4882a593Smuzhiyun * in kernel space. This essentially lets the user take over 141*4882a593Smuzhiyun * the kernel, since userspace controls RSP. 142*4882a593Smuzhiyun * 143*4882a593Smuzhiyun * If width of "canonical tail" ever becomes variable, this will need 144*4882a593Smuzhiyun * to be updated to remain correct on both old and new CPUs. 145*4882a593Smuzhiyun * 146*4882a593Smuzhiyun * Change top bits to match most significant bit (47th or 56th bit 147*4882a593Smuzhiyun * depending on paging mode) in the address. 148*4882a593Smuzhiyun */ 149*4882a593Smuzhiyun#ifdef CONFIG_X86_5LEVEL 150*4882a593Smuzhiyun ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \ 151*4882a593Smuzhiyun "shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57 152*4882a593Smuzhiyun#else 153*4882a593Smuzhiyun shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 154*4882a593Smuzhiyun sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 155*4882a593Smuzhiyun#endif 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun /* If this changed %rcx, it was not canonical */ 158*4882a593Smuzhiyun cmpq %rcx, %r11 159*4882a593Smuzhiyun jne swapgs_restore_regs_and_return_to_usermode 160*4882a593Smuzhiyun 161*4882a593Smuzhiyun cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */ 162*4882a593Smuzhiyun jne swapgs_restore_regs_and_return_to_usermode 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun movq R11(%rsp), %r11 165*4882a593Smuzhiyun cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */ 166*4882a593Smuzhiyun jne swapgs_restore_regs_and_return_to_usermode 167*4882a593Smuzhiyun 168*4882a593Smuzhiyun /* 169*4882a593Smuzhiyun * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot 170*4882a593Smuzhiyun * restore RF properly. If the slowpath sets it for whatever reason, we 171*4882a593Smuzhiyun * need to restore it correctly. 172*4882a593Smuzhiyun * 173*4882a593Smuzhiyun * SYSRET can restore TF, but unlike IRET, restoring TF results in a 174*4882a593Smuzhiyun * trap from userspace immediately after SYSRET. This would cause an 175*4882a593Smuzhiyun * infinite loop whenever #DB happens with register state that satisfies 176*4882a593Smuzhiyun * the opportunistic SYSRET conditions. For example, single-stepping 177*4882a593Smuzhiyun * this user code: 178*4882a593Smuzhiyun * 179*4882a593Smuzhiyun * movq $stuck_here, %rcx 180*4882a593Smuzhiyun * pushfq 181*4882a593Smuzhiyun * popq %r11 182*4882a593Smuzhiyun * stuck_here: 183*4882a593Smuzhiyun * 184*4882a593Smuzhiyun * would never get past 'stuck_here'. 185*4882a593Smuzhiyun */ 186*4882a593Smuzhiyun testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11 187*4882a593Smuzhiyun jnz swapgs_restore_regs_and_return_to_usermode 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun /* nothing to check for RSP */ 190*4882a593Smuzhiyun 191*4882a593Smuzhiyun cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */ 192*4882a593Smuzhiyun jne swapgs_restore_regs_and_return_to_usermode 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun /* 195*4882a593Smuzhiyun * We win! This label is here just for ease of understanding 196*4882a593Smuzhiyun * perf profiles. Nothing jumps here. 197*4882a593Smuzhiyun */ 198*4882a593Smuzhiyunsyscall_return_via_sysret: 199*4882a593Smuzhiyun IBRS_EXIT 200*4882a593Smuzhiyun POP_REGS pop_rdi=0 201*4882a593Smuzhiyun 202*4882a593Smuzhiyun /* 203*4882a593Smuzhiyun * Now all regs are restored except RSP and RDI. 204*4882a593Smuzhiyun * Save old stack pointer and switch to trampoline stack. 205*4882a593Smuzhiyun */ 206*4882a593Smuzhiyun movq %rsp, %rdi 207*4882a593Smuzhiyun movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp 208*4882a593Smuzhiyun UNWIND_HINT_EMPTY 209*4882a593Smuzhiyun 210*4882a593Smuzhiyun pushq RSP-RDI(%rdi) /* RSP */ 211*4882a593Smuzhiyun pushq (%rdi) /* RDI */ 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun /* 214*4882a593Smuzhiyun * We are on the trampoline stack. All regs except RDI are live. 215*4882a593Smuzhiyun * We can do future final exit work right here. 216*4882a593Smuzhiyun */ 217*4882a593Smuzhiyun STACKLEAK_ERASE_NOCLOBBER 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun popq %rdi 222*4882a593Smuzhiyun popq %rsp 223*4882a593Smuzhiyun USERGS_SYSRET64 224*4882a593SmuzhiyunSYM_CODE_END(entry_SYSCALL_64) 225*4882a593Smuzhiyun 226*4882a593Smuzhiyun/* 227*4882a593Smuzhiyun * %rdi: prev task 228*4882a593Smuzhiyun * %rsi: next task 229*4882a593Smuzhiyun */ 230*4882a593Smuzhiyun.pushsection .text, "ax" 231*4882a593SmuzhiyunSYM_FUNC_START(__switch_to_asm) 232*4882a593Smuzhiyun /* 233*4882a593Smuzhiyun * Save callee-saved registers 234*4882a593Smuzhiyun * This must match the order in inactive_task_frame 235*4882a593Smuzhiyun */ 236*4882a593Smuzhiyun pushq %rbp 237*4882a593Smuzhiyun pushq %rbx 238*4882a593Smuzhiyun pushq %r12 239*4882a593Smuzhiyun pushq %r13 240*4882a593Smuzhiyun pushq %r14 241*4882a593Smuzhiyun pushq %r15 242*4882a593Smuzhiyun 243*4882a593Smuzhiyun /* switch stack */ 244*4882a593Smuzhiyun movq %rsp, TASK_threadsp(%rdi) 245*4882a593Smuzhiyun movq TASK_threadsp(%rsi), %rsp 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun#ifdef CONFIG_STACKPROTECTOR 248*4882a593Smuzhiyun movq TASK_stack_canary(%rsi), %rbx 249*4882a593Smuzhiyun movq %rbx, PER_CPU_VAR(fixed_percpu_data) + stack_canary_offset 250*4882a593Smuzhiyun#endif 251*4882a593Smuzhiyun 252*4882a593Smuzhiyun /* 253*4882a593Smuzhiyun * When switching from a shallower to a deeper call stack 254*4882a593Smuzhiyun * the RSB may either underflow or use entries populated 255*4882a593Smuzhiyun * with userspace addresses. On CPUs where those concerns 256*4882a593Smuzhiyun * exist, overwrite the RSB with entries which capture 257*4882a593Smuzhiyun * speculative execution to prevent attack. 258*4882a593Smuzhiyun */ 259*4882a593Smuzhiyun FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW 260*4882a593Smuzhiyun 261*4882a593Smuzhiyun /* restore callee-saved registers */ 262*4882a593Smuzhiyun popq %r15 263*4882a593Smuzhiyun popq %r14 264*4882a593Smuzhiyun popq %r13 265*4882a593Smuzhiyun popq %r12 266*4882a593Smuzhiyun popq %rbx 267*4882a593Smuzhiyun popq %rbp 268*4882a593Smuzhiyun 269*4882a593Smuzhiyun jmp __switch_to 270*4882a593SmuzhiyunSYM_FUNC_END(__switch_to_asm) 271*4882a593Smuzhiyun.popsection 272*4882a593Smuzhiyun 273*4882a593Smuzhiyun/* 274*4882a593Smuzhiyun * A newly forked process directly context switches into this address. 275*4882a593Smuzhiyun * 276*4882a593Smuzhiyun * rax: prev task we switched from 277*4882a593Smuzhiyun * rbx: kernel thread func (NULL for user thread) 278*4882a593Smuzhiyun * r12: kernel thread arg 279*4882a593Smuzhiyun */ 280*4882a593Smuzhiyun.pushsection .text, "ax" 281*4882a593SmuzhiyunSYM_CODE_START(ret_from_fork) 282*4882a593Smuzhiyun UNWIND_HINT_EMPTY 283*4882a593Smuzhiyun movq %rax, %rdi 284*4882a593Smuzhiyun call schedule_tail /* rdi: 'prev' task parameter */ 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun testq %rbx, %rbx /* from kernel_thread? */ 287*4882a593Smuzhiyun jnz 1f /* kernel threads are uncommon */ 288*4882a593Smuzhiyun 289*4882a593Smuzhiyun2: 290*4882a593Smuzhiyun UNWIND_HINT_REGS 291*4882a593Smuzhiyun movq %rsp, %rdi 292*4882a593Smuzhiyun call syscall_exit_to_user_mode /* returns with IRQs disabled */ 293*4882a593Smuzhiyun jmp swapgs_restore_regs_and_return_to_usermode 294*4882a593Smuzhiyun 295*4882a593Smuzhiyun1: 296*4882a593Smuzhiyun /* kernel thread */ 297*4882a593Smuzhiyun UNWIND_HINT_EMPTY 298*4882a593Smuzhiyun movq %r12, %rdi 299*4882a593Smuzhiyun CALL_NOSPEC rbx 300*4882a593Smuzhiyun /* 301*4882a593Smuzhiyun * A kernel thread is allowed to return here after successfully 302*4882a593Smuzhiyun * calling kernel_execve(). Exit to userspace to complete the execve() 303*4882a593Smuzhiyun * syscall. 304*4882a593Smuzhiyun */ 305*4882a593Smuzhiyun movq $0, RAX(%rsp) 306*4882a593Smuzhiyun jmp 2b 307*4882a593SmuzhiyunSYM_CODE_END(ret_from_fork) 308*4882a593Smuzhiyun.popsection 309*4882a593Smuzhiyun 310*4882a593Smuzhiyun.macro DEBUG_ENTRY_ASSERT_IRQS_OFF 311*4882a593Smuzhiyun#ifdef CONFIG_DEBUG_ENTRY 312*4882a593Smuzhiyun pushq %rax 313*4882a593Smuzhiyun SAVE_FLAGS(CLBR_RAX) 314*4882a593Smuzhiyun testl $X86_EFLAGS_IF, %eax 315*4882a593Smuzhiyun jz .Lokay_\@ 316*4882a593Smuzhiyun ud2 317*4882a593Smuzhiyun.Lokay_\@: 318*4882a593Smuzhiyun popq %rax 319*4882a593Smuzhiyun#endif 320*4882a593Smuzhiyun.endm 321*4882a593Smuzhiyun 322*4882a593Smuzhiyun/** 323*4882a593Smuzhiyun * idtentry_body - Macro to emit code calling the C function 324*4882a593Smuzhiyun * @cfunc: C function to be called 325*4882a593Smuzhiyun * @has_error_code: Hardware pushed error code on stack 326*4882a593Smuzhiyun */ 327*4882a593Smuzhiyun.macro idtentry_body cfunc has_error_code:req 328*4882a593Smuzhiyun 329*4882a593Smuzhiyun call error_entry 330*4882a593Smuzhiyun UNWIND_HINT_REGS 331*4882a593Smuzhiyun 332*4882a593Smuzhiyun movq %rsp, %rdi /* pt_regs pointer into 1st argument*/ 333*4882a593Smuzhiyun 334*4882a593Smuzhiyun .if \has_error_code == 1 335*4882a593Smuzhiyun movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/ 336*4882a593Smuzhiyun movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 337*4882a593Smuzhiyun .endif 338*4882a593Smuzhiyun 339*4882a593Smuzhiyun call \cfunc 340*4882a593Smuzhiyun 341*4882a593Smuzhiyun jmp error_return 342*4882a593Smuzhiyun.endm 343*4882a593Smuzhiyun 344*4882a593Smuzhiyun/** 345*4882a593Smuzhiyun * idtentry - Macro to generate entry stubs for simple IDT entries 346*4882a593Smuzhiyun * @vector: Vector number 347*4882a593Smuzhiyun * @asmsym: ASM symbol for the entry point 348*4882a593Smuzhiyun * @cfunc: C function to be called 349*4882a593Smuzhiyun * @has_error_code: Hardware pushed error code on stack 350*4882a593Smuzhiyun * 351*4882a593Smuzhiyun * The macro emits code to set up the kernel context for straight forward 352*4882a593Smuzhiyun * and simple IDT entries. No IST stack, no paranoid entry checks. 353*4882a593Smuzhiyun */ 354*4882a593Smuzhiyun.macro idtentry vector asmsym cfunc has_error_code:req 355*4882a593SmuzhiyunSYM_CODE_START(\asmsym) 356*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS offset=\has_error_code*8 357*4882a593Smuzhiyun ASM_CLAC 358*4882a593Smuzhiyun 359*4882a593Smuzhiyun .if \has_error_code == 0 360*4882a593Smuzhiyun pushq $-1 /* ORIG_RAX: no syscall to restart */ 361*4882a593Smuzhiyun .endif 362*4882a593Smuzhiyun 363*4882a593Smuzhiyun .if \vector == X86_TRAP_BP 364*4882a593Smuzhiyun /* 365*4882a593Smuzhiyun * If coming from kernel space, create a 6-word gap to allow the 366*4882a593Smuzhiyun * int3 handler to emulate a call instruction. 367*4882a593Smuzhiyun */ 368*4882a593Smuzhiyun testb $3, CS-ORIG_RAX(%rsp) 369*4882a593Smuzhiyun jnz .Lfrom_usermode_no_gap_\@ 370*4882a593Smuzhiyun .rept 6 371*4882a593Smuzhiyun pushq 5*8(%rsp) 372*4882a593Smuzhiyun .endr 373*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS offset=8 374*4882a593Smuzhiyun.Lfrom_usermode_no_gap_\@: 375*4882a593Smuzhiyun .endif 376*4882a593Smuzhiyun 377*4882a593Smuzhiyun idtentry_body \cfunc \has_error_code 378*4882a593Smuzhiyun 379*4882a593Smuzhiyun_ASM_NOKPROBE(\asmsym) 380*4882a593SmuzhiyunSYM_CODE_END(\asmsym) 381*4882a593Smuzhiyun.endm 382*4882a593Smuzhiyun 383*4882a593Smuzhiyun/* 384*4882a593Smuzhiyun * Interrupt entry/exit. 385*4882a593Smuzhiyun * 386*4882a593Smuzhiyun + The interrupt stubs push (vector) onto the stack, which is the error_code 387*4882a593Smuzhiyun * position of idtentry exceptions, and jump to one of the two idtentry points 388*4882a593Smuzhiyun * (common/spurious). 389*4882a593Smuzhiyun * 390*4882a593Smuzhiyun * common_interrupt is a hotpath, align it to a cache line 391*4882a593Smuzhiyun */ 392*4882a593Smuzhiyun.macro idtentry_irq vector cfunc 393*4882a593Smuzhiyun .p2align CONFIG_X86_L1_CACHE_SHIFT 394*4882a593Smuzhiyun idtentry \vector asm_\cfunc \cfunc has_error_code=1 395*4882a593Smuzhiyun.endm 396*4882a593Smuzhiyun 397*4882a593Smuzhiyun/* 398*4882a593Smuzhiyun * System vectors which invoke their handlers directly and are not 399*4882a593Smuzhiyun * going through the regular common device interrupt handling code. 400*4882a593Smuzhiyun */ 401*4882a593Smuzhiyun.macro idtentry_sysvec vector cfunc 402*4882a593Smuzhiyun idtentry \vector asm_\cfunc \cfunc has_error_code=0 403*4882a593Smuzhiyun.endm 404*4882a593Smuzhiyun 405*4882a593Smuzhiyun/** 406*4882a593Smuzhiyun * idtentry_mce_db - Macro to generate entry stubs for #MC and #DB 407*4882a593Smuzhiyun * @vector: Vector number 408*4882a593Smuzhiyun * @asmsym: ASM symbol for the entry point 409*4882a593Smuzhiyun * @cfunc: C function to be called 410*4882a593Smuzhiyun * 411*4882a593Smuzhiyun * The macro emits code to set up the kernel context for #MC and #DB 412*4882a593Smuzhiyun * 413*4882a593Smuzhiyun * If the entry comes from user space it uses the normal entry path 414*4882a593Smuzhiyun * including the return to user space work and preemption checks on 415*4882a593Smuzhiyun * exit. 416*4882a593Smuzhiyun * 417*4882a593Smuzhiyun * If hits in kernel mode then it needs to go through the paranoid 418*4882a593Smuzhiyun * entry as the exception can hit any random state. No preemption 419*4882a593Smuzhiyun * check on exit to keep the paranoid path simple. 420*4882a593Smuzhiyun */ 421*4882a593Smuzhiyun.macro idtentry_mce_db vector asmsym cfunc 422*4882a593SmuzhiyunSYM_CODE_START(\asmsym) 423*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS 424*4882a593Smuzhiyun ASM_CLAC 425*4882a593Smuzhiyun 426*4882a593Smuzhiyun pushq $-1 /* ORIG_RAX: no syscall to restart */ 427*4882a593Smuzhiyun 428*4882a593Smuzhiyun /* 429*4882a593Smuzhiyun * If the entry is from userspace, switch stacks and treat it as 430*4882a593Smuzhiyun * a normal entry. 431*4882a593Smuzhiyun */ 432*4882a593Smuzhiyun testb $3, CS-ORIG_RAX(%rsp) 433*4882a593Smuzhiyun jnz .Lfrom_usermode_switch_stack_\@ 434*4882a593Smuzhiyun 435*4882a593Smuzhiyun /* paranoid_entry returns GS information for paranoid_exit in EBX. */ 436*4882a593Smuzhiyun call paranoid_entry 437*4882a593Smuzhiyun 438*4882a593Smuzhiyun UNWIND_HINT_REGS 439*4882a593Smuzhiyun 440*4882a593Smuzhiyun movq %rsp, %rdi /* pt_regs pointer */ 441*4882a593Smuzhiyun 442*4882a593Smuzhiyun call \cfunc 443*4882a593Smuzhiyun 444*4882a593Smuzhiyun jmp paranoid_exit 445*4882a593Smuzhiyun 446*4882a593Smuzhiyun /* Switch to the regular task stack and use the noist entry point */ 447*4882a593Smuzhiyun.Lfrom_usermode_switch_stack_\@: 448*4882a593Smuzhiyun idtentry_body noist_\cfunc, has_error_code=0 449*4882a593Smuzhiyun 450*4882a593Smuzhiyun_ASM_NOKPROBE(\asmsym) 451*4882a593SmuzhiyunSYM_CODE_END(\asmsym) 452*4882a593Smuzhiyun.endm 453*4882a593Smuzhiyun 454*4882a593Smuzhiyun#ifdef CONFIG_AMD_MEM_ENCRYPT 455*4882a593Smuzhiyun/** 456*4882a593Smuzhiyun * idtentry_vc - Macro to generate entry stub for #VC 457*4882a593Smuzhiyun * @vector: Vector number 458*4882a593Smuzhiyun * @asmsym: ASM symbol for the entry point 459*4882a593Smuzhiyun * @cfunc: C function to be called 460*4882a593Smuzhiyun * 461*4882a593Smuzhiyun * The macro emits code to set up the kernel context for #VC. The #VC handler 462*4882a593Smuzhiyun * runs on an IST stack and needs to be able to cause nested #VC exceptions. 463*4882a593Smuzhiyun * 464*4882a593Smuzhiyun * To make this work the #VC entry code tries its best to pretend it doesn't use 465*4882a593Smuzhiyun * an IST stack by switching to the task stack if coming from user-space (which 466*4882a593Smuzhiyun * includes early SYSCALL entry path) or back to the stack in the IRET frame if 467*4882a593Smuzhiyun * entered from kernel-mode. 468*4882a593Smuzhiyun * 469*4882a593Smuzhiyun * If entered from kernel-mode the return stack is validated first, and if it is 470*4882a593Smuzhiyun * not safe to use (e.g. because it points to the entry stack) the #VC handler 471*4882a593Smuzhiyun * will switch to a fall-back stack (VC2) and call a special handler function. 472*4882a593Smuzhiyun * 473*4882a593Smuzhiyun * The macro is only used for one vector, but it is planned to be extended in 474*4882a593Smuzhiyun * the future for the #HV exception. 475*4882a593Smuzhiyun */ 476*4882a593Smuzhiyun.macro idtentry_vc vector asmsym cfunc 477*4882a593SmuzhiyunSYM_CODE_START(\asmsym) 478*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS 479*4882a593Smuzhiyun ASM_CLAC 480*4882a593Smuzhiyun 481*4882a593Smuzhiyun /* 482*4882a593Smuzhiyun * If the entry is from userspace, switch stacks and treat it as 483*4882a593Smuzhiyun * a normal entry. 484*4882a593Smuzhiyun */ 485*4882a593Smuzhiyun testb $3, CS-ORIG_RAX(%rsp) 486*4882a593Smuzhiyun jnz .Lfrom_usermode_switch_stack_\@ 487*4882a593Smuzhiyun 488*4882a593Smuzhiyun /* 489*4882a593Smuzhiyun * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX. 490*4882a593Smuzhiyun * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS 491*4882a593Smuzhiyun */ 492*4882a593Smuzhiyun call paranoid_entry 493*4882a593Smuzhiyun 494*4882a593Smuzhiyun UNWIND_HINT_REGS 495*4882a593Smuzhiyun 496*4882a593Smuzhiyun /* 497*4882a593Smuzhiyun * Switch off the IST stack to make it free for nested exceptions. The 498*4882a593Smuzhiyun * vc_switch_off_ist() function will switch back to the interrupted 499*4882a593Smuzhiyun * stack if it is safe to do so. If not it switches to the VC fall-back 500*4882a593Smuzhiyun * stack. 501*4882a593Smuzhiyun */ 502*4882a593Smuzhiyun movq %rsp, %rdi /* pt_regs pointer */ 503*4882a593Smuzhiyun call vc_switch_off_ist 504*4882a593Smuzhiyun movq %rax, %rsp /* Switch to new stack */ 505*4882a593Smuzhiyun 506*4882a593Smuzhiyun ENCODE_FRAME_POINTER 507*4882a593Smuzhiyun UNWIND_HINT_REGS 508*4882a593Smuzhiyun 509*4882a593Smuzhiyun /* Update pt_regs */ 510*4882a593Smuzhiyun movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/ 511*4882a593Smuzhiyun movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 512*4882a593Smuzhiyun 513*4882a593Smuzhiyun movq %rsp, %rdi /* pt_regs pointer */ 514*4882a593Smuzhiyun 515*4882a593Smuzhiyun call kernel_\cfunc 516*4882a593Smuzhiyun 517*4882a593Smuzhiyun /* 518*4882a593Smuzhiyun * No need to switch back to the IST stack. The current stack is either 519*4882a593Smuzhiyun * identical to the stack in the IRET frame or the VC fall-back stack, 520*4882a593Smuzhiyun * so it is definitly mapped even with PTI enabled. 521*4882a593Smuzhiyun */ 522*4882a593Smuzhiyun jmp paranoid_exit 523*4882a593Smuzhiyun 524*4882a593Smuzhiyun /* Switch to the regular task stack */ 525*4882a593Smuzhiyun.Lfrom_usermode_switch_stack_\@: 526*4882a593Smuzhiyun idtentry_body user_\cfunc, has_error_code=1 527*4882a593Smuzhiyun 528*4882a593Smuzhiyun_ASM_NOKPROBE(\asmsym) 529*4882a593SmuzhiyunSYM_CODE_END(\asmsym) 530*4882a593Smuzhiyun.endm 531*4882a593Smuzhiyun#endif 532*4882a593Smuzhiyun 533*4882a593Smuzhiyun/* 534*4882a593Smuzhiyun * Double fault entry. Straight paranoid. No checks from which context 535*4882a593Smuzhiyun * this comes because for the espfix induced #DF this would do the wrong 536*4882a593Smuzhiyun * thing. 537*4882a593Smuzhiyun */ 538*4882a593Smuzhiyun.macro idtentry_df vector asmsym cfunc 539*4882a593SmuzhiyunSYM_CODE_START(\asmsym) 540*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS offset=8 541*4882a593Smuzhiyun ASM_CLAC 542*4882a593Smuzhiyun 543*4882a593Smuzhiyun /* paranoid_entry returns GS information for paranoid_exit in EBX. */ 544*4882a593Smuzhiyun call paranoid_entry 545*4882a593Smuzhiyun UNWIND_HINT_REGS 546*4882a593Smuzhiyun 547*4882a593Smuzhiyun movq %rsp, %rdi /* pt_regs pointer into first argument */ 548*4882a593Smuzhiyun movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/ 549*4882a593Smuzhiyun movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 550*4882a593Smuzhiyun call \cfunc 551*4882a593Smuzhiyun 552*4882a593Smuzhiyun jmp paranoid_exit 553*4882a593Smuzhiyun 554*4882a593Smuzhiyun_ASM_NOKPROBE(\asmsym) 555*4882a593SmuzhiyunSYM_CODE_END(\asmsym) 556*4882a593Smuzhiyun.endm 557*4882a593Smuzhiyun 558*4882a593Smuzhiyun/* 559*4882a593Smuzhiyun * Include the defines which emit the idt entries which are shared 560*4882a593Smuzhiyun * shared between 32 and 64 bit and emit the __irqentry_text_* markers 561*4882a593Smuzhiyun * so the stacktrace boundary checks work. 562*4882a593Smuzhiyun */ 563*4882a593Smuzhiyun .align 16 564*4882a593Smuzhiyun .globl __irqentry_text_start 565*4882a593Smuzhiyun__irqentry_text_start: 566*4882a593Smuzhiyun 567*4882a593Smuzhiyun#include <asm/idtentry.h> 568*4882a593Smuzhiyun 569*4882a593Smuzhiyun .align 16 570*4882a593Smuzhiyun .globl __irqentry_text_end 571*4882a593Smuzhiyun__irqentry_text_end: 572*4882a593Smuzhiyun 573*4882a593SmuzhiyunSYM_CODE_START_LOCAL(common_interrupt_return) 574*4882a593SmuzhiyunSYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL) 575*4882a593Smuzhiyun IBRS_EXIT 576*4882a593Smuzhiyun#ifdef CONFIG_DEBUG_ENTRY 577*4882a593Smuzhiyun /* Assert that pt_regs indicates user mode. */ 578*4882a593Smuzhiyun testb $3, CS(%rsp) 579*4882a593Smuzhiyun jnz 1f 580*4882a593Smuzhiyun ud2 581*4882a593Smuzhiyun1: 582*4882a593Smuzhiyun#endif 583*4882a593Smuzhiyun#ifdef CONFIG_XEN_PV 584*4882a593Smuzhiyun ALTERNATIVE "", "jmp xenpv_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV 585*4882a593Smuzhiyun#endif 586*4882a593Smuzhiyun 587*4882a593Smuzhiyun POP_REGS pop_rdi=0 588*4882a593Smuzhiyun 589*4882a593Smuzhiyun /* 590*4882a593Smuzhiyun * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS. 591*4882a593Smuzhiyun * Save old stack pointer and switch to trampoline stack. 592*4882a593Smuzhiyun */ 593*4882a593Smuzhiyun movq %rsp, %rdi 594*4882a593Smuzhiyun movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp 595*4882a593Smuzhiyun UNWIND_HINT_EMPTY 596*4882a593Smuzhiyun 597*4882a593Smuzhiyun /* Copy the IRET frame to the trampoline stack. */ 598*4882a593Smuzhiyun pushq 6*8(%rdi) /* SS */ 599*4882a593Smuzhiyun pushq 5*8(%rdi) /* RSP */ 600*4882a593Smuzhiyun pushq 4*8(%rdi) /* EFLAGS */ 601*4882a593Smuzhiyun pushq 3*8(%rdi) /* CS */ 602*4882a593Smuzhiyun pushq 2*8(%rdi) /* RIP */ 603*4882a593Smuzhiyun 604*4882a593Smuzhiyun /* Push user RDI on the trampoline stack. */ 605*4882a593Smuzhiyun pushq (%rdi) 606*4882a593Smuzhiyun 607*4882a593Smuzhiyun /* 608*4882a593Smuzhiyun * We are on the trampoline stack. All regs except RDI are live. 609*4882a593Smuzhiyun * We can do future final exit work right here. 610*4882a593Smuzhiyun */ 611*4882a593Smuzhiyun STACKLEAK_ERASE_NOCLOBBER 612*4882a593Smuzhiyun 613*4882a593Smuzhiyun SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi 614*4882a593Smuzhiyun 615*4882a593Smuzhiyun /* Restore RDI. */ 616*4882a593Smuzhiyun popq %rdi 617*4882a593Smuzhiyun SWAPGS 618*4882a593Smuzhiyun INTERRUPT_RETURN 619*4882a593Smuzhiyun 620*4882a593Smuzhiyun 621*4882a593SmuzhiyunSYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL) 622*4882a593Smuzhiyun#ifdef CONFIG_DEBUG_ENTRY 623*4882a593Smuzhiyun /* Assert that pt_regs indicates kernel mode. */ 624*4882a593Smuzhiyun testb $3, CS(%rsp) 625*4882a593Smuzhiyun jz 1f 626*4882a593Smuzhiyun ud2 627*4882a593Smuzhiyun1: 628*4882a593Smuzhiyun#endif 629*4882a593Smuzhiyun POP_REGS 630*4882a593Smuzhiyun addq $8, %rsp /* skip regs->orig_ax */ 631*4882a593Smuzhiyun /* 632*4882a593Smuzhiyun * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization 633*4882a593Smuzhiyun * when returning from IPI handler. 634*4882a593Smuzhiyun */ 635*4882a593Smuzhiyun INTERRUPT_RETURN 636*4882a593Smuzhiyun 637*4882a593SmuzhiyunSYM_INNER_LABEL_ALIGN(native_iret, SYM_L_GLOBAL) 638*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS 639*4882a593Smuzhiyun /* 640*4882a593Smuzhiyun * Are we returning to a stack segment from the LDT? Note: in 641*4882a593Smuzhiyun * 64-bit mode SS:RSP on the exception stack is always valid. 642*4882a593Smuzhiyun */ 643*4882a593Smuzhiyun#ifdef CONFIG_X86_ESPFIX64 644*4882a593Smuzhiyun testb $4, (SS-RIP)(%rsp) 645*4882a593Smuzhiyun jnz native_irq_return_ldt 646*4882a593Smuzhiyun#endif 647*4882a593Smuzhiyun 648*4882a593SmuzhiyunSYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL) 649*4882a593Smuzhiyun /* 650*4882a593Smuzhiyun * This may fault. Non-paranoid faults on return to userspace are 651*4882a593Smuzhiyun * handled by fixup_bad_iret. These include #SS, #GP, and #NP. 652*4882a593Smuzhiyun * Double-faults due to espfix64 are handled in exc_double_fault. 653*4882a593Smuzhiyun * Other faults here are fatal. 654*4882a593Smuzhiyun */ 655*4882a593Smuzhiyun iretq 656*4882a593Smuzhiyun 657*4882a593Smuzhiyun#ifdef CONFIG_X86_ESPFIX64 658*4882a593Smuzhiyunnative_irq_return_ldt: 659*4882a593Smuzhiyun /* 660*4882a593Smuzhiyun * We are running with user GSBASE. All GPRs contain their user 661*4882a593Smuzhiyun * values. We have a percpu ESPFIX stack that is eight slots 662*4882a593Smuzhiyun * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom 663*4882a593Smuzhiyun * of the ESPFIX stack. 664*4882a593Smuzhiyun * 665*4882a593Smuzhiyun * We clobber RAX and RDI in this code. We stash RDI on the 666*4882a593Smuzhiyun * normal stack and RAX on the ESPFIX stack. 667*4882a593Smuzhiyun * 668*4882a593Smuzhiyun * The ESPFIX stack layout we set up looks like this: 669*4882a593Smuzhiyun * 670*4882a593Smuzhiyun * --- top of ESPFIX stack --- 671*4882a593Smuzhiyun * SS 672*4882a593Smuzhiyun * RSP 673*4882a593Smuzhiyun * RFLAGS 674*4882a593Smuzhiyun * CS 675*4882a593Smuzhiyun * RIP <-- RSP points here when we're done 676*4882a593Smuzhiyun * RAX <-- espfix_waddr points here 677*4882a593Smuzhiyun * --- bottom of ESPFIX stack --- 678*4882a593Smuzhiyun */ 679*4882a593Smuzhiyun 680*4882a593Smuzhiyun pushq %rdi /* Stash user RDI */ 681*4882a593Smuzhiyun swapgs /* to kernel GS */ 682*4882a593Smuzhiyun SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi /* to kernel CR3 */ 683*4882a593Smuzhiyun UNTRAIN_RET 684*4882a593Smuzhiyun 685*4882a593Smuzhiyun movq PER_CPU_VAR(espfix_waddr), %rdi 686*4882a593Smuzhiyun movq %rax, (0*8)(%rdi) /* user RAX */ 687*4882a593Smuzhiyun movq (1*8)(%rsp), %rax /* user RIP */ 688*4882a593Smuzhiyun movq %rax, (1*8)(%rdi) 689*4882a593Smuzhiyun movq (2*8)(%rsp), %rax /* user CS */ 690*4882a593Smuzhiyun movq %rax, (2*8)(%rdi) 691*4882a593Smuzhiyun movq (3*8)(%rsp), %rax /* user RFLAGS */ 692*4882a593Smuzhiyun movq %rax, (3*8)(%rdi) 693*4882a593Smuzhiyun movq (5*8)(%rsp), %rax /* user SS */ 694*4882a593Smuzhiyun movq %rax, (5*8)(%rdi) 695*4882a593Smuzhiyun movq (4*8)(%rsp), %rax /* user RSP */ 696*4882a593Smuzhiyun movq %rax, (4*8)(%rdi) 697*4882a593Smuzhiyun /* Now RAX == RSP. */ 698*4882a593Smuzhiyun 699*4882a593Smuzhiyun andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */ 700*4882a593Smuzhiyun 701*4882a593Smuzhiyun /* 702*4882a593Smuzhiyun * espfix_stack[31:16] == 0. The page tables are set up such that 703*4882a593Smuzhiyun * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of 704*4882a593Smuzhiyun * espfix_waddr for any X. That is, there are 65536 RO aliases of 705*4882a593Smuzhiyun * the same page. Set up RSP so that RSP[31:16] contains the 706*4882a593Smuzhiyun * respective 16 bits of the /userspace/ RSP and RSP nonetheless 707*4882a593Smuzhiyun * still points to an RO alias of the ESPFIX stack. 708*4882a593Smuzhiyun */ 709*4882a593Smuzhiyun orq PER_CPU_VAR(espfix_stack), %rax 710*4882a593Smuzhiyun 711*4882a593Smuzhiyun SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi 712*4882a593Smuzhiyun swapgs /* to user GS */ 713*4882a593Smuzhiyun popq %rdi /* Restore user RDI */ 714*4882a593Smuzhiyun 715*4882a593Smuzhiyun movq %rax, %rsp 716*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS offset=8 717*4882a593Smuzhiyun 718*4882a593Smuzhiyun /* 719*4882a593Smuzhiyun * At this point, we cannot write to the stack any more, but we can 720*4882a593Smuzhiyun * still read. 721*4882a593Smuzhiyun */ 722*4882a593Smuzhiyun popq %rax /* Restore user RAX */ 723*4882a593Smuzhiyun 724*4882a593Smuzhiyun /* 725*4882a593Smuzhiyun * RSP now points to an ordinary IRET frame, except that the page 726*4882a593Smuzhiyun * is read-only and RSP[31:16] are preloaded with the userspace 727*4882a593Smuzhiyun * values. We can now IRET back to userspace. 728*4882a593Smuzhiyun */ 729*4882a593Smuzhiyun jmp native_irq_return_iret 730*4882a593Smuzhiyun#endif 731*4882a593SmuzhiyunSYM_CODE_END(common_interrupt_return) 732*4882a593Smuzhiyun_ASM_NOKPROBE(common_interrupt_return) 733*4882a593Smuzhiyun 734*4882a593Smuzhiyun/* 735*4882a593Smuzhiyun * Reload gs selector with exception handling 736*4882a593Smuzhiyun * edi: new selector 737*4882a593Smuzhiyun * 738*4882a593Smuzhiyun * Is in entry.text as it shouldn't be instrumented. 739*4882a593Smuzhiyun */ 740*4882a593SmuzhiyunSYM_FUNC_START(asm_load_gs_index) 741*4882a593Smuzhiyun FRAME_BEGIN 742*4882a593Smuzhiyun swapgs 743*4882a593Smuzhiyun.Lgs_change: 744*4882a593Smuzhiyun movl %edi, %gs 745*4882a593Smuzhiyun2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE 746*4882a593Smuzhiyun swapgs 747*4882a593Smuzhiyun FRAME_END 748*4882a593Smuzhiyun RET 749*4882a593SmuzhiyunSYM_FUNC_END(asm_load_gs_index) 750*4882a593SmuzhiyunEXPORT_SYMBOL(asm_load_gs_index) 751*4882a593Smuzhiyun 752*4882a593Smuzhiyun _ASM_EXTABLE(.Lgs_change, .Lbad_gs) 753*4882a593Smuzhiyun .section .fixup, "ax" 754*4882a593Smuzhiyun /* running with kernelgs */ 755*4882a593SmuzhiyunSYM_CODE_START_LOCAL_NOALIGN(.Lbad_gs) 756*4882a593Smuzhiyun swapgs /* switch back to user gs */ 757*4882a593Smuzhiyun.macro ZAP_GS 758*4882a593Smuzhiyun /* This can't be a string because the preprocessor needs to see it. */ 759*4882a593Smuzhiyun movl $__USER_DS, %eax 760*4882a593Smuzhiyun movl %eax, %gs 761*4882a593Smuzhiyun.endm 762*4882a593Smuzhiyun ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG 763*4882a593Smuzhiyun xorl %eax, %eax 764*4882a593Smuzhiyun movl %eax, %gs 765*4882a593Smuzhiyun jmp 2b 766*4882a593SmuzhiyunSYM_CODE_END(.Lbad_gs) 767*4882a593Smuzhiyun .previous 768*4882a593Smuzhiyun 769*4882a593Smuzhiyun/* 770*4882a593Smuzhiyun * rdi: New stack pointer points to the top word of the stack 771*4882a593Smuzhiyun * rsi: Function pointer 772*4882a593Smuzhiyun * rdx: Function argument (can be NULL if none) 773*4882a593Smuzhiyun */ 774*4882a593SmuzhiyunSYM_FUNC_START(asm_call_on_stack) 775*4882a593SmuzhiyunSYM_INNER_LABEL(asm_call_sysvec_on_stack, SYM_L_GLOBAL) 776*4882a593SmuzhiyunSYM_INNER_LABEL(asm_call_irq_on_stack, SYM_L_GLOBAL) 777*4882a593Smuzhiyun /* 778*4882a593Smuzhiyun * Save the frame pointer unconditionally. This allows the ORC 779*4882a593Smuzhiyun * unwinder to handle the stack switch. 780*4882a593Smuzhiyun */ 781*4882a593Smuzhiyun pushq %rbp 782*4882a593Smuzhiyun mov %rsp, %rbp 783*4882a593Smuzhiyun 784*4882a593Smuzhiyun /* 785*4882a593Smuzhiyun * The unwinder relies on the word at the top of the new stack 786*4882a593Smuzhiyun * page linking back to the previous RSP. 787*4882a593Smuzhiyun */ 788*4882a593Smuzhiyun mov %rsp, (%rdi) 789*4882a593Smuzhiyun mov %rdi, %rsp 790*4882a593Smuzhiyun /* Move the argument to the right place */ 791*4882a593Smuzhiyun mov %rdx, %rdi 792*4882a593Smuzhiyun 793*4882a593Smuzhiyun1: 794*4882a593Smuzhiyun .pushsection .discard.instr_begin 795*4882a593Smuzhiyun .long 1b - . 796*4882a593Smuzhiyun .popsection 797*4882a593Smuzhiyun 798*4882a593Smuzhiyun CALL_NOSPEC rsi 799*4882a593Smuzhiyun 800*4882a593Smuzhiyun2: 801*4882a593Smuzhiyun .pushsection .discard.instr_end 802*4882a593Smuzhiyun .long 2b - . 803*4882a593Smuzhiyun .popsection 804*4882a593Smuzhiyun 805*4882a593Smuzhiyun /* Restore the previous stack pointer from RBP. */ 806*4882a593Smuzhiyun leaveq 807*4882a593Smuzhiyun RET 808*4882a593SmuzhiyunSYM_FUNC_END(asm_call_on_stack) 809*4882a593Smuzhiyun 810*4882a593Smuzhiyun#ifdef CONFIG_XEN_PV 811*4882a593Smuzhiyun/* 812*4882a593Smuzhiyun * A note on the "critical region" in our callback handler. 813*4882a593Smuzhiyun * We want to avoid stacking callback handlers due to events occurring 814*4882a593Smuzhiyun * during handling of the last event. To do this, we keep events disabled 815*4882a593Smuzhiyun * until we've done all processing. HOWEVER, we must enable events before 816*4882a593Smuzhiyun * popping the stack frame (can't be done atomically) and so it would still 817*4882a593Smuzhiyun * be possible to get enough handler activations to overflow the stack. 818*4882a593Smuzhiyun * Although unlikely, bugs of that kind are hard to track down, so we'd 819*4882a593Smuzhiyun * like to avoid the possibility. 820*4882a593Smuzhiyun * So, on entry to the handler we detect whether we interrupted an 821*4882a593Smuzhiyun * existing activation in its critical region -- if so, we pop the current 822*4882a593Smuzhiyun * activation and restart the handler using the previous one. 823*4882a593Smuzhiyun * 824*4882a593Smuzhiyun * C calling convention: exc_xen_hypervisor_callback(struct *pt_regs) 825*4882a593Smuzhiyun */ 826*4882a593SmuzhiyunSYM_CODE_START_LOCAL(exc_xen_hypervisor_callback) 827*4882a593Smuzhiyun 828*4882a593Smuzhiyun/* 829*4882a593Smuzhiyun * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will 830*4882a593Smuzhiyun * see the correct pointer to the pt_regs 831*4882a593Smuzhiyun */ 832*4882a593Smuzhiyun UNWIND_HINT_FUNC 833*4882a593Smuzhiyun movq %rdi, %rsp /* we don't return, adjust the stack frame */ 834*4882a593Smuzhiyun UNWIND_HINT_REGS 835*4882a593Smuzhiyun 836*4882a593Smuzhiyun call xen_pv_evtchn_do_upcall 837*4882a593Smuzhiyun 838*4882a593Smuzhiyun jmp error_return 839*4882a593SmuzhiyunSYM_CODE_END(exc_xen_hypervisor_callback) 840*4882a593Smuzhiyun 841*4882a593Smuzhiyun/* 842*4882a593Smuzhiyun * Hypervisor uses this for application faults while it executes. 843*4882a593Smuzhiyun * We get here for two reasons: 844*4882a593Smuzhiyun * 1. Fault while reloading DS, ES, FS or GS 845*4882a593Smuzhiyun * 2. Fault while executing IRET 846*4882a593Smuzhiyun * Category 1 we do not need to fix up as Xen has already reloaded all segment 847*4882a593Smuzhiyun * registers that could be reloaded and zeroed the others. 848*4882a593Smuzhiyun * Category 2 we fix up by killing the current process. We cannot use the 849*4882a593Smuzhiyun * normal Linux return path in this case because if we use the IRET hypercall 850*4882a593Smuzhiyun * to pop the stack frame we end up in an infinite loop of failsafe callbacks. 851*4882a593Smuzhiyun * We distinguish between categories by comparing each saved segment register 852*4882a593Smuzhiyun * with its current contents: any discrepancy means we in category 1. 853*4882a593Smuzhiyun */ 854*4882a593SmuzhiyunSYM_CODE_START(xen_failsafe_callback) 855*4882a593Smuzhiyun UNWIND_HINT_EMPTY 856*4882a593Smuzhiyun movl %ds, %ecx 857*4882a593Smuzhiyun cmpw %cx, 0x10(%rsp) 858*4882a593Smuzhiyun jne 1f 859*4882a593Smuzhiyun movl %es, %ecx 860*4882a593Smuzhiyun cmpw %cx, 0x18(%rsp) 861*4882a593Smuzhiyun jne 1f 862*4882a593Smuzhiyun movl %fs, %ecx 863*4882a593Smuzhiyun cmpw %cx, 0x20(%rsp) 864*4882a593Smuzhiyun jne 1f 865*4882a593Smuzhiyun movl %gs, %ecx 866*4882a593Smuzhiyun cmpw %cx, 0x28(%rsp) 867*4882a593Smuzhiyun jne 1f 868*4882a593Smuzhiyun /* All segments match their saved values => Category 2 (Bad IRET). */ 869*4882a593Smuzhiyun movq (%rsp), %rcx 870*4882a593Smuzhiyun movq 8(%rsp), %r11 871*4882a593Smuzhiyun addq $0x30, %rsp 872*4882a593Smuzhiyun pushq $0 /* RIP */ 873*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS offset=8 874*4882a593Smuzhiyun jmp asm_exc_general_protection 875*4882a593Smuzhiyun1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */ 876*4882a593Smuzhiyun movq (%rsp), %rcx 877*4882a593Smuzhiyun movq 8(%rsp), %r11 878*4882a593Smuzhiyun addq $0x30, %rsp 879*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS 880*4882a593Smuzhiyun pushq $-1 /* orig_ax = -1 => not a system call */ 881*4882a593Smuzhiyun PUSH_AND_CLEAR_REGS 882*4882a593Smuzhiyun ENCODE_FRAME_POINTER 883*4882a593Smuzhiyun jmp error_return 884*4882a593SmuzhiyunSYM_CODE_END(xen_failsafe_callback) 885*4882a593Smuzhiyun#endif /* CONFIG_XEN_PV */ 886*4882a593Smuzhiyun 887*4882a593Smuzhiyun/* 888*4882a593Smuzhiyun * Save all registers in pt_regs. Return GSBASE related information 889*4882a593Smuzhiyun * in EBX depending on the availability of the FSGSBASE instructions: 890*4882a593Smuzhiyun * 891*4882a593Smuzhiyun * FSGSBASE R/EBX 892*4882a593Smuzhiyun * N 0 -> SWAPGS on exit 893*4882a593Smuzhiyun * 1 -> no SWAPGS on exit 894*4882a593Smuzhiyun * 895*4882a593Smuzhiyun * Y GSBASE value at entry, must be restored in paranoid_exit 896*4882a593Smuzhiyun * 897*4882a593Smuzhiyun * R14 - old CR3 898*4882a593Smuzhiyun * R15 - old SPEC_CTRL 899*4882a593Smuzhiyun */ 900*4882a593SmuzhiyunSYM_CODE_START_LOCAL(paranoid_entry) 901*4882a593Smuzhiyun UNWIND_HINT_FUNC 902*4882a593Smuzhiyun cld 903*4882a593Smuzhiyun PUSH_AND_CLEAR_REGS save_ret=1 904*4882a593Smuzhiyun ENCODE_FRAME_POINTER 8 905*4882a593Smuzhiyun 906*4882a593Smuzhiyun /* 907*4882a593Smuzhiyun * Always stash CR3 in %r14. This value will be restored, 908*4882a593Smuzhiyun * verbatim, at exit. Needed if paranoid_entry interrupted 909*4882a593Smuzhiyun * another entry that already switched to the user CR3 value 910*4882a593Smuzhiyun * but has not yet returned to userspace. 911*4882a593Smuzhiyun * 912*4882a593Smuzhiyun * This is also why CS (stashed in the "iret frame" by the 913*4882a593Smuzhiyun * hardware at entry) can not be used: this may be a return 914*4882a593Smuzhiyun * to kernel code, but with a user CR3 value. 915*4882a593Smuzhiyun * 916*4882a593Smuzhiyun * Switching CR3 does not depend on kernel GSBASE so it can 917*4882a593Smuzhiyun * be done before switching to the kernel GSBASE. This is 918*4882a593Smuzhiyun * required for FSGSBASE because the kernel GSBASE has to 919*4882a593Smuzhiyun * be retrieved from a kernel internal table. 920*4882a593Smuzhiyun */ 921*4882a593Smuzhiyun SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14 922*4882a593Smuzhiyun 923*4882a593Smuzhiyun /* 924*4882a593Smuzhiyun * Handling GSBASE depends on the availability of FSGSBASE. 925*4882a593Smuzhiyun * 926*4882a593Smuzhiyun * Without FSGSBASE the kernel enforces that negative GSBASE 927*4882a593Smuzhiyun * values indicate kernel GSBASE. With FSGSBASE no assumptions 928*4882a593Smuzhiyun * can be made about the GSBASE value when entering from user 929*4882a593Smuzhiyun * space. 930*4882a593Smuzhiyun */ 931*4882a593Smuzhiyun ALTERNATIVE "jmp .Lparanoid_entry_checkgs", "", X86_FEATURE_FSGSBASE 932*4882a593Smuzhiyun 933*4882a593Smuzhiyun /* 934*4882a593Smuzhiyun * Read the current GSBASE and store it in %rbx unconditionally, 935*4882a593Smuzhiyun * retrieve and set the current CPUs kernel GSBASE. The stored value 936*4882a593Smuzhiyun * has to be restored in paranoid_exit unconditionally. 937*4882a593Smuzhiyun * 938*4882a593Smuzhiyun * The unconditional write to GS base below ensures that no subsequent 939*4882a593Smuzhiyun * loads based on a mispredicted GS base can happen, therefore no LFENCE 940*4882a593Smuzhiyun * is needed here. 941*4882a593Smuzhiyun */ 942*4882a593Smuzhiyun SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx 943*4882a593Smuzhiyun jmp .Lparanoid_gsbase_done 944*4882a593Smuzhiyun 945*4882a593Smuzhiyun.Lparanoid_entry_checkgs: 946*4882a593Smuzhiyun /* EBX = 1 -> kernel GSBASE active, no restore required */ 947*4882a593Smuzhiyun movl $1, %ebx 948*4882a593Smuzhiyun 949*4882a593Smuzhiyun /* 950*4882a593Smuzhiyun * The kernel-enforced convention is a negative GSBASE indicates 951*4882a593Smuzhiyun * a kernel value. No SWAPGS needed on entry and exit. 952*4882a593Smuzhiyun */ 953*4882a593Smuzhiyun movl $MSR_GS_BASE, %ecx 954*4882a593Smuzhiyun rdmsr 955*4882a593Smuzhiyun testl %edx, %edx 956*4882a593Smuzhiyun js .Lparanoid_kernel_gsbase 957*4882a593Smuzhiyun 958*4882a593Smuzhiyun /* EBX = 0 -> SWAPGS required on exit */ 959*4882a593Smuzhiyun xorl %ebx, %ebx 960*4882a593Smuzhiyun swapgs 961*4882a593Smuzhiyun.Lparanoid_kernel_gsbase: 962*4882a593Smuzhiyun FENCE_SWAPGS_KERNEL_ENTRY 963*4882a593Smuzhiyun.Lparanoid_gsbase_done: 964*4882a593Smuzhiyun 965*4882a593Smuzhiyun /* 966*4882a593Smuzhiyun * Once we have CR3 and %GS setup save and set SPEC_CTRL. Just like 967*4882a593Smuzhiyun * CR3 above, keep the old value in a callee saved register. 968*4882a593Smuzhiyun */ 969*4882a593Smuzhiyun IBRS_ENTER save_reg=%r15 970*4882a593Smuzhiyun UNTRAIN_RET 971*4882a593Smuzhiyun 972*4882a593Smuzhiyun RET 973*4882a593SmuzhiyunSYM_CODE_END(paranoid_entry) 974*4882a593Smuzhiyun 975*4882a593Smuzhiyun/* 976*4882a593Smuzhiyun * "Paranoid" exit path from exception stack. This is invoked 977*4882a593Smuzhiyun * only on return from non-NMI IST interrupts that came 978*4882a593Smuzhiyun * from kernel space. 979*4882a593Smuzhiyun * 980*4882a593Smuzhiyun * We may be returning to very strange contexts (e.g. very early 981*4882a593Smuzhiyun * in syscall entry), so checking for preemption here would 982*4882a593Smuzhiyun * be complicated. Fortunately, there's no good reason to try 983*4882a593Smuzhiyun * to handle preemption here. 984*4882a593Smuzhiyun * 985*4882a593Smuzhiyun * R/EBX contains the GSBASE related information depending on the 986*4882a593Smuzhiyun * availability of the FSGSBASE instructions: 987*4882a593Smuzhiyun * 988*4882a593Smuzhiyun * FSGSBASE R/EBX 989*4882a593Smuzhiyun * N 0 -> SWAPGS on exit 990*4882a593Smuzhiyun * 1 -> no SWAPGS on exit 991*4882a593Smuzhiyun * 992*4882a593Smuzhiyun * Y User space GSBASE, must be restored unconditionally 993*4882a593Smuzhiyun * 994*4882a593Smuzhiyun * R14 - old CR3 995*4882a593Smuzhiyun * R15 - old SPEC_CTRL 996*4882a593Smuzhiyun */ 997*4882a593SmuzhiyunSYM_CODE_START_LOCAL(paranoid_exit) 998*4882a593Smuzhiyun UNWIND_HINT_REGS 999*4882a593Smuzhiyun 1000*4882a593Smuzhiyun /* 1001*4882a593Smuzhiyun * Must restore IBRS state before both CR3 and %GS since we need access 1002*4882a593Smuzhiyun * to the per-CPU x86_spec_ctrl_shadow variable. 1003*4882a593Smuzhiyun */ 1004*4882a593Smuzhiyun IBRS_EXIT save_reg=%r15 1005*4882a593Smuzhiyun 1006*4882a593Smuzhiyun /* 1007*4882a593Smuzhiyun * The order of operations is important. RESTORE_CR3 requires 1008*4882a593Smuzhiyun * kernel GSBASE. 1009*4882a593Smuzhiyun * 1010*4882a593Smuzhiyun * NB to anyone to try to optimize this code: this code does 1011*4882a593Smuzhiyun * not execute at all for exceptions from user mode. Those 1012*4882a593Smuzhiyun * exceptions go through error_exit instead. 1013*4882a593Smuzhiyun */ 1014*4882a593Smuzhiyun RESTORE_CR3 scratch_reg=%rax save_reg=%r14 1015*4882a593Smuzhiyun 1016*4882a593Smuzhiyun /* Handle the three GSBASE cases */ 1017*4882a593Smuzhiyun ALTERNATIVE "jmp .Lparanoid_exit_checkgs", "", X86_FEATURE_FSGSBASE 1018*4882a593Smuzhiyun 1019*4882a593Smuzhiyun /* With FSGSBASE enabled, unconditionally restore GSBASE */ 1020*4882a593Smuzhiyun wrgsbase %rbx 1021*4882a593Smuzhiyun jmp restore_regs_and_return_to_kernel 1022*4882a593Smuzhiyun 1023*4882a593Smuzhiyun.Lparanoid_exit_checkgs: 1024*4882a593Smuzhiyun /* On non-FSGSBASE systems, conditionally do SWAPGS */ 1025*4882a593Smuzhiyun testl %ebx, %ebx 1026*4882a593Smuzhiyun jnz restore_regs_and_return_to_kernel 1027*4882a593Smuzhiyun 1028*4882a593Smuzhiyun /* We are returning to a context with user GSBASE */ 1029*4882a593Smuzhiyun swapgs 1030*4882a593Smuzhiyun jmp restore_regs_and_return_to_kernel 1031*4882a593SmuzhiyunSYM_CODE_END(paranoid_exit) 1032*4882a593Smuzhiyun 1033*4882a593Smuzhiyun/* 1034*4882a593Smuzhiyun * Save all registers in pt_regs, and switch GS if needed. 1035*4882a593Smuzhiyun */ 1036*4882a593SmuzhiyunSYM_CODE_START_LOCAL(error_entry) 1037*4882a593Smuzhiyun UNWIND_HINT_FUNC 1038*4882a593Smuzhiyun cld 1039*4882a593Smuzhiyun PUSH_AND_CLEAR_REGS save_ret=1 1040*4882a593Smuzhiyun ENCODE_FRAME_POINTER 8 1041*4882a593Smuzhiyun testb $3, CS+8(%rsp) 1042*4882a593Smuzhiyun jz .Lerror_kernelspace 1043*4882a593Smuzhiyun 1044*4882a593Smuzhiyun /* 1045*4882a593Smuzhiyun * We entered from user mode or we're pretending to have entered 1046*4882a593Smuzhiyun * from user mode due to an IRET fault. 1047*4882a593Smuzhiyun */ 1048*4882a593Smuzhiyun SWAPGS 1049*4882a593Smuzhiyun FENCE_SWAPGS_USER_ENTRY 1050*4882a593Smuzhiyun /* We have user CR3. Change to kernel CR3. */ 1051*4882a593Smuzhiyun SWITCH_TO_KERNEL_CR3 scratch_reg=%rax 1052*4882a593Smuzhiyun IBRS_ENTER 1053*4882a593Smuzhiyun UNTRAIN_RET 1054*4882a593Smuzhiyun 1055*4882a593Smuzhiyun.Lerror_entry_from_usermode_after_swapgs: 1056*4882a593Smuzhiyun 1057*4882a593Smuzhiyun /* Put us onto the real thread stack. */ 1058*4882a593Smuzhiyun popq %r12 /* save return addr in %12 */ 1059*4882a593Smuzhiyun movq %rsp, %rdi /* arg0 = pt_regs pointer */ 1060*4882a593Smuzhiyun call sync_regs 1061*4882a593Smuzhiyun movq %rax, %rsp /* switch stack */ 1062*4882a593Smuzhiyun ENCODE_FRAME_POINTER 1063*4882a593Smuzhiyun pushq %r12 1064*4882a593Smuzhiyun RET 1065*4882a593Smuzhiyun 1066*4882a593Smuzhiyun /* 1067*4882a593Smuzhiyun * There are two places in the kernel that can potentially fault with 1068*4882a593Smuzhiyun * usergs. Handle them here. B stepping K8s sometimes report a 1069*4882a593Smuzhiyun * truncated RIP for IRET exceptions returning to compat mode. Check 1070*4882a593Smuzhiyun * for these here too. 1071*4882a593Smuzhiyun */ 1072*4882a593Smuzhiyun.Lerror_kernelspace: 1073*4882a593Smuzhiyun leaq native_irq_return_iret(%rip), %rcx 1074*4882a593Smuzhiyun cmpq %rcx, RIP+8(%rsp) 1075*4882a593Smuzhiyun je .Lerror_bad_iret 1076*4882a593Smuzhiyun movl %ecx, %eax /* zero extend */ 1077*4882a593Smuzhiyun cmpq %rax, RIP+8(%rsp) 1078*4882a593Smuzhiyun je .Lbstep_iret 1079*4882a593Smuzhiyun cmpq $.Lgs_change, RIP+8(%rsp) 1080*4882a593Smuzhiyun jne .Lerror_entry_done_lfence 1081*4882a593Smuzhiyun 1082*4882a593Smuzhiyun /* 1083*4882a593Smuzhiyun * hack: .Lgs_change can fail with user gsbase. If this happens, fix up 1084*4882a593Smuzhiyun * gsbase and proceed. We'll fix up the exception and land in 1085*4882a593Smuzhiyun * .Lgs_change's error handler with kernel gsbase. 1086*4882a593Smuzhiyun */ 1087*4882a593Smuzhiyun SWAPGS 1088*4882a593Smuzhiyun 1089*4882a593Smuzhiyun /* 1090*4882a593Smuzhiyun * Issue an LFENCE to prevent GS speculation, regardless of whether it is a 1091*4882a593Smuzhiyun * kernel or user gsbase. 1092*4882a593Smuzhiyun */ 1093*4882a593Smuzhiyun.Lerror_entry_done_lfence: 1094*4882a593Smuzhiyun FENCE_SWAPGS_KERNEL_ENTRY 1095*4882a593Smuzhiyun ANNOTATE_UNRET_END 1096*4882a593Smuzhiyun RET 1097*4882a593Smuzhiyun 1098*4882a593Smuzhiyun.Lbstep_iret: 1099*4882a593Smuzhiyun /* Fix truncated RIP */ 1100*4882a593Smuzhiyun movq %rcx, RIP+8(%rsp) 1101*4882a593Smuzhiyun /* fall through */ 1102*4882a593Smuzhiyun 1103*4882a593Smuzhiyun.Lerror_bad_iret: 1104*4882a593Smuzhiyun /* 1105*4882a593Smuzhiyun * We came from an IRET to user mode, so we have user 1106*4882a593Smuzhiyun * gsbase and CR3. Switch to kernel gsbase and CR3: 1107*4882a593Smuzhiyun */ 1108*4882a593Smuzhiyun SWAPGS 1109*4882a593Smuzhiyun FENCE_SWAPGS_USER_ENTRY 1110*4882a593Smuzhiyun SWITCH_TO_KERNEL_CR3 scratch_reg=%rax 1111*4882a593Smuzhiyun IBRS_ENTER 1112*4882a593Smuzhiyun UNTRAIN_RET 1113*4882a593Smuzhiyun 1114*4882a593Smuzhiyun /* 1115*4882a593Smuzhiyun * Pretend that the exception came from user mode: set up pt_regs 1116*4882a593Smuzhiyun * as if we faulted immediately after IRET. 1117*4882a593Smuzhiyun */ 1118*4882a593Smuzhiyun mov %rsp, %rdi 1119*4882a593Smuzhiyun call fixup_bad_iret 1120*4882a593Smuzhiyun mov %rax, %rsp 1121*4882a593Smuzhiyun jmp .Lerror_entry_from_usermode_after_swapgs 1122*4882a593SmuzhiyunSYM_CODE_END(error_entry) 1123*4882a593Smuzhiyun 1124*4882a593SmuzhiyunSYM_CODE_START_LOCAL(error_return) 1125*4882a593Smuzhiyun UNWIND_HINT_REGS 1126*4882a593Smuzhiyun DEBUG_ENTRY_ASSERT_IRQS_OFF 1127*4882a593Smuzhiyun testb $3, CS(%rsp) 1128*4882a593Smuzhiyun jz restore_regs_and_return_to_kernel 1129*4882a593Smuzhiyun jmp swapgs_restore_regs_and_return_to_usermode 1130*4882a593SmuzhiyunSYM_CODE_END(error_return) 1131*4882a593Smuzhiyun 1132*4882a593Smuzhiyun/* 1133*4882a593Smuzhiyun * Runs on exception stack. Xen PV does not go through this path at all, 1134*4882a593Smuzhiyun * so we can use real assembly here. 1135*4882a593Smuzhiyun * 1136*4882a593Smuzhiyun * Registers: 1137*4882a593Smuzhiyun * %r14: Used to save/restore the CR3 of the interrupted context 1138*4882a593Smuzhiyun * when PAGE_TABLE_ISOLATION is in use. Do not clobber. 1139*4882a593Smuzhiyun */ 1140*4882a593SmuzhiyunSYM_CODE_START(asm_exc_nmi) 1141*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS 1142*4882a593Smuzhiyun 1143*4882a593Smuzhiyun /* 1144*4882a593Smuzhiyun * We allow breakpoints in NMIs. If a breakpoint occurs, then 1145*4882a593Smuzhiyun * the iretq it performs will take us out of NMI context. 1146*4882a593Smuzhiyun * This means that we can have nested NMIs where the next 1147*4882a593Smuzhiyun * NMI is using the top of the stack of the previous NMI. We 1148*4882a593Smuzhiyun * can't let it execute because the nested NMI will corrupt the 1149*4882a593Smuzhiyun * stack of the previous NMI. NMI handlers are not re-entrant 1150*4882a593Smuzhiyun * anyway. 1151*4882a593Smuzhiyun * 1152*4882a593Smuzhiyun * To handle this case we do the following: 1153*4882a593Smuzhiyun * Check the a special location on the stack that contains 1154*4882a593Smuzhiyun * a variable that is set when NMIs are executing. 1155*4882a593Smuzhiyun * The interrupted task's stack is also checked to see if it 1156*4882a593Smuzhiyun * is an NMI stack. 1157*4882a593Smuzhiyun * If the variable is not set and the stack is not the NMI 1158*4882a593Smuzhiyun * stack then: 1159*4882a593Smuzhiyun * o Set the special variable on the stack 1160*4882a593Smuzhiyun * o Copy the interrupt frame into an "outermost" location on the 1161*4882a593Smuzhiyun * stack 1162*4882a593Smuzhiyun * o Copy the interrupt frame into an "iret" location on the stack 1163*4882a593Smuzhiyun * o Continue processing the NMI 1164*4882a593Smuzhiyun * If the variable is set or the previous stack is the NMI stack: 1165*4882a593Smuzhiyun * o Modify the "iret" location to jump to the repeat_nmi 1166*4882a593Smuzhiyun * o return back to the first NMI 1167*4882a593Smuzhiyun * 1168*4882a593Smuzhiyun * Now on exit of the first NMI, we first clear the stack variable 1169*4882a593Smuzhiyun * The NMI stack will tell any nested NMIs at that point that it is 1170*4882a593Smuzhiyun * nested. Then we pop the stack normally with iret, and if there was 1171*4882a593Smuzhiyun * a nested NMI that updated the copy interrupt stack frame, a 1172*4882a593Smuzhiyun * jump will be made to the repeat_nmi code that will handle the second 1173*4882a593Smuzhiyun * NMI. 1174*4882a593Smuzhiyun * 1175*4882a593Smuzhiyun * However, espfix prevents us from directly returning to userspace 1176*4882a593Smuzhiyun * with a single IRET instruction. Similarly, IRET to user mode 1177*4882a593Smuzhiyun * can fault. We therefore handle NMIs from user space like 1178*4882a593Smuzhiyun * other IST entries. 1179*4882a593Smuzhiyun */ 1180*4882a593Smuzhiyun 1181*4882a593Smuzhiyun ASM_CLAC 1182*4882a593Smuzhiyun 1183*4882a593Smuzhiyun /* Use %rdx as our temp variable throughout */ 1184*4882a593Smuzhiyun pushq %rdx 1185*4882a593Smuzhiyun 1186*4882a593Smuzhiyun testb $3, CS-RIP+8(%rsp) 1187*4882a593Smuzhiyun jz .Lnmi_from_kernel 1188*4882a593Smuzhiyun 1189*4882a593Smuzhiyun /* 1190*4882a593Smuzhiyun * NMI from user mode. We need to run on the thread stack, but we 1191*4882a593Smuzhiyun * can't go through the normal entry paths: NMIs are masked, and 1192*4882a593Smuzhiyun * we don't want to enable interrupts, because then we'll end 1193*4882a593Smuzhiyun * up in an awkward situation in which IRQs are on but NMIs 1194*4882a593Smuzhiyun * are off. 1195*4882a593Smuzhiyun * 1196*4882a593Smuzhiyun * We also must not push anything to the stack before switching 1197*4882a593Smuzhiyun * stacks lest we corrupt the "NMI executing" variable. 1198*4882a593Smuzhiyun */ 1199*4882a593Smuzhiyun 1200*4882a593Smuzhiyun swapgs 1201*4882a593Smuzhiyun cld 1202*4882a593Smuzhiyun FENCE_SWAPGS_USER_ENTRY 1203*4882a593Smuzhiyun SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx 1204*4882a593Smuzhiyun movq %rsp, %rdx 1205*4882a593Smuzhiyun movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 1206*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS base=%rdx offset=8 1207*4882a593Smuzhiyun pushq 5*8(%rdx) /* pt_regs->ss */ 1208*4882a593Smuzhiyun pushq 4*8(%rdx) /* pt_regs->rsp */ 1209*4882a593Smuzhiyun pushq 3*8(%rdx) /* pt_regs->flags */ 1210*4882a593Smuzhiyun pushq 2*8(%rdx) /* pt_regs->cs */ 1211*4882a593Smuzhiyun pushq 1*8(%rdx) /* pt_regs->rip */ 1212*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS 1213*4882a593Smuzhiyun pushq $-1 /* pt_regs->orig_ax */ 1214*4882a593Smuzhiyun PUSH_AND_CLEAR_REGS rdx=(%rdx) 1215*4882a593Smuzhiyun ENCODE_FRAME_POINTER 1216*4882a593Smuzhiyun 1217*4882a593Smuzhiyun IBRS_ENTER 1218*4882a593Smuzhiyun UNTRAIN_RET 1219*4882a593Smuzhiyun 1220*4882a593Smuzhiyun /* 1221*4882a593Smuzhiyun * At this point we no longer need to worry about stack damage 1222*4882a593Smuzhiyun * due to nesting -- we're on the normal thread stack and we're 1223*4882a593Smuzhiyun * done with the NMI stack. 1224*4882a593Smuzhiyun */ 1225*4882a593Smuzhiyun 1226*4882a593Smuzhiyun movq %rsp, %rdi 1227*4882a593Smuzhiyun movq $-1, %rsi 1228*4882a593Smuzhiyun call exc_nmi 1229*4882a593Smuzhiyun 1230*4882a593Smuzhiyun /* 1231*4882a593Smuzhiyun * Return back to user mode. We must *not* do the normal exit 1232*4882a593Smuzhiyun * work, because we don't want to enable interrupts. 1233*4882a593Smuzhiyun */ 1234*4882a593Smuzhiyun jmp swapgs_restore_regs_and_return_to_usermode 1235*4882a593Smuzhiyun 1236*4882a593Smuzhiyun.Lnmi_from_kernel: 1237*4882a593Smuzhiyun /* 1238*4882a593Smuzhiyun * Here's what our stack frame will look like: 1239*4882a593Smuzhiyun * +---------------------------------------------------------+ 1240*4882a593Smuzhiyun * | original SS | 1241*4882a593Smuzhiyun * | original Return RSP | 1242*4882a593Smuzhiyun * | original RFLAGS | 1243*4882a593Smuzhiyun * | original CS | 1244*4882a593Smuzhiyun * | original RIP | 1245*4882a593Smuzhiyun * +---------------------------------------------------------+ 1246*4882a593Smuzhiyun * | temp storage for rdx | 1247*4882a593Smuzhiyun * +---------------------------------------------------------+ 1248*4882a593Smuzhiyun * | "NMI executing" variable | 1249*4882a593Smuzhiyun * +---------------------------------------------------------+ 1250*4882a593Smuzhiyun * | iret SS } Copied from "outermost" frame | 1251*4882a593Smuzhiyun * | iret Return RSP } on each loop iteration; overwritten | 1252*4882a593Smuzhiyun * | iret RFLAGS } by a nested NMI to force another | 1253*4882a593Smuzhiyun * | iret CS } iteration if needed. | 1254*4882a593Smuzhiyun * | iret RIP } | 1255*4882a593Smuzhiyun * +---------------------------------------------------------+ 1256*4882a593Smuzhiyun * | outermost SS } initialized in first_nmi; | 1257*4882a593Smuzhiyun * | outermost Return RSP } will not be changed before | 1258*4882a593Smuzhiyun * | outermost RFLAGS } NMI processing is done. | 1259*4882a593Smuzhiyun * | outermost CS } Copied to "iret" frame on each | 1260*4882a593Smuzhiyun * | outermost RIP } iteration. | 1261*4882a593Smuzhiyun * +---------------------------------------------------------+ 1262*4882a593Smuzhiyun * | pt_regs | 1263*4882a593Smuzhiyun * +---------------------------------------------------------+ 1264*4882a593Smuzhiyun * 1265*4882a593Smuzhiyun * The "original" frame is used by hardware. Before re-enabling 1266*4882a593Smuzhiyun * NMIs, we need to be done with it, and we need to leave enough 1267*4882a593Smuzhiyun * space for the asm code here. 1268*4882a593Smuzhiyun * 1269*4882a593Smuzhiyun * We return by executing IRET while RSP points to the "iret" frame. 1270*4882a593Smuzhiyun * That will either return for real or it will loop back into NMI 1271*4882a593Smuzhiyun * processing. 1272*4882a593Smuzhiyun * 1273*4882a593Smuzhiyun * The "outermost" frame is copied to the "iret" frame on each 1274*4882a593Smuzhiyun * iteration of the loop, so each iteration starts with the "iret" 1275*4882a593Smuzhiyun * frame pointing to the final return target. 1276*4882a593Smuzhiyun */ 1277*4882a593Smuzhiyun 1278*4882a593Smuzhiyun /* 1279*4882a593Smuzhiyun * Determine whether we're a nested NMI. 1280*4882a593Smuzhiyun * 1281*4882a593Smuzhiyun * If we interrupted kernel code between repeat_nmi and 1282*4882a593Smuzhiyun * end_repeat_nmi, then we are a nested NMI. We must not 1283*4882a593Smuzhiyun * modify the "iret" frame because it's being written by 1284*4882a593Smuzhiyun * the outer NMI. That's okay; the outer NMI handler is 1285*4882a593Smuzhiyun * about to about to call exc_nmi() anyway, so we can just 1286*4882a593Smuzhiyun * resume the outer NMI. 1287*4882a593Smuzhiyun */ 1288*4882a593Smuzhiyun 1289*4882a593Smuzhiyun movq $repeat_nmi, %rdx 1290*4882a593Smuzhiyun cmpq 8(%rsp), %rdx 1291*4882a593Smuzhiyun ja 1f 1292*4882a593Smuzhiyun movq $end_repeat_nmi, %rdx 1293*4882a593Smuzhiyun cmpq 8(%rsp), %rdx 1294*4882a593Smuzhiyun ja nested_nmi_out 1295*4882a593Smuzhiyun1: 1296*4882a593Smuzhiyun 1297*4882a593Smuzhiyun /* 1298*4882a593Smuzhiyun * Now check "NMI executing". If it's set, then we're nested. 1299*4882a593Smuzhiyun * This will not detect if we interrupted an outer NMI just 1300*4882a593Smuzhiyun * before IRET. 1301*4882a593Smuzhiyun */ 1302*4882a593Smuzhiyun cmpl $1, -8(%rsp) 1303*4882a593Smuzhiyun je nested_nmi 1304*4882a593Smuzhiyun 1305*4882a593Smuzhiyun /* 1306*4882a593Smuzhiyun * Now test if the previous stack was an NMI stack. This covers 1307*4882a593Smuzhiyun * the case where we interrupt an outer NMI after it clears 1308*4882a593Smuzhiyun * "NMI executing" but before IRET. We need to be careful, though: 1309*4882a593Smuzhiyun * there is one case in which RSP could point to the NMI stack 1310*4882a593Smuzhiyun * despite there being no NMI active: naughty userspace controls 1311*4882a593Smuzhiyun * RSP at the very beginning of the SYSCALL targets. We can 1312*4882a593Smuzhiyun * pull a fast one on naughty userspace, though: we program 1313*4882a593Smuzhiyun * SYSCALL to mask DF, so userspace cannot cause DF to be set 1314*4882a593Smuzhiyun * if it controls the kernel's RSP. We set DF before we clear 1315*4882a593Smuzhiyun * "NMI executing". 1316*4882a593Smuzhiyun */ 1317*4882a593Smuzhiyun lea 6*8(%rsp), %rdx 1318*4882a593Smuzhiyun /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */ 1319*4882a593Smuzhiyun cmpq %rdx, 4*8(%rsp) 1320*4882a593Smuzhiyun /* If the stack pointer is above the NMI stack, this is a normal NMI */ 1321*4882a593Smuzhiyun ja first_nmi 1322*4882a593Smuzhiyun 1323*4882a593Smuzhiyun subq $EXCEPTION_STKSZ, %rdx 1324*4882a593Smuzhiyun cmpq %rdx, 4*8(%rsp) 1325*4882a593Smuzhiyun /* If it is below the NMI stack, it is a normal NMI */ 1326*4882a593Smuzhiyun jb first_nmi 1327*4882a593Smuzhiyun 1328*4882a593Smuzhiyun /* Ah, it is within the NMI stack. */ 1329*4882a593Smuzhiyun 1330*4882a593Smuzhiyun testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp) 1331*4882a593Smuzhiyun jz first_nmi /* RSP was user controlled. */ 1332*4882a593Smuzhiyun 1333*4882a593Smuzhiyun /* This is a nested NMI. */ 1334*4882a593Smuzhiyun 1335*4882a593Smuzhiyunnested_nmi: 1336*4882a593Smuzhiyun /* 1337*4882a593Smuzhiyun * Modify the "iret" frame to point to repeat_nmi, forcing another 1338*4882a593Smuzhiyun * iteration of NMI handling. 1339*4882a593Smuzhiyun */ 1340*4882a593Smuzhiyun subq $8, %rsp 1341*4882a593Smuzhiyun leaq -10*8(%rsp), %rdx 1342*4882a593Smuzhiyun pushq $__KERNEL_DS 1343*4882a593Smuzhiyun pushq %rdx 1344*4882a593Smuzhiyun pushfq 1345*4882a593Smuzhiyun pushq $__KERNEL_CS 1346*4882a593Smuzhiyun pushq $repeat_nmi 1347*4882a593Smuzhiyun 1348*4882a593Smuzhiyun /* Put stack back */ 1349*4882a593Smuzhiyun addq $(6*8), %rsp 1350*4882a593Smuzhiyun 1351*4882a593Smuzhiyunnested_nmi_out: 1352*4882a593Smuzhiyun popq %rdx 1353*4882a593Smuzhiyun 1354*4882a593Smuzhiyun /* We are returning to kernel mode, so this cannot result in a fault. */ 1355*4882a593Smuzhiyun iretq 1356*4882a593Smuzhiyun 1357*4882a593Smuzhiyunfirst_nmi: 1358*4882a593Smuzhiyun /* Restore rdx. */ 1359*4882a593Smuzhiyun movq (%rsp), %rdx 1360*4882a593Smuzhiyun 1361*4882a593Smuzhiyun /* Make room for "NMI executing". */ 1362*4882a593Smuzhiyun pushq $0 1363*4882a593Smuzhiyun 1364*4882a593Smuzhiyun /* Leave room for the "iret" frame */ 1365*4882a593Smuzhiyun subq $(5*8), %rsp 1366*4882a593Smuzhiyun 1367*4882a593Smuzhiyun /* Copy the "original" frame to the "outermost" frame */ 1368*4882a593Smuzhiyun .rept 5 1369*4882a593Smuzhiyun pushq 11*8(%rsp) 1370*4882a593Smuzhiyun .endr 1371*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS 1372*4882a593Smuzhiyun 1373*4882a593Smuzhiyun /* Everything up to here is safe from nested NMIs */ 1374*4882a593Smuzhiyun 1375*4882a593Smuzhiyun#ifdef CONFIG_DEBUG_ENTRY 1376*4882a593Smuzhiyun /* 1377*4882a593Smuzhiyun * For ease of testing, unmask NMIs right away. Disabled by 1378*4882a593Smuzhiyun * default because IRET is very expensive. 1379*4882a593Smuzhiyun */ 1380*4882a593Smuzhiyun pushq $0 /* SS */ 1381*4882a593Smuzhiyun pushq %rsp /* RSP (minus 8 because of the previous push) */ 1382*4882a593Smuzhiyun addq $8, (%rsp) /* Fix up RSP */ 1383*4882a593Smuzhiyun pushfq /* RFLAGS */ 1384*4882a593Smuzhiyun pushq $__KERNEL_CS /* CS */ 1385*4882a593Smuzhiyun pushq $1f /* RIP */ 1386*4882a593Smuzhiyun iretq /* continues at repeat_nmi below */ 1387*4882a593Smuzhiyun UNWIND_HINT_IRET_REGS 1388*4882a593Smuzhiyun1: 1389*4882a593Smuzhiyun#endif 1390*4882a593Smuzhiyun 1391*4882a593Smuzhiyunrepeat_nmi: 1392*4882a593Smuzhiyun /* 1393*4882a593Smuzhiyun * If there was a nested NMI, the first NMI's iret will return 1394*4882a593Smuzhiyun * here. But NMIs are still enabled and we can take another 1395*4882a593Smuzhiyun * nested NMI. The nested NMI checks the interrupted RIP to see 1396*4882a593Smuzhiyun * if it is between repeat_nmi and end_repeat_nmi, and if so 1397*4882a593Smuzhiyun * it will just return, as we are about to repeat an NMI anyway. 1398*4882a593Smuzhiyun * This makes it safe to copy to the stack frame that a nested 1399*4882a593Smuzhiyun * NMI will update. 1400*4882a593Smuzhiyun * 1401*4882a593Smuzhiyun * RSP is pointing to "outermost RIP". gsbase is unknown, but, if 1402*4882a593Smuzhiyun * we're repeating an NMI, gsbase has the same value that it had on 1403*4882a593Smuzhiyun * the first iteration. paranoid_entry will load the kernel 1404*4882a593Smuzhiyun * gsbase if needed before we call exc_nmi(). "NMI executing" 1405*4882a593Smuzhiyun * is zero. 1406*4882a593Smuzhiyun */ 1407*4882a593Smuzhiyun movq $1, 10*8(%rsp) /* Set "NMI executing". */ 1408*4882a593Smuzhiyun 1409*4882a593Smuzhiyun /* 1410*4882a593Smuzhiyun * Copy the "outermost" frame to the "iret" frame. NMIs that nest 1411*4882a593Smuzhiyun * here must not modify the "iret" frame while we're writing to 1412*4882a593Smuzhiyun * it or it will end up containing garbage. 1413*4882a593Smuzhiyun */ 1414*4882a593Smuzhiyun addq $(10*8), %rsp 1415*4882a593Smuzhiyun .rept 5 1416*4882a593Smuzhiyun pushq -6*8(%rsp) 1417*4882a593Smuzhiyun .endr 1418*4882a593Smuzhiyun subq $(5*8), %rsp 1419*4882a593Smuzhiyunend_repeat_nmi: 1420*4882a593Smuzhiyun 1421*4882a593Smuzhiyun /* 1422*4882a593Smuzhiyun * Everything below this point can be preempted by a nested NMI. 1423*4882a593Smuzhiyun * If this happens, then the inner NMI will change the "iret" 1424*4882a593Smuzhiyun * frame to point back to repeat_nmi. 1425*4882a593Smuzhiyun */ 1426*4882a593Smuzhiyun pushq $-1 /* ORIG_RAX: no syscall to restart */ 1427*4882a593Smuzhiyun 1428*4882a593Smuzhiyun /* 1429*4882a593Smuzhiyun * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit 1430*4882a593Smuzhiyun * as we should not be calling schedule in NMI context. 1431*4882a593Smuzhiyun * Even with normal interrupts enabled. An NMI should not be 1432*4882a593Smuzhiyun * setting NEED_RESCHED or anything that normal interrupts and 1433*4882a593Smuzhiyun * exceptions might do. 1434*4882a593Smuzhiyun */ 1435*4882a593Smuzhiyun call paranoid_entry 1436*4882a593Smuzhiyun UNWIND_HINT_REGS 1437*4882a593Smuzhiyun 1438*4882a593Smuzhiyun movq %rsp, %rdi 1439*4882a593Smuzhiyun movq $-1, %rsi 1440*4882a593Smuzhiyun call exc_nmi 1441*4882a593Smuzhiyun 1442*4882a593Smuzhiyun /* Always restore stashed SPEC_CTRL value (see paranoid_entry) */ 1443*4882a593Smuzhiyun IBRS_EXIT save_reg=%r15 1444*4882a593Smuzhiyun 1445*4882a593Smuzhiyun /* Always restore stashed CR3 value (see paranoid_entry) */ 1446*4882a593Smuzhiyun RESTORE_CR3 scratch_reg=%r15 save_reg=%r14 1447*4882a593Smuzhiyun 1448*4882a593Smuzhiyun /* 1449*4882a593Smuzhiyun * The above invocation of paranoid_entry stored the GSBASE 1450*4882a593Smuzhiyun * related information in R/EBX depending on the availability 1451*4882a593Smuzhiyun * of FSGSBASE. 1452*4882a593Smuzhiyun * 1453*4882a593Smuzhiyun * If FSGSBASE is enabled, restore the saved GSBASE value 1454*4882a593Smuzhiyun * unconditionally, otherwise take the conditional SWAPGS path. 1455*4882a593Smuzhiyun */ 1456*4882a593Smuzhiyun ALTERNATIVE "jmp nmi_no_fsgsbase", "", X86_FEATURE_FSGSBASE 1457*4882a593Smuzhiyun 1458*4882a593Smuzhiyun wrgsbase %rbx 1459*4882a593Smuzhiyun jmp nmi_restore 1460*4882a593Smuzhiyun 1461*4882a593Smuzhiyunnmi_no_fsgsbase: 1462*4882a593Smuzhiyun /* EBX == 0 -> invoke SWAPGS */ 1463*4882a593Smuzhiyun testl %ebx, %ebx 1464*4882a593Smuzhiyun jnz nmi_restore 1465*4882a593Smuzhiyun 1466*4882a593Smuzhiyunnmi_swapgs: 1467*4882a593Smuzhiyun swapgs 1468*4882a593Smuzhiyun 1469*4882a593Smuzhiyunnmi_restore: 1470*4882a593Smuzhiyun POP_REGS 1471*4882a593Smuzhiyun 1472*4882a593Smuzhiyun /* 1473*4882a593Smuzhiyun * Skip orig_ax and the "outermost" frame to point RSP at the "iret" 1474*4882a593Smuzhiyun * at the "iret" frame. 1475*4882a593Smuzhiyun */ 1476*4882a593Smuzhiyun addq $6*8, %rsp 1477*4882a593Smuzhiyun 1478*4882a593Smuzhiyun /* 1479*4882a593Smuzhiyun * Clear "NMI executing". Set DF first so that we can easily 1480*4882a593Smuzhiyun * distinguish the remaining code between here and IRET from 1481*4882a593Smuzhiyun * the SYSCALL entry and exit paths. 1482*4882a593Smuzhiyun * 1483*4882a593Smuzhiyun * We arguably should just inspect RIP instead, but I (Andy) wrote 1484*4882a593Smuzhiyun * this code when I had the misapprehension that Xen PV supported 1485*4882a593Smuzhiyun * NMIs, and Xen PV would break that approach. 1486*4882a593Smuzhiyun */ 1487*4882a593Smuzhiyun std 1488*4882a593Smuzhiyun movq $0, 5*8(%rsp) /* clear "NMI executing" */ 1489*4882a593Smuzhiyun 1490*4882a593Smuzhiyun /* 1491*4882a593Smuzhiyun * iretq reads the "iret" frame and exits the NMI stack in a 1492*4882a593Smuzhiyun * single instruction. We are returning to kernel mode, so this 1493*4882a593Smuzhiyun * cannot result in a fault. Similarly, we don't need to worry 1494*4882a593Smuzhiyun * about espfix64 on the way back to kernel mode. 1495*4882a593Smuzhiyun */ 1496*4882a593Smuzhiyun iretq 1497*4882a593SmuzhiyunSYM_CODE_END(asm_exc_nmi) 1498*4882a593Smuzhiyun 1499*4882a593Smuzhiyun#ifndef CONFIG_IA32_EMULATION 1500*4882a593Smuzhiyun/* 1501*4882a593Smuzhiyun * This handles SYSCALL from 32-bit code. There is no way to program 1502*4882a593Smuzhiyun * MSRs to fully disable 32-bit SYSCALL. 1503*4882a593Smuzhiyun */ 1504*4882a593SmuzhiyunSYM_CODE_START(ignore_sysret) 1505*4882a593Smuzhiyun UNWIND_HINT_EMPTY 1506*4882a593Smuzhiyun mov $-ENOSYS, %eax 1507*4882a593Smuzhiyun sysretl 1508*4882a593SmuzhiyunSYM_CODE_END(ignore_sysret) 1509*4882a593Smuzhiyun#endif 1510*4882a593Smuzhiyun 1511*4882a593Smuzhiyun.pushsection .text, "ax" 1512*4882a593SmuzhiyunSYM_CODE_START(rewind_stack_do_exit) 1513*4882a593Smuzhiyun UNWIND_HINT_FUNC 1514*4882a593Smuzhiyun /* Prevent any naive code from trying to unwind to our caller. */ 1515*4882a593Smuzhiyun xorl %ebp, %ebp 1516*4882a593Smuzhiyun 1517*4882a593Smuzhiyun movq PER_CPU_VAR(cpu_current_top_of_stack), %rax 1518*4882a593Smuzhiyun leaq -PTREGS_SIZE(%rax), %rsp 1519*4882a593Smuzhiyun UNWIND_HINT_REGS 1520*4882a593Smuzhiyun 1521*4882a593Smuzhiyun call do_exit 1522*4882a593SmuzhiyunSYM_CODE_END(rewind_stack_do_exit) 1523*4882a593Smuzhiyun.popsection 1524