entry-header.S 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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, irq = 0
  160. .if \irq != 0
  161. @ IRQs already off
  162. #ifdef CONFIG_TRACE_IRQFLAGS
  163. @ The parent context IRQs must have been enabled to get here in
  164. @ the first place, so there's no point checking the PSR I bit.
  165. bl trace_hardirqs_on
  166. #endif
  167. .else
  168. @ IRQs off again before pulling preserved data off the stack
  169. disable_irq_notrace
  170. #ifdef CONFIG_TRACE_IRQFLAGS
  171. tst \rpsr, #PSR_I_BIT
  172. bleq trace_hardirqs_on
  173. tst \rpsr, #PSR_I_BIT
  174. blne trace_hardirqs_off
  175. #endif
  176. .endif
  177. msr spsr_cxsf, \rpsr
  178. #if defined(CONFIG_CPU_V6)
  179. ldr r0, [sp]
  180. strex r1, r2, [sp] @ clear the exclusive monitor
  181. ldmib sp, {r1 - pc}^ @ load r1 - pc, cpsr
  182. #elif defined(CONFIG_CPU_32v6K)
  183. clrex @ clear the exclusive monitor
  184. ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
  185. #else
  186. ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
  187. #endif
  188. .endm
  189. .macro restore_user_regs, fast = 0, offset = 0
  190. ldr r1, [sp, #\offset + S_PSR] @ get calling cpsr
  191. ldr lr, [sp, #\offset + S_PC]! @ get pc
  192. msr spsr_cxsf, r1 @ save in spsr_svc
  193. #if defined(CONFIG_CPU_V6)
  194. strex r1, r2, [sp] @ clear the exclusive monitor
  195. #elif defined(CONFIG_CPU_32v6K)
  196. clrex @ clear the exclusive monitor
  197. #endif
  198. .if \fast
  199. ldmdb sp, {r1 - lr}^ @ get calling r1 - lr
  200. .else
  201. ldmdb sp, {r0 - lr}^ @ get calling r0 - lr
  202. .endif
  203. mov r0, r0 @ ARMv5T and earlier require a nop
  204. @ after ldm {}^
  205. add sp, sp, #S_FRAME_SIZE - S_PC
  206. movs pc, lr @ return & move spsr_svc into cpsr
  207. .endm
  208. .macro get_thread_info, rd
  209. mov \rd, sp, lsr #13
  210. mov \rd, \rd, lsl #13
  211. .endm
  212. @
  213. @ 32-bit wide "mov pc, reg"
  214. @
  215. .macro movw_pc, reg
  216. mov pc, \reg
  217. .endm
  218. #else /* CONFIG_THUMB2_KERNEL */
  219. .macro svc_exit, rpsr, irq = 0
  220. .if \irq != 0
  221. @ IRQs already off
  222. #ifdef CONFIG_TRACE_IRQFLAGS
  223. @ The parent context IRQs must have been enabled to get here in
  224. @ the first place, so there's no point checking the PSR I bit.
  225. bl trace_hardirqs_on
  226. #endif
  227. .else
  228. @ IRQs off again before pulling preserved data off the stack
  229. disable_irq_notrace
  230. #ifdef CONFIG_TRACE_IRQFLAGS
  231. tst \rpsr, #PSR_I_BIT
  232. bleq trace_hardirqs_on
  233. tst \rpsr, #PSR_I_BIT
  234. blne trace_hardirqs_off
  235. #endif
  236. .endif
  237. ldr lr, [sp, #S_SP] @ top of the stack
  238. ldrd r0, r1, [sp, #S_LR] @ calling lr and pc
  239. clrex @ clear the exclusive monitor
  240. stmdb lr!, {r0, r1, \rpsr} @ calling lr and rfe context
  241. ldmia sp, {r0 - r12}
  242. mov sp, lr
  243. ldr lr, [sp], #4
  244. rfeia sp!
  245. .endm
  246. #ifdef CONFIG_CPU_V7M
  247. /*
  248. * Note we don't need to do clrex here as clearing the local monitor is
  249. * part of each exception entry and exit sequence.
  250. */
  251. .macro restore_user_regs, fast = 0, offset = 0
  252. .if \offset
  253. add sp, #\offset
  254. .endif
  255. v7m_exception_slow_exit ret_r0 = \fast
  256. .endm
  257. #else /* ifdef CONFIG_CPU_V7M */
  258. .macro restore_user_regs, fast = 0, offset = 0
  259. clrex @ clear the exclusive monitor
  260. mov r2, sp
  261. load_user_sp_lr r2, r3, \offset + S_SP @ calling sp, lr
  262. ldr r1, [sp, #\offset + S_PSR] @ get calling cpsr
  263. ldr lr, [sp, #\offset + S_PC] @ get pc
  264. add sp, sp, #\offset + S_SP
  265. msr spsr_cxsf, r1 @ save in spsr_svc
  266. .if \fast
  267. ldmdb sp, {r1 - r12} @ get calling r1 - r12
  268. .else
  269. ldmdb sp, {r0 - r12} @ get calling r0 - r12
  270. .endif
  271. add sp, sp, #S_FRAME_SIZE - S_SP
  272. movs pc, lr @ return & move spsr_svc into cpsr
  273. .endm
  274. #endif /* ifdef CONFIG_CPU_V7M / else */
  275. .macro get_thread_info, rd
  276. mov \rd, sp
  277. lsr \rd, \rd, #13
  278. mov \rd, \rd, lsl #13
  279. .endm
  280. @
  281. @ 32-bit wide "mov pc, reg"
  282. @
  283. .macro movw_pc, reg
  284. mov pc, \reg
  285. nop
  286. .endm
  287. #endif /* !CONFIG_THUMB2_KERNEL */
  288. /*
  289. * Context tracking subsystem. Used to instrument transitions
  290. * between user and kernel mode.
  291. */
  292. .macro ct_user_exit, save = 1
  293. #ifdef CONFIG_CONTEXT_TRACKING
  294. .if \save
  295. stmdb sp!, {r0-r3, ip, lr}
  296. bl user_exit
  297. ldmia sp!, {r0-r3, ip, lr}
  298. .else
  299. bl user_exit
  300. .endif
  301. #endif
  302. .endm
  303. .macro ct_user_enter, save = 1
  304. #ifdef CONFIG_CONTEXT_TRACKING
  305. .if \save
  306. stmdb sp!, {r0-r3, ip, lr}
  307. bl user_enter
  308. ldmia sp!, {r0-r3, ip, lr}
  309. .else
  310. bl user_enter
  311. .endif
  312. #endif
  313. .endm
  314. /*
  315. * These are the registers used in the syscall handler, and allow us to
  316. * have in theory up to 7 arguments to a function - r0 to r6.
  317. *
  318. * r7 is reserved for the system call number for thumb mode.
  319. *
  320. * Note that tbl == why is intentional.
  321. *
  322. * We must set at least "tsk" and "why" when calling ret_with_reschedule.
  323. */
  324. scno .req r7 @ syscall number
  325. tbl .req r8 @ syscall table pointer
  326. why .req r8 @ Linux syscall (!= 0)
  327. tsk .req r9 @ current thread_info