From 8b7db86fe88c4115f2252fa45fd2f5330a086e70 Mon Sep 17 00:00:00 2001 From: Yinan Xu Date: Wed, 6 Sep 2023 20:03:44 +0800 Subject: [PATCH] flash: fixed address with MAP_FIXED to avoid leakage (#150) We are using fixed virtual address for flash space to avoid resource leakage when the nemu so is instantiated multiple times. The reason is noted in paddr.c:79 according to the Linux manual at https://man7.org/linux/man-pages/man2/mmap.2.html. --- src/device/flash.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/device/flash.c b/src/device/flash.c index f7bd993fd..9729bf1dd 100644 --- a/src/device/flash.c +++ b/src/device/flash.c @@ -17,7 +17,9 @@ #include #include -static uint8_t *flash_base = NULL; +// put flash below the physical memory and allow a max size of 256MB. +// See the notes at paddr.c:79 for why we fix the address here. +static uint8_t *flash_base = (uint8_t *)0xf0000000ul; static FILE *fp = NULL; static void flash_io_handler(uint32_t offset, int len, bool is_write) { @@ -27,9 +29,11 @@ static void flash_io_handler(uint32_t offset, int len, bool is_write) { void load_flash_contents(const char *flash_img) { // create mmap with zero contents - flash_base = mmap(NULL, CONFIG_FLASH_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); - if (flash_base == MAP_FAILED) { - Log("mmap for flash failed"); + assert(CONFIG_FLASH_SIZE < 0x10000000UL); + void *ret = mmap((void *)flash_base, CONFIG_FLASH_SIZE, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); + if (ret != flash_base) { + perror("mmap"); assert(0); }