Skip to content

Commit

Permalink
Update map_framebuffer to avail of the earlier initialization of pmm
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamos82 committed Aug 27, 2024
1 parent 3809cee commit 1399052
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/include/kernel/framebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void _fb_put_pixel(uint32_t, uint32_t, uint32_t);

uint32_t _fb_get_pixel(uint32_t x, uint32_t y);

void map_framebuffer(struct framebuffer_info fbdata);
void* map_framebuffer(struct framebuffer_info fbdata);
void set_fb_data(struct multiboot_tag_framebuffer *);
void _fb_printStrAndNumber(const char*, uint64_t, uint32_t, uint32_t);
void _fb_printStrAndNumberAt(const char*, uint64_t, size_t, size_t, uint32_t, uint32_t);
Expand Down
73 changes: 17 additions & 56 deletions src/kernel/arch/x86_64/framebuffer/framebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include <pmm.h>
#include <psf.h>
#include <stdio.h>
#include <vm.h>
#include <video.h>
#include <vm.h>
#include <vmm.h>
#include <vmm_mapping.h>

extern uint64_t p4_table[];
extern uint64_t p3_table_hh[];
Expand All @@ -19,69 +21,27 @@ extern uint64_t pt_tables[];
#endif

/*struct framebuffer_info framebuffer_data;*/
void map_framebuffer(framebuffer_info fbdata) {
uint32_t fb_entries = fbdata.memory_size / PAGE_SIZE_IN_BYTES;
pretty_logf(Verbose, "Fbdata size: 0x%x", fbdata.memory_size);

uint64_t phys_address = (uint64_t) fbdata.phys_address;

uint32_t pd = PD_ENTRY(_FRAMEBUFFER_MEM_START);
uint32_t pdpr = PDPR_ENTRY(_FRAMEBUFFER_MEM_START);
uint32_t pml4 = PML4_ENTRY(_FRAMEBUFFER_MEM_START);
#if SMALL_PAGES == 1
uint32_t fb_pd_entries = fb_entries / VM_PAGES_PER_TABLE;
//uint32_t pt = PT_ENTRY(_FRAMEBUFFER_MEM_START);
#endif

if(p4_table[pml4] == 0x00l || p3_table_hh[pdpr] == 0x00l){
pretty_log(Verbose, "PANIC - PML4 or PDPR Empty - not supported for now\n");
asm("hlt");
void* map_framebuffer(framebuffer_info fbdata) {
uint64_t address_to_map = (uint64_t) fbdata.phys_address;
uint64_t virtual_address_start = ensure_address_in_higher_half(address_to_map, VM_TYPE_MMIO);
uint64_t virtual_address = virtual_address_start;
uint64_t upper_address_to_map = address_to_map + fbdata.memory_size;
pretty_logf(Verbose, "Preparing framebuffer: phys_addr: 0x%x, virtual_address: 0x%x - Fb size: 0x%x ", address_to_map, virtual_address, fbdata.memory_size);
while ( address_to_map < upper_address_to_map) {
map_phys_to_virt_addr((void*)address_to_map, (void*)virtual_address, VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE);
address_to_map += PAGE_SIZE_IN_BYTES;
virtual_address += PAGE_SIZE_IN_BYTES;
}

#if SMALL_PAGES == 1
uint64_t *current_page_table = pt_tables;
for(uint32_t i = 0; i <= fb_pd_entries; i++){
bool newly_allocated = false;
// Probably should be safer to rely on the direct map if possible?
if(p2_table[pd] == 0x00){
uint64_t *new_table = pmm_prepare_new_pagetable();
p2_table[pd] = (uint64_t)new_table | (PRESENT_BIT | WRITE_BIT);
uint64_t *new_table_hhdm = hhdm_get_variable((uintptr_t)new_table);
current_page_table = new_table_hhdm;
clean_new_table((uint64_t *)new_table_hhdm);
newly_allocated = true;
}
for(int j=0; j < VM_PAGES_PER_TABLE && fb_entries > 0; j++){
if(newly_allocated == false){
} else {
current_page_table[j] = phys_address + (((VM_PAGES_PER_TABLE * i) + j) * PAGE_SIZE_IN_BYTES) | PAGE_ENTRY_FLAGS;
}
fb_entries--;
}
newly_allocated = false;
pd++;
}
#elif SMALL_PAGES == 0
uint32_t fb_entries_mod = fbdata.memory_size % PAGE_SIZE_IN_BYTES;
if(fb_entries_mod != 0){
fb_entries++;
}
for(int j=0; fb_entries > 0; j++){
fb_entries--;
if( (p2_table[pd+j] < phys_address
|| p2_table[pd+j] > (phys_address + fbdata.memory_size) )
|| p2_table[pd+j] == 0x00l ) {
p2_table[pd+j] = (phys_address + (j * PAGE_SIZE_IN_BYTES)) | PAGE_ENTRY_FLAGS;
}
}
#endif
pretty_logf(Verbose, "Framebuffer mapping end at 0x%x - virtual end: 0x%x", address_to_map, virtual_address);
return (void *) virtual_address_start;
}


void set_fb_data(struct multiboot_tag_framebuffer *fbtag){
//FRAMEBUFFER_MEM = (void*)(uint64_t)fbtag->common.framebuffer_addr;
#if USE_FRAMEBUFFER == 1
framebuffer_data.address = (void*)(uint64_t)_FRAMEBUFFER_MEM_START;
//framebuffer_data.address = (void*)(uint64_t)_FRAMEBUFFER_MEM_START;
//framebuffer_data.address = hhdm_get_variable((uintptr_t) (fbtag->common.framebuffer_addr));
framebuffer_data.pitch = fbtag->common.framebuffer_pitch;
framebuffer_data.bpp = fbtag->common.framebuffer_bpp;
Expand All @@ -93,6 +53,7 @@ void set_fb_data(struct multiboot_tag_framebuffer *fbtag){
number_of_lines = 0;

map_framebuffer(framebuffer_data);
framebuffer_data.address = (void*)map_framebuffer(framebuffer_data);
cur_fb_line = 0;
framebuffer_main_window.x_orig = 0;
framebuffer_main_window.y_orig = 0;
Expand Down
1 change: 1 addition & 0 deletions src/kernel/arch/x86_64/mem/vmm_mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <pmm.h>
#include <vm.h>
#include <vmm.h>
#include <vmm_mapping.h>

void *map_phys_to_virt_addr_hh(void* physical_address, void* address, size_t flags, uint64_t *pml4_root) {

Expand Down
4 changes: 2 additions & 2 deletions src/kernel/mem/hh_direct_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ void hhdm_map_physical_memory() {
}

// This is the kernel mapped in -2G
pretty_logf(Verbose, "Physical memory mapped end: 0x%x - Virtual memory direct end: 0x%x - counter: %d", end_of_mapped_physical_memory, end_of_mapped_memory, current_pml4_entry);

pretty_logf(Verbose, "Physical memory mapped end: 0x%x - Virtual memory end: 0x%x - counter: %d", end_of_mapped_physical_memory, end_of_mapped_memory, current_pml4_entry);
pretty_logf(Verbose, "hhdm end: 0x%x", virtual_address);
}

0 comments on commit 1399052

Please sign in to comment.