riscv.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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, Padding, Struct
  17. from corefile import BaseArchMethodsMixin, BaseTargetMethods, ESPCoreDumpLoaderError
  18. try:
  19. from typing import Any, Optional, Tuple
  20. except ImportError:
  21. pass
  22. RISCV_GP_REGS_COUNT = 32
  23. PRSTATUS_SIZE = 204
  24. PRSTATUS_OFFSET_PR_CURSIG = 12
  25. PRSTATUS_OFFSET_PR_PID = 24
  26. PRSTATUS_OFFSET_PR_REG = 72
  27. ELF_GREGSET_T_SIZE = 128
  28. PrStruct = Struct(
  29. Padding(PRSTATUS_OFFSET_PR_CURSIG),
  30. 'pr_cursig' / Int16ul,
  31. Padding(PRSTATUS_OFFSET_PR_PID - PRSTATUS_OFFSET_PR_CURSIG - Int16ul.sizeof()),
  32. 'pr_pid' / Int32ul,
  33. Padding(PRSTATUS_OFFSET_PR_REG - PRSTATUS_OFFSET_PR_PID - Int32ul.sizeof()),
  34. 'regs' / Int32ul[RISCV_GP_REGS_COUNT],
  35. Padding(PRSTATUS_SIZE - PRSTATUS_OFFSET_PR_REG - ELF_GREGSET_T_SIZE)
  36. )
  37. class RiscvMethodsMixin(BaseArchMethodsMixin):
  38. @staticmethod
  39. def get_registers_from_stack(data, grows_down):
  40. # type: (bytes, bool) -> Tuple[list[int], Optional[dict[int, int]]]
  41. regs = Int32ul[RISCV_GP_REGS_COUNT].parse(data)
  42. if not grows_down:
  43. raise ESPCoreDumpLoaderError('Growing up stacks are not supported for now!')
  44. return regs, None
  45. @staticmethod
  46. def build_prstatus_data(tcb_addr, task_regs): # type: (int, list[int]) -> Any
  47. return PrStruct.build({
  48. 'pr_cursig': 0,
  49. 'pr_pid': tcb_addr,
  50. 'regs': task_regs,
  51. })
  52. class Esp32c3Methods(BaseTargetMethods, RiscvMethodsMixin):
  53. TARGET = 'esp32c3'