From e0979ed357e745eeb64176c5e8337f7eb8c65d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Sun, 2 Jan 2022 17:25:07 +0100 Subject: [PATCH] Bootable multiboot2 kernel --- .gitignore | 7 ++++++- Makefile | 10 +++++++++ make | 2 +- src/kernel/Link.ld | 10 +++++++++ src/kernel/Makefile | 33 ++++++++++++++++++++++++++++++ src/kernel/boot/boot.S | 7 +++++++ src/kernel/boot/multiboot_header.S | 15 ++++++++++++++ src/kernel/include/multiboot.h | 7 +++++++ toolchain/gdbinit | 5 +++++ toolchain/setup-grub.sh | 13 ++++++++++++ 10 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/kernel/Link.ld create mode 100644 src/kernel/Makefile create mode 100644 src/kernel/boot/boot.S create mode 100644 src/kernel/boot/multiboot_header.S create mode 100644 src/kernel/include/multiboot.h create mode 100755 toolchain/setup-grub.sh diff --git a/.gitignore b/.gitignore index 840d338..501990d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ dist/ build/ -sysroot/ \ No newline at end of file +sysroot/ + +*.d +*.o + +src/kernel/kernel \ No newline at end of file diff --git a/Makefile b/Makefile index 6328a18..07dbace 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,18 @@ SYSROOT := $(BUILDROOT)/sysroot DIST := $(BUILDROOT)/dist/mittos.iso $(DIST): + setup-grub.sh grub-mkrescue -o $@ $(SYSROOT) +kernel: +ifeq ($(shell make -sqC src/kernel || echo 1), 1) + $(MAKE) -C src/kernel install +endif + install: $(DIST) +clean: + rm -rf $(DIST) + $(MAKE) -C src/kernel clean + .PHONY: install \ No newline at end of file diff --git a/make b/make index dd1199d..596a495 100755 --- a/make +++ b/make @@ -1,3 +1,3 @@ #!/usr/bin/env bash -docker-compose -f toolchain/docker-compose.yml run --rm make "$@" \ No newline at end of file +docker-compose -f toolchain/docker-compose.yml run --rm -u $(id -u):$(id -g) make make "$@" \ No newline at end of file diff --git a/src/kernel/Link.ld b/src/kernel/Link.ld new file mode 100644 index 0000000..ed6473f --- /dev/null +++ b/src/kernel/Link.ld @@ -0,0 +1,10 @@ +ENTRY(_start) + +SECTIONS +{ + .text : + { + *(.multiboot) + *(.text) + } +} \ No newline at end of file diff --git a/src/kernel/Makefile b/src/kernel/Makefile new file mode 100644 index 0000000..6a9b12a --- /dev/null +++ b/src/kernel/Makefile @@ -0,0 +1,33 @@ + +CC := x86_64-elf-gcc + +SRC := $(wildcard **/*.[cS]) +OBJ := $(patsubst %, %.o, $(basename $(SRC))) + +CFLAGS := -Wall -Wextra -pedantic -ffreestanding +CFLAGS += -ggdb -O0 +ASFLAGS += -ggdb +CPPFLAGS += -I include +LDFLAGS := -n -nostdlib -lgcc -T Link.ld + +kernel: $(OBJ) + $(LINK.c) $^ -o $@ + +DEP := $(OBJ:.o=.d) +DEPFLAGS = -MT $@ -MMD -MP -MF $*.d +$(OBJ): CPPFLAGS += $(DEPFLAGS) +%.d: ; + +DESTDIR ?= $(BUILDROOT)/sysroot + +$(DESTDIR)$(PREFIX)/kernel: kernel + install -D kernel $(DESTDIR)$(PREFIX)/kernel + +install: $(DESTDIR)$(PREFIX)/kernel + +clean: + rm -rf $(OBJ) $(DEP) kernel + +.PHONY: install + +include $(DEP) \ No newline at end of file diff --git a/src/kernel/boot/boot.S b/src/kernel/boot/boot.S new file mode 100644 index 0000000..9df8fd3 --- /dev/null +++ b/src/kernel/boot/boot.S @@ -0,0 +1,7 @@ +.intel_syntax noprefix +.section .text +.global _start +.code32 +_start: +cli +jmp $ \ No newline at end of file diff --git a/src/kernel/boot/multiboot_header.S b/src/kernel/boot/multiboot_header.S new file mode 100644 index 0000000..ba08c0a --- /dev/null +++ b/src/kernel/boot/multiboot_header.S @@ -0,0 +1,15 @@ +#include +.section .multiboot + +.align 0x8 +Multiboot2Header: +.long MBOOT2_MAGIC +.long MBOOT2_ARCH +.long MBOOT2_LENGTH +.long MBOOT2_CHECKSUM + +.short 0 +.short 0 +.long 8 + +Multiboot2HeaderEnd: \ No newline at end of file diff --git a/src/kernel/include/multiboot.h b/src/kernel/include/multiboot.h new file mode 100644 index 0000000..9964bd1 --- /dev/null +++ b/src/kernel/include/multiboot.h @@ -0,0 +1,7 @@ +#pragma once + +#define MBOOT2_MAGIC 0xE85250D6 +#define MBOOT2_REPLY 0x36D76289 +#define MBOOT2_ARCH 0 +#define MBOOT2_LENGTH (Multiboot2HeaderEnd - Multiboot2Header) +#define MBOOT2_CHECKSUM -(MBOOT2_MAGIC + MBOOT2_ARCH + MBOOT2_LENGTH) diff --git a/toolchain/gdbinit b/toolchain/gdbinit index 4c381b6..45fd97e 100644 --- a/toolchain/gdbinit +++ b/toolchain/gdbinit @@ -13,4 +13,9 @@ end define reset monitor system_reset +end + +python +import os +gdb.execute('file ' + os.environ['BUILDROOT'] + '/sysroot/kernel') end \ No newline at end of file diff --git a/toolchain/setup-grub.sh b/toolchain/setup-grub.sh new file mode 100755 index 0000000..753806e --- /dev/null +++ b/toolchain/setup-grub.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +mkdir -p ${BUILDROOT}/sysroot/boot/grub + +cat > ${BUILDROOT}/sysroot/boot/grub/grub.cfg << EOF +set timeout=1 +set default=0 + +menuentry "mittos64" { + multiboot2 /kernel +} + +EOF \ No newline at end of file