/* SPDX-License-Identifier: GPL-2.0 */1/*2* Compatibility mode system call entry point for x86-64.3*4* Copyright 2000-2002 Andi Kleen, SuSE Labs.5*/6#include <asm/asm-offsets.h>7#include <asm/current.h>8#include <asm/errno.h>9#include <asm/thread_info.h>10#include <asm/segment.h>11#include <asm/irqflags.h>12#include <asm/asm.h>13#include <asm/smap.h>14#include <asm/nospec-branch.h>15#include <linux/linkage.h>16#include <linux/err.h>1718#include "calling.h"1920.section .entry.text, "ax"2122/*23* 32-bit SYSENTER entry.24*25* 32-bit system calls through the vDSO's __kernel_vsyscall enter here26* on 64-bit kernels running on Intel CPUs.27*28* The SYSENTER instruction, in principle, should *only* occur in the29* vDSO. In practice, a small number of Android devices were shipped30* with a copy of Bionic that inlined a SYSENTER instruction. This31* never happened in any of Google's Bionic versions -- it only happened32* in a narrow range of Intel-provided versions.33*34* SYSENTER loads SS, RSP, CS, and RIP from previously programmed MSRs.35* IF and VM in RFLAGS are cleared (IOW: interrupts are off).36* SYSENTER does not save anything on the stack,37* and does not save old RIP (!!!), RSP, or RFLAGS.38*39* Arguments:40* eax system call number41* ebx arg142* ecx arg243* edx arg344* esi arg445* edi arg546* ebp user stack47* 0(%ebp) arg648*/49SYM_CODE_START(entry_SYSENTER_compat)50UNWIND_HINT_ENTRY51ENDBR52/* Interrupts are off on entry. */53swapgs5455pushq %rax56SWITCH_TO_KERNEL_CR3 scratch_reg=%rax57popq %rax5859movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp6061/* Construct struct pt_regs on stack */62pushq $__USER_DS /* pt_regs->ss */63pushq $0 /* pt_regs->sp = 0 (placeholder) */6465/*66* Push flags. This is nasty. First, interrupts are currently67* off, but we need pt_regs->flags to have IF set. Second, if TS68* was set in usermode, it's still set, and we're singlestepping69* through this code. do_SYSENTER_32() will fix up IF.70*/71pushfq /* pt_regs->flags (except IF = 0) */72pushq $__USER32_CS /* pt_regs->cs */73pushq $0 /* pt_regs->ip = 0 (placeholder) */74SYM_INNER_LABEL(entry_SYSENTER_compat_after_hwframe, SYM_L_GLOBAL)7576/*77* User tracing code (ptrace or signal handlers) might assume that78* the saved RAX contains a 32-bit number when we're invoking a 32-bit79* syscall. Just in case the high bits are nonzero, zero-extend80* the syscall number. (This could almost certainly be deleted81* with no ill effects.)82*/83movl %eax, %eax8485pushq %rax /* pt_regs->orig_ax */86PUSH_AND_CLEAR_REGS rax=$-ENOSYS87UNWIND_HINT_REGS8889cld9091/*92* SYSENTER doesn't filter flags, so we need to clear NT and AC93* ourselves. To save a few cycles, we can check whether94* either was set instead of doing an unconditional popfq.95* This needs to happen before enabling interrupts so that96* we don't get preempted with NT set.97*98* If TF is set, we will single-step all the way to here -- do_debug99* will ignore all the traps. (Yes, this is slow, but so is100* single-stepping in general. This allows us to avoid having101* a more complicated code to handle the case where a user program102* forces us to single-step through the SYSENTER entry code.)103*104* NB.: .Lsysenter_fix_flags is a label with the code under it moved105* out-of-line as an optimization: NT is unlikely to be set in the106* majority of the cases and instead of polluting the I$ unnecessarily,107* we're keeping that code behind a branch which will predict as108* not-taken and therefore its instructions won't be fetched.109*/110testl $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, EFLAGS(%rsp)111jnz .Lsysenter_fix_flags112.Lsysenter_flags_fixed:113114/*115* CPU bugs mitigations mechanisms can call other functions. They116* should be invoked after making sure TF is cleared because117* single-step is ignored only for instructions inside the118* entry_SYSENTER_compat function.119*/120IBRS_ENTER121UNTRAIN_RET122CLEAR_BRANCH_HISTORY123124movq %rsp, %rdi125call do_SYSENTER_32126jmp sysret32_from_system_call127128.Lsysenter_fix_flags:129pushq $X86_EFLAGS_FIXED130popfq131jmp .Lsysenter_flags_fixed132SYM_INNER_LABEL(__end_entry_SYSENTER_compat, SYM_L_GLOBAL)133SYM_CODE_END(entry_SYSENTER_compat)134135/*136* 32-bit SYSCALL entry.137*138* 32-bit system calls through the vDSO's __kernel_vsyscall enter here139* on 64-bit kernels running on AMD CPUs.140*141* The SYSCALL instruction, in principle, should *only* occur in the142* vDSO. In practice, it appears that this really is the case.143* As evidence:144*145* - The calling convention for SYSCALL has changed several times without146* anyone noticing.147*148* - Prior to the in-kernel X86_BUG_SYSRET_SS_ATTRS fixup, anything149* user task that did SYSCALL without immediately reloading SS150* would randomly crash.151*152* - Most programmers do not directly target AMD CPUs, and the 32-bit153* SYSCALL instruction does not exist on Intel CPUs. Even on AMD154* CPUs, Linux disables the SYSCALL instruction on 32-bit kernels155* because the SYSCALL instruction in legacy/native 32-bit mode (as156* opposed to compat mode) is sufficiently poorly designed as to be157* essentially unusable.158*159* 32-bit SYSCALL saves RIP to RCX, clears RFLAGS.RF, then saves160* RFLAGS to R11, then loads new SS, CS, and RIP from previously161* programmed MSRs. RFLAGS gets masked by a value from another MSR162* (so CLD and CLAC are not needed). SYSCALL does not save anything on163* the stack and does not change RSP.164*165* Note: RFLAGS saving+masking-with-MSR happens only in Long mode166* (in legacy 32-bit mode, IF, RF and VM bits are cleared and that's it).167* Don't get confused: RFLAGS saving+masking depends on Long Mode Active bit168* (EFER.LMA=1), NOT on bitness of userspace where SYSCALL executes169* or target CS descriptor's L bit (SYSCALL does not read segment descriptors).170*171* Arguments:172* eax system call number173* ecx return address174* ebx arg1175* ebp arg2 (note: not saved in the stack frame, should not be touched)176* edx arg3177* esi arg4178* edi arg5179* esp user stack180* 0(%esp) arg6181*/182SYM_CODE_START(entry_SYSCALL_compat)183UNWIND_HINT_ENTRY184ENDBR185/* Interrupts are off on entry. */186swapgs187188/* Stash user ESP */189movl %esp, %r8d190191/* Use %rsp as scratch reg. User ESP is stashed in r8 */192SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp193194/* Switch to the kernel stack */195movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp196197SYM_INNER_LABEL(entry_SYSCALL_compat_safe_stack, SYM_L_GLOBAL)198ANNOTATE_NOENDBR199200/* Construct struct pt_regs on stack */201pushq $__USER_DS /* pt_regs->ss */202pushq %r8 /* pt_regs->sp */203pushq %r11 /* pt_regs->flags */204pushq $__USER32_CS /* pt_regs->cs */205pushq %rcx /* pt_regs->ip */206SYM_INNER_LABEL(entry_SYSCALL_compat_after_hwframe, SYM_L_GLOBAL)207movl %eax, %eax /* discard orig_ax high bits */208pushq %rax /* pt_regs->orig_ax */209PUSH_AND_CLEAR_REGS rcx=%rbp rax=$-ENOSYS210UNWIND_HINT_REGS211212IBRS_ENTER213UNTRAIN_RET214CLEAR_BRANCH_HISTORY215216movq %rsp, %rdi217call do_fast_syscall_32218219sysret32_from_system_call:220/* XEN PV guests always use IRET path */221ALTERNATIVE "testb %al, %al; jz swapgs_restore_regs_and_return_to_usermode", \222"jmp swapgs_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV223224/*225* Opportunistic SYSRET226*227* We are not going to return to userspace from the trampoline228* stack. So let's erase the thread stack right now.229*/230STACKLEAK_ERASE231232IBRS_EXIT233234movq RBX(%rsp), %rbx /* pt_regs->rbx */235movq RBP(%rsp), %rbp /* pt_regs->rbp */236movq EFLAGS(%rsp), %r11 /* pt_regs->flags (in r11) */237movq RIP(%rsp), %rcx /* pt_regs->ip (in rcx) */238addq $RAX, %rsp /* Skip r8-r15 */239popq %rax /* pt_regs->rax */240popq %rdx /* Skip pt_regs->cx */241popq %rdx /* pt_regs->dx */242popq %rsi /* pt_regs->si */243popq %rdi /* pt_regs->di */244245/*246* USERGS_SYSRET32 does:247* GSBASE = user's GS base248* EIP = ECX249* RFLAGS = R11250* CS = __USER32_CS251* SS = __USER_DS252*253* ECX will not match pt_regs->cx, but we're returning to a vDSO254* trampoline that will fix up RCX, so this is okay.255*256* R12-R15 are callee-saved, so they contain whatever was in them257* when the system call started, which is already known to user258* code. We zero R8-R10 to avoid info leaks.259*/260movq RSP-ORIG_RAX(%rsp), %rsp261SYM_INNER_LABEL(entry_SYSRETL_compat_unsafe_stack, SYM_L_GLOBAL)262ANNOTATE_NOENDBR263264/*265* The original userspace %rsp (RSP-ORIG_RAX(%rsp)) is stored266* on the process stack which is not mapped to userspace and267* not readable after we SWITCH_TO_USER_CR3. Delay the CR3268* switch until after after the last reference to the process269* stack.270*271* %r8/%r9 are zeroed before the sysret, thus safe to clobber.272*/273SWITCH_TO_USER_CR3_NOSTACK scratch_reg=%r8 scratch_reg2=%r9274275xorl %r8d, %r8d276xorl %r9d, %r9d277xorl %r10d, %r10d278swapgs279CLEAR_CPU_BUFFERS280sysretl281SYM_INNER_LABEL(entry_SYSRETL_compat_end, SYM_L_GLOBAL)282ANNOTATE_NOENDBR283int3284SYM_CODE_END(entry_SYSCALL_compat)285286/*287* int 0x80 is used by 32 bit mode as a system call entry. Normally idt entries288* point to C routines, however since this is a system call interface the branch289* history needs to be scrubbed to protect against BHI attacks, and that290* scrubbing needs to take place in assembly code prior to entering any C291* routines.292*/293SYM_CODE_START(int80_emulation)294ANNOTATE_NOENDBR295UNWIND_HINT_FUNC296CLEAR_BRANCH_HISTORY297jmp do_int80_emulation298SYM_CODE_END(int80_emulation)299300301