Skip to content

Commit 7daa16d

Browse files
committed
Merge branch 'refactor/esp_tee_heap_rom' into 'master'
feat(esp_tee): Use the ROM TLSF implementation for the TEE build See merge request espressif/esp-idf!37277
2 parents d11ccf8 + d26e18c commit 7daa16d

File tree

16 files changed

+269
-94
lines changed

16 files changed

+269
-94
lines changed

components/esp_rom/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ endif()
133133
if(ESP_TEE_BUILD)
134134
if(target STREQUAL "esp32c6")
135135
rom_linker_script("spiflash")
136+
rom_linker_script("heap")
136137
endif()
137138
endif()
138139

components/esp_rom/include/esp_rom_tlsf.h

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -11,6 +11,60 @@
1111
extern "C" {
1212
#endif
1313

14+
/* tlsf_t: a TLSF structure. Can contain 1 to N pools. */
15+
/* pool_t: a block of memory that TLSF can manage. */
16+
typedef void* tlsf_t;
17+
typedef void* pool_t;
18+
19+
/* Create/destroy a memory pool. */
20+
tlsf_t tlsf_create(void* mem);
21+
tlsf_t tlsf_create_with_pool(void* mem, size_t bytes);
22+
pool_t tlsf_get_pool(tlsf_t tlsf);
23+
24+
/* Add/remove memory pools. */
25+
pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
26+
void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
27+
28+
/* malloc/memalign/realloc/free replacements. */
29+
void* tlsf_malloc(tlsf_t tlsf, size_t size);
30+
void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size);
31+
void* tlsf_memalign_offs(tlsf_t tlsf, size_t align, size_t size, size_t offset);
32+
void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
33+
void tlsf_free(tlsf_t tlsf, void* ptr);
34+
35+
/* Returns internal block size, not original request size */
36+
size_t tlsf_block_size(void* ptr);
37+
38+
/* Overheads/limits of internal structures. */
39+
size_t tlsf_size(void);
40+
size_t tlsf_pool_overhead(void);
41+
size_t tlsf_alloc_overhead(void);
42+
43+
#if ESP_TEE_BUILD
44+
/* NOTE: These declarations are only needed for the TEE build, since these
45+
* functions are (static inline) defined in tlsf_control_functions.h for
46+
* IDF builds.
47+
*/
48+
size_t tlsf_align_size(void);
49+
size_t tlsf_block_size_min(void);
50+
size_t tlsf_block_size_max(void);
51+
52+
/* NOTE: The consumer of this callback function (tlsf_walk_pool) is patched
53+
* in IDF builds to address issues in the ROM implementation. For TEE build,
54+
* the ROM declarations can be used directly, as heap integrity checking is not
55+
* supported.
56+
*/
57+
typedef void (*tlsf_walker)(void* ptr, size_t size, int used, void* user);
58+
#else
59+
typedef bool (*tlsf_walker)(void* ptr, size_t size, int used, void* user);
60+
#endif
61+
62+
/* Debugging. */
63+
void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user);
64+
/* Returns nonzero if any internal consistency check fails. */
65+
int tlsf_check(tlsf_t tlsf);
66+
int tlsf_check_pool(pool_t pool);
67+
1468
/*!
1569
* Defines the function prototypes for multi_heap_internal_poison_fill_region
1670
* and multi_heap_internal_check_block_poisoning, these two function will

components/esp_rom/patches/esp_rom_tlsf.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -24,10 +24,6 @@
2424
#include "tlsf_block_functions.h"
2525
#include "tlsf_control_functions.h"
2626

27-
/* Definition of types used in TLSF */
28-
typedef void* tlsf_t;
29-
typedef void* pool_t;
30-
3127
static poison_check_pfunc_t s_poison_check_region = NULL;
3228

3329
void tlsf_poison_check_pfunc_set(poison_check_pfunc_t pfunc)
@@ -43,8 +39,6 @@ typedef struct integrity_t
4339
int status;
4440
} integrity_t;
4541

