2015-10-10 16:03:53 +00:00
# include <base.hpp>
2015-10-10 12:59:23 +00:00
# include <output.hpp>
# include <serial.hpp>
2015-10-10 16:03:53 +00:00
# include <textDISP.hpp>
2015-10-10 17:34:31 +00:00
# include <gdt.hpp>
2015-10-11 10:38:56 +00:00
# include <idt.hpp>
2015-10-13 14:11:06 +00:00
# include <keyboard.hpp>
2016-04-15 20:04:01 +00:00
# include <Multitasking.hpp>
2016-02-04 11:57:35 +00:00
# include <multiboot.h>
2016-03-15 20:38:25 +00:00
# include <blockdev.hpp>
2016-06-17 19:39:43 +00:00
# include <pmm.hpp>
2015-10-11 10:38:56 +00:00
extern " C " void intr_stub_0 ( void ) ;
2016-06-26 15:14:09 +00:00
void main ( void * * programs , MTGosHAL : : Serial & debug , MTGosHAL : : PMM & mm , MTGosHAL : : Screen & out ,
MTGosHAL : : Screen & err , MTGosHAL : : Keyboard & in , MTGosHAL : : Multitasking & tasks , MTGosHAL : : BlockDevice & disk ) ;
2016-05-22 20:26:47 +00:00
void * * progs ;
2015-10-10 16:03:53 +00:00
namespace MTGosHAL {
2015-10-14 18:02:41 +00:00
Serial debug ;
2016-06-17 19:39:43 +00:00
GDT gdt ;
IDT idt ;
PMM mm ;
2015-10-14 18:02:41 +00:00
Screen out ;
Screen err ;
Keyboard in ;
2015-10-18 19:57:31 +00:00
Multitasking tasks ;
2016-03-15 20:38:25 +00:00
BlockDevice disk ;
2016-02-04 11:57:35 +00:00
void main ( int eax , struct multiboot_info * ebx ) {
2016-06-17 19:39:43 +00:00
//Init debug output
new ( & debug ) Serial ( ) ;
debug < < " Hello debugger! This is MTGos v00r01 \n These logs are probably very long, so please redirect the output to a file. \n " ;
debug < < " Init GDT \n " ;
new ( & gdt ) GDT ( ) ;
gdt . setEntry ( 0 , 0 , 0 , 0 ) ;
2015-10-10 17:34:31 +00:00
gdt . setEntry ( 1 , 0 , 0xfffff , GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_CODESEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT ) ;
gdt . setEntry ( 2 , 0 , 0xfffff , GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_DATASEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT ) ;
gdt . setEntry ( 3 , 0 , 0xfffff , GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_CODESEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT | GDT_FLAG_RING3 ) ;
gdt . setEntry ( 4 , 0 , 0xfffff , GDT_FLAG_SEGMENT | GDT_FLAG_32_BIT | GDT_FLAG_DATASEG | GDT_FLAG_4K_GRAN | GDT_FLAG_PRESENT | GDT_FLAG_RING3 ) ;
2016-04-15 20:04:01 +00:00
gdt . setEntry ( 5 , ( uint32_t ) tasks . tss , sizeof ( tasks . tss ) , GDT_FLAG_RING3 | GDT_FLAG_TSS | GDT_FLAG_PRESENT ) ;
gdt . setEntry ( 6 , 0 , 0xfffff , GDT_FLAG_RING3 | GDT_FLAG_TSS | GDT_FLAG_PRESENT ) ;
2015-10-10 17:34:31 +00:00
gdt . apply ( ) ;
2016-06-17 19:39:43 +00:00
debug < < " Init IDT \n " ;
new ( & idt ) IDT ( ) ;
for ( int i = 0 ; i < 256 ; i + + ) {
2015-10-14 18:02:41 +00:00
idt . setEntry ( i , ( void * ) ( ( uint32_t ) & intr_stub_0 + i * 16 ) , SEG_KERNEL , IDT_INTERRUPT_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED ) ;
2015-10-11 10:38:56 +00:00
}
2016-05-22 18:13:06 +00:00
idt . setEntry ( 48 , ( void * ) ( ( uint32_t ) & intr_stub_0 + 768 ) , SEG_KERNEL , IDT_TRAP_GATE | IDT_SEG_32_BIT | IDT_RING_3 | IDT_USED ) ;
2015-10-14 18:02:41 +00:00
idt . setEntry ( 8 , ( void * ) ( ( uint32_t ) & intr_stub_0 + 128 ) , SEG_DBL_FAULT , IDT_TASK_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED ) ;
idt . apply ( ) ;
2016-06-17 19:39:43 +00:00
asm volatile ( " ltr %%ax " : : " a " ( 5 < < 3 ) ) ;
debug < < " Init MM \n " ;
new ( & mm ) PMM ( ebx ) ;
debug < < " Init Screen output \n " ;
new ( & out ) Screen ( ebx ) ;
new ( & err ) Screen ( ebx ) ;
out < < BG_color : : BLACK < < FG_color : : WHITE < < " Loading MTGos... \n " ;
err < < BG_color : : BLACK < < FG_color : : RED ;
debug < < " Init Keyboard \n " ;
new ( & in ) Keyboard ( ) ;
debug < < " Init Multitasking \n " ;
new ( & tasks ) Multitasking ( ) ;
debug < < " Init Disk \n " ;
new ( & disk ) BlockDevice ( ) ;
debug < < " Kernel initialized \n " ;
if ( eax ! = 0x2BADB002 )
err < < " System wasn't loaded by a Multiboot-conformant launcher! \n " ;
2016-05-21 16:20:53 +00:00
multiboot_mod_list * mods = ( multiboot_mod_list * ) ebx - > mods_addr ;
2016-05-22 20:26:47 +00:00
progs = ( void * * ) mm . alloc ( 4096 ) ;
for ( int i = 0 ; i < 1024 ; i + + ) {
progs [ i ] = nullptr ;
}
for ( uint32_t i = 0 ; i < ( ebx - > mods_count < 1023 ? ebx - > mods_count : 1023 ) ; i + + ) { //Basically until MIN(ebx->mods_count, 1023), as we only support loading up to 1023 programs directly.
2016-04-26 18:43:12 +00:00
progs [ i ] = ( void * ) ( mods [ i ] . mod_start ) ;
2016-05-05 19:12:36 +00:00
debug < < " Found module! \n " ;
2016-04-26 18:43:12 +00:00
}
2016-06-26 15:14:09 +00:00
: : main ( progs , debug , mm , out , err , in , tasks , disk ) ;
2016-06-17 19:39:43 +00:00
uint8_t buf [ 512 ] ;
disk . readSector ( disk . getDriveNumByName ( " ATA0m1 " ) , 0 , buf ) ;
out < < ( char * ) buf ;
2016-04-15 20:04:01 +00:00
sti ( ) ;
2015-10-10 16:03:53 +00:00
for ( ; ; ) ;
2015-10-10 12:59:23 +00:00
}
}
2015-10-14 18:02:41 +00:00
typedef void ( * constructor ) ( ) ;
2016-02-06 18:57:44 +00:00
typedef void ( * destructor ) ( ) ;
extern " C " destructor start_dtors ;
extern " C " destructor end_dtors ;
2016-02-04 11:57:35 +00:00
extern " C " void init ( int eax , struct multiboot_info * ebx ) {
MTGosHAL : : main ( eax , ebx ) ;
2016-02-06 18:57:44 +00:00
for ( destructor * i = & start_dtors ; i ! = & end_dtors ; i + + )
( * i ) ( ) ;
2015-10-10 12:59:23 +00:00
}
extern " C " void __cxa_pure_virtual ( ) {
2015-10-14 18:02:41 +00:00
MTGosHAL : : debug < < " A pure virtual function just got called. \n " ;
2015-10-10 12:59:23 +00:00
}