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
10 #include <stdarg.h>
11
12 #include <linux/compat.h>
13 #include <linux/efi.h>
14 #include <linux/elf.h>
15 #include <linux/export.h>
16 #include <linux/sched.h>
17 #include <linux/sched/debug.h>
18 #include <linux/sched/task.h>
19 #include <linux/sched/task_stack.h>
20 #include <linux/kernel.h>
21 #include <linux/lockdep.h>
22 #include <linux/mman.h>
23 #include <linux/mm.h>
24 #include <linux/nospec.h>
25 #include <linux/stddef.h>
26 #include <linux/sysctl.h>
27 #include <linux/unistd.h>
28 #include <linux/user.h>
29 #include <linux/delay.h>
30 #include <linux/reboot.h>
31 #include <linux/interrupt.h>
32 #include <linux/init.h>
33 #include <linux/cpu.h>
34 #include <linux/elfcore.h>
35 #include <linux/pm.h>
36 #include <linux/tick.h>
37 #include <linux/utsname.h>
38 #include <linux/uaccess.h>
39 #include <linux/random.h>
40 #include <linux/hw_breakpoint.h>
41 #include <linux/personality.h>
42 #include <linux/notifier.h>
43 #include <trace/events/power.h>
44 #include <linux/percpu.h>
45 #include <linux/thread_info.h>
46 #include <linux/prctl.h>
47 #include <trace/hooks/fpsimd.h>
48
49 #include <asm/alternative.h>
50 #include <asm/arch_gicv3.h>
51 #include <asm/compat.h>
52 #include <asm/cpufeature.h>
53 #include <asm/cacheflush.h>
54 #include <asm/exec.h>
55 #include <asm/fpsimd.h>
56 #include <asm/mmu_context.h>
57 #include <asm/mte.h>
58 #include <asm/processor.h>
59 #include <asm/pointer_auth.h>
60 #include <asm/stacktrace.h>
61
62 #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK)
63 #include <linux/stackprotector.h>
64 unsigned long __stack_chk_guard __ro_after_init;
65 EXPORT_SYMBOL(__stack_chk_guard);
66 #endif
67
68 /*
69 * Function pointers to optional machine specific functions
70 */
71 void (*pm_power_off)(void);
72 EXPORT_SYMBOL_GPL(pm_power_off);
73
__cpu_do_idle(void)74 static void noinstr __cpu_do_idle(void)
75 {
76 dsb(sy);
77 wfi();
78 }
79
__cpu_do_idle_irqprio(void)80 static void noinstr __cpu_do_idle_irqprio(void)
81 {
82 unsigned long pmr;
83 unsigned long daif_bits;
84
85 daif_bits = read_sysreg(daif);
86 write_sysreg(daif_bits | PSR_I_BIT, daif);
87
88 /*
89 * Unmask PMR before going idle to make sure interrupts can
90 * be raised.
91 */
92 pmr = gic_read_pmr();
93 gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
94
95 __cpu_do_idle();
96
97 gic_write_pmr(pmr);
98 write_sysreg(daif_bits, daif);
99 }
100
101 /*
102 * cpu_do_idle()
103 *
104 * Idle the processor (wait for interrupt).
105 *
106 * If the CPU supports priority masking we must do additional work to
107 * ensure that interrupts are not masked at the PMR (because the core will
108 * not wake up if we block the wake up signal in the interrupt controller).
109 */
cpu_do_idle(void)110 void noinstr cpu_do_idle(void)
111 {
112 if (system_uses_irq_prio_masking())
113 __cpu_do_idle_irqprio();
114 else
115 __cpu_do_idle();
116 }
117
118 /*
119 * This is our default idle handler.
120 */
arch_cpu_idle(void)121 void noinstr arch_cpu_idle(void)
122 {
123 /*
124 * This should do all the clock switching and wait for interrupt
125 * tricks
126 */
127 cpu_do_idle();
128 raw_local_irq_enable();
129 }
130
arch_cpu_idle_enter(void)131 void arch_cpu_idle_enter(void)
132 {
133 idle_notifier_call_chain(IDLE_START);
134 }
135
arch_cpu_idle_exit(void)136 void arch_cpu_idle_exit(void)
137 {
138 idle_notifier_call_chain(IDLE_END);
139 }
140
141 #ifdef CONFIG_HOTPLUG_CPU
arch_cpu_idle_dead(void)142 void arch_cpu_idle_dead(void)
143 {
144 cpu_die();
145 }
146 #endif
147
148 /*
149 * Called by kexec, immediately prior to machine_kexec().
150 *
151 * This must completely disable all secondary CPUs; simply causing those CPUs
152 * to execute e.g. a RAM-based pin loop is not sufficient. This allows the
153 * kexec'd kernel to use any and all RAM as it sees fit, without having to
154 * avoid any code or data used by any SW CPU pin loop. The CPU hotplug
155 * functionality embodied in smpt_shutdown_nonboot_cpus() to achieve this.
156 */
machine_shutdown(void)157 void machine_shutdown(void)
158 {
159 smp_shutdown_nonboot_cpus(reboot_cpu);
160 }
161
162 /*
163 * Halting simply requires that the secondary CPUs stop performing any
164 * activity (executing tasks, handling interrupts). smp_send_stop()
165 * achieves this.
166 */
machine_halt(void)167 void machine_halt(void)
168 {
169 local_irq_disable();
170 smp_send_stop();
171 while (1);
172 }
173
174 /*
175 * Power-off simply requires that the secondary CPUs stop performing any
176 * activity (executing tasks, handling interrupts). smp_send_stop()
177 * achieves this. When the system power is turned off, it will take all CPUs
178 * with it.
179 */
machine_power_off(void)180 void machine_power_off(void)
181 {
182 local_irq_disable();
183 smp_send_stop();
184 if (pm_power_off)
185 pm_power_off();
186 }
187
188 /*
189 * Restart requires that the secondary CPUs stop performing any activity
190 * while the primary CPU resets the system. Systems with multiple CPUs must
191 * provide a HW restart implementation, to ensure that all CPUs reset at once.
192 * This is required so that any code running after reset on the primary CPU
193 * doesn't have to co-ordinate with other CPUs to ensure they aren't still
194 * executing pre-reset code, and using RAM that the primary CPU's code wishes
195 * to use. Implementing such co-ordination would be essentially impossible.
196 */
machine_restart(char * cmd)197 void machine_restart(char *cmd)
198 {
199 /* Disable interrupts first */
200 local_irq_disable();
201 smp_send_stop();
202
203 do_kernel_pre_restart(cmd);
204
205 /*
206 * UpdateCapsule() depends on the system being reset via
207 * ResetSystem().
208 */
209 if (efi_enabled(EFI_RUNTIME_SERVICES))
210 efi_reboot(reboot_mode, NULL);
211
212 /* Now call the architecture specific reboot code. */
213 do_kernel_restart(cmd);
214
215 /*
216 * Whoops - the architecture was unable to reboot.
217 */
218 printk("Reboot failed -- System halted\n");
219 while (1);
220 }
221
222 #define bstr(suffix, str) [PSR_BTYPE_ ## suffix >> PSR_BTYPE_SHIFT] = str
223 static const char *const btypes[] = {
224 bstr(NONE, "--"),
225 bstr( JC, "jc"),
226 bstr( C, "-c"),
227 bstr( J , "j-")
228 };
229 #undef bstr
230
print_pstate(struct pt_regs * regs)231 static void print_pstate(struct pt_regs *regs)
232 {
233 u64 pstate = regs->pstate;
234
235 if (compat_user_mode(regs)) {
236 printk("pstate: %08llx (%c%c%c%c %c %s %s %c%c%c)\n",
237 pstate,
238 pstate & PSR_AA32_N_BIT ? 'N' : 'n',
239 pstate & PSR_AA32_Z_BIT ? 'Z' : 'z',
240 pstate & PSR_AA32_C_BIT ? 'C' : 'c',
241 pstate & PSR_AA32_V_BIT ? 'V' : 'v',
242 pstate & PSR_AA32_Q_BIT ? 'Q' : 'q',
243 pstate & PSR_AA32_T_BIT ? "T32" : "A32",
244 pstate & PSR_AA32_E_BIT ? "BE" : "LE",
245 pstate & PSR_AA32_A_BIT ? 'A' : 'a',
246 pstate & PSR_AA32_I_BIT ? 'I' : 'i',
247 pstate & PSR_AA32_F_BIT ? 'F' : 'f');
248 } else {
249 const char *btype_str = btypes[(pstate & PSR_BTYPE_MASK) >>
250 PSR_BTYPE_SHIFT];
251
252 printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO %cTCO BTYPE=%s)\n",
253 pstate,
254 pstate & PSR_N_BIT ? 'N' : 'n',
255 pstate & PSR_Z_BIT ? 'Z' : 'z',
256 pstate & PSR_C_BIT ? 'C' : 'c',
257 pstate & PSR_V_BIT ? 'V' : 'v',
258 pstate & PSR_D_BIT ? 'D' : 'd',
259 pstate & PSR_A_BIT ? 'A' : 'a',
260 pstate & PSR_I_BIT ? 'I' : 'i',
261 pstate & PSR_F_BIT ? 'F' : 'f',
262 pstate & PSR_PAN_BIT ? '+' : '-',
263 pstate & PSR_UAO_BIT ? '+' : '-',
264 pstate & PSR_TCO_BIT ? '+' : '-',
265 btype_str);
266 }
267 }
268
269 /*
270 * dump a block of kernel memory from around the given address
271 */
show_data(unsigned long addr,int nbytes,const char * name)272 static void show_data(unsigned long addr, int nbytes, const char *name)
273 {
274 int i, j;
275 int nlines;
276 u32 *p;
277
278 /*
279 * don't attempt to dump non-kernel addresses or
280 * values that are probably just small negative numbers
281 */
282 if (addr < PAGE_OFFSET || addr > -4096UL)
283 return;
284
285 printk("\n%s: %#lx:\n", name, addr + nbytes / 2);
286
287 /*
288 * round address down to a 32 bit boundary
289 * and always dump a multiple of 32 bytes
290 */
291 p = (u32 *)(addr & ~(sizeof(u32) - 1));
292 nbytes += (addr & (sizeof(u32) - 1));
293 nlines = (nbytes + 31) / 32;
294
295
296 for (i = 0; i < nlines; i++) {
297 /*
298 * just display low 16 bits of address to keep
299 * each line of the dump < 80 characters
300 */
301 if (i == (nlines / 2))
302 printk("%04lx*", (unsigned long)p & 0xffff);
303 else
304 printk("%04lx ", (unsigned long)p & 0xffff);
305
306 for (j = 0; j < 8; j++) {
307 u32 data;
308
309 if (aarch64_insn_read((void *)p, &data)) {
310 pr_cont(" ********");
311 } else {
312 pr_cont(" %08x", data);
313 }
314 ++p;
315 }
316 pr_cont("\n");
317 }
318 }
319
show_extra_register_data(struct pt_regs * regs,int nbytes)320 static void show_extra_register_data(struct pt_regs *regs, int nbytes)
321 {
322 mm_segment_t fs;
323 unsigned int i;
324
325 fs = get_fs();
326 set_fs(KERNEL_DS);
327 show_data(regs->pc - nbytes, nbytes * 2, "PC");
328 show_data(regs->regs[30] - nbytes, nbytes * 2, "LR");
329 show_data(regs->sp - nbytes, nbytes * 2, "SP");
330 for (i = 0; i < 30; i++) {
331 char name[4];
332 snprintf(name, sizeof(name), "X%u", i);
333 show_data(regs->regs[i] - nbytes, nbytes * 2, name);
334 }
335 set_fs(fs);
336 }
337
__show_regs(struct pt_regs * regs)338 void __show_regs(struct pt_regs *regs)
339 {
340 int i, top_reg;
341 u64 lr, sp;
342
343 if (compat_user_mode(regs)) {
344 lr = regs->compat_lr;
345 sp = regs->compat_sp;
346 top_reg = 12;
347 } else {
348 lr = regs->regs[30];
349 sp = regs->sp;
350 top_reg = 29;
351 }
352
353 show_regs_print_info(KERN_DEFAULT);
354 print_pstate(regs);
355
356 if (!user_mode(regs)) {
357 printk("pc : %pS\n", (void *)regs->pc);
358 printk("lr : %pS\n", (void *)ptrauth_strip_insn_pac(lr));
359 } else {
360 printk("pc : %016llx\n", regs->pc);
361 printk("lr : %016llx\n", lr);
362 }
363
364 printk("sp : %016llx\n", sp);
365
366 if (system_uses_irq_prio_masking())
367 printk("pmr_save: %08llx\n", regs->pmr_save);
368
369 i = top_reg;
370
371 while (i >= 0) {
372 printk("x%-2d: %016llx ", i, regs->regs[i]);
373 i--;
374
375 if (i % 2 == 0) {
376 pr_cont("x%-2d: %016llx ", i, regs->regs[i]);
377 i--;
378 }
379
380 pr_cont("\n");
381 }
382 }
383
show_regs(struct pt_regs * regs)384 void show_regs(struct pt_regs * regs)
385 {
386 __show_regs(regs);
387 dump_backtrace(regs, NULL, KERN_DEFAULT);
388
389 if (!user_mode(regs))
390 show_extra_register_data(regs, 512);
391 }
392 EXPORT_SYMBOL_GPL(show_regs);
393
tls_thread_flush(void)394 static void tls_thread_flush(void)
395 {
396 write_sysreg(0, tpidr_el0);
397
398 if (is_compat_task()) {
399 current->thread.uw.tp_value = 0;
400
401 /*
402 * We need to ensure ordering between the shadow state and the
403 * hardware state, so that we don't corrupt the hardware state
404 * with a stale shadow state during context switch.
405 */
406 barrier();
407 write_sysreg(0, tpidrro_el0);
408 }
409 }
410
flush_tagged_addr_state(void)411 static void flush_tagged_addr_state(void)
412 {
413 if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI))
414 clear_thread_flag(TIF_TAGGED_ADDR);
415 }
416
flush_thread(void)417 void flush_thread(void)
418 {
419 fpsimd_flush_thread();
420 tls_thread_flush();
421 flush_ptrace_hw_breakpoint(current);
422 flush_tagged_addr_state();
423 }
424
release_thread(struct task_struct * dead_task)425 void release_thread(struct task_struct *dead_task)
426 {
427 }
428
arch_release_task_struct(struct task_struct * tsk)429 void arch_release_task_struct(struct task_struct *tsk)
430 {
431 fpsimd_release_task(tsk);
432 }
433
arch_dup_task_struct(struct task_struct * dst,struct task_struct * src)434 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
435 {
436 if (current->mm)
437 fpsimd_preserve_current_state();
438 *dst = *src;
439
440 /* We rely on the above assignment to initialize dst's thread_flags: */
441 BUILD_BUG_ON(!IS_ENABLED(CONFIG_THREAD_INFO_IN_TASK));
442
443 /*
444 * Detach src's sve_state (if any) from dst so that it does not
445 * get erroneously used or freed prematurely. dst's sve_state
446 * will be allocated on demand later on if dst uses SVE.
447 * For consistency, also clear TIF_SVE here: this could be done
448 * later in copy_process(), but to avoid tripping up future
449 * maintainers it is best not to leave TIF_SVE and sve_state in
450 * an inconsistent state, even temporarily.
451 */
452 dst->thread.sve_state = NULL;
453 clear_tsk_thread_flag(dst, TIF_SVE);
454
455 /* clear any pending asynchronous tag fault raised by the parent */
456 clear_tsk_thread_flag(dst, TIF_MTE_ASYNC_FAULT);
457
458 return 0;
459 }
460
461 asmlinkage void ret_from_fork(void) asm("ret_from_fork");
462
copy_thread(unsigned long clone_flags,unsigned long stack_start,unsigned long stk_sz,struct task_struct * p,unsigned long tls)463 int copy_thread(unsigned long clone_flags, unsigned long stack_start,
464 unsigned long stk_sz, struct task_struct *p, unsigned long tls)
465 {
466 struct pt_regs *childregs = task_pt_regs(p);
467
468 memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
469
470 /*
471 * In case p was allocated the same task_struct pointer as some
472 * other recently-exited task, make sure p is disassociated from
473 * any cpu that may have run that now-exited task recently.
474 * Otherwise we could erroneously skip reloading the FPSIMD
475 * registers for p.
476 */
477 fpsimd_flush_task_state(p);
478
479 ptrauth_thread_init_kernel(p);
480
481 if (likely(!(p->flags & (PF_KTHREAD | PF_IO_WORKER)))) {
482 *childregs = *current_pt_regs();
483 childregs->regs[0] = 0;
484
485 /*
486 * Read the current TLS pointer from tpidr_el0 as it may be
487 * out-of-sync with the saved value.
488 */
489 *task_user_tls(p) = read_sysreg(tpidr_el0);
490
491 if (stack_start) {
492 if (is_compat_thread(task_thread_info(p)))
493 childregs->compat_sp = stack_start;
494 else
495 childregs->sp = stack_start;
496 }
497
498 /*
499 * If a TLS pointer was passed to clone, use it for the new
500 * thread.
501 */
502 if (clone_flags & CLONE_SETTLS)
503 p->thread.uw.tp_value = tls;
504 } else {
505 /*
506 * A kthread has no context to ERET to, so ensure any buggy
507 * ERET is treated as an illegal exception return.
508 *
509 * When a user task is created from a kthread, childregs will
510 * be initialized by start_thread() or start_compat_thread().
511 */
512 memset(childregs, 0, sizeof(struct pt_regs));
513 childregs->pstate = PSR_MODE_EL1h | PSR_IL_BIT;
514
515 p->thread.cpu_context.x19 = stack_start;
516 p->thread.cpu_context.x20 = stk_sz;
517 }
518 p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
519 p->thread.cpu_context.sp = (unsigned long)childregs;
520
521 ptrace_hw_copy_thread(p);
522
523 return 0;
524 }
525
tls_preserve_current_state(void)526 void tls_preserve_current_state(void)
527 {
528 *task_user_tls(current) = read_sysreg(tpidr_el0);
529 }
530
tls_thread_switch(struct task_struct * next)531 static void tls_thread_switch(struct task_struct *next)
532 {
533 tls_preserve_current_state();
534
535 if (is_compat_thread(task_thread_info(next)))
536 write_sysreg(next->thread.uw.tp_value, tpidrro_el0);
537 else if (!arm64_kernel_unmapped_at_el0())
538 write_sysreg(0, tpidrro_el0);
539
540 write_sysreg(*task_user_tls(next), tpidr_el0);
541 }
542
543 /* Restore the UAO state depending on next's addr_limit */
uao_thread_switch(struct task_struct * next)544 void uao_thread_switch(struct task_struct *next)
545 {
546 if (IS_ENABLED(CONFIG_ARM64_UAO)) {
547 if (task_thread_info(next)->addr_limit == KERNEL_DS)
548 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
549 else
550 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO));
551 }
552 }
553
554 /*
555 * Force SSBS state on context-switch, since it may be lost after migrating
556 * from a CPU which treats the bit as RES0 in a heterogeneous system.
557 */
ssbs_thread_switch(struct task_struct * next)558 static void ssbs_thread_switch(struct task_struct *next)
559 {
560 /*
561 * Nothing to do for kernel threads, but 'regs' may be junk
562 * (e.g. idle task) so check the flags and bail early.
563 */
564 if (unlikely(next->flags & PF_KTHREAD))
565 return;
566
567 /*
568 * If all CPUs implement the SSBS extension, then we just need to
569 * context-switch the PSTATE field.
570 */
571 if (cpus_have_const_cap(ARM64_SSBS))
572 return;
573
574 spectre_v4_enable_task_mitigation(next);
575 }
576
577 /*
578 * We store our current task in sp_el0, which is clobbered by userspace. Keep a
579 * shadow copy so that we can restore this upon entry from userspace.
580 *
581 * This is *only* for exception entry from EL0, and is not valid until we
582 * __switch_to() a user task.
583 */
584 DEFINE_PER_CPU(struct task_struct *, __entry_task);
585
entry_task_switch(struct task_struct * next)586 static void entry_task_switch(struct task_struct *next)
587 {
588 __this_cpu_write(__entry_task, next);
589 }
590
591 /*
592 * ARM erratum 1418040 handling, affecting the 32bit view of CNTVCT.
593 * Ensure access is disabled when switching to a 32bit task, ensure
594 * access is enabled when switching to a 64bit task.
595 */
erratum_1418040_thread_switch(struct task_struct * next)596 static void erratum_1418040_thread_switch(struct task_struct *next)
597 {
598 if (!IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040) ||
599 !this_cpu_has_cap(ARM64_WORKAROUND_1418040))
600 return;
601
602 if (is_compat_thread(task_thread_info(next)))
603 sysreg_clear_set(cntkctl_el1, ARCH_TIMER_USR_VCT_ACCESS_EN, 0);
604 else
605 sysreg_clear_set(cntkctl_el1, 0, ARCH_TIMER_USR_VCT_ACCESS_EN);
606 }
607
erratum_1418040_new_exec(void)608 static void erratum_1418040_new_exec(void)
609 {
610 preempt_disable();
611 erratum_1418040_thread_switch(current);
612 preempt_enable();
613 }
614
615 /*
616 * __switch_to() checks current->thread.sctlr_user as an optimisation. Therefore
617 * this function must be called with preemption disabled and the update to
618 * sctlr_user must be made in the same preemption disabled block so that
619 * __switch_to() does not see the variable update before the SCTLR_EL1 one.
620 */
update_sctlr_el1(u64 sctlr)621 void update_sctlr_el1(u64 sctlr)
622 {
623 /*
624 * EnIA must not be cleared while in the kernel as this is necessary for
625 * in-kernel PAC. It will be cleared on kernel exit if needed.
626 */
627 sysreg_clear_set(sctlr_el1, SCTLR_USER_MASK & ~SCTLR_ELx_ENIA, sctlr);
628
629 /* ISB required for the kernel uaccess routines when setting TCF0. */
630 isb();
631 }
632
633 /*
634 * Thread switching.
635 */
__switch_to(struct task_struct * prev,struct task_struct * next)636 __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
637 struct task_struct *next)
638 {
639 struct task_struct *last;
640
641 fpsimd_thread_switch(next);
642 tls_thread_switch(next);
643 hw_breakpoint_thread_switch(next);
644 contextidr_thread_switch(next);
645 entry_task_switch(next);
646 uao_thread_switch(next);
647 ssbs_thread_switch(next);
648 erratum_1418040_thread_switch(next);
649 ptrauth_thread_switch_user(next);
650
651 /*
652 * Complete any pending TLB or cache maintenance on this CPU in case
653 * the thread migrates to a different CPU.
654 * This full barrier is also required by the membarrier system
655 * call.
656 */
657 dsb(ish);
658
659 /*
660 * MTE thread switching must happen after the DSB above to ensure that
661 * any asynchronous tag check faults have been logged in the TFSR*_EL1
662 * registers.
663 */
664 mte_thread_switch(next);
665 /* avoid expensive SCTLR_EL1 accesses if no change */
666 if (prev->thread.sctlr_user != next->thread.sctlr_user)
667 update_sctlr_el1(next->thread.sctlr_user);
668
669 trace_android_vh_is_fpsimd_save(prev, next);
670
671 /* the actual thread switch */
672 last = cpu_switch_to(prev, next);
673
674 return last;
675 }
676
get_wchan(struct task_struct * p)677 unsigned long get_wchan(struct task_struct *p)
678 {
679 struct stackframe frame;
680 unsigned long stack_page, ret = 0;
681 int count = 0;
682 if (!p || p == current || p->state == TASK_RUNNING)
683 return 0;
684
685 stack_page = (unsigned long)try_get_task_stack(p);
686 if (!stack_page)
687 return 0;
688
689 start_backtrace(&frame, thread_saved_fp(p), thread_saved_pc(p));
690
691 do {
692 if (unwind_frame(p, &frame))
693 goto out;
694 if (!in_sched_functions(frame.pc)) {
695 ret = frame.pc;
696 goto out;
697 }
698 } while (count ++ < 16);
699
700 out:
701 put_task_stack(p);
702 return ret;
703 }
704 EXPORT_SYMBOL_GPL(get_wchan);
705
arch_align_stack(unsigned long sp)706 unsigned long arch_align_stack(unsigned long sp)
707 {
708 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
709 sp -= get_random_int() & ~PAGE_MASK;
710 return sp & ~0xf;
711 }
712
713 /*
714 * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY.
715 */
arch_setup_new_exec(void)716 void arch_setup_new_exec(void)
717 {
718 unsigned long mmflags = 0;
719
720 if (is_compat_task()) {
721 mmflags = MMCF_AARCH32;
722
723 /*
724 * Restrict the CPU affinity mask for a 32-bit task so that
725 * it contains only 32-bit-capable CPUs.
726 *
727 * From the perspective of the task, this looks similar to
728 * what would happen if the 64-bit-only CPUs were hot-unplugged
729 * at the point of execve(), although we try a bit harder to
730 * honour the cpuset hierarchy.
731 */
732 if (static_branch_unlikely(&arm64_mismatched_32bit_el0))
733 force_compatible_cpus_allowed_ptr(current);
734 }
735
736 current->mm->context.flags = mmflags;
737 ptrauth_thread_init_user();
738 mte_thread_init_user();
739 erratum_1418040_new_exec();
740
741 if (task_spec_ssb_noexec(current)) {
742 arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS,
743 PR_SPEC_ENABLE);
744 }
745 }
746
747 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
748 /*
749 * Control the relaxed ABI allowing tagged user addresses into the kernel.
750 */
751 static unsigned int tagged_addr_disabled;
752
set_tagged_addr_ctrl(struct task_struct * task,unsigned long arg)753 long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg)
754 {
755 unsigned long valid_mask = PR_TAGGED_ADDR_ENABLE;
756 struct thread_info *ti = task_thread_info(task);
757
758 if (is_compat_thread(ti))
759 return -EINVAL;
760
761 if (system_supports_mte())
762 valid_mask |= PR_MTE_TCF_MASK | PR_MTE_TAG_MASK;
763
764 if (arg & ~valid_mask)
765 return -EINVAL;
766
767 /*
768 * Do not allow the enabling of the tagged address ABI if globally
769 * disabled via sysctl abi.tagged_addr_disabled.
770 */
771 if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled)
772 return -EINVAL;
773
774 if (set_mte_ctrl(task, arg) != 0)
775 return -EINVAL;
776
777 update_ti_thread_flag(ti, TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE);
778
779 return 0;
780 }
781
get_tagged_addr_ctrl(struct task_struct * task)782 long get_tagged_addr_ctrl(struct task_struct *task)
783 {
784 long ret = 0;
785 struct thread_info *ti = task_thread_info(task);
786
787 if (is_compat_thread(ti))
788 return -EINVAL;
789
790 if (test_ti_thread_flag(ti, TIF_TAGGED_ADDR))
791 ret = PR_TAGGED_ADDR_ENABLE;
792
793 ret |= get_mte_ctrl(task);
794
795 return ret;
796 }
797
798 /*
799 * Global sysctl to disable the tagged user addresses support. This control
800 * only prevents the tagged address ABI enabling via prctl() and does not
801 * disable it for tasks that already opted in to the relaxed ABI.
802 */
803
804 static struct ctl_table tagged_addr_sysctl_table[] = {
805 {
806 .procname = "tagged_addr_disabled",
807 .mode = 0644,
808 .data = &tagged_addr_disabled,
809 .maxlen = sizeof(int),
810 .proc_handler = proc_dointvec_minmax,
811 .extra1 = SYSCTL_ZERO,
812 .extra2 = SYSCTL_ONE,
813 },
814 { }
815 };
816
tagged_addr_init(void)817 static int __init tagged_addr_init(void)
818 {
819 if (!register_sysctl("abi", tagged_addr_sysctl_table))
820 return -EINVAL;
821 return 0;
822 }
823
824 core_initcall(tagged_addr_init);
825 #endif /* CONFIG_ARM64_TAGGED_ADDR_ABI */
826
arm64_preempt_schedule_irq(void)827 asmlinkage void __sched arm64_preempt_schedule_irq(void)
828 {
829 lockdep_assert_irqs_disabled();
830
831 /*
832 * Preempting a task from an IRQ means we leave copies of PSTATE
833 * on the stack. cpufeature's enable calls may modify PSTATE, but
834 * resuming one of these preempted tasks would undo those changes.
835 *
836 * Only allow a task to be preempted once cpufeatures have been
837 * enabled.
838 */
839 if (system_capabilities_finalized())
840 preempt_schedule_irq();
841 }
842
843 #ifdef CONFIG_BINFMT_ELF
arch_elf_adjust_prot(int prot,const struct arch_elf_state * state,bool has_interp,bool is_interp)844 int arch_elf_adjust_prot(int prot, const struct arch_elf_state *state,
845 bool has_interp, bool is_interp)
846 {
847 /*
848 * For dynamically linked executables the interpreter is
849 * responsible for setting PROT_BTI on everything except
850 * itself.
851 */
852 if (is_interp != has_interp)
853 return prot;
854
855 if (!(state->flags & ARM64_ELF_BTI))
856 return prot;
857
858 if (prot & PROT_EXEC)
859 prot |= PROT_BTI;
860
861 return prot;
862 }
863 #endif
864