entry.S 16 KB

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