Merge into main the new state of the operating system/kernel #1
							
								
								
									
										6
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								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)/io.o $(BUILD_DIR)/MMU.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/string.o
 | 
			
		||||
OFILES =	$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/io.o $(BUILD_DIR)/PageDirectory.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/string.o
 | 
			
		||||
 | 
			
		||||
SRC_DIR = src
 | 
			
		||||
BUILD_DIR = build
 | 
			
		||||
@ -71,8 +71,8 @@ $(BUILD_DIR)/crtn.o:
 | 
			
		||||
$(BUILD_DIR)/io.o:
 | 
			
		||||
		$(CPP) -c $(SRC_DIR)/kernel/io.cpp  -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/MMU.o:
 | 
			
		||||
	$(CPP) -c $(SRC_DIR)/kernel/MMU.cpp -o $(BUILD_DIR)/MMU.o $(CFLAGS) -fno-exceptions -fno-rtti 
 | 
			
		||||
$(BUILD_DIR)/PageDirectory.o:
 | 
			
		||||
	$(CPP) -c $(SRC_DIR)/kernel/memory/PageDirectory.cpp -o $(BUILD_DIR)/PageDirectory.o $(CFLAGS) -fno-exceptions -fno-rtti 
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/idt.o:
 | 
			
		||||
	$(CPP) -c $(SRC_DIR)/kernel/arch/i386/idt/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,5 @@
 | 
			
		||||
#pragma once 
 | 
			
		||||
#include "multiboot.h"
 | 
			
		||||
#include "bootloader/multiboot.h"
 | 
			
		||||
#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))
 | 
			
		||||
 | 
			
		||||
