From 9cc2436d33192e06ce2a227441f19f5a5e5ed2cb Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 3 Mar 2022 11:24:05 -0500 Subject: [PATCH] Add D barebones sources and Makefile --- .gitignore | 4 ++++ Makefile | 21 +++++++++++++++++++++ boot.S | 28 ++++++++++++++++++++++++++++ kernel_main.d | 5 +++++ link.ld | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 Makefile create mode 100644 boot.S create mode 100644 kernel_main.d create mode 100644 link.ld diff --git a/.gitignore b/.gitignore index 3f71dff..a3dc2ad 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,7 @@ /build-*/ /gcc-*/ /binutils-*/ +/*.o +/*.elf +/image/ +/image.img diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..40fda66 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +.PHONY: kernel +kernel: + ./i686-elf-gcc/bin/i686-elf-gcc -c -o boot.o -ffreestanding boot.S + ldc2 -march=x86 -mcpu=i686 --betterC -c -of=kernel_main.o kernel_main.d + ./i686-elf-gcc/bin/i686-elf-gcc -o kernel.elf -ffreestanding -nostdlib -T link.ld boot.o kernel_main.o + +.PHONY: image +image: kernel + rm -rf image + mkdir -p image/iso/boot/grub + echo 'set default="0"' >image/iso/boot/grub/grub.cfg + echo 'set timeout=1' >>image/iso/boot/grub/grub.cfg + echo 'menuentry "MyKernel" {' >>image/iso/boot/grub/grub.cfg + echo ' insmod multiboot' >>image/iso/boot/grub/grub.cfg + echo ' multiboot /kernel.elf' >>image/iso/boot/grub/grub.cfg + echo '}' >>image/iso/boot/grub/grub.cfg + cp kernel.elf image/iso + grub-mkrescue -o image.img image/iso + +run: image + qemu-system-x86_64 -hda image.img diff --git a/boot.S b/boot.S new file mode 100644 index 0000000..a24e6a5 --- /dev/null +++ b/boot.S @@ -0,0 +1,28 @@ +#define MAGIC 0x1BADB002 +#define FLAGS 0x3 + +.section .multiboot +MultiBootHeader: + .long MAGIC + .long FLAGS + .long -(MAGIC + FLAGS) + +.section .text +.global mykernel_start +.type mykernel_start, @function +mykernel_start: + /* Set stack pointer. */ + mov $_stack_end, %esp + + /* Jump to C. */ + push $0 + push $0 + push $0 + push %ebx + call mykernel_main + + cli +1: hlt + jmp 1b + +.size mykernel_start, . - mykernel_start diff --git a/kernel_main.d b/kernel_main.d new file mode 100644 index 0000000..9688603 --- /dev/null +++ b/kernel_main.d @@ -0,0 +1,5 @@ +extern (C) void mykernel_main() { + ubyte * vidmem = cast(ubyte *) 0xB8000u; + vidmem[0] = 'D'; + vidmem[1] = 0x7u; +} diff --git a/link.ld b/link.ld new file mode 100644 index 0000000..775fdaf --- /dev/null +++ b/link.ld @@ -0,0 +1,38 @@ +ENTRY(mykernel_start) + +SECTIONS +{ + . = 1M; + + .text BLOCK(4K) : ALIGN(4K) + { + *(.multiboot) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) + { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) + { + *(.data) + } + + _stack_size = 16K; + .stack (NOLOAD) : ALIGN(4K) + { + _stack_start = .; + . = . + _stack_size; + _stack_end = .; + } + + .bss BLOCK(4K) : ALIGN(4K) + { + *(COMMON) + *(.bss) + } + + . = ALIGN(4K); +}