spi_flash: fix stale data being read from mmaped region

The issue that cache entries are not invalidated correctly sometimes
can also be reproduced for non-encrypted flash as well.
This change updates the workaround to do Cache_Flush, enabling it for
non-encrypted flash, and adds a unit test.
This commit is contained in:
Ivan Grokhotkov 2017-03-04 17:48:44 +08:00
parent 4bf96e99b3
commit d790300215
2 changed files with 24 additions and 2 deletions

View File

@ -187,12 +187,12 @@ esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_
} }
/* This is a temporary fix for an issue where some /* This is a temporary fix for an issue where some
encrypted cache reads may see stale data. cache reads may see stale data.
Working on a long term fix that doesn't require invalidating Working on a long term fix that doesn't require invalidating
entire cache. entire cache.
*/ */
if (esp_flash_encryption_enabled() && !did_flush && need_flush) { if (!did_flush && need_flush) {
Cache_Flush(0); Cache_Flush(0);
Cache_Flush(1); Cache_Flush(1);
} }

View File

@ -138,6 +138,8 @@ TEST_CASE("Can mmap into data address space", "[spi_flash]")
TEST_CASE("Can mmap into instruction address space", "[mmap]") TEST_CASE("Can mmap into instruction address space", "[mmap]")
{ {
setup_mmap_tests();
printf("Mapping %x (+%x)\n", start, end - start); printf("Mapping %x (+%x)\n", start, end - start);
spi_flash_mmap_handle_t handle1; spi_flash_mmap_handle_t handle1;
const void *ptr1; const void *ptr1;
@ -288,3 +290,23 @@ TEST_CASE("mmap consistent with phys2cache/cache2phys", "[spi_flash]")
TEST_ASSERT_EQUAL_HEX(SPI_FLASH_CACHE2PHYS_FAIL, spi_flash_cache2phys(ptr)); TEST_ASSERT_EQUAL_HEX(SPI_FLASH_CACHE2PHYS_FAIL, spi_flash_cache2phys(ptr));
} }
TEST_CASE("munmap followed by mmap flushes cache", "[spi_flash]")
{
setup_mmap_tests();
const esp_partition_t *p = get_test_data_partition();
const uint32_t* data;
spi_flash_mmap_handle_t handle;
TEST_ESP_OK( esp_partition_mmap(p, 0, SPI_FLASH_MMU_PAGE_SIZE,
SPI_FLASH_MMAP_DATA, (const void **) &data, &handle) );
uint32_t buf[16];
memcpy(buf, data, sizeof(buf));
spi_flash_munmap(handle);
TEST_ESP_OK( esp_partition_mmap(p, SPI_FLASH_MMU_PAGE_SIZE, SPI_FLASH_MMU_PAGE_SIZE,
SPI_FLASH_MMAP_DATA, (const void **) &data, &handle) );
TEST_ASSERT_NOT_EQUAL(0, memcmp(buf, data, sizeof(buf)));
}