killed.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "common.h"
  2. #include "io.h"
  3. #include "sys.h"
  4. #include "console.h"
  5. #include "esp.h"
  6. #include "irq.h"
  7. /* Don't mark no_return or gcc moves it to SDRAM */
  8. static void __hot __text_hot killed(const char *how)
  9. {
  10. /* Cannot use con_printf() here */
  11. const uint16_t *pcp;
  12. size_t mtval;
  13. size_t mepc, meinfo, task;
  14. size_t pc;
  15. /* Try to move back to the previous instruction (wrong for jumps...) */
  16. asm volatile("csrr %0,mepc" : "=r" (mepc));
  17. asm volatile("csrr %0,%1" : "=r" (meinfo) : "i" (MEINFO_CSR));
  18. pc = mepc - (((meinfo & 3) + 1) << 4);
  19. pcp = (const uint16_t *)pc;
  20. con_puts(hotstr("ERROR: "));
  21. con_puts(how);
  22. con_puts(hotstr(" at 0x"));
  23. con_print_hex(pc);
  24. con_puts(hotstr(" (0x"));
  25. con_print_hex((pcp[1] << 16) + pcp[0]);
  26. con_puts(hotstr(")\nBad address: 0x"));
  27. asm volatile("csrr %0,mtval" : "=r" (mtval));
  28. con_print_hex(mtval);
  29. con_puts(hotstr(" task "));
  30. asm volatile("csrr %0,user_context" : "=r" (task));
  31. con_putc('0' + task);
  32. con_putc('\n');
  33. for (int i = 0; i < 32; i += 8) {
  34. for (int j = 0; j < 8; j++) {
  35. uint32_t v = rdxreg(i+j);
  36. con_print_hex(v);
  37. con_putc((j == 7) ? '\n' : ' ');
  38. }
  39. }
  40. con_flush();
  41. udelay(5000000);
  42. reset(SYS_RESET_SOFT);
  43. }
  44. IRQHANDLER(buserr,0)
  45. {
  46. killed(hotstr("misaligned"));
  47. }
  48. IRQHANDLER(ebreak,0)
  49. {
  50. killed(hotstr("invalid instruction"));
  51. }