From 08a80c7731a067cbd6110a265c2dc55133dac142 Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Mon, 30 Nov 2020 05:01:07 +0100 Subject: [PATCH] implement multi-architecure stuff --- kernel/Makefile | 32 +++++++++++-------- kernel/make.aarch64.config | 1 + kernel/make.x86_64.config | 1 + kernel/src/{ => arch/x86_64}/entry-stivale2.c | 2 +- kernel/src/arch/x86_64/ops.c | 4 +++ kernel/src/{ => arch/x86_64}/vga.c | 0 kernel/src/{ => arch/x86_64}/vga.h | 0 kernel/src/hal/ops.h | 1 + kernel/src/main.c | 5 +-- 9 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 kernel/make.aarch64.config create mode 100644 kernel/make.x86_64.config rename kernel/src/{ => arch/x86_64}/entry-stivale2.c (96%) create mode 100644 kernel/src/arch/x86_64/ops.c rename kernel/src/{ => arch/x86_64}/vga.c (100%) rename kernel/src/{ => arch/x86_64}/vga.h (100%) create mode 100644 kernel/src/hal/ops.h diff --git a/kernel/Makefile b/kernel/Makefile index 984a23b..1bd968a 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,27 +1,33 @@ +ARCH ?= $(shell uname -m) + +CC = zig cc -target $(ARCH)-freestanding +LD = ld -m elf_$(ARCH) + +-include make.$(ARCH).config + +CFLAGS ?= -O2 +CFLAGS := $(CFLAGS) $(ARCH_CFLAGS) \ + -Wall -Wextra -pedantic -Wno-language-extension-token -std=gnu11 -I../ext/limine/stivale -fPIE +LDFLAGS := $(LDFLAGS) -T linker.ld + + TARGET := kernel -CC = zig cc -target x86_64-freestanding -CFLAGS = -O2 -Wall -Wextra -Wno-language-extension-token -pedantic -std=gnu11 -pedantic -LD = ld -m elf_x86_64 -QEMU = qemu-system-x86_64 -QEMUFLAGS = -m 1G -enable-kvm -cpu host -LDINTERNALFLAGS := -Tlinker.ld -INTERNALCFLAGS := -I../ext/limine/stivale -mcmodel=kernel - -CFILES := $(shell find src -type f -name '*.c') +CFILES := $(shell find src -path src/arch -prune -o -type f -name '*.c' -print) +ARCHFILES := $(shell find src/arch/$(ARCH) -type f -name '*.c' -print) HFILES := $(shell find src -type f -name '*.h') -OBJ := $(patsubst src/%.c,obj/%.o,$(CFILES)) +OBJ := $(patsubst src/%.c,obj/%.o,$(CFILES) $(ARCHFILES)) .PHONY: all clean obj all: $(TARGET) $(TARGET): $(OBJ) - $(LD) $(LDINTERNALFLAGS) $(OBJ) -o $@ + $(LD) $(LDFLAGS) $(OBJ) -o $@ obj/%.o: src/%.c $(HFILES) - @mkdir -p obj - $(CC) $(CFLAGS) $(INTERNALCFLAGS) -c $< -o $@ + @mkdir -p obj/arch/$(ARCH) + $(CC) $(CFLAGS) -c $< -o $@ clean: rm -rf $(TARGET) obj zig-cache diff --git a/kernel/make.aarch64.config b/kernel/make.aarch64.config new file mode 100644 index 0000000..db9346d --- /dev/null +++ b/kernel/make.aarch64.config @@ -0,0 +1 @@ +LD=arm-none-eabi-ld \ No newline at end of file diff --git a/kernel/make.x86_64.config b/kernel/make.x86_64.config new file mode 100644 index 0000000..a1b4a65 --- /dev/null +++ b/kernel/make.x86_64.config @@ -0,0 +1 @@ +ARCH_CFLAGS = -mcmodel=kernel \ No newline at end of file diff --git a/kernel/src/entry-stivale2.c b/kernel/src/arch/x86_64/entry-stivale2.c similarity index 96% rename from kernel/src/entry-stivale2.c rename to kernel/src/arch/x86_64/entry-stivale2.c index c3a6174..35e306d 100644 --- a/kernel/src/entry-stivale2.c +++ b/kernel/src/arch/x86_64/entry-stivale2.c @@ -2,7 +2,7 @@ #include #include -#include "main.h" +#include "../../main.h" static uint8_t stack[4096] = {0}; void stivale2_main(struct stivale2_struct *info); diff --git a/kernel/src/arch/x86_64/ops.c b/kernel/src/arch/x86_64/ops.c new file mode 100644 index 0000000..4ebe70f --- /dev/null +++ b/kernel/src/arch/x86_64/ops.c @@ -0,0 +1,4 @@ +void halt_catch_fire() { + asm volatile ("cli"); + while(1) asm volatile ("hlt"); +} \ No newline at end of file diff --git a/kernel/src/vga.c b/kernel/src/arch/x86_64/vga.c similarity index 100% rename from kernel/src/vga.c rename to kernel/src/arch/x86_64/vga.c diff --git a/kernel/src/vga.h b/kernel/src/arch/x86_64/vga.h similarity index 100% rename from kernel/src/vga.h rename to kernel/src/arch/x86_64/vga.h diff --git a/kernel/src/hal/ops.h b/kernel/src/hal/ops.h new file mode 100644 index 0000000..138d132 --- /dev/null +++ b/kernel/src/hal/ops.h @@ -0,0 +1 @@ +void halt_catch_fire(); diff --git a/kernel/src/main.c b/kernel/src/main.c index afe663b..2e9d2f4 100644 --- a/kernel/src/main.c +++ b/kernel/src/main.c @@ -1,4 +1,5 @@ -#include "vga.h" +#include "arch/x86_64/vga.h" +#include "hal/ops.h" void kmain(int argc, char **argv) { vga_puts("Kernel initialized!\n"); @@ -10,5 +11,5 @@ void kmain(int argc, char **argv) { vga_setcolor(VGA_DARK_GRAY); vga_write_elsewhere("(c) Quinten Kock 2020 (MIT License)", 24, 0); - asm volatile ("hlt"); + halt_catch_fire(); }