From 26586deb7ddb4c29a82018efbffd10db35612226 Mon Sep 17 00:00:00 2001 From: "harshal.patil" Date: Mon, 5 May 2025 10:49:04 +0530 Subject: [PATCH] fix(esp_psram): Add XIP PSRAM alignment gaps in heap only if PSRAM protection is enabled --- components/esp_psram/esp_psram.c | 5 +++++ components/esp_system/ld/esp32c5/sections.ld.in | 8 ++++++-- components/esp_system/ld/esp32c61/sections.ld.in | 16 ++++++++++------ components/esp_system/ld/esp32p4/sections.ld.in | 16 ++++++++++------ 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/components/esp_psram/esp_psram.c b/components/esp_psram/esp_psram.c index 747af8e7e6..a5e650178e 100644 --- a/components/esp_psram/esp_psram.c +++ b/components/esp_psram/esp_psram.c @@ -411,6 +411,9 @@ esp_err_t esp_psram_extram_add_to_heap_allocator(void) ESP_EARLY_LOGI(TAG, "Adding pool of %dK of PSRAM memory to heap allocator", (s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size + s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].size) / 1024); + // To allow using the page alignment gaps created while mapping the flash segments, + // the alignment gaps must be configured with correct memory protection configurations. +#if CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION // Here, SOC_MMU_DI_VADDR_SHARED is necessary because, for the targets that have separate data and instruction virtual address spaces, // the SPIRAM gap created due to the alignment needed while placing the instruction segment in the instruction virtual address space // cannot be added in heap because the region cannot be configured with write permissions. @@ -436,6 +439,8 @@ esp_err_t esp_psram_extram_add_to_heap_allocator(void) } } #endif /* CONFIG_SPIRAM_RODATA */ +#endif /* CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION */ + return ESP_OK; } diff --git a/components/esp_system/ld/esp32c5/sections.ld.in b/components/esp_system/ld/esp32c5/sections.ld.in index 6bc7e69612..7acf239d24 100644 --- a/components/esp_system/ld/esp32c5/sections.ld.in +++ b/components/esp_system/ld/esp32c5/sections.ld.in @@ -281,7 +281,9 @@ SECTIONS . += _esp_flash_mmap_prefetch_pad_size; #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION - /* Align the end of flash text region as per PMP granularity as PSRAM memory protection is enabled */ + /* Align the end of flash text region as per PMP granularity to allow using the + * page alignment gap created while mapping the flash region into the PSRAM memory. + */ . = ALIGN(_esp_pmp_align_size); #endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION @@ -468,7 +470,9 @@ SECTIONS . = ALIGN(ALIGNOF(.flash.tbss)); #if CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION - /* Align the end of flash rodata region as per PMP granularity as PSRAM memory protection is enabled */ + /* Align the end of flash rodata region as per PMP granularity to allow using the + * page alignment gap created while mapping the flash region into the PSRAM memory. + */ . = ALIGN(_esp_pmp_align_size); #endif // CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION diff --git a/components/esp_system/ld/esp32c61/sections.ld.in b/components/esp_system/ld/esp32c61/sections.ld.in index 3d65825aa9..1693f7d628 100644 --- a/components/esp_system/ld/esp32c61/sections.ld.in +++ b/components/esp_system/ld/esp32c61/sections.ld.in @@ -138,10 +138,12 @@ SECTIONS */ . += _esp_flash_mmap_prefetch_pad_size; -#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS - /* Align the end of flash text region as per PMP granularity */ +#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION + /* Align the end of flash text region as per PMP granularity to allow using the + * page alignment gap created while mapping the flash region into the PSRAM memory. + */ . = ALIGN(_esp_pmp_align_size); -#endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS +#endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION _text_end = ABSOLUTE(.); /** @@ -326,10 +328,12 @@ SECTIONS . = ALIGN(ALIGNOF(.flash.tbss)); -#if CONFIG_SPIRAM_RODATA - /* Align the end of flash rodata region as per PMP granularity */ +#if CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION + /* Align the end of flash rodata region as per PMP granularity to allow using the + * page alignment gap created while mapping the flash region into the PSRAM memory. + */ . = ALIGN(_esp_pmp_align_size); -#endif // CONFIG_SPIRAM_RODATA +#endif // CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION _thread_local_data_end = ABSOLUTE(.); } > default_rodata_seg diff --git a/components/esp_system/ld/esp32p4/sections.ld.in b/components/esp_system/ld/esp32p4/sections.ld.in index 6c863841f1..ce7cca967c 100644 --- a/components/esp_system/ld/esp32p4/sections.ld.in +++ b/components/esp_system/ld/esp32p4/sections.ld.in @@ -295,10 +295,12 @@ SECTIONS */ . += _esp_flash_mmap_prefetch_pad_size; -#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS - /* Align the end of flash text region as per PMP granularity */ +#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION + /* Align the end of flash text region as per PMP granularity to allow using the + * page alignment gap created while mapping the flash region into the PSRAM memory. + */ . = ALIGN(_esp_pmp_align_size); -#endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS +#endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION _text_end = ABSOLUTE(.); /** @@ -450,10 +452,12 @@ SECTIONS . = ALIGN(ALIGNOF(.flash.tbss)); -#if CONFIG_SPIRAM_RODATA - /* Align the end of flash rodata region as per PMP granularity */ +#if CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION + /* Align the end of flash rodata region as per PMP granularity to allow using the + * page alignment gap created while mapping the flash region into the PSRAM memory. + */ . = ALIGN(_esp_pmp_align_size); -#endif // CONFIG_SPIRAM_RODATA +#endif // CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION _thread_local_data_end = ABSOLUTE(.); } > rodata_seg_low