From 82bb6371478d8fbaa150d7a37f6b93a1e42b889f Mon Sep 17 00:00:00 2001 From: Nocturn9x Date: Fri, 18 Nov 2022 17:24:45 +0100 Subject: [PATCH] Segment registers are now zeroed, kernel stack is expanded to 2MiB, load_disk now does not fill cl automatically --- Makefile | 5 ++++- src/boot/mbr.s | 21 +++++++++++++++------ src/boot/switch32.s | 2 +- src/boot/util/disk.s | 8 +++----- src/boot/util/io.s | 1 - src/entrypoint.s | 2 +- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 03f42ae..9dc16e9 100644 --- a/Makefile +++ b/Makefile @@ -88,4 +88,7 @@ image: $(BUILDDIR)/mbr.bin $(BUILDDIR)/kernel.bin run: image - qemu-system-x86_64 -drive format=raw,file=$(DISTDIR)/os.img,index=0,media=disk \ No newline at end of file + qemu-system-x86_64 -drive format=raw,file=$(DISTDIR)/os.img,index=0,media=disk + +debug: image + qemu-system-x86_64 -drive format=raw,file=$(DISTDIR)/os.img,index=0,media=disk -monitor stdio \ No newline at end of file diff --git a/src/boot/mbr.s b/src/boot/mbr.s index 8fde586..de9ebab 100644 --- a/src/boot/mbr.s +++ b/src/boot/mbr.s @@ -22,13 +22,21 @@ kernel_offset: equ 0x1000 ; We save the value of the current boot drive mov [boot_drive], dl +; This isn't needed inside the qemu emulator, but +; real hardware is unlikely to start up with the +; segment registers zeroed, so we do it here +mov ax, 0 +mov ds, ax +mov ss, ax +mov es, ax +xor ax, ax + ; Now we setup the stack by setting the -; base pointer to address 0x9000. The address -; itself doesn't matter as long as it's far -; enough away from memory already in use by -; the BIOS -mov bp, 0x9000 -mov sp, bp +; base pointer to a location that's far +; enough from where the code for the BIOS +; is located +mov sp, 0x9000 +mov bp, sp call bios_cls mov si, real_mode_msg call bios_println @@ -58,6 +66,7 @@ load_kernel: ; Loads the kernel into memory mov si, loading_kernel_msg call bios_println mov bx, kernel_offset + mov cl, 2 mov dh, 4 mov dl, [boot_drive] call load_disk diff --git a/src/boot/switch32.s b/src/boot/switch32.s index 693a09d..67d8716 100644 --- a/src/boot/switch32.s +++ b/src/boot/switch32.s @@ -43,7 +43,7 @@ switch32: ; We're not in 32 bit mode, yay! mov fs, ax mov gs, ax - mov ebp, 0x90000 ; We also move the stack further up + mov ebp, 0x1f8400 ; We also make the stack larger (2MiB) mov esp, ebp call BEGIN_32BIT ; We call back into mbr.s which loads the kernel \ No newline at end of file diff --git a/src/boot/util/disk.s b/src/boot/util/disk.s index f09be8a..79eb6d3 100644 --- a/src/boot/util/disk.s +++ b/src/boot/util/disk.s @@ -17,14 +17,12 @@ load_disk: - ; Loads 'dh' sectors from drive 'dl' (this - ; register is set by the BIOS before calling - ; the bootloader) into es:bx + ; Loads 'dh' sectors from drive 'dl' starting + ; from sector 'cl' pusha push dx mov ah, 0x2 ; Perform a read operation mov al, dh ; Number of sectors - mov cl, 0x2 ; 0x1 is us, so 0x2 is the first sector we want mov ch, 0x0 ; Cylinder 0 mov dh, 0x0 ; Head position is 0 too int 0x13 ; Once this returns, the data will be in es:bx @@ -58,4 +56,4 @@ disk_loop: disk_read_error_msg: db "TSOS: Read error: ", 0 -disk_sectors_error_msg: db "TSOS: Sector read count error", 0 +disk_sectors_error_msg: db "TSOS: Incomplete read", 0 diff --git a/src/boot/util/io.s b/src/boot/util/io.s index 3f8f9ac..96acdcd 100644 --- a/src/boot/util/io.s +++ b/src/boot/util/io.s @@ -201,5 +201,4 @@ vga_printh: ret -NEWLINE: db 0xA, 0xD HEX_OUT_LONG: db '0x00000000', 0 \ No newline at end of file diff --git a/src/entrypoint.s b/src/entrypoint.s index 96f1a89..a111ed0 100644 --- a/src/entrypoint.s +++ b/src/entrypoint.s @@ -17,4 +17,4 @@ [bits 32] [extern kmain] call kmain -jmp $ \ No newline at end of file +jmp $