added boilerplate (todo make it amd64)

This commit is contained in:
trimill 2024-01-26 22:29:49 -05:00
parent 15915e81dc
commit 4aad3cce1d
No known key found for this signature in database
GPG key ID: 4F77A16E17E10BCB
5 changed files with 154 additions and 0 deletions

View file

@ -0,0 +1,45 @@
CC=cc
LD=ld
CFLAGS=-ffreestanding -g -Wall -Wextra -pedantic -lgcc
SRC_DIR=src
INCLUDE_DIR=include
BUILD_DIR=build
K_BIN_NAME=kernel.bin
ISO_NAME=os_image.iso
C_SRC=$(shell find $(SRC_DIR) -type f -name "*.c")
C_OBJ=$(patsubst %.c,$(BUILD_DIR)/%.o,$(C_SRC))
H_SRC=$(shell find $(SRC_DIR) -type f -name "*.h") $(shell find $(INCLUDE_DIR) -type f -name "*.h")
A_SRC=$(shell find $(SRC_DIR) -type f -name "*.S")
A_OBJ=$(patsubst %.S,$(BUILD_DIR)/%.S.o,$(A_SRC))
.PHONY: all clean
all: $(BUILD_DIR)/$(ISO_NAME)
clean:
rm -rf $(BUILD_DIR)/*
$(A_OBJ): $(BUILD_DIR)/%.S.o : %.S
@mkdir -p $(@D)
nasm $< -f elf64 -o $@
$(C_OBJ): $(BUILD_DIR)/%.o : %.c
@mkdir -p $(@D)
$(CC) -c $(CFLAGS) -o $@ $<
$(BUILD_DIR)/$(K_BIN_NAME): $(SRC_DIR)/arch/amd64/linker.ld $(A_OBJ) $(C_OBJ) $(H_SRC)
@mkdir -p $(@D)
$(LD) -nmagic -o $(BUILD_DIR)/$(K_BIN_NAME) -T $(SRC_DIR)/arch/amd64/linker.ld $(A_OBJ) $(C_OBJ)
$(BUILD_DIR)/$(ISO_NAME): $(BUILD_DIR)/$(K_BIN_NAME) grub.cfg
@mkdir -p $(@D)
@mkdir -p $(BUILD_DIR)/iso/boot/grub
cp grub.cfg $(BUILD_DIR)/iso/boot/grub
cp $(BUILD_DIR)/$(K_BIN_NAME) $(BUILD_DIR)/iso/boot
grub-mkrescue -o $(BUILD_DIR)/$(ISO_NAME) $(BUILD_DIR)/iso

7
grub.cfg Normal file
View file

@ -0,0 +1,7 @@
set timeout=1
set default=0
menuentry "corn" {
multiboot2 /boot/kernel.bin
boot
}

View file

@ -0,0 +1,72 @@
global start
extern kmain
bits 32
; base, limit, access, flags
%macro gdt_entry 4
db %2 & 0xff
db (%2 >> 8) & 0xff
db %1 & 0xff
db (%1 >> 8) & 0xff
db (%1 >> 16) & 0xff
db %3
db ((%2 >> 16) & 0x0f) | (%4 << 4)
db (%1 >> 24) & 0xff
%endmacro
section .multiboot
align 8
mb_start:
; header
dd 0xe85250d6
dd 0
dd mb_end - mb_start
dd 0x100000000 - (0xe85250d6 + (mb_end - mb_start))
; null tag
dw 0
dw 0
dd 8
;
mb_end:
section .bss
align 16
stack_start:
resb 16384
stack_end:
section .rodata
align 16
gdt_start:
gdt_entry 0, 0, 0, 0
gdt_entry 0, 0xFFFFF, 0x9A, 0xC
gdt_entry 0, 0xFFFFF, 0x92, 0xC
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
section .text
align 8
start:
cli
lgdt [gdt_descriptor]
jmp 0x08:after_lgdt
after_lgdt:
mov ax, 0x10
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
mov esp, stack_end
mov ebp, stack_end
sti
push ebx
call kmain
cli
halt:
hlt
jmp halt

25
src/arch/amd64/linker.ld Normal file
View file

@ -0,0 +1,25 @@
ENTRY(start)
SECTIONS {
. = 1M;
.boot BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
}
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
.text BLOCK(4K) : ALIGN(4K)
{
*(.text)
}
.bss BLOCK(4K) : ALIGN(4K)
{
*(.bss)
}
}

View file

@ -0,0 +1,5 @@
void kmain(void *boot_info) {
while(1) {
asm("cli; hlt");
}
}