From 13e9beea79d3284523017a2fb82c14c495b893db Mon Sep 17 00:00:00 2001 From: Nigel Date: Thu, 1 Sep 2022 20:16:16 +0200 Subject: [PATCH] KERNEL: Implementing VMM & cleaning up Folders now are alll lower case Started working on the implementation of the Virtual memory manager. Implemented allocate and free page funtionality for as far as I can atm. Implemented the --- Makefile | 27 ++++--- source/kernel/Memory/VirtualMemoryManager.cpp | 24 ------ .../SuperVisorTerminal/superVisorTerminal.h | 8 -- source/kernel/boot/boot.s | 15 +--- source/kernel/cpu.cpp | 39 ++++++++++ source/kernel/cpu.h | 11 ++- source/kernel/cpu.s | 63 ---------------- .../kernel/{Drivers => drivers}/cmos/cmos.cpp | 0 .../{Drivers/PCI => drivers/pci}/pci.cpp | 0 .../kernel/{Drivers/PCI => drivers/pci}/pci.h | 0 .../{Drivers/PIC => drivers/pic}/pic.cpp | 0 .../kernel/{Drivers/PIC => drivers/pic}/pic.h | 0 .../{Drivers/PIT => drivers/pit}/pit.cpp | 2 +- .../kernel/{Drivers/PIT => drivers/pit}/pit.h | 1 + .../PS-2 => drivers/ps-2}/keyboard.cpp | 0 .../{Drivers/PS-2 => drivers/ps-2}/keyboard.h | 2 +- .../Serial => drivers/serial}/serial.cpp | 0 .../Serial => drivers/serial}/serial.h | 0 .../kernel/{Drivers/VGA => drivers/vga}/VBE.h | 0 .../{Drivers/VGA => drivers/vga}/colors.h | 0 .../{FileSystem => filesystem}/FAT/FAT16.cpp | 0 .../{FileSystem => filesystem}/FAT/FAT16.h | 0 .../{Interrupts => interrupts}/idt/idt.cpp | 4 +- .../{Interrupts => interrupts}/idt/idt.h | 6 +- .../{Interrupts => interrupts}/idt/idt.s | 0 .../idt/scancodes/set1.h | 0 source/kernel/kernel.cpp | 11 +-- source/kernel/kernel.h | 20 ++--- source/kernel/{Lib => lib}/mem.h | 0 source/kernel/{Lib => lib}/string.c | 0 source/kernel/{Lib => lib}/string.h | 0 .../kernel/{Memory => memory}/KernelHeap.cpp | 3 +- source/kernel/{Memory => memory}/KernelHeap.h | 0 .../PhysicalMemoryManager.cpp | 30 ++++---- .../PhysicalMemoryManager.h | 10 +-- source/kernel/memory/VirtualMemoryManager.cpp | 73 +++++++++++++++++++ .../{Memory => memory}/VirtualMemoryManager.h | 2 +- .../kernel/{Memory/GDT => memory/gdt}/gdt.s | 0 .../{Memory/GDT => memory/gdt}/gdtc.cpp | 2 +- .../kernel/{Memory/GDT => memory/gdt}/gdtc.h | 0 .../{PreKernel => prekernel}/bootstructure.h | 0 .../{PreKernel => prekernel}/multiboot.h | 0 .../{PreKernel => prekernel}/prekernel.cpp | 1 + source/kernel/serial.h | 2 +- .../superVisorTerminal.cpp | 0 .../supervisorterminal/superVisorTerminal.h | 8 ++ .../kernel/{Terminal => terminal}/kterm.cpp | 0 source/kernel/{Terminal => terminal}/kterm.h | 4 +- 48 files changed, 195 insertions(+), 173 deletions(-) delete mode 100644 source/kernel/Memory/VirtualMemoryManager.cpp delete mode 100644 source/kernel/SuperVisorTerminal/superVisorTerminal.h create mode 100644 source/kernel/cpu.cpp delete mode 100644 source/kernel/cpu.s rename source/kernel/{Drivers => drivers}/cmos/cmos.cpp (100%) rename source/kernel/{Drivers/PCI => drivers/pci}/pci.cpp (100%) rename source/kernel/{Drivers/PCI => drivers/pci}/pci.h (100%) rename source/kernel/{Drivers/PIC => drivers/pic}/pic.cpp (100%) rename source/kernel/{Drivers/PIC => drivers/pic}/pic.h (100%) rename source/kernel/{Drivers/PIT => drivers/pit}/pit.cpp (95%) rename source/kernel/{Drivers/PIT => drivers/pit}/pit.h (88%) rename source/kernel/{Drivers/PS-2 => drivers/ps-2}/keyboard.cpp (100%) rename source/kernel/{Drivers/PS-2 => drivers/ps-2}/keyboard.h (93%) rename source/kernel/{Drivers/Serial => drivers/serial}/serial.cpp (100%) rename source/kernel/{Drivers/Serial => drivers/serial}/serial.h (100%) rename source/kernel/{Drivers/VGA => drivers/vga}/VBE.h (100%) rename source/kernel/{Drivers/VGA => drivers/vga}/colors.h (100%) rename source/kernel/{FileSystem => filesystem}/FAT/FAT16.cpp (100%) rename source/kernel/{FileSystem => filesystem}/FAT/FAT16.h (100%) rename source/kernel/{Interrupts => interrupts}/idt/idt.cpp (99%) rename source/kernel/{Interrupts => interrupts}/idt/idt.h (94%) rename source/kernel/{Interrupts => interrupts}/idt/idt.s (100%) rename source/kernel/{Interrupts => interrupts}/idt/scancodes/set1.h (100%) rename source/kernel/{Lib => lib}/mem.h (100%) rename source/kernel/{Lib => lib}/string.c (100%) rename source/kernel/{Lib => lib}/string.h (100%) rename source/kernel/{Memory => memory}/KernelHeap.cpp (93%) rename source/kernel/{Memory => memory}/KernelHeap.h (100%) rename source/kernel/{Memory => memory}/PhysicalMemoryManager.cpp (84%) rename source/kernel/{Memory => memory}/PhysicalMemoryManager.h (78%) create mode 100644 source/kernel/memory/VirtualMemoryManager.cpp rename source/kernel/{Memory => memory}/VirtualMemoryManager.h (87%) rename source/kernel/{Memory/GDT => memory/gdt}/gdt.s (100%) rename source/kernel/{Memory/GDT => memory/gdt}/gdtc.cpp (97%) rename source/kernel/{Memory/GDT => memory/gdt}/gdtc.h (100%) rename source/kernel/{PreKernel => prekernel}/bootstructure.h (100%) rename source/kernel/{PreKernel => prekernel}/multiboot.h (100%) rename source/kernel/{PreKernel => prekernel}/prekernel.cpp (99%) rename source/kernel/{SuperVisorTerminal => supervisorterminal}/superVisorTerminal.cpp (100%) create mode 100644 source/kernel/supervisorterminal/superVisorTerminal.h rename source/kernel/{Terminal => terminal}/kterm.cpp (100%) rename source/kernel/{Terminal => terminal}/kterm.h (93%) diff --git a/Makefile b/Makefile index 01da565..3f64c5a 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 $(BUILD_DIR)/prekernel.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)/prekernel.o $(BUILD_DIR)/cpu.o SRC_DIR = source BUILD_DIR = build @@ -55,7 +55,7 @@ $(BUILD_DIR)/kernel.o: $(CPP) -c $(SRC_DIR)/kernel/kernel.cpp -o $(BUILD_DIR)/kernel.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/kterm.o: - $(CPP) -c $(SRC_DIR)/kernel/Terminal/kterm.cpp -o $(BUILD_DIR)/kterm.o $(CFLAGS) -fno-exceptions -fno-rtti + $(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 @@ -71,38 +71,41 @@ $(BUILD_DIR)/io.o: $(BUILD_DIR)/idt.o: - $(CPP) -c $(SRC_DIR)/kernel/Interrupts/idt/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c $(SRC_DIR)/kernel/interrupts/idt/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/gdtc.o: - $(CPP) -c $(SRC_DIR)/kernel/Memory/GDT/gdtc.cpp -o $(BUILD_DIR)/gdtc.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c $(SRC_DIR)/kernel/memory/gdt/gdtc.cpp -o $(BUILD_DIR)/gdtc.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/pic.o: - $(CPP) -c $(SRC_DIR)/kernel/Drivers/PIC/pic.cpp -o $(BUILD_DIR)/pic.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c $(SRC_DIR)/kernel/drivers/pic/pic.cpp -o $(BUILD_DIR)/pic.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/string.o: - $(CC) -c $(SRC_DIR)/kernel/Lib/string.c -o $(BUILD_DIR)/string.o $(CFLAGS) -std=gnu99 + $(CC) -c $(SRC_DIR)/kernel/lib/string.c -o $(BUILD_DIR)/string.o $(CFLAGS) -std=gnu99 $(BUILD_DIR)/pit.o: - $(CPP) -c $(SRC_DIR)/kernel/Drivers/PIT/pit.cpp -o $(BUILD_DIR)/pit.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c $(SRC_DIR)/kernel/drivers/pit/pit.cpp -o $(BUILD_DIR)/pit.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/keyboard.o: - $(CPP) -c $(SRC_DIR)/kernel/Drivers/PS-2/keyboard.cpp -o $(BUILD_DIR)/keyboard.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c $(SRC_DIR)/kernel/drivers/ps-2/keyboard.cpp -o $(BUILD_DIR)/keyboard.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/time.o: $(CPP) -c $(SRC_DIR)/kernel/time.cpp -o $(BUILD_DIR)/time.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/sv-terminal.o: - $(CPP) -c $(SRC_DIR)/kernel/SuperVisorTerminal/superVisorTerminal.cpp -o $(BUILD_DIR)/sv-terminal.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c $(SRC_DIR)/kernel/supervisorterminal/superVisorTerminal.cpp -o $(BUILD_DIR)/sv-terminal.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/memory.o: - $(CPP) -c $(SRC_DIR)/kernel/Memory/PhysicalMemoryManager.cpp -o $(BUILD_DIR)/memory.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c $(SRC_DIR)/kernel/memory/PhysicalMemoryManager.cpp -o $(BUILD_DIR)/memory.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/paging.o: - $(CPP) -c $(SRC_DIR)/kernel/Memory/VirtualMemoryManager.cpp -o $(BUILD_DIR)/paging.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c $(SRC_DIR)/kernel/memory/VirtualMemoryManager.cpp -o $(BUILD_DIR)/paging.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/prekernel.o: - $(CPP) -c $(SRC_DIR)/kernel/PreKernel/prekernel.cpp -o $(BUILD_DIR)/prekernel.o $(CFLAGS) -fno-exceptions -fno-rtti \ No newline at end of file + $(CPP) -c $(SRC_DIR)/kernel/prekernel/prekernel.cpp -o $(BUILD_DIR)/prekernel.o $(CFLAGS) -fno-exceptions -fno-rtti + +$(BUILD_DIR)/cpu.o: + $(CPP) -c $(SRC_DIR)/kernel/cpu.cpp -o $(BUILD_DIR)/cpu.o $(CFLAGS) -fno-exceptions -fno-rtti diff --git a/source/kernel/Memory/VirtualMemoryManager.cpp b/source/kernel/Memory/VirtualMemoryManager.cpp deleted file mode 100644 index 2af5da6..0000000 --- a/source/kernel/Memory/VirtualMemoryManager.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "VirtualMemoryManager.h" - -extern "C" void loadPageDirectory (uint32_t* addr ); -extern "C" void enablePaging(); - -void AllocatePage(uint32_t vaddr) -{ - -} - -void FreePage(uint32_t vaddr ) -{ - -} - -void Map ( uint32_t vaddr, uint32_t paddr) -{ - -} - -void Unmap(uint32_t vaddr) -{ - // NOTE: I will implement lazy unmapping for now -} diff --git a/source/kernel/SuperVisorTerminal/superVisorTerminal.h b/source/kernel/SuperVisorTerminal/superVisorTerminal.h deleted file mode 100644 index 7821191..0000000 --- a/source/kernel/SuperVisorTerminal/superVisorTerminal.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include "../Terminal/kterm.h" -#include "../time.h" -#include "../Drivers/PIT/pit.h" -#include "../Drivers/PS-2/keyboard.h" -#include "../Memory/PhysicalMemoryManager.h" - -void startSuperVisorTerminal(); \ No newline at end of file diff --git a/source/kernel/boot/boot.s b/source/kernel/boot/boot.s index 6c1faf6..21a1bb8 100644 --- a/source/kernel/boot/boot.s +++ b/source/kernel/boot/boot.s @@ -17,6 +17,7 @@ stack_top: .globl boot_page_directory boot_page_directory: .skip 4096 +.globl boot_page_table boot_page_table: .skip 4096 .globl multiboot_page_table @@ -60,15 +61,6 @@ _start: 3: # Map VGA video memory to 0xC03FF00 as "present, writable" movl $(0x000B8000 | 0x003), boot_page_table - 0xC0000000 + 1023 * 4 - # IMPORTANT NOTE FROM WIKI.OSDEV.ORG/HIGHER_HALF_X86_BARE_BONES - - # The page table is used at both page directory entry 0 (virtually from 0x0 - # to 0x3FFFFF) (thus identity mapping the kernel) and page directory entry - # 768 (virtually from 0xC0000000 to 0xC03FFFFF) (thus mapping it in the - # higher half). The kernel is identity mapped because enabling paging does - # not change the next instruction, which continues to be physical. The CPU - # would instead page fault if there was no identity mapping.\ - # Map the page table to both virtual addresss 0x00000000 and 0xC0000000 movl $(boot_page_table - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 0 movl $(boot_page_table - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 768 * 4 @@ -112,10 +104,9 @@ isPaging: jmp 1b -.include "./source/kernel/Memory/GDT/gdt.s" +.include "./source/kernel/memory/gdt/gdt.s" .include "./source/kernel/irs_table.s" .include "./source/kernel/irq_table.s" -.include "./source/kernel/Interrupts/idt/idt.s" -.include "./source/kernel/cpu.s" +.include "./source/kernel/interrupts/idt/idt.s" diff --git a/source/kernel/cpu.cpp b/source/kernel/cpu.cpp new file mode 100644 index 0000000..897e92d --- /dev/null +++ b/source/kernel/cpu.cpp @@ -0,0 +1,39 @@ +#include "cpu.h" + + +uint32_t GetEFLAGS() +{ + uint32_t EFLAGS = 0; + asm volatile ("pushfl;" "movl 4(%%esp), %%edx" : "=d"(EFLAGS)); + return EFLAGS; +} + + +uint32_t GetCR0() +{ + uint32_t cr0_value; + asm volatile ("movl %%cr0, %%edx" : "=d"(cr0_value)); + return cr0_value; + +} + + +uint32_t GetCR2(){ + uint32_t cr2_value; + __asm__ volatile("movl %%cr2, %%edx": "=d"(cr2_value)); + return cr2_value; +} + + +uint32_t GetCR3(){ + uint32_t cr3_value; + __asm__ volatile("movl %%cr3, %%edx": "=d"(cr3_value)); + return cr3_value; +} + + +uint32_t GetCR4(){ + uint32_t cr4_value; + __asm__ volatile("movl %%cr4, %%edx": "=d"(cr4_value)); + return cr4_value; +} \ No newline at end of file diff --git a/source/kernel/cpu.h b/source/kernel/cpu.h index 0994c99..99cf6a8 100644 --- a/source/kernel/cpu.h +++ b/source/kernel/cpu.h @@ -9,19 +9,18 @@ C++ interface for the cpu.s assembly file. ©Nigel Barink - 2022 */ - /* * EFLAGS FUNCTIONS */ -extern "C" uint32_t GetEFLAGS(); +uint32_t GetEFLAGS(); /* * CONTROL_REGISTER_0 FUNCTIONS */ -extern "C" uint32_t GetCR0(); +uint32_t GetCR0(); /* struct CR0_Register { @@ -57,7 +56,7 @@ extern "C" uint32_t GetCR0(); * CONTROL_REGISTER_4 FUNCTIONS */ -extern "C" uint32_t GetCR4(); +uint32_t GetCR4(); #define GET_PSE_BIT(CONTROL_REGISTER_4) (CONTROL_REGISTER_4&0x4) #define GET_PAE_BIT(CONTROL_REGISTER_4) (CONTROL_REGISTER_4&0x5) @@ -66,10 +65,10 @@ extern "C" uint32_t GetCR4(); * CONTROL_REGISTER_2 FUNCTIONS */ -extern "C" uint32_t GetCR2(); +uint32_t GetCR2(); /* * CONTROL_REGISTER_3 FUNCTIONS */ -extern "C" uint32_t GetCR3(); \ No newline at end of file +uint32_t GetCR3(); \ No newline at end of file diff --git a/source/kernel/cpu.s b/source/kernel/cpu.s deleted file mode 100644 index 2df41db..0000000 --- a/source/kernel/cpu.s +++ /dev/null @@ -1,63 +0,0 @@ -# Basic cpu functions -.globl GetCR0 -GetCR0: - push %ebp # save the base pointer on the stack - mov %esp, %ebp # Set the base pointer to the current stack pointer - - xor %eax, %eax # Clear EAX to make sure no weird stuff is going on - mov %cr0, %eax # Copy the value of the CR0 register into EAX - - mov %ebp, %esp # restore the base pointer - pop %ebp - ret - -.globl GetCR4 -GetCR4: - push %ebp - mov %esp, %ebp - - xor %eax, %eax - mov %cr4, %eax - - mov %ebp, %esp - pop %ebp - ret - - - -.globl GetEFLAGS -GetEFLAGS: - push %ebp - mov %esp, %ebp - - xor %eax, %eax - pushfl # Push the EFLAGS register content onto the stack, should be pushfd but GAS apparently doesn't agree - mov 4(%esp) , %eax - - mov %ebp, %esp - pop %ebp - ret - -.globl GetCR2 -GetCR2: - push %ebp - mov %esp, %ebp - - xor %eax, %eax - mov %cr2, %eax - - mov %ebp, %esp - pop %ebp - ret - -.globl GetCR3 -GetCR3: - push %ebp - mov %esp, %ebp - - xor %eax, %eax - mov %cr3, %eax - - mov %ebp, %esp - pop %ebp - ret diff --git a/source/kernel/Drivers/cmos/cmos.cpp b/source/kernel/drivers/cmos/cmos.cpp similarity index 100% rename from source/kernel/Drivers/cmos/cmos.cpp rename to source/kernel/drivers/cmos/cmos.cpp diff --git a/source/kernel/Drivers/PCI/pci.cpp b/source/kernel/drivers/pci/pci.cpp similarity index 100% rename from source/kernel/Drivers/PCI/pci.cpp rename to source/kernel/drivers/pci/pci.cpp diff --git a/source/kernel/Drivers/PCI/pci.h b/source/kernel/drivers/pci/pci.h similarity index 100% rename from source/kernel/Drivers/PCI/pci.h rename to source/kernel/drivers/pci/pci.h diff --git a/source/kernel/Drivers/PIC/pic.cpp b/source/kernel/drivers/pic/pic.cpp similarity index 100% rename from source/kernel/Drivers/PIC/pic.cpp rename to source/kernel/drivers/pic/pic.cpp diff --git a/source/kernel/Drivers/PIC/pic.h b/source/kernel/drivers/pic/pic.h similarity index 100% rename from source/kernel/Drivers/PIC/pic.h rename to source/kernel/drivers/pic/pic.h diff --git a/source/kernel/Drivers/PIT/pit.cpp b/source/kernel/drivers/pit/pit.cpp similarity index 95% rename from source/kernel/Drivers/PIT/pit.cpp rename to source/kernel/drivers/pit/pit.cpp index afe8536..6b5eaa3 100644 --- a/source/kernel/Drivers/PIT/pit.cpp +++ b/source/kernel/drivers/pit/pit.cpp @@ -1,5 +1,5 @@ #include "pit.h" -#include "../../Terminal/kterm.h" + uint32_t pit_tick = 0; diff --git a/source/kernel/Drivers/PIT/pit.h b/source/kernel/drivers/pit/pit.h similarity index 88% rename from source/kernel/Drivers/PIT/pit.h rename to source/kernel/drivers/pit/pit.h index 7e4d0f1..72bb383 100644 --- a/source/kernel/Drivers/PIT/pit.h +++ b/source/kernel/drivers/pit/pit.h @@ -1,6 +1,7 @@ #pragma once #include #include "../../io.h" +#include "../../terminal/kterm.h" #define PIT_DATA_0 0x40 #define PIT_DATA_1 0x41 #define PIT_DATA_2 0x42 diff --git a/source/kernel/Drivers/PS-2/keyboard.cpp b/source/kernel/drivers/ps-2/keyboard.cpp similarity index 100% rename from source/kernel/Drivers/PS-2/keyboard.cpp rename to source/kernel/drivers/ps-2/keyboard.cpp diff --git a/source/kernel/Drivers/PS-2/keyboard.h b/source/kernel/drivers/ps-2/keyboard.h similarity index 93% rename from source/kernel/Drivers/PS-2/keyboard.h rename to source/kernel/drivers/ps-2/keyboard.h index f93b0fb..52c53ba 100644 --- a/source/kernel/Drivers/PS-2/keyboard.h +++ b/source/kernel/drivers/ps-2/keyboard.h @@ -1,6 +1,6 @@ #pragma once #include -#include "../../Terminal/kterm.h" +#include "../../terminal/kterm.h" enum ScanCodeSet { None = 0, ScanCodeSet1 = 1, diff --git a/source/kernel/Drivers/Serial/serial.cpp b/source/kernel/drivers/serial/serial.cpp similarity index 100% rename from source/kernel/Drivers/Serial/serial.cpp rename to source/kernel/drivers/serial/serial.cpp diff --git a/source/kernel/Drivers/Serial/serial.h b/source/kernel/drivers/serial/serial.h similarity index 100% rename from source/kernel/Drivers/Serial/serial.h rename to source/kernel/drivers/serial/serial.h diff --git a/source/kernel/Drivers/VGA/VBE.h b/source/kernel/drivers/vga/VBE.h similarity index 100% rename from source/kernel/Drivers/VGA/VBE.h rename to source/kernel/drivers/vga/VBE.h diff --git a/source/kernel/Drivers/VGA/colors.h b/source/kernel/drivers/vga/colors.h similarity index 100% rename from source/kernel/Drivers/VGA/colors.h rename to source/kernel/drivers/vga/colors.h diff --git a/source/kernel/FileSystem/FAT/FAT16.cpp b/source/kernel/filesystem/FAT/FAT16.cpp similarity index 100% rename from source/kernel/FileSystem/FAT/FAT16.cpp rename to source/kernel/filesystem/FAT/FAT16.cpp diff --git a/source/kernel/FileSystem/FAT/FAT16.h b/source/kernel/filesystem/FAT/FAT16.h similarity index 100% rename from source/kernel/FileSystem/FAT/FAT16.h rename to source/kernel/filesystem/FAT/FAT16.h diff --git a/source/kernel/Interrupts/idt/idt.cpp b/source/kernel/interrupts/idt/idt.cpp similarity index 99% rename from source/kernel/Interrupts/idt/idt.cpp rename to source/kernel/interrupts/idt/idt.cpp index 8b4fd83..7ee01da 100644 --- a/source/kernel/Interrupts/idt/idt.cpp +++ b/source/kernel/interrupts/idt/idt.cpp @@ -1,6 +1,6 @@ #include "idt.h" -#include "../../Drivers/PIT/pit.h" -#include "../../Drivers/PS-2/keyboard.h" +#include "../../drivers/pit/pit.h" +#include "../../drivers/ps-2/keyboard.h" #include "../../cpu.h" IDT_entry idt_table[256]; IDT_ptr idt_ptr; diff --git a/source/kernel/Interrupts/idt/idt.h b/source/kernel/interrupts/idt/idt.h similarity index 94% rename from source/kernel/Interrupts/idt/idt.h rename to source/kernel/interrupts/idt/idt.h index 6ca3399..94e409f 100644 --- a/source/kernel/Interrupts/idt/idt.h +++ b/source/kernel/interrupts/idt/idt.h @@ -2,10 +2,10 @@ #include #include -#include "../../Drivers/VGA/colors.h" -#include "../../Drivers/PIC/pic.h" +#include "../../drivers/vga/colors.h" +#include "../../drivers/pic/pic.h" -#include "../../Terminal/kterm.h" +#include "../../terminal/kterm.h" extern "C" { diff --git a/source/kernel/Interrupts/idt/idt.s b/source/kernel/interrupts/idt/idt.s similarity index 100% rename from source/kernel/Interrupts/idt/idt.s rename to source/kernel/interrupts/idt/idt.s diff --git a/source/kernel/Interrupts/idt/scancodes/set1.h b/source/kernel/interrupts/idt/scancodes/set1.h similarity index 100% rename from source/kernel/Interrupts/idt/scancodes/set1.h rename to source/kernel/interrupts/idt/scancodes/set1.h diff --git a/source/kernel/kernel.cpp b/source/kernel/kernel.cpp index b66b1ed..6cc3d68 100644 --- a/source/kernel/kernel.cpp +++ b/source/kernel/kernel.cpp @@ -1,11 +1,6 @@ #include "kernel.h" extern "C" void early_main() { - /* - * Initialize terminal interface - */ - - kterm_init(); initGDT(); @@ -64,10 +59,11 @@ extern "C" void early_main() printf("MemoryInfoheap size : %d bytes\n", BootInfo->map_size); // Print the memory regions MemoryInfoBlock* currentBlock = (MemoryInfoBlock*) ((uint32_t)BootInfo->MemoryMap + KERNEL_BASE_ADDR) ; - + printf( "Starting address: 0x%x\n", currentBlock); while( (uint32_t)currentBlock->next != 0x0 ) { + printf("CurrentBlock: 0x%x \n", (uint32_t ) currentBlock ); if(IS_AVAILABLE_MEM(currentBlock->type)){ //printf("AVAILABLE RAM\n"); } @@ -91,11 +87,12 @@ extern "C" void early_main() } + printf("Starting physical memory management setup\n"); // Setup PhysicalMemoryManagement SetupPhysicalMemoryManager(BootInfo); } - + printf("Enable Protected mode and jump to kernel main\n"); asm volatile("mov %cr0, %eax "); asm volatile("or $1, %eax"); asm volatile("mov %eax, %cr0"); // re-enable protected mode ? diff --git a/source/kernel/kernel.h b/source/kernel/kernel.h index c335c46..7a955d1 100644 --- a/source/kernel/kernel.h +++ b/source/kernel/kernel.h @@ -1,27 +1,27 @@ #pragma once extern "C" { - #include "Lib/string.h" + #include "lib/string.h" } #include "definitions.h" -#include "Drivers/VGA/VBE.h" -#include "Terminal/kterm.h" +#include "drivers/vga/VBE.h" +#include "terminal/kterm.h" -#include "Memory/PhysicalMemoryManager.h" -#include "Memory/VirtualMemoryManager.h" +#include "memory/PhysicalMemoryManager.h" +#include "memory/VirtualMemoryManager.h" -#include "Memory/GDT/gdtc.h" -#include "Interrupts/idt/idt.h" +#include "memory/gdt/gdtc.h" +#include "interrupts/idt/idt.h" -#include "Drivers/PIT/pit.h" +#include "drivers/pit/pit.h" #include "io.h" #include "cpu.h" #include "serial.h" #include "time.h" -#include "SuperVisorTerminal/superVisorTerminal.h" -#include "PreKernel/bootstructure.h" +#include "supervisorterminal/superVisorTerminal.h" +#include "prekernel/bootstructure.h" #define CHECK_FLAG(flag, bit) ( flag & (1 << bit )) #define PANIC(message) {return;} diff --git a/source/kernel/Lib/mem.h b/source/kernel/lib/mem.h similarity index 100% rename from source/kernel/Lib/mem.h rename to source/kernel/lib/mem.h diff --git a/source/kernel/Lib/string.c b/source/kernel/lib/string.c similarity index 100% rename from source/kernel/Lib/string.c rename to source/kernel/lib/string.c diff --git a/source/kernel/Lib/string.h b/source/kernel/lib/string.h similarity index 100% rename from source/kernel/Lib/string.h rename to source/kernel/lib/string.h diff --git a/source/kernel/Memory/KernelHeap.cpp b/source/kernel/memory/KernelHeap.cpp similarity index 93% rename from source/kernel/Memory/KernelHeap.cpp rename to source/kernel/memory/KernelHeap.cpp index bc5abce..4144947 100644 --- a/source/kernel/Memory/KernelHeap.cpp +++ b/source/kernel/memory/KernelHeap.cpp @@ -14,6 +14,7 @@ void* malloc(size_t size) printf("Received request for %d bytes of memory", size); heap_block* current = start; + // look for a free block while(current < start + heap_size) { if(current->size >= size && current->Used == false ) @@ -33,7 +34,7 @@ void* malloc(size_t size) // If we are here we need more memory so we should // probably ask the VMM for more - // TODO: ask for more memory + // TODO: ask for more memory | Extend kernel heap } diff --git a/source/kernel/Memory/KernelHeap.h b/source/kernel/memory/KernelHeap.h similarity index 100% rename from source/kernel/Memory/KernelHeap.h rename to source/kernel/memory/KernelHeap.h diff --git a/source/kernel/Memory/PhysicalMemoryManager.cpp b/source/kernel/memory/PhysicalMemoryManager.cpp similarity index 84% rename from source/kernel/Memory/PhysicalMemoryManager.cpp rename to source/kernel/memory/PhysicalMemoryManager.cpp index bda801c..90af1ac 100644 --- a/source/kernel/Memory/PhysicalMemoryManager.cpp +++ b/source/kernel/memory/PhysicalMemoryManager.cpp @@ -1,29 +1,33 @@ #include "./PhysicalMemoryManager.h" PhysicalMemoryManagerInfoBlock* PMMInfoBlock; +extern uint32_t* boot_page_directory; +extern uint32_t* boot_page_table; const uint32_t KERNEL_OFFSET = 0xC0000000; void SetupPhysicalMemoryManager( BootInfoBlock* Bootinfo) { - - PMMInfoBlock = (PhysicalMemoryManagerInfoBlock*) ((uint32_t)MemoryMapHeap_pptr + Bootinfo->map_size + 0xC0000000); - + // NOTE: Physical memory map will override the boot info for now! + PMMInfoBlock = (PhysicalMemoryManagerInfoBlock*) (&BootInfoBlock_pptr + KERNEL_OFFSET ); + printf("Setting up physical memory infoblock (0x%x) \n", (uint32_t)&PMMInfoBlock); /* Every byte contains 8 pages A page is 4096 kib Every block (1 bit) represent an page */ - // calculate the maximum number of blocks - PMMInfoBlock->max_blocks =Bootinfo->MemorySize / BLOCK_SIZE / 8; - PMMInfoBlock->used_blocks = 0; + // Calculate the maximum number of blocks + printf("Maxblocks at address(0x%x)\n" , (uint32_t)&(PMMInfoBlock->max_blocks)); + + int maximum_blocks = (uint32_t)Bootinfo->MemorySize / BLOCK_SIZE / 8; + printf("Set bitmap block maximum: %d\n", maximum_blocks); + PMMInfoBlock->max_blocks = maximum_blocks; + + printf("Set used blocks to zero\n"); + PMMInfoBlock->used_blocks = 0; + + printf("Determine memory bit map address"); // put the map after the gdt PMMInfoBlock->memoryBitMap = (uint32_t*) ( 0xC010b100) ; - - // // Page in the address space please - // uint32_t PDEI = 0xC020a000 >> 22; - // uint32_t PTEI = (0xC020a000 >> 12) & 0x1FFF; - // printf("PDEI: %d, PTEI: %d\n", PDEI, PTEI); - printf("Maximum num blocks: %d \n",PMMInfoBlock->max_blocks); //Size of memory map @@ -54,7 +58,7 @@ void SetupPhysicalMemoryManager( BootInfoBlock* Bootinfo) { printf("kernel size in memory: 0x%x\n", kernel_size); allocate_region((uint32_t)&kernel_begin, kernel_size); - printf("allocate BIOS region"); + printf("allocate BIOS region\n"); allocate_region (0x0000000, 0x00100000); } diff --git a/source/kernel/Memory/PhysicalMemoryManager.h b/source/kernel/memory/PhysicalMemoryManager.h similarity index 78% rename from source/kernel/Memory/PhysicalMemoryManager.h rename to source/kernel/memory/PhysicalMemoryManager.h index 1122b25..f51c329 100644 --- a/source/kernel/Memory/PhysicalMemoryManager.h +++ b/source/kernel/memory/PhysicalMemoryManager.h @@ -1,8 +1,8 @@ #pragma once #include -#include "../PreKernel/bootstructure.h" -#include "../Terminal/kterm.h" -#include "../Lib/mem.h" +#include "../prekernel/bootstructure.h" +#include "../terminal/kterm.h" +#include "../lib/mem.h" #include "../bitmap.h" // Asumming i386 for now! @@ -14,8 +14,8 @@ struct PhysicalMemoryManagerInfoBlock { uint32_t* memoryBitMap; - size_t pmmap_size; - size_t max_blocks; + uint32_t pmmap_size; + uint32_t max_blocks; int used_blocks; }; diff --git a/source/kernel/memory/VirtualMemoryManager.cpp b/source/kernel/memory/VirtualMemoryManager.cpp new file mode 100644 index 0000000..a01f8d0 --- /dev/null +++ b/source/kernel/memory/VirtualMemoryManager.cpp @@ -0,0 +1,73 @@ +#include "VirtualMemoryManager.h" +extern uint32_t boot_page_directory[1024] ; +extern uint32_t boot_page_table[1024]; +void AllocatePage(uint32_t vaddr) +{ + uint32_t page_aligned_address = ALIGN(vaddr, 4096); + + // allocate a page at virtual address + int PageDirectoryEntryIndex = vaddr >> 22; + int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF; + + printf("Allocation happening at PDE: %d PTE: %d\n", PageDirectoryEntryIndex, PageTableEntryIndex); + + // check if the page directory entry is marked as present + if (boot_page_directory[PageDirectoryEntryIndex] & 0x1 ) { + + uint32_t* page_table = (uint32_t*)((boot_page_directory[PageDirectoryEntryIndex]) & 0xFFFFE000 + 0xC0000000); + + // check if the page table entry is marked as present + if ( page_table[PageTableEntryIndex] & 0x1 ) + { + // Map the entry to a physical page + page_table[PageTableEntryIndex] = (uint32_t)(allocate_block() + 0x3); + } else{ + // mark page as present + page_table[PageTableEntryIndex] = 0x3; + } + + } else { + // mark the page table as present and allocate a physical block for it + boot_page_directory[PageDirectoryEntryIndex] = (uint32_t)(allocate_block() + 0x3); + } + + +} + +void FreePage(uint32_t vaddr ) +{ + uint32_t page_aligned_address = ALIGN(vaddr, 4096); + + // allocate a page at virtual address + int PageDirectoryEntryIndex = vaddr >> 22; + int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF; + + uint32_t* pageTable = (uint32_t*)(boot_page_directory[PageDirectoryEntryIndex] & 0xFFFFE000 + 0xC0000000); + + + void* physicalAddressToFree = (void*)(pageTable[PageTableEntryIndex] & 0xFFFFE000 + 0xC0000000); + free_block(physicalAddressToFree); + + pageTable[PageTableEntryIndex] = 0x0; + + +} + +void Map ( uint32_t vaddr, uint32_t paddr) +{ + uint32_t page_aligned_address = ALIGN(vaddr, 4096); + + // allocate a page at virtual address + int PageDirectoryEntryIndex = vaddr >> 22; + int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF; +} + +void Unmap(uint32_t vaddr) +{ + // NOTE: I will implement lazy unmapping for now + uint32_t page_aligned_address = ALIGN(vaddr, 4096); + + // allocate a page at virtual address + int PageDirectoryEntryIndex = vaddr >> 22; + int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF; +} diff --git a/source/kernel/Memory/VirtualMemoryManager.h b/source/kernel/memory/VirtualMemoryManager.h similarity index 87% rename from source/kernel/Memory/VirtualMemoryManager.h rename to source/kernel/memory/VirtualMemoryManager.h index 69e146e..e54e3ed 100644 --- a/source/kernel/Memory/VirtualMemoryManager.h +++ b/source/kernel/memory/VirtualMemoryManager.h @@ -1,6 +1,6 @@ #pragma once #include "PhysicalMemoryManager.h" -#include "../Terminal/kterm.h" +#include "../terminal/kterm.h" #include "../cpu.h" void AllocatePage(uint32_t v_addr ); diff --git a/source/kernel/Memory/GDT/gdt.s b/source/kernel/memory/gdt/gdt.s similarity index 100% rename from source/kernel/Memory/GDT/gdt.s rename to source/kernel/memory/gdt/gdt.s diff --git a/source/kernel/Memory/GDT/gdtc.cpp b/source/kernel/memory/gdt/gdtc.cpp similarity index 97% rename from source/kernel/Memory/GDT/gdtc.cpp rename to source/kernel/memory/gdt/gdtc.cpp index bd10a08..32b8ea3 100644 --- a/source/kernel/Memory/GDT/gdtc.cpp +++ b/source/kernel/memory/gdt/gdtc.cpp @@ -1,5 +1,5 @@ #include "gdtc.h" -#include "../../Terminal/kterm.h" +#include "../../terminal/kterm.h" #define NULL_SEGMENT 0 #define KERNEL_CODE_SEGMENT 1 diff --git a/source/kernel/Memory/GDT/gdtc.h b/source/kernel/memory/gdt/gdtc.h similarity index 100% rename from source/kernel/Memory/GDT/gdtc.h rename to source/kernel/memory/gdt/gdtc.h diff --git a/source/kernel/PreKernel/bootstructure.h b/source/kernel/prekernel/bootstructure.h similarity index 100% rename from source/kernel/PreKernel/bootstructure.h rename to source/kernel/prekernel/bootstructure.h diff --git a/source/kernel/PreKernel/multiboot.h b/source/kernel/prekernel/multiboot.h similarity index 100% rename from source/kernel/PreKernel/multiboot.h rename to source/kernel/prekernel/multiboot.h diff --git a/source/kernel/PreKernel/prekernel.cpp b/source/kernel/prekernel/prekernel.cpp similarity index 99% rename from source/kernel/PreKernel/prekernel.cpp rename to source/kernel/prekernel/prekernel.cpp index 479af04..5a28801 100644 --- a/source/kernel/PreKernel/prekernel.cpp +++ b/source/kernel/prekernel/prekernel.cpp @@ -16,6 +16,7 @@ extern "C" void prekernelSetup ( unsigned long magic, multiboot_info_t* mbi) { if (magic != MULTIBOOT_BOOTLOADER_MAGIC) { BIB->MapIsInvalid = true; + // crash return; } else{ BIB->MapIsInvalid = false; diff --git a/source/kernel/serial.h b/source/kernel/serial.h index 343184b..b17afd8 100644 --- a/source/kernel/serial.h +++ b/source/kernel/serial.h @@ -1,6 +1,6 @@ #pragma once -#include "Terminal/kterm.h" +#include "terminal/kterm.h" #include "io.h" #define PORT 0x3f8 static int init_serial() { diff --git a/source/kernel/SuperVisorTerminal/superVisorTerminal.cpp b/source/kernel/supervisorterminal/superVisorTerminal.cpp similarity index 100% rename from source/kernel/SuperVisorTerminal/superVisorTerminal.cpp rename to source/kernel/supervisorterminal/superVisorTerminal.cpp diff --git a/source/kernel/supervisorterminal/superVisorTerminal.h b/source/kernel/supervisorterminal/superVisorTerminal.h new file mode 100644 index 0000000..f262ea3 --- /dev/null +++ b/source/kernel/supervisorterminal/superVisorTerminal.h @@ -0,0 +1,8 @@ +#pragma once +#include "../terminal/kterm.h" +#include "../time.h" +#include "../drivers/pit/pit.h" +#include "../drivers/ps-2/keyboard.h" +#include "../memory/PhysicalMemoryManager.h" + +void startSuperVisorTerminal(); \ No newline at end of file diff --git a/source/kernel/Terminal/kterm.cpp b/source/kernel/terminal/kterm.cpp similarity index 100% rename from source/kernel/Terminal/kterm.cpp rename to source/kernel/terminal/kterm.cpp diff --git a/source/kernel/Terminal/kterm.h b/source/kernel/terminal/kterm.h similarity index 93% rename from source/kernel/Terminal/kterm.h rename to source/kernel/terminal/kterm.h index 915c08a..c016549 100644 --- a/source/kernel/Terminal/kterm.h +++ b/source/kernel/terminal/kterm.h @@ -3,11 +3,11 @@ #include #include -#include "../Drivers/VGA/colors.h" +#include "../drivers/vga/colors.h" #include "../io.h" extern "C"{ - #include "../Lib/string.h" + #include "../lib/string.h" } void kterm_init();