xref: /linux/arch/arm64/kernel/process.c (revision 2993c9b04e616df0848b655d7202a707a70fc876)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Based on arch/arm/kernel/process.c
4  *
5  * Original Copyright (C) 1995  Linus Torvalds
6  * Copyright (C) 1996-2000 Russell King - Converted to ARM.
7  * Copyright (C) 2012 ARM Ltd.
8  */
9 
10 #include <stdarg.h>
11 
12 #include <linux/compat.h>
13 #include <linux/efi.h>
14 #include <linux/export.h>
15 #include <linux/sched.h>
16 #include <linux/sched/debug.h>
17 #include <linux/sched/task.h>
18 #include <linux/sched/task_stack.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/stddef.h>
22 #include <linux/sysctl.h>
23 #include <linux/unistd.h>
24 #include <linux/user.h>
25 #include <linux/delay.h>
26 #include <linux/reboot.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 #include <linux/cpu.h>
30 #include <linux/elfcore.h>
31 #include <linux/pm.h>
32 #include <linux/tick.h>
33 #include <linux/utsname.h>
34 #include <linux/uaccess.h>
35 #include <linux/random.h>
36 #include <linux/hw_breakpoint.h>
37 #include <linux/personality.h>
38 #include <linux/notifier.h>
39 #include <trace/events/power.h>
40 #include <linux/percpu.h>
41 #include <linux/thread_info.h>
42 #include <linux/prctl.h>
43 
44 #include <asm/alternative.h>
45 #include <asm/arch_gicv3.h>
46 #include <asm/compat.h>
47 #include <asm/cacheflush.h>
48 #include <asm/exec.h>
49 #include <asm/fpsimd.h>
50 #include <asm/mmu_context.h>
51 #include <asm/processor.h>
52 #include <asm/pointer_auth.h>
53 #include <asm/stacktrace.h>
54 
55 #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK)
56 #include <linux/stackprotector.h>
57 unsigned long __stack_chk_guard __read_mostly;
58 EXPORT_SYMBOL(__stack_chk_guard);
59 #endif
60 
61 /*
62  * Function pointers to optional machine specific functions
63  */
64 void (*pm_power_off)(void);
65 EXPORT_SYMBOL_GPL(pm_power_off);
66 
67 void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
68 
69 static void __cpu_do_idle(void)
70 {
71 	dsb(sy);
72 	wfi();
73 }
74 
75 static void __cpu_do_idle_irqprio(void)
76 {
77 	unsigned long pmr;
78 	unsigned long daif_bits;
79 
80 	daif_bits = read_sysreg(daif);
81 	write_sysreg(daif_bits | PSR_I_BIT, daif);
82 
83 	/*
84 	 * Unmask PMR before going idle to make sure interrupts can
85 	 * be raised.
86 	 */
87 	pmr = gic_read_pmr();
88 	gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
89 
90 	__cpu_do_idle();
91 
92 	gic_write_pmr(pmr);
93 	write_sysreg(daif_bits, daif);
94 }
95 
96 /*
97  *	cpu_do_idle()
98  *
99  *	Idle the processor (wait for interrupt).
100  *
101  *	If the CPU supports priority masking we must do additional work to
102  *	ensure that interrupts are not masked at the PMR (because the core will
103  *	not wake up if we block the wake up signal in the interrupt controller).
104  */
105 void cpu_do_idle(void)
106 {
107 	if (system_uses_irq_prio_masking())
108 		__cpu_do_idle_irqprio();
109 	else
110 		__cpu_do_idle();
111 }
112 
113 /*
114  * This is our default idle handler.
115  */
116 void arch_cpu_idle(void)
117 {
118 	/*
119 	 * This should do all the clock switching and wait for interrupt
120 	 * tricks
121 	 */
122 	trace_cpu_idle_rcuidle(1, smp_processor_id());
123 	cpu_do_idle();
124 	local_irq_enable();
125 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
126 }
127 
128 #ifdef CONFIG_HOTPLUG_CPU
129 void arch_cpu_idle_dead(void)
130 {
131        cpu_die();
132 }
133 #endif
134 
135 /*
136  * Called by kexec, immediately prior to machine_kexec().
137  *
138  * This must completely disable all secondary CPUs; simply causing those CPUs
139  * to execute e.g. a RAM-based pin loop is not sufficient. This allows the
140  * kexec'd kernel to use any and all RAM as it sees fit, without having to
141  * avoid any code or data used by any SW CPU pin loop. The CPU hotplug
142  * functionality embodied in disable_nonboot_cpus() to achieve this.
143  */
144 void machine_shutdown(void)
145 {
146 	disable_nonboot_cpus();
147 }
148 
149 /*
150  * Halting simply requires that the secondary CPUs stop performing any
151  * activity (executing tasks, handling interrupts). smp_send_stop()
152  * achieves this.
153  */
154 void machine_halt(void)
155 {
156 	local_irq_disable();
157 	smp_send_stop();
158 	while (1);
159 }
160 
161 /*
162  * Power-off simply requires that the secondary CPUs stop performing any
163  * activity (executing tasks, handling interrupts). smp_send_stop()
164  * achieves this. When the system power is turned off, it will take all CPUs
165  * with it.
166  */
167 void machine_power_off(void)
168 {
169 	local_irq_disable();
170 	smp_send_stop();
171 	if (pm_power_off)
172 		pm_power_off();
173 }
174 
175 /*
176  * Restart requires that the secondary CPUs stop performing any activity
177  * while the primary CPU resets the system. Systems with multiple CPUs must
178  * provide a HW restart implementation, to ensure that all CPUs reset at once.
179  * This is required so that any code running after reset on the primary CPU
180  * doesn't have to co-ordinate with other CPUs to ensure they aren't still
181  * executing pre-reset code, and using RAM that the primary CPU's code wishes
182  * to use. Implementing such co-ordination would be essentially impossible.
183  */
184 void machine_restart(char *cmd)
185 {
186 	/* Disable interrupts first */
187 	local_irq_disable();
188 	smp_send_stop();
189 
190 	/*
191 	 * UpdateCapsule() depends on the system being reset via
192 	 * ResetSystem().
193 	 */
194 	if (efi_enabled(EFI_RUNTIME_SERVICES))
195 		efi_reboot(reboot_mode, NULL);
196 
197 	/* Now call the architecture specific reboot code. */
198 	if (arm_pm_restart)
199 		arm_pm_restart(reboot_mode, cmd);
200 	else
201 		do_kernel_restart(cmd);
202 
203 	/*
204 	 * Whoops - the architecture was unable to reboot.
205 	 */
206 	printk("Reboot failed -- System halted\n");
207 	while (1);
208 }
209 
210 static void print_pstate(struct pt_regs *regs)
211 {
212 	u64 pstate = regs->pstate;
213 
214 	if (compat_user_mode(regs)) {
215 		printk("pstate: %08llx (%c%c%c%c %c %s %s %c%c%c)\n",
216 			pstate,
217 			pstate & PSR_AA32_N_BIT ? 'N' : 'n',
218 			pstate & PSR_AA32_Z_BIT ? 'Z' : 'z',
219 			pstate & PSR_AA32_C_BIT ? 'C' : 'c',
220 			pstate & PSR_AA32_V_BIT ? 'V' : 'v',
221 			pstate & PSR_AA32_Q_BIT ? 'Q' : 'q',
222 			pstate & PSR_AA32_T_BIT ? "T32" : "A32",
223 			pstate & PSR_AA32_E_BIT ? "BE" : "LE",
224 			pstate & PSR_AA32_A_BIT ? 'A' : 'a',
225 			pstate & PSR_AA32_I_BIT ? 'I' : 'i',
226 			pstate & PSR_AA32_F_BIT ? 'F' : 'f');
227 	} else {
228 		printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO)\n",
229 			pstate,
230 			pstate & PSR_N_BIT ? 'N' : 'n',
231 			pstate & PSR_Z_BIT ? 'Z' : 'z',
232 			pstate & PSR_C_BIT ? 'C' : 'c',
233 			pstate & PSR_V_BIT ? 'V' : 'v',
234 			pstate & PSR_D_BIT ? 'D' : 'd',
235 			pstate & PSR_A_BIT ? 'A' : 'a',
236 			pstate & PSR_I_BIT ? 'I' : 'i',
237 			pstate & PSR_F_BIT ? 'F' : 'f',
238 			pstate & PSR_PAN_BIT ? '+' : '-',
239 			pstate & PSR_UAO_BIT ? '+' : '-');
240 	}
241 }
242 
243 void __show_regs(struct pt_regs *regs)
244 {
245 	int i, top_reg;
246 	u64 lr, sp;
247 
248 	if (compat_user_mode(regs)) {
249 		lr = regs->compat_lr;
250 		sp = regs->compat_sp;
251 		top_reg = 12;
252 	} else {
253 		lr = regs->regs[30];
254 		sp = regs->sp;
255 		top_reg = 29;
256 	}
257 
258 	show_regs_print_info(KERN_DEFAULT);
259 	print_pstate(regs);
260 
261 	if (!user_mode(regs)) {
262 		printk("pc : %pS\n", (void *)regs->pc);
263 		printk("lr : %pS\n", (void *)lr);
264 	} else {
265 		printk("pc : %016llx\n", regs->pc);
266 		printk("lr : %016llx\n", lr);
267 	}
268 
269 	printk("sp : %016llx\n", sp);
270 
271 	if (system_uses_irq_prio_masking())
272 		printk("pmr_save: %08llx\n", regs->pmr_save);
273 
274 	i = top_reg;
275 
276 	while (i >= 0) {
277 		printk("x%-2d: %016llx ", i, regs->regs[i]);
278 		i--;
279 
280 		if (i % 2 == 0) {
281 			pr_cont("x%-2d: %016llx ", i, regs->regs[i]);
282 			i--;
283 		}
284 
285 		pr_cont("\n");
286 	}
287 }
288 
289 void show_regs(struct pt_regs * regs)
290 {
291 	__show_regs(regs);
292 	dump_backtrace(regs, NULL);
293 }
294 
295 static void tls_thread_flush(void)
296 {
297 	write_sysreg(0, tpidr_el0);
298 
299 	if (is_compat_task()) {
300 		current->thread.uw.tp_value = 0;
301 
302 		/*
303 		 * We need to ensure ordering between the shadow state and the
304 		 * hardware state, so that we don't corrupt the hardware state
305 		 * with a stale shadow state during context switch.
306 		 */
307 		barrier();
308 		write_sysreg(0, tpidrro_el0);
309 	}
310 }
311 
312 static void flush_tagged_addr_state(void)
313 {
314 	if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI))
315 		clear_thread_flag(TIF_TAGGED_ADDR);
316 }
317 
318 void flush_thread(void)
319 {
320 	fpsimd_flush_thread();
321 	tls_thread_flush();
322 	flush_ptrace_hw_breakpoint(current);
323 	flush_tagged_addr_state();
324 }
325 
326 void release_thread(struct task_struct *dead_task)
327 {
328 }
329 
330 void arch_release_task_struct(struct task_struct *tsk)
331 {
332 	fpsimd_release_task(tsk);
333 }
334 
335 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
336 {
337 	if (current->mm)
338 		fpsimd_preserve_current_state();
339 	*dst = *src;
340 
341 	/* We rely on the above assignment to initialize dst's thread_flags: */
342 	BUILD_BUG_ON(!IS_ENABLED(CONFIG_THREAD_INFO_IN_TASK));
343 
344 	/*
345 	 * Detach src's sve_state (if any) from dst so that it does not
346 	 * get erroneously used or freed prematurely.  dst's sve_state
347 	 * will be allocated on demand later on if dst uses SVE.
348 	 * For consistency, also clear TIF_SVE here: this could be done
349 	 * later in copy_process(), but to avoid tripping up future
350 	 * maintainers it is best not to leave TIF_SVE and sve_state in
351 	 * an inconsistent state, even temporarily.
352 	 */
353 	dst->thread.sve_state = NULL;
354 	clear_tsk_thread_flag(dst, TIF_SVE);
355 
356 	return 0;
357 }
358 
359 asmlinkage void ret_from_fork(void) asm("ret_from_fork");
360 
361 int copy_thread(unsigned long clone_flags, unsigned long stack_start,
362 		unsigned long stk_sz, struct task_struct *p)
363 {
364 	struct pt_regs *childregs = task_pt_regs(p);
365 
366 	memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
367 
368 	/*
369 	 * In case p was allocated the same task_struct pointer as some
370 	 * other recently-exited task, make sure p is disassociated from
371 	 * any cpu that may have run that now-exited task recently.
372 	 * Otherwise we could erroneously skip reloading the FPSIMD
373 	 * registers for p.
374 	 */
375 	fpsimd_flush_task_state(p);
376 
377 	if (likely(!(p->flags & PF_KTHREAD))) {
378 		*childregs = *current_pt_regs();
379 		childregs->regs[0] = 0;
380 
381 		/*
382 		 * Read the current TLS pointer from tpidr_el0 as it may be
383 		 * out-of-sync with the saved value.
384 		 */
385 		*task_user_tls(p) = read_sysreg(tpidr_el0);
386 
387 		if (stack_start) {
388 			if (is_compat_thread(task_thread_info(p)))
389 				childregs->compat_sp = stack_start;
390 			else
391 				childregs->sp = stack_start;
392 		}
393 
394 		/*
395 		 * If a TLS pointer was passed to clone (4th argument), use it
396 		 * for the new thread.
397 		 */
398 		if (clone_flags & CLONE_SETTLS)
399 			p->thread.uw.tp_value = childregs->regs[3];
400 	} else {
401 		memset(childregs, 0, sizeof(struct pt_regs));
402 		childregs->pstate = PSR_MODE_EL1h;
403 		if (IS_ENABLED(CONFIG_ARM64_UAO) &&
404 		    cpus_have_const_cap(ARM64_HAS_UAO))
405 			childregs->pstate |= PSR_UAO_BIT;
406 
407 		if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE)
408 			set_ssbs_bit(childregs);
409 
410 		if (system_uses_irq_prio_masking())
411 			childregs->pmr_save = GIC_PRIO_IRQON;
412 
413 		p->thread.cpu_context.x19 = stack_start;
414 		p->thread.cpu_context.x20 = stk_sz;
415 	}
416 	p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
417 	p->thread.cpu_context.sp = (unsigned long)childregs;
418 
419 	ptrace_hw_copy_thread(p);
420 
421 	return 0;
422 }
423 
424 void tls_preserve_current_state(void)
425 {
426 	*task_user_tls(current) = read_sysreg(tpidr_el0);
427 }
428 
429 static void tls_thread_switch(struct task_struct *next)
430 {
431 	tls_preserve_current_state();
432 
433 	if (is_compat_thread(task_thread_info(next)))
434 		write_sysreg(next->thread.uw.tp_value, tpidrro_el0);
435 	else if (!arm64_kernel_unmapped_at_el0())
436 		write_sysreg(0, tpidrro_el0);
437 
438 	write_sysreg(*task_user_tls(next), tpidr_el0);
439 }
440 
441 /* Restore the UAO state depending on next's addr_limit */
442 void uao_thread_switch(struct task_struct *next)
443 {
444 	if (IS_ENABLED(CONFIG_ARM64_UAO)) {
445 		if (task_thread_info(next)->addr_limit == KERNEL_DS)
446 			asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
447 		else
448 			asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO));
449 	}
450 }
451 
452 /*
453  * Force SSBS state on context-switch, since it may be lost after migrating
454  * from a CPU which treats the bit as RES0 in a heterogeneous system.
455  */
456 static void ssbs_thread_switch(struct task_struct *next)
457 {
458 	struct pt_regs *regs = task_pt_regs(next);
459 
460 	/*
461 	 * Nothing to do for kernel threads, but 'regs' may be junk
462 	 * (e.g. idle task) so check the flags and bail early.
463 	 */
464 	if (unlikely(next->flags & PF_KTHREAD))
465 		return;
466 
467 	/* If the mitigation is enabled, then we leave SSBS clear. */
468 	if ((arm64_get_ssbd_state() == ARM64_SSBD_FORCE_ENABLE) ||
469 	    test_tsk_thread_flag(next, TIF_SSBD))
470 		return;
471 
472 	if (compat_user_mode(regs))
473 		set_compat_ssbs_bit(regs);
474 	else if (user_mode(regs))
475 		set_ssbs_bit(regs);
476 }
477 
478 /*
479  * We store our current task in sp_el0, which is clobbered by userspace. Keep a
480  * shadow copy so that we can restore this upon entry from userspace.
481  *
482  * This is *only* for exception entry from EL0, and is not valid until we
483  * __switch_to() a user task.
484  */
485 DEFINE_PER_CPU(struct task_struct *, __entry_task);
486 
487 static void entry_task_switch(struct task_struct *next)
488 {
489 	__this_cpu_write(__entry_task, next);
490 }
491 
492 /*
493  * Thread switching.
494  */
495 __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
496 				struct task_struct *next)
497 {
498 	struct task_struct *last;
499 
500 	fpsimd_thread_switch(next);
501 	tls_thread_switch(next);
502 	hw_breakpoint_thread_switch(next);
503 	contextidr_thread_switch(next);
504 	entry_task_switch(next);
505 	uao_thread_switch(next);
506 	ptrauth_thread_switch(next);
507 	ssbs_thread_switch(next);
508 
509 	/*
510 	 * Complete any pending TLB or cache maintenance on this CPU in case
511 	 * the thread migrates to a different CPU.
512 	 * This full barrier is also required by the membarrier system
513 	 * call.
514 	 */
515 	dsb(ish);
516 
517 	/* the actual thread switch */
518 	last = cpu_switch_to(prev, next);
519 
520 	return last;
521 }
522 
523 unsigned long get_wchan(struct task_struct *p)
524 {
525 	struct stackframe frame;
526 	unsigned long stack_page, ret = 0;
527 	int count = 0;
528 	if (!p || p == current || p->state == TASK_RUNNING)
529 		return 0;
530 
531 	stack_page = (unsigned long)try_get_task_stack(p);
532 	if (!stack_page)
533 		return 0;
534 
535 	start_backtrace(&frame, thread_saved_fp(p), thread_saved_pc(p));
536 
537 	do {
538 		if (unwind_frame(p, &frame))
539 			goto out;
540 		if (!in_sched_functions(frame.pc)) {
541 			ret = frame.pc;
542 			goto out;
543 		}
544 	} while (count ++ < 16);
545 
546 out:
547 	put_task_stack(p);
548 	return ret;
549 }
550 
551 unsigned long arch_align_stack(unsigned long sp)
552 {
553 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
554 		sp -= get_random_int() & ~PAGE_MASK;
555 	return sp & ~0xf;
556 }
557 
558 /*
559  * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY.
560  */
561 void arch_setup_new_exec(void)
562 {
563 	current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0;
564 
565 	ptrauth_thread_init_user(current);
566 }
567 
568 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
569 /*
570  * Control the relaxed ABI allowing tagged user addresses into the kernel.
571  */
572 static unsigned int tagged_addr_disabled;
573 
574 long set_tagged_addr_ctrl(unsigned long arg)
575 {
576 	if (is_compat_task())
577 		return -EINVAL;
578 	if (arg & ~PR_TAGGED_ADDR_ENABLE)
579 		return -EINVAL;
580 
581 	/*
582 	 * Do not allow the enabling of the tagged address ABI if globally
583 	 * disabled via sysctl abi.tagged_addr_disabled.
584 	 */
585 	if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled)
586 		return -EINVAL;
587 
588 	update_thread_flag(TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE);
589 
590 	return 0;
591 }
592 
593 long get_tagged_addr_ctrl(void)
594 {
595 	if (is_compat_task())
596 		return -EINVAL;
597 
598 	if (test_thread_flag(TIF_TAGGED_ADDR))
599 		return PR_TAGGED_ADDR_ENABLE;
600 
601 	return 0;
602 }
603 
604 /*
605  * Global sysctl to disable the tagged user addresses support. This control
606  * only prevents the tagged address ABI enabling via prctl() and does not
607  * disable it for tasks that already opted in to the relaxed ABI.
608  */
609 static int zero;
610 static int one = 1;
611 
612 static struct ctl_table tagged_addr_sysctl_table[] = {
613 	{
614 		.procname	= "tagged_addr_disabled",
615 		.mode		= 0644,
616 		.data		= &tagged_addr_disabled,
617 		.maxlen		= sizeof(int),
618 		.proc_handler	= proc_dointvec_minmax,
619 		.extra1		= &zero,
620 		.extra2		= &one,
621 	},
622 	{ }
623 };
624 
625 static int __init tagged_addr_init(void)
626 {
627 	if (!register_sysctl("abi", tagged_addr_sysctl_table))
628 		return -EINVAL;
629 	return 0;
630 }
631 
632 core_initcall(tagged_addr_init);
633 #endif	/* CONFIG_ARM64_TAGGED_ADDR_ABI */
634