Skip to content

Commit ebfcec0

Browse files
authored
Merge pull request #537 from ChinYikMing/refine-hardcoded-memory-layout
Refine hard-coded memory layout for system emulation
2 parents c1f85f5 + 19c3495 commit ebfcec0

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed

.github/workflows/main.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
- name: install-dependencies
4747
run: |
4848
sudo apt-get update -q -y
49-
sudo apt-get install -q -y libsdl2-dev libsdl2-mixer-dev device-tree-compiler expect
49+
sudo apt-get install -q -y libsdl2-dev libsdl2-mixer-dev device-tree-compiler expect bc
5050
.ci/riscv-toolchain-install.sh
5151
echo "${{ github.workspace }}/toolchain/bin" >> $GITHUB_PATH
5252
wget https://apt.llvm.org/llvm.sh
@@ -161,7 +161,7 @@ jobs:
161161
# No 'sudo' is available
162162
install: |
163163
apt-get update -q -y
164-
apt-get install -q -y git build-essential libsdl2-dev libsdl2-mixer-dev lsb-release wget software-properties-common gnupg
164+
apt-get install -q -y git build-essential libsdl2-dev libsdl2-mixer-dev lsb-release wget software-properties-common gnupg bc
165165
git config --global --add safe.directory ${{ github.workspace }}
166166
git config --global --add safe.directory ${{ github.workspace }}/src/softfloat
167167
git config --global --add safe.directory ${{ github.workspace }}/src/mini-gdbstub

Makefile

+36
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ $(call set-feature, BLOCK_CHAINING)
3030
ENABLE_SYSTEM ?= 0
3131
$(call set-feature, SYSTEM)
3232

33+
# Definition that bridges:
34+
# Device Tree(initrd, memory range)
35+
# src/io.c(memory init)
36+
# src/riscv.c(system emulation layout init)
37+
ifeq ($(call has, SYSTEM), 1)
38+
ifeq ($(call has, ELF_LOADER), 0)
39+
MiB = 1024*1024
40+
MEM_START ?= 0
41+
MEM_SIZE ?= 512 # unit in MiB
42+
DTB_SIZE ?= 1 # unit in MiB
43+
INITRD_SIZE ?= 8 # unit in MiB
44+
45+
compute_size = $(shell echo "obase=16; ibase=10; $(1)*$(MiB)" | bc)
46+
REAL_MEM_SIZE = $(call compute_size, $(MEM_SIZE))
47+
REAL_DTB_SIZE = $(call compute_size, $(DTB_SIZE))
48+
REAL_INITRD_SIZE = $(call compute_size, $(INITRD_SIZE))
49+
50+
CFLAGS_dt += -DMEM_START=0x$(MEM_START) \
51+
-DMEM_END=0x$(shell echo "obase=16; ibase=16; $(MEM_START)+$(REAL_MEM_SIZE)" | bc) \
52+
-DINITRD_START=0x$(shell echo "obase=16; ibase=16; \
53+
$(REAL_MEM_SIZE) - $(call compute_size, ($(INITRD_SIZE)+$(DTB_SIZE)))" | bc) \
54+
-DINITRD_END=0x$(shell echo "obase=16; ibase=16; \
55+
$(REAL_MEM_SIZE) - $(call compute_size, $(DTB_SIZE)) - 1" | bc)
56+
57+
CFLAGS += -DMEM_SIZE=0x$(REAL_MEM_SIZE) -DDTB_SIZE=0x$(REAL_DTB_SIZE) -DINITRD_SIZE=0x$(REAL_INITRD_SIZE)
58+
endif
59+
endif
60+
3361
# Enable link-time optimization (LTO)
3462
ENABLE_LTO ?= 1
3563
ifeq ($(call has, LTO), 1)
@@ -149,6 +177,14 @@ LDFLAGS += $(shell pkg-config --libs SDL2_mixer)
149177
endif
150178
endif
151179

180+
# If SYSTEM is enabled and ELF_LOADER is not, then skip FULL4G bacause guestOS
181+
# has dedicated memory mapping range.
182+
ifeq ($(call has, SYSTEM), 1)
183+
ifeq ($(call has, ELF_LOADER), 0)
184+
override ENABLE_FULL4G := 0
185+
endif
186+
endif
187+
152188
# Full access to a 4 GiB address space, necessitating more memory mapping
153189
# during emulator initialization.
154190
$(call set-feature, FULL4G)

mk/system.mk

+1-1
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

+3-3
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

-6
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -467,15 +467,15 @@ 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
/*
474474
* Load optional initrd image at last 8 MiB before the dtb region to
475475
* prevent kernel from overwritting it
476476
*/
477477
if (attr->data.system.initrd) {
478-
uint32_t initrd_addr = dtb_addr - (8 * 1024 * 1024);
478+
uint32_t initrd_addr = dtb_addr - INITRD_SIZE;
479479
ram_loc = ((char *) attr->mem->mem_base) + initrd_addr;
480480
map_file(&ram_loc, attr->data.system.initrd);
481481
}

0 commit comments

Comments
 (0)