entry.S 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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/arm-smccc.h>
  21. #include <linux/init.h>
  22. #include <linux/linkage.h>
  23. #include <asm/alternative.h>
  24. #include <asm/assembler.h>
  25. #include <asm/asm-offsets.h>
  26. #include <asm/cpufeature.h>
  27. #include <asm/errno.h>
  28. #include <asm/esr.h>
  29. #include <asm/irq.h>
  30. #include <asm/memory.h>
  31. #include <asm/mmu.h>
  32. #include <asm/processor.h>
  33. #include <asm/thread_info.h>
  34. #include <asm/asm-uaccess.h>
  35. #include <asm/unistd.h>
  36. #include <asm/kernel-pgtable.h>
  37. /*
  38. * Context tracking subsystem. Used to instrument transitions
  39. * between user and kernel mode.
  40. */
  41. .macro ct_user_exit, syscall = 0
  42. #ifdef CONFIG_CONTEXT_TRACKING
  43. bl context_tracking_user_exit
  44. .if \syscall == 1
  45. /*
  46. * Save/restore needed during syscalls. Restore syscall arguments from
  47. * the values already saved on stack during kernel_entry.
  48. */
  49. ldp x0, x1, [sp]
  50. ldp x2, x3, [sp, #S_X2]
  51. ldp x4, x5, [sp, #S_X4]
  52. ldp x6, x7, [sp, #S_X6]
  53. .endif
  54. #endif
  55. .endm
  56. .macro ct_user_enter
  57. #ifdef CONFIG_CONTEXT_TRACKING
  58. bl context_tracking_user_enter
  59. #endif
  60. .endm
  61. /*
  62. * Bad Abort numbers
  63. *-----------------
  64. */
  65. #define BAD_SYNC 0
  66. #define BAD_IRQ 1
  67. #define BAD_FIQ 2
  68. #define BAD_ERROR 3
  69. .macro kernel_ventry, el, label, regsize = 64
  70. .align 7
  71. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  72. alternative_if ARM64_UNMAP_KERNEL_AT_EL0
  73. .if \el == 0
  74. .if \regsize == 64
  75. mrs x30, tpidrro_el0
  76. msr tpidrro_el0, xzr
  77. .else
  78. mov x30, xzr
  79. .endif
  80. .endif
  81. alternative_else_nop_endif
  82. #endif
  83. sub sp, sp, #S_FRAME_SIZE
  84. b el\()\el\()_\label
  85. .endm
  86. .macro tramp_alias, dst, sym
  87. mov_q \dst, TRAMP_VALIAS
  88. add \dst, \dst, #(\sym - .entry.tramp.text)
  89. .endm
  90. // This macro corrupts x0-x3. It is the caller's duty
  91. // to save/restore them if required.
  92. .macro apply_ssbd, state, targ, tmp1, tmp2
  93. #ifdef CONFIG_ARM64_SSBD
  94. alternative_cb arm64_enable_wa2_handling
  95. b \targ
  96. alternative_cb_end
  97. ldr_this_cpu \tmp2, arm64_ssbd_callback_required, \tmp1
  98. cbz \tmp2, \targ
  99. ldr \tmp2, [tsk, #TI_FLAGS]
  100. tbnz \tmp2, #TIF_SSBD, \targ
  101. mov w0, #ARM_SMCCC_ARCH_WORKAROUND_2
  102. mov w1, #\state
  103. alternative_cb arm64_update_smccc_conduit
  104. nop // Patched to SMC/HVC #0
  105. alternative_cb_end
  106. #endif
  107. .endm
  108. .macro kernel_entry, el, regsize = 64
  109. .if \regsize == 32
  110. mov w0, w0 // zero upper 32 bits of x0
  111. .endif
  112. stp x0, x1, [sp, #16 * 0]
  113. stp x2, x3, [sp, #16 * 1]
  114. stp x4, x5, [sp, #16 * 2]
  115. stp x6, x7, [sp, #16 * 3]
  116. stp x8, x9, [sp, #16 * 4]
  117. stp x10, x11, [sp, #16 * 5]
  118. stp x12, x13, [sp, #16 * 6]
  119. stp x14, x15, [sp, #16 * 7]
  120. stp x16, x17, [sp, #16 * 8]
  121. stp x18, x19, [sp, #16 * 9]
  122. stp x20, x21, [sp, #16 * 10]
  123. stp x22, x23, [sp, #16 * 11]
  124. stp x24, x25, [sp, #16 * 12]
  125. stp x26, x27, [sp, #16 * 13]
  126. stp x28, x29, [sp, #16 * 14]
  127. .if \el == 0
  128. mrs x21, sp_el0
  129. mov tsk, sp
  130. and tsk, tsk, #~(THREAD_SIZE - 1) // Ensure MDSCR_EL1.SS is clear,
  131. ldr x19, [tsk, #TI_FLAGS] // since we can unmask debug
  132. disable_step_tsk x19, x20 // exceptions when scheduling.
  133. apply_ssbd 1, 1f, x22, x23
  134. #ifdef CONFIG_ARM64_SSBD
  135. ldp x0, x1, [sp, #16 * 0]
  136. ldp x2, x3, [sp, #16 * 1]
  137. #endif
  138. 1:
  139. mov x29, xzr // fp pointed to user-space
  140. .else
  141. add x21, sp, #S_FRAME_SIZE
  142. get_thread_info tsk
  143. /* Save the task's original addr_limit and set USER_DS */
  144. ldr x20, [tsk, #TI_ADDR_LIMIT]
  145. str x20, [sp, #S_ORIG_ADDR_LIMIT]
  146. mov x20, #USER_DS
  147. str x20, [tsk, #TI_ADDR_LIMIT]
  148. /* No need to reset PSTATE.UAO, hardware's already set it to 0 for us */
  149. .endif /* \el == 0 */
  150. mrs x22, elr_el1
  151. mrs x23, spsr_el1
  152. stp lr, x21, [sp, #S_LR]
  153. stp x22, x23, [sp, #S_PC]
  154. /*
  155. * Set syscallno to -1 by default (overridden later if real syscall).
  156. */
  157. .if \el == 0
  158. mvn x21, xzr
  159. str x21, [sp, #S_SYSCALLNO]
  160. .endif
  161. /*
  162. * Set sp_el0 to current thread_info.
  163. */
  164. .if \el == 0
  165. msr sp_el0, tsk
  166. .endif
  167. /*
  168. * Registers that may be useful after this macro is invoked:
  169. *
  170. * x21 - aborted SP
  171. * x22 - aborted PC
  172. * x23 - aborted PSTATE
  173. */
  174. .endm
  175. .macro kernel_exit, el
  176. .if \el != 0
  177. /* Restore the task's original addr_limit. */
  178. ldr x20, [sp, #S_ORIG_ADDR_LIMIT]
  179. str x20, [tsk, #TI_ADDR_LIMIT]
  180. /* No need to restore UAO, it will be restored from SPSR_EL1 */
  181. .endif
  182. ldp x21, x22, [sp, #S_PC] // load ELR, SPSR
  183. .if \el == 0
  184. ct_user_enter
  185. ldr x23, [sp, #S_SP] // load return stack pointer
  186. msr sp_el0, x23
  187. tst x22, #PSR_MODE32_BIT // native task?
  188. b.eq 3f
  189. #ifdef CONFIG_ARM64_ERRATUM_845719
  190. alternative_if ARM64_WORKAROUND_845719
  191. #ifdef CONFIG_PID_IN_CONTEXTIDR
  192. mrs x29, contextidr_el1
  193. msr contextidr_el1, x29
  194. #else
  195. msr contextidr_el1, xzr
  196. #endif
  197. alternative_else_nop_endif
  198. #endif
  199. 3:
  200. apply_ssbd 0, 5f, x0, x1
  201. 5:
  202. .endif
  203. msr elr_el1, x21 // set up the return data
  204. msr spsr_el1, x22
  205. ldp x0, x1, [sp, #16 * 0]
  206. ldp x2, x3, [sp, #16 * 1]
  207. ldp x4, x5, [sp, #16 * 2]
  208. ldp x6, x7, [sp, #16 * 3]
  209. ldp x8, x9, [sp, #16 * 4]
  210. ldp x10, x11, [sp, #16 * 5]
  211. ldp x12, x13, [sp, #16 * 6]
  212. ldp x14, x15, [sp, #16 * 7]
  213. ldp x16, x17, [sp, #16 * 8]
  214. ldp x18, x19, [sp, #16 * 9]
  215. ldp x20, x21, [sp, #16 * 10]
  216. ldp x22, x23, [sp, #16 * 11]
  217. ldp x24, x25, [sp, #16 * 12]
  218. ldp x26, x27, [sp, #16 * 13]
  219. ldp x28, x29, [sp, #16 * 14]
  220. ldr lr, [sp, #S_LR]
  221. add sp, sp, #S_FRAME_SIZE // restore sp
  222. .if \el == 0
  223. alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0
  224. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  225. bne 4f
  226. msr far_el1, x30
  227. tramp_alias x30, tramp_exit_native
  228. br x30
  229. 4:
  230. tramp_alias x30, tramp_exit_compat
  231. br x30
  232. #endif
  233. .else
  234. eret
  235. .endif
  236. .endm
  237. .macro get_thread_info, rd
  238. mrs \rd, sp_el0
  239. .endm
  240. .macro irq_stack_entry
  241. mov x19, sp // preserve the original sp
  242. /*
  243. * Compare sp with the current thread_info, if the top
  244. * ~(THREAD_SIZE - 1) bits match, we are on a task stack, and
  245. * should switch to the irq stack.
  246. */
  247. and x25, x19, #~(THREAD_SIZE - 1)
  248. cmp x25, tsk
  249. b.ne 9998f
  250. adr_this_cpu x25, irq_stack, x26
  251. mov x26, #IRQ_STACK_START_SP
  252. add x26, x25, x26
  253. /* switch to the irq stack */
  254. mov sp, x26
  255. /*
  256. * Add a dummy stack frame, this non-standard format is fixed up
  257. * by unwind_frame()
  258. */
  259. stp x29, x19, [sp, #-16]!
  260. mov x29, sp
  261. 9998:
  262. .endm
  263. /*
  264. * x19 should be preserved between irq_stack_entry and
  265. * irq_stack_exit.
  266. */
  267. .macro irq_stack_exit
  268. mov sp, x19
  269. .endm
  270. /*
  271. * These are the registers used in the syscall handler, and allow us to
  272. * have in theory up to 7 arguments to a function - x0 to x6.
  273. *
  274. * x7 is reserved for the system call number in 32-bit mode.
  275. */
  276. sc_nr .req x25 // number of system calls
  277. scno .req x26 // syscall number
  278. stbl .req x27 // syscall table pointer
  279. tsk .req x28 // current thread_info
  280. /*
  281. * Interrupt handling.
  282. */
  283. .macro irq_handler
  284. ldr_l x1, handle_arch_irq
  285. mov x0, sp
  286. irq_stack_entry
  287. blr x1
  288. irq_stack_exit
  289. .endm
  290. .text
  291. /*
  292. * Exception vectors.
  293. */
  294. .pushsection ".entry.text", "ax"
  295. .align 11
  296. ENTRY(vectors)
  297. kernel_ventry 1, sync_invalid // Synchronous EL1t
  298. kernel_ventry 1, irq_invalid // IRQ EL1t
  299. kernel_ventry 1, fiq_invalid // FIQ EL1t
  300. kernel_ventry 1, error_invalid // Error EL1t
  301. kernel_ventry 1, sync // Synchronous EL1h
  302. kernel_ventry 1, irq // IRQ EL1h
  303. kernel_ventry 1, fiq_invalid // FIQ EL1h
  304. kernel_ventry 1, error_invalid // Error EL1h
  305. kernel_ventry 0, sync // Synchronous 64-bit EL0
  306. kernel_ventry 0, irq // IRQ 64-bit EL0
  307. kernel_ventry 0, fiq_invalid // FIQ 64-bit EL0
  308. kernel_ventry 0, error_invalid // Error 64-bit EL0
  309. #ifdef CONFIG_COMPAT
  310. kernel_ventry 0, sync_compat, 32 // Synchronous 32-bit EL0
  311. kernel_ventry 0, irq_compat, 32 // IRQ 32-bit EL0
  312. kernel_ventry 0, fiq_invalid_compat, 32 // FIQ 32-bit EL0
  313. kernel_ventry 0, error_invalid_compat, 32 // Error 32-bit EL0
  314. #else
  315. kernel_ventry 0, sync_invalid, 32 // Synchronous 32-bit EL0
  316. kernel_ventry 0, irq_invalid, 32 // IRQ 32-bit EL0
  317. kernel_ventry 0, fiq_invalid, 32 // FIQ 32-bit EL0
  318. kernel_ventry 0, error_invalid, 32 // Error 32-bit EL0
  319. #endif
  320. END(vectors)
  321. /*
  322. * Invalid mode handlers
  323. */
  324. .macro inv_entry, el, reason, regsize = 64
  325. kernel_entry \el, \regsize
  326. mov x0, sp
  327. mov x1, #\reason
  328. mrs x2, esr_el1
  329. b bad_mode
  330. .endm
  331. el0_sync_invalid:
  332. inv_entry 0, BAD_SYNC
  333. ENDPROC(el0_sync_invalid)
  334. el0_irq_invalid:
  335. inv_entry 0, BAD_IRQ
  336. ENDPROC(el0_irq_invalid)
  337. el0_fiq_invalid:
  338. inv_entry 0, BAD_FIQ
  339. ENDPROC(el0_fiq_invalid)
  340. el0_error_invalid:
  341. inv_entry 0, BAD_ERROR
  342. ENDPROC(el0_error_invalid)
  343. #ifdef CONFIG_COMPAT
  344. el0_fiq_invalid_compat:
  345. inv_entry 0, BAD_FIQ, 32
  346. ENDPROC(el0_fiq_invalid_compat)
  347. el0_error_invalid_compat:
  348. inv_entry 0, BAD_ERROR, 32
  349. ENDPROC(el0_error_invalid_compat)
  350. #endif
  351. el1_sync_invalid:
  352. inv_entry 1, BAD_SYNC
  353. ENDPROC(el1_sync_invalid)
  354. el1_irq_invalid:
  355. inv_entry 1, BAD_IRQ
  356. ENDPROC(el1_irq_invalid)
  357. el1_fiq_invalid:
  358. inv_entry 1, BAD_FIQ
  359. ENDPROC(el1_fiq_invalid)
  360. el1_error_invalid:
  361. inv_entry 1, BAD_ERROR
  362. ENDPROC(el1_error_invalid)
  363. /*
  364. * EL1 mode handlers.
  365. */
  366. .align 6
  367. el1_sync:
  368. kernel_entry 1
  369. mrs x1, esr_el1 // read the syndrome register
  370. lsr x24, x1, #ESR_ELx_EC_SHIFT // exception class
  371. cmp x24, #ESR_ELx_EC_DABT_CUR // data abort in EL1
  372. b.eq el1_da
  373. cmp x24, #ESR_ELx_EC_IABT_CUR // instruction abort in EL1
  374. b.eq el1_ia
  375. cmp x24, #ESR_ELx_EC_SYS64 // configurable trap
  376. b.eq el1_undef
  377. cmp x24, #ESR_ELx_EC_SP_ALIGN // stack alignment exception
  378. b.eq el1_sp_pc
  379. cmp x24, #ESR_ELx_EC_PC_ALIGN // pc alignment exception
  380. b.eq el1_sp_pc
  381. cmp x24, #ESR_ELx_EC_UNKNOWN // unknown exception in EL1
  382. b.eq el1_undef
  383. cmp x24, #ESR_ELx_EC_BREAKPT_CUR // debug exception in EL1
  384. b.ge el1_dbg
  385. b el1_inv
  386. el1_ia:
  387. /*
  388. * Fall through to the Data abort case
  389. */
  390. el1_da:
  391. /*
  392. * Data abort handling
  393. */
  394. mrs x3, far_el1
  395. enable_dbg
  396. // re-enable interrupts if they were enabled in the aborted context
  397. tbnz x23, #7, 1f // PSR_I_BIT
  398. enable_irq
  399. 1:
  400. clear_address_tag x0, x3
  401. mov x2, sp // struct pt_regs
  402. bl do_mem_abort
  403. // disable interrupts before pulling preserved data off the stack
  404. disable_irq
  405. kernel_exit 1
  406. el1_sp_pc:
  407. /*
  408. * Stack or PC alignment exception handling
  409. */
  410. mrs x0, far_el1
  411. enable_dbg
  412. mov x2, sp
  413. b do_sp_pc_abort
  414. el1_undef:
  415. /*
  416. * Undefined instruction
  417. */
  418. enable_dbg
  419. mov x0, sp
  420. b do_undefinstr
  421. el1_dbg:
  422. /*
  423. * Debug exception handling
  424. */
  425. cmp x24, #ESR_ELx_EC_BRK64 // if BRK64
  426. cinc x24, x24, eq // set bit '0'
  427. tbz x24, #0, el1_inv // EL1 only
  428. mrs x0, far_el1
  429. mov x2, sp // struct pt_regs
  430. bl do_debug_exception
  431. kernel_exit 1
  432. el1_inv:
  433. // TODO: add support for undefined instructions in kernel mode
  434. enable_dbg
  435. mov x0, sp
  436. mov x2, x1
  437. mov x1, #BAD_SYNC
  438. b bad_mode
  439. ENDPROC(el1_sync)
  440. .align 6
  441. el1_irq:
  442. kernel_entry 1
  443. enable_dbg
  444. #ifdef CONFIG_TRACE_IRQFLAGS
  445. bl trace_hardirqs_off
  446. #endif
  447. irq_handler
  448. #ifdef CONFIG_PREEMPT
  449. ldr w24, [tsk, #TI_PREEMPT] // get preempt count
  450. cbnz w24, 1f // preempt count != 0
  451. ldr x0, [tsk, #TI_FLAGS] // get flags
  452. tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  453. bl el1_preempt
  454. 1:
  455. #endif
  456. #ifdef CONFIG_TRACE_IRQFLAGS
  457. bl trace_hardirqs_on
  458. #endif
  459. kernel_exit 1
  460. ENDPROC(el1_irq)
  461. #ifdef CONFIG_PREEMPT
  462. el1_preempt:
  463. mov x24, lr
  464. 1: bl preempt_schedule_irq // irq en/disable is done inside
  465. ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
  466. tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
  467. ret x24
  468. #endif
  469. /*
  470. * EL0 mode handlers.
  471. */
  472. .align 6
  473. el0_sync:
  474. kernel_entry 0
  475. mrs x25, esr_el1 // read the syndrome register
  476. lsr x24, x25, #ESR_ELx_EC_SHIFT // exception class
  477. cmp x24, #ESR_ELx_EC_SVC64 // SVC in 64-bit state
  478. b.eq el0_svc
  479. cmp x24, #ESR_ELx_EC_DABT_LOW // data abort in EL0
  480. b.eq el0_da
  481. cmp x24, #ESR_ELx_EC_IABT_LOW // instruction abort in EL0
  482. b.eq el0_ia
  483. cmp x24, #ESR_ELx_EC_FP_ASIMD // FP/ASIMD access
  484. b.eq el0_fpsimd_acc
  485. cmp x24, #ESR_ELx_EC_FP_EXC64 // FP/ASIMD exception
  486. b.eq el0_fpsimd_exc
  487. cmp x24, #ESR_ELx_EC_SYS64 // configurable trap
  488. b.eq el0_sys
  489. cmp x24, #ESR_ELx_EC_SP_ALIGN // stack alignment exception
  490. b.eq el0_sp_pc
  491. cmp x24, #ESR_ELx_EC_PC_ALIGN // pc alignment exception
  492. b.eq el0_sp_pc
  493. cmp x24, #ESR_ELx_EC_UNKNOWN // unknown exception in EL0
  494. b.eq el0_undef
  495. cmp x24, #ESR_ELx_EC_BREAKPT_LOW // debug exception in EL0
  496. b.ge el0_dbg
  497. b el0_inv
  498. #ifdef CONFIG_COMPAT
  499. .align 6
  500. el0_sync_compat:
  501. kernel_entry 0, 32
  502. mrs x25, esr_el1 // read the syndrome register
  503. lsr x24, x25, #ESR_ELx_EC_SHIFT // exception class
  504. cmp x24, #ESR_ELx_EC_SVC32 // SVC in 32-bit state
  505. b.eq el0_svc_compat
  506. cmp x24, #ESR_ELx_EC_DABT_LOW // data abort in EL0
  507. b.eq el0_da
  508. cmp x24, #ESR_ELx_EC_IABT_LOW // instruction abort in EL0
  509. b.eq el0_ia
  510. cmp x24, #ESR_ELx_EC_FP_ASIMD // FP/ASIMD access
  511. b.eq el0_fpsimd_acc
  512. cmp x24, #ESR_ELx_EC_FP_EXC32 // FP/ASIMD exception
  513. b.eq el0_fpsimd_exc
  514. cmp x24, #ESR_ELx_EC_PC_ALIGN // pc alignment exception
  515. b.eq el0_sp_pc
  516. cmp x24, #ESR_ELx_EC_UNKNOWN // unknown exception in EL0
  517. b.eq el0_undef
  518. cmp x24, #ESR_ELx_EC_CP15_32 // CP15 MRC/MCR trap
  519. b.eq el0_undef
  520. cmp x24, #ESR_ELx_EC_CP15_64 // CP15 MRRC/MCRR trap
  521. b.eq el0_undef
  522. cmp x24, #ESR_ELx_EC_CP14_MR // CP14 MRC/MCR trap
  523. b.eq el0_undef
  524. cmp x24, #ESR_ELx_EC_CP14_LS // CP14 LDC/STC trap
  525. b.eq el0_undef
  526. cmp x24, #ESR_ELx_EC_CP14_64 // CP14 MRRC/MCRR trap
  527. b.eq el0_undef
  528. cmp x24, #ESR_ELx_EC_BREAKPT_LOW // debug exception in EL0
  529. b.ge el0_dbg
  530. b el0_inv
  531. el0_svc_compat:
  532. /*
  533. * AArch32 syscall handling
  534. */
  535. adrp stbl, compat_sys_call_table // load compat syscall table pointer
  536. uxtw scno, w7 // syscall number in w7 (r7)
  537. mov sc_nr, #__NR_compat_syscalls
  538. b el0_svc_naked
  539. .align 6
  540. el0_irq_compat:
  541. kernel_entry 0, 32
  542. b el0_irq_naked
  543. #endif
  544. el0_da:
  545. /*
  546. * Data abort handling
  547. */
  548. mrs x26, far_el1
  549. // enable interrupts before calling the main handler
  550. enable_dbg_and_irq
  551. ct_user_exit
  552. clear_address_tag x0, x26
  553. mov x1, x25
  554. mov x2, sp
  555. bl do_mem_abort
  556. b ret_to_user
  557. el0_ia:
  558. /*
  559. * Instruction abort handling
  560. */
  561. mrs x26, far_el1
  562. msr daifclr, #(8 | 4 | 1)
  563. #ifdef CONFIG_TRACE_IRQFLAGS
  564. bl trace_hardirqs_off
  565. #endif
  566. ct_user_exit
  567. mov x0, x26
  568. mov x1, x25
  569. mov x2, sp
  570. bl do_el0_ia_bp_hardening
  571. b ret_to_user
  572. el0_fpsimd_acc:
  573. /*
  574. * Floating Point or Advanced SIMD access
  575. */
  576. enable_dbg
  577. ct_user_exit
  578. mov x0, x25
  579. mov x1, sp
  580. bl do_fpsimd_acc
  581. b ret_to_user
  582. el0_fpsimd_exc:
  583. /*
  584. * Floating Point or Advanced SIMD exception
  585. */
  586. enable_dbg
  587. ct_user_exit
  588. mov x0, x25
  589. mov x1, sp
  590. bl do_fpsimd_exc
  591. b ret_to_user
  592. el0_sp_pc:
  593. /*
  594. * Stack or PC alignment exception handling
  595. */
  596. mrs x26, far_el1
  597. enable_dbg
  598. #ifdef CONFIG_TRACE_IRQFLAGS
  599. bl trace_hardirqs_off
  600. #endif
  601. ct_user_exit
  602. mov x0, x26
  603. mov x1, x25
  604. mov x2, sp
  605. bl do_sp_pc_abort
  606. b ret_to_user
  607. el0_undef:
  608. /*
  609. * Undefined instruction
  610. */
  611. // enable interrupts before calling the main handler
  612. enable_dbg_and_irq
  613. ct_user_exit
  614. mov x0, sp
  615. bl do_undefinstr
  616. b ret_to_user
  617. el0_sys:
  618. /*
  619. * System instructions, for trapped cache maintenance instructions
  620. */
  621. enable_dbg_and_irq
  622. ct_user_exit
  623. mov x0, x25
  624. mov x1, sp
  625. bl do_sysinstr
  626. b ret_to_user
  627. el0_dbg:
  628. /*
  629. * Debug exception handling
  630. */
  631. tbnz x24, #0, el0_inv // EL0 only
  632. mrs x0, far_el1
  633. mov x1, x25
  634. mov x2, sp
  635. bl do_debug_exception
  636. enable_dbg
  637. ct_user_exit
  638. b ret_to_user
  639. el0_inv:
  640. enable_dbg
  641. ct_user_exit
  642. mov x0, sp
  643. mov x1, #BAD_SYNC
  644. mov x2, x25
  645. bl bad_el0_sync
  646. b ret_to_user
  647. ENDPROC(el0_sync)
  648. .align 6
  649. el0_irq:
  650. kernel_entry 0
  651. el0_irq_naked:
  652. enable_dbg
  653. #ifdef CONFIG_TRACE_IRQFLAGS
  654. bl trace_hardirqs_off
  655. #endif
  656. ct_user_exit
  657. #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
  658. tbz x22, #55, 1f
  659. bl do_el0_irq_bp_hardening
  660. 1:
  661. #endif
  662. irq_handler
  663. #ifdef CONFIG_TRACE_IRQFLAGS
  664. bl trace_hardirqs_on
  665. #endif
  666. b ret_to_user
  667. ENDPROC(el0_irq)
  668. /*
  669. * Register switch for AArch64. The callee-saved registers need to be saved
  670. * and restored. On entry:
  671. * x0 = previous task_struct (must be preserved across the switch)
  672. * x1 = next task_struct
  673. * Previous and next are guaranteed not to be the same.
  674. *
  675. */
  676. ENTRY(cpu_switch_to)
  677. mov x10, #THREAD_CPU_CONTEXT
  678. add x8, x0, x10
  679. mov x9, sp
  680. stp x19, x20, [x8], #16 // store callee-saved registers
  681. stp x21, x22, [x8], #16
  682. stp x23, x24, [x8], #16
  683. stp x25, x26, [x8], #16
  684. stp x27, x28, [x8], #16
  685. stp x29, x9, [x8], #16
  686. str lr, [x8]
  687. add x8, x1, x10
  688. ldp x19, x20, [x8], #16 // restore callee-saved registers
  689. ldp x21, x22, [x8], #16
  690. ldp x23, x24, [x8], #16
  691. ldp x25, x26, [x8], #16
  692. ldp x27, x28, [x8], #16
  693. ldp x29, x9, [x8], #16
  694. ldr lr, [x8]
  695. mov sp, x9
  696. and x9, x9, #~(THREAD_SIZE - 1)
  697. msr sp_el0, x9
  698. ret
  699. ENDPROC(cpu_switch_to)
  700. /*
  701. * This is the fast syscall return path. We do as little as possible here,
  702. * and this includes saving x0 back into the kernel stack.
  703. */
  704. ret_fast_syscall:
  705. disable_irq // disable interrupts
  706. str x0, [sp, #S_X0] // returned x0
  707. ldr x1, [tsk, #TI_FLAGS] // re-check for syscall tracing
  708. and x2, x1, #_TIF_SYSCALL_WORK
  709. cbnz x2, ret_fast_syscall_trace
  710. and x2, x1, #_TIF_WORK_MASK
  711. cbnz x2, work_pending
  712. enable_step_tsk x1, x2
  713. kernel_exit 0
  714. ret_fast_syscall_trace:
  715. enable_irq // enable interrupts
  716. b __sys_trace_return_skipped // we already saved x0
  717. /*
  718. * Ok, we need to do extra processing, enter the slow path.
  719. */
  720. work_pending:
  721. mov x0, sp // 'regs'
  722. bl do_notify_resume
  723. #ifdef CONFIG_TRACE_IRQFLAGS
  724. bl trace_hardirqs_on // enabled while in userspace
  725. #endif
  726. ldr x1, [tsk, #TI_FLAGS] // re-check for single-step
  727. b finish_ret_to_user
  728. /*
  729. * "slow" syscall return path.
  730. */
  731. ret_to_user:
  732. disable_irq // disable interrupts
  733. ldr x1, [tsk, #TI_FLAGS]
  734. and x2, x1, #_TIF_WORK_MASK
  735. cbnz x2, work_pending
  736. finish_ret_to_user:
  737. enable_step_tsk x1, x2
  738. kernel_exit 0
  739. ENDPROC(ret_to_user)
  740. /*
  741. * This is how we return from a fork.
  742. */
  743. ENTRY(ret_from_fork)
  744. bl schedule_tail
  745. cbz x19, 1f // not a kernel thread
  746. mov x0, x20
  747. blr x19
  748. 1: get_thread_info tsk
  749. b ret_to_user
  750. ENDPROC(ret_from_fork)
  751. /*
  752. * SVC handler.
  753. */
  754. .align 6
  755. el0_svc:
  756. adrp stbl, sys_call_table // load syscall table pointer
  757. uxtw scno, w8 // syscall number in w8
  758. mov sc_nr, #__NR_syscalls
  759. el0_svc_naked: // compat entry point
  760. stp x0, scno, [sp, #S_ORIG_X0] // save the original x0 and syscall number
  761. enable_dbg_and_irq
  762. ct_user_exit 1
  763. ldr x16, [tsk, #TI_FLAGS] // check for syscall hooks
  764. tst x16, #_TIF_SYSCALL_WORK
  765. b.ne __sys_trace
  766. cmp scno, sc_nr // check upper syscall limit
  767. b.hs ni_sys
  768. mask_nospec64 scno, sc_nr, x19 // enforce bounds for syscall number
  769. ldr x16, [stbl, scno, lsl #3] // address in the syscall table
  770. blr x16 // call sys_* routine
  771. b ret_fast_syscall
  772. ni_sys:
  773. mov x0, sp
  774. bl do_ni_syscall
  775. b ret_fast_syscall
  776. ENDPROC(el0_svc)
  777. /*
  778. * This is the really slow path. We're going to be doing context
  779. * switches, and waiting for our parent to respond.
  780. */
  781. __sys_trace:
  782. mov w0, #-1 // set default errno for
  783. cmp scno, x0 // user-issued syscall(-1)
  784. b.ne 1f
  785. mov x0, #-ENOSYS
  786. str x0, [sp, #S_X0]
  787. 1: mov x0, sp
  788. bl syscall_trace_enter
  789. cmp w0, #-1 // skip the syscall?
  790. b.eq __sys_trace_return_skipped
  791. uxtw scno, w0 // syscall number (possibly new)
  792. mov x1, sp // pointer to regs
  793. cmp scno, sc_nr // check upper syscall limit
  794. b.hs __ni_sys_trace
  795. ldp x0, x1, [sp] // restore the syscall args
  796. ldp x2, x3, [sp, #S_X2]
  797. ldp x4, x5, [sp, #S_X4]
  798. ldp x6, x7, [sp, #S_X6]
  799. ldr x16, [stbl, scno, lsl #3] // address in the syscall table
  800. blr x16 // call sys_* routine
  801. __sys_trace_return:
  802. str x0, [sp, #S_X0] // save returned x0
  803. __sys_trace_return_skipped:
  804. mov x0, sp
  805. bl syscall_trace_exit
  806. b ret_to_user
  807. __ni_sys_trace:
  808. mov x0, sp
  809. bl do_ni_syscall
  810. b __sys_trace_return
  811. .popsection // .entry.text
  812. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  813. /*
  814. * Exception vectors trampoline.
  815. */
  816. .pushsection ".entry.tramp.text", "ax"
  817. .macro tramp_map_kernel, tmp
  818. mrs \tmp, ttbr1_el1
  819. sub \tmp, \tmp, #SWAPPER_DIR_SIZE
  820. bic \tmp, \tmp, #USER_ASID_FLAG
  821. msr ttbr1_el1, \tmp
  822. .endm
  823. .macro tramp_unmap_kernel, tmp
  824. mrs \tmp, ttbr1_el1
  825. add \tmp, \tmp, #SWAPPER_DIR_SIZE
  826. orr \tmp, \tmp, #USER_ASID_FLAG
  827. msr ttbr1_el1, \tmp
  828. /*
  829. * We avoid running the post_ttbr_update_workaround here because
  830. * it's only needed by Cavium ThunderX, which requires KPTI to be
  831. * disabled.
  832. */
  833. .endm
  834. .macro tramp_ventry, regsize = 64
  835. .align 7
  836. 1:
  837. .if \regsize == 64
  838. msr tpidrro_el0, x30 // Restored in kernel_ventry
  839. .endif
  840. /*
  841. * Defend against branch aliasing attacks by pushing a dummy
  842. * entry onto the return stack and using a RET instruction to
  843. * enter the full-fat kernel vectors.
  844. */
  845. bl 2f
  846. b .
  847. 2:
  848. tramp_map_kernel x30
  849. #ifdef CONFIG_RANDOMIZE_BASE
  850. adr x30, tramp_vectors + PAGE_SIZE
  851. isb
  852. ldr x30, [x30]
  853. #else
  854. ldr x30, =vectors
  855. #endif
  856. prfm plil1strm, [x30, #(1b - tramp_vectors)]
  857. msr vbar_el1, x30
  858. add x30, x30, #(1b - tramp_vectors)
  859. isb
  860. ret
  861. .endm
  862. .macro tramp_exit, regsize = 64
  863. adr x30, tramp_vectors
  864. msr vbar_el1, x30
  865. tramp_unmap_kernel x30
  866. .if \regsize == 64
  867. mrs x30, far_el1
  868. .endif
  869. eret
  870. .endm
  871. .align 11
  872. ENTRY(tramp_vectors)
  873. .space 0x400
  874. tramp_ventry
  875. tramp_ventry
  876. tramp_ventry
  877. tramp_ventry
  878. tramp_ventry 32
  879. tramp_ventry 32
  880. tramp_ventry 32
  881. tramp_ventry 32
  882. END(tramp_vectors)
  883. ENTRY(tramp_exit_native)
  884. tramp_exit
  885. END(tramp_exit_native)
  886. ENTRY(tramp_exit_compat)
  887. tramp_exit 32
  888. END(tramp_exit_compat)
  889. .ltorg
  890. .popsection // .entry.tramp.text
  891. #ifdef CONFIG_RANDOMIZE_BASE
  892. .pushsection ".rodata", "a"
  893. .align PAGE_SHIFT
  894. .globl __entry_tramp_data_start
  895. __entry_tramp_data_start:
  896. .quad vectors
  897. .popsection // .rodata
  898. #endif /* CONFIG_RANDOMIZE_BASE */
  899. #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
  900. /*
  901. * Special system call wrappers.
  902. */
  903. ENTRY(sys_rt_sigreturn_wrapper)
  904. mov x0, sp
  905. b sys_rt_sigreturn
  906. ENDPROC(sys_rt_sigreturn_wrapper)