extern "C" {
 | 
			
		||||
 | 
			
		||||
@ -1,16 +1,13 @@
 | 
			
		||||
#pragma once 
 | 
			
		||||
extern "C" {
 | 
			
		||||
 | 
			
		||||
    #include "arch/i386/tty/kterm.h"
 | 
			
		||||
    #include "pmm.h"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "../libc/include/string.h"
 | 
			
		||||
#include "multiboot.h"
 | 
			
		||||
#include "./bootloader/multiboot.h"
 | 
			
		||||
#include "bootcheck.h"
 | 
			
		||||
#include "arch/i386/idt/idt.h"
 | 
			
		||||
 | 
			
		||||
#include "MMU.h"
 | 
			
		||||
#include "io.h"
 | 
			
		||||
#include "time.h"
 | 
			
		||||
#include "cpu.h"
 | 
			
		||||
 | 
			
		||||
@ -1,9 +1,9 @@
 | 
			
		||||
#include "MMU.h"
 | 
			
		||||
#include "PageDirectory.h"
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void MMU::enable(){
 | 
			
		||||
void PageDirectory::enable(){
 | 
			
		||||
 | 
			
		||||
    //set each entry to not present
 | 
			
		||||
    int i;
 | 
			
		||||
@ -18,7 +18,7 @@ struct page_table_entry{};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MMU {
 | 
			
		||||
class PageDirectory {
 | 
			
		||||
    public:
 | 
			
		||||
    void enable ();
 | 
			
		||||
    
 | 
			
		||||
							
								
								
									
										1
									
								
								src/kernel/memory/PageFramAllocator.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/kernel/memory/PageFramAllocator.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
#include "PageFrameAllocator.h"
 | 
			
		||||
							
								
								
									
										8
									
								
								src/kernel/memory/PageFrameAllocator.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/kernel/memory/PageFrameAllocator.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
extern void *kernel_begin;
 | 
			
		||||
extern void *kernel_end;
 | 
			
		||||
 | 
			
		||||
@ -1 +0,0 @@
 | 
			
		||||
#include "pmm.h"
 | 
			
		||||
							
								
								
									
										125
									
								
								src/kernel/pmm.h
									
									
									
									
									
								
							
							
						
						
									
										125
									
								
								src/kernel/pmm.h
									
									
									
									
									
								
							@ -1,125 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
// Lets assume we have 2 gigabytes of memory 
 | 
			
		||||
// NOTE: We should really detect how much memory we have
 | 
			
		||||
#define KiloByte 1024 // bytes
 | 
			
		||||
#define MegaByte 1048576 // bytes
 | 
			
		||||
#define GigaByte 1073741824 // bytes
 | 
			
		||||
#define MemorySize 2147483648 // bytes
 | 
			
		||||
 | 
			
		||||
const uint32_t bitmapSize = MemorySize / 8;
 | 
			
		||||
 | 
			
		||||
extern void *kernel_begin;
 | 
			
		||||
extern void *kernel_end;
 | 
			
		||||
struct __attribute__((packed))  PMSegment {
 | 
			
		||||
    void* address;
 | 
			
		||||
    uint32_t size; 
 | 
			
		||||
    PMSegment* Next;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static PMSegment pmStart;
 | 
			
		||||
static uint32_t AvailablePhysicalMemory;  
 | 
			
		||||
static uint32_t AllocatedMemorySize;
 | 
			
		||||
 | 
			
		||||
void initPhysicalMemoryManager(){
 | 
			
		||||
    AvailablePhysicalMemory = MemorySize;
 | 
			
		||||
    AllocatedMemorySize = 0;
 | 
			
		||||
 | 
			
		||||
    // Map the kernel
 | 
			
		||||
    PMSegment KernelSegment = PMSegment();
 | 
			
		||||
    printf("end of kernel: 0x%x\n", &kernel_end);
 | 
			
		||||
    printf("start of kernel: 0x%x\n", &kernel_begin);
 | 
			
		||||
    printf("pointer to kernel: 0x%x\n", &KernelSegment);
 | 
			
		||||
 | 
			
		||||
    pmStart = KernelSegment;
 | 
			
		||||
    KernelSegment.address = kernel_begin;
 | 
			
		||||
    KernelSegment.size = &kernel_end - &kernel_begin;
 | 
			
		||||
    AllocatedMemorySize += KernelSegment.size;
 | 
			
		||||
    KernelSegment.Next = 0;
 | 
			
		||||
 | 
			
		||||
    // make sure We allocate space for ourselves 
 | 
			
		||||
    /*PMSegment start =  PMSegment();
 | 
			
		||||
    start.address = KernelSegment.address + KernelSegment.size ;
 | 
			
		||||
    start.size = (MemorySize /  sizeof(PMSegment) ) +  sizeof (uint32_t) * 2;
 | 
			
		||||
    */
 | 
			
		||||
 | 
			
		||||
    //KernelSegment.Next = &start;
 | 
			
		||||
    //AllocatedMemorySize += start.size;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void printMemorySegments() 
 | 
			
		||||
{
 | 
			
		||||
    printf("Memory Segments:\n");
 | 
			
		||||
    printf( "Start Segment: [addr: 0x%x, size: 0x%x, Next: 0x%x]\n" ,pmStart.address, pmStart.size, pmStart.Next);
 | 
			
		||||
    printf("----------------------------\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void pmem_free (void* address){
 | 
			
		||||
    
 | 
			
		||||
    PMSegment* before = 0;
 | 
			
		||||
    PMSegment* current = &pmStart;
 | 
			
		||||
 | 
			
		||||
    printf("Address of pmStart : 0x%x\n", pmStart);
 | 
			
		||||
    printf("Looking for segment with address: 0x%x \n", address );
 | 
			
		||||
    printf("Address of pmStart(a.k.a current) : 0x%x \n", current);
 | 
			
		||||
    while( current ) 
 | 
			
		||||
    {
 | 
			
		||||
        //printf("address of current segment 0x%x\n", current->address );
 | 
			
		||||
        if ( current->address == address ) {
 | 
			
		||||
            // this is the address we want to free
 | 
			
		||||
            printf("Segment found!! Segment address: 0x%x \n", current->address);
 | 
			
		||||
            if ( current->Next && before ){
 | 
			
		||||
                before->Next = current->Next;
 | 
			
		||||
            }else{
 | 
			
		||||
                before->Next = 0;
 | 
			
		||||
            }
 | 
			
		||||
            // TODO: Clean memory [ Something like memset to zeroes]  
 | 
			
		||||
            printf("Removing segment of size: 0x%x\n", current->size);
 | 
			
		||||
            AllocatedMemorySize -= current->size;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        before = current;
 | 
			
		||||
        current = current->Next;
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void* pmem_alloc ( uint32_t size ){
 | 
			
		||||
    // Get the last segment
 | 
			
		||||
    PMSegment* lastSegment = &pmStart;
 | 
			
		||||
 | 
			
		||||
    while (lastSegment ) {
 | 
			
		||||
        if( lastSegment->Next == 0){
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        lastSegment = lastSegment->Next;
 | 
			
		||||
    }   
 | 
			
		||||
 | 
			
		||||
    printf("LastSegment is for address: 0x%x\n", lastSegment->address);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // So now we have the last segment available
 | 
			
		||||
    PMSegment newSegment =  PMSegment();
 | 
			
		||||
    newSegment.address = (PMSegment*)((uint32_t)lastSegment->address + lastSegment->size +1);
 | 
			
		||||
 | 
			
		||||
    printf("NewSegment for Address: 0x%x\n", newSegment.address);
 | 
			
		||||
 | 
			
		||||
    newSegment.size = size;
 | 
			
		||||
 | 
			
		||||
    lastSegment->Next = &newSegment;
 | 
			
		||||
 | 
			
		||||
    if ( AllocatedMemorySize + newSegment.size > AvailablePhysicalMemory){
 | 
			
		||||
        // No segment available of this size
 | 
			
		||||
        /// ERROR!!
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    AllocatedMemorySize += newSegment.size;
 | 
			
		||||
 | 
			
		||||
    return newSegment.address;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user