Merge into main the new state of the operating system/kernel #1
							
								
								
									
										35
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								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 $(BUILD_DIR)/cpu.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 $(BUILD_DIR)/KHeap.o
 | 
			
		||||
 | 
			
		||||
SRC_DIR = source
 | 
			
		||||
BUILD_DIR = build
 | 
			
		||||
@ -51,24 +51,15 @@ build_x86_64:
 | 
			
		||||
clean: 
 | 
			
		||||
	rm -f $(BUILD_DIR)/myos.bin $(INTERNAL_OBJS)
 | 
			
		||||
 | 
			
		||||
# C++ definition -> Object files
 | 
			
		||||
$(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
 | 
			
		||||
 | 
			
		||||
$(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
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/crtn.o:
 | 
			
		||||
	$(AS) $(SRC_DIR)/kernel/crtn.s -o $(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
 | 
			
		||||
 | 
			
		||||
	$(CPP) -c $(SRC_DIR)/kernel/io.cpp  -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/idt.o:
 | 
			
		||||
	$(CPP) -c $(SRC_DIR)/kernel/interrupts/idt/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti
 | 
			
		||||
@ -76,22 +67,18 @@ $(BUILD_DIR)/idt.o:
 | 
			
		||||
$(BUILD_DIR)/gdtc.o:
 | 
			
		||||
	$(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
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/string.o:
 | 
			
		||||
	$(CC) -c $(SRC_DIR)/kernel/lib/string.c  -o $(BUILD_DIR)/string.o $(CFLAGS) -std=gnu99
 | 
			
		||||
 | 
			
		||||
	$(CPP) -c $(SRC_DIR)/kernel/lib/string.cpp  -o $(BUILD_DIR)/string.o $(CFLAGS) -fno-exceptions -fno-rtti
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/pit.o:
 | 
			
		||||
	$(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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/time.o:
 | 
			
		||||
	$(CPP) -c $(SRC_DIR)/kernel/time.cpp  -o $(BUILD_DIR)/time.o $(CFLAGS) -fno-exceptions -fno-rtti
 | 
			
		||||
 | 
			
		||||
@ -104,8 +91,22 @@ $(BUILD_DIR)/memory.o:
 | 
			
		||||
$(BUILD_DIR)/paging.o:
 | 
			
		||||
	$(CPP) -c $(SRC_DIR)/kernel/memory/VirtualMemoryManager.cpp -o $(BUILD_DIR)/paging.o $(CFLAGS) -fno-exceptions -fno-rtti
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/KHeap.o:
 | 
			
		||||
	$(CPP) -c $(SRC_DIR)/kernel/memory/KernelHeap.cpp -o $(BUILD_DIR)/KHeap.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
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/cpu.o:
 | 
			
		||||
	$(CPP) -c $(SRC_DIR)/kernel/cpu.cpp -o $(BUILD_DIR)/cpu.o $(CFLAGS) -fno-exceptions -fno-rtti
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Assembly -> Object files
 | 
			
		||||
$(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
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/crtn.o:
 | 
			
		||||
	$(AS) $(SRC_DIR)/kernel/crtn.s -o $(BUILD_DIR)/crtn.o
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										15
									
								
								source/kernel-test/PhysicalMemoryManagerTest.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								source/kernel-test/PhysicalMemoryManagerTest.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,15 @@
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
        free_block(block);
 | 
			
		||||
        free_block(block2);
 | 
			
		||||
        void* block3 = allocate_block();
 | 
			
		||||
        printf("Allocated addresss 3: 0x%x\n", (uint32_t)block3);
 | 
			
		||||
        free_block(block3);
 | 
			
		||||
    #endif
 | 
			
		||||
}
 | 
			
		||||
@ -83,7 +83,7 @@ _start:
 | 
			
		||||
	# At this point, paging is fully set up and enabled
 | 
			
		||||
isPaging:
 | 
			
		||||
	# Unmap the identity mapping as it is now unnecessary  
 | 
			
		||||
	movl $0, boot_page_directory + 0
 | 
			
		||||
	//movl $0, boot_page_directory + 0
 | 
			
		||||
	
 | 
			
		||||
	# Reload cr3 to force tlb flush 
 | 
			
		||||
	movl %cr3, %ecx
 | 
			
		||||
 | 
			
		||||
@ -1,122 +1,107 @@
 | 
			
		||||
#include "kernel.h"
 | 
			
		||||
extern "C" void early_main()
 | 
			
		||||
#include "lib/string.h" 
 | 
			
		||||
#include "definitions.h"
 | 
			
		||||
 | 
			
		||||
#include "prekernel/bootstructure.h"
 | 
			
		||||
 | 
			
		||||
#include "drivers/vga/VBE.h"
 | 
			
		||||
#include "drivers/pit/pit.h"
 | 
			
		||||
 | 
			
		||||
#include "memory/PhysicalMemoryManager.h"
 | 
			
		||||
#include "memory/VirtualMemoryManager.h"
 | 
			
		||||
#include "memory/KernelHeap.h"
 | 
			
		||||
#include "memory/gdt/gdtc.h"
 | 
			
		||||
 | 
			
		||||
#include "interrupts/idt/idt.h"
 | 
			
		||||
 | 
			
		||||
#include "io.h"
 | 
			
		||||
#include "cpu.h"
 | 
			
		||||
#include "serial.h"
 | 
			
		||||
#include "time.h"
 | 
			
		||||
 | 
			
		||||
#include "terminal/kterm.h"
 | 
			
		||||
 | 
			
		||||
#include "supervisorterminal/superVisorTerminal.h"
 | 
			
		||||
 | 
			
		||||
extern "C" void kernel_main ();
 | 
			
		||||
void ProcessBootInfo();
 | 
			
		||||
 | 
			
		||||
extern "C" void kernel_main ()
 | 
			
		||||
{
 | 
			
		||||
    kterm_init();
 | 
			
		||||
    initGDT();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    init_serial();
 | 
			
		||||
    print_serial("Hello Higher half kernel!\n");
 | 
			
		||||
 
 | 
			
		||||
    init_idt();
 | 
			
		||||
    // Enable interrupts
 | 
			
		||||
    asm volatile("STI");
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * Show a little banner for cuteness
 | 
			
		||||
     */
 | 
			
		||||
    printf("|===    BarinkOS       ===|\n");
 | 
			
		||||
    printf("Kernel End Addr: 0x%x\n" , &kernel_end );
 | 
			
		||||
 | 
			
		||||
    uint32_t PageDirectoryEntryIndex = ((uint32_t)&kernel_end + KERNEL_BASE_ADDR ) >> 22 ;
 | 
			
		||||
 | 
			
		||||
    uint32_t PageTableEntryIndex = (((uint32_t)&kernel_end + KERNEL_BASE_ADDR) >> 12) & 0x1FFF;
 | 
			
		||||
    
 | 
			
		||||
    printf("Kernel End PDE: %d, PTE: %d\n" , PageDirectoryEntryIndex, PageTableEntryIndex);
 | 
			
		||||
 | 
			
		||||
    uint32_t BootInfoStruct = BootInfoBlock_pptr + 0xC0000000;
 | 
			
		||||
    printf("Addr BootInfostruct: 0x%x\n", BootInfoStruct);
 | 
			
		||||
 | 
			
		||||
    uint32_t PageDirectoryEntryIndex2 = (BootInfoStruct  ) >> 2 ;
 | 
			
		||||
 | 
			
		||||
    uint32_t PageTableEntryIndex2 = (BootInfoStruct >> 12) & 0x1FFF;
 | 
			
		||||
 | 
			
		||||
    printf("PDE: 0x%x, PTE: 0x%x\n",  PageDirectoryEntryIndex2 , PageTableEntryIndex2 );
 | 
			
		||||
 | 
			
		||||
    BootInfoBlock* BootInfo = (BootInfoBlock*) ( BootInfoBlock_pptr + 0xC0000000 );
 | 
			
		||||
    startSuperVisorTerminal();
 | 
			
		||||
}   
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    printf("Size of BootInfoBlock: %d bytes\n", sizeof(BootInfoBlock));
 | 
			
		||||
    printf("Bootloader information:\n");
 | 
			
		||||
    if( BootInfo->ValidELFHeader )
 | 
			
		||||
    {
 | 
			
		||||
        printf("- Valid ELF Header is available!\n");
 | 
			
		||||
    }
 | 
			
		||||
extern "C" void early_main()
 | 
			
		||||
{
 | 
			
		||||
    init_serial();
 | 
			
		||||
    print_serial("Hello Higher half kernel!\n");
 | 
			
		||||
    kterm_init();
 | 
			
		||||
    initGDT();
 | 
			
		||||
    init_idt();
 | 
			
		||||
    // Enable interrupts
 | 
			
		||||
    asm volatile("STI");
 | 
			
		||||
   
 | 
			
		||||
    ProcessBootInfo(); 
 | 
			
		||||
 | 
			
		||||
    if(BootInfo->EnabledVBE)
 | 
			
		||||
    {
 | 
			
		||||
        printf("- VBE graphics mode is available!\n");
 | 
			
		||||
    }
 | 
			
		||||
    initHeap();
 | 
			
		||||
 | 
			
		||||
    if(BootInfo->ValidSymbolTable)
 | 
			
		||||
    {
 | 
			
		||||
        printf("- Valid Symbol Table available at 0x%x.\n Tab Size: %d, str Size: %d\n", BootInfo->SymbolTableAddr, BootInfo->SymbolTabSize, BootInfo->SymbolStrSize);
 | 
			
		||||
    }
 | 
			
		||||
    // test heap allocation
 | 
			
		||||
    /*
 | 
			
		||||
    struct KernelInfo {
 | 
			
		||||
        int bar;
 | 
			
		||||
        bool foo;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    if(BootInfo->PhysicalMemoryMapAvailable)
 | 
			
		||||
    {
 | 
			
		||||
        printf("- Physical Memory Map available!\n");
 | 
			
		||||
        printf("MemoryInfoheap size : %d bytes\n", BootInfo->map_size);
 | 
			
		||||
        // Print the memory regions 
 | 
			
		||||
        MemoryInfoBlock* currentBlock = (MemoryInfoBlock*) ((uint32_t)BootInfo->MemoryMap + KERNEL_BASE_ADDR) ;
 | 
			
		||||
    KernelInfo* MyInfo = (KernelInfo*) malloc(sizeof(KernelInfo));
 | 
			
		||||
 | 
			
		||||
        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");
 | 
			
		||||
            }
 | 
			
		||||
            else if(IS_ACPI_MEM(currentBlock->type)){
 | 
			
		||||
                //printf("ACPI MEMORY\n");   
 | 
			
		||||
            }
 | 
			
		||||
            else if(IS_RESERVED_MEM(currentBlock->type)){
 | 
			
		||||
               // printf("RESERVED MEMORY\n");
 | 
			
		||||
            }
 | 
			
		||||
            else if(IS_NVS_MEMORY(currentBlock->type)){
 | 
			
		||||
               // printf("NVS MEMORY \n");
 | 
			
		||||
            }
 | 
			
		||||
            else if(IS_BADRAM_MEMORY(currentBlock->type)){
 | 
			
		||||
               // printf("BADRAM MEMORY \n");
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
              //  printf("(TYPE 0x%x )TYPE NOT SPECIFIED\n", currentBlock->type);
 | 
			
		||||
            }
 | 
			
		||||
    MyInfo->bar = 6;
 | 
			
		||||
    MyInfo->foo = false;
 | 
			
		||||
 | 
			
		||||
            currentBlock = (MemoryInfoBlock*) ((uint32_t)currentBlock->next + KERNEL_BASE_ADDR );
 | 
			
		||||
          
 | 
			
		||||
        }
 | 
			
		||||
    free(MyInfo);
 | 
			
		||||
    */
 | 
			
		||||
 | 
			
		||||
        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 ? 
 | 
			
		||||
 | 
			
		||||
    pit_initialise();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
        free_block(block);
 | 
			
		||||
        free_block(block2);
 | 
			
		||||
        void* block3 = allocate_block();
 | 
			
		||||
        printf("Allocated addresss 3: 0x%x\n", (uint32_t)block3);
 | 
			
		||||
        free_block(block3);
 | 
			
		||||
    #endif
 | 
			
		||||
void ProcessBootInfo(){
 | 
			
		||||
    uint32_t BootInfoStruct = BootInfoBlock_pptr + 0xC0000000;
 | 
			
		||||
    BootInfoBlock* BootInfo = (BootInfoBlock*) ( BootInfoBlock_pptr + 0xC0000000 );
 | 
			
		||||
 | 
			
		||||
    if( BootInfo->ValidELFHeader )
 | 
			
		||||
    {
 | 
			
		||||
        // NOTE: Do something with it.. (Store it , process it etc...)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if(BootInfo->EnabledVBE)
 | 
			
		||||
    {
 | 
			
		||||
       // NOTE: Do something with it.. (Store it , process it etc...)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if(BootInfo->ValidSymbolTable)
 | 
			
		||||
    {
 | 
			
		||||
        // NOTE: Do something with it.. (Store it , process it etc...)
 | 
			
		||||
        // printf("- Valid Symbol Table available at 0x%x.\n Tab Size: %d, str Size: %d\n", BootInfo->SymbolTableAddr, BootInfo->SymbolTabSize, BootInfo->SymbolStrSize);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if(BootInfo->PhysicalMemoryMapAvailable)
 | 
			
		||||
    {   
 | 
			
		||||
        
 | 
			
		||||
       
 | 
			
		||||
        SetupPhysicalMemoryManager(BootInfo);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
extern "C" void kernel_main () {
 | 
			
		||||
    pit_initialise();
 | 
			
		||||
 | 
			
		||||
    startSuperVisorTerminal();
 | 
			
		||||
}   
 | 
			
		||||
@ -1,38 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
extern "C" 
 | 
			
		||||
{
 | 
			
		||||
   #include "lib/string.h" 
 | 
			
		||||
}
 | 
			
		||||
#include "definitions.h"
 | 
			
		||||
 | 
			
		||||
#include "drivers/vga/VBE.h"
 | 
			
		||||
#include "terminal/kterm.h"
 | 
			
		||||
 | 
			
		||||
#include "memory/PhysicalMemoryManager.h"
 | 
			
		||||
#include "memory/VirtualMemoryManager.h"
 | 
			
		||||
 | 
			
		||||
#include "memory/gdt/gdtc.h"
 | 
			
		||||
#include "interrupts/idt/idt.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"
 | 
			
		||||
 | 
			
		||||
#define CHECK_FLAG(flag, bit) ( flag & (1 << bit ))
 | 
			
		||||
#define PANIC(message) {return;} 
 | 
			
		||||
 | 
			
		||||
void map_multiboot_info_structure(unsigned long addr);
 | 
			
		||||
 | 
			
		||||
extern "C" void kernel_main ();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
extern "C" uint32_t boot_page_directory;
 | 
			
		||||
extern "C" uint32_t multiboot_page_table;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
const uint32_t KERNEL_BASE_ADDR = 0xC0000000;
 | 
			
		||||
@ -4,7 +4,7 @@
 | 
			
		||||
struct heap_block{
 | 
			
		||||
    uint8_t Used;
 | 
			
		||||
    uint32_t Size;
 | 
			
		||||
}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
uint32_t heap_size;
 | 
			
		||||
heap_block* start ;
 | 
			
		||||
@ -17,7 +17,7 @@ void* malloc(size_t size)
 | 
			
		||||
    // look for a free block
 | 
			
		||||
    while(current <  start + heap_size)
 | 
			
		||||
    {
 | 
			
		||||
        if(current->size >= size && current->Used == false )
 | 
			
		||||
        if(current->Size >= size && current->Used == false )
 | 
			
		||||
        {
 | 
			
		||||
            // We found a spot 
 | 
			
		||||
            // Set the spot to in-use 
 | 
			
		||||
@ -42,11 +42,12 @@ void free(void* addr)
 | 
			
		||||
{
 | 
			
		||||
    // clear the free boolean that corresponds to this adddress
 | 
			
		||||
    // This should be fairly simple
 | 
			
		||||
    heap_block* allocatedBlock = addr - sizeof(heap_block);
 | 
			
		||||
    allocate_block->Used = false;
 | 
			
		||||
    heap_block* allocatedBlock = (heap_block*)(addr - sizeof(heap_block));
 | 
			
		||||
    allocatedBlock->Used = false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void initHeap()
 | 
			
		||||
{
 | 
			
		||||
    // NOTE: What to do now??
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
@ -1,6 +1,7 @@
 | 
			
		||||
#pragma once 
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
#include "../terminal/kterm.h"
 | 
			
		||||
 | 
			
		||||
void initHeap();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,14 +1,17 @@
 | 
			
		||||
#include "./PhysicalMemoryManager.h"
 | 
			
		||||
 | 
			
		||||
PhysicalMemoryManagerInfoBlock* PMMInfoBlock;
 | 
			
		||||
const uint32_t KERNEL_OFFSET = 0xC0000000;
 | 
			
		||||
extern uint32_t* boot_page_directory;
 | 
			
		||||
extern uint32_t* boot_page_table;
 | 
			
		||||
extern uint32_t* multiboot_page_table;
 | 
			
		||||
 | 
			
		||||
const uint32_t KERNEL_OFFSET = 0xC0000000;
 | 
			
		||||
void SetupPhysicalMemoryManager( BootInfoBlock* Bootinfo) {
 | 
			
		||||
    // 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);
 | 
			
		||||
PhysicalMemoryManagerInfoBlock* PMMInfoBlock;
 | 
			
		||||
 | 
			
		||||
void SetupPhysicalMemoryManager( BootInfoBlock* Bootinfo) 
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    // NOTE: We should move our bitmap to just after the end of our kernel instead
 | 
			
		||||
    PMMInfoBlock = (PhysicalMemoryManagerInfoBlock*) ( ((uint32_t)MemoryMapHeap_pptr + 80 ) + KERNEL_OFFSET );
 | 
			
		||||
    /*
 | 
			
		||||
        Every byte contains 8 pages
 | 
			
		||||
        A page is 4096 kib 
 | 
			
		||||
@ -16,41 +19,33 @@ void SetupPhysicalMemoryManager( BootInfoBlock* Bootinfo) {
 | 
			
		||||
    */
 | 
			
		||||
 | 
			
		||||
    // 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) ;
 | 
			
		||||
    
 | 
			
		||||
    printf("Maximum num blocks: %d \n",PMMInfoBlock->max_blocks);
 | 
			
		||||
    //Size of memory map 
 | 
			
		||||
    uint32_t memMap_size = PMMInfoBlock->max_blocks / 8;
 | 
			
		||||
    printf("Memory map size: %d\n", memMap_size);
 | 
			
		||||
    printf("Address of memory map 0x%x\n", PMMInfoBlock->memoryBitMap);
 | 
			
		||||
    // Set all places in memory as free
 | 
			
		||||
    memset(PMMInfoBlock->memoryBitMap, 0xFF, memMap_size  ); 
 | 
			
		||||
    MemoryInfoBlock* currentBlock = (MemoryInfoBlock*) ((uint32_t)Bootinfo->MemoryMap + 0xC0000000) ;
 | 
			
		||||
 | 
			
		||||
    // Loop over memory map and allocate physical locations 
 | 
			
		||||
    // that are already in use 
 | 
			
		||||
    MemoryInfoBlock* currentBlock = (MemoryInfoBlock*) ((uint32_t)Bootinfo->MemoryMap + KERNEL_OFFSET) ;
 | 
			
		||||
    printf("Starting address: 0x%x\n", currentBlock);
 | 
			
		||||
    while( (uint32_t) currentBlock->next != 0x0)
 | 
			
		||||
    printf( "Starting address: 0x%x\n", currentBlock);
 | 
			
		||||
    while( (uint32_t)currentBlock->next != 0x0 )
 | 
			
		||||
    {
 | 
			
		||||
        
 | 
			
		||||
        if(IS_AVAILABLE_MEM(currentBlock->type)){
 | 
			
		||||
            printf("skip!\n");
 | 
			
		||||
        } else {   
 | 
			
		||||
            printf("allocate region 0x%x of size 0x%x\n" , currentBlock->Base_addr, currentBlock->Memory_Size);       
 | 
			
		||||
            allocate_region((uint32_t) currentBlock->Base_addr, currentBlock->Memory_Size);
 | 
			
		||||
           printf("skip!\n");
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            printf("allocate region 0x%x of size %d bytes\n", currentBlock->Base_addr, currentBlock->Memory_Size);
 | 
			
		||||
            // allocate_region( currentBlock->Base_addr, currentBlock->Memory_Size); // allocate region causes #PF Exception
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        currentBlock = (MemoryInfoBlock*) ((uint32_t)currentBlock->next + KERNEL_OFFSET );
 | 
			
		||||
        currentBlock = (MemoryInfoBlock*) ((uint32_t)currentBlock->next + 0xC0000000 );
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    uint32_t kernel_size = ((uint32_t)&kernel_end - (uint32_t)&kernel_begin ) - KERNEL_OFFSET;
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,9 @@
 | 
			
		||||
#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);
 | 
			
		||||
@ -60,6 +63,7 @@ void Map (  uint32_t vaddr, uint32_t paddr)
 | 
			
		||||
    // allocate a page at  virtual address
 | 
			
		||||
    int PageDirectoryEntryIndex = vaddr >> 22;
 | 
			
		||||
    int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Unmap(uint32_t vaddr)
 | 
			
		||||
 | 
			
		||||
@ -40,6 +40,7 @@ struct BootInfoBlock {
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// TODO Put the BootInfoBlock 1MB above the kernel.
 | 
			
		||||
const uint32_t BootInfoBlock_pptr = (uint32_t)&kernel_end - 0xC0000000 + 0x1;
 | 
			
		||||
const uint32_t MemoryMapHeap_pptr = BootInfoBlock_pptr + sizeof(BootInfoBlock);
 | 
			
		||||
 | 
			
		||||
const uint32_t pke = ((uint32_t)&kernel_end) - 0xC0000000;
 | 
			
		||||
const uint32_t BootInfoBlock_pptr = pke + 1000   - sizeof(BootInfoBlock);
 | 
			
		||||
const uint32_t MemoryMapHeap_pptr = pke + 0x1;
 | 
			
		||||
 | 
			
		||||
@ -99,9 +99,8 @@ if (CHECK_FLAG(mbi->flags, 6))
 | 
			
		||||
                // continue to the next block
 | 
			
		||||
                mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size));
 | 
			
		||||
              
 | 
			
		||||
              CurrentInfoBlock->next = (MemoryInfoBlock*) ((uint32_t)CurrentInfoBlock) + sizeof(MemoryInfoBlock);
 | 
			
		||||
              CurrentInfoBlock->next = (MemoryInfoBlock*) CurrentInfoBlock + 16;
 | 
			
		||||
              CurrentInfoBlock = CurrentInfoBlock->next;
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        CurrentInfoBlock->next = (MemoryInfoBlock*) 0x0;
 | 
			
		||||
 | 
			
		||||
@ -4,5 +4,6 @@
 | 
			
		||||
#include "../drivers/pit/pit.h"
 | 
			
		||||
#include "../drivers/ps-2/keyboard.h"
 | 
			
		||||
#include "../memory/PhysicalMemoryManager.h"
 | 
			
		||||
#include "../lib/string.h"
 | 
			
		||||
 | 
			
		||||
void startSuperVisorTerminal();
 | 
			
		||||
@ -6,9 +6,8 @@
 | 
			
		||||
#include "../drivers/vga/colors.h"
 | 
			
		||||
#include "../io.h"
 | 
			
		||||
 | 
			
		||||
extern "C"{
 | 
			
		||||
    #include "../lib/string.h"
 | 
			
		||||
}
 | 
			
		||||
#include "../lib/string.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void kterm_init();
 | 
			
		||||
 | 
			
		||||
@ -35,11 +34,3 @@ int get_cursor_y (uint16_t cursor_pos);
 | 
			
		||||
 | 
			
		||||
void printf ( const char *format, ...);
 | 
			
		||||
 | 
			
		||||
//static void itoa (char *buf, int base, int d);
 | 
			
		||||
 | 
			
		||||
#define KernelTag "[Kernel]: "
 | 
			
		||||
#define AS_KERNEL() (  kterm_setcolor(VGA_COLOR_LIGHT_BLUE),\
 | 
			
		||||
                       kterm_write(KernelTag, 10 ), \
 | 
			
		||||
                       kterm_resetcolor())
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user