Skip to content

Commit 537b36d

Browse files
committed
Merge branch 'fix/esp_tee_misc' into 'master'
feat(esp_tee): Miscellaneous fixes and updates Closes IDF-13856, IDFCI-3085, IDFCI-3094, and IDFCI-3105 See merge request espressif/esp-idf!41433
2 parents 961c10b + 57432bb commit 537b36d

File tree

23 files changed

+242
-214
lines changed

23 files changed

+242
-214
lines changed

components/esp_tee/Kconfig.projbuild

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ menu "ESP-TEE (Trusted Execution Environment)"
1212
config SECURE_TEE_IRAM_SIZE
1313
hex "IRAM region size"
1414
default 0x8000
15-
range 0x7000 0xA000
15+
range 0x5000 0xA000
1616
help
1717
This configuration sets the IRAM size for the TEE module.
1818
This should be 256-byte (0x100) aligned.
1919

2020
config SECURE_TEE_DRAM_SIZE
2121
hex "DRAM region size"
22-
default 0x6000
23-
range 0x5000 0x7000
22+
default 0x5000
23+
range 0x4000 0x7000
2424
help
2525
This configuration sets the DRAM size for the TEE module.
2626
This should be 256-byte (0x100) aligned.

components/esp_tee/src/esp_tee.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
*/
@@ -10,6 +10,7 @@
1010
#include "esp_private/cache_utils.h"
1111

1212
#include "freertos/FreeRTOS.h"
13+
#include "freertos/portmacro.h"
1314
#include "freertos/semphr.h"
1415
#include "freertos/task.h"
1516

@@ -43,7 +44,9 @@ uint32_t IRAM_ATTR esp_tee_service_call(int argc, ...)
4344
va_list ap = {0};
4445
va_start(ap, argc);
4546

