From 23c68d9863ad8ebfd436b94640b6f03914b5eb5e Mon Sep 17 00:00:00 2001 From: Nigel Date: Fri, 18 Mar 2022 22:09:04 +0100 Subject: [PATCH] Setup paging function signatures... Ready to be implemented. --- Makefile | 5 ++- src/kernel/filesystems/FAT/FAT16.cpp | 0 src/kernel/filesystems/FAT/FAT16.h | 8 ++++ src/kernel/kernel.cpp | 3 ++ src/kernel/kernel.h | 1 + src/kernel/memory/PageDirectory.cpp | 43 -------------------- src/kernel/memory/PageDirectory.h | 31 --------------- src/kernel/memory/paging.cpp | 49 +++++++++++++++++++++++ src/kernel/memory/paging.definitions.h | 55 ++++++++++++++++++++++++++ src/kernel/memory/paging.h | 21 ++++++++++ 10 files changed, 141 insertions(+), 75 deletions(-) create mode 100644 src/kernel/filesystems/FAT/FAT16.cpp create mode 100644 src/kernel/filesystems/FAT/FAT16.h delete mode 100644 src/kernel/memory/PageDirectory.cpp delete mode 100644 src/kernel/memory/PageDirectory.h create mode 100644 src/kernel/memory/paging.cpp create mode 100644 src/kernel/memory/paging.definitions.h create mode 100644 src/kernel/memory/paging.h diff --git a/Makefile b/Makefile index c1b4aa0..eea8447 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 -O2 -Wall -Wextra -OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.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 SRC_DIR = src BUILD_DIR = build @@ -96,3 +96,6 @@ $(BUILD_DIR)/sv-terminal.o: $(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 diff --git a/src/kernel/filesystems/FAT/FAT16.cpp b/src/kernel/filesystems/FAT/FAT16.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/kernel/filesystems/FAT/FAT16.h b/src/kernel/filesystems/FAT/FAT16.h new file mode 100644 index 0000000..bc24274 --- /dev/null +++ b/src/kernel/filesystems/FAT/FAT16.h @@ -0,0 +1,8 @@ +#pragma once +#include "../../vfs/File.h" + + +class FAT16 : File { +public: + +}; \ No newline at end of file diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 9e78828..7d15fcf 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -4,6 +4,9 @@ extern "C" void kernel_main (BootInfo* bootinfo) { init_serial(); pit_initialise(); + InitializePaging(); + //Enable(); + startSuperVisorTerminal(bootinfo); } diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h index 604c4ff..c3b257b 100644 --- a/src/kernel/kernel.h +++ b/src/kernel/kernel.h @@ -14,6 +14,7 @@ extern "C" #include "memory/memory.h" #include "memory/memoryinfo.h" +#include "memory/paging.h" #include "bootcheck.h" #include "gdt/gdtc.h" diff --git a/src/kernel/memory/PageDirectory.cpp b/src/kernel/memory/PageDirectory.cpp deleted file mode 100644 index 5e4ab31..0000000 --- a/src/kernel/memory/PageDirectory.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "PageDirectory.h" - -void PageDirectory::enable() -{ - - - // https://wiki.osdev.org/Setting_Up_Paging - //set each entry to not present - // int i; - // for(i = 0; i < 1024; i++) - // { - // // This sets the following flags to the pages: - // // Supervisor: Only kernel-mode can access them - // // Write Enabled: It can be both read from and written to - // // Not Present: The page table is not present - // this->page_directory[i] = 0x00000002; - // } - - // // holds the physical address where we want to start mapping these pages to. - // // in this case, we want to map these pages to the very beginning of memory. - - // //we will fill all 1024 entries in the table, mapping 4 megabytes - // for(unsigned int i = 0; i < 1024; i++) - // { - // // As the address is page aligned, it will always leave 12 bits zeroed. - // // Those bits are used by the attributes ;) - // first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present. - // } - - // // attributes: supervisor level, read/write, present - // this->page_directory[0] = ((unsigned int)first_page_table) | 3; - - printf("Enable Paging!\n"); - - loadPageDirectory(this->page_directory); - enablePaging(); -} - - -void PageDirectory::MapPhysicalToVirtualAddress ( address_t PAddress , address_t VAddress, uint32_t size ) -{ - -} diff --git a/src/kernel/memory/PageDirectory.h b/src/kernel/memory/PageDirectory.h deleted file mode 100644 index c43c8fa..0000000 --- a/src/kernel/memory/PageDirectory.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include -#include "./memory.h" -#include "./../tty/kterm.h" -#define KB 1024 - - -typedef uintptr_t address_t; - -static const int MAX_PAGES = 1024 * KB; // 4GB , 4kB/page -static volatile address_t pmem_stack[MAX_PAGES]; -static volatile address_t pmem_stack_top = MAX_PAGES; // top down allocation - -extern "C" void loadPageDirectory (uint32_t* addr ); -extern "C" void enablePaging(); - -struct page_directory_entry {}; -struct page_table_entry{}; - - - -class PageDirectory { - public: - void enable (); - void MapPhysicalToVirtualAddress ( address_t PAddress , address_t VAddress, uint32_t size ); - - private: - uint32_t page_directory[1024] __attribute__((aligned(4096))); // align on 4 kiloByte pages - uint32_t first_page_table[1024] __attribute__((aligned(4096))); // align on 4 kiloByte pages - -}; \ No newline at end of file diff --git a/src/kernel/memory/paging.cpp b/src/kernel/memory/paging.cpp new file mode 100644 index 0000000..6f50e3c --- /dev/null +++ b/src/kernel/memory/paging.cpp @@ -0,0 +1,49 @@ +#include "paging.h" +PageDirectoryEntry kernel_directory[MAX_DIRECTORY_ENTRIES]; + +void InitializePaging() +{ + // Identity map the first 8MB ... Physical addresses 0x00000000 to 0x007A1200 + + + // Identity map the kernel space || our map to 3GB space + + + // Identity map VGA memory + + +} + + +void AllocatePage(VIRTUAL_ADDRESS vaddr, PageDirectoryEntry& page_directory) +{ + +} + + +void FreePage(VIRTUAL_ADDRESS vaddr , PageDirectoryEntry& page_directory) +{ + +} + + +void Map ( PHYSICAL_ADDRESS paddr, VIRTUAL_ADDRESS vaddr, PageDirectoryEntry& page_directory) +{ + +} + +void Unmap(VIRTUAL_ADDRESS, PageDirectoryEntry& page_directory) +{ + // NOTE: I will implement lazy unmapping for now + +} + + +void Enable() +{ + printf("Load into CR3 address: 0x%x\n", (uint32_t)(&kernel_directory[0])); + loadPageDirectory(&kernel_directory[0]); + enablePaging(); + + printf("Paging enabled!\n"); +} diff --git a/src/kernel/memory/paging.definitions.h b/src/kernel/memory/paging.definitions.h new file mode 100644 index 0000000..bd9c089 --- /dev/null +++ b/src/kernel/memory/paging.definitions.h @@ -0,0 +1,55 @@ +#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 + + + + +// 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/src/kernel/memory/paging.h b/src/kernel/memory/paging.h new file mode 100644 index 0000000..04aa2fb --- /dev/null +++ b/src/kernel/memory/paging.h @@ -0,0 +1,21 @@ +#pragma once +#include "./memory.h" +#include "./../tty/kterm.h" +#include "paging.definitions.h" + + +extern "C" void loadPageDirectory (uint32_t* addr ); +extern "C" void enablePaging(); + +void InitializePaging(); + +void Enable(); + +void AllocatePage(VIRTUAL_ADDRESS, PageDirectoryEntry&); + + +void FreePage(VIRTUAL_ADDRESS, PageDirectoryEntry&); + + +void Map(PHYSICAL_ADDRESS, VIRTUAL_ADDRESS, PageDirectoryEntry&); +void Unmap (VIRTUAL_ADDRESS, PageDirectoryEntry&);