entry-common.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * linux/arch/arm/kernel/entry-common.S
  4. *
  5. * Copyright (C) 2000 Russell King
  6. */
  7. #include <asm/assembler.h>
  8. #include <asm/unistd.h>
  9. #include <asm/ftrace.h>
  10. #include <asm/unwind.h>
  11. #include <asm/page.h>
  12. #ifdef CONFIG_AEABI
  13. #include <asm/unistd-oabi.h>
  14. #endif
  15. .equ NR_syscalls, __NR_syscalls
  16. #include "entry-header.S"
  17. saved_psr .req r8
  18. #if defined(CONFIG_TRACE_IRQFLAGS) || defined(CONFIG_CONTEXT_TRACKING_USER)
  19. saved_pc .req r9
  20. #define TRACE(x...) x
  21. #else
  22. saved_pc .req lr
  23. #define TRACE(x...)
  24. #endif
  25. .section .entry.text,"ax",%progbits
  26. .align 5
  27. #if !(IS_ENABLED(CONFIG_TRACE_IRQFLAGS) || IS_ENABLED(CONFIG_CONTEXT_TRACKING_USER) || \
  28. IS_ENABLED(CONFIG_DEBUG_RSEQ))
  29. /*
  30. * This is the fast syscall return path. We do as little as possible here,
  31. * such as avoiding writing r0 to the stack. We only use this path if we
  32. * have tracing, context tracking and rseq debug disabled - the overheads
  33. * from those features make this path too inefficient.
  34. */
  35. ret_fast_syscall:
  36. __ret_fast_syscall:
  37. UNWIND(.fnstart )
  38. UNWIND(.cantunwind )
  39. disable_irq_notrace @ disable interrupts
  40. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  41. movs r1, r1, lsl #16
  42. bne fast_work_pending
  43. restore_user_regs fast = 1, offset = S_OFF
  44. UNWIND(.fnend )
  45. ENDPROC(ret_fast_syscall)
  46. /* Ok, we need to do extra processing, enter the slow path. */
  47. fast_work_pending:
  48. str r0, [sp, #S_R0+S_OFF]! @ returned r0
  49. /* fall through to work_pending */
  50. #else
  51. /*
  52. * The "replacement" ret_fast_syscall for when tracing, context tracking,
  53. * or rseq debug is enabled. As we will need to call out to some C functions,
  54. * we save r0 first to avoid needing to save registers around each C function
  55. * call.
  56. */
  57. ret_fast_syscall:
  58. __ret_fast_syscall:
  59. UNWIND(.fnstart )
  60. UNWIND(.cantunwind )
  61. str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
  62. #if IS_ENABLED(CONFIG_DEBUG_RSEQ)
  63. /* do_rseq_syscall needs interrupts enabled. */
  64. mov r0, sp @ 'regs'
  65. bl do_rseq_syscall
  66. #endif
  67. disable_irq_notrace @ disable interrupts
  68. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  69. movs r1, r1, lsl #16
  70. beq no_work_pending
  71. UNWIND(.fnend )
  72. ENDPROC(ret_fast_syscall)
  73. /* Slower path - fall through to work_pending */
  74. #endif
  75. tst r1, #_TIF_SYSCALL_WORK
  76. bne __sys_trace_return_nosave
  77. slow_work_pending:
  78. mov r0, sp @ 'regs'
  79. mov r2, why @ 'syscall'
  80. bl do_work_pending
  81. cmp r0, #0
  82. beq no_work_pending
  83. movlt scno, #(__NR_restart_syscall - __NR_SYSCALL_BASE)
  84. str scno, [tsk, #TI_ABI_SYSCALL] @ make sure tracers see update
  85. ldmia sp, {r0 - r6} @ have to reload r0 - r6
  86. b local_restart @ ... and off we go
  87. ENDPROC(ret_fast_syscall)
  88. /*
  89. * "slow" syscall return path. "why" tells us if this was a real syscall.
  90. * IRQs may be enabled here, so always disable them. Note that we use the
  91. * "notrace" version to avoid calling into the tracing code unnecessarily.
  92. * do_work_pending() will update this state if necessary.
  93. */
  94. ENTRY(ret_to_user)
  95. ret_slow_syscall:
  96. #if IS_ENABLED(CONFIG_DEBUG_RSEQ)
  97. /* do_rseq_syscall needs interrupts enabled. */
  98. enable_irq_notrace @ enable interrupts
  99. mov r0, sp @ 'regs'
  100. bl do_rseq_syscall
  101. #endif
  102. disable_irq_notrace @ disable interrupts
  103. ENTRY(ret_to_user_from_irq)
  104. ldr r1, [tsk, #TI_FLAGS]
  105. movs r1, r1, lsl #16
  106. bne slow_work_pending
  107. no_work_pending:
  108. asm_trace_hardirqs_on save = 0
  109. ct_user_enter save = 0
  110. #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
  111. bl stackleak_erase_on_task_stack
  112. #endif
  113. restore_user_regs fast = 0, offset = 0
  114. ENDPROC(ret_to_user_from_irq)
  115. ENDPROC(ret_to_user)
  116. /*
  117. * This is how we return from a fork.
  118. */
  119. ENTRY(ret_from_fork)
  120. bl schedule_tail
  121. cmp r5, #0
  122. movne r0, r4
  123. badrne lr, 1f
  124. retne r5
  125. 1: get_thread_info tsk
  126. b ret_slow_syscall
  127. ENDPROC(ret_from_fork)
  128. /*=============================================================================
  129. * SWI handler
  130. *-----------------------------------------------------------------------------
  131. */
  132. .align 5
  133. #ifdef CONFIG_HARDEN_BRANCH_HISTORY
  134. ENTRY(vector_bhb_loop8_swi)
  135. sub sp, sp, #PT_REGS_SIZE
  136. stmia sp, {r0 - r12}
  137. mov r8, #8
  138. 1: b 2f
  139. 2: subs r8, r8, #1
  140. bne 1b
  141. dsb nsh
  142. isb
  143. b 3f
  144. ENDPROC(vector_bhb_loop8_swi)
  145. .align 5
  146. ENTRY(vector_bhb_bpiall_swi)
  147. sub sp, sp, #PT_REGS_SIZE
  148. stmia sp, {r0 - r12}
  149. mcr p15, 0, r8, c7, c5, 6 @ BPIALL
  150. isb
  151. b 3f
  152. ENDPROC(vector_bhb_bpiall_swi)
  153. #endif
  154. .align 5
  155. ENTRY(vector_swi)
  156. #ifdef CONFIG_CPU_V7M
  157. v7m_exception_entry
  158. #else
  159. sub sp, sp, #PT_REGS_SIZE
  160. stmia sp, {r0 - r12} @ Calling r0 - r12
  161. 3:
  162. ARM( add r8, sp, #S_PC )
  163. ARM( stmdb r8, {sp, lr}^ ) @ Calling sp, lr
  164. THUMB( mov r8, sp )
  165. THUMB( store_user_sp_lr r8, r10, S_SP ) @ calling sp, lr
  166. mrs saved_psr, spsr @ called from non-FIQ mode, so ok.
  167. TRACE( mov saved_pc, lr )
  168. str saved_pc, [sp, #S_PC] @ Save calling PC
  169. str saved_psr, [sp, #S_PSR] @ Save CPSR
  170. str r0, [sp, #S_OLD_R0] @ Save OLD_R0
  171. #endif
  172. reload_current r10, ip
  173. zero_fp
  174. alignment_trap r10, ip, cr_alignment
  175. asm_trace_hardirqs_on save=0
  176. enable_irq_notrace
  177. ct_user_exit save=0
  178. /*
  179. * Get the system call number.
  180. */
  181. #if defined(CONFIG_OABI_COMPAT)
  182. /*
  183. * If we have CONFIG_OABI_COMPAT then we need to look at the swi
  184. * value to determine if it is an EABI or an old ABI call.
  185. */
  186. #ifdef CONFIG_ARM_THUMB
  187. tst saved_psr, #PSR_T_BIT
  188. movne r10, #0 @ no thumb OABI emulation
  189. USER( ldreq r10, [saved_pc, #-4] ) @ get SWI instruction
  190. #else
  191. USER( ldr r10, [saved_pc, #-4] ) @ get SWI instruction
  192. #endif
  193. ARM_BE8(rev r10, r10) @ little endian instruction
  194. #elif defined(CONFIG_AEABI)
  195. /*
  196. * Pure EABI user space always put syscall number into scno (r7).
  197. */
  198. #elif defined(CONFIG_ARM_THUMB)
  199. /* Legacy ABI only, possibly thumb mode. */
  200. tst saved_psr, #PSR_T_BIT @ this is SPSR from save_user_regs
  201. addne scno, r7, #__NR_SYSCALL_BASE @ put OS number in
  202. USER( ldreq scno, [saved_pc, #-4] )
  203. #else
  204. /* Legacy ABI only. */
  205. USER( ldr scno, [saved_pc, #-4] ) @ get SWI instruction
  206. #endif
  207. /* saved_psr and saved_pc are now dead */
  208. uaccess_disable tbl
  209. get_thread_info tsk
  210. adr tbl, sys_call_table @ load syscall table pointer
  211. #if defined(CONFIG_OABI_COMPAT)
  212. /*
  213. * If the swi argument is zero, this is an EABI call and we do nothing.
  214. *
  215. * If this is an old ABI call, get the syscall number into scno and
  216. * get the old ABI syscall table address.
  217. */
  218. bics r10, r10, #0xff000000
  219. strne r10, [tsk, #TI_ABI_SYSCALL]
  220. streq scno, [tsk, #TI_ABI_SYSCALL]
  221. eorne scno, r10, #__NR_OABI_SYSCALL_BASE
  222. ldrne tbl, =sys_oabi_call_table
  223. #elif !defined(CONFIG_AEABI)
  224. bic scno, scno, #0xff000000 @ mask off SWI op-code
  225. str scno, [tsk, #TI_ABI_SYSCALL]
  226. eor scno, scno, #__NR_SYSCALL_BASE @ check OS number
  227. #else
  228. str scno, [tsk, #TI_ABI_SYSCALL]
  229. #endif
  230. /*
  231. * Reload the registers that may have been corrupted on entry to
  232. * the syscall assembly (by tracing or context tracking.)
  233. */
  234. TRACE( ldmia sp, {r0 - r3} )
  235. local_restart:
  236. ldr r10, [tsk, #TI_FLAGS] @ check for syscall tracing
  237. stmdb sp!, {r4, r5} @ push fifth and sixth args
  238. tst r10, #_TIF_SYSCALL_WORK @ are we tracing syscalls?
  239. bne __sys_trace
  240. invoke_syscall tbl, scno, r10, __ret_fast_syscall
  241. add r1, sp, #S_OFF
  242. 2: cmp scno, #(__ARM_NR_BASE - __NR_SYSCALL_BASE)
  243. eor r0, scno, #__NR_SYSCALL_BASE @ put OS number back
  244. bcs arm_syscall
  245. mov why, #0 @ no longer a real syscall
  246. b sys_ni_syscall @ not private func
  247. #if defined(CONFIG_OABI_COMPAT) || !defined(CONFIG_AEABI)
  248. /*
  249. * We failed to handle a fault trying to access the page
  250. * containing the swi instruction, but we're not really in a
  251. * position to return -EFAULT. Instead, return back to the
  252. * instruction and re-enter the user fault handling path trying
  253. * to page it in. This will likely result in sending SEGV to the
  254. * current task.
  255. */
  256. 9001:
  257. sub lr, saved_pc, #4
  258. str lr, [sp, #S_PC]
  259. get_thread_info tsk
  260. b ret_fast_syscall
  261. #endif
  262. ENDPROC(vector_swi)
  263. .ltorg
  264. /*
  265. * This is the really slow path. We're going to be doing
  266. * context switches, and waiting for our parent to respond.
  267. */
  268. __sys_trace:
  269. add r0, sp, #S_OFF
  270. bl syscall_trace_enter
  271. mov scno, r0
  272. invoke_syscall tbl, scno, r10, __sys_trace_return, reload=1
  273. cmp scno, #-1 @ skip the syscall?
  274. bne 2b
  275. add sp, sp, #S_OFF @ restore stack
  276. __sys_trace_return_nosave:
  277. enable_irq_notrace
  278. mov r0, sp
  279. bl syscall_trace_exit
  280. b ret_slow_syscall
  281. __sys_trace_return:
  282. str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
  283. mov r0, sp
  284. bl syscall_trace_exit
  285. b ret_slow_syscall
  286. .macro syscall_table_start, sym
  287. .equ __sys_nr, 0
  288. .type \sym, #object
  289. ENTRY(\sym)
  290. .endm
  291. .macro syscall, nr, func
  292. .ifgt __sys_nr - \nr
  293. .error "Duplicated/unorded system call entry"
  294. .endif
  295. .rept \nr - __sys_nr
  296. .long sys_ni_syscall
  297. .endr
  298. .long \func
  299. .equ __sys_nr, \nr + 1
  300. .endm
  301. .macro syscall_table_end, sym
  302. .ifgt __sys_nr - __NR_syscalls
  303. .error "System call table too big"
  304. .endif
  305. .rept __NR_syscalls - __sys_nr
  306. .long sys_ni_syscall
  307. .endr
  308. .size \sym, . - \sym
  309. .endm
  310. #define __SYSCALL_WITH_COMPAT(nr, native, compat) __SYSCALL(nr, native)
  311. #define __SYSCALL(nr, func) syscall nr, func
  312. /*
  313. * This is the syscall table declaration for native ABI syscalls.
  314. * With EABI a couple syscalls are obsolete and defined as sys_ni_syscall.
  315. */
  316. syscall_table_start sys_call_table
  317. #ifdef CONFIG_AEABI
  318. #include <calls-eabi.S>
  319. #else
  320. #include <calls-oabi.S>
  321. #endif
  322. syscall_table_end sys_call_table
  323. /*============================================================================
  324. * Special system call wrappers
  325. */
  326. @ r0 = syscall number
  327. @ r8 = syscall table
  328. sys_syscall:
  329. bic scno, r0, #__NR_OABI_SYSCALL_BASE
  330. cmp scno, #__NR_syscall - __NR_SYSCALL_BASE
  331. cmpne scno, #NR_syscalls @ check range
  332. #ifdef CONFIG_CPU_SPECTRE
  333. movhs scno, #0
  334. csdb
  335. #endif
  336. stmialo sp, {r5, r6} @ shuffle args
  337. movlo r0, r1
  338. movlo r1, r2
  339. movlo r2, r3
  340. movlo r3, r4
  341. ldrlo pc, [tbl, scno, lsl #2]
  342. b sys_ni_syscall
  343. ENDPROC(sys_syscall)
  344. sys_sigreturn_wrapper:
  345. add r0, sp, #S_OFF
  346. mov why, #0 @ prevent syscall restart handling
  347. b sys_sigreturn
  348. ENDPROC(sys_sigreturn_wrapper)
  349. sys_rt_sigreturn_wrapper:
  350. add r0, sp, #S_OFF
  351. mov why, #0 @ prevent syscall restart handling
  352. b sys_rt_sigreturn
  353. ENDPROC(sys_rt_sigreturn_wrapper)
  354. sys_statfs64_wrapper:
  355. teq r1, #88
  356. moveq r1, #84
  357. b sys_statfs64
  358. ENDPROC(sys_statfs64_wrapper)
  359. sys_fstatfs64_wrapper:
  360. teq r1, #88
  361. moveq r1, #84
  362. b sys_fstatfs64
  363. ENDPROC(sys_fstatfs64_wrapper)
  364. /*
  365. * Note: off_4k (r5) is always units of 4K. If we can't do the requested
  366. * offset, we return EINVAL.
  367. */
  368. sys_mmap2:
  369. str r5, [sp, #4]
  370. b sys_mmap_pgoff
  371. ENDPROC(sys_mmap2)
  372. #ifdef CONFIG_OABI_COMPAT
  373. /*
  374. * These are syscalls with argument register differences
  375. */
  376. sys_oabi_pread64:
  377. stmia sp, {r3, r4}
  378. b sys_pread64
  379. ENDPROC(sys_oabi_pread64)
  380. sys_oabi_pwrite64:
  381. stmia sp, {r3, r4}
  382. b sys_pwrite64
  383. ENDPROC(sys_oabi_pwrite64)
  384. sys_oabi_truncate64:
  385. mov r3, r2
  386. mov r2, r1
  387. b sys_truncate64
  388. ENDPROC(sys_oabi_truncate64)
  389. sys_oabi_ftruncate64:
  390. mov r3, r2
  391. mov r2, r1
  392. b sys_ftruncate64
  393. ENDPROC(sys_oabi_ftruncate64)
  394. sys_oabi_readahead:
  395. str r3, [sp]
  396. mov r3, r2
  397. mov r2, r1
  398. b sys_readahead
  399. ENDPROC(sys_oabi_readahead)
  400. /*
  401. * Let's declare a second syscall table for old ABI binaries
  402. * using the compatibility syscall entries.
  403. */
  404. syscall_table_start sys_oabi_call_table
  405. #undef __SYSCALL_WITH_COMPAT
  406. #define __SYSCALL_WITH_COMPAT(nr, native, compat) __SYSCALL(nr, compat)
  407. #include <calls-oabi.S>
  408. syscall_table_end sys_oabi_call_table
  409. #endif