2021-05-10 20:33:25 +00:00
|
|
|
#include "kernel.h"
|
2021-12-20 20:53:57 +00:00
|
|
|
|
2022-08-17 23:26:49 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
extern "C" void kernel_main (BootInfo* bootinfo) {
|
2022-08-18 22:44:52 +00:00
|
|
|
pit_initialise();
|
2022-03-18 21:09:04 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
startSuperVisorTerminal(bootinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" void early_main(unsigned long magic, unsigned long addr){
|
|
|
|
/**
|
|
|
|
* Initialize terminal interface
|
|
|
|
* NOTE: This should be done later on , the magic value should be checked first.
|
|
|
|
*/
|
2022-08-17 23:26:49 +00:00
|
|
|
initGDT();
|
2022-02-26 19:44:16 +00:00
|
|
|
kterm_init();
|
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");
|
|
|
|
|
|
|
|
// map the multiboot structure into virtual memory
|
|
|
|
// so we can gather the necessary data from it.
|
|
|
|
/* const uint32_t KERNEL_BASE_ADDR = 0xC0000000;
|
|
|
|
|
|
|
|
uint32_t pageDirectoryIndex = (addr + KERNEL_BASE_ADDR) >> 22;
|
|
|
|
printf("pageDirectoryIndex: %d\n", pageDirectoryIndex);
|
|
|
|
|
|
|
|
uint32_t pageTableIndex = (addr + KERNEL_BASE_ADDR >> 12) & 0x1FFF;
|
|
|
|
printf("PagTableIndex: %d\n", pageTableIndex);
|
|
|
|
|
|
|
|
printf("boot_page_directory addr: 0x%x\n", &boot_page_directory);
|
|
|
|
printf("boot_page_table addr: 0x%x\n", &boot_page_table);
|
|
|
|
|
|
|
|
uint32_t* pageDirectoryEntry = (uint32_t*) ((uint32_t) &boot_page_directory) + (pageDirectoryIndex * 4);
|
|
|
|
printf("page_directory_entry addr: 0x%x\n", pageDirectoryEntry);
|
|
|
|
|
|
|
|
*pageDirectoryEntry = ( addr & 0xFFFFF000 ) | 0x003;
|
|
|
|
|
|
|
|
uint32_t* page_table_entry = (uint32_t*) ((uint32_t) &boot_page_table) + ( pageTableIndex * 4);
|
|
|
|
printf("page_table_entry addr: 0x%x\n" , page_table_entry);
|
|
|
|
|
|
|
|
*page_table_entry = addr | 0x003;
|
|
|
|
|
|
|
|
// Reload CR3 to force a flush
|
|
|
|
asm("movl %cr3, %ecx;" "movl %ecx, %cr3" );
|
|
|
|
*/
|
2022-08-18 23:05:10 +00:00
|
|
|
|
|
|
|
printf("DEBUG:\n Magic: 0x%x\n MBT_addr: 0x%x\n", magic, addr);
|
2022-02-26 19:44:16 +00:00
|
|
|
/**
|
|
|
|
* Check Multiboot magic number
|
|
|
|
* NOTE: Printf call should not be a thing this early on ...
|
|
|
|
*/
|
|
|
|
if (magic != MULTIBOOT_BOOTLOADER_MAGIC){
|
|
|
|
printf("Invalid magic number: 0x%x\n", magic);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a little banner for cuteness
|
|
|
|
*/
|
|
|
|
printf("|=== BarinkOS ===|\n");
|
2022-08-18 23:05:10 +00:00
|
|
|
const uint32_t KERNEL_BASE_ADDR = 0xC0000000;
|
2022-02-26 19:44:16 +00:00
|
|
|
/**
|
|
|
|
* Use the address given as an argument as the pointer
|
|
|
|
* to a Multiboot information structure.
|
|
|
|
*/
|
2022-08-18 23:05:10 +00:00
|
|
|
multiboot_info_t* mbt = (multiboot_info_t*) (addr + KERNEL_BASE_ADDR);
|
2022-02-26 19:44:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct our own bootInfo structure
|
|
|
|
*/
|
|
|
|
BootInfo bootinfo = {};
|
|
|
|
|
2021-12-20 20:53:57 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
/*
|
|
|
|
If we got a memory map from our bootloader we
|
|
|
|
should be parsing it to find out the memory regions available.
|
|
|
|
*/
|
|
|
|
if (CHECK_FLAG(mbt->flags, 6))
|
|
|
|
{
|
2021-07-22 21:14:58 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
/*
|
|
|
|
Setup Physical memory managment
|
|
|
|
*/
|
|
|
|
MemoryInfo meminfo = {};
|
|
|
|
bootinfo.memory = &meminfo;
|
|
|
|
|
|
|
|
mapMultibootMemoryMap(bootinfo.memory , mbt);
|
|
|
|
printf("Memory size: 0x%x bytes\n", bootinfo.memory->TotalMemory );
|
2021-12-28 18:47:32 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
PhysicalMemory memAlloc = PhysicalMemory{};
|
|
|
|
memAlloc.setup(bootinfo.memory );
|
2021-12-28 18:47:32 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
/*
|
|
|
|
Mark already in use sections
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Mark kernel memory as used
|
|
|
|
printf("Kernel Begin Pointer: 0x%x, Kernel end pointer: 0x%x\n", kernel_begin , kernel_end );
|
2021-11-02 20:03:11 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) mbt->mmap_addr;
|
2021-05-12 22:03:00 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
for (; (unsigned long) mmap < mbt->mmap_addr + mbt->mmap_length; mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size))){
|
2021-12-20 20:53:57 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
if ( mmap->type == MULTIBOOT_MEMORY_AVAILABLE){
|
|
|
|
|
|
|
|
} else{
|
|
|
|
printf("allocate region: 0x%x, size : 0x%x bytes\n", (unsigned) mmap->addr,(unsigned) mmap->len );
|
|
|
|
memAlloc.allocate_region((unsigned)mmap->addr , (unsigned)mmap->len);
|
2021-12-28 18:47:32 +00:00
|
|
|
}
|
2022-02-26 19:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printf("allocate region: 0x%x, size : 0x%x bytes\n", kernel_begin, kernel_end - kernel_begin );
|
|
|
|
memAlloc.allocate_region(kernel_end, kernel_end - kernel_begin);
|
|
|
|
|
|
|
|
// test alloc_block
|
2022-08-15 17:51:22 +00:00
|
|
|
/*
|
2022-02-26 19:44:16 +00:00
|
|
|
uint8_t* memory = (uint8_t*) memAlloc.allocate_block();
|
|
|
|
printf("Got a new pointer: 0x%x\n", memory);
|
2021-12-28 18:47:32 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
uint8_t* memory2 = (uint8_t*) memAlloc.allocate_block();
|
|
|
|
printf("Got a new pointer: 0x%x\n", memory2);
|
2021-12-28 18:47:32 +00:00
|
|
|
|
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
memAlloc.free_block((void*) memory);
|
2021-12-28 18:47:32 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
uint8_t* newBlockPlse = (uint8_t*) memAlloc.allocate_block();
|
2022-08-15 17:51:22 +00:00
|
|
|
*/
|
2022-02-26 19:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-08-17 12:17:58 +00:00
|
|
|
//memAlloc.free_block((void*) memory);
|
|
|
|
//InitializePaging();
|
2022-08-17 23:26:49 +00:00
|
|
|
//IdentityMap();
|
|
|
|
//Enable();
|
2022-08-18 22:44:52 +00:00
|
|
|
} else{
|
|
|
|
printf("memory flag not set!");
|
2022-02-26 19:44:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 22:44:52 +00:00
|
|
|
CheckMBT( (multiboot_info_t *) addr);
|
2021-12-28 18:47:32 +00:00
|
|
|
|
2022-02-26 19:44:16 +00:00
|
|
|
kernel_main(&bootinfo);
|
|
|
|
|
|
|
|
}
|
2021-11-03 19:03:38 +00:00
|
|
|
|