From f8399152d4ccf5b41f83c648bf5b643d84285dbc Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Tue, 5 Aug 2025 20:53:07 +0200 Subject: [PATCH] Hello world! --- .gitignore | 2 ++ Makefile | 30 +++++++++++++++++++++ kernel/.gitignore | 2 ++ kernel/Makefile | 58 ++++++++++++++++++++++++++++++++++++++++ kernel/kmain.c | 40 ++++++++++++++++++++++++++++ kernel/link.ld | 68 +++++++++++++++++++++++++++++++++++++++++++++++ kernel/types.h | 15 +++++++++++ limine.conf | 5 ++++ 8 files changed, 220 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 kernel/.gitignore create mode 100644 kernel/Makefile create mode 100644 kernel/kmain.c create mode 100644 kernel/link.ld create mode 100644 kernel/types.h create mode 100644 limine.conf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f519d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +disk.hdd +/limine diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..835ce42 --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ +.PHONY: all disk clean + +disk: + if [ ! -d limine ]; then \ + git clone https://github.com/limine-bootloader/limine.git --branch=v9.x-binary --depth=1; \ + make -C limine; \ + fi + make -C kernel + rm -f disk.hdd + dd if=/dev/zero bs=1M count=0 seek=64 of=disk.hdd + PATH=$$PATH:/usr/sbin:/sbin sgdisk disk.hdd -n 1:2048 -t 1:ef00 -m 1 + ./limine/limine bios-install disk.hdd + mformat -i disk.hdd@@1M + mmd -i disk.hdd@@1M ::/EFI ::/EFI/BOOT ::/boot ::/boot/limine + mcopy -i disk.hdd@@1M kernel/mop2 ::/boot + mcopy -i disk.hdd@@1M limine.conf limine/limine-bios.sys ::/boot/limine + mcopy -i disk.hdd@@1M limine/BOOTX64.EFI ::/EFI/BOOT + +clean: + rm -rf limine disk.hdd + make -C kernel clean + +all: disk + +run: + qemu-system-x86_64 \ + -d guest_errors \ + -serial stdio \ + -hda disk.hdd + diff --git a/kernel/.gitignore b/kernel/.gitignore new file mode 100644 index 0000000..a6eee84 --- /dev/null +++ b/kernel/.gitignore @@ -0,0 +1,2 @@ +*.o +mop2 diff --git a/kernel/Makefile b/kernel/Makefile new file mode 100644 index 0000000..123fc6b --- /dev/null +++ b/kernel/Makefile @@ -0,0 +1,58 @@ +.PHONY: all clean + +CC := x86_64-elf-gcc +LD := x86_64-elf-ld + +override CFLAGS = -Wall \ + -Wextra \ + -ffreestanding \ + -fno-stack-protector \ + -fno-stack-check \ + -fno-lto \ + -fno-PIC \ + -ffunction-sections \ + -fdata-sections \ + -m64 \ + -march=x86-64 \ + -mabi=sysv \ + -mno-80387 \ + -mno-mmx \ + -mno-sse \ + -mno-sse2 \ + -mno-red-zone \ + -mcmodel=kernel \ + -I. \ + -I../limine + +override LDFLAGS = \ + -m elf_x86_64 \ + -nostdlib \ + -static \ + -z max-page-size=0x1000 \ + --gc-sections \ + -T link.ld + +override NASMFLAGS = \ + -felf64 \ + -Wall \ + -F dwarf -g + +override SRCFILES := $(shell find -L . -type f 2>/dev/null | LC_ALL=C sort) +override CFILES := $(filter %.c,$(SRCFILES)) +override ASFILES := $(filter %.asm,$(SRCFILES)) +override OBJ := $(patsubst %.c,%.o,$(CFILES)) $(patsubst %.asm,%.o,$(ASFILES)) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +%.o: %.asm + nasm $(NASMFLAGS) $< -o $@ + +all: mop2 + +mop2: $(OBJ) + $(LD) $^ $(LDFLAGS) -o $@ + +clean: + rm *.o mop2 + diff --git a/kernel/kmain.c b/kernel/kmain.c new file mode 100644 index 0000000..2d9ac02 --- /dev/null +++ b/kernel/kmain.c @@ -0,0 +1,40 @@ +#include +#include + +__attribute__((used, section(".limine_requests"))) +static volatile LIMINE_BASE_REVISION(3); + +__attribute__((used, section(".limine_requests"))) +static volatile struct limine_framebuffer_request framebuffer_request = { + .id = LIMINE_FRAMEBUFFER_REQUEST, + .revision = 0 +}; + +__attribute__((used, section(".limine_requests_start"))) +static volatile LIMINE_REQUESTS_START_MARKER; + +__attribute__((used, section(".limine_requests_end"))) +static volatile LIMINE_REQUESTS_END_MARKER; + +// Halt and catch fire function. +static void hcf(void) { + for (;;) { + asm ("hlt"); + } +} + +void kmain(void) { + if (LIMINE_BASE_REVISION_SUPPORTED == false) { + hcf(); + } + + if (framebuffer_request.response == NULL + || framebuffer_request.response->framebuffer_count < 1) { + hcf(); + } + + struct limine_framebuffer *framebuffer = framebuffer_request.response->framebuffers[0]; + + + hcf(); +} diff --git a/kernel/link.ld b/kernel/link.ld new file mode 100644 index 0000000..b8e1485 --- /dev/null +++ b/kernel/link.ld @@ -0,0 +1,68 @@ +/* Tell the linker that we want an x86_64 ELF64 output file */ +OUTPUT_FORMAT(elf64-x86-64) + +/* We want the symbol kmain to be our entry point */ +ENTRY(kmain) + +/* Define the program headers we want so the bootloader gives us the right */ +/* MMU permissions; this also allows us to exert more control over the linking */ +/* process. */ +PHDRS +{ + limine_requests PT_LOAD; + text PT_LOAD; + rodata PT_LOAD; + data PT_LOAD; +} + +SECTIONS +{ + /* We want to be placed in the topmost 2GiB of the address space, for optimisations */ + /* and because that is what the Limine spec mandates. */ + /* Any address in this region will do, but often 0xffffffff80000000 is chosen as */ + /* that is the beginning of the region. */ + . = 0xffffffff80000000; + + /* Define a section to contain the Limine requests and assign it to its own PHDR */ + .limine_requests : { + KEEP(*(.limine_requests_start)) + KEEP(*(.limine_requests)) + KEEP(*(.limine_requests_end)) + } :limine_requests + + /* Move to the next memory page for .text */ + . = ALIGN(CONSTANT(MAXPAGESIZE)); + + .text : { + *(.text .text.*) + } :text + + /* Move to the next memory page for .rodata */ + . = ALIGN(CONSTANT(MAXPAGESIZE)); + + .rodata : { + *(.rodata .rodata.*) + } :rodata + + /* Move to the next memory page for .data */ + . = ALIGN(CONSTANT(MAXPAGESIZE)); + + .data : { + *(.data .data.*) + } :data + + /* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */ + /* unnecessary zeros will be written to the binary. */ + /* If you need, for example, .init_array and .fini_array, those should be placed */ + /* above this. */ + .bss : { + *(.bss .bss.*) + *(COMMON) + } :data + + /* Discard .note.* and .eh_frame* since they may cause issues on some hosts. */ + /DISCARD/ : { + *(.eh_frame*) + *(.note .note.*) + } +} diff --git a/kernel/types.h b/kernel/types.h new file mode 100644 index 0000000..669a3cf --- /dev/null +++ b/kernel/types.h @@ -0,0 +1,15 @@ +#ifndef TYPES_H_ +#define TYPES_H_ + +#define NULL ((void *)0) + +typedef char int8; +typedef unsigned char uint8; +typedef short int16; +typedef unsigned short uint16; +typedef int int32; +typedef unsigned int uint32; +typedef long long int64; +typedef unsigned long long uint64; + +#endif // TYPES_H_ diff --git a/limine.conf b/limine.conf new file mode 100644 index 0000000..f6df4c3 --- /dev/null +++ b/limine.conf @@ -0,0 +1,5 @@ +timeout: 5 + +/mop2 + protocol: limine + path: boot():/boot/mop2