entry.S 31 KB

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