diff --git a/Makefile b/Makefile index 8f68205a..5f143f13 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) @@ -151,10 +179,15 @@ endif # Full access to a 4 GiB address space, necessitating more memory mapping # during emulator initialization. +# +# If SYSTEM and not ELF_LOADER is enabled, then skip this bacause guestOS +# has dedicated memory mapping range. $(call set-feature, FULL4G) ifeq ($(call has, FULL4G), 1) +ifeq ($(or $(not $(call has, SYSTEM)), $(and $(call has, SYSTEM), $(call has, ELF_LOADER))), 1) $(OUT)/main.o: CFLAGS += -DMEM_SIZE=0xFFFFFFFFULL # 2^{32} - 1 endif +endif ENABLE_GDBSTUB ?= 0 $(call set-feature, GDBSTUB) 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); }