1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* |
3 | * Author: Huacai Chen <chenhuacai@loongson.cn> |
4 | * Copyright (C) 2020-2022 Loongson Technology Corporation Limited |
5 | * |
6 | * Derived from MIPS: |
7 | * Copyright (C) 1994 - 1999, 2000 by Ralf Baechle and others. |
8 | * Copyright (C) 2005, 2006 by Ralf Baechle (ralf@linux-mips.org) |
9 | * Copyright (C) 1999, 2000 Silicon Graphics, Inc. |
10 | * Copyright (C) 2004 Thiemo Seufer |
11 | * Copyright (C) 2013 Imagination Technologies Ltd. |
12 | */ |
13 | #include <linux/cpu.h> |
14 | #include <linux/init.h> |
15 | #include <linux/kernel.h> |
16 | #include <linux/errno.h> |
17 | #include <linux/sched.h> |
18 | #include <linux/sched/debug.h> |
19 | #include <linux/sched/task.h> |
20 | #include <linux/sched/task_stack.h> |
21 | #include <linux/hw_breakpoint.h> |
22 | #include <linux/mm.h> |
23 | #include <linux/stddef.h> |
24 | #include <linux/unistd.h> |
25 | #include <linux/export.h> |
26 | #include <linux/ptrace.h> |
27 | #include <linux/mman.h> |
28 | #include <linux/personality.h> |
29 | #include <linux/sys.h> |
30 | #include <linux/completion.h> |
31 | #include <linux/kallsyms.h> |
32 | #include <linux/random.h> |
33 | #include <linux/prctl.h> |
34 | #include <linux/nmi.h> |
35 | |
36 | #include <asm/asm.h> |
37 | #include <asm/bootinfo.h> |
38 | #include <asm/cpu.h> |
39 | #include <asm/elf.h> |
40 | #include <asm/exec.h> |
41 | #include <asm/fpu.h> |
42 | #include <asm/lbt.h> |
43 | #include <asm/io.h> |
44 | #include <asm/irq.h> |
45 | #include <asm/irq_regs.h> |
46 | #include <asm/loongarch.h> |
47 | #include <asm/pgtable.h> |
48 | #include <asm/processor.h> |
49 | #include <asm/reg.h> |
50 | #include <asm/unwind.h> |
51 | #include <asm/vdso.h> |
52 | |
53 | #ifdef CONFIG_STACKPROTECTOR |
54 | #include <linux/stackprotector.h> |
55 | unsigned long __stack_chk_guard __read_mostly; |
56 | EXPORT_SYMBOL(__stack_chk_guard); |
57 | #endif |
58 | |
59 | /* |
60 | * Idle related variables and functions |
61 | */ |
62 | |
63 | unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE; |
64 | EXPORT_SYMBOL(boot_option_idle_override); |
65 | |
66 | asmlinkage void ret_from_fork(void); |
67 | asmlinkage void ret_from_kernel_thread(void); |
68 | |
69 | void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp) |
70 | { |
71 | unsigned long crmd; |
72 | unsigned long prmd; |
73 | unsigned long euen; |
74 | |
75 | /* New thread loses kernel privileges. */ |
76 | crmd = regs->csr_crmd & ~(PLV_MASK); |
77 | crmd |= PLV_USER; |
78 | regs->csr_crmd = crmd; |
79 | |
80 | prmd = regs->csr_prmd & ~(PLV_MASK); |
81 | prmd |= PLV_USER; |
82 | regs->csr_prmd = prmd; |
83 | |
84 | euen = regs->csr_euen & ~(CSR_EUEN_FPEN); |
85 | regs->csr_euen = euen; |
86 | lose_fpu(0); |
87 | lose_lbt(0); |
88 | |
89 | clear_thread_flag(TIF_LSX_CTX_LIVE); |
90 | clear_thread_flag(TIF_LASX_CTX_LIVE); |
91 | clear_thread_flag(TIF_LBT_CTX_LIVE); |
92 | clear_used_math(); |
93 | regs->csr_era = pc; |
94 | regs->regs[3] = sp; |
95 | } |
96 | |
97 | void flush_thread(void) |
98 | { |
99 | flush_ptrace_hw_breakpoint(current); |
100 | } |
101 | |
102 | void exit_thread(struct task_struct *tsk) |
103 | { |
104 | } |
105 | |
106 | int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) |
107 | { |
108 | /* |
109 | * Save any process state which is live in hardware registers to the |
110 | * parent context prior to duplication. This prevents the new child |
111 | * state becoming stale if the parent is preempted before copy_thread() |
112 | * gets a chance to save the parent's live hardware registers to the |
113 | * child context. |
114 | */ |
115 | preempt_disable(); |
116 | |
117 | if (is_fpu_owner()) { |
118 | if (is_lasx_enabled()) |
119 | save_lasx(current); |
120 | else if (is_lsx_enabled()) |
121 | save_lsx(current); |
122 | else |
123 | save_fp(current); |
124 | } |
125 | |
126 | preempt_enable(); |
127 | |
128 | if (!used_math()) |
129 | memcpy(dst, src, offsetof(struct task_struct, thread.fpu.fpr)); |
130 | else |
131 | memcpy(dst, src, offsetof(struct task_struct, thread.lbt.scr0)); |
132 | |
133 | #ifdef CONFIG_CPU_HAS_LBT |
134 | memcpy(&dst->thread.lbt, &src->thread.lbt, sizeof(struct loongarch_lbt)); |
135 | #endif |
136 | |
137 | return 0; |
138 | } |
139 | |
140 | /* |
141 | * Copy architecture-specific thread state |
142 | */ |
143 | int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) |
144 | { |
145 | unsigned long childksp; |
146 | unsigned long tls = args->tls; |
147 | unsigned long usp = args->stack; |
148 | unsigned long clone_flags = args->flags; |
149 | struct pt_regs *childregs, *regs = current_pt_regs(); |
150 | |
151 | childksp = (unsigned long)task_stack_page(task: p) + THREAD_SIZE; |
152 | |
153 | /* set up new TSS. */ |
154 | childregs = (struct pt_regs *) childksp - 1; |
155 | /* Put the stack after the struct pt_regs. */ |
156 | childksp = (unsigned long) childregs; |
157 | p->thread.sched_cfa = 0; |
158 | p->thread.csr_euen = 0; |
159 | p->thread.csr_crmd = csr_read32(LOONGARCH_CSR_CRMD); |
160 | p->thread.csr_prmd = csr_read32(LOONGARCH_CSR_PRMD); |
161 | p->thread.csr_ecfg = csr_read32(LOONGARCH_CSR_ECFG); |
162 | if (unlikely(args->fn)) { |
163 | /* kernel thread */ |
164 | p->thread.reg03 = childksp; |
165 | p->thread.reg23 = (unsigned long)args->fn; |
166 | p->thread.reg24 = (unsigned long)args->fn_arg; |
167 | p->thread.reg01 = (unsigned long)ret_from_kernel_thread; |
168 | p->thread.sched_ra = (unsigned long)ret_from_kernel_thread; |
169 | memset(childregs, 0, sizeof(struct pt_regs)); |
170 | childregs->csr_euen = p->thread.csr_euen; |
171 | childregs->csr_crmd = p->thread.csr_crmd; |
172 | childregs->csr_prmd = p->thread.csr_prmd; |
173 | childregs->csr_ecfg = p->thread.csr_ecfg; |
174 | goto out; |
175 | } |
176 | |
177 | /* user thread */ |
178 | *childregs = *regs; |
179 | childregs->regs[4] = 0; /* Child gets zero as return value */ |
180 | if (usp) |
181 | childregs->regs[3] = usp; |
182 | |
183 | p->thread.reg03 = (unsigned long) childregs; |
184 | p->thread.reg01 = (unsigned long) ret_from_fork; |
185 | p->thread.sched_ra = (unsigned long) ret_from_fork; |
186 | |
187 | /* |
188 | * New tasks lose permission to use the fpu. This accelerates context |
189 | * switching for most programs since they don't use the fpu. |
190 | */ |
191 | childregs->csr_euen = 0; |
192 | |
193 | if (clone_flags & CLONE_SETTLS) |
194 | childregs->regs[2] = tls; |
195 | |
196 | out: |
197 | ptrace_hw_copy_thread(p); |
198 | clear_tsk_thread_flag(p, TIF_USEDFPU); |
199 | clear_tsk_thread_flag(p, TIF_USEDSIMD); |
200 | clear_tsk_thread_flag(p, TIF_USEDLBT); |
201 | clear_tsk_thread_flag(p, TIF_LSX_CTX_LIVE); |
202 | clear_tsk_thread_flag(p, TIF_LASX_CTX_LIVE); |
203 | clear_tsk_thread_flag(p, TIF_LBT_CTX_LIVE); |
204 | |
205 | return 0; |
206 | } |
207 | |
208 | unsigned long __get_wchan(struct task_struct *task) |
209 | { |
210 | unsigned long pc = 0; |
211 | struct unwind_state state; |
212 | |
213 | if (!try_get_task_stack(tsk: task)) |
214 | return 0; |
215 | |
216 | for (unwind_start(&state, task, NULL); |
217 | !unwind_done(state: &state); unwind_next_frame(state: &state)) { |
218 | pc = unwind_get_return_address(state: &state); |
219 | if (!pc) |
220 | break; |
221 | if (in_sched_functions(addr: pc)) |
222 | continue; |
223 | break; |
224 | } |
225 | |
226 | put_task_stack(tsk: task); |
227 | |
228 | return pc; |
229 | } |
230 | |
231 | bool in_irq_stack(unsigned long stack, struct stack_info *info) |
232 | { |
233 | unsigned long nextsp; |
234 | unsigned long begin = (unsigned long)this_cpu_read(irq_stack); |
235 | unsigned long end = begin + IRQ_STACK_START; |
236 | |
237 | if (stack < begin || stack >= end) |
238 | return false; |
239 | |
240 | nextsp = *(unsigned long *)end; |
241 | if (nextsp & (SZREG - 1)) |
242 | return false; |
243 | |
244 | info->begin = begin; |
245 | info->end = end; |
246 | info->next_sp = nextsp; |
247 | info->type = STACK_TYPE_IRQ; |
248 | |
249 | return true; |
250 | } |
251 | |
252 | bool in_task_stack(unsigned long stack, struct task_struct *task, |
253 | struct stack_info *info) |
254 | { |
255 | unsigned long begin = (unsigned long)task_stack_page(task); |
256 | unsigned long end = begin + THREAD_SIZE; |
257 | |
258 | if (stack < begin || stack >= end) |
259 | return false; |
260 | |
261 | info->begin = begin; |
262 | info->end = end; |
263 | info->next_sp = 0; |
264 | info->type = STACK_TYPE_TASK; |
265 | |
266 | return true; |
267 | } |
268 | |
269 | int get_stack_info(unsigned long stack, struct task_struct *task, |
270 | struct stack_info *info) |
271 | { |
272 | task = task ? : current; |
273 | |
274 | if (!stack || stack & (SZREG - 1)) |
275 | goto unknown; |
276 | |
277 | if (in_task_stack(stack, task, info)) |
278 | return 0; |
279 | |
280 | if (task != current) |
281 | goto unknown; |
282 | |
283 | if (in_irq_stack(stack, info)) |
284 | return 0; |
285 | |
286 | unknown: |
287 | info->type = STACK_TYPE_UNKNOWN; |
288 | return -EINVAL; |
289 | } |
290 | |
291 | unsigned long stack_top(void) |
292 | { |
293 | unsigned long top = TASK_SIZE & PAGE_MASK; |
294 | |
295 | /* Space for the VDSO & data page */ |
296 | top -= PAGE_ALIGN(current->thread.vdso->size); |
297 | top -= VVAR_SIZE; |
298 | |
299 | /* Space to randomize the VDSO base */ |
300 | if (current->flags & PF_RANDOMIZE) |
301 | top -= VDSO_RANDOMIZE_SIZE; |
302 | |
303 | return top; |
304 | } |
305 | |
306 | /* |
307 | * Don't forget that the stack pointer must be aligned on a 8 bytes |
308 | * boundary for 32-bits ABI and 16 bytes for 64-bits ABI. |
309 | */ |
310 | unsigned long arch_align_stack(unsigned long sp) |
311 | { |
312 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) |
313 | sp -= get_random_u32_below(PAGE_SIZE); |
314 | |
315 | return sp & STACK_ALIGN; |
316 | } |
317 | |
318 | static DEFINE_PER_CPU(call_single_data_t, backtrace_csd); |
319 | static struct cpumask backtrace_csd_busy; |
320 | |
321 | static void handle_backtrace(void *info) |
322 | { |
323 | nmi_cpu_backtrace(regs: get_irq_regs()); |
324 | cpumask_clear_cpu(smp_processor_id(), dstp: &backtrace_csd_busy); |
325 | } |
326 | |
327 | static void raise_backtrace(cpumask_t *mask) |
328 | { |
329 | call_single_data_t *csd; |
330 | int cpu; |
331 | |
332 | for_each_cpu(cpu, mask) { |
333 | /* |
334 | * If we previously sent an IPI to the target CPU & it hasn't |
335 | * cleared its bit in the busy cpumask then it didn't handle |
336 | * our previous IPI & it's not safe for us to reuse the |
337 | * call_single_data_t. |
338 | */ |
339 | if (cpumask_test_and_set_cpu(cpu, cpumask: &backtrace_csd_busy)) { |
340 | pr_warn("Unable to send backtrace IPI to CPU%u - perhaps it hung?\n" , |
341 | cpu); |
342 | continue; |
343 | } |
344 | |
345 | csd = &per_cpu(backtrace_csd, cpu); |
346 | csd->func = handle_backtrace; |
347 | smp_call_function_single_async(cpu, csd); |
348 | } |
349 | } |
350 | |
351 | void arch_trigger_cpumask_backtrace(const cpumask_t *mask, int exclude_cpu) |
352 | { |
353 | nmi_trigger_cpumask_backtrace(mask, exclude_cpu, raise: raise_backtrace); |
354 | } |
355 | |
356 | #ifdef CONFIG_64BIT |
357 | void loongarch_dump_regs64(u64 *uregs, const struct pt_regs *regs) |
358 | { |
359 | unsigned int i; |
360 | |
361 | for (i = LOONGARCH_EF_R1; i <= LOONGARCH_EF_R31; i++) { |
362 | uregs[i] = regs->regs[i - LOONGARCH_EF_R0]; |
363 | } |
364 | |
365 | uregs[LOONGARCH_EF_ORIG_A0] = regs->orig_a0; |
366 | uregs[LOONGARCH_EF_CSR_ERA] = regs->csr_era; |
367 | uregs[LOONGARCH_EF_CSR_BADV] = regs->csr_badvaddr; |
368 | uregs[LOONGARCH_EF_CSR_CRMD] = regs->csr_crmd; |
369 | uregs[LOONGARCH_EF_CSR_PRMD] = regs->csr_prmd; |
370 | uregs[LOONGARCH_EF_CSR_EUEN] = regs->csr_euen; |
371 | uregs[LOONGARCH_EF_CSR_ECFG] = regs->csr_ecfg; |
372 | uregs[LOONGARCH_EF_CSR_ESTAT] = regs->csr_estat; |
373 | } |
374 | #endif /* CONFIG_64BIT */ |
375 | |