xtensa.py 11 KB

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