Skip to content

Commit

Permalink
flash: fixed address with MAP_FIXED to avoid leakage (#150)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
poemonsense authored Sep 6, 2023
1 parent d65cd26 commit 8b7db86
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/device/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include <device/map.h>
#include <sys/mman.h>

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) {
Expand All @@ -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);
}

Expand Down

0 comments on commit 8b7db86

Please sign in to comment.