exception.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * File : cpu.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-05-17 swkyer first version
  13. */
  14. #include <rtthread.h>
  15. #include <rthw.h>
  16. #include "../common/exception.h"
  17. #include "../common/mipsregs.h"
  18. /**
  19. * @addtogroup Loongson
  20. */
  21. /*@{*/
  22. /**
  23. * exception handle table
  24. */
  25. #define RT_EXCEPTION_MAX 8
  26. exception_func_t sys_exception_handlers[RT_EXCEPTION_MAX];
  27. /**
  28. * setup the exception handle
  29. */
  30. exception_func_t rt_set_except_vector(int n, exception_func_t func)
  31. {
  32. exception_func_t old_handler = sys_exception_handlers[n];
  33. if ((n == 0) || (n > RT_EXCEPTION_MAX) || (!func))
  34. {
  35. return 0;
  36. }
  37. sys_exception_handlers[n] = func;
  38. return old_handler;
  39. }
  40. void tlb_refill_handler(void)
  41. {
  42. rt_kprintf("tlb-miss happens, epc: 0x%08x\n", read_c0_epc());
  43. rt_hw_cpu_shutdown();
  44. }
  45. void cache_error_handler(void)
  46. {
  47. rt_kprintf("cache exception happens, epc: 0x%08x\n", read_c0_epc());
  48. rt_hw_cpu_shutdown();
  49. }
  50. static void unhandled_exception_handle(pt_regs_t *regs)
  51. {
  52. rt_kprintf("exception happens, epc: 0x%08x, cause: 0x%08x\n", regs->cp0_epc, read_c0_cause());
  53. }
  54. void install_default_execpt_handle(void)
  55. {
  56. rt_int32_t i;
  57. for (i=0; i<RT_EXCEPTION_MAX; i++)
  58. sys_exception_handlers[i] = (exception_func_t)unhandled_exception_handle;
  59. }
  60. void exception_handler(pt_regs_t *regs)
  61. {
  62. rt_uint32_t cause;
  63. rt_uint32_t index;
  64. cause = (read_c0_cause() & read_c0_config());
  65. cause = (cause & 0xfc00) >> 8;
  66. for (index = RT_EXCEPTION_MAX; index > 0; index --)
  67. {
  68. if (cause & (1 << index))
  69. {
  70. sys_exception_handlers[index](regs);
  71. cause &= ~(1 << index);
  72. }
  73. }
  74. }
  75. /*@}*/