From 560dd64e64d54230254db7143381e3a7982dd447 Mon Sep 17 00:00:00 2001 From: Nigel Date: Fri, 19 Aug 2022 23:44:38 +0200 Subject: [PATCH] Kernel is working in its full former glory as a higher half kernel --- src/kernel/KernelLauncher/boot.s | 8 ++- src/kernel/Memory/paging.cpp | 13 +++-- src/kernel/kernel.cpp | 95 +++++++++++++++++--------------- 3 files changed, 66 insertions(+), 50 deletions(-) diff --git a/src/kernel/KernelLauncher/boot.s b/src/kernel/KernelLauncher/boot.s index 8b61af0..b627cfa 100644 --- a/src/kernel/KernelLauncher/boot.s +++ b/src/kernel/KernelLauncher/boot.s @@ -28,10 +28,14 @@ stack_top: */ .section .bss, "aw", @nobits .align 4096 +.globl boot_page_directory boot_page_directory: .skip 4096 boot_page_table: .skip 4096 +.globl multiboot_page_table +multiboot_page_table: + .skip 4096 # More page tables may be required # Entry point @@ -98,9 +102,9 @@ _start: 4: # At this point, paging is fully set up and enabled 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 - + # Reload cr3 to force tlb flush movl %cr3, %ecx movl %ecx, %cr3 diff --git a/src/kernel/Memory/paging.cpp b/src/kernel/Memory/paging.cpp index 67df499..41274f1 100644 --- a/src/kernel/Memory/paging.cpp +++ b/src/kernel/Memory/paging.cpp @@ -1,7 +1,7 @@ #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 (){ printf("\nInit paging\n"); @@ -43,6 +43,7 @@ void InitializePaging() Initial kernel page directory set all page tables to not present */ + /* for (int i = 0; i < MAX_DIRECTORY_ENTRIES; i++) { kernel_directory[i] = 0x2; @@ -57,6 +58,7 @@ void InitializePaging() uint8_t NUM_PDE = BIOSAddr_Max / (4 * 1024 * 1024); printf("The first 8MiB require %d Page Directory Entries\n", NUM_PDE); + */ /* for( int i = 0; i < NUM_PDE; i++) { @@ -71,7 +73,7 @@ void InitializePaging() // add page table as page directory entry kernel_directory[i] = ( (unsigned int) pagetable ) | 3; } -*/ + // map the kernel space VIRTUAL_ADDRESS Vaddr = KERNEL_VRT_MEMORY_BEGIN; PHYSICAL_ADDRESS KernelAddr = kernel_begin; @@ -82,7 +84,7 @@ void InitializePaging() NUM_PDE = KernelSizeInBytes / (4 * 1024* 1024); printf("Kernel requires %d Page Directory Entries\n", NUM_PDE); -/* + for(int i = 0; i < NUM_PDE; i++) { PageTableEntry pageTable [MAX_PAGE_TABLE_ENTRIES] = PhysicalMemory::allocate_block(); @@ -97,7 +99,7 @@ void InitializePaging() } - */ + @@ -168,3 +170,4 @@ void Enable() } } +*/ \ No newline at end of file diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 22aa47f..bbab188 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -1,13 +1,11 @@ #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) { - pit_initialise(); - - startSuperVisorTerminal(bootinfo); -} - - +const uint32_t KERNEL_BASE_ADDR = 0xC0000000; extern "C" void early_main(unsigned long magic, unsigned long addr){ /** * Initialize terminal interface @@ -21,34 +19,9 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){ 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" ); - */ + map_multiboot_info_structure(addr); + printf("DEBUG:\n Magic: 0x%x\n MBT_addr: 0x%x\n", magic, addr); /** * 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 */ printf("|=== BarinkOS ===|\n"); - const uint32_t KERNEL_BASE_ADDR = 0xC0000000; /** * Use the address given as an argument as the pointer * 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 @@ -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 should be parsing it to find out the memory regions available. - */ + */ if (CHECK_FLAG(mbt->flags, 6)) { - + /* Setup Physical memory managment */ @@ -92,15 +64,19 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){ mapMultibootMemoryMap(bootinfo.memory , mbt); printf("Memory size: 0x%x bytes\n", bootinfo.memory->TotalMemory ); + /* PhysicalMemory memAlloc = PhysicalMemory{}; memAlloc.setup(bootinfo.memory ); + */ + // TODO: FIX physical allocator /* 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 ); + 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; @@ -110,16 +86,16 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){ } 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); + // 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 ); - memAlloc.allocate_region(kernel_end, 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); + // test alloc_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); +} \ No newline at end of file