head_booke.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #ifndef __HEAD_BOOKE_H__
  2. #define __HEAD_BOOKE_H__
  3. #include <asm/ptrace.h> /* for STACK_FRAME_REGS_MARKER */
  4. /*
  5. * Macros used for common Book-e exception handling
  6. */
  7. #define SET_IVOR(vector_number, vector_label) \
  8. li r26,vector_label@l; \
  9. mtspr SPRN_IVOR##vector_number,r26; \
  10. sync
  11. #if (THREAD_SHIFT < 15)
  12. #define ALLOC_STACK_FRAME(reg, val) \
  13. addi reg,reg,val
  14. #else
  15. #define ALLOC_STACK_FRAME(reg, val) \
  16. addis reg,reg,val@ha; \
  17. addi reg,reg,val@l
  18. #endif
  19. /*
  20. * Macro used to get to thread save registers.
  21. * Note that entries 0-3 are used for the prolog code, and the remaining
  22. * entries are available for specific exception use in the event a handler
  23. * requires more than 4 scratch registers.
  24. */
  25. #define THREAD_NORMSAVE(offset) (THREAD_NORMSAVES + (offset * 4))
  26. #define NORMAL_EXCEPTION_PROLOG \
  27. mtspr SPRN_SPRG_WSCRATCH0, r10; /* save one register */ \
  28. mfspr r10, SPRN_SPRG_THREAD; \
  29. stw r11, THREAD_NORMSAVE(0)(r10); \
  30. stw r13, THREAD_NORMSAVE(2)(r10); \
  31. mfcr r13; /* save CR in r13 for now */\
  32. mfspr r11,SPRN_SRR1; /* check whether user or kernel */\
  33. andi. r11,r11,MSR_PR; \
  34. mr r11, r1; \
  35. beq 1f; \
  36. /* if from user, start at top of this thread's kernel stack */ \
  37. lwz r11, THREAD_INFO-THREAD(r10); \
  38. ALLOC_STACK_FRAME(r11, THREAD_SIZE); \
  39. 1 : subi r11, r11, INT_FRAME_SIZE; /* Allocate exception frame */ \
  40. stw r13, _CCR(r11); /* save various registers */ \
  41. stw r12,GPR12(r11); \
  42. stw r9,GPR9(r11); \
  43. mfspr r13, SPRN_SPRG_RSCRATCH0; \
  44. stw r13, GPR10(r11); \
  45. lwz r12, THREAD_NORMSAVE(0)(r10); \
  46. stw r12,GPR11(r11); \
  47. lwz r13, THREAD_NORMSAVE(2)(r10); /* restore r13 */ \
  48. mflr r10; \
  49. stw r10,_LINK(r11); \
  50. mfspr r12,SPRN_SRR0; \
  51. stw r1, GPR1(r11); \
  52. mfspr r9,SPRN_SRR1; \
  53. stw r1, 0(r11); \
  54. mr r1, r11; \
  55. rlwinm r9,r9,0,14,12; /* clear MSR_WE (necessary?) */\
  56. stw r0,GPR0(r11); \
  57. lis r10, STACK_FRAME_REGS_MARKER@ha;/* exception frame marker */ \
  58. addi r10, r10, STACK_FRAME_REGS_MARKER@l; \
  59. stw r10, 8(r11); \
  60. SAVE_4GPRS(3, r11); \
  61. SAVE_2GPRS(7, r11)
  62. /* To handle the additional exception priority levels on 40x and Book-E
  63. * processors we allocate a stack per additional priority level.
  64. *
  65. * On 40x critical is the only additional level
  66. * On 44x/e500 we have critical and machine check
  67. * On e200 we have critical and debug (machine check occurs via critical)
  68. *
  69. * Additionally we reserve a SPRG for each priority level so we can free up a
  70. * GPR to use as the base for indirect access to the exception stacks. This
  71. * is necessary since the MMU is always on, for Book-E parts, and the stacks
  72. * are offset from KERNELBASE.
  73. *
  74. * There is some space optimization to be had here if desired. However
  75. * to allow for a common kernel with support for debug exceptions either
  76. * going to critical or their own debug level we aren't currently
  77. * providing configurations that micro-optimize space usage.
  78. */
  79. #define MC_STACK_BASE mcheckirq_ctx
  80. #define CRIT_STACK_BASE critirq_ctx
  81. /* only on e500mc/e200 */
  82. #define DBG_STACK_BASE dbgirq_ctx
  83. #define EXC_LVL_FRAME_OVERHEAD (THREAD_SIZE - INT_FRAME_SIZE - EXC_LVL_SIZE)
  84. #ifdef CONFIG_SMP
  85. #define BOOKE_LOAD_EXC_LEVEL_STACK(level) \
  86. mfspr r8,SPRN_PIR; \
  87. slwi r8,r8,2; \
  88. addis r8,r8,level##_STACK_BASE@ha; \
  89. lwz r8,level##_STACK_BASE@l(r8); \
  90. addi r8,r8,EXC_LVL_FRAME_OVERHEAD;
  91. #else
  92. #define BOOKE_LOAD_EXC_LEVEL_STACK(level) \
  93. lis r8,level##_STACK_BASE@ha; \
  94. lwz r8,level##_STACK_BASE@l(r8); \
  95. addi r8,r8,EXC_LVL_FRAME_OVERHEAD;
  96. #endif
  97. /*
  98. * Exception prolog for critical/machine check exceptions. This is a
  99. * little different from the normal exception prolog above since a
  100. * critical/machine check exception can potentially occur at any point
  101. * during normal exception processing. Thus we cannot use the same SPRG
  102. * registers as the normal prolog above. Instead we use a portion of the
  103. * critical/machine check exception stack at low physical addresses.
  104. */
  105. #define EXC_LEVEL_EXCEPTION_PROLOG(exc_level, exc_level_srr0, exc_level_srr1) \
  106. mtspr SPRN_SPRG_WSCRATCH_##exc_level,r8; \
  107. BOOKE_LOAD_EXC_LEVEL_STACK(exc_level);/* r8 points to the exc_level stack*/ \
  108. stw r9,GPR9(r8); /* save various registers */\
  109. mfcr r9; /* save CR in r9 for now */\
  110. stw r10,GPR10(r8); \
  111. stw r11,GPR11(r8); \
  112. stw r9,_CCR(r8); /* save CR on stack */\
  113. mfspr r10,exc_level_srr1; /* check whether user or kernel */\
  114. andi. r10,r10,MSR_PR; \
  115. mfspr r11,SPRN_SPRG_THREAD; /* if from user, start at top of */\
  116. lwz r11,THREAD_INFO-THREAD(r11); /* this thread's kernel stack */\
  117. addi r11,r11,EXC_LVL_FRAME_OVERHEAD; /* allocate stack frame */\
  118. beq 1f; \
  119. /* COMING FROM USER MODE */ \
  120. stw r9,_CCR(r11); /* save CR */\
  121. lwz r10,GPR10(r8); /* copy regs from exception stack */\
  122. lwz r9,GPR9(r8); \
  123. stw r10,GPR10(r11); \
  124. lwz r10,GPR11(r8); \
  125. stw r9,GPR9(r11); \
  126. stw r10,GPR11(r11); \
  127. b 2f; \
  128. /* COMING FROM PRIV MODE */ \
  129. 1: lwz r9,TI_FLAGS-EXC_LVL_FRAME_OVERHEAD(r11); \
  130. lwz r10,TI_PREEMPT-EXC_LVL_FRAME_OVERHEAD(r11); \
  131. stw r9,TI_FLAGS-EXC_LVL_FRAME_OVERHEAD(r8); \
  132. stw r10,TI_PREEMPT-EXC_LVL_FRAME_OVERHEAD(r8); \
  133. lwz r9,TI_TASK-EXC_LVL_FRAME_OVERHEAD(r11); \
  134. stw r9,TI_TASK-EXC_LVL_FRAME_OVERHEAD(r8); \
  135. mr r11,r8; \
  136. 2: mfspr r8,SPRN_SPRG_RSCRATCH_##exc_level; \
  137. stw r12,GPR12(r11); /* save various registers */\
  138. mflr r10; \
  139. stw r10,_LINK(r11); \
  140. mfspr r12,SPRN_DEAR; /* save DEAR and ESR in the frame */\
  141. stw r12,_DEAR(r11); /* since they may have had stuff */\
  142. mfspr r9,SPRN_ESR; /* in them at the point where the */\
  143. stw r9,_ESR(r11); /* exception was taken */\
  144. mfspr r12,exc_level_srr0; \
  145. stw r1,GPR1(r11); \
  146. mfspr r9,exc_level_srr1; \
  147. stw r1,0(r11); \
  148. mr r1,r11; \
  149. rlwinm r9,r9,0,14,12; /* clear MSR_WE (necessary?) */\
  150. stw r0,GPR0(r11); \
  151. SAVE_4GPRS(3, r11); \
  152. SAVE_2GPRS(7, r11)
  153. #define CRITICAL_EXCEPTION_PROLOG \
  154. EXC_LEVEL_EXCEPTION_PROLOG(CRIT, SPRN_CSRR0, SPRN_CSRR1)
  155. #define DEBUG_EXCEPTION_PROLOG \
  156. EXC_LEVEL_EXCEPTION_PROLOG(DBG, SPRN_DSRR0, SPRN_DSRR1)
  157. #define MCHECK_EXCEPTION_PROLOG \
  158. EXC_LEVEL_EXCEPTION_PROLOG(MC, SPRN_MCSRR0, SPRN_MCSRR1)
  159. /*
  160. * Exception vectors.
  161. */
  162. #define START_EXCEPTION(label) \
  163. .align 5; \
  164. label:
  165. #define FINISH_EXCEPTION(func) \
  166. bl transfer_to_handler_full; \
  167. .long func; \
  168. .long ret_from_except_full
  169. #define EXCEPTION(n, label, hdlr, xfer) \
  170. START_EXCEPTION(label); \
  171. NORMAL_EXCEPTION_PROLOG; \
  172. addi r3,r1,STACK_FRAME_OVERHEAD; \
  173. xfer(n, hdlr)
  174. #define CRITICAL_EXCEPTION(n, label, hdlr) \
  175. START_EXCEPTION(label); \
  176. CRITICAL_EXCEPTION_PROLOG; \
  177. addi r3,r1,STACK_FRAME_OVERHEAD; \
  178. EXC_XFER_TEMPLATE(hdlr, n+2, (MSR_KERNEL & ~(MSR_ME|MSR_DE|MSR_CE)), \
  179. NOCOPY, crit_transfer_to_handler, \
  180. ret_from_crit_exc)
  181. #define MCHECK_EXCEPTION(n, label, hdlr) \
  182. START_EXCEPTION(label); \
  183. MCHECK_EXCEPTION_PROLOG; \
  184. mfspr r5,SPRN_ESR; \
  185. stw r5,_ESR(r11); \
  186. addi r3,r1,STACK_FRAME_OVERHEAD; \
  187. EXC_XFER_TEMPLATE(hdlr, n+4, (MSR_KERNEL & ~(MSR_ME|MSR_DE|MSR_CE)), \
  188. NOCOPY, mcheck_transfer_to_handler, \
  189. ret_from_mcheck_exc)
  190. #define EXC_XFER_TEMPLATE(hdlr, trap, msr, copyee, tfer, ret) \
  191. li r10,trap; \
  192. stw r10,_TRAP(r11); \
  193. lis r10,msr@h; \
  194. ori r10,r10,msr@l; \
  195. copyee(r10, r9); \
  196. bl tfer; \
  197. .long hdlr; \
  198. .long ret
  199. #define COPY_EE(d, s) rlwimi d,s,0,16,16
  200. #define NOCOPY(d, s)
  201. #define EXC_XFER_STD(n, hdlr) \
  202. EXC_XFER_TEMPLATE(hdlr, n, MSR_KERNEL, NOCOPY, transfer_to_handler_full, \
  203. ret_from_except_full)
  204. #define EXC_XFER_LITE(n, hdlr) \
  205. EXC_XFER_TEMPLATE(hdlr, n+1, MSR_KERNEL, NOCOPY, transfer_to_handler, \
  206. ret_from_except)
  207. #define EXC_XFER_EE(n, hdlr) \
  208. EXC_XFER_TEMPLATE(hdlr, n, MSR_KERNEL, COPY_EE, transfer_to_handler_full, \
  209. ret_from_except_full)
  210. #define EXC_XFER_EE_LITE(n, hdlr) \
  211. EXC_XFER_TEMPLATE(hdlr, n+1, MSR_KERNEL, COPY_EE, transfer_to_handler, \
  212. ret_from_except)
  213. /* Check for a single step debug exception while in an exception
  214. * handler before state has been saved. This is to catch the case
  215. * where an instruction that we are trying to single step causes
  216. * an exception (eg ITLB/DTLB miss) and thus the first instruction of
  217. * the exception handler generates a single step debug exception.
  218. *
  219. * If we get a debug trap on the first instruction of an exception handler,
  220. * we reset the MSR_DE in the _exception handler's_ MSR (the debug trap is
  221. * a critical exception, so we are using SPRN_CSRR1 to manipulate the MSR).
  222. * The exception handler was handling a non-critical interrupt, so it will
  223. * save (and later restore) the MSR via SPRN_CSRR1, which will still have
  224. * the MSR_DE bit set.
  225. */
  226. #define DEBUG_DEBUG_EXCEPTION \
  227. START_EXCEPTION(DebugDebug); \
  228. DEBUG_EXCEPTION_PROLOG; \
  229. \
  230. /* \
  231. * If there is a single step or branch-taken exception in an \
  232. * exception entry sequence, it was probably meant to apply to \
  233. * the code where the exception occurred (since exception entry \
  234. * doesn't turn off DE automatically). We simulate the effect \
  235. * of turning off DE on entry to an exception handler by turning \
  236. * off DE in the DSRR1 value and clearing the debug status. \
  237. */ \
  238. mfspr r10,SPRN_DBSR; /* check single-step/branch taken */ \
  239. andis. r10,r10,(DBSR_IC|DBSR_BT)@h; \
  240. beq+ 2f; \
  241. \
  242. lis r10,KERNELBASE@h; /* check if exception in vectors */ \
  243. ori r10,r10,KERNELBASE@l; \
  244. cmplw r12,r10; \
  245. blt+ 2f; /* addr below exception vectors */ \
  246. \
  247. lis r10,DebugDebug@h; \
  248. ori r10,r10,DebugDebug@l; \
  249. cmplw r12,r10; \
  250. bgt+ 2f; /* addr above exception vectors */ \
  251. \
  252. /* here it looks like we got an inappropriate debug exception. */ \
  253. 1: rlwinm r9,r9,0,~MSR_DE; /* clear DE in the CDRR1 value */ \
  254. lis r10,(DBSR_IC|DBSR_BT)@h; /* clear the IC event */ \
  255. mtspr SPRN_DBSR,r10; \
  256. /* restore state and get out */ \
  257. lwz r10,_CCR(r11); \
  258. lwz r0,GPR0(r11); \
  259. lwz r1,GPR1(r11); \
  260. mtcrf 0x80,r10; \
  261. mtspr SPRN_DSRR0,r12; \
  262. mtspr SPRN_DSRR1,r9; \
  263. lwz r9,GPR9(r11); \
  264. lwz r12,GPR12(r11); \
  265. mtspr SPRN_SPRG_WSCRATCH_DBG,r8; \
  266. BOOKE_LOAD_EXC_LEVEL_STACK(DBG); /* r8 points to the debug stack */ \
  267. lwz r10,GPR10(r8); \
  268. lwz r11,GPR11(r8); \
  269. mfspr r8,SPRN_SPRG_RSCRATCH_DBG; \
  270. \
  271. PPC_RFDI; \
  272. b .; \
  273. \
  274. /* continue normal handling for a debug exception... */ \
  275. 2: mfspr r4,SPRN_DBSR; \
  276. addi r3,r1,STACK_FRAME_OVERHEAD; \
  277. EXC_XFER_TEMPLATE(DebugException, 0x2008, (MSR_KERNEL & ~(MSR_ME|MSR_DE|MSR_CE)), NOCOPY, debug_transfer_to_handler, ret_from_debug_exc)
  278. #define DEBUG_CRIT_EXCEPTION \
  279. START_EXCEPTION(DebugCrit); \
  280. CRITICAL_EXCEPTION_PROLOG; \
  281. \
  282. /* \
  283. * If there is a single step or branch-taken exception in an \
  284. * exception entry sequence, it was probably meant to apply to \
  285. * the code where the exception occurred (since exception entry \
  286. * doesn't turn off DE automatically). We simulate the effect \
  287. * of turning off DE on entry to an exception handler by turning \
  288. * off DE in the CSRR1 value and clearing the debug status. \
  289. */ \
  290. mfspr r10,SPRN_DBSR; /* check single-step/branch taken */ \
  291. andis. r10,r10,(DBSR_IC|DBSR_BT)@h; \
  292. beq+ 2f; \
  293. \
  294. lis r10,KERNELBASE@h; /* check if exception in vectors */ \
  295. ori r10,r10,KERNELBASE@l; \
  296. cmplw r12,r10; \
  297. blt+ 2f; /* addr below exception vectors */ \
  298. \
  299. lis r10,DebugCrit@h; \
  300. ori r10,r10,DebugCrit@l; \
  301. cmplw r12,r10; \
  302. bgt+ 2f; /* addr above exception vectors */ \
  303. \
  304. /* here it looks like we got an inappropriate debug exception. */ \
  305. 1: rlwinm r9,r9,0,~MSR_DE; /* clear DE in the CSRR1 value */ \
  306. lis r10,(DBSR_IC|DBSR_BT)@h; /* clear the IC event */ \
  307. mtspr SPRN_DBSR,r10; \
  308. /* restore state and get out */ \
  309. lwz r10,_CCR(r11); \
  310. lwz r0,GPR0(r11); \
  311. lwz r1,GPR1(r11); \
  312. mtcrf 0x80,r10; \
  313. mtspr SPRN_CSRR0,r12; \
  314. mtspr SPRN_CSRR1,r9; \
  315. lwz r9,GPR9(r11); \
  316. lwz r12,GPR12(r11); \
  317. mtspr SPRN_SPRG_WSCRATCH_CRIT,r8; \
  318. BOOKE_LOAD_EXC_LEVEL_STACK(CRIT); /* r8 points to the debug stack */ \
  319. lwz r10,GPR10(r8); \
  320. lwz r11,GPR11(r8); \
  321. mfspr r8,SPRN_SPRG_RSCRATCH_CRIT; \
  322. \
  323. rfci; \
  324. b .; \
  325. \
  326. /* continue normal handling for a critical exception... */ \
  327. 2: mfspr r4,SPRN_DBSR; \
  328. addi r3,r1,STACK_FRAME_OVERHEAD; \
  329. EXC_XFER_TEMPLATE(DebugException, 0x2002, (MSR_KERNEL & ~(MSR_ME|MSR_DE|MSR_CE)), NOCOPY, crit_transfer_to_handler, ret_from_crit_exc)
  330. #define DATA_STORAGE_EXCEPTION \
  331. START_EXCEPTION(DataStorage) \
  332. NORMAL_EXCEPTION_PROLOG; \
  333. mfspr r5,SPRN_ESR; /* Grab the ESR and save it */ \
  334. stw r5,_ESR(r11); \
  335. mfspr r4,SPRN_DEAR; /* Grab the DEAR */ \
  336. EXC_XFER_LITE(0x0300, handle_page_fault)
  337. #define INSTRUCTION_STORAGE_EXCEPTION \
  338. START_EXCEPTION(InstructionStorage) \
  339. NORMAL_EXCEPTION_PROLOG; \
  340. mfspr r5,SPRN_ESR; /* Grab the ESR and save it */ \
  341. stw r5,_ESR(r11); \
  342. mr r4,r12; /* Pass SRR0 as arg2 */ \
  343. li r5,0; /* Pass zero as arg3 */ \
  344. EXC_XFER_LITE(0x0400, handle_page_fault)
  345. #define ALIGNMENT_EXCEPTION \
  346. START_EXCEPTION(Alignment) \
  347. NORMAL_EXCEPTION_PROLOG; \
  348. mfspr r4,SPRN_DEAR; /* Grab the DEAR and save it */ \
  349. stw r4,_DEAR(r11); \
  350. addi r3,r1,STACK_FRAME_OVERHEAD; \
  351. EXC_XFER_EE(0x0600, alignment_exception)
  352. #define PROGRAM_EXCEPTION \
  353. START_EXCEPTION(Program) \
  354. NORMAL_EXCEPTION_PROLOG; \
  355. mfspr r4,SPRN_ESR; /* Grab the ESR and save it */ \
  356. stw r4,_ESR(r11); \
  357. addi r3,r1,STACK_FRAME_OVERHEAD; \
  358. EXC_XFER_STD(0x0700, program_check_exception)
  359. #define DECREMENTER_EXCEPTION \
  360. START_EXCEPTION(Decrementer) \
  361. NORMAL_EXCEPTION_PROLOG; \
  362. lis r0,TSR_DIS@h; /* Setup the DEC interrupt mask */ \
  363. mtspr SPRN_TSR,r0; /* Clear the DEC interrupt */ \
  364. addi r3,r1,STACK_FRAME_OVERHEAD; \
  365. EXC_XFER_LITE(0x0900, timer_interrupt)
  366. #define FP_UNAVAILABLE_EXCEPTION \
  367. START_EXCEPTION(FloatingPointUnavailable) \
  368. NORMAL_EXCEPTION_PROLOG; \
  369. beq 1f; \
  370. bl load_up_fpu; /* if from user, just load it up */ \
  371. b fast_exception_return; \
  372. 1: addi r3,r1,STACK_FRAME_OVERHEAD; \
  373. EXC_XFER_EE_LITE(0x800, kernel_fp_unavailable_exception)
  374. #ifndef __ASSEMBLY__
  375. struct exception_regs {
  376. unsigned long mas0;
  377. unsigned long mas1;
  378. unsigned long mas2;
  379. unsigned long mas3;
  380. unsigned long mas6;
  381. unsigned long mas7;
  382. unsigned long srr0;
  383. unsigned long srr1;
  384. unsigned long csrr0;
  385. unsigned long csrr1;
  386. unsigned long dsrr0;
  387. unsigned long dsrr1;
  388. unsigned long saved_ksp_limit;
  389. };
  390. /* ensure this structure is always sized to a multiple of the stack alignment */
  391. #define STACK_EXC_LVL_FRAME_SIZE _ALIGN_UP(sizeof (struct exception_regs), 16)
  392. #endif /* __ASSEMBLY__ */
  393. #endif /* __HEAD_BOOKE_H__ */