1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /* thread_info.h: low-level thread information
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2002 David Howells (dhowells@redhat.com)
5*4882a593Smuzhiyun * - Incorporating suggestions made by Linus Torvalds and Dave Miller
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef _ASM_X86_THREAD_INFO_H
9*4882a593Smuzhiyun #define _ASM_X86_THREAD_INFO_H
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/compiler.h>
12*4882a593Smuzhiyun #include <asm/page.h>
13*4882a593Smuzhiyun #include <asm/percpu.h>
14*4882a593Smuzhiyun #include <asm/types.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /*
17*4882a593Smuzhiyun * TOP_OF_KERNEL_STACK_PADDING is a number of unused bytes that we
18*4882a593Smuzhiyun * reserve at the top of the kernel stack. We do it because of a nasty
19*4882a593Smuzhiyun * 32-bit corner case. On x86_32, the hardware stack frame is
20*4882a593Smuzhiyun * variable-length. Except for vm86 mode, struct pt_regs assumes a
21*4882a593Smuzhiyun * maximum-length frame. If we enter from CPL 0, the top 8 bytes of
22*4882a593Smuzhiyun * pt_regs don't actually exist. Ordinarily this doesn't matter, but it
23*4882a593Smuzhiyun * does in at least one case:
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * If we take an NMI early enough in SYSENTER, then we can end up with
26*4882a593Smuzhiyun * pt_regs that extends above sp0. On the way out, in the espfix code,
27*4882a593Smuzhiyun * we can read the saved SS value, but that value will be above sp0.
28*4882a593Smuzhiyun * Without this offset, that can result in a page fault. (We are
29*4882a593Smuzhiyun * careful that, in this case, the value we read doesn't matter.)
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * In vm86 mode, the hardware frame is much longer still, so add 16
32*4882a593Smuzhiyun * bytes to make room for the real-mode segments.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * x86_64 has a fixed-length stack frame.
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun #ifdef CONFIG_X86_32
37*4882a593Smuzhiyun # ifdef CONFIG_VM86
38*4882a593Smuzhiyun # define TOP_OF_KERNEL_STACK_PADDING 16
39*4882a593Smuzhiyun # else
40*4882a593Smuzhiyun # define TOP_OF_KERNEL_STACK_PADDING 8
41*4882a593Smuzhiyun # endif
42*4882a593Smuzhiyun #else
43*4882a593Smuzhiyun # define TOP_OF_KERNEL_STACK_PADDING 0
44*4882a593Smuzhiyun #endif
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * low level task data that entry.S needs immediate access to
48*4882a593Smuzhiyun * - this struct should fit entirely inside of one cache line
49*4882a593Smuzhiyun * - this struct shares the supervisor stack pages
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun #ifndef __ASSEMBLY__
52*4882a593Smuzhiyun struct task_struct;
53*4882a593Smuzhiyun #include <asm/cpufeature.h>
54*4882a593Smuzhiyun #include <linux/atomic.h>
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun struct thread_info {
57*4882a593Smuzhiyun unsigned long flags; /* low level flags */
58*4882a593Smuzhiyun u32 status; /* thread synchronous flags */
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #define INIT_THREAD_INFO(tsk) \
62*4882a593Smuzhiyun { \
63*4882a593Smuzhiyun .flags = 0, \
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun #else /* !__ASSEMBLY__ */
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun #include <asm/asm-offsets.h>
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun #endif
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun * thread information flags
74*4882a593Smuzhiyun * - these are process state flags that various assembly files
75*4882a593Smuzhiyun * may need to access
76*4882a593Smuzhiyun */
77*4882a593Smuzhiyun #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
78*4882a593Smuzhiyun #define TIF_NOTIFY_RESUME 1 /* callback before returning to user */
79*4882a593Smuzhiyun #define TIF_SIGPENDING 2 /* signal pending */
80*4882a593Smuzhiyun #define TIF_NEED_RESCHED 3 /* rescheduling necessary */
81*4882a593Smuzhiyun #define TIF_SINGLESTEP 4 /* reenable singlestep on user return*/
82*4882a593Smuzhiyun #define TIF_SSBD 5 /* Speculative store bypass disable */
83*4882a593Smuzhiyun #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
84*4882a593Smuzhiyun #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
85*4882a593Smuzhiyun #define TIF_SECCOMP 8 /* secure computing */
86*4882a593Smuzhiyun #define TIF_SPEC_IB 9 /* Indirect branch speculation mitigation */
87*4882a593Smuzhiyun #define TIF_SPEC_FORCE_UPDATE 10 /* Force speculation MSR update in context switch */
88*4882a593Smuzhiyun #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
89*4882a593Smuzhiyun #define TIF_UPROBE 12 /* breakpointed or singlestepping */
90*4882a593Smuzhiyun #define TIF_PATCH_PENDING 13 /* pending live patching update */
91*4882a593Smuzhiyun #define TIF_NEED_FPU_LOAD 14 /* load FPU on return to userspace */
92*4882a593Smuzhiyun #define TIF_NOCPUID 15 /* CPUID is not accessible in userland */
93*4882a593Smuzhiyun #define TIF_NOTSC 16 /* TSC is not accessible in userland */
94*4882a593Smuzhiyun #define TIF_IA32 17 /* IA32 compatibility process */
95*4882a593Smuzhiyun #define TIF_SLD 18 /* Restore split lock detection on context switch */
96*4882a593Smuzhiyun #define TIF_NOTIFY_SIGNAL 19 /* signal notifications exist */
97*4882a593Smuzhiyun #define TIF_MEMDIE 20 /* is terminating due to OOM killer */
98*4882a593Smuzhiyun #define TIF_POLLING_NRFLAG 21 /* idle is polling for TIF_NEED_RESCHED */
99*4882a593Smuzhiyun #define TIF_IO_BITMAP 22 /* uses I/O bitmap */
100*4882a593Smuzhiyun #define TIF_FORCED_TF 24 /* true if TF in eflags artificially */
101*4882a593Smuzhiyun #define TIF_BLOCKSTEP 25 /* set when we want DEBUGCTLMSR_BTF */
102*4882a593Smuzhiyun #define TIF_LAZY_MMU_UPDATES 27 /* task is updating the mmu lazily */
103*4882a593Smuzhiyun #define TIF_SYSCALL_TRACEPOINT 28 /* syscall tracepoint instrumentation */
104*4882a593Smuzhiyun #define TIF_ADDR32 29 /* 32-bit address space on 64 bits */
105*4882a593Smuzhiyun #define TIF_X32 30 /* 32-bit native x86-64 binary */
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
108*4882a593Smuzhiyun #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
109*4882a593Smuzhiyun #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
110*4882a593Smuzhiyun #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
111*4882a593Smuzhiyun #define _TIF_SINGLESTEP (1 << TIF_SINGLESTEP)
112*4882a593Smuzhiyun #define _TIF_SSBD (1 << TIF_SSBD)
113*4882a593Smuzhiyun #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
114*4882a593Smuzhiyun #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
115*4882a593Smuzhiyun #define _TIF_SECCOMP (1 << TIF_SECCOMP)
116*4882a593Smuzhiyun #define _TIF_SPEC_IB (1 << TIF_SPEC_IB)
117*4882a593Smuzhiyun #define _TIF_SPEC_FORCE_UPDATE (1 << TIF_SPEC_FORCE_UPDATE)
118*4882a593Smuzhiyun #define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
119*4882a593Smuzhiyun #define _TIF_UPROBE (1 << TIF_UPROBE)
120*4882a593Smuzhiyun #define _TIF_PATCH_PENDING (1 << TIF_PATCH_PENDING)
121*4882a593Smuzhiyun #define _TIF_NEED_FPU_LOAD (1 << TIF_NEED_FPU_LOAD)
122*4882a593Smuzhiyun #define _TIF_NOCPUID (1 << TIF_NOCPUID)
123*4882a593Smuzhiyun #define _TIF_NOTSC (1 << TIF_NOTSC)
124*4882a593Smuzhiyun #define _TIF_IA32 (1 << TIF_IA32)
125*4882a593Smuzhiyun #define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)
126*4882a593Smuzhiyun #define _TIF_SLD (1 << TIF_SLD)
127*4882a593Smuzhiyun #define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
128*4882a593Smuzhiyun #define _TIF_IO_BITMAP (1 << TIF_IO_BITMAP)
129*4882a593Smuzhiyun #define _TIF_FORCED_TF (1 << TIF_FORCED_TF)
130*4882a593Smuzhiyun #define _TIF_BLOCKSTEP (1 << TIF_BLOCKSTEP)
131*4882a593Smuzhiyun #define _TIF_LAZY_MMU_UPDATES (1 << TIF_LAZY_MMU_UPDATES)
132*4882a593Smuzhiyun #define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
133*4882a593Smuzhiyun #define _TIF_ADDR32 (1 << TIF_ADDR32)
134*4882a593Smuzhiyun #define _TIF_X32 (1 << TIF_X32)
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /* flags to check in __switch_to() */
137*4882a593Smuzhiyun #define _TIF_WORK_CTXSW_BASE \
138*4882a593Smuzhiyun (_TIF_NOCPUID | _TIF_NOTSC | _TIF_BLOCKSTEP | \
139*4882a593Smuzhiyun _TIF_SSBD | _TIF_SPEC_FORCE_UPDATE | _TIF_SLD)
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * Avoid calls to __switch_to_xtra() on UP as STIBP is not evaluated.
143*4882a593Smuzhiyun */
144*4882a593Smuzhiyun #ifdef CONFIG_SMP
145*4882a593Smuzhiyun # define _TIF_WORK_CTXSW (_TIF_WORK_CTXSW_BASE | _TIF_SPEC_IB)
146*4882a593Smuzhiyun #else
147*4882a593Smuzhiyun # define _TIF_WORK_CTXSW (_TIF_WORK_CTXSW_BASE)
148*4882a593Smuzhiyun #endif
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun #ifdef CONFIG_X86_IOPL_IOPERM
151*4882a593Smuzhiyun # define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW| _TIF_USER_RETURN_NOTIFY | \
152*4882a593Smuzhiyun _TIF_IO_BITMAP)
153*4882a593Smuzhiyun #else
154*4882a593Smuzhiyun # define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW| _TIF_USER_RETURN_NOTIFY)
155*4882a593Smuzhiyun #endif
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun #define STACK_WARN (THREAD_SIZE/8)
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun * macros/functions for gaining access to the thread information structure
163*4882a593Smuzhiyun *
164*4882a593Smuzhiyun * preempt_count needs to be 1 initially, until the scheduler is functional.
165*4882a593Smuzhiyun */
166*4882a593Smuzhiyun #ifndef __ASSEMBLY__
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /*
169*4882a593Smuzhiyun * Walks up the stack frames to make sure that the specified object is
170*4882a593Smuzhiyun * entirely contained by a single stack frame.
171*4882a593Smuzhiyun *
172*4882a593Smuzhiyun * Returns:
173*4882a593Smuzhiyun * GOOD_FRAME if within a frame
174*4882a593Smuzhiyun * BAD_STACK if placed across a frame boundary (or outside stack)
175*4882a593Smuzhiyun * NOT_STACK unable to determine (no frame pointers, etc)
176*4882a593Smuzhiyun */
arch_within_stack_frames(const void * const stack,const void * const stackend,const void * obj,unsigned long len)177*4882a593Smuzhiyun static inline int arch_within_stack_frames(const void * const stack,
178*4882a593Smuzhiyun const void * const stackend,
179*4882a593Smuzhiyun const void *obj, unsigned long len)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun #if defined(CONFIG_FRAME_POINTER)
182*4882a593Smuzhiyun const void *frame = NULL;
183*4882a593Smuzhiyun const void *oldframe;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun oldframe = __builtin_frame_address(1);
186*4882a593Smuzhiyun if (oldframe)
187*4882a593Smuzhiyun frame = __builtin_frame_address(2);
188*4882a593Smuzhiyun /*
189*4882a593Smuzhiyun * low ----------------------------------------------> high
190*4882a593Smuzhiyun * [saved bp][saved ip][args][local vars][saved bp][saved ip]
191*4882a593Smuzhiyun * ^----------------^
192*4882a593Smuzhiyun * allow copies only within here
193*4882a593Smuzhiyun */
194*4882a593Smuzhiyun while (stack <= frame && frame < stackend) {
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun * If obj + len extends past the last frame, this
197*4882a593Smuzhiyun * check won't pass and the next frame will be 0,
198*4882a593Smuzhiyun * causing us to bail out and correctly report
199*4882a593Smuzhiyun * the copy as invalid.
200*4882a593Smuzhiyun */
201*4882a593Smuzhiyun if (obj + len <= frame)
202*4882a593Smuzhiyun return obj >= oldframe + 2 * sizeof(void *) ?
203*4882a593Smuzhiyun GOOD_FRAME : BAD_STACK;
204*4882a593Smuzhiyun oldframe = frame;
205*4882a593Smuzhiyun frame = *(const void * const *)frame;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun return BAD_STACK;
208*4882a593Smuzhiyun #else
209*4882a593Smuzhiyun return NOT_STACK;
210*4882a593Smuzhiyun #endif
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun #else /* !__ASSEMBLY__ */
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun #ifdef CONFIG_X86_64
216*4882a593Smuzhiyun # define cpu_current_top_of_stack (cpu_tss_rw + TSS_sp1)
217*4882a593Smuzhiyun #endif
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun #endif
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun * Thread-synchronous status.
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun * This is different from the flags in that nobody else
225*4882a593Smuzhiyun * ever touches our thread-synchronous status, so we don't
226*4882a593Smuzhiyun * have to worry about atomic accesses.
227*4882a593Smuzhiyun */
228*4882a593Smuzhiyun #define TS_COMPAT 0x0002 /* 32bit syscall active (64BIT)*/
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun #ifndef __ASSEMBLY__
231*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
232*4882a593Smuzhiyun #define TS_I386_REGS_POKED 0x0004 /* regs poked by 32-bit ptracer */
233*4882a593Smuzhiyun #define TS_COMPAT_RESTART 0x0008
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun #define arch_set_restart_data arch_set_restart_data
236*4882a593Smuzhiyun
arch_set_restart_data(struct restart_block * restart)237*4882a593Smuzhiyun static inline void arch_set_restart_data(struct restart_block *restart)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun struct thread_info *ti = current_thread_info();
240*4882a593Smuzhiyun if (ti->status & TS_COMPAT)
241*4882a593Smuzhiyun ti->status |= TS_COMPAT_RESTART;
242*4882a593Smuzhiyun else
243*4882a593Smuzhiyun ti->status &= ~TS_COMPAT_RESTART;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun #endif
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun #ifdef CONFIG_X86_32
248*4882a593Smuzhiyun #define in_ia32_syscall() true
249*4882a593Smuzhiyun #else
250*4882a593Smuzhiyun #define in_ia32_syscall() (IS_ENABLED(CONFIG_IA32_EMULATION) && \
251*4882a593Smuzhiyun current_thread_info()->status & TS_COMPAT)
252*4882a593Smuzhiyun #endif
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun extern void arch_task_cache_init(void);
255*4882a593Smuzhiyun extern int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src);
256*4882a593Smuzhiyun extern void arch_release_task_struct(struct task_struct *tsk);
257*4882a593Smuzhiyun extern void arch_setup_new_exec(void);
258*4882a593Smuzhiyun #define arch_setup_new_exec arch_setup_new_exec
259*4882a593Smuzhiyun #endif /* !__ASSEMBLY__ */
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun #endif /* _ASM_X86_THREAD_INFO_H */
262