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