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

View File

@@ -3,9 +3,6 @@
#include <kernel/kernio.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 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) {

View File

@@ -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();

View File

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

View File

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

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>
// 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;

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 = "");