-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
130 lines (104 loc) · 4.03 KB
/
Makefile
File metadata and controls
130 lines (104 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
BOARD ?= virt
BUILD_UBOOT ?= true
SRCDIR = src/kernel
BUILDDIR = build
LIBDIR = $(SRCDIR)/lib
MEMDIR = $(SRCDIR)/memory
DRIVERDIR = $(SRCDIR)/drivers
USERDIR = src/user
DTS = $(SRCDIR)/device_tree/virt.dts
DTB_OBJ = $(BUILDDIR)/dtb.o
# Set DTB based on the BOARD value
ifeq ($(BOARD),virt)
DTS = $(SRCDIR)/device_tree/virt.dts
else ifeq ($(BOARD),board)
DTS = $(SRCDIR)/device_tree/deepcomp.dts
else
$(error Unsupported BOARD value: $(BOARD))
endif
DTB = $(BUILDDIR)/$(notdir $(DTS:.dts=.dtb))
DTB_OBJ = $(BUILDDIR)/dtb.o
# Compile rules
all: $(DTB_OBJ)
# Compile DTS to DTB
$(DTB): $(DTS)
dtc -q -I dts -O dtb -o $@ $<
# Convert DTB to object file
$(DTB_OBJ): $(DTB)
ld -r -b binary -o $@ $<
# Source files
KERNEL_SRC = $(SRCDIR)/kernel.c
C_SOURCES = $(filter-out $(KERNEL_SRC), $(wildcard $(SRCDIR)/*.c)) $(wildcard $(LIBDIR)/*.c) $(wildcard $(MEMDIR)/*.c) $(wildcard $(DRIVERDIR)/*.c)
ASM_SOURCES = $(wildcard $(SRCDIR)/*.s)
# Object files
C_OBJECTS = $(patsubst $(SRCDIR)/%.c, $(BUILDDIR)/%.o, $(C_SOURCES))
ASM_OBJECTS = $(patsubst $(SRCDIR)/%.s, $(BUILDDIR)/%.o, $(ASM_SOURCES))
KERNEL_OBJECT = $(BUILDDIR)/kernel.o
# Compiler settings
CC = riscv64-elf-gcc
AS = riscv64-elf-as
OBJCOPY = riscv64-elf-objcopy
CFLAGS = -Wall -Wextra -c -mcmodel=medany -ffreestanding -ggdb
LDFLAGS = -T $(SRCDIR)/linker.ld -nostdlib -lgcc
USERLDFLAGS = -T $(USERDIR)/user.ld -O2 -g3 -Wall -Wextra -fno-stack-protector -ffreestanding -nostdlib
# Qemu settings
QEMU = qemu-system-riscv64
define QFLAGS
-machine virt \
-cpu rv64,pmp=false \
-serial mon:stdio \
-device virtio-vga \
-bios opensbi/build/platform/generic/firmware/fw_jump.bin \
-kernel u-boot/u-boot.bin \
-device loader,file=build/kernel.bin,addr=0x84000000
endef
# Main kernel build (uses kernel.c)
damos: clean build_dirs $(DTB_OBJ) $(KERNEL_OBJECT) $(C_OBJECTS) $(ASM_OBJECTS)
$(CC) $(USERLDFLAGS) -o $(BUILDDIR)/shell.elf $(USERDIR)/shell.c $(USERDIR)/user.c
$(OBJCOPY) --set-section-flags .bss=alloc,contents -O binary $(BUILDDIR)/shell.elf $(BUILDDIR)/shell.bin
$(OBJCOPY) -Ibinary -Oelf64-littleriscv $(BUILDDIR)/shell.bin $(BUILDDIR)/shell.bin.o
$(CC) $(LDFLAGS) $(BUILDDIR)/shell.bin.o $(KERNEL_OBJECT) $(C_OBJECTS) $(ASM_OBJECTS) -o $(BUILDDIR)/kernel.elf
$(OBJCOPY) -I binary -O elf64-littleriscv -B riscv --rename-section .data=.dtb_blob $(DTB) $(DTB_OBJ)
$(CC) $(LDFLAGS) $(DTB_OBJ) $(BUILDDIR)/shell.bin.o $(KERNEL_OBJECT) $(C_OBJECTS) $(ASM_OBJECTS) -o $(BUILDDIR)/kernel.elf
riscv64-elf-objcopy -O binary $(BUILDDIR)/kernel.elf $(BUILDDIR)/kernel.bin
# Ensure build directories exist
build_dirs:
mkdir -p $(BUILDDIR) $(BUILDDIR)/lib $(BUILDDIR)/memory $(BUILDDIR)/drivers
# Compilation rules
$(BUILDDIR)/%.o: $(SRCDIR)/%.c | build_dirs
$(CC) $(CFLAGS) $< -o $@
$(BUILDDIR)/%.o: $(LIBDIR)/%.c | build_dirs
$(CC) $(CFLAGS) $< -o $@
$(BUILDDIR)/%.o: $(MEMDIR)/%.c | build_dirs
$(CC) $(CFLAGS) $< -o $@
$(BUILDDIR)/%.o: $(DRVDIR)/%.c | build_dirs
$(CC) $(CFLAGS) $< -o $@
$(BUILDDIR)/%.o: $(SRCDIR)/%.s | build_dirs
$(AS) -c $< -o $@
# Run main kernel
run: damos open-sbi
$(QEMU) $(QFLAGS)
tmux: damos open-sbi
@tmux split-window -h
@tmux send-keys "gdb -ex 'target remote localhost:1234' -ex 'symbol-file ./build/kernel.elf' -ex 'break *kmain' -ex 'c'" C-m
$(QEMU) $(QFLAGS) -s -S -nographic
debug: damos open-sbi
$(QEMU) $(QFLAGS) -s -S -nographic
present-virtual-memory: damos open-sbi
$(QEMU) $(QFLAGS) -s -S -nographic &
gdb -ex 'target remote localhost:1234' -ex 'symbol-file ./build/shell.elf' -ex 'break *main' -ex 'break *0x84000fb0' -ex 'c'
present-debugger: damos open-sbi
$(QEMU) $(QFLAGS) -s -S -nographic &
gdb -ex 'target remote localhost:1234' -ex 'symbol-file ./build/kernel.elf' -ex 'break *kmain' -ex 'c'
# Cleanup
clean:
rm -rf $(BUILDDIR)/*
open-sbi:
$(MAKE) -C opensbi PLATFORM=generic FW_TEXT_START=0x40000000 FW_OPTIONS=0
build-uboot: open-sbi
ifeq ($(BOARD),virt)
$(MAKE) -C u-boot qemu-riscv64_smode_defconfig
else
$(MAKE) -C u-boot starfive_visionfive2_defconfig
endif
MAKEFLAGS= $(MAKE) -C u-boot OPENSBI=../opensbi/build/platform/generic/firmware/fw_dynamic.bin