entry-header.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/init.h>
  3. #include <linux/linkage.h>
  4. #include <asm/assembler.h>
  5. #include <asm/asm-offsets.h>
  6. #include <asm/errno.h>
  7. #include <asm/thread_info.h>
  8. #include <asm/v7m.h>
  9. @ Bad Abort numbers
  10. @ -----------------
  11. @
  12. #define BAD_PREFETCH 0
  13. #define BAD_DATA 1
  14. #define BAD_ADDREXCPTN 2
  15. #define BAD_IRQ 3
  16. #define BAD_UNDEFINSTR 4
  17. @
  18. @ Most of the stack format comes from struct pt_regs, but with
  19. @ the addition of 8 bytes for storing syscall args 5 and 6.
  20. @ This _must_ remain a multiple of 8 for EABI.
  21. @
  22. #define S_OFF 8
  23. /*
  24. * The SWI code relies on the fact that R0 is at the bottom of the stack
  25. * (due to slow/fast restore user regs).
  26. */
  27. #if S_R0 != 0
  28. #error "Please fix"
  29. #endif
  30. .macro zero_fp
  31. #ifdef CONFIG_FRAME_POINTER
  32. mov fp, #0
  33. #endif
  34. .endm
  35. #ifdef CONFIG_ALIGNMENT_TRAP
  36. #define ATRAP(x...) x
  37. #else
  38. #define ATRAP(x...)
  39. #endif
  40. .macro alignment_trap, rtmp1, rtmp2, label
  41. #ifdef CONFIG_ALIGNMENT_TRAP
  42. mrc p15, 0, \rtmp2, c1, c0, 0
  43. ldr \rtmp1, \label
  44. ldr \rtmp1, [\rtmp1]
  45. teq \rtmp1, \rtmp2
  46. mcrne p15, 0, \rtmp1, c1, c0, 0
  47. #endif
  48. .endm
  49. #ifdef CONFIG_CPU_V7M
  50. /*
  51. * ARMv7-M exception entry/exit macros.
  52. *
  53. * xPSR, ReturnAddress(), LR (R14), R12, R3, R2, R1, and R0 are
  54. * automatically saved on the current stack (32 words) before
  55. * switching to the exception stack (SP_main).
  56. *
  57. * If exception is taken while in user mode, SP_main is
  58. * empty. Otherwise, SP_main is aligned to 64 bit automatically
  59. * (CCR.STKALIGN set).
  60. *
  61. * Linux assumes that the interrupts are disabled when entering an
  62. * exception handler and it may BUG if this is not the case. Interrupts
  63. * are disabled during entry and reenabled in the exit macro.
  64. *
  65. * v7m_exception_slow_exit is used when returning from SVC or PendSV.
  66. * When returning to kernel mode, we don't return from exception.
  67. */
  68. .macro v7m_exception_entry
  69. @ determine the location of the registers saved by the core during
  70. @ exception entry. Depending on the mode the cpu was in when the
  71. @ exception happend that is either on the main or the process stack.
  72. @ Bit 2 of EXC_RETURN stored in the lr register specifies which stack
  73. @ was used.
  74. tst lr, #EXC_RET_STACK_MASK
  75. mrsne r12, psp
  76. moveq r12, sp
  77. @ we cannot rely on r0-r3 and r12 matching the value saved in the
  78. @ exception frame because of tail-chaining. So these have to be
  79. @ reloaded.
  80. ldmia r12!, {r0-r3}
  81. @ Linux expects to have irqs off. Do it here before taking stack space
  82. cpsid i
  83. sub sp, #PT_REGS_SIZE-S_IP
  84. stmdb sp!, {r0-r11}
  85. @ load saved r12, lr, return address and xPSR.
  86. @ r0-r7 are used for signals and never touched from now on. Clobbering
  87. @ r8-r12 is OK.
  88. mov r9, r12
  89. ldmia r9!, {r8, r10-r12}
  90. @ calculate the original stack pointer value.
  91. @ r9 currently points to the memory location just above the auto saved
  92. @ xPSR.
  93. @ The cpu might automatically 8-byte align the stack. Bit 9
  94. @ of the saved xPSR specifies if stack aligning took place. In this case
  95. @ another 32-bit value is included in the stack.
  96. tst r12, V7M_xPSR_FRAMEPTRALIGN
  97. addne r9, r9, #4
  98. @ store saved r12 using str to have a register to hold the base for stm
  99. str r8, [sp, #S_IP]
  100. add r8, sp, #S_SP
  101. @ store r13-r15, xPSR
  102. stmia r8!, {r9-r12}
  103. @ store old_r0
  104. str r0, [r8]
  105. .endm
  106. /*
  107. * PENDSV and SVCALL are configured to have the same exception
  108. * priorities. As a kernel thread runs at SVCALL execution priority it
  109. * can never be preempted and so we will never have to return to a
  110. * kernel thread here.
  111. */
  112. .macro v7m_exception_slow_exit ret_r0
  113. cpsid i
  114. ldr lr, =EXC_RET_THREADMODE_PROCESSSTACK
  115. @ read original r12, sp, lr, pc and xPSR
  116. add r12, sp, #S_IP
  117. ldmia r12, {r1-r5}
  118. @ an exception frame is always 8-byte aligned. To tell the hardware if
  119. @ the sp to be restored is aligned or not set bit 9 of the saved xPSR
  120. @ accordingly.
  121. tst r2, #4
  122. subne r2, r2, #4
  123. orrne r5, V7M_xPSR_FRAMEPTRALIGN
  124. biceq r5, V7M_xPSR_FRAMEPTRALIGN
  125. @ ensure bit 0 is cleared in the PC, otherwise behaviour is
  126. @ unpredictable
  127. bic r4, #1
  128. @ write basic exception frame
  129. stmdb r2!, {r1, r3-r5}
  130. ldmia sp, {r1, r3-r5}
  131. .if \ret_r0
  132. stmdb r2!, {r0, r3-r5}
  133. .else
  134. stmdb r2!, {r1, r3-r5}
  135. .endif
  136. @ restore process sp
  137. msr psp, r2
  138. @ restore original r4-r11
  139. ldmia sp!, {r0-r11}
  140. @ restore main sp
  141. add sp, sp, #PT_REGS_SIZE-S_IP
  142. cpsie i
  143. bx lr
  144. .endm
  145. #endif /* CONFIG_CPU_V7M */
  146. @
  147. @ Store/load the USER SP and LR registers by switching to the SYS
  148. @ mode. Useful in Thumb-2 mode where "stm/ldm rd, {sp, lr}^" is not
  149. @ available. Should only be called from SVC mode
  150. @
  151. .macro store_user_sp_lr, rd, rtemp, offset = 0
  152. mrs \rtemp, cpsr
  153. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  154. msr cpsr_c, \rtemp @ switch to the SYS mode
  155. str sp, [\rd, #\offset] @ save sp_usr
  156. str lr, [\rd, #\offset + 4] @ save lr_usr
  157. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  158. msr cpsr_c, \rtemp @ switch back to the SVC mode
  159. .endm
  160. .macro load_user_sp_lr, rd, rtemp, offset = 0
  161. mrs \rtemp, cpsr
  162. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  163. msr cpsr_c, \rtemp @ switch to the SYS mode
  164. ldr sp, [\rd, #\offset] @ load sp_usr
  165. ldr lr, [\rd, #\offset + 4] @ load lr_usr
  166. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  167. msr cpsr_c, \rtemp @ switch back to the SVC mode
  168. .endm
  169. .macro svc_exit, rpsr, irq = 0
  170. .if \irq != 0
  171. @ IRQs already off
  172. #ifdef CONFIG_TRACE_IRQFLAGS
  173. @ The parent context IRQs must have been enabled to get here in
  174. @ the first place, so there's no point checking the PSR I bit.
  175. bl trace_hardirqs_on
  176. #endif
  177. .else
  178. @ IRQs off again before pulling preserved data off the stack
  179. disable_irq_notrace
  180. #ifdef CONFIG_TRACE_IRQFLAGS
  181. tst \rpsr, #PSR_I_BIT
  182. bleq trace_hardirqs_on
  183. tst \rpsr, #PSR_I_BIT
  184. blne trace_hardirqs_off
  185. #endif
  186. .endif
  187. ldr r1, [sp, #SVC_ADDR_LIMIT]
  188. uaccess_restore
  189. str r1, [tsk, #TI_ADDR_LIMIT]
  190. #ifndef CONFIG_THUMB2_KERNEL
  191. @ ARM mode SVC restore
  192. msr spsr_cxsf, \rpsr
  193. #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_32v6K)
  194. @ We must avoid clrex due to Cortex-A15 erratum #830321
  195. sub r0, sp, #4 @ uninhabited address
  196. strex r1, r2, [r0] @ clear the exclusive monitor
  197. #endif
  198. ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
  199. #else
  200. @ Thumb mode SVC restore
  201. ldr lr, [sp, #S_SP] @ top of the stack
  202. ldrd r0, r1, [sp, #S_LR] @ calling lr and pc
  203. @ We must avoid clrex due to Cortex-A15 erratum #830321
  204. strex r2, r1, [sp, #S_LR] @ clear the exclusive monitor
  205. stmdb lr!, {r0, r1, \rpsr} @ calling lr and rfe context
  206. ldmia sp, {r0 - r12}
  207. mov sp, lr
  208. ldr lr, [sp], #4
  209. rfeia sp!
  210. #endif
  211. .endm
  212. @
  213. @ svc_exit_via_fiq - like svc_exit but switches to FIQ mode before exit
  214. @
  215. @ This macro acts in a similar manner to svc_exit but switches to FIQ
  216. @ mode to restore the final part of the register state.
  217. @
  218. @ We cannot use the normal svc_exit procedure because that would
  219. @ clobber spsr_svc (FIQ could be delivered during the first few
  220. @ instructions of vector_swi meaning its contents have not been
  221. @ saved anywhere).
  222. @
  223. @ Note that, unlike svc_exit, this macro also does not allow a caller
  224. @ supplied rpsr. This is because the FIQ exceptions are not re-entrant
  225. @ and the handlers cannot call into the scheduler (meaning the value
  226. @ on the stack remains correct).
  227. @
  228. .macro svc_exit_via_fiq
  229. ldr r1, [sp, #SVC_ADDR_LIMIT]
  230. uaccess_restore
  231. str r1, [tsk, #TI_ADDR_LIMIT]
  232. #ifndef CONFIG_THUMB2_KERNEL
  233. @ ARM mode restore
  234. mov r0, sp
  235. ldmib r0, {r1 - r14} @ abort is deadly from here onward (it will
  236. @ clobber state restored below)
  237. msr cpsr_c, #FIQ_MODE | PSR_I_BIT | PSR_F_BIT
  238. add r8, r0, #S_PC
  239. ldr r9, [r0, #S_PSR]
  240. msr spsr_cxsf, r9
  241. ldr r0, [r0, #S_R0]
  242. ldmia r8, {pc}^
  243. #else
  244. @ Thumb mode restore
  245. add r0, sp, #S_R2
  246. ldr lr, [sp, #S_LR]
  247. ldr sp, [sp, #S_SP] @ abort is deadly from here onward (it will
  248. @ clobber state restored below)
  249. ldmia r0, {r2 - r12}
  250. mov r1, #FIQ_MODE | PSR_I_BIT | PSR_F_BIT
  251. msr cpsr_c, r1
  252. sub r0, #S_R2
  253. add r8, r0, #S_PC
  254. ldmia r0, {r0 - r1}
  255. rfeia r8
  256. #endif
  257. .endm
  258. .macro restore_user_regs, fast = 0, offset = 0
  259. uaccess_enable r1, isb=0
  260. #ifndef CONFIG_THUMB2_KERNEL
  261. @ ARM mode restore
  262. mov r2, sp
  263. ldr r1, [r2, #\offset + S_PSR] @ get calling cpsr
  264. ldr lr, [r2, #\offset + S_PC]! @ get pc
  265. tst r1, #PSR_I_BIT | 0x0f
  266. bne 1f
  267. msr spsr_cxsf, r1 @ save in spsr_svc
  268. #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_32v6K)
  269. @ We must avoid clrex due to Cortex-A15 erratum #830321
  270. strex r1, r2, [r2] @ clear the exclusive monitor
  271. #endif
  272. .if \fast
  273. ldmdb r2, {r1 - lr}^ @ get calling r1 - lr
  274. .else
  275. ldmdb r2, {r0 - lr}^ @ get calling r0 - lr
  276. .endif
  277. mov r0, r0 @ ARMv5T and earlier require a nop
  278. @ after ldm {}^
  279. add sp, sp, #\offset + PT_REGS_SIZE
  280. movs pc, lr @ return & move spsr_svc into cpsr
  281. 1: bug "Returning to usermode but unexpected PSR bits set?", \@
  282. #elif defined(CONFIG_CPU_V7M)
  283. @ V7M restore.
  284. @ Note that we don't need to do clrex here as clearing the local
  285. @ monitor is part of the exception entry and exit sequence.
  286. .if \offset
  287. add sp, #\offset
  288. .endif
  289. v7m_exception_slow_exit ret_r0 = \fast
  290. #else
  291. @ Thumb mode restore
  292. mov r2, sp
  293. load_user_sp_lr r2, r3, \offset + S_SP @ calling sp, lr
  294. ldr r1, [sp, #\offset + S_PSR] @ get calling cpsr
  295. ldr lr, [sp, #\offset + S_PC] @ get pc
  296. add sp, sp, #\offset + S_SP
  297. tst r1, #PSR_I_BIT | 0x0f
  298. bne 1f
  299. msr spsr_cxsf, r1 @ save in spsr_svc
  300. @ We must avoid clrex due to Cortex-A15 erratum #830321
  301. strex r1, r2, [sp] @ clear the exclusive monitor
  302. .if \fast
  303. ldmdb sp, {r1 - r12} @ get calling r1 - r12
  304. .else
  305. ldmdb sp, {r0 - r12} @ get calling r0 - r12
  306. .endif
  307. add sp, sp, #PT_REGS_SIZE - S_SP
  308. movs pc, lr @ return & move spsr_svc into cpsr
  309. 1: bug "Returning to usermode but unexpected PSR bits set?", \@
  310. #endif /* !CONFIG_THUMB2_KERNEL */
  311. .endm
  312. /*
  313. * Context tracking subsystem. Used to instrument transitions
  314. * between user and kernel mode.
  315. */
  316. .macro ct_user_exit, save = 1
  317. #ifdef CONFIG_CONTEXT_TRACKING
  318. .if \save
  319. stmdb sp!, {r0-r3, ip, lr}
  320. bl context_tracking_user_exit
  321. ldmia sp!, {r0-r3, ip, lr}
  322. .else
  323. bl context_tracking_user_exit
  324. .endif
  325. #endif
  326. .endm
  327. .macro ct_user_enter, save = 1
  328. #ifdef CONFIG_CONTEXT_TRACKING
  329. .if \save
  330. stmdb sp!, {r0-r3, ip, lr}
  331. bl context_tracking_user_enter
  332. ldmia sp!, {r0-r3, ip, lr}
  333. .else
  334. bl context_tracking_user_enter
  335. .endif
  336. #endif
  337. .endm
  338. .macro invoke_syscall, table, nr, tmp, ret, reload=0
  339. #ifdef CONFIG_CPU_SPECTRE
  340. mov \tmp, \nr
  341. cmp \tmp, #NR_syscalls @ check upper syscall limit
  342. movcs \tmp, #0
  343. csdb
  344. badr lr, \ret @ return address
  345. .if \reload
  346. add r1, sp, #S_R0 + S_OFF @ pointer to regs
  347. ldmccia r1, {r0 - r6} @ reload r0-r6
  348. stmccia sp, {r4, r5} @ update stack arguments
  349. .endif
  350. ldrcc pc, [\table, \tmp, lsl #2] @ call sys_* routine
  351. #else
  352. cmp \nr, #NR_syscalls @ check upper syscall limit
  353. badr lr, \ret @ return address
  354. .if \reload
  355. add r1, sp, #S_R0 + S_OFF @ pointer to regs
  356. ldmccia r1, {r0 - r6} @ reload r0-r6
  357. stmccia sp, {r4, r5} @ update stack arguments
  358. .endif
  359. ldrcc pc, [\table, \nr, lsl #2] @ call sys_* routine
  360. #endif
  361. .endm
  362. /*
  363. * These are the registers used in the syscall handler, and allow us to
  364. * have in theory up to 7 arguments to a function - r0 to r6.
  365. *
  366. * r7 is reserved for the system call number for thumb mode.
  367. *
  368. * Note that tbl == why is intentional.
  369. *
  370. * We must set at least "tsk" and "why" when calling ret_with_reschedule.
  371. */
  372. scno .req r7 @ syscall number
  373. tbl .req r8 @ syscall table pointer
  374. why .req r8 @ Linux syscall (!= 0)
  375. tsk .req r9 @ current thread_info