From 87c79f50bedc875e346e2bc3382f2fc02f75fb28 Mon Sep 17 00:00:00 2001 From: nub31 Date: Tue, 30 Dec 2025 21:00:20 +0100 Subject: [PATCH] ... --- makefile | 14 +++--- src/boot/{mem.c => main.c} | 7 +++ src/boot/{boot.asm => start.asm} | 77 +++++++++++++++++++++++++++++--- src/boot/start.c | 8 ---- 4 files changed, 84 insertions(+), 22 deletions(-) rename src/boot/{mem.c => main.c} (92%) rename src/boot/{boot.asm => start.asm} (55%) delete mode 100644 src/boot/start.c diff --git a/makefile b/makefile index d0836d9..f9a8cab 100644 --- a/makefile +++ b/makefile @@ -1,16 +1,16 @@ -CC = i686-elf-gcc -LD = i686-elf-ld +CC = x86_64-elf-gcc +LD = x86_64-elf-ld AS = nasm # Modify these settings here for defines and debug info -CFLAGS = -g -D DEBUG -LDFLAGS = -g -ASFLAGS = -g -F dwarf +CFLAGS = +LDFLAGS = +ASFLAGS = # Do not modify -CFLAGS += -m32 -ffreestanding -fno-stack-protector -nostdlib -nostdinc -Wall -Wextra -std=c23 -Isrc/shared +CFLAGS += -m64 -ffreestanding -fno-stack-protector -nostdlib -nostdinc -Wall -Wextra -std=c23 -Isrc/shared LDFLAGS += -ASFLAGS += -f elf32 +ASFLAGS += -f elf64 SRC_C := $(shell find src -name '*.c') SRC_ASM := $(shell find src -name '*.asm') diff --git a/src/boot/mem.c b/src/boot/main.c similarity index 92% rename from src/boot/mem.c rename to src/boot/main.c index c689238..8949380 100644 --- a/src/boot/mem.c +++ b/src/boot/main.c @@ -1,3 +1,6 @@ +#include "console.h" +#include "multiboot2.h" + #include #include "console.h" #include "multiboot2.h" @@ -57,4 +60,8 @@ static void find_memory_regions(uptr multiboot_info) { entry_ptr += tag->entry_size; } +} + +void kmain(uptr multiboot_info) { + find_memory_regions(multiboot_info); } \ No newline at end of file diff --git a/src/boot/boot.asm b/src/boot/start.asm similarity index 55% rename from src/boot/boot.asm rename to src/boot/start.asm index c56443b..fcddbf3 100644 --- a/src/boot/boot.asm +++ b/src/boot/start.asm @@ -1,4 +1,7 @@ global _start +extern kernel_start +extern kernel_end +extern kmain %define MAGIC 0xe85250d6 %define ARCH 0x0 @@ -20,10 +23,42 @@ header: section .data align 8 -multiboot_info: dd 0 +multiboot_info: dq 0 + +%define PRESENT 1 << 7 +%define NOT_SYS 1 << 4 +%define EXEC 1 << 3 +%define DC 1 << 2 +%define RW 1 << 1 +%define ACCESSED 1 << 0 + +%define GRAN_4K 1 << 7 +%define SZ_32 1 << 6 +%define LONG_MODE 1 << 5 + +gdt: + .null: equ $ - gdt + dq 0 + .code: equ $ - gdt + .code.limit_lo: dw 0xffff + .code.base_lo: dw 0 + .code.base_mid: db 0 + .code.access: db PRESENT | NOT_SYS | EXEC | RW + .code.flags: db GRAN_4K | LONG_MODE | 0xF + .code.base_hi: db 0 + .data: equ $ - gdt + .data.limit_lo: dw 0xffff + .data.base_lo: dw 0 + .data.base_mid: db 0 + .data.access: db PRESENT | NOT_SYS | RW + .data.Flags: db GRAN_4K | SZ_32 | 0xF + .data.base_hi: db 0 + .pointer: + dw $ - gdt - 1 + dq gdt section .bss -align 4096 +align 16 stack: .bottom: resb 1024 * 16 @@ -36,9 +71,8 @@ pdt: resb 4096 pt: resb 4096 section .text +bits 32 _start: - mov esp, stack.top - %define BOOTLOADER_MAGIC 0x36d76289 ; Make sure we booted with multiboot 2 @@ -75,10 +109,17 @@ _start: %define PAGE_SIZE 0x1000 %define START_ADDRESS 0x0 - ; Identity map first 2mb of memory + ; Identity map kernel pages mov edi, pt - mov ebx, START_ADDRESS | PT_PRESENT | PT_READABLE - mov ecx, ENTRIES_PER_PT + mov ebx, kernel_start + and ebx, 0xfffff000 + or ebx, PT_PRESENT | PT_READABLE + + mov eax, kernel_end + sub eax, kernel_start + add eax, PAGE_SIZE-1 + shr eax, 12 + mov ecx, eax .set_pt_entry: mov [edi], ebx @@ -108,6 +149,28 @@ _start: mov eax, cr0 or eax, CR0_PG_ENABLE mov cr0, eax + + ; Load gdt + lgdt [gdt.pointer] + jmp gdt.code:start64 +.halt: + cli + hlt + jmp .halt + +section .text +bits 64 +start64: + mov ax, gdt.data + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + + mov rsp, stack.top + mov rdi, [multiboot_info] + call kmain .halt: cli hlt diff --git a/src/boot/start.c b/src/boot/start.c deleted file mode 100644 index 51e0cc8..0000000 --- a/src/boot/start.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include "console.h" -#include "multiboot2.h" -#include "panic.h" - -void c_start() { - console_clear(); -} \ No newline at end of file