head_booke.h 15 KB

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