entry-common.S 11 KB

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