Improving the memory mapping boot code

Removed the need to map the extra MBI structure in as a seperate pagetable
Renaming / Restructuring the memory folder
This commit is contained in:
Nigel Barink 2022-08-21 21:15:15 +02:00
parent 560dd64e64
commit e70f56a005
14 changed files with 75 additions and 47 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
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)/launcher.o
SRC_DIR = src
BUILD_DIR = build
@ -102,4 +102,7 @@ $(BUILD_DIR)/memory.o:
$(CPP) -c $(SRC_DIR)/kernel/Memory/memory.cpp -o $(BUILD_DIR)/memory.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/paging.o:
$(CPP) -c $(SRC_DIR)/kernel/Memory/paging.cpp -o $(BUILD_DIR)/paging.o $(CFLAGS) -fno-exceptions -fno-rtti
$(CPP) -c $(SRC_DIR)/kernel/Memory/paging.cpp -o $(BUILD_DIR)/paging.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/launcher.o:
$(CPP) -c $(SRC_DIR)/kernel/KernelLauncher/launcher.cpp -o $(BUILD_DIR)/launcher.o $(CFLAGS) -fno-exceptions -fno-rtti

BIN
screenshots/must frustrating bug ever.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -1,8 +1,3 @@
GRUB_DEFAULT=0
GRUB_TIMEOUT=-1
GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUITE=true
menuentry "BarinkOS" {
multiboot /boot/myos.bin
}

View File

@ -47,12 +47,7 @@ _start:
movl $(boot_page_table - 0xC0000000), %edi
# Map address 0
movl $0, %esi
# Map 1023 pages the 1024th being the VGA text buffer
movl $1023, %ecx
1: # Map the kernel
cmpl $_kernel_start, %esi
jl 2f
1:
cmpl $(_kernel_end - 0xC0000000), %esi
jge 3f
@ -125,12 +120,8 @@ isPaging:
call early_main
mov %cr0, %eax
or $1, %eax
mov %eax, %cr0
//call kernel_main
cli

View File

@ -1,7 +1,7 @@
#pragma once
#include "../multiboot.h"
#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))
#define __VERBOSE__
#include "../Terminal/kterm.h"

View File

@ -0,0 +1,28 @@
#include <stdint.h>
#include <stddef.h>
void put_char(char ch, size_t x, size_t y ){
*((uint16_t*)0xb8000+(y * 80 + x)) = ch | ((7 | 0 << 4) << 8);
}
void write_ln(char* s, size_t length, size_t x , size_t y)
{
// Because read only data is linked at a virtual address we'll need to convert
// the string adddres from virtual to phys.
s = s - 0xC0000000;
size_t column , row;
column = x;
row = y;
for(int i = 0; i < length ; i ++)
{
put_char(s[i] , column,row );
column ++;
}
}
extern "C" void testLauncher () {
write_ln("hello", 5 ,0,0);
}

View File

@ -0,0 +1,2 @@
#pragma once

View File

