dump.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _BSP_DUMP_H
  16. #define _BSP_DUMP_H
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "syslog.h"
  20. #include "uarths.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #define DUMP_PRINTF printk
  25. static inline void
  26. dump_core(const char *reason, uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t fregs[32])
  27. {
  28. static const char *const reg_usage[][2] =
  29. {
  30. {"zero ", "Hard-wired zero"},
  31. {"ra ", "Return address"},
  32. {"sp ", "Stack pointer"},
  33. {"gp ", "Global pointer"},
  34. {"tp ", "Thread pointer"},
  35. {"t0 ", "Temporaries Caller"},
  36. {"t1 ", "Temporaries Caller"},
  37. {"t2 ", "Temporaries Caller"},
  38. {"s0/fp", "Saved register/frame pointer"},
  39. {"s1 ", "Saved register"},
  40. {"a0 ", "Function arguments/return values"},
  41. {"a1 ", "Function arguments/return values"},
  42. {"a2 ", "Function arguments values"},
  43. {"a3 ", "Function arguments values"},
  44. {"a4 ", "Function arguments values"},
  45. {"a5 ", "Function arguments values"},
  46. {"a6 ", "Function arguments values"},
  47. {"a7 ", "Function arguments values"},
  48. {"s2 ", "Saved registers"},
  49. {"s3 ", "Saved registers"},
  50. {"s4 ", "Saved registers"},
  51. {"s5 ", "Saved registers"},
  52. {"s6 ", "Saved registers"},
  53. {"s7 ", "Saved registers"},
  54. {"s8 ", "Saved registers"},
  55. {"s9 ", "Saved registers"},
  56. {"s10 ", "Saved registers"},
  57. {"s11 ", "Saved registers"},
  58. {"t3 ", "Temporaries Caller"},
  59. {"t4 ", "Temporaries Caller"},
  60. {"t5 ", "Temporaries Caller"},
  61. {"t6 ", "Temporaries Caller"},
  62. };
  63. static const char *const regf_usage[][2] =
  64. {
  65. {"ft0 ", "FP temporaries"},
  66. {"ft1 ", "FP temporaries"},
  67. {"ft2 ", "FP temporaries"},
  68. {"ft3 ", "FP temporaries"},
  69. {"ft4 ", "FP temporaries"},
  70. {"ft5 ", "FP temporaries"},
  71. {"ft6 ", "FP temporaries"},
  72. {"ft7 ", "FP temporaries"},
  73. {"fs0 ", "FP saved registers"},
  74. {"fs1 ", "FP saved registers"},
  75. {"fa0 ", "FP arguments/return values"},
  76. {"fa1 ", "FP arguments/return values"},
  77. {"fa2 ", "FP arguments values"},
  78. {"fa3 ", "FP arguments values"},
  79. {"fa4 ", "FP arguments values"},
  80. {"fa5 ", "FP arguments values"},
  81. {"fa6 ", "FP arguments values"},
  82. {"fa7 ", "FP arguments values"},
  83. {"fs2 ", "FP Saved registers"},
  84. {"fs3 ", "FP Saved registers"},
  85. {"fs4 ", "FP Saved registers"},
  86. {"fs5 ", "FP Saved registers"},
  87. {"fs6 ", "FP Saved registers"},
  88. {"fs7 ", "FP Saved registers"},
  89. {"fs8 ", "FP Saved registers"},
  90. {"fs9 ", "FP Saved registers"},
  91. {"fs10", "FP Saved registers"},
  92. {"fs11", "FP Saved registers"},
  93. {"ft8 ", "FP Temporaries Caller"},
  94. {"ft9 ", "FP Temporaries Caller"},
  95. {"ft10", "FP Temporaries Caller"},
  96. {"ft11", "FP Temporaries Caller"},
  97. };
  98. if(CONFIG_LOG_LEVEL >= LOG_ERROR)
  99. {
  100. const char unknown_reason[] = "unknown";
  101. if(!reason)
  102. reason = unknown_reason;
  103. DUMP_PRINTF("core dump: %s\r\n", reason);
  104. DUMP_PRINTF("Cause 0x%016lx, EPC 0x%016lx\r\n", cause, epc);
  105. int i = 0;
  106. for(i = 0; i < 32 / 2; i++)
  107. {
  108. DUMP_PRINTF(
  109. "reg[%02d](%s) = 0x%016lx, reg[%02d](%s) = 0x%016lx\r\n",
  110. i * 2, reg_usage[i * 2][0], regs[i * 2],
  111. i * 2 + 1, reg_usage[i * 2 + 1][0], regs[i * 2 + 1]);
  112. }
  113. for(i = 0; i < 32 / 2; i++)
  114. {
  115. DUMP_PRINTF(
  116. "freg[%02d](%s) = 0x%016lx(%f), freg[%02d](%s) = 0x%016lx(%f)\r\n",
  117. i * 2, regf_usage[i * 2][0], fregs[i * 2], (float)fregs[i * 2],
  118. i * 2 + 1, regf_usage[i * 2 + 1][0], fregs[i * 2 + 1], (float)fregs[i * 2 + 1]);
  119. }
  120. }
  121. }
  122. #undef DUMP_PRINTF
  123. #ifdef __cplusplus
  124. }
  125. #endif
  126. #endif /* _BSP_DUMP_H */