#include "arch/x86_64/hal/paging.h" #include "jove.h" #include "config.h" #include "hal/framebuffer.h" #include "lib/string.h" #include "lib/printf.h" /*We don't know the size of the framebuffer until runtime, but at the same * time we don't want to dynamically allocate from physical memory. In this * case we allocate memory for the maximum supported framebuffer at compile * time and only use what we need at runtime.*/ #define FRAMEBUFFER_MAX_BYTES (FRAMEBUFFER_MAX_WIDTH * FRAMEBUFFER_MAX_WIDTH * 4) #define FRAMEBUFFER_MAX_PAGES ((FRAMEBUFFER_MAX_BYTES + 4095) / 0x1000) #define FRAMEBUFFER_MAX_HUGEPAGES ((FRAMEBUFFER_MAX_PAGES + 511) / 512) #define FRAMEBUFFER_MAX_GIANTPAGES ((FRAMEBUFFER_MAX_HUGEPAGES + 511) / 512) PAGEALIGN static page_t fb_pml2[FRAMEBUFFER_MAX_GIANTPAGES][512]; PAGEALIGN static page_t fb_pml1[FRAMEBUFFER_MAX_HUGEPAGES][512]; extern page_t _kernel_pml3[512]; void* fb_remap(uintptr_t fb_address, size_t fb_bytes) { memset(fb_pml2, 0, 0x1000 * FRAMEBUFFER_MAX_GIANTPAGES); memset(fb_pml1, 0, 0x1000 * FRAMEBUFFER_MAX_HUGEPAGES); fb_address &= ~IDENT_VIRT_BASE; size_t fb_pages = (fb_bytes + 4095) >> 12; size_t fb_hugepages = (fb_pages + 511) >> 9; size_t fb_giantpages = (fb_hugepages + 511) >> 9; if(fb_pages > FRAMEBUFFER_MAX_PAGES) { kprintf("Framebuffer is too big! Please recompile the kernel with a bigger framebuffer allowance!"); return NULL; } for(size_t pml2 = 0; pml2 < fb_giantpages; pml2++) { uintptr_t fb_pml2_pptr = KPTR_TOPHYS(&fb_pml2[pml2]); _kernel_pml3[pml2].value = fb_pml2_pptr | PAGE_PRESENT | PAGE_GLOBAL | PAGE_WRITEABLE; } for(size_t pml1 = 0; pml1 < fb_hugepages; pml1++) { size_t pml2 = pml1 >> 9; size_t pml1_mod = pml1 % 512; uintptr_t pml1_pptr = KPTR_TOPHYS(&fb_pml1[pml1]); fb_pml2[pml2][pml1_mod].value = pml1_pptr | PAGE_PRESENT | PAGE_GLOBAL | PAGE_WRITEABLE; } for(size_t pml0 = 0; pml0 < fb_pages; pml0++) { size_t pml1 = pml0 >> 9; size_t pml0_mod = pml0 % 512; uintptr_t pml0_pptr = fb_address + (pml0 << 12); fb_pml1[pml1][pml0_mod].value = pml0_pptr | PAGE_PRESENT | PAGE_GLOBAL | PAGE_WRITEABLE; } uintptr_t fb_virt = IDENT_VIRT_BASE | (511ULL << PMLI_SHL(3)); return (void*)fb_virt; }