entry-compact.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2014-15 Synopsys, Inc. (www.synopsys.com)
  4. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  5. *
  6. * Vineetg: March 2009 (Supporting 2 levels of Interrupts)
  7. * Stack switching code can no longer reliably rely on the fact that
  8. * if we are NOT in user mode, stack is switched to kernel mode.
  9. * e.g. L2 IRQ interrupted a L1 ISR which had not yet completed
  10. * it's prologue including stack switching from user mode
  11. *
  12. * Vineetg: Aug 28th 2008: Bug #94984
  13. * -Zero Overhead Loop Context shd be cleared when entering IRQ/EXcp/Trap
  14. * Normally CPU does this automatically, however when doing FAKE rtie,
  15. * we also need to explicitly do this. The problem in macros
  16. * FAKE_RET_FROM_EXCPN and FAKE_RET_FROM_EXCPN_LOCK_IRQ was that this bit
  17. * was being "CLEARED" rather then "SET". Actually "SET" clears ZOL context
  18. *
  19. * Vineetg: May 5th 2008
  20. * -Modified CALLEE_REG save/restore macros to handle the fact that
  21. * r25 contains the kernel current task ptr
  22. * - Defined Stack Switching Macro to be reused in all intr/excp hdlrs
  23. * - Shaved off 11 instructions from RESTORE_ALL_INT1 by using the
  24. * address Write back load ld.ab instead of separate ld/add instn
  25. *
  26. * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
  27. */
  28. #ifndef __ASM_ARC_ENTRY_COMPACT_H
  29. #define __ASM_ARC_ENTRY_COMPACT_H
  30. #include <asm/asm-offsets.h>
  31. #include <asm/irqflags-compact.h>
  32. #include <asm/thread_info.h> /* For THREAD_SIZE */
  33. /*--------------------------------------------------------------
  34. * Switch to Kernel Mode stack if SP points to User Mode stack
  35. *
  36. * Entry : r9 contains pre-IRQ/exception/trap status32
  37. * Exit : SP set to K mode stack
  38. * SP at the time of entry (K/U) saved @ pt_regs->sp
  39. * Clobbers: r9
  40. *-------------------------------------------------------------*/
  41. .macro SWITCH_TO_KERNEL_STK
  42. /* User Mode when this happened ? Yes: Proceed to switch stack */
  43. bbit1 r9, STATUS_U_BIT, 88f
  44. /* OK we were already in kernel mode when this event happened, thus can
  45. * assume SP is kernel mode SP. _NO_ need to do any stack switching
  46. */
  47. #ifdef CONFIG_ARC_COMPACT_IRQ_LEVELS
  48. /* However....
  49. * If Level 2 Interrupts enabled, we may end up with a corner case:
  50. * 1. User Task executing
  51. * 2. L1 IRQ taken, ISR starts (CPU auto-switched to KERNEL mode)
  52. * 3. But before it could switch SP from USER to KERNEL stack
  53. * a L2 IRQ "Interrupts" L1
  54. * Thay way although L2 IRQ happened in Kernel mode, stack is still
  55. * not switched.
  56. * To handle this, we may need to switch stack even if in kernel mode
  57. * provided SP has values in range of USER mode stack ( < 0x7000_0000 )
  58. */
  59. brlo sp, VMALLOC_START, 88f
  60. /* TODO: vineetg:
  61. * We need to be a bit more cautious here. What if a kernel bug in
  62. * L1 ISR, caused SP to go whaco (some small value which looks like
  63. * USER stk) and then we take L2 ISR.
  64. * Above brlo alone would treat it as a valid L1-L2 scenario
  65. * instead of shouting around
  66. * The only feasible way is to make sure this L2 happened in
  67. * L1 prelogue ONLY i.e. ilink2 is less than a pre-set marker in
  68. * L1 ISR before it switches stack
  69. */
  70. #endif
  71. /*------Intr/Ecxp happened in kernel mode, SP already setup ------ */
  72. /* save it nevertheless @ pt_regs->sp for uniformity */
  73. b.d 66f
  74. st sp, [sp, PT_sp - SZ_PT_REGS]
  75. 88: /*------Intr/Ecxp happened in user mode, "switch" stack ------ */
  76. GET_CURR_TASK_ON_CPU r9
  77. /* With current tsk in r9, get it's kernel mode stack base */
  78. GET_TSK_STACK_BASE r9, r9
  79. /* save U mode SP @ pt_regs->sp */
  80. st sp, [r9, PT_sp - SZ_PT_REGS]
  81. /* final SP switch */
  82. mov sp, r9
  83. 66:
  84. .endm
  85. /*------------------------------------------------------------
  86. * "FAKE" a rtie to return from CPU Exception context
  87. * This is to re-enable Exceptions within exception
  88. * Look at EV_ProtV to see how this is actually used
  89. *-------------------------------------------------------------*/
  90. .macro FAKE_RET_FROM_EXCPN
  91. lr r9, [status32]
  92. bclr r9, r9, STATUS_AE_BIT
  93. or r9, r9, (STATUS_E1_MASK|STATUS_E2_MASK)
  94. sr r9, [erstatus]
  95. mov r9, 55f
  96. sr r9, [eret]
  97. rtie
  98. 55:
  99. .endm
  100. /*--------------------------------------------------------------
  101. * For early Exception/ISR Prologue, a core reg is temporarily needed to
  102. * code the rest of prolog (stack switching). This is done by stashing
  103. * it to memory (non-SMP case) or SCRATCH0 Aux Reg (SMP).
  104. *
  105. * Before saving the full regfile - this reg is restored back, only
  106. * to be saved again on kernel mode stack, as part of pt_regs.
  107. *-------------------------------------------------------------*/
  108. .macro PROLOG_FREEUP_REG reg, mem
  109. st \reg, [\mem]
  110. .endm
  111. .macro PROLOG_RESTORE_REG reg, mem
  112. ld \reg, [\mem]
  113. .endm
  114. /*--------------------------------------------------------------
  115. * Exception Entry prologue
  116. * -Switches stack to K mode (if not already)
  117. * -Saves the register file
  118. *
  119. * After this it is safe to call the "C" handlers
  120. *-------------------------------------------------------------*/
  121. .macro EXCEPTION_PROLOGUE
  122. /* Need at least 1 reg to code the early exception prologue */
  123. PROLOG_FREEUP_REG r9, @ex_saved_reg1
  124. /* U/K mode at time of exception (stack not switched if already K) */
  125. lr r9, [erstatus]
  126. /* ARC700 doesn't provide auto-stack switching */
  127. SWITCH_TO_KERNEL_STK
  128. #ifdef CONFIG_ARC_CURR_IN_REG
  129. /* Treat r25 as scratch reg (save on stack) and load with "current" */
  130. PUSH r25
  131. GET_CURR_TASK_ON_CPU r25
  132. #else
  133. sub sp, sp, 4
  134. #endif
  135. st.a r0, [sp, -8] /* orig_r0 needed for syscall (skip ECR slot) */
  136. sub sp, sp, 4 /* skip pt_regs->sp, already saved above */
  137. /* Restore r9 used to code the early prologue */
  138. PROLOG_RESTORE_REG r9, @ex_saved_reg1
  139. /* now we are ready to save the regfile */
  140. SAVE_R0_TO_R12
  141. PUSH gp
  142. PUSH fp
  143. PUSH blink
  144. PUSHAX eret
  145. PUSHAX erstatus
  146. PUSH lp_count
  147. PUSHAX lp_end
  148. PUSHAX lp_start
  149. PUSHAX erbta
  150. lr r10, [ecr]
  151. st r10, [sp, PT_event] /* EV_Trap expects r10 to have ECR */
  152. .endm
  153. /*--------------------------------------------------------------
  154. * Restore all registers used by system call or Exceptions
  155. * SP should always be pointing to the next free stack element
  156. * when entering this macro.
  157. *
  158. * NOTE:
  159. *
  160. * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
  161. * for memory load operations. If used in that way interrupts are deffered
  162. * by hardware and that is not good.
  163. *-------------------------------------------------------------*/
  164. .macro EXCEPTION_EPILOGUE
  165. POPAX erbta
  166. POPAX lp_start
  167. POPAX lp_end
  168. POP r9
  169. mov lp_count, r9 ;LD to lp_count is not allowed
  170. POPAX erstatus
  171. POPAX eret
  172. POP blink
  173. POP fp
  174. POP gp
  175. RESTORE_R12_TO_R0
  176. #ifdef CONFIG_ARC_CURR_IN_REG
  177. ld r25, [sp, 12]
  178. #endif
  179. ld sp, [sp] /* restore original sp */
  180. /* orig_r0, ECR, user_r25 skipped automatically */
  181. .endm
  182. /* Dummy ECR values for Interrupts */
  183. #define event_IRQ1 0x0031abcd
  184. #define event_IRQ2 0x0032abcd
  185. .macro INTERRUPT_PROLOGUE LVL
  186. /* free up r9 as scratchpad */
  187. PROLOG_FREEUP_REG r9, @int\LVL\()_saved_reg
  188. /* Which mode (user/kernel) was the system in when intr occurred */
  189. lr r9, [status32_l\LVL\()]
  190. SWITCH_TO_KERNEL_STK
  191. #ifdef CONFIG_ARC_CURR_IN_REG
  192. /* Treat r25 as scratch reg (save on stack) and load with "current" */
  193. PUSH r25
  194. GET_CURR_TASK_ON_CPU r25
  195. #else
  196. sub sp, sp, 4
  197. #endif
  198. PUSH 0x003\LVL\()abcd /* Dummy ECR */
  199. sub sp, sp, 8 /* skip orig_r0 (not needed)
  200. skip pt_regs->sp, already saved above */
  201. /* Restore r9 used to code the early prologue */
  202. PROLOG_RESTORE_REG r9, @int\LVL\()_saved_reg
  203. SAVE_R0_TO_R12
  204. PUSH gp
  205. PUSH fp
  206. PUSH blink
  207. PUSH ilink\LVL\()
  208. PUSHAX status32_l\LVL\()
  209. PUSH lp_count
  210. PUSHAX lp_end
  211. PUSHAX lp_start
  212. PUSHAX bta_l\LVL\()
  213. .endm
  214. /*--------------------------------------------------------------
  215. * Restore all registers used by interrupt handlers.
  216. *
  217. * NOTE:
  218. *
  219. * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
  220. * for memory load operations. If used in that way interrupts are deffered
  221. * by hardware and that is not good.
  222. *-------------------------------------------------------------*/
  223. .macro INTERRUPT_EPILOGUE LVL
  224. POPAX bta_l\LVL\()
  225. POPAX lp_start
  226. POPAX lp_end
  227. POP r9
  228. mov lp_count, r9 ;LD to lp_count is not allowed
  229. POPAX status32_l\LVL\()
  230. POP ilink\LVL\()
  231. POP blink
  232. POP fp
  233. POP gp
  234. RESTORE_R12_TO_R0
  235. #ifdef CONFIG_ARC_CURR_IN_REG
  236. ld r25, [sp, 12]
  237. #endif
  238. ld sp, [sp] /* restore original sp */
  239. /* orig_r0, ECR, user_r25 skipped automatically */
  240. .endm
  241. /* Get thread_info of "current" tsk */
  242. .macro GET_CURR_THR_INFO_FROM_SP reg
  243. bic \reg, sp, (THREAD_SIZE - 1)
  244. .endm
  245. /* Get CPU-ID of this core */
  246. .macro GET_CPU_ID reg
  247. lr \reg, [identity]
  248. lsr \reg, \reg, 8
  249. bmsk \reg, \reg, 7
  250. .endm
  251. #endif /* __ASM_ARC_ENTRY_COMPACT_H */