mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-02 03:00:34 +03:00
feat(cpu_region_protect): Enable basic memory protection for SPIRAM
This commit is contained in:
@@ -10,6 +10,30 @@ config SPIRAM_BOOT_INIT
|
||||
have specific requirements, you'll want to leave this enabled so memory allocated
|
||||
during boot-up can also be placed in SPI RAM.
|
||||
|
||||
config SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
|
||||
bool "Pre-configure memory protection for PSRAM"
|
||||
default y if SPIRAM_BOOT_INIT
|
||||
default n
|
||||
depends on SPIRAM
|
||||
help
|
||||
If this is enabled, the PSRAM will be pre-configured for memory protection during initial boot.
|
||||
This configuration takes into consideration the PSRAM memory configurations that are performed
|
||||
by ESP-IDF's default PSRAM initialization function, esp_psram_init().
|
||||
Thus, the config is enabled by default when SPIRAM_BOOT_INIT is enabled,
|
||||
because the function esp_psram_init() would be called in the startup code.
|
||||
|
||||
In case you wish to disable SPIRAM_BOOT_INIT just for delaying the PSRAM initialization and plan
|
||||
to use the ESP-IDF's default PSRAM initialization function, esp_psram_init() in the application code,
|
||||
you should still enable this config to enable memory protection for the PSRAM.
|
||||
|
||||
Note that enabling this config also considers that the rest of the PSRAM memory that is left after
|
||||
the memory configurations are performed by esp_psram_init(), can be allocated to the heap using the function
|
||||
esp_psram_extram_add_to_heap_allocator(), thus configures this region with heap memory protection (RW).
|
||||
|
||||
As an advanced usage, if you plan to initialize the PSRAM memory regions manually by yourself without
|
||||
using the function esp_psram_init(), you should disable this config to avoid any memory protection and
|
||||
usage conflicts.
|
||||
|
||||
config SPIRAM_IGNORE_NOTFOUND
|
||||
bool "Ignore PSRAM when not found"
|
||||
default "n"
|
||||
|
||||
@@ -592,3 +592,84 @@ void esp_psram_bss_init(void)
|
||||
memset(&_ext_ram_bss_start, 0, size);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
|
||||
static inline uint32_t s_get_ext_bss_size(void)
|
||||
{
|
||||
#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
|
||||
return ((intptr_t)&_ext_ram_bss_end - (intptr_t)&_ext_ram_bss_start);
|
||||
#else
|
||||
return 0;
|
||||
#endif /* CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY */
|
||||
}
|
||||
|
||||
static inline uint32_t s_get_ext_noinit_size(void)
|
||||
{
|
||||
#if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
|
||||
return ((intptr_t)&_ext_ram_noinit_end - (intptr_t)&_ext_ram_noinit_start);
|
||||
#else
|
||||
return 0;
|
||||
#endif /* CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates the effective PSRAM memory that would be / is mapped.
|
||||
*
|
||||
* @return The size of PSRAM memory that would be / is mapped in bytes, or 0 if PSRAM isn't successfully initialized
|
||||
*/
|
||||
static size_t esp_psram_get_effective_mapped_size(void)
|
||||
{
|
||||
size_t byte_aligned_size = 0;
|
||||
size_t total_mapped_size = 0;
|
||||
|
||||
if (s_psram_ctx.is_initialised) {
|
||||
return s_psram_ctx.mapped_regions[PSRAM_MEM_8BIT_ALIGNED].size + s_psram_ctx.mapped_regions[PSRAM_MEM_32BIT_ALIGNED].size;
|
||||
} else {
|
||||
uint32_t psram_available_size = 0;
|
||||
esp_err_t ret = esp_psram_impl_get_available_size(&psram_available_size);
|
||||
assert(ret == ESP_OK);
|
||||
|
||||
#if CONFIG_SPIRAM_RODATA
|
||||
psram_available_size -= mmu_psram_get_rodata_segment_length();
|
||||
#endif /* CONFIG_SPIRAM_RODATA */
|
||||
|
||||
#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
|
||||
psram_available_size -= mmu_psram_get_text_segment_length();
|
||||
#endif /* CONFIG_SPIRAM_FETCH_INSTRUCTIONS */
|
||||
|
||||
ret = esp_mmu_map_get_max_consecutive_free_block_size(MMU_MEM_CAP_READ | MMU_MEM_CAP_WRITE | MMU_MEM_CAP_8BIT | MMU_MEM_CAP_32BIT, MMU_TARGET_PSRAM0, &byte_aligned_size);
|
||||
assert(ret == ESP_OK);
|
||||
total_mapped_size += MIN(byte_aligned_size, psram_available_size - total_mapped_size);
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32S2
|
||||
if (total_mapped_size < psram_available_size) {
|
||||
size_t word_aligned_size = 0;
|
||||
ret = esp_mmu_map_get_max_consecutive_free_block_size(MMU_MEM_CAP_READ | MMU_MEM_CAP_WRITE | MMU_MEM_CAP_32BIT, MMU_TARGET_PSRAM0, &word_aligned_size);
|
||||
assert(ret == ESP_OK);
|
||||
total_mapped_size += MIN(word_aligned_size, psram_available_size - total_mapped_size);
|
||||
}
|
||||
#endif
|
||||
return total_mapped_size;
|
||||
}
|
||||
}
|
||||
|
||||
size_t esp_psram_get_heap_size_to_protect(void)
|
||||
{
|
||||
if (s_psram_ctx.is_initialised) {
|
||||
return s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size + s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].size;
|
||||
} else {
|
||||
size_t effective_mapped_size = esp_psram_get_effective_mapped_size();
|
||||
if (effective_mapped_size == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
effective_mapped_size -= s_get_ext_bss_size();
|
||||
effective_mapped_size -= s_get_ext_noinit_size();
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
effective_mapped_size -= esp_himem_reserved_area_size() - 1;
|
||||
#endif
|
||||
return effective_mapped_size;
|
||||
}
|
||||
}
|
||||
#endif /* CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -60,6 +60,18 @@ bool esp_psram_extram_test(void);
|
||||
*/
|
||||
void esp_psram_bss_init(void);
|
||||
|
||||
/**
|
||||
* @brief Calculates the effective PSRAM memory that would be / is added into the heap.
|
||||
*
|
||||
* @return The size of PSRAM memory that would be / is added into the heap in bytes, or 0 if PSRAM hardware isn't successfully initialized
|
||||
* @note The function pre-calculates the effective size of the PSRAM memory that would be added into the heap after performing the XIP or
|
||||
* ext bss and ext noinit considerations, thus, even if the function is called before esp_psram_init(), it will return the final
|
||||
* effective size of the PSRAM memory that would have been added into the heap after esp_psram_init() is performed
|
||||
* instead of the vanilla size of the PSRAM memory.
|
||||
* This function is only available if CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION is enabled.
|
||||
*/
|
||||
size_t esp_psram_get_heap_size_to_protect(void);
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
/**
|
||||
* @brief Force a writeback of the data in the PSRAM cache. This is to be called whenever
|
||||
|
||||
Reference in New Issue
Block a user