1/* SPDX-License-Identifier: GPL-2.0-only */ 2/* 3 * Low-level exception handling code 4 * 5 * Copyright (C) 2012 ARM Ltd. 6 * Authors: Catalin Marinas <catalin.marinas@arm.com> 7 * Will Deacon <will.deacon@arm.com> 8 */ 9 10#include <linux/arm-smccc.h> 11#include <linux/init.h> 12#include <linux/linkage.h> 13 14#include <asm/alternative.h> 15#include <asm/assembler.h> 16#include <asm/asm-offsets.h> 17#include <asm/asm_pointer_auth.h> 18#include <asm/cpufeature.h> 19#include <asm/errno.h> 20#include <asm/esr.h> 21#include <asm/irq.h> 22#include <asm/memory.h> 23#include <asm/mmu.h> 24#include <asm/processor.h> 25#include <asm/ptrace.h> 26#include <asm/scs.h> 27#include <asm/thread_info.h> 28#include <asm/asm-uaccess.h> 29#include <asm/unistd.h> 30 31/* 32 * Context tracking subsystem. Used to instrument transitions 33 * between user and kernel mode. 34 */ 35 .macro ct_user_exit_irqoff 36#ifdef CONFIG_CONTEXT_TRACKING 37 bl enter_from_user_mode 38#endif 39 .endm 40 41 .macro ct_user_enter 42#ifdef CONFIG_CONTEXT_TRACKING 43 bl context_tracking_user_enter 44#endif 45 .endm 46 47 .macro clear_gp_regs 48 .irp n,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29 49 mov x\n, xzr 50 .endr 51 .endm 52 53/* 54 * Bad Abort numbers 55 *----------------- 56 */ 57#define BAD_SYNC 0 58#define BAD_IRQ 1 59#define BAD_FIQ 2 60#define BAD_ERROR 3 61 62 .macro kernel_ventry, el, label, regsize = 64 63 .align 7 64#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 65 .if \el == 0 66alternative_if ARM64_UNMAP_KERNEL_AT_EL0 67 .if \regsize == 64 68 mrs x30, tpidrro_el0 69 msr tpidrro_el0, xzr 70 .else 71 mov x30, xzr 72 .endif 73alternative_else_nop_endif 74 .endif 75#endif 76 77 sub sp, sp, #S_FRAME_SIZE 78#ifdef CONFIG_VMAP_STACK 79 /* 80 * Test whether the SP has overflowed, without corrupting a GPR. 81 * Task and IRQ stacks are aligned so that SP & (1 << THREAD_SHIFT) 82 * should always be zero. 83 */ 84 add sp, sp, x0 // sp' = sp + x0 85 sub x0, sp, x0 // x0' = sp' - x0 = (sp + x0) - x0 = sp 86 tbnz x0, #THREAD_SHIFT, 0f 87 sub x0, sp, x0 // x0'' = sp' - x0' = (sp + x0) - sp = x0 88 sub sp, sp, x0 // sp'' = sp' - x0 = (sp + x0) - x0 = sp 89 b el\()\el\()_\label 90 910: 92 /* 93 * Either we've just detected an overflow, or we've taken an exception 94 * while on the overflow stack. Either way, we won't return to 95 * userspace, and can clobber EL0 registers to free up GPRs. 96 */ 97 98 /* Stash the original SP (minus S_FRAME_SIZE) in tpidr_el0. */ 99 msr tpidr_el0, x0 100 101 /* Recover the original x0 value and stash it in tpidrro_el0 */ 102 sub x0, sp, x0 103 msr tpidrro_el0, x0 104 105 /* Switch to the overflow stack */ 106 adr_this_cpu sp, overflow_stack + OVERFLOW_STACK_SIZE, x0 107 108 /* 109 * Check whether we were already on the overflow stack. This may happen 110 * after panic() re-enables interrupts. 111 */ 112 mrs x0, tpidr_el0 // sp of interrupted context 113 sub x0, sp, x0 // delta with top of overflow stack 114 tst x0, #~(OVERFLOW_STACK_SIZE - 1) // within range? 115 b.ne __bad_stack // no? -> bad stack pointer 116 117 /* We were already on the overflow stack. Restore sp/x0 and carry on. */ 118 sub sp, sp, x0 119 mrs x0, tpidrro_el0 120#endif 121 b el\()\el\()_\label 122 .endm 123 124 .macro tramp_alias, dst, sym 125 mov_q \dst, TRAMP_VALIAS 126 add \dst, \dst, #(\sym - .entry.tramp.text) 127 .endm 128 129 /* 130 * This macro corrupts x0-x3. It is the caller's duty to save/restore 131 * them if required. 132 */ 133 .macro apply_ssbd, state, tmp1, tmp2 134#ifdef CONFIG_ARM64_SSBD 135alternative_cb arm64_enable_wa2_handling 136 b .L__asm_ssbd_skip\@ 137alternative_cb_end 138 ldr_this_cpu \tmp2, arm64_ssbd_callback_required, \tmp1 139 cbz \tmp2, .L__asm_ssbd_skip\@ 140 ldr \tmp2, [tsk, #TSK_TI_FLAGS] 141 tbnz \tmp2, #TIF_SSBD, .L__asm_ssbd_skip\@ 142 mov w0, #ARM_SMCCC_ARCH_WORKAROUND_2 143 mov w1, #\state 144alternative_cb arm64_update_smccc_conduit 145 nop // Patched to SMC/HVC #0 146alternative_cb_end 147.L__asm_ssbd_skip\@: 148#endif 149 .endm 150 151 .macro kernel_entry, el, regsize = 64 152 .if \regsize == 32 153 mov w0, w0 // zero upper 32 bits of x0 154 .endif 155 stp x0, x1, [sp, #16 * 0] 156 stp x2, x3, [sp, #16 * 1] 157 stp x4, x5, [sp, #16 * 2] 158 stp x6, x7, [sp, #16 * 3] 159 stp x8, x9, [sp, #16 * 4] 160 stp x10, x11, [sp, #16 * 5] 161 stp x12, x13, [sp, #16 * 6] 162 stp x14, x15, [sp, #16 * 7] 163 stp x16, x17, [sp, #16 * 8] 164 stp x18, x19, [sp, #16 * 9] 165 stp x20, x21, [sp, #16 * 10] 166 stp x22, x23, [sp, #16 * 11] 167 stp x24, x25, [sp, #16 * 12] 168 stp x26, x27, [sp, #16 * 13] 169 stp x28, x29, [sp, #16 * 14] 170 171 .if \el == 0 172 .if \regsize == 32 173 /* 174 * If we're returning from a 32-bit task on a system affected by 175 * 1418040 then re-enable userspace access to the virtual counter. 176 */ 177#ifdef CONFIG_ARM64_ERRATUM_1418040 178alternative_if ARM64_WORKAROUND_1418040 179 mrs x0, cntkctl_el1 180 orr x0, x0, #2 // ARCH_TIMER_USR_VCT_ACCESS_EN 181 msr cntkctl_el1, x0 182alternative_else_nop_endif 183#endif 184 .endif 185 clear_gp_regs 186 mrs x21, sp_el0 187 ldr_this_cpu tsk, __entry_task, x20 188 msr sp_el0, tsk 189 190 /* 191 * Ensure MDSCR_EL1.SS is clear, since we can unmask debug exceptions 192 * when scheduling. 193 */ 194 ldr x19, [tsk, #TSK_TI_FLAGS] 195 disable_step_tsk x19, x20 196 197 apply_ssbd 1, x22, x23 198 199 ptrauth_keys_install_kernel tsk, x20, x22, x23 200 201 scs_load tsk, x20 202 .else 203 add x21, sp, #S_FRAME_SIZE 204 get_current_task tsk 205 /* Save the task's original addr_limit and set USER_DS */ 206 ldr x20, [tsk, #TSK_TI_ADDR_LIMIT] 207 str x20, [sp, #S_ORIG_ADDR_LIMIT] 208 mov x20, #USER_DS 209 str x20, [tsk, #TSK_TI_ADDR_LIMIT] 210 /* No need to reset PSTATE.UAO, hardware's already set it to 0 for us */ 211 .endif /* \el == 0 */ 212 mrs x22, elr_el1 213 mrs x23, spsr_el1 214 stp lr, x21, [sp, #S_LR] 215 216 /* 217 * In order to be able to dump the contents of struct pt_regs at the 218 * time the exception was taken (in case we attempt to walk the call 219 * stack later), chain it together with the stack frames. 220 */ 221 .if \el == 0 222 stp xzr, xzr, [sp, #S_STACKFRAME] 223 .else 224 stp x29, x22, [sp, #S_STACKFRAME] 225 .endif 226 add x29, sp, #S_STACKFRAME 227 228#ifdef CONFIG_ARM64_SW_TTBR0_PAN 229 /* 230 * Set the TTBR0 PAN bit in SPSR. When the exception is taken from 231 * EL0, there is no need to check the state of TTBR0_EL1 since 232 * accesses are always enabled. 233 * Note that the meaning of this bit differs from the ARMv8.1 PAN 234 * feature as all TTBR0_EL1 accesses are disabled, not just those to 235 * user mappings. 236 */ 237alternative_if ARM64_HAS_PAN 238 b 1f // skip TTBR0 PAN 239alternative_else_nop_endif 240 241 .if \el != 0 242 mrs x21, ttbr0_el1 243 tst x21, #TTBR_ASID_MASK // Check for the reserved ASID 244 orr x23, x23, #PSR_PAN_BIT // Set the emulated PAN in the saved SPSR 245 b.eq 1f // TTBR0 access already disabled 246 and x23, x23, #~PSR_PAN_BIT // Clear the emulated PAN in the saved SPSR 247 .endif 248 249 __uaccess_ttbr0_disable x21 2501: 251#endif 252 253 stp x22, x23, [sp, #S_PC] 254 255 /* Not in a syscall by default (el0_svc overwrites for real syscall) */ 256 .if \el == 0 257 mov w21, #NO_SYSCALL 258 str w21, [sp, #S_SYSCALLNO] 259 .endif 260 261 /* Save pmr */ 262alternative_if ARM64_HAS_IRQ_PRIO_MASKING 263 mrs_s x20, SYS_ICC_PMR_EL1 264 str x20, [sp, #S_PMR_SAVE] 265alternative_else_nop_endif 266 267 /* 268 * Registers that may be useful after this macro is invoked: 269 * 270 * x20 - ICC_PMR_EL1 271 * x21 - aborted SP 272 * x22 - aborted PC 273 * x23 - aborted PSTATE 274 */ 275 .endm 276 277 .macro kernel_exit, el 278 .if \el != 0 279 disable_daif 280 281 /* Restore the task's original addr_limit. */ 282 ldr x20, [sp, #S_ORIG_ADDR_LIMIT] 283 str x20, [tsk, #TSK_TI_ADDR_LIMIT] 284 285 /* No need to restore UAO, it will be restored from SPSR_EL1 */ 286 .endif 287 288 /* Restore pmr */ 289alternative_if ARM64_HAS_IRQ_PRIO_MASKING 290 ldr x20, [sp, #S_PMR_SAVE] 291 msr_s SYS_ICC_PMR_EL1, x20 292 mrs_s x21, SYS_ICC_CTLR_EL1 293 tbz x21, #6, .L__skip_pmr_sync\@ // Check for ICC_CTLR_EL1.PMHE 294 dsb sy // Ensure priority change is seen by redistributor 295.L__skip_pmr_sync\@: 296alternative_else_nop_endif 297 298 ldp x21, x22, [sp, #S_PC] // load ELR, SPSR 299 .if \el == 0 300 ct_user_enter 301 .endif 302 303#ifdef CONFIG_ARM64_SW_TTBR0_PAN 304 /* 305 * Restore access to TTBR0_EL1. If returning to EL0, no need for SPSR 306 * PAN bit checking. 307 */ 308alternative_if ARM64_HAS_PAN 309 b 2f // skip TTBR0 PAN 310alternative_else_nop_endif 311 312 .if \el != 0 313 tbnz x22, #22, 1f // Skip re-enabling TTBR0 access if the PSR_PAN_BIT is set 314 .endif 315 316 __uaccess_ttbr0_enable x0, x1 317 318 .if \el == 0 319 /* 320 * Enable errata workarounds only if returning to user. The only 321 * workaround currently required for TTBR0_EL1 changes are for the 322 * Cavium erratum 27456 (broadcast TLBI instructions may cause I-cache 323 * corruption). 324 */ 325 bl post_ttbr_update_workaround 326 .endif 3271: 328 .if \el != 0 329 and x22, x22, #~PSR_PAN_BIT // ARMv8.0 CPUs do not understand this bit 330 .endif 3312: 332#endif 333 334 .if \el == 0 335 ldr x23, [sp, #S_SP] // load return stack pointer 336 msr sp_el0, x23 337 tst x22, #PSR_MODE32_BIT // native task? 338 b.eq 3f 339 340#ifdef CONFIG_ARM64_ERRATUM_1418040 341alternative_if ARM64_WORKAROUND_1418040 342 mrs x0, cntkctl_el1 343 bic x0, x0, #2 // ARCH_TIMER_USR_VCT_ACCESS_EN 344 msr cntkctl_el1, x0 345alternative_else_nop_endif 346#endif 347 348#ifdef CONFIG_ARM64_ERRATUM_845719 349alternative_if ARM64_WORKAROUND_845719 350#ifdef CONFIG_PID_IN_CONTEXTIDR 351 mrs x29, contextidr_el1 352 msr contextidr_el1, x29 353#else 354 msr contextidr_el1, xzr 355#endif 356alternative_else_nop_endif 357#endif 3583: 359 scs_save tsk, x0 360 361 /* No kernel C function calls after this as user keys are set. */ 362 ptrauth_keys_install_user tsk, x0, x1, x2 363 364 apply_ssbd 0, x0, x1 365 .endif 366 367 msr elr_el1, x21 // set up the return data 368 msr spsr_el1, x22 369 ldp x0, x1, [sp, #16 * 0] 370 ldp x2, x3, [sp, #16 * 1] 371 ldp x4, x5, [sp, #16 * 2] 372 ldp x6, x7, [sp, #16 * 3] 373 ldp x8, x9, [sp, #16 * 4] 374 ldp x10, x11, [sp, #16 * 5] 375 ldp x12, x13, [sp, #16 * 6] 376 ldp x14, x15, [sp, #16 * 7] 377 ldp x16, x17, [sp, #16 * 8] 378 ldp x18, x19, [sp, #16 * 9] 379 ldp x20, x21, [sp, #16 * 10] 380 ldp x22, x23, [sp, #16 * 11] 381 ldp x24, x25, [sp, #16 * 12] 382 ldp x26, x27, [sp, #16 * 13] 383 ldp x28, x29, [sp, #16 * 14] 384 ldr lr, [sp, #S_LR] 385 add sp, sp, #S_FRAME_SIZE // restore sp 386 387 .if \el == 0 388alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0 389#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 390 bne 4f 391 msr far_el1, x30 392 tramp_alias x30, tramp_exit_native 393 br x30 3944: 395 tramp_alias x30, tramp_exit_compat 396 br x30 397#endif 398 .else 399 eret 400 .endif 401 sb 402 .endm 403 404 .macro irq_stack_entry 405 mov x19, sp // preserve the original sp 406#ifdef CONFIG_SHADOW_CALL_STACK 407 mov x24, scs_sp // preserve the original shadow stack 408#endif 409 410 /* 411 * Compare sp with the base of the task stack. 412 * If the top ~(THREAD_SIZE - 1) bits match, we are on a task stack, 413 * and should switch to the irq stack. 414 */ 415 ldr x25, [tsk, TSK_STACK] 416 eor x25, x25, x19 417 and x25, x25, #~(THREAD_SIZE - 1) 418 cbnz x25, 9998f 419 420 ldr_this_cpu x25, irq_stack_ptr, x26 421 mov x26, #IRQ_STACK_SIZE 422 add x26, x25, x26 423 424 /* switch to the irq stack */ 425 mov sp, x26 426 427#ifdef CONFIG_SHADOW_CALL_STACK 428 /* also switch to the irq shadow stack */ 429 adr_this_cpu scs_sp, irq_shadow_call_stack, x26 430#endif 431 4329998: 433 .endm 434 435 /* 436 * The callee-saved regs (x19-x29) should be preserved between 437 * irq_stack_entry and irq_stack_exit, but note that kernel_entry 438 * uses x20-x23 to store data for later use. 439 */ 440 .macro irq_stack_exit 441 mov sp, x19 442#ifdef CONFIG_SHADOW_CALL_STACK 443 mov scs_sp, x24 444#endif 445 .endm 446 447/* GPRs used by entry code */ 448tsk .req x28 // current thread_info 449 450/* 451 * Interrupt handling. 452 */ 453 .macro irq_handler 454 ldr_l x1, handle_arch_irq 455 mov x0, sp 456 irq_stack_entry 457 blr x1 458 irq_stack_exit 459 .endm 460 461#ifdef CONFIG_ARM64_PSEUDO_NMI 462 /* 463 * Set res to 0 if irqs were unmasked in interrupted context. 464 * Otherwise set res to non-0 value. 465 */ 466 .macro test_irqs_unmasked res:req, pmr:req 467alternative_if ARM64_HAS_IRQ_PRIO_MASKING 468 sub \res, \pmr, #GIC_PRIO_IRQON 469alternative_else 470 mov \res, xzr 471alternative_endif 472 .endm 473#endif 474 475 .macro gic_prio_kentry_setup, tmp:req 476#ifdef CONFIG_ARM64_PSEUDO_NMI 477 alternative_if ARM64_HAS_IRQ_PRIO_MASKING 478 mov \tmp, #(GIC_PRIO_PSR_I_SET | GIC_PRIO_IRQON) 479 msr_s SYS_ICC_PMR_EL1, \tmp 480 alternative_else_nop_endif 481#endif 482 .endm 483 484 .macro gic_prio_irq_setup, pmr:req, tmp:req 485#ifdef CONFIG_ARM64_PSEUDO_NMI 486 alternative_if ARM64_HAS_IRQ_PRIO_MASKING 487 orr \tmp, \pmr, #GIC_PRIO_PSR_I_SET 488 msr_s SYS_ICC_PMR_EL1, \tmp 489 alternative_else_nop_endif 490#endif 491 .endm 492 493 .text 494 495/* 496 * Exception vectors. 497 */ 498 .pushsection ".entry.text", "ax" 499 500 .align 11 501SYM_CODE_START(vectors) 502 kernel_ventry 1, sync_invalid // Synchronous EL1t 503 kernel_ventry 1, irq_invalid // IRQ EL1t 504 kernel_ventry 1, fiq_invalid // FIQ EL1t 505 kernel_ventry 1, error_invalid // Error EL1t 506 507 kernel_ventry 1, sync // Synchronous EL1h 508 kernel_ventry 1, irq // IRQ EL1h 509 kernel_ventry 1, fiq_invalid // FIQ EL1h 510 kernel_ventry 1, error // Error EL1h 511 512 kernel_ventry 0, sync // Synchronous 64-bit EL0 513 kernel_ventry 0, irq // IRQ 64-bit EL0 514 kernel_ventry 0, fiq_invalid // FIQ 64-bit EL0 515 kernel_ventry 0, error // Error 64-bit EL0 516 517#ifdef CONFIG_COMPAT 518 kernel_ventry 0, sync_compat, 32 // Synchronous 32-bit EL0 519 kernel_ventry 0, irq_compat, 32 // IRQ 32-bit EL0 520 kernel_ventry 0, fiq_invalid_compat, 32 // FIQ 32-bit EL0 521 kernel_ventry 0, error_compat, 32 // Error 32-bit EL0 522#else 523 kernel_ventry 0, sync_invalid, 32 // Synchronous 32-bit EL0 524 kernel_ventry 0, irq_invalid, 32 // IRQ 32-bit EL0 525 kernel_ventry 0, fiq_invalid, 32 // FIQ 32-bit EL0 526 kernel_ventry 0, error_invalid, 32 // Error 32-bit EL0 527#endif 528SYM_CODE_END(vectors) 529 530#ifdef CONFIG_VMAP_STACK 531 /* 532 * We detected an overflow in kernel_ventry, which switched to the 533 * overflow stack. Stash the exception regs, and head to our overflow 534 * handler. 535 */ 536__bad_stack: 537 /* Restore the original x0 value */ 538 mrs x0, tpidrro_el0 539 540 /* 541 * Store the original GPRs to the new stack. The orginal SP (minus 542 * S_FRAME_SIZE) was stashed in tpidr_el0 by kernel_ventry. 543 */ 544 sub sp, sp, #S_FRAME_SIZE 545 kernel_entry 1 546 mrs x0, tpidr_el0 547 add x0, x0, #S_FRAME_SIZE 548 str x0, [sp, #S_SP] 549 550 /* Stash the regs for handle_bad_stack */ 551 mov x0, sp 552 553 /* Time to die */ 554 bl handle_bad_stack 555 ASM_BUG() 556#endif /* CONFIG_VMAP_STACK */ 557 558/* 559 * Invalid mode handlers 560 */ 561 .macro inv_entry, el, reason, regsize = 64 562 kernel_entry \el, \regsize 563 mov x0, sp 564 mov x1, #\reason 565 mrs x2, esr_el1 566 bl bad_mode 567 ASM_BUG() 568 .endm 569 570SYM_CODE_START_LOCAL(el0_sync_invalid) 571 inv_entry 0, BAD_SYNC 572SYM_CODE_END(el0_sync_invalid) 573 574SYM_CODE_START_LOCAL(el0_irq_invalid) 575 inv_entry 0, BAD_IRQ 576SYM_CODE_END(el0_irq_invalid) 577 578SYM_CODE_START_LOCAL(el0_fiq_invalid) 579 inv_entry 0, BAD_FIQ 580SYM_CODE_END(el0_fiq_invalid) 581 582SYM_CODE_START_LOCAL(el0_error_invalid) 583 inv_entry 0, BAD_ERROR 584SYM_CODE_END(el0_error_invalid) 585 586#ifdef CONFIG_COMPAT 587SYM_CODE_START_LOCAL(el0_fiq_invalid_compat) 588 inv_entry 0, BAD_FIQ, 32 589SYM_CODE_END(el0_fiq_invalid_compat) 590#endif 591 592SYM_CODE_START_LOCAL(el1_sync_invalid) 593 inv_entry 1, BAD_SYNC 594SYM_CODE_END(el1_sync_invalid) 595 596SYM_CODE_START_LOCAL(el1_irq_invalid) 597 inv_entry 1, BAD_IRQ 598SYM_CODE_END(el1_irq_invalid) 599 600SYM_CODE_START_LOCAL(el1_fiq_invalid) 601 inv_entry 1, BAD_FIQ 602SYM_CODE_END(el1_fiq_invalid) 603 604SYM_CODE_START_LOCAL(el1_error_invalid) 605 inv_entry 1, BAD_ERROR 606SYM_CODE_END(el1_error_invalid) 607 608/* 609 * EL1 mode handlers. 610 */ 611 .align 6 612SYM_CODE_START_LOCAL_NOALIGN(el1_sync) 613 kernel_entry 1 614 mov x0, sp 615 bl el1_sync_handler 616 kernel_exit 1 617SYM_CODE_END(el1_sync) 618 619 .align 6 620SYM_CODE_START_LOCAL_NOALIGN(el1_irq) 621 kernel_entry 1 622 gic_prio_irq_setup pmr=x20, tmp=x1 623 enable_da_f 624 625#ifdef CONFIG_ARM64_PSEUDO_NMI 626 test_irqs_unmasked res=x0, pmr=x20 627 cbz x0, 1f 628 bl asm_nmi_enter 6291: 630#endif 631 632#ifdef CONFIG_TRACE_IRQFLAGS 633 bl trace_hardirqs_off 634#endif 635 636 irq_handler 637 638#ifdef CONFIG_PREEMPTION 639 ldr x24, [tsk, #TSK_TI_PREEMPT] // get preempt count 640alternative_if ARM64_HAS_IRQ_PRIO_MASKING 641 /* 642 * DA_F were cleared at start of handling. If anything is set in DAIF, 643 * we come back from an NMI, so skip preemption 644 */ 645 mrs x0, daif 646 orr x24, x24, x0 647alternative_else_nop_endif 648 cbnz x24, 1f // preempt count != 0 || NMI return path 649 bl arm64_preempt_schedule_irq // irq en/disable is done inside 6501: 651#endif 652 653#ifdef CONFIG_ARM64_PSEUDO_NMI 654 /* 655 * When using IRQ priority masking, we can get spurious interrupts while 656 * PMR is set to GIC_PRIO_IRQOFF. An NMI might also have occurred in a 657 * section with interrupts disabled. Skip tracing in those cases. 658 */ 659 test_irqs_unmasked res=x0, pmr=x20 660 cbz x0, 1f 661 bl asm_nmi_exit 6621: 663#endif 664 665#ifdef CONFIG_TRACE_IRQFLAGS 666#ifdef CONFIG_ARM64_PSEUDO_NMI 667 test_irqs_unmasked res=x0, pmr=x20 668 cbnz x0, 1f 669#endif 670 bl trace_hardirqs_on 6711: 672#endif 673 674 kernel_exit 1 675SYM_CODE_END(el1_irq) 676 677/* 678 * EL0 mode handlers. 679 */ 680 .align 6 681SYM_CODE_START_LOCAL_NOALIGN(el0_sync) 682 kernel_entry 0 683 mov x0, sp 684 bl el0_sync_handler 685 b ret_to_user 686SYM_CODE_END(el0_sync) 687 688#ifdef CONFIG_COMPAT 689 .align 6 690SYM_CODE_START_LOCAL_NOALIGN(el0_sync_compat) 691 kernel_entry 0, 32 692 mov x0, sp 693 bl el0_sync_compat_handler 694 b ret_to_user 695SYM_CODE_END(el0_sync_compat) 696 697 .align 6 698SYM_CODE_START_LOCAL_NOALIGN(el0_irq_compat) 699 kernel_entry 0, 32 700 b el0_irq_naked 701SYM_CODE_END(el0_irq_compat) 702 703SYM_CODE_START_LOCAL_NOALIGN(el0_error_compat) 704 kernel_entry 0, 32 705 b el0_error_naked 706SYM_CODE_END(el0_error_compat) 707#endif 708 709 .align 6 710SYM_CODE_START_LOCAL_NOALIGN(el0_irq) 711 kernel_entry 0 712el0_irq_naked: 713 gic_prio_irq_setup pmr=x20, tmp=x0 714 ct_user_exit_irqoff 715 enable_da_f 716 717#ifdef CONFIG_TRACE_IRQFLAGS 718 bl trace_hardirqs_off 719#endif 720 721#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR 722 tbz x22, #55, 1f 723 bl do_el0_irq_bp_hardening 7241: 725#endif 726 irq_handler 727 728#ifdef CONFIG_TRACE_IRQFLAGS 729 bl trace_hardirqs_on 730#endif 731 b ret_to_user 732SYM_CODE_END(el0_irq) 733 734SYM_CODE_START_LOCAL(el1_error) 735 kernel_entry 1 736 mrs x1, esr_el1 737 gic_prio_kentry_setup tmp=x2 738 enable_dbg 739 mov x0, sp 740 bl do_serror 741 kernel_exit 1 742SYM_CODE_END(el1_error) 743 744SYM_CODE_START_LOCAL(el0_error) 745 kernel_entry 0 746el0_error_naked: 747 mrs x25, esr_el1 748 gic_prio_kentry_setup tmp=x2 749 ct_user_exit_irqoff 750 enable_dbg 751 mov x0, sp 752 mov x1, x25 753 bl do_serror 754 enable_da_f 755 b ret_to_user 756SYM_CODE_END(el0_error) 757 758/* 759 * "slow" syscall return path. 760 */ 761SYM_CODE_START_LOCAL(ret_to_user) 762 disable_daif 763 gic_prio_kentry_setup tmp=x3 764 ldr x1, [tsk, #TSK_TI_FLAGS] 765 and x2, x1, #_TIF_WORK_MASK 766 cbnz x2, work_pending 767finish_ret_to_user: 768 enable_step_tsk x1, x2 769#ifdef CONFIG_GCC_PLUGIN_STACKLEAK 770 bl stackleak_erase 771#endif 772 kernel_exit 0 773 774/* 775 * Ok, we need to do extra processing, enter the slow path. 776 */ 777work_pending: 778 mov x0, sp // 'regs' 779 bl do_notify_resume 780#ifdef CONFIG_TRACE_IRQFLAGS 781 bl trace_hardirqs_on // enabled while in userspace 782#endif 783 ldr x1, [tsk, #TSK_TI_FLAGS] // re-check for single-step 784 b finish_ret_to_user 785SYM_CODE_END(ret_to_user) 786 787 .popsection // .entry.text 788 789#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 790/* 791 * Exception vectors trampoline. 792 */ 793 .pushsection ".entry.tramp.text", "ax" 794 795 .macro tramp_map_kernel, tmp 796 mrs \tmp, ttbr1_el1 797 add \tmp, \tmp, #(PAGE_SIZE + RESERVED_TTBR0_SIZE) 798 bic \tmp, \tmp, #USER_ASID_FLAG 799 msr ttbr1_el1, \tmp 800#ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003 801alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003 802 /* ASID already in \tmp[63:48] */ 803 movk \tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12) 804 movk \tmp, #:abs_g1_nc:(TRAMP_VALIAS >> 12) 805 /* 2MB boundary containing the vectors, so we nobble the walk cache */ 806 movk \tmp, #:abs_g0_nc:((TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12) 807 isb 808 tlbi vae1, \tmp 809 dsb nsh 810alternative_else_nop_endif 811#endif /* CONFIG_QCOM_FALKOR_ERRATUM_1003 */ 812 .endm 813 814 .macro tramp_unmap_kernel, tmp 815 mrs \tmp, ttbr1_el1 816 sub \tmp, \tmp, #(PAGE_SIZE + RESERVED_TTBR0_SIZE) 817 orr \tmp, \tmp, #USER_ASID_FLAG 818 msr ttbr1_el1, \tmp 819 /* 820 * We avoid running the post_ttbr_update_workaround here because 821 * it's only needed by Cavium ThunderX, which requires KPTI to be 822 * disabled. 823 */ 824 .endm 825 826 .macro tramp_ventry, regsize = 64 827 .align 7 8281: 829 .if \regsize == 64 830 msr tpidrro_el0, x30 // Restored in kernel_ventry 831 .endif 832 /* 833 * Defend against branch aliasing attacks by pushing a dummy 834 * entry onto the return stack and using a RET instruction to 835 * enter the full-fat kernel vectors. 836 */ 837 bl 2f 838 b . 8392: 840 tramp_map_kernel x30 841#ifdef CONFIG_RANDOMIZE_BASE 842 adr x30, tramp_vectors + PAGE_SIZE 843alternative_insn isb, nop, ARM64_WORKAROUND_QCOM_FALKOR_E1003 844 ldr x30, [x30] 845#else 846 ldr x30, =vectors 847#endif 848alternative_if_not ARM64_WORKAROUND_CAVIUM_TX2_219_PRFM 849 prfm plil1strm, [x30, #(1b - tramp_vectors)] 850alternative_else_nop_endif 851 msr vbar_el1, x30 852 add x30, x30, #(1b - tramp_vectors) 853 isb 854 ret 855 .endm 856 857 .macro tramp_exit, regsize = 64 858 adr x30, tramp_vectors 859 msr vbar_el1, x30 860 tramp_unmap_kernel x30 861 .if \regsize == 64 862 mrs x30, far_el1 863 .endif 864 eret 865 sb 866 .endm 867 868 .align 11 869SYM_CODE_START_NOALIGN(tramp_vectors) 870 .space 0x400 871 872 tramp_ventry 873 tramp_ventry 874 tramp_ventry 875 tramp_ventry 876 877 tramp_ventry 32 878 tramp_ventry 32 879 tramp_ventry 32 880 tramp_ventry 32 881SYM_CODE_END(tramp_vectors) 882 883SYM_CODE_START(tramp_exit_native) 884 tramp_exit 885SYM_CODE_END(tramp_exit_native) 886 887SYM_CODE_START(tramp_exit_compat) 888 tramp_exit 32 889SYM_CODE_END(tramp_exit_compat) 890 891 .ltorg 892 .popsection // .entry.tramp.text 893#ifdef CONFIG_RANDOMIZE_BASE 894 .pushsection ".rodata", "a" 895 .align PAGE_SHIFT 896SYM_DATA_START(__entry_tramp_data_start) 897 .quad vectors 898SYM_DATA_END(__entry_tramp_data_start) 899 .popsection // .rodata 900#endif /* CONFIG_RANDOMIZE_BASE */ 901#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */ 902 903/* 904 * Register switch for AArch64. The callee-saved registers need to be saved 905 * and restored. On entry: 906 * x0 = previous task_struct (must be preserved across the switch) 907 * x1 = next task_struct 908 * Previous and next are guaranteed not to be the same. 909 * 910 */ 911SYM_FUNC_START(cpu_switch_to) 912 mov x10, #THREAD_CPU_CONTEXT 913 add x8, x0, x10 914 mov x9, sp 915 stp x19, x20, [x8], #16 // store callee-saved registers 916 stp x21, x22, [x8], #16 917 stp x23, x24, [x8], #16 918 stp x25, x26, [x8], #16 919 stp x27, x28, [x8], #16 920 stp x29, x9, [x8], #16 921 str lr, [x8] 922 add x8, x1, x10 923 ldp x19, x20, [x8], #16 // restore callee-saved registers 924 ldp x21, x22, [x8], #16 925 ldp x23, x24, [x8], #16 926 ldp x25, x26, [x8], #16 927 ldp x27, x28, [x8], #16 928 ldp x29, x9, [x8], #16 929 ldr lr, [x8] 930 mov sp, x9 931 msr sp_el0, x1 932 ptrauth_keys_install_kernel x1, x8, x9, x10 933 scs_save x0, x8 934 scs_load x1, x8 935 ret 936SYM_FUNC_END(cpu_switch_to) 937NOKPROBE(cpu_switch_to) 938 939/* 940 * This is how we return from a fork. 941 */ 942SYM_CODE_START(ret_from_fork) 943 bl schedule_tail 944 cbz x19, 1f // not a kernel thread 945 mov x0, x20 946 blr x19 9471: get_current_task tsk 948 b ret_to_user 949SYM_CODE_END(ret_from_fork) 950NOKPROBE(ret_from_fork) 951 952#ifdef CONFIG_ARM_SDE_INTERFACE 953 954#include <asm/sdei.h> 955#include <uapi/linux/arm_sdei.h> 956 957.macro sdei_handler_exit exit_mode 958 /* On success, this call never returns... */ 959 cmp \exit_mode, #SDEI_EXIT_SMC 960 b.ne 99f 961 smc #0 962 b . 96399: hvc #0 964 b . 965.endm 966 967#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 968/* 969 * The regular SDEI entry point may have been unmapped along with the rest of 970 * the kernel. This trampoline restores the kernel mapping to make the x1 memory 971 * argument accessible. 972 * 973 * This clobbers x4, __sdei_handler() will restore this from firmware's 974 * copy. 975 */ 976.ltorg 977.pushsection ".entry.tramp.text", "ax" 978SYM_CODE_START(__sdei_asm_entry_trampoline) 979 mrs x4, ttbr1_el1 980 tbz x4, #USER_ASID_BIT, 1f 981 982 tramp_map_kernel tmp=x4 983 isb 984 mov x4, xzr 985 986 /* 987 * Use reg->interrupted_regs.addr_limit to remember whether to unmap 988 * the kernel on exit. 989 */ 9901: str x4, [x1, #(SDEI_EVENT_INTREGS + S_ORIG_ADDR_LIMIT)] 991 992#ifdef CONFIG_RANDOMIZE_BASE 993 adr x4, tramp_vectors + PAGE_SIZE 994 add x4, x4, #:lo12:__sdei_asm_trampoline_next_handler 995 ldr x4, [x4] 996#else 997 ldr x4, =__sdei_asm_handler 998#endif 999 br x4 1000SYM_CODE_END(__sdei_asm_entry_trampoline) 1001NOKPROBE(__sdei_asm_entry_trampoline) 1002 1003/* 1004 * Make the exit call and restore the original ttbr1_el1 1005 * 1006 * x0 & x1: setup for the exit API call 1007 * x2: exit_mode 1008 * x4: struct sdei_registered_event argument from registration time. 1009 */ 1010SYM_CODE_START(__sdei_asm_exit_trampoline) 1011 ldr x4, [x4, #(SDEI_EVENT_INTREGS + S_ORIG_ADDR_LIMIT)] 1012 cbnz x4, 1f 1013 1014 tramp_unmap_kernel tmp=x4 1015 10161: sdei_handler_exit exit_mode=x2 1017SYM_CODE_END(__sdei_asm_exit_trampoline) 1018NOKPROBE(__sdei_asm_exit_trampoline) 1019 .ltorg 1020.popsection // .entry.tramp.text 1021#ifdef CONFIG_RANDOMIZE_BASE 1022.pushsection ".rodata", "a" 1023SYM_DATA_START(__sdei_asm_trampoline_next_handler) 1024 .quad __sdei_asm_handler 1025SYM_DATA_END(__sdei_asm_trampoline_next_handler) 1026.popsection // .rodata 1027#endif /* CONFIG_RANDOMIZE_BASE */ 1028#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */ 1029 1030/* 1031 * Software Delegated Exception entry point. 1032 * 1033 * x0: Event number 1034 * x1: struct sdei_registered_event argument from registration time. 1035 * x2: interrupted PC 1036 * x3: interrupted PSTATE 1037 * x4: maybe clobbered by the trampoline 1038 * 1039 * Firmware has preserved x0->x17 for us, we must save/restore the rest to 1040 * follow SMC-CC. We save (or retrieve) all the registers as the handler may 1041 * want them. 1042 */ 1043SYM_CODE_START(__sdei_asm_handler) 1044 stp x2, x3, [x1, #SDEI_EVENT_INTREGS + S_PC] 1045 stp x4, x5, [x1, #SDEI_EVENT_INTREGS + 16 * 2] 1046 stp x6, x7, [x1, #SDEI_EVENT_INTREGS + 16 * 3] 1047 stp x8, x9, [x1, #SDEI_EVENT_INTREGS + 16 * 4] 1048 stp x10, x11, [x1, #SDEI_EVENT_INTREGS + 16 * 5] 1049 stp x12, x13, [x1, #SDEI_EVENT_INTREGS + 16 * 6] 1050 stp x14, x15, [x1, #SDEI_EVENT_INTREGS + 16 * 7] 1051 stp x16, x17, [x1, #SDEI_EVENT_INTREGS + 16 * 8] 1052 stp x18, x19, [x1, #SDEI_EVENT_INTREGS + 16 * 9] 1053 stp x20, x21, [x1, #SDEI_EVENT_INTREGS + 16 * 10] 1054 stp x22, x23, [x1, #SDEI_EVENT_INTREGS + 16 * 11] 1055 stp x24, x25, [x1, #SDEI_EVENT_INTREGS + 16 * 12] 1056 stp x26, x27, [x1, #SDEI_EVENT_INTREGS + 16 * 13] 1057 stp x28, x29, [x1, #SDEI_EVENT_INTREGS + 16 * 14] 1058 mov x4, sp 1059 stp lr, x4, [x1, #SDEI_EVENT_INTREGS + S_LR] 1060 1061 mov x19, x1 1062 1063#if defined(CONFIG_VMAP_STACK) || defined(CONFIG_SHADOW_CALL_STACK) 1064 ldrb w4, [x19, #SDEI_EVENT_PRIORITY] 1065#endif 1066 1067#ifdef CONFIG_VMAP_STACK 1068 /* 1069 * entry.S may have been using sp as a scratch register, find whether 1070 * this is a normal or critical event and switch to the appropriate 1071 * stack for this CPU. 1072 */ 1073 cbnz w4, 1f 1074 ldr_this_cpu dst=x5, sym=sdei_stack_normal_ptr, tmp=x6 1075 b 2f 10761: ldr_this_cpu dst=x5, sym=sdei_stack_critical_ptr, tmp=x6 10772: mov x6, #SDEI_STACK_SIZE 1078 add x5, x5, x6 1079 mov sp, x5 1080#endif 1081 1082#ifdef CONFIG_SHADOW_CALL_STACK 1083 /* Use a separate shadow call stack for normal and critical events */ 1084 cbnz w4, 3f 1085 adr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_normal, tmp=x6 1086 b 4f 10873: adr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical, tmp=x6 10884: 1089#endif 1090 1091 /* 1092 * We may have interrupted userspace, or a guest, or exit-from or 1093 * return-to either of these. We can't trust sp_el0, restore it. 1094 */ 1095 mrs x28, sp_el0 1096 ldr_this_cpu dst=x0, sym=__entry_task, tmp=x1 1097 msr sp_el0, x0 1098 1099 /* If we interrupted the kernel point to the previous stack/frame. */ 1100 and x0, x3, #0xc 1101 mrs x1, CurrentEL 1102 cmp x0, x1 1103 csel x29, x29, xzr, eq // fp, or zero 1104 csel x4, x2, xzr, eq // elr, or zero 1105 1106 stp x29, x4, [sp, #-16]! 1107 mov x29, sp 1108 1109 add x0, x19, #SDEI_EVENT_INTREGS 1110 mov x1, x19 1111 bl __sdei_handler 1112 1113 msr sp_el0, x28 1114 /* restore regs >x17 that we clobbered */ 1115 mov x4, x19 // keep x4 for __sdei_asm_exit_trampoline 1116 ldp x28, x29, [x4, #SDEI_EVENT_INTREGS + 16 * 14] 1117 ldp x18, x19, [x4, #SDEI_EVENT_INTREGS + 16 * 9] 1118 ldp lr, x1, [x4, #SDEI_EVENT_INTREGS + S_LR] 1119 mov sp, x1 1120 1121 mov x1, x0 // address to complete_and_resume 1122 /* x0 = (x0 <= 1) ? EVENT_COMPLETE:EVENT_COMPLETE_AND_RESUME */ 1123 cmp x0, #1 1124 mov_q x2, SDEI_1_0_FN_SDEI_EVENT_COMPLETE 1125 mov_q x3, SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME 1126 csel x0, x2, x3, ls 1127 1128 ldr_l x2, sdei_exit_mode 1129 1130alternative_if_not ARM64_UNMAP_KERNEL_AT_EL0 1131 sdei_handler_exit exit_mode=x2 1132alternative_else_nop_endif 1133 1134#ifdef CONFIG_UNMAP_KERNEL_AT_EL0 1135 tramp_alias dst=x5, sym=__sdei_asm_exit_trampoline 1136 br x5 1137#endif 1138SYM_CODE_END(__sdei_asm_handler) 1139NOKPROBE(__sdei_asm_handler) 1140#endif /* CONFIG_ARM_SDE_INTERFACE */ 1141