0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/compat.h>
0010 #include <linux/efi.h>
0011 #include <linux/elf.h>
0012 #include <linux/export.h>
0013 #include <linux/sched.h>
0014 #include <linux/sched/debug.h>
0015 #include <linux/sched/task.h>
0016 #include <linux/sched/task_stack.h>
0017 #include <linux/kernel.h>
0018 #include <linux/mman.h>
0019 #include <linux/mm.h>
0020 #include <linux/nospec.h>
0021 #include <linux/stddef.h>
0022 #include <linux/sysctl.h>
0023 #include <linux/unistd.h>
0024 #include <linux/user.h>
0025 #include <linux/delay.h>
0026 #include <linux/reboot.h>
0027 #include <linux/interrupt.h>
0028 #include <linux/init.h>
0029 #include <linux/cpu.h>
0030 #include <linux/elfcore.h>
0031 #include <linux/pm.h>
0032 #include <linux/tick.h>
0033 #include <linux/utsname.h>
0034 #include <linux/uaccess.h>
0035 #include <linux/random.h>
0036 #include <linux/hw_breakpoint.h>
0037 #include <linux/personality.h>
0038 #include <linux/notifier.h>
0039 #include <trace/events/power.h>
0040 #include <linux/percpu.h>
0041 #include <linux/thread_info.h>
0042 #include <linux/prctl.h>
0043 #include <linux/stacktrace.h>
0044
0045 #include <asm/alternative.h>
0046 #include <asm/compat.h>
0047 #include <asm/cpufeature.h>
0048 #include <asm/cacheflush.h>
0049 #include <asm/exec.h>
0050 #include <asm/fpsimd.h>
0051 #include <asm/mmu_context.h>
0052 #include <asm/mte.h>
0053 #include <asm/processor.h>
0054 #include <asm/pointer_auth.h>
0055 #include <asm/stacktrace.h>
0056 #include <asm/switch_to.h>
0057 #include <asm/system_misc.h>
0058
0059 #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK)
0060 #include <linux/stackprotector.h>
0061 unsigned long __stack_chk_guard __ro_after_init;
0062 EXPORT_SYMBOL(__stack_chk_guard);
0063 #endif
0064
0065
0066
0067
0068 void (*pm_power_off)(void);
0069 EXPORT_SYMBOL_GPL(pm_power_off);
0070
0071 #ifdef CONFIG_HOTPLUG_CPU
0072 void arch_cpu_idle_dead(void)
0073 {
0074 cpu_die();
0075 }
0076 #endif
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 void machine_shutdown(void)
0088 {
0089 smp_shutdown_nonboot_cpus(reboot_cpu);
0090 }
0091
0092
0093
0094
0095
0096
0097 void machine_halt(void)
0098 {
0099 local_irq_disable();
0100 smp_send_stop();
0101 while (1);
0102 }
0103
0104
0105
0106
0107
0108
0109
0110 void machine_power_off(void)
0111 {
0112 local_irq_disable();
0113 smp_send_stop();
0114 do_kernel_power_off();
0115 }
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126 void machine_restart(char *cmd)
0127 {
0128
0129 local_irq_disable();
0130 smp_send_stop();
0131
0132
0133
0134
0135
0136 if (efi_enabled(EFI_RUNTIME_SERVICES))
0137 efi_reboot(reboot_mode, NULL);
0138
0139
0140 do_kernel_restart(cmd);
0141
0142
0143
0144
0145 printk("Reboot failed -- System halted\n");
0146 while (1);
0147 }
0148
0149 #define bstr(suffix, str) [PSR_BTYPE_ ## suffix >> PSR_BTYPE_SHIFT] = str
0150 static const char *const btypes[] = {
0151 bstr(NONE, "--"),
0152 bstr( JC, "jc"),
0153 bstr( C, "-c"),
0154 bstr( J , "j-")
0155 };
0156 #undef bstr
0157
0158 static void print_pstate(struct pt_regs *regs)
0159 {
0160 u64 pstate = regs->pstate;
0161
0162 if (compat_user_mode(regs)) {
0163 printk("pstate: %08llx (%c%c%c%c %c %s %s %c%c%c %cDIT %cSSBS)\n",
0164 pstate,
0165 pstate & PSR_AA32_N_BIT ? 'N' : 'n',
0166 pstate & PSR_AA32_Z_BIT ? 'Z' : 'z',
0167 pstate & PSR_AA32_C_BIT ? 'C' : 'c',
0168 pstate & PSR_AA32_V_BIT ? 'V' : 'v',
0169 pstate & PSR_AA32_Q_BIT ? 'Q' : 'q',
0170 pstate & PSR_AA32_T_BIT ? "T32" : "A32",
0171 pstate & PSR_AA32_E_BIT ? "BE" : "LE",
0172 pstate & PSR_AA32_A_BIT ? 'A' : 'a',
0173 pstate & PSR_AA32_I_BIT ? 'I' : 'i',
0174 pstate & PSR_AA32_F_BIT ? 'F' : 'f',
0175 pstate & PSR_AA32_DIT_BIT ? '+' : '-',
0176 pstate & PSR_AA32_SSBS_BIT ? '+' : '-');
0177 } else {
0178 const char *btype_str = btypes[(pstate & PSR_BTYPE_MASK) >>
0179 PSR_BTYPE_SHIFT];
0180
0181 printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO %cTCO %cDIT %cSSBS BTYPE=%s)\n",
0182 pstate,
0183 pstate & PSR_N_BIT ? 'N' : 'n',
0184 pstate & PSR_Z_BIT ? 'Z' : 'z',
0185 pstate & PSR_C_BIT ? 'C' : 'c',
0186 pstate & PSR_V_BIT ? 'V' : 'v',
0187 pstate & PSR_D_BIT ? 'D' : 'd',
0188 pstate & PSR_A_BIT ? 'A' : 'a',
0189 pstate & PSR_I_BIT ? 'I' : 'i',
0190 pstate & PSR_F_BIT ? 'F' : 'f',
0191 pstate & PSR_PAN_BIT ? '+' : '-',
0192 pstate & PSR_UAO_BIT ? '+' : '-',
0193 pstate & PSR_TCO_BIT ? '+' : '-',
0194 pstate & PSR_DIT_BIT ? '+' : '-',
0195 pstate & PSR_SSBS_BIT ? '+' : '-',
0196 btype_str);
0197 }
0198 }
0199
0200 void __show_regs(struct pt_regs *regs)
0201 {
0202 int i, top_reg;
0203 u64 lr, sp;
0204
0205 if (compat_user_mode(regs)) {
0206 lr = regs->compat_lr;
0207 sp = regs->compat_sp;
0208 top_reg = 12;
0209 } else {
0210 lr = regs->regs[30];
0211 sp = regs->sp;
0212 top_reg = 29;
0213 }
0214
0215 show_regs_print_info(KERN_DEFAULT);
0216 print_pstate(regs);
0217
0218 if (!user_mode(regs)) {
0219 printk("pc : %pS\n", (void *)regs->pc);
0220 printk("lr : %pS\n", (void *)ptrauth_strip_insn_pac(lr));
0221 } else {
0222 printk("pc : %016llx\n", regs->pc);
0223 printk("lr : %016llx\n", lr);
0224 }
0225
0226 printk("sp : %016llx\n", sp);
0227
0228 if (system_uses_irq_prio_masking())
0229 printk("pmr_save: %08llx\n", regs->pmr_save);
0230
0231 i = top_reg;
0232
0233 while (i >= 0) {
0234 printk("x%-2d: %016llx", i, regs->regs[i]);
0235
0236 while (i-- % 3)
0237 pr_cont(" x%-2d: %016llx", i, regs->regs[i]);
0238
0239 pr_cont("\n");
0240 }
0241 }
0242
0243 void show_regs(struct pt_regs *regs)
0244 {
0245 __show_regs(regs);
0246 dump_backtrace(regs, NULL, KERN_DEFAULT);
0247 }
0248
0249 static void tls_thread_flush(void)
0250 {
0251 write_sysreg(0, tpidr_el0);
0252 if (system_supports_tpidr2())
0253 write_sysreg_s(0, SYS_TPIDR2_EL0);
0254
0255 if (is_compat_task()) {
0256 current->thread.uw.tp_value = 0;
0257
0258
0259
0260
0261
0262
0263 barrier();
0264 write_sysreg(0, tpidrro_el0);
0265 }
0266 }
0267
0268 static void flush_tagged_addr_state(void)
0269 {
0270 if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI))
0271 clear_thread_flag(TIF_TAGGED_ADDR);
0272 }
0273
0274 void flush_thread(void)
0275 {
0276 fpsimd_flush_thread();
0277 tls_thread_flush();
0278 flush_ptrace_hw_breakpoint(current);
0279 flush_tagged_addr_state();
0280 }
0281
0282 void release_thread(struct task_struct *dead_task)
0283 {
0284 }
0285
0286 void arch_release_task_struct(struct task_struct *tsk)
0287 {
0288 fpsimd_release_task(tsk);
0289 }
0290
0291 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
0292 {
0293 if (current->mm)
0294 fpsimd_preserve_current_state();
0295 *dst = *src;
0296
0297
0298 BUILD_BUG_ON(!IS_ENABLED(CONFIG_THREAD_INFO_IN_TASK));
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309 dst->thread.sve_state = NULL;
0310 clear_tsk_thread_flag(dst, TIF_SVE);
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320 if (thread_za_enabled(&src->thread)) {
0321 dst->thread.sve_state = kzalloc(sve_state_size(src),
0322 GFP_KERNEL);
0323 if (!dst->thread.sve_state)
0324 return -ENOMEM;
0325 dst->thread.za_state = kmemdup(src->thread.za_state,
0326 za_state_size(src),
0327 GFP_KERNEL);
0328 if (!dst->thread.za_state) {
0329 kfree(dst->thread.sve_state);
0330 dst->thread.sve_state = NULL;
0331 return -ENOMEM;
0332 }
0333 } else {
0334 dst->thread.za_state = NULL;
0335 clear_tsk_thread_flag(dst, TIF_SME);
0336 }
0337
0338
0339 clear_tsk_thread_flag(dst, TIF_MTE_ASYNC_FAULT);
0340
0341 return 0;
0342 }
0343
0344 asmlinkage void ret_from_fork(void) asm("ret_from_fork");
0345
0346 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
0347 {
0348 unsigned long clone_flags = args->flags;
0349 unsigned long stack_start = args->stack;
0350 unsigned long tls = args->tls;
0351 struct pt_regs *childregs = task_pt_regs(p);
0352
0353 memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
0354
0355
0356
0357
0358
0359
0360
0361
0362 fpsimd_flush_task_state(p);
0363
0364 ptrauth_thread_init_kernel(p);
0365
0366 if (likely(!args->fn)) {
0367 *childregs = *current_pt_regs();
0368 childregs->regs[0] = 0;
0369
0370
0371
0372
0373
0374 *task_user_tls(p) = read_sysreg(tpidr_el0);
0375 if (system_supports_tpidr2())
0376 p->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0);
0377
0378 if (stack_start) {
0379 if (is_compat_thread(task_thread_info(p)))
0380 childregs->compat_sp = stack_start;
0381 else
0382 childregs->sp = stack_start;
0383 }
0384
0385
0386
0387
0388
0389 if (clone_flags & CLONE_SETTLS) {
0390 p->thread.uw.tp_value = tls;
0391 p->thread.tpidr2_el0 = 0;
0392 }
0393 } else {
0394
0395
0396
0397
0398
0399
0400
0401 memset(childregs, 0, sizeof(struct pt_regs));
0402 childregs->pstate = PSR_MODE_EL1h | PSR_IL_BIT;
0403
0404 p->thread.cpu_context.x19 = (unsigned long)args->fn;
0405 p->thread.cpu_context.x20 = (unsigned long)args->fn_arg;
0406 }
0407 p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
0408 p->thread.cpu_context.sp = (unsigned long)childregs;
0409
0410
0411
0412
0413 p->thread.cpu_context.fp = (unsigned long)childregs->stackframe;
0414
0415 ptrace_hw_copy_thread(p);
0416
0417 return 0;
0418 }
0419
0420 void tls_preserve_current_state(void)
0421 {
0422 *task_user_tls(current) = read_sysreg(tpidr_el0);
0423 if (system_supports_tpidr2() && !is_compat_task())
0424 current->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0);
0425 }
0426
0427 static void tls_thread_switch(struct task_struct *next)
0428 {
0429 tls_preserve_current_state();
0430
0431 if (is_compat_thread(task_thread_info(next)))
0432 write_sysreg(next->thread.uw.tp_value, tpidrro_el0);
0433 else if (!arm64_kernel_unmapped_at_el0())
0434 write_sysreg(0, tpidrro_el0);
0435
0436 write_sysreg(*task_user_tls(next), tpidr_el0);
0437 if (system_supports_tpidr2())
0438 write_sysreg_s(next->thread.tpidr2_el0, SYS_TPIDR2_EL0);
0439 }
0440
0441
0442
0443
0444
0445 static void ssbs_thread_switch(struct task_struct *next)
0446 {
0447
0448
0449
0450
0451 if (unlikely(next->flags & PF_KTHREAD))
0452 return;
0453
0454
0455
0456
0457
0458 if (cpus_have_const_cap(ARM64_SSBS))
0459 return;
0460
0461 spectre_v4_enable_task_mitigation(next);
0462 }
0463
0464
0465
0466
0467
0468
0469
0470
0471 DEFINE_PER_CPU(struct task_struct *, __entry_task);
0472
0473 static void entry_task_switch(struct task_struct *next)
0474 {
0475 __this_cpu_write(__entry_task, next);
0476 }
0477
0478
0479
0480
0481
0482
0483 static void erratum_1418040_thread_switch(struct task_struct *next)
0484 {
0485 if (!IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040) ||
0486 !this_cpu_has_cap(ARM64_WORKAROUND_1418040))
0487 return;
0488
0489 if (is_compat_thread(task_thread_info(next)))
0490 sysreg_clear_set(cntkctl_el1, ARCH_TIMER_USR_VCT_ACCESS_EN, 0);
0491 else
0492 sysreg_clear_set(cntkctl_el1, 0, ARCH_TIMER_USR_VCT_ACCESS_EN);
0493 }
0494
0495 static void erratum_1418040_new_exec(void)
0496 {
0497 preempt_disable();
0498 erratum_1418040_thread_switch(current);
0499 preempt_enable();
0500 }
0501
0502
0503
0504
0505
0506
0507
0508 void update_sctlr_el1(u64 sctlr)
0509 {
0510
0511
0512
0513
0514 sysreg_clear_set(sctlr_el1, SCTLR_USER_MASK & ~SCTLR_ELx_ENIA, sctlr);
0515
0516
0517 isb();
0518 }
0519
0520
0521
0522
0523 __notrace_funcgraph __sched
0524 struct task_struct *__switch_to(struct task_struct *prev,
0525 struct task_struct *next)
0526 {
0527 struct task_struct *last;
0528
0529 fpsimd_thread_switch(next);
0530 tls_thread_switch(next);
0531 hw_breakpoint_thread_switch(next);
0532 contextidr_thread_switch(next);
0533 entry_task_switch(next);
0534 ssbs_thread_switch(next);
0535 erratum_1418040_thread_switch(next);
0536 ptrauth_thread_switch_user(next);
0537
0538
0539
0540
0541
0542
0543
0544 dsb(ish);
0545
0546
0547
0548
0549
0550
0551 mte_thread_switch(next);
0552
0553 if (prev->thread.sctlr_user != next->thread.sctlr_user)
0554 update_sctlr_el1(next->thread.sctlr_user);
0555
0556
0557 last = cpu_switch_to(prev, next);
0558
0559 return last;
0560 }
0561
0562 struct wchan_info {
0563 unsigned long pc;
0564 int count;
0565 };
0566
0567 static bool get_wchan_cb(void *arg, unsigned long pc)
0568 {
0569 struct wchan_info *wchan_info = arg;
0570
0571 if (!in_sched_functions(pc)) {
0572 wchan_info->pc = pc;
0573 return false;
0574 }
0575 return wchan_info->count++ < 16;
0576 }
0577
0578 unsigned long __get_wchan(struct task_struct *p)
0579 {
0580 struct wchan_info wchan_info = {
0581 .pc = 0,
0582 .count = 0,
0583 };
0584
0585 if (!try_get_task_stack(p))
0586 return 0;
0587
0588 arch_stack_walk(get_wchan_cb, &wchan_info, p, NULL);
0589
0590 put_task_stack(p);
0591
0592 return wchan_info.pc;
0593 }
0594
0595 unsigned long arch_align_stack(unsigned long sp)
0596 {
0597 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
0598 sp -= get_random_int() & ~PAGE_MASK;
0599 return sp & ~0xf;
0600 }
0601
0602 #ifdef CONFIG_COMPAT
0603 int compat_elf_check_arch(const struct elf32_hdr *hdr)
0604 {
0605 if (!system_supports_32bit_el0())
0606 return false;
0607
0608 if ((hdr)->e_machine != EM_ARM)
0609 return false;
0610
0611 if (!((hdr)->e_flags & EF_ARM_EABI_MASK))
0612 return false;
0613
0614
0615
0616
0617
0618
0619 return !static_branch_unlikely(&arm64_mismatched_32bit_el0) ||
0620 !dl_task_check_affinity(current, system_32bit_el0_cpumask());
0621 }
0622 #endif
0623
0624
0625
0626
0627 void arch_setup_new_exec(void)
0628 {
0629 unsigned long mmflags = 0;
0630
0631 if (is_compat_task()) {
0632 mmflags = MMCF_AARCH32;
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643 if (static_branch_unlikely(&arm64_mismatched_32bit_el0))
0644 force_compatible_cpus_allowed_ptr(current);
0645 } else if (static_branch_unlikely(&arm64_mismatched_32bit_el0)) {
0646 relax_compatible_cpus_allowed_ptr(current);
0647 }
0648
0649 current->mm->context.flags = mmflags;
0650 ptrauth_thread_init_user();
0651 mte_thread_init_user();
0652 erratum_1418040_new_exec();
0653
0654 if (task_spec_ssb_noexec(current)) {
0655 arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS,
0656 PR_SPEC_ENABLE);
0657 }
0658 }
0659
0660 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
0661
0662
0663
0664 static unsigned int tagged_addr_disabled;
0665
0666 long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg)
0667 {
0668 unsigned long valid_mask = PR_TAGGED_ADDR_ENABLE;
0669 struct thread_info *ti = task_thread_info(task);
0670
0671 if (is_compat_thread(ti))
0672 return -EINVAL;
0673
0674 if (system_supports_mte())
0675 valid_mask |= PR_MTE_TCF_SYNC | PR_MTE_TCF_ASYNC \
0676 | PR_MTE_TAG_MASK;
0677
0678 if (arg & ~valid_mask)
0679 return -EINVAL;
0680
0681
0682
0683
0684
0685 if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled)
0686 return -EINVAL;
0687
0688 if (set_mte_ctrl(task, arg) != 0)
0689 return -EINVAL;
0690
0691 update_ti_thread_flag(ti, TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE);
0692
0693 return 0;
0694 }
0695
0696 long get_tagged_addr_ctrl(struct task_struct *task)
0697 {
0698 long ret = 0;
0699 struct thread_info *ti = task_thread_info(task);
0700
0701 if (is_compat_thread(ti))
0702 return -EINVAL;
0703
0704 if (test_ti_thread_flag(ti, TIF_TAGGED_ADDR))
0705 ret = PR_TAGGED_ADDR_ENABLE;
0706
0707 ret |= get_mte_ctrl(task);
0708
0709 return ret;
0710 }
0711
0712
0713
0714
0715
0716
0717
0718 static struct ctl_table tagged_addr_sysctl_table[] = {
0719 {
0720 .procname = "tagged_addr_disabled",
0721 .mode = 0644,
0722 .data = &tagged_addr_disabled,
0723 .maxlen = sizeof(int),
0724 .proc_handler = proc_dointvec_minmax,
0725 .extra1 = SYSCTL_ZERO,
0726 .extra2 = SYSCTL_ONE,
0727 },
0728 { }
0729 };
0730
0731 static int __init tagged_addr_init(void)
0732 {
0733 if (!register_sysctl("abi", tagged_addr_sysctl_table))
0734 return -EINVAL;
0735 return 0;
0736 }
0737
0738 core_initcall(tagged_addr_init);
0739 #endif
0740
0741 #ifdef CONFIG_BINFMT_ELF
0742 int arch_elf_adjust_prot(int prot, const struct arch_elf_state *state,
0743 bool has_interp, bool is_interp)
0744 {
0745
0746
0747
0748
0749
0750 if (is_interp != has_interp)
0751 return prot;
0752
0753 if (!(state->flags & ARM64_ELF_BTI))
0754 return prot;
0755
0756 if (prot & PROT_EXEC)
0757 prot |= PROT_BTI;
0758
0759 return prot;
0760 }
0761 #endif