entry-common.S 11 KB

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