entry.S 18 KB

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