From 9e091d913cc431034c95cb71380db3e9743fb700 Mon Sep 17 00:00:00 2001 From: ChinYikMing Date: Sun, 19 Jan 2025 02:12:28 +0800 Subject: [PATCH 1/2] Refine hard-coded memory layout for system emulation During the enhancement of guestOS to run SDL-based applications like Doom, the Doom artifacts (DOOM1.wad) occupy 4.0MiB, and the default rootfs.cpio also uses 4.0MiB, totaling 8MiB. When additional applications are added to rootfs.cpio, the hard-coded 8MiB limit for rootfs.cpio in map_file becomes insufficient. This commit refines the hard-coded memory layout variables and makes them configurable during the build. The user can now define MEM_SIZE, DTB_SIZE, and INITRD_SIZE in MiB, while other memory offsets are calculated dynamically based on these values. Related: #510 --- Makefile | 36 ++++++++++++++++++++++++++++++++++++ mk/system.mk | 2 +- src/devices/minimal.dts | 6 +++--- src/main.c | 6 ------ src/riscv.c | 4 ++-- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 8f68205a..f2c8d4b9 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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) diff --git a/mk/system.mk b/mk/system.mk index 8df44823..d6b028ab 100644 --- a/mk/system.mk +++ b/mk/system.mk @@ -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 diff --git a/src/devices/minimal.dts b/src/devices/minimal.dts index b682473b..cb4951de 100644 --- a/src/devices/minimal.dts +++ b/src/devices/minimal.dts @@ -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 = ; + linux,initrd-end = ; }; cpus { @@ -37,7 +37,7 @@ sram: memory@0 { device_type = "memory"; - reg = <0x00000000 0x20000000>; + reg = ; reg-names = "sram0"; }; diff --git a/src/main.c b/src/main.c index 80d3d5ee..861a3bb1 100644 --- a/src/main.c +++ b/src/main.c @@ -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)) { diff --git a/src/riscv.c b/src/riscv.c index e870db98..60fb1cac 100644 --- a/src/riscv.c +++ b/src/riscv.c @@ -467,7 +467,7 @@ 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); /* @@ -475,7 +475,7 @@ riscv_t *rv_create(riscv_user_t rv_attr) * 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); } From 19c34952ccb9908c3e4df5f4f40223252ed1288c Mon Sep 17 00:00:00 2001 From: ChinYikMing Date: Sun, 19 Jan 2025 15:57:22 +0800 Subject: [PATCH 2/2] CI: Install GNU bc arbitrary precision calculator Levaraging GNU bc to calculate the memory layout for the system emulation. --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5653c744..d6ea6069 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 @@ -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