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> 14b3901d54SCatalin Marinas #include <linux/export.h> 15b3901d54SCatalin Marinas #include <linux/sched.h> 16b17b0153SIngo Molnar #include <linux/sched/debug.h> 1729930025SIngo Molnar #include <linux/sched/task.h> 1868db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 19b3901d54SCatalin Marinas #include <linux/kernel.h> 2019c95f26SJulien Thierry #include <linux/lockdep.h> 21b3901d54SCatalin Marinas #include <linux/mm.h> 22b3901d54SCatalin Marinas #include <linux/stddef.h> 2363f0c603SCatalin Marinas #include <linux/sysctl.h> 24b3901d54SCatalin Marinas #include <linux/unistd.h> 25b3901d54SCatalin Marinas #include <linux/user.h> 26b3901d54SCatalin Marinas #include <linux/delay.h> 27b3901d54SCatalin Marinas #include <linux/reboot.h> 28b3901d54SCatalin Marinas #include <linux/interrupt.h> 29b3901d54SCatalin Marinas #include <linux/init.h> 30b3901d54SCatalin Marinas #include <linux/cpu.h> 31b3901d54SCatalin Marinas #include <linux/elfcore.h> 32b3901d54SCatalin Marinas #include <linux/pm.h> 33b3901d54SCatalin Marinas #include <linux/tick.h> 34b3901d54SCatalin Marinas #include <linux/utsname.h> 35b3901d54SCatalin Marinas #include <linux/uaccess.h> 36b3901d54SCatalin Marinas #include <linux/random.h> 37b3901d54SCatalin Marinas #include <linux/hw_breakpoint.h> 38b3901d54SCatalin Marinas #include <linux/personality.h> 39b3901d54SCatalin Marinas #include <linux/notifier.h> 40096b3224SJisheng Zhang #include <trace/events/power.h> 41c02433ddSMark Rutland #include <linux/percpu.h> 42bc0ee476SDave Martin #include <linux/thread_info.h> 4363f0c603SCatalin Marinas #include <linux/prctl.h> 44b3901d54SCatalin Marinas 4557f4959bSJames Morse #include <asm/alternative.h> 46a9806aa2SJulien Thierry #include <asm/arch_gicv3.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> 53b3901d54SCatalin Marinas #include <asm/processor.h> 5475031975SMark Rutland #include <asm/pointer_auth.h> 55b3901d54SCatalin Marinas #include <asm/stacktrace.h> 56b3901d54SCatalin Marinas 570a1213faSArd Biesheuvel #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) 58c0c264aeSLaura Abbott #include <linux/stackprotector.h> 59c0c264aeSLaura Abbott unsigned long __stack_chk_guard __read_mostly; 60c0c264aeSLaura Abbott EXPORT_SYMBOL(__stack_chk_guard); 61c0c264aeSLaura Abbott #endif 62c0c264aeSLaura Abbott 63b3901d54SCatalin Marinas /* 64b3901d54SCatalin Marinas * Function pointers to optional machine specific functions 65b3901d54SCatalin Marinas */ 66b3901d54SCatalin Marinas void (*pm_power_off)(void); 67b3901d54SCatalin Marinas EXPORT_SYMBOL_GPL(pm_power_off); 68b3901d54SCatalin Marinas 69b0946fc8SCatalin Marinas void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd); 70b3901d54SCatalin Marinas 71a9806aa2SJulien Thierry static void __cpu_do_idle(void) 72a9806aa2SJulien Thierry { 73a9806aa2SJulien Thierry dsb(sy); 74a9806aa2SJulien Thierry wfi(); 75a9806aa2SJulien Thierry } 76a9806aa2SJulien Thierry 77a9806aa2SJulien Thierry static void __cpu_do_idle_irqprio(void) 78a9806aa2SJulien Thierry { 79a9806aa2SJulien Thierry unsigned long pmr; 80a9806aa2SJulien Thierry unsigned long daif_bits; 81a9806aa2SJulien Thierry 82a9806aa2SJulien Thierry daif_bits = read_sysreg(daif); 83a9806aa2SJulien Thierry write_sysreg(daif_bits | PSR_I_BIT, daif); 84a9806aa2SJulien Thierry 85a9806aa2SJulien Thierry /* 86a9806aa2SJulien Thierry * Unmask PMR before going idle to make sure interrupts can 87a9806aa2SJulien Thierry * be raised. 88a9806aa2SJulien Thierry */ 89a9806aa2SJulien Thierry pmr = gic_read_pmr(); 90bd82d4bdSJulien Thierry gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); 91a9806aa2SJulien Thierry 92a9806aa2SJulien Thierry __cpu_do_idle(); 93a9806aa2SJulien Thierry 94a9806aa2SJulien Thierry gic_write_pmr(pmr); 95a9806aa2SJulien Thierry write_sysreg(daif_bits, daif); 96a9806aa2SJulien Thierry } 97a9806aa2SJulien Thierry 98a9806aa2SJulien Thierry /* 99a9806aa2SJulien Thierry * cpu_do_idle() 100a9806aa2SJulien Thierry * 101a9806aa2SJulien Thierry * Idle the processor (wait for interrupt). 102a9806aa2SJulien Thierry * 103a9806aa2SJulien Thierry * If the CPU supports priority masking we must do additional work to 104a9806aa2SJulien Thierry * ensure that interrupts are not masked at the PMR (because the core will 105a9806aa2SJulien Thierry * not wake up if we block the wake up signal in the interrupt controller). 106a9806aa2SJulien Thierry */ 107a9806aa2SJulien Thierry void cpu_do_idle(void) 108a9806aa2SJulien Thierry { 109a9806aa2SJulien Thierry if (system_uses_irq_prio_masking()) 110a9806aa2SJulien Thierry __cpu_do_idle_irqprio(); 111a9806aa2SJulien Thierry else 112a9806aa2SJulien Thierry __cpu_do_idle(); 113a9806aa2SJulien Thierry } 114a9806aa2SJulien Thierry 115b3901d54SCatalin Marinas /* 116b3901d54SCatalin Marinas * This is our default idle handler. 117b3901d54SCatalin Marinas */ 1180087298fSThomas Gleixner void arch_cpu_idle(void) 119b3901d54SCatalin Marinas { 120b3901d54SCatalin Marinas /* 121b3901d54SCatalin Marinas * This should do all the clock switching and wait for interrupt 122b3901d54SCatalin Marinas * tricks 123b3901d54SCatalin Marinas */ 124096b3224SJisheng Zhang trace_cpu_idle_rcuidle(1, smp_processor_id()); 125b3901d54SCatalin Marinas cpu_do_idle(); 126b3901d54SCatalin Marinas local_irq_enable(); 127096b3224SJisheng Zhang trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id()); 128b3901d54SCatalin Marinas } 129b3901d54SCatalin Marinas 1309327e2c6SMark Rutland #ifdef CONFIG_HOTPLUG_CPU 1319327e2c6SMark Rutland void arch_cpu_idle_dead(void) 1329327e2c6SMark Rutland { 1339327e2c6SMark Rutland cpu_die(); 1349327e2c6SMark Rutland } 1359327e2c6SMark Rutland #endif 1369327e2c6SMark Rutland 13790f51a09SArun KS /* 13890f51a09SArun KS * Called by kexec, immediately prior to machine_kexec(). 13990f51a09SArun KS * 14090f51a09SArun KS * This must completely disable all secondary CPUs; simply causing those CPUs 14190f51a09SArun KS * to execute e.g. a RAM-based pin loop is not sufficient. This allows the 14290f51a09SArun KS * kexec'd kernel to use any and all RAM as it sees fit, without having to 14390f51a09SArun KS * avoid any code or data used by any SW CPU pin loop. The CPU hotplug 14490f51a09SArun KS * functionality embodied in disable_nonboot_cpus() to achieve this. 14590f51a09SArun KS */ 146b3901d54SCatalin Marinas void machine_shutdown(void) 147b3901d54SCatalin Marinas { 14890f51a09SArun KS disable_nonboot_cpus(); 149b3901d54SCatalin Marinas } 150b3901d54SCatalin Marinas 15190f51a09SArun KS /* 15290f51a09SArun KS * Halting simply requires that the secondary CPUs stop performing any 15390f51a09SArun KS * activity (executing tasks, handling interrupts). smp_send_stop() 15490f51a09SArun KS * achieves this. 15590f51a09SArun KS */ 156b3901d54SCatalin Marinas void machine_halt(void) 157b3901d54SCatalin Marinas { 158b9acc49eSArun KS local_irq_disable(); 15990f51a09SArun KS smp_send_stop(); 160b3901d54SCatalin Marinas while (1); 161b3901d54SCatalin Marinas } 162b3901d54SCatalin Marinas 16390f51a09SArun KS /* 16490f51a09SArun KS * Power-off simply requires that the secondary CPUs stop performing any 16590f51a09SArun KS * activity (executing tasks, handling interrupts). smp_send_stop() 16690f51a09SArun KS * achieves this. When the system power is turned off, it will take all CPUs 16790f51a09SArun KS * with it. 16890f51a09SArun KS */ 169b3901d54SCatalin Marinas void machine_power_off(void) 170b3901d54SCatalin Marinas { 171b9acc49eSArun KS local_irq_disable(); 17290f51a09SArun KS smp_send_stop(); 173b3901d54SCatalin Marinas if (pm_power_off) 174b3901d54SCatalin Marinas pm_power_off(); 175b3901d54SCatalin Marinas } 176b3901d54SCatalin Marinas 17790f51a09SArun KS /* 17890f51a09SArun KS * Restart requires that the secondary CPUs stop performing any activity 17968234df4SMark Rutland * while the primary CPU resets the system. Systems with multiple CPUs must 18090f51a09SArun KS * provide a HW restart implementation, to ensure that all CPUs reset at once. 18190f51a09SArun KS * This is required so that any code running after reset on the primary CPU 18290f51a09SArun KS * doesn't have to co-ordinate with other CPUs to ensure they aren't still 18390f51a09SArun KS * executing pre-reset code, and using RAM that the primary CPU's code wishes 18490f51a09SArun KS * to use. Implementing such co-ordination would be essentially impossible. 18590f51a09SArun KS */ 186b3901d54SCatalin Marinas void machine_restart(char *cmd) 187b3901d54SCatalin Marinas { 188b3901d54SCatalin Marinas /* Disable interrupts first */ 189b3901d54SCatalin Marinas local_irq_disable(); 190b9acc49eSArun KS smp_send_stop(); 191b3901d54SCatalin Marinas 19260c0d45aSArd Biesheuvel /* 19360c0d45aSArd Biesheuvel * UpdateCapsule() depends on the system being reset via 19460c0d45aSArd Biesheuvel * ResetSystem(). 19560c0d45aSArd Biesheuvel */ 19660c0d45aSArd Biesheuvel if (efi_enabled(EFI_RUNTIME_SERVICES)) 19760c0d45aSArd Biesheuvel efi_reboot(reboot_mode, NULL); 19860c0d45aSArd Biesheuvel 199b3901d54SCatalin Marinas /* Now call the architecture specific reboot code. */ 200aa1e8ec1SCatalin Marinas if (arm_pm_restart) 201ff701306SMarc Zyngier arm_pm_restart(reboot_mode, cmd); 2021c7ffc32SGuenter Roeck else 2031c7ffc32SGuenter Roeck do_kernel_restart(cmd); 204b3901d54SCatalin Marinas 205b3901d54SCatalin Marinas /* 206b3901d54SCatalin Marinas * Whoops - the architecture was unable to reboot. 207b3901d54SCatalin Marinas */ 208b3901d54SCatalin Marinas printk("Reboot failed -- System halted\n"); 209b3901d54SCatalin Marinas while (1); 210b3901d54SCatalin Marinas } 211b3901d54SCatalin Marinas 212b7300d4cSWill Deacon static void print_pstate(struct pt_regs *regs) 213b7300d4cSWill Deacon { 214b7300d4cSWill Deacon u64 pstate = regs->pstate; 215b7300d4cSWill Deacon 216b7300d4cSWill Deacon if (compat_user_mode(regs)) { 217b7300d4cSWill Deacon printk("pstate: %08llx (%c%c%c%c %c %s %s %c%c%c)\n", 218b7300d4cSWill Deacon pstate, 219d64567f6SMark Rutland pstate & PSR_AA32_N_BIT ? 'N' : 'n', 220d64567f6SMark Rutland pstate & PSR_AA32_Z_BIT ? 'Z' : 'z', 221d64567f6SMark Rutland pstate & PSR_AA32_C_BIT ? 'C' : 'c', 222d64567f6SMark Rutland pstate & PSR_AA32_V_BIT ? 'V' : 'v', 223d64567f6SMark Rutland pstate & PSR_AA32_Q_BIT ? 'Q' : 'q', 224d64567f6SMark Rutland pstate & PSR_AA32_T_BIT ? "T32" : "A32", 225d64567f6SMark Rutland pstate & PSR_AA32_E_BIT ? "BE" : "LE", 226d64567f6SMark Rutland pstate & PSR_AA32_A_BIT ? 'A' : 'a', 227d64567f6SMark Rutland pstate & PSR_AA32_I_BIT ? 'I' : 'i', 228d64567f6SMark Rutland pstate & PSR_AA32_F_BIT ? 'F' : 'f'); 229b7300d4cSWill Deacon } else { 230b7300d4cSWill Deacon printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO)\n", 231b7300d4cSWill Deacon pstate, 232b7300d4cSWill Deacon pstate & PSR_N_BIT ? 'N' : 'n', 233b7300d4cSWill Deacon pstate & PSR_Z_BIT ? 'Z' : 'z', 234b7300d4cSWill Deacon pstate & PSR_C_BIT ? 'C' : 'c', 235b7300d4cSWill Deacon pstate & PSR_V_BIT ? 'V' : 'v', 236b7300d4cSWill Deacon pstate & PSR_D_BIT ? 'D' : 'd', 237b7300d4cSWill Deacon pstate & PSR_A_BIT ? 'A' : 'a', 238b7300d4cSWill Deacon pstate & PSR_I_BIT ? 'I' : 'i', 239b7300d4cSWill Deacon pstate & PSR_F_BIT ? 'F' : 'f', 240b7300d4cSWill Deacon pstate & PSR_PAN_BIT ? '+' : '-', 241b7300d4cSWill Deacon pstate & PSR_UAO_BIT ? '+' : '-'); 242b7300d4cSWill Deacon } 243b7300d4cSWill Deacon } 244b7300d4cSWill Deacon 245b3901d54SCatalin Marinas void __show_regs(struct pt_regs *regs) 246b3901d54SCatalin Marinas { 2476ca68e80SCatalin Marinas int i, top_reg; 2486ca68e80SCatalin Marinas u64 lr, sp; 2496ca68e80SCatalin Marinas 2506ca68e80SCatalin Marinas if (compat_user_mode(regs)) { 2516ca68e80SCatalin Marinas lr = regs->compat_lr; 2526ca68e80SCatalin Marinas sp = regs->compat_sp; 2536ca68e80SCatalin Marinas top_reg = 12; 2546ca68e80SCatalin Marinas } else { 2556ca68e80SCatalin Marinas lr = regs->regs[30]; 2566ca68e80SCatalin Marinas sp = regs->sp; 2576ca68e80SCatalin Marinas top_reg = 29; 2586ca68e80SCatalin Marinas } 259b3901d54SCatalin Marinas 260a43cb95dSTejun Heo show_regs_print_info(KERN_DEFAULT); 261b7300d4cSWill Deacon print_pstate(regs); 262a06f818aSWill Deacon 263a06f818aSWill Deacon if (!user_mode(regs)) { 2644ef79638SSergey Senozhatsky printk("pc : %pS\n", (void *)regs->pc); 2654ef79638SSergey Senozhatsky printk("lr : %pS\n", (void *)lr); 266a06f818aSWill Deacon } else { 267a06f818aSWill Deacon printk("pc : %016llx\n", regs->pc); 268a06f818aSWill Deacon printk("lr : %016llx\n", lr); 269a06f818aSWill Deacon } 270a06f818aSWill Deacon 271b7300d4cSWill Deacon printk("sp : %016llx\n", sp); 272db4b0710SMark Rutland 273133d0518SJulien Thierry if (system_uses_irq_prio_masking()) 274133d0518SJulien Thierry printk("pmr_save: %08llx\n", regs->pmr_save); 275133d0518SJulien Thierry 276db4b0710SMark Rutland i = top_reg; 277db4b0710SMark Rutland 278db4b0710SMark Rutland while (i >= 0) { 279b3901d54SCatalin Marinas printk("x%-2d: %016llx ", i, regs->regs[i]); 280db4b0710SMark Rutland i--; 281db4b0710SMark Rutland 282db4b0710SMark Rutland if (i % 2 == 0) { 283db4b0710SMark Rutland pr_cont("x%-2d: %016llx ", i, regs->regs[i]); 284db4b0710SMark Rutland i--; 285db4b0710SMark Rutland } 286db4b0710SMark Rutland 287db4b0710SMark Rutland pr_cont("\n"); 288b3901d54SCatalin Marinas } 289b3901d54SCatalin Marinas } 290b3901d54SCatalin Marinas 291b3901d54SCatalin Marinas void show_regs(struct pt_regs * regs) 292b3901d54SCatalin Marinas { 293b3901d54SCatalin Marinas __show_regs(regs); 2941149aad1SKefeng Wang dump_backtrace(regs, NULL); 295b3901d54SCatalin Marinas } 296b3901d54SCatalin Marinas 297eb35bdd7SWill Deacon static void tls_thread_flush(void) 298eb35bdd7SWill Deacon { 299adf75899SMark Rutland write_sysreg(0, tpidr_el0); 300eb35bdd7SWill Deacon 301eb35bdd7SWill Deacon if (is_compat_task()) { 30265896545SDave Martin current->thread.uw.tp_value = 0; 303eb35bdd7SWill Deacon 304eb35bdd7SWill Deacon /* 305eb35bdd7SWill Deacon * We need to ensure ordering between the shadow state and the 306eb35bdd7SWill Deacon * hardware state, so that we don't corrupt the hardware state 307eb35bdd7SWill Deacon * with a stale shadow state during context switch. 308eb35bdd7SWill Deacon */ 309eb35bdd7SWill Deacon barrier(); 310adf75899SMark Rutland write_sysreg(0, tpidrro_el0); 311eb35bdd7SWill Deacon } 312eb35bdd7SWill Deacon } 313eb35bdd7SWill Deacon 31463f0c603SCatalin Marinas static void flush_tagged_addr_state(void) 31563f0c603SCatalin Marinas { 31663f0c603SCatalin Marinas if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI)) 31763f0c603SCatalin Marinas clear_thread_flag(TIF_TAGGED_ADDR); 31863f0c603SCatalin Marinas } 31963f0c603SCatalin Marinas 320b3901d54SCatalin Marinas void flush_thread(void) 321b3901d54SCatalin Marinas { 322b3901d54SCatalin Marinas fpsimd_flush_thread(); 323eb35bdd7SWill Deacon tls_thread_flush(); 324b3901d54SCatalin Marinas flush_ptrace_hw_breakpoint(current); 32563f0c603SCatalin Marinas flush_tagged_addr_state(); 326b3901d54SCatalin Marinas } 327b3901d54SCatalin Marinas 328b3901d54SCatalin Marinas void release_thread(struct task_struct *dead_task) 329b3901d54SCatalin Marinas { 330b3901d54SCatalin Marinas } 331b3901d54SCatalin Marinas 332bc0ee476SDave Martin void arch_release_task_struct(struct task_struct *tsk) 333bc0ee476SDave Martin { 334bc0ee476SDave Martin fpsimd_release_task(tsk); 335bc0ee476SDave Martin } 336bc0ee476SDave Martin 337b3901d54SCatalin Marinas int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) 338b3901d54SCatalin Marinas { 3396eb6c801SJanet Liu if (current->mm) 340c51f9269SArd Biesheuvel fpsimd_preserve_current_state(); 341b3901d54SCatalin Marinas *dst = *src; 342bc0ee476SDave Martin 3434585fc59SMasayoshi Mizuma /* We rely on the above assignment to initialize dst's thread_flags: */ 3444585fc59SMasayoshi Mizuma BUILD_BUG_ON(!IS_ENABLED(CONFIG_THREAD_INFO_IN_TASK)); 3454585fc59SMasayoshi Mizuma 3464585fc59SMasayoshi Mizuma /* 3474585fc59SMasayoshi Mizuma * Detach src's sve_state (if any) from dst so that it does not 3484585fc59SMasayoshi Mizuma * get erroneously used or freed prematurely. dst's sve_state 3494585fc59SMasayoshi Mizuma * will be allocated on demand later on if dst uses SVE. 3504585fc59SMasayoshi Mizuma * For consistency, also clear TIF_SVE here: this could be done 3514585fc59SMasayoshi Mizuma * later in copy_process(), but to avoid tripping up future 3524585fc59SMasayoshi Mizuma * maintainers it is best not to leave TIF_SVE and sve_state in 3534585fc59SMasayoshi Mizuma * an inconsistent state, even temporarily. 3544585fc59SMasayoshi Mizuma */ 3554585fc59SMasayoshi Mizuma dst->thread.sve_state = NULL; 3564585fc59SMasayoshi Mizuma clear_tsk_thread_flag(dst, TIF_SVE); 3574585fc59SMasayoshi Mizuma 358b3901d54SCatalin Marinas return 0; 359b3901d54SCatalin Marinas } 360b3901d54SCatalin Marinas 361b3901d54SCatalin Marinas asmlinkage void ret_from_fork(void) asm("ret_from_fork"); 362b3901d54SCatalin Marinas 363b3901d54SCatalin Marinas int copy_thread(unsigned long clone_flags, unsigned long stack_start, 364afa86fc4SAl Viro unsigned long stk_sz, struct task_struct *p) 365b3901d54SCatalin Marinas { 366b3901d54SCatalin Marinas struct pt_regs *childregs = task_pt_regs(p); 367b3901d54SCatalin Marinas 368c34501d2SCatalin Marinas memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context)); 369c34501d2SCatalin Marinas 370bc0ee476SDave Martin /* 371071b6d4aSDave Martin * In case p was allocated the same task_struct pointer as some 372071b6d4aSDave Martin * other recently-exited task, make sure p is disassociated from 373071b6d4aSDave Martin * any cpu that may have run that now-exited task recently. 374071b6d4aSDave Martin * Otherwise we could erroneously skip reloading the FPSIMD 375071b6d4aSDave Martin * registers for p. 376071b6d4aSDave Martin */ 377071b6d4aSDave Martin fpsimd_flush_task_state(p); 378071b6d4aSDave Martin 3799ac08002SAl Viro if (likely(!(p->flags & PF_KTHREAD))) { 3809ac08002SAl Viro *childregs = *current_pt_regs(); 381b3901d54SCatalin Marinas childregs->regs[0] = 0; 382d00a3810SWill Deacon 383b3901d54SCatalin Marinas /* 384b3901d54SCatalin Marinas * Read the current TLS pointer from tpidr_el0 as it may be 385b3901d54SCatalin Marinas * out-of-sync with the saved value. 386b3901d54SCatalin Marinas */ 387adf75899SMark Rutland *task_user_tls(p) = read_sysreg(tpidr_el0); 388d00a3810SWill Deacon 389e0fd18ceSAl Viro if (stack_start) { 390d00a3810SWill Deacon if (is_compat_thread(task_thread_info(p))) 391d00a3810SWill Deacon childregs->compat_sp = stack_start; 392d00a3810SWill Deacon else 393b3901d54SCatalin Marinas childregs->sp = stack_start; 394b3901d54SCatalin Marinas } 395d00a3810SWill Deacon 396c34501d2SCatalin Marinas /* 397c34501d2SCatalin Marinas * If a TLS pointer was passed to clone (4th argument), use it 398c34501d2SCatalin Marinas * for the new thread. 399c34501d2SCatalin Marinas */ 400b3901d54SCatalin Marinas if (clone_flags & CLONE_SETTLS) 40165896545SDave Martin p->thread.uw.tp_value = childregs->regs[3]; 402c34501d2SCatalin Marinas } else { 403c34501d2SCatalin Marinas memset(childregs, 0, sizeof(struct pt_regs)); 404c34501d2SCatalin Marinas childregs->pstate = PSR_MODE_EL1h; 40557f4959bSJames Morse if (IS_ENABLED(CONFIG_ARM64_UAO) && 406a4023f68SSuzuki K Poulose cpus_have_const_cap(ARM64_HAS_UAO)) 40757f4959bSJames Morse childregs->pstate |= PSR_UAO_BIT; 4088f04e8e6SWill Deacon 4098f04e8e6SWill Deacon if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) 410cbdf8a18SMarc Zyngier set_ssbs_bit(childregs); 4118f04e8e6SWill Deacon 412133d0518SJulien Thierry if (system_uses_irq_prio_masking()) 413133d0518SJulien Thierry childregs->pmr_save = GIC_PRIO_IRQON; 414133d0518SJulien Thierry 415c34501d2SCatalin Marinas p->thread.cpu_context.x19 = stack_start; 416c34501d2SCatalin Marinas p->thread.cpu_context.x20 = stk_sz; 417c34501d2SCatalin Marinas } 418c34501d2SCatalin Marinas p->thread.cpu_context.pc = (unsigned long)ret_from_fork; 419c34501d2SCatalin Marinas p->thread.cpu_context.sp = (unsigned long)childregs; 420b3901d54SCatalin Marinas 421b3901d54SCatalin Marinas ptrace_hw_copy_thread(p); 422b3901d54SCatalin Marinas 423b3901d54SCatalin Marinas return 0; 424b3901d54SCatalin Marinas } 425b3901d54SCatalin Marinas 426936eb65cSDave Martin void tls_preserve_current_state(void) 427936eb65cSDave Martin { 428936eb65cSDave Martin *task_user_tls(current) = read_sysreg(tpidr_el0); 429936eb65cSDave Martin } 430936eb65cSDave Martin 431b3901d54SCatalin Marinas static void tls_thread_switch(struct task_struct *next) 432b3901d54SCatalin Marinas { 433936eb65cSDave Martin tls_preserve_current_state(); 434b3901d54SCatalin Marinas 43518011eacSWill Deacon if (is_compat_thread(task_thread_info(next))) 43665896545SDave Martin write_sysreg(next->thread.uw.tp_value, tpidrro_el0); 43718011eacSWill Deacon else if (!arm64_kernel_unmapped_at_el0()) 43818011eacSWill Deacon write_sysreg(0, tpidrro_el0); 439b3901d54SCatalin Marinas 44018011eacSWill Deacon write_sysreg(*task_user_tls(next), tpidr_el0); 441b3901d54SCatalin Marinas } 442b3901d54SCatalin Marinas 44357f4959bSJames Morse /* Restore the UAO state depending on next's addr_limit */ 444d0854412SJames Morse void uao_thread_switch(struct task_struct *next) 44557f4959bSJames Morse { 446e950631eSCatalin Marinas if (IS_ENABLED(CONFIG_ARM64_UAO)) { 447e950631eSCatalin Marinas if (task_thread_info(next)->addr_limit == KERNEL_DS) 448e950631eSCatalin Marinas asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO)); 44957f4959bSJames Morse else 450e950631eSCatalin Marinas asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO)); 451e950631eSCatalin Marinas } 45257f4959bSJames Morse } 45357f4959bSJames Morse 454b3901d54SCatalin Marinas /* 455cbdf8a18SMarc Zyngier * Force SSBS state on context-switch, since it may be lost after migrating 456cbdf8a18SMarc Zyngier * from a CPU which treats the bit as RES0 in a heterogeneous system. 457cbdf8a18SMarc Zyngier */ 458cbdf8a18SMarc Zyngier static void ssbs_thread_switch(struct task_struct *next) 459cbdf8a18SMarc Zyngier { 460cbdf8a18SMarc Zyngier struct pt_regs *regs = task_pt_regs(next); 461cbdf8a18SMarc Zyngier 462cbdf8a18SMarc Zyngier /* 463cbdf8a18SMarc Zyngier * Nothing to do for kernel threads, but 'regs' may be junk 464cbdf8a18SMarc Zyngier * (e.g. idle task) so check the flags and bail early. 465cbdf8a18SMarc Zyngier */ 466cbdf8a18SMarc Zyngier if (unlikely(next->flags & PF_KTHREAD)) 467cbdf8a18SMarc Zyngier return; 468cbdf8a18SMarc Zyngier 469cbdf8a18SMarc Zyngier /* If the mitigation is enabled, then we leave SSBS clear. */ 470cbdf8a18SMarc Zyngier if ((arm64_get_ssbd_state() == ARM64_SSBD_FORCE_ENABLE) || 471cbdf8a18SMarc Zyngier test_tsk_thread_flag(next, TIF_SSBD)) 472cbdf8a18SMarc Zyngier return; 473cbdf8a18SMarc Zyngier 474cbdf8a18SMarc Zyngier if (compat_user_mode(regs)) 475cbdf8a18SMarc Zyngier set_compat_ssbs_bit(regs); 476cbdf8a18SMarc Zyngier else if (user_mode(regs)) 477cbdf8a18SMarc Zyngier set_ssbs_bit(regs); 478cbdf8a18SMarc Zyngier } 479cbdf8a18SMarc Zyngier 480cbdf8a18SMarc Zyngier /* 481c02433ddSMark Rutland * We store our current task in sp_el0, which is clobbered by userspace. Keep a 482c02433ddSMark Rutland * shadow copy so that we can restore this upon entry from userspace. 483c02433ddSMark Rutland * 484c02433ddSMark Rutland * This is *only* for exception entry from EL0, and is not valid until we 485c02433ddSMark Rutland * __switch_to() a user task. 486c02433ddSMark Rutland */ 487c02433ddSMark Rutland DEFINE_PER_CPU(struct task_struct *, __entry_task); 488c02433ddSMark Rutland 489c02433ddSMark Rutland static void entry_task_switch(struct task_struct *next) 490c02433ddSMark Rutland { 491c02433ddSMark Rutland __this_cpu_write(__entry_task, next); 492c02433ddSMark Rutland } 493c02433ddSMark Rutland 494c02433ddSMark Rutland /* 495b3901d54SCatalin Marinas * Thread switching. 496b3901d54SCatalin Marinas */ 4978f4b326dSJoel Fernandes __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev, 498b3901d54SCatalin Marinas struct task_struct *next) 499b3901d54SCatalin Marinas { 500b3901d54SCatalin Marinas struct task_struct *last; 501b3901d54SCatalin Marinas 502b3901d54SCatalin Marinas fpsimd_thread_switch(next); 503b3901d54SCatalin Marinas tls_thread_switch(next); 504b3901d54SCatalin Marinas hw_breakpoint_thread_switch(next); 5053325732fSChristopher Covington contextidr_thread_switch(next); 506c02433ddSMark Rutland entry_task_switch(next); 50757f4959bSJames Morse uao_thread_switch(next); 50875031975SMark Rutland ptrauth_thread_switch(next); 509cbdf8a18SMarc Zyngier ssbs_thread_switch(next); 510b3901d54SCatalin Marinas 5115108c67cSCatalin Marinas /* 5125108c67cSCatalin Marinas * Complete any pending TLB or cache maintenance on this CPU in case 5135108c67cSCatalin Marinas * the thread migrates to a different CPU. 51422e4ebb9SMathieu Desnoyers * This full barrier is also required by the membarrier system 51522e4ebb9SMathieu Desnoyers * call. 5165108c67cSCatalin Marinas */ 51798f7685eSWill Deacon dsb(ish); 518b3901d54SCatalin Marinas 519b3901d54SCatalin Marinas /* the actual thread switch */ 520b3901d54SCatalin Marinas last = cpu_switch_to(prev, next); 521b3901d54SCatalin Marinas 522b3901d54SCatalin Marinas return last; 523b3901d54SCatalin Marinas } 524b3901d54SCatalin Marinas 525b3901d54SCatalin Marinas unsigned long get_wchan(struct task_struct *p) 526b3901d54SCatalin Marinas { 527b3901d54SCatalin Marinas struct stackframe frame; 5289bbd4c56SMark Rutland unsigned long stack_page, ret = 0; 529b3901d54SCatalin Marinas int count = 0; 530b3901d54SCatalin Marinas if (!p || p == current || p->state == TASK_RUNNING) 531b3901d54SCatalin Marinas return 0; 532b3901d54SCatalin Marinas 5339bbd4c56SMark Rutland stack_page = (unsigned long)try_get_task_stack(p); 5349bbd4c56SMark Rutland if (!stack_page) 5359bbd4c56SMark Rutland return 0; 5369bbd4c56SMark Rutland 537f3dcbe67SDave Martin start_backtrace(&frame, thread_saved_fp(p), thread_saved_pc(p)); 538f3dcbe67SDave Martin 539b3901d54SCatalin Marinas do { 54031e43ad3SArd Biesheuvel if (unwind_frame(p, &frame)) 5419bbd4c56SMark Rutland goto out; 5429bbd4c56SMark Rutland if (!in_sched_functions(frame.pc)) { 5439bbd4c56SMark Rutland ret = frame.pc; 5449bbd4c56SMark Rutland goto out; 5459bbd4c56SMark Rutland } 546b3901d54SCatalin Marinas } while (count ++ < 16); 5479bbd4c56SMark Rutland 5489bbd4c56SMark Rutland out: 5499bbd4c56SMark Rutland put_task_stack(p); 5509bbd4c56SMark Rutland return ret; 551b3901d54SCatalin Marinas } 552b3901d54SCatalin Marinas 553b3901d54SCatalin Marinas unsigned long arch_align_stack(unsigned long sp) 554b3901d54SCatalin Marinas { 555b3901d54SCatalin Marinas if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) 556b3901d54SCatalin Marinas sp -= get_random_int() & ~PAGE_MASK; 557b3901d54SCatalin Marinas return sp & ~0xf; 558b3901d54SCatalin Marinas } 559b3901d54SCatalin Marinas 560d1be5c99SYury Norov /* 561d1be5c99SYury Norov * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY. 562d1be5c99SYury Norov */ 563d1be5c99SYury Norov void arch_setup_new_exec(void) 564d1be5c99SYury Norov { 565d1be5c99SYury Norov current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0; 56675031975SMark Rutland 56775031975SMark Rutland ptrauth_thread_init_user(current); 568d1be5c99SYury Norov } 56963f0c603SCatalin Marinas 57063f0c603SCatalin Marinas #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI 57163f0c603SCatalin Marinas /* 57263f0c603SCatalin Marinas * Control the relaxed ABI allowing tagged user addresses into the kernel. 57363f0c603SCatalin Marinas */ 574413235fcSCatalin Marinas static unsigned int tagged_addr_disabled; 57563f0c603SCatalin Marinas 57663f0c603SCatalin Marinas long set_tagged_addr_ctrl(unsigned long arg) 57763f0c603SCatalin Marinas { 57863f0c603SCatalin Marinas if (is_compat_task()) 57963f0c603SCatalin Marinas return -EINVAL; 58063f0c603SCatalin Marinas if (arg & ~PR_TAGGED_ADDR_ENABLE) 58163f0c603SCatalin Marinas return -EINVAL; 58263f0c603SCatalin Marinas 583413235fcSCatalin Marinas /* 584413235fcSCatalin Marinas * Do not allow the enabling of the tagged address ABI if globally 585413235fcSCatalin Marinas * disabled via sysctl abi.tagged_addr_disabled. 586413235fcSCatalin Marinas */ 587413235fcSCatalin Marinas if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled) 588413235fcSCatalin Marinas return -EINVAL; 589413235fcSCatalin Marinas 59063f0c603SCatalin Marinas update_thread_flag(TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE); 59163f0c603SCatalin Marinas 59263f0c603SCatalin Marinas return 0; 59363f0c603SCatalin Marinas } 59463f0c603SCatalin Marinas 59563f0c603SCatalin Marinas long get_tagged_addr_ctrl(void) 59663f0c603SCatalin Marinas { 59763f0c603SCatalin Marinas if (is_compat_task()) 59863f0c603SCatalin Marinas return -EINVAL; 59963f0c603SCatalin Marinas 60063f0c603SCatalin Marinas if (test_thread_flag(TIF_TAGGED_ADDR)) 60163f0c603SCatalin Marinas return PR_TAGGED_ADDR_ENABLE; 60263f0c603SCatalin Marinas 60363f0c603SCatalin Marinas return 0; 60463f0c603SCatalin Marinas } 60563f0c603SCatalin Marinas 60663f0c603SCatalin Marinas /* 60763f0c603SCatalin Marinas * Global sysctl to disable the tagged user addresses support. This control 60863f0c603SCatalin Marinas * only prevents the tagged address ABI enabling via prctl() and does not 60963f0c603SCatalin Marinas * disable it for tasks that already opted in to the relaxed ABI. 61063f0c603SCatalin Marinas */ 61163f0c603SCatalin Marinas static int zero; 61263f0c603SCatalin Marinas static int one = 1; 61363f0c603SCatalin Marinas 61463f0c603SCatalin Marinas static struct ctl_table tagged_addr_sysctl_table[] = { 61563f0c603SCatalin Marinas { 616413235fcSCatalin Marinas .procname = "tagged_addr_disabled", 61763f0c603SCatalin Marinas .mode = 0644, 618413235fcSCatalin Marinas .data = &tagged_addr_disabled, 61963f0c603SCatalin Marinas .maxlen = sizeof(int), 62063f0c603SCatalin Marinas .proc_handler = proc_dointvec_minmax, 62163f0c603SCatalin Marinas .extra1 = &zero, 62263f0c603SCatalin Marinas .extra2 = &one, 62363f0c603SCatalin Marinas }, 62463f0c603SCatalin Marinas { } 62563f0c603SCatalin Marinas }; 62663f0c603SCatalin Marinas 62763f0c603SCatalin Marinas static int __init tagged_addr_init(void) 62863f0c603SCatalin Marinas { 62963f0c603SCatalin Marinas if (!register_sysctl("abi", tagged_addr_sysctl_table)) 63063f0c603SCatalin Marinas return -EINVAL; 63163f0c603SCatalin Marinas return 0; 63263f0c603SCatalin Marinas } 63363f0c603SCatalin Marinas 63463f0c603SCatalin Marinas core_initcall(tagged_addr_init); 63563f0c603SCatalin Marinas #endif /* CONFIG_ARM64_TAGGED_ADDR_ABI */ 63619c95f26SJulien Thierry 63719c95f26SJulien Thierry asmlinkage void __sched arm64_preempt_schedule_irq(void) 63819c95f26SJulien Thierry { 63919c95f26SJulien Thierry lockdep_assert_irqs_disabled(); 64019c95f26SJulien Thierry 64119c95f26SJulien Thierry /* 64219c95f26SJulien Thierry * Preempting a task from an IRQ means we leave copies of PSTATE 64319c95f26SJulien Thierry * on the stack. cpufeature's enable calls may modify PSTATE, but 64419c95f26SJulien Thierry * resuming one of these preempted tasks would undo those changes. 64519c95f26SJulien Thierry * 64619c95f26SJulien Thierry * Only allow a task to be preempted once cpufeatures have been 64719c95f26SJulien Thierry * enabled. 64819c95f26SJulien Thierry */ 649*b51c6ac2SSuzuki K Poulose if (system_capabilities_finalized()) 65019c95f26SJulien Thierry preempt_schedule_irq(); 65119c95f26SJulien Thierry } 652