feat(build): add RISC-V ZCMP post-link workaround check

Validate linked RISC-V executables when
CONFIG_COMPILER_ENABLE_RISCV_ZCMP is enabled on affected chips.
Disassemble each function and reject mstatus.MIE clears that lack an
earlier mintthresh write of 0xff.

Handle csrrci, csrrw, and register-mask csrrc patterns while ignoring
csrrs. Add build-only coverage for valid and invalid sequences with
CMake v1 and v2.

Stop the hardware stack guard before switching stacks during restart,
and keep ZCMP disabled for TEE test apps that still require the
workaround.
This commit is contained in:
Alexey Lapshin
2026-07-30 12:14:54 +07:00
parent 8b166d2b7c
commit fbaf869fb9
19 changed files with 376 additions and 22 deletions
@@ -28,6 +28,9 @@
#endif
#include "esp_private/cache_err_int.h"
#include "esp_memory_utils.h"
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD && CONFIG_COMPILER_ENABLE_RISCV_ZCMP
#include "esp_private/hw_stack_guard.h"
#endif
#define ALIGN_DOWN(val, align) ((val) & ~((align) - 1))
extern int _bss_end;
@@ -163,6 +166,12 @@ void esp_restart_noos(void)
// If stack is in external RAM (CONFIG_FREERTOS_TASK_CREATE_ALLOW_EXT_MEM), switch SP to
// internal RAM before disabling the cache to avoid a "Cache disabled but cached memory
// region accessed" crash.
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD && CONFIG_COMPILER_ENABLE_RISCV_ZCMP
// Stop hw stack guard before changing SP: new SP is outside task stack bounds.
// rtc_clk_cpu_set_to_default_config() (called later) uses vPortEnterCritical() via
// ENABLE_CLK_GATE, which can trigger DIG-661 (assist-debug interrupt with mstatus.mie=0).
esp_hw_stack_guard_monitor_stop();
#endif
uint32_t new_sp = ALIGN_DOWN((uint32_t)&_bss_end, 16);
rv_utils_set_sp((void *)new_sp);
}
@@ -24,6 +24,9 @@
#include "hal/uart_ll.h"
#include "esp_private/cache_err_int.h"
#include "esp_memory_utils.h"
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD && CONFIG_COMPILER_ENABLE_RISCV_ZCMP
#include "esp_private/hw_stack_guard.h"
#endif
#define ALIGN_DOWN(val, align) ((val) & ~((align) - 1))
extern int _bss_end;
@@ -153,6 +156,12 @@ void esp_restart_noos(void)
// If stack is in external RAM (CONFIG_FREERTOS_TASK_CREATE_ALLOW_EXT_MEM), switch SP to
// internal RAM before disabling the cache to avoid a "Cache disabled but cached memory
// region accessed" crash.
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD && CONFIG_COMPILER_ENABLE_RISCV_ZCMP
// Stop hw stack guard before changing SP: new SP is outside task stack bounds.
// rtc_clk_cpu_set_to_default_config() (called later) uses vPortEnterCritical() via
// ENABLE_CLK_GATE, which can trigger DIG-661 (assist-debug interrupt with mstatus.mie=0).
esp_hw_stack_guard_monitor_stop();
#endif
uint32_t new_sp = ALIGN_DOWN((uint32_t)&_bss_end, 16);
rv_utils_set_sp((void *)new_sp);
}
@@ -23,6 +23,9 @@
#include "hal/wdt_hal.h"
#include "hal/uart_ll.h"
#include "esp_memory_utils.h"
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD && CONFIG_COMPILER_ENABLE_RISCV_ZCMP
#include "esp_private/hw_stack_guard.h"
#endif
#define ALIGN_DOWN(val, align) ((val) & ~((align) - 1))
extern int _bss_end;
@@ -170,6 +173,12 @@ void esp_restart_noos(void)
// If stack is in external RAM (CONFIG_FREERTOS_TASK_CREATE_ALLOW_EXT_MEM), switch SP to
// internal RAM before disabling the cache to avoid a "Cache disabled but cached memory
// region accessed" crash.
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD && CONFIG_COMPILER_ENABLE_RISCV_ZCMP
// Stop hw stack guard before changing SP: new SP is outside task stack bounds.
// rtc_clk_cpu_set_to_default_config() (called later) uses vPortEnterCritical() via
// ENABLE_CLK_GATE, which can trigger DIG-661 (assist-debug interrupt with mstatus.mie=0).
esp_hw_stack_guard_monitor_stop();
#endif
uint32_t new_sp = ALIGN_DOWN((uint32_t)&_bss_end, 16);
rv_utils_set_sp((void *)new_sp);
}
@@ -34,6 +34,9 @@
#include "hal/dma2d_ll.h"
#include "hal/efuse_hal.h"
#include "esp_memory_utils.h"
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD && CONFIG_COMPILER_ENABLE_RISCV_ZCMP
#include "esp_private/hw_stack_guard.h"
#endif
#define ALIGN_DOWN(val, align) ((val) & ~((align) - 1))
extern int _bss_end;
@@ -249,6 +252,12 @@ void esp_restart_noos(void)
// If stack is in external RAM (CONFIG_FREERTOS_TASK_CREATE_ALLOW_EXT_MEM), switch SP to
// internal RAM before disabling the cache to avoid a "Cache disabled but cached memory
// region accessed" crash.
#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD && CONFIG_COMPILER_ENABLE_RISCV_ZCMP
// Stop hw stack guard before changing SP: new SP is outside task stack bounds.
// rtc_clk_cpu_set_to_default_config() (called later) uses vPortEnterCritical() via
// ENABLE_CLK_GATE, which can trigger DIG-661 (assist-debug interrupt with mstatus.mie=0).
esp_hw_stack_guard_monitor_stop();
#endif
uint32_t new_sp = ALIGN_DOWN((uint32_t)&_bss_end, 16);
rv_utils_set_sp((void *)new_sp);
}