From cbf223b837f6f57e617d9cc8f88b6e5222fa91fe Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Mon, 30 Nov 2020 01:38:17 +0100 Subject: [PATCH] Restructure kernel project --- .gitignore | 8 ++-- Makefile | 42 ++++---------------- ext/limine.cfg | 2 +- {src-stivale2 => kernel}/Makefile | 18 ++++++--- {src-stivale2 => kernel}/linker.ld | 0 {src-stivale2 => kernel/src}/main-stivale2.c | 0 {src-stivale2 => kernel}/stivale2.h | 0 src-stivale2/README.md | 27 ------------- 8 files changed, 26 insertions(+), 71 deletions(-) rename {src-stivale2 => kernel}/Makefile (52%) rename {src-stivale2 => kernel}/linker.ld (100%) rename {src-stivale2 => kernel/src}/main-stivale2.c (100%) rename {src-stivale2 => kernel}/stivale2.h (100%) delete mode 100644 src-stivale2/README.md diff --git a/.gitignore b/.gitignore index 30fd1c6..7e54056 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ -build/* -kernel.elf -kernel2.elf +kornos.img + +kernel/kernel + +obj/ *.o zig-cache/ diff --git a/Makefile b/Makefile index 0076909..bb0c0ff 100644 --- a/Makefile +++ b/Makefile @@ -1,55 +1,29 @@ -CC = zig cc -target x86_64-freestanding -LD = ld -KERNEL_HDD = build/disk.hdd +COMPONENTS = kernel -CFLAGS = -O2 -pipe -Wall -Wextra +KERNEL_HDD = kornos.img -CHARDFLAGS := $(CFLAGS) \ - -std=gnu99 \ - -masm=intel \ - -fno-pic \ - -mno-sse \ - -mno-sse2 \ - -mno-mmx \ - -mno-80387 \ - -mno-red-zone \ - -mcmodel=kernel \ - -ffreestanding \ - -fno-stack-protector \ - -Isrc/ \ +.PHONY: clean all $(COMPONENTS) -LDHARDFLAGS := $(LDFLAGS) \ - -static \ - -nostdlib \ - -no-pie \ - -z max-page-size=0x1000 \ - -T src/linker.ld - -.PHONY: clean all -.DEFAULT_GOAL = $(KERNEL_HDD) +$(COMPONENTS): + $(MAKE) -C $@ disk: $(KERNEL_HDD) run: $(KERNEL_HDD) qemu-system-x86_64 -m 2G -hda $(KERNEL_HDD) -src-stivale2/kernel2.elf: - $(MAKE) -C src-stivale2 - ext/limine/limine-install: $(MAKE) -C ext/limine/ limine-install -$(KERNEL_HDD): ext/limine/limine-install src-stivale2/kernel2.elf - -mkdir build +$(KERNEL_HDD): ext/limine/limine-install $(COMPONENTS) rm -f $(KERNEL_HDD) dd if=/dev/zero bs=1M count=0 seek=64 of=$(KERNEL_HDD) parted -s $(KERNEL_HDD) mklabel msdos parted -s $(KERNEL_HDD) mkpart primary 1 100% echfs-utils -m -p0 $(KERNEL_HDD) quick-format 32768 - echfs-utils -m -p0 $(KERNEL_HDD) import src-stivale/kernel.elf kernel.elf - echfs-utils -m -p0 $(KERNEL_HDD) import src-stivale2/kernel2.elf kernel2.elf + for f in $(COMPONENTS); do echo "Installing $$f"; echfs-utils -m -p0 $@ import $$f/$$f $$f; done echfs-utils -m -p0 $(KERNEL_HDD) import ext/limine.cfg limine.cfg ext/limine/limine-install ext/limine/limine.bin $(KERNEL_HDD) clean: rm -f $(KERNEL_HDD) - $(MAKE) -C src-stivale2 clean + for f in $(COMPONENTS); do $(MAKE) -C $$f clean; done diff --git a/ext/limine.cfg b/ext/limine.cfg index 31e9f93..615f083 100644 --- a/ext/limine.cfg +++ b/ext/limine.cfg @@ -2,5 +2,5 @@ TIMEOUT=5 :OS2 PROTOCOL=stivale2 -KERNEL_PATH=bios://:1/kernel2.elf +KERNEL_PATH=bios://:1/kernel KERNEL_CMDLINE=Hi! diff --git a/src-stivale2/Makefile b/kernel/Makefile similarity index 52% rename from src-stivale2/Makefile rename to kernel/Makefile index 23f6b0d..95ab890 100644 --- a/src-stivale2/Makefile +++ b/kernel/Makefile @@ -1,4 +1,4 @@ -TARGET := kernel2.elf +TARGET := kernel CC = zig cc -target x86_64-freestanding CFLAGS = -O2 @@ -6,18 +6,24 @@ LD = ld -m elf_x86_64 QEMU = qemu-system-x86_64 QEMUFLAGS = -m 1G -enable-kvm -cpu host LDINTERNALFLAGS := -Tlinker.ld -INTERNALCFLAGS := -I. \ +INTERNALCFLAGS := -I../ext/limine/stivale \ -CFILES := $(shell find ./ -type f -name '*.c') -OBJ := $(CFILES:.c=.o) +CFILES := $(shell find src -type f -name '*.c') +HFILES := $(shell find src -type f -name '*.h') +OBJ := $(patsubst src/%.c,obj/%.o,$(CFILES)) + +.PHONY: all clean obj all: $(TARGET) $(TARGET): $(OBJ) $(LD) $(LDINTERNALFLAGS) $(OBJ) -o $@ -%.o: %.c +obj/%.o: src/%.c $(HFILES) obj $(CC) $(CFLAGS) $(INTERNALCFLAGS) -c $< -o $@ +obj: + mkdir -p obj + clean: - rm -rf $(TARGET) *.o + rm -rf $(TARGET) obj zig-cache diff --git a/src-stivale2/linker.ld b/kernel/linker.ld similarity index 100% rename from src-stivale2/linker.ld rename to kernel/linker.ld diff --git a/src-stivale2/main-stivale2.c b/kernel/src/main-stivale2.c similarity index 100% rename from src-stivale2/main-stivale2.c rename to kernel/src/main-stivale2.c diff --git a/src-stivale2/stivale2.h b/kernel/stivale2.h similarity index 100% rename from src-stivale2/stivale2.h rename to kernel/stivale2.h diff --git a/src-stivale2/README.md b/src-stivale2/README.md deleted file mode 100644 index 7b3342c..0000000 --- a/src-stivale2/README.md +++ /dev/null @@ -1,27 +0,0 @@ -The stivale 2 protocol is a replacement for the stivale protocol that is able to be cleanly extended without breaking changes, it has support for extra features such as SMP compared to the older protocol. - -## the stivale 2 header -This structure must be present in the `.stivale2hdr` section in order for your kernel to be loaded by stivale 2. - -```c -struct stivale2_header { - uint64_t entry_point; - uint64_t stack; - uint64_t flags; - uint64_t tags; -} __attribute__((packed)); -``` - -If `entry_point` is 0 then the ELF entry point will be jumped to, otherwise the address specified will be used, `stack` will be loaded into the stack pointer register, as of now `flags` is 1 if KASLR is to be enabled. - -`tags` is a pointer to the first of a linked list of tags which determine the features requested by the kernel, the base structure of a tag is: - -```c -struct stivale2_hdr_tag { - uint64_t identifier; - uint64_t next; -} __attribute__((packed)); -``` - -Where `identifier` determines the type of the tag, while `next` determines the next tag, a value of `NULL` indicates the end of the list. -For further information on the supported tags consule the [Stivale2 Specification](https://github.com/limine-bootloader/limine/blob/master/STIVALE2.md)