entry.S 17 KB

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