xref: /linux/arch/riscv/include/asm/processor.h (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  */
5 
6 #ifndef _ASM_RISCV_PROCESSOR_H
7 #define _ASM_RISCV_PROCESSOR_H
8 
9 #include <linux/const.h>
10 #include <linux/cache.h>
11 #include <linux/prctl.h>
12 
13 #include <vdso/processor.h>
14 
15 #include <asm/ptrace.h>
16 #include <asm/insn-def.h>
17 #include <asm/alternative-macros.h>
18 #include <asm/hwcap.h>
19 #include <asm/usercfi.h>
20 #include <asm/errata_list.h>
21 
22 #define arch_get_mmap_end(addr, len, flags)			\
23 ({								\
24 	STACK_TOP_MAX;						\
25 })
26 
27 #define arch_get_mmap_base(addr, base)				\
28 ({								\
29 	base;							\
30 })
31 
32 #ifdef CONFIG_64BIT
33 #define DEFAULT_MAP_WINDOW	(UL(1) << (MMAP_VA_BITS - 1))
34 #define STACK_TOP_MAX		TASK_SIZE_64
35 #else
36 #define DEFAULT_MAP_WINDOW	TASK_SIZE
37 #define STACK_TOP_MAX		TASK_SIZE
38 #endif
39 #define STACK_ALIGN		16
40 
41 #define STACK_TOP		DEFAULT_MAP_WINDOW
42 
43 #ifdef CONFIG_MMU
44 #define user_max_virt_addr() arch_get_mmap_end(ULONG_MAX, 0, 0)
45 #else
46 #define user_max_virt_addr() 0
47 #endif /* CONFIG_MMU */
48 
49 /*
50  * This decides where the kernel will search for a free chunk of vm
51  * space during mmap's.
52  */
53 #ifdef CONFIG_64BIT
54 #define TASK_UNMAPPED_BASE	PAGE_ALIGN((UL(1) << MMAP_MIN_VA_BITS) / 3)
55 #else
56 #define TASK_UNMAPPED_BASE	PAGE_ALIGN(TASK_SIZE / 3)
57 #endif
58 
59 #ifndef __ASSEMBLER__
60 
61 struct task_struct;
62 struct pt_regs;
63 
64 /*
65  * We use a flag to track in-kernel Vector context. Currently the flag has the
66  * following meaning:
67  *
68  *  - bit 0: indicates whether the in-kernel Vector context is active. The
69  *    activation of this state disables the preemption. On a non-RT kernel, it
70  *    also disable bh.
71  *  - bit 1: tells kvm that the vcpu process has guest context saved in vcpu's
72  *    context memory and need to be restore upon returing back to the guest.
73  *  - bit 2: represents that the vector context has now loaded and belongs to
74  *    the guest kernel. Any non-scheduler context saving routing needs to save
75  *    the register file to vcpu's context memory. The bit is set upon returing
76  *    back to the guest and cleared after loading the host's vector context.
77  *  - bits 8: is used for tracking preemptible kernel-mode Vector, when
78  *    RISCV_ISA_V_PREEMPTIVE is enabled. Calling kernel_vector_begin() does not
79  *    disable the preemption if the thread's kernel_vstate.datap is allocated.
80  *    Instead, the kernel set this bit field. Then the trap entry/exit code
81  *    knows if we are entering/exiting the context that owns preempt_v.
82  *     - 0: the task is not using preempt_v
83  *     - 1: the task is actively using preempt_v. But whether does the task own
84  *          the preempt_v context is decided by bits in RISCV_V_CTX_DEPTH_MASK.
85  *  - bit 16-23 are RISCV_V_CTX_DEPTH_MASK, used by context tracking routine
86  *     when preempt_v starts:
87  *     - 0: the task is actively using, and own preempt_v context.
88  *     - non-zero: the task was using preempt_v, but then took a trap within.
89  *       Thus, the task does not own preempt_v. Any use of Vector will have to
90  *       save preempt_v, if dirty, and fallback to non-preemptible kernel-mode
91  *       Vector.
92  *  - bit 29: The thread voluntarily calls schedule() while holding an active
93  *    preempt_v. All preempt_v context should be dropped in such case because
94  *    V-regs are caller-saved. Only sstatus.VS=ON is persisted across a
95  *    schedule() call.
96  *  - bit 30: The in-kernel preempt_v context is saved, and is required to be
97  *    restored when returning to the context that owns the preempt_v.
98  *  - bit 31: The in-kernel preempt_v context is dirty, as signaled by the
99  *    trap entry code. Any context switches out-of current task need to save
100  *    it to the task's in-kernel V context. Also, any traps nesting on-top-of
101  *    preempt_v requesting to use V needs a save.
102  */
103 #define RISCV_V_CTX_DEPTH_MASK		0x00ff0000
104 
105 #define RISCV_V_CTX_UNIT_DEPTH		0x00010000
106 #define RISCV_KERNEL_MODE_V		0x00000001
107 #define RISCV_V_VCPU_NEED_RESTORE	0x00000002
108 #define RISCV_V_VCPU_CTX		0x00000004
109 #define RISCV_PREEMPT_V			0x00000100
110 #define RISCV_PREEMPT_V_DIRTY		0x80000000
111 #define RISCV_PREEMPT_V_NEED_RESTORE	0x40000000
112 #define RISCV_PREEMPT_V_IN_SCHEDULE	0x20000000
113 
114 /* CPU-specific state of a task */
115 struct thread_struct {
116 	/* Callee-saved registers */
117 	unsigned long ra;
118 	unsigned long sp;	/* Kernel mode stack */
119 	unsigned long s[12];	/* s[0]: frame pointer */
120 	struct __riscv_d_ext_state fstate;
121 	unsigned long bad_cause;
122 	unsigned long envcfg;
123 	unsigned long sum;
124 	u32 riscv_v_flags;
125 	u32 vstate_ctrl;
126 	struct __riscv_v_ext_state vstate;
127 	unsigned long align_ctl;
128 	struct __riscv_v_ext_state kernel_vstate;
129 #ifdef CONFIG_SMP
130 	/* Flush the icache on migration */
131 	bool force_icache_flush;
132 	/* A forced icache flush is not needed if migrating to the previous cpu. */
133 	unsigned int prev_cpu;
134 #endif
135 #ifdef CONFIG_RISCV_ISA_SSQOSID
136 	u32 srmcfg;
137 #endif
138 };
139 
140 /* Whitelist the fstate from the task_struct for hardened usercopy */
141 static inline void arch_thread_struct_whitelist(unsigned long *offset,
142 						unsigned long *size)
143 {
144 	*offset = offsetof(struct thread_struct, fstate);
145 	*size = sizeof_field(struct thread_struct, fstate);
146 }
147 
148 #define INIT_THREAD {					\
149 	.sp = sizeof(init_stack) + (long)&init_stack,	\
150 	.align_ctl = PR_UNALIGN_NOPRINT,		\
151 }
152 
153 #define task_pt_regs(tsk)						\
154 	((struct pt_regs *)(task_stack_page(tsk) + THREAD_SIZE		\
155 			    - ALIGN(sizeof(struct pt_regs), STACK_ALIGN)))
156 
157 #define KSTK_EIP(tsk)		(task_pt_regs(tsk)->epc)
158 #define KSTK_ESP(tsk)		(task_pt_regs(tsk)->sp)
159 
160 #define PREFETCH_ASM(x)							\
161 	ALTERNATIVE(__nops(1), PREFETCH_R(x, 0), 0,			\
162 		    RISCV_ISA_EXT_ZICBOP, CONFIG_RISCV_ISA_ZICBOP)
163 
164 #define PREFETCHW_ASM(x)						\
165 	ALTERNATIVE(__nops(1), PREFETCH_W(x, 0), 0,			\
166 		    RISCV_ISA_EXT_ZICBOP, CONFIG_RISCV_ISA_ZICBOP)
167 
168 #ifdef CONFIG_RISCV_ISA_ZICBOP
169 #define ARCH_HAS_PREFETCH
170 static inline void prefetch(const void *x)
171 {
172 	__asm__ __volatile__(PREFETCH_ASM(%0) : : "r" (x) : "memory");
173 }
174 
175 #define ARCH_HAS_PREFETCHW
176 static inline void prefetchw(const void *x)
177 {
178 	__asm__ __volatile__(PREFETCHW_ASM(%0) : : "r" (x) : "memory");
179 }
180 #endif /* CONFIG_RISCV_ISA_ZICBOP */
181 
182 /* Do necessary setup to start up a newly executed thread. */
183 extern void start_thread(struct pt_regs *regs,
184 			unsigned long pc, unsigned long sp);
185 
186 extern unsigned long __get_wchan(struct task_struct *p);
187 
188 
189 static inline void wait_for_interrupt(void)
190 {
191 	ALT_RISCV_WFI();
192 }
193 
194 extern phys_addr_t dma32_phys_limit;
195 
196 struct device_node;
197 int riscv_of_processor_hartid(struct device_node *node, unsigned long *hartid);
198 int riscv_early_of_processor_hartid(struct device_node *node, unsigned long *hartid);
199 int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid);
200 
201 extern void riscv_fill_hwcap(void);
202 extern int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src);
203 
204 extern unsigned long signal_minsigstksz __ro_after_init;
205 
206 #ifdef CONFIG_RISCV_ISA_V
207 /* Userspace interface for PR_RISCV_V_{SET,GET}_VS prctl()s: */
208 #define RISCV_V_SET_CONTROL(arg)	riscv_v_vstate_ctrl_set_current(arg)
209 #define RISCV_V_GET_CONTROL()		riscv_v_vstate_ctrl_get_current()
210 extern long riscv_v_vstate_ctrl_set_current(unsigned long arg);
211 extern long riscv_v_vstate_ctrl_get_current(void);
212 #endif /* CONFIG_RISCV_ISA_V */
213 
214 extern int get_unalign_ctl(struct task_struct *tsk, unsigned long addr);
215 extern int set_unalign_ctl(struct task_struct *tsk, unsigned int val);
216 
217 #define GET_UNALIGN_CTL(tsk, addr)	get_unalign_ctl((tsk), (addr))
218 #define SET_UNALIGN_CTL(tsk, val)	set_unalign_ctl((tsk), (val))
219 
220 #define RISCV_SET_ICACHE_FLUSH_CTX(arg1, arg2)	riscv_set_icache_flush_ctx(arg1, arg2)
221 extern int riscv_set_icache_flush_ctx(unsigned long ctx, unsigned long per_thread);
222 
223 #ifdef CONFIG_RISCV_ISA_SUPM
224 /* PR_{SET,GET}_TAGGED_ADDR_CTRL prctl */
225 long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg);
226 long get_tagged_addr_ctrl(struct task_struct *task);
227 #define SET_TAGGED_ADDR_CTRL(arg)	set_tagged_addr_ctrl(current, arg)
228 #define GET_TAGGED_ADDR_CTRL()		get_tagged_addr_ctrl(current)
229 
230 bool riscv_have_user_pmlen(u8 len);
231 #else
232 static inline bool riscv_have_user_pmlen(u8 len) { return false; }
233 #endif
234 
235 #endif /* __ASSEMBLER__ */
236 
237 #endif /* _ASM_RISCV_PROCESSOR_H */
238