entry-header.S 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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
  35. #ifdef CONFIG_ALIGNMENT_TRAP
  36. ldr \rtemp, .LCcralign
  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. @ write basic exception frame
  118. stmdb r2!, {r1, r3-r5}
  119. ldmia sp, {r1, r3-r5}
  120. .if \ret_r0
  121. stmdb r2!, {r0, r3-r5}
  122. .else
  123. stmdb r2!, {r1, r3-r5}
  124. .endif
  125. @ restore process sp
  126. msr psp, r2
  127. @ restore original r4-r11
  128. ldmia sp!, {r0-r11}
  129. @ restore main sp
  130. add sp, sp, #S_FRAME_SIZE-S_IP
  131. cpsie i
  132. bx lr
  133. .endm
  134. #endif /* CONFIG_CPU_V7M */
  135. @
  136. @ Store/load the USER SP and LR registers by switching to the SYS
  137. @ mode. Useful in Thumb-2 mode where "stm/ldm rd, {sp, lr}^" is not
  138. @ available. Should only be called from SVC mode
  139. @
  140. .macro store_user_sp_lr, rd, rtemp, offset = 0
  141. mrs \rtemp, cpsr
  142. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  143. msr cpsr_c, \rtemp @ switch to the SYS mode
  144. str sp, [\rd, #\offset] @ save sp_usr
  145. str lr, [\rd, #\offset + 4] @ save lr_usr
  146. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  147. msr cpsr_c, \rtemp @ switch back to the SVC mode
  148. .endm
  149. .macro load_user_sp_lr, rd, rtemp, offset = 0
  150. mrs \rtemp, cpsr
  151. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  152. msr cpsr_c, \rtemp @ switch to the SYS mode
  153. ldr sp, [\rd, #\offset] @ load sp_usr
  154. ldr lr, [\rd, #\offset + 4] @ load lr_usr
  155. eor \rtemp, \rtemp, #(SVC_MODE ^ SYSTEM_MODE)
  156. msr cpsr_c, \rtemp @ switch back to the SVC mode
  157. .endm
  158. #ifndef CONFIG_THUMB2_KERNEL
  159. .macro svc_exit, rpsr
  160. msr spsr_cxsf, \rpsr
  161. #if defined(CONFIG_CPU_V6)
  162. ldr r0, [sp]
  163. strex r1, r2, [sp] @ clear the exclusive monitor
  164. ldmib sp, {r1 - pc}^ @ load r1 - pc, cpsr
  165. #elif defined(CONFIG_CPU_32v6K)
  166. clrex @ clear the exclusive monitor
  167. ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
  168. #else
  169. ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
  170. #endif
  171. .endm
  172. .macro restore_user_regs, fast = 0, offset = 0
  173. ldr r1, [sp, #\offset + S_PSR] @ get calling cpsr
  174. ldr lr, [sp, #\offset + S_PC]! @ get pc
  175. msr spsr_cxsf, r1 @ save in spsr_svc
  176. #if defined(CONFIG_CPU_V6)
  177. strex r1, r2, [sp] @ clear the exclusive monitor
  178. #elif defined(CONFIG_CPU_32v6K)
  179. clrex @ clear the exclusive monitor
  180. #endif
  181. .if \fast
  182. ldmdb sp, {r1 - lr}^ @ get calling r1 - lr
  183. .else
  184. ldmdb sp, {r0 - lr}^ @ get calling r0 - lr
  185. .endif
  186. mov r0, r0 @ ARMv5T and earlier require a nop
  187. @ after ldm {}^
  188. add sp, sp, #S_FRAME_SIZE - S_PC
  189. movs pc, lr @ return & move spsr_svc into cpsr
  190. .endm
  191. .macro get_thread_info, rd
  192. mov \rd, sp, lsr #13
  193. mov \rd, \rd, lsl #13
  194. .endm
  195. @
  196. @ 32-bit wide "mov pc, reg"
  197. @
  198. .macro movw_pc, reg
  199. mov pc, \reg
  200. .endm
  201. #else /* CONFIG_THUMB2_KERNEL */
  202. .macro svc_exit, rpsr
  203. ldr lr, [sp, #S_SP] @ top of the stack
  204. ldrd r0, r1, [sp, #S_LR] @ calling lr and pc
  205. clrex @ clear the exclusive monitor
  206. stmdb lr!, {r0, r1, \rpsr} @ calling lr and rfe context
  207. ldmia sp, {r0 - r12}
  208. mov sp, lr
  209. ldr lr, [sp], #4
  210. rfeia sp!
  211. .endm
  212. #ifdef CONFIG_CPU_V7M
  213. /*
  214. * Note we don't need to do clrex here as clearing the local monitor is
  215. * part of each exception entry and exit sequence.
  216. */
  217. .macro restore_user_regs, fast = 0, offset = 0
  218. .if \offset
  219. add sp, #\offset
  220. .endif
  221. v7m_exception_slow_exit ret_r0 = \fast
  222. .endm
  223. #else /* ifdef CONFIG_CPU_V7M */
  224. .macro restore_user_regs, fast = 0, offset = 0
  225. clrex @ clear the exclusive monitor
  226. mov r2, sp
  227. load_user_sp_lr r2, r3, \offset + S_SP @ calling sp, lr
  228. ldr r1, [sp, #\offset + S_PSR] @ get calling cpsr
  229. ldr lr, [sp, #\offset + S_PC] @ get pc
  230. add sp, sp, #\offset + S_SP
  231. msr spsr_cxsf, r1 @ save in spsr_svc
  232. .if \fast
  233. ldmdb sp, {r1 - r12} @ get calling r1 - r12
  234. .else
  235. ldmdb sp, {r0 - r12} @ get calling r0 - r12
  236. .endif
  237. add sp, sp, #S_FRAME_SIZE - S_SP
  238. movs pc, lr @ return & move spsr_svc into cpsr
  239. .endm
  240. #endif /* ifdef CONFIG_CPU_V7M / else */
  241. .macro get_thread_info, rd
  242. mov \rd, sp
  243. lsr \rd, \rd, #13
  244. mov \rd, \rd, lsl #13
  245. .endm
  246. @
  247. @ 32-bit wide "mov pc, reg"
  248. @
  249. .macro movw_pc, reg
  250. mov pc, \reg
  251. nop
  252. .endm
  253. #endif /* !CONFIG_THUMB2_KERNEL */
  254. /*
  255. * These are the registers used in the syscall handler, and allow us to
  256. * have in theory up to 7 arguments to a function - r0 to r6.
  257. *
  258. * r7 is reserved for the system call number for thumb mode.
  259. *
  260. * Note that tbl == why is intentional.
  261. *
  262. * We must set at least "tsk" and "why" when calling ret_with_reschedule.
  263. */
  264. scno .req r7 @ syscall number
  265. tbl .req r8 @ syscall table pointer
  266. why .req r8 @ Linux syscall (!= 0)
  267. tsk .req r9 @ current thread_info