162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/* thread_info.h: low-level thread information
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (C) 2002  David Howells (dhowells@redhat.com)
562306a36Sopenharmony_ci * - Incorporating suggestions made by Linus Torvalds and Dave Miller
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef _ASM_X86_THREAD_INFO_H
962306a36Sopenharmony_ci#define _ASM_X86_THREAD_INFO_H
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/compiler.h>
1262306a36Sopenharmony_ci#include <asm/page.h>
1362306a36Sopenharmony_ci#include <asm/percpu.h>
1462306a36Sopenharmony_ci#include <asm/types.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * TOP_OF_KERNEL_STACK_PADDING is a number of unused bytes that we
1862306a36Sopenharmony_ci * reserve at the top of the kernel stack.  We do it because of a nasty
1962306a36Sopenharmony_ci * 32-bit corner case.  On x86_32, the hardware stack frame is
2062306a36Sopenharmony_ci * variable-length.  Except for vm86 mode, struct pt_regs assumes a
2162306a36Sopenharmony_ci * maximum-length frame.  If we enter from CPL 0, the top 8 bytes of
2262306a36Sopenharmony_ci * pt_regs don't actually exist.  Ordinarily this doesn't matter, but it
2362306a36Sopenharmony_ci * does in at least one case:
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * If we take an NMI early enough in SYSENTER, then we can end up with
2662306a36Sopenharmony_ci * pt_regs that extends above sp0.  On the way out, in the espfix code,
2762306a36Sopenharmony_ci * we can read the saved SS value, but that value will be above sp0.
2862306a36Sopenharmony_ci * Without this offset, that can result in a page fault.  (We are
2962306a36Sopenharmony_ci * careful that, in this case, the value we read doesn't matter.)
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * In vm86 mode, the hardware frame is much longer still, so add 16
3262306a36Sopenharmony_ci * bytes to make room for the real-mode segments.
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * x86_64 has a fixed-length stack frame.
3562306a36Sopenharmony_ci */
3662306a36Sopenharmony_ci#ifdef CONFIG_X86_32
3762306a36Sopenharmony_ci# ifdef CONFIG_VM86
3862306a36Sopenharmony_ci#  define TOP_OF_KERNEL_STACK_PADDING 16
3962306a36Sopenharmony_ci# else
4062306a36Sopenharmony_ci#  define TOP_OF_KERNEL_STACK_PADDING 8
4162306a36Sopenharmony_ci# endif
4262306a36Sopenharmony_ci#else
4362306a36Sopenharmony_ci# define TOP_OF_KERNEL_STACK_PADDING 0
4462306a36Sopenharmony_ci#endif
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/*
4762306a36Sopenharmony_ci * low level task data that entry.S needs immediate access to
4862306a36Sopenharmony_ci * - this struct should fit entirely inside of one cache line
4962306a36Sopenharmony_ci * - this struct shares the supervisor stack pages
5062306a36Sopenharmony_ci */
5162306a36Sopenharmony_ci#ifndef __ASSEMBLY__
5262306a36Sopenharmony_cistruct task_struct;
5362306a36Sopenharmony_ci#include <asm/cpufeature.h>
5462306a36Sopenharmony_ci#include <linux/atomic.h>
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistruct thread_info {
5762306a36Sopenharmony_ci	unsigned long		flags;		/* low level flags */
5862306a36Sopenharmony_ci	unsigned long		syscall_work;	/* SYSCALL_WORK_ flags */
5962306a36Sopenharmony_ci	u32			status;		/* thread synchronous flags */
6062306a36Sopenharmony_ci#ifdef CONFIG_SMP
6162306a36Sopenharmony_ci	u32			cpu;		/* current CPU */
6262306a36Sopenharmony_ci#endif
6362306a36Sopenharmony_ci};
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci#define INIT_THREAD_INFO(tsk)			\
6662306a36Sopenharmony_ci{						\
6762306a36Sopenharmony_ci	.flags		= 0,			\
6862306a36Sopenharmony_ci}
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#else /* !__ASSEMBLY__ */
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#include <asm/asm-offsets.h>
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#endif
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci/*
7762306a36Sopenharmony_ci * thread information flags
7862306a36Sopenharmony_ci * - these are process state flags that various assembly files
7962306a36Sopenharmony_ci *   may need to access
8062306a36Sopenharmony_ci */
8162306a36Sopenharmony_ci#define TIF_NOTIFY_RESUME	1	/* callback before returning to user */
8262306a36Sopenharmony_ci#define TIF_SIGPENDING		2	/* signal pending */
8362306a36Sopenharmony_ci#define TIF_NEED_RESCHED	3	/* rescheduling necessary */
8462306a36Sopenharmony_ci#define TIF_SINGLESTEP		4	/* reenable singlestep on user return*/
8562306a36Sopenharmony_ci#define TIF_SSBD		5	/* Speculative store bypass disable */
8662306a36Sopenharmony_ci#define TIF_SPEC_IB		9	/* Indirect branch speculation mitigation */
8762306a36Sopenharmony_ci#define TIF_SPEC_L1D_FLUSH	10	/* Flush L1D on mm switches (processes) */
8862306a36Sopenharmony_ci#define TIF_USER_RETURN_NOTIFY	11	/* notify kernel of userspace return */
8962306a36Sopenharmony_ci#define TIF_UPROBE		12	/* breakpointed or singlestepping */
9062306a36Sopenharmony_ci#define TIF_PATCH_PENDING	13	/* pending live patching update */
9162306a36Sopenharmony_ci#define TIF_NEED_FPU_LOAD	14	/* load FPU on return to userspace */
9262306a36Sopenharmony_ci#define TIF_NOCPUID		15	/* CPUID is not accessible in userland */
9362306a36Sopenharmony_ci#define TIF_NOTSC		16	/* TSC is not accessible in userland */
9462306a36Sopenharmony_ci#define TIF_NOTIFY_SIGNAL	17	/* signal notifications exist */
9562306a36Sopenharmony_ci#define TIF_MEMDIE		20	/* is terminating due to OOM killer */
9662306a36Sopenharmony_ci#define TIF_POLLING_NRFLAG	21	/* idle is polling for TIF_NEED_RESCHED */
9762306a36Sopenharmony_ci#define TIF_IO_BITMAP		22	/* uses I/O bitmap */
9862306a36Sopenharmony_ci#define TIF_SPEC_FORCE_UPDATE	23	/* Force speculation MSR update in context switch */
9962306a36Sopenharmony_ci#define TIF_FORCED_TF		24	/* true if TF in eflags artificially */
10062306a36Sopenharmony_ci#define TIF_BLOCKSTEP		25	/* set when we want DEBUGCTLMSR_BTF */
10162306a36Sopenharmony_ci#define TIF_LAZY_MMU_UPDATES	27	/* task is updating the mmu lazily */
10262306a36Sopenharmony_ci#define TIF_ADDR32		29	/* 32-bit address space on 64 bits */
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci#define _TIF_NOTIFY_RESUME	(1 << TIF_NOTIFY_RESUME)
10562306a36Sopenharmony_ci#define _TIF_SIGPENDING		(1 << TIF_SIGPENDING)
10662306a36Sopenharmony_ci#define _TIF_NEED_RESCHED	(1 << TIF_NEED_RESCHED)
10762306a36Sopenharmony_ci#define _TIF_SINGLESTEP		(1 << TIF_SINGLESTEP)
10862306a36Sopenharmony_ci#define _TIF_SSBD		(1 << TIF_SSBD)
10962306a36Sopenharmony_ci#define _TIF_SPEC_IB		(1 << TIF_SPEC_IB)
11062306a36Sopenharmony_ci#define _TIF_SPEC_L1D_FLUSH	(1 << TIF_SPEC_L1D_FLUSH)
11162306a36Sopenharmony_ci#define _TIF_USER_RETURN_NOTIFY	(1 << TIF_USER_RETURN_NOTIFY)
11262306a36Sopenharmony_ci#define _TIF_UPROBE		(1 << TIF_UPROBE)
11362306a36Sopenharmony_ci#define _TIF_PATCH_PENDING	(1 << TIF_PATCH_PENDING)
11462306a36Sopenharmony_ci#define _TIF_NEED_FPU_LOAD	(1 << TIF_NEED_FPU_LOAD)
11562306a36Sopenharmony_ci#define _TIF_NOCPUID		(1 << TIF_NOCPUID)
11662306a36Sopenharmony_ci#define _TIF_NOTSC		(1 << TIF_NOTSC)
11762306a36Sopenharmony_ci#define _TIF_NOTIFY_SIGNAL	(1 << TIF_NOTIFY_SIGNAL)
11862306a36Sopenharmony_ci#define _TIF_POLLING_NRFLAG	(1 << TIF_POLLING_NRFLAG)
11962306a36Sopenharmony_ci#define _TIF_IO_BITMAP		(1 << TIF_IO_BITMAP)
12062306a36Sopenharmony_ci#define _TIF_SPEC_FORCE_UPDATE	(1 << TIF_SPEC_FORCE_UPDATE)
12162306a36Sopenharmony_ci#define _TIF_FORCED_TF		(1 << TIF_FORCED_TF)
12262306a36Sopenharmony_ci#define _TIF_BLOCKSTEP		(1 << TIF_BLOCKSTEP)
12362306a36Sopenharmony_ci#define _TIF_LAZY_MMU_UPDATES	(1 << TIF_LAZY_MMU_UPDATES)
12462306a36Sopenharmony_ci#define _TIF_ADDR32		(1 << TIF_ADDR32)
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci/* flags to check in __switch_to() */
12762306a36Sopenharmony_ci#define _TIF_WORK_CTXSW_BASE					\
12862306a36Sopenharmony_ci	(_TIF_NOCPUID | _TIF_NOTSC | _TIF_BLOCKSTEP |		\
12962306a36Sopenharmony_ci	 _TIF_SSBD | _TIF_SPEC_FORCE_UPDATE)
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci/*
13262306a36Sopenharmony_ci * Avoid calls to __switch_to_xtra() on UP as STIBP is not evaluated.
13362306a36Sopenharmony_ci */
13462306a36Sopenharmony_ci#ifdef CONFIG_SMP
13562306a36Sopenharmony_ci# define _TIF_WORK_CTXSW	(_TIF_WORK_CTXSW_BASE | _TIF_SPEC_IB)
13662306a36Sopenharmony_ci#else
13762306a36Sopenharmony_ci# define _TIF_WORK_CTXSW	(_TIF_WORK_CTXSW_BASE)
13862306a36Sopenharmony_ci#endif
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci#ifdef CONFIG_X86_IOPL_IOPERM
14162306a36Sopenharmony_ci# define _TIF_WORK_CTXSW_PREV	(_TIF_WORK_CTXSW| _TIF_USER_RETURN_NOTIFY | \
14262306a36Sopenharmony_ci				 _TIF_IO_BITMAP)
14362306a36Sopenharmony_ci#else
14462306a36Sopenharmony_ci# define _TIF_WORK_CTXSW_PREV	(_TIF_WORK_CTXSW| _TIF_USER_RETURN_NOTIFY)
14562306a36Sopenharmony_ci#endif
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci#define _TIF_WORK_CTXSW_NEXT	(_TIF_WORK_CTXSW)
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci#define STACK_WARN		(THREAD_SIZE/8)
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci/*
15262306a36Sopenharmony_ci * macros/functions for gaining access to the thread information structure
15362306a36Sopenharmony_ci *
15462306a36Sopenharmony_ci * preempt_count needs to be 1 initially, until the scheduler is functional.
15562306a36Sopenharmony_ci */
15662306a36Sopenharmony_ci#ifndef __ASSEMBLY__
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci/*
15962306a36Sopenharmony_ci * Walks up the stack frames to make sure that the specified object is
16062306a36Sopenharmony_ci * entirely contained by a single stack frame.
16162306a36Sopenharmony_ci *
16262306a36Sopenharmony_ci * Returns:
16362306a36Sopenharmony_ci *	GOOD_FRAME	if within a frame
16462306a36Sopenharmony_ci *	BAD_STACK	if placed across a frame boundary (or outside stack)
16562306a36Sopenharmony_ci *	NOT_STACK	unable to determine (no frame pointers, etc)
16662306a36Sopenharmony_ci *
16762306a36Sopenharmony_ci * This function reads pointers from the stack and dereferences them. The
16862306a36Sopenharmony_ci * pointers may not have their KMSAN shadow set up properly, which may result
16962306a36Sopenharmony_ci * in false positive reports. Disable instrumentation to avoid those.
17062306a36Sopenharmony_ci */
17162306a36Sopenharmony_ci__no_kmsan_checks
17262306a36Sopenharmony_cistatic inline int arch_within_stack_frames(const void * const stack,
17362306a36Sopenharmony_ci					   const void * const stackend,
17462306a36Sopenharmony_ci					   const void *obj, unsigned long len)
17562306a36Sopenharmony_ci{
17662306a36Sopenharmony_ci#if defined(CONFIG_FRAME_POINTER)
17762306a36Sopenharmony_ci	const void *frame = NULL;
17862306a36Sopenharmony_ci	const void *oldframe;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	oldframe = __builtin_frame_address(1);
18162306a36Sopenharmony_ci	if (oldframe)
18262306a36Sopenharmony_ci		frame = __builtin_frame_address(2);
18362306a36Sopenharmony_ci	/*
18462306a36Sopenharmony_ci	 * low ----------------------------------------------> high
18562306a36Sopenharmony_ci	 * [saved bp][saved ip][args][local vars][saved bp][saved ip]
18662306a36Sopenharmony_ci	 *                     ^----------------^
18762306a36Sopenharmony_ci	 *               allow copies only within here
18862306a36Sopenharmony_ci	 */
18962306a36Sopenharmony_ci	while (stack <= frame && frame < stackend) {
19062306a36Sopenharmony_ci		/*
19162306a36Sopenharmony_ci		 * If obj + len extends past the last frame, this
19262306a36Sopenharmony_ci		 * check won't pass and the next frame will be 0,
19362306a36Sopenharmony_ci		 * causing us to bail out and correctly report
19462306a36Sopenharmony_ci		 * the copy as invalid.
19562306a36Sopenharmony_ci		 */
19662306a36Sopenharmony_ci		if (obj + len <= frame)
19762306a36Sopenharmony_ci			return obj >= oldframe + 2 * sizeof(void *) ?
19862306a36Sopenharmony_ci				GOOD_FRAME : BAD_STACK;
19962306a36Sopenharmony_ci		oldframe = frame;
20062306a36Sopenharmony_ci		frame = *(const void * const *)frame;
20162306a36Sopenharmony_ci	}
20262306a36Sopenharmony_ci	return BAD_STACK;
20362306a36Sopenharmony_ci#else
20462306a36Sopenharmony_ci	return NOT_STACK;
20562306a36Sopenharmony_ci#endif
20662306a36Sopenharmony_ci}
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci#endif  /* !__ASSEMBLY__ */
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci/*
21162306a36Sopenharmony_ci * Thread-synchronous status.
21262306a36Sopenharmony_ci *
21362306a36Sopenharmony_ci * This is different from the flags in that nobody else
21462306a36Sopenharmony_ci * ever touches our thread-synchronous status, so we don't
21562306a36Sopenharmony_ci * have to worry about atomic accesses.
21662306a36Sopenharmony_ci */
21762306a36Sopenharmony_ci#define TS_COMPAT		0x0002	/* 32bit syscall active (64BIT)*/
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci#ifndef __ASSEMBLY__
22062306a36Sopenharmony_ci#ifdef CONFIG_COMPAT
22162306a36Sopenharmony_ci#define TS_I386_REGS_POKED	0x0004	/* regs poked by 32-bit ptracer */
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci#define arch_set_restart_data(restart)	\
22462306a36Sopenharmony_ci	do { restart->arch_data = current_thread_info()->status; } while (0)
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci#endif
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci#ifdef CONFIG_X86_32
22962306a36Sopenharmony_ci#define in_ia32_syscall() true
23062306a36Sopenharmony_ci#else
23162306a36Sopenharmony_ci#define in_ia32_syscall() (IS_ENABLED(CONFIG_IA32_EMULATION) && \
23262306a36Sopenharmony_ci			   current_thread_info()->status & TS_COMPAT)
23362306a36Sopenharmony_ci#endif
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ciextern void arch_setup_new_exec(void);
23662306a36Sopenharmony_ci#define arch_setup_new_exec arch_setup_new_exec
23762306a36Sopenharmony_ci#endif	/* !__ASSEMBLY__ */
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci#endif /* _ASM_X86_THREAD_INFO_H */
240