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 232*29ea1b25SAndy LutomirskiGLOBAL(int_ret_from_sys_call_irqs_off) 233*29ea1b25SAndy Lutomirski TRACE_IRQS_ON 234*29ea1b25SAndy Lutomirski ENABLE_INTERRUPTS(CLBR_NONE) 235*29ea1b25SAndy Lutomirski jmp int_ret_from_sys_call 236*29ea1b25SAndy Lutomirski 237905a36a2SIngo Molnar /* Do syscall entry tracing */ 238905a36a2SIngo Molnartracesys: 239905a36a2SIngo Molnar movq %rsp, %rdi 240905a36a2SIngo Molnar movl $AUDIT_ARCH_X86_64, %esi 241905a36a2SIngo Molnar call syscall_trace_enter_phase1 242905a36a2SIngo Molnar test %rax, %rax 243905a36a2SIngo Molnar jnz tracesys_phase2 /* if needed, run the slow path */ 244905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RAX /* else restore clobbered regs */ 245905a36a2SIngo Molnar movq ORIG_RAX(%rsp), %rax 246b2502b41SIngo Molnar jmp entry_SYSCALL_64_fastpath /* and return to the fast path */ 247905a36a2SIngo Molnar 248905a36a2SIngo Molnartracesys_phase2: 249905a36a2SIngo Molnar SAVE_EXTRA_REGS 250905a36a2SIngo Molnar movq %rsp, %rdi 251905a36a2SIngo Molnar movl $AUDIT_ARCH_X86_64, %esi 252905a36a2SIngo Molnar movq %rax, %rdx 253905a36a2SIngo Molnar call syscall_trace_enter_phase2 254905a36a2SIngo Molnar 255905a36a2SIngo Molnar /* 256905a36a2SIngo Molnar * Reload registers from stack in case ptrace changed them. 257905a36a2SIngo Molnar * We don't reload %rax because syscall_trace_entry_phase2() returned 258905a36a2SIngo Molnar * the value it wants us to use in the table lookup. 259905a36a2SIngo Molnar */ 260905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RAX 261905a36a2SIngo Molnar RESTORE_EXTRA_REGS 262905a36a2SIngo Molnar#if __SYSCALL_MASK == ~0 263905a36a2SIngo Molnar cmpq $__NR_syscall_max, %rax 264905a36a2SIngo Molnar#else 265905a36a2SIngo Molnar andl $__SYSCALL_MASK, %eax 266905a36a2SIngo Molnar cmpl $__NR_syscall_max, %eax 267905a36a2SIngo Molnar#endif 268905a36a2SIngo Molnar ja 1f /* return -ENOSYS (already in pt_regs->ax) */ 269905a36a2SIngo Molnar movq %r10, %rcx /* fixup for C */ 270905a36a2SIngo Molnar call *sys_call_table(, %rax, 8) 271905a36a2SIngo Molnar movq %rax, RAX(%rsp) 272905a36a2SIngo Molnar1: 273905a36a2SIngo Molnar /* Use IRET because user could have changed pt_regs->foo */ 274905a36a2SIngo Molnar 275905a36a2SIngo Molnar/* 276905a36a2SIngo Molnar * Syscall return path ending with IRET. 277905a36a2SIngo Molnar * Has correct iret frame. 278905a36a2SIngo Molnar */ 279905a36a2SIngo MolnarGLOBAL(int_ret_from_sys_call) 280905a36a2SIngo Molnar SAVE_EXTRA_REGS 281*29ea1b25SAndy Lutomirski movq %rsp, %rdi 282*29ea1b25SAndy Lutomirski call syscall_return_slowpath /* returns with IRQs disabled */ 283905a36a2SIngo Molnar RESTORE_EXTRA_REGS 284*29ea1b25SAndy Lutomirski TRACE_IRQS_IRETQ /* we're about to change IF */ 285905a36a2SIngo Molnar 286905a36a2SIngo Molnar /* 287905a36a2SIngo Molnar * Try to use SYSRET instead of IRET if we're returning to 288905a36a2SIngo Molnar * a completely clean 64-bit userspace context. 289905a36a2SIngo Molnar */ 290905a36a2SIngo Molnar movq RCX(%rsp), %rcx 291905a36a2SIngo Molnar movq RIP(%rsp), %r11 292905a36a2SIngo Molnar cmpq %rcx, %r11 /* RCX == RIP */ 293905a36a2SIngo Molnar jne opportunistic_sysret_failed 294905a36a2SIngo Molnar 295905a36a2SIngo Molnar /* 296905a36a2SIngo Molnar * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP 297905a36a2SIngo Molnar * in kernel space. This essentially lets the user take over 298905a36a2SIngo Molnar * the kernel, since userspace controls RSP. 299905a36a2SIngo Molnar * 300905a36a2SIngo Molnar * If width of "canonical tail" ever becomes variable, this will need 301905a36a2SIngo Molnar * to be updated to remain correct on both old and new CPUs. 302905a36a2SIngo Molnar */ 303905a36a2SIngo Molnar .ifne __VIRTUAL_MASK_SHIFT - 47 304905a36a2SIngo Molnar .error "virtual address width changed -- SYSRET checks need update" 305905a36a2SIngo Molnar .endif 3064d732138SIngo Molnar 307905a36a2SIngo Molnar /* Change top 16 bits to be the sign-extension of 47th bit */ 308905a36a2SIngo Molnar shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 309905a36a2SIngo Molnar sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx 3104d732138SIngo Molnar 311905a36a2SIngo Molnar /* If this changed %rcx, it was not canonical */ 312905a36a2SIngo Molnar cmpq %rcx, %r11 313905a36a2SIngo Molnar jne opportunistic_sysret_failed 314905a36a2SIngo Molnar 315905a36a2SIngo Molnar cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */ 316905a36a2SIngo Molnar jne opportunistic_sysret_failed 317905a36a2SIngo Molnar 318905a36a2SIngo Molnar movq R11(%rsp), %r11 319905a36a2SIngo Molnar cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */ 320905a36a2SIngo Molnar jne opportunistic_sysret_failed 321905a36a2SIngo Molnar 322905a36a2SIngo Molnar /* 323905a36a2SIngo Molnar * SYSRET can't restore RF. SYSRET can restore TF, but unlike IRET, 324905a36a2SIngo Molnar * restoring TF results in a trap from userspace immediately after 325905a36a2SIngo Molnar * SYSRET. This would cause an infinite loop whenever #DB happens 326905a36a2SIngo Molnar * with register state that satisfies the opportunistic SYSRET 327905a36a2SIngo Molnar * conditions. For example, single-stepping this user code: 328905a36a2SIngo Molnar * 329905a36a2SIngo Molnar * movq $stuck_here, %rcx 330905a36a2SIngo Molnar * pushfq 331905a36a2SIngo Molnar * popq %r11 332905a36a2SIngo Molnar * stuck_here: 333905a36a2SIngo Molnar * 334905a36a2SIngo Molnar * would never get past 'stuck_here'. 335905a36a2SIngo Molnar */ 336905a36a2SIngo Molnar testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11 337905a36a2SIngo Molnar jnz opportunistic_sysret_failed 338905a36a2SIngo Molnar 339905a36a2SIngo Molnar /* nothing to check for RSP */ 340905a36a2SIngo Molnar 341905a36a2SIngo Molnar cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */ 342905a36a2SIngo Molnar jne opportunistic_sysret_failed 343905a36a2SIngo Molnar 344905a36a2SIngo Molnar /* 345905a36a2SIngo Molnar * We win! This label is here just for ease of understanding 346905a36a2SIngo Molnar * perf profiles. Nothing jumps here. 347905a36a2SIngo Molnar */ 348905a36a2SIngo Molnarsyscall_return_via_sysret: 349905a36a2SIngo Molnar /* rcx and r11 are already restored (see code above) */ 350905a36a2SIngo Molnar RESTORE_C_REGS_EXCEPT_RCX_R11 351905a36a2SIngo Molnar movq RSP(%rsp), %rsp 352905a36a2SIngo Molnar USERGS_SYSRET64 353905a36a2SIngo Molnar 354905a36a2SIngo Molnaropportunistic_sysret_failed: 355905a36a2SIngo Molnar SWAPGS 356905a36a2SIngo Molnar jmp restore_c_regs_and_iret 357b2502b41SIngo MolnarEND(entry_SYSCALL_64) 358905a36a2SIngo Molnar 359905a36a2SIngo Molnar 360905a36a2SIngo Molnar .macro FORK_LIKE func 361905a36a2SIngo MolnarENTRY(stub_\func) 362905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 363905a36a2SIngo Molnar jmp sys_\func 364905a36a2SIngo MolnarEND(stub_\func) 365905a36a2SIngo Molnar .endm 366905a36a2SIngo Molnar 367905a36a2SIngo Molnar FORK_LIKE clone 368905a36a2SIngo Molnar FORK_LIKE fork 369905a36a2SIngo Molnar FORK_LIKE vfork 370905a36a2SIngo Molnar 371905a36a2SIngo MolnarENTRY(stub_execve) 372905a36a2SIngo Molnar call sys_execve 373905a36a2SIngo Molnarreturn_from_execve: 374905a36a2SIngo Molnar testl %eax, %eax 375905a36a2SIngo Molnar jz 1f 376905a36a2SIngo Molnar /* exec failed, can use fast SYSRET code path in this case */ 377905a36a2SIngo Molnar ret 378905a36a2SIngo Molnar1: 379905a36a2SIngo Molnar /* must use IRET code path (pt_regs->cs may have changed) */ 380905a36a2SIngo Molnar addq $8, %rsp 381905a36a2SIngo Molnar ZERO_EXTRA_REGS 382905a36a2SIngo Molnar movq %rax, RAX(%rsp) 383905a36a2SIngo Molnar jmp int_ret_from_sys_call 384905a36a2SIngo MolnarEND(stub_execve) 385905a36a2SIngo Molnar/* 386905a36a2SIngo Molnar * Remaining execve stubs are only 7 bytes long. 387905a36a2SIngo Molnar * ENTRY() often aligns to 16 bytes, which in this case has no benefits. 388905a36a2SIngo Molnar */ 389905a36a2SIngo Molnar .align 8 390905a36a2SIngo MolnarGLOBAL(stub_execveat) 391905a36a2SIngo Molnar call sys_execveat 392905a36a2SIngo Molnar jmp return_from_execve 393905a36a2SIngo MolnarEND(stub_execveat) 394905a36a2SIngo Molnar 395905a36a2SIngo Molnar#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION) 396905a36a2SIngo Molnar .align 8 397905a36a2SIngo MolnarGLOBAL(stub_x32_execve) 398905a36a2SIngo MolnarGLOBAL(stub32_execve) 399905a36a2SIngo Molnar call compat_sys_execve 400905a36a2SIngo Molnar jmp return_from_execve 401905a36a2SIngo MolnarEND(stub32_execve) 402905a36a2SIngo MolnarEND(stub_x32_execve) 403905a36a2SIngo Molnar .align 8 404905a36a2SIngo MolnarGLOBAL(stub_x32_execveat) 405905a36a2SIngo MolnarGLOBAL(stub32_execveat) 406905a36a2SIngo Molnar call compat_sys_execveat 407905a36a2SIngo Molnar jmp return_from_execve 408905a36a2SIngo MolnarEND(stub32_execveat) 409905a36a2SIngo MolnarEND(stub_x32_execveat) 410905a36a2SIngo Molnar#endif 411905a36a2SIngo Molnar 412905a36a2SIngo Molnar/* 413905a36a2SIngo Molnar * sigreturn is special because it needs to restore all registers on return. 414905a36a2SIngo Molnar * This cannot be done with SYSRET, so use the IRET return path instead. 415905a36a2SIngo Molnar */ 416905a36a2SIngo MolnarENTRY(stub_rt_sigreturn) 417905a36a2SIngo Molnar /* 418905a36a2SIngo Molnar * SAVE_EXTRA_REGS result is not normally needed: 419905a36a2SIngo Molnar * sigreturn overwrites all pt_regs->GPREGS. 420905a36a2SIngo Molnar * But sigreturn can fail (!), and there is no easy way to detect that. 421905a36a2SIngo Molnar * To make sure RESTORE_EXTRA_REGS doesn't restore garbage on error, 422905a36a2SIngo Molnar * we SAVE_EXTRA_REGS here. 423905a36a2SIngo Molnar */ 424905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 425905a36a2SIngo Molnar call sys_rt_sigreturn 426905a36a2SIngo Molnarreturn_from_stub: 427905a36a2SIngo Molnar addq $8, %rsp 428905a36a2SIngo Molnar RESTORE_EXTRA_REGS 429905a36a2SIngo Molnar movq %rax, RAX(%rsp) 430905a36a2SIngo Molnar jmp int_ret_from_sys_call 431905a36a2SIngo MolnarEND(stub_rt_sigreturn) 432905a36a2SIngo Molnar 433905a36a2SIngo Molnar#ifdef CONFIG_X86_X32_ABI 434905a36a2SIngo MolnarENTRY(stub_x32_rt_sigreturn) 435905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 436905a36a2SIngo Molnar call sys32_x32_rt_sigreturn 437905a36a2SIngo Molnar jmp return_from_stub 438905a36a2SIngo MolnarEND(stub_x32_rt_sigreturn) 439905a36a2SIngo Molnar#endif 440905a36a2SIngo Molnar 441905a36a2SIngo Molnar/* 442905a36a2SIngo Molnar * A newly forked process directly context switches into this address. 443905a36a2SIngo Molnar * 444905a36a2SIngo Molnar * rdi: prev task we switched from 445905a36a2SIngo Molnar */ 446905a36a2SIngo MolnarENTRY(ret_from_fork) 447905a36a2SIngo Molnar 448905a36a2SIngo Molnar LOCK ; btr $TIF_FORK, TI_flags(%r8) 449905a36a2SIngo Molnar 450905a36a2SIngo Molnar pushq $0x0002 4514d732138SIngo Molnar popfq /* reset kernel eflags */ 452905a36a2SIngo Molnar 4534d732138SIngo Molnar call schedule_tail /* rdi: 'prev' task parameter */ 454905a36a2SIngo Molnar 455905a36a2SIngo Molnar RESTORE_EXTRA_REGS 456905a36a2SIngo Molnar 4574d732138SIngo Molnar testb $3, CS(%rsp) /* from kernel_thread? */ 458905a36a2SIngo Molnar 459905a36a2SIngo Molnar /* 460905a36a2SIngo Molnar * By the time we get here, we have no idea whether our pt_regs, 461905a36a2SIngo Molnar * ti flags, and ti status came from the 64-bit SYSCALL fast path, 462138bd56aSIngo Molnar * the slow path, or one of the 32-bit compat paths. 463905a36a2SIngo Molnar * Use IRET code path to return, since it can safely handle 464905a36a2SIngo Molnar * all of the above. 465905a36a2SIngo Molnar */ 466905a36a2SIngo Molnar jnz int_ret_from_sys_call 467905a36a2SIngo Molnar 4684d732138SIngo Molnar /* 4694d732138SIngo Molnar * We came from kernel_thread 4704d732138SIngo Molnar * nb: we depend on RESTORE_EXTRA_REGS above 4714d732138SIngo Molnar */ 472905a36a2SIngo Molnar movq %rbp, %rdi 473905a36a2SIngo Molnar call *%rbx 474905a36a2SIngo Molnar movl $0, RAX(%rsp) 475905a36a2SIngo Molnar RESTORE_EXTRA_REGS 476905a36a2SIngo Molnar jmp int_ret_from_sys_call 477905a36a2SIngo MolnarEND(ret_from_fork) 478905a36a2SIngo Molnar 479905a36a2SIngo Molnar/* 480905a36a2SIngo Molnar * Build the entry stubs with some assembler magic. 481905a36a2SIngo Molnar * We pack 1 stub into every 8-byte block. 482905a36a2SIngo Molnar */ 483905a36a2SIngo Molnar .align 8 484905a36a2SIngo MolnarENTRY(irq_entries_start) 485905a36a2SIngo Molnar vector=FIRST_EXTERNAL_VECTOR 486905a36a2SIngo Molnar .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR) 487905a36a2SIngo Molnar pushq $(~vector+0x80) /* Note: always in signed byte range */ 488905a36a2SIngo Molnar vector=vector+1 489905a36a2SIngo Molnar jmp common_interrupt 490905a36a2SIngo Molnar .align 8 491905a36a2SIngo Molnar .endr 492905a36a2SIngo MolnarEND(irq_entries_start) 493905a36a2SIngo Molnar 494905a36a2SIngo Molnar/* 495905a36a2SIngo Molnar * Interrupt entry/exit. 496905a36a2SIngo Molnar * 497905a36a2SIngo Molnar * Interrupt entry points save only callee clobbered registers in fast path. 498905a36a2SIngo Molnar * 499905a36a2SIngo Molnar * Entry runs with interrupts off. 500905a36a2SIngo Molnar */ 501905a36a2SIngo Molnar 502905a36a2SIngo Molnar/* 0(%rsp): ~(interrupt number) */ 503905a36a2SIngo Molnar .macro interrupt func 504905a36a2SIngo Molnar cld 505905a36a2SIngo Molnar /* 506905a36a2SIngo Molnar * Since nothing in interrupt handling code touches r12...r15 members 507905a36a2SIngo Molnar * of "struct pt_regs", and since interrupts can nest, we can save 508905a36a2SIngo Molnar * four stack slots and simultaneously provide 509905a36a2SIngo Molnar * an unwind-friendly stack layout by saving "truncated" pt_regs 510905a36a2SIngo Molnar * exactly up to rbp slot, without these members. 511905a36a2SIngo Molnar */ 512905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK -RBP 513905a36a2SIngo Molnar SAVE_C_REGS -RBP 514905a36a2SIngo Molnar /* this goes to 0(%rsp) for unwinder, not for saving the value: */ 515905a36a2SIngo Molnar SAVE_EXTRA_REGS_RBP -RBP 516905a36a2SIngo Molnar 517905a36a2SIngo Molnar leaq -RBP(%rsp), %rdi /* arg1 for \func (pointer to pt_regs) */ 518905a36a2SIngo Molnar 519905a36a2SIngo Molnar testb $3, CS-RBP(%rsp) 520905a36a2SIngo Molnar jz 1f 521905a36a2SIngo Molnar SWAPGS 522905a36a2SIngo Molnar1: 523905a36a2SIngo Molnar /* 524905a36a2SIngo Molnar * Save previous stack pointer, optionally switch to interrupt stack. 525905a36a2SIngo Molnar * irq_count is used to check if a CPU is already on an interrupt stack 526905a36a2SIngo Molnar * or not. While this is essentially redundant with preempt_count it is 527905a36a2SIngo Molnar * a little cheaper to use a separate counter in the PDA (short of 528905a36a2SIngo Molnar * moving irq_enter into assembly, which would be too much work) 529905a36a2SIngo Molnar */ 530905a36a2SIngo Molnar movq %rsp, %rsi 531905a36a2SIngo Molnar incl PER_CPU_VAR(irq_count) 532905a36a2SIngo Molnar cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp 533905a36a2SIngo Molnar pushq %rsi 534905a36a2SIngo Molnar /* We entered an interrupt context - irqs are off: */ 535905a36a2SIngo Molnar TRACE_IRQS_OFF 536905a36a2SIngo Molnar 537905a36a2SIngo Molnar call \func 538905a36a2SIngo Molnar .endm 539905a36a2SIngo Molnar 540905a36a2SIngo Molnar /* 541905a36a2SIngo Molnar * The interrupt stubs push (~vector+0x80) onto the stack and 542905a36a2SIngo Molnar * then jump to common_interrupt. 543905a36a2SIngo Molnar */ 544905a36a2SIngo Molnar .p2align CONFIG_X86_L1_CACHE_SHIFT 545905a36a2SIngo Molnarcommon_interrupt: 546905a36a2SIngo Molnar ASM_CLAC 547905a36a2SIngo Molnar addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */ 548905a36a2SIngo Molnar interrupt do_IRQ 549905a36a2SIngo Molnar /* 0(%rsp): old RSP */ 550905a36a2SIngo Molnarret_from_intr: 551905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 552905a36a2SIngo Molnar TRACE_IRQS_OFF 553905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 554905a36a2SIngo Molnar 555905a36a2SIngo Molnar /* Restore saved previous stack */ 556905a36a2SIngo Molnar popq %rsi 557905a36a2SIngo Molnar /* return code expects complete pt_regs - adjust rsp accordingly: */ 558905a36a2SIngo Molnar leaq -RBP(%rsi), %rsp 559905a36a2SIngo Molnar 560905a36a2SIngo Molnar testb $3, CS(%rsp) 561905a36a2SIngo Molnar jz retint_kernel 562905a36a2SIngo Molnar /* Interrupt came from user space */ 5635e99cb7cSAndy LutomirskiGLOBAL(retint_user) 564905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 5654d732138SIngo Molnar 5664d732138SIngo Molnar /* %rcx: thread info. Interrupts are off. */ 567905a36a2SIngo Molnarretint_with_reschedule: 568905a36a2SIngo Molnar movl $_TIF_WORK_MASK, %edi 569905a36a2SIngo Molnarretint_check: 570905a36a2SIngo Molnar LOCKDEP_SYS_EXIT_IRQ 571905a36a2SIngo Molnar movl TI_flags(%rcx), %edx 572905a36a2SIngo Molnar andl %edi, %edx 573905a36a2SIngo Molnar jnz retint_careful 574905a36a2SIngo Molnar 575905a36a2SIngo Molnarretint_swapgs: /* return to user-space */ 576905a36a2SIngo Molnar /* 577905a36a2SIngo Molnar * The iretq could re-enable interrupts: 578905a36a2SIngo Molnar */ 579905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_ANY) 580905a36a2SIngo Molnar TRACE_IRQS_IRETQ 581905a36a2SIngo Molnar 582905a36a2SIngo Molnar SWAPGS 583905a36a2SIngo Molnar jmp restore_c_regs_and_iret 584905a36a2SIngo Molnar 585905a36a2SIngo Molnar/* Returning to kernel space */ 586905a36a2SIngo Molnarretint_kernel: 587905a36a2SIngo Molnar#ifdef CONFIG_PREEMPT 588905a36a2SIngo Molnar /* Interrupts are off */ 589905a36a2SIngo Molnar /* Check if we need preemption */ 5904d732138SIngo Molnar bt $9, EFLAGS(%rsp) /* were interrupts off? */ 591905a36a2SIngo Molnar jnc 1f 592905a36a2SIngo Molnar0: cmpl $0, PER_CPU_VAR(__preempt_count) 593905a36a2SIngo Molnar jnz 1f 594905a36a2SIngo Molnar call preempt_schedule_irq 595905a36a2SIngo Molnar jmp 0b 596905a36a2SIngo Molnar1: 597905a36a2SIngo Molnar#endif 598905a36a2SIngo Molnar /* 599905a36a2SIngo Molnar * The iretq could re-enable interrupts: 600905a36a2SIngo Molnar */ 601905a36a2SIngo Molnar TRACE_IRQS_IRETQ 602905a36a2SIngo Molnar 603905a36a2SIngo Molnar/* 604905a36a2SIngo Molnar * At this label, code paths which return to kernel and to user, 605905a36a2SIngo Molnar * which come from interrupts/exception and from syscalls, merge. 606905a36a2SIngo Molnar */ 607905a36a2SIngo Molnarrestore_c_regs_and_iret: 608905a36a2SIngo Molnar RESTORE_C_REGS 609905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 8 610905a36a2SIngo Molnar INTERRUPT_RETURN 611905a36a2SIngo Molnar 612905a36a2SIngo MolnarENTRY(native_iret) 613905a36a2SIngo Molnar /* 614905a36a2SIngo Molnar * Are we returning to a stack segment from the LDT? Note: in 615905a36a2SIngo Molnar * 64-bit mode SS:RSP on the exception stack is always valid. 616905a36a2SIngo Molnar */ 617905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 618905a36a2SIngo Molnar testb $4, (SS-RIP)(%rsp) 619905a36a2SIngo Molnar jnz native_irq_return_ldt 620905a36a2SIngo Molnar#endif 621905a36a2SIngo Molnar 622905a36a2SIngo Molnar.global native_irq_return_iret 623905a36a2SIngo Molnarnative_irq_return_iret: 624905a36a2SIngo Molnar /* 625905a36a2SIngo Molnar * This may fault. Non-paranoid faults on return to userspace are 626905a36a2SIngo Molnar * handled by fixup_bad_iret. These include #SS, #GP, and #NP. 627905a36a2SIngo Molnar * Double-faults due to espfix64 are handled in do_double_fault. 628905a36a2SIngo Molnar * Other faults here are fatal. 629905a36a2SIngo Molnar */ 630905a36a2SIngo Molnar iretq 631905a36a2SIngo Molnar 632905a36a2SIngo Molnar#ifdef CONFIG_X86_ESPFIX64 633905a36a2SIngo Molnarnative_irq_return_ldt: 634905a36a2SIngo Molnar pushq %rax 635905a36a2SIngo Molnar pushq %rdi 636905a36a2SIngo Molnar SWAPGS 637905a36a2SIngo Molnar movq PER_CPU_VAR(espfix_waddr), %rdi 638905a36a2SIngo Molnar movq %rax, (0*8)(%rdi) /* RAX */ 639905a36a2SIngo Molnar movq (2*8)(%rsp), %rax /* RIP */ 640905a36a2SIngo Molnar movq %rax, (1*8)(%rdi) 641905a36a2SIngo Molnar movq (3*8)(%rsp), %rax /* CS */ 642905a36a2SIngo Molnar movq %rax, (2*8)(%rdi) 643905a36a2SIngo Molnar movq (4*8)(%rsp), %rax /* RFLAGS */ 644905a36a2SIngo Molnar movq %rax, (3*8)(%rdi) 645905a36a2SIngo Molnar movq (6*8)(%rsp), %rax /* SS */ 646905a36a2SIngo Molnar movq %rax, (5*8)(%rdi) 647905a36a2SIngo Molnar movq (5*8)(%rsp), %rax /* RSP */ 648905a36a2SIngo Molnar movq %rax, (4*8)(%rdi) 649905a36a2SIngo Molnar andl $0xffff0000, %eax 650905a36a2SIngo Molnar popq %rdi 651905a36a2SIngo Molnar orq PER_CPU_VAR(espfix_stack), %rax 652905a36a2SIngo Molnar SWAPGS 653905a36a2SIngo Molnar movq %rax, %rsp 654905a36a2SIngo Molnar popq %rax 655905a36a2SIngo Molnar jmp native_irq_return_iret 656905a36a2SIngo Molnar#endif 657905a36a2SIngo Molnar 658905a36a2SIngo Molnar /* edi: workmask, edx: work */ 659905a36a2SIngo Molnarretint_careful: 660905a36a2SIngo Molnar bt $TIF_NEED_RESCHED, %edx 661905a36a2SIngo Molnar jnc retint_signal 662905a36a2SIngo Molnar TRACE_IRQS_ON 663905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 664905a36a2SIngo Molnar pushq %rdi 665905a36a2SIngo Molnar SCHEDULE_USER 666905a36a2SIngo Molnar popq %rdi 667905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 668905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 669905a36a2SIngo Molnar TRACE_IRQS_OFF 670905a36a2SIngo Molnar jmp retint_check 671905a36a2SIngo Molnar 672905a36a2SIngo Molnarretint_signal: 673905a36a2SIngo Molnar testl $_TIF_DO_NOTIFY_MASK, %edx 674905a36a2SIngo Molnar jz retint_swapgs 675905a36a2SIngo Molnar TRACE_IRQS_ON 676905a36a2SIngo Molnar ENABLE_INTERRUPTS(CLBR_NONE) 677905a36a2SIngo Molnar SAVE_EXTRA_REGS 678905a36a2SIngo Molnar movq $-1, ORIG_RAX(%rsp) 6794d732138SIngo Molnar xorl %esi, %esi /* oldset */ 6804d732138SIngo Molnar movq %rsp, %rdi /* &pt_regs */ 681905a36a2SIngo Molnar call do_notify_resume 682905a36a2SIngo Molnar RESTORE_EXTRA_REGS 683905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 684905a36a2SIngo Molnar TRACE_IRQS_OFF 685905a36a2SIngo Molnar GET_THREAD_INFO(%rcx) 686905a36a2SIngo Molnar jmp retint_with_reschedule 687905a36a2SIngo Molnar 688905a36a2SIngo MolnarEND(common_interrupt) 689905a36a2SIngo Molnar 690905a36a2SIngo Molnar/* 691905a36a2SIngo Molnar * APIC interrupts. 692905a36a2SIngo Molnar */ 693905a36a2SIngo Molnar.macro apicinterrupt3 num sym do_sym 694905a36a2SIngo MolnarENTRY(\sym) 695905a36a2SIngo Molnar ASM_CLAC 696905a36a2SIngo Molnar pushq $~(\num) 697905a36a2SIngo Molnar.Lcommon_\sym: 698905a36a2SIngo Molnar interrupt \do_sym 699905a36a2SIngo Molnar jmp ret_from_intr 700905a36a2SIngo MolnarEND(\sym) 701905a36a2SIngo Molnar.endm 702905a36a2SIngo Molnar 703905a36a2SIngo Molnar#ifdef CONFIG_TRACING 704905a36a2SIngo Molnar#define trace(sym) trace_##sym 705905a36a2SIngo Molnar#define smp_trace(sym) smp_trace_##sym 706905a36a2SIngo Molnar 707905a36a2SIngo Molnar.macro trace_apicinterrupt num sym 708905a36a2SIngo Molnarapicinterrupt3 \num trace(\sym) smp_trace(\sym) 709905a36a2SIngo Molnar.endm 710905a36a2SIngo Molnar#else 711905a36a2SIngo Molnar.macro trace_apicinterrupt num sym do_sym 712905a36a2SIngo Molnar.endm 713905a36a2SIngo Molnar#endif 714905a36a2SIngo Molnar 715905a36a2SIngo Molnar.macro apicinterrupt num sym do_sym 716905a36a2SIngo Molnarapicinterrupt3 \num \sym \do_sym 717905a36a2SIngo Molnartrace_apicinterrupt \num \sym 718905a36a2SIngo Molnar.endm 719905a36a2SIngo Molnar 720905a36a2SIngo Molnar#ifdef CONFIG_SMP 7214d732138SIngo Molnarapicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt 7224d732138SIngo Molnarapicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt 723905a36a2SIngo Molnar#endif 724905a36a2SIngo Molnar 725905a36a2SIngo Molnar#ifdef CONFIG_X86_UV 7264d732138SIngo Molnarapicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt 727905a36a2SIngo Molnar#endif 7284d732138SIngo Molnar 7294d732138SIngo Molnarapicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt 7304d732138SIngo Molnarapicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi 731905a36a2SIngo Molnar 732905a36a2SIngo Molnar#ifdef CONFIG_HAVE_KVM 7334d732138SIngo Molnarapicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi 7344d732138SIngo Molnarapicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi 735905a36a2SIngo Molnar#endif 736905a36a2SIngo Molnar 737905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE_THRESHOLD 7384d732138SIngo Molnarapicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt 739905a36a2SIngo Molnar#endif 740905a36a2SIngo Molnar 7419dda1658SIngo Molnar#ifdef CONFIG_X86_MCE_AMD 7424d732138SIngo Molnarapicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt 7439dda1658SIngo Molnar#endif 7449dda1658SIngo Molnar 745905a36a2SIngo Molnar#ifdef CONFIG_X86_THERMAL_VECTOR 7464d732138SIngo Molnarapicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt 747905a36a2SIngo Molnar#endif 748905a36a2SIngo Molnar 749905a36a2SIngo Molnar#ifdef CONFIG_SMP 7504d732138SIngo Molnarapicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt 7514d732138SIngo Molnarapicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt 7524d732138SIngo Molnarapicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt 753905a36a2SIngo Molnar#endif 754905a36a2SIngo Molnar 7554d732138SIngo Molnarapicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt 7564d732138SIngo Molnarapicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt 757905a36a2SIngo Molnar 758905a36a2SIngo Molnar#ifdef CONFIG_IRQ_WORK 7594d732138SIngo Molnarapicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt 760905a36a2SIngo Molnar#endif 761905a36a2SIngo Molnar 762905a36a2SIngo Molnar/* 763905a36a2SIngo Molnar * Exception entry points. 764905a36a2SIngo Molnar */ 765905a36a2SIngo Molnar#define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8) 766905a36a2SIngo Molnar 767905a36a2SIngo Molnar.macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1 768905a36a2SIngo MolnarENTRY(\sym) 769905a36a2SIngo Molnar /* Sanity check */ 770905a36a2SIngo Molnar .if \shift_ist != -1 && \paranoid == 0 771905a36a2SIngo Molnar .error "using shift_ist requires paranoid=1" 772905a36a2SIngo Molnar .endif 773905a36a2SIngo Molnar 774905a36a2SIngo Molnar ASM_CLAC 775905a36a2SIngo Molnar PARAVIRT_ADJUST_EXCEPTION_FRAME 776905a36a2SIngo Molnar 777905a36a2SIngo Molnar .ifeq \has_error_code 778905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 779905a36a2SIngo Molnar .endif 780905a36a2SIngo Molnar 781905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 782905a36a2SIngo Molnar 783905a36a2SIngo Molnar .if \paranoid 784905a36a2SIngo Molnar .if \paranoid == 1 7854d732138SIngo Molnar testb $3, CS(%rsp) /* If coming from userspace, switch stacks */ 7864d732138SIngo Molnar jnz 1f 787905a36a2SIngo Molnar .endif 788905a36a2SIngo Molnar call paranoid_entry 789905a36a2SIngo Molnar .else 790905a36a2SIngo Molnar call error_entry 791905a36a2SIngo Molnar .endif 792905a36a2SIngo Molnar /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */ 793905a36a2SIngo Molnar 794905a36a2SIngo Molnar .if \paranoid 795905a36a2SIngo Molnar .if \shift_ist != -1 796905a36a2SIngo Molnar TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */ 797905a36a2SIngo Molnar .else 798905a36a2SIngo Molnar TRACE_IRQS_OFF 799905a36a2SIngo Molnar .endif 800905a36a2SIngo Molnar .endif 801905a36a2SIngo Molnar 802905a36a2SIngo Molnar movq %rsp, %rdi /* pt_regs pointer */ 803905a36a2SIngo Molnar 804905a36a2SIngo Molnar .if \has_error_code 805905a36a2SIngo Molnar movq ORIG_RAX(%rsp), %rsi /* get error code */ 806905a36a2SIngo Molnar movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 807905a36a2SIngo Molnar .else 808905a36a2SIngo Molnar xorl %esi, %esi /* no error code */ 809905a36a2SIngo Molnar .endif 810905a36a2SIngo Molnar 811905a36a2SIngo Molnar .if \shift_ist != -1 812905a36a2SIngo Molnar subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist) 813905a36a2SIngo Molnar .endif 814905a36a2SIngo Molnar 815905a36a2SIngo Molnar call \do_sym 816905a36a2SIngo Molnar 817905a36a2SIngo Molnar .if \shift_ist != -1 818905a36a2SIngo Molnar addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist) 819905a36a2SIngo Molnar .endif 820905a36a2SIngo Molnar 821905a36a2SIngo Molnar /* these procedures expect "no swapgs" flag in ebx */ 822905a36a2SIngo Molnar .if \paranoid 823905a36a2SIngo Molnar jmp paranoid_exit 824905a36a2SIngo Molnar .else 825905a36a2SIngo Molnar jmp error_exit 826905a36a2SIngo Molnar .endif 827905a36a2SIngo Molnar 828905a36a2SIngo Molnar .if \paranoid == 1 829905a36a2SIngo Molnar /* 830905a36a2SIngo Molnar * Paranoid entry from userspace. Switch stacks and treat it 831905a36a2SIngo Molnar * as a normal entry. This means that paranoid handlers 832905a36a2SIngo Molnar * run in real process context if user_mode(regs). 833905a36a2SIngo Molnar */ 834905a36a2SIngo Molnar1: 835905a36a2SIngo Molnar call error_entry 836905a36a2SIngo Molnar 837905a36a2SIngo Molnar 838905a36a2SIngo Molnar movq %rsp, %rdi /* pt_regs pointer */ 839905a36a2SIngo Molnar call sync_regs 840905a36a2SIngo Molnar movq %rax, %rsp /* switch stack */ 841905a36a2SIngo Molnar 842905a36a2SIngo Molnar movq %rsp, %rdi /* pt_regs pointer */ 843905a36a2SIngo Molnar 844905a36a2SIngo Molnar .if \has_error_code 845905a36a2SIngo Molnar movq ORIG_RAX(%rsp), %rsi /* get error code */ 846905a36a2SIngo Molnar movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */ 847905a36a2SIngo Molnar .else 848905a36a2SIngo Molnar xorl %esi, %esi /* no error code */ 849905a36a2SIngo Molnar .endif 850905a36a2SIngo Molnar 851905a36a2SIngo Molnar call \do_sym 852905a36a2SIngo Molnar 853905a36a2SIngo Molnar jmp error_exit /* %ebx: no swapgs flag */ 854905a36a2SIngo Molnar .endif 855905a36a2SIngo MolnarEND(\sym) 856905a36a2SIngo Molnar.endm 857905a36a2SIngo Molnar 858905a36a2SIngo Molnar#ifdef CONFIG_TRACING 859905a36a2SIngo Molnar.macro trace_idtentry sym do_sym has_error_code:req 860905a36a2SIngo Molnaridtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code 861905a36a2SIngo Molnaridtentry \sym \do_sym has_error_code=\has_error_code 862905a36a2SIngo Molnar.endm 863905a36a2SIngo Molnar#else 864905a36a2SIngo Molnar.macro trace_idtentry sym do_sym has_error_code:req 865905a36a2SIngo Molnaridtentry \sym \do_sym has_error_code=\has_error_code 866905a36a2SIngo Molnar.endm 867905a36a2SIngo Molnar#endif 868905a36a2SIngo Molnar 869905a36a2SIngo Molnaridtentry divide_error do_divide_error has_error_code=0 870905a36a2SIngo Molnaridtentry overflow do_overflow has_error_code=0 871905a36a2SIngo Molnaridtentry bounds do_bounds has_error_code=0 872905a36a2SIngo Molnaridtentry invalid_op do_invalid_op has_error_code=0 873905a36a2SIngo Molnaridtentry device_not_available do_device_not_available has_error_code=0 874905a36a2SIngo Molnaridtentry double_fault do_double_fault has_error_code=1 paranoid=2 875905a36a2SIngo Molnaridtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0 876905a36a2SIngo Molnaridtentry invalid_TSS do_invalid_TSS has_error_code=1 877905a36a2SIngo Molnaridtentry segment_not_present do_segment_not_present has_error_code=1 878905a36a2SIngo Molnaridtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0 879905a36a2SIngo Molnaridtentry coprocessor_error do_coprocessor_error has_error_code=0 880905a36a2SIngo Molnaridtentry alignment_check do_alignment_check has_error_code=1 881905a36a2SIngo Molnaridtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0 882905a36a2SIngo Molnar 883905a36a2SIngo Molnar 8844d732138SIngo Molnar /* 8854d732138SIngo Molnar * Reload gs selector with exception handling 8864d732138SIngo Molnar * edi: new selector 8874d732138SIngo Molnar */ 888905a36a2SIngo MolnarENTRY(native_load_gs_index) 889905a36a2SIngo Molnar pushfq 890905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI) 891905a36a2SIngo Molnar SWAPGS 892905a36a2SIngo Molnargs_change: 893905a36a2SIngo Molnar movl %edi, %gs 894905a36a2SIngo Molnar2: mfence /* workaround */ 895905a36a2SIngo Molnar SWAPGS 896905a36a2SIngo Molnar popfq 897905a36a2SIngo Molnar ret 898905a36a2SIngo MolnarEND(native_load_gs_index) 899905a36a2SIngo Molnar 900905a36a2SIngo Molnar _ASM_EXTABLE(gs_change, bad_gs) 901905a36a2SIngo Molnar .section .fixup, "ax" 902905a36a2SIngo Molnar /* running with kernelgs */ 903905a36a2SIngo Molnarbad_gs: 904905a36a2SIngo Molnar SWAPGS /* switch back to user gs */ 905905a36a2SIngo Molnar xorl %eax, %eax 906905a36a2SIngo Molnar movl %eax, %gs 907905a36a2SIngo Molnar jmp 2b 908905a36a2SIngo Molnar .previous 909905a36a2SIngo Molnar 910905a36a2SIngo Molnar/* Call softirq on interrupt stack. Interrupts are off. */ 911905a36a2SIngo MolnarENTRY(do_softirq_own_stack) 912905a36a2SIngo Molnar pushq %rbp 913905a36a2SIngo Molnar mov %rsp, %rbp 914905a36a2SIngo Molnar incl PER_CPU_VAR(irq_count) 915905a36a2SIngo Molnar cmove PER_CPU_VAR(irq_stack_ptr), %rsp 9164d732138SIngo Molnar push %rbp /* frame pointer backlink */ 917905a36a2SIngo Molnar call __do_softirq 918905a36a2SIngo Molnar leaveq 919905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 920905a36a2SIngo Molnar ret 921905a36a2SIngo MolnarEND(do_softirq_own_stack) 922905a36a2SIngo Molnar 923905a36a2SIngo Molnar#ifdef CONFIG_XEN 924905a36a2SIngo Molnaridtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0 925905a36a2SIngo Molnar 926905a36a2SIngo Molnar/* 927905a36a2SIngo Molnar * A note on the "critical region" in our callback handler. 928905a36a2SIngo Molnar * We want to avoid stacking callback handlers due to events occurring 929905a36a2SIngo Molnar * during handling of the last event. To do this, we keep events disabled 930905a36a2SIngo Molnar * until we've done all processing. HOWEVER, we must enable events before 931905a36a2SIngo Molnar * popping the stack frame (can't be done atomically) and so it would still 932905a36a2SIngo Molnar * be possible to get enough handler activations to overflow the stack. 933905a36a2SIngo Molnar * Although unlikely, bugs of that kind are hard to track down, so we'd 934905a36a2SIngo Molnar * like to avoid the possibility. 935905a36a2SIngo Molnar * So, on entry to the handler we detect whether we interrupted an 936905a36a2SIngo Molnar * existing activation in its critical region -- if so, we pop the current 937905a36a2SIngo Molnar * activation and restart the handler using the previous one. 938905a36a2SIngo Molnar */ 9394d732138SIngo MolnarENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */ 9404d732138SIngo Molnar 941905a36a2SIngo Molnar/* 942905a36a2SIngo Molnar * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will 943905a36a2SIngo Molnar * see the correct pointer to the pt_regs 944905a36a2SIngo Molnar */ 9454d732138SIngo Molnar movq %rdi, %rsp /* we don't return, adjust the stack frame */ 946905a36a2SIngo Molnar11: incl PER_CPU_VAR(irq_count) 947905a36a2SIngo Molnar movq %rsp, %rbp 948905a36a2SIngo Molnar cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp 9494d732138SIngo Molnar pushq %rbp /* frame pointer backlink */ 950905a36a2SIngo Molnar call xen_evtchn_do_upcall 951905a36a2SIngo Molnar popq %rsp 952905a36a2SIngo Molnar decl PER_CPU_VAR(irq_count) 953905a36a2SIngo Molnar#ifndef CONFIG_PREEMPT 954905a36a2SIngo Molnar call xen_maybe_preempt_hcall 955905a36a2SIngo Molnar#endif 956905a36a2SIngo Molnar jmp error_exit 957905a36a2SIngo MolnarEND(xen_do_hypervisor_callback) 958905a36a2SIngo Molnar 959905a36a2SIngo Molnar/* 960905a36a2SIngo Molnar * Hypervisor uses this for application faults while it executes. 961905a36a2SIngo Molnar * We get here for two reasons: 962905a36a2SIngo Molnar * 1. Fault while reloading DS, ES, FS or GS 963905a36a2SIngo Molnar * 2. Fault while executing IRET 964905a36a2SIngo Molnar * Category 1 we do not need to fix up as Xen has already reloaded all segment 965905a36a2SIngo Molnar * registers that could be reloaded and zeroed the others. 966905a36a2SIngo Molnar * Category 2 we fix up by killing the current process. We cannot use the 967905a36a2SIngo Molnar * normal Linux return path in this case because if we use the IRET hypercall 968905a36a2SIngo Molnar * to pop the stack frame we end up in an infinite loop of failsafe callbacks. 969905a36a2SIngo Molnar * We distinguish between categories by comparing each saved segment register 970905a36a2SIngo Molnar * with its current contents: any discrepancy means we in category 1. 971905a36a2SIngo Molnar */ 972905a36a2SIngo MolnarENTRY(xen_failsafe_callback) 973905a36a2SIngo Molnar movl %ds, %ecx 974905a36a2SIngo Molnar cmpw %cx, 0x10(%rsp) 975905a36a2SIngo Molnar jne 1f 976905a36a2SIngo Molnar movl %es, %ecx 977905a36a2SIngo Molnar cmpw %cx, 0x18(%rsp) 978905a36a2SIngo Molnar jne 1f 979905a36a2SIngo Molnar movl %fs, %ecx 980905a36a2SIngo Molnar cmpw %cx, 0x20(%rsp) 981905a36a2SIngo Molnar jne 1f 982905a36a2SIngo Molnar movl %gs, %ecx 983905a36a2SIngo Molnar cmpw %cx, 0x28(%rsp) 984905a36a2SIngo Molnar jne 1f 985905a36a2SIngo Molnar /* All segments match their saved values => Category 2 (Bad IRET). */ 986905a36a2SIngo Molnar movq (%rsp), %rcx 987905a36a2SIngo Molnar movq 8(%rsp), %r11 988905a36a2SIngo Molnar addq $0x30, %rsp 989905a36a2SIngo Molnar pushq $0 /* RIP */ 990905a36a2SIngo Molnar pushq %r11 991905a36a2SIngo Molnar pushq %rcx 992905a36a2SIngo Molnar jmp general_protection 993905a36a2SIngo Molnar1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */ 994905a36a2SIngo Molnar movq (%rsp), %rcx 995905a36a2SIngo Molnar movq 8(%rsp), %r11 996905a36a2SIngo Molnar addq $0x30, %rsp 997905a36a2SIngo Molnar pushq $-1 /* orig_ax = -1 => not a system call */ 998905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 999905a36a2SIngo Molnar SAVE_C_REGS 1000905a36a2SIngo Molnar SAVE_EXTRA_REGS 1001905a36a2SIngo Molnar jmp error_exit 1002905a36a2SIngo MolnarEND(xen_failsafe_callback) 1003905a36a2SIngo Molnar 1004905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 1005905a36a2SIngo Molnar xen_hvm_callback_vector xen_evtchn_do_upcall 1006905a36a2SIngo Molnar 1007905a36a2SIngo Molnar#endif /* CONFIG_XEN */ 1008905a36a2SIngo Molnar 1009905a36a2SIngo Molnar#if IS_ENABLED(CONFIG_HYPERV) 1010905a36a2SIngo Molnarapicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \ 1011905a36a2SIngo Molnar hyperv_callback_vector hyperv_vector_handler 1012905a36a2SIngo Molnar#endif /* CONFIG_HYPERV */ 1013905a36a2SIngo Molnar 1014905a36a2SIngo Molnaridtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK 1015905a36a2SIngo Molnaridtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK 1016905a36a2SIngo Molnaridtentry stack_segment do_stack_segment has_error_code=1 10174d732138SIngo Molnar 1018905a36a2SIngo Molnar#ifdef CONFIG_XEN 1019905a36a2SIngo Molnaridtentry xen_debug do_debug has_error_code=0 1020905a36a2SIngo Molnaridtentry xen_int3 do_int3 has_error_code=0 1021905a36a2SIngo Molnaridtentry xen_stack_segment do_stack_segment has_error_code=1 1022905a36a2SIngo Molnar#endif 10234d732138SIngo Molnar 1024905a36a2SIngo Molnaridtentry general_protection do_general_protection has_error_code=1 1025905a36a2SIngo Molnartrace_idtentry page_fault do_page_fault has_error_code=1 10264d732138SIngo Molnar 1027905a36a2SIngo Molnar#ifdef CONFIG_KVM_GUEST 1028905a36a2SIngo Molnaridtentry async_page_fault do_async_page_fault has_error_code=1 1029905a36a2SIngo Molnar#endif 10304d732138SIngo Molnar 1031905a36a2SIngo Molnar#ifdef CONFIG_X86_MCE 1032905a36a2SIngo Molnaridtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip) 1033905a36a2SIngo Molnar#endif 1034905a36a2SIngo Molnar 1035905a36a2SIngo Molnar/* 1036905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed. 1037905a36a2SIngo Molnar * Use slow, but surefire "are we in kernel?" check. 1038905a36a2SIngo Molnar * Return: ebx=0: need swapgs on exit, ebx=1: otherwise 1039905a36a2SIngo Molnar */ 1040905a36a2SIngo MolnarENTRY(paranoid_entry) 1041905a36a2SIngo Molnar cld 1042905a36a2SIngo Molnar SAVE_C_REGS 8 1043905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 1044905a36a2SIngo Molnar movl $1, %ebx 1045905a36a2SIngo Molnar movl $MSR_GS_BASE, %ecx 1046905a36a2SIngo Molnar rdmsr 1047905a36a2SIngo Molnar testl %edx, %edx 1048905a36a2SIngo Molnar js 1f /* negative -> in kernel */ 1049905a36a2SIngo Molnar SWAPGS 1050905a36a2SIngo Molnar xorl %ebx, %ebx 1051905a36a2SIngo Molnar1: ret 1052905a36a2SIngo MolnarEND(paranoid_entry) 1053905a36a2SIngo Molnar 1054905a36a2SIngo Molnar/* 1055905a36a2SIngo Molnar * "Paranoid" exit path from exception stack. This is invoked 1056905a36a2SIngo Molnar * only on return from non-NMI IST interrupts that came 1057905a36a2SIngo Molnar * from kernel space. 1058905a36a2SIngo Molnar * 1059905a36a2SIngo Molnar * We may be returning to very strange contexts (e.g. very early 1060905a36a2SIngo Molnar * in syscall entry), so checking for preemption here would 1061905a36a2SIngo Molnar * be complicated. Fortunately, we there's no good reason 1062905a36a2SIngo Molnar * to try to handle preemption here. 10634d732138SIngo Molnar * 10644d732138SIngo Molnar * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it) 1065905a36a2SIngo Molnar */ 1066905a36a2SIngo MolnarENTRY(paranoid_exit) 1067905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 1068905a36a2SIngo Molnar TRACE_IRQS_OFF_DEBUG 1069905a36a2SIngo Molnar testl %ebx, %ebx /* swapgs needed? */ 1070905a36a2SIngo Molnar jnz paranoid_exit_no_swapgs 1071905a36a2SIngo Molnar TRACE_IRQS_IRETQ 1072905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1073905a36a2SIngo Molnar jmp paranoid_exit_restore 1074905a36a2SIngo Molnarparanoid_exit_no_swapgs: 1075905a36a2SIngo Molnar TRACE_IRQS_IRETQ_DEBUG 1076905a36a2SIngo Molnarparanoid_exit_restore: 1077905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1078905a36a2SIngo Molnar RESTORE_C_REGS 1079905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 8 1080905a36a2SIngo Molnar INTERRUPT_RETURN 1081905a36a2SIngo MolnarEND(paranoid_exit) 1082905a36a2SIngo Molnar 1083905a36a2SIngo Molnar/* 1084905a36a2SIngo Molnar * Save all registers in pt_regs, and switch gs if needed. 1085539f5113SAndy Lutomirski * Return: EBX=0: came from user mode; EBX=1: otherwise 1086905a36a2SIngo Molnar */ 1087905a36a2SIngo MolnarENTRY(error_entry) 1088905a36a2SIngo Molnar cld 1089905a36a2SIngo Molnar SAVE_C_REGS 8 1090905a36a2SIngo Molnar SAVE_EXTRA_REGS 8 1091905a36a2SIngo Molnar xorl %ebx, %ebx 1092905a36a2SIngo Molnar testb $3, CS+8(%rsp) 1093cb6f64edSAndy Lutomirski jz .Lerror_kernelspace 1094539f5113SAndy Lutomirski 1095cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_swapgs: 1096cb6f64edSAndy Lutomirski /* 1097cb6f64edSAndy Lutomirski * We entered from user mode or we're pretending to have entered 1098cb6f64edSAndy Lutomirski * from user mode due to an IRET fault. 1099cb6f64edSAndy Lutomirski */ 1100905a36a2SIngo Molnar SWAPGS 1101539f5113SAndy Lutomirski 1102cb6f64edSAndy Lutomirski.Lerror_entry_from_usermode_after_swapgs: 1103cb6f64edSAndy Lutomirski.Lerror_entry_done: 1104905a36a2SIngo Molnar TRACE_IRQS_OFF 1105905a36a2SIngo Molnar ret 1106905a36a2SIngo Molnar 1107905a36a2SIngo Molnar /* 1108905a36a2SIngo Molnar * There are two places in the kernel that can potentially fault with 1109905a36a2SIngo Molnar * usergs. Handle them here. B stepping K8s sometimes report a 1110905a36a2SIngo Molnar * truncated RIP for IRET exceptions returning to compat mode. Check 1111905a36a2SIngo Molnar * for these here too. 1112905a36a2SIngo Molnar */ 1113cb6f64edSAndy Lutomirski.Lerror_kernelspace: 1114905a36a2SIngo Molnar incl %ebx 1115905a36a2SIngo Molnar leaq native_irq_return_iret(%rip), %rcx 1116905a36a2SIngo Molnar cmpq %rcx, RIP+8(%rsp) 1117cb6f64edSAndy Lutomirski je .Lerror_bad_iret 1118905a36a2SIngo Molnar movl %ecx, %eax /* zero extend */ 1119905a36a2SIngo Molnar cmpq %rax, RIP+8(%rsp) 1120cb6f64edSAndy Lutomirski je .Lbstep_iret 1121905a36a2SIngo Molnar cmpq $gs_change, RIP+8(%rsp) 1122cb6f64edSAndy Lutomirski jne .Lerror_entry_done 1123539f5113SAndy Lutomirski 1124539f5113SAndy Lutomirski /* 1125539f5113SAndy Lutomirski * hack: gs_change can fail with user gsbase. If this happens, fix up 1126539f5113SAndy Lutomirski * gsbase and proceed. We'll fix up the exception and land in 1127539f5113SAndy Lutomirski * gs_change's error handler with kernel gsbase. 1128539f5113SAndy Lutomirski */ 1129cb6f64edSAndy Lutomirski jmp .Lerror_entry_from_usermode_swapgs 1130905a36a2SIngo Molnar 1131cb6f64edSAndy Lutomirski.Lbstep_iret: 1132905a36a2SIngo Molnar /* Fix truncated RIP */ 1133905a36a2SIngo Molnar movq %rcx, RIP+8(%rsp) 1134905a36a2SIngo Molnar /* fall through */ 1135905a36a2SIngo Molnar 1136cb6f64edSAndy Lutomirski.Lerror_bad_iret: 1137539f5113SAndy Lutomirski /* 1138539f5113SAndy Lutomirski * We came from an IRET to user mode, so we have user gsbase. 1139539f5113SAndy Lutomirski * Switch to kernel gsbase: 1140539f5113SAndy Lutomirski */ 1141905a36a2SIngo Molnar SWAPGS 1142539f5113SAndy Lutomirski 1143539f5113SAndy Lutomirski /* 1144539f5113SAndy Lutomirski * Pretend that the exception came from user mode: set up pt_regs 1145539f5113SAndy Lutomirski * as if we faulted immediately after IRET and clear EBX so that 1146539f5113SAndy Lutomirski * error_exit knows that we will be returning to user mode. 1147539f5113SAndy Lutomirski */ 1148905a36a2SIngo Molnar mov %rsp, %rdi 1149905a36a2SIngo Molnar call fixup_bad_iret 1150905a36a2SIngo Molnar mov %rax, %rsp 1151539f5113SAndy Lutomirski decl %ebx 1152cb6f64edSAndy Lutomirski jmp .Lerror_entry_from_usermode_after_swapgs 1153905a36a2SIngo MolnarEND(error_entry) 1154905a36a2SIngo Molnar 1155905a36a2SIngo Molnar 1156539f5113SAndy Lutomirski/* 1157539f5113SAndy Lutomirski * On entry, EBS is a "return to kernel mode" flag: 1158539f5113SAndy Lutomirski * 1: already in kernel mode, don't need SWAPGS 1159539f5113SAndy Lutomirski * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode 1160539f5113SAndy Lutomirski */ 1161905a36a2SIngo MolnarENTRY(error_exit) 1162905a36a2SIngo Molnar movl %ebx, %eax 1163905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1164905a36a2SIngo Molnar DISABLE_INTERRUPTS(CLBR_NONE) 1165905a36a2SIngo Molnar TRACE_IRQS_OFF 1166905a36a2SIngo Molnar testl %eax, %eax 1167905a36a2SIngo Molnar jnz retint_kernel 1168905a36a2SIngo Molnar jmp retint_user 1169905a36a2SIngo MolnarEND(error_exit) 1170905a36a2SIngo Molnar 1171905a36a2SIngo Molnar/* Runs on exception stack */ 1172905a36a2SIngo MolnarENTRY(nmi) 1173905a36a2SIngo Molnar PARAVIRT_ADJUST_EXCEPTION_FRAME 1174905a36a2SIngo Molnar /* 1175905a36a2SIngo Molnar * We allow breakpoints in NMIs. If a breakpoint occurs, then 1176905a36a2SIngo Molnar * the iretq it performs will take us out of NMI context. 1177905a36a2SIngo Molnar * This means that we can have nested NMIs where the next 1178905a36a2SIngo Molnar * NMI is using the top of the stack of the previous NMI. We 1179905a36a2SIngo Molnar * can't let it execute because the nested NMI will corrupt the 1180905a36a2SIngo Molnar * stack of the previous NMI. NMI handlers are not re-entrant 1181905a36a2SIngo Molnar * anyway. 1182905a36a2SIngo Molnar * 1183905a36a2SIngo Molnar * To handle this case we do the following: 1184905a36a2SIngo Molnar * Check the a special location on the stack that contains 1185905a36a2SIngo Molnar * a variable that is set when NMIs are executing. 1186905a36a2SIngo Molnar * The interrupted task's stack is also checked to see if it 1187905a36a2SIngo Molnar * is an NMI stack. 1188905a36a2SIngo Molnar * If the variable is not set and the stack is not the NMI 1189905a36a2SIngo Molnar * stack then: 1190905a36a2SIngo Molnar * o Set the special variable on the stack 1191905a36a2SIngo Molnar * o Copy the interrupt frame into a "saved" location on the stack 1192905a36a2SIngo Molnar * o Copy the interrupt frame into a "copy" location on the stack 1193905a36a2SIngo Molnar * o Continue processing the NMI 1194905a36a2SIngo Molnar * If the variable is set or the previous stack is the NMI stack: 1195905a36a2SIngo Molnar * o Modify the "copy" location to jump to the repeate_nmi 1196905a36a2SIngo Molnar * o return back to the first NMI 1197905a36a2SIngo Molnar * 1198905a36a2SIngo Molnar * Now on exit of the first NMI, we first clear the stack variable 1199905a36a2SIngo Molnar * The NMI stack will tell any nested NMIs at that point that it is 1200905a36a2SIngo Molnar * nested. Then we pop the stack normally with iret, and if there was 1201905a36a2SIngo Molnar * a nested NMI that updated the copy interrupt stack frame, a 1202905a36a2SIngo Molnar * jump will be made to the repeat_nmi code that will handle the second 1203905a36a2SIngo Molnar * NMI. 1204905a36a2SIngo Molnar */ 1205905a36a2SIngo Molnar 1206905a36a2SIngo Molnar /* Use %rdx as our temp variable throughout */ 1207905a36a2SIngo Molnar pushq %rdx 1208905a36a2SIngo Molnar 1209905a36a2SIngo Molnar /* 1210905a36a2SIngo Molnar * If %cs was not the kernel segment, then the NMI triggered in user 1211905a36a2SIngo Molnar * space, which means it is definitely not nested. 1212905a36a2SIngo Molnar */ 1213905a36a2SIngo Molnar cmpl $__KERNEL_CS, 16(%rsp) 1214905a36a2SIngo Molnar jne first_nmi 1215905a36a2SIngo Molnar 1216905a36a2SIngo Molnar /* 1217905a36a2SIngo Molnar * Check the special variable on the stack to see if NMIs are 1218905a36a2SIngo Molnar * executing. 1219905a36a2SIngo Molnar */ 1220905a36a2SIngo Molnar cmpl $1, -8(%rsp) 1221905a36a2SIngo Molnar je nested_nmi 1222905a36a2SIngo Molnar 1223905a36a2SIngo Molnar /* 1224905a36a2SIngo Molnar * Now test if the previous stack was an NMI stack. 1225905a36a2SIngo Molnar * We need the double check. We check the NMI stack to satisfy the 1226905a36a2SIngo Molnar * race when the first NMI clears the variable before returning. 1227905a36a2SIngo Molnar * We check the variable because the first NMI could be in a 1228905a36a2SIngo Molnar * breakpoint routine using a breakpoint stack. 1229905a36a2SIngo Molnar */ 1230905a36a2SIngo Molnar lea 6*8(%rsp), %rdx 1231905a36a2SIngo Molnar /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */ 1232905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1233905a36a2SIngo Molnar /* If the stack pointer is above the NMI stack, this is a normal NMI */ 1234905a36a2SIngo Molnar ja first_nmi 12354d732138SIngo Molnar 1236905a36a2SIngo Molnar subq $EXCEPTION_STKSZ, %rdx 1237905a36a2SIngo Molnar cmpq %rdx, 4*8(%rsp) 1238905a36a2SIngo Molnar /* If it is below the NMI stack, it is a normal NMI */ 1239905a36a2SIngo Molnar jb first_nmi 1240905a36a2SIngo Molnar /* Ah, it is within the NMI stack, treat it as nested */ 1241905a36a2SIngo Molnar 1242905a36a2SIngo Molnarnested_nmi: 1243905a36a2SIngo Molnar /* 1244905a36a2SIngo Molnar * Do nothing if we interrupted the fixup in repeat_nmi. 1245905a36a2SIngo Molnar * It's about to repeat the NMI handler, so we are fine 1246905a36a2SIngo Molnar * with ignoring this one. 1247905a36a2SIngo Molnar */ 1248905a36a2SIngo Molnar movq $repeat_nmi, %rdx 1249905a36a2SIngo Molnar cmpq 8(%rsp), %rdx 1250905a36a2SIngo Molnar ja 1f 1251905a36a2SIngo Molnar movq $end_repeat_nmi, %rdx 1252905a36a2SIngo Molnar cmpq 8(%rsp), %rdx 1253905a36a2SIngo Molnar ja nested_nmi_out 1254905a36a2SIngo Molnar 1255905a36a2SIngo Molnar1: 1256905a36a2SIngo Molnar /* Set up the interrupted NMIs stack to jump to repeat_nmi */ 1257905a36a2SIngo Molnar leaq -1*8(%rsp), %rdx 1258905a36a2SIngo Molnar movq %rdx, %rsp 1259905a36a2SIngo Molnar leaq -10*8(%rsp), %rdx 1260905a36a2SIngo Molnar pushq $__KERNEL_DS 1261905a36a2SIngo Molnar pushq %rdx 1262905a36a2SIngo Molnar pushfq 1263905a36a2SIngo Molnar pushq $__KERNEL_CS 1264905a36a2SIngo Molnar pushq $repeat_nmi 1265905a36a2SIngo Molnar 1266905a36a2SIngo Molnar /* Put stack back */ 1267905a36a2SIngo Molnar addq $(6*8), %rsp 1268905a36a2SIngo Molnar 1269905a36a2SIngo Molnarnested_nmi_out: 1270905a36a2SIngo Molnar popq %rdx 1271905a36a2SIngo Molnar 1272905a36a2SIngo Molnar /* No need to check faults here */ 1273905a36a2SIngo Molnar INTERRUPT_RETURN 1274905a36a2SIngo Molnar 1275905a36a2SIngo Molnarfirst_nmi: 1276905a36a2SIngo Molnar /* 1277905a36a2SIngo Molnar * Because nested NMIs will use the pushed location that we 1278905a36a2SIngo Molnar * stored in rdx, we must keep that space available. 1279905a36a2SIngo Molnar * Here's what our stack frame will look like: 1280905a36a2SIngo Molnar * +-------------------------+ 1281905a36a2SIngo Molnar * | original SS | 1282905a36a2SIngo Molnar * | original Return RSP | 1283905a36a2SIngo Molnar * | original RFLAGS | 1284905a36a2SIngo Molnar * | original CS | 1285905a36a2SIngo Molnar * | original RIP | 1286905a36a2SIngo Molnar * +-------------------------+ 1287905a36a2SIngo Molnar * | temp storage for rdx | 1288905a36a2SIngo Molnar * +-------------------------+ 1289905a36a2SIngo Molnar * | NMI executing variable | 1290905a36a2SIngo Molnar * +-------------------------+ 1291905a36a2SIngo Molnar * | copied SS | 1292905a36a2SIngo Molnar * | copied Return RSP | 1293905a36a2SIngo Molnar * | copied RFLAGS | 1294905a36a2SIngo Molnar * | copied CS | 1295905a36a2SIngo Molnar * | copied RIP | 1296905a36a2SIngo Molnar * +-------------------------+ 1297905a36a2SIngo Molnar * | Saved SS | 1298905a36a2SIngo Molnar * | Saved Return RSP | 1299905a36a2SIngo Molnar * | Saved RFLAGS | 1300905a36a2SIngo Molnar * | Saved CS | 1301905a36a2SIngo Molnar * | Saved RIP | 1302905a36a2SIngo Molnar * +-------------------------+ 1303905a36a2SIngo Molnar * | pt_regs | 1304905a36a2SIngo Molnar * +-------------------------+ 1305905a36a2SIngo Molnar * 1306905a36a2SIngo Molnar * The saved stack frame is used to fix up the copied stack frame 1307905a36a2SIngo Molnar * that a nested NMI may change to make the interrupted NMI iret jump 1308905a36a2SIngo Molnar * to the repeat_nmi. The original stack frame and the temp storage 1309905a36a2SIngo Molnar * is also used by nested NMIs and can not be trusted on exit. 1310905a36a2SIngo Molnar */ 1311905a36a2SIngo Molnar /* Do not pop rdx, nested NMIs will corrupt that part of the stack */ 1312905a36a2SIngo Molnar movq (%rsp), %rdx 1313905a36a2SIngo Molnar 1314905a36a2SIngo Molnar /* Set the NMI executing variable on the stack. */ 1315905a36a2SIngo Molnar pushq $1 1316905a36a2SIngo Molnar 13174d732138SIngo Molnar /* Leave room for the "copied" frame */ 1318905a36a2SIngo Molnar subq $(5*8), %rsp 1319905a36a2SIngo Molnar 1320905a36a2SIngo Molnar /* Copy the stack frame to the Saved frame */ 1321905a36a2SIngo Molnar .rept 5 1322905a36a2SIngo Molnar pushq 11*8(%rsp) 1323905a36a2SIngo Molnar .endr 1324905a36a2SIngo Molnar 1325905a36a2SIngo Molnar /* Everything up to here is safe from nested NMIs */ 1326905a36a2SIngo Molnar 1327905a36a2SIngo Molnar /* 1328905a36a2SIngo Molnar * If there was a nested NMI, the first NMI's iret will return 1329905a36a2SIngo Molnar * here. But NMIs are still enabled and we can take another 1330905a36a2SIngo Molnar * nested NMI. The nested NMI checks the interrupted RIP to see 1331905a36a2SIngo Molnar * if it is between repeat_nmi and end_repeat_nmi, and if so 1332905a36a2SIngo Molnar * it will just return, as we are about to repeat an NMI anyway. 1333905a36a2SIngo Molnar * This makes it safe to copy to the stack frame that a nested 1334905a36a2SIngo Molnar * NMI will update. 1335905a36a2SIngo Molnar */ 1336905a36a2SIngo Molnarrepeat_nmi: 1337905a36a2SIngo Molnar /* 1338905a36a2SIngo Molnar * Update the stack variable to say we are still in NMI (the update 1339905a36a2SIngo Molnar * is benign for the non-repeat case, where 1 was pushed just above 1340905a36a2SIngo Molnar * to this very stack slot). 1341905a36a2SIngo Molnar */ 1342905a36a2SIngo Molnar movq $1, 10*8(%rsp) 1343905a36a2SIngo Molnar 1344905a36a2SIngo Molnar /* Make another copy, this one may be modified by nested NMIs */ 1345905a36a2SIngo Molnar addq $(10*8), %rsp 1346905a36a2SIngo Molnar .rept 5 1347905a36a2SIngo Molnar pushq -6*8(%rsp) 1348905a36a2SIngo Molnar .endr 1349905a36a2SIngo Molnar subq $(5*8), %rsp 1350905a36a2SIngo Molnarend_repeat_nmi: 1351905a36a2SIngo Molnar 1352905a36a2SIngo Molnar /* 1353905a36a2SIngo Molnar * Everything below this point can be preempted by a nested 1354905a36a2SIngo Molnar * NMI if the first NMI took an exception and reset our iret stack 1355905a36a2SIngo Molnar * so that we repeat another NMI. 1356905a36a2SIngo Molnar */ 1357905a36a2SIngo Molnar pushq $-1 /* ORIG_RAX: no syscall to restart */ 1358905a36a2SIngo Molnar ALLOC_PT_GPREGS_ON_STACK 1359905a36a2SIngo Molnar 1360905a36a2SIngo Molnar /* 1361905a36a2SIngo Molnar * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit 1362905a36a2SIngo Molnar * as we should not be calling schedule in NMI context. 1363905a36a2SIngo Molnar * Even with normal interrupts enabled. An NMI should not be 1364905a36a2SIngo Molnar * setting NEED_RESCHED or anything that normal interrupts and 1365905a36a2SIngo Molnar * exceptions might do. 1366905a36a2SIngo Molnar */ 1367905a36a2SIngo Molnar call paranoid_entry 1368905a36a2SIngo Molnar 1369905a36a2SIngo Molnar /* 1370905a36a2SIngo Molnar * Save off the CR2 register. If we take a page fault in the NMI then 1371905a36a2SIngo Molnar * it could corrupt the CR2 value. If the NMI preempts a page fault 1372905a36a2SIngo Molnar * handler before it was able to read the CR2 register, and then the 1373905a36a2SIngo Molnar * NMI itself takes a page fault, the page fault that was preempted 1374905a36a2SIngo Molnar * will read the information from the NMI page fault and not the 1375905a36a2SIngo Molnar * origin fault. Save it off and restore it if it changes. 1376905a36a2SIngo Molnar * Use the r12 callee-saved register. 1377905a36a2SIngo Molnar */ 1378905a36a2SIngo Molnar movq %cr2, %r12 1379905a36a2SIngo Molnar 1380905a36a2SIngo Molnar /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */ 1381905a36a2SIngo Molnar movq %rsp, %rdi 1382905a36a2SIngo Molnar movq $-1, %rsi 1383905a36a2SIngo Molnar call do_nmi 1384905a36a2SIngo Molnar 1385905a36a2SIngo Molnar /* Did the NMI take a page fault? Restore cr2 if it did */ 1386905a36a2SIngo Molnar movq %cr2, %rcx 1387905a36a2SIngo Molnar cmpq %rcx, %r12 1388905a36a2SIngo Molnar je 1f 1389905a36a2SIngo Molnar movq %r12, %cr2 1390905a36a2SIngo Molnar1: 1391905a36a2SIngo Molnar testl %ebx, %ebx /* swapgs needed? */ 1392905a36a2SIngo Molnar jnz nmi_restore 1393905a36a2SIngo Molnarnmi_swapgs: 1394905a36a2SIngo Molnar SWAPGS_UNSAFE_STACK 1395905a36a2SIngo Molnarnmi_restore: 1396905a36a2SIngo Molnar RESTORE_EXTRA_REGS 1397905a36a2SIngo Molnar RESTORE_C_REGS 1398905a36a2SIngo Molnar /* Pop the extra iret frame at once */ 1399905a36a2SIngo Molnar REMOVE_PT_GPREGS_FROM_STACK 6*8 1400905a36a2SIngo Molnar 1401905a36a2SIngo Molnar /* Clear the NMI executing stack variable */ 1402905a36a2SIngo Molnar movq $0, 5*8(%rsp) 14035ca6f70fSAndy Lutomirski INTERRUPT_RETURN 1404905a36a2SIngo MolnarEND(nmi) 1405905a36a2SIngo Molnar 1406905a36a2SIngo MolnarENTRY(ignore_sysret) 1407905a36a2SIngo Molnar mov $-ENOSYS, %eax 1408905a36a2SIngo Molnar sysret 1409905a36a2SIngo MolnarEND(ignore_sysret) 1410