ESP-IDFを使ってみる

Memory Protection


ESP-IDF V5.5からESP32S2やESP32S3でMemory Protectionが有効になりました。
こちらがESP-IDF V5.4でターゲットをESP32S3にした時のメニューです。
Memory Protectionは選択することができません。


こちらがESP-IDF V5.5でターゲットをESP32S3にした時のメニューです。


Memory Protectionがデフォルトで有効になっています。


Helpには以下の様に表示されます。

If enabled, the permission control module watches all the memory access and fires the panic handler if a permission violation is detected.
This feature automatically splits the SRAM memory into data and instruction segments and sets Read/Execute permissions for the instruction part (below given splitting address) and Read/Write permissions for the data part (above the splitting address).
The memory protection is effective on all access through the IRAM0 and DRAM0 buses.

Google君に翻訳してもらいます。

有効にすると、パーミッション制御モジュールはすべてのメモリアクセスを監視し、パーミッション違反が検出された場合はパニックハンドラを起動し ます。
この機能は、SRAMメモリをデータセグメントと命令セグメントに自動的に分割し、命令セグメント(指定された分割アドレスより下)には読み取り /実行権限を、データセグメント(分割アドレスより上)には読み取り/書き込み権限を設定します。
このメモリ保護機能は、IRAM0およびDRAM0バスを介したすべてのアクセスに適用されます。

いま一つ良く分かりませんが、具体的な変更点として以下のコードで、Memory Protectionが有効と無効の違いを確認することができます。
#include <stdio.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "rom/ets_sys.h

void app_main()
{
   printf("heap_caps_get_free_size(MALLOC_CAP_32BIT) : %6d\n", heap_caps_get_free_size(MALLOC_CAP_32BIT) );
   printf("heap_caps_get_free_size(MALLOC_CAP_EXEC)  : %6d\n", heap_caps_get_free_size(MALLOC_CAP_EXEC) );
}

これがMemory Protectionが有効の時の表示です。
heap_caps_get_free_size(MALLOC_CAP_32BIT) : 389104
heap_caps_get_free_size(MALLOC_CAP_EXEC)  :      0

これがMemory Protectionが無効の時の表示です。
heap_caps_get_free_size(MALLOC_CAP_32BIT) : 389120
heap_caps_get_free_size(MALLOC_CAP_EXEC)  : 357088

MALLOC_CAP_EXECの定義はESP-IDF V6では完全に削除され、ビルドが通りません。
MALLOC_CAP_EXECはMALLOC_CAP_32BITなどに置き換える必要が有ります。
$ idf.py build
/home/nop/rtos/trace-heap/main/main.c:24:107: error: 'MALLOC_CAP_EXEC' undeclared (first use in this function); did you mean 'MALLOC_CAP_8BIT'?

ESP-IDF V5.5でもESP32ではMemory Protectionを有効にすることはできません。
ESP32Cシリーズではモデルによりメニュー項目が微妙に変わります。

- ESP32C2


- ESP32C3


- ESP32C5


-ESP32C6


続く....