Refactoring

This commit is contained in:
Loic Guegan 2021-04-28 15:07:12 +02:00
parent 778e43c0a2
commit 067d6e340b
9 changed files with 45 additions and 37 deletions

View file

@ -5,7 +5,7 @@
u32* mb2_find_tag(u32 *mb2_info_addr, char type){
PAGING_MAP(mb2_info_addr);
u32 size=(u32)mb2_info_addr[0];
u32 size=(u32)mb2_info_addr[0]; // Todo: check for size
for(u64 i=0;i<size/8;i++)
PAGING_MAP(mb2_info_addr+i*4096);
@ -38,6 +38,7 @@ char mb2_find_bootloader_name(u32* mb2_info_addr, char *return_name){
}
return 0;
}
char mb2_find_new_rsdp(u32* mb2_info_addr, u64 *return_addr, u32 *return_size){
u32* addr=mb2_find_tag(mb2_info_addr,15);
if(addr){

View file

@ -90,10 +90,6 @@ extern "C" u8* trampoline(u8* mb_infos_addr){
for(u64 i=0;i<=0x1000000;i+=4096){
trampoline_paging_allocate_addr(pml4,i,i,0x3);
}
// Test
for(u64 i=0xe0000000;i<=0xef000000;i+=4096){
trampoline_paging_allocate_addr(pml4,i,i,0x3);
}
// Load new pml4
asm volatile(

View file

@ -1,24 +1,30 @@
#include "boucane.hpp"
#include "core/idt.hpp"
#include "boot/multiboot2.hpp"
#include "core/paging.hpp"
#include "core/apic.hpp"
#include "drivers/acpi.hpp"
#include "drivers/psf.hpp"
#include "drivers/framebuffer.hpp"
#include "libs/stdio.hpp"
#include "core/asm.hpp"
#include "drivers/psf.hpp"
#include "libs/string.hpp"
u64 kernel_vma,stack_pma,userspace_pma;
u64 kvar_kernel_vma;
u64 kvar_stack_pma;
u64 kvar_userspace_pma;
u64 kvar_bss_start;
u64 kvar_bss_end;
u64 kvar_terminus_psf_start;
u64 kvar_terminus_psf_end;
extern "C" void boucane(u64 mb_info){
// Init linker variables
asm volatile ("movq $__kernel_vma, %0":"=m"(kernel_vma));
asm volatile ("movq $__userspace_pma, %0":"=m"(userspace_pma));
asm volatile ("movq $__stack_pma, %0":"=m"(stack_pma));
asm volatile ("call load_gdt");
asm volatile ("movq $__kernel_vma, %0":"=m"(kvar_kernel_vma));
asm volatile ("movq $__userspace_pma, %0":"=m"(kvar_userspace_pma));
asm volatile ("movq $__stack_pma, %0":"=m"(kvar_stack_pma));
asm volatile ("movq $__bss_start, %0":"=m"(kvar_bss_start));
asm volatile ("movq $__bss_end, %0":"=m"(kvar_bss_end));
asm volatile ("movq $res_binary_res_terminus_psf_start, %0":"=m"(kvar_terminus_psf_start));
asm volatile ("movq $res_binary_res_terminus_psf_end, %0":"=m"(kvar_terminus_psf_end));
// Init data structures
asm volatile ("call load_gdt");
paging_enable();
idt_enable_interrupt();
@ -26,7 +32,6 @@ extern "C" void boucane(u64 mb_info){
vga_t_init();
vga_t_clear();
FRAMEBUFFER fb_info;
if(mb2_find_framebuffer((u32*)mb_info, &fb_info)){
if(fb_info.bpp>16){
FB_CFG conf;
@ -36,7 +41,7 @@ extern "C" void boucane(u64 mb_info){
conf.width=fb_info.width;
conf.height=fb_info.height;
framebuffer_init(conf);
psf_init((void*)&res_binary_res_terminus_psf_start);
psf_init((void*)kvar_terminus_psf_start);
__putchar=psf_putchar;
}
}
@ -44,6 +49,5 @@ extern "C" void boucane(u64 mb_info){
// Booting!
printk("Booting Boucane v%d.%d.%d\n",VERSION_MAJOR,VERSION_MINOR, VERSION_PATH);
while(1);
}

View file

@ -9,7 +9,7 @@ int kpages_next=1; // First page is for the pml4
u64* paging_allocate_table(){
u64 addr=(u64)kpages[kpages_next];
u64* allocated=(u64*)(addr-kernel_vma);
u64* allocated=(u64*)(addr-kvar_kernel_vma);
kpages_next++;
return allocated;
}
@ -29,17 +29,17 @@ void paging_enable() {
// Allocate paging for the kernel (to not override the source
// code during the next paging_allocate_table() calls)
paging_allocate_contiguous(userspace_pma/4096);
paging_allocate_contiguous(kvar_userspace_pma/4096);
// Setting up new kernel address space
for(u64 i=0;i<=0x10000000;i+=4096){
paging_allocate_addr(kpages[0],kernel_vma+i,i, 0x3,0); // Identity map
paging_allocate_addr(kpages[0],kvar_kernel_vma+i,i, 0x3,0); // Identity map
}
// 4096 bytes stack
paging_allocate_addr(kpages[0],kernel_vma-4096,stack_pma,0x3,0);
paging_allocate_addr(kpages[0],kvar_kernel_vma-4096,kvar_stack_pma,0x3,0);
// Load new pml4
u64 kpage_phy=((u64)kpages[0]-kernel_vma);
u64 kpage_phy=((u64)kpages[0]-kvar_kernel_vma);
asm volatile(
"mov %0, %%rax \n\t"
"mov %%rax, %%cr3 \n\t"

View file

@ -2,6 +2,7 @@
#include "core/types.hpp"
#define MAX_TABLES 280
#define PAGING_MAX_PAGE (20*512)
/**
* Current number of page (from the beginning of the ram) used by the kernel that
@ -18,15 +19,17 @@
/// @brief Get page address that contain addr
#define PAGE(addr) (addr&(~(0xFFF)))
#define PAGING_MAP(addr) paging_allocate_addr(kpages[0],(u64)(addr),(u64)(addr),PAGING_OPT_P|PAGING_OPT_RW,kernel_vma)
#define MAX_TABLES 280
#define PAGING_MAP(addr) paging_allocate_addr(kpages[0],(u64)(addr),(u64)(addr),PAGING_OPT_P|PAGING_OPT_RW,kvar_kernel_vma)
#define PAGING_MAP_RANGE(addr, n) { \
for(u64 i=0;i<(n);i++){ \
paging_allocate_addr(kpages[0],((u64)(addr))+i,((u64)(addr))+i,PAGING_OPT_P|PAGING_OPT_RW,kvar_kernel_vma); \
}}
/// @brief All PAE table structures are allocated here
extern u64 kpages[MAX_TABLES][512];
/// cf boucane.hpp
extern u64 kernel_vma,stack_pma,userspace_pma;
/// CF boucane.hpp
extern u64 kvar_kernel_vma,kvar_stack_pma,kvar_userspace_pma;
/**
* Setup and enable PAE paging

View file

@ -7,11 +7,11 @@ FB_CFG fb_cfg;
void framebuffer_init(FB_CFG config){
fb_cfg=config;
PAGING_MAP_RANGE(fb_cfg.location, fb_cfg.pitch*fb_cfg.height);
}
void framebuffer_draw(FB_PIXEL p){
u8 *pixel=(u8*)(fb_cfg.location+p.x*(fb_cfg.depth/8)+p.y*fb_cfg.pitch);
PAGING_MAP(pixel);
pixel[0]=p.r;
pixel[1]=p.g;
pixel[2]=p.b;

View file

@ -38,7 +38,6 @@ void psf_putchar(char c){
u8* glyph=(psf_status.psf_addr+psf_status.header.header_length+c*psf_status.header.glyph_size);
FB_PIXEL pixel;
for(int i=0;i<psf_status.header.glyph_height;i++){
PAGING_MAP(glyph);
for(int k=0;k<(psf_status.header.glyph_width/8);k++){
u8 mask=1<<7;
u8 row=*(glyph+k);

View file

@ -11,8 +11,16 @@
#include "libs/string.hpp"
/// @brief Various variables from the linker
extern u64 kernel_vma,stack_pma,userspace_pma;
extern u64 kvar_kernel_vma;
extern u64 kvar_stack_pma;
extern u64 kvar_userspace_pma;
extern u64 kvar_bss_start;
extern u64 kvar_bss_end;
/// @brief Binary references
extern u64 res_binary_res_terminus_psf_start;
extern u64 res_binary_res_terminus_psf_end;
extern u64 kvar_terminus_psf_start;
extern u64 kvar_terminus_psf_end;
// ---- Debug
#define DUMP(var) asm volatile("push $0xABC; push %0; push $0xABC; _%=:; jmp _%="::"r"(var))
#define DUMP2(var1,var2) asm volatile("push $0xABC; push %0; push %1; push $0xABC; _%=:; jmp _%="::"a"(var1),"b"(var2))

View file

@ -1,13 +1,10 @@
#include "string.hpp"
#include "core/paging.hpp"
#include "math.hpp"
void memcpy(void* src, void* dst, u64 size){
u8 *c_src=(u8*)src;
u8 *c_dst=(u8*)dst;
for(u64 i=0;i<size;i++){
PAGING_MAP((c_dst+i));
PAGING_MAP((c_src+i));
*(c_dst+i)=*(c_src+i);
}
}