interrupt.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018/10/01 Bernard The first version
  9. * 2018/12/27 Jesven Change irq enable/disable to cpu0
  10. */
  11. #include "tick.h"
  12. #include <plic.h>
  13. #include "encoding.h"
  14. #include "riscv.h"
  15. #include "interrupt.h"
  16. #define MAX_HANDLERS 128
  17. static struct rt_irq_desc irq_desc[MAX_HANDLERS];
  18. static rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector, void *param)
  19. {
  20. rt_kprintf("UN-handled interrupt %d occurred!!!\n", vector);
  21. return RT_NULL;
  22. }
  23. int rt_hw_clint_ipi_enable(void)
  24. {
  25. /* Set the Machine-Software bit in MIE */
  26. set_csr(mie, MIP_MSIP);
  27. return 0;
  28. }
  29. int rt_hw_clint_ipi_disable(void)
  30. {
  31. /* Clear the Machine-Software bit in MIE */
  32. clear_csr(mie, MIP_MSIP);
  33. return 0;
  34. }
  35. int rt_hw_plic_irq_enable(int irq_number)
  36. {
  37. plic_irq_enable(irq_number);
  38. return 0;
  39. }
  40. int rt_hw_plic_irq_disable(int irq_number)
  41. {
  42. plic_irq_disable(irq_number);
  43. return 0;
  44. }
  45. /**
  46. * This function will initialize hardware interrupt
  47. */
  48. void rt_hw_interrupt_init(void)
  49. {
  50. int idx = 0;
  51. /* init exceptions table */
  52. for (idx = 0; idx < MAX_HANDLERS; idx++)
  53. {
  54. irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  55. irq_desc[idx].param = RT_NULL;
  56. #ifdef RT_USING_INTERRUPT_INFO
  57. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
  58. irq_desc[idx].counter = 0;
  59. #endif
  60. }
  61. plic_set_threshold(0);
  62. }
  63. /**
  64. * This function will mask a interrupt.
  65. * @param vector the interrupt number
  66. */
  67. void rt_hw_interrupt_mask(int vector)
  68. {
  69. rt_hw_plic_irq_disable(vector);
  70. }
  71. /**
  72. * This function will un-mask a interrupt.
  73. * @param vector the interrupt number
  74. */
  75. void rt_hw_interrupt_umask(int vector)
  76. {
  77. plic_set_priority(vector, 1);
  78. rt_hw_plic_irq_enable(vector);
  79. }
  80. /**
  81. * This function will install a interrupt service routine to a interrupt.
  82. * @param vector the interrupt number
  83. * @param new_handler the interrupt service routine to be installed
  84. * @param old_handler the old interrupt service routine
  85. */
  86. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  87. void *param, const char *name)
  88. {
  89. rt_isr_handler_t old_handler = RT_NULL;
  90. if(vector < MAX_HANDLERS)
  91. {
  92. old_handler = irq_desc[vector].handler;
  93. if (handler != RT_NULL)
  94. {
  95. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  96. irq_desc[vector].param = param;
  97. #ifdef RT_USING_INTERRUPT_INFO
  98. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  99. irq_desc[vector].counter = 0;
  100. #endif
  101. }
  102. }
  103. return old_handler;
  104. }
  105. RT_WEAK
  106. void plic_irq_handle(int irq)
  107. {
  108. rt_kprintf("UN-handled interrupt %d occurred!!!\n", irq);
  109. return ;
  110. }
  111. void dump_regs(struct rt_hw_stack_frame *regs)
  112. {
  113. rt_kprintf("--------------Dump Registers-----------------\n");
  114. rt_kprintf("Function Registers:\n");
  115. rt_kprintf("\tra(x1) = 0x%p\tuser_sp = 0x%p\n",regs -> ra,regs -> user_sp_exc_stack);
  116. rt_kprintf("\tgp(x3) = 0x%p\ttp(x4) = 0x%p\n",regs -> gp,regs -> tp);
  117. rt_kprintf("Temporary Registers:\n");
  118. rt_kprintf("\tt0(x5) = 0x%p\tt1(x6) = 0x%p\n",regs -> t0,regs -> t1);
  119. rt_kprintf("\tt2(x7) = 0x%p\n",regs -> t2);
  120. rt_kprintf("\tt3(x28) = 0x%p\tt4(x29) = 0x%p\n",regs -> t3,regs -> t4);
  121. rt_kprintf("\tt5(x30) = 0x%p\tt6(x31) = 0x%p\n",regs -> t5,regs -> t6);
  122. rt_kprintf("Saved Registers:\n");
  123. rt_kprintf("\ts0/fp(x8) = 0x%p\ts1(x9) = 0x%p\n",regs -> s0_fp,regs -> s1);
  124. rt_kprintf("\ts2(x18) = 0x%p\ts3(x19) = 0x%p\n",regs -> s2,regs -> s3);
  125. rt_kprintf("\ts4(x20) = 0x%p\ts5(x21) = 0x%p\n",regs -> s4,regs -> s5);
  126. rt_kprintf("\ts6(x22) = 0x%p\ts7(x23) = 0x%p\n",regs -> s6,regs -> s7);
  127. rt_kprintf("\ts8(x24) = 0x%p\ts9(x25) = 0x%p\n",regs -> s8,regs -> s9);
  128. rt_kprintf("\ts10(x26) = 0x%p\ts11(x27) = 0x%p\n",regs -> s10,regs -> s11);
  129. rt_kprintf("Function Arguments Registers:\n");
  130. rt_kprintf("\ta0(x10) = 0x%p\ta1(x11) = 0x%p\n",regs -> a0,regs -> a1);
  131. rt_kprintf("\ta2(x12) = 0x%p\ta3(x13) = 0x%p\n",regs -> a2,regs -> a3);
  132. rt_kprintf("\ta4(x14) = 0x%p\ta5(x15) = 0x%p\n",regs -> a4,regs -> a5);
  133. rt_kprintf("\ta6(x16) = 0x%p\ta7(x17) = 0x%p\n",regs -> a6,regs -> a7);
  134. rt_kprintf("xstatus = 0x%p\n",regs -> xstatus);
  135. rt_kprintf("\t%s\n",(regs -> xstatus & SSTATUS_SIE) ? "Supervisor Interrupt Enabled" : "Supervisor Interrupt Disabled");
  136. rt_kprintf("\t%s\n",(regs -> xstatus & SSTATUS_SPIE) ? "Last Time Supervisor Interrupt Enabled" : "Last Time Supervisor Interrupt Disabled");
  137. rt_kprintf("\t%s\n",(regs -> xstatus & SSTATUS_SPP) ? "Last Privilege is Supervisor Mode" : "Last Privilege is User Mode");
  138. rt_kprintf("\t%s\n",(regs -> xstatus & SSTATUS_PUM) ? "Permit to Access User Page" : "Not Permit to Access User Page");
  139. rt_kprintf("\t%s\n",(regs -> xstatus & (1 << 19)) ? "Permit to Read Executable-only Page" : "Not Permit to Read Executable-only Page");
  140. rt_size_t satp_v = read_csr(satp);
  141. rt_kprintf("satp = 0x%p\n",satp_v);
  142. const char *mode_str = "Unknown Address Translation/Protection Mode";
  143. switch(__MASKVALUE(satp_v >> 60,__MASK(4)))
  144. {
  145. case 0:
  146. mode_str = "No Address Translation/Protection Mode";
  147. break;
  148. case 8:
  149. mode_str = "Page-based 39-bit Virtual Addressing Mode";
  150. break;
  151. case 9:
  152. mode_str = "Page-based 48-bit Virtual Addressing Mode";
  153. break;
  154. }
  155. rt_kprintf("\tMode = %s\n",mode_str);
  156. rt_kprintf("-----------------Dump OK---------------------\n");
  157. }
  158. void handle_trap(rt_size_t xcause,rt_size_t xtval,rt_size_t xepc,struct rt_hw_stack_frame *sp)
  159. {
  160. int cause = (xcause & 0xFFFFFFFF);
  161. int plic_irq = 0;
  162. if (xcause & (1UL << 63))
  163. {
  164. switch (cause)
  165. {
  166. case IRQ_M_SOFT:
  167. {
  168. }
  169. break;
  170. case IRQ_M_TIMER:
  171. tick_isr();
  172. break;
  173. case IRQ_S_TIMER:
  174. tick_isr();
  175. break;
  176. case IRQ_S_EXT:
  177. plic_irq = plic_claim();
  178. plic_complete(plic_irq);
  179. irq_desc[plic_irq].handler(plic_irq, irq_desc[plic_irq].param);
  180. break;
  181. case IRQ_M_EXT:
  182. plic_irq = plic_claim();
  183. plic_complete(plic_irq);
  184. irq_desc[plic_irq].handler(plic_irq, irq_desc[plic_irq].param);
  185. break;
  186. }
  187. }
  188. else
  189. {
  190. rt_thread_t tid;
  191. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  192. extern long list_thread();
  193. #endif
  194. rt_hw_interrupt_disable();
  195. rt_kprintf("xcause = %08x,xtval = %08x,xepc = %08x\n", xcause, xtval, xepc);
  196. tid = rt_thread_self();
  197. rt_kprintf("\nException:\n");
  198. switch (cause)
  199. {
  200. case CAUSE_MISALIGNED_FETCH:
  201. rt_kprintf("Instruction address misaligned");
  202. break;
  203. case CAUSE_FAULT_FETCH:
  204. rt_kprintf("Instruction access fault");
  205. break;
  206. case CAUSE_ILLEGAL_INSTRUCTION:
  207. rt_kprintf("Illegal instruction");
  208. break;
  209. case CAUSE_BREAKPOINT:
  210. rt_kprintf("Breakpoint");
  211. break;
  212. case CAUSE_MISALIGNED_LOAD:
  213. rt_kprintf("Load address misaligned");
  214. break;
  215. case CAUSE_FAULT_LOAD:
  216. rt_kprintf("Load access fault");
  217. break;
  218. case CAUSE_MISALIGNED_STORE:
  219. rt_kprintf("Store address misaligned");
  220. break;
  221. case CAUSE_FAULT_STORE:
  222. rt_kprintf("Store access fault");
  223. break;
  224. case CAUSE_USER_ECALL:
  225. rt_kprintf("Environment call from U-mode");
  226. break;
  227. case CAUSE_SUPERVISOR_ECALL:
  228. rt_kprintf("Environment call from S-mode");
  229. break;
  230. case CAUSE_HYPERVISOR_ECALL:
  231. rt_kprintf("Environment call from H-mode");
  232. break;
  233. case CAUSE_MACHINE_ECALL:
  234. rt_kprintf("Environment call from M-mode");
  235. break;
  236. default:
  237. rt_kprintf("Uknown exception : %08lX", cause);
  238. break;
  239. }
  240. rt_kprintf("\n");
  241. dump_regs(sp);
  242. rt_kprintf("exception pc => 0x%08x\n", xepc);
  243. rt_kprintf("current thread: %.*s\n", RT_NAME_MAX, tid->name);
  244. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  245. list_thread();
  246. #endif
  247. while(1);
  248. }
  249. rt_hw_interrupt_enable(0);
  250. }