Skip to content

Commit

Permalink
Merge pull request #537 from ChinYikMing/refine-hardcoded-memory-layout
Browse files Browse the repository at this point in the history
Refine hard-coded memory layout for system emulation
  • Loading branch information
jserv authored Jan 19, 2025
2 parents c1f85f5 + 19c3495 commit ebfcec0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: install-dependencies
run: |
sudo apt-get update -q -y
sudo apt-get install -q -y libsdl2-dev libsdl2-mixer-dev device-tree-compiler expect
sudo apt-get install -q -y libsdl2-dev libsdl2-mixer-dev device-tree-compiler expect bc
.ci/riscv-toolchain-install.sh
echo "${{ github.workspace }}/toolchain/bin" >> $GITHUB_PATH
wget https://apt.llvm.org/llvm.sh
Expand Down Expand Up @@ -161,7 +161,7 @@ jobs:
# No 'sudo' is available
install: |
apt-get update -q -y
apt-get install -q -y git build-essential libsdl2-dev libsdl2-mixer-dev lsb-release wget software-properties-common gnupg
apt-get install -q -y git build-essential libsdl2-dev libsdl2-mixer-dev lsb-release wget software-properties-common gnupg bc
git config --global --add safe.directory ${{ github.workspace }}
git config --global --add safe.directory ${{ github.workspace }}/src/softfloat
git config --global --add safe.directory ${{ github.workspace }}/src/mini-gdbstub
Expand Down
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ $(call set-feature, BLOCK_CHAINING)
ENABLE_SYSTEM ?= 0
$(call set-feature, SYSTEM)

# Definition that bridges:
# Device Tree(initrd, memory range)
# src/io.c(memory init)
# src/riscv.c(system emulation layout init)
ifeq ($(call has, SYSTEM), 1)
ifeq ($(call has, ELF_LOADER), 0)
MiB = 1024*1024
MEM_START ?= 0
MEM_SIZE ?= 512 # unit in MiB
DTB_SIZE ?= 1 # unit in MiB
INITRD_SIZE ?= 8 # unit in MiB

compute_size = $(shell echo "obase=16; ibase=10; $(1)*$(MiB)" | bc)
REAL_MEM_SIZE = $(call compute_size, $(MEM_SIZE))
REAL_DTB_SIZE = $(call compute_size, $(DTB_SIZE))
REAL_INITRD_SIZE = $(call compute_size, $(INITRD_SIZE))

CFLAGS_dt += -DMEM_START=0x$(MEM_START) \
-DMEM_END=0x$(shell echo "obase=16; ibase=16; $(MEM_START)+$(REAL_MEM_SIZE)" | bc) \
-DINITRD_START=0x$(shell echo "obase=16; ibase=16; \
$(REAL_MEM_SIZE) - $(call compute_size, ($(INITRD_SIZE)+$(DTB_SIZE)))" | bc) \
-DINITRD_END=0x$(shell echo "obase=16; ibase=16; \
$(REAL_MEM_SIZE) - $(call compute_size, $(DTB_SIZE)) - 1" | bc)

CFLAGS += -DMEM_SIZE=0x$(REAL_MEM_SIZE) -DDTB_SIZE=0x$(REAL_DTB_SIZE) -DINITRD_SIZE=0x$(REAL_INITRD_SIZE)
endif
endif

# Enable link-time optimization (LTO)
ENABLE_LTO ?= 1
ifeq ($(call has, LTO), 1)
Expand Down Expand Up @@ -149,6 +177,14 @@ LDFLAGS += $(shell pkg-config --libs SDL2_mixer)
endif
endif

# If SYSTEM is enabled and ELF_LOADER is not, then skip FULL4G bacause guestOS
# has dedicated memory mapping range.
ifeq ($(call has, SYSTEM), 1)
ifeq ($(call has, ELF_LOADER), 0)
override ENABLE_FULL4G := 0
endif
endif

# Full access to a 4 GiB address space, necessitating more memory mapping
# during emulator initialization.
$(call set-feature, FULL4G)
Expand Down
2 changes: 1 addition & 1 deletion mk/system.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ DTC ?= dtc
BUILD_DTB := $(OUT)/minimal.dtb
$(BUILD_DTB): $(DEV_SRC)/minimal.dts
$(VECHO) " DTC\t$@\n"
$(Q)$(DTC) $^ -o $@
$(Q)$(CC) -nostdinc -E -P -x assembler-with-cpp -undef $(CFLAGS_dt) $^ | $(DTC) - > $@

BIN_TO_C := $(OUT)/bin2c
$(BIN_TO_C): tools/bin2c.c
Expand Down
6 changes: 3 additions & 3 deletions src/devices/minimal.dts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
chosen {
bootargs = "earlycon console=ttyS0";
stdout-path = "serial0";
linux,initrd-start = <0x1f700000>; /* @403 MiB (503 * 1024 * 1024) */
linux,initrd-end = <0x1fefffff>; /* @511 MiB (511 * 1024 * 1024 - 1) */
linux,initrd-start = <INITRD_START>;
linux,initrd-end = <INITRD_END>;
};

cpus {
Expand All @@ -37,7 +37,7 @@

sram: memory@0 {
device_type = "memory";
reg = <0x00000000 0x20000000>;
reg = <MEM_START MEM_END>;
reg-names = "sram0";
};

Expand Down
6 changes: 0 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,6 @@ void indirect_rv_halt()
}
#endif

#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
/* forcely undefine MEM_SIZE to prevent any define in Makefile */
#undef MEM_SIZE
#define MEM_SIZE 512 * 1024 * 1024
#endif

int main(int argc, char **args)
{
if (argc == 1 || !parse_args(argc, args)) {
Expand Down
4 changes: 2 additions & 2 deletions src/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,15 @@ riscv_t *rv_create(riscv_user_t rv_attr)
char *ram_loc = (char *) attr->mem->mem_base;
map_file(&ram_loc, attr->data.system.kernel);

uint32_t dtb_addr = attr->mem->mem_size - (1 * 1024 * 1024);
uint32_t dtb_addr = attr->mem->mem_size - DTB_SIZE;
ram_loc = ((char *) attr->mem->mem_base) + dtb_addr;
load_dtb(&ram_loc, attr->data.system.bootargs);
/*
* Load optional initrd image at last 8 MiB before the dtb region to
* prevent kernel from overwritting it
*/
if (attr->data.system.initrd) {
uint32_t initrd_addr = dtb_addr - (8 * 1024 * 1024);
uint32_t initrd_addr = dtb_addr - INITRD_SIZE;
ram_loc = ((char *) attr->mem->mem_base) + initrd_addr;
map_file(&ram_loc, attr->data.system.initrd);
}
Expand Down

0 comments on commit ebfcec0

Please sign in to comment.