trap.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2013-07-20 Bernard first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <board.h>
  13. #include <armv8.h>
  14. #include "interrupt.h"
  15. #include "mm_aspace.h"
  16. #define DBG_TAG "libcpu.trap"
  17. #define DBG_LVL DBG_LOG
  18. #include <rtdbg.h>
  19. #ifdef RT_USING_FINSH
  20. extern long list_thread(void);
  21. #endif
  22. #ifdef RT_USING_LWP
  23. #include <lwp.h>
  24. #include <lwp_arch.h>
  25. #ifdef LWP_USING_CORE_DUMP
  26. #include <lwp_core_dump.h>
  27. #endif
  28. static void _check_fault(struct rt_hw_exp_stack *regs, uint32_t pc_adj, char *info)
  29. {
  30. uint32_t is_user_fault;
  31. rt_thread_t th;
  32. is_user_fault = !(regs->cpsr & 0x1f);
  33. if (is_user_fault)
  34. {
  35. rt_kprintf("%s! pc = 0x%x\n", info, regs->pc - pc_adj);
  36. }
  37. /* user stack backtrace */
  38. th = rt_thread_self();
  39. if (th && th->lwp)
  40. {
  41. arch_backtrace_uthread(th);
  42. }
  43. if (is_user_fault)
  44. {
  45. #ifdef LWP_USING_CORE_DUMP
  46. lwp_core_dump(regs, pc_adj);
  47. #endif
  48. sys_exit_group(-1);
  49. }
  50. }
  51. rt_inline int _get_type(unsigned long esr)
  52. {
  53. int ret;
  54. int fsc = esr & 0x3f;
  55. switch (fsc)
  56. {
  57. case 0x4:
  58. case 0x5:
  59. case 0x6:
  60. case 0x7:
  61. ret = MM_FAULT_TYPE_PAGE_FAULT;
  62. break;
  63. case 0xc:
  64. case 0xd:
  65. case 0xe:
  66. case 0xf:
  67. ret = MM_FAULT_TYPE_ACCESS_FAULT;
  68. break;
  69. case 0x8:
  70. case 0x9:
  71. case 0xa:
  72. case 0xb:
  73. /* access flag fault */
  74. default:
  75. ret = MM_FAULT_TYPE_GENERIC;
  76. }
  77. return ret;
  78. }
  79. rt_inline long _irq_is_disable(long cpsr)
  80. {
  81. return !!(cpsr & 0x80);
  82. }
  83. static int user_fault_fixable(unsigned long esr, struct rt_hw_exp_stack *regs)
  84. {
  85. rt_ubase_t level;
  86. unsigned char ec;
  87. void *dfar;
  88. int ret = 0;
  89. ec = (unsigned char)((esr >> 26) & 0x3fU);
  90. enum rt_mm_fault_op fault_op;
  91. enum rt_mm_fault_type fault_type;
  92. struct rt_lwp *lwp;
  93. switch (ec)
  94. {
  95. case 0x20:
  96. fault_op = MM_FAULT_OP_EXECUTE;
  97. fault_type = _get_type(esr);
  98. break;
  99. case 0x21:
  100. case 0x24:
  101. case 0x25:
  102. fault_op = MM_FAULT_OP_WRITE;
  103. fault_type = _get_type(esr);
  104. break;
  105. default:
  106. fault_op = 0;
  107. break;
  108. }
  109. /* page fault exception only allow from user space */
  110. lwp = lwp_self();
  111. if (lwp && fault_op)
  112. {
  113. __asm__ volatile("mrs %0, far_el1":"=r"(dfar));
  114. struct rt_aspace_fault_msg msg = {
  115. .fault_op = fault_op,
  116. .fault_type = fault_type,
  117. .fault_vaddr = dfar,
  118. };
  119. lwp_user_setting_save(rt_thread_self());
  120. __asm__ volatile("mrs %0, daif\nmsr daifclr, 0x3\nisb\n":"=r"(level));
  121. if (rt_aspace_fault_try_fix(lwp->aspace, &msg))
  122. {
  123. ret = 1;
  124. }
  125. __asm__ volatile("msr daif, %0\nisb\n"::"r"(level));
  126. }
  127. return ret;
  128. }
  129. #endif
  130. /**
  131. * this function will show registers of CPU
  132. *
  133. * @param regs the registers point
  134. */
  135. void rt_hw_show_register(struct rt_hw_exp_stack *regs)
  136. {
  137. rt_kprintf("Execption:\n");
  138. rt_kprintf("X00:0x%16.16p X01:0x%16.16p X02:0x%16.16p X03:0x%16.16p\n", (void *)regs->x0, (void *)regs->x1, (void *)regs->x2, (void *)regs->x3);
  139. rt_kprintf("X04:0x%16.16p X05:0x%16.16p X06:0x%16.16p X07:0x%16.16p\n", (void *)regs->x4, (void *)regs->x5, (void *)regs->x6, (void *)regs->x7);
  140. rt_kprintf("X08:0x%16.16p X09:0x%16.16p X10:0x%16.16p X11:0x%16.16p\n", (void *)regs->x8, (void *)regs->x9, (void *)regs->x10, (void *)regs->x11);
  141. rt_kprintf("X12:0x%16.16p X13:0x%16.16p X14:0x%16.16p X15:0x%16.16p\n", (void *)regs->x12, (void *)regs->x13, (void *)regs->x14, (void *)regs->x15);
  142. rt_kprintf("X16:0x%16.16p X17:0x%16.16p X18:0x%16.16p X19:0x%16.16p\n", (void *)regs->x16, (void *)regs->x17, (void *)regs->x18, (void *)regs->x19);
  143. rt_kprintf("X20:0x%16.16p X21:0x%16.16p X22:0x%16.16p X23:0x%16.16p\n", (void *)regs->x20, (void *)regs->x21, (void *)regs->x22, (void *)regs->x23);
  144. rt_kprintf("X24:0x%16.16p X25:0x%16.16p X26:0x%16.16p X27:0x%16.16p\n", (void *)regs->x24, (void *)regs->x25, (void *)regs->x26, (void *)regs->x27);
  145. rt_kprintf("X28:0x%16.16p X29:0x%16.16p X30:0x%16.16p\n", (void *)regs->x28, (void *)regs->x29, (void *)regs->x30);
  146. rt_kprintf("SP_EL0:0x%16.16p\n", (void *)regs->sp_el0);
  147. rt_kprintf("SPSR :0x%16.16p\n", (void *)regs->cpsr);
  148. rt_kprintf("EPC :0x%16.16p\n", (void *)regs->pc);
  149. }
  150. #ifndef RT_USING_PIC
  151. void rt_hw_trap_irq(void)
  152. {
  153. #ifdef SOC_BCM283x
  154. extern rt_uint8_t core_timer_flag;
  155. void *param;
  156. uint32_t irq;
  157. rt_isr_handler_t isr_func;
  158. extern struct rt_irq_desc isr_table[];
  159. uint32_t value = 0;
  160. value = IRQ_PEND_BASIC & 0x3ff;
  161. if(core_timer_flag != 0)
  162. {
  163. uint32_t cpu_id = rt_hw_cpu_id();
  164. uint32_t int_source = CORE_IRQSOURCE(cpu_id);
  165. if (int_source & 0x0f)
  166. {
  167. if (int_source & 0x08)
  168. {
  169. isr_func = isr_table[IRQ_ARM_TIMER].handler;
  170. #ifdef RT_USING_INTERRUPT_INFO
  171. isr_table[IRQ_ARM_TIMER].counter++;
  172. #endif
  173. if (isr_func)
  174. {
  175. param = isr_table[IRQ_ARM_TIMER].param;
  176. isr_func(IRQ_ARM_TIMER, param);
  177. }
  178. }
  179. }
  180. }
  181. /* local interrupt*/
  182. if (value)
  183. {
  184. if (value & (1 << 8))
  185. {
  186. value = IRQ_PEND1;
  187. irq = __rt_ffs(value) - 1;
  188. }
  189. else if (value & (1 << 9))
  190. {
  191. value = IRQ_PEND2;
  192. irq = __rt_ffs(value) + 31;
  193. }
  194. else
  195. {
  196. value &= 0x0f;
  197. irq = __rt_ffs(value) + 63;
  198. }
  199. /* get interrupt service routine */
  200. isr_func = isr_table[irq].handler;
  201. #ifdef RT_USING_INTERRUPT_INFO
  202. isr_table[irq].counter++;
  203. #endif
  204. if (isr_func)
  205. {
  206. /* Interrupt for myself. */
  207. param = isr_table[irq].param;
  208. /* turn to interrupt service routine */
  209. isr_func(irq, param);
  210. }
  211. }
  212. #else
  213. void *param;
  214. int ir, ir_self;
  215. rt_isr_handler_t isr_func;
  216. extern struct rt_irq_desc isr_table[];
  217. ir = rt_hw_interrupt_get_irq();
  218. if (ir == 1023)
  219. {
  220. /* Spurious interrupt */
  221. return;
  222. }
  223. /* bit 10~12 is cpuid, bit 0~9 is interrupt id */
  224. ir_self = ir & 0x3ffUL;
  225. /* get interrupt service routine */
  226. isr_func = isr_table[ir_self].handler;
  227. #ifdef RT_USING_INTERRUPT_INFO
  228. isr_table[ir_self].counter++;
  229. #ifdef RT_USING_SMP
  230. isr_table[ir_self].cpu_counter[rt_hw_cpu_id()]++;
  231. #endif
  232. #endif
  233. if (isr_func)
  234. {
  235. /* Interrupt for myself. */
  236. param = isr_table[ir_self].param;
  237. /* turn to interrupt service routine */
  238. isr_func(ir_self, param);
  239. }
  240. /* end of interrupt */
  241. rt_hw_interrupt_ack(ir);
  242. #endif
  243. }
  244. #else
  245. void rt_hw_trap_irq(void)
  246. {
  247. rt_pic_do_traps();
  248. }
  249. #endif
  250. #ifdef RT_USING_SMART
  251. #define DBG_CHECK_EVENT(regs, esr) dbg_check_event(regs, esr)
  252. #else
  253. #define DBG_CHECK_EVENT(regs, esr) (0)
  254. #endif
  255. #ifndef RT_USING_PIC
  256. void rt_hw_trap_fiq(void)
  257. {
  258. void *param;
  259. int ir, ir_self;
  260. rt_isr_handler_t isr_func;
  261. extern struct rt_irq_desc isr_table[];
  262. ir = rt_hw_interrupt_get_irq();
  263. /* bit 10~12 is cpuid, bit 0~9 is interrup id */
  264. ir_self = ir & 0x3ffUL;
  265. /* get interrupt service routine */
  266. isr_func = isr_table[ir_self].handler;
  267. param = isr_table[ir_self].param;
  268. /* turn to interrupt service routine */
  269. isr_func(ir_self, param);
  270. /* end of interrupt */
  271. rt_hw_interrupt_ack(ir);
  272. }
  273. #else
  274. void rt_hw_trap_fiq(void)
  275. {
  276. rt_pic_do_traps();
  277. }
  278. #endif
  279. void print_exception(unsigned long esr, unsigned long epc);
  280. void SVC_Handler(struct rt_hw_exp_stack *regs);
  281. void rt_hw_trap_exception(struct rt_hw_exp_stack *regs)
  282. {
  283. unsigned long esr;
  284. unsigned char ec;
  285. asm volatile("mrs %0, esr_el1":"=r"(esr));
  286. ec = (unsigned char)((esr >> 26) & 0x3fU);
  287. if (DBG_CHECK_EVENT(regs, esr))
  288. {
  289. return;
  290. }
  291. else if (ec == 0x15) /* is 64bit syscall ? */
  292. {
  293. SVC_Handler(regs);
  294. /* never return here */
  295. }
  296. #ifdef RT_USING_SMART
  297. /**
  298. * Note: check_user_stack will take lock and it will possibly be a dead-lock
  299. * if exception comes from kernel.
  300. */
  301. if ((regs->cpsr & 0x1f) == 0)
  302. {
  303. if (user_fault_fixable(esr, regs))
  304. return;
  305. }
  306. else
  307. {
  308. if (_irq_is_disable(regs->cpsr))
  309. {
  310. LOG_E("Kernel fault from interrupt/critical section");
  311. }
  312. if (rt_critical_level() != 0)
  313. {
  314. LOG_E("scheduler is not available");
  315. }
  316. else if (user_fault_fixable(esr, regs))
  317. return;
  318. }
  319. #endif
  320. print_exception(esr, regs->pc);
  321. rt_hw_show_register(regs);
  322. LOG_E("current thread: %s\n", rt_thread_self()->parent.name);
  323. #ifdef RT_USING_FINSH
  324. list_thread();
  325. #endif
  326. #ifdef RT_USING_LWP
  327. /* restore normal execution environment */
  328. __asm__ volatile("msr daifclr, 0x3\ndmb ishst\nisb\n");
  329. _check_fault(regs, 0, "user fault");
  330. #endif
  331. struct rt_hw_backtrace_frame frame = {.fp = regs->x29, .pc = regs->pc};
  332. rt_backtrace_frame(&frame);
  333. rt_hw_cpu_shutdown();
  334. }
  335. void rt_hw_trap_serror(struct rt_hw_exp_stack *regs)
  336. {
  337. rt_kprintf("SError\n");
  338. rt_hw_show_register(regs);
  339. rt_kprintf("current: %s\n", rt_thread_self()->parent.name);
  340. #ifdef RT_USING_FINSH
  341. list_thread();
  342. #endif
  343. rt_hw_cpu_shutdown();
  344. }