First step for memory management

This commit is contained in:
Mathieu Maret 2018-08-06 18:41:45 +02:00
parent ca4489f4cc
commit 1f164a7a4e
4 changed files with 56 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include "interrupt.h"
#include "io.h"
#include "irq.h"
#include "mem.h"
#include "multiboot.h"
#include "pit.h"
#include "types.h"
@ -19,6 +20,7 @@ void cpuid(int code, uint32_t *a, uint32_t *d)
// Multiboot information available here : https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#kernel_002ec
void kmain(unsigned long magic, unsigned long addr)
{
unsigned long upper_mem = 0;
initVGA(BLACK, GREEN);
printString("Setting up IDT\n");
gdtSetup();
@ -35,6 +37,7 @@ void kmain(unsigned long magic, unsigned long addr)
printString("KB mem_upper = ");
printInt((unsigned)mbi->mem_upper);
printString("KB\n");
upper_mem = mbi->mem_upper;
}
/* Is boot_device valid? */
@ -52,6 +55,12 @@ void kmain(unsigned long magic, unsigned long addr)
}
}
if(upper_mem == 0){
printString("Cannot get upper phy mem bound. Using default value 32MB\n");
upper_mem = 32 * 1024;
}
memInit(upper_mem);
printString("Setting up IRQ handlers\n");
irqSetRoutine(IRQ_KEYBOARD, keyboard_handler);
irqSetRoutine(IRQ_TIMER, timer_handler);

26
core/mem.c Normal file
View File

@ -0,0 +1,26 @@
#include "mem.h"
#include "vga.h"
struct mem_desc *page_desc = (struct mem_desc *)&__ld_kernel_end;
int memInit(unsigned long upper_mem)
{
printString("Free Mem going from ");
printInt((unsigned long)&__ld_kernel_end);
printString(" to ");
printInt(upper_mem * 1024);
printString("\n");
unsigned long memdesc_end =
(unsigned long)page_desc +
((upper_mem) / (PAGE_SIZE / 1024)) * sizeof(struct mem_desc);
uint lastUsed = (memdesc_end >> PAGE_SHIFT) + 1;
for (uint i = 0; i < (upper_mem / (PAGE_SIZE / 1024)); i++) {
struct mem_desc *mem = &page_desc[i];
if (i < lastUsed)
mem->ref = 1;
else
mem->ref = 0;
mem->phy_addr = i * PAGE_SIZE;
}
return 0;
}

18
core/mem.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include "types.h"
#define PAGE_SHIFT 12
#define PAGE_SIZE (1 << PAGE_SHIFT)
// Defined in linker.ld script
extern uint32_t __ld_kernel_begin;
extern uint32_t __ld_kernel_end;
struct mem_desc{
unsigned long phy_addr;
unsigned long ref;
};
int memInit(unsigned long upper_mem);

View File

@ -16,6 +16,7 @@ SECTIONS
.text BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
__ld_kernel_begin = .;
*(.text)
}
@ -38,6 +39,8 @@ SECTIONS
*(.bss)
}
__ld_kernel_end = .;
/* The compiler may produce other sections, by default it will put them in
a segment with the same name. Simply add stuff here as needed. */
}