flash_mmap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #include "esp_spiram.h"
  33. #ifndef NDEBUG
  34. // Enable built-in checks in queue.h in debug builds
  35. #define INVARIANTS
  36. #endif
  37. #include "rom/queue.h"
  38. #define REGIONS_COUNT 4
  39. #define PAGES_PER_REGION 64
  40. #define INVALID_ENTRY_VAL 0x100
  41. #define VADDR0_START_ADDR 0x3F400000
  42. #define VADDR1_START_ADDR 0x40000000
  43. #define VADDR1_FIRST_USABLE_ADDR 0x400D0000
  44. #define PRO_IRAM0_FIRST_USABLE_PAGE ((VADDR1_FIRST_USABLE_ADDR - VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + 64)
  45. /* Ensure pages in a region haven't been marked as written via
  46. spi_flash_mark_modified_region(). If the page has
  47. been written, flush the entire flash cache before returning.
  48. This ensures stale cache entries are never read after fresh calls
  49. to spi_flash_mmap(), while keeping the number of cache flushes to a
  50. minimum.
  51. Returns true if cache was flushed.
  52. */
  53. static bool spi_flash_ensure_unmodified_region(size_t start_addr, size_t length);
  54. typedef struct mmap_entry_{
  55. uint32_t handle;
  56. int page;
  57. int count;
  58. LIST_ENTRY(mmap_entry_) entries;
  59. } mmap_entry_t;
  60. static LIST_HEAD(mmap_entries_head, mmap_entry_) s_mmap_entries_head =
  61. LIST_HEAD_INITIALIZER(s_mmap_entries_head);
  62. static uint8_t s_mmap_page_refcnt[REGIONS_COUNT * PAGES_PER_REGION] = {0};
  63. static uint32_t s_mmap_last_handle = 0;
  64. static void IRAM_ATTR spi_flash_mmap_init()
  65. {
  66. if (s_mmap_page_refcnt[0] != 0) {
  67. return; /* mmap data already initialised */
  68. }
  69. DPORT_INTERRUPT_DISABLE();
  70. for (int i = 0; i < REGIONS_COUNT * PAGES_PER_REGION; ++i) {
  71. uint32_t entry_pro = DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[i]);
  72. uint32_t entry_app = DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_APP_FLASH_MMU_TABLE[i]);
  73. if (entry_pro != entry_app) {
  74. // clean up entries used by boot loader
  75. entry_pro = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
  76. DPORT_PRO_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
  77. }
  78. if ((entry_pro & INVALID_ENTRY_VAL) == 0 && (i == 0 || i == PRO_IRAM0_FIRST_USABLE_PAGE || entry_pro != 0)) {
  79. s_mmap_page_refcnt[i] = 1;
  80. } else {
  81. DPORT_PRO_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
  82. DPORT_APP_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
  83. }
  84. }
  85. DPORT_INTERRUPT_RESTORE();
  86. }
  87. static void IRAM_ATTR get_mmu_region(spi_flash_mmap_memory_t memory, int* out_begin, int* out_size,uint32_t* region_addr)
  88. {
  89. if (memory == SPI_FLASH_MMAP_DATA) {
  90. // Vaddr0
  91. *out_begin = 0;
  92. *out_size = 64;
  93. *region_addr = VADDR0_START_ADDR;
  94. } else {
  95. // only part of VAddr1 is usable, so adjust for that
  96. *out_begin = PRO_IRAM0_FIRST_USABLE_PAGE;
  97. *out_size = 3 * 64 - *out_begin;
  98. *region_addr = VADDR1_FIRST_USABLE_ADDR;
  99. }
  100. }
  101. esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory,
  102. const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
  103. {
  104. esp_err_t ret;
  105. if (src_addr & 0xffff) {
  106. return ESP_ERR_INVALID_ARG;
  107. }
  108. if (src_addr + size > g_rom_flashchip.chip_size) {
  109. return ESP_ERR_INVALID_ARG;
  110. }
  111. // region which should be mapped
  112. int phys_page = src_addr / SPI_FLASH_MMU_PAGE_SIZE;
  113. int page_count = (size + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE;
  114. // prepare a linear pages array to feed into spi_flash_mmap_pages
  115. int *pages = heap_caps_malloc(sizeof(int)*page_count, MALLOC_CAP_INTERNAL);
  116. if (pages == NULL) {
  117. return ESP_ERR_NO_MEM;
  118. }
  119. for (int i = 0; i < page_count; i++) {
  120. pages[i] = phys_page+i;
  121. }
  122. ret = spi_flash_mmap_pages(pages, page_count, memory, out_ptr, out_handle);
  123. free(pages);
  124. return ret;
  125. }
  126. esp_err_t IRAM_ATTR spi_flash_mmap_pages(const int *pages, size_t page_count, spi_flash_mmap_memory_t memory,
  127. const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
  128. {
  129. esp_err_t ret;
  130. bool did_flush, need_flush = false;
  131. if (!page_count) {
  132. return ESP_ERR_INVALID_ARG;
  133. }
  134. if (!esp_ptr_internal(pages)) {
  135. return ESP_ERR_INVALID_ARG;
  136. }
  137. for (int i = 0; i < page_count; i++) {
  138. if (pages[i] < 0 || pages[i]*SPI_FLASH_MMU_PAGE_SIZE >= g_rom_flashchip.chip_size) {
  139. return ESP_ERR_INVALID_ARG;
  140. }
  141. }
  142. mmap_entry_t* new_entry = (mmap_entry_t*) heap_caps_malloc(sizeof(mmap_entry_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  143. if (new_entry == 0) {
  144. return ESP_ERR_NO_MEM;
  145. }
  146. spi_flash_disable_interrupts_caches_and_other_cpu();
  147. did_flush = 0;
  148. for (int i = 0; i < page_count; i++) {
  149. if (spi_flash_ensure_unmodified_region(pages[i]*SPI_FLASH_MMU_PAGE_SIZE, SPI_FLASH_MMU_PAGE_SIZE)) {
  150. did_flush = 1;
  151. }
  152. }
  153. spi_flash_mmap_init();
  154. // figure out the memory region where we should look for pages
  155. int region_begin; // first page to check
  156. int region_size; // number of pages to check
  157. uint32_t region_addr; // base address of memory region
  158. get_mmu_region(memory,&region_begin,&region_size,&region_addr);
  159. if (region_size < page_count) {
  160. return ESP_ERR_NO_MEM;
  161. }
  162. // The following part searches for a range of MMU entries which can be used.
  163. // Algorithm is essentially naïve strstr algorithm, except that unused MMU
  164. // entries are treated as wildcards.
  165. int start;
  166. // the " + 1" is a fix when loop the MMU table pages, because the last MMU page
  167. // is valid as well if it have not been used
  168. int end = region_begin + region_size - page_count + 1;
  169. for (start = region_begin; start < end; ++start) {
  170. int pageno = 0;
  171. int pos;
  172. DPORT_INTERRUPT_DISABLE();
  173. for (pos = start; pos < start + page_count; ++pos, ++pageno) {
  174. int table_val = (int) DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[pos]);
  175. uint8_t refcnt = s_mmap_page_refcnt[pos];
  176. if (refcnt != 0 && table_val != pages[pageno]) {
  177. break;
  178. }
  179. }
  180. DPORT_INTERRUPT_RESTORE();
  181. // whole mapping range matched, bail out
  182. if (pos - start == page_count) {
  183. break;
  184. }
  185. }
  186. // checked all the region(s) and haven't found anything?
  187. if (start == end) {
  188. *out_handle = 0;
  189. *out_ptr = NULL;
  190. ret = ESP_ERR_NO_MEM;
  191. } else {
  192. // set up mapping using pages
  193. uint32_t pageno = 0;
  194. DPORT_INTERRUPT_DISABLE();
  195. for (int i = start; i != start + page_count; ++i, ++pageno) {
  196. // sanity check: we won't reconfigure entries with non-zero reference count
  197. uint32_t entry_pro = DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[i]);
  198. uint32_t entry_app = DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_APP_FLASH_MMU_TABLE[i]);
  199. assert(s_mmap_page_refcnt[i] == 0 ||
  200. (entry_pro == pages[pageno] &&
  201. entry_app == pages[pageno]));
  202. if (s_mmap_page_refcnt[i] == 0) {
  203. if (entry_pro != pages[pageno] || entry_app != pages[pageno]) {
  204. DPORT_PRO_FLASH_MMU_TABLE[i] = pages[pageno];
  205. DPORT_APP_FLASH_MMU_TABLE[i] = pages[pageno];
  206. need_flush = true;
  207. }
  208. }
  209. ++s_mmap_page_refcnt[i];
  210. }
  211. DPORT_INTERRUPT_RESTORE();
  212. LIST_INSERT_HEAD(&s_mmap_entries_head, new_entry, entries);
  213. new_entry->page = start;
  214. new_entry->count = page_count;
  215. new_entry->handle = ++s_mmap_last_handle;
  216. *out_handle = new_entry->handle;
  217. *out_ptr = (void*) (region_addr + (start - region_begin) * SPI_FLASH_MMU_PAGE_SIZE);
  218. ret = ESP_OK;
  219. }
  220. /* This is a temporary fix for an issue where some
  221. cache reads may see stale data.
  222. Working on a long term fix that doesn't require invalidating
  223. entire cache.
  224. */
  225. if (!did_flush && need_flush) {
  226. #if CONFIG_SPIRAM_SUPPORT
  227. esp_spiram_writeback_cache();
  228. #endif
  229. Cache_Flush(0);
  230. Cache_Flush(1);
  231. }
  232. spi_flash_enable_interrupts_caches_and_other_cpu();
  233. if (*out_ptr == NULL) {
  234. free(new_entry);
  235. }
  236. return ret;
  237. }
  238. void IRAM_ATTR spi_flash_munmap(spi_flash_mmap_handle_t handle)
  239. {
  240. spi_flash_disable_interrupts_caches_and_other_cpu();
  241. mmap_entry_t* it;
  242. // look for handle in linked list
  243. for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
  244. if (it->handle == handle) {
  245. // for each page, decrement reference counter
  246. // if reference count is zero, disable MMU table entry to
  247. // facilitate debugging of use-after-free conditions
  248. for (int i = it->page; i < it->page + it->count; ++i) {
  249. assert(s_mmap_page_refcnt[i] > 0);
  250. if (--s_mmap_page_refcnt[i] == 0) {
  251. DPORT_PRO_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
  252. DPORT_APP_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
  253. }
  254. }
  255. LIST_REMOVE(it, entries);
  256. break;
  257. }
  258. }
  259. spi_flash_enable_interrupts_caches_and_other_cpu();
  260. if (it == NULL) {
  261. assert(0 && "invalid handle, or handle already unmapped");
  262. }
  263. free(it);
  264. }
  265. static void IRAM_ATTR NOINLINE_ATTR spi_flash_protected_mmap_init()
  266. {
  267. spi_flash_disable_interrupts_caches_and_other_cpu();
  268. spi_flash_mmap_init();
  269. spi_flash_enable_interrupts_caches_and_other_cpu();
  270. }
  271. static uint32_t IRAM_ATTR NOINLINE_ATTR spi_flash_protected_read_mmu_entry(int index)
  272. {
  273. uint32_t value;
  274. spi_flash_disable_interrupts_caches_and_other_cpu();
  275. value = DPORT_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[index]);
  276. spi_flash_enable_interrupts_caches_and_other_cpu();
  277. return value;
  278. }
  279. void spi_flash_mmap_dump()
  280. {
  281. spi_flash_protected_mmap_init();
  282. mmap_entry_t* it;
  283. for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
  284. printf("handle=%d page=%d count=%d\n", it->handle, it->page, it->count);
  285. }
  286. for (int i = 0; i < REGIONS_COUNT * PAGES_PER_REGION; ++i) {
  287. if (s_mmap_page_refcnt[i] != 0) {
  288. uint32_t paddr = spi_flash_protected_read_mmu_entry(i);
  289. printf("page %d: refcnt=%d paddr=%d\n", i, (int) s_mmap_page_refcnt[i], paddr);
  290. }
  291. }
  292. }
  293. uint32_t IRAM_ATTR spi_flash_mmap_get_free_pages(spi_flash_mmap_memory_t memory)
  294. {
  295. spi_flash_disable_interrupts_caches_and_other_cpu();
  296. spi_flash_mmap_init();
  297. int count = 0;
  298. int region_begin; // first page to check
  299. int region_size; // number of pages to check
  300. uint32_t region_addr; // base address of memory region
  301. get_mmu_region(memory,&region_begin,&region_size,&region_addr);
  302. DPORT_INTERRUPT_DISABLE();
  303. for (int i = region_begin; i < region_begin + region_size; ++i) {
  304. if (s_mmap_page_refcnt[i] == 0 && DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[i]) == INVALID_ENTRY_VAL) {
  305. count++;
  306. }
  307. }
  308. DPORT_INTERRUPT_RESTORE();
  309. spi_flash_enable_interrupts_caches_and_other_cpu();
  310. return count;
  311. }
  312. /* 256-bit (up to 16MB of 64KB pages) bitset of all flash pages
  313. that have been written to since last cache flush.
  314. Before mmaping a page, need to flush caches if that page has been
  315. written to.
  316. Note: It's possible to do some additional performance tweaks to
  317. this algorithm, as we actually only need to flush caches if a page
  318. was first mmapped, then written to, then is about to be mmaped a
  319. second time. This is a fair bit more complex though, so unless
  320. there's an access pattern that this would significantly boost then
  321. it's probably not worth it.
  322. */
  323. static uint32_t written_pages[256/32];
  324. static bool update_written_pages(size_t start_addr, size_t length, bool mark);
  325. void IRAM_ATTR spi_flash_mark_modified_region(size_t start_addr, size_t length)
  326. {
  327. update_written_pages(start_addr, length, true);
  328. }
  329. static IRAM_ATTR bool spi_flash_ensure_unmodified_region(size_t start_addr, size_t length)
  330. {
  331. return update_written_pages(start_addr, length, false);
  332. }
  333. /* generic implementation for the previous two functions */
  334. static inline IRAM_ATTR bool update_written_pages(size_t start_addr, size_t length, bool mark)
  335. {
  336. /* align start_addr & length to full MMU pages */
  337. uint32_t page_start_addr = start_addr & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
  338. length += (start_addr - page_start_addr);
  339. length = (length + SPI_FLASH_MMU_PAGE_SIZE - 1) & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
  340. for (uint32_t addr = page_start_addr; addr < page_start_addr + length; addr += SPI_FLASH_MMU_PAGE_SIZE) {
  341. int page = addr / SPI_FLASH_MMU_PAGE_SIZE;
  342. if (page >= 256) {
  343. return false; /* invalid address */
  344. }
  345. int idx = page / 32;
  346. uint32_t bit = 1 << (page % 32);
  347. if (mark) {
  348. written_pages[idx] |= bit;
  349. } else if (written_pages[idx] & bit) {
  350. /* it is tempting to write a version of this that only
  351. flushes each CPU's cache as needed. However this is
  352. tricky because mmaped memory can be used on un-pinned
  353. cores, or the pointer passed between CPUs.
  354. */
  355. #if CONFIG_SPIRAM_SUPPORT
  356. esp_spiram_writeback_cache();
  357. #endif
  358. Cache_Flush(0);
  359. #ifndef CONFIG_FREERTOS_UNICORE
  360. Cache_Flush(1);
  361. #endif
  362. bzero(written_pages, sizeof(written_pages));
  363. return true;
  364. }
  365. }
  366. return false;
  367. }
  368. uint32_t spi_flash_cache2phys(const void *cached)
  369. {
  370. intptr_t c = (intptr_t)cached;
  371. size_t cache_page;
  372. if (c >= VADDR1_START_ADDR && c < VADDR1_FIRST_USABLE_ADDR) {
  373. /* IRAM address, doesn't map to flash */
  374. return SPI_FLASH_CACHE2PHYS_FAIL;
  375. }
  376. else if (c < VADDR1_FIRST_USABLE_ADDR) {
  377. /* expect cache is in DROM */
  378. cache_page = (c - VADDR0_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE;
  379. } else {
  380. /* expect cache is in IROM */
  381. cache_page = (c - VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + 64;
  382. }
  383. if (cache_page >= 256) {
  384. /* cached address was not in IROM or DROM */
  385. return SPI_FLASH_CACHE2PHYS_FAIL;
  386. }
  387. uint32_t phys_page = spi_flash_protected_read_mmu_entry(cache_page);
  388. if (phys_page == INVALID_ENTRY_VAL) {
  389. /* page is not mapped */
  390. return SPI_FLASH_CACHE2PHYS_FAIL;
  391. }
  392. uint32_t phys_offs = phys_page * SPI_FLASH_MMU_PAGE_SIZE;
  393. return phys_offs | (c & (SPI_FLASH_MMU_PAGE_SIZE-1));
  394. }
  395. const void *IRAM_ATTR spi_flash_phys2cache(uint32_t phys_offs, spi_flash_mmap_memory_t memory)
  396. {
  397. uint32_t phys_page = phys_offs / SPI_FLASH_MMU_PAGE_SIZE;
  398. int start, end, page_delta;
  399. intptr_t base;
  400. if (memory == SPI_FLASH_MMAP_DATA) {
  401. start = 0;
  402. end = 64;
  403. base = VADDR0_START_ADDR;
  404. page_delta = 0;
  405. } else {
  406. start = PRO_IRAM0_FIRST_USABLE_PAGE;
  407. end = 256;
  408. base = VADDR1_START_ADDR;
  409. page_delta = 64;
  410. }
  411. spi_flash_disable_interrupts_caches_and_other_cpu();
  412. DPORT_INTERRUPT_DISABLE();
  413. for (int i = start; i < end; i++) {
  414. if (DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[i]) == phys_page) {
  415. i -= page_delta;
  416. intptr_t cache_page = base + (SPI_FLASH_MMU_PAGE_SIZE * i);
  417. DPORT_INTERRUPT_RESTORE();
  418. spi_flash_enable_interrupts_caches_and_other_cpu();
  419. return (const void *) (cache_page | (phys_offs & (SPI_FLASH_MMU_PAGE_SIZE-1)));
  420. }
  421. }
  422. DPORT_INTERRUPT_RESTORE();
  423. spi_flash_enable_interrupts_caches_and_other_cpu();
  424. return NULL;
  425. }