@ -102,11 +102,13 @@ void PhysicalMemory::deallocate_region(uint32_t StartAddress , uint32_t size )
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);
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))){
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){

View File

@ -8,8 +8,10 @@
#include "../Lib/mem.h"
#include "../bitmap.h"
// Asumming 32 bit x86 for now!
#define BLOCK_SIZE 4092
#define BLOCKS_PER_WORD 32 // A word is 16 bit in x86 machines according to my google search results!
#define WORD_SIZE 2
#define BLOCKS_PER_WORD 32
#define KB_TO_BLOCKS(x) (x / BLOCK_SIZE)
#define IS_ALIGNED(addr, align) !((addr) & ~((align) - 1))

View File

@ -1,7 +1,6 @@
#include "paging.h"
/*PageDirectoryEntry kernel_directory[MAX_DIRECTORY_ENTRIES]__attribute__((aligned(4096)));
//PageTableEntry first_page_table[MAX_PAGE_TABLE_ENTRIES]__attribute__((aligned(4096)));
// PageDirectoryEntry kernel_directory[MAX_DIRECTORY_ENTRIES]__attribute__((aligned(4096)));
// PageTableEntry first_page_table[MAX_PAGE_TABLE_ENTRIES]__attribute__((aligned(4096)));
void IdentityMap (){
printf("\nInit paging\n");
@ -11,7 +10,7 @@ void IdentityMap (){
int i = 0;
while ( i < 1024)
{
kernel_directory[i] = 0x2;
// kernel_directory[i] = 0x2;
i++;
}
@ -20,7 +19,7 @@ void IdentityMap (){
unsigned int j ;
for( j = 0; j < 1024; j++ )
{
first_page_table[j] = (j * 0x1000) | 3 ;
// first_page_table[j] = (j * 0x1000) | 3 ;
//Attributes:
//Supervisor Level ,
@ -32,7 +31,7 @@ void IdentityMap (){
// Put the page table in the page directory
// attributes: supervisor level, read/write, present;
kernel_directory[0] = ((unsigned int)first_page_table) | 3;
// kernel_directory[0] = ((unsigned int)first_page_table) | 3;
printf("Init paging DONE\n");
}
@ -43,10 +42,10 @@ void InitializePaging()
Initial kernel page directory
set all page tables to not present
*/
/*
for (int i = 0; i < MAX_DIRECTORY_ENTRIES; i++)
{
kernel_directory[i] = 0x2;
// kernel_directory[i] = 0x2;
}
// BIOS Address Identity mapping
@ -58,20 +57,20 @@ void InitializePaging()
uint8_t NUM_PDE = BIOSAddr_Max / (4 * 1024 * 1024);
printf("The first 8MiB require %d Page Directory Entries\n", NUM_PDE);
*/
/*
for( int i = 0; i < NUM_PDE; i++)
{
// setup a page table
PageTableEntry pagetable[MAX_PAGE_TABLE_ENTRIES] = PhysicalMemory::allocate_block(); // TODO :: Physical memory manager functions should be available here.
// PageTableEntry pagetable[MAX_PAGE_TABLE_ENTRIES] = PhysicalMemory::allocate_block(); // TODO :: Physical memory manager functions should be available here.
for(int j = 0; j < MAX_PAGE_TABLE_ENTRIES; j++)
{
pagetable[j] = ( j * 4096 ) | 3;
// pagetable[j] = ( j * 4096 ) | 3;
}
// add page table as page directory entry
kernel_directory[i] = ( (unsigned int) pagetable ) | 3;
// kernel_directory[i] = ( (unsigned int) pagetable ) | 3;
}
// map the kernel space
@ -87,11 +86,11 @@ void InitializePaging()
for(int i = 0; i < NUM_PDE; i++)
{
PageTableEntry pageTable [MAX_PAGE_TABLE_ENTRIES] = PhysicalMemory::allocate_block();
// PageTableEntry pageTable [MAX_PAGE_TABLE_ENTRIES] = PhysicalMemory::allocate_block();
for(int j = 0; j < MAX_PAGE_TABLE_ENTRIES; j++)
{
pageTable[j] = ( j * 4096) | 3; // NOTE: Check if page is actually supposed to be present
// pageTable[j] = ( j * 4096) | 3; // NOTE: Check if page is actually supposed to be present
}
// TODO: Calculate Page Directory index
@ -145,9 +144,9 @@ void Enable()
CR0 = GetCR0();
printf("PG bit = %d \n" , GET_PG_BIT(CR0));
printf("Load into CR3 address: 0x%x\n", (uint32_t)(&kernel_directory[0]));
loadPageDirectory(&kernel_directory[0]);
enablePaging();
// printf("Load into CR3 address: 0x%x\n", (uint32_t)(&kernel_directory[0]));
// loadPageDirectory(&kernel_directory[0]);
// enablePaging();
printf("Paging enabled!\n");
@ -170,4 +169,3 @@ void Enable()
}
}
*/

View File

@ -1,7 +1,6 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "../Drivers/VGA/colors.h"

View File

@ -7,6 +7,9 @@ extern "C" uint32_t multiboot_page_table;
const uint32_t KERNEL_BASE_ADDR = 0xC0000000;
extern "C" void early_main(unsigned long magic, unsigned long addr){
// Convert MBI address to higher quarter kernel space
addr += KERNEL_BASE_ADDR;
/**
* Initialize terminal interface
* NOTE: This should be done later on , the magic value should be checked first.
@ -20,7 +23,6 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){
// Enable interrupts
asm volatile("STI");
map_multiboot_info_structure(addr);
printf("DEBUG:\n Magic: 0x%x\n MBT_addr: 0x%x\n", magic, addr);
/**
@ -78,7 +80,7 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){
printf("Kernel Begin Pointer: 0x%x, Kernel end pointer: 0x%x\n", &kernel_begin , &kernel_end );
multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) mbt->mmap_addr;
multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) (mbt->mmap_addr + KERNEL_BASE_ADDR) ;
for (; (unsigned long) mmap < mbt->mmap_addr + mbt->mmap_length; mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size))){
@ -120,8 +122,10 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){
printf("memory flag not set!");
}
CheckMBT( (multiboot_info_t *) addr);
CheckMBT( (multiboot_info_t *) addr);
asm volatile("mov %cr0, %eax ");
asm volatile("or $1, %eax");
asm volatile("mov %eax, %cr0");
kernel_main(&bootinfo);
}

View File

@ -16,6 +16,7 @@ SECTIONS
.multiboot.text : {
*(multiboot.text)
*launcher.o(.text)
}
. += 0xC0000000; /* Addresses in the following code need to be above the 3Gb mark */