Improved build system
Added new entries to .gitignore Moved away from source directory as central spot for all source code
This commit is contained in:
86
kernel/memory/KernelHeap.cpp
Normal file
86
kernel/memory/KernelHeap.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include "KernelHeap.h"
|
||||
#include "VirtualMemoryManager.h"
|
||||
extern "C" const uint32_t kernel_end;
|
||||
// Size of heap metadata is 5 bytes
|
||||
struct heap_block{
|
||||
uint8_t Used;
|
||||
uint32_t Size;
|
||||
};
|
||||
|
||||
uint32_t heap_size;
|
||||
heap_block* start ;
|
||||
|
||||
void* malloc(size_t size)
|
||||
{
|
||||
//printf("Received request for %d bytes of memory\n", size);
|
||||
heap_block* current = start;
|
||||
|
||||
// look for a free block
|
||||
while(current < start + heap_size)
|
||||
{
|
||||
if(current->Size >= size && current->Used == false )
|
||||
{
|
||||
// We found a spot
|
||||
// printf("Block found!\n");
|
||||
|
||||
// Set the spot to in-use
|
||||
current->Used = true;
|
||||
|
||||
|
||||
// split the block
|
||||
//printf("Split block.\n");
|
||||
uint32_t oldSize = current->Size;
|
||||
current->Size = size;
|
||||
|
||||
heap_block* new_block = current + sizeof(heap_block) + current->Size;
|
||||
new_block->Size = oldSize - ( sizeof(heap_block) + size);
|
||||
new_block->Used = false;
|
||||
|
||||
// return the free address
|
||||
// NOTE: added an offset from the initial address to accomodate for
|
||||
// meta-data.
|
||||
return current + sizeof(heap_block);
|
||||
|
||||
}
|
||||
|
||||
current += current->Size + sizeof(heap_block);
|
||||
}
|
||||
|
||||
// If we are here we need more memory so we should
|
||||
// probably ask the VMM for more
|
||||
// TODO: ask for more memory | Extend kernel heap
|
||||
|
||||
printf("ERROR: OUT OF HEAP MEMORY CONDITION IS NOT IMPLEMENTED. HEAP NEEDS TO BE EXTENDED!\n");
|
||||
}
|
||||
|
||||
void free(void* addr)
|
||||
{
|
||||
// clear the free boolean that corresponds to this adddress
|
||||
// This should be fairly simple
|
||||
heap_block* allocatedBlock = (heap_block*)((uint32_t)addr - sizeof(heap_block));
|
||||
allocatedBlock->Used = false;
|
||||
}
|
||||
|
||||
void initHeap()
|
||||
{
|
||||
void* HEAP_ADDRESS = allocate_block();
|
||||
printf("0x%x HEAP Paddr\n", HEAP_ADDRESS);
|
||||
|
||||
Immediate_Map((uint32_t)HEAP_ADDRESS + 0xC0000000, (uint32_t)HEAP_ADDRESS );
|
||||
start = (heap_block*) ((uint32_t)HEAP_ADDRESS + 0xC0000000);
|
||||
heap_size = 4096;
|
||||
|
||||
printf("Clear heap\n");
|
||||
// Clear the heap
|
||||
printf("set at 0x%x %d bytes to zero\n", start , heap_size);
|
||||
|
||||
memset((void*)start, 0x00, heap_size /4);
|
||||
|
||||
|
||||
printf("Init first heap block\n");
|
||||
// initialzie
|
||||
start->Size = heap_size - sizeof(heap_block);
|
||||
|
||||
start->Used = false;
|
||||
|
||||
}
|
10
kernel/memory/KernelHeap.h
Normal file
10
kernel/memory/KernelHeap.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "../terminal/kterm.h"
|
||||
|
||||
void initHeap();
|
||||
|
||||
void* malloc (size_t size );
|
||||
void free(void* addr);
|
||||
|
43
kernel/memory/PageDirectory.cpp
Normal file
43
kernel/memory/PageDirectory.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#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 )
|
||||
{
|
||||
|
||||
}
|
31
kernel/memory/PageDirectory.h
Normal file
31
kernel/memory/PageDirectory.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "./memory.h"
|
||||
#include "./../terminal/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
|
||||
|
||||
};
|
115
kernel/memory/PhysicalMemoryManager.cpp
Normal file
115
kernel/memory/PhysicalMemoryManager.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
#include "./PhysicalMemoryManager.h"
|
||||
#define IS_ALIGNED(addr, align) !((addr) & ~((align) - 1))
|
||||
#define ALIGN(addr, align) (((addr) & ~((align) - 1 )) + (align))
|
||||
|
||||
const uint32_t KERNEL_OFFSET = 0xC0000000;
|
||||
|
||||
uint32_t* memoryBitMap;
|
||||
uint32_t pmmap_size;
|
||||
uint32_t max_blocks;
|
||||
int used_blocks;
|
||||
|
||||
|
||||
void SetupPhysicalMemoryManager(uint32_t mapAddress, uint32_t memorySize )
|
||||
{
|
||||
/*
|
||||
Every byte contains 8 pages
|
||||
A page is 4096 kib
|
||||
Every block (1 bit) represent an page
|
||||
*/
|
||||
|
||||
// Set the maximum number of blocks
|
||||
max_blocks = (uint32_t)memorySize / BLOCK_SIZE ;
|
||||
printf("Max Blocks: %d\n", max_blocks);
|
||||
|
||||
// Set size of the bitmap
|
||||
uint32_t bitmap_size = max_blocks / 32;
|
||||
printf("Bitmap size: %d bytes\n",bitmap_size);
|
||||
|
||||
// Set blocks used to zero
|
||||
used_blocks = max_blocks;
|
||||
|
||||
// set the address of the memory bitmap
|
||||
memoryBitMap = (uint32_t*) mapAddress;
|
||||
|
||||
// Set all places in memory as free
|
||||
memset(memoryBitMap, 0xFFFFFFFF, max_blocks / 32 );
|
||||
}
|
||||
|
||||
// NOTE: This can only give blocks of 4kb at a time!
|
||||
// We might at some point want to allocate multiple blocks at once.
|
||||
void* allocate_block() {
|
||||
uint8_t blocks_available = max_blocks - used_blocks;
|
||||
// Are there any blocks available?
|
||||
if( blocks_available <= 0)
|
||||
{
|
||||
printf("No blocks available. Blocks Delta: 0x%x\n", blocks_available);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Find 1 free block somewhere
|
||||
int free_block_index = bitmap_first_unset(memoryBitMap, max_blocks / 8 );
|
||||
|
||||
if(free_block_index == -1)
|
||||
{
|
||||
printf("Could not find a good block!\n");
|
||||
// Could not find a block
|
||||
return (void*)0xFFFF;
|
||||
}
|
||||
|
||||
if(free_block_index == 0)
|
||||
printf("Somethings wrong!!!\n");
|
||||
|
||||
// Set the block to be used!
|
||||
bitmap_unset(memoryBitMap, free_block_index);
|
||||
// Increase the used_block count!
|
||||
used_blocks++;
|
||||
printf("used blocks: 0x%x\n", used_blocks);
|
||||
// return the pointer to the physical address
|
||||
return (void*) (BLOCK_SIZE * free_block_index);
|
||||
}
|
||||
|
||||
void free_block(void* p) {
|
||||
// If it is a null pointer we don't need to do anything.
|
||||
if(p==0) {
|
||||
return;
|
||||
}
|
||||
// calculate the index into the bitmap
|
||||
int index = ((uint32_t) p) / BLOCK_SIZE;
|
||||
|
||||
// set the block to be free
|
||||
bitmap_set(memoryBitMap, index);
|
||||
used_blocks--;
|
||||
printf("used blocks: 0x%x, after free\n", used_blocks);
|
||||
|
||||
}
|
||||
|
||||
void allocate_region(uint32_t startAddress, uint32_t size) {
|
||||
// every bit should be 4KiB
|
||||
// every byte is 8*4KiB = 32KiB
|
||||
|
||||
int NumberOfBlocksToAllocate = ( size / 1024) / 4 / 8 + 1;
|
||||
int startBlock = (startAddress / 1024) / 4 / 8 ;
|
||||
|
||||
for( int i = 0; i < NumberOfBlocksToAllocate; i++)
|
||||
{
|
||||
bitmap_unset(memoryBitMap, startBlock + i);// allocate region causes #PF Exception
|
||||
used_blocks++;
|
||||
}
|
||||
}
|
||||
|
||||
void deallocate_region(uint32_t StartAddress , uint32_t size ) {
|
||||
// reverse of what happened in allocate_region
|
||||
int NumberOfBlocks = (size / 1024) / 4 / 8 + 1;
|
||||
int startBlock = (StartAddress / 1024) / 4 / 8;
|
||||
|
||||
for(int i = 0; i < NumberOfBlocks; i++)
|
||||
{
|
||||
bitmap_set(memoryBitMap, startBlock + i);
|
||||
used_blocks --;
|
||||
}
|
||||
}
|
||||
|
||||
int GetUsedBlocks (){
|
||||
return used_blocks;
|
||||
}
|
20
kernel/memory/PhysicalMemoryManager.h
Normal file
20
kernel/memory/PhysicalMemoryManager.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <CoreLib/Memory.h>
|
||||
|
||||
#include "../prekernel/bootstructure.h"
|
||||
#include "../terminal/kterm.h"
|
||||
#include "../bitmap.h"
|
||||
|
||||
#define BLOCK_SIZE 4092
|
||||
|
||||
|
||||
void SetupPhysicalMemoryManager(uint32_t mapAddress, uint32_t memorySize);
|
||||
|
||||
void* allocate_block();
|
||||
void free_block(void* ptr);
|
||||
|
||||
void allocate_region(uint32_t address, uint32_t size);
|
||||
void deallocate_region(uint32_t address, uint32_t size);
|
||||
|
||||
int GetUsedBlocks();
|
60
kernel/memory/TaskStateSegment.h
Normal file
60
kernel/memory/TaskStateSegment.h
Normal file
@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
#include "gdt/gdtc.h"
|
||||
#include <CoreLib/Memory.h>
|
||||
|
||||
struct TaskStateSegment {
|
||||
uint32_t prev_tss;
|
||||
uint32_t esp0;
|
||||
uint32_t ss0;
|
||||
// everythinge else is unused
|
||||
uint32_t esp1;
|
||||
uint32_t ss1;
|
||||
uint32_t esp2;
|
||||
uint32_t ss2;
|
||||
uint32_t cr3;
|
||||
uint32_t eip;
|
||||
uint32_t eflags;
|
||||
uint32_t eax;
|
||||
uint32_t ecx;
|
||||
uint32_t edx;
|
||||
uint32_t ebx;
|
||||
uint32_t esp;
|
||||
uint32_t ebp;
|
||||
uint32_t esi;
|
||||
uint32_t edi;
|
||||
uint32_t es;
|
||||
uint32_t cs;
|
||||
uint32_t ss;
|
||||
uint32_t ds;
|
||||
uint32_t fs;
|
||||
uint32_t gs;
|
||||
uint32_t ldt;
|
||||
uint16_t trap;
|
||||
uint16_t iomap_base;
|
||||
}__attribute__((packed));
|
||||
|
||||
|
||||
TaskStateSegment tss0 ={};
|
||||
|
||||
inline void flush_tss()
|
||||
{
|
||||
asm volatile("mov $0x2B, %ax ; ltr %ax");
|
||||
}
|
||||
|
||||
void setup_tss(){
|
||||
// ensure the tss is zero'd
|
||||
memset((void*)&tss0, 0, sizeof(tss0));
|
||||
tss0.ss0 = (uint32_t) &GlobalDescriptorTable[KERNEL_DATA_SEGMENT];
|
||||
|
||||
extern uint32_t stack_top;
|
||||
tss0.esp0 = (unsigned long)&stack_top;
|
||||
|
||||
// Task Segment Descriptor
|
||||
|
||||
uint32_t address = (unsigned long) &tss0;
|
||||
uint32_t size = sizeof(tss0);
|
||||
uint32_t limit = (address + size );
|
||||
|
||||
add_descriptor(TASK_STATE_SEGMENT, address, limit- 1, 0xE9, 0x0);
|
||||
}
|
||||
|
126
kernel/memory/VirtualMemoryManager.cpp
Normal file
126
kernel/memory/VirtualMemoryManager.cpp
Normal file
@ -0,0 +1,126 @@
|
||||
#include "VirtualMemoryManager.h"
|
||||
#define ALIGN(addr, align) (((addr) & ~((align) - 1 )) + (align))
|
||||
|
||||
extern uint32_t boot_page_directory[1024] ; // points to the wrong location
|
||||
|
||||
extern uint32_t boot_page_table[1024];
|
||||
|
||||
|
||||
void flush_cr3(){
|
||||
asm volatile("movl %cr3, %ecx;"
|
||||
"movl %ecx, %cr3");
|
||||
}
|
||||
|
||||
|
||||
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 )
|
||||
{
|
||||
printf("Directory entry is marked as present\n");
|
||||
uint32_t* page_table = (uint32_t*)((boot_page_directory[PageDirectoryEntryIndex]) & 0xFFFFE000) ;
|
||||
//page_table = (uint32_t*) ((uint32_t)page_table + 0xC0000000); // Add kernel offset
|
||||
printf("Page table address: 0x%x\n", (uint32_t)&page_table);
|
||||
|
||||
// check if the page table entry is marked as present
|
||||
if ( page_table[PageTableEntryIndex] & 0x1 )
|
||||
{
|
||||
printf("page already present!\n");
|
||||
} else{
|
||||
printf("Mapping a physical page.\n");
|
||||
// Map the entry to a physical page
|
||||
page_table[PageTableEntryIndex] = (uint32_t)allocate_block() | 0x3;
|
||||
}
|
||||
|
||||
} else {
|
||||
printf("Mapping a new page directory entry with a page table\n");
|
||||
// mark the page table as present and allocate a physical block for it
|
||||
boot_page_directory[PageDirectoryEntryIndex] = (uint32_t)allocate_block() | 0x3;
|
||||
|
||||
}
|
||||
|
||||
asm ("cli; invlpg (%0); sti" :: "r" (vaddr) : "memory" );
|
||||
|
||||
|
||||
}
|
||||
|
||||
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 Immediate_Map ( uint32_t vaddr, uint32_t paddr)
|
||||
{
|
||||
printf("map 0x%x to 0x%x\n", paddr, vaddr);
|
||||
// allocate a page at virtual address
|
||||
int PageDirectoryEntryIndex = vaddr >> 22;
|
||||
int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF;
|
||||
|
||||
printf("Map address at PDE 0x%x PTE 0x%x\n", PageDirectoryEntryIndex, PageTableEntryIndex);
|
||||
|
||||
if ((boot_page_directory - 0xC0000000)[PageDirectoryEntryIndex] & 0x1 )
|
||||
{
|
||||
printf("Directory entry is marked as present\n");
|
||||
|
||||
} else {
|
||||
printf("Mapping a new page directory entry with a page table\n");
|
||||
// mark the page table as present and allocate a physical block for it
|
||||
|
||||
void* new_page_dir = allocate_block();
|
||||
printf("New page directory address 0x%x\n", new_page_dir);
|
||||
boot_page_directory[PageDirectoryEntryIndex] = (uint32_t)new_page_dir | 0x3;
|
||||
|
||||
}
|
||||
|
||||
printf("PDE found at : 0x%x\n", (uint32_t) &boot_page_directory[PageDirectoryEntryIndex]);
|
||||
uint32_t* page_table = (uint32_t*)(boot_page_directory[PageDirectoryEntryIndex] & 0xFFFFE000) ;
|
||||
//page_table = (uint32_t*) ((uint32_t)page_table - 0xC0000000); // remove kernel offset
|
||||
printf("Page table address: 0x%x\n", (uint32_t)page_table);
|
||||
|
||||
// check if the page table entry is marked as present
|
||||
if ( page_table[PageTableEntryIndex] & 0x1 )
|
||||
{
|
||||
printf("page already present!\n");
|
||||
printf("Entry found at addr: 0x%x\n", &(page_table[PageTableEntryIndex]));
|
||||
} else{
|
||||
printf("Mapping a physical page.\n");
|
||||
// Map the entry to a physical page
|
||||
page_table[PageTableEntryIndex] = (uint32_t)(paddr | 0x3);
|
||||
}
|
||||
|
||||
|
||||
asm ("cli; invlpg (%0); sti" :: "r" (vaddr) : "memory" );
|
||||
}
|
||||
|
||||
void Immediate_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;
|
||||
}
|
16
kernel/memory/VirtualMemoryManager.h
Normal file
16
kernel/memory/VirtualMemoryManager.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include "../terminal/kterm.h"
|
||||
#include "../i386/processor.h"
|
||||
#include "PhysicalMemoryManager.h"
|
||||
|
||||
|
||||
void SetupVMM();
|
||||
|
||||
void AllocatePage(uint32_t v_addr );
|
||||
void FreePage(uint32_t v_addr);
|
||||
|
||||
void Immediate_Map(uint32_t vaddr, uint32_t paddr);
|
||||
void Immediate_Unmap (uint32_t v_addr);
|
||||
|
||||
// void Demand_map(uint32_t p_addr, uint32_t v_addr);
|
||||
// void Demand_Unmap (uint32_t v_addr);
|
19
kernel/memory/gdt/gdt.s
Normal file
19
kernel/memory/gdt/gdt.s
Normal file
@ -0,0 +1,19 @@
|
||||
.global LoadGlobalDescriptorTable
|
||||
|
||||
LoadGlobalDescriptorTable:
|
||||
lgdt gdtDescriptor
|
||||
|
||||
movw $16, %ax
|
||||
movw %ax, %ds
|
||||
movw %ax, %es
|
||||
movw %ax, %fs
|
||||
movw %ax, %gs
|
||||
movw %ax, %ss
|
||||
|
||||
jmp $8,$flush
|
||||
|
||||
flush:
|
||||
ret
|
||||
|
||||
|
||||
|
46
kernel/memory/gdt/gdtc.cpp
Normal file
46
kernel/memory/gdt/gdtc.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include "gdtc.h"
|
||||
#include "../../terminal/kterm.h"
|
||||
|
||||
|
||||
SegmentDescriptor GlobalDescriptorTable[6];
|
||||
GlobalDescriptorTableDescriptor gdtDescriptor;
|
||||
|
||||
|
||||
void add_descriptor(int which , unsigned long base, unsigned long limit, unsigned char access, unsigned char granularity ){
|
||||
GlobalDescriptorTable[which].base_low = (base & 0xFFFF );
|
||||
GlobalDescriptorTable[which].base_middle = (base >> 6) & 0xFF;
|
||||
GlobalDescriptorTable[which].base_high = (base >> 24) & 0xFF;
|
||||
|
||||
GlobalDescriptorTable[which].limit_low = (limit & 0xFFFF);
|
||||
GlobalDescriptorTable[which].granularity = ((limit >> 16) & 0x0F);
|
||||
|
||||
GlobalDescriptorTable[which].granularity |= (granularity & 0xF0);
|
||||
GlobalDescriptorTable[which].access = access;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void initGDT(){
|
||||
|
||||
// NULL segment
|
||||
add_descriptor(NULL_SEGMENT, 0,0,0,0);
|
||||
|
||||
// Kernel Code Segment
|
||||
add_descriptor(KERNEL_CODE_SEGMENT, 0, 0xFFFFFFFF, 0x9A, 0xCF);
|
||||
|
||||
// Kernel Data Segment
|
||||
add_descriptor(KERNEL_DATA_SEGMENT, 0, 0xFFFFFFFF, 0x92, 0xCF);
|
||||
|
||||
// User Code Segment
|
||||
add_descriptor(USER_CODE_SEGMENT, 0, 0xFFFFFFFF, 0xFA, 0xCF);
|
||||
|
||||
// User Data Segement
|
||||
add_descriptor(USER_DATA_SEGMENT, 0, 0xFFFFFFFF, 0xF2, 0xCF);
|
||||
|
||||
// init Gdt Descriptor
|
||||
gdtDescriptor.limit = ((sizeof(SegmentDescriptor ) * 6 ) - 1);
|
||||
gdtDescriptor.base = (unsigned int) (&GlobalDescriptorTable);
|
||||
|
||||
}
|
35
kernel/memory/gdt/gdtc.h
Normal file
35
kernel/memory/gdt/gdtc.h
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#define NULL_SEGMENT 0
|
||||
#define KERNEL_CODE_SEGMENT 1
|
||||
#define KERNEL_DATA_SEGMENT 2
|
||||
#define USER_CODE_SEGMENT 3
|
||||
#define USER_DATA_SEGMENT 4
|
||||
#define TASK_STATE_SEGMENT 5
|
||||
|
||||
|
||||
|
||||
struct SegmentDescriptor {
|
||||
unsigned short limit_low;
|
||||
unsigned short base_low;
|
||||
unsigned char base_middle;
|
||||
unsigned char access;
|
||||
unsigned char granularity;
|
||||
unsigned char base_high;
|
||||
}__attribute__((packed));
|
||||
|
||||
extern SegmentDescriptor GlobalDescriptorTable[6];
|
||||
|
||||
struct GlobalDescriptorTableDescriptor{
|
||||
unsigned short limit;
|
||||
unsigned int base;
|
||||
}__attribute__((packed)) ;
|
||||
|
||||
|
||||
|
||||
void add_descriptor(int which , unsigned long base, unsigned long limit, unsigned char access, unsigned char granularity );
|
||||
|
||||
|
||||
void initGDT();
|
142
kernel/memory/memory.cpp
Normal file
142
kernel/memory/memory.cpp
Normal file
@ -0,0 +1,142 @@
|
||||
#include "./memory.h"
|
||||
uint32_t* memoryBitMap;
|
||||
/*
|
||||
|
||||
*/
|
||||
void PhysicalMemory::setup( MemoryInfo* memory) {
|
||||
|
||||
// calculate the maximum number of blocks
|
||||
max_blocks = KB_TO_BLOCKS(memory->TotalMemory);
|
||||
|
||||
used_blocks = 0;
|
||||
|
||||
memoryBitMap = (uint32_t*) 0x00a00000;
|
||||
|
||||
|
||||
printf("Maximum Number of blocks: 0x%x, Number of bytes for memMap: 0x%x\n", max_blocks , (max_blocks/8));
|
||||
|
||||
//Size of memory map
|
||||
uint32_t memMap_size = (max_blocks / 8 ) ;
|
||||
|
||||
printf("Memory Map size: 0x%x\n", memMap_size );
|
||||
printf("size of int in bytes: 0x%x \n" , sizeof(int));
|
||||
|
||||
// Set all places in memory as free
|
||||
memset(memoryBitMap, 0xFF, memMap_size );
|
||||
}
|
||||
|
||||
// NOTE: this can only give blocks of 4kb at a time!
|
||||
void* PhysicalMemory::allocate_block() {
|
||||
uint8_t blocks_available = max_blocks - used_blocks;
|
||||
// Are there any blocks available?
|
||||
if( blocks_available <= 0)
|
||||
{
|
||||
printf("No blocks available. Blocks Delta: 0x%x\n", blocks_available);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Find 1 free block somewhere
|
||||
int free_block_index = bitmap_first_unset(memoryBitMap, (max_blocks /8) /*memMap Size*/ );
|
||||
|
||||
|
||||
|
||||
if(free_block_index == -1)
|
||||
{
|
||||
printf("Could not find a good block!\n");
|
||||
// Could not find a block
|
||||
return (void*)0xFFFF;
|
||||
}
|
||||
|
||||
if(free_block_index == 0)
|
||||
printf("Somethings wrong!!!\n");
|
||||
|
||||
// Set the block to be used!
|
||||
bitmap_unset(memoryBitMap, free_block_index);
|
||||
// Increase the used_block count!
|
||||
used_blocks++;
|
||||
printf("used blocks: 0x%x\n", used_blocks);
|
||||
// return the pointer to the physical address
|
||||
return (void*) (BLOCK_SIZE * free_block_index);
|
||||
}
|
||||
|
||||
|
||||
void PhysicalMemory::free_block(void* p) {
|
||||
// If it is a null pointer we don't need to do anything.
|
||||
if(p==0) {
|
||||
return;
|
||||
}
|
||||
// calculate the index into the bitmap
|
||||
int index = ((uint32_t) p) / BLOCK_SIZE;
|
||||
|
||||
// set the block to be free
|
||||
bitmap_set(memoryBitMap, index);
|
||||
used_blocks--;
|
||||
printf("used blocks: 0x%x, after free\n", used_blocks);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PhysicalMemory::allocate_region(uint32_t startAddress, uint32_t size) {
|
||||
// every bit should be 4KiB
|
||||
// every byte is 8*4KiB = 32KiB
|
||||
|
||||
int NumberOfBlocksToAllocate = ( size / 1024) / 4 / 8 + 1;
|
||||
int startBlock = (startAddress / 1024) / 4 / 8 ;
|
||||
|
||||
// printf("NumberOfBlocksToAllocate: 0x%x\n", NumberOfBlocksToAllocate);
|
||||
//printf( "start block: 0x%x\n" , startBlock);
|
||||
for( int i = 0; i < NumberOfBlocksToAllocate; i++)
|
||||
{
|
||||
|
||||
//printf("ALLOCATE BLOCK: 0x%x\n" , startBlock + i );
|
||||
bitmap_unset(memoryBitMap, startBlock+ i);
|
||||
used_blocks++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
void PhysicalMemory::deallocate_region(uint32_t StartAddress , uint32_t size ) {
|
||||
// NOT IMPLEMENTED YET
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
|
46
kernel/memory/memory.h
Normal file
46
kernel/memory/memory.h
Normal file
@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "memoryinfo.h"
|
||||
#include "../prekernel/multiboot.h"
|
||||
#include "../terminal/kterm.h"
|
||||
#include <CoreLib/Memory.h>
|
||||
|
||||
#include "../bitmap.h"
|
||||
|
||||
#define BLOCK_SIZE 4092
|
||||
#define BLOCKS_PER_WORD 32 // A word is 16 bit in x86 machines according to my google search results!
|
||||
|
||||
#define KB_TO_BLOCKS(x) (x / BLOCK_SIZE)
|
||||
#define IS_ALIGNED(addr, align) !((addr) & ~((align) - 1))
|
||||
#define ALIGN(addr, align) (((addr) & ~((align) - 1 )) + (align))
|
||||
|
||||
void initialise_available_regions(uint32_t memoryMapAddr, uint32_t memoryMapLastAddr, uint32_t* memoryBitMap, int* used_blocks);
|
||||
|
||||
extern uint32_t* memoryBitMap;
|
||||
|
||||
class PhysicalMemory
|
||||
{
|
||||
public:
|
||||
void setup(MemoryInfo* memory);
|
||||
void destroy();
|
||||
void free_block(void* ptr);
|
||||
void* allocate_block();
|
||||
void allocate_region(uint32_t, uint32_t);
|
||||
void deallocate_region(uint32_t , uint32_t );
|
||||
|
||||
private:
|
||||
size_t pmmap_size;
|
||||
size_t max_blocks;
|
||||
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);
|
20
kernel/memory/memoryinfo.h
Normal file
20
kernel/memory/memoryinfo.h
Normal file
@ -0,0 +1,20 @@
|
||||
#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));
|
||||
|
||||
|
||||
|
20
kernel/memory/paging.s
Normal file
20
kernel/memory/paging.s
Normal file
@ -0,0 +1,20 @@
|
||||
.globl enablePaging
|
||||
enablePaging:
|
||||
push %ebp
|
||||
mov %esp, %ebp
|
||||
mov %cr0, %eax
|
||||
or $0x80000000, %eax
|
||||
mov %eax, %cr0
|
||||
mov %ebp, %esp
|
||||
pop %ebp
|
||||
ret
|
||||
|
||||
.globl loadPageDirectory
|
||||
loadPageDirectory:
|
||||
push %ebp
|
||||
mov %esp, %ebp
|
||||
mov 8(%esp), %eax
|
||||
mov %eax, %cr3
|
||||
mov %ebp, %esp
|
||||
pop %ebp
|
||||
ret
|
Reference in New Issue
Block a user