46-
typedef bool (*tlsf_walker)(void* ptr, size_t size, int used, void* user);
47-
4842
static bool integrity_walker(void* ptr, size_t size, int used, void* user)
4943
{
5044
block_header_t* block = block_from_ptr(ptr);

components/esp_tee/subproject/main/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ list(APPEND include "include"
4040
list(APPEND srcs "common/multi_heap.c")
4141

4242
# TLSF implementation for heap
43-
list(APPEND include "${heap_dir}/tlsf"
44-
"${heap_dir}/tlsf/include")
45-
46-
list(APPEND srcs "${heap_dir}/tlsf/tlsf.c")
43+
list(APPEND include "${heap_dir}/tlsf")
4744

4845
# esp_app_desc_t configuration structure for TEE
4946
list(APPEND srcs "common/esp_app_desc_tee.c")

components/esp_tee/subproject/main/common/multi_heap.c

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#include <stdio.h>
7-
#include "tlsf.h"
7+
#include <stdbool.h>
8+
#include "esp_rom_tlsf.h"
89
#include "tlsf_block_functions.h"
910
#include "multi_heap.h"
1011

@@ -34,36 +35,33 @@ static void assert_valid_block(const heap_t *heap, const block_header_t *block)
3435
(uintptr_t)ptr);
3536
}
3637

