blob: aa93496f86e4ade01bd05371e1afd3344d01a0b3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include "arch/x86_64/hal/paging.h"
#include "jove.h"
#include "config.h"
#include "hal/framebuffer.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];
extern page_t _kernel_pml3[512];
void*
fb_remap(uintptr_t fb_address,
size_t fb_bytes)
{
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((uintptr_t)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 fb_off = fb_address + (pml1 << 21);
fb_pml2[pml2][pml1_mod].value = fb_off | PAGE_PRESENT | PAGE_GLOBAL | PAGE_WRITEABLE | PAGE_PS;
}
uintptr_t fb_virt = IDENT_VIRT_BASE | (511ULL << PMLI_SHL(3));
return (void*)fb_virt;
}
|