hw_exception_handler.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Exception handling for Microblaze
  3. *
  4. * Rewriten interrupt handling
  5. *
  6. * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
  7. * Copyright (C) 2008-2009 PetaLogix
  8. *
  9. * uClinux customisation (C) 2005 John Williams
  10. *
  11. * MMU code derived from arch/ppc/kernel/head_4xx.S:
  12. * Copyright (C) 1995-1996 Gary Thomas <gdt@linuxppc.org>
  13. * Initial PowerPC version.
  14. * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
  15. * Rewritten for PReP
  16. * Copyright (C) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
  17. * Low-level exception handers, MMU support, and rewrite.
  18. * Copyright (C) 1997 Dan Malek <dmalek@jlc.net>
  19. * PowerPC 8xx modifications.
  20. * Copyright (C) 1998-1999 TiVo, Inc.
  21. * PowerPC 403GCX modifications.
  22. * Copyright (C) 1999 Grant Erickson <grant@lcse.umn.edu>
  23. * PowerPC 403GCX/405GP modifications.
  24. * Copyright 2000 MontaVista Software Inc.
  25. * PPC405 modifications
  26. * PowerPC 403GCX/405GP modifications.
  27. * Author: MontaVista Software, Inc.
  28. * frank_rowand@mvista.com or source@mvista.com
  29. * debbie_chu@mvista.com
  30. *
  31. * Original code
  32. * Copyright (C) 2004 Xilinx, Inc.
  33. *
  34. * This program is free software; you can redistribute it and/or modify it
  35. * under the terms of the GNU General Public License version 2 as published
  36. * by the Free Software Foundation.
  37. */
  38. /*
  39. * Here are the handlers which don't require enabling translation
  40. * and calling other kernel code thus we can keep their design very simple
  41. * and do all processing in real mode. All what they need is a valid current
  42. * (that is an issue for the CONFIG_REGISTER_TASK_PTR case)
  43. * This handlers use r3,r4,r5,r6 and optionally r[current] to work therefore
  44. * these registers are saved/restored
  45. * The handlers which require translation are in entry.S --KAA
  46. *
  47. * Microblaze HW Exception Handler
  48. * - Non self-modifying exception handler for the following exception conditions
  49. * - Unalignment
  50. * - Instruction bus error
  51. * - Data bus error
  52. * - Illegal instruction opcode
  53. * - Divide-by-zero
  54. *
  55. * Note we disable interrupts during exception handling, otherwise we will
  56. * possibly get multiple re-entrancy if interrupt handles themselves cause
  57. * exceptions. JW
  58. */
  59. #include <asm/exceptions.h>
  60. #include <asm/unistd.h>
  61. #include <asm/page.h>
  62. #include <asm/entry.h>
  63. #include <asm/current.h>
  64. #include <linux/linkage.h>
  65. #include <asm/mmu.h>
  66. #include <asm/pgtable.h>
  67. #include <asm/asm-offsets.h>
  68. /* Helpful Macros */
  69. #define EX_HANDLER_STACK_SIZ (4*19)
  70. #define NUM_TO_REG(num) r ## num
  71. #define LWREG_NOP \
  72. bri ex_handler_unhandled; \
  73. nop;
  74. #define SWREG_NOP \
  75. bri ex_handler_unhandled; \
  76. nop;
  77. /* FIXME this is weird - for noMMU kernel is not possible to use brid
  78. * instruction which can shorten executed time
  79. */
  80. /* r3 is the source */
  81. #define R3_TO_LWREG_V(regnum) \
  82. swi r3, r1, 4 * regnum; \
  83. bri ex_handler_done;
  84. /* r3 is the source */
  85. #define R3_TO_LWREG(regnum) \
  86. or NUM_TO_REG (regnum), r0, r3; \
  87. bri ex_handler_done;
  88. /* r3 is the target */
  89. #define SWREG_TO_R3_V(regnum) \
  90. lwi r3, r1, 4 * regnum; \
  91. bri ex_sw_tail;
  92. /* r3 is the target */
  93. #define SWREG_TO_R3(regnum) \
  94. or r3, r0, NUM_TO_REG (regnum); \
  95. bri ex_sw_tail;
  96. .extern other_exception_handler /* Defined in exception.c */
  97. /*
  98. * hw_exception_handler - Handler for exceptions
  99. *
  100. * Exception handler notes:
  101. * - Handles all exceptions
  102. * - Does not handle unaligned exceptions during load into r17, r1, r0.
  103. * - Does not handle unaligned exceptions during store from r17 (cannot be
  104. * done) and r1 (slows down common case)
  105. *
  106. * Relevant register structures
  107. *
  108. * EAR - |----|----|----|----|----|----|----|----|
  109. * - < ## 32 bit faulting address ## >
  110. *
  111. * ESR - |----|----|----|----|----| - | - |-----|-----|
  112. * - W S REG EXC
  113. *
  114. *
  115. * STACK FRAME STRUCTURE (for NO_MMU)
  116. * ---------------------------------
  117. *
  118. * +-------------+ + 0
  119. * | MSR |
  120. * +-------------+ + 4
  121. * | r1 |
  122. * | . |
  123. * | . |
  124. * | . |
  125. * | . |
  126. * | r18 |
  127. * +-------------+ + 76
  128. * | . |
  129. * | . |
  130. *
  131. * NO_MMU kernel use the same r0_ram pointed space - look to vmlinux.lds.S
  132. * which is used for storing register values - old style was, that value were
  133. * stored in stack but in case of failure you lost information about register.
  134. * Currently you can see register value in memory in specific place.
  135. * In compare to with previous solution the speed should be the same.
  136. *
  137. * MMU exception handler has different handling compare to no MMU kernel.
  138. * Exception handler use jump table for directing of what happen. For MMU kernel
  139. * is this approach better because MMU relate exception are handled by asm code
  140. * in this file. In compare to with MMU expect of unaligned exception
  141. * is everything handled by C code.
  142. */
  143. /*
  144. * every of these handlers is entered having R3/4/5/6/11/current saved on stack
  145. * and clobbered so care should be taken to restore them if someone is going to
  146. * return from exception
  147. */
  148. /* wrappers to restore state before coming to entry.S */
  149. .global _hw_exception_handler
  150. .section .text
  151. .align 4
  152. .ent _hw_exception_handler
  153. _hw_exception_handler:
  154. addik r1, r1, -(EX_HANDLER_STACK_SIZ); /* Create stack frame */
  155. swi r3, r1, PT_R3
  156. swi r4, r1, PT_R4
  157. swi r5, r1, PT_R5
  158. swi r6, r1, PT_R6
  159. mfs r5, rmsr;
  160. nop
  161. swi r5, r1, 0;
  162. mfs r4, rbtr /* Save BTR before jumping to handler */
  163. nop
  164. mfs r3, resr
  165. nop
  166. andi r5, r3, 0x1000; /* Check ESR[DS] */
  167. beqi r5, not_in_delay_slot; /* Branch if ESR[DS] not set */
  168. mfs r17, rbtr; /* ESR[DS] set - return address in BTR */
  169. nop
  170. not_in_delay_slot:
  171. swi r17, r1, PT_R17
  172. andi r5, r3, 0x1F; /* Extract ESR[EXC] */
  173. /* Exceptions enabled here. This will allow nested exceptions */
  174. mfs r6, rmsr;
  175. nop
  176. swi r6, r1, 0; /* RMSR_OFFSET */
  177. ori r6, r6, 0x100; /* Turn ON the EE bit */
  178. andi r6, r6, ~2; /* Disable interrupts */
  179. mts rmsr, r6;
  180. nop
  181. xori r6, r5, 1; /* 00001 = Unaligned Exception */
  182. /* Jump to unalignment exception handler */
  183. beqi r6, handle_unaligned_ex;
  184. handle_other_ex: /* Handle Other exceptions here */
  185. /* Save other volatiles before we make procedure calls below */
  186. swi r7, r1, PT_R7
  187. swi r8, r1, PT_R8
  188. swi r9, r1, PT_R9
  189. swi r10, r1, PT_R10
  190. swi r11, r1, PT_R11
  191. swi r12, r1, PT_R12
  192. swi r14, r1, PT_R14
  193. swi r15, r1, PT_R15
  194. swi r18, r1, PT_R18
  195. or r5, r1, r0
  196. andi r6, r3, 0x1F; /* Load ESR[EC] */
  197. lwi r7, r0, PER_CPU(KM) /* MS: saving current kernel mode to regs */
  198. swi r7, r1, PT_MODE
  199. mfs r7, rfsr
  200. nop
  201. addk r8, r17, r0; /* Load exception address */
  202. bralid r15, full_exception; /* Branch to the handler */
  203. nop;
  204. /*
  205. * Trigger execution of the signal handler by enabling
  206. * interrupts and calling an invalid syscall.
  207. */
  208. mfs r5, rmsr;
  209. nop
  210. ori r5, r5, 2;
  211. mts rmsr, r5; /* enable interrupt */
  212. nop
  213. addi r12, r0, __NR_syscalls;
  214. brki r14, 0x08;
  215. mfs r5, rmsr; /* disable interrupt */
  216. nop
  217. andi r5, r5, ~2;
  218. mts rmsr, r5;
  219. nop
  220. lwi r7, r1, PT_R7
  221. lwi r8, r1, PT_R8
  222. lwi r9, r1, PT_R9
  223. lwi r10, r1, PT_R10
  224. lwi r11, r1, PT_R11
  225. lwi r12, r1, PT_R12
  226. lwi r14, r1, PT_R14
  227. lwi r15, r1, PT_R15
  228. lwi r18, r1, PT_R18
  229. bri ex_handler_done; /* Complete exception handling */
  230. /* 0x01 - Unaligned data access exception
  231. * This occurs when a word access is not aligned on a word boundary,
  232. * or when a 16-bit access is not aligned on a 16-bit boundary.
  233. * This handler perform the access, and returns, except for MMU when
  234. * the unaligned address is last on a 4k page or the physical address is
  235. * not found in the page table, in which case unaligned_data_trap is called.
  236. */
  237. handle_unaligned_ex:
  238. /* Working registers already saved: R3, R4, R5, R6
  239. * R3 = ESR
  240. * R4 = BTR
  241. */
  242. mfs r4, rear;
  243. nop
  244. andi r6, r3, 0x3E0; /* Mask and extract the register operand */
  245. srl r6, r6; /* r6 >> 5 */
  246. srl r6, r6;
  247. srl r6, r6;
  248. srl r6, r6;
  249. srl r6, r6;
  250. /* Store the register operand in a temporary location */
  251. sbi r6, r0, TOPHYS(ex_reg_op);
  252. andi r6, r3, 0x400; /* Extract ESR[S] */
  253. bnei r6, ex_sw;
  254. ex_lw:
  255. andi r6, r3, 0x800; /* Extract ESR[W] */
  256. beqi r6, ex_lhw;
  257. lbui r5, r4, 0; /* Exception address in r4 */
  258. /* Load a word, byte-by-byte from destination address
  259. and save it in tmp space */
  260. sbi r5, r0, TOPHYS(ex_tmp_data_loc_0);
  261. lbui r5, r4, 1;
  262. sbi r5, r0, TOPHYS(ex_tmp_data_loc_1);
  263. lbui r5, r4, 2;
  264. sbi r5, r0, TOPHYS(ex_tmp_data_loc_2);
  265. lbui r5, r4, 3;
  266. sbi r5, r0, TOPHYS(ex_tmp_data_loc_3);
  267. /* Get the destination register value into r3 */
  268. lwi r3, r0, TOPHYS(ex_tmp_data_loc_0);
  269. bri ex_lw_tail;
  270. ex_lhw:
  271. lbui r5, r4, 0; /* Exception address in r4 */
  272. /* Load a half-word, byte-by-byte from destination
  273. address and save it in tmp space */
  274. sbi r5, r0, TOPHYS(ex_tmp_data_loc_0);
  275. lbui r5, r4, 1;
  276. sbi r5, r0, TOPHYS(ex_tmp_data_loc_1);
  277. /* Get the destination register value into r3 */
  278. lhui r3, r0, TOPHYS(ex_tmp_data_loc_0);
  279. ex_lw_tail:
  280. /* Get the destination register number into r5 */
  281. lbui r5, r0, TOPHYS(ex_reg_op);
  282. /* Form load_word jump table offset (lw_table + (8 * regnum)) */
  283. la r6, r0, TOPHYS(lw_table);
  284. addk r5, r5, r5;
  285. addk r5, r5, r5;
  286. addk r5, r5, r5;
  287. addk r5, r5, r6;
  288. bra r5;
  289. ex_lw_end: /* Exception handling of load word, ends */
  290. ex_sw:
  291. /* Get the destination register number into r5 */
  292. lbui r5, r0, TOPHYS(ex_reg_op);
  293. /* Form store_word jump table offset (sw_table + (8 * regnum)) */
  294. la r6, r0, TOPHYS(sw_table);
  295. add r5, r5, r5;
  296. add r5, r5, r5;
  297. add r5, r5, r5;
  298. add r5, r5, r6;
  299. bra r5;
  300. ex_sw_tail:
  301. mfs r6, resr;
  302. nop
  303. andi r6, r6, 0x800; /* Extract ESR[W] */
  304. beqi r6, ex_shw;
  305. /* Get the word - delay slot */
  306. swi r3, r0, TOPHYS(ex_tmp_data_loc_0);
  307. /* Store the word, byte-by-byte into destination address */
  308. lbui r3, r0, TOPHYS(ex_tmp_data_loc_0);
  309. sbi r3, r4, 0;
  310. lbui r3, r0, TOPHYS(ex_tmp_data_loc_1);
  311. sbi r3, r4, 1;
  312. lbui r3, r0, TOPHYS(ex_tmp_data_loc_2);
  313. sbi r3, r4, 2;
  314. lbui r3, r0, TOPHYS(ex_tmp_data_loc_3);
  315. sbi r3, r4, 3;
  316. bri ex_handler_done;
  317. ex_shw:
  318. /* Store the lower half-word, byte-by-byte into destination address */
  319. swi r3, r0, TOPHYS(ex_tmp_data_loc_0);
  320. lbui r3, r0, TOPHYS(ex_tmp_data_loc_2);
  321. sbi r3, r4, 0;
  322. lbui r3, r0, TOPHYS(ex_tmp_data_loc_3);
  323. sbi r3, r4, 1;
  324. ex_sw_end: /* Exception handling of store word, ends. */
  325. ex_handler_done:
  326. lwi r5, r1, 0 /* RMSR */
  327. mts rmsr, r5
  328. nop
  329. lwi r3, r1, PT_R3
  330. lwi r4, r1, PT_R4
  331. lwi r5, r1, PT_R5
  332. lwi r6, r1, PT_R6
  333. lwi r17, r1, PT_R17
  334. rted r17, 0
  335. addik r1, r1, (EX_HANDLER_STACK_SIZ); /* Restore stack frame */
  336. .end _hw_exception_handler
  337. ex_handler_unhandled:
  338. /* FIXME add handle function for unhandled exception - dump register */
  339. bri 0
  340. .section .text
  341. .align 4
  342. lw_table:
  343. lw_r0: R3_TO_LWREG (0);
  344. lw_r1: LWREG_NOP;
  345. lw_r2: R3_TO_LWREG (2);
  346. lw_r3: R3_TO_LWREG_V (3);
  347. lw_r4: R3_TO_LWREG_V (4);
  348. lw_r5: R3_TO_LWREG_V (5);
  349. lw_r6: R3_TO_LWREG_V (6);
  350. lw_r7: R3_TO_LWREG (7);
  351. lw_r8: R3_TO_LWREG (8);
  352. lw_r9: R3_TO_LWREG (9);
  353. lw_r10: R3_TO_LWREG (10);
  354. lw_r11: R3_TO_LWREG (11);
  355. lw_r12: R3_TO_LWREG (12);
  356. lw_r13: R3_TO_LWREG (13);
  357. lw_r14: R3_TO_LWREG (14);
  358. lw_r15: R3_TO_LWREG (15);
  359. lw_r16: R3_TO_LWREG (16);
  360. lw_r17: LWREG_NOP;
  361. lw_r18: R3_TO_LWREG (18);
  362. lw_r19: R3_TO_LWREG (19);
  363. lw_r20: R3_TO_LWREG (20);
  364. lw_r21: R3_TO_LWREG (21);
  365. lw_r22: R3_TO_LWREG (22);
  366. lw_r23: R3_TO_LWREG (23);
  367. lw_r24: R3_TO_LWREG (24);
  368. lw_r25: R3_TO_LWREG (25);
  369. lw_r26: R3_TO_LWREG (26);
  370. lw_r27: R3_TO_LWREG (27);
  371. lw_r28: R3_TO_LWREG (28);
  372. lw_r29: R3_TO_LWREG (29);
  373. lw_r30: R3_TO_LWREG (30);
  374. lw_r31: R3_TO_LWREG (31);
  375. sw_table:
  376. sw_r0: SWREG_TO_R3 (0);
  377. sw_r1: SWREG_NOP;
  378. sw_r2: SWREG_TO_R3 (2);
  379. sw_r3: SWREG_TO_R3_V (3);
  380. sw_r4: SWREG_TO_R3_V (4);
  381. sw_r5: SWREG_TO_R3_V (5);
  382. sw_r6: SWREG_TO_R3_V (6);
  383. sw_r7: SWREG_TO_R3 (7);
  384. sw_r8: SWREG_TO_R3 (8);
  385. sw_r9: SWREG_TO_R3 (9);
  386. sw_r10: SWREG_TO_R3 (10);
  387. sw_r11: SWREG_TO_R3 (11);
  388. sw_r12: SWREG_TO_R3 (12);
  389. sw_r13: SWREG_TO_R3 (13);
  390. sw_r14: SWREG_TO_R3 (14);
  391. sw_r15: SWREG_TO_R3 (15);
  392. sw_r16: SWREG_TO_R3 (16);
  393. sw_r17: SWREG_NOP;
  394. sw_r18: SWREG_TO_R3 (18);
  395. sw_r19: SWREG_TO_R3 (19);
  396. sw_r20: SWREG_TO_R3 (20);
  397. sw_r21: SWREG_TO_R3 (21);
  398. sw_r22: SWREG_TO_R3 (22);
  399. sw_r23: SWREG_TO_R3 (23);
  400. sw_r24: SWREG_TO_R3 (24);
  401. sw_r25: SWREG_TO_R3 (25);
  402. sw_r26: SWREG_TO_R3 (26);
  403. sw_r27: SWREG_TO_R3 (27);
  404. sw_r28: SWREG_TO_R3 (28);
  405. sw_r29: SWREG_TO_R3 (29);
  406. sw_r30: SWREG_TO_R3 (30);
  407. sw_r31: SWREG_TO_R3 (31);
  408. /* Temporary data structures used in the handler */
  409. .section .data
  410. .align 4
  411. ex_tmp_data_loc_0:
  412. .byte 0
  413. ex_tmp_data_loc_1:
  414. .byte 0
  415. ex_tmp_data_loc_2:
  416. .byte 0
  417. ex_tmp_data_loc_3:
  418. .byte 0
  419. ex_reg_op:
  420. .byte 0