diff --git a/src/boucane.cc b/src/boucane.cc index 060f9cd..96c9ba7 100644 --- a/src/boucane.cc +++ b/src/boucane.cc @@ -19,8 +19,33 @@ u64 kvar_terminus_psf_start; u64 kvar_terminus_psf_end; u64 kvar_logo_bmp_start; u64 kvar_logo_bmp_end; +TSS kvar_tss; void (*printk)(char *str,...)=printf; +extern u64 gdt64_tss; + +void configure_tss(){ + u64 tss_addr=(u64)&kvar_tss; + u32 desc1=(tss_addr&0xFFFF)<<16|(sizeof(TSS)&0xFFFF); + tss_addr>>=16; + u32 desc2=(tss_addr&0xFF)|((tss_addr>>8)<<24); + tss_addr>>=16; + u32 desc3=tss_addr; + + // Options + u32 options=0b1001; // 64-bits TSS type + options|=(0b1000<<4); // Present + + // Configure GDT + u32 *gdt_entry=(u32*)&gdt64_tss; + gdt_entry[0]=desc1; + gdt_entry[1]=desc2|(options<<8); + gdt_entry[2]=desc3; + gdt_entry[3]=0; + + // Configure segment + kvar_tss.rsp0=(u64)VIRT(kvar_stack_pma); +} extern "C" void boucane(u64 mb_info){ // Init linker variables @@ -35,6 +60,7 @@ extern "C" void boucane(u64 mb_info){ asm volatile ("movq $res_binary_res_logo_bmp_end, %0":"=m"(kvar_logo_bmp_end)); // Init data structures + configure_tss(); asm volatile ("call load_gdt"); paging_enable(); memtext_init(); @@ -74,5 +100,8 @@ extern "C" void boucane(u64 mb_info){ mem/=1024; printk("RAM:%dMB\n", mem); } + + printk("%x",&kvar_tss); + DUMP(&gdt64_tss); while(1); } \ No newline at end of file diff --git a/src/boucane.hpp b/src/boucane.hpp index 125b192..db27420 100644 --- a/src/boucane.hpp +++ b/src/boucane.hpp @@ -10,22 +10,42 @@ #include "libs/stdio.hpp" #include "libs/string.hpp" +// ---- 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)) + +// Functions +extern void (*printk)(char *str,...); + +typedef struct { + u32 reserved; + u64 rsp0; + u64 rsp1; + u64 rsp2; + u64 reserved2; + u64 ist1; + u64 ist2; + u64 ist3; + u64 ist4; + u64 ist5; + u64 ist6; + u64 ist7; + u64 reserved3; + u16 reserved4; + u16 iomap_address; +} __attribute__((packed)) TSS; + /// @brief Various variables from the linker 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; +extern TSS kvar_tss; + /// @brief Binary references extern u64 kvar_terminus_psf_start; extern u64 kvar_terminus_psf_end; extern u64 kvar_logo_bmp_start; -extern u64 kvar_logo_bmp_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)) - - -extern void (*printk)(char *str,...); \ No newline at end of file +extern u64 kvar_logo_bmp_end; \ No newline at end of file diff --git a/src/core/asm.hpp b/src/core/asm.hpp index 8a931f6..a1d7437 100644 --- a/src/core/asm.hpp +++ b/src/core/asm.hpp @@ -21,3 +21,6 @@ #define inb(port,dst) \ asm volatile ("inb %%dx, %%al": "=a" (dst) : "d" (port)) + +#define lpml4(pml4) \ + asm volatile ("mov %0, %%rax; mov %%rax, %%cr3":: "r" (pml4)) diff --git a/src/core/gdt.S b/src/core/gdt.S index f928b3f..c32db59 100644 --- a/src/core/gdt.S +++ b/src/core/gdt.S @@ -1,4 +1,5 @@ .code64 +.global gdt64_tss gdt64: gdt64_null: @@ -15,6 +16,22 @@ gdt64: .byte 0 .byte 0b10010010 .word 0 + gdt64_cs_user: + .long 0 + .byte 0 + .byte 0b11111100 + .byte 0b00100000 + .byte 0 + gdt64_ds_user: + .long 0 + .byte 0 + .byte 0b11110010 + .word 0 + gdt64_tss: + .long 0 + .long 0 + .long 0 + .long 0 gdtr: .word . - gdt64 - 1 .quad gdt64 diff --git a/src/core/paging.cc b/src/core/paging.cc index b12fb4c..bad776f 100644 --- a/src/core/paging.cc +++ b/src/core/paging.cc @@ -1,4 +1,5 @@ #include "paging.hpp" +#include "core/asm.hpp" #include "core/types.hpp" #include "libs/stdio.hpp" #include "libs/string.hpp" @@ -47,10 +48,7 @@ void paging_enable() { // Load new pml4 u64 kpage_phy=((u64)kpages[0]-kvar_kernel_vma); - asm volatile( - "mov %0, %%rax \n\t" - "mov %%rax, %%cr3 \n\t" - :: "r" (kpage_phy)); + lpml4(kpage_phy); } u64* paging_allocate_contiguous(int npages){ @@ -187,6 +185,13 @@ void paging_allocate_addr(u64* pml4_table, u64 virt, u64 phy, u16 options, char u64* paging_create_task(int npages){ u64 *pml4=PAGE_ALLOCATE(); - // paging_allocate_addr(pml4, 0, (u64)PAGE_ALLOCATE(), PAGING_OPT_P|PAGING_OPT_RW, 0); + + for(int i=0;i>39&0x1FF; + pml4[pml4_entry]=kpages[0][pml4_entry]; + return pml4; } \ No newline at end of file diff --git a/src/core/paging.hpp b/src/core/paging.hpp index 641821f..caaaf9e 100644 --- a/src/core/paging.hpp +++ b/src/core/paging.hpp @@ -1,6 +1,7 @@ #pragma once -#include "core/types.hpp" +#include "types.hpp" +#include "asm.hpp" /** * Current number of page (from the beginning of the ram) used by the kernel that diff --git a/src/drivers/bmp.cc b/src/drivers/bmp.cc index 6f9dad6..39c7d92 100644 --- a/src/drivers/bmp.cc +++ b/src/drivers/bmp.cc @@ -28,7 +28,6 @@ void bmp_draw(u8* bmp_data, int posx, int posy){ p.r=pixel[0]; p.g=pixel[1]; p.b=pixel[2]; - //printk("R%d G%d B%d\n",p.r,p.g,p.b); p.a=0; framebuffer_draw(p);