Merge into main the new state of the operating system/kernel #1

Open
Nigel wants to merge 120 commits from dev into main
3 changed files with 66 additions and 50 deletions
Showing only changes of commit 560dd64e64 - Show all commits

View File

@ -28,10 +28,14 @@ stack_top:
*/ */
.section .bss, "aw", @nobits .section .bss, "aw", @nobits
.align 4096 .align 4096
.globl boot_page_directory
boot_page_directory: boot_page_directory:
.skip 4096 .skip 4096
boot_page_table: boot_page_table:
.skip 4096 .skip 4096
.globl multiboot_page_table
multiboot_page_table:
.skip 4096
# More page tables may be required # More page tables may be required
# Entry point # Entry point
@ -98,9 +102,9 @@ _start:
4: 4:
# At this point, paging is fully set up and enabled # At this point, paging is fully set up and enabled
isPaging: isPaging:
# Unmap the identity mapping as it is now unnecessary # Unmap the identity mapping as it is now unnecessary
movl $0, boot_page_directory + 0 movl $0, boot_page_directory + 0
# Reload cr3 to force tlb flush # Reload cr3 to force tlb flush
movl %cr3, %ecx movl %cr3, %ecx
movl %ecx, %cr3 movl %ecx, %cr3

View File

@ -1,7 +1,7 @@
#include "paging.h" #include "paging.h"
PageDirectoryEntry kernel_directory[MAX_DIRECTORY_ENTRIES]__attribute__((aligned(4096))); /*PageDirectoryEntry kernel_directory[MAX_DIRECTORY_ENTRIES]__attribute__((aligned(4096)));
PageTableEntry first_page_table[MAX_PAGE_TABLE_ENTRIES]__attribute__((aligned(4096))); //PageTableEntry first_page_table[MAX_PAGE_TABLE_ENTRIES]__attribute__((aligned(4096)));
void IdentityMap (){ void IdentityMap (){
printf("\nInit paging\n"); printf("\nInit paging\n");
@ -43,6 +43,7 @@ void InitializePaging()
Initial kernel page directory Initial kernel page directory
set all page tables to not present set all page tables to not present
*/ */
/*
for (int i = 0; i < MAX_DIRECTORY_ENTRIES; i++) for (int i = 0; i < MAX_DIRECTORY_ENTRIES; i++)
{ {
kernel_directory[i] = 0x2; kernel_directory[i] = 0x2;
@ -57,6 +58,7 @@ void InitializePaging()
uint8_t NUM_PDE = BIOSAddr_Max / (4 * 1024 * 1024); uint8_t NUM_PDE = BIOSAddr_Max / (4 * 1024 * 1024);
printf("The first 8MiB require %d Page Directory Entries\n", NUM_PDE); printf("The first 8MiB require %d Page Directory Entries\n", NUM_PDE);
*/
/* /*
for( int i = 0; i < NUM_PDE; i++) for( int i = 0; i < NUM_PDE; i++)
{ {
@ -71,7 +73,7 @@ void InitializePaging()
// add page table as page directory entry // add page table as page directory entry
kernel_directory[i] = ( (unsigned int) pagetable ) | 3; kernel_directory[i] = ( (unsigned int) pagetable ) | 3;
} }
*/
// map the kernel space // map the kernel space
VIRTUAL_ADDRESS Vaddr = KERNEL_VRT_MEMORY_BEGIN; VIRTUAL_ADDRESS Vaddr = KERNEL_VRT_MEMORY_BEGIN;
PHYSICAL_ADDRESS KernelAddr = kernel_begin; PHYSICAL_ADDRESS KernelAddr = kernel_begin;
@ -82,7 +84,7 @@ void InitializePaging()
NUM_PDE = KernelSizeInBytes / (4 * 1024* 1024); NUM_PDE = KernelSizeInBytes / (4 * 1024* 1024);
printf("Kernel requires %d Page Directory Entries\n", NUM_PDE); printf("Kernel requires %d Page Directory Entries\n", NUM_PDE);
/*
for(int i = 0; i < NUM_PDE; i++) for(int i = 0; i < NUM_PDE; i++)
{ {
PageTableEntry pageTable [MAX_PAGE_TABLE_ENTRIES] = PhysicalMemory::allocate_block(); PageTableEntry pageTable [MAX_PAGE_TABLE_ENTRIES] = PhysicalMemory::allocate_block();
@ -97,7 +99,7 @@ void InitializePaging()
} }
*/
@ -168,3 +170,4 @@ void Enable()
} }
} }
*/

View File

