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