Kernel panic

This commit is contained in:
Ben
2019-04-30 19:32:23 +01:00
parent fdff77afd1
commit 390fc87c20
18 changed files with 323 additions and 90 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -2,6 +2,7 @@
#include <kernel/drivers/terminal/terminal.h> #include <kernel/drivers/terminal/terminal.h>
#include <kernel/kernio.h> #include <kernel/kernio.h>
#include <kernel/panic.h>
#include <kernel/gdt.h> #include <kernel/gdt.h>
#include <lib/std/stdlib.h> #include <lib/std/stdlib.h>
@@ -14,34 +15,37 @@ extern "C" {
extern "C" extern "C"
int kernel_main(uint32_t magic, multibootInfo_t* multiboot) { int kernel_main(uint32_t magic, MultibootInfo_t* multiboot) {
cls(); Cls();
showCursor(); ShowCursor();
setFGColour(VGA_GREEN); SetFGColour(VGA_GREEN);
writeln("OwOS V0.2 Starting Up..."); Writeln("OwOS V0.2 Starting Up...");
setFGColour(VGA_WHITE); SetFGColour(VGA_WHITE);
nline(); Nline();
// Init systems // Init systems
initGDT(); int igdt = InitGDT();
loggerOK("GDT Loaded"); 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(); SetFGColour(VGA_YELLOW);
write("OwO, What's This?"); Writeln("Welcome to OwOS");
setFGColour(VGA_BRIGHT_MAGENTA); SetFGColour(VGA_BRIGHT_MAGENTA);
write(" *notices "); Write("~#");
write(itoa(multiboot->mem_upper / 1024));
writeln("mb of ram*");
setFGColour(VGA_WHITE);
nline();
setFGColour(VGA_YELLOW); PanicKernel(0x00, "Self Triggered Panic", "kernel.cpp:48", "kernel_main(uint32_t magic, MultibootInfo_t* multiboot)");
writeln("Welcome to OwOS");
setFGColour(VGA_BRIGHT_MAGENTA);
write("~#");
for (;;) for (;;)
asm("hlt"); asm("hlt");

View File

@@ -3,9 +3,6 @@
#include <kernel/kernio.h> #include <kernel/kernio.h>
#include <lib/std/string.h> #include <lib/std/string.h>
static const int TERMINAL_WIDTH = 80;
static const int TERMINAL_HEIGHT = 24;
static VGAChar_t* frameBuffer = (VGAChar_t*)0xB8000; static VGAChar_t* frameBuffer = (VGAChar_t*)0xB8000;
static char clearColour = 0x0; static char clearColour = 0x0;
@@ -14,74 +11,79 @@ static char fgColour = 0xF;
Cursor cursor; Cursor cursor;
void cls() { void Cls() {
for (uint8_t x = 0; x < TERMINAL_WIDTH; x++) for (uint8_t x = 0; x < TERMINAL_WIDTH; x++)
for (uint8_t y = 0; y < TERMINAL_HEIGHT; y++) 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; cursor.x = 0; cursor.y = 0;
updateCursor(cursor); UpdateCursor(cursor);
} }
void puts(char input) { void Puts(char input) {
if (cursor.x + 1 > TERMINAL_WIDTH) { if (cursor.x + 1 > TERMINAL_WIDTH) {
nline(); Nline();
} }
if (input == '\n') { if (input == '\n') {
nline(); Nline();
} else { } else {
puts(cursor.x, cursor.y, input, fgColour, bgColour); Puts(cursor.x, cursor.y, input, fgColour, bgColour);
cursor.x++; 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].c = c;
frameBuffer[(y * TERMINAL_WIDTH) + x].foreground = foreground; frameBuffer[(y * TERMINAL_WIDTH) + x].foreground = foreground;
frameBuffer[(y * TERMINAL_WIDTH) + x].background = background; frameBuffer[(y * TERMINAL_WIDTH) + x].background = background;
} }
void nline() { void Nline() {
cursor.y++; cursor.y++;
cursor.x = 0; cursor.x = 0;
updateCursor(cursor); UpdateCursor(cursor);
} }
void write(char* input) { void Write(char* input) {
for (uint32_t i = 0; i < strlen(input); i++) { for (uint32_t i = 0; i < strlen(input); i++) {
if (cursor.x + 1 > TERMINAL_WIDTH) { if (cursor.x + 1 > TERMINAL_WIDTH) {
nline(); Nline();
} }
if (input[i] == '\n') { if (input[i] == '\n') {
nline(); Nline();
} }
else { else {
puts(cursor.x, cursor.y, input[i], fgColour, bgColour); Puts(cursor.x, cursor.y, input[i], fgColour, bgColour);
cursor.x++; cursor.x++;
} }
} }
updateCursor(cursor); UpdateCursor(cursor);
} }
void writeln(char* input) { void Writeln(char* input) {
write(input); Write(input);
nline(); Nline();
} }
void setClearColour(char col) { void SetClearColour(char col) {
clearColour = col; clearColour = col;
} }
void setFGColour(char col) { void SetFGColour(char col) {
fgColour = col; fgColour = col;
} }
void setBGColour(char col) { void SetBGColour(char col) {
bgColour = col; bgColour = col;
} }
void showCursor() { void ResetTermColours() {
bgColour = 0x0;
fgColour = 0xF;
}
void ShowCursor() {
outb(0x3D4, 0x0A); outb(0x3D4, 0x0A);
outb(0x3D5, (inb(0x3D5) & 0xC0) | 0x0); // Cursor start top outb(0x3D5, (inb(0x3D5) & 0xC0) | 0x0); // Cursor start top
@@ -89,12 +91,12 @@ void showCursor() {
outb(0x3D5, (inb(0x3D5) & 0xE0) | 0xF); // Cursor start bottom outb(0x3D5, (inb(0x3D5) & 0xE0) | 0xF); // Cursor start bottom
} }
void hideCursor() { void HideCursor() {
outb(0x3D4, 0x0A); outb(0x3D4, 0x0A);
outb(0x3D5, 0x20); outb(0x3D5, 0x20);
} }
void updateCursor(Cursor c) { void UpdateCursor(Cursor c) {
uint16_t pos = c.y * TERMINAL_WIDTH + c.x; uint16_t pos = c.y * TERMINAL_WIDTH + c.x;
outb(0x3D4, 0x0F); outb(0x3D4, 0x0F);
@@ -103,9 +105,13 @@ void updateCursor(Cursor c) {
outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF)); 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; cursor.x = x; cursor.y = y;
updateCursor(cursor); UpdateCursor(cursor);
}
Cursor GetCursorPosition() {
return cursor;
} }
uint8_t getVGACol(char f, char b) { uint8_t getVGACol(char f, char b) {

View File

@@ -32,22 +32,27 @@ struct Cursor {
uint8_t y; uint8_t y;
} __attribute__((packed)); } __attribute__((packed));
void cls(); static const int TERMINAL_WIDTH = 80;
static const int TERMINAL_HEIGHT = 24;
void puts(char input); void Cls();
void puts(int x, int y, char c, char foreground, char background);
void nline(); void Puts(char input);
void Puts(int x, int y, char c, char foreground, char background);
void write(char* input); void Nline();
void writeln(char* input);
void setClearColour(char col); void Write(char* input);
void setFGColour(char col); void Writeln(char* input);
void setBGColour(char col);
void showCursor(); void SetClearColour(char col);
void hideCursor(); void SetFGColour(char col);
void SetBGColour(char col);
void ResetTermColours();
void updateCursor(Cursor c); void ShowCursor();
void setCursorPosition(int x, int y); void HideCursor();
void UpdateCursor(Cursor c);
void SetCursorPosition(int x, int y);
Cursor GetCursorPosition();

View File

@@ -25,24 +25,23 @@ void lgdt(GDT_t GDT) {
asm ("lgdt %0" : : "m"(GDT)); asm ("lgdt %0" : : "m"(GDT));
} }
void initGDT() { int InitGDT() {
#define BochsBreak() outw(0x8A00,0x8A00); outw(0x8A00,0x08AE0);
_GDTptr.limit = (sizeof(SegmentDescriptor_t) * 5) - 1; _GDTptr.limit = (sizeof(SegmentDescriptor_t) * 5) - 1;
_GDTptr.base = (uint32_t)&_GDT; _GDTptr.base = (uint32_t)&_GDT;
// Null segment // Null segment
setGDTGate(0, 0, 0, 0, 0); SetGDTGate(0, 0, 0, 0, 0);
// Code Base 0 limit 32 Access EXEC + RW // 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 // 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); lgdt(_GDTptr);
BochsBreak();
SEGMENTS_RELOAD(); 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; if (index > 5) return;
_GDT[index].base_low = (baseAddr & 0xFFFF); _GDT[index].base_low = (baseAddr & 0xFFFF);
_GDT[index].base_middle = (baseAddr >> 16) & 0xFF; _GDT[index].base_middle = (baseAddr >> 16) & 0xFF;

View File

@@ -19,5 +19,5 @@ extern "C" {
extern void SEGMENTS_RELOAD(void); extern void SEGMENTS_RELOAD(void);
} }
void initGDT(); int InitGDT();
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);

0
kernel/idt.cpp Normal file
View File

0
kernel/idt.h Normal file
View File

View File

@@ -3,7 +3,7 @@
#include <lib/stdint.h> #include <lib/stdint.h>
// Totally stolen from here https://forum.osdev.org/viewtopic.php?f=1&t=8881 // 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 flags;
uint32_t mem_lower; uint32_t mem_lower;
uint32_t mem_upper; uint32_t mem_upper;

83
kernel/panic.cpp Normal file
View File

@@ -0,0 +1,83 @@
#include "panic.h"
#include <lib/stdint.h>
#include <lib/std/stdlib.h>
#include <lib/std/string.h>
#include <kernel/drivers/terminal/terminal.h>
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
}

3
kernel/panic.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
void PanicKernel(char code, char* why = "", char* where = "", char* what = "");

View File

@@ -3,24 +3,24 @@
#include <kernel/drivers/terminal/terminal.h> #include <kernel/drivers/terminal/terminal.h>
void loggerLog(char* str) { void loggerLog(char* str) {
write(" "); Write(" ");
writeln(str); Writeln(str);
} }
void loggerOK(char* str) { void loggerOK(char* str) {
write("["); Write("[");
setFGColour(VGA_BRIGHT_GREEN); SetFGColour(VGA_BRIGHT_GREEN);
write(" OK "); Write(" OK ");
setFGColour(VGA_WHITE); SetFGColour(VGA_WHITE);
write("] "); Write("] ");
writeln(str); Writeln(str);
} }
void loggerFailed(char* str) { void loggerFailed(char* str) {
write("["); Write("[");
setFGColour(VGA_BRIGHT_RED); SetFGColour(VGA_BRIGHT_RED);
write("FAILED"); Write("FAILED");
setFGColour(VGA_WHITE); SetFGColour(VGA_WHITE);
write("] "); Write("] ");
writeln(str); Writeln(str);
} }

View File

@@ -30,7 +30,7 @@ void reverse(char *s) {
} }
char* itoa(int n, char s[]) { char* itoa(int n, char* s) {
int i = 0; int i = 0;
bool isNegative = false; bool isNegative = false;
@@ -58,3 +58,28 @@ char* itoa(int n, char s[]) {
reverse(s); reverse(s);
return 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;
}

View File

@@ -2,4 +2,5 @@
void swap(int* a, int* b); void swap(int* a, int* b);
void reverse(char* s); void reverse(char* s);
char* itoa(int i, char s[] = ""); char* itoa (int i, char* s = "");
char* itohx(int i, char* s = "");

51
linux.bxrc Normal file
View File

@@ -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

56
win32.bxrc Normal file
View File

@@ -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