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