@ -1,13 +1,11 @@
#include "kernel.h" #include "kernel.h"
void map_multiboot_info_structure(unsigned long addr);
extern "C" void kernel_main (BootInfo* bootinfo);
extern "C" uint32_t boot_page_directory;
extern "C" uint32_t multiboot_page_table;
extern "C" void kernel_main (BootInfo* bootinfo) { const uint32_t KERNEL_BASE_ADDR = 0xC0000000;
pit_initialise();
startSuperVisorTerminal(bootinfo);
}
extern "C" void early_main(unsigned long magic, unsigned long addr){ extern "C" void early_main(unsigned long magic, unsigned long addr){
/** /**
* Initialize terminal interface * Initialize terminal interface
@ -21,34 +19,9 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){
init_idt(); init_idt();
// Enable interrupts // Enable interrupts
asm volatile("STI"); 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" );
*/
map_multiboot_info_structure(addr);
printf("DEBUG:\n Magic: 0x%x\n MBT_addr: 0x%x\n", magic, addr); printf("DEBUG:\n Magic: 0x%x\n MBT_addr: 0x%x\n", magic, addr);
/** /**
* Check Multiboot magic number * Check Multiboot magic number
@ -63,12 +36,11 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){
* Show a little banner for cuteness * Show a little banner for cuteness
*/ */
printf("|=== BarinkOS ===|\n"); printf("|=== BarinkOS ===|\n");
const uint32_t KERNEL_BASE_ADDR = 0xC0000000;
/** /**
* Use the address given as an argument as the pointer * Use the address given as an argument as the pointer
* to a Multiboot information structure. * to a Multiboot information structure.
*/ */
multiboot_info_t* mbt = (multiboot_info_t*) (addr + KERNEL_BASE_ADDR); multiboot_info_t* mbt = (multiboot_info_t*) (addr );
/** /**
* Construct our own bootInfo structure * Construct our own bootInfo structure
@ -79,10 +51,10 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){
/* /*
If we got a memory map from our bootloader we If we got a memory map from our bootloader we
should be parsing it to find out the memory regions available. should be parsing it to find out the memory regions available.
*/ */
if (CHECK_FLAG(mbt->flags, 6)) if (CHECK_FLAG(mbt->flags, 6))
{ {
/* /*
Setup Physical memory managment Setup Physical memory managment
*/ */
@ -92,15 +64,19 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){
mapMultibootMemoryMap(bootinfo.memory , mbt); mapMultibootMemoryMap(bootinfo.memory , mbt);
printf("Memory size: 0x%x bytes\n", bootinfo.memory->TotalMemory ); printf("Memory size: 0x%x bytes\n", bootinfo.memory->TotalMemory );
/*
PhysicalMemory memAlloc = PhysicalMemory{}; PhysicalMemory memAlloc = PhysicalMemory{};
memAlloc.setup(bootinfo.memory ); memAlloc.setup(bootinfo.memory );
*/
// TODO: FIX physical allocator
/* /*
Mark already in use sections Mark already in use sections
*/ */
// Mark kernel memory as used // Mark kernel memory as used
printf("Kernel Begin Pointer: 0x%x, Kernel end pointer: 0x%x\n", kernel_begin , kernel_end ); printf("Kernel Begin Pointer: 0x%x, Kernel end pointer: 0x%x\n", &kernel_begin , &kernel_end );
multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) mbt->mmap_addr; multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) mbt->mmap_addr;
@ -110,16 +86,16 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){
} else{ } else{
printf("allocate region: 0x%x, size : 0x%x bytes\n", (unsigned) mmap->addr,(unsigned) mmap->len ); 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); // memAlloc.allocate_region((unsigned)mmap->addr , (unsigned)mmap->len);
} }
} }
printf("allocate region: 0x%x, size : 0x%x bytes\n", kernel_begin, kernel_end - kernel_begin ); 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); //memAlloc.allocate_region(kernel_end, kernel_end - kernel_begin);
// test alloc_block // test alloc_block
/* /*
uint8_t* memory = (uint8_t*) memAlloc.allocate_block(); uint8_t* memory = (uint8_t*) memAlloc.allocate_block();
@ -150,3 +126,36 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){
} }
void map_multiboot_info_structure(unsigned long addr){
// map the multiboot structure into virtual memory
// so we can gather the necessary data from it.
uint32_t pageDirectoryIndex = (addr ) >> 22;
printf("pageDirectoryIndex: %d\n", pageDirectoryIndex);
uint32_t pageTableIndex = (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", &multiboot_page_table);
uint32_t* current_page_directory = &boot_page_directory;
uint32_t* needed_page_table = &multiboot_page_table - KERNEL_BASE_ADDR;
// set the page tabel reference;
current_page_directory[pageDirectoryIndex] = (uint32_t)&multiboot_page_table - KERNEL_BASE_ADDR + 0x003;
// set the page reference;
needed_page_table[ pageTableIndex ] = addr | 0x003;
// Reload CR3 to force a flush
asm("movl %cr3, %ecx;" "movl %ecx, %cr3" );
}
extern "C" void kernel_main (BootInfo* bootinfo) {
pit_initialise();
startSuperVisorTerminal(bootinfo);
}