Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 #ifndef _ASM_X86_ENTRY_COMMON_H
0003 #define _ASM_X86_ENTRY_COMMON_H
0004 
0005 #include <linux/randomize_kstack.h>
0006 #include <linux/user-return-notifier.h>
0007 
0008 #include <asm/nospec-branch.h>
0009 #include <asm/io_bitmap.h>
0010 #include <asm/fpu/api.h>
0011 
0012 /* Check that the stack and regs on entry from user mode are sane. */
0013 static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs)
0014 {
0015     if (IS_ENABLED(CONFIG_DEBUG_ENTRY)) {
0016         /*
0017          * Make sure that the entry code gave us a sensible EFLAGS
0018          * register.  Native because we want to check the actual CPU
0019          * state, not the interrupt state as imagined by Xen.
0020          */
0021         unsigned long flags = native_save_fl();
0022         unsigned long mask = X86_EFLAGS_DF | X86_EFLAGS_NT;
0023 
0024         /*
0025          * For !SMAP hardware we patch out CLAC on entry.
0026          */
0027         if (boot_cpu_has(X86_FEATURE_SMAP) ||
0028             (IS_ENABLED(CONFIG_64BIT) && boot_cpu_has(X86_FEATURE_XENPV)))
0029             mask |= X86_EFLAGS_AC;
0030 
0031         WARN_ON_ONCE(flags & mask);
0032 
0033         /* We think we came from user mode. Make sure pt_regs agrees. */
0034         WARN_ON_ONCE(!user_mode(regs));
0035 
0036         /*
0037          * All entries from user mode (except #DF) should be on the
0038          * normal thread stack and should have user pt_regs in the
0039          * correct location.
0040          */
0041         WARN_ON_ONCE(!on_thread_stack());
0042         WARN_ON_ONCE(regs != task_pt_regs(current));
0043     }
0044 }
0045 #define arch_enter_from_user_mode arch_enter_from_user_mode
0046 
0047 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
0048                           unsigned long ti_work)
0049 {
0050     if (ti_work & _TIF_USER_RETURN_NOTIFY)
0051         fire_user_return_notifiers();
0052 
0053     if (unlikely(ti_work & _TIF_IO_BITMAP))
0054         tss_update_io_bitmap();
0055 
0056     fpregs_assert_state_consistent();
0057     if (unlikely(ti_work & _TIF_NEED_FPU_LOAD))
0058         switch_fpu_return();
0059 
0060 #ifdef CONFIG_COMPAT
0061     /*
0062      * Compat syscalls set TS_COMPAT.  Make sure we clear it before
0063      * returning to user mode.  We need to clear it *after* signal
0064      * handling, because syscall restart has a fixup for compat
0065      * syscalls.  The fixup is exercised by the ptrace_syscall_32
0066      * selftest.
0067      *
0068      * We also need to clear TS_REGS_POKED_I386: the 32-bit tracer
0069      * special case only applies after poking regs and before the
0070      * very next return to user mode.
0071      */
0072     current_thread_info()->status &= ~(TS_COMPAT | TS_I386_REGS_POKED);
0073 #endif
0074 
0075     /*
0076      * Ultimately, this value will get limited by KSTACK_OFFSET_MAX(),
0077      * but not enough for x86 stack utilization comfort. To keep
0078      * reasonable stack head room, reduce the maximum offset to 8 bits.
0079      *
0080      * The actual entropy will be further reduced by the compiler when
0081      * applying stack alignment constraints (see cc_stack_align4/8 in
0082      * arch/x86/Makefile), which will remove the 3 (x86_64) or 2 (ia32)
0083      * low bits from any entropy chosen here.
0084      *
0085      * Therefore, final stack offset entropy will be 5 (x86_64) or
0086      * 6 (ia32) bits.
0087      */
0088     choose_random_kstack_offset(rdtsc() & 0xFF);
0089 }
0090 #define arch_exit_to_user_mode_prepare arch_exit_to_user_mode_prepare
0091 
0092 static __always_inline void arch_exit_to_user_mode(void)
0093 {
0094     mds_user_clear_cpu_buffers();
0095 }
0096 #define arch_exit_to_user_mode arch_exit_to_user_mode
0097 
0098 #endif