1905a36a2SIngo Molnar/* 2905a36a2SIngo Molnar * linux/arch/x86_64/entry.S 3905a36a2SIngo Molnar * 4905a36a2SIngo Molnar * Copyright (C) 1991, 1992 Linus Torvalds 5905a36a2SIngo Molnar * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs 6905a36a2SIngo Molnar * Copyright (C) 2000 Pavel Machek <pavel@suse.cz> 74d732138SIngo Molnar * 8905a36a2SIngo Molnar * entry.S contains the system-call and fault low-level handling routines. 9905a36a2SIngo Molnar * 10905a36a2SIngo Molnar * Some of this is documented in Documentation/x86/entry_64.txt 11905a36a2SIngo Molnar * 12905a36a2SIngo Molnar * A note on terminology: 13905a36a2SIngo Molnar * - iret frame: Architecture defined interrupt frame from SS to RIP 14905a36a2SIngo Molnar * at the top of the kernel process stack. 15905a36a2SIngo Molnar * 16905a36a2SIngo Molnar * Some macro usage: 174d732138SIngo Molnar * - ENTRY/END: Define functions in the symbol table. 184d732138SIngo Molnar * - TRACE_IRQ_*: Trace hardirq state for lock debugging. 194d732138SIngo Molnar * - idtentry: Define exception entry points. 20905a36a2SIngo Molnar */ 21905a36a2SIngo Molnar#include <linux/linkage.h> 22905a36a2SIngo Molnar#include <asm/segment.h> 23905a36a2SIngo Molnar#include <asm/cache.h> 24905a36a2SIngo Molnar#include <asm/errno.h> 25d36f9479SIngo Molnar#include "calling.h" 26905a36a2SIngo Molnar#include <asm/asm-offsets.h> 27905a36a2SIngo Molnar#include <asm/msr.h> 28905a36a2SIngo Molnar#include <asm/unistd.h> 29905a36a2SIngo Molnar#include <asm/thread_info.h> 30905a36a2SIngo Molnar#include <asm/hw_irq.h> 31905a36a2SIngo Molnar#include <asm/page_types.h> 32905a36a2SIngo Molnar#include <asm/irqflags.h> 33905a36a2SIngo Molnar#include <asm/paravirt.h> 34905a36a2SIngo Molnar#include <asm/percpu.h> 35905a36a2SIngo Molnar#include <asm/asm.h> 36905a36a2SIngo Molnar#include <asm/context_tracking.h> 37905a36a2SIngo Molnar#include <asm/smap.h> 38905a36a2SIngo Molnar#include <asm/pgtable_types.h> 39905a36a2SIngo Molnar#include <linux/err.h> 40905a36a2SIngo Molnar 41905a36a2SIngo Molnar/* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */ 42905a36a2SIngo Molnar#include <linux/elf-em.h> 43905a36a2SIngo Molnar#define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE) 44905a36a2SIngo Molnar#define __AUDIT_ARCH_64BIT 0x80000000 45905a36a2SIngo Molnar#define __AUDIT_ARCH_LE 0x40000000 46905a36a2SIngo Molnar 47905a36a2SIngo Molnar.code64 48905a36a2SIngo Molnar.section .entry.text, "ax" 49905a36a2SIngo Molnar 50905a36a2SIngo Molnar#ifdef CONFIG_PARAVIRT 51905a36a2SIngo MolnarENTRY(native_usergs_sysret64) 52905a36a2SIngo Molnar swapgs 53905a36a2SIngo Molnar sysretq 54905a36a2SIngo MolnarENDPROC(native_usergs_sysret64) 55905a36a2SIngo Molnar#endif /* CONFIG_PARAVIRT */ 56905a36a2SIngo Molnar 57905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ 58905a36a2SIngo Molnar#ifdef CONFIG_TRACE_IRQFLAGS 59905a36a2SIngo Molnar bt $9, EFLAGS(%rsp) /* interrupts off? */ 60905a36a2SIngo Molnar jnc 1f 61905a36a2SIngo Molnar TRACE_IRQS_ON 62905a36a2SIngo Molnar1: 63905a36a2SIngo Molnar#endif 64905a36a2SIngo Molnar.endm 65905a36a2SIngo Molnar 66905a36a2SIngo Molnar/* 67905a36a2SIngo Molnar * When dynamic function tracer is enabled it will add a breakpoint 68905a36a2SIngo Molnar * to all locations that it is about to modify, sync CPUs, update 69905a36a2SIngo Molnar * all the code, sync CPUs, then remove the breakpoints. In this time 70905a36a2SIngo Molnar * if lockdep is enabled, it might jump back into the debug handler 71905a36a2SIngo Molnar * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF). 72905a36a2SIngo Molnar * 73905a36a2SIngo Molnar * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to 74905a36a2SIngo Molnar * make sure the stack pointer does not get reset back to the top 75905a36a2SIngo Molnar * of the debug stack, and instead just reuses the current stack. 76905a36a2SIngo Molnar */ 77905a36a2SIngo Molnar#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS) 78905a36a2SIngo Molnar 79905a36a2SIngo Molnar.macro TRACE_IRQS_OFF_DEBUG 80905a36a2SIngo Molnar call debug_stack_set_zero 81905a36a2SIngo Molnar TRACE_IRQS_OFF 82905a36a2SIngo Molnar call debug_stack_reset 83905a36a2SIngo Molnar.endm 84905a36a2SIngo Molnar 85905a36a2SIngo Molnar.macro TRACE_IRQS_ON_DEBUG 86905a36a2SIngo Molnar call debug_stack_set_zero 87905a36a2SIngo Molnar TRACE_IRQS_ON 88905a36a2SIngo Molnar call debug_stack_reset 89905a36a2SIngo Molnar.endm 90905a36a2SIngo Molnar 91905a36a2SIngo Molnar.macro TRACE_IRQS_IRETQ_DEBUG 92905a36a2SIngo Molnar bt $9, EFLAGS(%rsp) /* interrupts off? */ 93905a36a2SIngo Molnar jnc 1f 94905a36a2SIngo Molnar TRACE_IRQS_ON_DEBUG 95905a36a2SIngo Molnar1: 96905a36a2SIngo Molnar.endm 97905a36a2SIngo Molnar 98905a36a2SIngo Molnar#else 99905a36a2SIngo Molnar# define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF 100905a36a2SIngo Molnar# define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON 101905a36a2SIngo Molnar# define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ 102905a36a2SIngo Molnar#endif 103905a36a2SIngo Molnar 104905a36a2SIngo Molnar/* 1054d732138SIngo Molnar * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers. 106905a36a2SIngo Molnar * 1074d732138SIngo Molnar * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11, 108905a36a2SIngo Molnar * then loads new ss, cs, and rip from previously programmed MSRs. 109905a36a2SIngo Molnar * rflags gets masked by a value from another MSR (so CLD and CLAC 110905a36a2SIngo Molnar * are not needed). SYSCALL does not save anything on the stack 111905a36a2SIngo Molnar * and does not change rsp. 112905a36a2SIngo Molnar * 113905a36a2SIngo Molnar * Registers on entry: 114905a36a2SIngo Molnar * rax system call number 115905a36a2SIngo Molnar * rcx return address 116905a36a2SIngo Molnar * r11 saved rflags (note: r11 is callee-clobbered register in C ABI) 117905a36a2SIngo Molnar * rdi arg0 118905a36a2SIngo Molnar * rsi arg1 119905a36a2SIngo Molnar * rdx arg2 120905a36a2SIngo Molnar * r10 arg3 (needs to be moved to rcx to conform to C ABI) 121905a36a2SIngo Molnar * r8 arg4 122905a36a2SIngo Molnar * r9 arg5 123905a36a2SIngo Molnar * (note: r12-r15, rbp, rbx are callee-preserved in C ABI) 124905a36a2SIngo Molnar * 125905a36a2SIngo Molnar * Only called from user space. 126905a36a2SIngo Molnar * 127905a36a2SIngo Molnar * When user can change pt_regs->foo always force IRET. That is because 128905a36a2SIngo Molnar * it deals with uncanonical addresses better. SYSRET has trouble 129905a36a2SIngo Molnar * with them due to bugs in both AMD and Intel CPUs. 130905a36a2SIngo Molnar */ 131905a36a2SIngo Molnar 132b2502b41SIngo MolnarENTRY(entry_SYSCALL_64) 133905a36a2SIngo Molnar /* 134905a36a2SIngo Molnar * Interrupts are off on entry. 135905a36a2SIngo Molnar * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON, 136905a36a2SIngo Molnar * it is too small to ever cause noticeable irq latency. 137905a36a2SIngo Molnar */ 138905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 139905a36a2SIngo Molnar /* 140905a36a2SIngo Molnar * A hypervisor implementation might want to use a label 141905a36a2SIngo Molnar * after the swapgs, so that it can do the swapgs 142905a36a2SIngo Molnar * for the guest and jump here on syscall. 143905a36a2SIngo Molnar */ 144b2502b41SIngo MolnarGLOBAL(entry_SYSCALL_64_after_swapgs) 145905a36a2SIngo Molnar 146905a36a2SIngo Molnar movq %rsp, PER_CPU_VAR(rsp_scratch) 147905a36a2SIngo Molnar movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 148905a36a2SIngo Molnar 149905a36a2SIngo Molnar /* Construct struct pt_regs on stack */ 150905a36a2SIngo Molnar pushq $__USER_DS /* pt_regs->ss */ 151905a36a2SIngo Molnar pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */ 152905a36a2SIngo Molnar /* 153905a36a2SIngo Molnar * Re-enable interrupts. 154905a36a2SIngo Molnar * We use 'rsp_scratch' as a scratch space, hence irq-off block above 155905a36a2SIngo Molnar * must execute atomically in the face of possible interrupt-driven 156905a36a2SIngo Molnar * task preemption. We must enable interrupts only after we're done 157905a36a2SIngo Molnar * with using rsp_scratch: 158905a36a2SIngo Molnar */ 159905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 160905a36a2SIngo Molnar pushq %r11 /* pt_regs->flags */ 161905a36a2SIngo Molnar pushq $__USER_CS /* pt_regs->cs */ 162905a36a2SIngo Molnar pushq %rcx /* pt_regs->ip */ 163905a36a2SIngo Molnar pushq %rax /* pt_regs->orig_ax */ 164905a36a2SIngo Molnar pushq %rdi /* pt_regs->di */ 165905a36a2SIngo Molnar pushq %rsi /* pt_regs->si */ 166905a36a2SIngo Molnar pushq %rdx /* pt_regs->dx */ 167905a36a2SIngo Molnar pushq %rcx /* pt_regs->cx */ 168905a36a2SIngo Molnar pushq $-ENOSYS /* pt_regs->ax */ 169905a36a2SIngo Molnar pushq %r8 /* pt_regs->r8 */ 170905a36a2SIngo Molnar pushq %r9 /* pt_regs->r9 */ 171905a36a2SIngo Molnar pushq %r10 /* pt_regs->r10 */ 172905a36a2SIngo Molnar pushq %r11 /* pt_regs->r11 */ 173905a36a2SIngo Molnar sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */ 174905a36a2SIngo Molnar 175905a36a2SIngo Molnar testl $_TIF_WORK_SYSCALL_ENTRY, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS) 176905a36a2SIngo Molnar jnz tracesys 177b2502b41SIngo Molnarentry_SYSCALL_64_fastpath: 178905a36a2SIngo Molnar#if __SYSCALL_MASK == ~0 179905a36a2SIngo Molnar cmpq $__NR_syscall_max, %rax 180905a36a2SIngo Molnar#else 181905a36a2SIngo Molnar andl $__SYSCALL_MASK, %eax 182905a36a2SIngo Molnar cmpl $__NR_syscall_max, %eax 183905a36a2SIngo Molnar#endif 184905a36a2SIngo Molnar ja 1f /* return -ENOSYS (already in pt_regs->ax) */ 185905a36a2SIngo Molnar movq %r10, %rcx 186905a36a2SIngo Molnar call *sys_call_table(, %rax, 8) 187905a36a2SIngo Molnar movq %rax, RAX(%rsp) 188905a36a2SIngo Molnar1: 189905a36a2SIngo Molnar/* 190905a36a2SIngo Molnar * Syscall return path ending with SYSRET (fast path). 191905a36a2SIngo Molnar * Has incompletely filled pt_regs. 192905a36a2SIngo Molnar */ 193905a36a2SIngo Molnar LOCKDEP_SYS_EXIT 194905a36a2SIngo Molnar /* 195905a36a2SIngo Molnar * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON, 196905a36a2SIngo Molnar * it is too small to ever cause noticeable irq latency. 197905a36a2SIngo Molnar */ 198905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 199905a36a2SIngo Molnar 200905a36a2SIngo Molnar /* 201905a36a2SIngo Molnar * We must check ti flags with interrupts (or at least preemption) 202905a36a2SIngo Molnar * off because we must *never* return to userspace without 203905a36a2SIngo Molnar * processing exit work that is enqueued if we're preempted here. 204905a36a2SIngo Molnar * In particular, returning to userspace with any of the one-shot 205905a36a2SIngo Molnar * flags (TIF_NOTIFY_RESUME, TIF_USER_RETURN_NOTIFY, etc) set is 206905a36a2SIngo Molnar * very bad. 207905a36a2SIngo Molnar */ 208905a36a2SIngo Molnar testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS) 209905a36a2SIngo Molnar jnz int_ret_from_sys_call_irqs_off /* Go to the slow path */ 210905a36a2SIngo Molnar 211905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RCX_R11 212905a36a2SIngo Molnar movq RIP(%rsp), %rcx 213905a36a2SIngo Molnar movq EFLAGS(%rsp), %r11 214905a36a2SIngo Molnar movq RSP(%rsp), %rsp 215905a36a2SIngo Molnar /* 2164d732138SIngo Molnar * 64-bit SYSRET restores rip from rcx, 217905a36a2SIngo Molnar * rflags from r11 (but RF and VM bits are forced to 0), 218905a36a2SIngo Molnar * cs and ss are loaded from MSRs. 219905a36a2SIngo Molnar * Restoration of rflags re-enables interrupts. 220905a36a2SIngo Molnar * 221905a36a2SIngo Molnar * NB: On AMD CPUs with the X86_BUG_SYSRET_SS_ATTRS bug, the ss 222905a36a2SIngo Molnar * descriptor is not reinitialized. This means that we should 223905a36a2SIngo Molnar * avoid SYSRET with SS == NULL, which could happen if we schedule, 224905a36a2SIngo Molnar * exit the kernel, and re-enter using an interrupt vector. (All 225905a36a2SIngo Molnar * interrupt entries on x86_64 set SS to NULL.) We prevent that 226905a36a2SIngo Molnar * from happening by reloading SS in __switch_to. (Actually 227905a36a2SIngo Molnar * detecting the failure in 64-bit userspace is tricky but can be 228905a36a2SIngo Molnar * done.) 229905a36a2SIngo Molnar */ 230905a36a2SIngo Molnar USERGS_SYSRET64 231905a36a2SIngo Molnar 232905a36a2SIngo Molnar /* Do syscall entry tracing */ 233905a36a2SIngo Molnartracesys: 234905a36a2SIngo Molnar movq %rsp, %rdi 235905a36a2SIngo Molnar movl $AUDIT_ARCH_X86_64, %esi 236905a36a2SIngo Molnar call syscall_trace_enter_phase1 237905a36a2SIngo Molnar test %rax, %rax 238905a36a2SIngo Molnar jnz tracesys_phase2 /* if needed, run the slow path */ 239905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RAX /* else restore clobbered regs */ 240905a36a2SIngo Molnar movq ORIG_RAX(%rsp), %rax 241b2502b41SIngo Molnar jmp entry_SYSCALL_64_fastpath /* and return to the fast path */ 242905a36a2SIngo Molnar 243905a36a2SIngo Molnartracesys_phase2: 244905a36a2SIngo Molnar SAVE_EXTRA_REGS 245905a36a2SIngo Molnar movq %rsp, %rdi 246905a36a2SIngo Molnar movl $AUDIT_ARCH_X86_64, %esi 247905a36a2SIngo Molnar movq %rax, %rdx 248905a36a2SIngo Molnar call syscall_trace_enter_phase2 249905a36a2SIngo Molnar 250905a36a2SIngo Molnar /* 251905a36a2SIngo Molnar * Reload registers from stack in case ptrace changed them. 252905a36a2SIngo Molnar * We don't reload %rax because syscall_trace_entry_phase2() returned 253905a36a2SIngo Molnar * the value it wants us to use in the table lookup. 254905a36a2SIngo Molnar */ 255905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RAX 256905a36a2SIngo Molnar RESTORE_EXTRA_REGS 257905a36a2SIngo Molnar#if __SYSCALL_MASK == ~0 258905a36a2SIngo Molnar cmpq $__NR_syscall_max, %rax 259905a36a2SIngo Molnar#else 260905a36a2SIngo Molnar andl $__SYSCALL_MASK, %eax 261905a36a2SIngo Molnar cmpl $__NR_syscall_max, %eax 262905a36a2SIngo Molnar#endif 263905a36a2SIngo Molnar ja 1f /* return -ENOSYS (already in pt_regs->ax) */ 264905a36a2SIngo Molnar movq %r10, %rcx /* fixup for C */ 265905a36a2SIngo Molnar call *sys_call_table(, %rax, 8) 266905a36a2SIngo Molnar movq %rax, RAX(%rsp) 267905a36a2SIngo Molnar1: 268905a36a2SIngo Molnar /* Use IRET because user could have changed pt_regs->foo */ 269905a36a2SIngo Molnar 270905a36a2SIngo Molnar/* 271905a36a2SIngo Molnar * Syscall return path ending with IRET. 272905a36a2SIngo Molnar * Has correct iret frame. 273905a36a2SIngo Molnar */ 274905a36a2SIngo MolnarGLOBAL(int_ret_from_sys_call) 275905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 276905a36a2SIngo Molnarint_ret_from_sys_call_irqs_off: /* jumps come here from the irqs-off SYSRET path */ 277905a36a2SIngo Molnar TRACE_IRQS_OFF 278905a36a2SIngo Molnar movl $_TIF_ALLWORK_MASK, %edi 279905a36a2SIngo Molnar /* edi: mask to check */ 280905a36a2SIngo MolnarGLOBAL(int_with_check) 281905a36a2SIngo Molnar LOCKDEP_SYS_EXIT_IRQ 282905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 283905a36a2SIngo Molnar movl TI_flags(%rcx), %edx 284905a36a2SIngo Molnar andl %edi, %edx 285905a36a2SIngo Molnar jnz int_careful 286905a36a2SIngo Molnar andl $~TS_COMPAT, TI_status(%rcx) 287905a36a2SIngo Molnar jmp syscall_return 288905a36a2SIngo Molnar 2894d732138SIngo Molnar /* 2904d732138SIngo Molnar * Either reschedule or signal or syscall exit tracking needed. 2914d732138SIngo Molnar * First do a reschedule test. 2924d732138SIngo Molnar * edx: work, edi: workmask 2934d732138SIngo Molnar */ 294905a36a2SIngo Molnarint_careful: 295905a36a2SIngo Molnar bt $TIF_NEED_RESCHED, %edx 296905a36a2SIngo Molnar jnc int_very_careful 297905a36a2SIngo Molnar TRACE_IRQS_ON 298905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 299905a36a2SIngo Molnar pushq %rdi 300905a36a2SIngo Molnar SCHEDULE_USER 301905a36a2SIngo Molnar popq %rdi 302905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 303905a36a2SIngo Molnar TRACE_IRQS_OFF 304905a36a2SIngo Molnar jmp int_with_check 305905a36a2SIngo Molnar 306905a36a2SIngo Molnar /* handle signals and tracing -- both require a full pt_regs */ 307905a36a2SIngo Molnarint_very_careful: 308905a36a2SIngo Molnar TRACE_IRQS_ON 309905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 310905a36a2SIngo Molnar SAVE_EXTRA_REGS 311905a36a2SIngo Molnar /* Check for syscall exit trace */ 312905a36a2SIngo Molnar testl $_TIF_WORK_SYSCALL_EXIT, %edx 313905a36a2SIngo Molnar jz int_signal 314905a36a2SIngo Molnar pushq %rdi 3154d732138SIngo Molnar leaq 8(%rsp), %rdi /* &ptregs -> arg1 */ 316905a36a2SIngo Molnar call syscall_trace_leave 317905a36a2SIngo Molnar popq %rdi 318905a36a2SIngo Molnar andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU), %edi 319905a36a2SIngo Molnar jmp int_restore_rest 320905a36a2SIngo Molnar 321905a36a2SIngo Molnarint_signal: 322905a36a2SIngo Molnar testl $_TIF_DO_NOTIFY_MASK, %edx 323905a36a2SIngo Molnar jz 1f 3244d732138SIngo Molnar movq %rsp, %rdi /* &ptregs -> arg1 */ 3254d732138SIngo Molnar xorl %esi, %esi /* oldset -> arg2 */ 326905a36a2SIngo Molnar call do_notify_resume 327905a36a2SIngo Molnar1: movl $_TIF_WORK_MASK, %edi 328905a36a2SIngo Molnarint_restore_rest: 329905a36a2SIngo Molnar RESTORE_EXTRA_REGS 330905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 331905a36a2SIngo Molnar TRACE_IRQS_OFF 332905a36a2SIngo Molnar jmp int_with_check 333905a36a2SIngo Molnar 334905a36a2SIngo Molnarsyscall_return: 335905a36a2SIngo Molnar /* The IRETQ could re-enable interrupts: */ 336905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_ANY) 337905a36a2SIngo Molnar TRACE_IRQS_IRETQ 338905a36a2SIngo Molnar 339905a36a2SIngo Molnar /* 340905a36a2SIngo Molnar * Try to use SYSRET instead of IRET if we're returning to 341905a36a2SIngo Molnar * a completely clean 64-bit userspace context. 342905a36a2SIngo Molnar */ 343905a36a2SIngo Molnar movq RCX(%rsp), %rcx 344905a36a2SIngo Molnar movq RIP(%rsp), %r11 345905a36a2SIngo Molnar cmpq %rcx, %r11 /* RCX == RIP */ 346905a36a2SIngo Molnar jne opportunistic_sysret_failed 347905a36a2SIngo Molnar 348905a36a2SIngo Molnar /* 349905a36a2SIngo Molnar * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP 350905a36a2SIngo Molnar * in kernel space. This essentially lets the user take over 351905a36a2SIngo Molnar * the kernel, since userspace controls RSP. 352905a36a2SIngo Molnar * 353905a36a2SIngo Molnar * If width of "canonical tail" ever becomes variable, this will need 354905a36a2SIngo Molnar * to be updated to remain correct on both old and new CPUs. 355905a36a2SIngo Molnar */ 356905a36a2SIngo Molnar .ifne __VIRTUAL_MASK_SHIFT - 47 357905a36a2SIngo Molnar .error "virtual address width changed -- SYSRET checks need update" 358905a36a2SIngo Molnar .endif 3594d732138SIngo Molnar 360905a36a2SIngo Molnar /* Change top 16 bits to be the sign-extension of 47th bit */ 361905a36a2SIngo Molnar shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 362905a36a2SIngo Molnar sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 3634d732138SIngo Molnar 364905a36a2SIngo Molnar /* If this changed %rcx, it was not canonical */ 365905a36a2SIngo Molnar cmpq %rcx, %r11 366905a36a2SIngo Molnar jne opportunistic_sysret_failed 367905a36a2SIngo Molnar 368905a36a2SIngo Molnar cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */ 369905a36a2SIngo Molnar jne opportunistic_sysret_failed 370905a36a2SIngo Molnar 371905a36a2SIngo Molnar movq R11(%rsp), %r11 372905a36a2SIngo Molnar cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */ 373905a36a2SIngo Molnar jne opportunistic_sysret_failed 374905a36a2SIngo Molnar 375905a36a2SIngo Molnar /* 376905a36a2SIngo Molnar * SYSRET can't restore RF. SYSRET can restore TF, but unlike IRET, 377905a36a2SIngo Molnar * restoring TF results in a trap from userspace immediately after 378905a36a2SIngo Molnar * SYSRET. This would cause an infinite loop whenever #DB happens 379905a36a2SIngo Molnar * with register state that satisfies the opportunistic SYSRET 380905a36a2SIngo Molnar * conditions. For example, single-stepping this user code: 381905a36a2SIngo Molnar * 382905a36a2SIngo Molnar * movq $stuck_here, %rcx 383905a36a2SIngo Molnar * pushfq 384905a36a2SIngo Molnar * popq %r11 385905a36a2SIngo Molnar * stuck_here: 386905a36a2SIngo Molnar * 387905a36a2SIngo Molnar * would never get past 'stuck_here'. 388905a36a2SIngo Molnar */ 389905a36a2SIngo Molnar testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11 390905a36a2SIngo Molnar jnz opportunistic_sysret_failed 391905a36a2SIngo Molnar 392905a36a2SIngo Molnar /* nothing to check for RSP */ 393905a36a2SIngo Molnar 394905a36a2SIngo Molnar cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */ 395905a36a2SIngo Molnar jne opportunistic_sysret_failed 396905a36a2SIngo Molnar 397905a36a2SIngo Molnar /* 398905a36a2SIngo Molnar * We win! This label is here just for ease of understanding 399905a36a2SIngo Molnar * perf profiles. Nothing jumps here. 400905a36a2SIngo Molnar */ 401905a36a2SIngo Molnarsyscall_return_via_sysret: 402905a36a2SIngo Molnar /* rcx and r11 are already restored (see code above) */ 403905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RCX_R11 404905a36a2SIngo Molnar movq RSP(%rsp), %rsp 405905a36a2SIngo Molnar USERGS_SYSRET64 406905a36a2SIngo Molnar 407905a36a2SIngo Molnaropportunistic_sysret_failed: 408905a36a2SIngo Molnar SWAPGS 409905a36a2SIngo Molnar jmp restore_c_regs_and_iret 410b2502b41SIngo MolnarEND(entry_SYSCALL_64) 411905a36a2SIngo Molnar 412905a36a2SIngo Molnar 413905a36a2SIngo Molnar .macro FORK_LIKE func 414905a36a2SIngo MolnarENTRY(stub_\func) 415905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 416905a36a2SIngo Molnar jmp sys_\func 417905a36a2SIngo MolnarEND(stub_\func) 418905a36a2SIngo Molnar .endm 419905a36a2SIngo Molnar 420905a36a2SIngo Molnar FORK_LIKE clone 421905a36a2SIngo Molnar FORK_LIKE fork 422905a36a2SIngo Molnar FORK_LIKE vfork 423905a36a2SIngo Molnar 424905a36a2SIngo MolnarENTRY(stub_execve) 425905a36a2SIngo Molnar call sys_execve 426905a36a2SIngo Molnarreturn_from_execve: 427905a36a2SIngo Molnar testl %eax, %eax 428905a36a2SIngo Molnar jz 1f 429905a36a2SIngo Molnar /* exec failed, can use fast SYSRET code path in this case */ 430905a36a2SIngo Molnar ret 431905a36a2SIngo Molnar1: 432905a36a2SIngo Molnar /* must use IRET code path (pt_regs->cs may have changed) */ 433905a36a2SIngo Molnar addq $8, %rsp 434905a36a2SIngo Molnar ZERO_EXTRA_REGS 435905a36a2SIngo Molnar movq %rax, RAX(%rsp) 436905a36a2SIngo Molnar jmp int_ret_from_sys_call 437905a36a2SIngo MolnarEND(stub_execve) 438905a36a2SIngo Molnar/* 439905a36a2SIngo Molnar * Remaining execve stubs are only 7 bytes long. 440905a36a2SIngo Molnar * ENTRY() often aligns to 16 bytes, which in this case has no benefits. 441905a36a2SIngo Molnar */ 442905a36a2SIngo Molnar .align 8 443905a36a2SIngo MolnarGLOBAL(stub_execveat) 444905a36a2SIngo Molnar call sys_execveat 445905a36a2SIngo Molnar jmp return_from_execve 446905a36a2SIngo MolnarEND(stub_execveat) 447905a36a2SIngo Molnar 448905a36a2SIngo Molnar#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION) 449905a36a2SIngo Molnar .align 8 450905a36a2SIngo MolnarGLOBAL(stub_x32_execve) 451905a36a2SIngo MolnarGLOBAL(stub32_execve) 452905a36a2SIngo Molnar call compat_sys_execve 453905a36a2SIngo Molnar jmp return_from_execve 454905a36a2SIngo MolnarEND(stub32_execve) 455905a36a2SIngo MolnarEND(stub_x32_execve) 456905a36a2SIngo Molnar .align 8 457905a36a2SIngo MolnarGLOBAL(stub_x32_execveat) 458905a36a2SIngo MolnarGLOBAL(stub32_execveat) 459905a36a2SIngo Molnar call compat_sys_execveat 460905a36a2SIngo Molnar jmp return_from_execve 461905a36a2SIngo MolnarEND(stub32_execveat) 462905a36a2SIngo MolnarEND(stub_x32_execveat) 463905a36a2SIngo Molnar#endif 464905a36a2SIngo Molnar 465905a36a2SIngo Molnar/* 466905a36a2SIngo Molnar * sigreturn is special because it needs to restore all registers on return. 467905a36a2SIngo Molnar * This cannot be done with SYSRET, so use the IRET return path instead. 468905a36a2SIngo Molnar */ 469905a36a2SIngo MolnarENTRY(stub_rt_sigreturn) 470905a36a2SIngo Molnar /* 471905a36a2SIngo Molnar * SAVE_EXTRA_REGS result is not normally needed: 472905a36a2SIngo Molnar * sigreturn overwrites all pt_regs->GPREGS. 473905a36a2SIngo Molnar * But sigreturn can fail (!), and there is no easy way to detect that. 474905a36a2SIngo Molnar * To make sure RESTORE_EXTRA_REGS doesn't restore garbage on error, 475905a36a2SIngo Molnar * we SAVE_EXTRA_REGS here. 476905a36a2SIngo Molnar */ 477905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 478905a36a2SIngo Molnar call sys_rt_sigreturn 479905a36a2SIngo Molnarreturn_from_stub: 480905a36a2SIngo Molnar addq $8, %rsp 481905a36a2SIngo Molnar RESTORE_EXTRA_REGS 482905a36a2SIngo Molnar movq %rax, RAX(%rsp) 483905a36a2SIngo Molnar jmp int_ret_from_sys_call 484905a36a2SIngo MolnarEND(stub_rt_sigreturn) 485905a36a2SIngo Molnar 486905a36a2SIngo Molnar#ifdef CONFIG_X86_X32_ABI 487905a36a2SIngo MolnarENTRY(stub_x32_rt_sigreturn) 488905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 489905a36a2SIngo Molnar call sys32_x32_rt_sigreturn 490905a36a2SIngo Molnar jmp return_from_stub 491905a36a2SIngo MolnarEND(stub_x32_rt_sigreturn) 492905a36a2SIngo Molnar#endif 493905a36a2SIngo Molnar 494905a36a2SIngo Molnar/* 495905a36a2SIngo Molnar * A newly forked process directly context switches into this address. 496905a36a2SIngo Molnar * 497905a36a2SIngo Molnar * rdi: prev task we switched from 498905a36a2SIngo Molnar */ 499905a36a2SIngo MolnarENTRY(ret_from_fork) 500905a36a2SIngo Molnar 501905a36a2SIngo Molnar LOCK ; btr $TIF_FORK, TI_flags(%r8) 502905a36a2SIngo Molnar 503905a36a2SIngo Molnar pushq $0x0002 5044d732138SIngo Molnar popfq /* reset kernel eflags */ 505905a36a2SIngo Molnar 5064d732138SIngo Molnar call schedule_tail /* rdi: 'prev' task parameter */ 507905a36a2SIngo Molnar 508905a36a2SIngo Molnar RESTORE_EXTRA_REGS 509905a36a2SIngo Molnar 5104d732138SIngo Molnar testb $3, CS(%rsp) /* from kernel_thread? */ 511905a36a2SIngo Molnar 512905a36a2SIngo Molnar /* 513905a36a2SIngo Molnar * By the time we get here, we have no idea whether our pt_regs, 514905a36a2SIngo Molnar * ti flags, and ti status came from the 64-bit SYSCALL fast path, 515138bd56aSIngo Molnar * the slow path, or one of the 32-bit compat paths. 516905a36a2SIngo Molnar * Use IRET code path to return, since it can safely handle 517905a36a2SIngo Molnar * all of the above. 518905a36a2SIngo Molnar */ 519905a36a2SIngo Molnar jnz int_ret_from_sys_call 520905a36a2SIngo Molnar 5214d732138SIngo Molnar /* 5224d732138SIngo Molnar * We came from kernel_thread 5234d732138SIngo Molnar * nb: we depend on RESTORE_EXTRA_REGS above 5244d732138SIngo Molnar */ 525905a36a2SIngo Molnar movq %rbp, %rdi 526905a36a2SIngo Molnar call *%rbx 527905a36a2SIngo Molnar movl $0, RAX(%rsp) 528905a36a2SIngo Molnar RESTORE_EXTRA_REGS 529905a36a2SIngo Molnar jmp int_ret_from_sys_call 530905a36a2SIngo MolnarEND(ret_from_fork) 531905a36a2SIngo Molnar 532905a36a2SIngo Molnar/* 533905a36a2SIngo Molnar * Build the entry stubs with some assembler magic. 534905a36a2SIngo Molnar * We pack 1 stub into every 8-byte block. 535905a36a2SIngo Molnar */ 536905a36a2SIngo Molnar .align 8 537905a36a2SIngo MolnarENTRY(irq_entries_start) 538905a36a2SIngo Molnar vector=FIRST_EXTERNAL_VECTOR 539905a36a2SIngo Molnar .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR) 540905a36a2SIngo Molnar pushq $(~vector+0x80) /* Note: always in signed byte range */ 541905a36a2SIngo Molnar vector=vector+1 542905a36a2SIngo Molnar jmp common_interrupt 543905a36a2SIngo Molnar .align 8 544905a36a2SIngo Molnar .endr 545905a36a2SIngo MolnarEND(irq_entries_start) 546905a36a2SIngo Molnar 547905a36a2SIngo Molnar/* 548905a36a2SIngo Molnar * Interrupt entry/exit. 549905a36a2SIngo Molnar * 550905a36a2SIngo Molnar * Interrupt entry points save only callee clobbered registers in fast path. 551905a36a2SIngo Molnar * 552905a36a2SIngo Molnar * Entry runs with interrupts off. 553905a36a2SIngo Molnar */ 554905a36a2SIngo Molnar 555905a36a2SIngo Molnar/* 0(%rsp): ~(interrupt number) */ 556905a36a2SIngo Molnar .macro interrupt func 557905a36a2SIngo Molnar cld 558905a36a2SIngo Molnar /* 559905a36a2SIngo Molnar * Since nothing in interrupt handling code touches r12...r15 members 560905a36a2SIngo Molnar * of "struct pt_regs", and since interrupts can nest, we can save 561905a36a2SIngo Molnar * four stack slots and simultaneously provide 562905a36a2SIngo Molnar * an unwind-friendly stack layout by saving "truncated" pt_regs 563905a36a2SIngo Molnar * exactly up to rbp slot, without these members. 564905a36a2SIngo Molnar */ 565905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK -RBP 566905a36a2SIngo Molnar SAVE_C_REGS -RBP 567905a36a2SIngo Molnar /* this goes to 0(%rsp) for unwinder, not for saving the value: */ 568905a36a2SIngo Molnar SAVE_EXTRA_REGS_RBP -RBP 569905a36a2SIngo Molnar 570905a36a2SIngo Molnar leaq -RBP(%rsp), %rdi /* arg1 for \func (pointer to pt_regs) */ 571905a36a2SIngo Molnar 572905a36a2SIngo Molnar testb $3, CS-RBP(%rsp) 573905a36a2SIngo Molnar jz 1f 574905a36a2SIngo Molnar SWAPGS 575905a36a2SIngo Molnar1: 576905a36a2SIngo Molnar /* 577905a36a2SIngo Molnar * Save previous stack pointer, optionally switch to interrupt stack. 578905a36a2SIngo Molnar * irq_count is used to check if a CPU is already on an interrupt stack 579905a36a2SIngo Molnar * or not. While this is essentially redundant with preempt_count it is 580905a36a2SIngo Molnar * a little cheaper to use a separate counter in the PDA (short of 581905a36a2SIngo Molnar * moving irq_enter into assembly, which would be too much work) 582905a36a2SIngo Molnar */ 583905a36a2SIngo Molnar movq %rsp, %rsi 584905a36a2SIngo Molnar incl PER_CPU_VAR(irq_count) 585905a36a2SIngo Molnar cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp 586905a36a2SIngo Molnar pushq %rsi 587905a36a2SIngo Molnar /* We entered an interrupt context - irqs are off: */ 588905a36a2SIngo Molnar TRACE_IRQS_OFF 589905a36a2SIngo Molnar 590905a36a2SIngo Molnar call \func 591905a36a2SIngo Molnar .endm 592905a36a2SIngo Molnar 593905a36a2SIngo Molnar /* 594905a36a2SIngo Molnar * The interrupt stubs push (~vector+0x80) onto the stack and 595905a36a2SIngo Molnar * then jump to common_interrupt. 596905a36a2SIngo Molnar */ 597905a36a2SIngo Molnar .p2align CONFIG_X86_L1_CACHE_SHIFT 598905a36a2SIngo Molnarcommon_interrupt: 599905a36a2SIngo Molnar ASM_CLAC 600905a36a2SIngo Molnar addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */ 601905a36a2SIngo Molnar interrupt do_IRQ 602905a36a2SIngo Molnar /* 0(%rsp): old RSP */ 603905a36a2SIngo Molnarret_from_intr: 604905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 605905a36a2SIngo Molnar TRACE_IRQS_OFF 606905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 607905a36a2SIngo Molnar 608905a36a2SIngo Molnar /* Restore saved previous stack */ 609905a36a2SIngo Molnar popq %rsi 610905a36a2SIngo Molnar /* return code expects complete pt_regs - adjust rsp accordingly: */ 611905a36a2SIngo Molnar leaq -RBP(%rsi), %rsp 612905a36a2SIngo Molnar 613905a36a2SIngo Molnar testb $3, CS(%rsp) 614905a36a2SIngo Molnar jz retint_kernel 615905a36a2SIngo Molnar /* Interrupt came from user space */ 616905a36a2SIngo Molnarretint_user: 617905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 6184d732138SIngo Molnar 6194d732138SIngo Molnar /* %rcx: thread info. Interrupts are off. */ 620905a36a2SIngo Molnarretint_with_reschedule: 621905a36a2SIngo Molnar movl $_TIF_WORK_MASK, %edi 622905a36a2SIngo Molnarretint_check: 623905a36a2SIngo Molnar LOCKDEP_SYS_EXIT_IRQ 624905a36a2SIngo Molnar movl TI_flags(%rcx), %edx 625905a36a2SIngo Molnar andl %edi, %edx 626905a36a2SIngo Molnar jnz retint_careful 627905a36a2SIngo Molnar 628905a36a2SIngo Molnarretint_swapgs: /* return to user-space */ 629905a36a2SIngo Molnar /* 630905a36a2SIngo Molnar * The iretq could re-enable interrupts: 631905a36a2SIngo Molnar */ 632905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_ANY) 633905a36a2SIngo Molnar TRACE_IRQS_IRETQ 634905a36a2SIngo Molnar 635905a36a2SIngo Molnar SWAPGS 636905a36a2SIngo Molnar jmp restore_c_regs_and_iret 637905a36a2SIngo Molnar 638905a36a2SIngo Molnar/* Returning to kernel space */ 639905a36a2SIngo Molnarretint_kernel: 640905a36a2SIngo Molnar#ifdef CONFIG_PREEMPT 641905a36a2SIngo Molnar /* Interrupts are off */ 642905a36a2SIngo Molnar /* Check if we need preemption */ 6434d732138SIngo Molnar bt $9, EFLAGS(%rsp) /* were interrupts off? */ 644905a36a2SIngo Molnar jnc 1f 645905a36a2SIngo Molnar0: cmpl $0, PER_CPU_VAR(__preempt_count) 646905a36a2SIngo Molnar jnz 1f 647905a36a2SIngo Molnar call preempt_schedule_irq 648905a36a2SIngo Molnar jmp 0b 649905a36a2SIngo Molnar1: 650905a36a2SIngo Molnar#endif 651905a36a2SIngo Molnar /* 652905a36a2SIngo Molnar * The iretq could re-enable interrupts: 653905a36a2SIngo Molnar */ 654905a36a2SIngo Molnar TRACE_IRQS_IRETQ 655905a36a2SIngo Molnar 656905a36a2SIngo Molnar/* 657905a36a2SIngo Molnar * At this label, code paths which return to kernel and to user, 658905a36a2SIngo Molnar * which come from interrupts/exception and from syscalls, merge. 659905a36a2SIngo Molnar */ 660905a36a2SIngo Molnarrestore_c_regs_and_iret: 661905a36a2SIngo Molnar RESTORE_C_REGS 662905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 8 663905a36a2SIngo Molnar INTERRUPT_RETURN 664905a36a2SIngo Molnar 665905a36a2SIngo MolnarENTRY(native_iret) 666905a36a2SIngo Molnar /* 667905a36a2SIngo Molnar * Are we returning to a stack segment from the LDT? Note: in 668905a36a2SIngo Molnar * 64-bit mode SS:RSP on the exception stack is always valid. 669905a36a2SIngo Molnar */ 670905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 671905a36a2SIngo Molnar testb $4, (SS-RIP)(%rsp) 672905a36a2SIngo Molnar jnz native_irq_return_ldt 673905a36a2SIngo Molnar#endif 674905a36a2SIngo Molnar 675905a36a2SIngo Molnar.global native_irq_return_iret 676905a36a2SIngo Molnarnative_irq_return_iret: 677905a36a2SIngo Molnar /* 678905a36a2SIngo Molnar * This may fault. Non-paranoid faults on return to userspace are 679905a36a2SIngo Molnar * handled by fixup_bad_iret. These include #SS, #GP, and #NP. 680905a36a2SIngo Molnar * Double-faults due to espfix64 are handled in do_double_fault. 681905a36a2SIngo Molnar * Other faults here are fatal. 682905a36a2SIngo Molnar */ 683905a36a2SIngo Molnar iretq 684905a36a2SIngo Molnar 685905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 686905a36a2SIngo Molnarnative_irq_return_ldt: 687905a36a2SIngo Molnar pushq %rax 688905a36a2SIngo Molnar pushq %rdi 689905a36a2SIngo Molnar SWAPGS 690905a36a2SIngo Molnar movq PER_CPU_VAR(espfix_waddr), %rdi 691905a36a2SIngo Molnar movq %rax, (0*8)(%rdi) /* RAX */ 692905a36a2SIngo Molnar movq (2*8)(%rsp), %rax /* RIP */ 693905a36a2SIngo Molnar movq %rax, (1*8)(%rdi) 694905a36a2SIngo Molnar movq (3*8)(%rsp), %rax /* CS */ 695905a36a2SIngo Molnar movq %rax, (2*8)(%rdi) 696905a36a2SIngo Molnar movq (4*8)(%rsp), %rax /* RFLAGS */ 697905a36a2SIngo Molnar movq %rax, (3*8)(%rdi) 698905a36a2SIngo Molnar movq (6*8)(%rsp), %rax /* SS */ 699905a36a2SIngo Molnar movq %rax, (5*8)(%rdi) 700905a36a2SIngo Molnar movq (5*8)(%rsp), %rax /* RSP */ 701905a36a2SIngo Molnar movq %rax, (4*8)(%rdi) 702905a36a2SIngo Molnar andl $0xffff0000, %eax 703905a36a2SIngo Molnar popq %rdi 704905a36a2SIngo Molnar orq PER_CPU_VAR(espfix_stack), %rax 705905a36a2SIngo Molnar SWAPGS 706905a36a2SIngo Molnar movq %rax, %rsp 707905a36a2SIngo Molnar popq %rax 708905a36a2SIngo Molnar jmp native_irq_return_iret 709905a36a2SIngo Molnar#endif 710905a36a2SIngo Molnar 711905a36a2SIngo Molnar /* edi: workmask, edx: work */ 712905a36a2SIngo Molnarretint_careful: 713905a36a2SIngo Molnar bt $TIF_NEED_RESCHED, %edx 714905a36a2SIngo Molnar jnc retint_signal 715905a36a2SIngo Molnar TRACE_IRQS_ON 716905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 717905a36a2SIngo Molnar pushq %rdi 718905a36a2SIngo Molnar SCHEDULE_USER 719905a36a2SIngo Molnar popq %rdi 720905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 721905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 722905a36a2SIngo Molnar TRACE_IRQS_OFF 723905a36a2SIngo Molnar jmp retint_check 724905a36a2SIngo Molnar 725905a36a2SIngo Molnarretint_signal: 726905a36a2SIngo Molnar testl $_TIF_DO_NOTIFY_MASK, %edx 727905a36a2SIngo Molnar jz retint_swapgs 728905a36a2SIngo Molnar TRACE_IRQS_ON 729905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 730905a36a2SIngo Molnar SAVE_EXTRA_REGS 731905a36a2SIngo Molnar movq $-1, ORIG_RAX(%rsp) 7324d732138SIngo Molnar xorl %esi, %esi /* oldset */ 7334d732138SIngo Molnar movq %rsp, %rdi /* &pt_regs */ 734905a36a2SIngo Molnar call do_notify_resume 735905a36a2SIngo Molnar RESTORE_EXTRA_REGS 736905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 737905a36a2SIngo Molnar TRACE_IRQS_OFF 738905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 739905a36a2SIngo Molnar jmp retint_with_reschedule 740905a36a2SIngo Molnar 741905a36a2SIngo MolnarEND(common_interrupt) 742905a36a2SIngo Molnar 743905a36a2SIngo Molnar/* 744905a36a2SIngo Molnar * APIC interrupts. 745905a36a2SIngo Molnar */ 746905a36a2SIngo Molnar.macro apicinterrupt3 num sym do_sym 747905a36a2SIngo MolnarENTRY(\sym) 748905a36a2SIngo Molnar ASM_CLAC 749905a36a2SIngo Molnar pushq $~(\num) 750905a36a2SIngo Molnar.Lcommon_\sym: 751905a36a2SIngo Molnar interrupt \do_sym 752905a36a2SIngo Molnar jmp ret_from_intr 753905a36a2SIngo MolnarEND(\sym) 754905a36a2SIngo Molnar.endm 755905a36a2SIngo Molnar 756905a36a2SIngo Molnar#ifdef CONFIG_TRACING 757905a36a2SIngo Molnar#define trace(sym) trace_##sym 758905a36a2SIngo Molnar#define smp_trace(sym) smp_trace_##sym 759905a36a2SIngo Molnar 760905a36a2SIngo Molnar.macro trace_apicinterrupt num sym 761905a36a2SIngo Molnarapicinterrupt3 \num trace(\sym) smp_trace(\sym) 762905a36a2SIngo Molnar.endm 763905a36a2SIngo Molnar#else 764905a36a2SIngo Molnar.macro trace_apicinterrupt num sym do_sym 765905a36a2SIngo Molnar.endm 766905a36a2SIngo Molnar#endif 767905a36a2SIngo Molnar 768905a36a2SIngo Molnar.macro apicinterrupt num sym do_sym 769905a36a2SIngo Molnarapicinterrupt3 \num \sym \do_sym 770905a36a2SIngo Molnartrace_apicinterrupt \num \sym 771905a36a2SIngo Molnar.endm 772905a36a2SIngo Molnar 773905a36a2SIngo Molnar#ifdef CONFIG_SMP 7744d732138SIngo Molnarapicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt 7754d732138SIngo Molnarapicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt 776905a36a2SIngo Molnar#endif 777905a36a2SIngo Molnar 778905a36a2SIngo Molnar#ifdef CONFIG_X86_UV 7794d732138SIngo Molnarapicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt 780905a36a2SIngo Molnar#endif 7814d732138SIngo Molnar 7824d732138SIngo Molnarapicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt 7834d732138SIngo Molnarapicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi 784905a36a2SIngo Molnar 785905a36a2SIngo Molnar#ifdef CONFIG_HAVE_KVM 7864d732138SIngo Molnarapicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi 7874d732138SIngo Molnarapicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi 788905a36a2SIngo Molnar#endif 789905a36a2SIngo Molnar 790905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE_THRESHOLD 7914d732138SIngo Molnarapicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt 792905a36a2SIngo Molnar#endif 793905a36a2SIngo Molnar 7949dda1658SIngo Molnar#ifdef CONFIG_X86_MCE_AMD 7954d732138SIngo Molnarapicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt 7969dda1658SIngo Molnar#endif 7979dda1658SIngo Molnar 798905a36a2SIngo Molnar#ifdef CONFIG_X86_THERMAL_VECTOR 7994d732138SIngo Molnarapicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt 800905a36a2SIngo Molnar#endif 801905a36a2SIngo Molnar 802905a36a2SIngo Molnar#ifdef CONFIG_SMP 8034d732138SIngo Molnarapicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt 8044d732138SIngo Molnarapicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt 8054d732138SIngo Molnarapicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt 806905a36a2SIngo Molnar#endif 807905a36a2SIngo Molnar 8084d732138SIngo Molnarapicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt 8094d732138SIngo Molnarapicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt 810905a36a2SIngo Molnar 811905a36a2SIngo Molnar#ifdef CONFIG_IRQ_WORK 8124d732138SIngo Molnarapicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt 813905a36a2SIngo Molnar#endif 814905a36a2SIngo Molnar 815905a36a2SIngo Molnar/* 816905a36a2SIngo Molnar * Exception entry points. 817905a36a2SIngo Molnar */ 818905a36a2SIngo Molnar#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8) 819905a36a2SIngo Molnar 820905a36a2SIngo Molnar.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 821905a36a2SIngo MolnarENTRY(\sym) 822905a36a2SIngo Molnar /* Sanity check */ 823905a36a2SIngo Molnar .if \shift_ist != -1 && \paranoid == 0 824905a36a2SIngo Molnar .error "using shift_ist requires paranoid=1" 825905a36a2SIngo Molnar .endif 826905a36a2SIngo Molnar 827905a36a2SIngo Molnar ASM_CLAC 828905a36a2SIngo Molnar PARAVIRT_ADJUST_EXCEPTION_FRAME 829905a36a2SIngo Molnar 830905a36a2SIngo Molnar .ifeq \has_error_code 831905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 832905a36a2SIngo Molnar .endif 833905a36a2SIngo Molnar 834905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 835905a36a2SIngo Molnar 836905a36a2SIngo Molnar .if \paranoid 837905a36a2SIngo Molnar .if \paranoid == 1 8384d732138SIngo Molnar testb $3, CS(%rsp) /* If coming from userspace, switch stacks */ 8394d732138SIngo Molnar jnz 1f 840905a36a2SIngo Molnar .endif 841905a36a2SIngo Molnar call paranoid_entry 842905a36a2SIngo Molnar .else 843905a36a2SIngo Molnar call error_entry 844905a36a2SIngo Molnar .endif 845905a36a2SIngo Molnar /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */ 846905a36a2SIngo Molnar 847905a36a2SIngo Molnar .if \paranoid 848905a36a2SIngo Molnar .if \shift_ist != -1 849905a36a2SIngo Molnar TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */ 850905a36a2SIngo Molnar .else 851905a36a2SIngo Molnar TRACE_IRQS_OFF 852905a36a2SIngo Molnar .endif 853905a36a2SIngo Molnar .endif 854905a36a2SIngo Molnar 855905a36a2SIngo Molnar movq %rsp, %rdi /* pt_regs pointer */ 856905a36a2SIngo Molnar 857905a36a2SIngo Molnar .if \has_error_code 858905a36a2SIngo Molnar movq ORIG_RAX(%rsp), %rsi /* get error code */ 859905a36a2SIngo Molnar movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 860905a36a2SIngo Molnar .else 861905a36a2SIngo Molnar xorl %esi, %esi /* no error code */ 862905a36a2SIngo Molnar .endif 863905a36a2SIngo Molnar 864905a36a2SIngo Molnar .if \shift_ist != -1 865905a36a2SIngo Molnar subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist) 866905a36a2SIngo Molnar .endif 867905a36a2SIngo Molnar 868905a36a2SIngo Molnar call \do_sym 869905a36a2SIngo Molnar 870905a36a2SIngo Molnar .if \shift_ist != -1 871905a36a2SIngo Molnar addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist) 872905a36a2SIngo Molnar .endif 873905a36a2SIngo Molnar 874905a36a2SIngo Molnar /* these procedures expect "no swapgs" flag in ebx */ 875905a36a2SIngo Molnar .if \paranoid 876905a36a2SIngo Molnar jmp paranoid_exit 877905a36a2SIngo Molnar .else 878905a36a2SIngo Molnar jmp error_exit 879905a36a2SIngo Molnar .endif 880905a36a2SIngo Molnar 881905a36a2SIngo Molnar .if \paranoid == 1 882905a36a2SIngo Molnar /* 883905a36a2SIngo Molnar * Paranoid entry from userspace. Switch stacks and treat it 884905a36a2SIngo Molnar * as a normal entry. This means that paranoid handlers 885905a36a2SIngo Molnar * run in real process context if user_mode(regs). 886905a36a2SIngo Molnar */ 887905a36a2SIngo Molnar1: 888905a36a2SIngo Molnar call error_entry 889905a36a2SIngo Molnar 890905a36a2SIngo Molnar 891905a36a2SIngo Molnar movq %rsp, %rdi /* pt_regs pointer */ 892905a36a2SIngo Molnar call sync_regs 893905a36a2SIngo Molnar movq %rax, %rsp /* switch stack */ 894905a36a2SIngo Molnar 895905a36a2SIngo Molnar movq %rsp, %rdi /* pt_regs pointer */ 896905a36a2SIngo Molnar 897905a36a2SIngo Molnar .if \has_error_code 898905a36a2SIngo Molnar movq ORIG_RAX(%rsp), %rsi /* get error code */ 899905a36a2SIngo Molnar movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 900905a36a2SIngo Molnar .else 901905a36a2SIngo Molnar xorl %esi, %esi /* no error code */ 902905a36a2SIngo Molnar .endif 903905a36a2SIngo Molnar 904905a36a2SIngo Molnar call \do_sym 905905a36a2SIngo Molnar 906905a36a2SIngo Molnar jmp error_exit /* %ebx: no swapgs flag */ 907905a36a2SIngo Molnar .endif 908905a36a2SIngo MolnarEND(\sym) 909905a36a2SIngo Molnar.endm 910905a36a2SIngo Molnar 911905a36a2SIngo Molnar#ifdef CONFIG_TRACING 912905a36a2SIngo Molnar.macro trace_idtentry sym do_sym has_error_code:req 913905a36a2SIngo Molnaridtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code 914905a36a2SIngo Molnaridtentry \sym \do_sym has_error_code=\has_error_code 915905a36a2SIngo Molnar.endm 916905a36a2SIngo Molnar#else 917905a36a2SIngo Molnar.macro trace_idtentry sym do_sym has_error_code:req 918905a36a2SIngo Molnaridtentry \sym \do_sym has_error_code=\has_error_code 919905a36a2SIngo Molnar.endm 920905a36a2SIngo Molnar#endif 921905a36a2SIngo Molnar 922905a36a2SIngo Molnaridtentry divide_error do_divide_error has_error_code=0 923905a36a2SIngo Molnaridtentry overflow do_overflow has_error_code=0 924905a36a2SIngo Molnaridtentry bounds do_bounds has_error_code=0 925905a36a2SIngo Molnaridtentry invalid_op do_invalid_op has_error_code=0 926905a36a2SIngo Molnaridtentry device_not_available do_device_not_available has_error_code=0 927905a36a2SIngo Molnaridtentry double_fault do_double_fault has_error_code=1 paranoid=2 928905a36a2SIngo Molnaridtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0 929905a36a2SIngo Molnaridtentry invalid_TSS do_invalid_TSS has_error_code=1 930905a36a2SIngo Molnaridtentry segment_not_present do_segment_not_present has_error_code=1 931905a36a2SIngo Molnaridtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0 932905a36a2SIngo Molnaridtentry coprocessor_error do_coprocessor_error has_error_code=0 933905a36a2SIngo Molnaridtentry alignment_check do_alignment_check has_error_code=1 934905a36a2SIngo Molnaridtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0 935905a36a2SIngo Molnar 936905a36a2SIngo Molnar 9374d732138SIngo Molnar /* 9384d732138SIngo Molnar * Reload gs selector with exception handling 9394d732138SIngo Molnar * edi: new selector 9404d732138SIngo Molnar */ 941905a36a2SIngo MolnarENTRY(native_load_gs_index) 942905a36a2SIngo Molnar pushfq 943905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI) 944905a36a2SIngo Molnar SWAPGS 945905a36a2SIngo Molnargs_change: 946905a36a2SIngo Molnar movl %edi, %gs 947905a36a2SIngo Molnar2: mfence /* workaround */ 948905a36a2SIngo Molnar SWAPGS 949905a36a2SIngo Molnar popfq 950905a36a2SIngo Molnar ret 951905a36a2SIngo MolnarEND(native_load_gs_index) 952905a36a2SIngo Molnar 953905a36a2SIngo Molnar _ASM_EXTABLE(gs_change, bad_gs) 954905a36a2SIngo Molnar .section .fixup, "ax" 955905a36a2SIngo Molnar /* running with kernelgs */ 956905a36a2SIngo Molnarbad_gs: 957905a36a2SIngo Molnar SWAPGS /* switch back to user gs */ 958905a36a2SIngo Molnar xorl %eax, %eax 959905a36a2SIngo Molnar movl %eax, %gs 960905a36a2SIngo Molnar jmp 2b 961905a36a2SIngo Molnar .previous 962905a36a2SIngo Molnar 963905a36a2SIngo Molnar/* Call softirq on interrupt stack. Interrupts are off. */ 964905a36a2SIngo MolnarENTRY(do_softirq_own_stack) 965905a36a2SIngo Molnar pushq %rbp 966905a36a2SIngo Molnar mov %rsp, %rbp 967905a36a2SIngo Molnar incl PER_CPU_VAR(irq_count) 968905a36a2SIngo Molnar cmove PER_CPU_VAR(irq_stack_ptr), %rsp 9694d732138SIngo Molnar push %rbp /* frame pointer backlink */ 970905a36a2SIngo Molnar call __do_softirq 971905a36a2SIngo Molnar leaveq 972905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 973905a36a2SIngo Molnar ret 974905a36a2SIngo MolnarEND(do_softirq_own_stack) 975905a36a2SIngo Molnar 976905a36a2SIngo Molnar#ifdef CONFIG_XEN 977905a36a2SIngo Molnaridtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0 978905a36a2SIngo Molnar 979905a36a2SIngo Molnar/* 980905a36a2SIngo Molnar * A note on the "critical region" in our callback handler. 981905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring 982905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled 983905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before 984905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still 985905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack. 986905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd 987905a36a2SIngo Molnar * like to avoid the possibility. 988905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an 989905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current 990905a36a2SIngo Molnar * activation and restart the handler using the previous one. 991905a36a2SIngo Molnar */ 9924d732138SIngo MolnarENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */ 9934d732138SIngo Molnar 994905a36a2SIngo Molnar/* 995905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will 996905a36a2SIngo Molnar * see the correct pointer to the pt_regs 997905a36a2SIngo Molnar */ 9984d732138SIngo Molnar movq %rdi, %rsp /* we don't return, adjust the stack frame */ 999905a36a2SIngo Molnar11: incl PER_CPU_VAR(irq_count) 1000905a36a2SIngo Molnar movq %rsp, %rbp 1001905a36a2SIngo Molnar cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp 10024d732138SIngo Molnar pushq %rbp /* frame pointer backlink */ 1003905a36a2SIngo Molnar call xen_evtchn_do_upcall 1004905a36a2SIngo Molnar popq %rsp 1005905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 1006905a36a2SIngo Molnar#ifndef CONFIG_PREEMPT 1007905a36a2SIngo Molnar call xen_maybe_preempt_hcall 1008905a36a2SIngo Molnar#endif 1009905a36a2SIngo Molnar jmp error_exit 1010905a36a2SIngo MolnarEND(xen_do_hypervisor_callback) 1011905a36a2SIngo Molnar 1012905a36a2SIngo Molnar/* 1013905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes. 1014905a36a2SIngo Molnar * We get here for two reasons: 1015905a36a2SIngo Molnar * 1. Fault while reloading DS, ES, FS or GS 1016905a36a2SIngo Molnar * 2. Fault while executing IRET 1017905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment 1018905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others. 1019905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the 1020905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall 1021905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks. 1022905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register 1023905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1. 1024905a36a2SIngo Molnar */ 1025905a36a2SIngo MolnarENTRY(xen_failsafe_callback) 1026905a36a2SIngo Molnar movl %ds, %ecx 1027905a36a2SIngo Molnar cmpw %cx, 0x10(%rsp) 1028905a36a2SIngo Molnar jne 1f 1029905a36a2SIngo Molnar movl %es, %ecx 1030905a36a2SIngo Molnar cmpw %cx, 0x18(%rsp) 1031905a36a2SIngo Molnar jne 1f 1032905a36a2SIngo Molnar movl %fs, %ecx 1033905a36a2SIngo Molnar cmpw %cx, 0x20(%rsp) 1034905a36a2SIngo Molnar jne 1f 1035905a36a2SIngo Molnar movl %gs, %ecx 1036905a36a2SIngo Molnar cmpw %cx, 0x28(%rsp) 1037905a36a2SIngo Molnar jne 1f 1038905a36a2SIngo Molnar /* All segments match their saved values => Category 2 (Bad IRET). */ 1039905a36a2SIngo Molnar movq (%rsp), %rcx 1040905a36a2SIngo Molnar movq 8(%rsp), %r11 1041905a36a2SIngo Molnar addq $0x30, %rsp 1042905a36a2SIngo Molnar pushq $0 /* RIP */ 1043905a36a2SIngo Molnar pushq %r11 1044905a36a2SIngo Molnar pushq %rcx 1045905a36a2SIngo Molnar jmp general_protection 1046905a36a2SIngo Molnar1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */ 1047905a36a2SIngo Molnar movq (%rsp), %rcx 1048905a36a2SIngo Molnar movq 8(%rsp), %r11 1049905a36a2SIngo Molnar addq $0x30, %rsp 1050905a36a2SIngo Molnar pushq $-1 /* orig_ax = -1 => not a system call */ 1051905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 1052905a36a2SIngo Molnar SAVE_C_REGS 1053905a36a2SIngo Molnar SAVE_EXTRA_REGS 1054905a36a2SIngo Molnar jmp error_exit 1055905a36a2SIngo MolnarEND(xen_failsafe_callback) 1056905a36a2SIngo Molnar 1057905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 1058905a36a2SIngo Molnar xen_hvm_callback_vector xen_evtchn_do_upcall 1059905a36a2SIngo Molnar 1060905a36a2SIngo Molnar#endif /* CONFIG_XEN */ 1061905a36a2SIngo Molnar 1062905a36a2SIngo Molnar#if IS_ENABLED(CONFIG_HYPERV) 1063905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 1064905a36a2SIngo Molnar hyperv_callback_vector hyperv_vector_handler 1065905a36a2SIngo Molnar#endif /* CONFIG_HYPERV */ 1066905a36a2SIngo Molnar 1067905a36a2SIngo Molnaridtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK 1068905a36a2SIngo Molnaridtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK 1069905a36a2SIngo Molnaridtentry stack_segment do_stack_segment has_error_code=1 10704d732138SIngo Molnar 1071905a36a2SIngo Molnar#ifdef CONFIG_XEN 1072905a36a2SIngo Molnaridtentry xen_debug do_debug has_error_code=0 1073905a36a2SIngo Molnaridtentry xen_int3 do_int3 has_error_code=0 1074905a36a2SIngo Molnaridtentry xen_stack_segment do_stack_segment has_error_code=1 1075905a36a2SIngo Molnar#endif 10764d732138SIngo Molnar 1077905a36a2SIngo Molnaridtentry general_protection do_general_protection has_error_code=1 1078905a36a2SIngo Molnartrace_idtentry page_fault do_page_fault has_error_code=1 10794d732138SIngo Molnar 1080905a36a2SIngo Molnar#ifdef CONFIG_KVM_GUEST 1081905a36a2SIngo Molnaridtentry async_page_fault do_async_page_fault has_error_code=1 1082905a36a2SIngo Molnar#endif 10834d732138SIngo Molnar 1084905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE 1085905a36a2SIngo Molnaridtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip) 1086905a36a2SIngo Molnar#endif 1087905a36a2SIngo Molnar 1088905a36a2SIngo Molnar/* 1089905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed. 1090905a36a2SIngo Molnar * Use slow, but surefire "are we in kernel?" check. 1091905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise 1092905a36a2SIngo Molnar */ 1093905a36a2SIngo MolnarENTRY(paranoid_entry) 1094905a36a2SIngo Molnar cld 1095905a36a2SIngo Molnar SAVE_C_REGS 8 1096905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 1097905a36a2SIngo Molnar movl $1, %ebx 1098905a36a2SIngo Molnar movl $MSR_GS_BASE, %ecx 1099905a36a2SIngo Molnar rdmsr 1100905a36a2SIngo Molnar testl %edx, %edx 1101905a36a2SIngo Molnar js 1f /* negative -> in kernel */ 1102905a36a2SIngo Molnar SWAPGS 1103905a36a2SIngo Molnar xorl %ebx, %ebx 1104905a36a2SIngo Molnar1: ret 1105905a36a2SIngo MolnarEND(paranoid_entry) 1106905a36a2SIngo Molnar 1107905a36a2SIngo Molnar/* 1108905a36a2SIngo Molnar * "Paranoid" exit path from exception stack. This is invoked 1109905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came 1110905a36a2SIngo Molnar * from kernel space. 1111905a36a2SIngo Molnar * 1112905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early 1113905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would 1114905a36a2SIngo Molnar * be complicated. Fortunately, we there's no good reason 1115905a36a2SIngo Molnar * to try to handle preemption here. 11164d732138SIngo Molnar * 11174d732138SIngo Molnar * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) 1118905a36a2SIngo Molnar */ 1119905a36a2SIngo MolnarENTRY(paranoid_exit) 1120905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 1121905a36a2SIngo Molnar TRACE_IRQS_OFF_DEBUG 1122905a36a2SIngo Molnar testl %ebx, %ebx /* swapgs needed? */ 1123905a36a2SIngo Molnar jnz paranoid_exit_no_swapgs 1124905a36a2SIngo Molnar TRACE_IRQS_IRETQ 1125905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1126905a36a2SIngo Molnar jmp paranoid_exit_restore 1127905a36a2SIngo Molnarparanoid_exit_no_swapgs: 1128905a36a2SIngo Molnar TRACE_IRQS_IRETQ_DEBUG 1129905a36a2SIngo Molnarparanoid_exit_restore: 1130905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1131905a36a2SIngo Molnar RESTORE_C_REGS 1132905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 8 1133905a36a2SIngo Molnar INTERRUPT_RETURN 1134905a36a2SIngo MolnarEND(paranoid_exit) 1135905a36a2SIngo Molnar 1136905a36a2SIngo Molnar/* 1137905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed. 1138539f5113SAndy Lutomirski * Return: EBX=0: came from user mode; EBX=1: otherwise 1139905a36a2SIngo Molnar */ 1140905a36a2SIngo MolnarENTRY(error_entry) 1141905a36a2SIngo Molnar cld 1142905a36a2SIngo Molnar SAVE_C_REGS 8 1143905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 1144905a36a2SIngo Molnar xorl %ebx, %ebx 1145905a36a2SIngo Molnar testb $3, CS+8(%rsp) 1146905a36a2SIngo Molnar jz error_kernelspace 1147539f5113SAndy Lutomirski 1148539f5113SAndy Lutomirski /* We entered from user mode */ 1149905a36a2SIngo Molnar SWAPGS 1150539f5113SAndy Lutomirski 1151539f5113SAndy Lutomirskierror_entry_done: 1152905a36a2SIngo Molnar TRACE_IRQS_OFF 1153905a36a2SIngo Molnar ret 1154905a36a2SIngo Molnar 1155905a36a2SIngo Molnar /* 1156905a36a2SIngo Molnar * There are two places in the kernel that can potentially fault with 1157905a36a2SIngo Molnar * usergs. Handle them here. B stepping K8s sometimes report a 1158905a36a2SIngo Molnar * truncated RIP for IRET exceptions returning to compat mode. Check 1159905a36a2SIngo Molnar * for these here too. 1160905a36a2SIngo Molnar */ 1161905a36a2SIngo Molnarerror_kernelspace: 1162905a36a2SIngo Molnar incl %ebx 1163905a36a2SIngo Molnar leaq native_irq_return_iret(%rip), %rcx 1164905a36a2SIngo Molnar cmpq %rcx, RIP+8(%rsp) 1165905a36a2SIngo Molnar je error_bad_iret 1166905a36a2SIngo Molnar movl %ecx, %eax /* zero extend */ 1167905a36a2SIngo Molnar cmpq %rax, RIP+8(%rsp) 1168905a36a2SIngo Molnar je bstep_iret 1169905a36a2SIngo Molnar cmpq $gs_change, RIP+8(%rsp) 1170539f5113SAndy Lutomirski jne error_entry_done 1171539f5113SAndy Lutomirski 1172539f5113SAndy Lutomirski /* 1173539f5113SAndy Lutomirski * hack: gs_change can fail with user gsbase. If this happens, fix up 1174539f5113SAndy Lutomirski * gsbase and proceed. We'll fix up the exception and land in 1175539f5113SAndy Lutomirski * gs_change's error handler with kernel gsbase. 1176539f5113SAndy Lutomirski */ 1177539f5113SAndy Lutomirski SWAPGS 1178539f5113SAndy Lutomirski jmp error_entry_done 1179905a36a2SIngo Molnar 1180905a36a2SIngo Molnarbstep_iret: 1181905a36a2SIngo Molnar /* Fix truncated RIP */ 1182905a36a2SIngo Molnar movq %rcx, RIP+8(%rsp) 1183905a36a2SIngo Molnar /* fall through */ 1184905a36a2SIngo Molnar 1185905a36a2SIngo Molnarerror_bad_iret: 1186539f5113SAndy Lutomirski /* 1187539f5113SAndy Lutomirski * We came from an IRET to user mode, so we have user gsbase. 1188539f5113SAndy Lutomirski * Switch to kernel gsbase: 1189539f5113SAndy Lutomirski */ 1190905a36a2SIngo Molnar SWAPGS 1191539f5113SAndy Lutomirski 1192539f5113SAndy Lutomirski /* 1193539f5113SAndy Lutomirski * Pretend that the exception came from user mode: set up pt_regs 1194539f5113SAndy Lutomirski * as if we faulted immediately after IRET and clear EBX so that 1195539f5113SAndy Lutomirski * error_exit knows that we will be returning to user mode. 1196539f5113SAndy Lutomirski */ 1197905a36a2SIngo Molnar mov %rsp, %rdi 1198905a36a2SIngo Molnar call fixup_bad_iret 1199905a36a2SIngo Molnar mov %rax, %rsp 1200539f5113SAndy Lutomirski decl %ebx 1201539f5113SAndy Lutomirski jmp error_entry_done 1202905a36a2SIngo MolnarEND(error_entry) 1203905a36a2SIngo Molnar 1204905a36a2SIngo Molnar 1205539f5113SAndy Lutomirski/* 1206539f5113SAndy Lutomirski * On entry, EBS is a "return to kernel mode" flag: 1207539f5113SAndy Lutomirski * 1: already in kernel mode, don't need SWAPGS 1208539f5113SAndy Lutomirski * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode 1209539f5113SAndy Lutomirski */ 1210905a36a2SIngo MolnarENTRY(error_exit) 1211905a36a2SIngo Molnar movl %ebx, %eax 1212905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1213905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 1214905a36a2SIngo Molnar TRACE_IRQS_OFF 1215905a36a2SIngo Molnar testl %eax, %eax 1216905a36a2SIngo Molnar jnz retint_kernel 1217905a36a2SIngo Molnar jmp retint_user 1218905a36a2SIngo MolnarEND(error_exit) 1219905a36a2SIngo Molnar 1220905a36a2SIngo Molnar/* Runs on exception stack */ 1221905a36a2SIngo MolnarENTRY(nmi) 1222905a36a2SIngo Molnar PARAVIRT_ADJUST_EXCEPTION_FRAME 1223905a36a2SIngo Molnar /* 1224905a36a2SIngo Molnar * We allow breakpoints in NMIs. If a breakpoint occurs, then 1225905a36a2SIngo Molnar * the iretq it performs will take us out of NMI context. 1226905a36a2SIngo Molnar * This means that we can have nested NMIs where the next 1227905a36a2SIngo Molnar * NMI is using the top of the stack of the previous NMI. We 1228905a36a2SIngo Molnar * can't let it execute because the nested NMI will corrupt the 1229905a36a2SIngo Molnar * stack of the previous NMI. NMI handlers are not re-entrant 1230905a36a2SIngo Molnar * anyway. 1231905a36a2SIngo Molnar * 1232905a36a2SIngo Molnar * To handle this case we do the following: 1233905a36a2SIngo Molnar * Check the a special location on the stack that contains 1234905a36a2SIngo Molnar * a variable that is set when NMIs are executing. 1235905a36a2SIngo Molnar * The interrupted task's stack is also checked to see if it 1236905a36a2SIngo Molnar * is an NMI stack. 1237905a36a2SIngo Molnar * If the variable is not set and the stack is not the NMI 1238905a36a2SIngo Molnar * stack then: 1239905a36a2SIngo Molnar * o Set the special variable on the stack 1240*0b22930eSAndy Lutomirski * o Copy the interrupt frame into an "outermost" location on the 1241*0b22930eSAndy Lutomirski * stack 1242*0b22930eSAndy Lutomirski * o Copy the interrupt frame into an "iret" location on the stack 1243905a36a2SIngo Molnar * o Continue processing the NMI 1244905a36a2SIngo Molnar * If the variable is set or the previous stack is the NMI stack: 1245*0b22930eSAndy Lutomirski * o Modify the "iret" location to jump to the repeat_nmi 1246905a36a2SIngo Molnar * o return back to the first NMI 1247905a36a2SIngo Molnar * 1248905a36a2SIngo Molnar * Now on exit of the first NMI, we first clear the stack variable 1249905a36a2SIngo Molnar * The NMI stack will tell any nested NMIs at that point that it is 1250905a36a2SIngo Molnar * nested. Then we pop the stack normally with iret, and if there was 1251905a36a2SIngo Molnar * a nested NMI that updated the copy interrupt stack frame, a 1252905a36a2SIngo Molnar * jump will be made to the repeat_nmi code that will handle the second 1253905a36a2SIngo Molnar * NMI. 12549b6e6a83SAndy Lutomirski * 12559b6e6a83SAndy Lutomirski * However, espfix prevents us from directly returning to userspace 12569b6e6a83SAndy Lutomirski * with a single IRET instruction. Similarly, IRET to user mode 12579b6e6a83SAndy Lutomirski * can fault. We therefore handle NMIs from user space like 12589b6e6a83SAndy Lutomirski * other IST entries. 1259905a36a2SIngo Molnar */ 1260905a36a2SIngo Molnar 1261905a36a2SIngo Molnar /* Use %rdx as our temp variable throughout */ 1262905a36a2SIngo Molnar pushq %rdx 1263905a36a2SIngo Molnar 12649b6e6a83SAndy Lutomirski testb $3, CS-RIP+8(%rsp) 12659b6e6a83SAndy Lutomirski jz .Lnmi_from_kernel 1266905a36a2SIngo Molnar 1267905a36a2SIngo Molnar /* 12689b6e6a83SAndy Lutomirski * NMI from user mode. We need to run on the thread stack, but we 12699b6e6a83SAndy Lutomirski * can't go through the normal entry paths: NMIs are masked, and 12709b6e6a83SAndy Lutomirski * we don't want to enable interrupts, because then we'll end 12719b6e6a83SAndy Lutomirski * up in an awkward situation in which IRQs are on but NMIs 12729b6e6a83SAndy Lutomirski * are off. 12739b6e6a83SAndy Lutomirski */ 12749b6e6a83SAndy Lutomirski 12759b6e6a83SAndy Lutomirski SWAPGS 12769b6e6a83SAndy Lutomirski cld 12779b6e6a83SAndy Lutomirski movq %rsp, %rdx 12789b6e6a83SAndy Lutomirski movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp 12799b6e6a83SAndy Lutomirski pushq 5*8(%rdx) /* pt_regs->ss */ 12809b6e6a83SAndy Lutomirski pushq 4*8(%rdx) /* pt_regs->rsp */ 12819b6e6a83SAndy Lutomirski pushq 3*8(%rdx) /* pt_regs->flags */ 12829b6e6a83SAndy Lutomirski pushq 2*8(%rdx) /* pt_regs->cs */ 12839b6e6a83SAndy Lutomirski pushq 1*8(%rdx) /* pt_regs->rip */ 12849b6e6a83SAndy Lutomirski pushq $-1 /* pt_regs->orig_ax */ 12859b6e6a83SAndy Lutomirski pushq %rdi /* pt_regs->di */ 12869b6e6a83SAndy Lutomirski pushq %rsi /* pt_regs->si */ 12879b6e6a83SAndy Lutomirski pushq (%rdx) /* pt_regs->dx */ 12889b6e6a83SAndy Lutomirski pushq %rcx /* pt_regs->cx */ 12899b6e6a83SAndy Lutomirski pushq %rax /* pt_regs->ax */ 12909b6e6a83SAndy Lutomirski pushq %r8 /* pt_regs->r8 */ 12919b6e6a83SAndy Lutomirski pushq %r9 /* pt_regs->r9 */ 12929b6e6a83SAndy Lutomirski pushq %r10 /* pt_regs->r10 */ 12939b6e6a83SAndy Lutomirski pushq %r11 /* pt_regs->r11 */ 12949b6e6a83SAndy Lutomirski pushq %rbx /* pt_regs->rbx */ 12959b6e6a83SAndy Lutomirski pushq %rbp /* pt_regs->rbp */ 12969b6e6a83SAndy Lutomirski pushq %r12 /* pt_regs->r12 */ 12979b6e6a83SAndy Lutomirski pushq %r13 /* pt_regs->r13 */ 12989b6e6a83SAndy Lutomirski pushq %r14 /* pt_regs->r14 */ 12999b6e6a83SAndy Lutomirski pushq %r15 /* pt_regs->r15 */ 13009b6e6a83SAndy Lutomirski 13019b6e6a83SAndy Lutomirski /* 13029b6e6a83SAndy Lutomirski * At this point we no longer need to worry about stack damage 13039b6e6a83SAndy Lutomirski * due to nesting -- we're on the normal thread stack and we're 13049b6e6a83SAndy Lutomirski * done with the NMI stack. 13059b6e6a83SAndy Lutomirski */ 13069b6e6a83SAndy Lutomirski 13079b6e6a83SAndy Lutomirski movq %rsp, %rdi 13089b6e6a83SAndy Lutomirski movq $-1, %rsi 13099b6e6a83SAndy Lutomirski call do_nmi 13109b6e6a83SAndy Lutomirski 13119b6e6a83SAndy Lutomirski /* 13129b6e6a83SAndy Lutomirski * Return back to user mode. We must *not* do the normal exit 13139b6e6a83SAndy Lutomirski * work, because we don't want to enable interrupts. Fortunately, 13149b6e6a83SAndy Lutomirski * do_nmi doesn't modify pt_regs. 13159b6e6a83SAndy Lutomirski */ 13169b6e6a83SAndy Lutomirski SWAPGS 13179b6e6a83SAndy Lutomirski jmp restore_c_regs_and_iret 13189b6e6a83SAndy Lutomirski 13199b6e6a83SAndy Lutomirski.Lnmi_from_kernel: 13209b6e6a83SAndy Lutomirski /* 1321*0b22930eSAndy Lutomirski * Here's what our stack frame will look like: 1322*0b22930eSAndy Lutomirski * +---------------------------------------------------------+ 1323*0b22930eSAndy Lutomirski * | original SS | 1324*0b22930eSAndy Lutomirski * | original Return RSP | 1325*0b22930eSAndy Lutomirski * | original RFLAGS | 1326*0b22930eSAndy Lutomirski * | original CS | 1327*0b22930eSAndy Lutomirski * | original RIP | 1328*0b22930eSAndy Lutomirski * +---------------------------------------------------------+ 1329*0b22930eSAndy Lutomirski * | temp storage for rdx | 1330*0b22930eSAndy Lutomirski * +---------------------------------------------------------+ 1331*0b22930eSAndy Lutomirski * | "NMI executing" variable | 1332*0b22930eSAndy Lutomirski * +---------------------------------------------------------+ 1333*0b22930eSAndy Lutomirski * | iret SS } Copied from "outermost" frame | 1334*0b22930eSAndy Lutomirski * | iret Return RSP } on each loop iteration; overwritten | 1335*0b22930eSAndy Lutomirski * | iret RFLAGS } by a nested NMI to force another | 1336*0b22930eSAndy Lutomirski * | iret CS } iteration if needed. | 1337*0b22930eSAndy Lutomirski * | iret RIP } | 1338*0b22930eSAndy Lutomirski * +---------------------------------------------------------+ 1339*0b22930eSAndy Lutomirski * | outermost SS } initialized in first_nmi; | 1340*0b22930eSAndy Lutomirski * | outermost Return RSP } will not be changed before | 1341*0b22930eSAndy Lutomirski * | outermost RFLAGS } NMI processing is done. | 1342*0b22930eSAndy Lutomirski * | outermost CS } Copied to "iret" frame on each | 1343*0b22930eSAndy Lutomirski * | outermost RIP } iteration. | 1344*0b22930eSAndy Lutomirski * +---------------------------------------------------------+ 1345*0b22930eSAndy Lutomirski * | pt_regs | 1346*0b22930eSAndy Lutomirski * +---------------------------------------------------------+ 1347*0b22930eSAndy Lutomirski * 1348*0b22930eSAndy Lutomirski * The "original" frame is used by hardware. Before re-enabling 1349*0b22930eSAndy Lutomirski * NMIs, we need to be done with it, and we need to leave enough 1350*0b22930eSAndy Lutomirski * space for the asm code here. 1351*0b22930eSAndy Lutomirski * 1352*0b22930eSAndy Lutomirski * We return by executing IRET while RSP points to the "iret" frame. 1353*0b22930eSAndy Lutomirski * That will either return for real or it will loop back into NMI 1354*0b22930eSAndy Lutomirski * processing. 1355*0b22930eSAndy Lutomirski * 1356*0b22930eSAndy Lutomirski * The "outermost" frame is copied to the "iret" frame on each 1357*0b22930eSAndy Lutomirski * iteration of the loop, so each iteration starts with the "iret" 1358*0b22930eSAndy Lutomirski * frame pointing to the final return target. 1359*0b22930eSAndy Lutomirski */ 1360*0b22930eSAndy Lutomirski 1361*0b22930eSAndy Lutomirski /* 1362*0b22930eSAndy Lutomirski * Determine whether we're a nested NMI. 1363*0b22930eSAndy Lutomirski * 1364*0b22930eSAndy Lutomirski * First check "NMI executing". If it's set, then we're nested. 1365*0b22930eSAndy Lutomirski * This will not detect if we interrupted an outer NMI just 1366*0b22930eSAndy Lutomirski * before IRET. 1367905a36a2SIngo Molnar */ 1368905a36a2SIngo Molnar cmpl $1, -8(%rsp) 1369905a36a2SIngo Molnar je nested_nmi 1370905a36a2SIngo Molnar 1371905a36a2SIngo Molnar /* 1372*0b22930eSAndy Lutomirski * Now test if the previous stack was an NMI stack. This covers 1373*0b22930eSAndy Lutomirski * the case where we interrupt an outer NMI after it clears 1374*0b22930eSAndy Lutomirski * "NMI executing" but before IRET. 1375905a36a2SIngo Molnar */ 1376905a36a2SIngo Molnar lea 6*8(%rsp), %rdx 1377905a36a2SIngo Molnar /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */ 1378905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1379905a36a2SIngo Molnar /* If the stack pointer is above the NMI stack, this is a normal NMI */ 1380905a36a2SIngo Molnar ja first_nmi 13814d732138SIngo Molnar 1382905a36a2SIngo Molnar subq $EXCEPTION_STKSZ, %rdx 1383905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1384905a36a2SIngo Molnar /* If it is below the NMI stack, it is a normal NMI */ 1385905a36a2SIngo Molnar jb first_nmi 1386905a36a2SIngo Molnar /* Ah, it is within the NMI stack, treat it as nested */ 1387905a36a2SIngo Molnar 1388905a36a2SIngo Molnarnested_nmi: 1389905a36a2SIngo Molnar /* 1390*0b22930eSAndy Lutomirski * If we interrupted an NMI that is between repeat_nmi and 1391*0b22930eSAndy Lutomirski * end_repeat_nmi, then we must not modify the "iret" frame 1392*0b22930eSAndy Lutomirski * because it's being written by the outer NMI. That's okay; 1393*0b22930eSAndy Lutomirski * the outer NMI handler is about to call do_nmi anyway, 1394*0b22930eSAndy Lutomirski * so we can just resume the outer NMI. 1395905a36a2SIngo Molnar */ 1396905a36a2SIngo Molnar movq $repeat_nmi, %rdx 1397905a36a2SIngo Molnar cmpq 8(%rsp), %rdx 1398905a36a2SIngo Molnar ja 1f 1399905a36a2SIngo Molnar movq $end_repeat_nmi, %rdx 1400905a36a2SIngo Molnar cmpq 8(%rsp), %rdx 1401905a36a2SIngo Molnar ja nested_nmi_out 1402905a36a2SIngo Molnar 1403905a36a2SIngo Molnar1: 1404*0b22930eSAndy Lutomirski /* 1405*0b22930eSAndy Lutomirski * Modify the "iret" frame to point to repeat_nmi, forcing another 1406*0b22930eSAndy Lutomirski * iteration of NMI handling. 1407*0b22930eSAndy Lutomirski */ 1408905a36a2SIngo Molnar leaq -1*8(%rsp), %rdx 1409905a36a2SIngo Molnar movq %rdx, %rsp 1410905a36a2SIngo Molnar leaq -10*8(%rsp), %rdx 1411905a36a2SIngo Molnar pushq $__KERNEL_DS 1412905a36a2SIngo Molnar pushq %rdx 1413905a36a2SIngo Molnar pushfq 1414905a36a2SIngo Molnar pushq $__KERNEL_CS 1415905a36a2SIngo Molnar pushq $repeat_nmi 1416905a36a2SIngo Molnar 1417905a36a2SIngo Molnar /* Put stack back */ 1418905a36a2SIngo Molnar addq $(6*8), %rsp 1419905a36a2SIngo Molnar 1420905a36a2SIngo Molnarnested_nmi_out: 1421905a36a2SIngo Molnar popq %rdx 1422905a36a2SIngo Molnar 1423*0b22930eSAndy Lutomirski /* We are returning to kernel mode, so this cannot result in a fault. */ 1424905a36a2SIngo Molnar INTERRUPT_RETURN 1425905a36a2SIngo Molnar 1426905a36a2SIngo Molnarfirst_nmi: 1427*0b22930eSAndy Lutomirski /* Restore rdx. */ 1428905a36a2SIngo Molnar movq (%rsp), %rdx 1429905a36a2SIngo Molnar 1430*0b22930eSAndy Lutomirski /* Set "NMI executing" on the stack. */ 1431905a36a2SIngo Molnar pushq $1 1432905a36a2SIngo Molnar 1433*0b22930eSAndy Lutomirski /* Leave room for the "iret" frame */ 1434905a36a2SIngo Molnar subq $(5*8), %rsp 1435905a36a2SIngo Molnar 1436*0b22930eSAndy Lutomirski /* Copy the "original" frame to the "outermost" frame */ 1437905a36a2SIngo Molnar .rept 5 1438905a36a2SIngo Molnar pushq 11*8(%rsp) 1439905a36a2SIngo Molnar .endr 1440905a36a2SIngo Molnar 1441905a36a2SIngo Molnar /* Everything up to here is safe from nested NMIs */ 1442905a36a2SIngo Molnar 1443*0b22930eSAndy Lutomirskirepeat_nmi: 1444905a36a2SIngo Molnar /* 1445905a36a2SIngo Molnar * If there was a nested NMI, the first NMI's iret will return 1446905a36a2SIngo Molnar * here. But NMIs are still enabled and we can take another 1447905a36a2SIngo Molnar * nested NMI. The nested NMI checks the interrupted RIP to see 1448905a36a2SIngo Molnar * if it is between repeat_nmi and end_repeat_nmi, and if so 1449905a36a2SIngo Molnar * it will just return, as we are about to repeat an NMI anyway. 1450905a36a2SIngo Molnar * This makes it safe to copy to the stack frame that a nested 1451905a36a2SIngo Molnar * NMI will update. 1452*0b22930eSAndy Lutomirski * 1453*0b22930eSAndy Lutomirski * RSP is pointing to "outermost RIP". gsbase is unknown, but, if 1454*0b22930eSAndy Lutomirski * we're repeating an NMI, gsbase has the same value that it had on 1455*0b22930eSAndy Lutomirski * the first iteration. paranoid_entry will load the kernel 1456*0b22930eSAndy Lutomirski * gsbase if needed before we call do_nmi. 1457*0b22930eSAndy Lutomirski * 1458*0b22930eSAndy Lutomirski * Set "NMI executing" in case we came back here via IRET. 1459905a36a2SIngo Molnar */ 1460905a36a2SIngo Molnar movq $1, 10*8(%rsp) 1461905a36a2SIngo Molnar 1462*0b22930eSAndy Lutomirski /* 1463*0b22930eSAndy Lutomirski * Copy the "outermost" frame to the "iret" frame. NMIs that nest 1464*0b22930eSAndy Lutomirski * here must not modify the "iret" frame while we're writing to 1465*0b22930eSAndy Lutomirski * it or it will end up containing garbage. 1466*0b22930eSAndy Lutomirski */ 1467905a36a2SIngo Molnar addq $(10*8), %rsp 1468905a36a2SIngo Molnar .rept 5 1469905a36a2SIngo Molnar pushq -6*8(%rsp) 1470905a36a2SIngo Molnar .endr 1471905a36a2SIngo Molnar subq $(5*8), %rsp 1472905a36a2SIngo Molnarend_repeat_nmi: 1473905a36a2SIngo Molnar 1474905a36a2SIngo Molnar /* 1475*0b22930eSAndy Lutomirski * Everything below this point can be preempted by a nested NMI. 1476*0b22930eSAndy Lutomirski * If this happens, then the inner NMI will change the "iret" 1477*0b22930eSAndy Lutomirski * frame to point back to repeat_nmi. 1478905a36a2SIngo Molnar */ 1479905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 1480905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 1481905a36a2SIngo Molnar 1482905a36a2SIngo Molnar /* 1483905a36a2SIngo Molnar * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit 1484905a36a2SIngo Molnar * as we should not be calling schedule in NMI context. 1485905a36a2SIngo Molnar * Even with normal interrupts enabled. An NMI should not be 1486905a36a2SIngo Molnar * setting NEED_RESCHED or anything that normal interrupts and 1487905a36a2SIngo Molnar * exceptions might do. 1488905a36a2SIngo Molnar */ 1489905a36a2SIngo Molnar call paranoid_entry 1490905a36a2SIngo Molnar 1491905a36a2SIngo Molnar /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */ 1492905a36a2SIngo Molnar movq %rsp, %rdi 1493905a36a2SIngo Molnar movq $-1, %rsi 1494905a36a2SIngo Molnar call do_nmi 1495905a36a2SIngo Molnar 1496905a36a2SIngo Molnar testl %ebx, %ebx /* swapgs needed? */ 1497905a36a2SIngo Molnar jnz nmi_restore 1498905a36a2SIngo Molnarnmi_swapgs: 1499905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1500905a36a2SIngo Molnarnmi_restore: 1501905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1502905a36a2SIngo Molnar RESTORE_C_REGS 1503*0b22930eSAndy Lutomirski 1504*0b22930eSAndy Lutomirski /* Point RSP at the "iret" frame. */ 1505905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 6*8 1506905a36a2SIngo Molnar 1507*0b22930eSAndy Lutomirski /* Clear "NMI executing". */ 1508905a36a2SIngo Molnar movq $0, 5*8(%rsp) 1509*0b22930eSAndy Lutomirski 1510*0b22930eSAndy Lutomirski /* 1511*0b22930eSAndy Lutomirski * INTERRUPT_RETURN reads the "iret" frame and exits the NMI 1512*0b22930eSAndy Lutomirski * stack in a single instruction. We are returning to kernel 1513*0b22930eSAndy Lutomirski * mode, so this cannot result in a fault. 1514*0b22930eSAndy Lutomirski */ 15155ca6f70fSAndy Lutomirski INTERRUPT_RETURN 1516905a36a2SIngo MolnarEND(nmi) 1517905a36a2SIngo Molnar 1518905a36a2SIngo MolnarENTRY(ignore_sysret) 1519905a36a2SIngo Molnar mov $-ENOSYS, %eax 1520905a36a2SIngo Molnar sysret 1521905a36a2SIngo MolnarEND(ignore_sysret) 1522