From f75fd15a7acbc4e99b3eb9a659ff443419087bfe Mon Sep 17 00:00:00 2001 From: Tyler McGurrin Date: Mon, 4 Aug 2025 07:42:37 -0400 Subject: [PATCH] Bunch of stuff, and readd stuff for bochs --- .ccls | 4 ++ .gitignore | 4 +- bochs_config | 7 +++ bx_enh_dbg.ini | 26 ++++++++++ debug.sh | 94 +++++++++++++++++++++++++++++++++++ grub/grub.cfg | 10 ++-- src/kernel/arch/i686/isr.c | 15 +++--- src/kernel/arch/i686/util.asm | 20 +++++--- src/kernel/arch/i686/util.h | 1 + src/kernel/core/memory/page.c | 11 ++-- src/kernel/linker.ld | 7 ++- src/kernel/main.c | 4 +- src/kernel/util/binary.h | 2 + src/kernel/util/util.c | 34 ++++++------- src/kernel/util/util.h | 2 +- src/kernel/version.h | 2 +- 16 files changed, 196 insertions(+), 47 deletions(-) create mode 100644 .ccls create mode 100644 bochs_config create mode 100644 bx_enh_dbg.ini create mode 100755 debug.sh diff --git a/.ccls b/.ccls new file mode 100644 index 0000000..560a40b --- /dev/null +++ b/.ccls @@ -0,0 +1,4 @@ +clang +%c -std=c99 +%c -isystem./src/kernel/ +%c -fno-builtin \ No newline at end of file diff --git a/.gitignore b/.gitignore index fa5eac0..7e28ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .vscode/ +.ccls-cache/ build/ -toolchain/ \ No newline at end of file +toolchain/ +bochs_dbg.log \ No newline at end of file diff --git a/bochs_config b/bochs_config new file mode 100644 index 0000000..a944a86 --- /dev/null +++ b/bochs_config @@ -0,0 +1,7 @@ +megs: 128 +romimage: file=/usr/share/bochs/BIOS-bochs-legacy, address=0xffff0000 +ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14 +ata0-master: type=disk, path=build/main.img, mode=flat +boot: disk +mouse: enabled=0 +display_library: x, options="gui_debug" \ No newline at end of file diff --git a/bx_enh_dbg.ini b/bx_enh_dbg.ini new file mode 100644 index 0000000..9de38a8 --- /dev/null +++ b/bx_enh_dbg.ini @@ -0,0 +1,26 @@ +# bx_enh_dbg_ini +SeeReg[0] = TRUE +SeeReg[1] = TRUE +SeeReg[2] = TRUE +SeeReg[3] = TRUE +SeeReg[4] = FALSE +SeeReg[5] = FALSE +SeeReg[6] = FALSE +SeeReg[7] = FALSE +SingleCPU = FALSE +ShowIOWindows = TRUE +ShowButtons = TRUE +SeeRegColors = TRUE +ignoreNxtT = TRUE +ignSSDisasm = TRUE +UprCase = 0 +DumpInAsciiMode = 3 +isLittleEndian = TRUE +DefaultAsmLines = 512 +DumpWSIndex = 0 +DockOrder = 0x123 +ListWidthPix[0] = 487 +ListWidthPix[1] = 665 +ListWidthPix[2] = 760 +MainWindow = 1440, 23, 1916, 1030 +FontName = Normal diff --git a/debug.sh b/debug.sh new file mode 100755 index 0000000..d0d4d27 --- /dev/null +++ b/debug.sh @@ -0,0 +1,94 @@ +#!/bin/bash +echo -------- +echo STARTING +echo -------- +read -p "Do you want to clean old build files? (y/n) " yn + +case $yn in + y ) + + echo ---------------------------; + echo Removing Old Build Files...; + echo ---------------------------; + + make clean; + echo -----; + echo Done!; + echo -----; + ;; + n ) + echo ---------; + echo Proceding; + echo ---------; + ;; +esac +echo ------------ +echo COMPILING OS +echo ------------ + +make -s + +echo --------- +echo Finished! +echo --------- +read -p "Do you want to make to make a bootable image? [Requires sudo] (y/n) " yn +case $yn in + y) + echo ---------------- + echo Installing GRUB! + echo ---------------- + + sudo losetup -d /dev/loop800 + dd if=/dev/zero of=build/main.img bs=512 count=50000 + mkdir -p build/mnt + sudo losetup -P /dev/loop800 build/main.img + sudo parted /dev/loop800 mktable msdos + sudo parted /dev/loop800 mkpart primary fat16 2048s 100% + sudo parted /dev/loop800p1 set 1 boot on + sudo mkfs.fat -F16 /dev/loop800p1 + sudo mount /dev/loop800p1 build/mnt + sudo mkdir build/mnt/boot + sudo mkdir build/mnt/boot/grub + sudo mkdir build/mnt/misc + sudo mkdir build/mnt/misc/src + sudo cp -r src/* build/mnt/src + sudo cp -r grub/* build/mnt/boot/grub + sudo cp build/nanite.bin build/mnt/boot/nanite + sudo grub-install \ + --modules="part_msdos" \ + --boot-directory=build/mnt/boot \ + --target=i386-pc \ + --bootloader-id=GRUB \ + /dev/loop800 -v + sudo umount build/mnt + sudo losetup -d /dev/loop800 + + ;; + n ) echo exiting...; + exit;; + * ) echo invalid response; + exit 1;; +esac +echo --------- +echo Finished! +echo --------- + +read -p "Do you want to Start BOCHS? (y/n) " yn + +case $yn in + y ) + echo ------------- + echo STARTING BOCHS + echo ------------- + bochs -f bochs_config -dbg -dbglog bochs_dbg.log + echo -------- + echo Finshed! + echo -------- + + ;; + n ) echo exiting...; + exit;; + * ) echo invalid response; + exit 1;; +esac + diff --git a/grub/grub.cfg b/grub/grub.cfg index 85fd2c4..7267caa 100644 --- a/grub/grub.cfg +++ b/grub/grub.cfg @@ -16,11 +16,11 @@ terminal_input console terminal_output gfxterm insmod gfxmenu -loadfont /boot/grub/themes/nanite/font10.pf2 -loadfont /boot/grub/themes/nanite/font12.pf2 -loadfont /boot/grub/themes/nanite/font14.pf2 -set theme=/boot/grub/themes/nanite/theme.txt -export theme +# loadfont /boot/grub/themes/nanite/font10.pf2 +# loadfont /boot/grub/themes/nanite/font12.pf2 +# loadfont /boot/grub/themes/nanite/font14.pf2 +# set theme=/boot/grub/themes/nanite/theme.txt +# export theme insmod png menuentry "NANITE" { diff --git a/src/kernel/arch/i686/isr.c b/src/kernel/arch/i686/isr.c index ab7064f..70fdc47 100644 --- a/src/kernel/arch/i686/isr.c +++ b/src/kernel/arch/i686/isr.c @@ -9,6 +9,9 @@ #include "io.h" #include #include +#include > + +extern uint16_t DEBUG_COM_PORT; ISRHandler g_ISRHandlers[256]; @@ -62,13 +65,13 @@ void __attribute__((cdecl)) ISR_Handler(Registers* regs) { g_ISRHandlers[regs->interrupt](regs); else if (regs->interrupt >= 32) - printf("Unhandled Interrupt %d\n", regs->interrupt); + Serial_Printf(DEBUG_COM_PORT, "Unhandled Interrupt %d\n", regs->interrupt); else { - printf("Unhandled Exception %d %s\n", regs->interrupt, g_Exceptions[regs->interrupt]); - printf(" EAX=%x EBX=%x ECX=%x EDX=%x ESI=%x EDI=%x\n", regs->eax, regs->ebx, regs->ecx, regs->edx, regs->esi, regs->edi); - printf(" ESP=%x EBP=%x EIP=%x EFLAGS=%x CS=%x DS=%x SS=%x\n", regs->esp, regs->ebp, regs->eip, regs->eflags, regs->cs, regs->ds, regs->ss); - printf(" INTERRUPT=%x ERRORCODE=%x\n", regs->interrupt, regs->error); - printf("KERNEL PANIC!\n"); + Serial_Printf(DEBUG_COM_PORT, "Unhandled Exception %d %s\n", regs->interrupt, g_Exceptions[regs->interrupt]); + Serial_Printf(DEBUG_COM_PORT, " EAX=%x EBX=%x ECX=%x EDX=%x ESI=%x EDI=%x\n", regs->eax, regs->ebx, regs->ecx, regs->edx, regs->esi, regs->edi); + Serial_Printf(DEBUG_COM_PORT, " ESP=%x EBP=%x EIP=%x EFLAGS=%x CS=%x DS=%x SS=%x\n", regs->esp, regs->ebp, regs->eip, regs->eflags, regs->cs, regs->ds, regs->ss); + Serial_Printf(DEBUG_COM_PORT, " INTERRUPT=%x ERRORCODE=%x\n", regs->interrupt, regs->error); + Serial_Printf(DEBUG_COM_PORT, "KERNEL PANIC!\n"); kernel_panic(); } diff --git a/src/kernel/arch/i686/util.asm b/src/kernel/arch/i686/util.asm index 6ebb88a..07fe9df 100644 --- a/src/kernel/arch/i686/util.asm +++ b/src/kernel/arch/i686/util.asm @@ -7,13 +7,13 @@ global Reboot Reboot: - XOR AL, AL - IN AL, 0x64 - TEST AL, 0x02 - JNZ Reboot + xor al, al + in al, 0x64 + test al, 0x02 + jnz Reboot - MOV AL, 0xFC - OUT 0x64, AL + mov al, 0xFC + out 0x64, al global Read_CR0 Read_CR0: @@ -25,7 +25,9 @@ Write_CR0: push ebp mov ebp, esp mov eax, [ebp+8] + push eax mov cr0, eax + pop eax pop ebp retn @@ -42,3 +44,9 @@ Write_CR3: mov cr3, eax pop ebp retn + +global Enable_Paging +Enable_Paging: + mov eax, cr4 + or eax, 0x00000010 + mov cr4, eax diff --git a/src/kernel/arch/i686/util.h b/src/kernel/arch/i686/util.h index 1d33184..f6c7c74 100644 --- a/src/kernel/arch/i686/util.h +++ b/src/kernel/arch/i686/util.h @@ -12,3 +12,4 @@ unsigned long __attribute__((cdecl)) Read_CR0(); void __attribute__((cdecl)) Write_CR0(unsigned long value); unsigned long* __attribute__((cdecl)) Read_CR3(); void __attribute__((cdecl)) Write_CR3(unsigned long* value); +void __attribute__((cdecl)) Enable_Paging(); diff --git a/src/kernel/core/memory/page.c b/src/kernel/core/memory/page.c index 7474d7c..d33fb7e 100644 --- a/src/kernel/core/memory/page.c +++ b/src/kernel/core/memory/page.c @@ -12,8 +12,8 @@ extern uint16_t DEBUG_COM_PORT; -unsigned long* Page_Directory = (unsigned long*) 0x9c000; -unsigned long* Page_Table = (unsigned long*) 0x9d000; +unsigned long* Page_Directory = (unsigned long*) 0x9C000; +unsigned long* Page_Table = (unsigned long*) 0x9D000; void Memory_Page_Init() { @@ -28,11 +28,14 @@ void Memory_Page_Init() Serial_Printf(DEBUG_COM_PORT, "MEMORY:> Inititalizing Page Directory and Table.\n"); // fill the first entry of the page directory - // Page_Directory[0] = Page_Table; + Page_Directory[0] = *Page_Table; Page_Directory[0] = Page_Directory[0] | 3; // attribute set to: supervisor level, read/write for(i=1; i<1024; i++) { Page_Directory[i] = 0 | 2; // attribute set to: supervisor level, read/write } + Enable_Paging(); Write_CR3(Page_Directory); - Write_CR0(Read_CR0() | 0x80000000); // set the paging bit in CR0 to 1 + // Write_CR0(Read_CR0() | 0x80000000); // set the paging bit in CR0 to 1 + /* THATS IT... I GIVE UP! */ + Serial_Printf(DEBUG_COM_PORT, "HERE!\n"); } \ No newline at end of file diff --git a/src/kernel/linker.ld b/src/kernel/linker.ld index 68936aa..0e93ada 100644 --- a/src/kernel/linker.ld +++ b/src/kernel/linker.ld @@ -2,11 +2,10 @@ ENTRY(start) SECTIONS { - . = 1M; + . = ALIGN(8); - .boot : { KEEP(*(.multiboot_header))} - .entry : { __entry_start = .; *(.entry) } - .text : { __text_start = .; *(.text) } + .entry : { KEEP(*(.multiboot_header)) __entry_start = .; *(.entry) } + .text : { __text_start = .; *(.text) } .data : { __data_start = .; *(.data) } .rodata : { __rodata_start = .; *(.rodata) } .bss : { __bss_start = .; *(.bss) } diff --git a/src/kernel/main.c b/src/kernel/main.c index 5b5ecbd..f57a423 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -51,7 +51,7 @@ void __attribute__((section(".entry"))) start(uint64_t multiboot_magic, void *mu IRQ_RegisterHandler(6, Floppy_Handler); IRQ_RegisterHandler(8, CMOS_RTC_Handler); printf("Done!\n"); - + // Init Drivers printf("Initializing Basic Drivers..."); Serial_Init(DEBUG_COM_PORT, 9600); @@ -61,7 +61,7 @@ void __attribute__((section(".entry"))) start(uint64_t multiboot_magic, void *mu // unsure why this is not working, will look into it later... printf("Initializing Memory Paging..."); - // Memory_Page_Init(); + Memory_Page_Init(); printf("Done!\n"); printf("Multiboot Magic: %d\n", multiboot_magic); diff --git a/src/kernel/util/binary.h b/src/kernel/util/binary.h index e08812d..e80b6b6 100644 --- a/src/kernel/util/binary.h +++ b/src/kernel/util/binary.h @@ -5,6 +5,8 @@ \*----------------*/ #pragma once +#include + #define FLAG_SET(x, flag) x |= (flag) #define FLAG_UNSET(x, flag) x &= ~(flag) #define BCD2BIN(bcd) ((((bcd)&15) + ((bcd)>>4)*10)) diff --git a/src/kernel/util/util.c b/src/kernel/util/util.c index a9b4c4d..d249e41 100644 --- a/src/kernel/util/util.c +++ b/src/kernel/util/util.c @@ -21,20 +21,20 @@ void append(char s[], char n) { s[len + 1] = '\0'; } -void Print_Storage_Types(int masterFDDType, int slaveFDDType) -{ - // Master FDD - if (masterFDDType == 0) printf("No Master Floppy Drive Detected!\n"); - if (masterFDDType == 1) printf("Master Floppy Drive Detected!\nType: 360 KB 5.25 Inch Drive\n"); - if (masterFDDType == 2) printf("Master Floppy Drive Detected!\nType: 1.2 MB 5.25 Inch Drive\n"); - if (masterFDDType == 3) printf("Master Floppy Drive Detected!\nType: 720 KB 3.5 Inch Drive\n"); - if (masterFDDType == 4) printf("Master Floppy Drive Detected!\nType: 1.44 MB 3.5 Inch Drive\n"); - if (masterFDDType == 5) printf("Master Floppy Drive Detected!\nType: 2.88 MB 3.5 Inch Drive\n"); - // Slave FDD - if (slaveFDDType == 0) printf("No Slave Floppy Drive Detected!\n"); - if (slaveFDDType == 1) printf("Slave Floppy Drive Detected!\nType: 360 KB 5.25 Inch Drive\n"); - if (slaveFDDType == 2) printf("Slave Floppy Drive Detected!\nType: 1.2 MB 5.25 Inch Drive\n"); - if (slaveFDDType == 3) printf("Slave Floppy Drive Detected!\nType: 720 KB 3.5 Inch Drive\n"); - if (slaveFDDType == 4) printf("Slave Floppy Drive Detected!\nType: 1.44 MB 3.5 Inch Drive\n"); - if (slaveFDDType == 5) printf("Slave Floppy Drive Detected!\nType: 2.88 MB 3.5 Inch Drive\n"); -} \ No newline at end of file +// void Print_Storage_Types(int masterFDDType, int slaveFDDType) +// { +// // Master FDD +// if (masterFDDType == 0) printf("No Master Floppy Drive Detected!\n"); +// if (masterFDDType == 1) printf("Master Floppy Drive Detected!\nType: 360 KB 5.25 Inch Drive\n"); +// if (masterFDDType == 2) printf("Master Floppy Drive Detected!\nType: 1.2 MB 5.25 Inch Drive\n"); +// if (masterFDDType == 3) printf("Master Floppy Drive Detected!\nType: 720 KB 3.5 Inch Drive\n"); +// if (masterFDDType == 4) printf("Master Floppy Drive Detected!\nType: 1.44 MB 3.5 Inch Drive\n"); +// if (masterFDDType == 5) printf("Master Floppy Drive Detected!\nType: 2.88 MB 3.5 Inch Drive\n"); +// // Slave FDD +// if (slaveFDDType == 0) printf("No Slave Floppy Drive Detected!\n"); +// if (slaveFDDType == 1) printf("Slave Floppy Drive Detected!\nType: 360 KB 5.25 Inch Drive\n"); +// if (slaveFDDType == 2) printf("Slave Floppy Drive Detected!\nType: 1.2 MB 5.25 Inch Drive\n"); +// if (slaveFDDType == 3) printf("Slave Floppy Drive Detected!\nType: 720 KB 3.5 Inch Drive\n"); +// if (slaveFDDType == 4) printf("Slave Floppy Drive Detected!\nType: 1.44 MB 3.5 Inch Drive\n"); +// if (slaveFDDType == 5) printf("Slave Floppy Drive Detected!\nType: 2.88 MB 3.5 Inch Drive\n"); +// } \ No newline at end of file diff --git a/src/kernel/util/util.h b/src/kernel/util/util.h index cef5dac..4210ecd 100644 --- a/src/kernel/util/util.h +++ b/src/kernel/util/util.h @@ -9,4 +9,4 @@ int string_length(char s[]); void append(char s[], char n); -void Print_Storage_Types(int masterFDDType, int slaveFDDType); \ No newline at end of file +// void Print_Storage_Types(int masterFDDType, int slaveFDDType); \ No newline at end of file diff --git a/src/kernel/version.h b/src/kernel/version.h index b8e0ae1..c3cbab5 100644 --- a/src/kernel/version.h +++ b/src/kernel/version.h @@ -6,4 +6,4 @@ #pragma once #define LOGO " _ _____ _ __________________\n / | / / | / | / / _/_ __/ ____/\n / |/ / /| | / |/ // / / / / __/ \n / /| / ___ |/ /| // / / / / /___ \n/_/ |_/_/ |_/_/ |_/___/ /_/ /_____/ \n" -#define VERSION "RD-00042" \ No newline at end of file +#define VERSION "RD-00043" \ No newline at end of file