-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
232 lines (196 loc) · 8.24 KB
/
Makefile
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# RISCV should either be unset, or set to point to a directory that contains
# a toolchain install tree that was built via other means.
RISCV ?= $(CURDIR)/toolchain
PATH := $(RISCV)/bin:$(PATH)
ISA ?= rv64imafdc
ABI ?= lp64d
# choose opensbi or bbl here
BL ?= opensbi
# capstone-specific arguments
SPIKE_NCORES ?= 1
SPIKE_SECURE_MEM ?= 0x100000000:0x80000000
topdir := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
topdir := $(topdir:/=)
srcdir := $(topdir)/repo
confdir := $(topdir)/conf
wrkdir := $(CURDIR)/build
toolchain_srcdir := $(srcdir)/riscv-gnu-toolchain
toolchain_wrkdir := $(wrkdir)/riscv-gnu-toolchain
toolchain_dest := $(CURDIR)/toolchain
buildroot_srcdir := $(srcdir)/buildroot
buildroot_initramfs_wrkdir := $(wrkdir)/buildroot_initramfs
buildroot_initramfs_tar := $(buildroot_initramfs_wrkdir)/images/rootfs.tar
buildroot_initramfs_config := $(confdir)/buildroot_initramfs_config
buildroot_initramfs_sysroot_stamp := $(wrkdir)/.buildroot_initramfs_sysroot
buildroot_initramfs_sysroot := $(wrkdir)/buildroot_initramfs_sysroot
linux_srcdir := $(srcdir)/linux
linux_wrkdir := $(wrkdir)/linux
linux_defconfig := $(confdir)/linux_defconfig
vmlinux := $(linux_wrkdir)/vmlinux
vmlinux_stripped := $(linux_wrkdir)/vmlinux-stripped
linux_image := $(linux_wrkdir)/arch/riscv/boot/Image
pk_srcdir := $(srcdir)/riscv-pk
pk_wrkdir := $(wrkdir)/riscv-pk
bbl := $(pk_wrkdir)/bbl
pk := $(pk_wrkdir)/pk
opensbi_srcdir := $(srcdir)/opensbi
opensbi_wrkdir := $(wrkdir)/opensbi
fw_jump := $(opensbi_wrkdir)/platform/generic/firmware/fw_jump.elf
spike_srcdir := $(srcdir)/riscv-isa-sim
spike_wrkdir := $(wrkdir)/riscv-isa-sim
spike := $(toolchain_dest)/bin/spike
qemu_srcdir := $(srcdir)/riscv-gnu-toolchain/qemu
qemu_wrkdir := $(wrkdir)/qemu
qemu := $(toolchain_dest)/bin/qemu-system-riscv64
target_linux := riscv64-unknown-linux-gnu
target_newlib := riscv64-unknown-elf
.PHONY: all
all: sim-capstone
newlib: $(RISCV)/bin/$(target_newlib)-gcc
ifneq ($(RISCV),$(toolchain_dest))
$(RISCV)/bin/$(target_linux)-gcc:
$(error The RISCV environment variable was set, but is not pointing at a toolchain install tree)
endif
$(toolchain_dest)/bin/$(target_linux)-gcc: $(toolchain_srcdir)
mkdir -p $(toolchain_wrkdir)
$(MAKE) -C $(linux_srcdir) O=$(toolchain_wrkdir) ARCH=riscv INSTALL_HDR_PATH=$(abspath $(toolchain_srcdir)/linux-headers) headers_install
cd $(toolchain_wrkdir); $(toolchain_srcdir)/configure \
--prefix=$(toolchain_dest) \
--with-arch=$(ISA) \
--with-abi=$(ABI)
$(MAKE) -C $(toolchain_wrkdir) linux
# sed 's/^#define LINUX_VERSION_CODE.*/#define LINUX_VERSION_CODE 329226/' -i $(toolchain_dest)/sysroot/usr/include/linux/version.h
$(toolchain_dest)/bin/$(target_newlib)-gcc: $(toolchain_srcdir)
mkdir -p $(toolchain_wrkdir)
cd $(toolchain_wrkdir); $(toolchain_srcdir)/configure \
--prefix=$(toolchain_dest) \
--enable-multilib
$(MAKE) -C $(toolchain_wrkdir)
$(buildroot_initramfs_wrkdir)/.config: $(buildroot_srcdir)
rm -rf $(dir $@)
mkdir -p $(dir $@)
cp $(buildroot_initramfs_config) $@
$(MAKE) -C $< RISCV=$(RISCV) PATH="$(PATH)" O=$(buildroot_initramfs_wrkdir) olddefconfig CROSS_COMPILE=riscv64-unknown-linux-gnu-
$(buildroot_initramfs_tar): $(buildroot_srcdir) $(buildroot_initramfs_wrkdir)/.config $(RISCV)/bin/$(target_linux)-gcc $(buildroot_initramfs_config)
$(MAKE) -C $< RISCV=$(RISCV) PATH="$(PATH)" O=$(buildroot_initramfs_wrkdir)
.PHONY: buildroot_initramfs-menuconfig
buildroot_initramfs-menuconfig: $(buildroot_initramfs_wrkdir)/.config $(buildroot_srcdir)
$(MAKE) -C $(dir $<) O=$(buildroot_initramfs_wrkdir) menuconfig
$(MAKE) -C $(dir $<) O=$(buildroot_initramfs_wrkdir) savedefconfig
cp $(dir $<)/defconfig conf/buildroot_initramfs_config
$(buildroot_initramfs_sysroot): $(buildroot_initramfs_tar)
mkdir -p $(buildroot_initramfs_sysroot)
tar -xpf $< -C $(buildroot_initramfs_sysroot) --exclude ./dev --exclude ./usr/share/locale
$(linux_wrkdir)/.config: $(linux_defconfig) $(linux_srcdir)
mkdir -p $(dir $@)
cp -p $< $@
$(MAKE) -C $(linux_srcdir) O=$(linux_wrkdir) ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- olddefconfig
echo $(ISA)
echo $(filter rv32%,$(ISA))
ifeq (,$(filter rv%c,$(ISA)))
sed 's/^.*CONFIG_RISCV_ISA_C.*$$/CONFIG_RISCV_ISA_C=n/' -i $@
$(MAKE) -C $(linux_srcdir) O=$(linux_wrkdir) ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- olddefconfig
endif
ifeq ($(ISA),$(filter rv32%,$(ISA)))
sed 's/^.*CONFIG_ARCH_RV32I.*$$/CONFIG_ARCH_RV32I=y/' -i $@
sed 's/^.*CONFIG_ARCH_RV64I.*$$/CONFIG_ARCH_RV64I=n/' -i $@
$(MAKE) -C $(linux_srcdir) O=$(linux_wrkdir) ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- olddefconfig
endif
$(vmlinux): $(linux_srcdir) $(linux_wrkdir)/.config $(buildroot_initramfs_sysroot)
$(MAKE) -C $< O=$(linux_wrkdir) \
CONFIG_INITRAMFS_SOURCE="$(confdir)/initramfs.txt $(buildroot_initramfs_sysroot)" \
CONFIG_INITRAMFS_ROOT_UID=$(shell id -u) \
CONFIG_INITRAMFS_ROOT_GID=$(shell id -g) \
CROSS_COMPILE=riscv64-unknown-linux-gnu- \
ARCH=riscv \
KBUILD_CFLAGS_KERNEL="-save-temps=obj" \
all
$(vmlinux_stripped): $(vmlinux)
$(target_linux)-strip -o $@ $<
$(linux_image): $(vmlinux)
.PHONY: linux-menuconfig
linux-menuconfig: $(linux_wrkdir)/.config
$(MAKE) -C $(linux_srcdir) O=$(dir $<) ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- menuconfig
$(MAKE) -C $(linux_srcdir) O=$(dir $<) ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- savedefconfig
# cp $(dir $<)/defconfig conf/linux_defconfig
$(bbl): $(pk_srcdir) $(vmlinux_stripped)
rm -rf $(pk_wrkdir)
mkdir -p $(pk_wrkdir)
cd $(pk_wrkdir) && $</configure \
--host=$(target_linux) \
--with-payload=$(vmlinux_stripped) \
--enable-logo \
--with-logo=$(abspath conf/logo.txt) \
--with-dts=$(abspath conf/spike.dts)
CFLAGS="-mabi=$(ABI) -march=$(ISA)" $(MAKE) -C $(pk_wrkdir)
$(pk): $(pk_srcdir) $(RISCV)/bin/$(target_newlib)-gcc
rm -rf $(pk_wrkdir)
mkdir -p $(pk_wrkdir)
cd $(pk_wrkdir) && $</configure \
--host=$(target_newlib) \
--prefix=$(abspath $(toolchain_dest))
CFLAGS="-mabi=$(ABI) -march=$(ISA)" $(MAKE) -C $(pk_wrkdir)
$(MAKE) -C $(pk_wrkdir) install
$(fw_jump): $(RISCV)/bin/$(target_linux)-gcc $(opensbi_srcdir) $(linux_image)
rm -rf $(opensbi_wrkdir)
mkdir -p $(opensbi_wrkdir)
$(MAKE) -C $(opensbi_srcdir) FW_PAYLOAD_PATH=$(linux_image) PLATFORM=generic O=$(opensbi_wrkdir) CROSS_COMPILE=riscv64-unknown-linux-gnu-
$(spike): $(spike_srcdir)
rm -rf $(spike_wrkdir)
mkdir -p $(spike_wrkdir)
mkdir -p $(dir $@)
cd $(spike_wrkdir) && $</configure \
--prefix=$(dir $(abspath $(dir $@)))
$(MAKE) -C $(spike_wrkdir)
$(MAKE) -C $(spike_wrkdir) install
touch -c $@
$(qemu): $(qemu_srcdir)
rm -rf $(qemu_wrkdir)
mkdir -p $(qemu_wrkdir)
mkdir -p $(dir $@)
cd $(qemu_wrkdir) && $</configure \
--disable-docs \
--prefix=$(dir $(abspath $(dir $@))) \
--target-list=riscv64-linux-user,riscv64-softmmu
$(MAKE) -C $(qemu_wrkdir)
$(MAKE) -C $(qemu_wrkdir) install
touch -c $@
.PHONY: buildroot_initramfs_sysroot vmlinux bbl fw_image spike pk
buildroot_initramfs_sysroot: $(buildroot_initramfs_sysroot)
vmlinux: $(vmlinux)
bbl: $(bbl)
fw_image: $(fw_jump)
spike: $(spike)
pk: $(pk)
.PHONY: clean
clean:
rm -rf -- $(wrkdir) $(toolchain_dest)
ifeq ($(BL),opensbi)
# Simulate normal RISC-V Linux
.PHONY: sim
sim: $(fw_jump) $(spike)
$(spike) --isa=$(ISA) -p$(SPIKE_NCORES) --kernel $(linux_image) $(fw_jump)
# Simulate normal RISC-V Linux with QEMU
.PHONY: qemu
qemu: $(qemu) $(fw_jump)
$(qemu) -nographic -machine virt -m 256M -bios $(fw_jump) -kernel $(linux_image) \
-netdev user,id=net0 -device virtio-net-device,netdev=net0
# Simulate Capstone-RISC-V Linux
.PHONY: sim-capstone
sim-capstone: $(fw_jump) $(spike)
$(spike) --isa=$(ISA) -p$(SPIKE_NCORES) -M${SPIKE_SECURE_MEM} --kernel $(linux_image) $(fw_jump)
# Debugging
.PHONY: debug-capstone
debug-capstone: $(fw_jump) $(spike)
$(spike) --isa=$(ISA) -p$(SPIKE_NCORES) -M${SPIKE_SECURE_MEM} -D --kernel $(linux_image) $(fw_jump)
.PHONY: gdb-capstone
gdb-capstone: $(fw_jump) $(spike)
gdb --args $(spike) --isa=$(ISA) -p$(SPIKE_NCORES) -M${SPIKE_SECURE_MEM} -D --kernel $(linux_image) $(fw_jump)
.PHONY: valgrind-capstone
valgrind-capstone: $(fw_jump) $(spike)
valgrind --leak-check=full $(spike) --isa=$(ISA) -p$(SPIKE_NCORES) -M${SPIKE_SECURE_MEM} -D --kernel $(linux_image) $(fw_jump)
else ifeq ($(BL),bbl)
.PHONY: sim
sim: $(bbl) $(spike)
$(spike) --isa=$(ISA) -p$(SPIKE_NCORES) $(bbl)
endif