From 59ba41f3d2d04d793893e8703f8dfaac42ec4f26 Mon Sep 17 00:00:00 2001 From: Nigel Date: Tue, 23 Aug 2022 21:35:19 +0200 Subject: [PATCH] Multiboot Memory Map get copied to a "safe" place --- source/kernel/Boot/Multiboot.S | 15 ++++++ source/kernel/Boot/boot.s | 22 +------- source/kernel/PreKernel/bootstructure.h | 21 +++++++- source/kernel/PreKernel/prekernel.cpp | 64 +++++++++++++++------- source/kernel/kernel.cpp | 72 +++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 43 deletions(-) create mode 100644 source/kernel/Boot/Multiboot.S diff --git a/source/kernel/Boot/Multiboot.S b/source/kernel/Boot/Multiboot.S new file mode 100644 index 0000000..041f103 --- /dev/null +++ b/source/kernel/Boot/Multiboot.S @@ -0,0 +1,15 @@ +/* + * Multiboot + */ +.set ALIGN, 1<<0 /* align loaded modules on page boundaries */ +.set MEMINFO, 1<<1 /* provide memory map */ +.set FLAGS, ALIGN | MEMINFO /* this is the Multiboot 'flag' field */ +.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */ +.set CHECKSUM, -(MAGIC + FLAGS) /* checksum of above, to prove we are multiboot */ + +.section .multiboot.data, "aw" +.align 4 +.long MAGIC +.long FLAGS +.long CHECKSUM + \ No newline at end of file diff --git a/source/kernel/Boot/boot.s b/source/kernel/Boot/boot.s index fb98575..b32fdaa 100644 --- a/source/kernel/Boot/boot.s +++ b/source/kernel/Boot/boot.s @@ -1,18 +1,4 @@ -/* - * Multiboot - */ -.set ALIGN, 1<<0 /* align loaded modules on page boundaries */ -.set MEMINFO, 1<<1 /* provide memory map */ -.set FLAGS, ALIGN | MEMINFO /* this is the Multiboot 'flag' field */ -.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */ -.set CHECKSUM, -(MAGIC + FLAGS) /* checksum of above, to prove we are multiboot */ - -.section .multiboot.data, "aw" -.align 4 -.long MAGIC -.long FLAGS -.long CHECKSUM - +.include "./source/kernel/Boot/Multiboot.S" /* * Allocate initial stack */ @@ -120,14 +106,8 @@ isPaging: pushl $0 popf - - call early_main - - - - cli 1: hlt jmp 1b diff --git a/source/kernel/PreKernel/bootstructure.h b/source/kernel/PreKernel/bootstructure.h index 6d87ab1..70f9abf 100644 --- a/source/kernel/PreKernel/bootstructure.h +++ b/source/kernel/PreKernel/bootstructure.h @@ -5,6 +5,24 @@ extern "C" const uint32_t kernel_begin; extern "C" const uint32_t kernel_end; +// Put the BootInfoBlock 1MB above the kernel. +const uint32_t BootInfoBlock_pptr = (uint32_t)&kernel_end - 0xC0000000 + 0x1; +const uint32_t MemoryMapHeap_pptr = BootInfoBlock_pptr + 0x1; + + +#define IS_AVAILABLE_MEM(MEM_TYPE) MEM_TYPE & 0x1 +#define IS_ACPI_MEM(MEM_TYPE) MEM_TYPE & 0x2 +#define IS_RESERVED_MEM(MEM_TYPE) MEM_TYPE & 0x3 +#define IS_NVS_MEMORY(MEM_TYPE) MEM_TYPE & 0x8 +#define IS_BADRAM_MEMORY(MEM_TYPE) MEM_TYPE & 0x10 + +struct MemoryInfoBlock { + uint32_t Base_addr ; + uint32_t Memory_Size; + MemoryInfoBlock* next; + uint8_t type; + +}; struct BootInfoBlock { bool MapIsInvalid; uint32_t bootDeviceID ; @@ -21,8 +39,7 @@ struct BootInfoBlock { bool EnabledVBE; bool PhysicalMemoryMapAvailable; + MemoryInfoBlock* MemoryMap; }; -// Put the BootInfoBlock 1MB above the kernel. -const uint32_t BootInfoBlock_pptr = kernel_end - 0xC0000000 + 0x1000; diff --git a/source/kernel/PreKernel/prekernel.cpp b/source/kernel/PreKernel/prekernel.cpp index 6b910a2..48ab238 100644 --- a/source/kernel/PreKernel/prekernel.cpp +++ b/source/kernel/PreKernel/prekernel.cpp @@ -25,6 +25,8 @@ extern "C" void testLauncher ( unsigned long magic, multiboot_info_t* mbi) { if (CHECK_FLAG (mbi->flags, 1)) { BIB->bootDeviceID = mbi->boot_device; + } else{ + BIB->bootDeviceID = 0x11111111; } /* Are mods_* valid? */ @@ -36,7 +38,7 @@ extern "C" void testLauncher ( unsigned long magic, multiboot_info_t* mbi) { for(i = 0, mod = (multiboot_module_t *) mbi->mods_addr; i < mbi->mods_count; i++ , mod++){ - + } } @@ -60,29 +62,51 @@ extern "C" void testLauncher ( unsigned long magic, multiboot_info_t* mbi) { BIB->ValidELFHeader = false; } /* - If we got a memory map from our bootloader we - should be parsing it to find out the memory regions available. - */ - if (CHECK_FLAG(mbi->flags, 6)) - { +If we got a memory map from our bootloader we +should be parsing it to find out the memory regions available. +*/ +if (CHECK_FLAG(mbi->flags, 6)) +{ BIB->PhysicalMemoryMapAvailable = true; + BIB->MemoryMap = (MemoryInfoBlock*) MemoryMapHeap_pptr; multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) (mbi->mmap_addr) ; - - for (; (unsigned long) mmap < mbi->mmap_addr + mbi->mmap_length; mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size))){ - - if ( mmap->type == MULTIBOOT_MEMORY_AVAILABLE){ - - } else{ - - - } - - - } + auto MemoryMapEnd = mbi->mmap_addr + mbi->mmap_length; - } else{ + auto CurrentInfoBlock = BIB->MemoryMap; + + + while((unsigned long) mmap < MemoryMapEnd){ + + CurrentInfoBlock->Base_addr = mmap->addr; + CurrentInfoBlock->Memory_Size = mmap->len; + + + if(mmap->type == MULTIBOOT_MEMORY_AVAILABLE) + CurrentInfoBlock->type &= 0x1; + if(mmap->type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) + CurrentInfoBlock->type &= 0x2; + if(mmap->type == MULTIBOOT_MEMORY_RESERVED) + CurrentInfoBlock->type &= 0x4; + if(mmap->type == MULTIBOOT_MEMORY_NVS) + CurrentInfoBlock->type &= 0x8; + if(mmap->type == MULTIBOOT_MEMORY_BADRAM) + CurrentInfoBlock->type &= 0x10; + + + // continue to the next block + mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size)); + + CurrentInfoBlock->next = (MemoryInfoBlock*) ((uint32_t)CurrentInfoBlock) + sizeof(MemoryInfoBlock); + CurrentInfoBlock = CurrentInfoBlock->next; + + } + + CurrentInfoBlock->next = (MemoryInfoBlock*) 0x0; + +} else +{ BIB->PhysicalMemoryMapAvailable = false; - } +} /* Draw diagonal blue line */ if (CHECK_FLAG (mbi->flags, 12)){ diff --git a/source/kernel/kernel.cpp b/source/kernel/kernel.cpp index 0b232a9..c9b8aff 100644 --- a/source/kernel/kernel.cpp +++ b/source/kernel/kernel.cpp @@ -19,6 +19,22 @@ extern "C" void early_main() * Show a little banner for cuteness */ printf("|=== BarinkOS ===|\n"); + printf("Kernel End Addr: 0x%x\n" , &kernel_end + KERNEL_BASE_ADDR); + + uint32_t PageDirectoryEntryIndex = ((uint32_t)&kernel_end + KERNEL_BASE_ADDR ) >> 2 ; + + 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 ); BootInfoBlock* BootInfo = (BootInfoBlock*) ( BootInfoBlock_pptr + 0xC0000000 ); @@ -41,6 +57,62 @@ extern "C" void early_main() if(BootInfo->PhysicalMemoryMapAvailable) { printf("- Physical Memory Map available!\n"); + + // Print the memory regions + MemoryInfoBlock* currentBlock = (MemoryInfoBlock*) ((uint32_t)BootInfo->MemoryMap + KERNEL_BASE_ADDR) ; + + kterm_setcolor(VGA_COLOR_RED); + printf("size of MemoryInfoBlock: 0x%x\n", sizeof(MemoryInfoBlock)); + kterm_setcolor(VGA_COLOR_CYAN); + printf("Kernel End is at address: 0x%x\n", &kernel_end); + printf("BootInfo is at address: 0x%x\n", BootInfo); + printf("map is at address: 0x%x\n", currentBlock + KERNEL_BASE_ADDR); + kterm_setcolor(VGA_COLOR_WHITE); + while( (uint32_t)currentBlock->next != 0x0 ) + { + kterm_setcolor(VGA_COLOR_CYAN); + printf("map is at address: 0x%x\n", ( (uint32_t)currentBlock )); + kterm_setcolor(VGA_COLOR_WHITE); + + /* + uint32_t pageDirectoryIndex = ((uint32_t)¤tBlock ) >> 22; + printf("pageDirectoryIndex: %d\n", pageDirectoryIndex); + + uint32_t pageTableIndex = ((uint32_t)¤tBlock >> 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); + printf("Memory Region: \n"); + + if(IS_AVAILABLE_MEM(currentBlock->type)){ + //printf("AVAILABLE RAM\n"); + } + else if(IS_ACPI_MEM(currentBlock->type)){ + printf("ACPI MEMORY\n"); + } + else if(IS_RESERVED_MEM(currentBlock->type)){ + printf("RESERVED MEMORY\n"); + } + else if(IS_NVS_MEMORY(currentBlock->type)){ + printf("NVS MEMORY \n"); + } + else if(IS_BADRAM_MEMORY(currentBlock->type)){ + printf("BADRAM MEMORY \n"); + } + else { + // printf("(TYPE 0x%x )TYPE NOT SPECIFIED\n", currentBlock->type); + } + + // printf("Base address: 0x%x, Memory size: 0x%x\n", currentBlock->Base_addr, currentBlock->Memory_Size); + + currentBlock = (MemoryInfoBlock*) ((uint32_t)currentBlock->next + KERNEL_BASE_ADDR ); + + } + + + } asm volatile("mov %cr0, %eax ");