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> 43*4f62bb7cSMadhavan T. Venkataraman #include <linux/stacktrace.h> 44b3901d54SCatalin Marinas 4557f4959bSJames Morse #include <asm/alternative.h> 46b3901d54SCatalin Marinas #include <asm/compat.h> 4719c95f26SJulien Thierry #include <asm/cpufeature.h> 48b3901d54SCatalin Marinas #include <asm/cacheflush.h> 49d0854412SJames Morse #include <asm/exec.h> 50ec45d1cfSWill Deacon #include <asm/fpsimd.h> 51ec45d1cfSWill Deacon #include <asm/mmu_context.h> 52637ec831SVincenzo Frascino #include <asm/mte.h> 53b3901d54SCatalin Marinas #include <asm/processor.h> 5475031975SMark Rutland #include <asm/pointer_auth.h> 55b3901d54SCatalin Marinas #include <asm/stacktrace.h> 56baa96377SManinder Singh #include <asm/switch_to.h> 57baa96377SManinder Singh #include <asm/system_misc.h> 58b3901d54SCatalin Marinas 590a1213faSArd Biesheuvel #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) 60c0c264aeSLaura Abbott #include <linux/stackprotector.h> 619fcb2e93SDan Li unsigned long __stack_chk_guard __ro_after_init; 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 719327e2c6SMark Rutland #ifdef CONFIG_HOTPLUG_CPU 729327e2c6SMark Rutland void arch_cpu_idle_dead(void) 739327e2c6SMark Rutland { 749327e2c6SMark Rutland cpu_die(); 759327e2c6SMark Rutland } 769327e2c6SMark Rutland #endif 779327e2c6SMark Rutland 7890f51a09SArun KS /* 7990f51a09SArun KS * Called by kexec, immediately prior to machine_kexec(). 8090f51a09SArun KS * 8190f51a09SArun KS * This must completely disable all secondary CPUs; simply causing those CPUs 8290f51a09SArun KS * to execute e.g. a RAM-based pin loop is not sufficient. This allows the 8390f51a09SArun KS * kexec'd kernel to use any and all RAM as it sees fit, without having to 8490f51a09SArun KS * avoid any code or data used by any SW CPU pin loop. The CPU hotplug 85d66b16f5SQais Yousef * functionality embodied in smpt_shutdown_nonboot_cpus() to achieve this. 8690f51a09SArun KS */ 87b3901d54SCatalin Marinas void machine_shutdown(void) 88b3901d54SCatalin Marinas { 895efbe6a6SQais Yousef smp_shutdown_nonboot_cpus(reboot_cpu); 90b3901d54SCatalin Marinas } 91b3901d54SCatalin Marinas 9290f51a09SArun KS /* 9390f51a09SArun KS * Halting simply requires that the secondary CPUs stop performing any 9490f51a09SArun KS * activity (executing tasks, handling interrupts). smp_send_stop() 9590f51a09SArun KS * achieves this. 9690f51a09SArun KS */ 97b3901d54SCatalin Marinas void machine_halt(void) 98b3901d54SCatalin Marinas { 99b9acc49eSArun KS local_irq_disable(); 10090f51a09SArun KS smp_send_stop(); 101b3901d54SCatalin Marinas while (1); 102b3901d54SCatalin Marinas } 103b3901d54SCatalin Marinas 10490f51a09SArun KS /* 10590f51a09SArun KS * Power-off simply requires that the secondary CPUs stop performing any 10690f51a09SArun KS * activity (executing tasks, handling interrupts). smp_send_stop() 10790f51a09SArun KS * achieves this. When the system power is turned off, it will take all CPUs 10890f51a09SArun KS * with it. 10990f51a09SArun KS */ 110b3901d54SCatalin Marinas void machine_power_off(void) 111b3901d54SCatalin Marinas { 112b9acc49eSArun KS local_irq_disable(); 11390f51a09SArun KS smp_send_stop(); 114b3901d54SCatalin Marinas if (pm_power_off) 115b3901d54SCatalin Marinas pm_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 */ 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 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 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); 221cdcb61aeSAmit Daniel Kachhap printk("lr : %pS\n", (void *)ptrauth_strip_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 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 250eb35bdd7SWill Deacon static void tls_thread_flush(void) 251eb35bdd7SWill Deacon { 252adf75899SMark Rutland write_sysreg(0, tpidr_el0); 253eb35bdd7SWill Deacon 254eb35bdd7SWill Deacon if (is_compat_task()) { 25565896545SDave Martin current->thread.uw.tp_value = 0; 256eb35bdd7SWill Deacon 257eb35bdd7SWill Deacon /* 258eb35bdd7SWill Deacon * We need to ensure ordering between the shadow state and the 259eb35bdd7SWill Deacon * hardware state, so that we don't corrupt the hardware state 260eb35bdd7SWill Deacon * with a stale shadow state during context switch. 261eb35bdd7SWill Deacon */ 262eb35bdd7SWill Deacon barrier(); 263adf75899SMark Rutland write_sysreg(0, tpidrro_el0); 264eb35bdd7SWill Deacon } 265eb35bdd7SWill Deacon } 266eb35bdd7SWill Deacon 26763f0c603SCatalin Marinas static void flush_tagged_addr_state(void) 26863f0c603SCatalin Marinas { 26963f0c603SCatalin Marinas if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI)) 27063f0c603SCatalin Marinas clear_thread_flag(TIF_TAGGED_ADDR); 27163f0c603SCatalin Marinas } 27263f0c603SCatalin Marinas 273b3901d54SCatalin Marinas void flush_thread(void) 274b3901d54SCatalin Marinas { 275b3901d54SCatalin Marinas fpsimd_flush_thread(); 276eb35bdd7SWill Deacon tls_thread_flush(); 277b3901d54SCatalin Marinas flush_ptrace_hw_breakpoint(current); 27863f0c603SCatalin Marinas flush_tagged_addr_state(); 279b3901d54SCatalin Marinas } 280b3901d54SCatalin Marinas 281b3901d54SCatalin Marinas void release_thread(struct task_struct *dead_task) 282b3901d54SCatalin Marinas { 283b3901d54SCatalin Marinas } 284b3901d54SCatalin Marinas 285bc0ee476SDave Martin void arch_release_task_struct(struct task_struct *tsk) 286bc0ee476SDave Martin { 287bc0ee476SDave Martin fpsimd_release_task(tsk); 288bc0ee476SDave Martin } 289bc0ee476SDave Martin 290b3901d54SCatalin Marinas int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) 291b3901d54SCatalin Marinas { 2926eb6c801SJanet Liu if (current->mm) 293c51f9269SArd Biesheuvel fpsimd_preserve_current_state(); 294b3901d54SCatalin Marinas *dst = *src; 295bc0ee476SDave Martin 2964585fc59SMasayoshi Mizuma /* We rely on the above assignment to initialize dst's thread_flags: */ 2974585fc59SMasayoshi Mizuma BUILD_BUG_ON(!IS_ENABLED(CONFIG_THREAD_INFO_IN_TASK)); 2984585fc59SMasayoshi Mizuma 2994585fc59SMasayoshi Mizuma /* 3004585fc59SMasayoshi Mizuma * Detach src's sve_state (if any) from dst so that it does not 3014585fc59SMasayoshi Mizuma * get erroneously used or freed prematurely. dst's sve_state 3024585fc59SMasayoshi Mizuma * will be allocated on demand later on if dst uses SVE. 3034585fc59SMasayoshi Mizuma * For consistency, also clear TIF_SVE here: this could be done 3044585fc59SMasayoshi Mizuma * later in copy_process(), but to avoid tripping up future 3054585fc59SMasayoshi Mizuma * maintainers it is best not to leave TIF_SVE and sve_state in 3064585fc59SMasayoshi Mizuma * an inconsistent state, even temporarily. 3074585fc59SMasayoshi Mizuma */ 3084585fc59SMasayoshi Mizuma dst->thread.sve_state = NULL; 3094585fc59SMasayoshi Mizuma clear_tsk_thread_flag(dst, TIF_SVE); 3104585fc59SMasayoshi Mizuma 311637ec831SVincenzo Frascino /* clear any pending asynchronous tag fault raised by the parent */ 312637ec831SVincenzo Frascino clear_tsk_thread_flag(dst, TIF_MTE_ASYNC_FAULT); 313637ec831SVincenzo Frascino 314b3901d54SCatalin Marinas return 0; 315b3901d54SCatalin Marinas } 316b3901d54SCatalin Marinas 317b3901d54SCatalin Marinas asmlinkage void ret_from_fork(void) asm("ret_from_fork"); 318b3901d54SCatalin Marinas 319714acdbdSChristian Brauner int copy_thread(unsigned long clone_flags, unsigned long stack_start, 320a4376f2fSAmanieu d'Antras unsigned long stk_sz, struct task_struct *p, unsigned long tls) 321b3901d54SCatalin Marinas { 322b3901d54SCatalin Marinas struct pt_regs *childregs = task_pt_regs(p); 323b3901d54SCatalin Marinas 324c34501d2SCatalin Marinas memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context)); 325c34501d2SCatalin Marinas 326bc0ee476SDave Martin /* 327071b6d4aSDave Martin * In case p was allocated the same task_struct pointer as some 328071b6d4aSDave Martin * other recently-exited task, make sure p is disassociated from 329071b6d4aSDave Martin * any cpu that may have run that now-exited task recently. 330071b6d4aSDave Martin * Otherwise we could erroneously skip reloading the FPSIMD 331071b6d4aSDave Martin * registers for p. 332071b6d4aSDave Martin */ 333071b6d4aSDave Martin fpsimd_flush_task_state(p); 334071b6d4aSDave Martin 33533e45234SKristina Martsenko ptrauth_thread_init_kernel(p); 33633e45234SKristina Martsenko 3374727dc20SJens Axboe if (likely(!(p->flags & (PF_KTHREAD | PF_IO_WORKER)))) { 3389ac08002SAl Viro *childregs = *current_pt_regs(); 339b3901d54SCatalin Marinas childregs->regs[0] = 0; 340d00a3810SWill Deacon 341b3901d54SCatalin Marinas /* 342b3901d54SCatalin Marinas * Read the current TLS pointer from tpidr_el0 as it may be 343b3901d54SCatalin Marinas * out-of-sync with the saved value. 344b3901d54SCatalin Marinas */ 345adf75899SMark Rutland *task_user_tls(p) = read_sysreg(tpidr_el0); 346d00a3810SWill Deacon 347e0fd18ceSAl Viro if (stack_start) { 348d00a3810SWill Deacon if (is_compat_thread(task_thread_info(p))) 349d00a3810SWill Deacon childregs->compat_sp = stack_start; 350d00a3810SWill Deacon else 351b3901d54SCatalin Marinas childregs->sp = stack_start; 352b3901d54SCatalin Marinas } 353d00a3810SWill Deacon 354c34501d2SCatalin Marinas /* 355a4376f2fSAmanieu d'Antras * If a TLS pointer was passed to clone, use it for the new 356a4376f2fSAmanieu d'Antras * thread. 357c34501d2SCatalin Marinas */ 358b3901d54SCatalin Marinas if (clone_flags & CLONE_SETTLS) 359a4376f2fSAmanieu d'Antras p->thread.uw.tp_value = tls; 360c34501d2SCatalin Marinas } else { 361f80d0340SMark Rutland /* 362f80d0340SMark Rutland * A kthread has no context to ERET to, so ensure any buggy 363f80d0340SMark Rutland * ERET is treated as an illegal exception return. 364f80d0340SMark Rutland * 365f80d0340SMark Rutland * When a user task is created from a kthread, childregs will 366f80d0340SMark Rutland * be initialized by start_thread() or start_compat_thread(). 367f80d0340SMark Rutland */ 368c34501d2SCatalin Marinas memset(childregs, 0, sizeof(struct pt_regs)); 369f80d0340SMark Rutland childregs->pstate = PSR_MODE_EL1h | PSR_IL_BIT; 370133d0518SJulien Thierry 371c34501d2SCatalin Marinas p->thread.cpu_context.x19 = stack_start; 372c34501d2SCatalin Marinas p->thread.cpu_context.x20 = stk_sz; 373c34501d2SCatalin Marinas } 374c34501d2SCatalin Marinas p->thread.cpu_context.pc = (unsigned long)ret_from_fork; 375c34501d2SCatalin Marinas p->thread.cpu_context.sp = (unsigned long)childregs; 3767d7b720aSMadhavan T. Venkataraman /* 3777d7b720aSMadhavan T. Venkataraman * For the benefit of the unwinder, set up childregs->stackframe 3787d7b720aSMadhavan T. Venkataraman * as the final frame for the new task. 3797d7b720aSMadhavan T. Venkataraman */ 3807d7b720aSMadhavan T. Venkataraman p->thread.cpu_context.fp = (unsigned long)childregs->stackframe; 381b3901d54SCatalin Marinas 382b3901d54SCatalin Marinas ptrace_hw_copy_thread(p); 383b3901d54SCatalin Marinas 384b3901d54SCatalin Marinas return 0; 385b3901d54SCatalin Marinas } 386b3901d54SCatalin Marinas 387936eb65cSDave Martin void tls_preserve_current_state(void) 388936eb65cSDave Martin { 389936eb65cSDave Martin *task_user_tls(current) = read_sysreg(tpidr_el0); 390936eb65cSDave Martin } 391936eb65cSDave Martin 392b3901d54SCatalin Marinas static void tls_thread_switch(struct task_struct *next) 393b3901d54SCatalin Marinas { 394936eb65cSDave Martin tls_preserve_current_state(); 395b3901d54SCatalin Marinas 39618011eacSWill Deacon if (is_compat_thread(task_thread_info(next))) 39765896545SDave Martin write_sysreg(next->thread.uw.tp_value, tpidrro_el0); 39818011eacSWill Deacon else if (!arm64_kernel_unmapped_at_el0()) 39918011eacSWill Deacon write_sysreg(0, tpidrro_el0); 400b3901d54SCatalin Marinas 40118011eacSWill Deacon write_sysreg(*task_user_tls(next), tpidr_el0); 402b3901d54SCatalin Marinas } 403b3901d54SCatalin Marinas 404b3901d54SCatalin Marinas /* 405cbdf8a18SMarc Zyngier * Force SSBS state on context-switch, since it may be lost after migrating 406cbdf8a18SMarc Zyngier * from a CPU which treats the bit as RES0 in a heterogeneous system. 407cbdf8a18SMarc Zyngier */ 408cbdf8a18SMarc Zyngier static void ssbs_thread_switch(struct task_struct *next) 409cbdf8a18SMarc Zyngier { 410cbdf8a18SMarc Zyngier /* 411cbdf8a18SMarc Zyngier * Nothing to do for kernel threads, but 'regs' may be junk 412cbdf8a18SMarc Zyngier * (e.g. idle task) so check the flags and bail early. 413cbdf8a18SMarc Zyngier */ 414cbdf8a18SMarc Zyngier if (unlikely(next->flags & PF_KTHREAD)) 415cbdf8a18SMarc Zyngier return; 416cbdf8a18SMarc Zyngier 417fca3d33dSWill Deacon /* 418fca3d33dSWill Deacon * If all CPUs implement the SSBS extension, then we just need to 419fca3d33dSWill Deacon * context-switch the PSTATE field. 420fca3d33dSWill Deacon */ 421c2876207SWill Deacon if (cpus_have_const_cap(ARM64_SSBS)) 422fca3d33dSWill Deacon return; 423fca3d33dSWill Deacon 424c2876207SWill Deacon spectre_v4_enable_task_mitigation(next); 425cbdf8a18SMarc Zyngier } 426cbdf8a18SMarc Zyngier 427cbdf8a18SMarc Zyngier /* 428c02433ddSMark Rutland * We store our current task in sp_el0, which is clobbered by userspace. Keep a 429c02433ddSMark Rutland * shadow copy so that we can restore this upon entry from userspace. 430c02433ddSMark Rutland * 431c02433ddSMark Rutland * This is *only* for exception entry from EL0, and is not valid until we 432c02433ddSMark Rutland * __switch_to() a user task. 433c02433ddSMark Rutland */ 434c02433ddSMark Rutland DEFINE_PER_CPU(struct task_struct *, __entry_task); 435c02433ddSMark Rutland 436c02433ddSMark Rutland static void entry_task_switch(struct task_struct *next) 437c02433ddSMark Rutland { 438c02433ddSMark Rutland __this_cpu_write(__entry_task, next); 439c02433ddSMark Rutland } 440c02433ddSMark Rutland 441c02433ddSMark Rutland /* 442d49f7d73SMarc Zyngier * ARM erratum 1418040 handling, affecting the 32bit view of CNTVCT. 443d49f7d73SMarc Zyngier * Assuming the virtual counter is enabled at the beginning of times: 444d49f7d73SMarc Zyngier * 445d49f7d73SMarc Zyngier * - disable access when switching from a 64bit task to a 32bit task 446d49f7d73SMarc Zyngier * - enable access when switching from a 32bit task to a 64bit task 447d49f7d73SMarc Zyngier */ 448d49f7d73SMarc Zyngier static void erratum_1418040_thread_switch(struct task_struct *prev, 449d49f7d73SMarc Zyngier struct task_struct *next) 450d49f7d73SMarc Zyngier { 451d49f7d73SMarc Zyngier bool prev32, next32; 452d49f7d73SMarc Zyngier u64 val; 453d49f7d73SMarc Zyngier 454f969f038SWill Deacon if (!IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040)) 455d49f7d73SMarc Zyngier return; 456d49f7d73SMarc Zyngier 457d49f7d73SMarc Zyngier prev32 = is_compat_thread(task_thread_info(prev)); 458d49f7d73SMarc Zyngier next32 = is_compat_thread(task_thread_info(next)); 459d49f7d73SMarc Zyngier 460f969f038SWill Deacon if (prev32 == next32 || !this_cpu_has_cap(ARM64_WORKAROUND_1418040)) 461d49f7d73SMarc Zyngier return; 462d49f7d73SMarc Zyngier 463d49f7d73SMarc Zyngier val = read_sysreg(cntkctl_el1); 464d49f7d73SMarc Zyngier 465d49f7d73SMarc Zyngier if (!next32) 466d49f7d73SMarc Zyngier val |= ARCH_TIMER_USR_VCT_ACCESS_EN; 467d49f7d73SMarc Zyngier else 468d49f7d73SMarc Zyngier val &= ~ARCH_TIMER_USR_VCT_ACCESS_EN; 469d49f7d73SMarc Zyngier 470d49f7d73SMarc Zyngier write_sysreg(val, cntkctl_el1); 471d49f7d73SMarc Zyngier } 472d49f7d73SMarc Zyngier 473d2e0d8f9SPeter Collingbourne /* 474d2e0d8f9SPeter Collingbourne * __switch_to() checks current->thread.sctlr_user as an optimisation. Therefore 475d2e0d8f9SPeter Collingbourne * this function must be called with preemption disabled and the update to 476d2e0d8f9SPeter Collingbourne * sctlr_user must be made in the same preemption disabled block so that 477d2e0d8f9SPeter Collingbourne * __switch_to() does not see the variable update before the SCTLR_EL1 one. 478d2e0d8f9SPeter Collingbourne */ 479d2e0d8f9SPeter Collingbourne void update_sctlr_el1(u64 sctlr) 4802f79d2fcSPeter Collingbourne { 48120169862SPeter Collingbourne /* 48220169862SPeter Collingbourne * EnIA must not be cleared while in the kernel as this is necessary for 48320169862SPeter Collingbourne * in-kernel PAC. It will be cleared on kernel exit if needed. 48420169862SPeter Collingbourne */ 48520169862SPeter Collingbourne sysreg_clear_set(sctlr_el1, SCTLR_USER_MASK & ~SCTLR_ELx_ENIA, sctlr); 4862f79d2fcSPeter Collingbourne 4872f79d2fcSPeter Collingbourne /* ISB required for the kernel uaccess routines when setting TCF0. */ 4882f79d2fcSPeter Collingbourne isb(); 4892f79d2fcSPeter Collingbourne } 4902f79d2fcSPeter Collingbourne 491d49f7d73SMarc Zyngier /* 492b3901d54SCatalin Marinas * Thread switching. 493b3901d54SCatalin Marinas */ 49486bcbafcSMark Rutland __notrace_funcgraph __sched 49586bcbafcSMark Rutland struct task_struct *__switch_to(struct task_struct *prev, 496b3901d54SCatalin Marinas struct task_struct *next) 497b3901d54SCatalin Marinas { 498b3901d54SCatalin Marinas struct task_struct *last; 499b3901d54SCatalin Marinas 500b3901d54SCatalin Marinas fpsimd_thread_switch(next); 501b3901d54SCatalin Marinas tls_thread_switch(next); 502b3901d54SCatalin Marinas hw_breakpoint_thread_switch(next); 5033325732fSChristopher Covington contextidr_thread_switch(next); 504c02433ddSMark Rutland entry_task_switch(next); 505cbdf8a18SMarc Zyngier ssbs_thread_switch(next); 506d49f7d73SMarc Zyngier erratum_1418040_thread_switch(prev, next); 507b90e4839SPeter Collingbourne ptrauth_thread_switch_user(next); 508b3901d54SCatalin Marinas 5095108c67cSCatalin Marinas /* 5105108c67cSCatalin Marinas * Complete any pending TLB or cache maintenance on this CPU in case 5115108c67cSCatalin Marinas * the thread migrates to a different CPU. 51222e4ebb9SMathieu Desnoyers * This full barrier is also required by the membarrier system 51322e4ebb9SMathieu Desnoyers * call. 5145108c67cSCatalin Marinas */ 51598f7685eSWill Deacon dsb(ish); 516b3901d54SCatalin Marinas 5171c101da8SCatalin Marinas /* 5181c101da8SCatalin Marinas * MTE thread switching must happen after the DSB above to ensure that 5191c101da8SCatalin Marinas * any asynchronous tag check faults have been logged in the TFSR*_EL1 5201c101da8SCatalin Marinas * registers. 5211c101da8SCatalin Marinas */ 5221c101da8SCatalin Marinas mte_thread_switch(next); 5232f79d2fcSPeter Collingbourne /* avoid expensive SCTLR_EL1 accesses if no change */ 5242f79d2fcSPeter Collingbourne if (prev->thread.sctlr_user != next->thread.sctlr_user) 5252f79d2fcSPeter Collingbourne update_sctlr_el1(next->thread.sctlr_user); 5261c101da8SCatalin Marinas 527b3901d54SCatalin Marinas /* the actual thread switch */ 528b3901d54SCatalin Marinas last = cpu_switch_to(prev, next); 529b3901d54SCatalin Marinas 530b3901d54SCatalin Marinas return last; 531b3901d54SCatalin Marinas } 532b3901d54SCatalin Marinas 533*4f62bb7cSMadhavan T. Venkataraman struct wchan_info { 534*4f62bb7cSMadhavan T. Venkataraman unsigned long pc; 535*4f62bb7cSMadhavan T. Venkataraman int count; 536*4f62bb7cSMadhavan T. Venkataraman }; 537*4f62bb7cSMadhavan T. Venkataraman 538*4f62bb7cSMadhavan T. Venkataraman static bool get_wchan_cb(void *arg, unsigned long pc) 539*4f62bb7cSMadhavan T. Venkataraman { 540*4f62bb7cSMadhavan T. Venkataraman struct wchan_info *wchan_info = arg; 541*4f62bb7cSMadhavan T. Venkataraman 542*4f62bb7cSMadhavan T. Venkataraman if (!in_sched_functions(pc)) { 543*4f62bb7cSMadhavan T. Venkataraman wchan_info->pc = pc; 544*4f62bb7cSMadhavan T. Venkataraman return false; 545*4f62bb7cSMadhavan T. Venkataraman } 546*4f62bb7cSMadhavan T. Venkataraman return wchan_info->count++ < 16; 547*4f62bb7cSMadhavan T. Venkataraman } 548*4f62bb7cSMadhavan T. Venkataraman 54942a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p) 550b3901d54SCatalin Marinas { 551*4f62bb7cSMadhavan T. Venkataraman struct wchan_info wchan_info = { 552*4f62bb7cSMadhavan T. Venkataraman .pc = 0, 553*4f62bb7cSMadhavan T. Venkataraman .count = 0, 554*4f62bb7cSMadhavan T. Venkataraman }; 555b3901d54SCatalin Marinas 556*4f62bb7cSMadhavan T. Venkataraman if (!try_get_task_stack(p)) 5579bbd4c56SMark Rutland return 0; 5589bbd4c56SMark Rutland 559*4f62bb7cSMadhavan T. Venkataraman arch_stack_walk(get_wchan_cb, &wchan_info, p, NULL); 560f3dcbe67SDave Martin 5619bbd4c56SMark Rutland put_task_stack(p); 562*4f62bb7cSMadhavan T. Venkataraman 563*4f62bb7cSMadhavan T. Venkataraman return wchan_info.pc; 564b3901d54SCatalin Marinas } 565b3901d54SCatalin Marinas 566b3901d54SCatalin Marinas unsigned long arch_align_stack(unsigned long sp) 567b3901d54SCatalin Marinas { 568b3901d54SCatalin Marinas if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) 569b3901d54SCatalin Marinas sp -= get_random_int() & ~PAGE_MASK; 570b3901d54SCatalin Marinas return sp & ~0xf; 571b3901d54SCatalin Marinas } 572b3901d54SCatalin Marinas 57308cd8f41SWill Deacon #ifdef CONFIG_COMPAT 57408cd8f41SWill Deacon int compat_elf_check_arch(const struct elf32_hdr *hdr) 57508cd8f41SWill Deacon { 57608cd8f41SWill Deacon if (!system_supports_32bit_el0()) 57708cd8f41SWill Deacon return false; 57808cd8f41SWill Deacon 57908cd8f41SWill Deacon if ((hdr)->e_machine != EM_ARM) 58008cd8f41SWill Deacon return false; 58108cd8f41SWill Deacon 58208cd8f41SWill Deacon if (!((hdr)->e_flags & EF_ARM_EABI_MASK)) 58308cd8f41SWill Deacon return false; 58408cd8f41SWill Deacon 58508cd8f41SWill Deacon /* 58608cd8f41SWill Deacon * Prevent execve() of a 32-bit program from a deadline task 58708cd8f41SWill Deacon * if the restricted affinity mask would be inadmissible on an 58808cd8f41SWill Deacon * asymmetric system. 58908cd8f41SWill Deacon */ 59008cd8f41SWill Deacon return !static_branch_unlikely(&arm64_mismatched_32bit_el0) || 59108cd8f41SWill Deacon !dl_task_check_affinity(current, system_32bit_el0_cpumask()); 59208cd8f41SWill Deacon } 59308cd8f41SWill Deacon #endif 59408cd8f41SWill Deacon 595d1be5c99SYury Norov /* 596d1be5c99SYury Norov * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY. 597d1be5c99SYury Norov */ 598d1be5c99SYury Norov void arch_setup_new_exec(void) 599d1be5c99SYury Norov { 600873c3e89SWill Deacon unsigned long mmflags = 0; 60175031975SMark Rutland 602873c3e89SWill Deacon if (is_compat_task()) { 603873c3e89SWill Deacon mmflags = MMCF_AARCH32; 60408cd8f41SWill Deacon 60508cd8f41SWill Deacon /* 60608cd8f41SWill Deacon * Restrict the CPU affinity mask for a 32-bit task so that 60708cd8f41SWill Deacon * it contains only 32-bit-capable CPUs. 60808cd8f41SWill Deacon * 60908cd8f41SWill Deacon * From the perspective of the task, this looks similar to 61008cd8f41SWill Deacon * what would happen if the 64-bit-only CPUs were hot-unplugged 61108cd8f41SWill Deacon * at the point of execve(), although we try a bit harder to 61208cd8f41SWill Deacon * honour the cpuset hierarchy. 61308cd8f41SWill Deacon */ 614873c3e89SWill Deacon if (static_branch_unlikely(&arm64_mismatched_32bit_el0)) 61508cd8f41SWill Deacon force_compatible_cpus_allowed_ptr(current); 61608cd8f41SWill Deacon } else if (static_branch_unlikely(&arm64_mismatched_32bit_el0)) { 61708cd8f41SWill Deacon relax_compatible_cpus_allowed_ptr(current); 618873c3e89SWill Deacon } 619873c3e89SWill Deacon 620873c3e89SWill Deacon current->mm->context.flags = mmflags; 62120169862SPeter Collingbourne ptrauth_thread_init_user(); 62220169862SPeter Collingbourne mte_thread_init_user(); 623780c083aSWill Deacon 624780c083aSWill Deacon if (task_spec_ssb_noexec(current)) { 625780c083aSWill Deacon arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS, 626780c083aSWill Deacon PR_SPEC_ENABLE); 627780c083aSWill Deacon } 628d1be5c99SYury Norov } 62963f0c603SCatalin Marinas 63063f0c603SCatalin Marinas #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI 63163f0c603SCatalin Marinas /* 63263f0c603SCatalin Marinas * Control the relaxed ABI allowing tagged user addresses into the kernel. 63363f0c603SCatalin Marinas */ 634413235fcSCatalin Marinas static unsigned int tagged_addr_disabled; 63563f0c603SCatalin Marinas 63693f067f6SCatalin Marinas long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg) 63763f0c603SCatalin Marinas { 6381c101da8SCatalin Marinas unsigned long valid_mask = PR_TAGGED_ADDR_ENABLE; 63993f067f6SCatalin Marinas struct thread_info *ti = task_thread_info(task); 6401c101da8SCatalin Marinas 64193f067f6SCatalin Marinas if (is_compat_thread(ti)) 64263f0c603SCatalin Marinas return -EINVAL; 6431c101da8SCatalin Marinas 6441c101da8SCatalin Marinas if (system_supports_mte()) 645af5ce952SCatalin Marinas valid_mask |= PR_MTE_TCF_MASK | PR_MTE_TAG_MASK; 6461c101da8SCatalin Marinas 6471c101da8SCatalin Marinas if (arg & ~valid_mask) 64863f0c603SCatalin Marinas return -EINVAL; 64963f0c603SCatalin Marinas 650413235fcSCatalin Marinas /* 651413235fcSCatalin Marinas * Do not allow the enabling of the tagged address ABI if globally 652413235fcSCatalin Marinas * disabled via sysctl abi.tagged_addr_disabled. 653413235fcSCatalin Marinas */ 654413235fcSCatalin Marinas if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled) 655413235fcSCatalin Marinas return -EINVAL; 656413235fcSCatalin Marinas 65793f067f6SCatalin Marinas if (set_mte_ctrl(task, arg) != 0) 6581c101da8SCatalin Marinas return -EINVAL; 6591c101da8SCatalin Marinas 66093f067f6SCatalin Marinas update_ti_thread_flag(ti, TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE); 66163f0c603SCatalin Marinas 66263f0c603SCatalin Marinas return 0; 66363f0c603SCatalin Marinas } 66463f0c603SCatalin Marinas 66593f067f6SCatalin Marinas long get_tagged_addr_ctrl(struct task_struct *task) 66663f0c603SCatalin Marinas { 6671c101da8SCatalin Marinas long ret = 0; 66893f067f6SCatalin Marinas struct thread_info *ti = task_thread_info(task); 6691c101da8SCatalin Marinas 67093f067f6SCatalin Marinas if (is_compat_thread(ti)) 67163f0c603SCatalin Marinas return -EINVAL; 67263f0c603SCatalin Marinas 67393f067f6SCatalin Marinas if (test_ti_thread_flag(ti, TIF_TAGGED_ADDR)) 6741c101da8SCatalin Marinas ret = PR_TAGGED_ADDR_ENABLE; 67563f0c603SCatalin Marinas 67693f067f6SCatalin Marinas ret |= get_mte_ctrl(task); 6771c101da8SCatalin Marinas 6781c101da8SCatalin Marinas return ret; 67963f0c603SCatalin Marinas } 68063f0c603SCatalin Marinas 68163f0c603SCatalin Marinas /* 68263f0c603SCatalin Marinas * Global sysctl to disable the tagged user addresses support. This control 68363f0c603SCatalin Marinas * only prevents the tagged address ABI enabling via prctl() and does not 68463f0c603SCatalin Marinas * disable it for tasks that already opted in to the relaxed ABI. 68563f0c603SCatalin Marinas */ 68663f0c603SCatalin Marinas 68763f0c603SCatalin Marinas static struct ctl_table tagged_addr_sysctl_table[] = { 68863f0c603SCatalin Marinas { 689413235fcSCatalin Marinas .procname = "tagged_addr_disabled", 69063f0c603SCatalin Marinas .mode = 0644, 691413235fcSCatalin Marinas .data = &tagged_addr_disabled, 69263f0c603SCatalin Marinas .maxlen = sizeof(int), 69363f0c603SCatalin Marinas .proc_handler = proc_dointvec_minmax, 6942c614c11SMatteo Croce .extra1 = SYSCTL_ZERO, 6952c614c11SMatteo Croce .extra2 = SYSCTL_ONE, 69663f0c603SCatalin Marinas }, 69763f0c603SCatalin Marinas { } 69863f0c603SCatalin Marinas }; 69963f0c603SCatalin Marinas 70063f0c603SCatalin Marinas static int __init tagged_addr_init(void) 70163f0c603SCatalin Marinas { 70263f0c603SCatalin Marinas if (!register_sysctl("abi", tagged_addr_sysctl_table)) 70363f0c603SCatalin Marinas return -EINVAL; 70463f0c603SCatalin Marinas return 0; 70563f0c603SCatalin Marinas } 70663f0c603SCatalin Marinas 70763f0c603SCatalin Marinas core_initcall(tagged_addr_init); 70863f0c603SCatalin Marinas #endif /* CONFIG_ARM64_TAGGED_ADDR_ABI */ 70919c95f26SJulien Thierry 710ab7876a9SDave Martin #ifdef CONFIG_BINFMT_ELF 711ab7876a9SDave Martin int arch_elf_adjust_prot(int prot, const struct arch_elf_state *state, 712ab7876a9SDave Martin bool has_interp, bool is_interp) 713ab7876a9SDave Martin { 7145d1b631cSMark Brown /* 7155d1b631cSMark Brown * For dynamically linked executables the interpreter is 7165d1b631cSMark Brown * responsible for setting PROT_BTI on everything except 7175d1b631cSMark Brown * itself. 7185d1b631cSMark Brown */ 719ab7876a9SDave Martin if (is_interp != has_interp) 720ab7876a9SDave Martin return prot; 721ab7876a9SDave Martin 722ab7876a9SDave Martin if (!(state->flags & ARM64_ELF_BTI)) 723ab7876a9SDave Martin return prot; 724ab7876a9SDave Martin 725ab7876a9SDave Martin if (prot & PROT_EXEC) 726ab7876a9SDave Martin prot |= PROT_BTI; 727ab7876a9SDave Martin 728ab7876a9SDave Martin return prot; 729ab7876a9SDave Martin } 730ab7876a9SDave Martin #endif 731