entry.S 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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 <[email protected]>
  7. * Will Deacon <[email protected]>
  8. */
  9. #include <linux/arm-smccc.h>
  10. #include <linux/init.h>
  11. #include <linux/linkage.h>
  12. #include <asm/alternative.h>
  13. #include <asm/assembler.h>
  14. #include <asm/asm-offsets.h>
  15. #include <asm/asm_pointer_auth.h>
  16. #include <asm/bug.h>
  17. #include <asm/cpufeature.h>
  18. #include <asm/errno.h>
  19. #include <asm/esr.h>
  20. #include <asm/irq.h>
  21. #include <asm/memory.h>
  22. #include <asm/mmu.h>
  23. #include <asm/processor.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/scs.h>
  26. #include <asm/thread_info.h>
  27. #include <asm/asm-uaccess.h>
  28. #include <asm/unistd.h>
  29. .macro clear_gp_regs
  30. .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
  31. mov x\n, xzr
  32. .endr
  33. .endm
  34. .macro kernel_ventry, el:req, ht:req, regsize:req, label:req
  35. .align 7
  36. .Lventry_start\@:
  37. .if \el == 0
  38. /*
  39. * This must be the first instruction of the EL0 vector entries. It is
  40. * skipped by the trampoline vectors, to trigger the cleanup.
  41. */
  42. b .Lskip_tramp_vectors_cleanup\@
  43. .if \regsize == 64
  44. mrs x30, tpidrro_el0
  45. msr tpidrro_el0, xzr
  46. .else
  47. mov x30, xzr
  48. .endif
  49. .Lskip_tramp_vectors_cleanup\@:
  50. .endif
  51. sub sp, sp, #PT_REGS_SIZE
  52. #ifdef CONFIG_VMAP_STACK
  53. /*
  54. * Test whether the SP has overflowed, without corrupting a GPR.
  55. * Task and IRQ stacks are aligned so that SP & (1 << THREAD_SHIFT)
  56. * should always be zero.
  57. */
  58. add sp, sp, x0 // sp' = sp + x0
  59. sub x0, sp, x0 // x0' = sp' - x0 = (sp + x0) - x0 = sp
  60. tbnz x0, #THREAD_SHIFT, 0f
  61. sub x0, sp, x0 // x0'' = sp' - x0' = (sp + x0) - sp = x0
  62. sub sp, sp, x0 // sp'' = sp' - x0 = (sp + x0) - x0 = sp
  63. b el\el\ht\()_\regsize\()_\label
  64. 0:
  65. /*
  66. * Either we've just detected an overflow, or we've taken an exception
  67. * while on the overflow stack. Either way, we won't return to
  68. * userspace, and can clobber EL0 registers to free up GPRs.
  69. */
  70. /* Stash the original SP (minus PT_REGS_SIZE) in tpidr_el0. */
  71. msr tpidr_el0, x0
  72. /* Recover the original x0 value and stash it in tpidrro_el0 */
  73. sub x0, sp, x0
  74. msr tpidrro_el0, x0
  75. /* Switch to the overflow stack */
  76. adr_this_cpu sp, overflow_stack + OVERFLOW_STACK_SIZE, x0
  77. /*
  78. * Check whether we were already on the overflow stack. This may happen
  79. * after panic() re-enables interrupts.
  80. */
  81. mrs x0, tpidr_el0 // sp of interrupted context
  82. sub x0, sp, x0 // delta with top of overflow stack
  83. tst x0, #~(OVERFLOW_STACK_SIZE - 1) // within range?
  84. b.ne __bad_stack // no? -> bad stack pointer
  85. /* We were already on the overflow stack. Restore sp/x0 and carry on. */
  86. sub sp, sp, x0
  87. mrs x0, tpidrro_el0
  88. #endif
  89. b el\el\ht\()_\regsize\()_\label
  90. .org .Lventry_start\@ + 128 // Did we overflow the ventry slot?
  91. .endm
  92. .macro tramp_alias, dst, sym, tmp
  93. mov_q \dst, TRAMP_VALIAS
  94. adr_l \tmp, \sym
  95. add \dst, \dst, \tmp
  96. adr_l \tmp, .entry.tramp.text
  97. sub \dst, \dst, \tmp
  98. .endm
  99. /*
  100. * This macro corrupts x0-x3. It is the caller's duty to save/restore
  101. * them if required.
  102. */
  103. .macro apply_ssbd, state, tmp1, tmp2
  104. alternative_cb ARM64_ALWAYS_SYSTEM, spectre_v4_patch_fw_mitigation_enable
  105. b .L__asm_ssbd_skip\@ // Patched to NOP
  106. alternative_cb_end
  107. ldr_this_cpu \tmp2, arm64_ssbd_callback_required, \tmp1
  108. cbz \tmp2, .L__asm_ssbd_skip\@
  109. ldr \tmp2, [tsk, #TSK_TI_FLAGS]
  110. tbnz \tmp2, #TIF_SSBD, .L__asm_ssbd_skip\@
  111. mov w0, #ARM_SMCCC_ARCH_WORKAROUND_2
  112. mov w1, #\state
  113. alternative_cb ARM64_ALWAYS_SYSTEM, smccc_patch_fw_mitigation_conduit
  114. nop // Patched to SMC/HVC #0
  115. alternative_cb_end
  116. .L__asm_ssbd_skip\@:
  117. .endm
  118. /* Check for MTE asynchronous tag check faults */
  119. .macro check_mte_async_tcf, tmp, ti_flags, thread_sctlr
  120. #ifdef CONFIG_ARM64_MTE
  121. .arch_extension lse
  122. alternative_if_not ARM64_MTE
  123. b 1f
  124. alternative_else_nop_endif
  125. /*
  126. * Asynchronous tag check faults are only possible in ASYNC (2) or
  127. * ASYM (3) modes. In each of these modes bit 1 of SCTLR_EL1.TCF0 is
  128. * set, so skip the check if it is unset.
  129. */
  130. tbz \thread_sctlr, #(SCTLR_EL1_TCF0_SHIFT + 1), 1f
  131. mrs_s \tmp, SYS_TFSRE0_EL1
  132. tbz \tmp, #SYS_TFSR_EL1_TF0_SHIFT, 1f
  133. /* Asynchronous TCF occurred for TTBR0 access, set the TI flag */
  134. mov \tmp, #_TIF_MTE_ASYNC_FAULT
  135. add \ti_flags, tsk, #TSK_TI_FLAGS
  136. stset \tmp, [\ti_flags]
  137. 1:
  138. #endif
  139. .endm
  140. /* Clear the MTE asynchronous tag check faults */
  141. .macro clear_mte_async_tcf thread_sctlr
  142. #ifdef CONFIG_ARM64_MTE
  143. alternative_if ARM64_MTE
  144. /* See comment in check_mte_async_tcf above. */
  145. tbz \thread_sctlr, #(SCTLR_EL1_TCF0_SHIFT + 1), 1f
  146. dsb ish
  147. msr_s SYS_TFSRE0_EL1, xzr
  148. 1:
  149. alternative_else_nop_endif
  150. #endif
  151. .endm
  152. .macro mte_set_gcr, mte_ctrl, tmp
  153. #ifdef CONFIG_ARM64_MTE
  154. ubfx \tmp, \mte_ctrl, #MTE_CTRL_GCR_USER_EXCL_SHIFT, #16
  155. orr \tmp, \tmp, #SYS_GCR_EL1_RRND
  156. msr_s SYS_GCR_EL1, \tmp
  157. #endif
  158. .endm
  159. .macro mte_set_kernel_gcr, tmp, tmp2
  160. #ifdef CONFIG_KASAN_HW_TAGS
  161. alternative_cb ARM64_ALWAYS_SYSTEM, kasan_hw_tags_enable
  162. b 1f
  163. alternative_cb_end
  164. mov \tmp, KERNEL_GCR_EL1
  165. msr_s SYS_GCR_EL1, \tmp
  166. 1:
  167. #endif
  168. .endm
  169. .macro mte_set_user_gcr, tsk, tmp, tmp2
  170. #ifdef CONFIG_KASAN_HW_TAGS
  171. alternative_cb ARM64_ALWAYS_SYSTEM, kasan_hw_tags_enable
  172. b 1f
  173. alternative_cb_end
  174. ldr \tmp, [\tsk, #THREAD_MTE_CTRL]
  175. mte_set_gcr \tmp, \tmp2
  176. 1:
  177. #endif
  178. .endm
  179. .macro kernel_entry, el, regsize = 64
  180. .if \regsize == 32
  181. mov w0, w0 // zero upper 32 bits of x0
  182. .endif
  183. stp x0, x1, [sp, #16 * 0]
  184. stp x2, x3, [sp, #16 * 1]
  185. stp x4, x5, [sp, #16 * 2]
  186. stp x6, x7, [sp, #16 * 3]
  187. stp x8, x9, [sp, #16 * 4]
  188. stp x10, x11, [sp, #16 * 5]
  189. stp x12, x13, [sp, #16 * 6]
  190. stp x14, x15, [sp, #16 * 7]
  191. stp x16, x17, [sp, #16 * 8]
  192. stp x18, x19, [sp, #16 * 9]
  193. stp x20, x21, [sp, #16 * 10]
  194. stp x22, x23, [sp, #16 * 11]
  195. stp x24, x25, [sp, #16 * 12]
  196. stp x26, x27, [sp, #16 * 13]
  197. stp x28, x29, [sp, #16 * 14]
  198. .if \el == 0
  199. clear_gp_regs
  200. mrs x21, sp_el0
  201. ldr_this_cpu tsk, __entry_task, x20
  202. msr sp_el0, tsk
  203. /*
  204. * Ensure MDSCR_EL1.SS is clear, since we can unmask debug exceptions
  205. * when scheduling.
  206. */
  207. ldr x19, [tsk, #TSK_TI_FLAGS]
  208. disable_step_tsk x19, x20
  209. /* Check for asynchronous tag check faults in user space */
  210. ldr x0, [tsk, THREAD_SCTLR_USER]
  211. check_mte_async_tcf x22, x23, x0
  212. #ifdef CONFIG_ARM64_PTR_AUTH
  213. alternative_if ARM64_HAS_ADDRESS_AUTH
  214. /*
  215. * Enable IA for in-kernel PAC if the task had it disabled. Although
  216. * this could be implemented with an unconditional MRS which would avoid
  217. * a load, this was measured to be slower on Cortex-A75 and Cortex-A76.
  218. *
  219. * Install the kernel IA key only if IA was enabled in the task. If IA
  220. * was disabled on kernel exit then we would have left the kernel IA
  221. * installed so there is no need to install it again.
  222. */
  223. tbz x0, SCTLR_ELx_ENIA_SHIFT, 1f
  224. __ptrauth_keys_install_kernel_nosync tsk, x20, x22, x23
  225. b 2f
  226. 1:
  227. mrs x0, sctlr_el1
  228. orr x0, x0, SCTLR_ELx_ENIA
  229. msr sctlr_el1, x0
  230. 2:
  231. alternative_else_nop_endif
  232. #endif
  233. apply_ssbd 1, x22, x23
  234. mte_set_kernel_gcr x22, x23
  235. /*
  236. * Any non-self-synchronizing system register updates required for
  237. * kernel entry should be placed before this point.
  238. */
  239. alternative_if ARM64_MTE
  240. isb
  241. b 1f
  242. alternative_else_nop_endif
  243. alternative_if ARM64_HAS_ADDRESS_AUTH
  244. isb
  245. alternative_else_nop_endif
  246. 1:
  247. scs_load_current
  248. .else
  249. add x21, sp, #PT_REGS_SIZE
  250. get_current_task tsk
  251. .endif /* \el == 0 */
  252. mrs x22, elr_el1
  253. mrs x23, spsr_el1
  254. stp lr, x21, [sp, #S_LR]
  255. /*
  256. * For exceptions from EL0, create a final frame record.
  257. * For exceptions from EL1, create a synthetic frame record so the
  258. * interrupted code shows up in the backtrace.
  259. */
  260. .if \el == 0
  261. stp xzr, xzr, [sp, #S_STACKFRAME]
  262. .else
  263. stp x29, x22, [sp, #S_STACKFRAME]
  264. .endif
  265. add x29, sp, #S_STACKFRAME
  266. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  267. alternative_if_not ARM64_HAS_PAN
  268. bl __swpan_entry_el\el
  269. alternative_else_nop_endif
  270. #endif
  271. stp x22, x23, [sp, #S_PC]
  272. /* Not in a syscall by default (el0_svc overwrites for real syscall) */
  273. .if \el == 0
  274. mov w21, #NO_SYSCALL
  275. str w21, [sp, #S_SYSCALLNO]
  276. .endif
  277. #ifdef CONFIG_ARM64_PSEUDO_NMI
  278. /* Save pmr */
  279. alternative_if ARM64_HAS_IRQ_PRIO_MASKING
  280. mrs_s x20, SYS_ICC_PMR_EL1
  281. str x20, [sp, #S_PMR_SAVE]
  282. mov x20, #GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET
  283. msr_s SYS_ICC_PMR_EL1, x20
  284. alternative_else_nop_endif
  285. #endif
  286. /*
  287. * Registers that may be useful after this macro is invoked:
  288. *
  289. * x20 - ICC_PMR_EL1
  290. * x21 - aborted SP
  291. * x22 - aborted PC
  292. * x23 - aborted PSTATE
  293. */
  294. .endm
  295. .macro kernel_exit, el
  296. .if \el != 0
  297. disable_daif
  298. .endif
  299. #ifdef CONFIG_ARM64_PSEUDO_NMI
  300. /* Restore pmr */
  301. alternative_if ARM64_HAS_IRQ_PRIO_MASKING
  302. ldr x20, [sp, #S_PMR_SAVE]
  303. msr_s SYS_ICC_PMR_EL1, x20
  304. mrs_s x21, SYS_ICC_CTLR_EL1
  305. tbz x21, #6, .L__skip_pmr_sync\@ // Check for ICC_CTLR_EL1.PMHE
  306. dsb sy // Ensure priority change is seen by redistributor
  307. .L__skip_pmr_sync\@:
  308. alternative_else_nop_endif
  309. #endif
  310. ldp x21, x22, [sp, #S_PC] // load ELR, SPSR
  311. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  312. alternative_if_not ARM64_HAS_PAN
  313. bl __swpan_exit_el\el
  314. alternative_else_nop_endif
  315. #endif
  316. .if \el == 0
  317. ldr x23, [sp, #S_SP] // load return stack pointer
  318. msr sp_el0, x23
  319. tst x22, #PSR_MODE32_BIT // native task?
  320. b.eq 3f
  321. #ifdef CONFIG_ARM64_ERRATUM_845719
  322. alternative_if ARM64_WORKAROUND_845719
  323. #ifdef CONFIG_PID_IN_CONTEXTIDR
  324. mrs x29, contextidr_el1
  325. msr contextidr_el1, x29
  326. #else
  327. msr contextidr_el1, xzr
  328. #endif
  329. alternative_else_nop_endif
  330. #endif
  331. 3:
  332. scs_save tsk
  333. /* Ignore asynchronous tag check faults in the uaccess routines */
  334. ldr x0, [tsk, THREAD_SCTLR_USER]
  335. clear_mte_async_tcf x0
  336. #ifdef CONFIG_ARM64_PTR_AUTH
  337. alternative_if ARM64_HAS_ADDRESS_AUTH
  338. /*
  339. * IA was enabled for in-kernel PAC. Disable it now if needed, or
  340. * alternatively install the user's IA. All other per-task keys and
  341. * SCTLR bits were updated on task switch.
  342. *
  343. * No kernel C function calls after this.
  344. */
  345. tbz x0, SCTLR_ELx_ENIA_SHIFT, 1f
  346. __ptrauth_keys_install_user tsk, x0, x1, x2
  347. b 2f
  348. 1:
  349. mrs x0, sctlr_el1
  350. bic x0, x0, SCTLR_ELx_ENIA
  351. msr sctlr_el1, x0
  352. 2:
  353. alternative_else_nop_endif
  354. #endif
  355. mte_set_user_gcr tsk, x0, x1
  356. apply_ssbd 0, x0, x1
  357. .endif
  358. msr elr_el1, x21 // set up the return data
  359. msr spsr_el1, x22
  360. ldp x0, x1, [sp, #16 * 0]
  361. ldp x2, x3, [sp, #16 * 1]
  362. ldp x4, x5, [sp, #16 * 2]
  363. ldp x6, x7, [sp, #16 * 3]
  364. ldp x8, x9, [sp, #16 * 4]
  365. ldp x10, x11, [sp, #16 * 5]
  366. ldp x12, x13, [sp, #16 * 6]
  367. ldp x14, x15, [sp, #16 * 7]
  368. ldp x16, x17, [sp, #16 * 8]
  369. ldp x18, x19, [sp, #16 * 9]
  370. ldp x20, x21, [sp, #16 * 10]
  371. ldp x22, x23, [sp, #16 * 11]
  372. ldp x24, x25, [sp, #16 * 12]
  373. ldp x26, x27, [sp, #16 * 13]
  374. ldp x28, x29, [sp, #16 * 14]
  375. .if \el == 0
  376. alternative_if_not ARM64_UNMAP_KERNEL_AT_EL0
  377. ldr lr, [sp, #S_LR]
  378. add sp, sp, #PT_REGS_SIZE // restore sp
  379. eret
  380. alternative_else_nop_endif
  381. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  382. bne 4f
  383. msr far_el1, x29
  384. tramp_alias x30, tramp_exit_native, x29
  385. br x30
  386. 4:
  387. tramp_alias x30, tramp_exit_compat, x29
  388. br x30
  389. #endif
  390. .else
  391. ldr lr, [sp, #S_LR]
  392. add sp, sp, #PT_REGS_SIZE // restore sp
  393. /* Ensure any device/NC reads complete */
  394. alternative_insn nop, "dmb sy", ARM64_WORKAROUND_1508412
  395. eret
  396. .endif
  397. sb
  398. .endm
  399. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  400. /*
  401. * Set the TTBR0 PAN bit in SPSR. When the exception is taken from
  402. * EL0, there is no need to check the state of TTBR0_EL1 since
  403. * accesses are always enabled.
  404. * Note that the meaning of this bit differs from the ARMv8.1 PAN
  405. * feature as all TTBR0_EL1 accesses are disabled, not just those to
  406. * user mappings.
  407. */
  408. SYM_CODE_START_LOCAL(__swpan_entry_el1)
  409. mrs x21, ttbr0_el1
  410. tst x21, #TTBR_ASID_MASK // Check for the reserved ASID
  411. orr x23, x23, #PSR_PAN_BIT // Set the emulated PAN in the saved SPSR
  412. b.eq 1f // TTBR0 access already disabled
  413. and x23, x23, #~PSR_PAN_BIT // Clear the emulated PAN in the saved SPSR
  414. SYM_INNER_LABEL(__swpan_entry_el0, SYM_L_LOCAL)
  415. __uaccess_ttbr0_disable x21
  416. 1: ret
  417. SYM_CODE_END(__swpan_entry_el1)
  418. /*
  419. * Restore access to TTBR0_EL1. If returning to EL0, no need for SPSR
  420. * PAN bit checking.
  421. */
  422. SYM_CODE_START_LOCAL(__swpan_exit_el1)
  423. tbnz x22, #22, 1f // Skip re-enabling TTBR0 access if the PSR_PAN_BIT is set
  424. __uaccess_ttbr0_enable x0, x1
  425. 1: and x22, x22, #~PSR_PAN_BIT // ARMv8.0 CPUs do not understand this bit
  426. ret
  427. SYM_CODE_END(__swpan_exit_el1)
  428. SYM_CODE_START_LOCAL(__swpan_exit_el0)
  429. __uaccess_ttbr0_enable x0, x1
  430. /*
  431. * Enable errata workarounds only if returning to user. The only
  432. * workaround currently required for TTBR0_EL1 changes are for the
  433. * Cavium erratum 27456 (broadcast TLBI instructions may cause I-cache
  434. * corruption).
  435. */
  436. b post_ttbr_update_workaround
  437. SYM_CODE_END(__swpan_exit_el0)
  438. #endif
  439. /* GPRs used by entry code */
  440. tsk .req x28 // current thread_info
  441. .text
  442. /*
  443. * Exception vectors.
  444. */
  445. .pushsection ".entry.text", "ax"
  446. .align 11
  447. SYM_CODE_START(vectors)
  448. kernel_ventry 1, t, 64, sync // Synchronous EL1t
  449. kernel_ventry 1, t, 64, irq // IRQ EL1t
  450. kernel_ventry 1, t, 64, fiq // FIQ EL1t
  451. kernel_ventry 1, t, 64, error // Error EL1t
  452. kernel_ventry 1, h, 64, sync // Synchronous EL1h
  453. kernel_ventry 1, h, 64, irq // IRQ EL1h
  454. kernel_ventry 1, h, 64, fiq // FIQ EL1h
  455. kernel_ventry 1, h, 64, error // Error EL1h
  456. kernel_ventry 0, t, 64, sync // Synchronous 64-bit EL0
  457. kernel_ventry 0, t, 64, irq // IRQ 64-bit EL0
  458. kernel_ventry 0, t, 64, fiq // FIQ 64-bit EL0
  459. kernel_ventry 0, t, 64, error // Error 64-bit EL0
  460. kernel_ventry 0, t, 32, sync // Synchronous 32-bit EL0
  461. kernel_ventry 0, t, 32, irq // IRQ 32-bit EL0
  462. kernel_ventry 0, t, 32, fiq // FIQ 32-bit EL0
  463. kernel_ventry 0, t, 32, error // Error 32-bit EL0
  464. SYM_CODE_END(vectors)
  465. #ifdef CONFIG_VMAP_STACK
  466. SYM_CODE_START_LOCAL(__bad_stack)
  467. /*
  468. * We detected an overflow in kernel_ventry, which switched to the
  469. * overflow stack. Stash the exception regs, and head to our overflow
  470. * handler.
  471. */
  472. /* Restore the original x0 value */
  473. mrs x0, tpidrro_el0
  474. /*
  475. * Store the original GPRs to the new stack. The orginal SP (minus
  476. * PT_REGS_SIZE) was stashed in tpidr_el0 by kernel_ventry.
  477. */
  478. sub sp, sp, #PT_REGS_SIZE
  479. kernel_entry 1
  480. mrs x0, tpidr_el0
  481. add x0, x0, #PT_REGS_SIZE
  482. str x0, [sp, #S_SP]
  483. /* Stash the regs for handle_bad_stack */
  484. mov x0, sp
  485. /* Time to die */
  486. bl handle_bad_stack
  487. ASM_BUG()
  488. SYM_CODE_END(__bad_stack)
  489. #endif /* CONFIG_VMAP_STACK */
  490. .macro entry_handler el:req, ht:req, regsize:req, label:req
  491. SYM_CODE_START_LOCAL(el\el\ht\()_\regsize\()_\label)
  492. kernel_entry \el, \regsize
  493. mov x0, sp
  494. bl el\el\ht\()_\regsize\()_\label\()_handler
  495. .if \el == 0
  496. b ret_to_user
  497. .else
  498. b ret_to_kernel
  499. .endif
  500. SYM_CODE_END(el\el\ht\()_\regsize\()_\label)
  501. .endm
  502. /*
  503. * Early exception handlers
  504. */
  505. entry_handler 1, t, 64, sync
  506. entry_handler 1, t, 64, irq
  507. entry_handler 1, t, 64, fiq
  508. entry_handler 1, t, 64, error
  509. entry_handler 1, h, 64, sync
  510. entry_handler 1, h, 64, irq
  511. entry_handler 1, h, 64, fiq
  512. entry_handler 1, h, 64, error
  513. entry_handler 0, t, 64, sync
  514. entry_handler 0, t, 64, irq
  515. entry_handler 0, t, 64, fiq
  516. entry_handler 0, t, 64, error
  517. entry_handler 0, t, 32, sync
  518. entry_handler 0, t, 32, irq
  519. entry_handler 0, t, 32, fiq
  520. entry_handler 0, t, 32, error
  521. SYM_CODE_START_LOCAL(ret_to_kernel)
  522. kernel_exit 1
  523. SYM_CODE_END(ret_to_kernel)
  524. SYM_CODE_START_LOCAL(ret_to_user)
  525. ldr x19, [tsk, #TSK_TI_FLAGS] // re-check for single-step
  526. enable_step_tsk x19, x2
  527. #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
  528. bl stackleak_erase_on_task_stack
  529. #endif
  530. kernel_exit 0
  531. SYM_CODE_END(ret_to_user)
  532. .popsection // .entry.text
  533. // Move from tramp_pg_dir to swapper_pg_dir
  534. .macro tramp_map_kernel, tmp
  535. mrs \tmp, ttbr1_el1
  536. add \tmp, \tmp, #TRAMP_SWAPPER_OFFSET
  537. bic \tmp, \tmp, #USER_ASID_FLAG
  538. msr ttbr1_el1, \tmp
  539. #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
  540. alternative_if ARM64_WORKAROUND_QCOM_FALKOR_E1003
  541. /* ASID already in \tmp[63:48] */
  542. movk \tmp, #:abs_g2_nc:(TRAMP_VALIAS >> 12)
  543. movk \tmp, #:abs_g1_nc:(TRAMP_VALIAS >> 12)
  544. /* 2MB boundary containing the vectors, so we nobble the walk cache */
  545. movk \tmp, #:abs_g0_nc:((TRAMP_VALIAS & ~(SZ_2M - 1)) >> 12)
  546. isb
  547. tlbi vae1, \tmp
  548. dsb nsh
  549. alternative_else_nop_endif
  550. #endif /* CONFIG_QCOM_FALKOR_ERRATUM_1003 */
  551. .endm
  552. // Move from swapper_pg_dir to tramp_pg_dir
  553. .macro tramp_unmap_kernel, tmp
  554. mrs \tmp, ttbr1_el1
  555. sub \tmp, \tmp, #TRAMP_SWAPPER_OFFSET
  556. orr \tmp, \tmp, #USER_ASID_FLAG
  557. msr ttbr1_el1, \tmp
  558. /*
  559. * We avoid running the post_ttbr_update_workaround here because
  560. * it's only needed by Cavium ThunderX, which requires KPTI to be
  561. * disabled.
  562. */
  563. .endm
  564. .macro tramp_data_read_var dst, var
  565. #ifdef CONFIG_RELOCATABLE
  566. ldr \dst, .L__tramp_data_\var
  567. .ifndef .L__tramp_data_\var
  568. .pushsection ".entry.tramp.rodata", "a", %progbits
  569. .align 3
  570. .L__tramp_data_\var:
  571. .quad \var
  572. .popsection
  573. .endif
  574. #else
  575. /*
  576. * As !RELOCATABLE implies !RANDOMIZE_BASE the address is always a
  577. * compile time constant (and hence not secret and not worth hiding).
  578. *
  579. * As statically allocated kernel code and data always live in the top
  580. * 47 bits of the address space we can sign-extend bit 47 and avoid an
  581. * instruction to load the upper 16 bits (which must be 0xFFFF).
  582. */
  583. movz \dst, :abs_g2_s:\var
  584. movk \dst, :abs_g1_nc:\var
  585. movk \dst, :abs_g0_nc:\var
  586. #endif
  587. .endm
  588. #define BHB_MITIGATION_NONE 0
  589. #define BHB_MITIGATION_LOOP 1
  590. #define BHB_MITIGATION_FW 2
  591. #define BHB_MITIGATION_INSN 3
  592. .macro tramp_ventry, vector_start, regsize, kpti, bhb
  593. .align 7
  594. 1:
  595. .if \regsize == 64
  596. msr tpidrro_el0, x30 // Restored in kernel_ventry
  597. .endif
  598. .if \bhb == BHB_MITIGATION_LOOP
  599. /*
  600. * This sequence must appear before the first indirect branch. i.e. the
  601. * ret out of tramp_ventry. It appears here because x30 is free.
  602. */
  603. __mitigate_spectre_bhb_loop x30
  604. .endif // \bhb == BHB_MITIGATION_LOOP
  605. .if \bhb == BHB_MITIGATION_INSN
  606. clearbhb
  607. isb
  608. .endif // \bhb == BHB_MITIGATION_INSN
  609. .if \kpti == 1
  610. /*
  611. * Defend against branch aliasing attacks by pushing a dummy
  612. * entry onto the return stack and using a RET instruction to
  613. * enter the full-fat kernel vectors.
  614. */
  615. bl 2f
  616. b .
  617. 2:
  618. tramp_map_kernel x30
  619. alternative_insn isb, nop, ARM64_WORKAROUND_QCOM_FALKOR_E1003
  620. tramp_data_read_var x30, vectors
  621. alternative_if_not ARM64_WORKAROUND_CAVIUM_TX2_219_PRFM
  622. prfm plil1strm, [x30, #(1b - \vector_start)]
  623. alternative_else_nop_endif
  624. msr vbar_el1, x30
  625. isb
  626. .else
  627. adr_l x30, vectors
  628. .endif // \kpti == 1
  629. .if \bhb == BHB_MITIGATION_FW
  630. /*
  631. * The firmware sequence must appear before the first indirect branch.
  632. * i.e. the ret out of tramp_ventry. But it also needs the stack to be
  633. * mapped to save/restore the registers the SMC clobbers.
  634. */
  635. __mitigate_spectre_bhb_fw
  636. .endif // \bhb == BHB_MITIGATION_FW
  637. add x30, x30, #(1b - \vector_start + 4)
  638. ret
  639. .org 1b + 128 // Did we overflow the ventry slot?
  640. .endm
  641. .macro tramp_exit, regsize = 64
  642. tramp_data_read_var x30, this_cpu_vector
  643. get_this_cpu_offset x29
  644. ldr x30, [x30, x29]
  645. msr vbar_el1, x30
  646. ldr lr, [sp, #S_LR]
  647. tramp_unmap_kernel x29
  648. .if \regsize == 64
  649. mrs x29, far_el1
  650. .endif
  651. add sp, sp, #PT_REGS_SIZE // restore sp
  652. eret
  653. sb
  654. .endm
  655. .macro generate_tramp_vector, kpti, bhb
  656. .Lvector_start\@:
  657. .space 0x400
  658. .rept 4
  659. tramp_ventry .Lvector_start\@, 64, \kpti, \bhb
  660. .endr
  661. .rept 4
  662. tramp_ventry .Lvector_start\@, 32, \kpti, \bhb
  663. .endr
  664. .endm
  665. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  666. /*
  667. * Exception vectors trampoline.
  668. * The order must match __bp_harden_el1_vectors and the
  669. * arm64_bp_harden_el1_vectors enum.
  670. */
  671. .pushsection ".entry.tramp.text", "ax"
  672. .align 11
  673. SYM_CODE_START_NOALIGN(tramp_vectors)
  674. #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
  675. generate_tramp_vector kpti=1, bhb=BHB_MITIGATION_LOOP
  676. generate_tramp_vector kpti=1, bhb=BHB_MITIGATION_FW
  677. generate_tramp_vector kpti=1, bhb=BHB_MITIGATION_INSN
  678. #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
  679. generate_tramp_vector kpti=1, bhb=BHB_MITIGATION_NONE
  680. SYM_CODE_END(tramp_vectors)
  681. SYM_CODE_START(tramp_exit_native)
  682. tramp_exit
  683. SYM_CODE_END(tramp_exit_native)
  684. SYM_CODE_START(tramp_exit_compat)
  685. tramp_exit 32
  686. SYM_CODE_END(tramp_exit_compat)
  687. .popsection // .entry.tramp.text
  688. #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
  689. /*
  690. * Exception vectors for spectre mitigations on entry from EL1 when
  691. * kpti is not in use.
  692. */
  693. .macro generate_el1_vector, bhb
  694. .Lvector_start\@:
  695. kernel_ventry 1, t, 64, sync // Synchronous EL1t
  696. kernel_ventry 1, t, 64, irq // IRQ EL1t
  697. kernel_ventry 1, t, 64, fiq // FIQ EL1h
  698. kernel_ventry 1, t, 64, error // Error EL1t
  699. kernel_ventry 1, h, 64, sync // Synchronous EL1h
  700. kernel_ventry 1, h, 64, irq // IRQ EL1h
  701. kernel_ventry 1, h, 64, fiq // FIQ EL1h
  702. kernel_ventry 1, h, 64, error // Error EL1h
  703. .rept 4
  704. tramp_ventry .Lvector_start\@, 64, 0, \bhb
  705. .endr
  706. .rept 4
  707. tramp_ventry .Lvector_start\@, 32, 0, \bhb
  708. .endr
  709. .endm
  710. /* The order must match tramp_vecs and the arm64_bp_harden_el1_vectors enum. */
  711. .pushsection ".entry.text", "ax"
  712. .align 11
  713. SYM_CODE_START(__bp_harden_el1_vectors)
  714. #ifdef CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
  715. generate_el1_vector bhb=BHB_MITIGATION_LOOP
  716. generate_el1_vector bhb=BHB_MITIGATION_FW
  717. generate_el1_vector bhb=BHB_MITIGATION_INSN
  718. #endif /* CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY */
  719. SYM_CODE_END(__bp_harden_el1_vectors)
  720. .popsection
  721. /*
  722. * Register switch for AArch64. The callee-saved registers need to be saved
  723. * and restored. On entry:
  724. * x0 = previous task_struct (must be preserved across the switch)
  725. * x1 = next task_struct
  726. * Previous and next are guaranteed not to be the same.
  727. *
  728. */
  729. SYM_FUNC_START(cpu_switch_to)
  730. mov x10, #THREAD_CPU_CONTEXT
  731. add x8, x0, x10
  732. mov x9, sp
  733. stp x19, x20, [x8], #16 // store callee-saved registers
  734. stp x21, x22, [x8], #16
  735. stp x23, x24, [x8], #16
  736. stp x25, x26, [x8], #16
  737. stp x27, x28, [x8], #16
  738. stp x29, x9, [x8], #16
  739. str lr, [x8]
  740. add x8, x1, x10
  741. ldp x19, x20, [x8], #16 // restore callee-saved registers
  742. ldp x21, x22, [x8], #16
  743. ldp x23, x24, [x8], #16
  744. ldp x25, x26, [x8], #16
  745. ldp x27, x28, [x8], #16
  746. ldp x29, x9, [x8], #16
  747. ldr lr, [x8]
  748. mov sp, x9
  749. msr sp_el0, x1
  750. ptrauth_keys_install_kernel x1, x8, x9, x10
  751. scs_save x0
  752. scs_load_current
  753. ret
  754. SYM_FUNC_END(cpu_switch_to)
  755. NOKPROBE(cpu_switch_to)
  756. /*
  757. * This is how we return from a fork.
  758. */
  759. SYM_CODE_START(ret_from_fork)
  760. bl schedule_tail
  761. cbz x19, 1f // not a kernel thread
  762. mov x0, x20
  763. blr x19
  764. 1: get_current_task tsk
  765. mov x0, sp
  766. bl asm_exit_to_user_mode
  767. b ret_to_user
  768. SYM_CODE_END(ret_from_fork)
  769. NOKPROBE(ret_from_fork)
  770. /*
  771. * void call_on_irq_stack(struct pt_regs *regs,
  772. * void (*func)(struct pt_regs *));
  773. *
  774. * Calls func(regs) using this CPU's irq stack and shadow irq stack.
  775. */
  776. SYM_FUNC_START(call_on_irq_stack)
  777. #ifdef CONFIG_SHADOW_CALL_STACK
  778. get_current_task x16
  779. scs_save x16
  780. ldr_this_cpu scs_sp, irq_shadow_call_stack_ptr, x17
  781. #endif
  782. /* Create a frame record to save our LR and SP (implicit in FP) */
  783. stp x29, x30, [sp, #-16]!
  784. mov x29, sp
  785. ldr_this_cpu x16, irq_stack_ptr, x17
  786. /* Move to the new stack and call the function there */
  787. add sp, x16, #IRQ_STACK_SIZE
  788. blr x1
  789. /*
  790. * Restore the SP from the FP, and restore the FP and LR from the frame
  791. * record.
  792. */
  793. mov sp, x29
  794. ldp x29, x30, [sp], #16
  795. scs_load_current
  796. ret
  797. SYM_FUNC_END(call_on_irq_stack)
  798. NOKPROBE(call_on_irq_stack)
  799. #ifdef CONFIG_ARM_SDE_INTERFACE
  800. #include <asm/sdei.h>
  801. #include <uapi/linux/arm_sdei.h>
  802. .macro sdei_handler_exit exit_mode
  803. /* On success, this call never returns... */
  804. cmp \exit_mode, #SDEI_EXIT_SMC
  805. b.ne 99f
  806. smc #0
  807. b .
  808. 99: hvc #0
  809. b .
  810. .endm
  811. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  812. /*
  813. * The regular SDEI entry point may have been unmapped along with the rest of
  814. * the kernel. This trampoline restores the kernel mapping to make the x1 memory
  815. * argument accessible.
  816. *
  817. * This clobbers x4, __sdei_handler() will restore this from firmware's
  818. * copy.
  819. */
  820. .pushsection ".entry.tramp.text", "ax"
  821. SYM_CODE_START(__sdei_asm_entry_trampoline)
  822. mrs x4, ttbr1_el1
  823. tbz x4, #USER_ASID_BIT, 1f
  824. tramp_map_kernel tmp=x4
  825. isb
  826. mov x4, xzr
  827. /*
  828. * Remember whether to unmap the kernel on exit.
  829. */
  830. 1: str x4, [x1, #(SDEI_EVENT_INTREGS + S_SDEI_TTBR1)]
  831. tramp_data_read_var x4, __sdei_asm_handler
  832. br x4
  833. SYM_CODE_END(__sdei_asm_entry_trampoline)
  834. NOKPROBE(__sdei_asm_entry_trampoline)
  835. /*
  836. * Make the exit call and restore the original ttbr1_el1
  837. *
  838. * x0 & x1: setup for the exit API call
  839. * x2: exit_mode
  840. * x4: struct sdei_registered_event argument from registration time.
  841. */
  842. SYM_CODE_START(__sdei_asm_exit_trampoline)
  843. ldr x4, [x4, #(SDEI_EVENT_INTREGS + S_SDEI_TTBR1)]
  844. cbnz x4, 1f
  845. tramp_unmap_kernel tmp=x4
  846. 1: sdei_handler_exit exit_mode=x2
  847. SYM_CODE_END(__sdei_asm_exit_trampoline)
  848. NOKPROBE(__sdei_asm_exit_trampoline)
  849. .popsection // .entry.tramp.text
  850. #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
  851. /*
  852. * Software Delegated Exception entry point.
  853. *
  854. * x0: Event number
  855. * x1: struct sdei_registered_event argument from registration time.
  856. * x2: interrupted PC
  857. * x3: interrupted PSTATE
  858. * x4: maybe clobbered by the trampoline
  859. *
  860. * Firmware has preserved x0->x17 for us, we must save/restore the rest to
  861. * follow SMC-CC. We save (or retrieve) all the registers as the handler may
  862. * want them.
  863. */
  864. SYM_CODE_START(__sdei_asm_handler)
  865. stp x2, x3, [x1, #SDEI_EVENT_INTREGS + S_PC]
  866. stp x4, x5, [x1, #SDEI_EVENT_INTREGS + 16 * 2]
  867. stp x6, x7, [x1, #SDEI_EVENT_INTREGS + 16 * 3]
  868. stp x8, x9, [x1, #SDEI_EVENT_INTREGS + 16 * 4]
  869. stp x10, x11, [x1, #SDEI_EVENT_INTREGS + 16 * 5]
  870. stp x12, x13, [x1, #SDEI_EVENT_INTREGS + 16 * 6]
  871. stp x14, x15, [x1, #SDEI_EVENT_INTREGS + 16 * 7]
  872. stp x16, x17, [x1, #SDEI_EVENT_INTREGS + 16 * 8]
  873. stp x18, x19, [x1, #SDEI_EVENT_INTREGS + 16 * 9]
  874. stp x20, x21, [x1, #SDEI_EVENT_INTREGS + 16 * 10]
  875. stp x22, x23, [x1, #SDEI_EVENT_INTREGS + 16 * 11]
  876. stp x24, x25, [x1, #SDEI_EVENT_INTREGS + 16 * 12]
  877. stp x26, x27, [x1, #SDEI_EVENT_INTREGS + 16 * 13]
  878. stp x28, x29, [x1, #SDEI_EVENT_INTREGS + 16 * 14]
  879. mov x4, sp
  880. stp lr, x4, [x1, #SDEI_EVENT_INTREGS + S_LR]
  881. mov x19, x1
  882. /* Store the registered-event for crash_smp_send_stop() */
  883. ldrb w4, [x19, #SDEI_EVENT_PRIORITY]
  884. cbnz w4, 1f
  885. adr_this_cpu dst=x5, sym=sdei_active_normal_event, tmp=x6
  886. b 2f
  887. 1: adr_this_cpu dst=x5, sym=sdei_active_critical_event, tmp=x6
  888. 2: str x19, [x5]
  889. #ifdef CONFIG_VMAP_STACK
  890. /*
  891. * entry.S may have been using sp as a scratch register, find whether
  892. * this is a normal or critical event and switch to the appropriate
  893. * stack for this CPU.
  894. */
  895. cbnz w4, 1f
  896. ldr_this_cpu dst=x5, sym=sdei_stack_normal_ptr, tmp=x6
  897. b 2f
  898. 1: ldr_this_cpu dst=x5, sym=sdei_stack_critical_ptr, tmp=x6
  899. 2: mov x6, #SDEI_STACK_SIZE
  900. add x5, x5, x6
  901. mov sp, x5
  902. #endif
  903. #ifdef CONFIG_SHADOW_CALL_STACK
  904. /* Use a separate shadow call stack for normal and critical events */
  905. cbnz w4, 3f
  906. ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_normal_ptr, tmp=x6
  907. b 4f
  908. 3: ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical_ptr, tmp=x6
  909. 4:
  910. #endif
  911. /*
  912. * We may have interrupted userspace, or a guest, or exit-from or
  913. * return-to either of these. We can't trust sp_el0, restore it.
  914. */
  915. mrs x28, sp_el0
  916. ldr_this_cpu dst=x0, sym=__entry_task, tmp=x1
  917. msr sp_el0, x0
  918. /* If we interrupted the kernel point to the previous stack/frame. */
  919. and x0, x3, #0xc
  920. mrs x1, CurrentEL
  921. cmp x0, x1
  922. csel x29, x29, xzr, eq // fp, or zero
  923. csel x4, x2, xzr, eq // elr, or zero
  924. stp x29, x4, [sp, #-16]!
  925. mov x29, sp
  926. add x0, x19, #SDEI_EVENT_INTREGS
  927. mov x1, x19
  928. bl __sdei_handler
  929. msr sp_el0, x28
  930. /* restore regs >x17 that we clobbered */
  931. mov x4, x19 // keep x4 for __sdei_asm_exit_trampoline
  932. ldp x28, x29, [x4, #SDEI_EVENT_INTREGS + 16 * 14]
  933. ldp x18, x19, [x4, #SDEI_EVENT_INTREGS + 16 * 9]
  934. ldp lr, x1, [x4, #SDEI_EVENT_INTREGS + S_LR]
  935. mov sp, x1
  936. mov x1, x0 // address to complete_and_resume
  937. /* x0 = (x0 <= SDEI_EV_FAILED) ?
  938. * EVENT_COMPLETE:EVENT_COMPLETE_AND_RESUME
  939. */
  940. cmp x0, #SDEI_EV_FAILED
  941. mov_q x2, SDEI_1_0_FN_SDEI_EVENT_COMPLETE
  942. mov_q x3, SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME
  943. csel x0, x2, x3, ls
  944. ldr_l x2, sdei_exit_mode
  945. /* Clear the registered-event seen by crash_smp_send_stop() */
  946. ldrb w3, [x4, #SDEI_EVENT_PRIORITY]
  947. cbnz w3, 1f
  948. adr_this_cpu dst=x5, sym=sdei_active_normal_event, tmp=x6
  949. b 2f
  950. 1: adr_this_cpu dst=x5, sym=sdei_active_critical_event, tmp=x6
  951. 2: str xzr, [x5]
  952. alternative_if_not ARM64_UNMAP_KERNEL_AT_EL0
  953. sdei_handler_exit exit_mode=x2
  954. alternative_else_nop_endif
  955. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  956. tramp_alias dst=x5, sym=__sdei_asm_exit_trampoline, tmp=x3
  957. br x5
  958. #endif
  959. SYM_CODE_END(__sdei_asm_handler)
  960. NOKPROBE(__sdei_asm_handler)
  961. SYM_CODE_START(__sdei_handler_abort)
  962. mov_q x0, SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME
  963. adr x1, 1f
  964. ldr_l x2, sdei_exit_mode
  965. sdei_handler_exit exit_mode=x2
  966. // exit the handler and jump to the next instruction.
  967. // Exit will stomp x0-x17, PSTATE, ELR_ELx, and SPSR_ELx.
  968. 1: ret
  969. SYM_CODE_END(__sdei_handler_abort)
  970. NOKPROBE(__sdei_handler_abort)
  971. #endif /* CONFIG_ARM_SDE_INTERFACE */