flash_mmap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <freertos/FreeRTOS.h>
  19. #include <freertos/task.h>
  20. #include <freertos/semphr.h>
  21. #include <rom/spi_flash.h>
  22. #include <rom/cache.h>
  23. #include <soc/soc.h>
  24. #include <soc/dport_reg.h>
  25. #include "sdkconfig.h"
  26. #include "esp_ipc.h"
  27. #include "esp_attr.h"
  28. #include "esp_spi_flash.h"
  29. #include "esp_flash_encrypt.h"
  30. #include "esp_log.h"
  31. #include "cache_utils.h"
  32. #ifndef NDEBUG
  33. // Enable built-in checks in queue.h in debug builds
  34. #define INVARIANTS
  35. #endif
  36. #include "rom/queue.h"
  37. #define REGIONS_COUNT 4
  38. #define PAGES_PER_REGION 64
  39. #define INVALID_ENTRY_VAL 0x100
  40. #define VADDR0_START_ADDR 0x3F400000
  41. #define VADDR1_START_ADDR 0x40000000
  42. #define VADDR1_FIRST_USABLE_ADDR 0x400D0000
  43. #define PRO_IRAM0_FIRST_USABLE_PAGE ((VADDR1_FIRST_USABLE_ADDR - VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + 64)
  44. /* Ensure pages in a region haven't been marked as written via
  45. spi_flash_mark_modified_region(). If the page has
  46. been written, flush the entire flash cache before returning.
  47. This ensures stale cache entries are never read after fresh calls
  48. to spi_flash_mmap(), while keeping the number of cache flushes to a
  49. minimum.
  50. Returns true if cache was flushed.
  51. */
  52. static bool spi_flash_ensure_unmodified_region(size_t start_addr, size_t length);
  53. typedef struct mmap_entry_{
  54. uint32_t handle;
  55. int page;
  56. int count;
  57. LIST_ENTRY(mmap_entry_) entries;
  58. } mmap_entry_t;
  59. static LIST_HEAD(mmap_entries_head, mmap_entry_) s_mmap_entries_head =
  60. LIST_HEAD_INITIALIZER(s_mmap_entries_head);
  61. static uint8_t s_mmap_page_refcnt[REGIONS_COUNT * PAGES_PER_REGION] = {0};
  62. static uint32_t s_mmap_last_handle = 0;
  63. static void IRAM_ATTR spi_flash_mmap_init()
  64. {
  65. if (s_mmap_page_refcnt[0] != 0) {
  66. return; /* mmap data already initialised */
  67. }
  68. for (int i = 0; i < REGIONS_COUNT * PAGES_PER_REGION; ++i) {
  69. uint32_t entry_pro = DPORT_PRO_FLASH_MMU_TABLE[i];
  70. uint32_t entry_app = DPORT_APP_FLASH_MMU_TABLE[i];
  71. if (entry_pro != entry_app) {
  72. // clean up entries used by boot loader
  73. entry_pro = INVALID_ENTRY_VAL;
  74. DPORT_PRO_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
  75. }
  76. if ((entry_pro & INVALID_ENTRY_VAL) == 0 && (i == 0 || i == PRO_IRAM0_FIRST_USABLE_PAGE || entry_pro != 0)) {
  77. s_mmap_page_refcnt[i] = 1;
  78. } else {
  79. DPORT_PRO_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
  80. DPORT_APP_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
  81. }
  82. }
  83. }
  84. esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory,
  85. const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
  86. {
  87. esp_err_t ret;
  88. bool did_flush, need_flush = false;
  89. if (src_addr & 0xffff) {
  90. return ESP_ERR_INVALID_ARG;
  91. }
  92. if (src_addr + size > g_rom_flashchip.chip_size) {
  93. return ESP_ERR_INVALID_ARG;
  94. }
  95. mmap_entry_t* new_entry = (mmap_entry_t*) malloc(sizeof(mmap_entry_t));
  96. if (new_entry == 0) {
  97. return ESP_ERR_NO_MEM;
  98. }
  99. spi_flash_disable_interrupts_caches_and_other_cpu();
  100. did_flush = spi_flash_ensure_unmodified_region(src_addr, size);
  101. spi_flash_mmap_init();
  102. // figure out the memory region where we should look for pages
  103. int region_begin; // first page to check
  104. int region_size; // number of pages to check
  105. uint32_t region_addr; // base address of memory region
  106. if (memory == SPI_FLASH_MMAP_DATA) {
  107. // Vaddr0
  108. region_begin = 0;
  109. region_size = 64;
  110. region_addr = VADDR0_START_ADDR;
  111. } else {
  112. // only part of VAddr1 is usable, so adjust for that
  113. region_begin = PRO_IRAM0_FIRST_USABLE_PAGE;
  114. region_size = 3 * 64 - region_begin;
  115. region_addr = VADDR1_FIRST_USABLE_ADDR;
  116. }
  117. // region which should be mapped
  118. int phys_page = src_addr / SPI_FLASH_MMU_PAGE_SIZE;
  119. int page_count = (size + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE;
  120. // The following part searches for a range of MMU entries which can be used.
  121. // Algorithm is essentially naïve strstr algorithm, except that unused MMU
  122. // entries are treated as wildcards.
  123. int start;
  124. int end = region_begin + region_size - page_count;
  125. for (start = region_begin; start < end; ++start) {
  126. int page = phys_page;
  127. int pos;
  128. for (pos = start; pos < start + page_count; ++pos, ++page) {
  129. int table_val = (int) DPORT_PRO_FLASH_MMU_TABLE[pos];
  130. uint8_t refcnt = s_mmap_page_refcnt[pos];
  131. if (refcnt != 0 && table_val != page) {
  132. break;
  133. }
  134. }
  135. // whole mapping range matched, bail out
  136. if (pos - start == page_count) {
  137. break;
  138. }
  139. }
  140. // checked all the region(s) and haven't found anything?
  141. if (start == end) {
  142. *out_handle = 0;
  143. *out_ptr = NULL;
  144. ret = ESP_ERR_NO_MEM;
  145. } else {
  146. // set up mapping using pages [start, start + page_count)
  147. uint32_t entry_val = (uint32_t) phys_page;
  148. for (int i = start; i != start + page_count; ++i, ++entry_val) {
  149. // sanity check: we won't reconfigure entries with non-zero reference count
  150. assert(s_mmap_page_refcnt[i] == 0 ||
  151. (DPORT_PRO_FLASH_MMU_TABLE[i] == entry_val &&
  152. DPORT_APP_FLASH_MMU_TABLE[i] == entry_val));
  153. if (s_mmap_page_refcnt[i] == 0) {
  154. if (DPORT_PRO_FLASH_MMU_TABLE[i] != entry_val || DPORT_APP_FLASH_MMU_TABLE[i] != entry_val) {
  155. DPORT_PRO_FLASH_MMU_TABLE[i] = entry_val;
  156. DPORT_APP_FLASH_MMU_TABLE[i] = entry_val;
  157. need_flush = true;
  158. }
  159. }
  160. ++s_mmap_page_refcnt[i];
  161. }
  162. LIST_INSERT_HEAD(&s_mmap_entries_head, new_entry, entries);
  163. new_entry->page = start;
  164. new_entry->count = page_count;
  165. new_entry->handle = ++s_mmap_last_handle;
  166. *out_handle = new_entry->handle;
  167. *out_ptr = (void*) (region_addr + (start - region_begin) * SPI_FLASH_MMU_PAGE_SIZE);
  168. ret = ESP_OK;
  169. }
  170. /* This is a temporary fix for an issue where some
  171. cache reads may see stale data.
  172. Working on a long term fix that doesn't require invalidating
  173. entire cache.
  174. */
  175. if (!did_flush && need_flush) {
  176. Cache_Flush(0);
  177. Cache_Flush(1);
  178. }
  179. spi_flash_enable_interrupts_caches_and_other_cpu();
  180. if (*out_ptr == NULL) {
  181. free(new_entry);
  182. }
  183. return ret;
  184. }
  185. void IRAM_ATTR spi_flash_munmap(spi_flash_mmap_handle_t handle)
  186. {
  187. spi_flash_disable_interrupts_caches_and_other_cpu();
  188. mmap_entry_t* it;
  189. // look for handle in linked list
  190. for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
  191. if (it->handle == handle) {
  192. // for each page, decrement reference counter
  193. // if reference count is zero, disable MMU table entry to
  194. // facilitate debugging of use-after-free conditions
  195. for (int i = it->page; i < it->page + it->count; ++i) {
  196. assert(s_mmap_page_refcnt[i] > 0);
  197. if (--s_mmap_page_refcnt[i] == 0) {
  198. DPORT_PRO_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
  199. DPORT_APP_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
  200. }
  201. }
  202. LIST_REMOVE(it, entries);
  203. break;
  204. }
  205. }
  206. spi_flash_enable_interrupts_caches_and_other_cpu();
  207. if (it == NULL) {
  208. assert(0 && "invalid handle, or handle already unmapped");
  209. }
  210. free(it);
  211. }
  212. void spi_flash_mmap_dump()
  213. {
  214. spi_flash_mmap_init();
  215. mmap_entry_t* it;
  216. for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
  217. printf("handle=%d page=%d count=%d\n", it->handle, it->page, it->count);
  218. }
  219. for (int i = 0; i < REGIONS_COUNT * PAGES_PER_REGION; ++i) {
  220. if (s_mmap_page_refcnt[i] != 0) {
  221. printf("page %d: refcnt=%d paddr=%d\n",
  222. i, (int) s_mmap_page_refcnt[i], DPORT_PRO_FLASH_MMU_TABLE[i]);
  223. }
  224. }
  225. }
  226. /* 256-bit (up to 16MB of 64KB pages) bitset of all flash pages
  227. that have been written to since last cache flush.
  228. Before mmaping a page, need to flush caches if that page has been
  229. written to.
  230. Note: It's possible to do some additional performance tweaks to
  231. this algorithm, as we actually only need to flush caches if a page
  232. was first mmapped, then written to, then is about to be mmaped a
  233. second time. This is a fair bit more complex though, so unless
  234. there's an access pattern that this would significantly boost then
  235. it's probably not worth it.
  236. */
  237. static uint32_t written_pages[256/32];
  238. static bool update_written_pages(size_t start_addr, size_t length, bool mark);
  239. void IRAM_ATTR spi_flash_mark_modified_region(size_t start_addr, size_t length)
  240. {
  241. update_written_pages(start_addr, length, true);
  242. }
  243. static IRAM_ATTR bool spi_flash_ensure_unmodified_region(size_t start_addr, size_t length)
  244. {
  245. return update_written_pages(start_addr, length, false);
  246. }
  247. /* generic implementation for the previous two functions */
  248. static inline IRAM_ATTR bool update_written_pages(size_t start_addr, size_t length, bool mark)
  249. {
  250. /* align start_addr & length to full MMU pages */
  251. uint32_t page_start_addr = start_addr & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
  252. length += (start_addr - page_start_addr);
  253. length = (length + SPI_FLASH_MMU_PAGE_SIZE - 1) & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
  254. for (uint32_t addr = page_start_addr; addr < page_start_addr + length; addr += SPI_FLASH_MMU_PAGE_SIZE) {
  255. int page = addr / SPI_FLASH_MMU_PAGE_SIZE;
  256. if (page >= 256) {
  257. return false; /* invalid address */
  258. }
  259. int idx = page / 32;
  260. uint32_t bit = 1 << (page % 32);
  261. if (mark) {
  262. written_pages[idx] |= bit;
  263. } else if (written_pages[idx] & bit) {
  264. /* it is tempting to write a version of this that only
  265. flushes each CPU's cache as needed. However this is
  266. tricky because mmaped memory can be used on un-pinned
  267. cores, or the pointer passed between CPUs.
  268. */
  269. Cache_Flush(0);
  270. #ifndef CONFIG_FREERTOS_UNICORE
  271. Cache_Flush(1);
  272. #endif
  273. bzero(written_pages, sizeof(written_pages));
  274. return true;
  275. }
  276. }
  277. return false;
  278. }
  279. uint32_t spi_flash_cache2phys(const void *cached)
  280. {
  281. intptr_t c = (intptr_t)cached;
  282. size_t cache_page;
  283. if (c >= VADDR1_START_ADDR && c < VADDR1_FIRST_USABLE_ADDR) {
  284. /* IRAM address, doesn't map to flash */
  285. return SPI_FLASH_CACHE2PHYS_FAIL;
  286. }
  287. else if (c < VADDR1_FIRST_USABLE_ADDR) {
  288. /* expect cache is in DROM */
  289. cache_page = (c - VADDR0_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE;
  290. } else {
  291. /* expect cache is in IROM */
  292. cache_page = (c - VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + 64;
  293. }
  294. if (cache_page >= 256) {
  295. /* cached address was not in IROM or DROM */
  296. return SPI_FLASH_CACHE2PHYS_FAIL;
  297. }
  298. uint32_t phys_page = DPORT_PRO_FLASH_MMU_TABLE[cache_page];
  299. if (phys_page == INVALID_ENTRY_VAL) {
  300. /* page is not mapped */
  301. return SPI_FLASH_CACHE2PHYS_FAIL;
  302. }
  303. uint32_t phys_offs = phys_page * SPI_FLASH_MMU_PAGE_SIZE;
  304. return phys_offs | (c & (SPI_FLASH_MMU_PAGE_SIZE-1));
  305. }
  306. const void *spi_flash_phys2cache(uint32_t phys_offs, spi_flash_mmap_memory_t memory)
  307. {
  308. uint32_t phys_page = phys_offs / SPI_FLASH_MMU_PAGE_SIZE;
  309. int start, end, page_delta;
  310. intptr_t base;
  311. if (memory == SPI_FLASH_MMAP_DATA) {
  312. start = 0;
  313. end = 64;
  314. base = VADDR0_START_ADDR;
  315. page_delta = 0;
  316. } else {
  317. start = PRO_IRAM0_FIRST_USABLE_PAGE;
  318. end = 256;
  319. base = VADDR1_START_ADDR;
  320. page_delta = 64;
  321. }
  322. for (int i = start; i < end; i++) {
  323. if (DPORT_PRO_FLASH_MMU_TABLE[i] == phys_page) {
  324. i -= page_delta;
  325. intptr_t cache_page = base + (SPI_FLASH_MMU_PAGE_SIZE * i);
  326. return (const void *) (cache_page | (phys_offs & (SPI_FLASH_MMU_PAGE_SIZE-1)));
  327. }
  328. }
  329. return NULL;
  330. }