37-
int tee_heap_register(void *start_ptr, size_t size)
38+
esp_err_t esp_tee_heap_init(void *start_ptr, size_t size)
3839
{
3940
assert(start_ptr);
40-
if (size < (sizeof(heap_t))) {
41-
//Region too small to be a heap.
42-
return -1;
41+
if (size < (tlsf_size() + tlsf_block_size_min() + sizeof(heap_t))) {
42+
// Region too small to be a heap.
43+
return ESP_ERR_INVALID_SIZE;
4344
}
4445

4546
heap_t *result = (heap_t *)start_ptr;
4647
size -= sizeof(heap_t);
4748

48-
/* Do not specify any maximum size for the allocations so that the default configuration is used */
49-
const size_t max_bytes = 0;
50-
51-
result->heap_data = tlsf_create_with_pool(start_ptr + sizeof(heap_t), size, max_bytes);
49+
result->heap_data = tlsf_create_with_pool(start_ptr + sizeof(heap_t), size);
5250
if (result->heap_data == NULL) {
53-
return -1;
51+
return ESP_FAIL;
5452
}
5553

5654
result->lock = NULL;
57-
result->free_bytes = size - tlsf_size(result->heap_data);
55+
result->free_bytes = size - tlsf_size();
5856
result->pool_size = size;
5957
result->minimum_free_bytes = result->free_bytes;
6058

6159
tee_heap = (multi_heap_handle_t)result;
6260

63-
return 0;
61+
return ESP_OK;
6462
}
6563

66-
void *tee_heap_malloc(size_t size)
64+
void *esp_tee_heap_malloc(size_t size)
6765
{
6866
if (tee_heap == NULL || size == 0) {
6967
return NULL;
@@ -81,17 +79,17 @@ void *tee_heap_malloc(size_t size)
8179
return result;
8280
}
8381

84-
void *tee_heap_calloc(size_t n, size_t size)
82+
void *esp_tee_heap_calloc(size_t n, size_t size)
8583
{
8684
size_t reg_size = n * size;
87-
void *ptr = tee_heap_malloc(reg_size);
85+
void *ptr = esp_tee_heap_malloc(reg_size);
8886
if (ptr != NULL) {
8987
memset(ptr, 0x00, reg_size);
9088
}
9189
return ptr;
9290
}
9391

94-
void *tee_heap_aligned_alloc(size_t size, size_t alignment)
92+
void *esp_tee_heap_aligned_alloc(size_t size, size_t alignment)
9593
{
9694
if (tee_heap == NULL || size == 0) {
9795
return NULL;
@@ -114,7 +112,7 @@ void *tee_heap_aligned_alloc(size_t size, size_t alignment)
114112
return result;
115113
}
116114

117-
void tee_heap_free(void *p)
115+
void esp_tee_heap_free(void *p)
118116
{
119117
if (tee_heap == NULL || p == NULL) {
120118
return;
@@ -129,67 +127,64 @@ void tee_heap_free(void *p)
129127

130128
void *malloc(size_t size)
131129
{
132-
return tee_heap_malloc(size);
130+
return esp_tee_heap_malloc(size);
133131
}
134132

135133
void *calloc(size_t n, size_t size)
136134
{
137-
return tee_heap_calloc(n, size);
135+
return esp_tee_heap_calloc(n, size);
138136
}
139137

140138
void free(void *ptr)
141139
{
142-
tee_heap_free(ptr);
140+
esp_tee_heap_free(ptr);
143141
}
144142

145-
void tee_heap_dump_free_size(void)
143+
size_t esp_tee_heap_get_free_size(void)
146144
{
147-
if (tee_heap == NULL) {
148-
return;
149-
}
150-
printf("Free: %uB | Minimum free: %uB\n", tee_heap->free_bytes, tee_heap->minimum_free_bytes);
145+
return tee_heap->free_bytes;
146+
}
147+
148+
size_t esp_tee_heap_get_min_free_size(void)
149+
{
150+
return tee_heap->minimum_free_bytes;
151151
}
152152

153-
static bool tee_heap_dump_tlsf(void* ptr, size_t size, int used, void* user)
153+
static void heap_dump_tlsf(void* ptr, size_t size, int used, void* user)
154154
{
155155
(void)user;
156156
printf("Block %p data, size: %d bytes, Free: %s\n",
157157
(void *)ptr,
158158
size,
159159
used ? "No" : "Yes");
160-
return true;
161160
}
162161

163-
void tee_heap_dump_info(void)
162+
void esp_tee_heap_dump_info(void)
164163
{
165-
if (tee_heap == NULL) {
166-
return;
167-
}
168-
printf("Showing data for TEE heap: %p\n", (void *)tee_heap);
169-
tee_heap_dump_free_size();
170-
tlsf_walk_pool(tlsf_get_pool(tee_heap->heap_data), tee_heap_dump_tlsf, NULL);
164+
printf("Showing data for TEE heap: %p (%uB)\n", (void *)tee_heap, tee_heap->pool_size);
165+
tlsf_walk_pool(tlsf_get_pool(tee_heap->heap_data), heap_dump_tlsf, NULL);
171166
}
172167

173168
/* Definitions for functions from the heap component, used in files shared with ESP-IDF */
174169

175170
void *heap_caps_malloc(size_t alignment, size_t size, uint32_t caps)
176171
{
177172
(void) caps;
178-
return tee_heap_malloc(size);
173+
return esp_tee_heap_malloc(size);
179174
}
180175

181176
void *heap_caps_aligned_alloc(size_t alignment, size_t size, uint32_t caps)
182177
{
183178
(void) caps;
184-
return tee_heap_aligned_alloc(size, alignment);
179+
return esp_tee_heap_aligned_alloc(size, alignment);
185180
}
186181

187182
void *heap_caps_aligned_calloc(size_t alignment, size_t n, size_t size, uint32_t caps)
188183
{
189184
(void) caps;
190185
uint32_t reg_size = n * size;
191186

192-
void *ptr = tee_heap_aligned_alloc(reg_size, alignment);
187+
void *ptr = esp_tee_heap_aligned_alloc(reg_size, alignment);
193188
if (ptr != NULL) {
194189
memset(ptr, 0x00, reg_size);
195190
}

components/esp_tee/subproject/main/core/esp_tee_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void __attribute__((noreturn)) esp_tee_init(uint32_t ree_entry_addr, uint32_t re
132132
tee_init_app_config();
133133

134134
/* TEE Secure World heap initialization. */
135-
assert(tee_heap_register(((void *)&_tee_heap_start), TEE_HEAP_SIZE) == 0);
135+
assert(esp_tee_heap_init(((void *)&_tee_heap_start), TEE_HEAP_SIZE) == ESP_OK);
136136

137137
/* SoC specific secure initialization. */
138138
esp_tee_soc_secure_sys_init();

0 commit comments

Comments
 (0)