diff --git a/arch/x86/paging.c b/arch/x86/paging.c index e32c1e5..8020a21 100644 --- a/arch/x86/paging.c +++ b/arch/x86/paging.c @@ -71,7 +71,7 @@ static inline void __native_flush_tlb_single(unsigned long addr) asm volatile("invlpg (%0)" ::"r"(addr) : "memory"); } -int pagingSetup(paddr_t upperKernelAddr) +int pagingSetup(paddr_t lowerKernelAddr, paddr_t upperKernelAddr) { struct pdbr cr3; @@ -85,7 +85,7 @@ int pagingSetup(paddr_t upperKernelAddr) // MMU not enabled for the moment. No need to use mirroring // Identity mapping up to upperKernelAddr - for (paddr_t i = 0; i < upperKernelAddr; i += PAGE_SIZE) { + for (paddr_t i = lowerKernelAddr; i < upperKernelAddr; i += PAGE_SIZE) { uint pdEntry = i >> (PD_SHIFT); uint ptEntry = (i >> PT_SHIFT) & PTE_MASK; struct pte *pt; diff --git a/arch/x86/paging.h b/arch/x86/paging.h index 9b143d1..11490de 100644 --- a/arch/x86/paging.h +++ b/arch/x86/paging.h @@ -5,7 +5,7 @@ #define PAGING_MEM_READ (1U << 1) #define PAGING_MEM_WRITE (1U << 2) -int pagingSetup(paddr_t upperKernelAddr); +int pagingSetup(paddr_t lowerKernelAddr, paddr_t upperKernelAddr); int pageMap(vaddr_t vaddr, paddr_t paddr, int flags); int pageUnmap(vaddr_t vaddr); diff --git a/core/allocArea.h b/core/allocArea.h index c292442..0bea2cc 100644 --- a/core/allocArea.h +++ b/core/allocArea.h @@ -2,3 +2,5 @@ #include "paging.h" void areaInit(void); +vaddr_t areaAlloc(unsigned int nbPages); +int areaFree(vaddr_t addr); diff --git a/core/main.c b/core/main.c index c83d8b0..673dda9 100644 --- a/core/main.c +++ b/core/main.c @@ -40,6 +40,7 @@ void kmain(unsigned long magic, unsigned long addr) unsigned long upperMemKB = 0; int memMapAvailable = 0; paddr_t lastUsedByMem; + paddr_t firstUsedByMem; VGASetup(BLACK, GREEN); @@ -82,6 +83,7 @@ void kmain(unsigned long magic, unsigned long addr) if (CHECK_FLAG(mbi->flags, 6)) { memMapAvailable = 1; } + printf("Framebuffer from 0x%llx size (%dx%d) pitch %d bpp %d\n", mbi->framebuffer_addr, mbi->framebuffer_width, mbi->framebuffer_height, mbi->framebuffer_pitch, mbi->framebuffer_bpp); } if (upperMemKB == 0) { @@ -90,7 +92,8 @@ void kmain(unsigned long magic, unsigned long addr) } printf("Setting up Mem\n"); - memSetup(upperMemKB, &lastUsedByMem); + + memSetup(upperMemKB, &firstUsedByMem, &lastUsedByMem); if (memMapAvailable) { multiboot_info_t *mbi = (multiboot_info_t *)addr; @@ -102,20 +105,27 @@ void kmain(unsigned long magic, unsigned long addr) for (uint i = 0; i < size; i++) { printf(" base_addr 0x%llx, length = 0x%llx, type = 0x%x\n", mmap[i].addr, mmap[i].len, (uint32_t)mmap[i].type); + if(mmap[i].addr < 0x100000){ //Consider low memory taken (https://wiki.osdev.org/Detecting_Memory_(x86). For example by VGA + continue; + } memAddBank(max(mmap[i].addr, (multiboot_uint64_t)lastUsedByMem), mmap[i].addr + mmap[i].len, mmap[i].type == MULTIBOOT_MEMORY_AVAILABLE); } } else { printf("Cannot get memory Mapping information, using default value\n"); - memAddBank(0, lastUsedByMem, 0); memAddBank(lastUsedByMem, upperMemKB * 1024, 1); } + printf("%d pages taken by kernel(0x%x->0x%x))\n", (lastUsedByMem - firstUsedByMem)/PAGE_SIZE, firstUsedByMem, lastUsedByMem); + printf("with %d pages taken for memory management\n", (lastUsedByMem - (paddr_t)&__ld_kernel_end)/PAGE_SIZE); #ifdef RUN_TEST testPhymem(); #endif printf("Setting up Pagination\n"); - pagingSetup(lastUsedByMem); + pagingSetup(firstUsedByMem, lastUsedByMem); + for (paddr_t i = VGA_ADDR; i < VGA_ADDR + VGA_WIDTH * VGA_HEIGHT * sizeof(short) ; i += PAGE_SIZE) { + pageMap(i, i, PAGING_MEM_WRITE); + } printf("Setting up IRQ handlers\n"); irqSetRoutine(IRQ_KEYBOARD, keyboard_handler); diff --git a/core/mem.c b/core/mem.c index 823cc6c..2406fd0 100644 --- a/core/mem.c +++ b/core/mem.c @@ -1,5 +1,5 @@ -#include "assert.h" #include "mem.h" +#include "assert.h" #include "kernel.h" #include "klibc.h" #include "list.h" @@ -13,7 +13,7 @@ paddr_t upperMem; static unsigned long allocatedPage = 0; -int memSetup(paddr_t upperMemKB, paddr_t *lastMemUsedOut) +int memSetup(paddr_t upperMemKB, paddr_t *firstUsed, paddr_t *lastMemUsedOut) { list_init(freePage); list_init(usedPage); @@ -30,8 +30,13 @@ int memSetup(paddr_t upperMemKB, paddr_t *lastMemUsedOut) pageDescEnd = (unsigned long)pageDesc + nbPage * sizeof(struct memDesc); *lastMemUsedOut = ALIGN(pageDescEnd, PAGE_SIZE); upperMem = upperMemKB * 1024; + *firstUsed = ALIGN_DOWN((paddr_t)&__ld_kernel_begin, PAGE_SIZE); + + // Remove addr from 0 to PAGE_SIZE so we can return 0 for no page available + memAddBank(0, PAGE_SIZE, 0); + + memAddBank(*lastMemUsedOut, *lastMemUsedOut, 0); - memAddBank(0, *lastMemUsedOut, 0); return 0; } @@ -136,7 +141,7 @@ int unrefPhyPage(paddr_t addr) if (!mem) { return -1; } - assert(mem->ref >0); + assert(mem->ref > 0); mem->ref--; if (mem->ref == 0) { allocatedPage--; diff --git a/core/mem.h b/core/mem.h index 6e68948..784055a 100644 --- a/core/mem.h +++ b/core/mem.h @@ -15,7 +15,7 @@ struct memDesc { struct memDesc *next, *prev; }; -int memSetup(paddr_t upperMem, paddr_t *lastUsed); +int memSetup(paddr_t upperMem, paddr_t * firstUsed, paddr_t *lastUsed); int memAddBank(paddr_t bottomMem, paddr_t topMem, int isFree); paddr_t allocPhyPage(uint nbPage); int unrefPhyPage(paddr_t addr);