/* SPDX-License-Identifier: GPL-2.0 */1#include <linux/jump_label.h>2#include <asm/unwind_hints.h>3#include <asm/cpufeatures.h>4#include <asm/page_types.h>5#include <asm/percpu.h>6#include <asm/asm-offsets.h>7#include <asm/processor-flags.h>8#include <asm/ptrace-abi.h>9#include <asm/msr.h>10#include <asm/nospec-branch.h>1112/*1314x86 function call convention, 64-bit:15-------------------------------------16arguments | callee-saved | extra caller-saved | return17[callee-clobbered] | | [callee-clobbered] |18---------------------------------------------------------------------------19rdi rsi rdx rcx r8-9 | rbx rbp [*] r12-15 | r10-11 | rax, rdx [**]2021( rsp is obviously invariant across normal function calls. (gcc can 'merge'22functions when it sees tail-call optimization possibilities) rflags is23clobbered. Leftover arguments are passed over the stack frame.)2425[*] In the frame-pointers case rbp is fixed to the stack frame.2627[**] for struct return values wider than 64 bits the return convention is a28bit more complex: up to 128 bits width we return small structures29straight in rax, rdx. For structures larger than that (3 words or30larger) the caller puts a pointer to an on-stack return struct31[allocated in the caller's stack frame] into the first argument - i.e.32into rdi. All other arguments shift up by one in this case.33Fortunately this case is rare in the kernel.3435For 32-bit we have the following conventions - kernel is built with36-mregparm=3 and -freg-struct-return:3738x86 function calling convention, 32-bit:39----------------------------------------40arguments | callee-saved | extra caller-saved | return41[callee-clobbered] | | [callee-clobbered] |42-------------------------------------------------------------------------43eax edx ecx | ebx edi esi ebp [*] | <none> | eax, edx [**]4445( here too esp is obviously invariant across normal function calls. eflags46is clobbered. Leftover arguments are passed over the stack frame. )4748[*] In the frame-pointers case ebp is fixed to the stack frame.4950[**] We build with -freg-struct-return, which on 32-bit means similar51semantics as on 64-bit: edx can be used for a second return value52(i.e. covering integer and structure sizes up to 64 bits) - after that53it gets more complex and more expensive: 3-word or larger struct returns54get done in the caller's frame and the pointer to the return struct goes55into regparm0, i.e. eax - the other arguments shift up and the56function's register parameters degenerate to regparm=2 in essence.5758*/5960#ifdef CONFIG_X86_646162/*63* 64-bit system call stack frame layout defines and helpers,64* for assembly code:65*/6667.macro PUSH_REGS rdx=%rdx rcx=%rcx rax=%rax save_ret=0 unwind_hint=168.if \save_ret69pushq %rsi /* pt_regs->si */70movq 8(%rsp), %rsi /* temporarily store the return address in %rsi */71movq %rdi, 8(%rsp) /* pt_regs->di (overwriting original return address) */72/* We just clobbered the return address - use the IRET frame for unwinding: */73UNWIND_HINT_IRET_REGS offset=3*874.else75pushq %rdi /* pt_regs->di */76pushq %rsi /* pt_regs->si */77.endif78pushq \rdx /* pt_regs->dx */79pushq \rcx /* pt_regs->cx */80pushq \rax /* pt_regs->ax */81pushq %r8 /* pt_regs->r8 */82pushq %r9 /* pt_regs->r9 */83pushq %r10 /* pt_regs->r10 */84pushq %r11 /* pt_regs->r11 */85pushq %rbx /* pt_regs->rbx */86pushq %rbp /* pt_regs->rbp */87pushq %r12 /* pt_regs->r12 */88pushq %r13 /* pt_regs->r13 */89pushq %r14 /* pt_regs->r14 */90pushq %r15 /* pt_regs->r15 */9192.if \unwind_hint93UNWIND_HINT_REGS94.endif9596.if \save_ret97pushq %rsi /* return address on top of stack */98.endif99.endm100101.macro CLEAR_REGS clear_bp=1102/*103* Sanitize registers of values that a speculation attack might104* otherwise want to exploit. The lower registers are likely clobbered105* well before they could be put to use in a speculative execution106* gadget.107*/108xorl %esi, %esi /* nospec si */109xorl %edx, %edx /* nospec dx */110xorl %ecx, %ecx /* nospec cx */111xorl %r8d, %r8d /* nospec r8 */112xorl %r9d, %r9d /* nospec r9 */113xorl %r10d, %r10d /* nospec r10 */114xorl %r11d, %r11d /* nospec r11 */115xorl %ebx, %ebx /* nospec rbx */116.if \clear_bp117xorl %ebp, %ebp /* nospec rbp */118.endif119xorl %r12d, %r12d /* nospec r12 */120xorl %r13d, %r13d /* nospec r13 */121xorl %r14d, %r14d /* nospec r14 */122xorl %r15d, %r15d /* nospec r15 */123124.endm125126.macro PUSH_AND_CLEAR_REGS rdx=%rdx rcx=%rcx rax=%rax save_ret=0 clear_bp=1 unwind_hint=1127PUSH_REGS rdx=\rdx, rcx=\rcx, rax=\rax, save_ret=\save_ret unwind_hint=\unwind_hint128CLEAR_REGS clear_bp=\clear_bp129.endm130131.macro POP_REGS pop_rdi=1132popq %r15133popq %r14134popq %r13135popq %r12136popq %rbp137popq %rbx138popq %r11139popq %r10140popq %r9141popq %r8142popq %rax143popq %rcx144popq %rdx145popq %rsi146.if \pop_rdi147popq %rdi148.endif149.endm150151#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION152153/*154* MITIGATION_PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two155* halves:156*/157#define PTI_USER_PGTABLE_BIT PAGE_SHIFT158#define PTI_USER_PGTABLE_MASK (1 << PTI_USER_PGTABLE_BIT)159#define PTI_USER_PCID_BIT X86_CR3_PTI_PCID_USER_BIT160#define PTI_USER_PCID_MASK (1 << PTI_USER_PCID_BIT)161#define PTI_USER_PGTABLE_AND_PCID_MASK (PTI_USER_PCID_MASK | PTI_USER_PGTABLE_MASK)162163.macro SET_NOFLUSH_BIT reg:req164bts $X86_CR3_PCID_NOFLUSH_BIT, \reg165.endm166167.macro ADJUST_KERNEL_CR3 reg:req168ALTERNATIVE "", "SET_NOFLUSH_BIT \reg", X86_FEATURE_PCID169/* Clear PCID and "MITIGATION_PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */170andq $(~PTI_USER_PGTABLE_AND_PCID_MASK), \reg171.endm172173.macro SWITCH_TO_KERNEL_CR3 scratch_reg:req174ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI175mov %cr3, \scratch_reg176ADJUST_KERNEL_CR3 \scratch_reg177mov \scratch_reg, %cr3178.Lend_\@:179.endm180181#define THIS_CPU_user_pcid_flush_mask \182PER_CPU_VAR(cpu_tlbstate + TLB_STATE_user_pcid_flush_mask)183184.macro SWITCH_TO_USER_CR3 scratch_reg:req scratch_reg2:req185mov %cr3, \scratch_reg186187ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID188189/*190* Test if the ASID needs a flush.191*/192movq \scratch_reg, \scratch_reg2193andq $(0x7FF), \scratch_reg /* mask ASID */194bt \scratch_reg, THIS_CPU_user_pcid_flush_mask195jnc .Lnoflush_\@196197/* Flush needed, clear the bit */198btr \scratch_reg, THIS_CPU_user_pcid_flush_mask199movq \scratch_reg2, \scratch_reg200jmp .Lwrcr3_pcid_\@201202.Lnoflush_\@:203movq \scratch_reg2, \scratch_reg204SET_NOFLUSH_BIT \scratch_reg205206.Lwrcr3_pcid_\@:207/* Flip the ASID to the user version */208orq $(PTI_USER_PCID_MASK), \scratch_reg209210.Lwrcr3_\@:211/* Flip the PGD to the user version */212orq $(PTI_USER_PGTABLE_MASK), \scratch_reg213mov \scratch_reg, %cr3214.endm215216.macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req217ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI218SWITCH_TO_USER_CR3 \scratch_reg \scratch_reg2219.Lend_\@:220.endm221222.macro SWITCH_TO_USER_CR3_STACK scratch_reg:req223ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI224pushq %rax225SWITCH_TO_USER_CR3 scratch_reg=\scratch_reg scratch_reg2=%rax226popq %rax227.Lend_\@:228.endm229230.macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req231ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_PTI232movq %cr3, \scratch_reg233movq \scratch_reg, \save_reg234/*235* Test the user pagetable bit. If set, then the user page tables236* are active. If clear CR3 already has the kernel page table237* active.238*/239bt $PTI_USER_PGTABLE_BIT, \scratch_reg240jnc .Ldone_\@241242ADJUST_KERNEL_CR3 \scratch_reg243movq \scratch_reg, %cr3244245.Ldone_\@:246.endm247248/* Restore CR3 from a kernel context. May restore a user CR3 value. */249.macro PARANOID_RESTORE_CR3 scratch_reg:req save_reg:req250ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI251252/*253* If CR3 contained the kernel page tables at the paranoid exception254* entry, then there is nothing to restore as CR3 is not modified while255* handling the exception.256*/257bt $PTI_USER_PGTABLE_BIT, \save_reg258jnc .Lend_\@259260ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID261262/*263* Check if there's a pending flush for the user ASID we're264* about to set.265*/266movq \save_reg, \scratch_reg267andq $(0x7FF), \scratch_reg268btr \scratch_reg, THIS_CPU_user_pcid_flush_mask269jc .Lwrcr3_\@270271SET_NOFLUSH_BIT \save_reg272273.Lwrcr3_\@:274movq \save_reg, %cr3275.Lend_\@:276.endm277278#else /* CONFIG_MITIGATION_PAGE_TABLE_ISOLATION=n: */279280.macro SWITCH_TO_KERNEL_CR3 scratch_reg:req281.endm282.macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req283.endm284.macro SWITCH_TO_USER_CR3_STACK scratch_reg:req285.endm286.macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req287.endm288.macro PARANOID_RESTORE_CR3 scratch_reg:req save_reg:req289.endm290291#endif292293/*294* IBRS kernel mitigation for Spectre_v2.295*296* Assumes full context is established (PUSH_REGS, CR3 and GS) and it clobbers297* the regs it uses (AX, CX, DX). Must be called before the first RET298* instruction (NOTE! UNTRAIN_RET includes a RET instruction)299*300* The optional argument is used to save/restore the current value,301* which is used on the paranoid paths.302*303* Assumes x86_spec_ctrl_{base,current} to have SPEC_CTRL_IBRS set.304*/305.macro IBRS_ENTER save_reg306#ifdef CONFIG_MITIGATION_IBRS_ENTRY307ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_KERNEL_IBRS308movl $MSR_IA32_SPEC_CTRL, %ecx309310.ifnb \save_reg311rdmsr312shl $32, %rdx313or %rdx, %rax314mov %rax, \save_reg315test $SPEC_CTRL_IBRS, %eax316jz .Ldo_wrmsr_\@317lfence318jmp .Lend_\@319.Ldo_wrmsr_\@:320.endif321322movq PER_CPU_VAR(x86_spec_ctrl_current), %rdx323movl %edx, %eax324shr $32, %rdx325wrmsr326.Lend_\@:327#endif328.endm329330/*331* Similar to IBRS_ENTER, requires KERNEL GS,CR3 and clobbers (AX, CX, DX)332* regs. Must be called after the last RET.333*/334.macro IBRS_EXIT save_reg335#ifdef CONFIG_MITIGATION_IBRS_ENTRY336ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_KERNEL_IBRS337movl $MSR_IA32_SPEC_CTRL, %ecx338339.ifnb \save_reg340mov \save_reg, %rdx341.else342movq PER_CPU_VAR(x86_spec_ctrl_current), %rdx343andl $(~SPEC_CTRL_IBRS), %edx344.endif345346movl %edx, %eax347shr $32, %rdx348wrmsr349.Lend_\@:350#endif351.endm352353/*354* Mitigate Spectre v1 for conditional swapgs code paths.355*356* FENCE_SWAPGS_USER_ENTRY is used in the user entry swapgs code path, to357* prevent a speculative swapgs when coming from kernel space.358*359* FENCE_SWAPGS_KERNEL_ENTRY is used in the kernel entry non-swapgs code path,360* to prevent the swapgs from getting speculatively skipped when coming from361* user space.362*/363.macro FENCE_SWAPGS_USER_ENTRY364ALTERNATIVE "", "lfence", X86_FEATURE_FENCE_SWAPGS_USER365.endm366.macro FENCE_SWAPGS_KERNEL_ENTRY367ALTERNATIVE "", "lfence", X86_FEATURE_FENCE_SWAPGS_KERNEL368.endm369370.macro STACKLEAK_ERASE_NOCLOBBER371#ifdef CONFIG_KSTACK_ERASE372PUSH_AND_CLEAR_REGS373call stackleak_erase374POP_REGS375#endif376.endm377378.macro SAVE_AND_SET_GSBASE scratch_reg:req save_reg:req379rdgsbase \save_reg380GET_PERCPU_BASE \scratch_reg381wrgsbase \scratch_reg382.endm383384#else /* CONFIG_X86_64 */385# undef UNWIND_HINT_IRET_REGS386# define UNWIND_HINT_IRET_REGS387#endif /* !CONFIG_X86_64 */388389.macro STACKLEAK_ERASE390#ifdef CONFIG_KSTACK_ERASE391call stackleak_erase392#endif393.endm394395#ifdef CONFIG_SMP396397/*398* CPU/node NR is loaded from the limit (size) field of a special segment399* descriptor entry in GDT.400*/401.macro LOAD_CPU_AND_NODE_SEG_LIMIT reg:req402movq $__CPUNODE_SEG, \reg403lsl \reg, \reg404.endm405406/*407* Fetch the per-CPU GSBASE value for this processor and put it in @reg.408* We normally use %gs for accessing per-CPU data, but we are setting up409* %gs here and obviously can not use %gs itself to access per-CPU data.410*411* Do not use RDPID, because KVM loads guest's TSC_AUX on vm-entry and412* may not restore the host's value until the CPU returns to userspace.413* Thus the kernel would consume a guest's TSC_AUX if an NMI arrives414* while running KVM's run loop.415*/416.macro GET_PERCPU_BASE reg:req417LOAD_CPU_AND_NODE_SEG_LIMIT \reg418andq $VDSO_CPUNODE_MASK, \reg419movq __per_cpu_offset(, \reg, 8), \reg420.endm421422#else423424.macro GET_PERCPU_BASE reg:req425movq pcpu_unit_offsets(%rip), \reg426.endm427428#endif /* CONFIG_SMP */429430#ifdef CONFIG_X86_64431432/* rdi: arg1 ... normal C conventions. rax is saved/restored. */433.macro THUNK name, func434SYM_FUNC_START(\name)435ANNOTATE_NOENDBR436pushq %rbp437movq %rsp, %rbp438439pushq %rdi440pushq %rsi441pushq %rdx442pushq %rcx443pushq %rax444pushq %r8445pushq %r9446pushq %r10447pushq %r11448449call \func450451popq %r11452popq %r10453popq %r9454popq %r8455popq %rax456popq %rcx457popq %rdx458popq %rsi459popq %rdi460popq %rbp461RET462SYM_FUNC_END(\name)463_ASM_NOKPROBE(\name)464.endm465466#else /* CONFIG_X86_32 */467468/* put return address in eax (arg1) */469.macro THUNK name, func, put_ret_addr_in_eax=0470SYM_CODE_START_NOALIGN(\name)471pushl %eax472pushl %ecx473pushl %edx474475.if \put_ret_addr_in_eax476/* Place EIP in the arg1 */477movl 3*4(%esp), %eax478.endif479480call \func481popl %edx482popl %ecx483popl %eax484RET485_ASM_NOKPROBE(\name)486SYM_CODE_END(\name)487.endm488489#endif490491492