Bootable multiboot2 kernel

This commit is contained in:
Thomas Lovén 2022-01-02 17:25:07 +01:00
parent fdcb46d6f2
commit e0979ed357
10 changed files with 107 additions and 2 deletions

7
.gitignore vendored
View File

@ -1,3 +1,8 @@
dist/ dist/
build/ build/
sysroot/ sysroot/
*.d
*.o
src/kernel/kernel

View File

@ -7,8 +7,18 @@ SYSROOT := $(BUILDROOT)/sysroot
DIST := $(BUILDROOT)/dist/mittos.iso DIST := $(BUILDROOT)/dist/mittos.iso
$(DIST): $(DIST):
setup-grub.sh
grub-mkrescue -o $@ $(SYSROOT) grub-mkrescue -o $@ $(SYSROOT)
kernel:
ifeq ($(shell make -sqC src/kernel || echo 1), 1)
$(MAKE) -C src/kernel install
endif
install: $(DIST) install: $(DIST)
clean:
rm -rf $(DIST)
$(MAKE) -C src/kernel clean
.PHONY: install .PHONY: install

2
make
View File

@ -1,3 +1,3 @@
#!/usr/bin/env bash #!/usr/bin/env bash
docker-compose -f toolchain/docker-compose.yml run --rm make "$@" docker-compose -f toolchain/docker-compose.yml run --rm -u $(id -u):$(id -g) make make "$@"

10
src/kernel/Link.ld Normal file
View File

@ -0,0 +1,10 @@
ENTRY(_start)
SECTIONS
{
.text :
{
*(.multiboot)
*(.text)
}
}

33
src/kernel/Makefile Normal file
View File

@ -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)

7
src/kernel/boot/boot.S Normal file
View File

@ -0,0 +1,7 @@
.intel_syntax noprefix
.section .text
.global _start
.code32
_start:
cli
jmp $

View File

@ -0,0 +1,15 @@
#include <multiboot.h>
.section .multiboot
.align 0x8
Multiboot2Header:
.long MBOOT2_MAGIC
.long MBOOT2_ARCH
.long MBOOT2_LENGTH
.long MBOOT2_CHECKSUM
.short 0
.short 0
.long 8
Multiboot2HeaderEnd:

View File

@ -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)

View File

@ -13,4 +13,9 @@ end
define reset define reset
monitor system_reset monitor system_reset
end
python
import os
gdb.execute('file ' + os.environ['BUILDROOT'] + '/sysroot/kernel')
end end

13
toolchain/setup-grub.sh Executable file
View File

@ -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