18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci#ifndef _ASM_X86_ENTRY_COMMON_H
38c2ecf20Sopenharmony_ci#define _ASM_X86_ENTRY_COMMON_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/user-return-notifier.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <asm/nospec-branch.h>
88c2ecf20Sopenharmony_ci#include <asm/io_bitmap.h>
98c2ecf20Sopenharmony_ci#include <asm/fpu/api.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/* Check that the stack and regs on entry from user mode are sane. */
128c2ecf20Sopenharmony_cistatic __always_inline void arch_check_user_regs(struct pt_regs *regs)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_DEBUG_ENTRY)) {
158c2ecf20Sopenharmony_ci		/*
168c2ecf20Sopenharmony_ci		 * Make sure that the entry code gave us a sensible EFLAGS
178c2ecf20Sopenharmony_ci		 * register.  Native because we want to check the actual CPU
188c2ecf20Sopenharmony_ci		 * state, not the interrupt state as imagined by Xen.
198c2ecf20Sopenharmony_ci		 */
208c2ecf20Sopenharmony_ci		unsigned long flags = native_save_fl();
218c2ecf20Sopenharmony_ci		unsigned long mask = X86_EFLAGS_DF | X86_EFLAGS_NT;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci		/*
248c2ecf20Sopenharmony_ci		 * For !SMAP hardware we patch out CLAC on entry.
258c2ecf20Sopenharmony_ci		 */
268c2ecf20Sopenharmony_ci		if (boot_cpu_has(X86_FEATURE_SMAP) ||
278c2ecf20Sopenharmony_ci		    (IS_ENABLED(CONFIG_64BIT) && boot_cpu_has(X86_FEATURE_XENPV)))
288c2ecf20Sopenharmony_ci			mask |= X86_EFLAGS_AC;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci		WARN_ON_ONCE(flags & mask);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci		/* We think we came from user mode. Make sure pt_regs agrees. */
338c2ecf20Sopenharmony_ci		WARN_ON_ONCE(!user_mode(regs));
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci		/*
368c2ecf20Sopenharmony_ci		 * All entries from user mode (except #DF) should be on the
378c2ecf20Sopenharmony_ci		 * normal thread stack and should have user pt_regs in the
388c2ecf20Sopenharmony_ci		 * correct location.
398c2ecf20Sopenharmony_ci		 */
408c2ecf20Sopenharmony_ci		WARN_ON_ONCE(!on_thread_stack());
418c2ecf20Sopenharmony_ci		WARN_ON_ONCE(regs != task_pt_regs(current));
428c2ecf20Sopenharmony_ci	}
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci#define arch_check_user_regs arch_check_user_regs
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define ARCH_SYSCALL_EXIT_WORK		(_TIF_SINGLESTEP)
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
498c2ecf20Sopenharmony_ci						  unsigned long ti_work)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	if (ti_work & _TIF_USER_RETURN_NOTIFY)
528c2ecf20Sopenharmony_ci		fire_user_return_notifiers();
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (unlikely(ti_work & _TIF_IO_BITMAP))
558c2ecf20Sopenharmony_ci		tss_update_io_bitmap();
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	fpregs_assert_state_consistent();
588c2ecf20Sopenharmony_ci	if (unlikely(ti_work & _TIF_NEED_FPU_LOAD))
598c2ecf20Sopenharmony_ci		switch_fpu_return();
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
628c2ecf20Sopenharmony_ci	/*
638c2ecf20Sopenharmony_ci	 * Compat syscalls set TS_COMPAT.  Make sure we clear it before
648c2ecf20Sopenharmony_ci	 * returning to user mode.  We need to clear it *after* signal
658c2ecf20Sopenharmony_ci	 * handling, because syscall restart has a fixup for compat
668c2ecf20Sopenharmony_ci	 * syscalls.  The fixup is exercised by the ptrace_syscall_32
678c2ecf20Sopenharmony_ci	 * selftest.
688c2ecf20Sopenharmony_ci	 *
698c2ecf20Sopenharmony_ci	 * We also need to clear TS_REGS_POKED_I386: the 32-bit tracer
708c2ecf20Sopenharmony_ci	 * special case only applies after poking regs and before the
718c2ecf20Sopenharmony_ci	 * very next return to user mode.
728c2ecf20Sopenharmony_ci	 */
738c2ecf20Sopenharmony_ci	current_thread_info()->status &= ~(TS_COMPAT | TS_I386_REGS_POKED);
748c2ecf20Sopenharmony_ci#endif
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci#define arch_exit_to_user_mode_prepare arch_exit_to_user_mode_prepare
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic __always_inline void arch_exit_to_user_mode(void)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	amd_clear_divider();
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci#define arch_exit_to_user_mode arch_exit_to_user_mode
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#endif
85