-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMakefile
406 lines (341 loc) · 12.4 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
QEMU_LAUNCHER := ./scripts/qemu-launcher.py
ifeq ($(DBG),1)
QEMU_LAUNCHER += --debug
endif
RISCV64_GCC ?= riscv64-linux-gnu-gcc
ifeq (, $(shell which $(RISCV64_GCC)))
RISCV64_GCC = riscv64-unknown-elf-gcc
endif
RISCV64_OBJDUMP ?= riscv64-linux-gnu-objdump
ifeq (, $(shell which $(RISCV64_OBJDUMP)))
RISCV64_OBJDUMP = riscv64-unknown-elf-objdump
endif
RISCV64_OBJCOPY ?= riscv64-linux-gnu-objcopy
ifeq (, $(shell which $(RISCV64_OBJCOPY)))
RISCV64_OBJCOPY = riscv64-unknown-elf-objcopy
endif
FREEDOM_SDK_DIR := sifive-freedom-toolchain
ifneq ("$(wildcard $(HOME)/binutils-gdb/bin/riscv-gdb)","")
GDB := $(HOME)/binutils-gdb/bin/riscv-gdb
else
GDB := $(FREEDOM_SDK_DIR)/*/bin/riscv64-unknown-elf-gdb
endif
# If the links below get outdated, head to https://www.sifive.com/software and
# download GNU Embedded Toolchain.
UNAME := $(shell uname)
ifeq ($(UNAME), Linux)
SIFIVE_TOOLCHAIN_URL := https://static.dev.sifive.com/dev-tools/freedom-tools/v2020.12/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz
endif
ifeq ($(UNAME), Darwin)
SIFIVE_TOOLCHAIN_URL := https://static.dev.sifive.com/dev-tools/freedom-tools/v2020.12/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-apple-darwin.tar.gz
endif
OUT := out
BINS := \
$(OUT)/os_sifive_u \
$(OUT)/os_sifive_e \
$(OUT)/os_sifive_e32 \
$(OUT)/os_test_sifive_e \
$(OUT)/os_test_sifive_e32 \
$(OUT)/os_sifive_u32 \
$(OUT)/os_hifive1_revb \
$(OUT)/os_ox64 \
$(OUT)/os_star64 \
$(OUT)/os_d1 \
$(OUT)/os_virt
# This target makes all the binaries depend on existence (but not timestamp) of
# $(OUT), which lets us avoid repetitive 'mkdir -p out'
$(BINS): | $(OUT)
all: $(BINS)
# boot.S has to go first in the list of dependencies so that the _start
# function is the very first thing in the .text section. Keeping it in a
# separate variable to prevent an occasional re-sorting of source files from
# moving it down the list. Let's keep context.S second in order to be compiled
# somewhere very low in the address space in order to fit in the trampoline
# page.
BOOT = src/boot.S \
src/context.S
BASE_DEPS = $(BOOT) \
src/bakedinfs.c \
src/baremetal-poweroff.S \
src/cpu.c \
src/drivers/drivers.c \
src/drivers/hd44780/hd44780.c \
src/drivers/uart/uart.c \
src/fdt.c \
src/fs.c \
src/gpio.c \
src/kernel.c \
src/kprintf.c \
src/kprintf.S \
src/mem.c \
src/pagealloc.c \
src/pipe.c \
src/plic.c \
src/pmp.c \
src/proc.c \
src/proc_test.c \
src/riscv.c \
src/runflags.c \
src/sbi.c \
src/spinlock.c \
src/string.c \
src/syscall.c \
src/syscalls.c \
src/timer.c \
src/vm-stub.c \
user/src/errno.c \
user/src/shell.c \
user/src/userland.c \
user/src/user-printf.c \
user/src/user-printf.S \
user/src/ustr.c \
user/src/usyscalls.S
OS_SIFIVE_U_DEPS = $(BASE_DEPS) \
src/drivers/uart/uart-generic.c \
src/machine/qemu/timer.c \
src/vm.c
OS_SIFIVE_U32_DEPS = $(BASE_DEPS) \
src/drivers/uart/uart-generic.c \
src/machine/qemu/timer.c
OS_SIFIVE_E_DEPS = $(BASE_DEPS) \
src/drivers/uart/uart-generic.c \
src/machine/qemu/timer.c
OS_SIFIVE_E32_DEPS = $(BASE_DEPS) \
src/drivers/uart/uart-generic.c \
src/machine/qemu/timer.c
OS_VIRT_DEPS = $(BASE_DEPS) \
src/drivers/uart/uart-ns16550a.c \
src/machine/qemu/timer.c \
src/vm.c
OS_OX64_DEPS = $(BASE_DEPS) \
src/drivers/uart/uart-ox64.c \
src/timer-sbi.c
OS_STAR64_DEPS = $(BASE_DEPS) \
src/drivers/uart/uart-dw-apb.c \
src/timer-sbi.c \
src/vm.c
OS_D1_DEPS = $(BASE_DEPS) \
src/drivers/uart/uart-dw-apb.c \
src/machine/d1/timer.c
.PHONY: run-u
run-u: $(OUT)/os_sifive_u
$(QEMU_LAUNCHER) --binary=$<
.PHONY: run-e
run-e: $(OUT)/os_sifive_e
$(QEMU_LAUNCHER) --binary=$<
.PHONY: run-e32
run-e32: $(OUT)/os_sifive_e32
$(QEMU_LAUNCHER) --binary=$<
.PHONY: run-e32t
run-e32t: $(OUT)/os_test_sifive_e32
$(QEMU_LAUNCHER) --binary=$<
.PHONY: run-u32
run-u32: $(OUT)/os_sifive_u32
$(QEMU_LAUNCHER) --binary=$<
.PHONY: run-virt
run-virt: $(OUT)/os_virt
$(QEMU_LAUNCHER) --binary=$<
# Note:
# * if this target complains that it can't find riscv64-unknown-elf-gdb,
# run make download-sifive-toolchain
# * .debug-session file is not there permanently; it's being created by
# qemu-launcher for the duration of the session and is being cleaned up on
# exit. The file contains the name of the binary to debug
# * qemu-launcher also creates a .gbdinit file so that gdb sets the correct
# arch and automatically attaches to the target
.PHONY: gdb
gdb:
$(GDB) $(shell cat .debug-session)
user/inc/usyscalls.h: include/syscalls.hh scripts/gen-syscalls.py
./scripts/gen-syscalls.py
user/inc/usyscalls.S: include/syscalls.hh scripts/gen-syscalls.py
./scripts/gen-syscalls.py
include/syscallnums.h: include/syscalls.hh scripts/gen-syscalls.py
./scripts/gen-syscalls.py
include/syscalldecls.h: include/syscalls.hh scripts/gen-syscalls.py
./scripts/gen-syscalls.py
src/syscalls.c: include/syscalls.hh scripts/gen-syscalls.py
./scripts/gen-syscalls.py
GCC_FLAGS=-static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles \
-ffreestanding \
-fno-plt -fno-pic \
-std=c17 \
--param=case-values-threshold=20 \
-Wa,-Iinclude \
-Tsrc/kernel.ld -Iinclude -Iuser/inc
$(OUT)/os_sifive_u: ${OS_SIFIVE_U_DEPS}
$(RISCV64_GCC) -march=rv64g -mabi=lp64 $(GCC_FLAGS) \
-g -include include/machine/qemu_u.h \
${OS_SIFIVE_U_DEPS} -o $@
$(OUT)/os_sifive_u32: ${OS_SIFIVE_U32_DEPS}
$(RISCV64_GCC) -march=rv32g -mabi=ilp32 $(GCC_FLAGS) \
-Wa,--defsym,XLEN=32 \
-g -include include/machine/qemu_u.h \
${OS_SIFIVE_U32_DEPS} -o $@
$(OUT)/os_sifive_e: ${OS_SIFIVE_E_DEPS}
$(RISCV64_GCC) -march=rv64g -mabi=lp64 $(GCC_FLAGS) \
-Wl,--defsym,ROM_START=0x20400000 -g \
-Wl,--defsym,RAM_SIZE=0x4000 \
-include include/machine/qemu_e.h \
${OS_SIFIVE_E_DEPS} -o $@
$(OUT)/os_sifive_e32: ${OS_SIFIVE_E32_DEPS}
$(RISCV64_GCC) -march=rv32g -mabi=ilp32 $(GCC_FLAGS) \
-Wl,--defsym,ROM_START=0x20400000 \
-Wa,--defsym,XLEN=32 \
-g -Wl,--defsym,RAM_SIZE=0x4000 \
-include include/machine/qemu_e.h \
${OS_SIFIVE_E32_DEPS} -o $@
$(OUT)/os_test_sifive_e32: ${OS_SIFIVE_E32_DEPS}
$(RISCV64_GCC) -march=rv32g -mabi=ilp32 $(GCC_FLAGS) \
-Wl,--defsym,ROM_START=0x20400000 \
-Wa,--defsym,XLEN=32 \
-g -Wl,--defsym,RAM_SIZE=0x4000 \
-DHARDCODED_TEST=0x1001 \
-include include/machine/qemu_e.h \
${OS_SIFIVE_E32_DEPS} -o $@
$(OUT)/os_test_sifive_e: ${OS_SIFIVE_E_DEPS}
$(RISCV64_GCC) -march=rv64g -mabi=lp64 $(GCC_FLAGS) \
-Wl,--defsym,ROM_START=0x20400000 -g \
-Wl,--defsym,RAM_SIZE=0x4000 \
-DHARDCODED_TEST=0x1001 \
-include include/machine/qemu_e.h \
${OS_SIFIVE_E_DEPS} -o $@
$(OUT)/os_virt: ${OS_VIRT_DEPS}
$(RISCV64_GCC) -march=rv64g -mabi=lp64 $(GCC_FLAGS) \
-Wa,--defsym,QEMU_EXIT=0x100000 -g \
-include include/machine/qemu_virt.h \
${OS_VIRT_DEPS} -o $@
$(OUT)/os_hifive1_revb: ${OS_SIFIVE_E32_DEPS}
$(RISCV64_GCC) -march=rv32imac -mabi=ilp32 $(GCC_FLAGS) \
-Wl,--defsym,ROM_START=0x20010000 \
-Wa,--defsym,XLEN=32 \
-Wl,--defsym,RAM_SIZE=0x4000 \
-include include/machine/hifive1-revb.h \
${OS_SIFIVE_E32_DEPS} -o $@
$(OUT)/os_ox64: ${OS_OX64_DEPS}
$(RISCV64_GCC) -march=rv64g -mabi=lp64 $(GCC_FLAGS) \
-Wl,--defsym,RAM_START=0x50200000 -g \
-include include/machine/ox64.h \
${OS_OX64_DEPS} -o $@
$(OUT)/os_ox64.bin: $(OUT)/os_ox64
$(RISCV64_OBJCOPY) -O binary $< $@
$(OUT)/os_ox64.s: $(OUT)/os_ox64
$(RISCV64_OBJDUMP) --source --all-headers --demangle --line-numbers --wide -D $< > $@
$(OUT)/os_star64: ${OS_STAR64_DEPS}
$(RISCV64_GCC) -march=rv64g -mabi=lp64 $(GCC_FLAGS) \
-Wl,--defsym,RAM_START=0x80200000 -g \
-include include/machine/star64.h \
${OS_STAR64_DEPS} -o $@
$(OUT)/os_star64.bin: $(OUT)/os_star64
$(RISCV64_OBJCOPY) -O binary $< $@
$(OUT)/os_star64.s: $(OUT)/os_star64
$(RISCV64_OBJDUMP) --source --all-headers --demangle --line-numbers --wide -D $< > $@
$(OUT)/os_d1: ${OS_D1_DEPS}
$(RISCV64_GCC) -march=rv64g -mabi=lp64 $(GCC_FLAGS) \
-Wl,--defsym,RAM_START=0x40200000 -g \
-include include/machine/d1.h \
${OS_D1_DEPS} -o $@
$(OUT)/os_d1.bin: $(OUT)/os_d1
$(RISCV64_OBJCOPY) -O binary $< $@
$(OUT)/os_d1.s: $(OUT)/os_d1
$(RISCV64_OBJDUMP) --source --all-headers --demangle --line-numbers --wide -D $< > $@
$(OUT)/test-output-u64.txt: $(OUT)/os_sifive_u
@$(QEMU_LAUNCHER) --bootargs dry-run --timeout=1s --binary=$< > $@
@diff -u testdata/want-output-u64.txt $@
@echo "OK"
$(OUT)/test-output-u32.txt: $(OUT)/os_sifive_u32
@$(QEMU_LAUNCHER) --bootargs dry-run --timeout=1s --binary=$< > $@
@diff -u testdata/want-output-u32.txt $@
@echo "OK"
$(OUT)/test-output-virt.txt: $(OUT)/os_virt
@$(QEMU_LAUNCHER) --bootargs dry-run --timeout=1s --binary=$< > $@
@diff -u testdata/want-output-virt.txt $@
@echo "OK"
$(OUT)/smoke-test-output-u32.txt: $(OUT)/os_sifive_u32
@$(QEMU_LAUNCHER) --bootargs test-script=/home/smoke-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-smoke-test-output-u32.txt $@
@echo "OK"
$(OUT)/smoke-test-output-u64.txt: $(OUT)/os_sifive_u
@$(QEMU_LAUNCHER) --bootargs test-script=/home/smoke-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-smoke-test-output-u64.txt $@
@echo "OK"
$(OUT)/smoke-test-output-virt.txt: $(OUT)/os_virt
@$(QEMU_LAUNCHER) --bootargs test-script=/home/smoke-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-smoke-test-output-virt.txt $@
@echo "OK"
$(OUT)/daemon-test-output-virt.txt: $(OUT)/os_virt
@$(QEMU_LAUNCHER) --bootargs test-script=/home/daemon-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-daemon-test-output-virt.txt $@
@echo "OK"
$(OUT)/daemon-test-output-u64.txt: $(OUT)/os_sifive_u
@$(QEMU_LAUNCHER) --bootargs test-script=/home/daemon-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-daemon-test-output-u64.txt $@
@echo "OK"
$(OUT)/daemon-test-output-tiny-stack.txt: $(OUT)/os_virt
@$(QEMU_LAUNCHER) --bootargs tiny-stack=/home/daemon-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-daemon-test-output-tiny-stack.txt $@
@echo "OK"
$(OUT)/ls-test-output-virt.txt: $(OUT)/os_virt
@$(QEMU_LAUNCHER) --bootargs test-script=/home/ls-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-ls-test-output-virt.txt $@
@echo "OK"
$(OUT)/smoke-test-output-tiny-stack.txt: $(OUT)/os_virt
@$(QEMU_LAUNCHER) --bootargs tiny-stack=/home/smoke-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-smoke-test-output-tiny-stack.txt $@
@echo "OK"
$(OUT)/leaky-test-output-u64.txt: $(OUT)/os_sifive_u
@$(QEMU_LAUNCHER) --bootargs test-script=/home/leaky-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-leaky-test-output-u64.txt $@
@echo "OK"
$(OUT)/leaky-test-output-u32.txt: $(OUT)/os_sifive_u32
@$(QEMU_LAUNCHER) --bootargs test-script=/home/leaky-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-leaky-test-output-u32.txt $@
@echo "OK"
$(OUT)/leaky-test-output-virt.txt: $(OUT)/os_virt
@$(QEMU_LAUNCHER) --bootargs test-script=/home/leaky-test.sh --timeout=5s --binary=$< > $@
@diff -u testdata/want-leaky-test-output-virt.txt $@
$(OUT)/smoke-test-output-e32.txt: $(OUT)/os_test_sifive_e32
@$(QEMU_LAUNCHER) --timeout=5s --binary=$< > $@
@diff -u testdata/want-smoke-test-output-e32.txt $@
@echo "OK"
$(OUT)/smoke-test-output-e64.txt: $(OUT)/os_test_sifive_e
@$(QEMU_LAUNCHER) --timeout=5s --binary=$< > $@
@diff -u testdata/want-smoke-test-output-e64.txt $@
@echo "OK"
$(OUT):
mkdir -p $(OUT)
$(OUT)/%.s: $(OUT)/%
$(RISCV64_OBJDUMP) -D $< > $@
$(OUT)/os_hifive1_revb.hex: $(OUT)/os_hifive1_revb
$(RISCV64_OBJCOPY) -O ihex $< $@
# This target assumes a Segger J-Link software is installed on the system. Get it at
# https://www.segger.com/downloads/jlink/#J-LinkSoftwareAndDocumentationPack
.PHONY: flash-hifive1-revb
flash-hifive1-revb: $(OUT)/os_hifive1_revb.hex $(OUT)/os_hifive1_revb.s
echo "loadfile $<\nrnh\nexit" \
| JLinkExe -device FE310 -if JTAG -speed 4000 -jtagconf -1,-1 -autoconnect 1
.PHONY: debug-board
debug-board: $(OUT)/os_hifive1_revb
JLinkGDBServer -device RISC-V -port 1234 &
$(GDB) $< -ex "set remotetimeout 240" -ex "target extended-remote localhost:1234"
.PHONY: clean
clean:
rm -Rf $(OUT)
.PHONY: tags
tags:
ctags --recurse include/ src/ user/
# On OSX:
# brew tap riscv-software-src/riscv
# brew install riscv-tools
prereqs:
sudo apt --yes install \
build-essential \
gcc-riscv64-linux-gnu
$(FREEDOM_SDK_DIR):
mkdir -p $@
wget -qO- "$(SIFIVE_TOOLCHAIN_URL)" | tar xzv -C "$@"
QEMU_DOCKER_IMG_NAME := qemu-image
# builds the docker image
.PHONY: build-qemu-image
build-qemu-image:
docker build -f scripts/Dockerfile -t ${QEMU_DOCKER_IMG_NAME} .