18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/* thread_info.h: low-level thread information
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2002  David Howells (dhowells@redhat.com)
58c2ecf20Sopenharmony_ci * - Incorporating suggestions made by Linus Torvalds and Dave Miller
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _ASM_X86_THREAD_INFO_H
98c2ecf20Sopenharmony_ci#define _ASM_X86_THREAD_INFO_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/compiler.h>
128c2ecf20Sopenharmony_ci#include <asm/page.h>
138c2ecf20Sopenharmony_ci#include <asm/percpu.h>
148c2ecf20Sopenharmony_ci#include <asm/types.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci * TOP_OF_KERNEL_STACK_PADDING is a number of unused bytes that we
188c2ecf20Sopenharmony_ci * reserve at the top of the kernel stack.  We do it because of a nasty
198c2ecf20Sopenharmony_ci * 32-bit corner case.  On x86_32, the hardware stack frame is
208c2ecf20Sopenharmony_ci * variable-length.  Except for vm86 mode, struct pt_regs assumes a
218c2ecf20Sopenharmony_ci * maximum-length frame.  If we enter from CPL 0, the top 8 bytes of
228c2ecf20Sopenharmony_ci * pt_regs don't actually exist.  Ordinarily this doesn't matter, but it
238c2ecf20Sopenharmony_ci * does in at least one case:
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * If we take an NMI early enough in SYSENTER, then we can end up with
268c2ecf20Sopenharmony_ci * pt_regs that extends above sp0.  On the way out, in the espfix code,
278c2ecf20Sopenharmony_ci * we can read the saved SS value, but that value will be above sp0.
288c2ecf20Sopenharmony_ci * Without this offset, that can result in a page fault.  (We are
298c2ecf20Sopenharmony_ci * careful that, in this case, the value we read doesn't matter.)
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * In vm86 mode, the hardware frame is much longer still, so add 16
328c2ecf20Sopenharmony_ci * bytes to make room for the real-mode segments.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * x86_64 has a fixed-length stack frame.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
378c2ecf20Sopenharmony_ci# ifdef CONFIG_VM86
388c2ecf20Sopenharmony_ci#  define TOP_OF_KERNEL_STACK_PADDING 16
398c2ecf20Sopenharmony_ci# else
408c2ecf20Sopenharmony_ci#  define TOP_OF_KERNEL_STACK_PADDING 8
418c2ecf20Sopenharmony_ci# endif
428c2ecf20Sopenharmony_ci#else
438c2ecf20Sopenharmony_ci# define TOP_OF_KERNEL_STACK_PADDING 0
448c2ecf20Sopenharmony_ci#endif
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/*
478c2ecf20Sopenharmony_ci * low level task data that entry.S needs immediate access to
488c2ecf20Sopenharmony_ci * - this struct should fit entirely inside of one cache line
498c2ecf20Sopenharmony_ci * - this struct shares the supervisor stack pages
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
528c2ecf20Sopenharmony_cistruct task_struct;
538c2ecf20Sopenharmony_ci#include <asm/cpufeature.h>
548c2ecf20Sopenharmony_ci#include <linux/atomic.h>
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistruct thread_info {
578c2ecf20Sopenharmony_ci	unsigned long		flags;		/* low level flags */
588c2ecf20Sopenharmony_ci	u32			status;		/* thread synchronous flags */
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define INIT_THREAD_INFO(tsk)			\
628c2ecf20Sopenharmony_ci{						\
638c2ecf20Sopenharmony_ci	.flags		= 0,			\
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#else /* !__ASSEMBLY__ */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#include <asm/asm-offsets.h>
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#endif
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/*
738c2ecf20Sopenharmony_ci * thread information flags
748c2ecf20Sopenharmony_ci * - these are process state flags that various assembly files
758c2ecf20Sopenharmony_ci *   may need to access
768c2ecf20Sopenharmony_ci */
778c2ecf20Sopenharmony_ci#define TIF_SYSCALL_TRACE	0	/* syscall trace active */
788c2ecf20Sopenharmony_ci#define TIF_NOTIFY_RESUME	1	/* callback before returning to user */
798c2ecf20Sopenharmony_ci#define TIF_SIGPENDING		2	/* signal pending */
808c2ecf20Sopenharmony_ci#define TIF_NEED_RESCHED	3	/* rescheduling necessary */
818c2ecf20Sopenharmony_ci#define TIF_SINGLESTEP		4	/* reenable singlestep on user return*/
828c2ecf20Sopenharmony_ci#define TIF_SSBD		5	/* Speculative store bypass disable */
838c2ecf20Sopenharmony_ci#define TIF_SYSCALL_EMU		6	/* syscall emulation active */
848c2ecf20Sopenharmony_ci#define TIF_SYSCALL_AUDIT	7	/* syscall auditing active */
858c2ecf20Sopenharmony_ci#define TIF_SECCOMP		8	/* secure computing */
868c2ecf20Sopenharmony_ci#define TIF_SPEC_IB		9	/* Indirect branch speculation mitigation */
878c2ecf20Sopenharmony_ci#define TIF_SPEC_FORCE_UPDATE	10	/* Force speculation MSR update in context switch */
888c2ecf20Sopenharmony_ci#define TIF_USER_RETURN_NOTIFY	11	/* notify kernel of userspace return */
898c2ecf20Sopenharmony_ci#define TIF_UPROBE		12	/* breakpointed or singlestepping */
908c2ecf20Sopenharmony_ci#define TIF_PATCH_PENDING	13	/* pending live patching update */
918c2ecf20Sopenharmony_ci#define TIF_NEED_FPU_LOAD	14	/* load FPU on return to userspace */
928c2ecf20Sopenharmony_ci#define TIF_NOCPUID		15	/* CPUID is not accessible in userland */
938c2ecf20Sopenharmony_ci#define TIF_NOTSC		16	/* TSC is not accessible in userland */
948c2ecf20Sopenharmony_ci#define TIF_IA32		17	/* IA32 compatibility process */
958c2ecf20Sopenharmony_ci#define TIF_SLD			18	/* Restore split lock detection on context switch */
968c2ecf20Sopenharmony_ci#define TIF_NOTIFY_SIGNAL	19	/* signal notifications exist */
978c2ecf20Sopenharmony_ci#define TIF_MEMDIE		20	/* is terminating due to OOM killer */
988c2ecf20Sopenharmony_ci#define TIF_POLLING_NRFLAG	21	/* idle is polling for TIF_NEED_RESCHED */
998c2ecf20Sopenharmony_ci#define TIF_IO_BITMAP		22	/* uses I/O bitmap */
1008c2ecf20Sopenharmony_ci#define TIF_FORCED_TF		24	/* true if TF in eflags artificially */
1018c2ecf20Sopenharmony_ci#define TIF_BLOCKSTEP		25	/* set when we want DEBUGCTLMSR_BTF */
1028c2ecf20Sopenharmony_ci#define TIF_LAZY_MMU_UPDATES	27	/* task is updating the mmu lazily */
1038c2ecf20Sopenharmony_ci#define TIF_SYSCALL_TRACEPOINT	28	/* syscall tracepoint instrumentation */
1048c2ecf20Sopenharmony_ci#define TIF_ADDR32		29	/* 32-bit address space on 64 bits */
1058c2ecf20Sopenharmony_ci#define TIF_X32			30	/* 32-bit native x86-64 binary */
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci#define _TIF_SYSCALL_TRACE	(1 << TIF_SYSCALL_TRACE)
1088c2ecf20Sopenharmony_ci#define _TIF_NOTIFY_RESUME	(1 << TIF_NOTIFY_RESUME)
1098c2ecf20Sopenharmony_ci#define _TIF_SIGPENDING		(1 << TIF_SIGPENDING)
1108c2ecf20Sopenharmony_ci#define _TIF_NEED_RESCHED	(1 << TIF_NEED_RESCHED)
1118c2ecf20Sopenharmony_ci#define _TIF_SINGLESTEP		(1 << TIF_SINGLESTEP)
1128c2ecf20Sopenharmony_ci#define _TIF_SSBD		(1 << TIF_SSBD)
1138c2ecf20Sopenharmony_ci#define _TIF_SYSCALL_EMU	(1 << TIF_SYSCALL_EMU)
1148c2ecf20Sopenharmony_ci#define _TIF_SYSCALL_AUDIT	(1 << TIF_SYSCALL_AUDIT)
1158c2ecf20Sopenharmony_ci#define _TIF_SECCOMP		(1 << TIF_SECCOMP)
1168c2ecf20Sopenharmony_ci#define _TIF_SPEC_IB		(1 << TIF_SPEC_IB)
1178c2ecf20Sopenharmony_ci#define _TIF_SPEC_FORCE_UPDATE	(1 << TIF_SPEC_FORCE_UPDATE)
1188c2ecf20Sopenharmony_ci#define _TIF_USER_RETURN_NOTIFY	(1 << TIF_USER_RETURN_NOTIFY)
1198c2ecf20Sopenharmony_ci#define _TIF_UPROBE		(1 << TIF_UPROBE)
1208c2ecf20Sopenharmony_ci#define _TIF_PATCH_PENDING	(1 << TIF_PATCH_PENDING)
1218c2ecf20Sopenharmony_ci#define _TIF_NEED_FPU_LOAD	(1 << TIF_NEED_FPU_LOAD)
1228c2ecf20Sopenharmony_ci#define _TIF_NOCPUID		(1 << TIF_NOCPUID)
1238c2ecf20Sopenharmony_ci#define _TIF_NOTSC		(1 << TIF_NOTSC)
1248c2ecf20Sopenharmony_ci#define _TIF_IA32		(1 << TIF_IA32)
1258c2ecf20Sopenharmony_ci#define _TIF_NOTIFY_SIGNAL	(1 << TIF_NOTIFY_SIGNAL)
1268c2ecf20Sopenharmony_ci#define _TIF_SLD		(1 << TIF_SLD)
1278c2ecf20Sopenharmony_ci#define _TIF_POLLING_NRFLAG	(1 << TIF_POLLING_NRFLAG)
1288c2ecf20Sopenharmony_ci#define _TIF_IO_BITMAP		(1 << TIF_IO_BITMAP)
1298c2ecf20Sopenharmony_ci#define _TIF_FORCED_TF		(1 << TIF_FORCED_TF)
1308c2ecf20Sopenharmony_ci#define _TIF_BLOCKSTEP		(1 << TIF_BLOCKSTEP)
1318c2ecf20Sopenharmony_ci#define _TIF_LAZY_MMU_UPDATES	(1 << TIF_LAZY_MMU_UPDATES)
1328c2ecf20Sopenharmony_ci#define _TIF_SYSCALL_TRACEPOINT	(1 << TIF_SYSCALL_TRACEPOINT)
1338c2ecf20Sopenharmony_ci#define _TIF_ADDR32		(1 << TIF_ADDR32)
1348c2ecf20Sopenharmony_ci#define _TIF_X32		(1 << TIF_X32)
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/* flags to check in __switch_to() */
1378c2ecf20Sopenharmony_ci#define _TIF_WORK_CTXSW_BASE					\
1388c2ecf20Sopenharmony_ci	(_TIF_NOCPUID | _TIF_NOTSC | _TIF_BLOCKSTEP |		\
1398c2ecf20Sopenharmony_ci	 _TIF_SSBD | _TIF_SPEC_FORCE_UPDATE | _TIF_SLD)
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/*
1428c2ecf20Sopenharmony_ci * Avoid calls to __switch_to_xtra() on UP as STIBP is not evaluated.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
1458c2ecf20Sopenharmony_ci# define _TIF_WORK_CTXSW	(_TIF_WORK_CTXSW_BASE | _TIF_SPEC_IB)
1468c2ecf20Sopenharmony_ci#else
1478c2ecf20Sopenharmony_ci# define _TIF_WORK_CTXSW	(_TIF_WORK_CTXSW_BASE)
1488c2ecf20Sopenharmony_ci#endif
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_IOPL_IOPERM
1518c2ecf20Sopenharmony_ci# define _TIF_WORK_CTXSW_PREV	(_TIF_WORK_CTXSW| _TIF_USER_RETURN_NOTIFY | \
1528c2ecf20Sopenharmony_ci				 _TIF_IO_BITMAP)
1538c2ecf20Sopenharmony_ci#else
1548c2ecf20Sopenharmony_ci# define _TIF_WORK_CTXSW_PREV	(_TIF_WORK_CTXSW| _TIF_USER_RETURN_NOTIFY)
1558c2ecf20Sopenharmony_ci#endif
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci#define _TIF_WORK_CTXSW_NEXT	(_TIF_WORK_CTXSW)
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#define STACK_WARN		(THREAD_SIZE/8)
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/*
1628c2ecf20Sopenharmony_ci * macros/functions for gaining access to the thread information structure
1638c2ecf20Sopenharmony_ci *
1648c2ecf20Sopenharmony_ci * preempt_count needs to be 1 initially, until the scheduler is functional.
1658c2ecf20Sopenharmony_ci */
1668c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/*
1698c2ecf20Sopenharmony_ci * Walks up the stack frames to make sure that the specified object is
1708c2ecf20Sopenharmony_ci * entirely contained by a single stack frame.
1718c2ecf20Sopenharmony_ci *
1728c2ecf20Sopenharmony_ci * Returns:
1738c2ecf20Sopenharmony_ci *	GOOD_FRAME	if within a frame
1748c2ecf20Sopenharmony_ci *	BAD_STACK	if placed across a frame boundary (or outside stack)
1758c2ecf20Sopenharmony_ci *	NOT_STACK	unable to determine (no frame pointers, etc)
1768c2ecf20Sopenharmony_ci */
1778c2ecf20Sopenharmony_cistatic inline int arch_within_stack_frames(const void * const stack,
1788c2ecf20Sopenharmony_ci					   const void * const stackend,
1798c2ecf20Sopenharmony_ci					   const void *obj, unsigned long len)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci#if defined(CONFIG_FRAME_POINTER)
1828c2ecf20Sopenharmony_ci	const void *frame = NULL;
1838c2ecf20Sopenharmony_ci	const void *oldframe;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	oldframe = __builtin_frame_address(1);
1868c2ecf20Sopenharmony_ci	if (oldframe)
1878c2ecf20Sopenharmony_ci		frame = __builtin_frame_address(2);
1888c2ecf20Sopenharmony_ci	/*
1898c2ecf20Sopenharmony_ci	 * low ----------------------------------------------> high
1908c2ecf20Sopenharmony_ci	 * [saved bp][saved ip][args][local vars][saved bp][saved ip]
1918c2ecf20Sopenharmony_ci	 *                     ^----------------^
1928c2ecf20Sopenharmony_ci	 *               allow copies only within here
1938c2ecf20Sopenharmony_ci	 */
1948c2ecf20Sopenharmony_ci	while (stack <= frame && frame < stackend) {
1958c2ecf20Sopenharmony_ci		/*
1968c2ecf20Sopenharmony_ci		 * If obj + len extends past the last frame, this
1978c2ecf20Sopenharmony_ci		 * check won't pass and the next frame will be 0,
1988c2ecf20Sopenharmony_ci		 * causing us to bail out and correctly report
1998c2ecf20Sopenharmony_ci		 * the copy as invalid.
2008c2ecf20Sopenharmony_ci		 */
2018c2ecf20Sopenharmony_ci		if (obj + len <= frame)
2028c2ecf20Sopenharmony_ci			return obj >= oldframe + 2 * sizeof(void *) ?
2038c2ecf20Sopenharmony_ci				GOOD_FRAME : BAD_STACK;
2048c2ecf20Sopenharmony_ci		oldframe = frame;
2058c2ecf20Sopenharmony_ci		frame = *(const void * const *)frame;
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci	return BAD_STACK;
2088c2ecf20Sopenharmony_ci#else
2098c2ecf20Sopenharmony_ci	return NOT_STACK;
2108c2ecf20Sopenharmony_ci#endif
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci#else /* !__ASSEMBLY__ */
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
2168c2ecf20Sopenharmony_ci# define cpu_current_top_of_stack (cpu_tss_rw + TSS_sp1)
2178c2ecf20Sopenharmony_ci#endif
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci#endif
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/*
2228c2ecf20Sopenharmony_ci * Thread-synchronous status.
2238c2ecf20Sopenharmony_ci *
2248c2ecf20Sopenharmony_ci * This is different from the flags in that nobody else
2258c2ecf20Sopenharmony_ci * ever touches our thread-synchronous status, so we don't
2268c2ecf20Sopenharmony_ci * have to worry about atomic accesses.
2278c2ecf20Sopenharmony_ci */
2288c2ecf20Sopenharmony_ci#define TS_COMPAT		0x0002	/* 32bit syscall active (64BIT)*/
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
2318c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
2328c2ecf20Sopenharmony_ci#define TS_I386_REGS_POKED	0x0004	/* regs poked by 32-bit ptracer */
2338c2ecf20Sopenharmony_ci#define TS_COMPAT_RESTART	0x0008
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci#define arch_set_restart_data	arch_set_restart_data
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic inline void arch_set_restart_data(struct restart_block *restart)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct thread_info *ti = current_thread_info();
2408c2ecf20Sopenharmony_ci	if (ti->status & TS_COMPAT)
2418c2ecf20Sopenharmony_ci		ti->status |= TS_COMPAT_RESTART;
2428c2ecf20Sopenharmony_ci	else
2438c2ecf20Sopenharmony_ci		ti->status &= ~TS_COMPAT_RESTART;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci#endif
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
2488c2ecf20Sopenharmony_ci#define in_ia32_syscall() true
2498c2ecf20Sopenharmony_ci#else
2508c2ecf20Sopenharmony_ci#define in_ia32_syscall() (IS_ENABLED(CONFIG_IA32_EMULATION) && \
2518c2ecf20Sopenharmony_ci			   current_thread_info()->status & TS_COMPAT)
2528c2ecf20Sopenharmony_ci#endif
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciextern void arch_task_cache_init(void);
2558c2ecf20Sopenharmony_ciextern int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src);
2568c2ecf20Sopenharmony_ciextern void arch_release_task_struct(struct task_struct *tsk);
2578c2ecf20Sopenharmony_ciextern void arch_setup_new_exec(void);
2588c2ecf20Sopenharmony_ci#define arch_setup_new_exec arch_setup_new_exec
2598c2ecf20Sopenharmony_ci#endif	/* !__ASSEMBLY__ */
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci#endif /* _ASM_X86_THREAD_INFO_H */
262