From 726666109718e09032f9942bcd75ef7e2524b514 Mon Sep 17 00:00:00 2001 From: lordtet Date: Wed, 30 Jul 2025 17:17:12 -0400 Subject: [PATCH] Boilerplate + Protected mode Added make debug to Makefile Added the gdb script from the kernel for easy debugging now makes it to protected mode and shows a green box when done Added a gdt (of course) --- Makefile | 9 +++++++-- connect_gdb.sh | 5 +++++ src/boot.asm | 15 -------------- src/boot.s | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/gdt.s | 26 ++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 17 deletions(-) create mode 100755 connect_gdb.sh delete mode 100644 src/boot.asm create mode 100644 src/boot.s create mode 100644 src/gdt.s diff --git a/Makefile b/Makefile index 1d754c8..75d4cc0 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Makefile SRC_DIR = src BUILD_DIR = build -BOOTLOADER_SRC = $(SRC_DIR)/boot.asm +BOOTLOADER_SRC = $(SRC_DIR)/boot.s BOOTLOADER_IMG = $(BUILD_DIR)/boot.img QEMU = qemu-system-i386 @@ -12,8 +12,13 @@ $(BUILD_DIR): mkdir -p $(BUILD_DIR) $(BOOTLOADER_IMG): $(BOOTLOADER_SRC) | $(BUILD_DIR) - nasm -f bin $(BOOTLOADER_SRC) -o $(BOOTLOADER_IMG) + nasm -f bin $(BOOTLOADER_SRC) -i $(SRC_DIR) -o $(BOOTLOADER_IMG) +.PHONY: debug +debug: + $(QEMU) $(QEMU_FLAGS) -s -S -drive format=raw,file=$(BOOTLOADER_IMG) + +.PHONY: run run: all $(QEMU) -drive format=raw,file=$(BOOTLOADER_IMG) -display gtk diff --git a/connect_gdb.sh b/connect_gdb.sh new file mode 100755 index 0000000..7d72346 --- /dev/null +++ b/connect_gdb.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +i686-elf-gdb build/ukern.elf \ + -ex "target remote localhost:1234" \ + -ex "break start" \ diff --git a/src/boot.asm b/src/boot.asm deleted file mode 100644 index c53fb71..0000000 --- a/src/boot.asm +++ /dev/null @@ -1,15 +0,0 @@ -; boot.asm - simple boot sector -BITS 16 -ORG 0x7C00 - -start: - mov ah, 0x0E - mov al, 'H' - int 0x10 - mov al, 'i' - int 0x10 - - jmp $ - -times 510 - ($ - $$) db 0 -dw 0xAA55 diff --git a/src/boot.s b/src/boot.s new file mode 100644 index 0000000..9e240c4 --- /dev/null +++ b/src/boot.s @@ -0,0 +1,55 @@ +; boot.asm - simple boot sector +[ORG 0x7C00] +[BITS 16] +jmp start + +%include "gdt.s" + +;Opting against using ORG 0x7C00 as I want to access memory literally for clarity. +;Remember to set a segment to 0x07C0 :) +;ORG 0x7C00 + + +start: + ;Hello world! Let's get protected mode moving so we don't have to work in real mode the whole time. + cli + ;gimme dat + lgdt [gdtr] + ;Enable protected mode bit + mov eax, cr0 + or eax, 1 + mov cr0, eax + + ;32 bits! + jmp 0x08:prot_mode + + +[BITS 32] +prot_mode: + ;Hello 32 bit! + ;Some real setup now... + ;Set the segments + mov ax, 0x10 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + + ;Clear the screen + mov ax, 0x0720 + mov edi, 0xB8000 + mov ecx, 80*25 + rep stosw + + +done: + mov cx, 0x2220 + mov [0xB8000], cx + + jmp $ + +;Bootloader should be 512 bytes. So we are defining a padding of zeroes that would round out the size. 510 to account for the 0xAA55 after. +times 510 - ($ - $$) db 0 +;Old BIOS expect the sector to end in 0xAA55. We are, in fact, targeting old hardware. +dw 0xAA55 diff --git a/src/gdt.s b/src/gdt.s new file mode 100644 index 0000000..4adbc26 --- /dev/null +++ b/src/gdt.s @@ -0,0 +1,26 @@ +;GDT code taken straight from Untitled Kernel. The Kernel mode ranges will do just fine. +align 8 +gdt: + ;Null descriptor + dd 0x00000000 + dd 0x00000000 + ;Kernel code segment. + dw 0xFFFF + dw 0 + db 0 + db 0b10011010 + db 0b11001111 + db 0 + ;Kernel data segment. + dw 0xFFFF + dw 0x0000 + db 0x00 + db 0b10010010 + db 0b11001111 + db 0x00 +gdt_end: +gdtr: + ;size of the gdt + dw gdt_end - gdt - 1 + ;location of the gdt + dd gdt