-
Notifications
You must be signed in to change notification settings - Fork 0
/
GNUmakefile
457 lines (361 loc) · 12.8 KB
/
GNUmakefile
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#
# This makefile system follows the structuring conventions
# recommended by Peter Miller in his excellent paper:
#
# Recursive Make Considered Harmful
# http://aegis.sourceforge.net/auug97.pdf
#
OBJDIR := obj
SHELL := /bin/bash
# UEFI firmware definitions
JOS_LOADER_DEPS := LoaderPkg/Loader/*.c LoaderPkg/Loader/*.h LoaderPkg/Loader/*.inf LoaderPkg/*.dsc LoaderPkg/*.dec
JOS_LOADER_BUILD := LoaderPkg/UDK/Build/LoaderPkg
ifeq ($(ARCHS),IA32)
OVMF_FIRMWARE := LoaderPkg/Binaries/OVMF-IA32.fd
JOS_LOADER := LoaderPkg/Binaries/Loader-IA32.efi
JOS_LOADER_DEPS += LoaderPkg/Loader/Ia32/*.nasm LoaderPkg/Loader/Ia32/*.c
JOS_BOOTER := BOOTIa32.efi
else
OVMF_FIRMWARE := LoaderPkg/Binaries/OVMF-X64.fd
JOS_LOADER := LoaderPkg/Binaries/Loader-X64.efi
JOS_LOADER_DEPS += LoaderPkg/Loader/X64/*.c
JOS_BOOTER := BOOTX64.efi
endif
JOS_ESP := LoaderPkg/ESP
# Run 'make V=1' to turn on verbose commands, or 'make V=0' to turn them off.
ifeq ($(V),1)
override V =
endif
ifeq ($(V),0)
override V = @
endif
-include conf/lab.mk
-include conf/env.mk
LABSETUP ?= ./
TOP = .
ifdef JOSLLVM
CC := clang -target x86_64-gnu-linux -pipe
AS := $(shell command -v llvm-as >/dev/null 2>&1 && echo llvm-as || echo as)
AR := $(shell command -v llvm-ar >/dev/null 2>&1 && echo llvm-ar || echo ar)
LD := ld.lld
OBJCOPY := $(shell command -v llvm-objcopy >/dev/null 2>&1 && echo llvm/gnu-objcopy || echo objcopy)
OBJDUMP := $(shell command -v llvm-objdump >/dev/null 2>&1 && echo llvm-objdump || echo objdump)
NM := $(shell command -v llvm-nm >/dev/null 2>&1 && echo llvm-nm || echo nm)
EXTRA_CFLAGS += -Wno-self-assign -Wno-format-nonliteral -Wno-address-of-packed-member \
-Wno-frame-address -Wno-unknown-warning-option
GCC_LIB := $(shell A="$$($(CC) $(CFLAGS) -print-resource-dir)/lib"; [ -d "$$A/linux" ] && echo "$$A/linux" || echo "$$A/ispras")/libclang_rt.builtins-x86_64.a
else
# Cross-compiler jos toolchain
#
# This Makefile will automatically use the cross-compiler toolchain
# installed as 'i386-jos-elf-*', if one exists. If the host tools ('gcc',
# 'objdump', and so forth) compile for a 32-bit x86 ELF target, that will
# be detected as well. If you have the right compiler toolchain installed
# using a different name, set GCCPREFIX explicitly in conf/env.mk
# try to infer the correct GCCPREFIX
ifndef GCCPREFIX
GCCPREFIX := $(shell if x86_64-ispras-elf-objdump -i 2>&1 | grep '^elf64-x86-64$$' >/dev/null 2>&1; \
then echo 'x86_64-ispras-elf-'; \
elif objdump -i 2>&1 | grep 'elf64-x86-64' >/dev/null 2>&1; \
then echo ''; \
else echo "***" 1>&2; \
echo "*** Error: Couldn't find an x86_64-*-elf version of GCC/binutils." 1>&2; \
echo "*** Is the directory with x86_64-ispras-elf-gcc in your PATH?" 1>&2; \
echo "*** If your x86_64-*-elf toolchain is installed with a command" 1>&2; \
echo "*** prefix other than 'x86_64-ispras-elf-', set your GCCPREFIX" 1>&2; \
echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
echo "*** To turn off this error, run 'gmake GCCPREFIX= ...'." 1>&2; \
echo "*** Perhaps you wanted to use llvm toolchain, in this case ensure" 1>&2; \
echo "*** JOSLLVM environment variable is defined." 1>&2; \
echo "***" 1>&2; exit 1; fi)
endif
CC := $(GCCPREFIX)gcc -fno-pic -pipe
AS := $(GCCPREFIX)as
AR := $(GCCPREFIX)ar
LD := $(GCCPREFIX)ld
OBJCOPY := $(GCCPREFIX)objcopy
OBJDUMP := $(GCCPREFIX)objdump
NM := $(GCCPREFIX)nm
EXTRA_CFLAGS := $(EXTRA_CFLAGS) -Wno-unused-but-set-variable
GCC_LIB := $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
endif
# Native commands
NCC := gcc $(CC_VER) -pipe
NATIVE_CFLAGS := $(CFLAGS) $(DEFS) $(LABDEFS) -I$(TOP) -MD -Wall
TAR := gtar
PERL := perl
# Try to infer the correct QEMU
ifndef QEMU
QEMU := $(shell if which qemu-system-x86_64 > /dev/null 2>&1; \
then echo qemu-system-x86_64; exit; \
else \
qemu=/Applications/Q.app/Contents/MacOS/x86_64-softmmu.app/Contents/MacOS/x86_64-softmmu; \
if test -x $$qemu; then echo $$qemu; exit; fi; fi; \
echo "***" 1>&2; \
echo "*** Error: Couldn't find a working QEMU executable." 1>&2; \
echo "*** Is the directory containing the qemu binary in your PATH" 1>&2; \
echo "*** or have you tried setting the QEMU variable in conf/env.mk?" 1>&2; \
echo "***" 1>&2; exit 1)
endif
# Try to generate a unique GDB port if it is not set already.
ifeq ($(GDBPORT),)
GDBPORT := $(shell expr `id -u` % 5000 + 25000)
endif
# Compiler flags
# -fno-builtin is required to avoid refs to undefined functions in the kernel.
CFLAGS := $(CFLAGS) $(DEFS) $(LABDEFS) -fno-builtin -I$(TOP) -MD
ifeq ($(D),1)
CFLAGS += -O0
else
# Only optimize to -O1 to discourage inlining, which complicates backtraces.
CFLAGS += -O1
endif
CFLAGS += -ffreestanding -fno-omit-frame-pointer -mno-red-zone
CFLAGS += -Wall -Wformat=2 -Wno-unused-function -Werror -g -gpubnames
# Add -fno-stack-protector if the option exists.
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
CFLAGS += $(EXTRA_CFLAGS)
CFLAGS += -mno-sse -mno-sse2 -mno-mmx
KERN_SAN_CFLAGS :=
KERN_SAN_LDFLAGS :=
ifdef KASAN
CFLAGS += -DSAN_ENABLE_KASAN
# ***ITS REGEX TIME!***
# Instead of trying to keep all these addresses in sync
# just find them
# We need to find KERN_SHADOW_BASE and KERN_BASE_ADDR separately
# since SANITIZE_SHADOW_OFF is defined in terms of them and we need evaluated equvaluent
KERNBASE := $(shell sed -n 's/^\#define KERN_BASE_ADDR \(.*\)/\1/p' inc/memlayout.h)
KERN_SHADOW_BASE := $(shell sed -n 's/^\#define SANITIZE_SHADOW_BASE \(.*\)/\1/p' inc/memlayout.h)
KERN_SAN_CFLAGS := -fsanitize=address -fsanitize-blacklist=llvm/blacklist.txt -mllvm
KERN_SAN_CFLAGS += $(shell printf "\-asan-mapping-offset=0x%x" $$(($(KERN_SHADOW_BASE) - $(KERNBASE)/8)))
KERN_SAN_LDFLAGS := --wrap memcpy \
--wrap memset \
--wrap memmove \
--wrap bcopy \
--wrap bzero \
--wrap bcmp \
--wrap memcmp \
--wrap strcat \
--wrap strcpy \
--wrap strlcpy \
--wrap strncpy \
--wrap strlcat \
--wrap strncat \
--wrap strnlen \
--wrap strlen
endif
ifdef KUBSAN
CFLAGS += -DSAN_ENABLE_KUBSAN
KERN_SAN_CFLAGS += -fsanitize=undefined \
-fsanitize=implicit-integer-truncation \
-fno-sanitize=function \
-fno-sanitize=vptr \
-fno-sanitize=return
endif
USER_SAN_CFLAGS :=
USER_SAN_LDFLAGS :=
ifdef UASAN
CFLAGS += -DSAN_ENABLE_UASAN
# The definitions assume user base address at 0x0, see user/user.ld for details.
# SANITIZE_SHADOW_SIZE 32 MB allows 256 MB of addressible memory (due to byte granularity).
# Extra page (+0x1000 to offset) avoids an optimisation via 'or' that assumes that unsigned wrap-around is impossible.
USER_SAN_CFLAGS := -fsanitize=address -fsanitize-blacklist=llvm/ublacklist.txt -mllvm
USER_SAN_CFLAGS += $(shell sed -n 's/^\#define SANITIZE_USER_SHADOW_BASE \(.*\)/ -asan-mapping-offset=\1 /p' inc/memlayout.h)
USER_SAN_LDFLAGS := --wrap memcpy \
--wrap memset \
--wrap memmove \
--wrap bcopy \
--wrap bzero \
--wrap bcmp \
--wrap memcmp \
--wrap strcat \
--wrap strcpy \
--wrap strlcpy \
--wrap strncpy \
--wrap strlcat \
--wrap strncat \
--wrap strnlen \
--wrap strlen
endif
ifdef UUBSAN
CFLAGS += -DSAN_ENABLE_UUBSAN
USER_SAN_CFLAGS += -fsanitize=undefined \
-fsanitize=implicit-integer-truncation \
-fno-sanitize=function \
-fno-sanitize=vptr \
-fno-sanitize=return
endif
ifdef GRADE3_TEST
CFLAGS += -DGRADE3_TEST=$(GRADE3_TEST)
CFLAGS += -DGRADE3_FUNC=$(GRADE3_FUNC)
CFLAGS += -DGRADE3_FAIL=$(GRADE3_FAIL)
CFLAGS += -DGRADE3_PFX1=$(GRADE3_PFX1)
CFLAGS += -DGRADE3_PFX2=$(GRADE3_PFX2)
.SILENT:
endif
# Common linker flags
LDFLAGS := -m elf_x86_64 -z max-page-size=0x1000 --print-gc-sections --warn-common
# Linker flags for JOS programs
ULDFLAGS := -T user/user.ld --warn-common
# Lists that the */Makefrag makefile fragments will add to
OBJDIRS :=
# Make sure that 'all' is the first target
all: .git/hooks/post-checkout .git/hooks/pre-commit
# Eliminate default suffix rules
.SUFFIXES:
# Delete target files if there is an error (or make is interrupted)
.DELETE_ON_ERROR:
# make it so that no intermediate .o files are ever deleted
.PRECIOUS: $(OBJDIR)/kern/%.o \
$(OBJDIR)/lib/%.o $(OBJDIR)/fs/%.o $(OBJDIR)/net/%.o \
$(OBJDIR)/user/%.o \
$(OBJDIR)/prog/%.o
KERN_CFLAGS := $(CFLAGS) -DJOS_KERNEL -DLAB=$(LAB) -mcmodel=large -m64
USER_CFLAGS := $(CFLAGS) -DLAB=$(LAB) -mcmodel=large -m64
ifeq ($(CONFIG_KSPACE),y)
KERN_CFLAGS += -DCONFIG_KSPACE
USER_CFLAGS += -DCONFIG_KSPACE -DJOS_PROG
else
USER_CFLAGS += -DJOS_USER
endif
# Update .vars.X if variable X has changed since the last make run.
#
# Rules that use variable X should depend on $(OBJDIR)/.vars.X. If
# the variable's value has changed, this will update the vars file and
# force a rebuild of the rule that depends on it.
$(OBJDIR)/.vars.%: FORCE
@test -f $@ || touch $@
$(V)echo "$($*)" | cmp -s $@ || echo "$($*)" > $@
.PRECIOUS: $(OBJDIR)/.vars.%
.PHONY: FORCE
# Include Makefrags for subdirectories
include kern/Makefrag
include lib/Makefrag
ifeq ($(CONFIG_KSPACE),y)
include prog/Makefrag
else
include user/Makefrag
include fs/Makefrag
endif
QEMUOPTS = -hda fat:rw:$(JOS_ESP) -serial mon:stdio -gdb tcp::$(GDBPORT)
QEMUOPTS += -m 512M -d int,cpu_reset,mmu,pcall -no-reboot
QEMUOPTS += $(shell if $(QEMU) -display none -help | grep -q '^-D '; then echo '-D qemu.log'; fi)
IMAGES = $(OVMF_FIRMWARE) $(JOS_LOADER) $(OBJDIR)/kern/kernel $(JOS_ESP)/EFI/BOOT/kernel $(JOS_ESP)/EFI/BOOT/$(JOS_BOOTER)
ifeq ($(CONFIG_SNAPSHOT),y)
QEMUOPTS += -drive file=$(OBJDIR)/fs/fs.img,if=ide,snapshot=on
else
QEMUOPTS += -drive file=$(OBJDIR)/fs/fs.img,if=ide
endif
IMAGES += $(OBJDIR)/fs/fs.img
QEMUOPTS += -bios $(OVMF_FIRMWARE)
# QEMUOPTS += -debugcon file:$(UEFIDIR)/debug.log -global isa-debugcon.iobase=0x402
define POST_CHECKOUT
#!/bin/sh -x
make clean
endef
export POST_CHECKOUT
define PRE_COMMIT
#!/bin/sh
if git diff --cached --name-only --diff-filter=DMR | grep -q grade
then
echo "FAIL: Don't change grade files."
exit 1
else
exit 0
fi
endef
export PRE_COMMIT
.git/hooks/post-checkout:
@echo "$$POST_CHECKOUT" > $@
@chmod +x $@
.git/hooks/pre-commit:
@echo "$$PRE_COMMIT" > $@
@chmod +x $@
.gdbinit: .gdbinit.tmpl
sed "s/localhost:1234/localhost:$(GDBPORT)/" < $^ > $@
$(OVMF_FIRMWARE):
LoaderPkg/build_ovmf.sh
$(JOS_LOADER): $(OVMF_FIRMWARE) $(JOS_LOADER_DEPS)
LoaderPkg/build_ldr.sh
$(JOS_ESP)/EFI/BOOT/kernel: $(OBJDIR)/kern/kernel
mkdir -p $(JOS_ESP)/EFI/BOOT
cp $(OBJDIR)/kern/kernel $(JOS_ESP)/EFI/BOOT/kernel
$(JOS_ESP)/EFI/BOOT/$(JOS_BOOTER): $(JOS_LOADER)
mkdir -p $(JOS_ESP)/EFI/BOOT
cp $(JOS_LOADER) $(JOS_ESP)/EFI/BOOT/$(JOS_BOOTER)
# cp $(JOSLOADER)/Loader.debug $(UEFIDIR)/EFI/BOOT/BOOTIA32.DEBUG
pre-qemu: .gdbinit
qemu: $(IMAGES) pre-qemu
$(QEMU) $(QEMUOPTS)
qemu-oscheck: $(IMAGES) pre-qemu
$(QEMU) $(QEMUOPTS) -oscourse
qemu-oscheck-nox-gdb: $(IMAGES) pre-qemu
@echo "***"
@echo "*** Now run 'gdb'." 1>&2
@echo "***"
$(QEMU) -nographic $(QEMUOPTS) -S -oscourse
qemu-nox: $(IMAGES) pre-qemu
@echo "***"
@echo "*** Use Ctrl-a x to exit qemu"
@echo "***"
$(QEMU) -display none $(QEMUOPTS)
qemu-gdb: $(IMAGES) pre-qemu
@echo "***"
@echo "*** Now run 'gdb'." 1>&2
@echo "***"
$(QEMU) $(QEMUOPTS) -S
qemu-nox-gdb: $(IMAGES) pre-qemu
@echo "***"
@echo "*** Now run 'gdb'." 1>&2
@echo "***"
$(QEMU) -display none $(QEMUOPTS) -S
print-qemu:
@echo $(QEMU)
print-gdbport:
@echo $(GDBPORT)
format:
@find . -name *.[ch] -not -path "./LoaderPkg/*" -exec clang-format -i {} \;
# For deleting the build
clean:
rm -rf $(OBJDIR) .gdbinit jos.in qemu.log $(JOS_LOADER) $(JOS_LOADER_BUILD) $(JOS_ESP) kern/kernel.ld
realclean: clean
rm -rf lab$(LAB).tar.gz \
jos.out $(wildcard jos.out.*) \
qemu.pcap $(wildcard qemu.pcap.*)
distclean: realclean
rm -f .git/hooks/pre-commit .git/hooks/post-checkout $(OVMF_FIRMWARE) LoaderPkg/efibuild.sh
ifneq ($(V),@)
GRADEFLAGS += -v
endif
grade:
@echo $(MAKE) clean
@$(MAKE) clean || \
(echo "'make clean' failed. HINT: Do you have another running instance of JOS?" && exit 1)
ARCHS=X64 ./grade-lab$(LAB) $(GRADEFLAGS)
@echo $(MAKE) clean
@$(MAKE) clean || \
(echo "'make clean' failed. HINT: Do you have another running instance of JOS?" && exit 1)
ARCHS=IA32 ./grade-lab$(LAB) $(GRADEFLAGS)
# For test runs
prep-%:
$(V)$(MAKE) "INIT_CFLAGS=${INIT_CFLAGS} -DTEST=`case $* in *_*) echo $*;; *) echo user_$*;; esac`" $(IMAGES)
run-%-nox-gdb: prep-% pre-qemu
$(QEMU) -display none $(QEMUOPTS) -S
run-%-gdb: prep-% pre-qemu
$(QEMU) $(QEMUOPTS) -S
run-%-nox: prep-% pre-qemu
$(QEMU) -display none $(QEMUOPTS)
run-%: prep-% pre-qemu
$(QEMU) $(QEMUOPTS)
# This magic automatically generates makefile dependencies
# for header files included from C source files we compile,
# and keeps those dependencies up-to-date every time we recompile.
# See 'mergedep.pl' for more information.
$(OBJDIR)/.deps: $(foreach dir, $(OBJDIRS), $(wildcard $(OBJDIR)/$(dir)/*.d))
@mkdir -p $(@D)
@$(PERL) mergedep.pl $@ $^
-include $(OBJDIR)/.deps
always:
@:
.PHONY: all always clean realclean distclean grade