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); 265*cdcb61aeSAmit Daniel Kachhap printk("lr : %pS\n", (void *)ptrauth_strip_insn_pac(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 363a4376f2fSAmanieu d'Antras int copy_thread_tls(unsigned long clone_flags, unsigned long stack_start, 364a4376f2fSAmanieu d'Antras unsigned long stk_sz, struct task_struct *p, unsigned long tls) 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 37933e45234SKristina Martsenko ptrauth_thread_init_kernel(p); 38033e45234SKristina Martsenko 3819ac08002SAl Viro if (likely(!(p->flags & PF_KTHREAD))) { 3829ac08002SAl Viro *childregs = *current_pt_regs(); 383b3901d54SCatalin Marinas childregs->regs[0] = 0; 384d00a3810SWill Deacon 385b3901d54SCatalin Marinas /* 386b3901d54SCatalin Marinas * Read the current TLS pointer from tpidr_el0 as it may be 387b3901d54SCatalin Marinas * out-of-sync with the saved value. 388b3901d54SCatalin Marinas */ 389adf75899SMark Rutland *task_user_tls(p) = read_sysreg(tpidr_el0); 390d00a3810SWill Deacon 391e0fd18ceSAl Viro if (stack_start) { 392d00a3810SWill Deacon if (is_compat_thread(task_thread_info(p))) 393d00a3810SWill Deacon childregs->compat_sp = stack_start; 394d00a3810SWill Deacon else 395b3901d54SCatalin Marinas childregs->sp = stack_start; 396b3901d54SCatalin Marinas } 397d00a3810SWill Deacon 398c34501d2SCatalin Marinas /* 399a4376f2fSAmanieu d'Antras * If a TLS pointer was passed to clone, use it for the new 400a4376f2fSAmanieu d'Antras * thread. 401c34501d2SCatalin Marinas */ 402b3901d54SCatalin Marinas if (clone_flags & CLONE_SETTLS) 403a4376f2fSAmanieu d'Antras p->thread.uw.tp_value = tls; 404c34501d2SCatalin Marinas } else { 405c34501d2SCatalin Marinas memset(childregs, 0, sizeof(struct pt_regs)); 406c34501d2SCatalin Marinas childregs->pstate = PSR_MODE_EL1h; 40757f4959bSJames Morse if (IS_ENABLED(CONFIG_ARM64_UAO) && 408a4023f68SSuzuki K Poulose cpus_have_const_cap(ARM64_HAS_UAO)) 40957f4959bSJames Morse childregs->pstate |= PSR_UAO_BIT; 4108f04e8e6SWill Deacon 4118f04e8e6SWill Deacon if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) 412cbdf8a18SMarc Zyngier set_ssbs_bit(childregs); 4138f04e8e6SWill Deacon 414133d0518SJulien Thierry if (system_uses_irq_prio_masking()) 415133d0518SJulien Thierry childregs->pmr_save = GIC_PRIO_IRQON; 416133d0518SJulien Thierry 417c34501d2SCatalin Marinas p->thread.cpu_context.x19 = stack_start; 418c34501d2SCatalin Marinas p->thread.cpu_context.x20 = stk_sz; 419c34501d2SCatalin Marinas } 420c34501d2SCatalin Marinas p->thread.cpu_context.pc = (unsigned long)ret_from_fork; 421c34501d2SCatalin Marinas p->thread.cpu_context.sp = (unsigned long)childregs; 422b3901d54SCatalin Marinas 423b3901d54SCatalin Marinas ptrace_hw_copy_thread(p); 424b3901d54SCatalin Marinas 425b3901d54SCatalin Marinas return 0; 426b3901d54SCatalin Marinas } 427b3901d54SCatalin Marinas 428936eb65cSDave Martin void tls_preserve_current_state(void) 429936eb65cSDave Martin { 430936eb65cSDave Martin *task_user_tls(current) = read_sysreg(tpidr_el0); 431936eb65cSDave Martin } 432936eb65cSDave Martin 433b3901d54SCatalin Marinas static void tls_thread_switch(struct task_struct *next) 434b3901d54SCatalin Marinas { 435936eb65cSDave Martin tls_preserve_current_state(); 436b3901d54SCatalin Marinas 43718011eacSWill Deacon if (is_compat_thread(task_thread_info(next))) 43865896545SDave Martin write_sysreg(next->thread.uw.tp_value, tpidrro_el0); 43918011eacSWill Deacon else if (!arm64_kernel_unmapped_at_el0()) 44018011eacSWill Deacon write_sysreg(0, tpidrro_el0); 441b3901d54SCatalin Marinas 44218011eacSWill Deacon write_sysreg(*task_user_tls(next), tpidr_el0); 443b3901d54SCatalin Marinas } 444b3901d54SCatalin Marinas 44557f4959bSJames Morse /* Restore the UAO state depending on next's addr_limit */ 446d0854412SJames Morse void uao_thread_switch(struct task_struct *next) 44757f4959bSJames Morse { 448e950631eSCatalin Marinas if (IS_ENABLED(CONFIG_ARM64_UAO)) { 449e950631eSCatalin Marinas if (task_thread_info(next)->addr_limit == KERNEL_DS) 450e950631eSCatalin Marinas asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO)); 45157f4959bSJames Morse else 452e950631eSCatalin Marinas asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO)); 453e950631eSCatalin Marinas } 45457f4959bSJames Morse } 45557f4959bSJames Morse 456b3901d54SCatalin Marinas /* 457cbdf8a18SMarc Zyngier * Force SSBS state on context-switch, since it may be lost after migrating 458cbdf8a18SMarc Zyngier * from a CPU which treats the bit as RES0 in a heterogeneous system. 459cbdf8a18SMarc Zyngier */ 460cbdf8a18SMarc Zyngier static void ssbs_thread_switch(struct task_struct *next) 461cbdf8a18SMarc Zyngier { 462cbdf8a18SMarc Zyngier struct pt_regs *regs = task_pt_regs(next); 463cbdf8a18SMarc Zyngier 464cbdf8a18SMarc Zyngier /* 465cbdf8a18SMarc Zyngier * Nothing to do for kernel threads, but 'regs' may be junk 466cbdf8a18SMarc Zyngier * (e.g. idle task) so check the flags and bail early. 467cbdf8a18SMarc Zyngier */ 468cbdf8a18SMarc Zyngier if (unlikely(next->flags & PF_KTHREAD)) 469cbdf8a18SMarc Zyngier return; 470cbdf8a18SMarc Zyngier 471fca3d33dSWill Deacon /* 472fca3d33dSWill Deacon * If all CPUs implement the SSBS extension, then we just need to 473fca3d33dSWill Deacon * context-switch the PSTATE field. 474fca3d33dSWill Deacon */ 475fca3d33dSWill Deacon if (cpu_have_feature(cpu_feature(SSBS))) 476fca3d33dSWill Deacon return; 477fca3d33dSWill Deacon 478cbdf8a18SMarc Zyngier /* If the mitigation is enabled, then we leave SSBS clear. */ 479cbdf8a18SMarc Zyngier if ((arm64_get_ssbd_state() == ARM64_SSBD_FORCE_ENABLE) || 480cbdf8a18SMarc Zyngier test_tsk_thread_flag(next, TIF_SSBD)) 481cbdf8a18SMarc Zyngier return; 482cbdf8a18SMarc Zyngier 483cbdf8a18SMarc Zyngier if (compat_user_mode(regs)) 484cbdf8a18SMarc Zyngier set_compat_ssbs_bit(regs); 485cbdf8a18SMarc Zyngier else if (user_mode(regs)) 486cbdf8a18SMarc Zyngier set_ssbs_bit(regs); 487cbdf8a18SMarc Zyngier } 488cbdf8a18SMarc Zyngier 489cbdf8a18SMarc Zyngier /* 490c02433ddSMark Rutland * We store our current task in sp_el0, which is clobbered by userspace. Keep a 491c02433ddSMark Rutland * shadow copy so that we can restore this upon entry from userspace. 492c02433ddSMark Rutland * 493c02433ddSMark Rutland * This is *only* for exception entry from EL0, and is not valid until we 494c02433ddSMark Rutland * __switch_to() a user task. 495c02433ddSMark Rutland */ 496c02433ddSMark Rutland DEFINE_PER_CPU(struct task_struct *, __entry_task); 497c02433ddSMark Rutland 498c02433ddSMark Rutland static void entry_task_switch(struct task_struct *next) 499c02433ddSMark Rutland { 500c02433ddSMark Rutland __this_cpu_write(__entry_task, next); 501c02433ddSMark Rutland } 502c02433ddSMark Rutland 503c02433ddSMark Rutland /* 504b3901d54SCatalin Marinas * Thread switching. 505b3901d54SCatalin Marinas */ 5068f4b326dSJoel Fernandes __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev, 507b3901d54SCatalin Marinas struct task_struct *next) 508b3901d54SCatalin Marinas { 509b3901d54SCatalin Marinas struct task_struct *last; 510b3901d54SCatalin Marinas 511b3901d54SCatalin Marinas fpsimd_thread_switch(next); 512b3901d54SCatalin Marinas tls_thread_switch(next); 513b3901d54SCatalin Marinas hw_breakpoint_thread_switch(next); 5143325732fSChristopher Covington contextidr_thread_switch(next); 515c02433ddSMark Rutland entry_task_switch(next); 51657f4959bSJames Morse uao_thread_switch(next); 517cbdf8a18SMarc Zyngier ssbs_thread_switch(next); 518b3901d54SCatalin Marinas 5195108c67cSCatalin Marinas /* 5205108c67cSCatalin Marinas * Complete any pending TLB or cache maintenance on this CPU in case 5215108c67cSCatalin Marinas * the thread migrates to a different CPU. 52222e4ebb9SMathieu Desnoyers * This full barrier is also required by the membarrier system 52322e4ebb9SMathieu Desnoyers * call. 5245108c67cSCatalin Marinas */ 52598f7685eSWill Deacon dsb(ish); 526b3901d54SCatalin 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 533b3901d54SCatalin Marinas unsigned long get_wchan(struct task_struct *p) 534b3901d54SCatalin Marinas { 535b3901d54SCatalin Marinas struct stackframe frame; 5369bbd4c56SMark Rutland unsigned long stack_page, ret = 0; 537b3901d54SCatalin Marinas int count = 0; 538b3901d54SCatalin Marinas if (!p || p == current || p->state == TASK_RUNNING) 539b3901d54SCatalin Marinas return 0; 540b3901d54SCatalin Marinas 5419bbd4c56SMark Rutland stack_page = (unsigned long)try_get_task_stack(p); 5429bbd4c56SMark Rutland if (!stack_page) 5439bbd4c56SMark Rutland return 0; 5449bbd4c56SMark Rutland 545f3dcbe67SDave Martin start_backtrace(&frame, thread_saved_fp(p), thread_saved_pc(p)); 546f3dcbe67SDave Martin 547b3901d54SCatalin Marinas do { 54831e43ad3SArd Biesheuvel if (unwind_frame(p, &frame)) 5499bbd4c56SMark Rutland goto out; 5509bbd4c56SMark Rutland if (!in_sched_functions(frame.pc)) { 5519bbd4c56SMark Rutland ret = frame.pc; 5529bbd4c56SMark Rutland goto out; 5539bbd4c56SMark Rutland } 554b3901d54SCatalin Marinas } while (count ++ < 16); 5559bbd4c56SMark Rutland 5569bbd4c56SMark Rutland out: 5579bbd4c56SMark Rutland put_task_stack(p); 5589bbd4c56SMark Rutland return ret; 559b3901d54SCatalin Marinas } 560b3901d54SCatalin Marinas 561b3901d54SCatalin Marinas unsigned long arch_align_stack(unsigned long sp) 562b3901d54SCatalin Marinas { 563b3901d54SCatalin Marinas if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) 564b3901d54SCatalin Marinas sp -= get_random_int() & ~PAGE_MASK; 565b3901d54SCatalin Marinas return sp & ~0xf; 566b3901d54SCatalin Marinas } 567b3901d54SCatalin Marinas 568d1be5c99SYury Norov /* 569d1be5c99SYury Norov * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY. 570d1be5c99SYury Norov */ 571d1be5c99SYury Norov void arch_setup_new_exec(void) 572d1be5c99SYury Norov { 573d1be5c99SYury Norov current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0; 57475031975SMark Rutland 57575031975SMark Rutland ptrauth_thread_init_user(current); 576d1be5c99SYury Norov } 57763f0c603SCatalin Marinas 57863f0c603SCatalin Marinas #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI 57963f0c603SCatalin Marinas /* 58063f0c603SCatalin Marinas * Control the relaxed ABI allowing tagged user addresses into the kernel. 58163f0c603SCatalin Marinas */ 582413235fcSCatalin Marinas static unsigned int tagged_addr_disabled; 58363f0c603SCatalin Marinas 58463f0c603SCatalin Marinas long set_tagged_addr_ctrl(unsigned long arg) 58563f0c603SCatalin Marinas { 58663f0c603SCatalin Marinas if (is_compat_task()) 58763f0c603SCatalin Marinas return -EINVAL; 58863f0c603SCatalin Marinas if (arg & ~PR_TAGGED_ADDR_ENABLE) 58963f0c603SCatalin Marinas return -EINVAL; 59063f0c603SCatalin Marinas 591413235fcSCatalin Marinas /* 592413235fcSCatalin Marinas * Do not allow the enabling of the tagged address ABI if globally 593413235fcSCatalin Marinas * disabled via sysctl abi.tagged_addr_disabled. 594413235fcSCatalin Marinas */ 595413235fcSCatalin Marinas if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled) 596413235fcSCatalin Marinas return -EINVAL; 597413235fcSCatalin Marinas 59863f0c603SCatalin Marinas update_thread_flag(TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE); 59963f0c603SCatalin Marinas 60063f0c603SCatalin Marinas return 0; 60163f0c603SCatalin Marinas } 60263f0c603SCatalin Marinas 60363f0c603SCatalin Marinas long get_tagged_addr_ctrl(void) 60463f0c603SCatalin Marinas { 60563f0c603SCatalin Marinas if (is_compat_task()) 60663f0c603SCatalin Marinas return -EINVAL; 60763f0c603SCatalin Marinas 60863f0c603SCatalin Marinas if (test_thread_flag(TIF_TAGGED_ADDR)) 60963f0c603SCatalin Marinas return PR_TAGGED_ADDR_ENABLE; 61063f0c603SCatalin Marinas 61163f0c603SCatalin Marinas return 0; 61263f0c603SCatalin Marinas } 61363f0c603SCatalin Marinas 61463f0c603SCatalin Marinas /* 61563f0c603SCatalin Marinas * Global sysctl to disable the tagged user addresses support. This control 61663f0c603SCatalin Marinas * only prevents the tagged address ABI enabling via prctl() and does not 61763f0c603SCatalin Marinas * disable it for tasks that already opted in to the relaxed ABI. 61863f0c603SCatalin Marinas */ 61963f0c603SCatalin Marinas 62063f0c603SCatalin Marinas static struct ctl_table tagged_addr_sysctl_table[] = { 62163f0c603SCatalin Marinas { 622413235fcSCatalin Marinas .procname = "tagged_addr_disabled", 62363f0c603SCatalin Marinas .mode = 0644, 624413235fcSCatalin Marinas .data = &tagged_addr_disabled, 62563f0c603SCatalin Marinas .maxlen = sizeof(int), 62663f0c603SCatalin Marinas .proc_handler = proc_dointvec_minmax, 6272c614c11SMatteo Croce .extra1 = SYSCTL_ZERO, 6282c614c11SMatteo Croce .extra2 = SYSCTL_ONE, 62963f0c603SCatalin Marinas }, 63063f0c603SCatalin Marinas { } 63163f0c603SCatalin Marinas }; 63263f0c603SCatalin Marinas 63363f0c603SCatalin Marinas static int __init tagged_addr_init(void) 63463f0c603SCatalin Marinas { 63563f0c603SCatalin Marinas if (!register_sysctl("abi", tagged_addr_sysctl_table)) 63663f0c603SCatalin Marinas return -EINVAL; 63763f0c603SCatalin Marinas return 0; 63863f0c603SCatalin Marinas } 63963f0c603SCatalin Marinas 64063f0c603SCatalin Marinas core_initcall(tagged_addr_init); 64163f0c603SCatalin Marinas #endif /* CONFIG_ARM64_TAGGED_ADDR_ABI */ 64219c95f26SJulien Thierry 64319c95f26SJulien Thierry asmlinkage void __sched arm64_preempt_schedule_irq(void) 64419c95f26SJulien Thierry { 64519c95f26SJulien Thierry lockdep_assert_irqs_disabled(); 64619c95f26SJulien Thierry 64719c95f26SJulien Thierry /* 64819c95f26SJulien Thierry * Preempting a task from an IRQ means we leave copies of PSTATE 64919c95f26SJulien Thierry * on the stack. cpufeature's enable calls may modify PSTATE, but 65019c95f26SJulien Thierry * resuming one of these preempted tasks would undo those changes. 65119c95f26SJulien Thierry * 65219c95f26SJulien Thierry * Only allow a task to be preempted once cpufeatures have been 65319c95f26SJulien Thierry * enabled. 65419c95f26SJulien Thierry */ 655b51c6ac2SSuzuki K Poulose if (system_capabilities_finalized()) 65619c95f26SJulien Thierry preempt_schedule_irq(); 65719c95f26SJulien Thierry } 658