xtensa.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #
  2. # Copyright 2021 Espressif Systems (Shanghai) PTE LTD
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from construct import Int16ul, Int32ul, Int64ul, Struct
  17. from . import ESPCoreDumpLoaderError, _ArchMethodsBase, _TargetMethodsBase
  18. INVALID_CAUSE_VALUE = 0xFFFF
  19. XCHAL_EXCCAUSE_NUM = 64
  20. # Exception cause dictionary to get translation of exccause register
  21. # From 4.4.1.5 table 4-64 Exception Causes of Xtensa
  22. # Instruction Set Architecture (ISA) Reference Manual
  23. XTENSA_EXCEPTION_CAUSE_DICT = {
  24. 0: ('IllegalInstructionCause', 'Illegal instruction'),
  25. 1: ('SyscallCause', 'SYSCALL instruction'),
  26. 2: ('InstructionFetchErrorCause',
  27. 'Processor internal physical address or data error during instruction fetch. (See EXCVADDR for more information)'),
  28. 3: ('LoadStoreErrorCause',
  29. 'Processor internal physical address or data error during load or store. (See EXCVADDR for more information)'),
  30. 4: ('Level1InterruptCause', 'Level-1 interrupt as indicated by set level-1 bits in the INTERRUPT register'),
  31. 5: ('AllocaCause', 'MOVSP instruction, if caller`s registers are not in the register file'),
  32. 6: ('IntegerDivideByZeroCause', 'QUOS: QUOU, REMS: or REMU divisor operand is zero'),
  33. 8: ('PrivilegedCause', 'Attempt to execute a privileged operation when CRING ? 0'),
  34. 9: ('LoadStoreAlignmentCause', 'Load or store to an unaligned address. (See EXCVADDR for more information)'),
  35. 12: ('InstrPIFDataErrorCause', 'PIF data error during instruction fetch. (See EXCVADDR for more information)'),
  36. 13: ('LoadStorePIFDataErrorCause',
  37. 'Synchronous PIF data error during LoadStore access. (See EXCVADDR for more information)'),
  38. 14: ('InstrPIFAddrErrorCause', 'PIF address error during instruction fetch. (See EXCVADDR for more information)'),
  39. 15: ('LoadStorePIFAddrErrorCause',
  40. 'Synchronous PIF address error during LoadStore access. (See EXCVADDR for more information)'),
  41. 16: ('InstTLBMissCause', 'Error during Instruction TLB refill. (See EXCVADDR for more information)'),
  42. 17: ('InstTLBMultiHitCause', 'Multiple instruction TLB entries matched. (See EXCVADDR for more information)'),
  43. 18: ('InstFetchPrivilegeCause',
  44. 'An instruction fetch referenced a virtual address at a ring level less than CRING. (See EXCVADDR for more information)'),
  45. 20: ('InstFetchProhibitedCause',
  46. 'An instruction fetch referenced a page mapped with an attribute that does not permit instruction fetch (EXCVADDR).'),
  47. 24: ('LoadStoreTLBMissCause', 'Error during TLB refill for a load or store. (See EXCVADDR for more information)'),
  48. 25: ('LoadStoreTLBMultiHitCause',
  49. 'Multiple TLB entries matched for a load or store. (See EXCVADDR for more information)'),
  50. 26: ('LoadStorePrivilegeCause',
  51. 'A load or store referenced a virtual address at a ring level less than CRING. (See EXCVADDR for more information)'),
  52. 28: ('LoadProhibitedCause',
  53. 'A load referenced a page mapped with an attribute that does not permit loads. (See EXCVADDR for more information)'),
  54. 29: ('StoreProhibitedCause',
  55. 'A store referenced a page mapped with an attribute that does not permit stores [Region Protection Option or MMU Option].'),
  56. 32: ('Coprocessor0Disabled', 'Coprocessor 0 instruction when cp0 disabled'),
  57. 33: ('Coprocessor1Disabled', 'Coprocessor 1 instruction when cp1 disabled'),
  58. 34: ('Coprocessor2Disabled', 'Coprocessor 2 instruction when cp2 disabled'),
  59. 35: ('Coprocessor3Disabled', 'Coprocessor 3 instruction when cp3 disabled'),
  60. 36: ('Coprocessor4Disabled', 'Coprocessor 4 instruction when cp4 disabled'),
  61. 37: ('Coprocessor5Disabled', 'Coprocessor 5 instruction when cp5 disabled'),
  62. 38: ('Coprocessor6Disabled', 'Coprocessor 6 instruction when cp6 disabled'),
  63. 39: ('Coprocessor7Disabled', 'Coprocessor 7 instruction when cp7 disabled'),
  64. INVALID_CAUSE_VALUE: (
  65. 'InvalidCauseRegister', 'Invalid EXCCAUSE register value or current task is broken and was skipped'),
  66. # ESP panic pseudo reasons
  67. XCHAL_EXCCAUSE_NUM + 0: ('UnknownException', 'Unknown exception'),
  68. XCHAL_EXCCAUSE_NUM + 1: ('DebugException', 'Unhandled debug exception'),
  69. XCHAL_EXCCAUSE_NUM + 2: ('DoubleException', 'Double exception'),
  70. XCHAL_EXCCAUSE_NUM + 3: ('KernelException', 'Unhandled kernel exception'),
  71. XCHAL_EXCCAUSE_NUM + 4: ('CoprocessorException', 'Coprocessor exception'),
  72. XCHAL_EXCCAUSE_NUM + 5: ('InterruptWDTTimoutCPU0', 'Interrupt wdt timeout on CPU0'),
  73. XCHAL_EXCCAUSE_NUM + 6: ('InterruptWDTTimoutCPU1', 'Interrupt wdt timeout on CPU1'),
  74. XCHAL_EXCCAUSE_NUM + 7: ('CacheError', 'Cache disabled but cached memory region accessed'),
  75. }
  76. class XtensaRegisters(object):
  77. # extra regs IDs used in EXTRA_INFO note
  78. EXCCAUSE_IDX = 0
  79. EXCVADDR_IDX = 1
  80. EPC1_IDX = 177
  81. EPC2_IDX = 178
  82. EPC3_IDX = 179
  83. EPC4_IDX = 180
  84. EPC5_IDX = 181
  85. EPC6_IDX = 182
  86. EPC7_IDX = 183
  87. EPS2_IDX = 194
  88. EPS3_IDX = 195
  89. EPS4_IDX = 196
  90. EPS5_IDX = 197
  91. EPS6_IDX = 198
  92. EPS7_IDX = 199
  93. @property
  94. def registers(self):
  95. return {k: v for k, v in self.__class__.__dict__.items()
  96. if not k.startswith('__') and isinstance(v, int)}
  97. # Following structs are based on source code
  98. # IDF_PATH/components/espcoredump/src/core_dump_port.c
  99. XtensaPrStatus = Struct(
  100. 'si_signo' / Int32ul,
  101. 'si_code' / Int32ul,
  102. 'si_errno' / Int32ul,
  103. 'pr_cursig' / Int16ul,
  104. 'pr_pad0' / Int16ul,
  105. 'pr_sigpend' / Int32ul,
  106. 'pr_sighold' / Int32ul,
  107. 'pr_pid' / Int32ul,
  108. 'pr_ppid' / Int32ul,
  109. 'pr_pgrp' / Int32ul,
  110. 'pr_sid' / Int32ul,
  111. 'pr_utime' / Int64ul,
  112. 'pr_stime' / Int64ul,
  113. 'pr_cutime' / Int64ul,
  114. 'pr_cstime' / Int64ul,
  115. )
  116. def print_exc_regs_info(extra_info):
  117. """
  118. Print the register info by parsing extra_info
  119. :param extra_info: extra info data str
  120. :return: None
  121. """
  122. exccause = extra_info[1 + 2 * XtensaRegisters.EXCCAUSE_IDX + 1]
  123. exccause_str = XTENSA_EXCEPTION_CAUSE_DICT.get(exccause)
  124. if not exccause_str:
  125. exccause_str = ('Invalid EXCCAUSE code', 'Invalid EXCAUSE description or not found.')
  126. print('exccause 0x%x (%s)' % (exccause, exccause_str[0]))
  127. print('excvaddr 0x%x' % extra_info[1 + 2 * XtensaRegisters.EXCVADDR_IDX + 1])
  128. # skip crashed_task_tcb, exccause, and excvaddr
  129. for i in range(5, len(extra_info), 2):
  130. if (extra_info[i] >= XtensaRegisters.EPC1_IDX and extra_info[i] <= XtensaRegisters.EPC7_IDX):
  131. print('epc%d 0x%x' % ((extra_info[i] - XtensaRegisters.EPC1_IDX + 1), extra_info[i + 1]))
  132. # skip crashed_task_tcb, exccause, and excvaddr
  133. for i in range(5, len(extra_info), 2):
  134. if (extra_info[i] >= XtensaRegisters.EPS2_IDX and extra_info[i] <= XtensaRegisters.EPS7_IDX):
  135. print('eps%d 0x%x' % ((extra_info[i] - XtensaRegisters.EPS2_IDX + 2), extra_info[i + 1]))
  136. # from "gdb/xtensa-tdep.h"
  137. # typedef struct
  138. # {
  139. # 0 xtensa_elf_greg_t pc;
  140. # 1 xtensa_elf_greg_t ps;
  141. # 2 xtensa_elf_greg_t lbeg;
  142. # 3 xtensa_elf_greg_t lend;
  143. # 4 xtensa_elf_greg_t lcount;
  144. # 5 xtensa_elf_greg_t sar;
  145. # 6 xtensa_elf_greg_t windowstart;
  146. # 7 xtensa_elf_greg_t windowbase;
  147. # 8..63 xtensa_elf_greg_t reserved[8+48];
  148. # 64 xtensa_elf_greg_t ar[64];
  149. # } xtensa_elf_gregset_t;
  150. REG_PC_IDX = 0
  151. REG_PS_IDX = 1
  152. REG_LB_IDX = 2
  153. REG_LE_IDX = 3
  154. REG_LC_IDX = 4
  155. REG_SAR_IDX = 5
  156. # REG_WS_IDX = 6
  157. # REG_WB_IDX = 7
  158. REG_AR_START_IDX = 64
  159. # REG_AR_NUM = 64
  160. # FIXME: acc to xtensa_elf_gregset_t number of regs must be 128,
  161. # but gdb complains when it less then 129
  162. REG_NUM = 129
  163. # XT_SOL_EXIT = 0
  164. XT_SOL_PC = 1
  165. XT_SOL_PS = 2
  166. # XT_SOL_NEXT = 3
  167. XT_SOL_AR_START = 4
  168. XT_SOL_AR_NUM = 4
  169. # XT_SOL_FRMSZ = 8
  170. XT_STK_EXIT = 0
  171. XT_STK_PC = 1
  172. XT_STK_PS = 2
  173. XT_STK_AR_START = 3
  174. XT_STK_AR_NUM = 16
  175. XT_STK_SAR = 19
  176. XT_STK_EXCCAUSE = 20
  177. XT_STK_EXCVADDR = 21
  178. XT_STK_LBEG = 22
  179. XT_STK_LEND = 23
  180. XT_STK_LCOUNT = 24
  181. XT_STK_FRMSZ = 25
  182. class _TargetMethodsESP32(_TargetMethodsBase):
  183. @staticmethod
  184. def tcb_is_sane(tcb_addr, tcb_size):
  185. return not (tcb_addr < 0x3ffae000 or (tcb_addr + tcb_size) > 0x40000000)
  186. @staticmethod
  187. def stack_is_sane(sp):
  188. return not (sp < 0x3ffae010 or sp > 0x3fffffff)
  189. @staticmethod
  190. def addr_is_fake(addr):
  191. return (0x20000000 <= addr < 0x3f3fffff) or addr >= 0x80000000
  192. class _ArchMethodsXtensa(_ArchMethodsBase):
  193. @staticmethod
  194. def get_registers_from_stack(data, grows_down):
  195. extra_regs = {v: 0 for v in XtensaRegisters().registers.values()}
  196. regs = [0] * REG_NUM
  197. # TODO: support for growing up stacks
  198. if not grows_down:
  199. raise ESPCoreDumpLoaderError('Growing up stacks are not supported for now!')
  200. ex_struct = Struct(
  201. 'stack' / Int32ul[XT_STK_FRMSZ]
  202. )
  203. if len(data) < ex_struct.sizeof():
  204. raise ESPCoreDumpLoaderError('Too small stack to keep frame: %d bytes!' % len(data))
  205. stack = ex_struct.parse(data).stack
  206. # Stack frame type indicator is always the first item
  207. rc = stack[XT_STK_EXIT]
  208. if rc != 0:
  209. regs[REG_PC_IDX] = stack[XT_STK_PC]
  210. regs[REG_PS_IDX] = stack[XT_STK_PS]
  211. for i in range(XT_STK_AR_NUM):
  212. regs[REG_AR_START_IDX + i] = stack[XT_STK_AR_START + i]
  213. regs[REG_SAR_IDX] = stack[XT_STK_SAR]
  214. regs[REG_LB_IDX] = stack[XT_STK_LBEG]
  215. regs[REG_LE_IDX] = stack[XT_STK_LEND]
  216. regs[REG_LC_IDX] = stack[XT_STK_LCOUNT]
  217. # FIXME: crashed and some running tasks (e.g. prvIdleTask) have EXCM bit set
  218. # and GDB can not unwind callstack properly (it implies not windowed call0)
  219. if regs[REG_PS_IDX] & (1 << 5):
  220. regs[REG_PS_IDX] &= ~(1 << 4)
  221. if stack[XT_STK_EXCCAUSE] in XTENSA_EXCEPTION_CAUSE_DICT:
  222. extra_regs[XtensaRegisters.EXCCAUSE_IDX] = stack[XT_STK_EXCCAUSE]
  223. else:
  224. extra_regs[XtensaRegisters.EXCCAUSE_IDX] = INVALID_CAUSE_VALUE
  225. extra_regs[XtensaRegisters.EXCVADDR_IDX] = stack[XT_STK_EXCVADDR]
  226. else:
  227. regs[REG_PC_IDX] = stack[XT_SOL_PC]
  228. regs[REG_PS_IDX] = stack[XT_SOL_PS]
  229. for i in range(XT_SOL_AR_NUM):
  230. regs[REG_AR_START_IDX + i] = stack[XT_SOL_AR_START + i]
  231. # nxt = stack[XT_SOL_NEXT]
  232. return regs, extra_regs
  233. @staticmethod
  234. def build_prstatus_data(tcb_addr, task_regs):
  235. return XtensaPrStatus.build({
  236. 'si_signo': 0,
  237. 'si_code': 0,
  238. 'si_errno': 0,
  239. 'pr_cursig': 0, # TODO: set sig only for current/failed task
  240. 'pr_pad0': 0,
  241. 'pr_sigpend': 0,
  242. 'pr_sighold': 0,
  243. 'pr_pid': tcb_addr,
  244. 'pr_ppid': 0,
  245. 'pr_pgrp': 0,
  246. 'pr_sid': 0,
  247. 'pr_utime': 0,
  248. 'pr_stime': 0,
  249. 'pr_cutime': 0,
  250. 'pr_cstime': 0,
  251. }) + Int32ul[len(task_regs)].build(task_regs)