1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Based on arch/arm/kernel/process.c 4 * 5 * Original Copyright (C) 1995 Linus Torvalds 6 * Copyright (C) 1996-2000 Russell King - Converted to ARM. 7 * Copyright (C) 2012 ARM Ltd. 8 */ 9 #include <linux/compat.h> 10 #include <linux/efi.h> 11 #include <linux/elf.h> 12 #include <linux/export.h> 13 #include <linux/sched.h> 14 #include <linux/sched/debug.h> 15 #include <linux/sched/task.h> 16 #include <linux/sched/task_stack.h> 17 #include <linux/kernel.h> 18 #include <linux/mman.h> 19 #include <linux/mm.h> 20 #include <linux/nospec.h> 21 #include <linux/stddef.h> 22 #include <linux/sysctl.h> 23 #include <linux/unistd.h> 24 #include <linux/user.h> 25 #include <linux/delay.h> 26 #include <linux/reboot.h> 27 #include <linux/interrupt.h> 28 #include <linux/init.h> 29 #include <linux/cpumask.h> 30 #include <linux/cpu.h> 31 #include <linux/elfcore.h> 32 #include <linux/pm.h> 33 #include <linux/tick.h> 34 #include <linux/utsname.h> 35 #include <linux/uaccess.h> 36 #include <linux/random.h> 37 #include <linux/hw_breakpoint.h> 38 #include <linux/personality.h> 39 #include <linux/notifier.h> 40 #include <trace/events/power.h> 41 #include <linux/percpu.h> 42 #include <linux/thread_info.h> 43 #include <linux/prctl.h> 44 #include <linux/stacktrace.h> 45 46 #include <asm/alternative.h> 47 #include <asm/arch_timer.h> 48 #include <asm/compat.h> 49 #include <asm/cpufeature.h> 50 #include <asm/cacheflush.h> 51 #include <asm/exec.h> 52 #include <asm/fpsimd.h> 53 #include <asm/gcs.h> 54 #include <asm/mmu_context.h> 55 #include <asm/mpam.h> 56 #include <asm/mte.h> 57 #include <asm/processor.h> 58 #include <asm/pointer_auth.h> 59 #include <asm/stacktrace.h> 60 #include <asm/switch_to.h> 61 #include <asm/system_misc.h> 62 63 #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) 64 #include <linux/stackprotector.h> 65 unsigned long __stack_chk_guard __ro_after_init; 66 EXPORT_SYMBOL(__stack_chk_guard); 67 #endif 68 69 /* 70 * Function pointers to optional machine specific functions 71 */ 72 void (*pm_power_off)(void); 73 EXPORT_SYMBOL_GPL(pm_power_off); 74 75 #ifdef CONFIG_HOTPLUG_CPU 76 void __noreturn arch_cpu_idle_dead(void) 77 { 78 cpu_die(); 79 } 80 #endif 81 82 /* 83 * Called by kexec, immediately prior to machine_kexec(). 84 * 85 * This must completely disable all secondary CPUs; simply causing those CPUs 86 * to execute e.g. a RAM-based pin loop is not sufficient. This allows the 87 * kexec'd kernel to use any and all RAM as it sees fit, without having to 88 * avoid any code or data used by any SW CPU pin loop. The CPU hotplug 89 * functionality embodied in smpt_shutdown_nonboot_cpus() to achieve this. 90 */ 91 void machine_shutdown(void) 92 { 93 smp_shutdown_nonboot_cpus(reboot_cpu); 94 } 95 96 /* 97 * Halting simply requires that the secondary CPUs stop performing any 98 * activity (executing tasks, handling interrupts). smp_send_stop() 99 * achieves this. 100 */ 101 void machine_halt(void) 102 { 103 local_irq_disable(); 104 smp_send_stop(); 105 while (1); 106 } 107 108 /* 109 * Power-off simply requires that the secondary CPUs stop performing any 110 * activity (executing tasks, handling interrupts). smp_send_stop() 111 * achieves this. When the system power is turned off, it will take all CPUs 112 * with it. 113 */ 114 void machine_power_off(void) 115 { 116 local_irq_disable(); 117 smp_send_stop(); 118 do_kernel_power_off(); 119 } 120 121 /* 122 * Restart requires that the secondary CPUs stop performing any activity 123 * while the primary CPU resets the system. Systems with multiple CPUs must 124 * provide a HW restart implementation, to ensure that all CPUs reset at once. 125 * This is required so that any code running after reset on the primary CPU 126 * doesn't have to co-ordinate with other CPUs to ensure they aren't still 127 * executing pre-reset code, and using RAM that the primary CPU's code wishes 128 * to use. Implementing such co-ordination would be essentially impossible. 129 */ 130 void machine_restart(char *cmd) 131 { 132 /* Disable interrupts first */ 133 local_irq_disable(); 134 smp_send_stop(); 135 136 /* 137 * UpdateCapsule() depends on the system being reset via 138 * ResetSystem(). 139 */ 140 if (efi_enabled(EFI_RUNTIME_SERVICES)) 141 efi_reboot(reboot_mode, NULL); 142 143 /* Now call the architecture specific reboot code. */ 144 do_kernel_restart(cmd); 145 146 /* 147 * Whoops - the architecture was unable to reboot. 148 */ 149 printk("Reboot failed -- System halted\n"); 150 while (1); 151 } 152 153 #define bstr(suffix, str) [PSR_BTYPE_ ## suffix >> PSR_BTYPE_SHIFT] = str 154 static const char *const btypes[] = { 155 bstr(NONE, "--"), 156 bstr( JC, "jc"), 157 bstr( C, "-c"), 158 bstr( J , "j-") 159 }; 160 #undef bstr 161 162 static void print_pstate(struct pt_regs *regs) 163 { 164 u64 pstate = regs->pstate; 165 166 if (compat_user_mode(regs)) { 167 printk("pstate: %08llx (%c%c%c%c %c %s %s %c%c%c %cDIT %cSSBS)\n", 168 pstate, 169 pstate & PSR_AA32_N_BIT ? 'N' : 'n', 170 pstate & PSR_AA32_Z_BIT ? 'Z' : 'z', 171 pstate & PSR_AA32_C_BIT ? 'C' : 'c', 172 pstate & PSR_AA32_V_BIT ? 'V' : 'v', 173 pstate & PSR_AA32_Q_BIT ? 'Q' : 'q', 174 pstate & PSR_AA32_T_BIT ? "T32" : "A32", 175 pstate & PSR_AA32_E_BIT ? "BE" : "LE", 176 pstate & PSR_AA32_A_BIT ? 'A' : 'a', 177 pstate & PSR_AA32_I_BIT ? 'I' : 'i', 178 pstate & PSR_AA32_F_BIT ? 'F' : 'f', 179 pstate & PSR_AA32_DIT_BIT ? '+' : '-', 180 pstate & PSR_AA32_SSBS_BIT ? '+' : '-'); 181 } else { 182 const char *btype_str = btypes[(pstate & PSR_BTYPE_MASK) >> 183 PSR_BTYPE_SHIFT]; 184 185 printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO %cTCO %cDIT %cSSBS BTYPE=%s)\n", 186 pstate, 187 pstate & PSR_N_BIT ? 'N' : 'n', 188 pstate & PSR_Z_BIT ? 'Z' : 'z', 189 pstate & PSR_C_BIT ? 'C' : 'c', 190 pstate & PSR_V_BIT ? 'V' : 'v', 191 pstate & PSR_D_BIT ? 'D' : 'd', 192 pstate & PSR_A_BIT ? 'A' : 'a', 193 pstate & PSR_I_BIT ? 'I' : 'i', 194 pstate & PSR_F_BIT ? 'F' : 'f', 195 pstate & PSR_PAN_BIT ? '+' : '-', 196 pstate & PSR_UAO_BIT ? '+' : '-', 197 pstate & PSR_TCO_BIT ? '+' : '-', 198 pstate & PSR_DIT_BIT ? '+' : '-', 199 pstate & PSR_SSBS_BIT ? '+' : '-', 200 btype_str); 201 } 202 } 203 204 void __show_regs(struct pt_regs *regs) 205 { 206 int i, top_reg; 207 u64 lr, sp; 208 209 if (compat_user_mode(regs)) { 210 lr = regs->compat_lr; 211 sp = regs->compat_sp; 212 top_reg = 12; 213 } else { 214 lr = regs->regs[30]; 215 sp = regs->sp; 216 top_reg = 29; 217 } 218 219 show_regs_print_info(KERN_DEFAULT); 220 print_pstate(regs); 221 222 if (!user_mode(regs)) { 223 printk("pc : %pS\n", (void *)regs->pc); 224 printk("lr : %pS\n", (void *)ptrauth_strip_kernel_insn_pac(lr)); 225 } else { 226 printk("pc : %016llx\n", regs->pc); 227 printk("lr : %016llx\n", lr); 228 } 229 230 printk("sp : %016llx\n", sp); 231 232 if (system_uses_irq_prio_masking()) 233 printk("pmr: %08x\n", regs->pmr); 234 235 i = top_reg; 236 237 while (i >= 0) { 238 printk("x%-2d: %016llx", i, regs->regs[i]); 239 240 while (i-- % 3) 241 pr_cont(" x%-2d: %016llx", i, regs->regs[i]); 242 243 pr_cont("\n"); 244 } 245 } 246 247 void show_regs(struct pt_regs *regs) 248 { 249 __show_regs(regs); 250 dump_backtrace(regs, NULL, KERN_DEFAULT); 251 } 252 253 static void tls_thread_flush(void) 254 { 255 write_sysreg(0, tpidr_el0); 256 if (system_supports_tpidr2()) 257 write_sysreg_s(0, SYS_TPIDR2_EL0); 258 259 if (is_compat_task()) { 260 current->thread.uw.tp_value = 0; 261 262 /* 263 * We need to ensure ordering between the shadow state and the 264 * hardware state, so that we don't corrupt the hardware state 265 * with a stale shadow state during context switch. 266 */ 267 barrier(); 268 write_sysreg(0, tpidrro_el0); 269 } 270 } 271 272 static void flush_tagged_addr_state(void) 273 { 274 if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI)) 275 clear_thread_flag(TIF_TAGGED_ADDR); 276 } 277 278 static void flush_poe(void) 279 { 280 if (!system_supports_poe()) 281 return; 282 283 write_sysreg_s(POR_EL0_INIT, SYS_POR_EL0); 284 } 285 286 #ifdef CONFIG_ARM64_GCS 287 288 static void flush_gcs(void) 289 { 290 if (!system_supports_gcs()) 291 return; 292 293 current->thread.gcspr_el0 = 0; 294 current->thread.gcs_base = 0; 295 current->thread.gcs_size = 0; 296 current->thread.gcs_el0_mode = 0; 297 current->thread.gcs_el0_locked = 0; 298 write_sysreg_s(GCSCRE0_EL1_nTR, SYS_GCSCRE0_EL1); 299 write_sysreg_s(0, SYS_GCSPR_EL0); 300 } 301 302 static int copy_thread_gcs(struct task_struct *p, 303 const struct kernel_clone_args *args) 304 { 305 unsigned long gcs; 306 307 if (!system_supports_gcs()) 308 return 0; 309 310 p->thread.gcs_base = 0; 311 p->thread.gcs_size = 0; 312 313 p->thread.gcs_el0_mode = current->thread.gcs_el0_mode; 314 p->thread.gcs_el0_locked = current->thread.gcs_el0_locked; 315 316 gcs = gcs_alloc_thread_stack(p, args); 317 if (IS_ERR_VALUE(gcs)) 318 return PTR_ERR((void *)gcs); 319 320 return 0; 321 } 322 323 static void gcs_reset_thread(struct task_struct *task) 324 { 325 task->thread.gcs_base = 0; 326 task->thread.gcs_size = 0; 327 task->thread.gcspr_el0 = 0; 328 } 329 330 #else 331 332 static void flush_gcs(void) { } 333 static void gcs_reset_thread(struct task_struct *task) { } 334 335 static int copy_thread_gcs(struct task_struct *p, 336 const struct kernel_clone_args *args) 337 { 338 return 0; 339 } 340 341 #endif 342 343 void flush_thread(void) 344 { 345 fpsimd_flush_thread(); 346 tls_thread_flush(); 347 flush_ptrace_hw_breakpoint(current); 348 flush_tagged_addr_state(); 349 flush_poe(); 350 flush_gcs(); 351 } 352 353 void exit_thread(struct task_struct *tsk) 354 { 355 /* Failed fork after copy_thread(); deactivate_mm() does not run. */ 356 gcs_free(tsk); 357 } 358 359 void arch_release_task_struct(struct task_struct *tsk) 360 { 361 fpsimd_release_task(tsk); 362 } 363 364 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) 365 { 366 /* 367 * The current/src task's FPSIMD state may or may not be live, and may 368 * have been altered by ptrace after entry to the kernel. Save the 369 * effective FPSIMD state so that this will be copied into dst. 370 */ 371 fpsimd_save_and_flush_current_state(); 372 fpsimd_sync_from_effective_state(src); 373 374 *dst = *src; 375 376 /* 377 * Drop stale reference to src's sve_state and convert dst to 378 * non-streaming FPSIMD mode. 379 */ 380 dst->thread.fp_type = FP_STATE_FPSIMD; 381 dst->thread.sve_state = NULL; 382 clear_tsk_thread_flag(dst, TIF_SVE); 383 task_smstop_sm(dst); 384 385 /* 386 * Drop stale reference to src's sme_state and ensure dst has ZA 387 * disabled. 388 * 389 * When necessary, ZA will be inherited later in copy_thread_za(). 390 */ 391 dst->thread.sme_state = NULL; 392 clear_tsk_thread_flag(dst, TIF_SME); 393 dst->thread.svcr &= ~SVCR_ZA_MASK; 394 395 /* clear any pending asynchronous tag fault raised by the parent */ 396 clear_tsk_thread_flag(dst, TIF_MTE_ASYNC_FAULT); 397 398 return 0; 399 } 400 401 static int copy_thread_za(struct task_struct *dst, struct task_struct *src) 402 { 403 if (!thread_za_enabled(&src->thread)) 404 return 0; 405 406 dst->thread.sve_state = kzalloc(sve_state_size(src), 407 GFP_KERNEL); 408 if (!dst->thread.sve_state) 409 return -ENOMEM; 410 411 dst->thread.sme_state = kmemdup(src->thread.sme_state, 412 sme_state_size(src), 413 GFP_KERNEL); 414 if (!dst->thread.sme_state) { 415 kfree(dst->thread.sve_state); 416 dst->thread.sve_state = NULL; 417 return -ENOMEM; 418 } 419 420 set_tsk_thread_flag(dst, TIF_SME); 421 dst->thread.svcr |= SVCR_ZA_MASK; 422 423 return 0; 424 } 425 426 asmlinkage void ret_from_fork(void) asm("ret_from_fork"); 427 428 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) 429 { 430 u64 clone_flags = args->flags; 431 unsigned long stack_start = args->stack; 432 unsigned long tls = args->tls; 433 struct pt_regs *childregs = task_pt_regs(p); 434 int ret; 435 436 memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context)); 437 438 /* 439 * In case p was allocated the same task_struct pointer as some 440 * other recently-exited task, make sure p is disassociated from 441 * any cpu that may have run that now-exited task recently. 442 * Otherwise we could erroneously skip reloading the FPSIMD 443 * registers for p. 444 */ 445 fpsimd_flush_task_state(p); 446 447 ptrauth_thread_init_kernel(p); 448 449 if (likely(!args->fn)) { 450 *childregs = *current_pt_regs(); 451 childregs->regs[0] = 0; 452 453 /* 454 * Read the current TLS pointer from tpidr_el0 as it may be 455 * out-of-sync with the saved value. 456 */ 457 *task_user_tls(p) = read_sysreg(tpidr_el0); 458 459 if (system_supports_poe()) 460 p->thread.por_el0 = read_sysreg_s(SYS_POR_EL0); 461 462 if (stack_start) { 463 if (is_compat_thread(task_thread_info(p))) 464 childregs->compat_sp = stack_start; 465 else 466 childregs->sp = stack_start; 467 } 468 469 /* 470 * Due to the AAPCS64 "ZA lazy saving scheme", PSTATE.ZA and 471 * TPIDR2 need to be manipulated as a pair, and either both 472 * need to be inherited or both need to be reset. 473 * 474 * Within a process, child threads must not inherit their 475 * parent's TPIDR2 value or they may clobber their parent's 476 * stack at some later point. 477 * 478 * When a process is fork()'d, the child must inherit ZA and 479 * TPIDR2 from its parent in case there was dormant ZA state. 480 * 481 * Use CLONE_VM to determine when the child will share the 482 * address space with the parent, and cannot safely inherit the 483 * state. 484 */ 485 if (system_supports_sme()) { 486 if (!(clone_flags & CLONE_VM)) { 487 p->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0); 488 ret = copy_thread_za(p, current); 489 if (ret) 490 return ret; 491 } else { 492 p->thread.tpidr2_el0 = 0; 493 WARN_ON_ONCE(p->thread.svcr & SVCR_ZA_MASK); 494 } 495 } 496 497 /* 498 * If a TLS pointer was passed to clone, use it for the new 499 * thread. 500 */ 501 if (clone_flags & CLONE_SETTLS) 502 p->thread.uw.tp_value = tls; 503 504 ret = copy_thread_gcs(p, args); 505 if (ret != 0) 506 return ret; 507 } else { 508 /* 509 * A kthread has no context to ERET to, so ensure any buggy 510 * ERET is treated as an illegal exception return. 511 * 512 * When a user task is created from a kthread, childregs will 513 * be initialized by start_thread() or start_compat_thread(). 514 */ 515 memset(childregs, 0, sizeof(struct pt_regs)); 516 childregs->pstate = PSR_MODE_EL1h | PSR_IL_BIT; 517 childregs->stackframe.type = FRAME_META_TYPE_FINAL; 518 519 p->thread.cpu_context.x19 = (unsigned long)args->fn; 520 p->thread.cpu_context.x20 = (unsigned long)args->fn_arg; 521 /* Kernel threads sharing the parent's mm must not free its GCS. */ 522 gcs_reset_thread(p); 523 524 if (system_supports_poe()) 525 p->thread.por_el0 = POR_EL0_INIT; 526 } 527 p->thread.cpu_context.pc = (unsigned long)ret_from_fork; 528 p->thread.cpu_context.sp = (unsigned long)childregs; 529 /* 530 * For the benefit of the unwinder, set up childregs->stackframe 531 * as the final frame for the new task. 532 */ 533 p->thread.cpu_context.fp = (unsigned long)&childregs->stackframe; 534 535 ptrace_hw_copy_thread(p); 536 537 return 0; 538 } 539 540 void tls_preserve_current_state(void) 541 { 542 *task_user_tls(current) = read_sysreg(tpidr_el0); 543 if (system_supports_tpidr2() && !is_compat_task()) 544 current->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0); 545 } 546 547 static void tls_thread_switch(struct task_struct *next) 548 { 549 tls_preserve_current_state(); 550 551 if (is_compat_thread(task_thread_info(next))) 552 write_sysreg(next->thread.uw.tp_value, tpidrro_el0); 553 else 554 write_sysreg(0, tpidrro_el0); 555 556 write_sysreg(*task_user_tls(next), tpidr_el0); 557 if (system_supports_tpidr2()) 558 write_sysreg_s(next->thread.tpidr2_el0, SYS_TPIDR2_EL0); 559 } 560 561 /* 562 * Force SSBS state on context-switch, since it may be lost after migrating 563 * from a CPU which treats the bit as RES0 in a heterogeneous system. 564 */ 565 static void ssbs_thread_switch(struct task_struct *next) 566 { 567 /* 568 * Nothing to do for kernel threads, but 'regs' may be junk 569 * (e.g. idle task) so check the flags and bail early. 570 */ 571 if (unlikely(next->flags & PF_KTHREAD)) 572 return; 573 574 /* 575 * If all CPUs implement the SSBS extension, then we just need to 576 * context-switch the PSTATE field. 577 */ 578 if (alternative_has_cap_unlikely(ARM64_SSBS)) 579 return; 580 581 spectre_v4_enable_task_mitigation(next); 582 } 583 584 /* 585 * We store our current task in sp_el0, which is clobbered by userspace. Keep a 586 * shadow copy so that we can restore this upon entry from userspace. 587 * 588 * This is *only* for exception entry from EL0, and is not valid until we 589 * __switch_to() a user task. 590 */ 591 DEFINE_PER_CPU(struct task_struct *, __entry_task); 592 593 static void entry_task_switch(struct task_struct *next) 594 { 595 __this_cpu_write(__entry_task, next); 596 } 597 598 #ifdef CONFIG_ARM64_GCS 599 600 void gcs_preserve_current_state(void) 601 { 602 current->thread.gcspr_el0 = read_sysreg_s(SYS_GCSPR_EL0); 603 } 604 605 static void gcs_thread_switch(struct task_struct *next) 606 { 607 if (!system_supports_gcs()) 608 return; 609 610 /* GCSPR_EL0 is always readable */ 611 gcs_preserve_current_state(); 612 write_sysreg_s(next->thread.gcspr_el0, SYS_GCSPR_EL0); 613 614 if (current->thread.gcs_el0_mode != next->thread.gcs_el0_mode) 615 gcs_set_el0_mode(next); 616 617 /* 618 * Ensure that GCS memory effects of the 'prev' thread are 619 * ordered before other memory accesses with release semantics 620 * (or preceded by a DMB) on the current PE. In addition, any 621 * memory accesses with acquire semantics (or succeeded by a 622 * DMB) are ordered before GCS memory effects of the 'next' 623 * thread. This will ensure that the GCS memory effects are 624 * visible to other PEs in case of migration. 625 */ 626 if (task_gcs_el0_enabled(current) || task_gcs_el0_enabled(next)) 627 gcsb_dsync(); 628 } 629 630 #else 631 632 static void gcs_thread_switch(struct task_struct *next) 633 { 634 } 635 636 #endif 637 638 /* 639 * Handle sysreg updates for ARM erratum 1418040 which affects the 32bit view of 640 * CNTVCT, various other errata which require trapping all CNTVCT{,_EL0} 641 * accesses and prctl(PR_SET_TSC). Ensure access is disabled iff a workaround is 642 * required or PR_TSC_SIGSEGV is set. 643 */ 644 static void update_cntkctl_el1(struct task_struct *next) 645 { 646 struct thread_info *ti = task_thread_info(next); 647 648 if (test_ti_thread_flag(ti, TIF_TSC_SIGSEGV) || 649 has_erratum_handler(read_cntvct_el0) || 650 (IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040) && 651 this_cpu_has_cap(ARM64_WORKAROUND_1418040) && 652 is_compat_thread(ti))) 653 sysreg_clear_set(cntkctl_el1, ARCH_TIMER_USR_VCT_ACCESS_EN, 0); 654 else 655 sysreg_clear_set(cntkctl_el1, 0, ARCH_TIMER_USR_VCT_ACCESS_EN); 656 } 657 658 static void cntkctl_thread_switch(struct task_struct *prev, 659 struct task_struct *next) 660 { 661 if ((read_ti_thread_flags(task_thread_info(prev)) & 662 (_TIF_32BIT | _TIF_TSC_SIGSEGV)) != 663 (read_ti_thread_flags(task_thread_info(next)) & 664 (_TIF_32BIT | _TIF_TSC_SIGSEGV))) 665 update_cntkctl_el1(next); 666 } 667 668 static int do_set_tsc_mode(unsigned int val) 669 { 670 bool tsc_sigsegv; 671 672 if (val == PR_TSC_SIGSEGV) 673 tsc_sigsegv = true; 674 else if (val == PR_TSC_ENABLE) 675 tsc_sigsegv = false; 676 else 677 return -EINVAL; 678 679 preempt_disable(); 680 update_thread_flag(TIF_TSC_SIGSEGV, tsc_sigsegv); 681 update_cntkctl_el1(current); 682 preempt_enable(); 683 684 return 0; 685 } 686 687 static void permission_overlay_switch(struct task_struct *next) 688 { 689 if (!system_supports_poe()) 690 return; 691 692 current->thread.por_el0 = read_sysreg_s(SYS_POR_EL0); 693 if (current->thread.por_el0 != next->thread.por_el0) { 694 write_sysreg_s(next->thread.por_el0, SYS_POR_EL0); 695 /* 696 * No ISB required as we can tolerate spurious Overlay faults - 697 * the fault handler will check again based on the new value 698 * of POR_EL0. 699 */ 700 } 701 } 702 703 /* 704 * __switch_to() checks current->thread.sctlr_user as an optimisation. Therefore 705 * this function must be called with preemption disabled and the update to 706 * sctlr_user must be made in the same preemption disabled block so that 707 * __switch_to() does not see the variable update before the SCTLR_EL1 one. 708 */ 709 void update_sctlr_el1(u64 sctlr) 710 { 711 /* 712 * EnIA must not be cleared while in the kernel as this is necessary for 713 * in-kernel PAC. It will be cleared on kernel exit if needed. 714 */ 715 sysreg_clear_set(sctlr_el1, SCTLR_USER_MASK & ~SCTLR_ELx_ENIA, sctlr); 716 717 /* ISB required for the kernel uaccess routines when setting TCF0. */ 718 isb(); 719 } 720 721 static inline void debug_switch_state(void) 722 { 723 if (system_uses_irq_prio_masking()) { 724 unsigned long daif_expected = 0; 725 unsigned long daif_actual = read_sysreg(daif); 726 unsigned long pmr_expected = GIC_PRIO_IRQOFF; 727 unsigned long pmr_actual = read_sysreg_s(SYS_ICC_PMR_EL1); 728 729 WARN_ONCE(daif_actual != daif_expected || 730 pmr_actual != pmr_expected, 731 "Unexpected DAIF + PMR: 0x%lx + 0x%lx (expected 0x%lx + 0x%lx)\n", 732 daif_actual, pmr_actual, 733 daif_expected, pmr_expected); 734 } else { 735 unsigned long daif_expected = DAIF_PROCCTX_NOIRQ; 736 unsigned long daif_actual = read_sysreg(daif); 737 738 WARN_ONCE(daif_actual != daif_expected, 739 "Unexpected DAIF value: 0x%lx (expected 0x%lx)\n", 740 daif_actual, daif_expected); 741 } 742 } 743 744 /* 745 * Thread switching. 746 */ 747 __notrace_funcgraph __sched 748 struct task_struct *__switch_to(struct task_struct *prev, 749 struct task_struct *next) 750 { 751 struct task_struct *last; 752 753 debug_switch_state(); 754 755 fpsimd_thread_switch(next); 756 tls_thread_switch(next); 757 hw_breakpoint_thread_switch(next); 758 contextidr_thread_switch(next); 759 entry_task_switch(next); 760 ssbs_thread_switch(next); 761 cntkctl_thread_switch(prev, next); 762 ptrauth_thread_switch_user(next); 763 permission_overlay_switch(next); 764 gcs_thread_switch(next); 765 766 /* 767 * Complete any pending TLB or cache maintenance on this CPU in case the 768 * thread migrates to a different CPU. This full barrier is also 769 * required by the membarrier system call. Additionally it makes any 770 * in-progress pgtable writes visible to the table walker; See 771 * emit_pte_barriers(). 772 */ 773 dsb(ish); 774 775 /* 776 * MTE thread switching must happen after the DSB above to ensure that 777 * any asynchronous tag check faults have been logged in the TFSR*_EL1 778 * registers. 779 */ 780 mte_thread_switch(next); 781 /* avoid expensive SCTLR_EL1 accesses if no change */ 782 if (prev->thread.sctlr_user != next->thread.sctlr_user) 783 update_sctlr_el1(next->thread.sctlr_user); 784 785 /* 786 * MPAM thread switch happens after the DSB to ensure prev's accesses 787 * use prev's MPAM settings. 788 */ 789 mpam_thread_switch(next); 790 791 /* the actual thread switch */ 792 last = cpu_switch_to(prev, next); 793 794 return last; 795 } 796 797 struct wchan_info { 798 unsigned long pc; 799 int count; 800 }; 801 802 static bool get_wchan_cb(void *arg, unsigned long pc) 803 { 804 struct wchan_info *wchan_info = arg; 805 806 if (!in_sched_functions(pc)) { 807 wchan_info->pc = pc; 808 return false; 809 } 810 return wchan_info->count++ < 16; 811 } 812 813 unsigned long __get_wchan(struct task_struct *p) 814 { 815 struct wchan_info wchan_info = { 816 .pc = 0, 817 .count = 0, 818 }; 819 820 if (!try_get_task_stack(p)) 821 return 0; 822 823 arch_stack_walk(get_wchan_cb, &wchan_info, p, NULL); 824 825 put_task_stack(p); 826 827 return wchan_info.pc; 828 } 829 830 unsigned long arch_align_stack(unsigned long sp) 831 { 832 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) 833 sp -= get_random_u32_below(PAGE_SIZE); 834 return sp & ~0xf; 835 } 836 837 #ifdef CONFIG_COMPAT 838 int compat_elf_check_arch(const struct elf32_hdr *hdr) 839 { 840 if (!system_supports_32bit_el0()) 841 return false; 842 843 if ((hdr)->e_machine != EM_ARM) 844 return false; 845 846 if (!((hdr)->e_flags & EF_ARM_EABI_MASK)) 847 return false; 848 849 /* 850 * Prevent execve() of a 32-bit program from a deadline task 851 * if the restricted affinity mask would be inadmissible on an 852 * asymmetric system. 853 */ 854 return !static_branch_unlikely(&arm64_mismatched_32bit_el0) || 855 !dl_task_check_affinity(current, system_32bit_el0_cpumask()); 856 } 857 #endif 858 859 /* 860 * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY. 861 */ 862 void arch_setup_new_exec(void) 863 { 864 unsigned long mmflags = 0; 865 866 if (is_compat_task()) { 867 mmflags = MMCF_AARCH32; 868 869 /* 870 * Restrict the CPU affinity mask for a 32-bit task so that 871 * it contains only 32-bit-capable CPUs. 872 * 873 * From the perspective of the task, this looks similar to 874 * what would happen if the 64-bit-only CPUs were hot-unplugged 875 * at the point of execve(), although we try a bit harder to 876 * honour the cpuset hierarchy. 877 */ 878 if (static_branch_unlikely(&arm64_mismatched_32bit_el0)) 879 force_compatible_cpus_allowed_ptr(current); 880 } else if (static_branch_unlikely(&arm64_mismatched_32bit_el0)) { 881 relax_compatible_cpus_allowed_ptr(current); 882 } 883 884 current->mm->context.flags = mmflags; 885 ptrauth_thread_init_user(); 886 mte_thread_init_user(); 887 do_set_tsc_mode(PR_TSC_ENABLE); 888 889 if (task_spec_ssb_noexec(current)) { 890 arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS, 891 PR_SPEC_ENABLE); 892 } 893 } 894 895 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI 896 /* 897 * Control the relaxed ABI allowing tagged user addresses into the kernel. 898 */ 899 static unsigned int tagged_addr_disabled; 900 901 long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg) 902 { 903 unsigned long valid_mask = PR_TAGGED_ADDR_ENABLE; 904 struct thread_info *ti = task_thread_info(task); 905 906 if (is_compat_thread(ti)) 907 return -EINVAL; 908 909 if (system_supports_mte()) { 910 valid_mask |= PR_MTE_TCF_SYNC | PR_MTE_TCF_ASYNC \ 911 | PR_MTE_TAG_MASK; 912 913 if (cpus_have_cap(ARM64_MTE_STORE_ONLY)) 914 valid_mask |= PR_MTE_STORE_ONLY; 915 } 916 917 if (arg & ~valid_mask) 918 return -EINVAL; 919 920 /* 921 * Do not allow the enabling of the tagged address ABI if globally 922 * disabled via sysctl abi.tagged_addr_disabled. 923 */ 924 if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled) 925 return -EINVAL; 926 927 if (set_mte_ctrl(task, arg) != 0) 928 return -EINVAL; 929 930 update_ti_thread_flag(ti, TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE); 931 932 return 0; 933 } 934 935 long get_tagged_addr_ctrl(struct task_struct *task) 936 { 937 long ret = 0; 938 struct thread_info *ti = task_thread_info(task); 939 940 if (is_compat_thread(ti)) 941 return -EINVAL; 942 943 if (test_ti_thread_flag(ti, TIF_TAGGED_ADDR)) 944 ret = PR_TAGGED_ADDR_ENABLE; 945 946 ret |= get_mte_ctrl(task); 947 948 return ret; 949 } 950 951 /* 952 * Global sysctl to disable the tagged user addresses support. This control 953 * only prevents the tagged address ABI enabling via prctl() and does not 954 * disable it for tasks that already opted in to the relaxed ABI. 955 */ 956 957 static const struct ctl_table tagged_addr_sysctl_table[] = { 958 { 959 .procname = "tagged_addr_disabled", 960 .mode = 0644, 961 .data = &tagged_addr_disabled, 962 .maxlen = sizeof(int), 963 .proc_handler = proc_dointvec_minmax, 964 .extra1 = SYSCTL_ZERO, 965 .extra2 = SYSCTL_ONE, 966 }, 967 }; 968 969 static int __init tagged_addr_init(void) 970 { 971 if (!register_sysctl("abi", tagged_addr_sysctl_table)) 972 return -EINVAL; 973 return 0; 974 } 975 976 core_initcall(tagged_addr_init); 977 #endif /* CONFIG_ARM64_TAGGED_ADDR_ABI */ 978 979 #ifdef CONFIG_BINFMT_ELF 980 int arch_elf_adjust_prot(int prot, const struct arch_elf_state *state, 981 bool has_interp, bool is_interp) 982 { 983 /* 984 * For dynamically linked executables the interpreter is 985 * responsible for setting PROT_BTI on everything except 986 * itself. 987 */ 988 if (is_interp != has_interp) 989 return prot; 990 991 if (!(state->flags & ARM64_ELF_BTI)) 992 return prot; 993 994 if (prot & PROT_EXEC) 995 prot |= PROT_BTI; 996 997 return prot; 998 } 999 #endif 1000 1001 int get_tsc_mode(unsigned long adr) 1002 { 1003 unsigned int val; 1004 1005 if (is_compat_task()) 1006 return -EINVAL; 1007 1008 if (test_thread_flag(TIF_TSC_SIGSEGV)) 1009 val = PR_TSC_SIGSEGV; 1010 else 1011 val = PR_TSC_ENABLE; 1012 1013 return put_user(val, (unsigned int __user *)adr); 1014 } 1015 1016 int set_tsc_mode(unsigned int val) 1017 { 1018 if (is_compat_task()) 1019 return -EINVAL; 1020 1021 return do_set_tsc_mode(val); 1022 } 1023