1
This commit is contained in:
2022-06-23 13:15:23 +02:00
parent ceb17dd2c1
commit 704af6a6fd
4 changed files with 180 additions and 201 deletions

View File

@ -48,105 +48,98 @@
* *
* Autor: Michael Schoettner, 20.12.2018 *
*****************************************************************************/
#include "kernel/Globals.h"
#include "kernel/Paging.h"
#include "kernel/Globals.h"
// Bits fuer Eintraege in der Page-Table
#define PAGE_PRESENT 0x001
#define PAGE_WRITEABLE 0x002
#define PAGE_BIGSIZE 0x080
#define PAGE_RESERVED 0x800 // Bit 11 ist frei fuer das OS
#define PAGE_PRESENT 0x001
#define PAGE_WRITEABLE 0x002
#define PAGE_BIGSIZE 0x080
#define PAGE_RESERVED 0x800 // Bit 11 ist frei fuer das OS
// Adresse des Page-Directory (benoetigt 4 KB)
#define PAGE_DIRECTORY 0x200000
#define PAGE_DIRECTORY 0x200000
// Adresse der Page-Table (benoetigt 4 KB)
#define PAGE_TABLE 0x201000
#define PAGE_TABLE 0x201000
// Start- und End-Adresse der 4 KB Seiten die durch die Page-Table adressiert werden
#define FST_ALLOCABLE_PAGE 0x202000
#define LST_ALLOCABLE_PAGE 0x2FF000
// Start- und End-Adresse der 4 KB Seiten die durch die Page-Table adressiert werden
#define FST_ALLOCABLE_PAGE 0x202000
#define LST_ALLOCABLE_PAGE 0x2FF000
// Externe Funktionen in startup.asm
// Externe Funktionen in startup.asm
extern "C" {
void paging_on (unsigned int* p_pdir); // Paging einschalten
void invalidate_tlb_entry(unsigned int* ptr); // Page in TLB invalid.
void paging_on(unsigned int* p_pdir); // Paging einschalten
void invalidate_tlb_entry(unsigned int* ptr); // Page in TLB invalid.
}
/*****************************************************************************
* Funktion: pg_alloc_page *
*---------------------------------------------------------------------------*
* Beschreibung: Alloziert eine 4 KB Seite. Allozieren heisst hier *
* lediglich Setzen eines eigenen RESERVED-Bits. *
*****************************************************************************/
unsigned int * pg_alloc_page() {
unsigned int *p_page;
p_page = (unsigned int*) PAGE_TABLE;
unsigned int* pg_alloc_page() {
unsigned int* p_page;
p_page = (unsigned int*)PAGE_TABLE;
// 1. Eintrag ist fuer Null-Pointer-Exception reserviert
// ausserdem liegt an die Page-Table an Adresse PAGE_TABLE
// somit ist est PAGE_TABLE + 4 KB frei (bis max. 3 MB, da beginnt der Heap)
for (int i = 1; i < 1024; i++) {
p_page ++;
p_page++;
// pruefe ob Page frei
if ( ((*p_page) & PAGE_RESERVED) == 0) {
*p_page = ( *p_page | PAGE_RESERVED);
return (unsigned int*)(i<<12);
if (((*p_page) & PAGE_RESERVED) == 0) {
*p_page = (*p_page | PAGE_RESERVED);
return (unsigned int*)(i << 12);
}
}
return 0;
}
/*****************************************************************************
* Funktion: pg_write_protect_page *
*---------------------------------------------------------------------------*
* Beschreibung: Schreibschutz fuer die uebergebene Seite aktivieren. *
* Dies fuer das Debugging nuetzlich. *
*****************************************************************************/
void pg_write_protect_page(unsigned int *p_page) {
/* hier muss Code eingefügt werden */
void pg_write_protect_page(unsigned int* p_page) {
/* hier muss Code eingefügt werden */
}
/*****************************************************************************
* Funktion: pg_notpresent_page *
*---------------------------------------------------------------------------*
* Beschreibung: Seite als ausgelagert markieren. Nur fuer Testzwecke. *
*****************************************************************************/
void pg_notpresent_page(unsigned int *p_page) {
/* hier muss Code eingefügt werden */
void pg_notpresent_page(unsigned int* p_page) {
/* hier muss Code eingefügt werden */
}
/*****************************************************************************
* Funktion: pg_free_page *
*---------------------------------------------------------------------------*
* Beschreibung: Gibt eine 4 KB Seite frei. Es wird hierbei das RESERVED- *
* Bit geloescht. *
*****************************************************************************/
void pg_free_page(unsigned int *p_page) {
void pg_free_page(unsigned int* p_page) {
int idx = (unsigned int)p_page >> 12;
// ausserhalb Page ?
if (idx < 1 || idx > 1023) return ;
if (idx < 1 || idx > 1023) {
return;
}
// Eintrag einlesen und aendern (PAGE_WRITEABLE loeschen)
p_page = (unsigned int*) PAGE_TABLE;
p_page = (unsigned int*)PAGE_TABLE;
p_page += idx;
*p_page = ((idx << 12) | PAGE_WRITEABLE | PAGE_PRESENT);
}
/*****************************************************************************
* Funktion: pg_init *
*---------------------------------------------------------------------------*
@ -155,58 +148,58 @@ void pg_free_page(unsigned int *p_page) {
*****************************************************************************/
void pg_init() {
unsigned int i;
unsigned int *p_pdir; // Zeiger auf Page-Directory
unsigned int *p_page; // Zeiger auf einzige Page-Table fuer 4 KB Pages
unsigned int num_pages; // Anzahl 4 MB Pages die phys. Adressraum umfassen
unsigned int* p_pdir; // Zeiger auf Page-Directory
unsigned int* p_page; // Zeiger auf einzige Page-Table fuer 4 KB Pages
unsigned int num_pages; // Anzahl 4 MB Pages die phys. Adressraum umfassen
// wie viele 4 MB Seiten sollen als 'Present' angelegt werden,
// sodass genau der physikalische Adressraum abgedeckt ist?
num_pages = total_mem/(4096*1024);
num_pages = total_mem / (4096 * 1024);
kout << "pg_init: " << total_mem << endl;
kout << " total_mem: " << total_mem << endl;
kout << " #pages: " << total_mem/(4096*1024) << endl;
kout << " #pages: " << total_mem / (4096 * 1024) << endl;
//
// Aufbau des Page-Directory
//
// Eintrag 0: Zeiger auf 4 KB Page-Table
p_pdir = (unsigned int*) PAGE_DIRECTORY;
p_pdir = (unsigned int*)PAGE_DIRECTORY;
*p_pdir = PAGE_TABLE | PAGE_WRITEABLE | PAGE_PRESENT;
// Eintraege 1-1023: Direktes Mapping (1:1) auf 4 MB Pages (ohne Page-Table)
for (i = 1; i < 1024; i++) {
p_pdir ++;
if (i>num_pages)
*p_pdir = ( (i<<22) | PAGE_BIGSIZE);
else
*p_pdir = ( (i<<22) | PAGE_BIGSIZE | PAGE_WRITEABLE | PAGE_PRESENT);
p_pdir++;
if (i > num_pages) {
*p_pdir = ((i << 22) | PAGE_BIGSIZE);
} else {
*p_pdir = ((i << 22) | PAGE_BIGSIZE | PAGE_WRITEABLE | PAGE_PRESENT);
}
}
//
// 1. Page-Table
//
p_page = (unsigned int*) PAGE_TABLE;
p_page = (unsigned int*)PAGE_TABLE;
// ersten Eintrag loeschen -> not present, write protected -> Null-Pointer abfangen
*p_page = 0;
// Eintraege 1-1023: Direktes Mapping (1:1) auf 4 KB page frames
for (i = 1; i < 1024; i++) {
p_page ++;
p_page++;
// Seiten unter FST_ALLOCABLE_PAGE reservieren, damit diese nicht
// alloziert werden und das System kaputt geht
if ( (i<<12) >= FST_ALLOCABLE_PAGE)
if ((i << 12) >= FST_ALLOCABLE_PAGE) {
*p_page = ((i << 12) | PAGE_WRITEABLE | PAGE_PRESENT);
else
} else {
*p_page = ((i << 12) | PAGE_WRITEABLE | PAGE_PRESENT | PAGE_RESERVED);
}
}
// Paging aktivieren (in startup.asm)
paging_on( (unsigned int*) PAGE_DIRECTORY );
paging_on((unsigned int*)PAGE_DIRECTORY);
}

View File

@ -56,17 +56,15 @@
extern void pg_init();
// alloziert eine 4 KB Page
extern unsigned int * pg_alloc_page();
extern unsigned int* pg_alloc_page();
// Schreibschutz auf Seite setzen -> fuer debugging nuetzlich
extern void pg_write_protect_page(unsigned int *p_page);
extern void pg_write_protect_page(unsigned int* p_page);
// Present Bit loeschen
extern void pg_notpresent_page(unsigned int *p_page);
extern void pg_notpresent_page(unsigned int* p_page);
// gibt eine 4 KB Page frei
extern void pg_free_page(unsigned int *p_page);
extern void pg_free_page(unsigned int* p_page);
#endif

View File

@ -9,15 +9,14 @@
* *
* Autor: Michael Schoettner, 11.12.2018 *
*****************************************************************************/
#include "kernel/Globals.h"
#include "devices/CGA.h"
#include "kernel/Globals.h"
// in startup.asm
extern "C" {
// CR2 auslesen
unsigned int get_page_fault_address();
// 1st level interrupt handler in startup.asm sichert Zeiger auf Stackframe
// unmittelbar nach dem Interrupt und nachdem alle Register mit PUSHAD
// gesichert wurden
@ -47,43 +46,40 @@ extern "C" {
// | EDI |
// |-------------| <-- int_esp
void get_int_esp (unsigned int** esp);
void get_int_esp(unsigned int** esp);
}
void break_on_bluescreen() {
/* wenn auf diese Methode ein breakpoint in GDB gesetzt wird
so kann man sich mithilfe des Hex-Dumps umsehen
*/
}
// Cursor-Position
int bs_xpos = 0;
int bs_ypos = 0;
/*****************************************************************************
* Funktion: bs_clear *
*---------------------------------------------------------------------------*
* Beschreibung: Bildschirm loeschen. *
*****************************************************************************/
void bs_clear() {
unsigned int x,y;
unsigned short *ptr = (unsigned short*)0xb8000;
for (x=0; x<80; x++) {
for (y=0; y<25; y++)
*(ptr + y*80 + x) = (short)0x1F00;
unsigned int x;
unsigned int y;
unsigned short* ptr = (unsigned short*)0xb8000;
for (x = 0; x < 80; x++) {
for (y = 0; y < 25; y++) {
*(ptr + y * 80 + x) = (short)0x1F00;
}
}
bs_xpos = 0;
bs_ypos = 0;
}
/*****************************************************************************
* Funktion: bs_lf *
*---------------------------------------------------------------------------*
@ -91,98 +87,96 @@ void bs_clear() {
*****************************************************************************/
void bs_lf() {
bs_ypos++;
bs_xpos=0;
bs_xpos = 0;
}
/*****************************************************************************
* Funktion: bs_print_char *
*---------------------------------------------------------------------------*
* Beschreibung: Ein Zeichen ausgeben. *
*****************************************************************************/
void bs_print_char(char c) {
unsigned char *ptr = (unsigned char*)0xb8000;
*(ptr + bs_ypos*80*2 + bs_xpos*2) = c;
unsigned char* ptr = (unsigned char*)0xb8000;
*(ptr + bs_ypos * 80 * 2 + bs_xpos * 2) = c;
bs_xpos++;
}
/*****************************************************************************
* Funktion: bs_print_string *
*---------------------------------------------------------------------------*
* Beschreibung: Eine Zeichenkette ausgeben. *
*****************************************************************************/
void bs_print_string(char *str) {
while ( *str != '\0' ) {
bs_print_char( *str );
void bs_print_string(char* str) {
while (*str != '\0') {
bs_print_char(*str);
str++;
}
}
/*****************************************************************************
* Funktion: bs_printHexDigit *
*---------------------------------------------------------------------------*
* Beschreibung: Ein Hex-Zeichen ausgeben. *
*****************************************************************************/
void bs_printHexDigit(int c) {
if (c<10) bs_print_char('0'+(unsigned char)c);
else bs_print_char('A'+(unsigned char)(c-10));
if (c < 10) {
bs_print_char('0' + (unsigned char)c);
} else {
bs_print_char('A' + (unsigned char)(c - 10));
}
}
/*****************************************************************************
* Funktion: bs_print_uintHex *
*---------------------------------------------------------------------------*
* Beschreibung: Integer ausgeben. *
*****************************************************************************/
void bs_print_uintHex(unsigned int c) {
for (int i=28; i>=0; i=i-4) {
bs_printHexDigit( (c>>i)&0xF );
for (int i = 28; i >= 0; i = i - 4) {
bs_printHexDigit((c >> i) & 0xF);
}
}
/*****************************************************************************
* Funktion: bs_printReg *
*---------------------------------------------------------------------------*
* Beschreibung: String mit Integer ausgeben. Wird verwendet um ein *
* Register auszugeben. *
*****************************************************************************/
void bs_printReg(char *str, int value) {
void bs_printReg(char* str, int value) {
bs_print_string(str);
bs_print_uintHex(value);
bs_print_string(" \0");
}
/*****************************************************************************
* Funktion: bs_dump *
*---------------------------------------------------------------------------*
* Beschreibung: Hauptroutine des Bluescreens. *
*****************************************************************************/
void bs_dump (unsigned int exceptionNr) {
unsigned int *int_esp;
unsigned int *sptr;
void bs_dump(unsigned int exceptionNr) {
unsigned int* int_esp;
unsigned int* sptr;
unsigned int faultAdress;
unsigned int has_error_code = 0;
bs_clear ();
bs_clear();
bs_print_string("HHUos crashed with Exception\0");
// Exception mit Error-Code?
if ( (exceptionNr>=8 && exceptionNr<=14) || exceptionNr==17 || exceptionNr==30)
has_error_code = 1;
if ((exceptionNr >= 8 && exceptionNr <= 14) || exceptionNr == 17 || exceptionNr == 30) {
has_error_code = 1;
}
// Liegt ein Page-Fault vor?
if (exceptionNr==14) {
// Liegt ein Page-Fault vor?
if (exceptionNr == 14) {
faultAdress = get_page_fault_address();
// Zugriff auf Seite 0 ? -> Null-Ptr. Exception
if ((faultAdress & 0xFFFFF000) == 0)
exceptionNr=0x1B;
if ((faultAdress & 0xFFFFF000) == 0) {
exceptionNr = 0x1B;
}
}
bs_print_uintHex(exceptionNr);
@ -190,127 +184,123 @@ void bs_dump (unsigned int exceptionNr) {
// Spruch ausgeben
switch (exceptionNr) {
case 0x00: bs_print_string("Divide Error\0"); break;
case 0x01: bs_print_string("Debug Exception\0"); break;
case 0x02: bs_print_string("NMI\0"); break;
case 0x03: bs_print_string("Breakpoint Exception\0"); break;
case 0x04: bs_print_string("Into Exception\0"); break;
case 0x05: bs_print_string("Index out of range Exception\0"); break;
case 0x06: bs_print_string("Invalid Opcode\0"); break;
case 0x08: bs_print_string("Double Fault\0"); break;
case 0x0D: bs_print_string("General Protection Error\0"); break;
case 0x0E: bs_print_string("Page Fault\0"); break;
case 0x18: bs_print_string("Stack invalid\0"); break;
case 0x19: bs_print_string("Return missing\0"); break;
case 0x1A: bs_print_string("Type Test Failed\0"); break;
case 0x1B: bs_print_string("Null pointer exception\0"); break;
case 0x1C: bs_print_string("MAGIC.StackTest failed\0"); break;
case 0x1D: bs_print_string("Memory-Panic\0"); break;
case 0x1E: bs_print_string("Pageload failed\0"); break;
case 0x1F: bs_print_string("Stack overflow\0"); break;
default: bs_print_string("unknown\0");
case 0x00: bs_print_string("Divide Error\0"); break;
case 0x01: bs_print_string("Debug Exception\0"); break;
case 0x02: bs_print_string("NMI\0"); break;
case 0x03: bs_print_string("Breakpoint Exception\0"); break;
case 0x04: bs_print_string("Into Exception\0"); break;
case 0x05: bs_print_string("Index out of range Exception\0"); break;
case 0x06: bs_print_string("Invalid Opcode\0"); break;
case 0x08: bs_print_string("Double Fault\0"); break;
case 0x0D: bs_print_string("General Protection Error\0"); break;
case 0x0E: bs_print_string("Page Fault\0"); break;
case 0x18: bs_print_string("Stack invalid\0"); break;
case 0x19: bs_print_string("Return missing\0"); break;
case 0x1A: bs_print_string("Type Test Failed\0"); break;
case 0x1B: bs_print_string("Null pointer exception\0"); break;
case 0x1C: bs_print_string("MAGIC.StackTest failed\0"); break;
case 0x1D: bs_print_string("Memory-Panic\0"); break;
case 0x1E: bs_print_string("Pageload failed\0"); break;
case 0x1F: bs_print_string("Stack overflow\0"); break;
default: bs_print_string("unknown\0");
}
bs_print_string(")\0"); bs_lf();
bs_print_string(")\0");
bs_lf();
// Zeiger auf int_esp ueber startup.asm beschaffen (Stack-Layout siehe Anfang dieser Datei)
get_int_esp (&int_esp);
get_int_esp(&int_esp);
// wir müssen den Inhalt auslesen und das als Zeiger verwenden, um den Stack auszulesen
sptr = (unsigned int*) *int_esp;
sptr = (unsigned int*)*int_esp;
bs_lf();
// wichtigste Register ausgeben
// Exception mit Error-Code?
bs_printReg("EIP=\0", *(sptr + 8 + has_error_code) );
bs_printReg("EBP=\0", *(sptr + 2) );
bs_printReg("ESP=\0", *(sptr + 3) );
bs_printReg(" CS=\0", *(sptr + 9 + has_error_code) );
bs_lf();
// Exception mit Error-Code?
bs_printReg("EIP=\0", *(sptr + 8 + has_error_code));
bs_printReg("EBP=\0", *(sptr + 2));
bs_printReg("ESP=\0", *(sptr + 3));
bs_printReg(" CS=\0", *(sptr + 9 + has_error_code));
bs_lf();
// verbleibende nicht-fluechtige Register ausgeben
bs_printReg("EBX=\0", *(sptr + 4) );
bs_printReg("ESI=\0", *(sptr + 1) );
bs_printReg("EDI=\0", *(sptr) );
bs_lf();
// verbleibende fluechtige Register ausgeben
bs_printReg("EDX=\0", *(sptr + 5) );
bs_printReg("ECX=\0", *(sptr + 6) );
bs_printReg("EAX=\0", *(sptr + 7) );
bs_printReg("EFL=\0", *(sptr + 10) );
bs_printReg("EBX=\0", *(sptr + 4));
bs_printReg("ESI=\0", *(sptr + 1));
bs_printReg("EDI=\0", *(sptr));
bs_lf();
// verbleibende fluechtige Register ausgeben
bs_printReg("EDX=\0", *(sptr + 5));
bs_printReg("ECX=\0", *(sptr + 6));
bs_printReg("EAX=\0", *(sptr + 7));
bs_printReg("EFL=\0", *(sptr + 10));
bs_lf();
// Pagefault oder Null-Pointer?
if (exceptionNr==14 || exceptionNr==0x1B) {
if (exceptionNr == 14 || exceptionNr == 0x1B) {
bs_lf();
bs_print_string("Fault address = \0");
bs_print_uintHex(faultAdress);
bs_lf();
bs_print_string("Last useable address = \0");
bs_print_uintHex(total_mem-1);
bs_print_uintHex(total_mem - 1);
bs_lf();
}
// Exception mit Error-Code?
if ( has_error_code == 1) {
int error_nr = *(sptr + 8);
if (exceptionNr==14) {
if (error_nr==3) {
bs_print_string("Error: write access to read-only page.\0");
if (has_error_code == 1) {
int error_nr = *(sptr + 8);
if (exceptionNr == 14) {
if (error_nr == 3) {
bs_print_string("Error: write access to read-only page.\0");
} else if (error_nr == 2) {
bs_print_string("Error: read access to not-present page.\0");
} else if (error_nr == 0) {
bs_print_string("Error: access to a not-present page.\0");
} else {
bs_print_string("Error code = \0");
bs_print_uintHex(error_nr);
}
else if (error_nr==2) {
bs_print_string("Error: read access to not-present page.\0");
}
else if (error_nr==0) {
bs_print_string("Error: access to a not-present page.\0");
}
else {
bs_print_string("Error code = \0");
bs_print_uintHex( error_nr );
}
bs_lf();
}
else {
bs_print_string("Error code = \0");
bs_print_uintHex( error_nr );
bs_lf();
}
} else {
bs_print_string("Error code = \0");
bs_print_uintHex(error_nr);
bs_lf();
}
}
// Calling stack ...
bs_lf();
bs_print_string("Calling Stack:\0"); bs_lf();
bs_print_string("Calling Stack:\0");
bs_lf();
int x = 0;
unsigned int *ebp = (unsigned int *) *(sptr + 2);
unsigned int* ebp = (unsigned int*)*(sptr + 2);
unsigned int raddr;
// solange eip > 1 MB && ebp < 128 MB, max. Aufruftiefe 10
while ( *ebp>0x100000 && *ebp<0x8000000 && x<10 ) {
raddr = *(ebp+1);
bs_printReg(" raddr=\0", raddr ); bs_lf();
while (*ebp > 0x100000 && *ebp < 0x8000000 && x < 10) {
raddr = *(ebp + 1);
bs_printReg(" raddr=\0", raddr);
bs_lf();
// dynamische Kette -> zum Aufrufer
ebp = (unsigned int*) *ebp;
ebp = (unsigned int*)*ebp;
x++;
}
if (x==0) {
bs_print_string(" empty\0"); bs_lf();
if (x == 0) {
bs_print_string(" empty\0");
bs_lf();
}
bs_lf();
// nur falls gdb benutzt werden soll
break_on_bluescreen();
bs_print_string("System halted\0");
}

View File

@ -14,8 +14,6 @@
#define __Bluescreen_include__
// dump blue screen (will not return)
void bs_dump (unsigned int exceptionNr);
void bs_dump(unsigned int exceptionNr);
#endif