entry.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Vineetg: Aug 28th 2008: Bug #94984
  9. * -Zero Overhead Loop Context shd be cleared when entering IRQ/EXcp/Trap
  10. * Normally CPU does this automatically, however when doing FAKE rtie,
  11. * we also need to explicitly do this. The problem in macros
  12. * FAKE_RET_FROM_EXCPN and FAKE_RET_FROM_EXCPN_LOCK_IRQ was that this bit
  13. * was being "CLEARED" rather then "SET". Actually "SET" clears ZOL context
  14. *
  15. * Vineetg: May 5th 2008
  16. * - Defined Stack Switching Macro to be reused in all intr/excp hdlrs
  17. * - Shaved off 11 instructions from RESTORE_ALL_INT1 by using the
  18. * address Write back load ld.ab instead of seperate ld/add instn
  19. *
  20. * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
  21. */
  22. #ifndef __ASM_ARC_ENTRY_H
  23. #define __ASM_ARC_ENTRY_H
  24. #ifdef __ASSEMBLY__
  25. #include <asm/unistd.h> /* For NR_syscalls defination */
  26. #include <asm/asm-offsets.h>
  27. #include <asm/arcregs.h>
  28. #include <asm/ptrace.h>
  29. #include <asm/thread_info.h> /* For THREAD_SIZE */
  30. /* Note on the LD/ST addr modes with addr reg wback
  31. *
  32. * LD.a same as LD.aw
  33. *
  34. * LD.a reg1, [reg2, x] => Pre Incr
  35. * Eff Addr for load = [reg2 + x]
  36. *
  37. * LD.ab reg1, [reg2, x] => Post Incr
  38. * Eff Addr for load = [reg2]
  39. */
  40. /*--------------------------------------------------------------
  41. * Save caller saved registers (scratch registers) ( r0 - r12 )
  42. * Registers are pushed / popped in the order defined in struct ptregs
  43. * in asm/ptrace.h
  44. *-------------------------------------------------------------*/
  45. .macro SAVE_CALLER_SAVED
  46. st.a r0, [sp, -4]
  47. st.a r1, [sp, -4]
  48. st.a r2, [sp, -4]
  49. st.a r3, [sp, -4]
  50. st.a r4, [sp, -4]
  51. st.a r5, [sp, -4]
  52. st.a r6, [sp, -4]
  53. st.a r7, [sp, -4]
  54. st.a r8, [sp, -4]
  55. st.a r9, [sp, -4]
  56. st.a r10, [sp, -4]
  57. st.a r11, [sp, -4]
  58. st.a r12, [sp, -4]
  59. .endm
  60. /*--------------------------------------------------------------
  61. * Restore caller saved registers (scratch registers)
  62. *-------------------------------------------------------------*/
  63. .macro RESTORE_CALLER_SAVED
  64. ld.ab r12, [sp, 4]
  65. ld.ab r11, [sp, 4]
  66. ld.ab r10, [sp, 4]
  67. ld.ab r9, [sp, 4]
  68. ld.ab r8, [sp, 4]
  69. ld.ab r7, [sp, 4]
  70. ld.ab r6, [sp, 4]
  71. ld.ab r5, [sp, 4]
  72. ld.ab r4, [sp, 4]
  73. ld.ab r3, [sp, 4]
  74. ld.ab r2, [sp, 4]
  75. ld.ab r1, [sp, 4]
  76. ld.ab r0, [sp, 4]
  77. .endm
  78. /*--------------------------------------------------------------
  79. * Save callee saved registers (non scratch registers) ( r13 - r25 )
  80. * on kernel stack.
  81. * User mode callee regs need to be saved in case of
  82. * -fork and friends for replicating from parent to child
  83. * -before going into do_signal( ) for ptrace/core-dump
  84. * Special case handling is required for r25 in case it is used by kernel
  85. * for caching task ptr. Low level exception/ISR save user mode r25
  86. * into task->thread.user_r25. So it needs to be retrieved from there and
  87. * saved into kernel stack with rest of callee reg-file
  88. *-------------------------------------------------------------*/
  89. .macro SAVE_CALLEE_SAVED_USER
  90. st.a r13, [sp, -4]
  91. st.a r14, [sp, -4]
  92. st.a r15, [sp, -4]
  93. st.a r16, [sp, -4]
  94. st.a r17, [sp, -4]
  95. st.a r18, [sp, -4]
  96. st.a r19, [sp, -4]
  97. st.a r20, [sp, -4]
  98. st.a r21, [sp, -4]
  99. st.a r22, [sp, -4]
  100. st.a r23, [sp, -4]
  101. st.a r24, [sp, -4]
  102. st.a r25, [sp, -4]
  103. /* move up by 1 word to "create" callee_regs->"stack_place_holder" */
  104. sub sp, sp, 4
  105. .endm
  106. /*--------------------------------------------------------------
  107. * Save callee saved registers (non scratch registers) ( r13 - r25 )
  108. * kernel mode callee regs needed to be saved in case of context switch
  109. * If r25 is used for caching task pointer then that need not be saved
  110. * as it can be re-created from current task global
  111. *-------------------------------------------------------------*/
  112. .macro SAVE_CALLEE_SAVED_KERNEL
  113. st.a r13, [sp, -4]
  114. st.a r14, [sp, -4]
  115. st.a r15, [sp, -4]
  116. st.a r16, [sp, -4]
  117. st.a r17, [sp, -4]
  118. st.a r18, [sp, -4]
  119. st.a r19, [sp, -4]
  120. st.a r20, [sp, -4]
  121. st.a r21, [sp, -4]
  122. st.a r22, [sp, -4]
  123. st.a r23, [sp, -4]
  124. st.a r24, [sp, -4]
  125. st.a r25, [sp, -4]
  126. sub sp, sp, 4
  127. .endm
  128. /*--------------------------------------------------------------
  129. * RESTORE_CALLEE_SAVED_KERNEL:
  130. * Loads callee (non scratch) Reg File by popping from Kernel mode stack.
  131. * This is reverse of SAVE_CALLEE_SAVED,
  132. *
  133. * NOTE:
  134. * Ideally this shd only be called in switch_to for loading
  135. * switched-IN task's CALLEE Reg File.
  136. * For all other cases RESTORE_CALLEE_SAVED_FAST must be used
  137. * which simply pops the stack w/o touching regs.
  138. *-------------------------------------------------------------*/
  139. .macro RESTORE_CALLEE_SAVED_KERNEL
  140. add sp, sp, 4 /* skip "callee_regs->stack_place_holder" */
  141. ld.ab r25, [sp, 4]
  142. ld.ab r24, [sp, 4]
  143. ld.ab r23, [sp, 4]
  144. ld.ab r22, [sp, 4]
  145. ld.ab r21, [sp, 4]
  146. ld.ab r20, [sp, 4]
  147. ld.ab r19, [sp, 4]
  148. ld.ab r18, [sp, 4]
  149. ld.ab r17, [sp, 4]
  150. ld.ab r16, [sp, 4]
  151. ld.ab r15, [sp, 4]
  152. ld.ab r14, [sp, 4]
  153. ld.ab r13, [sp, 4]
  154. .endm
  155. /*--------------------------------------------------------------
  156. * Super FAST Restore callee saved regs by simply re-adjusting SP
  157. *-------------------------------------------------------------*/
  158. .macro DISCARD_CALLEE_SAVED_USER
  159. add sp, sp, 14 * 4
  160. .endm
  161. /*--------------------------------------------------------------
  162. * Restore User mode r25 saved in task_struct->thread.user_r25
  163. *-------------------------------------------------------------*/
  164. .macro RESTORE_USER_R25
  165. ld r25, [r25, TASK_THREAD + THREAD_USER_R25]
  166. .endm
  167. /*-------------------------------------------------------------
  168. * given a tsk struct, get to the base of it's kernel mode stack
  169. * tsk->thread_info is really a PAGE, whose bottom hoists stack
  170. * which grows upwards towards thread_info
  171. *------------------------------------------------------------*/
  172. .macro GET_TSK_STACK_BASE tsk, out
  173. /* Get task->thread_info (this is essentially start of a PAGE) */
  174. ld \out, [\tsk, TASK_THREAD_INFO]
  175. /* Go to end of page where stack begins (grows upwards) */
  176. add2 \out, \out, (THREAD_SIZE - 4)/4 /* one word GUTTER */
  177. .endm
  178. /*--------------------------------------------------------------
  179. * Switch to Kernel Mode stack if SP points to User Mode stack
  180. *
  181. * Entry : r9 contains pre-IRQ/exception/trap status32
  182. * Exit : SP is set to kernel mode stack pointer
  183. * Clobbers: r9
  184. *-------------------------------------------------------------*/
  185. .macro SWITCH_TO_KERNEL_STK
  186. /* User Mode when this happened ? Yes: Proceed to switch stack */
  187. bbit1 r9, STATUS_U_BIT, 88f
  188. /* OK we were already in kernel mode when this event happened, thus can
  189. * assume SP is kernel mode SP. _NO_ need to do any stack switching
  190. */
  191. /* Save Pre Intr/Exception KERNEL MODE SP on kernel stack
  192. * safe-keeping not really needed, but it keeps the epilogue code
  193. * (SP restore) simpler/uniform.
  194. */
  195. b.d 77f
  196. st.a sp, [sp, -12] ; Make room for orig_r0 and orig_r8
  197. 88: /*------Intr/Ecxp happened in user mode, "switch" stack ------ */
  198. GET_CURR_TASK_ON_CPU r9
  199. /* With current tsk in r9, get it's kernel mode stack base */
  200. GET_TSK_STACK_BASE r9, r9
  201. #ifdef PT_REGS_CANARY
  202. st 0xabcdabcd, [r9, 0]
  203. #endif
  204. /* Save Pre Intr/Exception User SP on kernel stack */
  205. st.a sp, [r9, -12] ; Make room for orig_r0 and orig_r8
  206. /* CAUTION:
  207. * SP should be set at the very end when we are done with everything
  208. * In case of 2 levels of interrupt we depend on value of SP to assume
  209. * that everything else is done (loading r25 etc)
  210. */
  211. /* set SP to point to kernel mode stack */
  212. mov sp, r9
  213. 77: /* ----- Stack Switched to kernel Mode, Now save REG FILE ----- */
  214. .endm
  215. /*------------------------------------------------------------
  216. * "FAKE" a rtie to return from CPU Exception context
  217. * This is to re-enable Exceptions within exception
  218. * Look at EV_ProtV to see how this is actually used
  219. *-------------------------------------------------------------*/
  220. .macro FAKE_RET_FROM_EXCPN reg
  221. ld \reg, [sp, PT_status32]
  222. bic \reg, \reg, (STATUS_U_MASK|STATUS_DE_MASK)
  223. bset \reg, \reg, STATUS_L_BIT
  224. sr \reg, [erstatus]
  225. mov \reg, 55f
  226. sr \reg, [eret]
  227. rtie
  228. 55:
  229. .endm
  230. /*
  231. * @reg [OUT] &thread_info of "current"
  232. */
  233. .macro GET_CURR_THR_INFO_FROM_SP reg
  234. and \reg, sp, ~(THREAD_SIZE - 1)
  235. .endm
  236. /*
  237. * @reg [OUT] thread_info->flags of "current"
  238. */
  239. .macro GET_CURR_THR_INFO_FLAGS reg
  240. GET_CURR_THR_INFO_FROM_SP \reg
  241. ld \reg, [\reg, THREAD_INFO_FLAGS]
  242. .endm
  243. /*--------------------------------------------------------------
  244. * For early Exception Prologue, a core reg is temporarily needed to
  245. * code the rest of prolog (stack switching). This is done by stashing
  246. * it to memory (non-SMP case) or SCRATCH0 Aux Reg (SMP).
  247. *
  248. * Before saving the full regfile - this reg is restored back, only
  249. * to be saved again on kernel mode stack, as part of ptregs.
  250. *-------------------------------------------------------------*/
  251. .macro EXCPN_PROLOG_FREEUP_REG reg
  252. st \reg, [@ex_saved_reg1]
  253. .endm
  254. .macro EXCPN_PROLOG_RESTORE_REG reg
  255. ld \reg, [@ex_saved_reg1]
  256. .endm
  257. /*--------------------------------------------------------------
  258. * Save all registers used by Exceptions (TLB Miss, Prot-V, Mem err etc)
  259. * Requires SP to be already switched to kernel mode Stack
  260. * sp points to the next free element on the stack at exit of this macro.
  261. * Registers are pushed / popped in the order defined in struct ptregs
  262. * in asm/ptrace.h
  263. * Note that syscalls are implemented via TRAP which is also a exception
  264. * from CPU's point of view
  265. *-------------------------------------------------------------*/
  266. .macro SAVE_ALL_EXCEPTION marker
  267. /* Restore r9 used to code the early prologue */
  268. EXCPN_PROLOG_RESTORE_REG r9
  269. /* Save the complete regfile now */
  270. /* orig_r8 marker:
  271. * syscalls -> 1 to NR_SYSCALLS
  272. * Exceptions -> NR_SYSCALLS + 1
  273. * Break-point-> NR_SYSCALLS + 2
  274. */
  275. st \marker, [sp, 8]
  276. st r0, [sp, 4] /* orig_r0, needed only for sys calls */
  277. SAVE_CALLER_SAVED
  278. st.a r26, [sp, -4] /* gp */
  279. st.a fp, [sp, -4]
  280. st.a blink, [sp, -4]
  281. lr r9, [eret]
  282. st.a r9, [sp, -4]
  283. lr r9, [erstatus]
  284. st.a r9, [sp, -4]
  285. st.a lp_count, [sp, -4]
  286. lr r9, [lp_end]
  287. st.a r9, [sp, -4]
  288. lr r9, [lp_start]
  289. st.a r9, [sp, -4]
  290. lr r9, [erbta]
  291. st.a r9, [sp, -4]
  292. #ifdef PT_REGS_CANARY
  293. mov r9, 0xdeadbeef
  294. st r9, [sp, -4]
  295. #endif
  296. /* move up by 1 word to "create" pt_regs->"stack_place_holder" */
  297. sub sp, sp, 4
  298. .endm
  299. /*--------------------------------------------------------------
  300. * Save scratch regs for exceptions
  301. *-------------------------------------------------------------*/
  302. .macro SAVE_ALL_SYS
  303. SAVE_ALL_EXCEPTION (NR_syscalls + 1)
  304. .endm
  305. /*--------------------------------------------------------------
  306. * Save scratch regs for sys calls
  307. *-------------------------------------------------------------*/
  308. .macro SAVE_ALL_TRAP
  309. SAVE_ALL_EXCEPTION r8
  310. .endm
  311. /*--------------------------------------------------------------
  312. * Restore all registers used by system call or Exceptions
  313. * SP should always be pointing to the next free stack element
  314. * when entering this macro.
  315. *
  316. * NOTE:
  317. *
  318. * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
  319. * for memory load operations. If used in that way interrupts are deffered
  320. * by hardware and that is not good.
  321. *-------------------------------------------------------------*/
  322. .macro RESTORE_ALL_SYS
  323. add sp, sp, 4 /* hop over unused "pt_regs->stack_place_holder" */
  324. ld.ab r9, [sp, 4]
  325. sr r9, [erbta]
  326. ld.ab r9, [sp, 4]
  327. sr r9, [lp_start]
  328. ld.ab r9, [sp, 4]
  329. sr r9, [lp_end]
  330. ld.ab r9, [sp, 4]
  331. mov lp_count, r9
  332. ld.ab r9, [sp, 4]
  333. sr r9, [erstatus]
  334. ld.ab r9, [sp, 4]
  335. sr r9, [eret]
  336. ld.ab blink, [sp, 4]
  337. ld.ab fp, [sp, 4]
  338. ld.ab r26, [sp, 4] /* gp */
  339. RESTORE_CALLER_SAVED
  340. ld sp, [sp] /* restore original sp */
  341. /* orig_r0 and orig_r8 skipped automatically */
  342. .endm
  343. /*--------------------------------------------------------------
  344. * Save all registers used by interrupt handlers.
  345. *-------------------------------------------------------------*/
  346. .macro SAVE_ALL_INT1
  347. /* restore original r9 , saved in int1_saved_reg
  348. * It will be saved on stack in macro: SAVE_CALLER_SAVED
  349. */
  350. ld r9, [@int1_saved_reg]
  351. /* now we are ready to save the remaining context :) */
  352. st -1, [sp, 8] /* orig_r8, -1 for interuppt level one */
  353. st 0, [sp, 4] /* orig_r0 , N/A for IRQ */
  354. SAVE_CALLER_SAVED
  355. st.a r26, [sp, -4] /* gp */
  356. st.a fp, [sp, -4]
  357. st.a blink, [sp, -4]
  358. st.a ilink1, [sp, -4]
  359. lr r9, [status32_l1]
  360. st.a r9, [sp, -4]
  361. st.a lp_count, [sp, -4]
  362. lr r9, [lp_end]
  363. st.a r9, [sp, -4]
  364. lr r9, [lp_start]
  365. st.a r9, [sp, -4]
  366. lr r9, [bta_l1]
  367. st.a r9, [sp, -4]
  368. #ifdef PT_REGS_CANARY
  369. mov r9, 0xdeadbee1
  370. st r9, [sp, -4]
  371. #endif
  372. /* move up by 1 word to "create" pt_regs->"stack_place_holder" */
  373. sub sp, sp, 4
  374. .endm
  375. /*--------------------------------------------------------------
  376. * Restore all registers used by interrupt handlers.
  377. *
  378. * NOTE:
  379. *
  380. * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
  381. * for memory load operations. If used in that way interrupts are deffered
  382. * by hardware and that is not good.
  383. *-------------------------------------------------------------*/
  384. .macro RESTORE_ALL_INT1
  385. add sp, sp, 4 /* hop over unused "pt_regs->stack_place_holder" */
  386. ld.ab r9, [sp, 4] /* Actual reg file */
  387. sr r9, [bta_l1]
  388. ld.ab r9, [sp, 4]
  389. sr r9, [lp_start]
  390. ld.ab r9, [sp, 4]
  391. sr r9, [lp_end]
  392. ld.ab r9, [sp, 4]
  393. mov lp_count, r9
  394. ld.ab r9, [sp, 4]
  395. sr r9, [status32_l1]
  396. ld.ab r9, [sp, 4]
  397. mov ilink1, r9
  398. ld.ab blink, [sp, 4]
  399. ld.ab fp, [sp, 4]
  400. ld.ab r26, [sp, 4] /* gp */
  401. RESTORE_CALLER_SAVED
  402. ld sp, [sp] /* restore original sp */
  403. /* orig_r0 and orig_r8 skipped automatically */
  404. .endm
  405. /* Get CPU-ID of this core */
  406. .macro GET_CPU_ID reg
  407. lr \reg, [identity]
  408. lsr \reg, \reg, 8
  409. bmsk \reg, \reg, 7
  410. .endm
  411. .macro GET_CURR_TASK_ON_CPU reg
  412. ld \reg, [@_current_task]
  413. .endm
  414. .macro SET_CURR_TASK_ON_CPU tsk, tmp
  415. st \tsk, [@_current_task]
  416. .endm
  417. /* ------------------------------------------------------------------
  418. * Get the ptr to some field of Current Task at @off in task struct
  419. */
  420. .macro GET_CURR_TASK_FIELD_PTR off, reg
  421. GET_CURR_TASK_ON_CPU \reg
  422. add \reg, \reg, \off
  423. .endm
  424. #endif /* __ASSEMBLY__ */
  425. #endif /* __ASM_ARC_ENTRY_H */