entry-header.S 9.2 KB

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