2021-05-10 20:33:25 +00:00
# include "kernel.h"
2022-08-22 19:16:34 +00:00
extern " C " void early_main ( )
{
2022-02-26 19:44:16 +00:00
kterm_init ( ) ;
2022-09-01 14:11:35 +00:00
initGDT ( ) ;
2022-08-22 19:16:34 +00:00
2022-08-17 23:26:49 +00:00
init_serial ( ) ;
2022-08-18 22:44:52 +00:00
print_serial ( " Hello Higher half kernel! \n " ) ;
init_idt ( ) ;
// Enable interrupts
asm volatile ( " STI " ) ;
2022-08-18 23:05:10 +00:00
2022-08-22 19:16:34 +00:00
/*
2022-02-26 19:44:16 +00:00
* Show a little banner for cuteness
*/
printf ( " |=== BarinkOS ===| \n " ) ;
2022-09-01 15:02:04 +00:00
printf ( " Kernel End Addr: 0x%x \n " , & kernel_end ) ;
2022-08-23 19:35:19 +00:00
2022-09-01 14:11:35 +00:00
uint32_t PageDirectoryEntryIndex = ( ( uint32_t ) & kernel_end + KERNEL_BASE_ADDR ) > > 22 ;
2022-08-23 19:35:19 +00:00
uint32_t PageTableEntryIndex = ( ( ( uint32_t ) & kernel_end + KERNEL_BASE_ADDR ) > > 12 ) & 0x1FFF ;
printf ( " Kernel End PDE: %d, PTE: %d \n " , PageDirectoryEntryIndex , PageTableEntryIndex ) ;
uint32_t BootInfoStruct = BootInfoBlock_pptr + 0xC0000000 ;
printf ( " Addr BootInfostruct: 0x%x \n " , BootInfoStruct ) ;
uint32_t PageDirectoryEntryIndex2 = ( BootInfoStruct ) > > 2 ;
uint32_t PageTableEntryIndex2 = ( BootInfoStruct > > 12 ) & 0x1FFF ;
printf ( " PDE: 0x%x, PTE: 0x%x \n " , PageDirectoryEntryIndex2 , PageTableEntryIndex2 ) ;
2021-12-28 18:47:32 +00:00
2022-08-22 19:16:34 +00:00
BootInfoBlock * BootInfo = ( BootInfoBlock * ) ( BootInfoBlock_pptr + 0xC0000000 ) ;
2021-12-28 18:47:32 +00:00
2022-09-01 14:11:35 +00:00
printf ( " Size of BootInfoBlock: %d bytes \n " , sizeof ( BootInfoBlock ) ) ;
2022-08-22 19:16:34 +00:00
printf ( " Bootloader information: \n " ) ;
if ( BootInfo - > ValidELFHeader )
{
printf ( " - Valid ELF Header is available! \n " ) ;
}
2022-02-26 19:44:16 +00:00
2022-08-22 19:16:34 +00:00
if ( BootInfo - > EnabledVBE )
{
printf ( " - VBE graphics mode is available! \n " ) ;
}
2022-02-26 19:44:16 +00:00
2022-08-22 19:16:34 +00:00
if ( BootInfo - > ValidSymbolTable )
{
printf ( " - Valid Symbol Table available at 0x%x. \n Tab Size: %d, str Size: %d \n " , BootInfo - > SymbolTableAddr , BootInfo - > SymbolTabSize , BootInfo - > SymbolStrSize ) ;
}
2022-02-26 19:44:16 +00:00
2022-08-22 19:16:34 +00:00
if ( BootInfo - > PhysicalMemoryMapAvailable )
{
printf ( " - Physical Memory Map available! \n " ) ;
2022-09-01 14:11:35 +00:00
printf ( " MemoryInfoheap size : %d bytes \n " , BootInfo - > map_size ) ;
2022-08-23 19:35:19 +00:00
// Print the memory regions
MemoryInfoBlock * currentBlock = ( MemoryInfoBlock * ) ( ( uint32_t ) BootInfo - > MemoryMap + KERNEL_BASE_ADDR ) ;
2022-09-01 18:16:16 +00:00
2022-09-01 14:11:35 +00:00
printf ( " Starting address: 0x%x \n " , currentBlock ) ;
2022-08-23 19:35:19 +00:00
while ( ( uint32_t ) currentBlock - > next ! = 0x0 )
{
2022-09-01 18:16:16 +00:00
printf ( " CurrentBlock: 0x%x \n " , ( uint32_t ) currentBlock ) ;
2022-08-23 19:35:19 +00:00
if ( IS_AVAILABLE_MEM ( currentBlock - > type ) ) {
//printf("AVAILABLE RAM\n");
}
else if ( IS_ACPI_MEM ( currentBlock - > type ) ) {
2022-09-01 14:11:35 +00:00
//printf("ACPI MEMORY\n");
2022-08-23 19:35:19 +00:00
}
else if ( IS_RESERVED_MEM ( currentBlock - > type ) ) {
2022-09-01 14:11:35 +00:00
// printf("RESERVED MEMORY\n");
2022-08-23 19:35:19 +00:00
}
else if ( IS_NVS_MEMORY ( currentBlock - > type ) ) {
2022-09-01 14:11:35 +00:00
// printf("NVS MEMORY \n");
2022-08-23 19:35:19 +00:00
}
else if ( IS_BADRAM_MEMORY ( currentBlock - > type ) ) {
2022-09-01 14:11:35 +00:00
// printf("BADRAM MEMORY \n");
2022-08-23 19:35:19 +00:00
}
else {
// printf("(TYPE 0x%x )TYPE NOT SPECIFIED\n", currentBlock->type);
}
currentBlock = ( MemoryInfoBlock * ) ( ( uint32_t ) currentBlock - > next + KERNEL_BASE_ADDR ) ;
}
2022-09-01 18:16:16 +00:00
printf ( " Starting physical memory management setup \n " ) ;
2022-09-01 14:11:35 +00:00
// Setup PhysicalMemoryManagement
SetupPhysicalMemoryManager ( BootInfo ) ;
2022-09-01 15:02:04 +00:00
2022-02-26 19:44:16 +00:00
}
2022-09-01 18:16:16 +00:00
printf ( " Enable Protected mode and jump to kernel main \n " ) ;
2022-08-21 19:15:15 +00:00
asm volatile ( " mov %cr0, %eax " ) ;
asm volatile ( " or $1, %eax " ) ;
2022-09-01 14:11:35 +00:00
asm volatile ( " mov %eax, %cr0 " ) ; // re-enable protected mode ?
2022-08-22 19:16:34 +00:00
kernel_main ( ) ;
2022-02-26 19:44:16 +00:00
}
2021-11-03 19:03:38 +00:00
2022-08-22 19:16:34 +00:00
void PhysicalMemoryAllocatorTest ( ) {
2022-09-01 15:02:04 +00:00
# ifdef UNIT_TESTS
// Small test!
void * block = allocate_block ( ) ;
void * block2 = allocate_block ( ) ;
printf ( " Allocated addresss 1: 0x%x 2: 0x%x \n " , ( uint32_t ) block , ( uint32_t ) block2 ) ;
free_block ( block ) ;
free_block ( block2 ) ;
void * block3 = allocate_block ( ) ;
printf ( " Allocated addresss 3: 0x%x \n " , ( uint32_t ) block3 ) ;
free_block ( block3 ) ;
# endif
2022-08-22 19:16:34 +00:00
}
extern " C " void kernel_main ( ) {
2022-08-19 21:44:38 +00:00
pit_initialise ( ) ;
2022-09-01 15:02:04 +00:00
startSuperVisorTerminal ( ) ;
2022-08-19 21:44:38 +00:00
}