Skip to content

Commit c97b07e

Browse files
committed
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: sysprog21#510
1 parent 42d5378 commit c97b07e

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,42 @@ $(call set-feature, BLOCK_CHAINING)
3030
ENABLE_SYSTEM ?= 0
3131
$(call set-feature, SYSTEM)
3232

33+
# Enable logging use color
34+
ENABLE_LOG_COLOR ?= 1
35+
$(call set-feature, LOG_COLOR)
36+
37+
# Enable logging callback
38+
ENABLE_LOG_CALLBACK ?= 0
39+
$(call set-feature, LOG_CALLBACK)
40+
41+
# Definition that bridges:
42+
# Device Tree(initrd, memory range)
43+
# src/io.c(memory init)
44+
# src/riscv.c(system emulation layout init)
45+
ifeq ($(call has, SYSTEM), 1)
46+
ifeq ($(call has, ELF_LOADER), 0)
47+
MiB = 1024*1024
48+
MEM_START ?= 0
49+
MEM_SIZE ?= 512 # unit in MiB
50+
DTB_SIZE ?= 1 # unit in MiB
51+
INITRD_SIZE ?= 8 # unit in MiB
52+
53+
compute_size = $(shell echo "obase=16; ibase=10; $(1)*$(MiB)" | bc)
54+
REAL_MEM_SIZE = $(call compute_size, $(MEM_SIZE))
55+
REAL_DTB_SIZE = $(call compute_size, $(DTB_SIZE))
56+
REAL_INITRD_SIZE = $(call compute_size, $(INITRD_SIZE))
57+
58+
CFLAGS_dt += -DMEM_START=0x$(MEM_START) \
59+
-DMEM_END=0x$(shell echo "obase=16; ibase=16; $(MEM_START)+$(REAL_MEM_SIZE)" | bc) \
60+
-DINITRD_START=0x$(shell echo "obase=16; ibase=16; \
61+
$(REAL_MEM_SIZE) - $(call compute_size, ($(INITRD_SIZE)+$(DTB_SIZE)))" | bc) \
62+
-DINITRD_END=0x$(shell echo "obase=16; ibase=16; \
63+
$(REAL_MEM_SIZE) - $(call compute_size, $(DTB_SIZE)) - 1" | bc)
64+
65+
CFLAGS += -DMEM_SIZE=0x$(REAL_MEM_SIZE) -DDTB_SIZE=0x$(REAL_DTB_SIZE) -DINITRD_SIZE=0x$(REAL_INITRD_SIZE)
66+
endif
67+
endif
68+
3369
# Enable link-time optimization (LTO)
3470
ENABLE_LTO ?= 1
3571
ifeq ($(call has, LTO), 1)
@@ -149,6 +185,14 @@ LDFLAGS += $(shell pkg-config --libs SDL2_mixer)
149185
endif
150186
endif
151187

188+
# If SYSTEM is enabled and ELF_LOADER is not, then skip FULL4G bacause guestOS
189+
# has dedicated memory mapping range.
190+
ifeq ($(call has, SYSTEM), 1)
191+
ifeq ($(call has, ELF_LOADER), 0)
192+
override ENABLE_FULL4G := 0
193+
endif
194+
endif
195+
152196
# Full access to a 4 GiB address space, necessitating more memory mapping
153197
# during emulator initialization.
154198
$(call set-feature, FULL4G)

mk/system.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ DTC ?= dtc
1010
BUILD_DTB := $(OUT)/minimal.dtb
1111
$(BUILD_DTB): $(DEV_SRC)/minimal.dts
1212
$(VECHO) " DTC\t$@\n"
13-
$(Q)$(DTC) $^ -o $@
13+
$(Q)$(CC) -nostdinc -E -P -x assembler-with-cpp -undef $(CFLAGS_dt) $^ | $(DTC) - > $@
1414

1515
BIN_TO_C := $(OUT)/bin2c
1616
$(BIN_TO_C): tools/bin2c.c

src/devices/minimal.dts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
chosen {
1313
bootargs = "earlycon console=ttyS0";
1414
stdout-path = "serial0";
15-
linux,initrd-start = <0x1f700000>; /* @403 MiB (503 * 1024 * 1024) */
16-
linux,initrd-end = <0x1fefffff>; /* @511 MiB (511 * 1024 * 1024 - 1) */
15+
linux,initrd-start = <INITRD_START>;
16+
linux,initrd-end = <INITRD_END>;
1717
};
1818

1919
cpus {
@@ -37,7 +37,7 @@
3737

3838
sram: memory@0 {
3939
device_type = "memory";
40-
reg = <0x00000000 0x20000000>;
40+
reg = <MEM_START MEM_END>;
4141
reg-names = "sram0";
4242
};
4343

src/main.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,6 @@ void indirect_rv_halt()
226226
}
227227
#endif
228228

229-
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
230-
/* forcely undefine MEM_SIZE to prevent any define in Makefile */
231-
#undef MEM_SIZE
232-
#define MEM_SIZE 512 * 1024 * 1024
233-
#endif
234-
235229
int main(int argc, char **args)
236230
{
237231
if (argc == 1 || !parse_args(argc, args)) {

src/riscv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ riscv_t *rv_create(riscv_user_t rv_attr)
467467
char *ram_loc = (char *) attr->mem->mem_base;
468468
map_file(&ram_loc, attr->data.system.kernel);
469469

470-
uint32_t dtb_addr = attr->mem->mem_size - (1 * 1024 * 1024);
470+
uint32_t dtb_addr = attr->mem->mem_size - DTB_SIZE;
471471
ram_loc = ((char *) attr->mem->mem_base) + dtb_addr;
472472
load_dtb(&ram_loc, attr->data.system.bootargs);
473473
rv_log_info("DTB loaded");
@@ -476,7 +476,7 @@ riscv_t *rv_create(riscv_user_t rv_attr)
476476
* prevent kernel from overwritting it
477477
*/
478478
if (attr->data.system.initrd) {
479-
uint32_t initrd_addr = dtb_addr - (8 * 1024 * 1024);
479+
uint32_t initrd_addr = dtb_addr - INITRD_SIZE;
480480
ram_loc = ((char *) attr->mem->mem_base) + initrd_addr;
481481
map_file(&ram_loc, attr->data.system.initrd);
482482
}

0 commit comments

Comments
 (0)