.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Based on arch/arm/kernel/process.c |
---|
3 | 4 | * |
---|
4 | 5 | * Original Copyright (C) 1995 Linus Torvalds |
---|
5 | 6 | * Copyright (C) 1996-2000 Russell King - Converted to ARM. |
---|
6 | 7 | * Copyright (C) 2012 ARM Ltd. |
---|
7 | | - * |
---|
8 | | - * This program is free software; you can redistribute it and/or modify |
---|
9 | | - * it under the terms of the GNU General Public License version 2 as |
---|
10 | | - * published by the Free Software Foundation. |
---|
11 | | - * |
---|
12 | | - * This program is distributed in the hope that it will be useful, |
---|
13 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | | - * GNU General Public License for more details. |
---|
16 | | - * |
---|
17 | | - * You should have received a copy of the GNU General Public License |
---|
18 | | - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | 8 | */ |
---|
20 | 9 | |
---|
21 | 10 | #include <stdarg.h> |
---|
22 | 11 | |
---|
23 | 12 | #include <linux/compat.h> |
---|
24 | 13 | #include <linux/efi.h> |
---|
| 14 | +#include <linux/elf.h> |
---|
25 | 15 | #include <linux/export.h> |
---|
26 | 16 | #include <linux/sched.h> |
---|
27 | 17 | #include <linux/sched/debug.h> |
---|
28 | 18 | #include <linux/sched/task.h> |
---|
29 | 19 | #include <linux/sched/task_stack.h> |
---|
30 | 20 | #include <linux/kernel.h> |
---|
| 21 | +#include <linux/lockdep.h> |
---|
| 22 | +#include <linux/mman.h> |
---|
31 | 23 | #include <linux/mm.h> |
---|
| 24 | +#include <linux/nospec.h> |
---|
32 | 25 | #include <linux/stddef.h> |
---|
33 | 26 | #include <linux/sysctl.h> |
---|
34 | 27 | #include <linux/unistd.h> |
---|
.. | .. |
---|
51 | 44 | #include <linux/percpu.h> |
---|
52 | 45 | #include <linux/thread_info.h> |
---|
53 | 46 | #include <linux/prctl.h> |
---|
| 47 | +#include <trace/hooks/fpsimd.h> |
---|
54 | 48 | |
---|
55 | 49 | #include <asm/alternative.h> |
---|
| 50 | +#include <asm/arch_gicv3.h> |
---|
56 | 51 | #include <asm/compat.h> |
---|
| 52 | +#include <asm/cpufeature.h> |
---|
57 | 53 | #include <asm/cacheflush.h> |
---|
58 | 54 | #include <asm/exec.h> |
---|
59 | 55 | #include <asm/fpsimd.h> |
---|
60 | 56 | #include <asm/mmu_context.h> |
---|
| 57 | +#include <asm/mte.h> |
---|
61 | 58 | #include <asm/processor.h> |
---|
62 | | -#include <asm/scs.h> |
---|
| 59 | +#include <asm/pointer_auth.h> |
---|
63 | 60 | #include <asm/stacktrace.h> |
---|
64 | 61 | |
---|
65 | | -#ifdef CONFIG_STACKPROTECTOR |
---|
| 62 | +#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) |
---|
66 | 63 | #include <linux/stackprotector.h> |
---|
67 | 64 | unsigned long __stack_chk_guard __ro_after_init; |
---|
68 | 65 | EXPORT_SYMBOL(__stack_chk_guard); |
---|
.. | .. |
---|
74 | 71 | void (*pm_power_off)(void); |
---|
75 | 72 | EXPORT_SYMBOL_GPL(pm_power_off); |
---|
76 | 73 | |
---|
77 | | -void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd); |
---|
78 | | -EXPORT_SYMBOL_GPL(arm_pm_restart); |
---|
| 74 | +static void noinstr __cpu_do_idle(void) |
---|
| 75 | +{ |
---|
| 76 | + dsb(sy); |
---|
| 77 | + wfi(); |
---|
| 78 | +} |
---|
| 79 | + |
---|
| 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 | + */ |
---|
| 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 | +} |
---|
79 | 117 | |
---|
80 | 118 | /* |
---|
81 | 119 | * This is our default idle handler. |
---|
82 | 120 | */ |
---|
83 | | -void arch_cpu_idle(void) |
---|
| 121 | +void noinstr arch_cpu_idle(void) |
---|
84 | 122 | { |
---|
85 | 123 | /* |
---|
86 | 124 | * This should do all the clock switching and wait for interrupt |
---|
87 | 125 | * tricks |
---|
88 | 126 | */ |
---|
89 | | - trace_cpu_idle_rcuidle(1, smp_processor_id()); |
---|
90 | 127 | cpu_do_idle(); |
---|
91 | | - local_irq_enable(); |
---|
92 | | - trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id()); |
---|
| 128 | + raw_local_irq_enable(); |
---|
93 | 129 | } |
---|
94 | 130 | |
---|
95 | 131 | void arch_cpu_idle_enter(void) |
---|
.. | .. |
---|
116 | 152 | * to execute e.g. a RAM-based pin loop is not sufficient. This allows the |
---|
117 | 153 | * kexec'd kernel to use any and all RAM as it sees fit, without having to |
---|
118 | 154 | * avoid any code or data used by any SW CPU pin loop. The CPU hotplug |
---|
119 | | - * functionality embodied in disable_nonboot_cpus() to achieve this. |
---|
| 155 | + * functionality embodied in smpt_shutdown_nonboot_cpus() to achieve this. |
---|
120 | 156 | */ |
---|
121 | 157 | void machine_shutdown(void) |
---|
122 | 158 | { |
---|
123 | | - disable_nonboot_cpus(); |
---|
| 159 | + smp_shutdown_nonboot_cpus(reboot_cpu); |
---|
124 | 160 | } |
---|
125 | 161 | |
---|
126 | 162 | /* |
---|
.. | .. |
---|
174 | 210 | efi_reboot(reboot_mode, NULL); |
---|
175 | 211 | |
---|
176 | 212 | /* Now call the architecture specific reboot code. */ |
---|
177 | | - if (arm_pm_restart) |
---|
178 | | - arm_pm_restart(reboot_mode, cmd); |
---|
179 | | - else |
---|
180 | | - do_kernel_restart(cmd); |
---|
| 213 | + do_kernel_restart(cmd); |
---|
181 | 214 | |
---|
182 | 215 | /* |
---|
183 | 216 | * Whoops - the architecture was unable to reboot. |
---|
.. | .. |
---|
185 | 218 | printk("Reboot failed -- System halted\n"); |
---|
186 | 219 | while (1); |
---|
187 | 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 |
---|
188 | 230 | |
---|
189 | 231 | static void print_pstate(struct pt_regs *regs) |
---|
190 | 232 | { |
---|
.. | .. |
---|
204 | 246 | pstate & PSR_AA32_I_BIT ? 'I' : 'i', |
---|
205 | 247 | pstate & PSR_AA32_F_BIT ? 'F' : 'f'); |
---|
206 | 248 | } else { |
---|
207 | | - printk("pstate: %08llx (%c%c%c%c %c%c%c%c %cPAN %cUAO)\n", |
---|
| 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", |
---|
208 | 253 | pstate, |
---|
209 | 254 | pstate & PSR_N_BIT ? 'N' : 'n', |
---|
210 | 255 | pstate & PSR_Z_BIT ? 'Z' : 'z', |
---|
.. | .. |
---|
215 | 260 | pstate & PSR_I_BIT ? 'I' : 'i', |
---|
216 | 261 | pstate & PSR_F_BIT ? 'F' : 'f', |
---|
217 | 262 | pstate & PSR_PAN_BIT ? '+' : '-', |
---|
218 | | - pstate & PSR_UAO_BIT ? '+' : '-'); |
---|
| 263 | + pstate & PSR_UAO_BIT ? '+' : '-', |
---|
| 264 | + pstate & PSR_TCO_BIT ? '+' : '-', |
---|
| 265 | + btype_str); |
---|
219 | 266 | } |
---|
220 | 267 | } |
---|
221 | 268 | |
---|
.. | .. |
---|
258 | 305 | |
---|
259 | 306 | for (j = 0; j < 8; j++) { |
---|
260 | 307 | u32 data; |
---|
261 | | - if (probe_kernel_address(p, data)) { |
---|
| 308 | + |
---|
| 309 | + if (aarch64_insn_read((void *)p, &data)) { |
---|
262 | 310 | pr_cont(" ********"); |
---|
263 | 311 | } else { |
---|
264 | 312 | pr_cont(" %08x", data); |
---|
.. | .. |
---|
307 | 355 | |
---|
308 | 356 | if (!user_mode(regs)) { |
---|
309 | 357 | printk("pc : %pS\n", (void *)regs->pc); |
---|
310 | | - printk("lr : %pS\n", (void *)lr); |
---|
| 358 | + printk("lr : %pS\n", (void *)ptrauth_strip_insn_pac(lr)); |
---|
311 | 359 | } else { |
---|
312 | 360 | printk("pc : %016llx\n", regs->pc); |
---|
313 | 361 | printk("lr : %016llx\n", lr); |
---|
314 | 362 | } |
---|
315 | 363 | |
---|
316 | 364 | printk("sp : %016llx\n", sp); |
---|
| 365 | + |
---|
| 366 | + if (system_uses_irq_prio_masking()) |
---|
| 367 | + printk("pmr_save: %08llx\n", regs->pmr_save); |
---|
317 | 368 | |
---|
318 | 369 | i = top_reg; |
---|
319 | 370 | |
---|
.. | .. |
---|
333 | 384 | void show_regs(struct pt_regs * regs) |
---|
334 | 385 | { |
---|
335 | 386 | __show_regs(regs); |
---|
336 | | - dump_backtrace(regs, NULL); |
---|
| 387 | + dump_backtrace(regs, NULL, KERN_DEFAULT); |
---|
337 | 388 | |
---|
338 | 389 | if (!user_mode(regs)) |
---|
339 | 390 | show_extra_register_data(regs, 512); |
---|
340 | 391 | } |
---|
| 392 | +EXPORT_SYMBOL_GPL(show_regs); |
---|
341 | 393 | |
---|
342 | 394 | static void tls_thread_flush(void) |
---|
343 | 395 | { |
---|
.. | .. |
---|
400 | 452 | dst->thread.sve_state = NULL; |
---|
401 | 453 | clear_tsk_thread_flag(dst, TIF_SVE); |
---|
402 | 454 | |
---|
| 455 | + /* clear any pending asynchronous tag fault raised by the parent */ |
---|
| 456 | + clear_tsk_thread_flag(dst, TIF_MTE_ASYNC_FAULT); |
---|
| 457 | + |
---|
403 | 458 | return 0; |
---|
404 | 459 | } |
---|
405 | 460 | |
---|
406 | 461 | asmlinkage void ret_from_fork(void) asm("ret_from_fork"); |
---|
407 | 462 | |
---|
408 | 463 | int copy_thread(unsigned long clone_flags, unsigned long stack_start, |
---|
409 | | - unsigned long stk_sz, struct task_struct *p) |
---|
| 464 | + unsigned long stk_sz, struct task_struct *p, unsigned long tls) |
---|
410 | 465 | { |
---|
411 | 466 | struct pt_regs *childregs = task_pt_regs(p); |
---|
412 | 467 | |
---|
.. | .. |
---|
421 | 476 | */ |
---|
422 | 477 | fpsimd_flush_task_state(p); |
---|
423 | 478 | |
---|
424 | | - if (likely(!(p->flags & PF_KTHREAD))) { |
---|
| 479 | + ptrauth_thread_init_kernel(p); |
---|
| 480 | + |
---|
| 481 | + if (likely(!(p->flags & (PF_KTHREAD | PF_IO_WORKER)))) { |
---|
425 | 482 | *childregs = *current_pt_regs(); |
---|
426 | 483 | childregs->regs[0] = 0; |
---|
427 | 484 | |
---|
.. | .. |
---|
439 | 496 | } |
---|
440 | 497 | |
---|
441 | 498 | /* |
---|
442 | | - * If a TLS pointer was passed to clone (4th argument), use it |
---|
443 | | - * for the new thread. |
---|
| 499 | + * If a TLS pointer was passed to clone, use it for the new |
---|
| 500 | + * thread. |
---|
444 | 501 | */ |
---|
445 | 502 | if (clone_flags & CLONE_SETTLS) |
---|
446 | | - p->thread.uw.tp_value = childregs->regs[3]; |
---|
| 503 | + p->thread.uw.tp_value = tls; |
---|
447 | 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 | + */ |
---|
448 | 512 | memset(childregs, 0, sizeof(struct pt_regs)); |
---|
449 | | - childregs->pstate = PSR_MODE_EL1h; |
---|
450 | | - if (IS_ENABLED(CONFIG_ARM64_UAO) && |
---|
451 | | - cpus_have_const_cap(ARM64_HAS_UAO)) |
---|
452 | | - childregs->pstate |= PSR_UAO_BIT; |
---|
453 | | - |
---|
454 | | - if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) |
---|
455 | | - set_ssbs_bit(childregs); |
---|
| 513 | + childregs->pstate = PSR_MODE_EL1h | PSR_IL_BIT; |
---|
456 | 514 | |
---|
457 | 515 | p->thread.cpu_context.x19 = stack_start; |
---|
458 | 516 | p->thread.cpu_context.x20 = stk_sz; |
---|
.. | .. |
---|
499 | 557 | */ |
---|
500 | 558 | static void ssbs_thread_switch(struct task_struct *next) |
---|
501 | 559 | { |
---|
502 | | - struct pt_regs *regs = task_pt_regs(next); |
---|
503 | | - |
---|
504 | 560 | /* |
---|
505 | 561 | * Nothing to do for kernel threads, but 'regs' may be junk |
---|
506 | 562 | * (e.g. idle task) so check the flags and bail early. |
---|
.. | .. |
---|
512 | 568 | * If all CPUs implement the SSBS extension, then we just need to |
---|
513 | 569 | * context-switch the PSTATE field. |
---|
514 | 570 | */ |
---|
515 | | - if (cpu_have_feature(cpu_feature(SSBS))) |
---|
| 571 | + if (cpus_have_const_cap(ARM64_SSBS)) |
---|
516 | 572 | return; |
---|
517 | 573 | |
---|
518 | | - /* If the mitigation is enabled, then we leave SSBS clear. */ |
---|
519 | | - if ((arm64_get_ssbd_state() == ARM64_SSBD_FORCE_ENABLE) || |
---|
520 | | - test_tsk_thread_flag(next, TIF_SSBD)) |
---|
521 | | - return; |
---|
522 | | - |
---|
523 | | - if (compat_user_mode(regs)) |
---|
524 | | - set_compat_ssbs_bit(regs); |
---|
525 | | - else if (user_mode(regs)) |
---|
526 | | - set_ssbs_bit(regs); |
---|
| 574 | + spectre_v4_enable_task_mitigation(next); |
---|
527 | 575 | } |
---|
528 | 576 | |
---|
529 | 577 | /* |
---|
.. | .. |
---|
541 | 589 | } |
---|
542 | 590 | |
---|
543 | 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 | + */ |
---|
| 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 | + |
---|
| 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 | + */ |
---|
| 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 | +/* |
---|
544 | 634 | * Thread switching. |
---|
545 | 635 | */ |
---|
546 | 636 | __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev, |
---|
.. | .. |
---|
555 | 645 | entry_task_switch(next); |
---|
556 | 646 | uao_thread_switch(next); |
---|
557 | 647 | ssbs_thread_switch(next); |
---|
558 | | - scs_overflow_check(next); |
---|
| 648 | + erratum_1418040_thread_switch(next); |
---|
| 649 | + ptrauth_thread_switch_user(next); |
---|
559 | 650 | |
---|
560 | 651 | /* |
---|
561 | 652 | * Complete any pending TLB or cache maintenance on this CPU in case |
---|
.. | .. |
---|
564 | 655 | * call. |
---|
565 | 656 | */ |
---|
566 | 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); |
---|
567 | 670 | |
---|
568 | 671 | /* the actual thread switch */ |
---|
569 | 672 | last = cpu_switch_to(prev, next); |
---|
.. | .. |
---|
583 | 686 | if (!stack_page) |
---|
584 | 687 | return 0; |
---|
585 | 688 | |
---|
586 | | - frame.fp = thread_saved_fp(p); |
---|
587 | | - frame.pc = thread_saved_pc(p); |
---|
588 | | -#ifdef CONFIG_FUNCTION_GRAPH_TRACER |
---|
589 | | - frame.graph = p->curr_ret_stack; |
---|
590 | | -#endif |
---|
| 689 | + start_backtrace(&frame, thread_saved_fp(p), thread_saved_pc(p)); |
---|
| 690 | + |
---|
591 | 691 | do { |
---|
592 | 692 | if (unwind_frame(p, &frame)) |
---|
593 | 693 | goto out; |
---|
.. | .. |
---|
601 | 701 | put_task_stack(p); |
---|
602 | 702 | return ret; |
---|
603 | 703 | } |
---|
| 704 | +EXPORT_SYMBOL_GPL(get_wchan); |
---|
604 | 705 | |
---|
605 | 706 | unsigned long arch_align_stack(unsigned long sp) |
---|
606 | 707 | { |
---|
.. | .. |
---|
609 | 710 | return sp & ~0xf; |
---|
610 | 711 | } |
---|
611 | 712 | |
---|
612 | | -unsigned long arch_randomize_brk(struct mm_struct *mm) |
---|
613 | | -{ |
---|
614 | | - if (is_compat_task()) |
---|
615 | | - return randomize_page(mm->brk, SZ_32M); |
---|
616 | | - else |
---|
617 | | - return randomize_page(mm->brk, SZ_1G); |
---|
618 | | -} |
---|
619 | | - |
---|
620 | 713 | /* |
---|
621 | 714 | * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY. |
---|
622 | 715 | */ |
---|
623 | 716 | void arch_setup_new_exec(void) |
---|
624 | 717 | { |
---|
625 | | - current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0; |
---|
| 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 | + } |
---|
626 | 745 | } |
---|
627 | | - |
---|
628 | | -#ifdef CONFIG_GCC_PLUGIN_STACKLEAK |
---|
629 | | -void __used stackleak_check_alloca(unsigned long size) |
---|
630 | | -{ |
---|
631 | | - unsigned long stack_left; |
---|
632 | | - unsigned long current_sp = current_stack_pointer; |
---|
633 | | - struct stack_info info; |
---|
634 | | - |
---|
635 | | - BUG_ON(!on_accessible_stack(current, current_sp, &info)); |
---|
636 | | - |
---|
637 | | - stack_left = current_sp - info.low; |
---|
638 | | - |
---|
639 | | - /* |
---|
640 | | - * There's a good chance we're almost out of stack space if this |
---|
641 | | - * is true. Using panic() over BUG() is more likely to give |
---|
642 | | - * reliable debugging output. |
---|
643 | | - */ |
---|
644 | | - if (size >= stack_left) |
---|
645 | | - panic("alloca() over the kernel stack boundary\n"); |
---|
646 | | -} |
---|
647 | | -EXPORT_SYMBOL(stackleak_check_alloca); |
---|
648 | | -#endif |
---|
649 | 746 | |
---|
650 | 747 | #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI |
---|
651 | 748 | /* |
---|
.. | .. |
---|
653 | 750 | */ |
---|
654 | 751 | static unsigned int tagged_addr_disabled; |
---|
655 | 752 | |
---|
656 | | -long set_tagged_addr_ctrl(unsigned long arg) |
---|
| 753 | +long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg) |
---|
657 | 754 | { |
---|
658 | | - if (is_compat_task()) |
---|
| 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)) |
---|
659 | 759 | return -EINVAL; |
---|
660 | | - if (arg & ~PR_TAGGED_ADDR_ENABLE) |
---|
| 760 | + |
---|
| 761 | + if (system_supports_mte()) |
---|
| 762 | + valid_mask |= PR_MTE_TCF_MASK | PR_MTE_TAG_MASK; |
---|
| 763 | + |
---|
| 764 | + if (arg & ~valid_mask) |
---|
661 | 765 | return -EINVAL; |
---|
662 | 766 | |
---|
663 | 767 | /* |
---|
.. | .. |
---|
667 | 771 | if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled) |
---|
668 | 772 | return -EINVAL; |
---|
669 | 773 | |
---|
670 | | - update_thread_flag(TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE); |
---|
| 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); |
---|
671 | 778 | |
---|
672 | 779 | return 0; |
---|
673 | 780 | } |
---|
674 | 781 | |
---|
675 | | -long get_tagged_addr_ctrl(void) |
---|
| 782 | +long get_tagged_addr_ctrl(struct task_struct *task) |
---|
676 | 783 | { |
---|
677 | | - if (is_compat_task()) |
---|
| 784 | + long ret = 0; |
---|
| 785 | + struct thread_info *ti = task_thread_info(task); |
---|
| 786 | + |
---|
| 787 | + if (is_compat_thread(ti)) |
---|
678 | 788 | return -EINVAL; |
---|
679 | 789 | |
---|
680 | | - if (test_thread_flag(TIF_TAGGED_ADDR)) |
---|
681 | | - return PR_TAGGED_ADDR_ENABLE; |
---|
| 790 | + if (test_ti_thread_flag(ti, TIF_TAGGED_ADDR)) |
---|
| 791 | + ret = PR_TAGGED_ADDR_ENABLE; |
---|
682 | 792 | |
---|
683 | | - return 0; |
---|
| 793 | + ret |= get_mte_ctrl(task); |
---|
| 794 | + |
---|
| 795 | + return ret; |
---|
684 | 796 | } |
---|
685 | 797 | |
---|
686 | 798 | /* |
---|
.. | .. |
---|
688 | 800 | * only prevents the tagged address ABI enabling via prctl() and does not |
---|
689 | 801 | * disable it for tasks that already opted in to the relaxed ABI. |
---|
690 | 802 | */ |
---|
691 | | -static int zero; |
---|
692 | | -static int one = 1; |
---|
693 | 803 | |
---|
694 | 804 | static struct ctl_table tagged_addr_sysctl_table[] = { |
---|
695 | 805 | { |
---|
.. | .. |
---|
698 | 808 | .data = &tagged_addr_disabled, |
---|
699 | 809 | .maxlen = sizeof(int), |
---|
700 | 810 | .proc_handler = proc_dointvec_minmax, |
---|
701 | | - .extra1 = &zero, |
---|
702 | | - .extra2 = &one, |
---|
| 811 | + .extra1 = SYSCTL_ZERO, |
---|
| 812 | + .extra2 = SYSCTL_ONE, |
---|
703 | 813 | }, |
---|
704 | 814 | { } |
---|
705 | 815 | }; |
---|
.. | .. |
---|
713 | 823 | |
---|
714 | 824 | core_initcall(tagged_addr_init); |
---|
715 | 825 | #endif /* CONFIG_ARM64_TAGGED_ADDR_ABI */ |
---|
| 826 | + |
---|
| 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 |
---|
| 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 |
---|