xref: /linux/arch/arm64/kernel/process.c (revision 714acdbd1c94e7e3ab90f6b6938f1ccb27b662f0)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b3901d54SCatalin Marinas /*
3b3901d54SCatalin Marinas  * Based on arch/arm/kernel/process.c
4b3901d54SCatalin Marinas  *
5b3901d54SCatalin Marinas  * Original Copyright (C) 1995  Linus Torvalds
6b3901d54SCatalin Marinas  * Copyright (C) 1996-2000 Russell King - Converted to ARM.
7b3901d54SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
8b3901d54SCatalin Marinas  */
9b3901d54SCatalin Marinas 
10b3901d54SCatalin Marinas #include <stdarg.h>
11b3901d54SCatalin Marinas 
12fd92d4a5SAKASHI Takahiro #include <linux/compat.h>
1360c0d45aSArd Biesheuvel #include <linux/efi.h>
14ab7876a9SDave Martin #include <linux/elf.h>
15b3901d54SCatalin Marinas #include <linux/export.h>
16b3901d54SCatalin Marinas #include <linux/sched.h>
17b17b0153SIngo Molnar #include <linux/sched/debug.h>
1829930025SIngo Molnar #include <linux/sched/task.h>
1968db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
20b3901d54SCatalin Marinas #include <linux/kernel.h>
2119c95f26SJulien Thierry #include <linux/lockdep.h>
22ab7876a9SDave Martin #include <linux/mman.h>
23b3901d54SCatalin Marinas #include <linux/mm.h>
24b3901d54SCatalin Marinas #include <linux/stddef.h>
2563f0c603SCatalin Marinas #include <linux/sysctl.h>
26b3901d54SCatalin Marinas #include <linux/unistd.h>
27b3901d54SCatalin Marinas #include <linux/user.h>
28b3901d54SCatalin Marinas #include <linux/delay.h>
29b3901d54SCatalin Marinas #include <linux/reboot.h>
30b3901d54SCatalin Marinas #include <linux/interrupt.h>
31b3901d54SCatalin Marinas #include <linux/init.h>
32b3901d54SCatalin Marinas #include <linux/cpu.h>
33b3901d54SCatalin Marinas #include <linux/elfcore.h>
34b3901d54SCatalin Marinas #include <linux/pm.h>
35b3901d54SCatalin Marinas #include <linux/tick.h>
36b3901d54SCatalin Marinas #include <linux/utsname.h>
37b3901d54SCatalin Marinas #include <linux/uaccess.h>
38b3901d54SCatalin Marinas #include <linux/random.h>
39b3901d54SCatalin Marinas #include <linux/hw_breakpoint.h>
40b3901d54SCatalin Marinas #include <linux/personality.h>
41b3901d54SCatalin Marinas #include <linux/notifier.h>
42096b3224SJisheng Zhang #include <trace/events/power.h>
43c02433ddSMark Rutland #include <linux/percpu.h>
44bc0ee476SDave Martin #include <linux/thread_info.h>
4563f0c603SCatalin Marinas #include <linux/prctl.h>
46b3901d54SCatalin Marinas 
4757f4959bSJames Morse #include <asm/alternative.h>
48a9806aa2SJulien Thierry #include <asm/arch_gicv3.h>
49b3901d54SCatalin Marinas #include <asm/compat.h>
5019c95f26SJulien Thierry #include <asm/cpufeature.h>
51b3901d54SCatalin Marinas #include <asm/cacheflush.h>
52d0854412SJames Morse #include <asm/exec.h>
53ec45d1cfSWill Deacon #include <asm/fpsimd.h>
54ec45d1cfSWill Deacon #include <asm/mmu_context.h>
55b3901d54SCatalin Marinas #include <asm/processor.h>
5675031975SMark Rutland #include <asm/pointer_auth.h>
57b3901d54SCatalin Marinas #include <asm/stacktrace.h>
58b3901d54SCatalin Marinas 
590a1213faSArd Biesheuvel #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK)
60c0c264aeSLaura Abbott #include <linux/stackprotector.h>
61c0c264aeSLaura Abbott unsigned long __stack_chk_guard __read_mostly;
62c0c264aeSLaura Abbott EXPORT_SYMBOL(__stack_chk_guard);
63c0c264aeSLaura Abbott #endif
64c0c264aeSLaura Abbott 
65b3901d54SCatalin Marinas /*
66b3901d54SCatalin Marinas  * Function pointers to optional machine specific functions
67b3901d54SCatalin Marinas  */
68b3901d54SCatalin Marinas void (*pm_power_off)(void);
69b3901d54SCatalin Marinas EXPORT_SYMBOL_GPL(pm_power_off);
70b3901d54SCatalin Marinas 
71b0946fc8SCatalin Marinas void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
72b3901d54SCatalin Marinas 
73a9806aa2SJulien Thierry static void __cpu_do_idle(void)
74a9806aa2SJulien Thierry {
75a9806aa2SJulien Thierry 	dsb(sy);
76a9806aa2SJulien Thierry 	wfi();
77a9806aa2SJulien Thierry }
78a9806aa2SJulien Thierry 
79a9806aa2SJulien Thierry static void __cpu_do_idle_irqprio(void)
80a9806aa2SJulien Thierry {
81a9806aa2SJulien Thierry 	unsigned long pmr;
82a9806aa2SJulien Thierry 	unsigned long daif_bits;
83a9806aa2SJulien Thierry 
84a9806aa2SJulien Thierry 	daif_bits = read_sysreg(daif);
85a9806aa2SJulien Thierry 	write_sysreg(daif_bits | PSR_I_BIT, daif);
86a9806aa2SJulien Thierry 
87a9806aa2SJulien Thierry 	/*
88a9806aa2SJulien Thierry 	 * Unmask PMR before going idle to make sure interrupts can
89a9806aa2SJulien Thierry 	 * be raised.
90a9806aa2SJulien Thierry 	 */
91a9806aa2SJulien Thierry 	pmr = gic_read_pmr();
92bd82d4bdSJulien Thierry 	gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
93a9806aa2SJulien Thierry 
94a9806aa2SJulien Thierry 	__cpu_do_idle();
95a9806aa2SJulien Thierry 
96a9806aa2SJulien Thierry 	gic_write_pmr(pmr);
97a9806aa2SJulien Thierry 	write_sysreg(daif_bits, daif);
98a9806aa2SJulien Thierry }
99a9806aa2SJulien Thierry 
100a9806aa2SJulien Thierry /*
101a9806aa2SJulien Thierry  *	cpu_do_idle()
102a9806aa2SJulien Thierry  *
103a9806aa2SJulien Thierry  *	Idle the processor (wait for interrupt).
104a9806aa2SJulien Thierry  *
105a9806aa2SJulien Thierry  *	If the CPU supports priority masking we must do additional work to
106a9806aa2SJulien Thierry  *	ensure that interrupts are not masked at the PMR (because the core will
107a9806aa2SJulien Thierry  *	not wake up if we block the wake up signal in the interrupt controller).
108a9806aa2SJulien Thierry  */
109a9806aa2SJulien Thierry void cpu_do_idle(void)
110a9806aa2SJulien Thierry {
111a9806aa2SJulien Thierry 	if (system_uses_irq_prio_masking())
112a9806aa2SJulien Thierry 		__cpu_do_idle_irqprio();
113a9806aa2SJulien Thierry 	else
114a9806aa2SJulien Thierry 		__cpu_do_idle();
115a9806aa2SJulien Thierry }
116a9806aa2SJulien Thierry 
117b3901d54SCatalin Marinas /*
118b3901d54SCatalin Marinas  * This is our default idle handler.
119b3901d54SCatalin Marinas  */
1200087298fSThomas Gleixner void arch_cpu_idle(void)
121b3901d54SCatalin Marinas {
122b3901d54SCatalin Marinas 	/*
123b3901d54SCatalin Marinas 	 * This should do all the clock switching and wait for interrupt
124b3901d54SCatalin Marinas 	 * tricks
125b3901d54SCatalin Marinas 	 */
126096b3224SJisheng Zhang 	trace_cpu_idle_rcuidle(1, smp_processor_id());
127b3901d54SCatalin Marinas 	cpu_do_idle();
128b3901d54SCatalin Marinas 	local_irq_enable();
129096b3224SJisheng Zhang 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
130b3901d54SCatalin Marinas }
131b3901d54SCatalin Marinas 
1329327e2c6SMark Rutland #ifdef CONFIG_HOTPLUG_CPU
1339327e2c6SMark Rutland void arch_cpu_idle_dead(void)
1349327e2c6SMark Rutland {
1359327e2c6SMark Rutland        cpu_die();
1369327e2c6SMark Rutland }
1379327e2c6SMark Rutland #endif
1389327e2c6SMark Rutland 
13990f51a09SArun KS /*
14090f51a09SArun KS  * Called by kexec, immediately prior to machine_kexec().
14190f51a09SArun KS  *
14290f51a09SArun KS  * This must completely disable all secondary CPUs; simply causing those CPUs
14390f51a09SArun KS  * to execute e.g. a RAM-based pin loop is not sufficient. This allows the
14490f51a09SArun KS  * kexec'd kernel to use any and all RAM as it sees fit, without having to
14590f51a09SArun KS  * avoid any code or data used by any SW CPU pin loop. The CPU hotplug
146d66b16f5SQais Yousef  * functionality embodied in smpt_shutdown_nonboot_cpus() to achieve this.
14790f51a09SArun KS  */
148b3901d54SCatalin Marinas void machine_shutdown(void)
149b3901d54SCatalin Marinas {
1505efbe6a6SQais Yousef 	smp_shutdown_nonboot_cpus(reboot_cpu);
151b3901d54SCatalin Marinas }
152b3901d54SCatalin Marinas 
15390f51a09SArun KS /*
15490f51a09SArun KS  * Halting simply requires that the secondary CPUs stop performing any
15590f51a09SArun KS  * activity (executing tasks, handling interrupts). smp_send_stop()
15690f51a09SArun KS  * achieves this.
15790f51a09SArun KS  */
158b3901d54SCatalin Marinas void machine_halt(void)
159b3901d54SCatalin Marinas {
160b9acc49eSArun KS 	local_irq_disable();
16190f51a09SArun KS 	smp_send_stop();
162b3901d54SCatalin Marinas 	while (1);
163b3901d54SCatalin Marinas }
164b3901d54SCatalin Marinas 
16590f51a09SArun KS /*
16690f51a09SArun KS  * Power-off simply requires that the secondary CPUs stop performing any
16790f51a09SArun KS  * activity (executing tasks, handling interrupts). smp_send_stop()
16890f51a09SArun KS  * achieves this. When the system power is turned off, it will take all CPUs
16990f51a09SArun KS  * with it.
17090f51a09SArun KS  */
171b3901d54SCatalin Marinas void machine_power_off(void)
172b3901d54SCatalin Marinas {
173b9acc49eSArun KS 	local_irq_disable();
17490f51a09SArun KS 	smp_send_stop();
175b3901d54SCatalin Marinas 	if (pm_power_off)
176b3901d54SCatalin Marinas 		pm_power_off();
177b3901d54SCatalin Marinas }
178b3901d54SCatalin Marinas 
17990f51a09SArun KS /*
18090f51a09SArun KS  * Restart requires that the secondary CPUs stop performing any activity
18168234df4SMark Rutland  * while the primary CPU resets the system. Systems with multiple CPUs must
18290f51a09SArun KS  * provide a HW restart implementation, to ensure that all CPUs reset at once.
18390f51a09SArun KS  * This is required so that any code running after reset on the primary CPU
18490f51a09SArun KS  * doesn't have to co-ordinate with other CPUs to ensure they aren't still
18590f51a09SArun KS  * executing pre-reset code, and using RAM that the primary CPU's code wishes
18690f51a09SArun KS  * to use. Implementing such co-ordination would be essentially impossible.
18790f51a09SArun KS  */
188b3901d54SCatalin Marinas void machine_restart(char *cmd)
189b3901d54SCatalin Marinas {
190b3901d54SCatalin Marinas 	/* Disable interrupts first */
191b3901d54SCatalin Marinas 	local_irq_disable();
192b9acc49eSArun KS 	smp_send_stop();
193b3901d54SCatalin Marinas 
19460c0d45aSArd Biesheuvel 	/*
19560c0d45aSArd Biesheuvel 	 * UpdateCapsule() depends on the system being reset via
19660c0d45aSArd Biesheuvel 	 * ResetSystem().
19760c0d45aSArd Biesheuvel 	 */
19860c0d45aSArd Biesheuvel 	if (efi_enabled(EFI_RUNTIME_SERVICES))
19960c0d45aSArd Biesheuvel 		efi_reboot(reboot_mode, NULL);
20060c0d45aSArd Biesheuvel 
201b3901d54SCatalin Marinas 	/* Now call the architecture specific reboot code. */
202aa1e8ec1SCatalin Marinas 	if (arm_pm_restart)
203ff701306SMarc Zyngier 		arm_pm_restart(reboot_mode, cmd);
2041c7ffc32SGuenter Roeck 	else
2051c7ffc32SGuenter Roeck 		do_kernel_restart(cmd);
206b3901d54SCatalin Marinas 
207b3901d54SCatalin Marinas 	/*
208b3901d54SCatalin Marinas 	 * Whoops - the architecture was unable to reboot.
209b3901d54SCatalin Marinas 	 */
210b3901d54SCatalin Marinas 	printk("Reboot failed -- System halted\n");
211b3901d54SCatalin Marinas 	while (1);
212b3901d54SCatalin Marinas }
213b3901d54SCatalin Marinas 
214ec94a46eSDave Martin #define bstr(suffix, str) [PSR_BTYPE_ ## suffix >> PSR_BTYPE_SHIFT] = str
215ec94a46eSDave Martin static const char *const btypes[] = {
216ec94a46eSDave Martin 	bstr(NONE, "--"),
217ec94a46eSDave Martin 	bstr(  JC, "jc"),
218ec94a46eSDave Martin 	bstr(   C, "-c"),
219ec94a46eSDave Martin 	bstr(  J , "j-")
220ec94a46eSDave Martin };
221ec94a46eSDave Martin #undef bstr
222ec94a46eSDave Martin 
223b7300d4cSWill Deacon static void print_pstate(struct pt_regs *regs)
224b7300d4cSWill Deacon {
225b7300d4cSWill Deacon 	u64 pstate = regs->pstate;
226b7300d4cSWill Deacon 
227b7300d4cSWill Deacon 	if (compat_user_mode(regs)) {
228b7300d4cSWill Deacon 		printk("pstate: %08llx (%c%c%c%c %c %s %s %c%c%c)\n",
229b7300d4cSWill Deacon 			pstate,
230d64567f6SMark Rutland 			pstate & PSR_AA32_N_BIT ? 'N' : 'n',
231d64567f6SMark Rutland 			pstate & PSR_AA32_Z_BIT ? 'Z' : 'z',
232d64567f6SMark Rutland 			pstate & PSR_AA32_C_BIT ? 'C' : 'c',
233d64567f6SMark Rutland 			pstate & PSR_AA32_V_BIT ? 'V' : 'v',
234d64567f6SMark Rutland 			pstate & PSR_AA32_Q_BIT ? 'Q' : 'q',
235d64567f6SMark Rutland 			pstate & PSR_AA32_T_BIT ? "T32" : "A32",
236d64567f6SMark Rutland 			pstate & PSR_AA32_E_BIT ? "BE" : "LE",
237d64567f6SMark Rutland 			pstate & PSR_AA32_A_BIT ? 'A' : 'a',
238d64567f6SMark Rutland 			pstate & PSR_AA32_I_BIT ? 'I' : 'i',
239d64567f6SMark Rutland 			pstate & PSR_AA32_F_BIT ? 'F' : 'f');
240b7300d4cSWill Deacon 	} else {
241ec94a46eSDave Martin 		const char *btype_str = btypes[(pstate & PSR_BTYPE_MASK) >>
242ec94a46eSDave Martin 					       PSR_BTYPE_SHIFT];
243ec94a46eSDave Martin 
244ec94a46eSDave Martin 		printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO BTYPE=%s)\n",
245b7300d4cSWill Deacon 			pstate,
246b7300d4cSWill Deacon 			pstate & PSR_N_BIT ? 'N' : 'n',
247b7300d4cSWill Deacon 			pstate & PSR_Z_BIT ? 'Z' : 'z',
248b7300d4cSWill Deacon 			pstate & PSR_C_BIT ? 'C' : 'c',
249b7300d4cSWill Deacon 			pstate & PSR_V_BIT ? 'V' : 'v',
250b7300d4cSWill Deacon 			pstate & PSR_D_BIT ? 'D' : 'd',
251b7300d4cSWill Deacon 			pstate & PSR_A_BIT ? 'A' : 'a',
252b7300d4cSWill Deacon 			pstate & PSR_I_BIT ? 'I' : 'i',
253b7300d4cSWill Deacon 			pstate & PSR_F_BIT ? 'F' : 'f',
254b7300d4cSWill Deacon 			pstate & PSR_PAN_BIT ? '+' : '-',
255ec94a46eSDave Martin 			pstate & PSR_UAO_BIT ? '+' : '-',
256ec94a46eSDave Martin 			btype_str);
257b7300d4cSWill Deacon 	}
258b7300d4cSWill Deacon }
259b7300d4cSWill Deacon 
260b3901d54SCatalin Marinas void __show_regs(struct pt_regs *regs)
261b3901d54SCatalin Marinas {
2626ca68e80SCatalin Marinas 	int i, top_reg;
2636ca68e80SCatalin Marinas 	u64 lr, sp;
2646ca68e80SCatalin Marinas 
2656ca68e80SCatalin Marinas 	if (compat_user_mode(regs)) {
2666ca68e80SCatalin Marinas 		lr = regs->compat_lr;
2676ca68e80SCatalin Marinas 		sp = regs->compat_sp;
2686ca68e80SCatalin Marinas 		top_reg = 12;
2696ca68e80SCatalin Marinas 	} else {
2706ca68e80SCatalin Marinas 		lr = regs->regs[30];
2716ca68e80SCatalin Marinas 		sp = regs->sp;
2726ca68e80SCatalin Marinas 		top_reg = 29;
2736ca68e80SCatalin Marinas 	}
274b3901d54SCatalin Marinas 
275a43cb95dSTejun Heo 	show_regs_print_info(KERN_DEFAULT);
276b7300d4cSWill Deacon 	print_pstate(regs);
277a06f818aSWill Deacon 
278a06f818aSWill Deacon 	if (!user_mode(regs)) {
2794ef79638SSergey Senozhatsky 		printk("pc : %pS\n", (void *)regs->pc);
280cdcb61aeSAmit Daniel Kachhap 		printk("lr : %pS\n", (void *)ptrauth_strip_insn_pac(lr));
281a06f818aSWill Deacon 	} else {
282a06f818aSWill Deacon 		printk("pc : %016llx\n", regs->pc);
283a06f818aSWill Deacon 		printk("lr : %016llx\n", lr);
284a06f818aSWill Deacon 	}
285a06f818aSWill Deacon 
286b7300d4cSWill Deacon 	printk("sp : %016llx\n", sp);
287db4b0710SMark Rutland 
288133d0518SJulien Thierry 	if (system_uses_irq_prio_masking())
289133d0518SJulien Thierry 		printk("pmr_save: %08llx\n", regs->pmr_save);
290133d0518SJulien Thierry 
291db4b0710SMark Rutland 	i = top_reg;
292db4b0710SMark Rutland 
293db4b0710SMark Rutland 	while (i >= 0) {
294b3901d54SCatalin Marinas 		printk("x%-2d: %016llx ", i, regs->regs[i]);
295db4b0710SMark Rutland 		i--;
296db4b0710SMark Rutland 
297db4b0710SMark Rutland 		if (i % 2 == 0) {
298db4b0710SMark Rutland 			pr_cont("x%-2d: %016llx ", i, regs->regs[i]);
299db4b0710SMark Rutland 			i--;
300db4b0710SMark Rutland 		}
301db4b0710SMark Rutland 
302db4b0710SMark Rutland 		pr_cont("\n");
303b3901d54SCatalin Marinas 	}
304b3901d54SCatalin Marinas }
305b3901d54SCatalin Marinas 
306b3901d54SCatalin Marinas void show_regs(struct pt_regs * regs)
307b3901d54SCatalin Marinas {
308b3901d54SCatalin Marinas 	__show_regs(regs);
309c7689837SDmitry Safonov 	dump_backtrace(regs, NULL, KERN_DEFAULT);
310b3901d54SCatalin Marinas }
311b3901d54SCatalin Marinas 
312eb35bdd7SWill Deacon static void tls_thread_flush(void)
313eb35bdd7SWill Deacon {
314adf75899SMark Rutland 	write_sysreg(0, tpidr_el0);
315eb35bdd7SWill Deacon 
316eb35bdd7SWill Deacon 	if (is_compat_task()) {
31765896545SDave Martin 		current->thread.uw.tp_value = 0;
318eb35bdd7SWill Deacon 
319eb35bdd7SWill Deacon 		/*
320eb35bdd7SWill Deacon 		 * We need to ensure ordering between the shadow state and the
321eb35bdd7SWill Deacon 		 * hardware state, so that we don't corrupt the hardware state
322eb35bdd7SWill Deacon 		 * with a stale shadow state during context switch.
323eb35bdd7SWill Deacon 		 */
324eb35bdd7SWill Deacon 		barrier();
325adf75899SMark Rutland 		write_sysreg(0, tpidrro_el0);
326eb35bdd7SWill Deacon 	}
327eb35bdd7SWill Deacon }
328eb35bdd7SWill Deacon 
32963f0c603SCatalin Marinas static void flush_tagged_addr_state(void)
33063f0c603SCatalin Marinas {
33163f0c603SCatalin Marinas 	if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI))
33263f0c603SCatalin Marinas 		clear_thread_flag(TIF_TAGGED_ADDR);
33363f0c603SCatalin Marinas }
33463f0c603SCatalin Marinas 
335b3901d54SCatalin Marinas void flush_thread(void)
336b3901d54SCatalin Marinas {
337b3901d54SCatalin Marinas 	fpsimd_flush_thread();
338eb35bdd7SWill Deacon 	tls_thread_flush();
339b3901d54SCatalin Marinas 	flush_ptrace_hw_breakpoint(current);
34063f0c603SCatalin Marinas 	flush_tagged_addr_state();
341b3901d54SCatalin Marinas }
342b3901d54SCatalin Marinas 
343b3901d54SCatalin Marinas void release_thread(struct task_struct *dead_task)
344b3901d54SCatalin Marinas {
345b3901d54SCatalin Marinas }
346b3901d54SCatalin Marinas 
347bc0ee476SDave Martin void arch_release_task_struct(struct task_struct *tsk)
348bc0ee476SDave Martin {
349bc0ee476SDave Martin 	fpsimd_release_task(tsk);
350bc0ee476SDave Martin }
351bc0ee476SDave Martin 
352b3901d54SCatalin Marinas int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
353b3901d54SCatalin Marinas {
3546eb6c801SJanet Liu 	if (current->mm)
355c51f9269SArd Biesheuvel 		fpsimd_preserve_current_state();
356b3901d54SCatalin Marinas 	*dst = *src;
357bc0ee476SDave Martin 
3584585fc59SMasayoshi Mizuma 	/* We rely on the above assignment to initialize dst's thread_flags: */
3594585fc59SMasayoshi Mizuma 	BUILD_BUG_ON(!IS_ENABLED(CONFIG_THREAD_INFO_IN_TASK));
3604585fc59SMasayoshi Mizuma 
3614585fc59SMasayoshi Mizuma 	/*
3624585fc59SMasayoshi Mizuma 	 * Detach src's sve_state (if any) from dst so that it does not
3634585fc59SMasayoshi Mizuma 	 * get erroneously used or freed prematurely.  dst's sve_state
3644585fc59SMasayoshi Mizuma 	 * will be allocated on demand later on if dst uses SVE.
3654585fc59SMasayoshi Mizuma 	 * For consistency, also clear TIF_SVE here: this could be done
3664585fc59SMasayoshi Mizuma 	 * later in copy_process(), but to avoid tripping up future
3674585fc59SMasayoshi Mizuma 	 * maintainers it is best not to leave TIF_SVE and sve_state in
3684585fc59SMasayoshi Mizuma 	 * an inconsistent state, even temporarily.
3694585fc59SMasayoshi Mizuma 	 */
3704585fc59SMasayoshi Mizuma 	dst->thread.sve_state = NULL;
3714585fc59SMasayoshi Mizuma 	clear_tsk_thread_flag(dst, TIF_SVE);
3724585fc59SMasayoshi Mizuma 
373b3901d54SCatalin Marinas 	return 0;
374b3901d54SCatalin Marinas }
375b3901d54SCatalin Marinas 
376b3901d54SCatalin Marinas asmlinkage void ret_from_fork(void) asm("ret_from_fork");
377b3901d54SCatalin Marinas 
378*714acdbdSChristian Brauner int copy_thread(unsigned long clone_flags, unsigned long stack_start,
379a4376f2fSAmanieu d'Antras 		unsigned long stk_sz, struct task_struct *p, unsigned long tls)
380b3901d54SCatalin Marinas {
381b3901d54SCatalin Marinas 	struct pt_regs *childregs = task_pt_regs(p);
382b3901d54SCatalin Marinas 
383c34501d2SCatalin Marinas 	memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
384c34501d2SCatalin Marinas 
385bc0ee476SDave Martin 	/*
386071b6d4aSDave Martin 	 * In case p was allocated the same task_struct pointer as some
387071b6d4aSDave Martin 	 * other recently-exited task, make sure p is disassociated from
388071b6d4aSDave Martin 	 * any cpu that may have run that now-exited task recently.
389071b6d4aSDave Martin 	 * Otherwise we could erroneously skip reloading the FPSIMD
390071b6d4aSDave Martin 	 * registers for p.
391071b6d4aSDave Martin 	 */
392071b6d4aSDave Martin 	fpsimd_flush_task_state(p);
393071b6d4aSDave Martin 
39433e45234SKristina Martsenko 	ptrauth_thread_init_kernel(p);
39533e45234SKristina Martsenko 
3969ac08002SAl Viro 	if (likely(!(p->flags & PF_KTHREAD))) {
3979ac08002SAl Viro 		*childregs = *current_pt_regs();
398b3901d54SCatalin Marinas 		childregs->regs[0] = 0;
399d00a3810SWill Deacon 
400b3901d54SCatalin Marinas 		/*
401b3901d54SCatalin Marinas 		 * Read the current TLS pointer from tpidr_el0 as it may be
402b3901d54SCatalin Marinas 		 * out-of-sync with the saved value.
403b3901d54SCatalin Marinas 		 */
404adf75899SMark Rutland 		*task_user_tls(p) = read_sysreg(tpidr_el0);
405d00a3810SWill Deacon 
406e0fd18ceSAl Viro 		if (stack_start) {
407d00a3810SWill Deacon 			if (is_compat_thread(task_thread_info(p)))
408d00a3810SWill Deacon 				childregs->compat_sp = stack_start;
409d00a3810SWill Deacon 			else
410b3901d54SCatalin Marinas 				childregs->sp = stack_start;
411b3901d54SCatalin Marinas 		}
412d00a3810SWill Deacon 
413c34501d2SCatalin Marinas 		/*
414a4376f2fSAmanieu d'Antras 		 * If a TLS pointer was passed to clone, use it for the new
415a4376f2fSAmanieu d'Antras 		 * thread.
416c34501d2SCatalin Marinas 		 */
417b3901d54SCatalin Marinas 		if (clone_flags & CLONE_SETTLS)
418a4376f2fSAmanieu d'Antras 			p->thread.uw.tp_value = tls;
419c34501d2SCatalin Marinas 	} else {
420c34501d2SCatalin Marinas 		memset(childregs, 0, sizeof(struct pt_regs));
421c34501d2SCatalin Marinas 		childregs->pstate = PSR_MODE_EL1h;
42257f4959bSJames Morse 		if (IS_ENABLED(CONFIG_ARM64_UAO) &&
423a4023f68SSuzuki K Poulose 		    cpus_have_const_cap(ARM64_HAS_UAO))
42457f4959bSJames Morse 			childregs->pstate |= PSR_UAO_BIT;
4258f04e8e6SWill Deacon 
4268f04e8e6SWill Deacon 		if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE)
427cbdf8a18SMarc Zyngier 			set_ssbs_bit(childregs);
4288f04e8e6SWill Deacon 
429133d0518SJulien Thierry 		if (system_uses_irq_prio_masking())
430133d0518SJulien Thierry 			childregs->pmr_save = GIC_PRIO_IRQON;
431133d0518SJulien Thierry 
432c34501d2SCatalin Marinas 		p->thread.cpu_context.x19 = stack_start;
433c34501d2SCatalin Marinas 		p->thread.cpu_context.x20 = stk_sz;
434c34501d2SCatalin Marinas 	}
435c34501d2SCatalin Marinas 	p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
436c34501d2SCatalin Marinas 	p->thread.cpu_context.sp = (unsigned long)childregs;
437b3901d54SCatalin Marinas 
438b3901d54SCatalin Marinas 	ptrace_hw_copy_thread(p);
439b3901d54SCatalin Marinas 
440b3901d54SCatalin Marinas 	return 0;
441b3901d54SCatalin Marinas }
442b3901d54SCatalin Marinas 
443936eb65cSDave Martin void tls_preserve_current_state(void)
444936eb65cSDave Martin {
445936eb65cSDave Martin 	*task_user_tls(current) = read_sysreg(tpidr_el0);
446936eb65cSDave Martin }
447936eb65cSDave Martin 
448b3901d54SCatalin Marinas static void tls_thread_switch(struct task_struct *next)
449b3901d54SCatalin Marinas {
450936eb65cSDave Martin 	tls_preserve_current_state();
451b3901d54SCatalin Marinas 
45218011eacSWill Deacon 	if (is_compat_thread(task_thread_info(next)))
45365896545SDave Martin 		write_sysreg(next->thread.uw.tp_value, tpidrro_el0);
45418011eacSWill Deacon 	else if (!arm64_kernel_unmapped_at_el0())
45518011eacSWill Deacon 		write_sysreg(0, tpidrro_el0);
456b3901d54SCatalin Marinas 
45718011eacSWill Deacon 	write_sysreg(*task_user_tls(next), tpidr_el0);
458b3901d54SCatalin Marinas }
459b3901d54SCatalin Marinas 
46057f4959bSJames Morse /* Restore the UAO state depending on next's addr_limit */
461d0854412SJames Morse void uao_thread_switch(struct task_struct *next)
46257f4959bSJames Morse {
463e950631eSCatalin Marinas 	if (IS_ENABLED(CONFIG_ARM64_UAO)) {
464e950631eSCatalin Marinas 		if (task_thread_info(next)->addr_limit == KERNEL_DS)
465e950631eSCatalin Marinas 			asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
46657f4959bSJames Morse 		else
467e950631eSCatalin Marinas 			asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO));
468e950631eSCatalin Marinas 	}
46957f4959bSJames Morse }
47057f4959bSJames Morse 
471b3901d54SCatalin Marinas /*
472cbdf8a18SMarc Zyngier  * Force SSBS state on context-switch, since it may be lost after migrating
473cbdf8a18SMarc Zyngier  * from a CPU which treats the bit as RES0 in a heterogeneous system.
474cbdf8a18SMarc Zyngier  */
475cbdf8a18SMarc Zyngier static void ssbs_thread_switch(struct task_struct *next)
476cbdf8a18SMarc Zyngier {
477cbdf8a18SMarc Zyngier 	struct pt_regs *regs = task_pt_regs(next);
478cbdf8a18SMarc Zyngier 
479cbdf8a18SMarc Zyngier 	/*
480cbdf8a18SMarc Zyngier 	 * Nothing to do for kernel threads, but 'regs' may be junk
481cbdf8a18SMarc Zyngier 	 * (e.g. idle task) so check the flags and bail early.
482cbdf8a18SMarc Zyngier 	 */
483cbdf8a18SMarc Zyngier 	if (unlikely(next->flags & PF_KTHREAD))
484cbdf8a18SMarc Zyngier 		return;
485cbdf8a18SMarc Zyngier 
486fca3d33dSWill Deacon 	/*
487fca3d33dSWill Deacon 	 * If all CPUs implement the SSBS extension, then we just need to
488fca3d33dSWill Deacon 	 * context-switch the PSTATE field.
489fca3d33dSWill Deacon 	 */
490fca3d33dSWill Deacon 	if (cpu_have_feature(cpu_feature(SSBS)))
491fca3d33dSWill Deacon 		return;
492fca3d33dSWill Deacon 
493cbdf8a18SMarc Zyngier 	/* If the mitigation is enabled, then we leave SSBS clear. */
494cbdf8a18SMarc Zyngier 	if ((arm64_get_ssbd_state() == ARM64_SSBD_FORCE_ENABLE) ||
495cbdf8a18SMarc Zyngier 	    test_tsk_thread_flag(next, TIF_SSBD))
496cbdf8a18SMarc Zyngier 		return;
497cbdf8a18SMarc Zyngier 
498cbdf8a18SMarc Zyngier 	if (compat_user_mode(regs))
499cbdf8a18SMarc Zyngier 		set_compat_ssbs_bit(regs);
500cbdf8a18SMarc Zyngier 	else if (user_mode(regs))
501cbdf8a18SMarc Zyngier 		set_ssbs_bit(regs);
502cbdf8a18SMarc Zyngier }
503cbdf8a18SMarc Zyngier 
504cbdf8a18SMarc Zyngier /*
505c02433ddSMark Rutland  * We store our current task in sp_el0, which is clobbered by userspace. Keep a
506c02433ddSMark Rutland  * shadow copy so that we can restore this upon entry from userspace.
507c02433ddSMark Rutland  *
508c02433ddSMark Rutland  * This is *only* for exception entry from EL0, and is not valid until we
509c02433ddSMark Rutland  * __switch_to() a user task.
510c02433ddSMark Rutland  */
511c02433ddSMark Rutland DEFINE_PER_CPU(struct task_struct *, __entry_task);
512c02433ddSMark Rutland 
513c02433ddSMark Rutland static void entry_task_switch(struct task_struct *next)
514c02433ddSMark Rutland {
515c02433ddSMark Rutland 	__this_cpu_write(__entry_task, next);
516c02433ddSMark Rutland }
517c02433ddSMark Rutland 
518c02433ddSMark Rutland /*
519b3901d54SCatalin Marinas  * Thread switching.
520b3901d54SCatalin Marinas  */
5218f4b326dSJoel Fernandes __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
522b3901d54SCatalin Marinas 				struct task_struct *next)
523b3901d54SCatalin Marinas {
524b3901d54SCatalin Marinas 	struct task_struct *last;
525b3901d54SCatalin Marinas 
526b3901d54SCatalin Marinas 	fpsimd_thread_switch(next);
527b3901d54SCatalin Marinas 	tls_thread_switch(next);
528b3901d54SCatalin Marinas 	hw_breakpoint_thread_switch(next);
5293325732fSChristopher Covington 	contextidr_thread_switch(next);
530c02433ddSMark Rutland 	entry_task_switch(next);
53157f4959bSJames Morse 	uao_thread_switch(next);
532cbdf8a18SMarc Zyngier 	ssbs_thread_switch(next);
533b3901d54SCatalin Marinas 
5345108c67cSCatalin Marinas 	/*
5355108c67cSCatalin Marinas 	 * Complete any pending TLB or cache maintenance on this CPU in case
5365108c67cSCatalin Marinas 	 * the thread migrates to a different CPU.
53722e4ebb9SMathieu Desnoyers 	 * This full barrier is also required by the membarrier system
53822e4ebb9SMathieu Desnoyers 	 * call.
5395108c67cSCatalin Marinas 	 */
54098f7685eSWill Deacon 	dsb(ish);
541b3901d54SCatalin Marinas 
542b3901d54SCatalin Marinas 	/* the actual thread switch */
543b3901d54SCatalin Marinas 	last = cpu_switch_to(prev, next);
544b3901d54SCatalin Marinas 
545b3901d54SCatalin Marinas 	return last;
546b3901d54SCatalin Marinas }
547b3901d54SCatalin Marinas 
548b3901d54SCatalin Marinas unsigned long get_wchan(struct task_struct *p)
549b3901d54SCatalin Marinas {
550b3901d54SCatalin Marinas 	struct stackframe frame;
5519bbd4c56SMark Rutland 	unsigned long stack_page, ret = 0;
552b3901d54SCatalin Marinas 	int count = 0;
553b3901d54SCatalin Marinas 	if (!p || p == current || p->state == TASK_RUNNING)
554b3901d54SCatalin Marinas 		return 0;
555b3901d54SCatalin Marinas 
5569bbd4c56SMark Rutland 	stack_page = (unsigned long)try_get_task_stack(p);
5579bbd4c56SMark Rutland 	if (!stack_page)
5589bbd4c56SMark Rutland 		return 0;
5599bbd4c56SMark Rutland 
560f3dcbe67SDave Martin 	start_backtrace(&frame, thread_saved_fp(p), thread_saved_pc(p));
561f3dcbe67SDave Martin 
562b3901d54SCatalin Marinas 	do {
56331e43ad3SArd Biesheuvel 		if (unwind_frame(p, &frame))
5649bbd4c56SMark Rutland 			goto out;
5659bbd4c56SMark Rutland 		if (!in_sched_functions(frame.pc)) {
5669bbd4c56SMark Rutland 			ret = frame.pc;
5679bbd4c56SMark Rutland 			goto out;
5689bbd4c56SMark Rutland 		}
569b3901d54SCatalin Marinas 	} while (count ++ < 16);
5709bbd4c56SMark Rutland 
5719bbd4c56SMark Rutland out:
5729bbd4c56SMark Rutland 	put_task_stack(p);
5739bbd4c56SMark Rutland 	return ret;
574b3901d54SCatalin Marinas }
575b3901d54SCatalin Marinas 
576b3901d54SCatalin Marinas unsigned long arch_align_stack(unsigned long sp)
577b3901d54SCatalin Marinas {
578b3901d54SCatalin Marinas 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
579b3901d54SCatalin Marinas 		sp -= get_random_int() & ~PAGE_MASK;
580b3901d54SCatalin Marinas 	return sp & ~0xf;
581b3901d54SCatalin Marinas }
582b3901d54SCatalin Marinas 
583d1be5c99SYury Norov /*
584d1be5c99SYury Norov  * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY.
585d1be5c99SYury Norov  */
586d1be5c99SYury Norov void arch_setup_new_exec(void)
587d1be5c99SYury Norov {
588d1be5c99SYury Norov 	current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0;
58975031975SMark Rutland 
59075031975SMark Rutland 	ptrauth_thread_init_user(current);
591d1be5c99SYury Norov }
59263f0c603SCatalin Marinas 
59363f0c603SCatalin Marinas #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
59463f0c603SCatalin Marinas /*
59563f0c603SCatalin Marinas  * Control the relaxed ABI allowing tagged user addresses into the kernel.
59663f0c603SCatalin Marinas  */
597413235fcSCatalin Marinas static unsigned int tagged_addr_disabled;
59863f0c603SCatalin Marinas 
59963f0c603SCatalin Marinas long set_tagged_addr_ctrl(unsigned long arg)
60063f0c603SCatalin Marinas {
60163f0c603SCatalin Marinas 	if (is_compat_task())
60263f0c603SCatalin Marinas 		return -EINVAL;
60363f0c603SCatalin Marinas 	if (arg & ~PR_TAGGED_ADDR_ENABLE)
60463f0c603SCatalin Marinas 		return -EINVAL;
60563f0c603SCatalin Marinas 
606413235fcSCatalin Marinas 	/*
607413235fcSCatalin Marinas 	 * Do not allow the enabling of the tagged address ABI if globally
608413235fcSCatalin Marinas 	 * disabled via sysctl abi.tagged_addr_disabled.
609413235fcSCatalin Marinas 	 */
610413235fcSCatalin Marinas 	if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled)
611413235fcSCatalin Marinas 		return -EINVAL;
612413235fcSCatalin Marinas 
61363f0c603SCatalin Marinas 	update_thread_flag(TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE);
61463f0c603SCatalin Marinas 
61563f0c603SCatalin Marinas 	return 0;
61663f0c603SCatalin Marinas }
61763f0c603SCatalin Marinas 
61863f0c603SCatalin Marinas long get_tagged_addr_ctrl(void)
61963f0c603SCatalin Marinas {
62063f0c603SCatalin Marinas 	if (is_compat_task())
62163f0c603SCatalin Marinas 		return -EINVAL;
62263f0c603SCatalin Marinas 
62363f0c603SCatalin Marinas 	if (test_thread_flag(TIF_TAGGED_ADDR))
62463f0c603SCatalin Marinas 		return PR_TAGGED_ADDR_ENABLE;
62563f0c603SCatalin Marinas 
62663f0c603SCatalin Marinas 	return 0;
62763f0c603SCatalin Marinas }
62863f0c603SCatalin Marinas 
62963f0c603SCatalin Marinas /*
63063f0c603SCatalin Marinas  * Global sysctl to disable the tagged user addresses support. This control
63163f0c603SCatalin Marinas  * only prevents the tagged address ABI enabling via prctl() and does not
63263f0c603SCatalin Marinas  * disable it for tasks that already opted in to the relaxed ABI.
63363f0c603SCatalin Marinas  */
63463f0c603SCatalin Marinas 
63563f0c603SCatalin Marinas static struct ctl_table tagged_addr_sysctl_table[] = {
63663f0c603SCatalin Marinas 	{
637413235fcSCatalin Marinas 		.procname	= "tagged_addr_disabled",
63863f0c603SCatalin Marinas 		.mode		= 0644,
639413235fcSCatalin Marinas 		.data		= &tagged_addr_disabled,
64063f0c603SCatalin Marinas 		.maxlen		= sizeof(int),
64163f0c603SCatalin Marinas 		.proc_handler	= proc_dointvec_minmax,
6422c614c11SMatteo Croce 		.extra1		= SYSCTL_ZERO,
6432c614c11SMatteo Croce 		.extra2		= SYSCTL_ONE,
64463f0c603SCatalin Marinas 	},
64563f0c603SCatalin Marinas 	{ }
64663f0c603SCatalin Marinas };
64763f0c603SCatalin Marinas 
64863f0c603SCatalin Marinas static int __init tagged_addr_init(void)
64963f0c603SCatalin Marinas {
65063f0c603SCatalin Marinas 	if (!register_sysctl("abi", tagged_addr_sysctl_table))
65163f0c603SCatalin Marinas 		return -EINVAL;
65263f0c603SCatalin Marinas 	return 0;
65363f0c603SCatalin Marinas }
65463f0c603SCatalin Marinas 
65563f0c603SCatalin Marinas core_initcall(tagged_addr_init);
65663f0c603SCatalin Marinas #endif	/* CONFIG_ARM64_TAGGED_ADDR_ABI */
65719c95f26SJulien Thierry 
65819c95f26SJulien Thierry asmlinkage void __sched arm64_preempt_schedule_irq(void)
65919c95f26SJulien Thierry {
66019c95f26SJulien Thierry 	lockdep_assert_irqs_disabled();
66119c95f26SJulien Thierry 
66219c95f26SJulien Thierry 	/*
66319c95f26SJulien Thierry 	 * Preempting a task from an IRQ means we leave copies of PSTATE
66419c95f26SJulien Thierry 	 * on the stack. cpufeature's enable calls may modify PSTATE, but
66519c95f26SJulien Thierry 	 * resuming one of these preempted tasks would undo those changes.
66619c95f26SJulien Thierry 	 *
66719c95f26SJulien Thierry 	 * Only allow a task to be preempted once cpufeatures have been
66819c95f26SJulien Thierry 	 * enabled.
66919c95f26SJulien Thierry 	 */
670b51c6ac2SSuzuki K Poulose 	if (system_capabilities_finalized())
67119c95f26SJulien Thierry 		preempt_schedule_irq();
67219c95f26SJulien Thierry }
673ab7876a9SDave Martin 
674ab7876a9SDave Martin #ifdef CONFIG_BINFMT_ELF
675ab7876a9SDave Martin int arch_elf_adjust_prot(int prot, const struct arch_elf_state *state,
676ab7876a9SDave Martin 			 bool has_interp, bool is_interp)
677ab7876a9SDave Martin {
6785d1b631cSMark Brown 	/*
6795d1b631cSMark Brown 	 * For dynamically linked executables the interpreter is
6805d1b631cSMark Brown 	 * responsible for setting PROT_BTI on everything except
6815d1b631cSMark Brown 	 * itself.
6825d1b631cSMark Brown 	 */
683ab7876a9SDave Martin 	if (is_interp != has_interp)
684ab7876a9SDave Martin 		return prot;
685ab7876a9SDave Martin 
686ab7876a9SDave Martin 	if (!(state->flags & ARM64_ELF_BTI))
687ab7876a9SDave Martin 		return prot;
688ab7876a9SDave Martin 
689ab7876a9SDave Martin 	if (prot & PROT_EXEC)
690ab7876a9SDave Martin 		prot |= PROT_BTI;
691ab7876a9SDave Martin 
692ab7876a9SDave Martin 	return prot;
693ab7876a9SDave Martin }
694ab7876a9SDave Martin #endif
695