/* 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_callee=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 */115.if \clear_callee116xorl %ebx, %ebx /* nospec rbx */117xorl %ebp, %ebp /* nospec rbp */118xorl %r12d, %r12d /* nospec r12 */119xorl %r13d, %r13d /* nospec r13 */120xorl %r14d, %r14d /* nospec r14 */121xorl %r15d, %r15d /* nospec r15 */122.endif123.endm124125.macro PUSH_AND_CLEAR_REGS rdx=%rdx rcx=%rcx rax=%rax save_ret=0 clear_callee=1 unwind_hint=1126PUSH_REGS rdx=\rdx, rcx=\rcx, rax=\rax, save_ret=\save_ret unwind_hint=\unwind_hint127CLEAR_REGS clear_callee=\clear_callee128.endm129130.macro POP_REGS pop_rdi=1131popq %r15132popq %r14133popq %r13134popq %r12135popq %rbp136popq %rbx137popq %r11138popq %r10139popq %r9140popq %r8141popq %rax142popq %rcx143popq %rdx144popq %rsi145.if \pop_rdi146popq %rdi147.endif148.endm149150#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION151152/*153* MITIGATION_PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two154* halves:155*/156#define PTI_USER_PGTABLE_BIT PAGE_SHIFT157#define PTI_USER_PGTABLE_MASK (1 << PTI_USER_PGTABLE_BIT)158#define PTI_USER_PCID_BIT X86_CR3_PTI_PCID_USER_BIT159#define PTI_USER_PCID_MASK (1 << PTI_USER_PCID_BIT)160#define PTI_USER_PGTABLE_AND_PCID_MASK (PTI_USER_PCID_MASK | PTI_USER_PGTABLE_MASK)161162.macro SET_NOFLUSH_BIT reg:req163bts $X86_CR3_PCID_NOFLUSH_BIT, \reg164.endm165166.macro ADJUST_KERNEL_CR3 reg:req167ALTERNATIVE "", "SET_NOFLUSH_BIT \reg", X86_FEATURE_PCID168/* Clear PCID and "MITIGATION_PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */169andq $(~PTI_USER_PGTABLE_AND_PCID_MASK), \reg170.endm171172.macro SWITCH_TO_KERNEL_CR3 scratch_reg:req173ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI174mov %cr3, \scratch_reg175ADJUST_KERNEL_CR3 \scratch_reg176mov \scratch_reg, %cr3177.Lend_\@:178.endm179180#define THIS_CPU_user_pcid_flush_mask \181PER_CPU_VAR(cpu_tlbstate + TLB_STATE_user_pcid_flush_mask)182183.macro SWITCH_TO_USER_CR3 scratch_reg:req scratch_reg2:req184mov %cr3, \scratch_reg185186ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID187188/*189* Test if the ASID needs a flush.190*/191movq \scratch_reg, \scratch_reg2192andq $(0x7FF), \scratch_reg /* mask ASID */193bt \scratch_reg, THIS_CPU_user_pcid_flush_mask194jnc .Lnoflush_\@195196/* Flush needed, clear the bit */197btr \scratch_reg, THIS_CPU_user_pcid_flush_mask198movq \scratch_reg2, \scratch_reg199jmp .Lwrcr3_pcid_\@200201.Lnoflush_\@:202movq \scratch_reg2, \scratch_reg203SET_NOFLUSH_BIT \scratch_reg204205.Lwrcr3_pcid_\@:206/* Flip the ASID to the user version */207orq $(PTI_USER_PCID_MASK), \scratch_reg208209.Lwrcr3_\@:210/* Flip the PGD to the user version */211orq $(PTI_USER_PGTABLE_MASK), \scratch_reg212mov \scratch_reg, %cr3213.endm214215.macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req216ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI217SWITCH_TO_USER_CR3 \scratch_reg \scratch_reg2218.Lend_\@:219.endm220221.macro SWITCH_TO_USER_CR3_STACK scratch_reg:req222ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI223pushq %rax224SWITCH_TO_USER_CR3 scratch_reg=\scratch_reg scratch_reg2=%rax225popq %rax226.Lend_\@:227.endm228229.macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req230ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_PTI231movq %cr3, \scratch_reg232movq \scratch_reg, \save_reg233/*234* Test the user pagetable bit. If set, then the user page tables235* are active. If clear CR3 already has the kernel page table236* active.237*/238bt $PTI_USER_PGTABLE_BIT, \scratch_reg239jnc .Ldone_\@240241ADJUST_KERNEL_CR3 \scratch_reg242movq \scratch_reg, %cr3243244.Ldone_\@:245.endm246247/* Restore CR3 from a kernel context. May restore a user CR3 value. */248.macro PARANOID_RESTORE_CR3 scratch_reg:req save_reg:req249ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI250251/*252* If CR3 contained the kernel page tables at the paranoid exception253* entry, then there is nothing to restore as CR3 is not modified while254* handling the exception.255*/256bt $PTI_USER_PGTABLE_BIT, \save_reg257jnc .Lend_\@258259ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID260261/*262* Check if there's a pending flush for the user ASID we're263* about to set.264*/265movq \save_reg, \scratch_reg266andq $(0x7FF), \scratch_reg267btr \scratch_reg, THIS_CPU_user_pcid_flush_mask268jc .Lwrcr3_\@269270SET_NOFLUSH_BIT \save_reg271272.Lwrcr3_\@:273movq \save_reg, %cr3274.Lend_\@:275.endm276277#else /* CONFIG_MITIGATION_PAGE_TABLE_ISOLATION=n: */278279.macro SWITCH_TO_KERNEL_CR3 scratch_reg:req280.endm281.macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req282.endm283.macro SWITCH_TO_USER_CR3_STACK scratch_reg:req284.endm285.macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req286.endm287.macro PARANOID_RESTORE_CR3 scratch_reg:req save_reg:req288.endm289290#endif291292/*293* IBRS kernel mitigation for Spectre_v2.294*295* Assumes full context is established (PUSH_REGS, CR3 and GS) and it clobbers296* the regs it uses (AX, CX, DX). Must be called before the first RET297* instruction (NOTE! UNTRAIN_RET includes a RET instruction)298*299* The optional argument is used to save/restore the current value,300* which is used on the paranoid paths.301*302* Assumes x86_spec_ctrl_{base,current} to have SPEC_CTRL_IBRS set.303*/304.macro IBRS_ENTER save_reg305#ifdef CONFIG_MITIGATION_IBRS_ENTRY306ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_KERNEL_IBRS307movl $MSR_IA32_SPEC_CTRL, %ecx308309.ifnb \save_reg310rdmsr311shl $32, %rdx312or %rdx, %rax313mov %rax, \save_reg314test $SPEC_CTRL_IBRS, %eax315jz .Ldo_wrmsr_\@316lfence317jmp .Lend_\@318.Ldo_wrmsr_\@:319.endif320321movq PER_CPU_VAR(x86_spec_ctrl_current), %rdx322movl %edx, %eax323shr $32, %rdx324wrmsr325.Lend_\@:326#endif327.endm328329/*330* Similar to IBRS_ENTER, requires KERNEL GS,CR3 and clobbers (AX, CX, DX)331* regs. Must be called after the last RET.332*/333.macro IBRS_EXIT save_reg334#ifdef CONFIG_MITIGATION_IBRS_ENTRY335ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_KERNEL_IBRS336movl $MSR_IA32_SPEC_CTRL, %ecx337338.ifnb \save_reg339mov \save_reg, %rdx340.else341movq PER_CPU_VAR(x86_spec_ctrl_current), %rdx342andl $(~SPEC_CTRL_IBRS), %edx343.endif344345movl %edx, %eax346shr $32, %rdx347wrmsr348.Lend_\@:349#endif350.endm351352/*353* Mitigate Spectre v1 for conditional swapgs code paths.354*355* FENCE_SWAPGS_USER_ENTRY is used in the user entry swapgs code path, to356* prevent a speculative swapgs when coming from kernel space.357*358* FENCE_SWAPGS_KERNEL_ENTRY is used in the kernel entry non-swapgs code path,359* to prevent the swapgs from getting speculatively skipped when coming from360* user space.361*/362.macro FENCE_SWAPGS_USER_ENTRY363ALTERNATIVE "", "lfence", X86_FEATURE_FENCE_SWAPGS_USER364.endm365.macro FENCE_SWAPGS_KERNEL_ENTRY366ALTERNATIVE "", "lfence", X86_FEATURE_FENCE_SWAPGS_KERNEL367.endm368369.macro STACKLEAK_ERASE_NOCLOBBER370#ifdef CONFIG_KSTACK_ERASE371PUSH_AND_CLEAR_REGS372call stackleak_erase373POP_REGS374#endif375.endm376377.macro SAVE_AND_SET_GSBASE scratch_reg:req save_reg:req378rdgsbase \save_reg379GET_PERCPU_BASE \scratch_reg380wrgsbase \scratch_reg381.endm382383#else /* CONFIG_X86_64 */384# undef UNWIND_HINT_IRET_REGS385# define UNWIND_HINT_IRET_REGS386#endif /* !CONFIG_X86_64 */387388.macro STACKLEAK_ERASE389#ifdef CONFIG_KSTACK_ERASE390call stackleak_erase391#endif392.endm393394#ifdef CONFIG_SMP395396/*397* CPU/node NR is loaded from the limit (size) field of a special segment398* descriptor entry in GDT.399*/400.macro LOAD_CPU_AND_NODE_SEG_LIMIT reg:req401movq $__CPUNODE_SEG, \reg402lsl \reg, \reg403.endm404405/*406* Fetch the per-CPU GSBASE value for this processor and put it in @reg.407* We normally use %gs for accessing per-CPU data, but we are setting up408* %gs here and obviously can not use %gs itself to access per-CPU data.409*410* Do not use RDPID, because KVM loads guest's TSC_AUX on vm-entry and411* may not restore the host's value until the CPU returns to userspace.412* Thus the kernel would consume a guest's TSC_AUX if an NMI arrives413* while running KVM's run loop.414*/415.macro GET_PERCPU_BASE reg:req416LOAD_CPU_AND_NODE_SEG_LIMIT \reg417andq $VDSO_CPUNODE_MASK, \reg418movq __per_cpu_offset(, \reg, 8), \reg419.endm420421#else422423.macro GET_PERCPU_BASE reg:req424movq pcpu_unit_offsets(%rip), \reg425.endm426427#endif /* CONFIG_SMP */428429#ifdef CONFIG_X86_64430431/* rdi: arg1 ... normal C conventions. rax is saved/restored. */432.macro THUNK name, func433SYM_FUNC_START(\name)434ANNOTATE_NOENDBR435pushq %rbp436movq %rsp, %rbp437438pushq %rdi439pushq %rsi440pushq %rdx441pushq %rcx442pushq %rax443pushq %r8444pushq %r9445pushq %r10446pushq %r11447448call \func449450popq %r11451popq %r10452popq %r9453popq %r8454popq %rax455popq %rcx456popq %rdx457popq %rsi458popq %rdi459popq %rbp460RET461SYM_FUNC_END(\name)462_ASM_NOKPROBE(\name)463.endm464465#else /* CONFIG_X86_32 */466467/* put return address in eax (arg1) */468.macro THUNK name, func, put_ret_addr_in_eax=0469SYM_CODE_START_NOALIGN(\name)470pushl %eax471pushl %ecx472pushl %edx473474.if \put_ret_addr_in_eax475/* Place EIP in the arg1 */476movl 3*4(%esp), %eax477.endif478479call \func480popl %edx481popl %ecx482popl %eax483RET484_ASM_NOKPROBE(\name)485SYM_CODE_END(\name)486.endm487488#endif489490491