-
Notifications
You must be signed in to change notification settings - Fork 0
/
GNUmakefile
355 lines (301 loc) · 9.46 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
# Nuke built-in rules and variables.
MAKEFLAGS += -rR
.SUFFIXES:
# Convenience macro to reliably declare user overridable variables.
override USER_VARIABLE = $(if $(filter $(origin $(1)),default undefined),$(eval override $(1) := $(2)))
# Target architecture to build for. Default to x86_64.
$(call USER_VARIABLE,ARCH,x86_64)
# Destination directory on install (should always be empty by default).
$(call USER_VARIABLE,DESTDIR,)
# Install prefix; /usr/local is a good, standard default pick.
$(call USER_VARIABLE,PREFIX,/usr/local)
# Check if the architecture is supported.
ifeq ($(filter $(ARCH),aarch64 loongarch64 riscv64 x86_64),)
$(error Architecture $(ARCH) not supported)
endif
$(call USER_VARIABLE,MEM,4G)
$(call USER_VARIABLE,SMP,4)
override IMAGE_NAME := template-$(ARCH)
DEFAULT_MACHINE=virt
ifeq ($(ARCH),x86_64)
DEFAULT_MACHINE=q35
endif
MACHINE=$(DEFAULT_MACHINE)
ifeq ($(ARCH),x86_64)
ifeq ($(DISABLE_ACPI),1)
$(error ACPI cannot be disabled on x86_64)
endif
else
ifeq ($(DISABLE_ACPI),1)
MACHINE := $(MACHINE),acpi=off
endif
endif
ifeq ($(ARCH),aarch64)
MACHINE := $(MACHINE),gic-version=max
endif
ifeq ($(ARCH),riscv64)
MACHINE := $(MACHINE),aclint=on,aia=aplic-imsic,aia-guests=1
endif
# Default user QEMU flags. These are appended to the QEMU command calls.
$(call USER_VARIABLE,QEMUFLAGS,-M $(MACHINE) -m $(MEM) -smp $(SMP))
EXTRA_QEMU_ARGS=-d unimp -d guest_errors -d int -D ./log.txt -rtc base=localtime
ifeq ($(DEBUG),1)
EXTRA_QEMU_ARGS += -s -S -serial stdio
endif
ifeq ($(CONSOLE),1)
EXTRA_QEMU_ARGS += -s -S -monitor stdio -no-reboot -no-shutdown
endif
ifneq ($(CONSOLE),1)
ifneq ($(DEBUG),1)
EXTRA_QEMU_ARGS += -serial stdio
endif
endif
DEFAULT_DRIVE_KIND=nvme
ifeq ($(ARCH),riscv64)
DEFAULT_DRIVE_KIND = scsi
endif
$(call USER_VARIABLE,DRIVE_KIND,$(DEFAULT_DRIVE_KIND))
$(call USER_VARIABLE,TRACE,)
ifneq ($(TRACE),)
TRACE_LIST=$(wordlist 1, 2147483647, $(TRACE))
EXTRA_QEMU_ARGS += $(foreach trace_var,$(TRACE_LIST),$(addprefix -trace , $(trace_var)))
endif
$(call USER_VARIABLE,DISABLE_FLANTERM,0)
$(call USER_VARIABLE,DEBUG_LOCKS,0)
$(call USER_VARIABLE,CHECK_SLABS,0)
VIRTIO_CD_QEMU_ARG=""
VIRTIO_HDD_QEMU_ARG=""
QEMU_CDROM_ARGS=\
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-$(ARCH).fd,readonly=on \
QEMU_HDD_ARGS=\
-drive if=pflash,unit=0,format=raw,file=ovmf/ovmf-code-$(ARCH).fd,readonly=on \
ifeq ($(DRIVE_KIND),block)
ifeq ($(ARCH),riscv64)
$(error "block device not supported on riscv64")
endif
QEMU_CDROM_ARGS += -cdrom $(IMAGE_NAME).iso
QEMU_HDD_ARGS += -hda $(IMAGE_NAME).hdd
endif
ifeq ($(DRIVE_KIND),scsi)
QEMU_CDROM_ARGS += \
-device virtio-scsi-pci,id=scsi \
-device scsi-cd,drive=cd0 \
-drive id=cd0,if=none,format=raw,file=$(IMAGE_NAME).iso
QEMU_HDD_ARGS += \
-device virtio-scsi-pci,id=scsi \
-device scsi-hd,drive=hd0 \
-drive id=hd0,if=none,format=raw,file=$(IMAGE_NAME).hdd
endif
ifeq ($(DRIVE_KIND),nvme)
$(call USER_VARIABLE,NVME_MAX_QUEUE_COUNT,64)
QEMU_CDROM_ARGS += \
-drive file=$(IMAGE_NAME).iso,if=none,id=osdrive,format=raw \
-device nvme,serial=1234,drive=osdrive,id=nvme0,max_ioqpairs=$(NVME_MAX_QUEUE_COUNT)
QEMU_HDD_ARGS += \
-drive file=$(IMAGE_NAME).hdd,if=none,id=osdrive,format=raw \
-device nvme,serial=1234,drive=osdrive,id=nvme0,max_ioqpairs=$(NVME_MAX_QUEUE_COUNT)
endif
.PHONY: all
all: $(IMAGE_NAME).iso
.PHONY: all-hdd
all-hdd: $(IMAGE_NAME).hdd
.PHONY: run
run: run-$(ARCH)
.PHONY: run-hdd
run-hdd: run-hdd-$(ARCH)
.PHONY: run-x86_64
run-x86_64: ovmf/ovmf-code-$(ARCH).fd $(IMAGE_NAME).iso
qemu-system-$(ARCH) \
-cpu max \
$(QEMUFLAGS) \
$(QEMU_CDROM_ARGS) \
$(EXTRA_QEMU_ARGS)
.PHONY: run-hdd-x86_64
run-hdd-x86_64: ovmf/ovmf-code-$(ARCH).fd $(IMAGE_NAME).hdd
qemu-system-$(ARCH) \
-cpu max \
$(QEMUFLAGS) \
$(QEMU_HDD_ARGS) \
$(EXTRA_QEMU_ARGS)
.PHONY: run-aarch64
run-aarch64: ovmf/ovmf-code-$(ARCH).fd $(IMAGE_NAME).iso
qemu-system-$(ARCH) \
-cpu max \
-device ramfb \
-device qemu-xhci \
-device usb-kbd \
-device usb-mouse \
$(QEMUFLAGS) \
$(QEMU_CDROM_ARGS) \
$(EXTRA_QEMU_ARGS)
.PHONY: run-hdd-aarch64
run-hdd-aarch64: ovmf/ovmf-code-$(ARCH).fd $(IMAGE_NAME).hdd
qemu-system-$(ARCH) \
-cpu max \
-device ramfb \
-device qemu-xhci \
-device usb-kbd \
-device usb-mouse \
$(QEMUFLAGS) \
$(QEMU_HDD_ARGS) \
$(EXTRA_QEMU_ARGS)
.PHONY: run-riscv64
run-riscv64: ovmf/ovmf-code-$(ARCH).fd $(IMAGE_NAME).iso
qemu-system-$(ARCH) \
-cpu max \
-device ramfb \
-device qemu-xhci \
-device usb-kbd \
-device usb-mouse \
$(QEMUFLAGS) \
$(QEMU_CDROM_ARGS) \
$(EXTRA_QEMU_ARGS)
.PHONY: run-hdd-riscv64
run-hdd-riscv64: ovmf/ovmf-code-$(ARCH).fd $(IMAGE_NAME).hdd
qemu-system-$(ARCH) \
-cpu max \
-device ramfb \
-device qemu-xhci \
-device usb-kbd \
-device usb-mouse \
$(QEMUFLAGS) \
$(QEMU_HDD_ARGS) \
$(EXTRA_QEMU_ARGS)
.PHONY: run-loongarch64
run-loongarch64: ovmf/ovmf-code-$(ARCH).fd $(IMAGE_NAME).iso
qemu-system-$(ARCH) \
-device ramfb \
-device qemu-xhci \
-device usb-kbd \
-device usb-mouse \
$(QEMUFLAGS) \
$(QEMU_CDROM_ARGS) \
$(EXTRA_QEMU_ARGS)
.PHONY: run-hdd-loongarch64
run-hdd-loongarch64: ovmf/ovmf-code-$(ARCH).fd $(IMAGE_NAME).hdd
qemu-system-$(ARCH) \
-device ramfb \
-device qemu-xhci \
-device usb-kbd \
-device usb-mouse \
$(QEMUFLAGS) \
$(QEMU_HDD_ARGS) \
$(EXTRA_QEMU_ARGS)
.PHONY: run-bios
run-bios: $(IMAGE_NAME).iso
qemu-system-x86_64 \
-cpu max \
-cdrom $(IMAGE_NAME).iso \
-boot d \
$(QEMUFLAGS) \
$(EXTRA_QEMU_ARGS)
.PHONY: run-hdd-bios
run-hdd-bios: $(IMAGE_NAME).hdd
qemu-system-x86_64 \
-cpu max \
-hda $(IMAGE_NAME).hdd \
$(QEMUFLAGS) \
$(EXTRA_QEMU_ARGS)
ovmf/ovmf-code-$(ARCH).fd:
mkdir -p ovmf
curl -Lo $@ https://github.com/osdev0/edk2-ovmf-nightly/releases/latest/download/ovmf-code-$(ARCH).fd
case "$(ARCH)" in \
aarch64) dd if=/dev/zero of=$@ bs=1 count=0 seek=67108864 2>/dev/null;; \
riscv64) dd if=/dev/zero of=$@ bs=1 count=0 seek=33554432 2>/dev/null;; \
esac
limine/limine:
rm -rf limine
git clone https://github.com/limine-bootloader/limine.git --branch=v8.x-binary --depth=1
$(MAKE) -C limine
kernel-deps:
./kernel/get-deps
touch kernel-deps
.PHONY: kernel
kernel: kernel-deps
$(MAKE) -C kernel DEBUG=$(DEBUG) DISABLE_FLANTERM=$(DISABLE_FLANTERM) DEBUG_LOCKS=$(DEBUG_LOCKS) CHECK_SLABS_=$(CHECK_SLABS)
$(IMAGE_NAME).iso: limine/limine kernel
rm -rf iso_root
mkdir -p iso_root/boot
cp -v kernel/bin-$(ARCH)/kernel iso_root/boot/
mkdir -p iso_root/boot/limine
cp -v limine.conf iso_root/boot/limine/
mkdir -p iso_root/EFI/BOOT
ifeq ($(ARCH),x86_64)
cp -v limine/limine-bios.sys limine/limine-bios-cd.bin limine/limine-uefi-cd.bin iso_root/boot/limine/
cp -v limine/BOOTX64.EFI iso_root/EFI/BOOT/
cp -v limine/BOOTIA32.EFI iso_root/EFI/BOOT/
xorriso -as mkisofs -R -r -J -b boot/limine/limine-bios-cd.bin \
-no-emul-boot -boot-load-size 4 -boot-info-table -hfsplus \
-apm-block-size 2048 --efi-boot boot/limine/limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
iso_root -o $(IMAGE_NAME).iso
./limine/limine bios-install $(IMAGE_NAME).iso
endif
ifeq ($(ARCH),aarch64)
cp -v limine/limine-uefi-cd.bin iso_root/boot/limine/
cp -v limine/BOOTAA64.EFI iso_root/EFI/BOOT/
xorriso -as mkisofs -R -r -J \
-hfsplus -apm-block-size 2048 \
--efi-boot boot/limine/limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
iso_root -o $(IMAGE_NAME).iso
endif
ifeq ($(ARCH),riscv64)
cp -v limine/limine-uefi-cd.bin iso_root/boot/limine/
cp -v limine/BOOTRISCV64.EFI iso_root/EFI/BOOT/
xorriso -as mkisofs -R -r -J \
-hfsplus -apm-block-size 2048 \
--efi-boot boot/limine/limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
iso_root -o $(IMAGE_NAME).iso
endif
ifeq ($(ARCH),loongarch64)
cp -v limine/limine-uefi-cd.bin iso_root/boot/limine/
cp -v limine/BOOTLOONGARCH64.EFI iso_root/EFI/BOOT/
xorriso -as mkisofs -R -r -J \
-hfsplus -apm-block-size 2048 \
--efi-boot boot/limine/limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
iso_root -o $(IMAGE_NAME).iso
endif
rm -rf iso_root
$(IMAGE_NAME).hdd: limine/limine kernel
rm -f $(IMAGE_NAME).hdd
dd if=/dev/zero bs=1M count=0 seek=64 of=$(IMAGE_NAME).hdd
sgdisk $(IMAGE_NAME).hdd -n 1:2048 -t 1:ef00
ifeq ($(ARCH),x86_64)
./limine/limine bios-install $(IMAGE_NAME).hdd
endif
mformat -i $(IMAGE_NAME).hdd@@1M
mmd -i $(IMAGE_NAME).hdd@@1M ::/EFI ::/EFI/BOOT ::/boot ::/boot/limine
mcopy -i $(IMAGE_NAME).hdd@@1M kernel/bin-$(ARCH)/kernel ::/boot
mcopy -i $(IMAGE_NAME).hdd@@1M limine.conf ::/boot/limine
ifeq ($(ARCH),x86_64)
mcopy -i $(IMAGE_NAME).hdd@@1M limine/limine-bios.sys ::/boot/limine
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTX64.EFI ::/EFI/BOOT
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTIA32.EFI ::/EFI/BOOT
endif
ifeq ($(ARCH),aarch64)
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTAA64.EFI ::/EFI/BOOT
endif
ifeq ($(ARCH),riscv64)
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTRISCV64.EFI ::/EFI/BOOT
endif
ifeq ($(ARCH),loongarch64)
mcopy -i $(IMAGE_NAME).hdd@@1M limine/BOOTLOONGARCH64.EFI ::/EFI/BOOT
endif
.PHONY: clean
clean:
$(MAKE) -C kernel clean
rm -rf iso_root-$(ARCH)/boot/limine || true
rm iso_root-$(ARCH)/boot/kernel || true
rm $(IMAGE_NAME).iso $(IMAGE_NAME).hdd || true
.PHONY: distclean
distclean:
$(MAKE) -C kernel clean
rm -rf iso_root-* *.iso *.hdd kernel-deps limine ovmf*
# Try to undo whatever the "install" target did.
.PHONY: uninstall
uninstall:
rm -f "$(DESTDIR)$(PREFIX)/share/$(OUTPUT)/$(OUTPUT)-$(ARCH)"
-rmdir "$(DESTDIR)$(PREFIX)/share/$(OUTPUT)"