46-
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
47+
/* NOTE: Cannot take the mutex if the scheduler is suspended or
48+
* service call is requested from a critical section */
49+
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING && xPortCanYield()) {
4750
if (xSemaphoreTake(s_tee_mutex, portMAX_DELAY) == pdTRUE) {
4851
val = _u2m_switch(argc, ap);
4952
xSemaphoreGive(s_tee_mutex);

components/esp_tee/src/esp_tee_config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extern int _tee_interrupt_handler(void);
1212
/* U-to-M mode switch */
1313
extern uint32_t _u2m_switch(int argc, va_list ap);
1414
/* REE IRAM end */
15-
extern uint32_t _iram_end;
15+
extern uint32_t _iram_text_end;
1616
/* REE IROM end */
1717
extern uint32_t _instruction_reserved_end;
1818
/* REE DROM start */
@@ -31,7 +31,7 @@ esp_tee_config_t esp_tee_app_config __attribute__((section(".esp_tee_app_cfg")))
3131

3232
.ns_int_handler = &_tee_interrupt_handler,
3333
.ns_entry_addr = &_u2m_switch,
34-
.ns_iram_end = &_iram_end,
34+
.ns_iram_end = &_iram_text_end,
3535
.ns_irom_end = &_instruction_reserved_end,
3636
.ns_drom_start = &_rodata_reserved_start,
3737
.ns_drom_end = &_rodata_reserved_end,

components/esp_tee/subproject/components/tee_ota_ops/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
idf_build_get_property(esp_tee_build ESP_TEE_BUILD)
22

33
set(srcs)
4-
set(priv_requires)
4+
set(priv_requires esp_tee)
55
set(include_dirs "include")
66

77
if(esp_tee_build)
88
list(APPEND srcs "esp_tee_ota_ops.c")
9-
list(APPEND priv_requires bootloader_support esp_tee log spi_flash tee_flash_mgr)
9+
list(APPEND priv_requires bootloader_support log spi_flash tee_flash_mgr)
1010
else()
11-
list(APPEND srcs "esp_tee_ota_ops_wrapper.c")
12-
list(APPEND priv_requires esp_tee)
11+
if(CONFIG_SECURE_ENABLE_TEE)
12+
list(APPEND srcs "esp_tee_ota_ops_wrapper.c")
13+
endif()
1314
endif()
1415

1516
idf_component_register(SRCS ${srcs}

components/esp_tee/subproject/components/tee_sec_storage/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ if(esp_tee_build)
77
list(APPEND srcs "tee_sec_storage.c")
88
list(APPEND priv_requires efuse esp_partition log mbedtls nvs_flash spi_flash tee_flash_mgr)
99
else()
10-
list(APPEND srcs "tee_sec_storage_wrapper.c")
10+
if(CONFIG_SECURE_ENABLE_TEE)
11+
list(APPEND srcs "tee_sec_storage_wrapper.c")
12+
endif()
1113
endif()
1214

1315
idf_component_register(SRCS ${srcs}

components/esp_tee/subproject/main/arch/riscv/esp_tee_vector_table_plic.S

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
*/
@@ -11,7 +11,7 @@
1111
#endif
1212

1313
/* Handlers defined in the `esp_tee_vectors.S` file */
14-
.global _panic_handler
14+
.global _tee_panic_handler
1515
.global _tee_ns_intr_handler
1616
.global _tee_s_intr_handler
1717

@@ -36,7 +36,7 @@
3636
.global _vector_table
3737
.type _vector_table, @function
3838
_vector_table:
39-
j _panic_handler /* 0: Exception entry */
39+
j _tee_panic_handler /* 0: Exception entry */
4040
/* NOTE: All of the free interrupts are used by the REE */
4141
j _tee_ns_intr_handler /* 1: Free interrupt number */
4242
j _tee_ns_intr_handler /* 2: Free interrupt number */
@@ -61,17 +61,16 @@ _vector_table:
6161
j _tee_ns_intr_handler /* 21: Free interrupt number */
6262
j _tee_ns_intr_handler /* 22: Free interrupt number */
6363
j _tee_ns_intr_handler /* 23: Free interrupt number */
64-
j _panic_handler /* 24: ETS_INT_WDT_INUM panic-interrupt (soc-level panic) */
65-
j _panic_handler /* 25: ETS_CACHEERR_INUM panic-interrupt (soc-level panic) */
64+
j _tee_panic_handler /* 24: ETS_INT_WDT_INUM panic-interrupt (soc-level panic) */
65+
j _tee_panic_handler /* 25: ETS_CACHEERR_INUM panic-interrupt (soc-level panic) */
6666
/* NOTE: Triggers panic irrespective of the Kconfig setting with ESP-TEE */
67-
j _panic_handler /* 26: ETS_MEMPROT_ERR_INUM handler (soc-level panic) */
67+
j _tee_panic_handler /* 26: ETS_MEMPROT_ERR_INUM handler (soc-level panic) */
6868
/* TODO: [IDF-10770] Not supported yet with ESP-TEE */
69-
j _panic_handler /* 27: ETS_ASSIST_DEBUG_INUM handler (soc-level panic) */
69+
j _tee_panic_handler /* 27: ETS_ASSIST_DEBUG_INUM handler (soc-level panic) */
7070
j _tee_ns_intr_handler /* 28: Free interrupt number */
7171
j _tee_ns_intr_handler /* 29: Free interrupt number */
7272
j _tee_ns_intr_handler /* 30: Free interrupt number */
7373
j _tee_s_intr_handler /* 31: ESP-TEE: Secure interrupt handler entry */
74-
j _panic_handler /* exception handler, entry 0 */
7574

7675
.size _vector_table, .-_vector_table
7776

components/esp_tee/subproject/main/arch/riscv/esp_tee_vectors_clic.S

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,14 @@ _s_sp:
176176
.global _tee_panic_handler
177177
.type _tee_panic_handler, @function
178178
_tee_panic_handler:
179-
/* Exception handler. */
180-
.global _panic_handler
181-
.type _panic_handler, @function
182-
_panic_handler:
183179
/* Backup t0, t1 on the stack before using it */
184180
addi sp, sp, -16
185181
sw t0, 0(sp)
186182
sw t1, 4(sp)
187183

188184
/* Read mcause */
189185
csrr t0, mcause
190-
li t1, VECTORS_MCAUSE_INTBIT_MASK | VECTORS_MCAUSE_REASON_MASK
186+
li t1, VECTORS_MCAUSE_REASON_MASK
191187
and t0, t0, t1
192188

193189
/* Check whether the exception is an M-mode ecall */
@@ -265,7 +261,7 @@ _return_from_exception:
265261
restore_general_regs RV_STK_FRMSZ
266262
mret
267263

268-
.size _panic_handler, .-_panic_handler
264+
.size _tee_panic_handler, .-_tee_panic_handler
269265

270266
/* ECALL handler. */
271267
.type _ecall_handler, @function
@@ -274,13 +270,12 @@ _ecall_handler:
274270
_machine_ecall:
275271
/* Enable the U-mode delegation of all interrupts */
276272
li t0, INTMTX_SIG_IDX_ASSERT_IN_SEC_REG
277-
li t1, 0x00
278-
sw t1, 0(t0)
273+
sw zero, 0(t0)
279274
fence
280275
/* Verify the above */
281276
_1:
282-
lw t2, 0(t0)
283-
bne t2, t1, _2
277+
lw t1, 0(t0)
278+
bnez t1, _1
284279

285280
/* Set the privilege mode to transition to after mret to U-mode */
286281
li t0, MSTATUS_MPP
@@ -365,7 +360,7 @@ _rtn_from_ns_int:
365360
/* Verify the above */
366361
_3:
367362
lw t2, 0(t0)
368-
bne t2, t1, _2
363+
bne t2, t1, _3
369364

370365
/* Restore the secure stack pointer */
371366
la t0, _s_sp
@@ -457,13 +452,12 @@ _found_intr:
457452

458453
/* Enable the U-mode interrupt delegation */
459454
li t0, INTMTX_SIG_IDX_ASSERT_IN_SEC_REG
460-
li t1, 0x00
461-
sw t1, 0(t0)
455+
sw zero, 0(t0)
462456
fence
463457
/* Verify the above */
464458
_4:
465-
lw t2, 0(t0)
466-
bne t2, t1, _2
459+
lw t1, 0(t0)
460+
bnez t1, _4
467461

468462
/* For U-mode interrupts, we use mret to switch to U-mode after executing the below steps - */
469463
/* Disable the U-mode global interrupts */
@@ -484,7 +478,7 @@ _4:
484478
csrc mstatus, t1
485479

486480
/* Save the current secure stack pointer and switch to the U-mode interrupt stack
487-
* saved while entering the secure service call routine (see `sec_world_entry`) */
481+
* saved while entering the secure service call routine (see `_tee_s_entry`) */
488482
la t0, _s_sp
489483
sw sp, 0(t0)
490484
la t1, _ns_sp
@@ -589,7 +583,7 @@ _intr_hdlr_exec:
589583
mv a0, sp /* argument 1, stack pointer */
590584
mv a1, s1 /* argument 2, interrupt number (mcause) */
591585
/* mask off the interrupt flag of mcause */
592-
li t0, VECTORS_MCAUSE_INTBIT_MASK | VECTORS_MCAUSE_REASON_MASK
586+
li t0, VECTORS_MCAUSE_REASON_MASK
593587
and a1, a1, t0
594588

595589
jal esp_tee_global_interrupt_handler

0 commit comments

Comments
 (0)