diff options
author | Samuel Johnson <[email protected]> | 2025-06-27 17:46:28 -0400 |
---|---|---|
committer | Samuel Johnson <[email protected]> | 2025-06-27 17:46:28 -0400 |
commit | 12fd3a91f138bf90520134682e38bf3b0341e660 (patch) | |
tree | 66b3f4a19f50a0f9883ca3f5a286a1e8dc3bc8c6 | |
parent | 6e65e584a3baf2dfe8906c2d847ca5169b91b3fc (diff) |
Create kernel build infrastructure
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | LICENSE | 11 | ||||
-rw-r--r-- | Makefile | 68 | ||||
-rw-r--r-- | kernel/cxx/constructors.cxx | 18 | ||||
-rw-r--r-- | kernel/cxx/gcc_required_functions.cxx | 1 | ||||
-rw-r--r-- | kernel/cxx/icxxabi.cxx | 0 | ||||
-rw-r--r-- | kernel/cxx/icxxabi.h | 0 | ||||
-rw-r--r-- | kernel/entry.cxx | 49 | ||||
-rw-r--r-- | kernel/ldscript | 69 | ||||
-rw-r--r-- | limine.conf | 5 | ||||
-rwxr-xr-x | toolchain/binutils.sh | 16 |
11 files changed, 241 insertions, 2 deletions
@@ -1,4 +1,8 @@ +bin/ +iso/ +toolchain/ vendor/ -build/ +*.iso +*.o *.swp @@ -0,0 +1,11 @@ +Copyright 2025 Samuel Johnson + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0b534fb --- /dev/null +++ b/Makefile @@ -0,0 +1,68 @@ +CC := vendor/bin/x86_64-elf-g++ +LD := vendor/bin/x86_64-elf-ld + +CFLAGS := -g -O2 -pipe +override CFLAGS += \ + -Wall \ + -Wextra \ + -std=c++17 \ + -ffreestanding \ + -mno-red-zone \ + -msoft-float \ + -mno-sse \ + -fno-stack-protector \ + -fno-stack-check \ + -fno-rtti \ + -fno-exceptions \ + -ffunction-sections \ + -fdata-sections \ + -mcmodel=kernel + +KERNEL := bin/core +K_OBJS := \ + kernel/entry.o \ + kernel/cxx/constructors.o \ + kernel/cxx/icxxabi.o +K_LINKSCRIPT := kernel/ldscript + +BINARIES := \ + $(KERNEL) + +OVMF_ROOT := /usr/share/qemu + +.PHONY: all clean run + +all: hinterOS.iso + +run: + qemu-system-x86_64 --bios $(OVMF_ROOT)/ovmf-x86_64.bin -cdrom hinterOS.iso + +clean: + - find kernel -type f -name '*.o' -exec rm {} + + - rm hinterOS.iso + - rm $(BINARIES) + +hinterOS.iso: $(BINARIES) + rm -rf iso + mkdir -p iso/bin + cp -v $^ iso/bin/ + mkdir -p iso/boot + mkdir -p iso/boot/limine + cp -v limine.conf iso/boot/limine/ + mkdir -p iso/EFI/BOOT + cp -v vendor/limine/limine-bios.sys vendor/limine/limine-bios-cd.bin vendor/limine/limine-uefi-cd.bin iso/boot/limine/ + cp -v vendor/limine/BOOTX64.EFI iso/EFI/BOOT/ + xorriso -as mkisofs -R -r -J -b boot/limine/limine-bios-cd.bin \ + -no-emul-boot -boot-load-size 4 -boot-info-table -hfsplus \ + -apm-block-size 2048 --efi-boot boot/limine/limine-uefi-cd.bin \ + -efi-boot-part --efi-boot-image --protective-msdos-label \ + iso -o $@ + vendor/limine/limine bios-install $@ + +$(KERNEL): $(K_OBJS) + rm -rf bin + mkdir -p bin + $(LD) -o $@ -T$(K_LINKSCRIPT) $^ + +$(K_OBJS): %.o: %.cxx + $(CC) -c $(CFLAGS) $^ -o $@ diff --git a/kernel/cxx/constructors.cxx b/kernel/cxx/constructors.cxx new file mode 100644 index 0000000..8c21453 --- /dev/null +++ b/kernel/cxx/constructors.cxx @@ -0,0 +1,18 @@ +extern void (*_ctors_start[0])(void), (*_ctors_end[0])(void); +extern void (*_dtors_start[0])(void), (*_dtors_end[0])(void); + +void init (void) +{ + for (void (**ctor)(void) = _ctors_start; ctor != _ctors_end; ctor++) + { + (*ctor)(); + } +} + +void fini (void) +{ + for (void (**dtor)(void) = _dtors_start; dtor != _dtors_end; dtor++) + { + (*dtor)(); + } +} diff --git a/kernel/cxx/gcc_required_functions.cxx b/kernel/cxx/gcc_required_functions.cxx new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/kernel/cxx/gcc_required_functions.cxx @@ -0,0 +1 @@ + diff --git a/kernel/cxx/icxxabi.cxx b/kernel/cxx/icxxabi.cxx new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/kernel/cxx/icxxabi.cxx diff --git a/kernel/cxx/icxxabi.h b/kernel/cxx/icxxabi.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/kernel/cxx/icxxabi.h diff --git a/kernel/entry.cxx b/kernel/entry.cxx new file mode 100644 index 0000000..b5bc210 --- /dev/null +++ b/kernel/entry.cxx @@ -0,0 +1,49 @@ +#include "../vendor/limine/limine.h" +#include <cstdint> + +__attribute__ ((used, section(".requests"))) +static volatile LIMINE_BASE_REVISION(3); + +__attribute__ ((used, section(".requests"))) +static volatile limine_framebuffer_request framebuffer_request = { + .id = LIMINE_FRAMEBUFFER_REQUEST, + .revision = 0, + .response = nullptr +}; + +__attribute__ ((used, section(".requests_start"))) +static volatile LIMINE_REQUESTS_START_MARKER; + +__attribute__ ((used, section(".requests_end"))) +static volatile LIMINE_REQUESTS_END_MARKER; + +static void hcf (void) +{ + for (;;) + { + asm ("hlt"); + } +} + +void init (void); + +extern "C" void _launch (void); +void _launch (void) +{ + if (LIMINE_BASE_REVISION_SUPPORTED == false) + { + hcf(); + } + + struct limine_framebuffer *fb = framebuffer_request.response->framebuffers[0]; + for (std::size_t i = 0; i < (fb->width - 1) * (fb->height - 1); i += 2) + { + volatile std::uint32_t *fbptr = static_cast<std::uint32_t *>(fb->address); + fbptr[i] = 0xffffff; + } + + // Global C++ constructors + init(); + + hcf(); +} diff --git a/kernel/ldscript b/kernel/ldscript new file mode 100644 index 0000000..2f1e1f4 --- /dev/null +++ b/kernel/ldscript @@ -0,0 +1,69 @@ +ENTRY(_launch) + +PHDRS +{ + text PT_LOAD; + rodata PT_LOAD; + data PT_LOAD; + eh_frame PT_LOAD; + dynamic PT_LOAD; +} + +SECTIONS +{ + /* Load in upper, you know the drill */ + . = 0xFFFFFFFF80000000; + + _kernel_start = .; + + .text : { + *(.text .text.*) + } :text + + . += CONSTANT(MAXPAGESIZE); + + .rodata : { + *(.rodata, .rodata.*) + *(.srodata, .srodata.*) + } :rodata + + .init_array : { + _ctors_start = .; + KEEP(*(SORT(.init_array.*))) + KEEP(*(.init_array)) + _ctors_end = .; + } :rodata + + .fini_array : { + _dtors_start = .; + KEEP(*(SORT(.fini_array))) + KEEP(*(.fini_array)) + _dtors_end = .; + } :rodata + + . += CONSTANT(MAXPAGESIZE); + + .data : { + *(.data .data.*) + *(.sdata .sdata.*) + + KEEP(*(.requests_start)) + KEEP(*(.requests)) + KEEP(*(.requests_end)) + } :data + + .dynamic : { + *(.dynamic) + } :data :dynamic + + .bss : { + *(.bss .bss.*) + *(.sbss .sbss.*) + *(COMMON) + } :data + + /DISCARD/ : { + *(.eh_frame*) + *(.note .note.*) + } +} diff --git a/limine.conf b/limine.conf index e69de29..4f1963e 100644 --- a/limine.conf +++ b/limine.conf @@ -0,0 +1,5 @@ +timeout: 3 + +/hinterOS + protocol: limine + path: boot():/bin/core diff --git a/toolchain/binutils.sh b/toolchain/binutils.sh index 5881dbb..23c4d98 100755 --- a/toolchain/binutils.sh +++ b/toolchain/binutils.sh @@ -7,16 +7,19 @@ SCRIPT=$(realpath "$0") SCRIPTPATH=$(dirname "$SCRIPT") cd "$SCRIPTPATH" -export PREFIX="$HOME/opt/cross" +export PREFIX="$SCRIPTPATH/../vendor" export SYSROOT="$SCRIPTPATH/../target" export PATH="$PREFIX/bin:$PATH" mkdir -p temp cd temp wget -nc https://ftp.gnu.org/gnu/binutils/binutils-2.43.tar.gz +wget -nc https://ftp.gnu.org/gnu/gcc/gcc-14.2.0/gcc-14.2.0.tar.gz tar xpvf binutils-2.43.tar.gz +tar xpvf gcc-14.2.0.tar.gz mkdir -p binutils-2.43/build_amd +mkdir -p gcc-14.2.0/build_amd cd binutils-2.43/build_amd @@ -24,5 +27,16 @@ cd binutils-2.43/build_amd make -j$(nproc) make install +cd ../../ +cd gcc-14.2.0/build_amd + +../configure --target="x86_64-elf" --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers --disable-hosted-libstdcxx +make all-gcc -j$(nproc) +make all-target-libgcc -j$(nproc) +make all-target-libstdc++-v3 +make install-gcc +make install-target-libgcc +make install-target-libstdc++-v3 + cd ../../../ rm -rf temp |