Thinner phy page pre-booking

This commit is contained in:
Mathieu Maret 2021-04-10 00:24:02 +02:00
parent 32446b3603
commit 4887984322
6 changed files with 28 additions and 11 deletions

View File

@ -71,7 +71,7 @@ static inline void __native_flush_tlb_single(unsigned long addr)
asm volatile("invlpg (%0)" ::"r"(addr) : "memory"); asm volatile("invlpg (%0)" ::"r"(addr) : "memory");
} }
int pagingSetup(paddr_t upperKernelAddr) int pagingSetup(paddr_t lowerKernelAddr, paddr_t upperKernelAddr)
{ {
struct pdbr cr3; struct pdbr cr3;
@ -85,7 +85,7 @@ int pagingSetup(paddr_t upperKernelAddr)
// MMU not enabled for the moment. No need to use mirroring // MMU not enabled for the moment. No need to use mirroring
// Identity mapping up to upperKernelAddr // 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 pdEntry = i >> (PD_SHIFT);
uint ptEntry = (i >> PT_SHIFT) & PTE_MASK; uint ptEntry = (i >> PT_SHIFT) & PTE_MASK;
struct pte *pt; struct pte *pt;

View File

@ -5,7 +5,7 @@
#define PAGING_MEM_READ (1U << 1) #define PAGING_MEM_READ (1U << 1)
#define PAGING_MEM_WRITE (1U << 2) #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 pageMap(vaddr_t vaddr, paddr_t paddr, int flags);
int pageUnmap(vaddr_t vaddr); int pageUnmap(vaddr_t vaddr);

View File

@ -2,3 +2,5 @@
#include "paging.h" #include "paging.h"
void areaInit(void); void areaInit(void);
vaddr_t areaAlloc(unsigned int nbPages);
int areaFree(vaddr_t addr);

View File

@ -40,6 +40,7 @@ void kmain(unsigned long magic, unsigned long addr)
unsigned long upperMemKB = 0; unsigned long upperMemKB = 0;
int memMapAvailable = 0; int memMapAvailable = 0;
paddr_t lastUsedByMem; paddr_t lastUsedByMem;
paddr_t firstUsedByMem;
VGASetup(BLACK, GREEN); VGASetup(BLACK, GREEN);
@ -82,6 +83,7 @@ void kmain(unsigned long magic, unsigned long addr)
if (CHECK_FLAG(mbi->flags, 6)) { if (CHECK_FLAG(mbi->flags, 6)) {
memMapAvailable = 1; 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) { if (upperMemKB == 0) {
@ -90,7 +92,8 @@ void kmain(unsigned long magic, unsigned long addr)
} }
printf("Setting up Mem\n"); printf("Setting up Mem\n");
memSetup(upperMemKB, &lastUsedByMem);
memSetup(upperMemKB, &firstUsedByMem, &lastUsedByMem);
if (memMapAvailable) { if (memMapAvailable) {
multiboot_info_t *mbi = (multiboot_info_t *)addr; 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++) { for (uint i = 0; i < size; i++) {
printf(" base_addr 0x%llx, length = 0x%llx, type = 0x%x\n", mmap[i].addr, printf(" base_addr 0x%llx, length = 0x%llx, type = 0x%x\n", mmap[i].addr,
mmap[i].len, (uint32_t)mmap[i].type); 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), memAddBank(max(mmap[i].addr, (multiboot_uint64_t)lastUsedByMem),
mmap[i].addr + mmap[i].len, mmap[i].type == MULTIBOOT_MEMORY_AVAILABLE); mmap[i].addr + mmap[i].len, mmap[i].type == MULTIBOOT_MEMORY_AVAILABLE);
} }
} else { } else {
printf("Cannot get memory Mapping information, using default value\n"); printf("Cannot get memory Mapping information, using default value\n");
memAddBank(0, lastUsedByMem, 0);
memAddBank(lastUsedByMem, upperMemKB * 1024, 1); 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 #ifdef RUN_TEST
testPhymem(); testPhymem();
#endif #endif
printf("Setting up Pagination\n"); 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"); printf("Setting up IRQ handlers\n");
irqSetRoutine(IRQ_KEYBOARD, keyboard_handler); irqSetRoutine(IRQ_KEYBOARD, keyboard_handler);

View File

@ -1,5 +1,5 @@
#include "assert.h"
#include "mem.h" #include "mem.h"
#include "assert.h"
#include "kernel.h" #include "kernel.h"
#include "klibc.h" #include "klibc.h"
#include "list.h" #include "list.h"
@ -13,7 +13,7 @@ paddr_t upperMem;
static unsigned long allocatedPage = 0; 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(freePage);
list_init(usedPage); list_init(usedPage);
@ -30,8 +30,13 @@ int memSetup(paddr_t upperMemKB, paddr_t *lastMemUsedOut)
pageDescEnd = (unsigned long)pageDesc + nbPage * sizeof(struct memDesc); pageDescEnd = (unsigned long)pageDesc + nbPage * sizeof(struct memDesc);
*lastMemUsedOut = ALIGN(pageDescEnd, PAGE_SIZE); *lastMemUsedOut = ALIGN(pageDescEnd, PAGE_SIZE);
upperMem = upperMemKB * 1024; 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; return 0;
} }
@ -136,7 +141,7 @@ int unrefPhyPage(paddr_t addr)
if (!mem) { if (!mem) {
return -1; return -1;
} }
assert(mem->ref >0); assert(mem->ref > 0);
mem->ref--; mem->ref--;
if (mem->ref == 0) { if (mem->ref == 0) {
allocatedPage--; allocatedPage--;

View File

@ -15,7 +15,7 @@ struct memDesc {
struct memDesc *next, *prev; 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); int memAddBank(paddr_t bottomMem, paddr_t topMem, int isFree);
paddr_t allocPhyPage(uint nbPage); paddr_t allocPhyPage(uint nbPage);
int unrefPhyPage(paddr_t addr); int unrefPhyPage(paddr_t addr);