xref: /linux/arch/loongarch/kernel/process.c (revision 32bd966050486d3fed6980aa3de3e60b9e383589)
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 	if (!used_math())
140 		memcpy(dst, src, offsetof(struct task_struct, thread.fpu.fpr));
141 	else
142 		memcpy(dst, src, offsetof(struct task_struct, thread.lbt.scr0));
143 
144 #ifdef CONFIG_CPU_HAS_LBT
145 	memcpy(&dst->thread.lbt, &src->thread.lbt, sizeof(struct loongarch_lbt));
146 #endif
147 
148 	return 0;
149 }
150 
151 asmlinkage void noinstr __no_stack_protector ret_from_fork(struct task_struct *prev,
152 							   struct pt_regs *regs)
153 {
154 	schedule_tail(prev);
155 	syscall_exit_to_user_mode(regs);
156 }
157 
158 asmlinkage void noinstr __no_stack_protector ret_from_kernel_thread(struct task_struct *prev,
159 								    struct pt_regs *regs,
160 								    int (*fn)(void *),
161 								    void *fn_arg)
162 {
163 	schedule_tail(prev);
164 	fn(fn_arg);
165 	syscall_exit_to_user_mode(regs);
166 }
167 
168 /*
169  * Copy architecture-specific thread state
170  */
171 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
172 {
173 	unsigned long childksp;
174 	unsigned long tls = args->tls;
175 	unsigned long usp = args->stack;
176 	u64 clone_flags = args->flags;
177 	struct pt_regs *childregs, *regs = current_pt_regs();
178 
179 	childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
180 
181 	/* set up new TSS. */
182 	childregs = (struct pt_regs *) childksp - 1;
183 	/*  Put the stack after the struct pt_regs.  */
184 	childksp = (unsigned long) childregs;
185 	p->thread.sched_cfa = 0;
186 	p->thread.csr_euen = 0;
187 	p->thread.csr_crmd = csr_read32(LOONGARCH_CSR_CRMD);
188 	p->thread.csr_prmd = csr_read32(LOONGARCH_CSR_PRMD);
189 	p->thread.csr_ecfg = csr_read32(LOONGARCH_CSR_ECFG);
190 	if (unlikely(args->fn)) {
191 		/* kernel thread */
192 		p->thread.reg03 = childksp;
193 		p->thread.reg23 = (unsigned long)args->fn;
194 		p->thread.reg24 = (unsigned long)args->fn_arg;
195 		p->thread.reg01 = (unsigned long)ret_from_kernel_thread_asm;
196 		p->thread.sched_ra = (unsigned long)ret_from_kernel_thread_asm;
197 		memset(childregs, 0, sizeof(struct pt_regs));
198 		childregs->csr_euen = p->thread.csr_euen;
199 		childregs->csr_crmd = p->thread.csr_crmd;
200 		childregs->csr_prmd = p->thread.csr_prmd;
201 		childregs->csr_ecfg = p->thread.csr_ecfg;
202 		goto out;
203 	}
204 
205 	/* user thread */
206 	*childregs = *regs;
207 	childregs->regs[4] = 0; /* Child gets zero as return value */
208 	if (usp)
209 		childregs->regs[3] = usp;
210 
211 	p->thread.reg03 = (unsigned long) childregs;
212 	p->thread.reg01 = (unsigned long) ret_from_fork_asm;
213 	p->thread.sched_ra = (unsigned long) ret_from_fork_asm;
214 
215 	/*
216 	 * New tasks lose permission to use the fpu. This accelerates context
217 	 * switching for most programs since they don't use the fpu.
218 	 */
219 	childregs->csr_euen = 0;
220 
221 	if (clone_flags & CLONE_SETTLS)
222 		childregs->regs[2] = tls;
223 
224 out:
225 	ptrace_hw_copy_thread(p);
226 	clear_tsk_thread_flag(p, TIF_USEDFPU);
227 	clear_tsk_thread_flag(p, TIF_USEDSIMD);
228 	clear_tsk_thread_flag(p, TIF_USEDLBT);
229 	clear_tsk_thread_flag(p, TIF_LSX_CTX_LIVE);
230 	clear_tsk_thread_flag(p, TIF_LASX_CTX_LIVE);
231 	clear_tsk_thread_flag(p, TIF_LBT_CTX_LIVE);
232 
233 	return 0;
234 }
235 
236 unsigned long __get_wchan(struct task_struct *task)
237 {
238 	unsigned long pc = 0;
239 	struct unwind_state state;
240 
241 	if (!try_get_task_stack(task))
242 		return 0;
243 
244 	for (unwind_start(&state, task, NULL);
245 	     !unwind_done(&state); unwind_next_frame(&state)) {
246 		pc = unwind_get_return_address(&state);
247 		if (!pc)
248 			break;
249 		if (in_sched_functions(pc))
250 			continue;
251 		break;
252 	}
253 
254 	put_task_stack(task);
255 
256 	return pc;
257 }
258 
259 bool in_irq_stack(unsigned long stack, struct stack_info *info)
260 {
261 	unsigned long nextsp;
262 	unsigned long begin = (unsigned long)this_cpu_read(irq_stack);
263 	unsigned long end = begin + IRQ_STACK_START;
264 
265 	if (stack < begin || stack >= end)
266 		return false;
267 
268 	nextsp = *(unsigned long *)end;
269 	if (nextsp & (SZREG - 1))
270 		return false;
271 
272 	info->begin = begin;
273 	info->end = end;
274 	info->next_sp = nextsp;
275 	info->type = STACK_TYPE_IRQ;
276 
277 	return true;
278 }
279 
280 bool in_task_stack(unsigned long stack, struct task_struct *task,
281 			struct stack_info *info)
282 {
283 	unsigned long begin = (unsigned long)task_stack_page(task);
284 	unsigned long end = begin + THREAD_SIZE;
285 
286 	if (stack < begin || stack >= end)
287 		return false;
288 
289 	info->begin = begin;
290 	info->end = end;
291 	info->next_sp = 0;
292 	info->type = STACK_TYPE_TASK;
293 
294 	return true;
295 }
296 
297 int get_stack_info(unsigned long stack, struct task_struct *task,
298 		   struct stack_info *info)
299 {
300 	task = task ? : current;
301 
302 	if (!stack || stack & (SZREG - 1))
303 		goto unknown;
304 
305 	if (in_task_stack(stack, task, info))
306 		return 0;
307 
308 	if (task != current)
309 		goto unknown;
310 
311 	if (in_irq_stack(stack, info))
312 		return 0;
313 
314 unknown:
315 	info->type = STACK_TYPE_UNKNOWN;
316 	return -EINVAL;
317 }
318 
319 unsigned long stack_top(void)
320 {
321 	unsigned long top = TASK_SIZE & PAGE_MASK;
322 
323 	if (current->thread.vdso) {
324 		/* Space for the VDSO & data page */
325 		top -= PAGE_ALIGN(current->thread.vdso->size);
326 		top -= VVAR_SIZE;
327 
328 		/* Space to randomize the VDSO base */
329 		if (current->flags & PF_RANDOMIZE)
330 			top -= VDSO_RANDOMIZE_SIZE;
331 	}
332 
333 	return top;
334 }
335 
336 /*
337  * Don't forget that the stack pointer must be aligned on a 8 bytes
338  * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
339  */
340 unsigned long arch_align_stack(unsigned long sp)
341 {
342 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
343 		sp -= get_random_u32_below(PAGE_SIZE);
344 
345 	return sp & STACK_ALIGN;
346 }
347 
348 static DEFINE_PER_CPU(call_single_data_t, backtrace_csd);
349 static struct cpumask backtrace_csd_busy;
350 
351 static void handle_backtrace(void *info)
352 {
353 	nmi_cpu_backtrace(get_irq_regs());
354 	cpumask_clear_cpu(smp_processor_id(), &backtrace_csd_busy);
355 }
356 
357 static void raise_backtrace(cpumask_t *mask)
358 {
359 	call_single_data_t *csd;
360 	int cpu;
361 
362 	for_each_cpu(cpu, mask) {
363 		/*
364 		 * If we previously sent an IPI to the target CPU & it hasn't
365 		 * cleared its bit in the busy cpumask then it didn't handle
366 		 * our previous IPI & it's not safe for us to reuse the
367 		 * call_single_data_t.
368 		 */
369 		if (cpumask_test_and_set_cpu(cpu, &backtrace_csd_busy)) {
370 			pr_warn("Unable to send backtrace IPI to CPU%u - perhaps it hung?\n",
371 				cpu);
372 			continue;
373 		}
374 
375 		csd = &per_cpu(backtrace_csd, cpu);
376 		csd->func = handle_backtrace;
377 		smp_call_function_single_async(cpu, csd);
378 	}
379 }
380 
381 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, int exclude_cpu)
382 {
383 	nmi_trigger_cpumask_backtrace(mask, exclude_cpu, raise_backtrace);
384 }
385 
386 #ifdef CONFIG_32BIT
387 void loongarch_dump_regs32(u32 *uregs, const struct pt_regs *regs)
388 #else
389 void loongarch_dump_regs64(u64 *uregs, const struct pt_regs *regs)
390 #endif
391 {
392 	unsigned int i;
393 
394 	for (i = LOONGARCH_EF_R1; i <= LOONGARCH_EF_R31; i++) {
395 		uregs[i] = regs->regs[i - LOONGARCH_EF_R0];
396 	}
397 
398 	uregs[LOONGARCH_EF_ORIG_A0] = regs->orig_a0;
399 	uregs[LOONGARCH_EF_CSR_ERA] = regs->csr_era;
400 	uregs[LOONGARCH_EF_CSR_BADV] = regs->csr_badvaddr;
401 	uregs[LOONGARCH_EF_CSR_CRMD] = regs->csr_crmd;
402 	uregs[LOONGARCH_EF_CSR_PRMD] = regs->csr_prmd;
403 	uregs[LOONGARCH_EF_CSR_EUEN] = regs->csr_euen;
404 	uregs[LOONGARCH_EF_CSR_ECFG] = regs->csr_ecfg;
405 	uregs[LOONGARCH_EF_CSR_ESTAT] = regs->csr_estat;
406 }
407