Skip to content

Commit

Permalink
Refine hard-coded memory layout for system emulation
Browse files Browse the repository at this point in the history
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: sysprog21#510
  • Loading branch information
ChinYikMing committed Jan 19, 2025
1 parent e2993fc commit 7425cb6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
33 changes: 33 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 @@ -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)
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 7425cb6

Please sign in to comment.