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