entry.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. * RESTORE_CALLEE_SAVED_USER:
  157. * This is called after do_signal where tracer might have changed callee regs
  158. * thus we need to restore the reg file.
  159. * Special case handling is required for r25 in case it is used by kernel
  160. * for caching task ptr. Ptrace would have modified on-kernel-stack value of
  161. * r25, which needs to be shoved back into task->thread.user_r25 where from
  162. * Low level exception/ISR return code will retrieve to populate with rest of
  163. * callee reg-file.
  164. *-------------------------------------------------------------*/
  165. .macro RESTORE_CALLEE_SAVED_USER
  166. add sp, sp, 4 /* skip "callee_regs->stack_place_holder" */
  167. #ifdef CONFIG_ARC_CURR_IN_REG
  168. ld.ab r12, [sp, 4]
  169. st r12, [r25, TASK_THREAD + THREAD_USER_R25]
  170. #else
  171. ld.ab r25, [sp, 4]
  172. #endif
  173. ld.ab r24, [sp, 4]
  174. ld.ab r23, [sp, 4]
  175. ld.ab r22, [sp, 4]
  176. ld.ab r21, [sp, 4]
  177. ld.ab r20, [sp, 4]
  178. ld.ab r19, [sp, 4]
  179. ld.ab r18, [sp, 4]
  180. ld.ab r17, [sp, 4]
  181. ld.ab r16, [sp, 4]
  182. ld.ab r15, [sp, 4]
  183. ld.ab r14, [sp, 4]
  184. ld.ab r13, [sp, 4]
  185. .endm
  186. /*--------------------------------------------------------------
  187. * Super FAST Restore callee saved regs by simply re-adjusting SP
  188. *-------------------------------------------------------------*/
  189. .macro DISCARD_CALLEE_SAVED_USER
  190. add sp, sp, 14 * 4
  191. .endm
  192. /*--------------------------------------------------------------
  193. * Restore User mode r25 saved in task_struct->thread.user_r25
  194. *-------------------------------------------------------------*/
  195. .macro RESTORE_USER_R25
  196. ld r25, [r25, TASK_THREAD + THREAD_USER_R25]
  197. .endm
  198. /*-------------------------------------------------------------
  199. * given a tsk struct, get to the base of it's kernel mode stack
  200. * tsk->thread_info is really a PAGE, whose bottom hoists stack
  201. * which grows upwards towards thread_info
  202. *------------------------------------------------------------*/
  203. .macro GET_TSK_STACK_BASE tsk, out
  204. /* Get task->thread_info (this is essentially start of a PAGE) */
  205. ld \out, [\tsk, TASK_THREAD_INFO]
  206. /* Go to end of page where stack begins (grows upwards) */
  207. add2 \out, \out, (THREAD_SIZE - 4)/4 /* one word GUTTER */
  208. .endm
  209. /*--------------------------------------------------------------
  210. * Switch to Kernel Mode stack if SP points to User Mode stack
  211. *
  212. * Entry : r9 contains pre-IRQ/exception/trap status32
  213. * Exit : SP is set to kernel mode stack pointer
  214. * Clobbers: r9
  215. *-------------------------------------------------------------*/
  216. .macro SWITCH_TO_KERNEL_STK
  217. /* User Mode when this happened ? Yes: Proceed to switch stack */
  218. bbit1 r9, STATUS_U_BIT, 88f
  219. /* OK we were already in kernel mode when this event happened, thus can
  220. * assume SP is kernel mode SP. _NO_ need to do any stack switching
  221. */
  222. /* Save Pre Intr/Exception KERNEL MODE SP on kernel stack
  223. * safe-keeping not really needed, but it keeps the epilogue code
  224. * (SP restore) simpler/uniform.
  225. */
  226. b.d 77f
  227. st.a sp, [sp, -12] ; Make room for orig_r0 and orig_r8
  228. 88: /*------Intr/Ecxp happened in user mode, "switch" stack ------ */
  229. GET_CURR_TASK_ON_CPU r9
  230. /* With current tsk in r9, get it's kernel mode stack base */
  231. GET_TSK_STACK_BASE r9, r9
  232. #ifdef PT_REGS_CANARY
  233. st 0xabcdabcd, [r9, 0]
  234. #endif
  235. /* Save Pre Intr/Exception User SP on kernel stack */
  236. st.a sp, [r9, -12] ; Make room for orig_r0 and orig_r8
  237. /* CAUTION:
  238. * SP should be set at the very end when we are done with everything
  239. * In case of 2 levels of interrupt we depend on value of SP to assume
  240. * that everything else is done (loading r25 etc)
  241. */
  242. /* set SP to point to kernel mode stack */
  243. mov sp, r9
  244. 77: /* ----- Stack Switched to kernel Mode, Now save REG FILE ----- */
  245. .endm
  246. /*------------------------------------------------------------
  247. * "FAKE" a rtie to return from CPU Exception context
  248. * This is to re-enable Exceptions within exception
  249. * Look at EV_ProtV to see how this is actually used
  250. *-------------------------------------------------------------*/
  251. .macro FAKE_RET_FROM_EXCPN reg
  252. ld \reg, [sp, PT_status32]
  253. bic \reg, \reg, (STATUS_U_MASK|STATUS_DE_MASK)
  254. bset \reg, \reg, STATUS_L_BIT
  255. sr \reg, [erstatus]
  256. mov \reg, 55f
  257. sr \reg, [eret]
  258. rtie
  259. 55:
  260. .endm
  261. /*
  262. * @reg [OUT] &thread_info of "current"
  263. */
  264. .macro GET_CURR_THR_INFO_FROM_SP reg
  265. and \reg, sp, ~(THREAD_SIZE - 1)
  266. .endm
  267. /*
  268. * @reg [OUT] thread_info->flags of "current"
  269. */
  270. .macro GET_CURR_THR_INFO_FLAGS reg
  271. GET_CURR_THR_INFO_FROM_SP \reg
  272. ld \reg, [\reg, THREAD_INFO_FLAGS]
  273. .endm
  274. /*--------------------------------------------------------------
  275. * For early Exception Prologue, a core reg is temporarily needed to
  276. * code the rest of prolog (stack switching). This is done by stashing
  277. * it to memory (non-SMP case) or SCRATCH0 Aux Reg (SMP).
  278. *
  279. * Before saving the full regfile - this reg is restored back, only
  280. * to be saved again on kernel mode stack, as part of ptregs.
  281. *-------------------------------------------------------------*/
  282. .macro EXCPN_PROLOG_FREEUP_REG reg
  283. st \reg, [@ex_saved_reg1]
  284. .endm
  285. .macro EXCPN_PROLOG_RESTORE_REG reg
  286. ld \reg, [@ex_saved_reg1]
  287. .endm
  288. /*--------------------------------------------------------------
  289. * Save all registers used by Exceptions (TLB Miss, Prot-V, Mem err etc)
  290. * Requires SP to be already switched to kernel mode Stack
  291. * sp points to the next free element on the stack at exit of this macro.
  292. * Registers are pushed / popped in the order defined in struct ptregs
  293. * in asm/ptrace.h
  294. * Note that syscalls are implemented via TRAP which is also a exception
  295. * from CPU's point of view
  296. *-------------------------------------------------------------*/
  297. .macro SAVE_ALL_EXCEPTION marker
  298. st \marker, [sp, 8]
  299. st r0, [sp, 4] /* orig_r0, needed only for sys calls */
  300. /* Restore r9 used to code the early prologue */
  301. EXCPN_PROLOG_RESTORE_REG r9
  302. SAVE_CALLER_SAVED
  303. st.a r26, [sp, -4] /* gp */
  304. st.a fp, [sp, -4]
  305. st.a blink, [sp, -4]
  306. lr r9, [eret]
  307. st.a r9, [sp, -4]
  308. lr r9, [erstatus]
  309. st.a r9, [sp, -4]
  310. st.a lp_count, [sp, -4]
  311. lr r9, [lp_end]
  312. st.a r9, [sp, -4]
  313. lr r9, [lp_start]
  314. st.a r9, [sp, -4]
  315. lr r9, [erbta]
  316. st.a r9, [sp, -4]
  317. #ifdef PT_REGS_CANARY
  318. mov r9, 0xdeadbeef
  319. st r9, [sp, -4]
  320. #endif
  321. /* move up by 1 word to "create" pt_regs->"stack_place_holder" */
  322. sub sp, sp, 4
  323. .endm
  324. /*--------------------------------------------------------------
  325. * Save scratch regs for exceptions
  326. *-------------------------------------------------------------*/
  327. .macro SAVE_ALL_SYS
  328. SAVE_ALL_EXCEPTION orig_r8_IS_EXCPN
  329. .endm
  330. /*--------------------------------------------------------------
  331. * Save scratch regs for sys calls
  332. *-------------------------------------------------------------*/
  333. .macro SAVE_ALL_TRAP
  334. /*
  335. * Setup pt_regs->orig_r8.
  336. * Encode syscall number (r8) in upper short word of event type (r9)
  337. * N.B. #1: This is already endian safe (see ptrace.h)
  338. * #2: Only r9 can be used as scratch as it is already clobbered
  339. * and it's contents are no longer needed by the latter part
  340. * of exception prologue
  341. */
  342. lsl r9, r8, 16
  343. or r9, r9, orig_r8_IS_SCALL
  344. SAVE_ALL_EXCEPTION r9
  345. .endm
  346. /*--------------------------------------------------------------
  347. * Restore all registers used by system call or Exceptions
  348. * SP should always be pointing to the next free stack element
  349. * when entering this macro.
  350. *
  351. * NOTE:
  352. *
  353. * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
  354. * for memory load operations. If used in that way interrupts are deffered
  355. * by hardware and that is not good.
  356. *-------------------------------------------------------------*/
  357. .macro RESTORE_ALL_SYS
  358. add sp, sp, 4 /* hop over unused "pt_regs->stack_place_holder" */
  359. ld.ab r9, [sp, 4]
  360. sr r9, [erbta]
  361. ld.ab r9, [sp, 4]
  362. sr r9, [lp_start]
  363. ld.ab r9, [sp, 4]
  364. sr r9, [lp_end]
  365. ld.ab r9, [sp, 4]
  366. mov lp_count, r9
  367. ld.ab r9, [sp, 4]
  368. sr r9, [erstatus]
  369. ld.ab r9, [sp, 4]
  370. sr r9, [eret]
  371. ld.ab blink, [sp, 4]
  372. ld.ab fp, [sp, 4]
  373. ld.ab r26, [sp, 4] /* gp */
  374. RESTORE_CALLER_SAVED
  375. ld sp, [sp] /* restore original sp */
  376. /* orig_r0 and orig_r8 skipped automatically */
  377. .endm
  378. /*--------------------------------------------------------------
  379. * Save all registers used by interrupt handlers.
  380. *-------------------------------------------------------------*/
  381. .macro SAVE_ALL_INT1
  382. /* restore original r9 , saved in int1_saved_reg
  383. * It will be saved on stack in macro: SAVE_CALLER_SAVED
  384. */
  385. ld r9, [@int1_saved_reg]
  386. /* now we are ready to save the remaining context :) */
  387. st orig_r8_IS_IRQ1, [sp, 8] /* Event Type */
  388. st 0, [sp, 4] /* orig_r0 , N/A for IRQ */
  389. SAVE_CALLER_SAVED
  390. st.a r26, [sp, -4] /* gp */
  391. st.a fp, [sp, -4]
  392. st.a blink, [sp, -4]
  393. st.a ilink1, [sp, -4]
  394. lr r9, [status32_l1]
  395. st.a r9, [sp, -4]
  396. st.a lp_count, [sp, -4]
  397. lr r9, [lp_end]
  398. st.a r9, [sp, -4]
  399. lr r9, [lp_start]
  400. st.a r9, [sp, -4]
  401. lr r9, [bta_l1]
  402. st.a r9, [sp, -4]
  403. #ifdef PT_REGS_CANARY
  404. mov r9, 0xdeadbee1
  405. st r9, [sp, -4]
  406. #endif
  407. /* move up by 1 word to "create" pt_regs->"stack_place_holder" */
  408. sub sp, sp, 4
  409. .endm
  410. /*--------------------------------------------------------------
  411. * Restore all registers used by interrupt handlers.
  412. *
  413. * NOTE:
  414. *
  415. * It is recommended that lp_count/ilink1/ilink2 not be used as a dest reg
  416. * for memory load operations. If used in that way interrupts are deffered
  417. * by hardware and that is not good.
  418. *-------------------------------------------------------------*/
  419. .macro RESTORE_ALL_INT1
  420. add sp, sp, 4 /* hop over unused "pt_regs->stack_place_holder" */
  421. ld.ab r9, [sp, 4] /* Actual reg file */
  422. sr r9, [bta_l1]
  423. ld.ab r9, [sp, 4]
  424. sr r9, [lp_start]
  425. ld.ab r9, [sp, 4]
  426. sr r9, [lp_end]
  427. ld.ab r9, [sp, 4]
  428. mov lp_count, r9
  429. ld.ab r9, [sp, 4]
  430. sr r9, [status32_l1]
  431. ld.ab r9, [sp, 4]
  432. mov ilink1, r9
  433. ld.ab blink, [sp, 4]
  434. ld.ab fp, [sp, 4]
  435. ld.ab r26, [sp, 4] /* gp */
  436. RESTORE_CALLER_SAVED
  437. ld sp, [sp] /* restore original sp */
  438. /* orig_r0 and orig_r8 skipped automatically */
  439. .endm
  440. /* Get CPU-ID of this core */
  441. .macro GET_CPU_ID reg
  442. lr \reg, [identity]
  443. lsr \reg, \reg, 8
  444. bmsk \reg, \reg, 7
  445. .endm
  446. .macro GET_CURR_TASK_ON_CPU reg
  447. ld \reg, [@_current_task]
  448. .endm
  449. .macro SET_CURR_TASK_ON_CPU tsk, tmp
  450. st \tsk, [@_current_task]
  451. .endm
  452. /* ------------------------------------------------------------------
  453. * Get the ptr to some field of Current Task at @off in task struct
  454. */
  455. .macro GET_CURR_TASK_FIELD_PTR off, reg
  456. GET_CURR_TASK_ON_CPU \reg
  457. add \reg, \reg, \off
  458. .endm
  459. #endif /* __ASSEMBLY__ */
  460. #endif /* __ASM_ARC_ENTRY_H */