diff --git a/build/OwOS.bin b/build/OwOS.bin index 6baee35..a0a9551 100755 Binary files a/build/OwOS.bin and b/build/OwOS.bin differ diff --git a/build/OwOS.iso b/build/OwOS.iso index 8941932..3d4933f 100644 Binary files a/build/OwOS.iso and b/build/OwOS.iso differ diff --git a/iso/boot/OwOS.bin b/iso/boot/OwOS.bin index 6baee35..a0a9551 100644 Binary files a/iso/boot/OwOS.bin and b/iso/boot/OwOS.bin differ diff --git a/kernel.cpp b/kernel.cpp index beca0a0..a90e20e 100644 --- a/kernel.cpp +++ b/kernel.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -14,34 +15,37 @@ extern "C" { extern "C" -int kernel_main(uint32_t magic, multibootInfo_t* multiboot) { - cls(); - showCursor(); - setFGColour(VGA_GREEN); - writeln("OwOS V0.2 Starting Up..."); - setFGColour(VGA_WHITE); - nline(); +int kernel_main(uint32_t magic, MultibootInfo_t* multiboot) { + Cls(); + ShowCursor(); + SetFGColour(VGA_GREEN); + Writeln("OwOS V0.2 Starting Up..."); + SetFGColour(VGA_WHITE); + Nline(); // Init systems - initGDT(); - loggerOK("GDT Loaded"); + int igdt = InitGDT(); + if (igdt == 1) + loggerOK("GDT Loaded"); + else + Nline(); Nline(); + Write("OwO, What's This?"); + SetFGColour(VGA_BRIGHT_MAGENTA); + Write(" *notices "); + Write(itoa(multiboot->mem_upper / 1024)); + Writeln("mb of ram*"); + SetFGColour(VGA_WHITE); + Nline(); - nline(); nline(); - write("OwO, What's This?"); - setFGColour(VGA_BRIGHT_MAGENTA); - write(" *notices "); - write(itoa(multiboot->mem_upper / 1024)); - writeln("mb of ram*"); - setFGColour(VGA_WHITE); - nline(); + SetFGColour(VGA_YELLOW); + Writeln("Welcome to OwOS"); + SetFGColour(VGA_BRIGHT_MAGENTA); + Write("~#"); - setFGColour(VGA_YELLOW); - writeln("Welcome to OwOS"); - setFGColour(VGA_BRIGHT_MAGENTA); - write("~#"); + PanicKernel(0x00, "Self Triggered Panic", "kernel.cpp:48", "kernel_main(uint32_t magic, MultibootInfo_t* multiboot)"); for (;;) asm("hlt"); diff --git a/kernel/drivers/terminal/terminal.cpp b/kernel/drivers/terminal/terminal.cpp index 8891923..b998b38 100644 --- a/kernel/drivers/terminal/terminal.cpp +++ b/kernel/drivers/terminal/terminal.cpp @@ -3,9 +3,6 @@ #include #include -static const int TERMINAL_WIDTH = 80; -static const int TERMINAL_HEIGHT = 24; - static VGAChar_t* frameBuffer = (VGAChar_t*)0xB8000; static char clearColour = 0x0; @@ -14,74 +11,79 @@ static char fgColour = 0xF; Cursor cursor; -void cls() { +void Cls() { for (uint8_t x = 0; x < TERMINAL_WIDTH; x++) for (uint8_t y = 0; y < TERMINAL_HEIGHT; y++) - puts(x, y, ' ', fgColour, clearColour); + Puts(x, y, ' ', fgColour, clearColour); cursor.x = 0; cursor.y = 0; - updateCursor(cursor); + UpdateCursor(cursor); } -void puts(char input) { +void Puts(char input) { if (cursor.x + 1 > TERMINAL_WIDTH) { - nline(); + Nline(); } if (input == '\n') { - nline(); + Nline(); } else { - puts(cursor.x, cursor.y, input, fgColour, bgColour); + Puts(cursor.x, cursor.y, input, fgColour, bgColour); cursor.x++; } - updateCursor(cursor); + UpdateCursor(cursor); } -void puts(int x, int y, char c, char foreground, char background) { +void Puts(int x, int y, char c, char foreground, char background) { frameBuffer[(y * TERMINAL_WIDTH) + x].c = c; frameBuffer[(y * TERMINAL_WIDTH) + x].foreground = foreground; frameBuffer[(y * TERMINAL_WIDTH) + x].background = background; } -void nline() { +void Nline() { cursor.y++; cursor.x = 0; - updateCursor(cursor); + UpdateCursor(cursor); } -void write(char* input) { +void Write(char* input) { for (uint32_t i = 0; i < strlen(input); i++) { if (cursor.x + 1 > TERMINAL_WIDTH) { - nline(); + Nline(); } if (input[i] == '\n') { - nline(); + Nline(); } else { - puts(cursor.x, cursor.y, input[i], fgColour, bgColour); + Puts(cursor.x, cursor.y, input[i], fgColour, bgColour); cursor.x++; } } - updateCursor(cursor); + UpdateCursor(cursor); } -void writeln(char* input) { - write(input); - nline(); +void Writeln(char* input) { + Write(input); + Nline(); } -void setClearColour(char col) { +void SetClearColour(char col) { clearColour = col; } -void setFGColour(char col) { +void SetFGColour(char col) { fgColour = col; } -void setBGColour(char col) { +void SetBGColour(char col) { bgColour = col; } -void showCursor() { +void ResetTermColours() { + bgColour = 0x0; + fgColour = 0xF; +} + +void ShowCursor() { outb(0x3D4, 0x0A); outb(0x3D5, (inb(0x3D5) & 0xC0) | 0x0); // Cursor start top @@ -89,12 +91,12 @@ void showCursor() { outb(0x3D5, (inb(0x3D5) & 0xE0) | 0xF); // Cursor start bottom } -void hideCursor() { +void HideCursor() { outb(0x3D4, 0x0A); outb(0x3D5, 0x20); } -void updateCursor(Cursor c) { +void UpdateCursor(Cursor c) { uint16_t pos = c.y * TERMINAL_WIDTH + c.x; outb(0x3D4, 0x0F); @@ -103,9 +105,13 @@ void updateCursor(Cursor c) { outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF)); } -void setCursorPosition(int x, int y) { +void SetCursorPosition(int x, int y) { cursor.x = x; cursor.y = y; - updateCursor(cursor); + UpdateCursor(cursor); +} + +Cursor GetCursorPosition() { + return cursor; } uint8_t getVGACol(char f, char b) { diff --git a/kernel/drivers/terminal/terminal.h b/kernel/drivers/terminal/terminal.h index 53680d2..09ac294 100644 --- a/kernel/drivers/terminal/terminal.h +++ b/kernel/drivers/terminal/terminal.h @@ -32,22 +32,27 @@ struct Cursor { uint8_t y; } __attribute__((packed)); -void cls(); +static const int TERMINAL_WIDTH = 80; +static const int TERMINAL_HEIGHT = 24; -void puts(char input); -void puts(int x, int y, char c, char foreground, char background); +void Cls(); -void nline(); +void Puts(char input); +void Puts(int x, int y, char c, char foreground, char background); -void write(char* input); -void writeln(char* input); +void Nline(); -void setClearColour(char col); -void setFGColour(char col); -void setBGColour(char col); +void Write(char* input); +void Writeln(char* input); -void showCursor(); -void hideCursor(); +void SetClearColour(char col); +void SetFGColour(char col); +void SetBGColour(char col); +void ResetTermColours(); -void updateCursor(Cursor c); -void setCursorPosition(int x, int y); +void ShowCursor(); +void HideCursor(); + +void UpdateCursor(Cursor c); +void SetCursorPosition(int x, int y); +Cursor GetCursorPosition(); diff --git a/kernel/gdt.cpp b/kernel/gdt.cpp index 70f8a78..755b25d 100644 --- a/kernel/gdt.cpp +++ b/kernel/gdt.cpp @@ -25,24 +25,23 @@ void lgdt(GDT_t GDT) { asm ("lgdt %0" : : "m"(GDT)); } -void initGDT() { - #define BochsBreak() outw(0x8A00,0x8A00); outw(0x8A00,0x08AE0); +int InitGDT() { _GDTptr.limit = (sizeof(SegmentDescriptor_t) * 5) - 1; _GDTptr.base = (uint32_t)&_GDT; // Null segment - setGDTGate(0, 0, 0, 0, 0); + SetGDTGate(0, 0, 0, 0, 0); // Code Base 0 limit 32 Access EXEC + RW - setGDTGate(1, 0x00000000, 0xFFFFFFFF, GDT_ACCESS_PRESENT | GDT_ACCESS_EXEC | GDT_ACCESS_RW, GDT_FLAG_GR_PAGE | GDT_FLAG_SZ_32B); + SetGDTGate(1, 0x00000000, 0xFFFFFFFF, GDT_ACCESS_PRESENT | GDT_ACCESS_EXEC | GDT_ACCESS_RW, GDT_FLAG_GR_PAGE | GDT_FLAG_SZ_32B); // Data Base 0 Limit 32 Access RW - setGDTGate(2, 0x00000000, 0xFFFFFFFF, GDT_ACCESS_PRESENT | GDT_ACCESS_RW, GDT_FLAG_GR_PAGE | GDT_FLAG_SZ_32B); + SetGDTGate(2, 0x00000000, 0xFFFFFFFF, GDT_ACCESS_PRESENT | GDT_ACCESS_RW, GDT_FLAG_GR_PAGE | GDT_FLAG_SZ_32B); lgdt(_GDTptr); - BochsBreak(); SEGMENTS_RELOAD(); + return 1; } -void setGDTGate(uint32_t index, uint32_t baseAddr, uint32_t limitAddr, uint8_t accessLvl, uint8_t flags) { +void SetGDTGate(uint32_t index, uint32_t baseAddr, uint32_t limitAddr, uint8_t accessLvl, uint8_t flags) { if (index > 5) return; _GDT[index].base_low = (baseAddr & 0xFFFF); _GDT[index].base_middle = (baseAddr >> 16) & 0xFF; diff --git a/kernel/gdt.h b/kernel/gdt.h index 8049c91..0b0a08a 100644 --- a/kernel/gdt.h +++ b/kernel/gdt.h @@ -19,5 +19,5 @@ extern "C" { extern void SEGMENTS_RELOAD(void); } -void initGDT(); -void setGDTGate(uint32_t index, uint32_t baseAddr, uint32_t limitAddr, uint8_t accessLvl, uint8_t flags); +int InitGDT(); +void SetGDTGate(uint32_t index, uint32_t baseAddr, uint32_t limitAddr, uint8_t accessLvl, uint8_t flags); diff --git a/kernel/idt.cpp b/kernel/idt.cpp new file mode 100644 index 0000000..e69de29 diff --git a/kernel/idt.h b/kernel/idt.h new file mode 100644 index 0000000..e69de29 diff --git a/kernel/multiboot.h b/kernel/multiboot.h index ba4ff57..0a66564 100644 --- a/kernel/multiboot.h +++ b/kernel/multiboot.h @@ -3,7 +3,7 @@ #include // Totally stolen from here https://forum.osdev.org/viewtopic.php?f=1&t=8881 -struct multibootInfo_t { +struct MultibootInfo_t { uint32_t flags; uint32_t mem_lower; uint32_t mem_upper; diff --git a/kernel/panic.cpp b/kernel/panic.cpp new file mode 100644 index 0000000..462b3e7 --- /dev/null +++ b/kernel/panic.cpp @@ -0,0 +1,83 @@ +#include "panic.h" + +#include +#include +#include + +#include + +void PanicKernel(char code, char* why, char* where, char* what) { + if (why == "") { + switch (code) { + case 0x00: + why = "Self triggered panic"; + break; + + default: + why = "Uknown Error"; + break; + } + } + + ResetTermColours(); + HideCursor(); + Cls(); + + SetBGColour(VGA_RED); + for (uint8_t i = 0; i < TERMINAL_WIDTH; i++) + Puts(' '); + + SetCursorPosition(34, 0); + Write("KERNEL PANIC"); + + ResetTermColours(); + SetFGColour(VGA_DARK_GREY); + SetCursorPosition(1, 3); + Write("OOPSIE WOOPSIE! WE MADE A FUCKIE WUCKIE!! A WIDDLE FUCKO BOINGO! \n THE CODE MONKEYS AT HEADQUARTERS ARE WORKING VEWWY HAWD TO FIX THIS!"); + + ResetTermColours(); + SetCursorPosition(1, 6); + Write("EXCEPTION: "); + Write(why); + Puts(' '); + Write(" (Error code 0x"); + Write(itohx(code)); + Puts(')'); + Nline(); Nline(); + + SetFGColour(VGA_GREY); + Write(" at: "); + Write(where); + Puts(' '); + + bool seenBracket = false; + for(uint32_t i = 0; i < strlen(what); i++) { + if (what[i] == '(') + seenBracket = true; + + if (GetCursorPosition().x > 60) { + Write(" ..."); + break; + } + + if (!seenBracket) { + SetFGColour(VGA_YELLOW); + } else { + SetFGColour(VGA_GREY); + } + Puts(what[i]); + } + + + SetCursorPosition(0, TERMINAL_HEIGHT); + SetBGColour(VGA_GREY); + for (uint8_t i = 0; i < TERMINAL_WIDTH; i++) + Puts(' '); + + SetFGColour(VGA_BLACK); + SetCursorPosition(1, TERMINAL_HEIGHT); + Write("Error Code: 0x"); + Write(itohx(code)); + + asm("cli"); // Clear interrupt bit / dissable interrupts +} diff --git a/kernel/panic.h b/kernel/panic.h new file mode 100644 index 0000000..a84e2f7 --- /dev/null +++ b/kernel/panic.h @@ -0,0 +1,3 @@ +#pragma once + +void PanicKernel(char code, char* why = "", char* where = "", char* what = ""); diff --git a/lib/kernel/logger/logger.cpp b/lib/kernel/logger/logger.cpp index 4a4995a..3006189 100644 --- a/lib/kernel/logger/logger.cpp +++ b/lib/kernel/logger/logger.cpp @@ -3,24 +3,24 @@ #include void loggerLog(char* str) { - write(" "); - writeln(str); + Write(" "); + Writeln(str); } void loggerOK(char* str) { - write("["); - setFGColour(VGA_BRIGHT_GREEN); - write(" OK "); - setFGColour(VGA_WHITE); - write("] "); - writeln(str); + Write("["); + SetFGColour(VGA_BRIGHT_GREEN); + Write(" OK "); + SetFGColour(VGA_WHITE); + Write("] "); + Writeln(str); } void loggerFailed(char* str) { - write("["); - setFGColour(VGA_BRIGHT_RED); - write("FAILED"); - setFGColour(VGA_WHITE); - write("] "); - writeln(str); + Write("["); + SetFGColour(VGA_BRIGHT_RED); + Write("FAILED"); + SetFGColour(VGA_WHITE); + Write("] "); + Writeln(str); } diff --git a/lib/std/stdlib.cpp b/lib/std/stdlib.cpp index ecc11fb..9bb9c89 100644 --- a/lib/std/stdlib.cpp +++ b/lib/std/stdlib.cpp @@ -30,7 +30,7 @@ void reverse(char *s) { } -char* itoa(int n, char s[]) { +char* itoa(int n, char* s) { int i = 0; bool isNegative = false; @@ -58,3 +58,28 @@ char* itoa(int n, char s[]) { reverse(s); return s; } + +int hexlen(int n) { + if (!n) return 1; + + int ret = 0; + for (; n; n >>= 4) { + ++ret; + } + return ret; +} + +char* itohx(int n, char* s) { + const char hex_lookup[] = "0123456789ABCDEF"; + int len = hexlen(n); + + if (len & 1) { + *s++ = '0'; + } + s[len] = '\0'; + + for (--len; len >= 0; n >>= 4, --len) { + s[len] = hex_lookup[n & 0xf]; + } + return s; +} diff --git a/lib/std/stdlib.h b/lib/std/stdlib.h index 1a820d1..89637cf 100644 --- a/lib/std/stdlib.h +++ b/lib/std/stdlib.h @@ -2,4 +2,5 @@ void swap(int* a, int* b); void reverse(char* s); -char* itoa(int i, char s[] = ""); +char* itoa (int i, char* s = ""); +char* itohx(int i, char* s = ""); diff --git a/linux.bxrc b/linux.bxrc new file mode 100644 index 0000000..56d6eb0 --- /dev/null +++ b/linux.bxrc @@ -0,0 +1,51 @@ +# configuration file generated by Bochs +plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, parallel=1, serial=1, gameport=1 +#config_interface: win32config +display_library: x, options="gui_debug" +memory: host=2048, guest=2048 +romimage: file="/home/Ben/programming/OS/owos/build/OwOS.iso", address=0x0, options=none +boot: cdrom +floppy_bootsig_check: disabled=0 +ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14 +ata0-master: type=cdrom, path="/home/Ben/programming/OS/owos/build/OwOS.iso", status=inserted, model="Generic 1234", biosdetect=auto +ata0-slave: type=none +ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15 +ata1-master: type=none +ata1-slave: type=none +ata2: enabled=0 +ata3: enabled=0 +optromimage1: file=none +optromimage2: file=none +optromimage3: file=none +optromimage4: file=none +optramimage1: file=none +optramimage2: file=none +optramimage3: file=none +optramimage4: file=none +pci: enabled=1, chipset=i440fx +vga: extension=vbe, update_freq=5, realtime=1 +cpu: count=1, ips=4000000, model=bx_generic, reset_on_triple_fault=1, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0 +cpuid: level=6, stepping=3, model=3, family=6, vendor_string="GenuineIntel", brand_string=" Intel(R) Pentium(R) 4 CPU " +cpuid: mmx=1, apic=xapic, simd=sse2, sse4a=0, misaligned_sse=0, sep=1, movbe=0, adx=0 +cpuid: aes=0, sha=0, xsave=0, xsaveopt=0, x86_64=1, 1g_pages=0, pcid=0, fsgsbase=0 +cpuid: smep=0, smap=0, mwait=1, vmx=1 +print_timestamps: enabled=0 +port_e9_hack: enabled=0 +private_colormap: enabled=0 +clock: sync=none, time0=local, rtc_sync=0 +log: - +logprefix: %t%e%d +debug: action=ignore +info: action=report +error: action=report +panic: action=ask +keyboard: type=mf, serial_delay=250, paste_delay=100000, user_shortcut=none +mouse: type=ps2, enabled=0, toggle=ctrl+mbutton +sound: waveoutdrv=win, waveout=none, waveindrv=win, wavein=none, midioutdrv=win, midiout=none +speaker: enabled=1, mode=sound +parport1: enabled=1, file=none +parport2: enabled=0 +com1: enabled=1, mode=null +com2: enabled=0 +com3: enabled=0 +com4: enabled=0 \ No newline at end of file diff --git a/win32.bxrc b/win32.bxrc new file mode 100644 index 0000000..8522a71 --- /dev/null +++ b/win32.bxrc @@ -0,0 +1,56 @@ +# configuration file generated by Bochs +plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, parallel=1, serial=1, gameport=1 +config_interface: win32config +display_library: win32, options="gui_debug" +memory: host=32, guest=32 +romimage: file="C:\Program Files (x86)\Bochs-2.6.9/BIOS-bochs-latest", address=0x0, options=none +vgaromimage: file="C:\Program Files (x86)\Bochs-2.6.9/VGABIOS-lgpl-latest" +boot: cdrom +floppy_bootsig_check: disabled=0 +# no floppya +# no floppyb +ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14 +ata0-master: type=cdrom, path="E:\OS\OwOS\build\OwOS.iso", status=inserted, model="Generic 1234", biosdetect=auto +ata0-slave: type=none +ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15 +ata1-master: type=none +ata1-slave: type=none +ata2: enabled=0 +ata3: enabled=0 +optromimage1: file=none +optromimage2: file=none +optromimage3: file=none +optromimage4: file=none +optramimage1: file=none +optramimage2: file=none +optramimage3: file=none +optramimage4: file=none +pci: enabled=1, chipset=i440fx +vga: extension=vbe, update_freq=5, realtime=1 +cpu: count=1, ips=4000000, model=bx_generic, reset_on_triple_fault=1, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0 +cpuid: level=6, stepping=3, model=3, family=6, vendor_string="GenuineIntel", brand_string=" Intel(R) Pentium(R) 4 CPU " +cpuid: mmx=1, apic=xapic, simd=sse2, sse4a=0, misaligned_sse=0, sep=1, movbe=0, adx=0 +cpuid: aes=0, sha=0, xsave=0, xsaveopt=0, x86_64=1, 1g_pages=0, pcid=0, fsgsbase=0 +cpuid: smep=0, smap=0, mwait=1, vmx=1 +print_timestamps: enabled=0 +port_e9_hack: enabled=0 +private_colormap: enabled=0 +clock: sync=none, time0=local, rtc_sync=0 +# no cmosimage +# no loader +log: - +logprefix: %t%e%d +debug: action=ignore +info: action=report +error: action=report +panic: action=ask +keyboard: type=mf, serial_delay=250, paste_delay=100000, user_shortcut=none +mouse: type=ps2, enabled=0, toggle=ctrl+mbutton +sound: waveoutdrv=win, waveout=none, waveindrv=win, wavein=none, midioutdrv=win, midiout=none +speaker: enabled=1, mode=sound +parport1: enabled=1, file=none +parport2: enabled=0 +com1: enabled=1, mode=null +com2: enabled=0 +com3: enabled=0 +com4: enabled=0 \ No newline at end of file