File Index
Files and directories
Sign in
FILE

process.c

File preview with syntax highlighting.

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