exception.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * File : exception.c
  3. * Change Logs:
  4. * Date Author Notes
  5. * 2010-05-17 swkyer first version
  6. */
  7. #include <rtthread.h>
  8. #include <rthw.h>
  9. #include "../common/exception.h"
  10. #include "../common/mipsregs.h"
  11. /**
  12. * @addtogroup SoC3210
  13. */
  14. /*@{*/
  15. /**
  16. * exception handle table
  17. */
  18. #define RT_EXCEPTION_MAX 8
  19. exception_func_t sys_exception_handlers[RT_EXCEPTION_MAX];
  20. /**
  21. * setup the exception handle
  22. */
  23. exception_func_t rt_set_except_vector(int n, exception_func_t func)
  24. {
  25. exception_func_t old_handler = sys_exception_handlers[n];
  26. if ((n == 0) || (n > RT_EXCEPTION_MAX) || (!func))
  27. {
  28. return 0;
  29. }
  30. sys_exception_handlers[n] = func;
  31. return old_handler;
  32. }
  33. void tlb_refill_handler(void)
  34. {
  35. rt_kprintf("tlb-miss happens, epc: 0x%08x\n", read_c0_epc());
  36. rt_hw_cpu_shutdown();
  37. }
  38. void cache_error_handler(void)
  39. {
  40. rt_kprintf("cache exception happens, epc: 0x%08x\n", read_c0_epc());
  41. rt_hw_cpu_shutdown();
  42. }
  43. static void unhandled_exception_handle(pt_regs_t *regs)
  44. {
  45. rt_kprintf("exception happens, epc: 0x%08x, cause: 0x%08x\n", regs->cp0_epc, read_c0_cause());
  46. }
  47. void install_default_execpt_handle(void)
  48. {
  49. rt_int32_t i;
  50. for (i=0; i<RT_EXCEPTION_MAX; i++)
  51. sys_exception_handlers[i] = (exception_func_t)unhandled_exception_handle;
  52. }
  53. void exception_handler(pt_regs_t *regs)
  54. {
  55. rt_uint32_t cause;
  56. rt_uint32_t index;
  57. cause = (read_c0_cause() & read_c0_config());
  58. cause = (cause & 0xfc00) >> 8;
  59. for (index = RT_EXCEPTION_MAX; index > 0; index --)
  60. {
  61. if (cause & (1 << index))
  62. {
  63. sys_exception_handlers[index](regs);
  64. cause &= ~(1 << index);
  65. }
  66. }
  67. }
  68. /*@}*/