Compare commits

...

2 Commits

Author SHA1 Message Date
13e9beea79 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
2022-09-01 20:16:16 +02:00
9893a0bd17 KERNEL: Cleanup
Removing quite a few unnecessary parts.
2022-09-01 17:02:04 +02:00
55 changed files with 220 additions and 446 deletions

View File

@ -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,10 +55,10 @@ $(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
$(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
@ -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
$(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

View File

@ -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
);
}
*/

View File

@ -1,18 +0,0 @@
#pragma once
#include <stddef.h>
//#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);
*/

View File

@ -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
}

View File

@ -1,20 +0,0 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
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));

View File

@ -1,56 +0,0 @@
#pragma once
#include <stdint.h>
/*
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)

View File

@ -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

View File

@ -1,9 +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"
#include "../bootinfo.h"
void startSuperVisorTerminal(BootInfo * );

View File

@ -1,4 +1,4 @@
.include "./source/kernel/Boot/Multiboot.S"
.include "./source/kernel/boot/multiboot.s"
/*
* Allocate initial stack
*/
@ -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,11 +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/Memory/paging.s"
.include "./source/kernel/cpu.s"
.include "./source/kernel/interrupts/idt/idt.s"

View File

@ -1,8 +0,0 @@
#pragma once
#include "Memory/memoryinfo.h"
struct BootInfo{
const char* BootStructureID = "BarinkOS";
MemoryInfo* memory;
};

39
source/kernel/cpu.cpp Normal file
View File

@ -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;
}

View File

@ -1,4 +1,5 @@
#pragma once
#include <stdint.h>
/*
Based on Intel specifications.
@ -8,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 {
@ -56,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)
@ -65,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();
uint32_t GetCR3();

View File

@ -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

View File

@ -1,5 +1,5 @@
#include "pit.h"
#include "../../Terminal/kterm.h"
uint32_t pit_tick = 0;

View File

@ -1,6 +1,7 @@
#pragma once
#include <stdint.h>
#include "../../io.h"
#include "../../terminal/kterm.h"
#define PIT_DATA_0 0x40
#define PIT_DATA_1 0x41
#define PIT_DATA_2 0x42

View File

@ -1,6 +1,6 @@
#pragma once
#include <stdint.h>
#include "../../Terminal/kterm.h"
#include "../../terminal/kterm.h"
enum ScanCodeSet {
None = 0,
ScanCodeSet1 = 1,

View File

@ -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;

View File

@ -1,11 +1,11 @@
#pragma once
#include "stdint.h"
#include "stddef.h"
#include "../../Drivers/VGA/colors.h"
#include "../../Drivers/PIC/pic.h"
#include <stdint.h>
#include <stddef.h>
#include "../../drivers/vga/colors.h"
#include "../../drivers/pic/pic.h"
#include "../../Terminal/kterm.h"
#include "../../terminal/kterm.h"
extern "C" {

View File

@ -1,11 +1,6 @@
#include "kernel.h"
extern "C" void early_main()
{
/*
* Initialize terminal interface
*/
kterm_init();
initGDT();
@ -21,7 +16,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 ;
@ -68,6 +63,7 @@ extern "C" void early_main()
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,10 +87,22 @@ extern "C" void early_main()
}
printf("Starting physical memory management setup\n");
// Setup PhysicalMemoryManagement
SetupPhysicalMemoryManager(BootInfo);
// Small test!
}
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 ?
kernel_main();
}
void PhysicalMemoryAllocatorTest(){
#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);
@ -103,55 +111,6 @@ extern "C" void early_main()
void* block3 = allocate_block();
printf("Allocated addresss 3: 0x%x\n", (uint32_t)block3);
free_block(block3);
}
asm volatile("mov %cr0, %eax ");
asm volatile("or $1, %eax");
asm volatile("mov %eax, %cr0"); // re-enable protected mode ?
kernel_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
}
@ -159,10 +118,5 @@ void PhysicalMemoryAllocatorTest(){
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();
}

View File

@ -1,30 +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 "bootinfo.h"
#include "memory/PhysicalMemoryManager.h"
#include "memory/VirtualMemoryManager.h"
#include "Memory/PhysicalMemoryManager.h"
#include "Memory/memoryinfo.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;}

View File

@ -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
}

View File

@ -1,30 +1,34 @@
#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;
// 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
uint32_t memMap_size = PMMInfoBlock->max_blocks / 8;
@ -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);
}

View File

@ -1,8 +1,8 @@
#pragma once
#include <stddef.h>
#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;
};

View File

@ -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;
}

View File

@ -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 );

View File

@ -1,5 +1,5 @@
#include "gdtc.h"
#include "../../Terminal/kterm.h"
#include "../../terminal/kterm.h"
#define NULL_SEGMENT 0
#define KERNEL_CODE_SEGMENT 1

View File

@ -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;

View File

@ -1,6 +1,6 @@
#pragma once
#include "Terminal/kterm.h"
#include "terminal/kterm.h"
#include "io.h"
#define PORT 0x3f8
static int init_serial() {

View File

@ -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)
{

View File

@ -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();

View File

@ -3,11 +3,11 @@
#include <stddef.h>
#include <stdbool.h>
#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();