-
Notifications
You must be signed in to change notification settings - Fork 4
/
Makefile
executable file
·186 lines (148 loc) · 4.93 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
OBJDIR := obj
INCDIR := include
BOOTDIR := boot
KERNDIR := kernel
LIBDIR := lib
USRDIR := user
FSDIR := fs
NETDIR := net
CC := gcc -pipe
AS := as
AR := ar
LD := ld
OBJCOPY := objcopy
OBJDUMP := objdump
NM := nm
CFLAGS := -Wall
CFLAGS += -Werror -Wno-address-of-packed-member
# Native commands
NATIVE_CFLAGS := $(CFLAGS) $(DEFS) $(LABDEFS) -I. -MD
# Compiler flags
# -fno-builtin is required to avoid refs to undefined functions in the kernel.
# Only optimize to -O1 to discourage inlining, which complicates backtraces.
CFLAGS := $(CFLAGS) $(DEFS) $(LABDEFS) -O1 -fno-builtin -I$(INCDIR) -MD
CFLAGS += -fno-omit-frame-pointer
CFLAGS += -std=gnu11
CFLAGS += -static
CFLAGS += -gstabs -m32
# -fno-tree-ch prevented gcc from sometimes reordering read_ebp() before
# mon_backtrace()'s function prologue on gcc version: (Debian 4.7.2-5) 4.7.2
CFLAGS += -fno-tree-ch
# include network header files
CFLAGS += -I$(NETDIR)/lwip/include \
-I$(NETDIR)/lwip/include/ipv4 \
-I$(NETDIR)/lwip/toynix
# 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 += -fstack-protector-all
# Common linker flags
LDFLAGS := -m elf_i386
GCC_LIB =
# GCC_LIB := $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
# Make sure that 'all' is the first target
all:
BOOT_CFLAGS := $(CFLAGS)
KERN_CFLAGS := $(CFLAGS) -DTOYNIX_KERNEL -gstabs
USER_CFLAGS := $(CFLAGS) -DTOYNIX_USER -gstabs
# 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
echo "$($*)" | cmp -s $@ || echo "$($*)" > $@
.PRECIOUS: $(OBJDIR)/.vars.%
.PHONY: FORCE
# Include Makefrags for subdirectories
include $(BOOTDIR)/Makefile
include $(LIBDIR)/Makefile
include $(USRDIR)/Makefile
include $(FSDIR)/Makefile
include $(NETDIR)/Makefile
include $(KERNDIR)/Makefile
clean:
rm -rf $(OBJDIR) .gdbinit qemu.log
# try to infer the correct QEMU
ifndef QEMU
QEMU := $(shell if which qemu >/dev/null 2>&1; \
then echo qemu; exit; \
elif which qemu-system-i386 >/dev/null 2>&1; \
then echo qemu-system-i386; exit; \
else \
qemu=/Applications/Q.app/Contents/MacOS/i386-softmmu.app/Contents/MacOS/i386-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
GDBPORT := $(shell expr `id -u` % 5000 + 25000)
CPUS ?= 4
PORT7 := $(shell expr $(GDBPORT) + 1)
PORT80 := $(shell expr $(GDBPORT) + 2)
# disk 0: kernel.img, disk 1: fs.img
QEMUOPTS = -m 256 -drive file=$(OBJDIR)/$(KERNDIR)/kernel.img,index=0,media=disk,format=raw -serial mon:stdio -gdb tcp::$(GDBPORT)
QEMUOPTS += $(shell if $(QEMU) -nographic -help | grep -q '^-D '; then echo '-D qemu.log'; fi)
IMAGES = $(OBJDIR)/$(KERNDIR)/kernel.img
QEMUOPTS += -smp $(CPUS)
QEMUOPTS += -drive file=$(OBJDIR)/$(FSDIR)/fs.img,index=1,media=disk,format=raw
IMAGES += $(OBJDIR)/$(FSDIR)/fs.img
QEMUOPTS += -net user -net nic,model=e1000 \
-nic user,hostfwd=tcp::$(PORT7)-:7 \
-nic user,hostfwd=tcp::$(PORT80)-:80 \
-nic user,hostfwd=udp::$(PORT7)-:7 \
# -net dump,file=qemu.pcap \
QEMUOPTS += $(QEMUEXTRA)
# debug bootloader, or debug kernel in default
#DEBUG_BOOT = -e "s/obj\/kernel\/kernel/obj\/boot\/boot.out/"
.gdbinit: .gdbinit.tmpl
sed -e "s/localhost:1234/localhost:$(GDBPORT)/" $(DEBUG_BOOT) < $^ > $@
gdb:
gdb -n -x .gdbinit
pre-qemu: .gdbinit
# QEMU doesn't truncate the pcap file. Work around this.
@rm -f qemu.pcap
qemu: $(IMAGES) pre-qemu
$(QEMU) $(QEMUOPTS)
qemu-nox: $(IMAGES) pre-qemu
@echo "***"
@echo "*** Use Ctrl-a x to exit qemu"
@echo "***"
$(QEMU) -nographic $(QEMUOPTS)
qemu-gdb: $(IMAGES) pre-qemu
@echo "***"
@echo "*** Now run 'make gdb'." 1>&2
@echo "***"
$(QEMU) $(QEMUOPTS) -S
qemu-nox-gdb: $(IMAGES) pre-qemu
@echo "***"
@echo "*** Now run 'make gdb'." 1>&2
@echo "***"
$(QEMU) -nographic $(QEMUOPTS) -S
# For test runs
prep-net_%: override INIT_CFLAGS+=-DTEST_NO_NS
prep-%:
$(MAKE) "INIT_CFLAGS=${INIT_CFLAGS} -DTEST=`case $* in *_*) echo $*;; *) echo user_$*;; esac`" $(IMAGES)
run-%-nox-gdb: prep-% pre-qemu
$(QEMU) -nographic $(QEMUOPTS) -S
run-%-gdb: prep-% pre-qemu
$(QEMU) $(QEMUOPTS) -S
run-%-nox: prep-% pre-qemu
$(QEMU) -nographic $(QEMUOPTS)
run-%: prep-% pre-qemu
$(QEMU) $(QEMUOPTS)
print-gdbport:
@echo $(GDBPORT)
# For network connections
which-ports:
@echo "Local port $(PORT7) forwards to port 7 (echo server)"
@echo "Local port $(PORT80) forwards to port 80 (web server)"
nc-80:
nc localhost $(PORT80)
nc-7:
nc localhost $(PORT7)
telnet-80:
telnet localhost $(PORT80)
telnet-7:
telnet localhost $(PORT7)