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 */
9fd92d4a5SAKASHI Takahiro #include <linux/compat.h>
1060c0d45aSArd Biesheuvel #include <linux/efi.h>
11ab7876a9SDave Martin #include <linux/elf.h>
12b3901d54SCatalin Marinas #include <linux/export.h>
13b3901d54SCatalin Marinas #include <linux/sched.h>
14b17b0153SIngo Molnar #include <linux/sched/debug.h>
1529930025SIngo Molnar #include <linux/sched/task.h>
1668db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
17b3901d54SCatalin Marinas #include <linux/kernel.h>
18ab7876a9SDave Martin #include <linux/mman.h>
19b3901d54SCatalin Marinas #include <linux/mm.h>
20780c083aSWill Deacon #include <linux/nospec.h>
21b3901d54SCatalin Marinas #include <linux/stddef.h>
2263f0c603SCatalin Marinas #include <linux/sysctl.h>
23b3901d54SCatalin Marinas #include <linux/unistd.h>
24b3901d54SCatalin Marinas #include <linux/user.h>
25b3901d54SCatalin Marinas #include <linux/delay.h>
26b3901d54SCatalin Marinas #include <linux/reboot.h>
27b3901d54SCatalin Marinas #include <linux/interrupt.h>
28b3901d54SCatalin Marinas #include <linux/init.h>
29b3901d54SCatalin Marinas #include <linux/cpu.h>
30b3901d54SCatalin Marinas #include <linux/elfcore.h>
31b3901d54SCatalin Marinas #include <linux/pm.h>
32b3901d54SCatalin Marinas #include <linux/tick.h>
33b3901d54SCatalin Marinas #include <linux/utsname.h>
34b3901d54SCatalin Marinas #include <linux/uaccess.h>
35b3901d54SCatalin Marinas #include <linux/random.h>
36b3901d54SCatalin Marinas #include <linux/hw_breakpoint.h>
37b3901d54SCatalin Marinas #include <linux/personality.h>
38b3901d54SCatalin Marinas #include <linux/notifier.h>
39096b3224SJisheng Zhang #include <trace/events/power.h>
40c02433ddSMark Rutland #include <linux/percpu.h>
41bc0ee476SDave Martin #include <linux/thread_info.h>
4263f0c603SCatalin Marinas #include <linux/prctl.h>
434f62bb7cSMadhavan T. Venkataraman #include <linux/stacktrace.h>
44b3901d54SCatalin Marinas
4557f4959bSJames Morse #include <asm/alternative.h>
463e9e67e1SPeter Collingbourne #include <asm/arch_timer.h>
47b3901d54SCatalin Marinas #include <asm/compat.h>
4819c95f26SJulien Thierry #include <asm/cpufeature.h>
49b3901d54SCatalin Marinas #include <asm/cacheflush.h>
50d0854412SJames Morse #include <asm/exec.h>
51ec45d1cfSWill Deacon #include <asm/fpsimd.h>
52ec45d1cfSWill Deacon #include <asm/mmu_context.h>
53637ec831SVincenzo Frascino #include <asm/mte.h>
54b3901d54SCatalin Marinas #include <asm/processor.h>
5575031975SMark Rutland #include <asm/pointer_auth.h>
56b3901d54SCatalin Marinas #include <asm/stacktrace.h>
57baa96377SManinder Singh #include <asm/switch_to.h>
58baa96377SManinder Singh #include <asm/system_misc.h>
59b3901d54SCatalin Marinas
600a1213faSArd Biesheuvel #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK)
61c0c264aeSLaura Abbott #include <linux/stackprotector.h>
629fcb2e93SDan Li unsigned long __stack_chk_guard __ro_after_init;
63c0c264aeSLaura Abbott EXPORT_SYMBOL(__stack_chk_guard);
64c0c264aeSLaura Abbott #endif
65c0c264aeSLaura Abbott
66b3901d54SCatalin Marinas /*
67b3901d54SCatalin Marinas * Function pointers to optional machine specific functions
68b3901d54SCatalin Marinas */
69b3901d54SCatalin Marinas void (*pm_power_off)(void);
70b3901d54SCatalin Marinas EXPORT_SYMBOL_GPL(pm_power_off);
71b3901d54SCatalin Marinas
729327e2c6SMark Rutland #ifdef CONFIG_HOTPLUG_CPU
arch_cpu_idle_dead(void)73071c44e4SJosh Poimboeuf void __noreturn arch_cpu_idle_dead(void)
749327e2c6SMark Rutland {
759327e2c6SMark Rutland cpu_die();
769327e2c6SMark Rutland }
779327e2c6SMark Rutland #endif
789327e2c6SMark Rutland
7990f51a09SArun KS /*
8090f51a09SArun KS * Called by kexec, immediately prior to machine_kexec().
8190f51a09SArun KS *
8290f51a09SArun KS * This must completely disable all secondary CPUs; simply causing those CPUs
8390f51a09SArun KS * to execute e.g. a RAM-based pin loop is not sufficient. This allows the
8490f51a09SArun KS * kexec'd kernel to use any and all RAM as it sees fit, without having to
8590f51a09SArun KS * avoid any code or data used by any SW CPU pin loop. The CPU hotplug
86d66b16f5SQais Yousef * functionality embodied in smpt_shutdown_nonboot_cpus() to achieve this.
8790f51a09SArun KS */
machine_shutdown(void)88b3901d54SCatalin Marinas void machine_shutdown(void)
89b3901d54SCatalin Marinas {
905efbe6a6SQais Yousef smp_shutdown_nonboot_cpus(reboot_cpu);
91b3901d54SCatalin Marinas }
92b3901d54SCatalin Marinas
9390f51a09SArun KS /*
9490f51a09SArun KS * Halting simply requires that the secondary CPUs stop performing any
9590f51a09SArun KS * activity (executing tasks, handling interrupts). smp_send_stop()
9690f51a09SArun KS * achieves this.
9790f51a09SArun KS */
machine_halt(void)98b3901d54SCatalin Marinas void machine_halt(void)
99b3901d54SCatalin Marinas {
100b9acc49eSArun KS local_irq_disable();
10190f51a09SArun KS smp_send_stop();
102b3901d54SCatalin Marinas while (1);
103b3901d54SCatalin Marinas }
104b3901d54SCatalin Marinas
10590f51a09SArun KS /*
10690f51a09SArun KS * Power-off simply requires that the secondary CPUs stop performing any
10790f51a09SArun KS * activity (executing tasks, handling interrupts). smp_send_stop()
10890f51a09SArun KS * achieves this. When the system power is turned off, it will take all CPUs
10990f51a09SArun KS * with it.
11090f51a09SArun KS */
machine_power_off(void)111b3901d54SCatalin Marinas void machine_power_off(void)
112b3901d54SCatalin Marinas {
113b9acc49eSArun KS local_irq_disable();
11490f51a09SArun KS smp_send_stop();
1150c649914SDmitry Osipenko do_kernel_power_off();
116b3901d54SCatalin Marinas }
117b3901d54SCatalin Marinas
11890f51a09SArun KS /*
11990f51a09SArun KS * Restart requires that the secondary CPUs stop performing any activity
12068234df4SMark Rutland * while the primary CPU resets the system. Systems with multiple CPUs must
12190f51a09SArun KS * provide a HW restart implementation, to ensure that all CPUs reset at once.
12290f51a09SArun KS * This is required so that any code running after reset on the primary CPU
12390f51a09SArun KS * doesn't have to co-ordinate with other CPUs to ensure they aren't still
12490f51a09SArun KS * executing pre-reset code, and using RAM that the primary CPU's code wishes
12590f51a09SArun KS * to use. Implementing such co-ordination would be essentially impossible.
12690f51a09SArun KS */
machine_restart(char * cmd)127b3901d54SCatalin Marinas void machine_restart(char *cmd)
128b3901d54SCatalin Marinas {
129b3901d54SCatalin Marinas /* Disable interrupts first */
130b3901d54SCatalin Marinas local_irq_disable();
131b9acc49eSArun KS smp_send_stop();
132b3901d54SCatalin Marinas
13360c0d45aSArd Biesheuvel /*
13460c0d45aSArd Biesheuvel * UpdateCapsule() depends on the system being reset via
13560c0d45aSArd Biesheuvel * ResetSystem().
13660c0d45aSArd Biesheuvel */
13760c0d45aSArd Biesheuvel if (efi_enabled(EFI_RUNTIME_SERVICES))
13860c0d45aSArd Biesheuvel efi_reboot(reboot_mode, NULL);
13960c0d45aSArd Biesheuvel
140b3901d54SCatalin Marinas /* Now call the architecture specific reboot code. */
1411c7ffc32SGuenter Roeck do_kernel_restart(cmd);
142b3901d54SCatalin Marinas
143b3901d54SCatalin Marinas /*
144b3901d54SCatalin Marinas * Whoops - the architecture was unable to reboot.
145b3901d54SCatalin Marinas */
146b3901d54SCatalin Marinas printk("Reboot failed -- System halted\n");
147b3901d54SCatalin Marinas while (1);
148b3901d54SCatalin Marinas }
149b3901d54SCatalin Marinas
150ec94a46eSDave Martin #define bstr(suffix, str) [PSR_BTYPE_ ## suffix >> PSR_BTYPE_SHIFT] = str
151ec94a46eSDave Martin static const char *const btypes[] = {
152ec94a46eSDave Martin bstr(NONE, "--"),
153ec94a46eSDave Martin bstr( JC, "jc"),
154ec94a46eSDave Martin bstr( C, "-c"),
155ec94a46eSDave Martin bstr( J , "j-")
156ec94a46eSDave Martin };
157ec94a46eSDave Martin #undef bstr
158ec94a46eSDave Martin
print_pstate(struct pt_regs * regs)159b7300d4cSWill Deacon static void print_pstate(struct pt_regs *regs)
160b7300d4cSWill Deacon {
161b7300d4cSWill Deacon u64 pstate = regs->pstate;
162b7300d4cSWill Deacon
163b7300d4cSWill Deacon if (compat_user_mode(regs)) {
164ec63e300SLingyan Huang printk("pstate: %08llx (%c%c%c%c %c %s %s %c%c%c %cDIT %cSSBS)\n",
165b7300d4cSWill Deacon pstate,
166d64567f6SMark Rutland pstate & PSR_AA32_N_BIT ? 'N' : 'n',
167d64567f6SMark Rutland pstate & PSR_AA32_Z_BIT ? 'Z' : 'z',
168d64567f6SMark Rutland pstate & PSR_AA32_C_BIT ? 'C' : 'c',
169d64567f6SMark Rutland pstate & PSR_AA32_V_BIT ? 'V' : 'v',
170d64567f6SMark Rutland pstate & PSR_AA32_Q_BIT ? 'Q' : 'q',
171d64567f6SMark Rutland pstate & PSR_AA32_T_BIT ? "T32" : "A32",
172d64567f6SMark Rutland pstate & PSR_AA32_E_BIT ? "BE" : "LE",
173d64567f6SMark Rutland pstate & PSR_AA32_A_BIT ? 'A' : 'a',
174d64567f6SMark Rutland pstate & PSR_AA32_I_BIT ? 'I' : 'i',
175ec63e300SLingyan Huang pstate & PSR_AA32_F_BIT ? 'F' : 'f',
176ec63e300SLingyan Huang pstate & PSR_AA32_DIT_BIT ? '+' : '-',
177ec63e300SLingyan Huang pstate & PSR_AA32_SSBS_BIT ? '+' : '-');
178b7300d4cSWill Deacon } else {
179ec94a46eSDave Martin const char *btype_str = btypes[(pstate & PSR_BTYPE_MASK) >>
180ec94a46eSDave Martin PSR_BTYPE_SHIFT];
181ec94a46eSDave Martin
182ec63e300SLingyan Huang printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO %cTCO %cDIT %cSSBS BTYPE=%s)\n",
183b7300d4cSWill Deacon pstate,
184b7300d4cSWill Deacon pstate & PSR_N_BIT ? 'N' : 'n',
185b7300d4cSWill Deacon pstate & PSR_Z_BIT ? 'Z' : 'z',
186b7300d4cSWill Deacon pstate & PSR_C_BIT ? 'C' : 'c',
187b7300d4cSWill Deacon pstate & PSR_V_BIT ? 'V' : 'v',
188b7300d4cSWill Deacon pstate & PSR_D_BIT ? 'D' : 'd',
189b7300d4cSWill Deacon pstate & PSR_A_BIT ? 'A' : 'a',
190b7300d4cSWill Deacon pstate & PSR_I_BIT ? 'I' : 'i',
191b7300d4cSWill Deacon pstate & PSR_F_BIT ? 'F' : 'f',
192b7300d4cSWill Deacon pstate & PSR_PAN_BIT ? '+' : '-',
193ec94a46eSDave Martin pstate & PSR_UAO_BIT ? '+' : '-',
194637ec831SVincenzo Frascino pstate & PSR_TCO_BIT ? '+' : '-',
195ec63e300SLingyan Huang pstate & PSR_DIT_BIT ? '+' : '-',
196ec63e300SLingyan Huang pstate & PSR_SSBS_BIT ? '+' : '-',
197ec94a46eSDave Martin btype_str);
198b7300d4cSWill Deacon }
199b7300d4cSWill Deacon }
200b7300d4cSWill Deacon
__show_regs(struct pt_regs * regs)201b3901d54SCatalin Marinas void __show_regs(struct pt_regs *regs)
202b3901d54SCatalin Marinas {
2036ca68e80SCatalin Marinas int i, top_reg;
2046ca68e80SCatalin Marinas u64 lr, sp;
2056ca68e80SCatalin Marinas
2066ca68e80SCatalin Marinas if (compat_user_mode(regs)) {
2076ca68e80SCatalin Marinas lr = regs->compat_lr;
2086ca68e80SCatalin Marinas sp = regs->compat_sp;
2096ca68e80SCatalin Marinas top_reg = 12;
2106ca68e80SCatalin Marinas } else {
2116ca68e80SCatalin Marinas lr = regs->regs[30];
2126ca68e80SCatalin Marinas sp = regs->sp;
2136ca68e80SCatalin Marinas top_reg = 29;
2146ca68e80SCatalin Marinas }
215b3901d54SCatalin Marinas
216a43cb95dSTejun Heo show_regs_print_info(KERN_DEFAULT);
217b7300d4cSWill Deacon print_pstate(regs);
218a06f818aSWill Deacon
219a06f818aSWill Deacon if (!user_mode(regs)) {
2204ef79638SSergey Senozhatsky printk("pc : %pS\n", (void *)regs->pc);
221ca708599SMark Rutland printk("lr : %pS\n", (void *)ptrauth_strip_kernel_insn_pac(lr));
222a06f818aSWill Deacon } else {
223a06f818aSWill Deacon printk("pc : %016llx\n", regs->pc);
224a06f818aSWill Deacon printk("lr : %016llx\n", lr);
225a06f818aSWill Deacon }
226a06f818aSWill Deacon
227b7300d4cSWill Deacon printk("sp : %016llx\n", sp);
228db4b0710SMark Rutland
229133d0518SJulien Thierry if (system_uses_irq_prio_masking())
230133d0518SJulien Thierry printk("pmr_save: %08llx\n", regs->pmr_save);
231133d0518SJulien Thierry
232db4b0710SMark Rutland i = top_reg;
233db4b0710SMark Rutland
234db4b0710SMark Rutland while (i >= 0) {
235b3901d54SCatalin Marinas printk("x%-2d: %016llx", i, regs->regs[i]);
236db4b0710SMark Rutland
2370bca3ec8SMatthew Wilcox (Oracle) while (i-- % 3)
238db4b0710SMark Rutland pr_cont(" x%-2d: %016llx", i, regs->regs[i]);
239db4b0710SMark Rutland
240db4b0710SMark Rutland pr_cont("\n");
241b3901d54SCatalin Marinas }
242b3901d54SCatalin Marinas }
243b3901d54SCatalin Marinas
show_regs(struct pt_regs * regs)244b3901d54SCatalin Marinas void show_regs(struct pt_regs *regs)
245b3901d54SCatalin Marinas {
246b3901d54SCatalin Marinas __show_regs(regs);
247c7689837SDmitry Safonov dump_backtrace(regs, NULL, KERN_DEFAULT);
248b3901d54SCatalin Marinas }
249b3901d54SCatalin Marinas
tls_thread_flush(void)250eb35bdd7SWill Deacon static void tls_thread_flush(void)
251eb35bdd7SWill Deacon {
252adf75899SMark Rutland write_sysreg(0, tpidr_el0);
253a9d69158SMark Brown if (system_supports_tpidr2())
254a9d69158SMark Brown write_sysreg_s(0, SYS_TPIDR2_EL0);
255eb35bdd7SWill Deacon
256eb35bdd7SWill Deacon if (is_compat_task()) {
25765896545SDave Martin current->thread.uw.tp_value = 0;
258eb35bdd7SWill Deacon
259eb35bdd7SWill Deacon /*
260eb35bdd7SWill Deacon * We need to ensure ordering between the shadow state and the
261eb35bdd7SWill Deacon * hardware state, so that we don't corrupt the hardware state
262eb35bdd7SWill Deacon * with a stale shadow state during context switch.
263eb35bdd7SWill Deacon */
264eb35bdd7SWill Deacon barrier();
265adf75899SMark Rutland write_sysreg(0, tpidrro_el0);
266eb35bdd7SWill Deacon }
267eb35bdd7SWill Deacon }
268eb35bdd7SWill Deacon
flush_tagged_addr_state(void)26963f0c603SCatalin Marinas static void flush_tagged_addr_state(void)
27063f0c603SCatalin Marinas {
27163f0c603SCatalin Marinas if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI))
27263f0c603SCatalin Marinas clear_thread_flag(TIF_TAGGED_ADDR);
27363f0c603SCatalin Marinas }
27463f0c603SCatalin Marinas
flush_poe(void)275160a8e13SJoey Gouly static void flush_poe(void)
276160a8e13SJoey Gouly {
277160a8e13SJoey Gouly if (!system_supports_poe())
278160a8e13SJoey Gouly return;
279160a8e13SJoey Gouly
280160a8e13SJoey Gouly write_sysreg_s(POR_EL0_INIT, SYS_POR_EL0);
281160a8e13SJoey Gouly }
282160a8e13SJoey Gouly
flush_thread(void)283b3901d54SCatalin Marinas void flush_thread(void)
284b3901d54SCatalin Marinas {
285b3901d54SCatalin Marinas fpsimd_flush_thread();
286eb35bdd7SWill Deacon tls_thread_flush();
287b3901d54SCatalin Marinas flush_ptrace_hw_breakpoint(current);
28863f0c603SCatalin Marinas flush_tagged_addr_state();
289160a8e13SJoey Gouly flush_poe();
290b3901d54SCatalin Marinas }
291b3901d54SCatalin Marinas
arch_release_task_struct(struct task_struct * tsk)292bc0ee476SDave Martin void arch_release_task_struct(struct task_struct *tsk)
293bc0ee476SDave Martin {
294bc0ee476SDave Martin fpsimd_release_task(tsk);
295bc0ee476SDave Martin }
296bc0ee476SDave Martin
arch_dup_task_struct(struct task_struct * dst,struct task_struct * src)297b3901d54SCatalin Marinas int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
298b3901d54SCatalin Marinas {
2996eb6c801SJanet Liu if (current->mm)
300c51f9269SArd Biesheuvel fpsimd_preserve_current_state();
301b3901d54SCatalin Marinas *dst = *src;
302bc0ee476SDave Martin
3034585fc59SMasayoshi Mizuma /*
3044585fc59SMasayoshi Mizuma * Detach src's sve_state (if any) from dst so that it does not
3058bd7f91cSMark Brown * get erroneously used or freed prematurely. dst's copies
3064585fc59SMasayoshi Mizuma * will be allocated on demand later on if dst uses SVE.
3074585fc59SMasayoshi Mizuma * For consistency, also clear TIF_SVE here: this could be done
3084585fc59SMasayoshi Mizuma * later in copy_process(), but to avoid tripping up future
3098bd7f91cSMark Brown * maintainers it is best not to leave TIF flags and buffers in
3104585fc59SMasayoshi Mizuma * an inconsistent state, even temporarily.
3114585fc59SMasayoshi Mizuma */
3124585fc59SMasayoshi Mizuma dst->thread.sve_state = NULL;
3134585fc59SMasayoshi Mizuma clear_tsk_thread_flag(dst, TIF_SVE);
3144585fc59SMasayoshi Mizuma
3158bd7f91cSMark Brown /*
3168bd7f91cSMark Brown * In the unlikely event that we create a new thread with ZA
317d6138b4aSMark Brown * enabled we should retain the ZA and ZT state so duplicate
318d6138b4aSMark Brown * it here. This may be shortly freed if we exec() or if
319d6138b4aSMark Brown * CLONE_SETTLS but it's simpler to do it here. To avoid
320d6138b4aSMark Brown * confusing the rest of the code ensure that we have a
321d6138b4aSMark Brown * sve_state allocated whenever sme_state is allocated.
3228bd7f91cSMark Brown */
3238bd7f91cSMark Brown if (thread_za_enabled(&src->thread)) {
3248bd7f91cSMark Brown dst->thread.sve_state = kzalloc(sve_state_size(src),
3258bd7f91cSMark Brown GFP_KERNEL);
3262e29b997SWan Jiabing if (!dst->thread.sve_state)
3278bd7f91cSMark Brown return -ENOMEM;
328ce514000SMark Brown
329ce514000SMark Brown dst->thread.sme_state = kmemdup(src->thread.sme_state,
330ce514000SMark Brown sme_state_size(src),
3318bd7f91cSMark Brown GFP_KERNEL);
332ce514000SMark Brown if (!dst->thread.sme_state) {
3338bd7f91cSMark Brown kfree(dst->thread.sve_state);
3348bd7f91cSMark Brown dst->thread.sve_state = NULL;
3358bd7f91cSMark Brown return -ENOMEM;
3368bd7f91cSMark Brown }
3378bd7f91cSMark Brown } else {
338ce514000SMark Brown dst->thread.sme_state = NULL;
3398bd7f91cSMark Brown clear_tsk_thread_flag(dst, TIF_SME);
3408bd7f91cSMark Brown }
341b40c559bSMark Brown
342baa85152SMark Brown dst->thread.fp_type = FP_STATE_FPSIMD;
343baa85152SMark Brown
344637ec831SVincenzo Frascino /* clear any pending asynchronous tag fault raised by the parent */
345637ec831SVincenzo Frascino clear_tsk_thread_flag(dst, TIF_MTE_ASYNC_FAULT);
346637ec831SVincenzo Frascino
347b3901d54SCatalin Marinas return 0;
348b3901d54SCatalin Marinas }
349b3901d54SCatalin Marinas
350b3901d54SCatalin Marinas asmlinkage void ret_from_fork(void) asm("ret_from_fork");
351b3901d54SCatalin Marinas
copy_thread(struct task_struct * p,const struct kernel_clone_args * args)352c5febea0SEric W. Biederman int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
353b3901d54SCatalin Marinas {
354c5febea0SEric W. Biederman unsigned long clone_flags = args->flags;
355c5febea0SEric W. Biederman unsigned long stack_start = args->stack;
356c5febea0SEric W. Biederman unsigned long tls = args->tls;
357b3901d54SCatalin Marinas struct pt_regs *childregs = task_pt_regs(p);
358b3901d54SCatalin Marinas
359c34501d2SCatalin Marinas memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
360c34501d2SCatalin Marinas
361bc0ee476SDave Martin /*
362071b6d4aSDave Martin * In case p was allocated the same task_struct pointer as some
363071b6d4aSDave Martin * other recently-exited task, make sure p is disassociated from
364071b6d4aSDave Martin * any cpu that may have run that now-exited task recently.
365071b6d4aSDave Martin * Otherwise we could erroneously skip reloading the FPSIMD
366071b6d4aSDave Martin * registers for p.
367071b6d4aSDave Martin */
368071b6d4aSDave Martin fpsimd_flush_task_state(p);
369071b6d4aSDave Martin
37033e45234SKristina Martsenko ptrauth_thread_init_kernel(p);
37133e45234SKristina Martsenko
3725bd2e97cSEric W. Biederman if (likely(!args->fn)) {
3739ac08002SAl Viro *childregs = *current_pt_regs();
374b3901d54SCatalin Marinas childregs->regs[0] = 0;
375d00a3810SWill Deacon
376b3901d54SCatalin Marinas /*
377b3901d54SCatalin Marinas * Read the current TLS pointer from tpidr_el0 as it may be
378b3901d54SCatalin Marinas * out-of-sync with the saved value.
379b3901d54SCatalin Marinas */
380adf75899SMark Rutland *task_user_tls(p) = read_sysreg(tpidr_el0);
381a9d69158SMark Brown if (system_supports_tpidr2())
382a9d69158SMark Brown p->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0);
383d00a3810SWill Deacon
384160a8e13SJoey Gouly if (system_supports_poe())
385160a8e13SJoey Gouly p->thread.por_el0 = read_sysreg_s(SYS_POR_EL0);
386160a8e13SJoey Gouly
387e0fd18ceSAl Viro if (stack_start) {
388d00a3810SWill Deacon if (is_compat_thread(task_thread_info(p)))
389d00a3810SWill Deacon childregs->compat_sp = stack_start;
390d00a3810SWill Deacon else
391b3901d54SCatalin Marinas childregs->sp = stack_start;
392b3901d54SCatalin Marinas }
393d00a3810SWill Deacon
394c34501d2SCatalin Marinas /*
395a4376f2fSAmanieu d'Antras * If a TLS pointer was passed to clone, use it for the new
396a9d69158SMark Brown * thread. We also reset TPIDR2 if it's in use.
397c34501d2SCatalin Marinas */
398a9d69158SMark Brown if (clone_flags & CLONE_SETTLS) {
399a4376f2fSAmanieu d'Antras p->thread.uw.tp_value = tls;
400a9d69158SMark Brown p->thread.tpidr2_el0 = 0;
401a9d69158SMark Brown }
402c34501d2SCatalin Marinas } else {
403f80d0340SMark Rutland /*
404f80d0340SMark Rutland * A kthread has no context to ERET to, so ensure any buggy
405f80d0340SMark Rutland * ERET is treated as an illegal exception return.
406f80d0340SMark Rutland *
407f80d0340SMark Rutland * When a user task is created from a kthread, childregs will
408f80d0340SMark Rutland * be initialized by start_thread() or start_compat_thread().
409f80d0340SMark Rutland */
410c34501d2SCatalin Marinas memset(childregs, 0, sizeof(struct pt_regs));
411f80d0340SMark Rutland childregs->pstate = PSR_MODE_EL1h | PSR_IL_BIT;
412133d0518SJulien Thierry
4135bd2e97cSEric W. Biederman p->thread.cpu_context.x19 = (unsigned long)args->fn;
4145bd2e97cSEric W. Biederman p->thread.cpu_context.x20 = (unsigned long)args->fn_arg;
415*e3e85271SJoey Gouly
416*e3e85271SJoey Gouly if (system_supports_poe())
417*e3e85271SJoey Gouly p->thread.por_el0 = POR_EL0_INIT;
418c34501d2SCatalin Marinas }
419c34501d2SCatalin Marinas p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
420c34501d2SCatalin Marinas p->thread.cpu_context.sp = (unsigned long)childregs;
4217d7b720aSMadhavan T. Venkataraman /*
4227d7b720aSMadhavan T. Venkataraman * For the benefit of the unwinder, set up childregs->stackframe
4237d7b720aSMadhavan T. Venkataraman * as the final frame for the new task.
4247d7b720aSMadhavan T. Venkataraman */
4257d7b720aSMadhavan T. Venkataraman p->thread.cpu_context.fp = (unsigned long)childregs->stackframe;
426b3901d54SCatalin Marinas
427b3901d54SCatalin Marinas ptrace_hw_copy_thread(p);
428b3901d54SCatalin Marinas
429b3901d54SCatalin Marinas return 0;
430b3901d54SCatalin Marinas }
431b3901d54SCatalin Marinas
tls_preserve_current_state(void)432936eb65cSDave Martin void tls_preserve_current_state(void)
433936eb65cSDave Martin {
434936eb65cSDave Martin *task_user_tls(current) = read_sysreg(tpidr_el0);
435a9d69158SMark Brown if (system_supports_tpidr2() && !is_compat_task())
436a9d69158SMark Brown current->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0);
437936eb65cSDave Martin }
438936eb65cSDave Martin
tls_thread_switch(struct task_struct * next)439b3901d54SCatalin Marinas static void tls_thread_switch(struct task_struct *next)
440b3901d54SCatalin Marinas {
441936eb65cSDave Martin tls_preserve_current_state();
442b3901d54SCatalin Marinas
44318011eacSWill Deacon if (is_compat_thread(task_thread_info(next)))
44465896545SDave Martin write_sysreg(next->thread.uw.tp_value, tpidrro_el0);
44518011eacSWill Deacon else if (!arm64_kernel_unmapped_at_el0())
44618011eacSWill Deacon write_sysreg(0, tpidrro_el0);
447b3901d54SCatalin Marinas
44818011eacSWill Deacon write_sysreg(*task_user_tls(next), tpidr_el0);
449a9d69158SMark Brown if (system_supports_tpidr2())
450a9d69158SMark Brown write_sysreg_s(next->thread.tpidr2_el0, SYS_TPIDR2_EL0);
451b3901d54SCatalin Marinas }
452b3901d54SCatalin Marinas
453b3901d54SCatalin Marinas /*
454cbdf8a18SMarc Zyngier * Force SSBS state on context-switch, since it may be lost after migrating
455cbdf8a18SMarc Zyngier * from a CPU which treats the bit as RES0 in a heterogeneous system.
456cbdf8a18SMarc Zyngier */
ssbs_thread_switch(struct task_struct * next)457cbdf8a18SMarc Zyngier static void ssbs_thread_switch(struct task_struct *next)
458cbdf8a18SMarc Zyngier {
459cbdf8a18SMarc Zyngier /*
460cbdf8a18SMarc Zyngier * Nothing to do for kernel threads, but 'regs' may be junk
461cbdf8a18SMarc Zyngier * (e.g. idle task) so check the flags and bail early.
462cbdf8a18SMarc Zyngier */
463cbdf8a18SMarc Zyngier if (unlikely(next->flags & PF_KTHREAD))
464cbdf8a18SMarc Zyngier return;
465cbdf8a18SMarc Zyngier
466fca3d33dSWill Deacon /*
467fca3d33dSWill Deacon * If all CPUs implement the SSBS extension, then we just need to
468fca3d33dSWill Deacon * context-switch the PSTATE field.
469fca3d33dSWill Deacon */
470bc75d0c0SMark Rutland if (alternative_has_cap_unlikely(ARM64_SSBS))
471fca3d33dSWill Deacon return;
472fca3d33dSWill Deacon
473c2876207SWill Deacon spectre_v4_enable_task_mitigation(next);
474cbdf8a18SMarc Zyngier }
475cbdf8a18SMarc Zyngier
476cbdf8a18SMarc Zyngier /*
477c02433ddSMark Rutland * We store our current task in sp_el0, which is clobbered by userspace. Keep a
478c02433ddSMark Rutland * shadow copy so that we can restore this upon entry from userspace.
479c02433ddSMark Rutland *
480c02433ddSMark Rutland * This is *only* for exception entry from EL0, and is not valid until we
481c02433ddSMark Rutland * __switch_to() a user task.
482c02433ddSMark Rutland */
483c02433ddSMark Rutland DEFINE_PER_CPU(struct task_struct *, __entry_task);
484c02433ddSMark Rutland
entry_task_switch(struct task_struct * next)485c02433ddSMark Rutland static void entry_task_switch(struct task_struct *next)
486c02433ddSMark Rutland {
487c02433ddSMark Rutland __this_cpu_write(__entry_task, next);
488c02433ddSMark Rutland }
489c02433ddSMark Rutland
490c02433ddSMark Rutland /*
4913e9e67e1SPeter Collingbourne * Handle sysreg updates for ARM erratum 1418040 which affects the 32bit view of
4923e9e67e1SPeter Collingbourne * CNTVCT, various other errata which require trapping all CNTVCT{,_EL0}
4933e9e67e1SPeter Collingbourne * accesses and prctl(PR_SET_TSC). Ensure access is disabled iff a workaround is
4943e9e67e1SPeter Collingbourne * required or PR_TSC_SIGSEGV is set.
495d49f7d73SMarc Zyngier */
update_cntkctl_el1(struct task_struct * next)4963e9e67e1SPeter Collingbourne static void update_cntkctl_el1(struct task_struct *next)
497d49f7d73SMarc Zyngier {
4983e9e67e1SPeter Collingbourne struct thread_info *ti = task_thread_info(next);
499d49f7d73SMarc Zyngier
5003e9e67e1SPeter Collingbourne if (test_ti_thread_flag(ti, TIF_TSC_SIGSEGV) ||
5013e9e67e1SPeter Collingbourne has_erratum_handler(read_cntvct_el0) ||
5023e9e67e1SPeter Collingbourne (IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040) &&
5033e9e67e1SPeter Collingbourne this_cpu_has_cap(ARM64_WORKAROUND_1418040) &&
5043e9e67e1SPeter Collingbourne is_compat_thread(ti)))
50538e0257eSD Scott Phillips sysreg_clear_set(cntkctl_el1, ARCH_TIMER_USR_VCT_ACCESS_EN, 0);
506d49f7d73SMarc Zyngier else
50738e0257eSD Scott Phillips sysreg_clear_set(cntkctl_el1, 0, ARCH_TIMER_USR_VCT_ACCESS_EN);
50838e0257eSD Scott Phillips }
509d49f7d73SMarc Zyngier
cntkctl_thread_switch(struct task_struct * prev,struct task_struct * next)5103e9e67e1SPeter Collingbourne static void cntkctl_thread_switch(struct task_struct *prev,
5113e9e67e1SPeter Collingbourne struct task_struct *next)
51238e0257eSD Scott Phillips {
5133e9e67e1SPeter Collingbourne if ((read_ti_thread_flags(task_thread_info(prev)) &
5143e9e67e1SPeter Collingbourne (_TIF_32BIT | _TIF_TSC_SIGSEGV)) !=
5153e9e67e1SPeter Collingbourne (read_ti_thread_flags(task_thread_info(next)) &
5163e9e67e1SPeter Collingbourne (_TIF_32BIT | _TIF_TSC_SIGSEGV)))
5173e9e67e1SPeter Collingbourne update_cntkctl_el1(next);
5183e9e67e1SPeter Collingbourne }
5193e9e67e1SPeter Collingbourne
do_set_tsc_mode(unsigned int val)5203e9e67e1SPeter Collingbourne static int do_set_tsc_mode(unsigned int val)
5213e9e67e1SPeter Collingbourne {
5223e9e67e1SPeter Collingbourne bool tsc_sigsegv;
5233e9e67e1SPeter Collingbourne
5243e9e67e1SPeter Collingbourne if (val == PR_TSC_SIGSEGV)
5253e9e67e1SPeter Collingbourne tsc_sigsegv = true;
5263e9e67e1SPeter Collingbourne else if (val == PR_TSC_ENABLE)
5273e9e67e1SPeter Collingbourne tsc_sigsegv = false;
5283e9e67e1SPeter Collingbourne else
5293e9e67e1SPeter Collingbourne return -EINVAL;
5303e9e67e1SPeter Collingbourne
53138e0257eSD Scott Phillips preempt_disable();
5323e9e67e1SPeter Collingbourne update_thread_flag(TIF_TSC_SIGSEGV, tsc_sigsegv);
5333e9e67e1SPeter Collingbourne update_cntkctl_el1(current);
53438e0257eSD Scott Phillips preempt_enable();
5353e9e67e1SPeter Collingbourne
5363e9e67e1SPeter Collingbourne return 0;
537d49f7d73SMarc Zyngier }
538d49f7d73SMarc Zyngier
permission_overlay_switch(struct task_struct * next)539160a8e13SJoey Gouly static void permission_overlay_switch(struct task_struct *next)
540160a8e13SJoey Gouly {
541160a8e13SJoey Gouly if (!system_supports_poe())
542160a8e13SJoey Gouly return;
543160a8e13SJoey Gouly
544160a8e13SJoey Gouly current->thread.por_el0 = read_sysreg_s(SYS_POR_EL0);
545160a8e13SJoey Gouly if (current->thread.por_el0 != next->thread.por_el0) {
546160a8e13SJoey Gouly write_sysreg_s(next->thread.por_el0, SYS_POR_EL0);
547160a8e13SJoey Gouly }
548160a8e13SJoey Gouly }
549160a8e13SJoey Gouly
550d2e0d8f9SPeter Collingbourne /*
551d2e0d8f9SPeter Collingbourne * __switch_to() checks current->thread.sctlr_user as an optimisation. Therefore
552d2e0d8f9SPeter Collingbourne * this function must be called with preemption disabled and the update to
553d2e0d8f9SPeter Collingbourne * sctlr_user must be made in the same preemption disabled block so that
554d2e0d8f9SPeter Collingbourne * __switch_to() does not see the variable update before the SCTLR_EL1 one.
555d2e0d8f9SPeter Collingbourne */
update_sctlr_el1(u64 sctlr)556d2e0d8f9SPeter Collingbourne void update_sctlr_el1(u64 sctlr)
5572f79d2fcSPeter Collingbourne {
55820169862SPeter Collingbourne /*
55920169862SPeter Collingbourne * EnIA must not be cleared while in the kernel as this is necessary for
56020169862SPeter Collingbourne * in-kernel PAC. It will be cleared on kernel exit if needed.
56120169862SPeter Collingbourne */
56220169862SPeter Collingbourne sysreg_clear_set(sctlr_el1, SCTLR_USER_MASK & ~SCTLR_ELx_ENIA, sctlr);
5632f79d2fcSPeter Collingbourne
5642f79d2fcSPeter Collingbourne /* ISB required for the kernel uaccess routines when setting TCF0. */
5652f79d2fcSPeter Collingbourne isb();
5662f79d2fcSPeter Collingbourne }
5672f79d2fcSPeter Collingbourne
568d49f7d73SMarc Zyngier /*
569b3901d54SCatalin Marinas * Thread switching.
570b3901d54SCatalin Marinas */
57186bcbafcSMark Rutland __notrace_funcgraph __sched
__switch_to(struct task_struct * prev,struct task_struct * next)57286bcbafcSMark Rutland struct task_struct *__switch_to(struct task_struct *prev,
573b3901d54SCatalin Marinas struct task_struct *next)
574b3901d54SCatalin Marinas {
575b3901d54SCatalin Marinas struct task_struct *last;
576b3901d54SCatalin Marinas
577b3901d54SCatalin Marinas fpsimd_thread_switch(next);
578b3901d54SCatalin Marinas tls_thread_switch(next);
579b3901d54SCatalin Marinas hw_breakpoint_thread_switch(next);
5803325732fSChristopher Covington contextidr_thread_switch(next);
581c02433ddSMark Rutland entry_task_switch(next);
582cbdf8a18SMarc Zyngier ssbs_thread_switch(next);
5833e9e67e1SPeter Collingbourne cntkctl_thread_switch(prev, next);
584b90e4839SPeter Collingbourne ptrauth_thread_switch_user(next);
585160a8e13SJoey Gouly permission_overlay_switch(next);
586b3901d54SCatalin Marinas
5875108c67cSCatalin Marinas /*
5885108c67cSCatalin Marinas * Complete any pending TLB or cache maintenance on this CPU in case
5895108c67cSCatalin Marinas * the thread migrates to a different CPU.
59022e4ebb9SMathieu Desnoyers * This full barrier is also required by the membarrier system
59122e4ebb9SMathieu Desnoyers * call.
5925108c67cSCatalin Marinas */
59398f7685eSWill Deacon dsb(ish);
594b3901d54SCatalin Marinas
5951c101da8SCatalin Marinas /*
5961c101da8SCatalin Marinas * MTE thread switching must happen after the DSB above to ensure that
5971c101da8SCatalin Marinas * any asynchronous tag check faults have been logged in the TFSR*_EL1
5981c101da8SCatalin Marinas * registers.
5991c101da8SCatalin Marinas */
6001c101da8SCatalin Marinas mte_thread_switch(next);
6012f79d2fcSPeter Collingbourne /* avoid expensive SCTLR_EL1 accesses if no change */
6022f79d2fcSPeter Collingbourne if (prev->thread.sctlr_user != next->thread.sctlr_user)
6032f79d2fcSPeter Collingbourne update_sctlr_el1(next->thread.sctlr_user);
6041c101da8SCatalin Marinas
605b3901d54SCatalin Marinas /* the actual thread switch */
606b3901d54SCatalin Marinas last = cpu_switch_to(prev, next);
607b3901d54SCatalin Marinas
608b3901d54SCatalin Marinas return last;
609b3901d54SCatalin Marinas }
610b3901d54SCatalin Marinas
6114f62bb7cSMadhavan T. Venkataraman struct wchan_info {
6124f62bb7cSMadhavan T. Venkataraman unsigned long pc;
6134f62bb7cSMadhavan T. Venkataraman int count;
6144f62bb7cSMadhavan T. Venkataraman };
6154f62bb7cSMadhavan T. Venkataraman
get_wchan_cb(void * arg,unsigned long pc)6164f62bb7cSMadhavan T. Venkataraman static bool get_wchan_cb(void *arg, unsigned long pc)
6174f62bb7cSMadhavan T. Venkataraman {
6184f62bb7cSMadhavan T. Venkataraman struct wchan_info *wchan_info = arg;
6194f62bb7cSMadhavan T. Venkataraman
6204f62bb7cSMadhavan T. Venkataraman if (!in_sched_functions(pc)) {
6214f62bb7cSMadhavan T. Venkataraman wchan_info->pc = pc;
6224f62bb7cSMadhavan T. Venkataraman return false;
6234f62bb7cSMadhavan T. Venkataraman }
6244f62bb7cSMadhavan T. Venkataraman return wchan_info->count++ < 16;
6254f62bb7cSMadhavan T. Venkataraman }
6264f62bb7cSMadhavan T. Venkataraman
__get_wchan(struct task_struct * p)62742a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p)
628b3901d54SCatalin Marinas {
6294f62bb7cSMadhavan T. Venkataraman struct wchan_info wchan_info = {
6304f62bb7cSMadhavan T. Venkataraman .pc = 0,
6314f62bb7cSMadhavan T. Venkataraman .count = 0,
6324f62bb7cSMadhavan T. Venkataraman };
633b3901d54SCatalin Marinas
6344f62bb7cSMadhavan T. Venkataraman if (!try_get_task_stack(p))
6359bbd4c56SMark Rutland return 0;
6369bbd4c56SMark Rutland
6374f62bb7cSMadhavan T. Venkataraman arch_stack_walk(get_wchan_cb, &wchan_info, p, NULL);
638f3dcbe67SDave Martin
6399bbd4c56SMark Rutland put_task_stack(p);
6404f62bb7cSMadhavan T. Venkataraman
6414f62bb7cSMadhavan T. Venkataraman return wchan_info.pc;
642b3901d54SCatalin Marinas }
643b3901d54SCatalin Marinas
arch_align_stack(unsigned long sp)644b3901d54SCatalin Marinas unsigned long arch_align_stack(unsigned long sp)
645b3901d54SCatalin Marinas {
646b3901d54SCatalin Marinas if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
6478032bf12SJason A. Donenfeld sp -= get_random_u32_below(PAGE_SIZE);
648b3901d54SCatalin Marinas return sp & ~0xf;
649b3901d54SCatalin Marinas }
650b3901d54SCatalin Marinas
65108cd8f41SWill Deacon #ifdef CONFIG_COMPAT
compat_elf_check_arch(const struct elf32_hdr * hdr)65208cd8f41SWill Deacon int compat_elf_check_arch(const struct elf32_hdr *hdr)
65308cd8f41SWill Deacon {
65408cd8f41SWill Deacon if (!system_supports_32bit_el0())
65508cd8f41SWill Deacon return false;
65608cd8f41SWill Deacon
65708cd8f41SWill Deacon if ((hdr)->e_machine != EM_ARM)
65808cd8f41SWill Deacon return false;
65908cd8f41SWill Deacon
66008cd8f41SWill Deacon if (!((hdr)->e_flags & EF_ARM_EABI_MASK))
66108cd8f41SWill Deacon return false;
66208cd8f41SWill Deacon
66308cd8f41SWill Deacon /*
66408cd8f41SWill Deacon * Prevent execve() of a 32-bit program from a deadline task
66508cd8f41SWill Deacon * if the restricted affinity mask would be inadmissible on an
66608cd8f41SWill Deacon * asymmetric system.
66708cd8f41SWill Deacon */
66808cd8f41SWill Deacon return !static_branch_unlikely(&arm64_mismatched_32bit_el0) ||
66908cd8f41SWill Deacon !dl_task_check_affinity(current, system_32bit_el0_cpumask());
67008cd8f41SWill Deacon }
67108cd8f41SWill Deacon #endif
67208cd8f41SWill Deacon
673d1be5c99SYury Norov /*
674d1be5c99SYury Norov * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY.
675d1be5c99SYury Norov */
arch_setup_new_exec(void)676d1be5c99SYury Norov void arch_setup_new_exec(void)
677d1be5c99SYury Norov {
678873c3e89SWill Deacon unsigned long mmflags = 0;
67975031975SMark Rutland
680873c3e89SWill Deacon if (is_compat_task()) {
681873c3e89SWill Deacon mmflags = MMCF_AARCH32;
68208cd8f41SWill Deacon
68308cd8f41SWill Deacon /*
68408cd8f41SWill Deacon * Restrict the CPU affinity mask for a 32-bit task so that
68508cd8f41SWill Deacon * it contains only 32-bit-capable CPUs.
68608cd8f41SWill Deacon *
68708cd8f41SWill Deacon * From the perspective of the task, this looks similar to
68808cd8f41SWill Deacon * what would happen if the 64-bit-only CPUs were hot-unplugged
68908cd8f41SWill Deacon * at the point of execve(), although we try a bit harder to
69008cd8f41SWill Deacon * honour the cpuset hierarchy.
69108cd8f41SWill Deacon */
692873c3e89SWill Deacon if (static_branch_unlikely(&arm64_mismatched_32bit_el0))
69308cd8f41SWill Deacon force_compatible_cpus_allowed_ptr(current);
69408cd8f41SWill Deacon } else if (static_branch_unlikely(&arm64_mismatched_32bit_el0)) {
69508cd8f41SWill Deacon relax_compatible_cpus_allowed_ptr(current);
696873c3e89SWill Deacon }
697873c3e89SWill Deacon
698873c3e89SWill Deacon current->mm->context.flags = mmflags;
69920169862SPeter Collingbourne ptrauth_thread_init_user();
70020169862SPeter Collingbourne mte_thread_init_user();
7013e9e67e1SPeter Collingbourne do_set_tsc_mode(PR_TSC_ENABLE);
702780c083aSWill Deacon
703780c083aSWill Deacon if (task_spec_ssb_noexec(current)) {
704780c083aSWill Deacon arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS,
705780c083aSWill Deacon PR_SPEC_ENABLE);
706780c083aSWill Deacon }
707d1be5c99SYury Norov }
70863f0c603SCatalin Marinas
70963f0c603SCatalin Marinas #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
71063f0c603SCatalin Marinas /*
71163f0c603SCatalin Marinas * Control the relaxed ABI allowing tagged user addresses into the kernel.
71263f0c603SCatalin Marinas */
713413235fcSCatalin Marinas static unsigned int tagged_addr_disabled;
71463f0c603SCatalin Marinas
set_tagged_addr_ctrl(struct task_struct * task,unsigned long arg)71593f067f6SCatalin Marinas long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg)
71663f0c603SCatalin Marinas {
7171c101da8SCatalin Marinas unsigned long valid_mask = PR_TAGGED_ADDR_ENABLE;
71893f067f6SCatalin Marinas struct thread_info *ti = task_thread_info(task);
7191c101da8SCatalin Marinas
72093f067f6SCatalin Marinas if (is_compat_thread(ti))
72163f0c603SCatalin Marinas return -EINVAL;
7221c101da8SCatalin Marinas
7231c101da8SCatalin Marinas if (system_supports_mte())
724766121baSMark Brown valid_mask |= PR_MTE_TCF_SYNC | PR_MTE_TCF_ASYNC \
725766121baSMark Brown | PR_MTE_TAG_MASK;
7261c101da8SCatalin Marinas
7271c101da8SCatalin Marinas if (arg & ~valid_mask)
72863f0c603SCatalin Marinas return -EINVAL;
72963f0c603SCatalin Marinas
730413235fcSCatalin Marinas /*
731413235fcSCatalin Marinas * Do not allow the enabling of the tagged address ABI if globally
732413235fcSCatalin Marinas * disabled via sysctl abi.tagged_addr_disabled.
733413235fcSCatalin Marinas */
734413235fcSCatalin Marinas if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled)
735413235fcSCatalin Marinas return -EINVAL;
736413235fcSCatalin Marinas
73793f067f6SCatalin Marinas if (set_mte_ctrl(task, arg) != 0)
7381c101da8SCatalin Marinas return -EINVAL;
7391c101da8SCatalin Marinas
74093f067f6SCatalin Marinas update_ti_thread_flag(ti, TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE);
74163f0c603SCatalin Marinas
74263f0c603SCatalin Marinas return 0;
74363f0c603SCatalin Marinas }
74463f0c603SCatalin Marinas
get_tagged_addr_ctrl(struct task_struct * task)74593f067f6SCatalin Marinas long get_tagged_addr_ctrl(struct task_struct *task)
74663f0c603SCatalin Marinas {
7471c101da8SCatalin Marinas long ret = 0;
74893f067f6SCatalin Marinas struct thread_info *ti = task_thread_info(task);
7491c101da8SCatalin Marinas
75093f067f6SCatalin Marinas if (is_compat_thread(ti))
75163f0c603SCatalin Marinas return -EINVAL;
75263f0c603SCatalin Marinas
75393f067f6SCatalin Marinas if (test_ti_thread_flag(ti, TIF_TAGGED_ADDR))
7541c101da8SCatalin Marinas ret = PR_TAGGED_ADDR_ENABLE;
75563f0c603SCatalin Marinas
75693f067f6SCatalin Marinas ret |= get_mte_ctrl(task);
7571c101da8SCatalin Marinas
7581c101da8SCatalin Marinas return ret;
75963f0c603SCatalin Marinas }
76063f0c603SCatalin Marinas
76163f0c603SCatalin Marinas /*
76263f0c603SCatalin Marinas * Global sysctl to disable the tagged user addresses support. This control
76363f0c603SCatalin Marinas * only prevents the tagged address ABI enabling via prctl() and does not
76463f0c603SCatalin Marinas * disable it for tasks that already opted in to the relaxed ABI.
76563f0c603SCatalin Marinas */
76663f0c603SCatalin Marinas
76763f0c603SCatalin Marinas static struct ctl_table tagged_addr_sysctl_table[] = {
76863f0c603SCatalin Marinas {
769413235fcSCatalin Marinas .procname = "tagged_addr_disabled",
77063f0c603SCatalin Marinas .mode = 0644,
771413235fcSCatalin Marinas .data = &tagged_addr_disabled,
77263f0c603SCatalin Marinas .maxlen = sizeof(int),
77363f0c603SCatalin Marinas .proc_handler = proc_dointvec_minmax,
7742c614c11SMatteo Croce .extra1 = SYSCTL_ZERO,
7752c614c11SMatteo Croce .extra2 = SYSCTL_ONE,
77663f0c603SCatalin Marinas },
77763f0c603SCatalin Marinas };
77863f0c603SCatalin Marinas
tagged_addr_init(void)77963f0c603SCatalin Marinas static int __init tagged_addr_init(void)
78063f0c603SCatalin Marinas {
78163f0c603SCatalin Marinas if (!register_sysctl("abi", tagged_addr_sysctl_table))
78263f0c603SCatalin Marinas return -EINVAL;
78363f0c603SCatalin Marinas return 0;
78463f0c603SCatalin Marinas }
78563f0c603SCatalin Marinas
78663f0c603SCatalin Marinas core_initcall(tagged_addr_init);
78763f0c603SCatalin Marinas #endif /* CONFIG_ARM64_TAGGED_ADDR_ABI */
78819c95f26SJulien Thierry
789ab7876a9SDave Martin #ifdef CONFIG_BINFMT_ELF
arch_elf_adjust_prot(int prot,const struct arch_elf_state * state,bool has_interp,bool is_interp)790ab7876a9SDave Martin int arch_elf_adjust_prot(int prot, const struct arch_elf_state *state,
791ab7876a9SDave Martin bool has_interp, bool is_interp)
792ab7876a9SDave Martin {
7935d1b631cSMark Brown /*
7945d1b631cSMark Brown * For dynamically linked executables the interpreter is
7955d1b631cSMark Brown * responsible for setting PROT_BTI on everything except
7965d1b631cSMark Brown * itself.
7975d1b631cSMark Brown */
798ab7876a9SDave Martin if (is_interp != has_interp)
799ab7876a9SDave Martin return prot;
800ab7876a9SDave Martin
801ab7876a9SDave Martin if (!(state->flags & ARM64_ELF_BTI))
802ab7876a9SDave Martin return prot;
803ab7876a9SDave Martin
804ab7876a9SDave Martin if (prot & PROT_EXEC)
805ab7876a9SDave Martin prot |= PROT_BTI;
806ab7876a9SDave Martin
807ab7876a9SDave Martin return prot;
808ab7876a9SDave Martin }
809ab7876a9SDave Martin #endif
8103e9e67e1SPeter Collingbourne
get_tsc_mode(unsigned long adr)8113e9e67e1SPeter Collingbourne int get_tsc_mode(unsigned long adr)
8123e9e67e1SPeter Collingbourne {
8133e9e67e1SPeter Collingbourne unsigned int val;
8143e9e67e1SPeter Collingbourne
8153e9e67e1SPeter Collingbourne if (is_compat_task())
8163e9e67e1SPeter Collingbourne return -EINVAL;
8173e9e67e1SPeter Collingbourne
8183e9e67e1SPeter Collingbourne if (test_thread_flag(TIF_TSC_SIGSEGV))
8193e9e67e1SPeter Collingbourne val = PR_TSC_SIGSEGV;
8203e9e67e1SPeter Collingbourne else
8213e9e67e1SPeter Collingbourne val = PR_TSC_ENABLE;
8223e9e67e1SPeter Collingbourne
8233e9e67e1SPeter Collingbourne return put_user(val, (unsigned int __user *)adr);
8243e9e67e1SPeter Collingbourne }
8253e9e67e1SPeter Collingbourne
set_tsc_mode(unsigned int val)8263e9e67e1SPeter Collingbourne int set_tsc_mode(unsigned int val)
8273e9e67e1SPeter Collingbourne {
8283e9e67e1SPeter Collingbourne if (is_compat_task())
8293e9e67e1SPeter Collingbourne return -EINVAL;
8303e9e67e1SPeter Collingbourne
8313e9e67e1SPeter Collingbourne return do_set_tsc_mode(val);
8323e9e67e1SPeter Collingbourne }
833