xref: /linux/arch/x86/include/asm/entry-common.h (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
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 #include <asm/fred.h>
11 
12 /* Check that the stack and regs on entry from user mode are sane. */
13 static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs)
14 {
15 	if (IS_ENABLED(CONFIG_DEBUG_ENTRY)) {
16 		/*
17 		 * Make sure that the entry code gave us a sensible EFLAGS
18 		 * register.  Native because we want to check the actual CPU
19 		 * state, not the interrupt state as imagined by Xen.
20 		 */
21 		unsigned long flags = native_save_fl();
22 		unsigned long mask = X86_EFLAGS_DF | X86_EFLAGS_NT;
23 
24 		/*
25 		 * For !SMAP hardware we patch out CLAC on entry.
26 		 */
27 		if (cpu_feature_enabled(X86_FEATURE_SMAP) ||
28 		    cpu_feature_enabled(X86_FEATURE_XENPV))
29 			mask |= X86_EFLAGS_AC;
30 
31 		WARN_ON_ONCE(flags & mask);
32 
33 		/* We think we came from user mode. Make sure pt_regs agrees. */
34 		WARN_ON_ONCE(!user_mode(regs));
35 
36 		/*
37 		 * All entries from user mode (except #DF) should be on the
38 		 * normal thread stack and should have user pt_regs in the
39 		 * correct location.
40 		 */
41 		WARN_ON_ONCE(!on_thread_stack());
42 		WARN_ON_ONCE(regs != task_pt_regs(current));
43 	}
44 }
45 #define arch_enter_from_user_mode arch_enter_from_user_mode
46 
47 static inline void arch_exit_work(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 	if (unlikely(ti_work & _TIF_NEED_FPU_LOAD))
56 		switch_fpu_return();
57 }
58 
59 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
60 						  unsigned long ti_work)
61 {
62 	fpregs_assert_state_consistent();
63 
64 	if (unlikely(ti_work))
65 		arch_exit_work(ti_work);
66 
67 	fred_update_rsp0();
68 
69 #ifdef CONFIG_COMPAT
70 	/*
71 	 * Compat syscalls set TS_COMPAT.  Make sure we clear it before
72 	 * returning to user mode.  We need to clear it *after* signal
73 	 * handling, because syscall restart has a fixup for compat
74 	 * syscalls.  The fixup is exercised by the ptrace_syscall_32
75 	 * selftest.
76 	 *
77 	 * We also need to clear TS_REGS_POKED_I386: the 32-bit tracer
78 	 * special case only applies after poking regs and before the
79 	 * very next return to user mode.
80 	 */
81 	current_thread_info()->status &= ~(TS_COMPAT | TS_I386_REGS_POKED);
82 #endif
83 
84 	/* Avoid unnecessary reads of 'x86_ibpb_exit_to_user' */
85 	if (cpu_feature_enabled(X86_FEATURE_IBPB_EXIT_TO_USER) &&
86 	    this_cpu_read(x86_ibpb_exit_to_user)) {
87 		indirect_branch_prediction_barrier();
88 		this_cpu_write(x86_ibpb_exit_to_user, false);
89 	}
90 }
91 #define arch_exit_to_user_mode_prepare arch_exit_to_user_mode_prepare
92 
93 static __always_inline void arch_exit_to_user_mode(void)
94 {
95 	amd_clear_divider();
96 }
97 #define arch_exit_to_user_mode arch_exit_to_user_mode
98 
99 extern void x86_entry_from_kvm(unsigned int entry_type, unsigned int vector);
100 
101 #endif
102