entry-header.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/uaccess-asm.h>
  9. #include <asm/v7m.h>
  10. @ Bad Abort numbers
  11. @ -----------------
  12. @
  13. #define BAD_PREFETCH 0
  14. #define BAD_DATA 1
  15. #define BAD_ADDREXCPTN 2
  16. #define BAD_IRQ 3
  17. #define BAD_UNDEFINSTR 4
  18. @
  19. @ Most of the stack format comes from struct pt_regs, but with
  20. @ the addition of 8 bytes for storing syscall args 5 and 6.
  21. @ This _must_ remain a multiple of 8 for EABI.
  22. @
  23. #define S_OFF 8
  24. /*
  25. * The SWI code relies on the fact that R0 is at the bottom of the stack
  26. * (due to slow/fast restore user regs).
  27. */
  28. #if S_R0 != 0
  29. #error "Please fix"
  30. #endif
  31. .macro zero_fp
  32. #ifdef CONFIG_FRAME_POINTER
  33. mov fp, #0
  34. #endif
  35. .endm
  36. #ifdef CONFIG_ALIGNMENT_TRAP
  37. #define ATRAP(x...) x
  38. #else
  39. #define ATRAP(x...)
  40. #endif
  41. .macro alignment_trap, rtmp1, rtmp2, label
  42. #ifdef CONFIG_ALIGNMENT_TRAP
  43. mrc p15, 0, \rtmp2, c1, c0, 0
  44. ldr_va \rtmp1, \label
  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
  115. ldr lr, [lr]
  116. @ read original r12, sp, lr, pc and xPSR
  117. add r12, sp, #S_IP
  118. ldmia r12, {r1-r5}
  119. @ an exception frame is always 8-byte aligned. To tell the hardware if
  120. @ the sp to be restored is aligned or not set bit 9 of the saved xPSR
  121. @ accordingly.
  122. tst r2, #4
  123. subne r2, r2, #4
  124. orrne r5, V7M_xPSR_FRAMEPTRALIGN
  125. biceq r5, V7M_xPSR_FRAMEPTRALIGN
  126. @ ensure bit 0 is cleared in the PC, otherwise behaviour is
  127. @ unpredictable
  128. bic r4, #1
  129. @ write basic exception frame
  130. stmdb r2!, {r1, r3-r5}
  131. ldmia sp, {r1, r3-r5}
  132. .if \ret_r0
  133. stmdb r2!, {r0, r3-r5}
  134. .else
  135. stmdb r2!, {r1, r3-r5}
  136. .endif
  137. @ restore process sp
  138. msr psp, r2
  139. @ restore original r4-r11
  140. ldmia sp!, {r0-r11}
  141. @ restore main sp
  142. add sp, sp, #PT_REGS_SIZE-S_IP
  143. cpsie i
  144. bx lr
  145. .endm
  146. #endif /* CONFIG_CPU_V7M */
  147. @
  148. @ Store/load the USER SP and LR registers by switching to the SYS
  149. @ mode. Useful in Thumb-2 mode where "stm/ldm rd, {sp, lr}^" is not
  150. @ available. Should only be called from SVC mode
  151. @
  152. .macro store_user_sp_lr, rd, rtemp, offset = 0
  153. mrs \rtemp, cpsr
  154. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  155. msr cpsr_c, \rtemp @ switch to the SYS mode
  156. str sp, [\rd, #\offset] @ save sp_usr
  157. str lr, [\rd, #\offset + 4] @ save lr_usr
  158. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  159. msr cpsr_c, \rtemp @ switch back to the SVC mode
  160. .endm
  161. .macro load_user_sp_lr, rd, rtemp, offset = 0
  162. mrs \rtemp, cpsr
  163. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  164. msr cpsr_c, \rtemp @ switch to the SYS mode
  165. ldr sp, [\rd, #\offset] @ load sp_usr
  166. ldr lr, [\rd, #\offset + 4] @ load lr_usr
  167. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  168. msr cpsr_c, \rtemp @ switch back to the SVC mode
  169. .endm
  170. .macro svc_exit, rpsr, irq = 0
  171. .if \irq != 0
  172. @ IRQs already off
  173. #ifdef CONFIG_TRACE_IRQFLAGS
  174. @ The parent context IRQs must have been enabled to get here in
  175. @ the first place, so there's no point checking the PSR I bit.
  176. bl trace_hardirqs_on
  177. #endif
  178. .else
  179. @ IRQs off again before pulling preserved data off the stack
  180. disable_irq_notrace
  181. #ifdef CONFIG_TRACE_IRQFLAGS
  182. tst \rpsr, #PSR_I_BIT
  183. bleq trace_hardirqs_on
  184. tst \rpsr, #PSR_I_BIT
  185. blne trace_hardirqs_off
  186. #endif
  187. .endif
  188. uaccess_exit tsk, r0, r1
  189. #ifndef CONFIG_THUMB2_KERNEL
  190. @ ARM mode SVC restore
  191. msr spsr_cxsf, \rpsr
  192. #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_32v6K)
  193. @ We must avoid clrex due to Cortex-A15 erratum #830321
  194. sub r0, sp, #4 @ uninhabited address
  195. strex r1, r2, [r0] @ clear the exclusive monitor
  196. #endif
  197. ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
  198. #else
  199. @ Thumb mode SVC restore
  200. ldr lr, [sp, #S_SP] @ top of the stack
  201. ldrd r0, r1, [sp, #S_LR] @ calling lr and pc
  202. @ We must avoid clrex due to Cortex-A15 erratum #830321
  203. strex r2, r1, [sp, #S_LR] @ clear the exclusive monitor
  204. stmdb lr!, {r0, r1, \rpsr} @ calling lr and rfe context
  205. ldmia sp, {r0 - r12}
  206. mov sp, lr
  207. ldr lr, [sp], #4
  208. rfeia sp!
  209. #endif
  210. .endm
  211. @
  212. @ svc_exit_via_fiq - like svc_exit but switches to FIQ mode before exit
  213. @
  214. @ This macro acts in a similar manner to svc_exit but switches to FIQ
  215. @ mode to restore the final part of the register state.
  216. @
  217. @ We cannot use the normal svc_exit procedure because that would
  218. @ clobber spsr_svc (FIQ could be delivered during the first few
  219. @ instructions of vector_swi meaning its contents have not been
  220. @ saved anywhere).
  221. @
  222. @ Note that, unlike svc_exit, this macro also does not allow a caller
  223. @ supplied rpsr. This is because the FIQ exceptions are not re-entrant
  224. @ and the handlers cannot call into the scheduler (meaning the value
  225. @ on the stack remains correct).
  226. @
  227. .macro svc_exit_via_fiq
  228. uaccess_exit tsk, r0, r1
  229. #ifndef CONFIG_THUMB2_KERNEL
  230. @ ARM mode restore
  231. mov r0, sp
  232. ldmib r0, {r1 - r14} @ abort is deadly from here onward (it will
  233. @ clobber state restored below)
  234. msr cpsr_c, #FIQ_MODE | PSR_I_BIT | PSR_F_BIT
  235. add r8, r0, #S_PC
  236. ldr r9, [r0, #S_PSR]
  237. msr spsr_cxsf, r9
  238. ldr r0, [r0, #S_R0]
  239. ldmia r8, {pc}^
  240. #else
  241. @ Thumb mode restore
  242. add r0, sp, #S_R2
  243. ldr lr, [sp, #S_LR]
  244. ldr sp, [sp, #S_SP] @ abort is deadly from here onward (it will
  245. @ clobber state restored below)
  246. ldmia r0, {r2 - r12}
  247. mov r1, #FIQ_MODE | PSR_I_BIT | PSR_F_BIT
  248. msr cpsr_c, r1
  249. sub r0, #S_R2
  250. add r8, r0, #S_PC
  251. ldmia r0, {r0 - r1}
  252. rfeia r8
  253. #endif
  254. .endm
  255. .macro restore_user_regs, fast = 0, offset = 0
  256. #if defined(CONFIG_CPU_32v6K) && \
  257. (!defined(CONFIG_CPU_V6) || defined(CONFIG_SMP))
  258. #ifdef CONFIG_CPU_V6
  259. ALT_SMP(nop)
  260. ALT_UP_B(.L1_\@)
  261. #endif
  262. @ The TLS register update is deferred until return to user space so we
  263. @ can use it for other things while running in the kernel
  264. mrc p15, 0, r1, c13, c0, 3 @ get current_thread_info pointer
  265. ldr r1, [r1, #TI_TP_VALUE]
  266. mcr p15, 0, r1, c13, c0, 3 @ set TLS register
  267. .L1_\@:
  268. #endif
  269. uaccess_enable r1, isb=0
  270. #ifndef CONFIG_THUMB2_KERNEL
  271. @ ARM mode restore
  272. mov r2, sp
  273. ldr r1, [r2, #\offset + S_PSR] @ get calling cpsr
  274. ldr lr, [r2, #\offset + S_PC]! @ get pc
  275. tst r1, #PSR_I_BIT | 0x0f
  276. bne 1f
  277. msr spsr_cxsf, r1 @ save in spsr_svc
  278. #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_32v6K)
  279. @ We must avoid clrex due to Cortex-A15 erratum #830321
  280. strex r1, r2, [r2] @ clear the exclusive monitor
  281. #endif
  282. .if \fast
  283. ldmdb r2, {r1 - lr}^ @ get calling r1 - lr
  284. .else
  285. ldmdb r2, {r0 - lr}^ @ get calling r0 - lr
  286. .endif
  287. mov r0, r0 @ ARMv5T and earlier require a nop
  288. @ after ldm {}^
  289. add sp, sp, #\offset + PT_REGS_SIZE
  290. movs pc, lr @ return & move spsr_svc into cpsr
  291. 1: bug "Returning to usermode but unexpected PSR bits set?", \@
  292. #elif defined(CONFIG_CPU_V7M)
  293. @ V7M restore.
  294. @ Note that we don't need to do clrex here as clearing the local
  295. @ monitor is part of the exception entry and exit sequence.
  296. .if \offset
  297. add sp, #\offset
  298. .endif
  299. v7m_exception_slow_exit ret_r0 = \fast
  300. #else
  301. @ Thumb mode restore
  302. mov r2, sp
  303. load_user_sp_lr r2, r3, \offset + S_SP @ calling sp, lr
  304. ldr r1, [sp, #\offset + S_PSR] @ get calling cpsr
  305. ldr lr, [sp, #\offset + S_PC] @ get pc
  306. add sp, sp, #\offset + S_SP
  307. tst r1, #PSR_I_BIT | 0x0f
  308. bne 1f
  309. msr spsr_cxsf, r1 @ save in spsr_svc
  310. @ We must avoid clrex due to Cortex-A15 erratum #830321
  311. strex r1, r2, [sp] @ clear the exclusive monitor
  312. .if \fast
  313. ldmdb sp, {r1 - r12} @ get calling r1 - r12
  314. .else
  315. ldmdb sp, {r0 - r12} @ get calling r0 - r12
  316. .endif
  317. add sp, sp, #PT_REGS_SIZE - S_SP
  318. movs pc, lr @ return & move spsr_svc into cpsr
  319. 1: bug "Returning to usermode but unexpected PSR bits set?", \@
  320. #endif /* !CONFIG_THUMB2_KERNEL */
  321. .endm
  322. /*
  323. * Context tracking subsystem. Used to instrument transitions
  324. * between user and kernel mode.
  325. */
  326. .macro ct_user_exit, save = 1
  327. #ifdef CONFIG_CONTEXT_TRACKING_USER
  328. .if \save
  329. stmdb sp!, {r0-r3, ip, lr}
  330. bl user_exit_callable
  331. ldmia sp!, {r0-r3, ip, lr}
  332. .else
  333. bl user_exit_callable
  334. .endif
  335. #endif
  336. .endm
  337. .macro ct_user_enter, save = 1
  338. #ifdef CONFIG_CONTEXT_TRACKING_USER
  339. .if \save
  340. stmdb sp!, {r0-r3, ip, lr}
  341. bl user_enter_callable
  342. ldmia sp!, {r0-r3, ip, lr}
  343. .else
  344. bl user_enter_callable
  345. .endif
  346. #endif
  347. .endm
  348. .macro invoke_syscall, table, nr, tmp, ret, reload=0
  349. #ifdef CONFIG_CPU_SPECTRE
  350. mov \tmp, \nr
  351. cmp \tmp, #NR_syscalls @ check upper syscall limit
  352. movcs \tmp, #0
  353. csdb
  354. badr lr, \ret @ return address
  355. .if \reload
  356. add r1, sp, #S_R0 + S_OFF @ pointer to regs
  357. ldmiacc r1, {r0 - r6} @ reload r0-r6
  358. stmiacc sp, {r4, r5} @ update stack arguments
  359. .endif
  360. ldrcc pc, [\table, \tmp, lsl #2] @ call sys_* routine
  361. #else
  362. cmp \nr, #NR_syscalls @ check upper syscall limit
  363. badr lr, \ret @ return address
  364. .if \reload
  365. add r1, sp, #S_R0 + S_OFF @ pointer to regs
  366. ldmiacc r1, {r0 - r6} @ reload r0-r6
  367. stmiacc sp, {r4, r5} @ update stack arguments
  368. .endif
  369. ldrcc pc, [\table, \nr, lsl #2] @ call sys_* routine
  370. #endif
  371. .endm
  372. /*
  373. * These are the registers used in the syscall handler, and allow us to
  374. * have in theory up to 7 arguments to a function - r0 to r6.
  375. *
  376. * r7 is reserved for the system call number for thumb mode.
  377. *
  378. * Note that tbl == why is intentional.
  379. *
  380. * We must set at least "tsk" and "why" when calling ret_with_reschedule.
  381. */
  382. scno .req r7 @ syscall number
  383. tbl .req r8 @ syscall table pointer
  384. why .req r8 @ Linux syscall (!= 0)
  385. tsk .req r9 @ current thread_info
  386. .macro do_overflow_check, frame_size:req
  387. #ifdef CONFIG_VMAP_STACK
  388. @
  389. @ Test whether the SP has overflowed. Task and IRQ stacks are aligned
  390. @ so that SP & BIT(THREAD_SIZE_ORDER + PAGE_SHIFT) should always be
  391. @ zero.
  392. @
  393. ARM( tst sp, #1 << (THREAD_SIZE_ORDER + PAGE_SHIFT) )
  394. THUMB( tst r1, #1 << (THREAD_SIZE_ORDER + PAGE_SHIFT) )
  395. THUMB( it ne )
  396. bne .Lstack_overflow_check\@
  397. .pushsection .text
  398. .Lstack_overflow_check\@:
  399. @
  400. @ The stack pointer is not pointing to a valid vmap'ed stack, but it
  401. @ may be pointing into the linear map instead, which may happen if we
  402. @ are already running from the overflow stack. We cannot detect overflow
  403. @ in such cases so just carry on.
  404. @
  405. str ip, [r0, #12] @ Stash IP on the mode stack
  406. ldr_va ip, high_memory @ Start of VMALLOC space
  407. ARM( cmp sp, ip ) @ SP in vmalloc space?
  408. THUMB( cmp r1, ip )
  409. THUMB( itt lo )
  410. ldrlo ip, [r0, #12] @ Restore IP
  411. blo .Lout\@ @ Carry on
  412. THUMB( sub r1, sp, r1 ) @ Restore original R1
  413. THUMB( sub sp, r1 ) @ Restore original SP
  414. add sp, sp, #\frame_size @ Undo svc_entry's SP change
  415. b __bad_stack @ Handle VMAP stack overflow
  416. .popsection
  417. .Lout\@:
  418. #endif
  419. .endm