未验证 提交 b3e798d7 编写于 作者: Y Yinan Xu 提交者: GitHub

Merge pull request #279 from RISCVERS/increase-ram-size

support simulating larger ram by using mmap
......@@ -125,9 +125,8 @@ Emulator::Emulator(int argc, const char *argv[]):
}
Emulator::~Emulator() {
#ifdef WITH_DRAMSIM3
dramsim3_finish();
#endif
ram_finish();
#ifdef VM_SAVABLE
snapshot_slot[0].save();
snapshot_slot[1].save();
......
#include <sys/mman.h>
#include <zlib.h>
#include "common.h"
#include "ram.h"
#include <zlib.h>
#define RAMSIZE (256 * 1024 * 1024)
#define RAMSIZE (256 * 1024 * 1024UL)
#ifdef WITH_DRAMSIM3
#include "cosimulation.h"
CoDRAMsim3 *dram = NULL;
#endif
static uint64_t ram[RAMSIZE / sizeof(uint64_t)];
static uint64_t *ram;
static long img_size = 0;
void* get_img_start() { return &ram[0]; }
long get_img_size() { return img_size; }
void* get_ram_start() { return &ram[0]; }
long get_ram_size() { return RAMSIZE; }
#ifdef TLB_UNITTEST
void addpageSv39() {
//three layers
//addr range: 0x0000000080000000 - 0x0000000088000000 for 128MB from 2GB - 2GB128MB
......@@ -99,6 +101,7 @@ void addpageSv39() {
memcpy((char *)ram+(RAMSIZE-PAGESIZE*(PTENUM+PDENUM)), pde, PAGESIZE*PDENUM);
memcpy((char *)ram+(RAMSIZE-PAGESIZE*PTENUM), pte, PAGESIZE*PTENUM);
}
#endif
// Return whether the file is a gz file
int isGzFile(const char *img) {
......@@ -116,23 +119,28 @@ int readFromGz(void* ptr, const char *file_name) {
}
uint64_t curr_size = 0;
// read 16KB each time
const uint32_t chunk_size = 16384;
long *temp_page = new long[chunk_size];
long *pmem_current = (long*)ptr;
if ((RAMSIZE % chunk_size) != 0) {
printf("RAMSIZE must be divisible by chunk_size\n");
assert(0);
}
uint64_t *temp_page = new uint64_t[chunk_size];
uint64_t *pmem_current = (uint64_t *)ptr;
while (curr_size < RAMSIZE) {
uint32_t bytes_read = gzread(compressed_mem, temp_page, chunk_size);
if (bytes_read == 0) { break; }
assert(bytes_read % sizeof(long) == 0);
for (uint32_t x = 0; x < bytes_read / sizeof(long); x++) {
assert(bytes_read % sizeof(uint64_t) == 0);
for (uint32_t x = 0; x < bytes_read / sizeof(uint64_t); x++) {
if (*(temp_page + x) != 0) {
pmem_current = (long*)((uint8_t*)ptr + curr_size + x * sizeof(long));
pmem_current = (uint64_t*)((uint8_t*)ptr + curr_size + x * sizeof(uint64_t));
*pmem_current = *(temp_page + x);
}
}
curr_size += bytes_read;
}
printf("Read %lu bytes from gz stream in total", curr_size);
// printf("Read 0x%lx bytes from gz stream in total.\n", curr_size);
delete [] temp_page;
......@@ -148,19 +156,21 @@ void init_ram(const char *img) {
printf("The image is %s\n", img);
// initialize memory using Linux mmap
printf("Using simulated %luMB RAM\n", RAMSIZE / (1024 * 1024));
ram = (uint64_t *)mmap(NULL, RAMSIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (ram == (uint64_t *)MAP_FAILED) {
printf("Cound not mmap 0x%lx bytes\n", RAMSIZE);
assert(0);
}
int ret;
if (isGzFile(img)) {
printf("Read from gz file\n");
printf("Gzip file detected and loading image from extracted gz file\n");
img_size = readFromGz(ram, img);
if (img_size > RAMSIZE) {
img_size = RAMSIZE;
}
assert(img_size >= 0);
}
else {
printf("Read from bin file\n");
FILE *fp = fopen(img, "rb");
if (fp == NULL) {
printf("Can not open '%s'\n", img);
......@@ -180,9 +190,11 @@ void init_ram(const char *img) {
fclose(fp);
}
#ifdef TLB_UNITTEST
//new add
addpageSv39();
//new end
#endif
#ifdef WITH_DRAMSIM3
#if !defined(DRAMSIM3_CONFIG) || !defined(DRAMSIM3_OUTDIR)
......@@ -194,6 +206,13 @@ void init_ram(const char *img) {
}
void ram_finish() {
munmap(ram, RAMSIZE);
#ifdef WITH_DRAMSIM3
dramsim3_finish();
#endif
}
extern "C" uint64_t ram_read_helper(uint8_t en, uint64_t rIdx) {
if (en && rIdx >= RAMSIZE / sizeof(uint64_t)) {
printf("ERROR: ram idx = 0x%lx out of bound!\n", rIdx);
......
......@@ -3,8 +3,8 @@
#include "common.h"
// #define WITH_DRAMSIM3
void init_ram(const char *img);
void ram_finish();
#ifdef WITH_DRAMSIM3
// 4*64 bits
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册