-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
152 lines (135 loc) · 3.69 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
# Use bash because why not?
SHELL:=/bin/bash
# We have to types of stuff: Kernel and user programs
K=kernel
U=user
F=$K/CrowFS
# Define the compilers
CC = gcc
LD = ld
AS = as
# Compiler flags for kernel
CFLAGS = -Wall \
-Wextra \
-Werror \
-std=gnu11 \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fno-lto \
-fno-PIC \
-m64 \
-march=x86-64 \
-mno-80387 \
-mno-mmx \
-mno-sse \
-mno-sse2 \
-mno-red-zone \
-masm=intel \
-D__CROWOS__ \
-ggdb -gdwarf-2 \
-O0 \
# Kernel compiling
KOBJS=$K/init.o \
$K/common/condvar.o \
$K/common/lib.o \
$K/common/printf.o \
$K/common/spinlock.o \
$K/cpu/gdt.o \
$K/cpu/idt.o \
$K/cpu/isr.o \
$K/cpu/trap.o \
$K/cpu/snippets.o \
$K/device/nvme.o \
$K/device/pcie.o \
$K/device/pic.o \
$K/device/serial_port.o \
$K/fs/device.o \
$K/fs/file.o \
$K/fs/fs.o \
$K/fs/syscall.o \
$K/mem/mem.o \
$K/mem/vmm.o \
$K/userspace/exec.o \
$K/userspace/ring3.o \
$K/userspace/proc.o \
$K/userspace/trampoline.o \
$K/userspace/syscall.o \
$F/crowfs.o \
LDFLAGS = -m elf_x86_64 \
-nostdlib \
-static \
-z max-page-size=0x1000
# Target specific variables https://stackoverflow.com/a/1305879/4213397
$(KOBJS): CFLAGS+=-Ikernel -I. -mcmodel=kernel
$(KOBJS): ASFLAGS+=-Ikernel -I.
$K/kernel: $(KOBJS) $K/linker.ld
$(LD) $(LDFLAGS) -T $K/linker.ld -o $@ $(KOBJS)
$K/cpu/isr.S: $K/cpu/isr.sh
$K/cpu/isr.sh > $K/cpu/isr.S
# Create the CrowFS interactor to create file system and manipulate
# files via a command line
$F/crowfs: $F/crowfs.c $F/main.c
gcc -O2 -o $F/crowfs $F/crowfs.c $F/main.c
# Userspace
# Create the syscall assembly file
$U/usyscalls.S: $U/usyscalls.sh
$U/usyscalls.sh > $U/usyscalls.S
# Define the userspace libraries
ULIB = $U/ulib.o $U/usyscalls.o $U/printf.o
$(ULIB): CFLAGS+=-Iuser -I.
$(ULIB): ASFLAGS+=-Iuser -I.
# User programs
UPROGS=$U/_init \
$U/_echo
%.o: CFLAGS+=-Iuser -I.
%.o: ASFLAGS+=-Iuser -I.
# Compile the user programs and the libraries
_%: %.o $(ULIB)
$(LD) $(LDFLAGS) -T $U/linker.ld -o $@ $^
# Creating the bootable image
boot/disk.img: $K/kernel boot/limine.conf boot/BOOTX64.EFI $F/crowfs $(UPROGS)
# Create the image
rm boot/disk.img || true
truncate -s 100M boot/disk.img
sgdisk boot/disk.img -n 1:2048:133119 -t 1:ef00 -n 2:133120 -t 2:8300
# Copy the EFI partition
mformat -i boot/disk.img@@1M
mmd -i boot/disk.img@@1M ::/EFI ::/EFI/BOOT
mcopy -i boot/disk.img@@1M $K/kernel ::/
mcopy -i boot/disk.img@@1M boot/limine.conf ::/
mcopy -i boot/disk.img@@1M boot/BOOTX64.EFI ::/EFI/BOOT
# Create the OS partition
sudo losetup --partscan /dev/loop0 boot/disk.img
sudo $F/crowfs /dev/loop0p2 new
# Copy the user programs
for prog in $(UPROGS); do \
sudo $F/crowfs /dev/loop0p2 copyin $$prog /$$(cut -d _ -f 2 <<< $$prog) ; \
done
# Unmount the OS partition
sudo losetup -d /dev/loop0
# Emulation
QEMU=qemu-system-x86_64
# Do not add KVM here or you are unable to debug the OS
QEMUOPT = -M q35 -smp 1 -m 128M -bios /usr/share/ovmf/OVMF.fd
QEMUOPT += -serial mon:stdio
QEMUOPT += -monitor telnet:127.0.0.1:38592,server,nowait
QEMUOPT += -drive file=boot/disk.img,if=none,id=nvm,format=raw -device nvme,serial=deadbeef,drive=nvm
#QEMUOPT += -d int,cpu_reset
.PHONY: qemu
qemu: boot/disk.img
$(QEMU) -cpu SandyBridge $(QEMUOPT)
.PHONY: qemu-kvm
qemu-kvm: boot/disk.img
$(QEMU) -enable-kvm -cpu host $(QEMUOPT)
.PHONY: qemu-gdb
qemu-gdb: boot/disk.img
@echo "*** Now run 'gdb' in another window." 1>&2
$(QEMU) -cpu SandyBridge $(QEMUOPT) -s -S
.PHONY: qemu-kvm-gdb
qemu-kvm-gdb: boot/disk.img
@echo "*** Now run 'gdb' in another window." 1>&2
$(QEMU) -enable-kvm -cpu host $(QEMUOPT) -s -S
.PHONY: clean
clean:
rm -f $K/kernel $K/*.o $K/**/*.o $K/cpu/isr.S $F/crowfs $U/*.o $U/**/*.o $U/_* $U/usyscalls.S boot/disk.img