NJU-ProjectN/nemu ics2023 initialized
NJU-ProjectN/nemu eb63cf3568dbf4e0c3c6ef462e6ec685550fabbc Merge pull request #76 from rijuyuezhu/master
This commit is contained in:
parent
1efe03efb9
commit
2824efad33
141 changed files with 19573 additions and 0 deletions
34
nemu/src/memory/Kconfig
Normal file
34
nemu/src/memory/Kconfig
Normal file
|
@ -0,0 +1,34 @@
|
|||
menu "Memory Configuration"
|
||||
|
||||
config MBASE
|
||||
hex "Memory base address"
|
||||
default 0x0 if ISA_x86
|
||||
default 0x80000000
|
||||
|
||||
config MSIZE
|
||||
hex "Memory size"
|
||||
default 0x8000000
|
||||
|
||||
config PC_RESET_OFFSET
|
||||
hex "Offset of reset vector from the base of memory"
|
||||
default 0x100000 if ISA_x86
|
||||
default 0
|
||||
|
||||
choice
|
||||
prompt "Physical memory definition"
|
||||
default PMEM_GARRAY
|
||||
config PMEM_MALLOC
|
||||
bool "Using malloc()"
|
||||
config PMEM_GARRAY
|
||||
depends on !TARGET_AM
|
||||
bool "Using global array"
|
||||
endchoice
|
||||
|
||||
config MEM_RANDOM
|
||||
depends on MODE_SYSTEM && !DIFFTEST && !TARGET_AM
|
||||
bool "Initialize the memory with random values"
|
||||
default y
|
||||
help
|
||||
This may help to find undefined behaviors.
|
||||
|
||||
endmenu #MEMORY
|
64
nemu/src/memory/paddr.c
Normal file
64
nemu/src/memory/paddr.c
Normal file
|
@ -0,0 +1,64 @@
|
|||
/***************************************************************************************
|
||||
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
|
||||
*
|
||||
* NEMU is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* See the Mulan PSL v2 for more details.
|
||||
***************************************************************************************/
|
||||
|
||||
#include <memory/host.h>
|
||||
#include <memory/paddr.h>
|
||||
#include <device/mmio.h>
|
||||
#include <isa.h>
|
||||
|
||||
#if defined(CONFIG_PMEM_MALLOC)
|
||||
static uint8_t *pmem = NULL;
|
||||
#else // CONFIG_PMEM_GARRAY
|
||||
static uint8_t pmem[CONFIG_MSIZE] PG_ALIGN = {};
|
||||
#endif
|
||||
|
||||
uint8_t* guest_to_host(paddr_t paddr) { return pmem + paddr - CONFIG_MBASE; }
|
||||
paddr_t host_to_guest(uint8_t *haddr) { return haddr - pmem + CONFIG_MBASE; }
|
||||
|
||||
static word_t pmem_read(paddr_t addr, int len) {
|
||||
word_t ret = host_read(guest_to_host(addr), len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void pmem_write(paddr_t addr, int len, word_t data) {
|
||||
host_write(guest_to_host(addr), len, data);
|
||||
}
|
||||
|
||||
static void out_of_bound(paddr_t addr) {
|
||||
panic("address = " FMT_PADDR " is out of bound of pmem [" FMT_PADDR ", " FMT_PADDR "] at pc = " FMT_WORD,
|
||||
addr, PMEM_LEFT, PMEM_RIGHT, cpu.pc);
|
||||
}
|
||||
|
||||
void init_mem() {
|
||||
#if defined(CONFIG_PMEM_MALLOC)
|
||||
pmem = malloc(CONFIG_MSIZE);
|
||||
assert(pmem);
|
||||
#endif
|
||||
IFDEF(CONFIG_MEM_RANDOM, memset(pmem, rand(), CONFIG_MSIZE));
|
||||
Log("physical memory area [" FMT_PADDR ", " FMT_PADDR "]", PMEM_LEFT, PMEM_RIGHT);
|
||||
}
|
||||
|
||||
word_t paddr_read(paddr_t addr, int len) {
|
||||
if (likely(in_pmem(addr))) return pmem_read(addr, len);
|
||||
IFDEF(CONFIG_DEVICE, return mmio_read(addr, len));
|
||||
out_of_bound(addr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void paddr_write(paddr_t addr, int len, word_t data) {
|
||||
if (likely(in_pmem(addr))) { pmem_write(addr, len, data); return; }
|
||||
IFDEF(CONFIG_DEVICE, mmio_write(addr, len, data); return);
|
||||
out_of_bound(addr);
|
||||
}
|
29
nemu/src/memory/vaddr.c
Normal file
29
nemu/src/memory/vaddr.c
Normal file
|
@ -0,0 +1,29 @@
|
|||
/***************************************************************************************
|
||||
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
|
||||
*
|
||||
* NEMU is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* See the Mulan PSL v2 for more details.
|
||||
***************************************************************************************/
|
||||
|
||||
#include <isa.h>
|
||||
#include <memory/paddr.h>
|
||||
|
||||
word_t vaddr_ifetch(vaddr_t addr, int len) {
|
||||
return paddr_read(addr, len);
|
||||
}
|
||||
|
||||
word_t vaddr_read(vaddr_t addr, int len) {
|
||||
return paddr_read(addr, len);
|
||||
}
|
||||
|
||||
void vaddr_write(vaddr_t addr, int len, word_t data) {
|
||||
paddr_write(addr, len, data);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue