entry-common.S 11 KB

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