fix(mmap): fixed some API read wrong data via mmap when flash being erased/written while XIP on PSRAM

Before:

The cache won't be disabled when XIP on psram. But during flash
erasing/programming, read data will be courrupt.

When XIP in psram is enabled, the image is not mapped to the cache so
usually there will be no flash access. The only way to read from flash
is via the driver or use mmap. The driver has protection during erasing,
while th mmap region not.

Now:

Mmap APIs provide a flag to make mmap->unmap region mutually exclusive
to flash erase/programming when XIP from psram. SPI Flash write APIs
will benefit from this. When the flag is used, no concurrent access to
mapped region will happen while writing; otherwise the cache will be
disable to avoid data corruption.

Most ESP-IDF APIs calls mmap with this flag. As for users calling
mmap-like APIs directly, they can choose whether to enable this by a
flag.

Closes https://github.com/espressif/esp-idf/issues/14897
This commit is contained in:
Xiao Xufeng
2026-06-16 01:00:43 +08:00
committed by Michael (XIAO Xufeng)
parent ec956162ab
commit 789ce684c9
73 changed files with 1678 additions and 487 deletions
+8 -3
View File
@@ -48,7 +48,7 @@ esp_err_t esp_partition_read(const esp_partition_t *partition,
esp_partition_mmap_handle_t handle;
esp_err_t err = esp_partition_mmap(partition, src_offset, size,
SPI_FLASH_MMAP_DATA, &buf, &handle);
ESP_PARTITION_MMAP_DATA | ESP_PARTITION_MMAP_BLOCKS_WRITE, &buf, &handle);
if (err != ESP_OK) {
return err;
}
@@ -152,7 +152,7 @@ esp_err_t esp_partition_erase_range(const esp_partition_t *partition,
* mapped pointers, and a single handle for all these regions.
*/
esp_err_t esp_partition_mmap(const esp_partition_t *partition, size_t offset, size_t size,
esp_partition_mmap_memory_t memory,
esp_partition_mmap_flag_t flags,
const void **out_ptr, esp_partition_mmap_handle_t *out_handle)
{
assert(partition != NULL);
@@ -169,7 +169,7 @@ esp_err_t esp_partition_mmap(const esp_partition_t *partition, size_t offset, si
// offset within mmu page size block
size_t region_offset = phys_addr & (CONFIG_MMU_PAGE_SIZE - 1);
size_t mmap_addr = phys_addr & ~(CONFIG_MMU_PAGE_SIZE - 1);
esp_err_t rc = spi_flash_mmap(mmap_addr, size + region_offset, (spi_flash_mmap_memory_t) memory, out_ptr, (spi_flash_mmap_handle_t*) out_handle);
esp_err_t rc = spi_flash_mmap(mmap_addr, size + region_offset, (spi_flash_mmap_flag_t) flags, out_ptr, (spi_flash_mmap_handle_t*) out_handle);
// adjust returned pointer to point to the correct offset
if (rc == ESP_OK) {
*out_ptr = (void *) (((ptrdiff_t) * out_ptr) + region_offset);
@@ -177,6 +177,11 @@ esp_err_t esp_partition_mmap(const esp_partition_t *partition, size_t offset, si
return rc;
}
ESP_STATIC_ASSERT((int)ESP_PARTITION_MMAP_DATA == (int)SPI_FLASH_MMAP_FLAG_DATA &&
(int)ESP_PARTITION_MMAP_INST == (int)SPI_FLASH_MMAP_FLAG_INST &&
(int)ESP_PARTITION_MMAP_BLOCKS_WRITE == (int)SPI_FLASH_MMAP_FLAG_BLOCKS_WRITE,
"esp_partition_mmap_flag_t enum not equal to spi_flash_mmap_flag_t.");
void esp_partition_munmap(esp_partition_mmap_handle_t handle)
{
spi_flash_munmap((spi_flash_mmap_handle_t) handle);