head_booke.h 16 KB

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