1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _ASM_X86_ENTRY_COMMON_H
3 #define _ASM_X86_ENTRY_COMMON_H
4 
5 #include <linux/user-return-notifier.h>
6 
7 #include <asm/nospec-branch.h>
8 #include <asm/io_bitmap.h>
9 #include <asm/fpu/api.h>
10 
11 /* Check that the stack and regs on entry from user mode are sane. */
12 static __always_inline void arch_check_user_regs(struct pt_regs *regs)
13 {
14 	if (IS_ENABLED(CONFIG_DEBUG_ENTRY)) {
15 		/*
16 		 * Make sure that the entry code gave us a sensible EFLAGS
17 		 * register.  Native because we want to check the actual CPU
18 		 * state, not the interrupt state as imagined by Xen.
19 		 */
20 		unsigned long flags = native_save_fl();
21 		unsigned long mask = X86_EFLAGS_DF | X86_EFLAGS_NT;
22 
23 		/*
24 		 * For !SMAP hardware we patch out CLAC on entry.
25 		 */
26 		if (boot_cpu_has(X86_FEATURE_SMAP) ||
27 		    (IS_ENABLED(CONFIG_64_BIT) && boot_cpu_has(X86_FEATURE_XENPV)))
28 			mask |= X86_EFLAGS_AC;
29 
30 		WARN_ON_ONCE(flags & mask);
31 
32 		/* We think we came from user mode. Make sure pt_regs agrees. */
33 		WARN_ON_ONCE(!user_mode(regs));
34 
35 		/*
36 		 * All entries from user mode (except #DF) should be on the
37 		 * normal thread stack and should have user pt_regs in the
38 		 * correct location.
39 		 */
40 		WARN_ON_ONCE(!on_thread_stack());
41 		WARN_ON_ONCE(regs != task_pt_regs(current));
42 	}
43 }
44 #define arch_check_user_regs arch_check_user_regs
45 
46 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
47 						  unsigned long ti_work)
48 {
49 	if (ti_work & _TIF_USER_RETURN_NOTIFY)
50 		fire_user_return_notifiers();
51 
52 	if (unlikely(ti_work & _TIF_IO_BITMAP))
53 		tss_update_io_bitmap();
54 
55 	fpregs_assert_state_consistent();
56 	if (unlikely(ti_work & _TIF_NEED_FPU_LOAD))
57 		switch_fpu_return();
58 
59 #ifdef CONFIG_COMPAT
60 	/*
61 	 * Compat syscalls set TS_COMPAT.  Make sure we clear it before
62 	 * returning to user mode.  We need to clear it *after* signal
63 	 * handling, because syscall restart has a fixup for compat
64 	 * syscalls.  The fixup is exercised by the ptrace_syscall_32
65 	 * selftest.
66 	 *
67 	 * We also need to clear TS_REGS_POKED_I386: the 32-bit tracer
68 	 * special case only applies after poking regs and before the
69 	 * very next return to user mode.
70 	 */
71 	current_thread_info()->status &= ~(TS_COMPAT | TS_I386_REGS_POKED);
72 #endif
73 }
74 #define arch_exit_to_user_mode_prepare arch_exit_to_user_mode_prepare
75 
76 static __always_inline void arch_exit_to_user_mode(void)
77 {
78 	mds_user_clear_cpu_buffers();
79 }
80 #define arch_exit_to_user_mode arch_exit_to_user_mode
81 
82 #endif
83