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