1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2009 Chen Liqin <liqin.chen@sunplusct.com>
4  * Copyright (C) 2012 Regents of the University of California
5  * Copyright (C) 2017 SiFive
6  */
7 
8 #ifndef _ASM_RISCV_THREAD_INFO_H
9 #define _ASM_RISCV_THREAD_INFO_H
10 
11 #include <asm/page.h>
12 #include <linux/const.h>
13 
14 /* thread information allocation */
15 #ifdef CONFIG_KASAN
16 #define KASAN_STACK_ORDER	1
17 #else
18 #define KASAN_STACK_ORDER	0
19 #endif
20 #define THREAD_SIZE_ORDER	(CONFIG_THREAD_SIZE_ORDER + KASAN_STACK_ORDER)
21 #define THREAD_SIZE		(PAGE_SIZE << THREAD_SIZE_ORDER)
22 
23 /*
24  * By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by
25  * checking sp & (1 << THREAD_SHIFT), which we can do cheaply in the entry
26  * assembly.
27  */
28 #ifdef CONFIG_VMAP_STACK
29 #define THREAD_ALIGN            (2 * THREAD_SIZE)
30 #else
31 #define THREAD_ALIGN            THREAD_SIZE
32 #endif
33 
34 #define THREAD_SHIFT            (PAGE_SHIFT + THREAD_SIZE_ORDER)
35 #define OVERFLOW_STACK_SIZE     SZ_4K
36 #define SHADOW_OVERFLOW_STACK_SIZE (1024)
37 
38 #define IRQ_STACK_SIZE		THREAD_SIZE
39 
40 #ifndef __ASSEMBLY__
41 
42 #include <asm/processor.h>
43 #include <asm/csr.h>
44 
45 /*
46  * low level task data that entry.S needs immediate access to
47  * - this struct should fit entirely inside of one cache line
48  * - if the members of this struct changes, the assembly constants
49  *   in asm-offsets.c must be updated accordingly
50  * - thread_info is included in task_struct at an offset of 0.  This means that
51  *   tp points to both thread_info and task_struct.
52  */
53 struct thread_info {
54 	unsigned long		flags;		/* low level flags */
55 	int                     preempt_count;  /* 0=>preemptible, <0=>BUG */
56 	/*
57 	 * These stack pointers are overwritten on every system call or
58 	 * exception.  SP is also saved to the stack it can be recovered when
59 	 * overwritten.
60 	 */
61 	long			kernel_sp;	/* Kernel stack pointer */
62 	long			user_sp;	/* User stack pointer */
63 	int			cpu;
64 	unsigned long		syscall_work;	/* SYSCALL_WORK_ flags */
65 };
66 
67 /*
68  * macros/functions for gaining access to the thread information structure
69  *
70  * preempt_count needs to be 1 initially, until the scheduler is functional.
71  */
72 #define INIT_THREAD_INFO(tsk)			\
73 {						\
74 	.flags		= 0,			\
75 	.preempt_count	= INIT_PREEMPT_COUNT,	\
76 }
77 
78 void arch_release_task_struct(struct task_struct *tsk);
79 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src);
80 
81 #endif /* !__ASSEMBLY__ */
82 
83 /*
84  * thread information flags
85  * - these are process state flags that various assembly files may need to
86  *   access
87  * - pending work-to-be-done flags are in lowest half-word
88  * - other flags in upper half-word(s)
89  */
90 #define TIF_NOTIFY_RESUME	1	/* callback before returning to user */
91 #define TIF_SIGPENDING		2	/* signal pending */
92 #define TIF_NEED_RESCHED	3	/* rescheduling necessary */
93 #define TIF_RESTORE_SIGMASK	4	/* restore signal mask in do_signal() */
94 #define TIF_MEMDIE		5	/* is terminating due to OOM killer */
95 #define TIF_NOTIFY_SIGNAL	9	/* signal notifications exist */
96 #define TIF_UPROBE		10	/* uprobe breakpoint or singlestep */
97 #define TIF_32BIT		11	/* compat-mode 32bit process */
98 
99 #define _TIF_NOTIFY_RESUME	(1 << TIF_NOTIFY_RESUME)
100 #define _TIF_SIGPENDING		(1 << TIF_SIGPENDING)
101 #define _TIF_NEED_RESCHED	(1 << TIF_NEED_RESCHED)
102 #define _TIF_NOTIFY_SIGNAL	(1 << TIF_NOTIFY_SIGNAL)
103 #define _TIF_UPROBE		(1 << TIF_UPROBE)
104 
105 #define _TIF_WORK_MASK \
106 	(_TIF_NOTIFY_RESUME | _TIF_SIGPENDING | _TIF_NEED_RESCHED | \
107 	 _TIF_NOTIFY_SIGNAL | _TIF_UPROBE)
108 
109 #endif /* _ASM_RISCV_THREAD_INFO_H */
110