From 9893a0bd172fef2ae26ce92e2b0de3ad43aec517 Mon Sep 17 00:00:00 2001 From: Nigel Date: Thu, 1 Sep 2022 17:02:04 +0200 Subject: [PATCH] KERNEL: Cleanup Removing quite a few unnecessary parts. --- Makefile | 2 +- source/kernel/Interrupts/idt/idt.h | 4 +- source/kernel/Memory/MBIMMap/MBI_MMap.cpp | 44 ------------ source/kernel/Memory/MBIMMap/MBI_MMap.h | 18 ----- source/kernel/Memory/memoryinfo.h | 20 ------ source/kernel/Memory/paging.definitions.h | 56 --------------- source/kernel/Memory/paging.s | 44 ------------ .../SuperVisorTerminal/superVisorTerminal.cpp | 21 ++---- .../SuperVisorTerminal/superVisorTerminal.h | 3 +- source/kernel/{Boot => boot}/boot.s | 3 +- .../{Boot/Multiboot.S => boot/multiboot.s} | 0 source/kernel/bootinfo.h | 8 --- source/kernel/cpu.h | 3 +- source/kernel/kernel.cpp | 71 ++++--------------- source/kernel/kernel.h | 3 - 15 files changed, 26 insertions(+), 274 deletions(-) delete mode 100644 source/kernel/Memory/MBIMMap/MBI_MMap.cpp delete mode 100644 source/kernel/Memory/MBIMMap/MBI_MMap.h delete mode 100644 source/kernel/Memory/memoryinfo.h delete mode 100644 source/kernel/Memory/paging.definitions.h delete mode 100644 source/kernel/Memory/paging.s rename source/kernel/{Boot => boot}/boot.s (97%) rename source/kernel/{Boot/Multiboot.S => boot/multiboot.s} (100%) delete mode 100644 source/kernel/bootinfo.h diff --git a/Makefile b/Makefile index 2e1aff2..01da565 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,7 @@ $(BUILD_DIR)/kterm.o: $(CPP) -c $(SRC_DIR)/kernel/Terminal/kterm.cpp -o $(BUILD_DIR)/kterm.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/boot.o: - $(AS) $(SRC_DIR)/kernel/Boot/boot.s -o $(BUILD_DIR)/boot.o + $(AS) $(SRC_DIR)/kernel/boot/boot.s -o $(BUILD_DIR)/boot.o $(BUILD_DIR)/crti.o: $(AS) $(SRC_DIR)/kernel/crti.s -o $(BUILD_DIR)/crti.o diff --git a/source/kernel/Interrupts/idt/idt.h b/source/kernel/Interrupts/idt/idt.h index 8157fbe..6ca3399 100644 --- a/source/kernel/Interrupts/idt/idt.h +++ b/source/kernel/Interrupts/idt/idt.h @@ -1,7 +1,7 @@ #pragma once -#include "stdint.h" -#include "stddef.h" +#include +#include #include "../../Drivers/VGA/colors.h" #include "../../Drivers/PIC/pic.h" diff --git a/source/kernel/Memory/MBIMMap/MBI_MMap.cpp b/source/kernel/Memory/MBIMMap/MBI_MMap.cpp deleted file mode 100644 index 4dffba6..0000000 --- a/source/kernel/Memory/MBIMMap/MBI_MMap.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "MBI_MMap.h" - -/* -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 ); - - - 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))){ - - if ( mmap->type == MULTIBOOT_MEMORY_AVAILABLE){ - - memInfo->TotalMemory += mmap->len; - } else { - memInfo->ReservedMemory += mmap->len; - } - - print_Multiboot_memory_Map(mmap); - - } -} - - -*/ -/** - * @brief Debug Verbose functions - * - * @param mmap - */ -/* -void print_Multiboot_memory_Map(multiboot_memory_map_t* mmap) { - printf( - "size = 0x%x, base_addr = 0x%x%08x, length = 0x%x%08x, type = 0x%x\n", - (unsigned) mmap->size, - (unsigned) (mmap->addr >> 32), - (unsigned) (mmap->addr & 0xffffffff), - (unsigned) (mmap->len >> 32), - (unsigned) (mmap->len & 0xffffffff), - (unsigned) mmap->type - ); -} -*/ \ No newline at end of file diff --git a/source/kernel/Memory/MBIMMap/MBI_MMap.h b/source/kernel/Memory/MBIMMap/MBI_MMap.h deleted file mode 100644 index 1bd1ad3..0000000 --- a/source/kernel/Memory/MBIMMap/MBI_MMap.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include -//#include "../../multiboot.h" -#include "../memoryinfo.h" - -void initialise_available_regions(uint32_t memoryMapAddr, uint32_t memoryMapLastAddr, uint32_t* memoryBitMap, int* used_blocks); - -/* -void mapMultibootMemoryMap( MemoryInfo* memInfo , multiboot_info_t *mbt); -*/ -/** - * @brief Debug Verbose Functions - * - * @param mmap - */ -/* -void print_Multiboot_memory_Map(multiboot_memory_map_t* mmap); -*/ diff --git a/source/kernel/Memory/memoryinfo.h b/source/kernel/Memory/memoryinfo.h deleted file mode 100644 index 9bbb626..0000000 --- a/source/kernel/Memory/memoryinfo.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include -#include - -struct MemoryArea{ - void* StartAddress; - size_t Size; - unsigned int type; - MemoryArea* Next; - -}__attribute__((packed)); - -struct MemoryInfo { - uint32_t TotalMemory; - uint32_t ReservedMemory; - MemoryArea* MemoryRegionList; -}__attribute__((packed)); - - - diff --git a/source/kernel/Memory/paging.definitions.h b/source/kernel/Memory/paging.definitions.h deleted file mode 100644 index 2f771f7..0000000 --- a/source/kernel/Memory/paging.definitions.h +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once -#include -/* - This file contains some handy definitions for different types - that have to do with paging in atleast 32 bit and maybe sometime in the future - also 64 bit mode. -*/ - -#define MAX_DIRECTORY_ENTRIES 1024 -#define MAX_PAGE_TABLE_ENTRIES 1024 -#define MAX_PAGES 1024 - -#define PHYSICAL_ADDRESS uint32_t -#define VIRTUAL_ADDRESS uint32_t - -#define PageDirectoryEntry uint32_t -#define PageTableEntry uint32_t - - -#define KERNEL_VRT_MEMORY_BEGIN 0xC0000000 -#define KERNEL_VRT_MEMORY_END 0xCFFFFFFF -#define PAGE_SIZE 4096; - - -// NOTE: FIXME: I am fairly certain these masks are off by one! - -#define PD32_PRESENT_MASK (0x1 << 0) -#define PD32_READ_WRITE_MASK (0x1 << 1) -#define PD32_SUPERVISOR_MASK (0x1 << 2) -#define PD32_WRITE_THROUGH_MASK (0x1 << 3) -#define PD32_CACHE_DISABLE_MASK (0x1 << 4) -#define PD32_ACCESSED_MASK (0x1 << 5) -#define PD32_AVAILABLE_1_4KB_MASK (0x1 << 6) -#define PD32_DISABLE_4MB_MASK (0x1 << 6) -#define PD32_PAGE_SIZE_MASK (0x1 << 7) -#define PD32_GLOBAL_4MB_MASK (0x1 << 8) -#define PD32_AVAILABLE_2_4MB_MASK ( 14 << 9) -#define PD32_AVAILABLE_2_4KB_MASK ( 15 << 8) -#define PD32_ADDRESS_4KB_MASK (0x8FFFF << 12) -#define PD32_PAGE_ATTRIBUTE_TABLE_4MB_MASK (0x1 << 12) -#define PD32_HIGH_HALF_ADDRESS_4MB_MASK (0x7F<< 13) -#define PD32_RESERVED_4MB_MASK (0x1 << 21) -#define PD32_LOWER_HALF_ADDRESS_4MB_MASK (0x1FF << 22) - -#define PT32_PRESENT_MASK (0x1 << 0) -#define PT32_READ_WRITE_MASK (0x1 << 1) -#define PT32_SUPERVISOR_MASK (0x1 << 2) -#define PT32_WRITE_THROUGH_MASK (0x1 << 3) -#define PT32_CACHE_DISABLE_MASK (0x1 << 4) -#define PT32_ACCESSED_MASK (0x1 << 5) -#define PT32_DIRTY_MASK (0x1 << 6) -#define PT32_PAGE_ATTRIBUTE_TABLE_MASK (0x1 << 7) -#define PT32_GLOBAL_MASK (0x1 << 8) -#define PT32_AVAILABLE_MASK (0x7 << 9) -#define PT32_CACHE_DISABLE_MASK (0x7FFFF << 12) - diff --git a/source/kernel/Memory/paging.s b/source/kernel/Memory/paging.s deleted file mode 100644 index ca25a7d..0000000 --- a/source/kernel/Memory/paging.s +++ /dev/null @@ -1,44 +0,0 @@ -# NOTE: I wish this wasn't AT&T Syntax its horrible -# REMINDER: INSTRUCTION FROM_REGISTER, TO_REGISTER -.globl enablePaging -enablePaging: - # Create a new call frame - push %ebp - mov %esp, %ebp - - # Set the PG bit of CR0 - mov %cr0, %eax - or $0x80000000, %eax - mov %eax, %cr0 - - # Restore to the previous call frame - mov %ebp, %esp - pop %ebp - ret - -.globl loadPageDirectory -loadPageDirectory: - push %ebp - mov %esp, %ebp - - /* NOTE: We should probably check if paging is already enabled. - Changing the CR3 register whilst paging is enabled might - result in unwanted behaviour (in the worst case) or cause a - fault (in the best case). - */ - - mov 8(%esp), %eax # Move the first argument in the eax register - - mov %eax, %cr3 # Move the value of eax into the CR3 register - - /* - Moving the value of the argument passed to this function - into the CR3 register will allow the MMU to access the paging - structure we setup in memory once we enable paging - */ - - mov %ebp, %esp - pop %ebp - ret - - diff --git a/source/kernel/SuperVisorTerminal/superVisorTerminal.cpp b/source/kernel/SuperVisorTerminal/superVisorTerminal.cpp index 79dd349..74e5dcb 100644 --- a/source/kernel/SuperVisorTerminal/superVisorTerminal.cpp +++ b/source/kernel/SuperVisorTerminal/superVisorTerminal.cpp @@ -1,6 +1,6 @@ #include "superVisorTerminal.h" -void startSuperVisorTerminal(BootInfo* bootinfo){ +void startSuperVisorTerminal(){ while (true){ printf("SUPERVISOR:>$ " ); @@ -38,22 +38,11 @@ void startSuperVisorTerminal(BootInfo* bootinfo){ { // Show memory layout printf("========= Memory ==========\n"); + printf("Not Available!\n"); printf("Kernel MemoryMap:\n"); - //printf("kernel: 0x%x - 0x%x\n", &kernel_begin , &kernel_end); - printf("Frames used: 0x%x blocks of 4 KiB\n", 0); - const int bytesInGiB = 1073741824; - int64_t bytesLeft = (bootinfo->memory->TotalMemory % bytesInGiB) / bytesInGiB; - int64_t effectiveNumberOfGib = bootinfo->memory->TotalMemory / bytesInGiB; - - int64_t GiBs = effectiveNumberOfGib + bytesLeft; - - printf("Available Memory: %d bytes, %d GiB\n", bootinfo->memory->TotalMemory, GiBs ); - printf("Reserved Memory: %d bytes\n", bootinfo->memory->ReservedMemory); - - //printf("\n\n"); - //PrintPhysicalMemoryAllocation( ); - - + printf("Frames used: -- \n"); + printf("Available Memory:-- \n"); + printf("Reserved Memory: -- bytes\n"); } else if(strncmp("TEST", command, characterCount) == 0) { diff --git a/source/kernel/SuperVisorTerminal/superVisorTerminal.h b/source/kernel/SuperVisorTerminal/superVisorTerminal.h index eeed1df..7821191 100644 --- a/source/kernel/SuperVisorTerminal/superVisorTerminal.h +++ b/source/kernel/SuperVisorTerminal/superVisorTerminal.h @@ -4,6 +4,5 @@ #include "../Drivers/PIT/pit.h" #include "../Drivers/PS-2/keyboard.h" #include "../Memory/PhysicalMemoryManager.h" -#include "../bootinfo.h" -void startSuperVisorTerminal(BootInfo * ); \ No newline at end of file +void startSuperVisorTerminal(); \ No newline at end of file diff --git a/source/kernel/Boot/boot.s b/source/kernel/boot/boot.s similarity index 97% rename from source/kernel/Boot/boot.s rename to source/kernel/boot/boot.s index f8eb7f6..6c1faf6 100644 --- a/source/kernel/Boot/boot.s +++ b/source/kernel/boot/boot.s @@ -1,4 +1,4 @@ -.include "./source/kernel/Boot/Multiboot.S" +.include "./source/kernel/boot/multiboot.s" /* * Allocate initial stack */ @@ -116,7 +116,6 @@ isPaging: .include "./source/kernel/irs_table.s" .include "./source/kernel/irq_table.s" .include "./source/kernel/Interrupts/idt/idt.s" -.include "./source/kernel/Memory/paging.s" .include "./source/kernel/cpu.s" diff --git a/source/kernel/Boot/Multiboot.S b/source/kernel/boot/multiboot.s similarity index 100% rename from source/kernel/Boot/Multiboot.S rename to source/kernel/boot/multiboot.s diff --git a/source/kernel/bootinfo.h b/source/kernel/bootinfo.h deleted file mode 100644 index 1daa198..0000000 --- a/source/kernel/bootinfo.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include "Memory/memoryinfo.h" - -struct BootInfo{ - const char* BootStructureID = "BarinkOS"; - MemoryInfo* memory; - -}; \ No newline at end of file diff --git a/source/kernel/cpu.h b/source/kernel/cpu.h index 94d9485..0994c99 100644 --- a/source/kernel/cpu.h +++ b/source/kernel/cpu.h @@ -1,4 +1,5 @@ -#pragma once +#pragma once +#include /* Based on Intel specifications. diff --git a/source/kernel/kernel.cpp b/source/kernel/kernel.cpp index d2f1018..b66b1ed 100644 --- a/source/kernel/kernel.cpp +++ b/source/kernel/kernel.cpp @@ -21,7 +21,7 @@ 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); + printf("Kernel End Addr: 0x%x\n" , &kernel_end ); uint32_t PageDirectoryEntryIndex = ((uint32_t)&kernel_end + KERNEL_BASE_ADDR ) >> 22 ; @@ -93,16 +93,7 @@ extern "C" void early_main() // Setup PhysicalMemoryManagement SetupPhysicalMemoryManager(BootInfo); - // Small test! - - void* block = allocate_block(); - void* block2 = allocate_block(); - printf("Allocated addresss 1: 0x%x 2: 0x%x\n", (uint32_t)block ,(uint32_t)block2); - free_block(block); - free_block(block2); - void* block3 = allocate_block(); - printf("Allocated addresss 3: 0x%x\n", (uint32_t)block3); - free_block(block3); + } asm volatile("mov %cr0, %eax "); @@ -112,57 +103,23 @@ extern "C" void early_main() } - -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" ); -} - void PhysicalMemoryAllocatorTest(){ - -#ifdef UNIT_TESTS - // test alloc_block - uint8_t* memory = (uint8_t*) memAlloc.allocate_block(); - printf("Got a new pointer: 0x%x\n", memory); - - uint8_t* memory2 = (uint8_t*) memAlloc.allocate_block(); - printf("Got a new pointer: 0x%x\n", memory2); - - memAlloc.free_block((void*) memory); - uint8_t* newBlockPlse = (uint8_t*) memAlloc.allocate_block(); -#endif + #ifdef UNIT_TESTS + // Small test! + void* block = allocate_block(); + void* block2 = allocate_block(); + printf("Allocated addresss 1: 0x%x 2: 0x%x\n", (uint32_t)block ,(uint32_t)block2); + free_block(block); + free_block(block2); + void* block3 = allocate_block(); + printf("Allocated addresss 3: 0x%x\n", (uint32_t)block3); + free_block(block3); + #endif } extern "C" void kernel_main () { pit_initialise(); - // Create a dummy BootInfo object - // TODO: This should be done properly or the dependency should - // be removed from the SuperVisorTerminal. - BootInfo* bootinfo = {}; - - startSuperVisorTerminal(bootinfo); + startSuperVisorTerminal(); } \ No newline at end of file diff --git a/source/kernel/kernel.h b/source/kernel/kernel.h index 86bf667..c335c46 100644 --- a/source/kernel/kernel.h +++ b/source/kernel/kernel.h @@ -8,10 +8,7 @@ extern "C" #include "Drivers/VGA/VBE.h" #include "Terminal/kterm.h" -#include "bootinfo.h" - #include "Memory/PhysicalMemoryManager.h" -#include "Memory/memoryinfo.h" #include "Memory/VirtualMemoryManager.h" #include "Memory/GDT/gdtc.h"