Skip to content

Commit 3123fff

Browse files
committed
fix(esp_psram): Add XIP PSRAM alignment gaps in heap only if PSRAM protection is enabled
1 parent 2195b2c commit 3123fff

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

components/esp_psram/system_layer/esp_psram.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ esp_err_t esp_psram_extram_add_to_heap_allocator(void)
427427
ESP_EARLY_LOGI(TAG, "Adding pool of %dK of PSRAM memory to heap allocator",
428428
(s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size + s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].size) / 1024);
429429

430+
// To allow using the page alignment gaps created while mapping the flash segments,
431+
// the alignment gaps must be configured with correct memory protection configurations.
432+
#if CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
430433
// Here, SOC_MMU_DI_VADDR_SHARED is necessary because, for the targets that have separate data and instruction virtual address spaces,
431434
// the SPIRAM gap created due to the alignment needed while placing the instruction segment in the instruction virtual address space
432435
// cannot be added in heap because the region cannot be configured with write permissions.
@@ -452,6 +455,8 @@ esp_err_t esp_psram_extram_add_to_heap_allocator(void)
452455
}
453456
}
454457
#endif /* CONFIG_SPIRAM_RODATA */
458+
#endif /* CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION */
459+
455460
return ESP_OK;
456461
}
457462

components/esp_system/ld/esp32c5/sections.ld.in

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ SECTIONS
279279
. += _esp_flash_mmap_prefetch_pad_size;
280280

281281
#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
282-
/* Align the end of flash text region as per PMP granularity as PSRAM memory protection is enabled */
282+
/* Align the end of flash text region as per PMP granularity to allow using the
283+
* page alignment gap created while mapping the flash region into the PSRAM memory.
284+
*/
283285
. = ALIGN(_esp_pmp_align_size);
284286
#endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
285287

@@ -425,7 +427,9 @@ SECTIONS
425427
. = ALIGN(ALIGNOF(.flash.tbss));
426428

427429
#if CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
428-
/* Align the end of flash rodata region as per PMP granularity as PSRAM memory protection is enabled */
430+
/* Align the end of flash rodata region as per PMP granularity to allow using the
431+
* page alignment gap created while mapping the flash region into the PSRAM memory.
432+
*/
429433
. = ALIGN(_esp_pmp_align_size);
430434
#endif // CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
431435

components/esp_system/ld/esp32c61/sections.ld.in

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ SECTIONS
136136
*/
137137
. += _esp_flash_mmap_prefetch_pad_size;
138138

139-
#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
140-
/* Align the end of flash text region as per PMP granularity */
139+
#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
140+
/* Align the end of flash text region as per PMP granularity to allow using the
141+
* page alignment gap created while mapping the flash region into the PSRAM memory.
142+
*/
141143
. = ALIGN(_esp_pmp_align_size);
142-
#endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS
144+
#endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
143145

144146
_text_end = ABSOLUTE(.);
145147
/**
@@ -282,10 +284,12 @@ SECTIONS
282284

283285
. = ALIGN(ALIGNOF(.flash.tbss));
284286

285-
#if CONFIG_SPIRAM_RODATA
286-
/* Align the end of flash rodata region as per PMP granularity */
287+
#if CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
288+
/* Align the end of flash rodata region as per PMP granularity to allow using the
289+
* page alignment gap created while mapping the flash region into the PSRAM memory.
290+
*/
287291
. = ALIGN(_esp_pmp_align_size);
288-
#endif // CONFIG_SPIRAM_RODATA
292+
#endif // CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
289293

290294
_thread_local_data_end = ABSOLUTE(.);
291295
} > default_rodata_seg

components/esp_system/ld/esp32p4/sections.ld.in

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,12 @@ SECTIONS
293293
*/
294294
. += _esp_flash_mmap_prefetch_pad_size;
295295

296-
#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
297-
/* Align the end of flash text region as per PMP granularity */
296+
#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
297+
/* Align the end of flash text region as per PMP granularity to allow using the
298+
* page alignment gap created while mapping the flash region into the PSRAM memory.
299+
*/
298300
. = ALIGN(_esp_pmp_align_size);
299-
#endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS
301+
#endif // CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
300302

301303
_text_end = ABSOLUTE(.);
302304
/**
@@ -446,10 +448,12 @@ SECTIONS
446448

447449
. = ALIGN(ALIGNOF(.flash.tbss));
448450

449-
#if CONFIG_SPIRAM_RODATA
450-
/* Align the end of flash rodata region as per PMP granularity */
451+
#if CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
452+
/* Align the end of flash rodata region as per PMP granularity to allow using the
453+
* page alignment gap created while mapping the flash region into the PSRAM memory.
454+
*/
451455
. = ALIGN(_esp_pmp_align_size);
452-
#endif // CONFIG_SPIRAM_RODATA
456+
#endif // CONFIG_SPIRAM_RODATA && CONFIG_SPIRAM_PRE_CONFIGURE_MEMORY_PROTECTION
453457

454458
_thread_local_data_end = ABSOLUTE(.);
455459
} > rodata_seg_low

0 commit comments

Comments
 (0)