entry.S 17 KB

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