head_booke.h 16 KB

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