entry-compact.h 8.8 KB

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