From e70f56a0053c7429fed3a1b16155e91e54921efe Mon Sep 17 00:00:00 2001 From: Nigel Date: Sun, 21 Aug 2022 21:15:15 +0200 Subject: [PATCH] Improving the memory mapping boot code Removed the need to map the extra MBI structure in as a seperate pagetable Renaming / Restructuring the memory folder --- Makefile | 7 ++-- screenshots/must frustrating bug ever.png | 3 ++ src/grub.cfg | 5 --- src/kernel/KernelLauncher/boot.s | 13 ++----- src/kernel/KernelLauncher/bootcheck.h | 2 +- src/kernel/KernelLauncher/launcher.cpp | 28 +++++++++++++++ src/kernel/Memory/KernelHeap.h | 2 ++ .../{memory.cpp => PhysicalMemoryManager.cpp} | 8 +++-- .../{memory.h => PhysicalMemoryManager.h} | 4 ++- .../{paging.cpp => VirtualMemoryManager.cpp} | 36 +++++++++---------- .../{paging.h => VirtualMemoryManager.h} | 0 src/kernel/Terminal/kterm.h | 1 - src/kernel/kernel.cpp | 12 ++++--- src/kernel/linker.ld | 1 + 14 files changed, 75 insertions(+), 47 deletions(-) create mode 100644 screenshots/must frustrating bug ever.png create mode 100644 src/kernel/KernelLauncher/launcher.cpp create mode 100644 src/kernel/Memory/KernelHeap.h rename src/kernel/Memory/{memory.cpp => PhysicalMemoryManager.cpp} (92%) rename src/kernel/Memory/{memory.h => PhysicalMemoryManager.h} (91%) rename src/kernel/Memory/{paging.cpp => VirtualMemoryManager.cpp} (75%) rename src/kernel/Memory/{paging.h => VirtualMemoryManager.h} (100%) diff --git a/Makefile b/Makefile index 8b655ac..95f137b 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CC = ${HOME}/opt/cross/bin/i686-elf-gcc CPP = ${HOME}/opt/cross/bin/i686-elf-g++ CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra -OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o +OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o $(BUILD_DIR)/launcher.o SRC_DIR = src BUILD_DIR = build @@ -102,4 +102,7 @@ $(BUILD_DIR)/memory.o: $(CPP) -c $(SRC_DIR)/kernel/Memory/memory.cpp -o $(BUILD_DIR)/memory.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/paging.o: - $(CPP) -c $(SRC_DIR)/kernel/Memory/paging.cpp -o $(BUILD_DIR)/paging.o $(CFLAGS) -fno-exceptions -fno-rtti \ No newline at end of file + $(CPP) -c $(SRC_DIR)/kernel/Memory/paging.cpp -o $(BUILD_DIR)/paging.o $(CFLAGS) -fno-exceptions -fno-rtti + +$(BUILD_DIR)/launcher.o: + $(CPP) -c $(SRC_DIR)/kernel/KernelLauncher/launcher.cpp -o $(BUILD_DIR)/launcher.o $(CFLAGS) -fno-exceptions -fno-rtti \ No newline at end of file diff --git a/screenshots/must frustrating bug ever.png b/screenshots/must frustrating bug ever.png new file mode 100644 index 0000000..5b908d8 --- /dev/null +++ b/screenshots/must frustrating bug ever.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a55e6e0d5beb8d3d390c6877c9eabc7f8e7aa539d124e24f433ae4eda4cf4c21 +size 10397 diff --git a/src/grub.cfg b/src/grub.cfg index 27391d2..df39e08 100644 --- a/src/grub.cfg +++ b/src/grub.cfg @@ -1,8 +1,3 @@ -GRUB_DEFAULT=0 -GRUB_TIMEOUT=-1 -GRUB_HIDDEN_TIMEOUT=0 -GRUB_HIDDEN_TIMEOUT_QUITE=true - menuentry "BarinkOS" { multiboot /boot/myos.bin } diff --git a/src/kernel/KernelLauncher/boot.s b/src/kernel/KernelLauncher/boot.s index b627cfa..140630c 100644 --- a/src/kernel/KernelLauncher/boot.s +++ b/src/kernel/KernelLauncher/boot.s @@ -47,12 +47,7 @@ _start: movl $(boot_page_table - 0xC0000000), %edi # Map address 0 movl $0, %esi - # Map 1023 pages the 1024th being the VGA text buffer - movl $1023, %ecx - -1: # Map the kernel - cmpl $_kernel_start, %esi - jl 2f +1: cmpl $(_kernel_end - 0xC0000000), %esi jge 3f @@ -125,12 +120,8 @@ isPaging: call early_main - mov %cr0, %eax - or $1, %eax - mov %eax, %cr0 - - //call kernel_main + cli diff --git a/src/kernel/KernelLauncher/bootcheck.h b/src/kernel/KernelLauncher/bootcheck.h index 1a11c05..50335d2 100644 --- a/src/kernel/KernelLauncher/bootcheck.h +++ b/src/kernel/KernelLauncher/bootcheck.h @@ -1,7 +1,7 @@ #pragma once #include "../multiboot.h" #define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit))) - +#define __VERBOSE__ #include "../Terminal/kterm.h" diff --git a/src/kernel/KernelLauncher/launcher.cpp b/src/kernel/KernelLauncher/launcher.cpp new file mode 100644 index 0000000..471053c --- /dev/null +++ b/src/kernel/KernelLauncher/launcher.cpp @@ -0,0 +1,28 @@ +#include +#include + +void put_char(char ch, size_t x, size_t y ){ + *((uint16_t*)0xb8000+(y * 80 + x)) = ch | ((7 | 0 << 4) << 8); +} + +void write_ln(char* s, size_t length, size_t x , size_t y) +{ + // Because read only data is linked at a virtual address we'll need to convert + // the string adddres from virtual to phys. + s = s - 0xC0000000; + size_t column , row; + column = x; + row = y; + for(int i = 0; i < length ; i ++) + { + + put_char(s[i] , column,row ); + column ++; + + } + +} + +extern "C" void testLauncher () { + write_ln("hello", 5 ,0,0); +} \ No newline at end of file diff --git a/src/kernel/Memory/KernelHeap.h b/src/kernel/Memory/KernelHeap.h new file mode 100644 index 0000000..84a1624 --- /dev/null +++ b/src/kernel/Memory/KernelHeap.h @@ -0,0 +1,2 @@ +#pragma once + diff --git a/src/kernel/Memory/memory.cpp b/src/kernel/Memory/PhysicalMemoryManager.cpp similarity index 92% rename from src/kernel/Memory/memory.cpp rename to src/kernel/Memory/PhysicalMemoryManager.cpp index 3106c7c..a24ff3d 100644 --- a/src/kernel/Memory/memory.cpp +++ b/src/kernel/Memory/PhysicalMemoryManager.cpp @@ -102,11 +102,13 @@ void PhysicalMemory::deallocate_region(uint32_t StartAddress , uint32_t size ) void mapMultibootMemoryMap( MemoryInfo* memInfo , multiboot_info_t *mbt) { - printf("mmap_addr = 0x%x, mmap_length = 0x%x\n", - (unsigned) mbt->mmap_addr, (unsigned) mbt->mmap_length); + + printf("mmap_addr = 0x%x, mmap_length = 0x%x\n", (unsigned) mbt->mmap_addr , (unsigned) mbt->mmap_length ); + + multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) mbt->mmap_addr; - for (; (unsigned long) mmap < mbt->mmap_addr + mbt->mmap_length; mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size))){ + for (; (unsigned long) mmap < mbt->mmap_addr + mbt->mmap_length; mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size))){ if ( mmap->type == MULTIBOOT_MEMORY_AVAILABLE){ diff --git a/src/kernel/Memory/memory.h b/src/kernel/Memory/PhysicalMemoryManager.h similarity index 91% rename from src/kernel/Memory/memory.h rename to src/kernel/Memory/PhysicalMemoryManager.h index 7b04151..cef690c 100644 --- a/src/kernel/Memory/memory.h +++ b/src/kernel/Memory/PhysicalMemoryManager.h @@ -8,8 +8,10 @@ #include "../Lib/mem.h" #include "../bitmap.h" +// Asumming 32 bit x86 for now! #define BLOCK_SIZE 4092 -#define BLOCKS_PER_WORD 32 // A word is 16 bit in x86 machines according to my google search results! +#define WORD_SIZE 2 +#define BLOCKS_PER_WORD 32 #define KB_TO_BLOCKS(x) (x / BLOCK_SIZE) #define IS_ALIGNED(addr, align) !((addr) & ~((align) - 1)) diff --git a/src/kernel/Memory/paging.cpp b/src/kernel/Memory/VirtualMemoryManager.cpp similarity index 75% rename from src/kernel/Memory/paging.cpp rename to src/kernel/Memory/VirtualMemoryManager.cpp index 41274f1..45f0344 100644 --- a/src/kernel/Memory/paging.cpp +++ b/src/kernel/Memory/VirtualMemoryManager.cpp @@ -1,7 +1,6 @@ #include "paging.h" -/*PageDirectoryEntry kernel_directory[MAX_DIRECTORY_ENTRIES]__attribute__((aligned(4096))); - -//PageTableEntry first_page_table[MAX_PAGE_TABLE_ENTRIES]__attribute__((aligned(4096))); +// PageDirectoryEntry kernel_directory[MAX_DIRECTORY_ENTRIES]__attribute__((aligned(4096))); +// PageTableEntry first_page_table[MAX_PAGE_TABLE_ENTRIES]__attribute__((aligned(4096))); void IdentityMap (){ printf("\nInit paging\n"); @@ -11,7 +10,7 @@ void IdentityMap (){ int i = 0; while ( i < 1024) { - kernel_directory[i] = 0x2; + // kernel_directory[i] = 0x2; i++; } @@ -20,7 +19,7 @@ void IdentityMap (){ unsigned int j ; for( j = 0; j < 1024; j++ ) { - first_page_table[j] = (j * 0x1000) | 3 ; + // first_page_table[j] = (j * 0x1000) | 3 ; //Attributes: //Supervisor Level , @@ -32,7 +31,7 @@ void IdentityMap (){ // Put the page table in the page directory // attributes: supervisor level, read/write, present; - kernel_directory[0] = ((unsigned int)first_page_table) | 3; + // kernel_directory[0] = ((unsigned int)first_page_table) | 3; printf("Init paging DONE\n"); } @@ -43,10 +42,10 @@ 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; + // kernel_directory[i] = 0x2; } // BIOS Address Identity mapping @@ -58,20 +57,20 @@ 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++) { // setup a page table - PageTableEntry pagetable[MAX_PAGE_TABLE_ENTRIES] = PhysicalMemory::allocate_block(); // TODO :: Physical memory manager functions should be available here. + // PageTableEntry pagetable[MAX_PAGE_TABLE_ENTRIES] = PhysicalMemory::allocate_block(); // TODO :: Physical memory manager functions should be available here. for(int j = 0; j < MAX_PAGE_TABLE_ENTRIES; j++) { - pagetable[j] = ( j * 4096 ) | 3; + // pagetable[j] = ( j * 4096 ) | 3; } // 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 @@ -87,11 +86,11 @@ void InitializePaging() 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(); for(int j = 0; j < MAX_PAGE_TABLE_ENTRIES; j++) { - pageTable[j] = ( j * 4096) | 3; // NOTE: Check if page is actually supposed to be present + // pageTable[j] = ( j * 4096) | 3; // NOTE: Check if page is actually supposed to be present } // TODO: Calculate Page Directory index @@ -145,9 +144,9 @@ void Enable() CR0 = GetCR0(); printf("PG bit = %d \n" , GET_PG_BIT(CR0)); - printf("Load into CR3 address: 0x%x\n", (uint32_t)(&kernel_directory[0])); - loadPageDirectory(&kernel_directory[0]); - enablePaging(); + // printf("Load into CR3 address: 0x%x\n", (uint32_t)(&kernel_directory[0])); + // loadPageDirectory(&kernel_directory[0]); + // enablePaging(); printf("Paging enabled!\n"); @@ -170,4 +169,3 @@ void Enable() } } -*/ \ No newline at end of file diff --git a/src/kernel/Memory/paging.h b/src/kernel/Memory/VirtualMemoryManager.h similarity index 100% rename from src/kernel/Memory/paging.h rename to src/kernel/Memory/VirtualMemoryManager.h diff --git a/src/kernel/Terminal/kterm.h b/src/kernel/Terminal/kterm.h index b260b73..915c08a 100644 --- a/src/kernel/Terminal/kterm.h +++ b/src/kernel/Terminal/kterm.h @@ -1,7 +1,6 @@ #pragma once #include #include -#include #include #include "../Drivers/VGA/colors.h" diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index bbab188..e120dab 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -7,6 +7,9 @@ extern "C" uint32_t multiboot_page_table; const uint32_t KERNEL_BASE_ADDR = 0xC0000000; extern "C" void early_main(unsigned long magic, unsigned long addr){ + + // Convert MBI address to higher quarter kernel space + addr += KERNEL_BASE_ADDR; /** * Initialize terminal interface * NOTE: This should be done later on , the magic value should be checked first. @@ -20,7 +23,6 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){ // Enable interrupts asm volatile("STI"); - map_multiboot_info_structure(addr); printf("DEBUG:\n Magic: 0x%x\n MBT_addr: 0x%x\n", magic, addr); /** @@ -78,7 +80,7 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){ 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 + KERNEL_BASE_ADDR) ; for (; (unsigned long) mmap < mbt->mmap_addr + mbt->mmap_length; mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size))){ @@ -120,8 +122,10 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){ printf("memory flag not set!"); } - CheckMBT( (multiboot_info_t *) addr); - + CheckMBT( (multiboot_info_t *) addr); + asm volatile("mov %cr0, %eax "); + asm volatile("or $1, %eax"); + asm volatile("mov %eax, %cr0"); kernel_main(&bootinfo); } diff --git a/src/kernel/linker.ld b/src/kernel/linker.ld index bcb2584..b564172 100644 --- a/src/kernel/linker.ld +++ b/src/kernel/linker.ld @@ -16,6 +16,7 @@ SECTIONS .multiboot.text : { *(multiboot.text) + *launcher.o(.text) } . += 0xC0000000; /* Addresses in the following code need to be above the 3Gb mark */