From 4861cd58c3b9878530fe287e9dc9682b3e107130 Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Mon, 6 Jul 2026 13:48:12 +0530 Subject: [PATCH] fix(esp_tee): Miscellaneous optimizations and fixes Closes https://github.com/espressif/esp-idf/issues/18591 --- .../bootloader_flash/src/bootloader_flash.c | 2 +- components/esp_tee/CMakeLists.txt | 2 +- components/esp_tee/subproject/main/common/multi_heap.c | 6 +++++- components/esp_tee/subproject/main/core/esp_tee_init.c | 8 +++++++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/components/bootloader_support/bootloader_flash/src/bootloader_flash.c b/components/bootloader_support/bootloader_flash/src/bootloader_flash.c index f1abd50036d..d85ebcec856 100644 --- a/components/bootloader_support/bootloader_flash/src/bootloader_flash.c +++ b/components/bootloader_support/bootloader_flash/src/bootloader_flash.c @@ -416,8 +416,8 @@ void bootloader_munmap(const void *mapping) mmu_hal_unmap_all(); #else cache_hal_suspend(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); - mmu_hal_unmap_region(0, FLASH_MMAP_VADDR, current_mapped_size); cache_hal_invalidate_addr(FLASH_MMAP_VADDR, current_mapped_size); + mmu_hal_unmap_region(0, FLASH_MMAP_VADDR, current_mapped_size); cache_hal_resume(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); #endif #endif diff --git a/components/esp_tee/CMakeLists.txt b/components/esp_tee/CMakeLists.txt index dcba631e4d3..33dd106bc34 100644 --- a/components/esp_tee/CMakeLists.txt +++ b/components/esp_tee/CMakeLists.txt @@ -9,7 +9,7 @@ idf_build_get_property(target IDF_TARGET) # ESP-TEE is currently supported only on the ESP32-C6, H2, C5 and C61 SoCs set(SUPPORTED_TARGETS "esp32c6" "esp32h2" "esp32c5" "esp32c61") if(NOT target IN_LIST SUPPORTED_TARGETS) - message(STATUS "ESP-TEE is currently supported only on the ${SUPPORTED_TARGETS} SoCs") + # ESP-TEE Kconfig is gated on the supported targets; nothing to register elsewhere. return() endif() diff --git a/components/esp_tee/subproject/main/common/multi_heap.c b/components/esp_tee/subproject/main/common/multi_heap.c index e735ec73037..f84d2bed1a7 100644 --- a/components/esp_tee/subproject/main/common/multi_heap.c +++ b/components/esp_tee/subproject/main/common/multi_heap.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "rom_patch_tlsf.h" #include "esp_rom_sys.h" #include "tlsf_block_functions.h" @@ -63,6 +64,9 @@ esp_err_t esp_tee_heap_init(void *start_ptr, size_t size) return ESP_ERR_INVALID_SIZE; } + /* Zeroize the entire region before registering it as the TEE heap*/ + memset(start_ptr, 0, size); + #if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 void *heap = tlsf_create_with_pool(start_ptr + sizeof(heap_t), usable_size); size_t overhead = tlsf_size(); @@ -227,7 +231,7 @@ void esp_tee_heap_dump_info(void) /* Definitions for functions from the heap component, used in files shared with ESP-IDF */ -void *heap_caps_malloc(size_t alignment, size_t size, uint32_t caps) +void *heap_caps_malloc(size_t size, uint32_t caps) { (void) caps; return esp_tee_heap_malloc(size); diff --git a/components/esp_tee/subproject/main/core/esp_tee_init.c b/components/esp_tee/subproject/main/core/esp_tee_init.c index d42e155f579..998ea0c1134 100644 --- a/components/esp_tee/subproject/main/core/esp_tee_init.c +++ b/components/esp_tee/subproject/main/core/esp_tee_init.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -26,6 +26,9 @@ /* TEE symbols */ extern uint32_t _tee_stack; +extern uint32_t _tee_stack_bottom; +extern uint32_t _tee_intr_stack; +extern uint32_t _tee_intr_stack_bottom; extern uint32_t _tee_bss_start; extern uint32_t _tee_bss_end; extern uint32_t _tee_s_intr_handler; @@ -119,6 +122,9 @@ void __attribute__((noreturn)) esp_tee_init(uint32_t ree_entry_addr, uint32_t re { /* Clear BSS */ memset(&_tee_bss_start, 0, (&_tee_bss_end - &_tee_bss_start) * sizeof(_tee_bss_start)); + /* Clear the TEE stack and interrupt stack */ + memset(&_tee_stack_bottom, 0, (&_tee_stack - &_tee_stack_bottom) * sizeof(_tee_stack_bottom)); + memset(&_tee_intr_stack_bottom, 0, (&_tee_intr_stack - &_tee_intr_stack_bottom) * sizeof(_tee_intr_stack_bottom)); static uint32_t btld_sp;