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