entry.S 20 KB

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