entry-compact.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #ifdef CONFIG_ARC_PLAT_EZNPS
  37. #include <plat/ctop.h>
  38. #endif
  39. /*--------------------------------------------------------------
  40. * Switch to Kernel Mode stack if SP points to User Mode stack
  41. *
  42. * Entry : r9 contains pre-IRQ/exception/trap status32
  43. * Exit : SP set to K mode stack
  44. * SP at the time of entry (K/U) saved @ pt_regs->sp
  45. * Clobbers: r9
  46. *-------------------------------------------------------------*/
  47. .macro SWITCH_TO_KERNEL_STK
  48. /* User Mode when this happened ? Yes: Proceed to switch stack */
  49. bbit1 r9, STATUS_U_BIT, 88f
  50. /* OK we were already in kernel mode when this event happened, thus can
  51. * assume SP is kernel mode SP. _NO_ need to do any stack switching
  52. */
  53. #ifdef CONFIG_ARC_COMPACT_IRQ_LEVELS
  54. /* However....
  55. * If Level 2 Interrupts enabled, we may end up with a corner case:
  56. * 1. User Task executing
  57. * 2. L1 IRQ taken, ISR starts (CPU auto-switched to KERNEL mode)
  58. * 3. But before it could switch SP from USER to KERNEL stack
  59. * a L2 IRQ "Interrupts" L1
  60. * Thay way although L2 IRQ happened in Kernel mode, stack is still
  61. * not switched.
  62. * To handle this, we may need to switch stack even if in kernel mode
  63. * provided SP has values in range of USER mode stack ( < 0x7000_0000 )
  64. */
  65. brlo sp, VMALLOC_START, 88f
  66. /* TODO: vineetg:
  67. * We need to be a bit more cautious here. What if a kernel bug in
  68. * L1 ISR, caused SP to go whaco (some small value which looks like
  69. * USER stk) and then we take L2 ISR.
  70. * Above brlo alone would treat it as a valid L1-L2 scenario
  71. * instead of shouting around
  72. * The only feasible way is to make sure this L2 happened in
  73. * L1 prelogue ONLY i.e. ilink2 is less than a pre-set marker in
  74. * L1 ISR before it switches stack
  75. */
  76. #endif
  77. /*------Intr/Ecxp happened in kernel mode, SP already setup ------ */
  78. /* save it nevertheless @ pt_regs->sp for uniformity */
  79. b.d 66f
  80. st sp, [sp, PT_sp - SZ_PT_REGS]
  81. 88: /*------Intr/Ecxp happened in user mode, "switch" stack ------ */
  82. GET_CURR_TASK_ON_CPU r9
  83. /* With current tsk in r9, get it's kernel mode stack base */
  84. GET_TSK_STACK_BASE r9, r9
  85. /* save U mode SP @ pt_regs->sp */
  86. st sp, [r9, PT_sp - SZ_PT_REGS]
  87. /* final SP switch */
  88. mov sp, r9
  89. 66:
  90. .endm
  91. /*------------------------------------------------------------
  92. * "FAKE" a rtie to return from CPU Exception context
  93. * This is to re-enable Exceptions within exception
  94. * Look at EV_ProtV to see how this is actually used
  95. *-------------------------------------------------------------*/
  96. .macro FAKE_RET_FROM_EXCPN
  97. lr r9, [status32]
  98. bclr r9, r9, STATUS_AE_BIT
  99. or r9, r9, (STATUS_E1_MASK|STATUS_E2_MASK)
  100. sr r9, [erstatus]
  101. mov r9, 55f
  102. sr r9, [eret]
  103. rtie
  104. 55:
  105. .endm
  106. /*--------------------------------------------------------------
  107. * For early Exception/ISR Prologue, a core reg is temporarily needed to
  108. * code the rest of prolog (stack switching). This is done by stashing
  109. * it to memory (non-SMP case) or SCRATCH0 Aux Reg (SMP).
  110. *
  111. * Before saving the full regfile - this reg is restored back, only
  112. * to be saved again on kernel mode stack, as part of pt_regs.
  113. *-------------------------------------------------------------*/
  114. .macro PROLOG_FREEUP_REG reg, mem
  115. #ifdef CONFIG_SMP
  116. sr \reg, [ARC_REG_SCRATCH_DATA0]
  117. #else
  118. st \reg, [\mem]
  119. #endif
  120. .endm
  121. .macro PROLOG_RESTORE_REG reg, mem
  122. #ifdef CONFIG_SMP
  123. lr \reg, [ARC_REG_SCRATCH_DATA0]
  124. #else
  125. ld \reg, [\mem]
  126. #endif
  127. .endm
  128. /*--------------------------------------------------------------
  129. * Exception Entry prologue
  130. * -Switches stack to K mode (if not already)
  131. * -Saves the register file
  132. *
  133. * After this it is safe to call the "C" handlers
  134. *-------------------------------------------------------------*/
  135. .macro EXCEPTION_PROLOGUE
  136. /* Need at least 1 reg to code the early exception prologue */
  137. PROLOG_FREEUP_REG r9, @ex_saved_reg1
  138. /* U/K mode at time of exception (stack not switched if already K) */
  139. lr r9, [erstatus]
  140. /* ARC700 doesn't provide auto-stack switching */
  141. SWITCH_TO_KERNEL_STK
  142. #ifdef CONFIG_ARC_CURR_IN_REG
  143. /* Treat r25 as scratch reg (save on stack) and load with "current" */
  144. PUSH r25
  145. GET_CURR_TASK_ON_CPU r25
  146. #else
  147. sub sp, sp, 4
  148. #endif
  149. st.a r0, [sp, -8] /* orig_r0 needed for syscall (skip ECR slot) */
  150. sub sp, sp, 4 /* skip pt_regs->sp, already saved above */
  151. /* Restore r9 used to code the early prologue */
  152. PROLOG_RESTORE_REG r9, @ex_saved_reg1
  153. /* now we are ready to save the regfile */
  154. SAVE_R0_TO_R12
  155. PUSH gp
  156. PUSH fp
  157. PUSH blink
  158. PUSHAX eret
  159. PUSHAX erstatus
  160. PUSH lp_count
  161. PUSHAX lp_end
  162. PUSHAX lp_start
  163. PUSHAX erbta
  164. #ifdef CONFIG_ARC_PLAT_EZNPS
  165. .word CTOP_INST_SCHD_RW
  166. PUSHAX CTOP_AUX_GPA1
  167. PUSHAX CTOP_AUX_EFLAGS
  168. #endif
  169. lr r9, [ecr]
  170. st r9, [sp, PT_event] /* EV_Trap expects r9 to have ECR */
  171. .endm
  172. /*--------------------------------------------------------------
  173. * Restore all registers used by system call or Exceptions
  174. * SP should always be pointing to the next free stack element
  175. * when entering this macro.
  176. *
  177. * NOTE:
  178. *
  179. * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
  180. * for memory load operations. If used in that way interrupts are deffered
  181. * by hardware and that is not good.
  182. *-------------------------------------------------------------*/
  183. .macro EXCEPTION_EPILOGUE
  184. #ifdef CONFIG_ARC_PLAT_EZNPS
  185. .word CTOP_INST_SCHD_RW
  186. POPAX CTOP_AUX_EFLAGS
  187. POPAX CTOP_AUX_GPA1
  188. #endif
  189. POPAX erbta
  190. POPAX lp_start
  191. POPAX lp_end
  192. POP r9
  193. mov lp_count, r9 ;LD to lp_count is not allowed
  194. POPAX erstatus
  195. POPAX eret
  196. POP blink
  197. POP fp
  198. POP gp
  199. RESTORE_R12_TO_R0
  200. #ifdef CONFIG_ARC_CURR_IN_REG
  201. ld r25, [sp, 12]
  202. #endif
  203. ld sp, [sp] /* restore original sp */
  204. /* orig_r0, ECR, user_r25 skipped automatically */
  205. .endm
  206. /* Dummy ECR values for Interrupts */
  207. #define event_IRQ1 0x0031abcd
  208. #define event_IRQ2 0x0032abcd
  209. .macro INTERRUPT_PROLOGUE LVL
  210. /* free up r9 as scratchpad */
  211. PROLOG_FREEUP_REG r9, @int\LVL\()_saved_reg
  212. /* Which mode (user/kernel) was the system in when intr occurred */
  213. lr r9, [status32_l\LVL\()]
  214. SWITCH_TO_KERNEL_STK
  215. #ifdef CONFIG_ARC_CURR_IN_REG
  216. /* Treat r25 as scratch reg (save on stack) and load with "current" */
  217. PUSH r25
  218. GET_CURR_TASK_ON_CPU r25
  219. #else
  220. sub sp, sp, 4
  221. #endif
  222. PUSH 0x003\LVL\()abcd /* Dummy ECR */
  223. sub sp, sp, 8 /* skip orig_r0 (not needed)
  224. skip pt_regs->sp, already saved above */
  225. /* Restore r9 used to code the early prologue */
  226. PROLOG_RESTORE_REG r9, @int\LVL\()_saved_reg
  227. SAVE_R0_TO_R12
  228. PUSH gp
  229. PUSH fp
  230. PUSH blink
  231. PUSH ilink\LVL\()
  232. PUSHAX status32_l\LVL\()
  233. PUSH lp_count
  234. PUSHAX lp_end
  235. PUSHAX lp_start
  236. PUSHAX bta_l\LVL\()
  237. #ifdef CONFIG_ARC_PLAT_EZNPS
  238. .word CTOP_INST_SCHD_RW
  239. PUSHAX CTOP_AUX_GPA1
  240. PUSHAX CTOP_AUX_EFLAGS
  241. #endif
  242. .endm
  243. /*--------------------------------------------------------------
  244. * Restore all registers used by interrupt handlers.
  245. *
  246. * NOTE:
  247. *
  248. * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
  249. * for memory load operations. If used in that way interrupts are deffered
  250. * by hardware and that is not good.
  251. *-------------------------------------------------------------*/
  252. .macro INTERRUPT_EPILOGUE LVL
  253. #ifdef CONFIG_ARC_PLAT_EZNPS
  254. .word CTOP_INST_SCHD_RW
  255. POPAX CTOP_AUX_EFLAGS
  256. POPAX CTOP_AUX_GPA1
  257. #endif
  258. POPAX bta_l\LVL\()
  259. POPAX lp_start
  260. POPAX lp_end
  261. POP r9
  262. mov lp_count, r9 ;LD to lp_count is not allowed
  263. POPAX status32_l\LVL\()
  264. POP ilink\LVL\()
  265. POP blink
  266. POP fp
  267. POP gp
  268. RESTORE_R12_TO_R0
  269. #ifdef CONFIG_ARC_CURR_IN_REG
  270. ld r25, [sp, 12]
  271. #endif
  272. ld sp, [sp] /* restore original sp */
  273. /* orig_r0, ECR, user_r25 skipped automatically */
  274. .endm
  275. /* Get thread_info of "current" tsk */
  276. .macro GET_CURR_THR_INFO_FROM_SP reg
  277. bic \reg, sp, (THREAD_SIZE - 1)
  278. .endm
  279. #ifndef CONFIG_ARC_PLAT_EZNPS
  280. /* Get CPU-ID of this core */
  281. .macro GET_CPU_ID reg
  282. lr \reg, [identity]
  283. lsr \reg, \reg, 8
  284. bmsk \reg, \reg, 7
  285. .endm
  286. #endif
  287. #endif /* __ASM_ARC_ENTRY_COMPACT_H */