debug_stubs.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // This module implements debug/trace stubs. The stub is a piece of special code which can invoked by OpenOCD
  7. // Currently one stub is used for GCOV functionality
  8. //
  9. #include "esp_private/dbg_stubs.h"
  10. #include "esp_attr.h"
  11. /*
  12. Debug stubs is actually a table of 4-byte entries. Every entry is equal to zero or must contain meaningfull data.
  13. The first entry is a service one and has the followinf format:
  14. - tramp_addr, 4 bytes; Address of buffer for trampoline/code. Max size is ESP_DBG_STUBS_CODE_BUF_SIZE.
  15. - min_stack_addr, 4 bytes; Start of the buffer for minimal onboard stack or data. Max size is ESP_DBG_STUBS_STACK_MIN_SIZE.
  16. - data_alloc, 4 bytes; Address of function to allocate memory on target.
  17. - data_free, 4 bytes; Address of function to free target memory.
  18. This entry is used by OpenOCD code to invoke other stub entries and allocate memory for them.
  19. */
  20. #include "esp_log.h"
  21. const static char *TAG = "esp_dbg_stubs";
  22. #define ESP_DBG_STUBS_CODE_BUF_SIZE 32
  23. #define ESP_DBG_STUBS_STACK_MIN_SIZE 2048
  24. #define DBG_STUB_TRAMP_ATTR IRAM_ATTR
  25. static struct {
  26. uint32_t tramp_addr;
  27. uint32_t min_stack_addr; // minimal stack addr
  28. uint32_t data_alloc;
  29. uint32_t data_free;
  30. } s_dbg_stubs_ctl_data;
  31. static uint32_t s_stub_entry[ESP_DBG_STUB_ENTRY_MAX];
  32. static uint8_t s_stub_min_stack[ESP_DBG_STUBS_STACK_MIN_SIZE];
  33. static DBG_STUB_TRAMP_ATTR uint8_t s_stub_code_buf[ESP_DBG_STUBS_CODE_BUF_SIZE];
  34. extern void esp_dbg_stubs_ll_init(void *stub_table_addr);
  35. // TODO: all called funcs should be in IRAM to work with disabled flash cache
  36. static void * esp_dbg_stubs_data_alloc(uint32_t size)
  37. {
  38. ESP_LOGV(TAG, "%s %d", __func__, size);
  39. void *p = malloc(size);
  40. ESP_LOGV(TAG, "%s EXIT %p", __func__, p);
  41. return p;
  42. }
  43. static void esp_dbg_stubs_data_free(void *addr)
  44. {
  45. ESP_LOGV(TAG, "%s %p", __func__, addr);
  46. free(addr);
  47. ESP_LOGV(TAG, "%s EXIT %p", __func__, addr);
  48. }
  49. void esp_dbg_stubs_init(void)
  50. {
  51. s_dbg_stubs_ctl_data.tramp_addr = (uint32_t)s_stub_code_buf;
  52. s_dbg_stubs_ctl_data.min_stack_addr = (uint32_t)s_stub_min_stack;
  53. s_dbg_stubs_ctl_data.data_alloc = (uint32_t)esp_dbg_stubs_data_alloc;
  54. s_dbg_stubs_ctl_data.data_free = (uint32_t)esp_dbg_stubs_data_free;
  55. s_stub_entry[ESP_DBG_STUB_MAGIC_NUM] = ESP_DBG_STUB_MAGIC_NUM_VAL;
  56. s_stub_entry[ESP_DBG_STUB_TABLE_SIZE] = ESP_DBG_STUB_ENTRY_MAX;
  57. s_stub_entry[ESP_DBG_STUB_CONTROL_DATA] = (uint32_t)&s_dbg_stubs_ctl_data;
  58. esp_dbg_stubs_ll_init(s_stub_entry);
  59. }
  60. // TODO: add lock mechanism. Not now but in the future ESP_DBG_STUB_ENTRY_CAPABILITIES can be set from different places.
  61. esp_err_t esp_dbg_stub_entry_set(esp_dbg_stub_id_t id, uint32_t entry)
  62. {
  63. if (id < ESP_DBG_STUB_ENTRY_FIRST || id >= ESP_DBG_STUB_ENTRY_MAX) {
  64. ESP_LOGE(TAG, "Invalid stub id %d!", id);
  65. return ESP_ERR_INVALID_ARG;
  66. }
  67. s_stub_entry[id] = entry;
  68. return ESP_OK;
  69. }
  70. esp_err_t esp_dbg_stub_entry_get(esp_dbg_stub_id_t id, uint32_t *entry)
  71. {
  72. if (id < ESP_DBG_STUB_ENTRY_FIRST || id >= ESP_DBG_STUB_ENTRY_MAX) {
  73. ESP_LOGE(TAG, "Invalid stub id %d!", id);
  74. return ESP_ERR_INVALID_ARG;
  75. }
  76. *entry = s_stub_entry[id];
  77. return ESP_OK;
  78. }