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