diff --git a/components/esp_system/Kconfig b/components/esp_system/Kconfig index 7a03588903..27ce305750 100644 --- a/components/esp_system/Kconfig +++ b/components/esp_system/Kconfig @@ -113,9 +113,15 @@ menu "ESP System Settings" menu "Memory protection" + config ESP_SYSTEM_MEMPROT_DEPCHECK + bool + default y if IDF_TARGET_ESP32S2 + default y if IDF_TARGET_ESP32C3 + default y if IDF_TARGET_ESP32H2 + config ESP_SYSTEM_MEMPROT_FEATURE bool "Enable memory protection" - depends on IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32H2 + depends on ESP_SYSTEM_MEMPROT_DEPCHECK default "y" help If enabled, the permission control module watches all the memory access and fires the panic handler @@ -133,6 +139,20 @@ menu "ESP System Settings" Once locked, memory protection settings cannot be changed anymore. The lock is reset only on the chip startup. + config ESP_SYSTEM_MEMPROT_CPU_PREFETCH_PAD_SIZE + # Hidden option for linker script usage + int + depends on ESP_SYSTEM_MEMPROT_DEPCHECK + default 16 + + config ESP_SYSTEM_MEMPROT_MEM_ALIGN_SIZE + # Hidden option for linker script usage + int + depends on ESP_SYSTEM_MEMPROT_DEPCHECK + default 4 if IDF_TARGET_ESP32S2 + default 256 if IDF_TARGET_ESP32S3 + default 512 + endmenu # Memory protection config ESP_SYSTEM_EVENT_QUEUE_SIZE diff --git a/components/esp_system/component.mk b/components/esp_system/component.mk index 6f603f880b..c67e3064e1 100644 --- a/components/esp_system/component.mk +++ b/components/esp_system/component.mk @@ -53,7 +53,7 @@ $(COMPONENT_LIBRARY): $(ld_output) $(ld_output): $(ld_input) ../include/sdkconfig.h mkdir -p $(COMPONENT_BUILD_DIR)/ld - $(CC) -I ../include -C -P -x c -E $< -o $@ + $(CC) -I ../include -I $(COMPONENT_PATH)/ld -C -P -x c -E $< -o $@ COMPONENT_EXTRA_CLEAN := $(ld_output) $(sections_ld) endif diff --git a/components/esp_system/ld/esp32/memory.ld.in b/components/esp_system/ld/esp32/memory.ld.in index 9a8af9e096..798076ab1c 100644 --- a/components/esp_system/ld/esp32/memory.ld.in +++ b/components/esp_system/ld/esp32/memory.ld.in @@ -20,6 +20,7 @@ to simple macros with numeric values, and/or #if/#endif blocks. */ #include "sdkconfig.h" +#include "ld.common" /* If BT is not built at all */ #ifndef CONFIG_BT_RESERVE_DRAM diff --git a/components/esp_system/ld/esp32/sections.ld.in b/components/esp_system/ld/esp32/sections.ld.in index 2277dec6e1..c2a6c66c5b 100644 --- a/components/esp_system/ld/esp32/sections.ld.in +++ b/components/esp_system/ld/esp32/sections.ld.in @@ -349,7 +349,7 @@ SECTIONS * safe access to up to 16 bytes after the last real instruction, add * dummy bytes to ensure this */ - . += 16; + . += _esp_flash_mmap_prefetch_pad_size; _text_end = ABSOLUTE(.); _etext = .; diff --git a/components/esp_system/ld/esp32c3/memory.ld.in b/components/esp_system/ld/esp32c3/memory.ld.in index 66bf5d9ac4..d387915f93 100644 --- a/components/esp_system/ld/esp32c3/memory.ld.in +++ b/components/esp_system/ld/esp32c3/memory.ld.in @@ -1,3 +1,9 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + /** * ESP32-C3 Linker Script Memory Layout * This file describes the memory layout (memory blocks) by virtual memory addresses. @@ -7,6 +13,7 @@ */ #include "sdkconfig.h" +#include "ld.common" #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE) diff --git a/components/esp_system/ld/esp32c3/sections.ld.in b/components/esp_system/ld/esp32c3/sections.ld.in index a4e41e7123..96b0bdd346 100644 --- a/components/esp_system/ld/esp32c3/sections.ld.in +++ b/components/esp_system/ld/esp32c3/sections.ld.in @@ -1,3 +1,9 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + /* Default entry point */ ENTRY(call_start_cpu0); @@ -228,7 +234,7 @@ SECTIONS * safe access to up to 16 bytes after the last real instruction, add * dummy bytes to ensure this */ - . += 16; + . += _esp_flash_mmap_prefetch_pad_size; _text_end = ABSOLUTE(.); _instruction_reserved_end = ABSOLUTE(.); @@ -368,9 +374,9 @@ SECTIONS /* Marks the end of IRAM code segment */ .iram0.text_end (NOLOAD) : { - /* C3 memprot requires 16B padding for possible CPU prefetch and 512B alignment for PMS split lines */ - . += 16; - . = ALIGN (0x200); + /* ESP32-C3 memprot requires 16B padding for possible CPU prefetch and 512B alignment for PMS split lines */ + . += _esp_memprot_prefetch_pad_size; + . = ALIGN(_esp_memprot_align_size); /* iram_end_test section exists for use by memprot unit tests only */ *(.iram_end_test) _iram_text_end = ABSOLUTE(.); diff --git a/components/esp_system/ld/esp32h2/memory.ld.in b/components/esp_system/ld/esp32h2/memory.ld.in index 58ab761a0e..03f45cabd9 100644 --- a/components/esp_system/ld/esp32h2/memory.ld.in +++ b/components/esp_system/ld/esp32h2/memory.ld.in @@ -1,3 +1,9 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + /** * ESP32-H2 Linker Script Memory Layout * This file describes the memory layout (memory blocks) by virtual memory addresses. @@ -7,6 +13,7 @@ */ #include "sdkconfig.h" +#include "ld.common" #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE) diff --git a/components/esp_system/ld/esp32h2/sections.ld.in b/components/esp_system/ld/esp32h2/sections.ld.in index 65eb17b45c..eaf8a5a41b 100644 --- a/components/esp_system/ld/esp32h2/sections.ld.in +++ b/components/esp_system/ld/esp32h2/sections.ld.in @@ -1,3 +1,9 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + /* Default entry point */ ENTRY(call_start_cpu0); @@ -365,8 +371,9 @@ SECTIONS /* Marks the end of IRAM code segment */ .iram0.text_end (NOLOAD) : { - /* C3 memprot requires 512 B alignment for split lines */ - . = ALIGN (0x200); + /* ESP32-H2 memprot requires 16B padding for possible CPU prefetch and 512B alignment for PMS split lines */ + . += _esp_memprot_prefetch_pad_size; + . = ALIGN(_esp_memprot_align_size); /* iram_end_test section exists for use by memprot unit tests only */ *(.iram_end_test) _iram_text_end = ABSOLUTE(.); diff --git a/components/esp_system/ld/esp32s2/memory.ld.in b/components/esp_system/ld/esp32s2/memory.ld.in index d4c50f4253..f40fca87a4 100644 --- a/components/esp_system/ld/esp32s2/memory.ld.in +++ b/components/esp_system/ld/esp32s2/memory.ld.in @@ -13,6 +13,7 @@ Restrict to simple macros with numeric values, and/or #if/#endif blocks. */ #include "sdkconfig.h" +#include "ld.common" #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE) diff --git a/components/esp_system/ld/esp32s2/sections.ld.in b/components/esp_system/ld/esp32s2/sections.ld.in index 3c6bbe87ed..2bbfdad5ce 100644 --- a/components/esp_system/ld/esp32s2/sections.ld.in +++ b/components/esp_system/ld/esp32s2/sections.ld.in @@ -1,3 +1,9 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + /* Default entry point: */ ENTRY(call_start_cpu0); @@ -177,7 +183,7 @@ SECTIONS _coredump_iram_end = 0; /* align + add 16B for CPU dummy speculative instr. fetch */ - . = ALIGN(4) + 16; + . = ALIGN(_esp_memprot_align_size) + _esp_memprot_prefetch_pad_size; /* iram_end_test section exists for use by memprot unit tests only */ *(.iram_end_test) _iram_text_end = ABSOLUTE(.); @@ -363,7 +369,7 @@ SECTIONS * safe access to up to 16 bytes after the last real instruction, add * dummy bytes to ensure this */ - . += 16; + . += _esp_flash_mmap_prefetch_pad_size; _text_end = ABSOLUTE(.); _instruction_reserved_end = ABSOLUTE(.); diff --git a/components/esp_system/ld/esp32s3/memory.ld.in b/components/esp_system/ld/esp32s3/memory.ld.in index 79dcd53487..4f6b70d490 100644 --- a/components/esp_system/ld/esp32s3/memory.ld.in +++ b/components/esp_system/ld/esp32s3/memory.ld.in @@ -12,6 +12,7 @@ */ #include "sdkconfig.h" +#include "ld.common" #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE) diff --git a/components/esp_system/ld/esp32s3/sections.ld.in b/components/esp_system/ld/esp32s3/sections.ld.in index 02246b3f9d..7ef4075a31 100644 --- a/components/esp_system/ld/esp32s3/sections.ld.in +++ b/components/esp_system/ld/esp32s3/sections.ld.in @@ -1,3 +1,9 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + /* Default entry point */ ENTRY(call_start_cpu0); @@ -264,7 +270,7 @@ SECTIONS * safe access to up to 16 bytes after the last real instruction, add * dummy bytes to ensure this */ - . += 16; + . += _esp_flash_mmap_prefetch_pad_size; _text_end = ABSOLUTE(.); _instruction_reserved_end = ABSOLUTE(.); @@ -376,7 +382,11 @@ SECTIONS /* Marks the end of IRAM code segment */ .iram0.text_end (NOLOAD) : { - . = ALIGN (4); + /* ESP32-S3 memprot requires 16B padding for possible CPU prefetch and 256B alignment for PMS split lines */ + . += _esp_memprot_prefetch_pad_size; + . = ALIGN(_esp_memprot_align_size); + /* iram_end_test section exists for use by memprot unit tests only */ + *(.iram_end_test) _iram_text_end = ABSOLUTE(.); } > iram0_0_seg diff --git a/components/esp_system/ld/ld.cmake b/components/esp_system/ld/ld.cmake index 93376b69c1..5e80268da7 100644 --- a/components/esp_system/ld/ld.cmake +++ b/components/esp_system/ld/ld.cmake @@ -22,7 +22,8 @@ idf_build_get_property(config_dir CONFIG_DIR) # Preprocess memory.ld.in linker script to include configuration, becomes memory.ld add_custom_command( OUTPUT ${ld_output} - COMMAND "${CMAKE_C_COMPILER}" -C -P -x c -E -o ${ld_output} -I ${config_dir} ${ld_input} + COMMAND "${CMAKE_C_COMPILER}" -C -P -x c -E -o ${ld_output} -I ${config_dir} + -I "${CMAKE_CURRENT_LIST_DIR}" ${ld_input} MAIN_DEPENDENCY ${ld_input} DEPENDS ${sdkconfig_header} COMMENT "Generating memory.ld linker script..." diff --git a/components/esp_system/ld/ld.common b/components/esp_system/ld/ld.common new file mode 100644 index 0000000000..974cafe5d4 --- /dev/null +++ b/components/esp_system/ld/ld.common @@ -0,0 +1,24 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "sdkconfig.h" + +/* CPU instruction prefetch padding size for flash mmap scenario */ +_esp_flash_mmap_prefetch_pad_size = 16; + +/* CPU instruction prefetch padding size for memory protection scenario */ +#ifdef CONFIG_ESP_SYSTEM_MEMPROT_CPU_PREFETCH_PAD_SIZE +_esp_memprot_prefetch_pad_size = CONFIG_ESP_SYSTEM_MEMPROT_CPU_PREFETCH_PAD_SIZE; +#else +_esp_memprot_prefetch_pad_size = 0; +#endif + +/* Memory alignment size for PMS */ +#ifdef CONFIG_ESP_SYSTEM_MEMPROT_MEM_ALIGN_SIZE +_esp_memprot_align_size = CONFIG_ESP_SYSTEM_MEMPROT_MEM_ALIGN_SIZE; +#else +_esp_memprot_align_size = 0; +#endif