boucane/src/boucane.cc

144 lines
3.9 KiB
C++
Raw Normal View History

2021-04-21 12:23:54 +02:00
#include "boucane.hpp"
2021-05-01 11:37:52 +02:00
#include "core/apic.hpp"
2021-04-29 08:49:41 +02:00
#include "core/paging.hpp"
#include "drivers/memtext.hpp"
2021-04-21 18:54:50 +02:00
#include "core/idt.hpp"
#include "boot/multiboot2.hpp"
#include "drivers/framebuffer.hpp"
2021-04-29 08:49:41 +02:00
#include "drivers/psftext.hpp"
#include "drivers/vgatext.hpp"
#include "libs/stdio.hpp"
2021-04-28 15:07:12 +02:00
#include "libs/string.hpp"
#include "drivers/bmp.hpp"
2021-05-01 11:37:52 +02:00
#include "core/scheduler.hpp"
/// @brief Temporary
#define TASK_DELAY 10000000
2021-04-28 15:07:12 +02:00
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;
u64 kvar_logo_bmp_start;
u64 kvar_logo_bmp_end;
2021-04-30 10:43:44 +02:00
TSS kvar_tss;
2021-04-29 08:49:41 +02:00
void (*printk)(char *str,...)=printf;
2021-04-30 10:43:44 +02:00
extern u64 gdt64_tss;
void configure_tss(){
2021-05-01 11:37:52 +02:00
// Get TSS physical address
2021-05-02 14:46:18 +02:00
u64 tss_addr=(u64)&kvar_tss;
2021-05-01 11:37:52 +02:00
u32 limit=sizeof(TSS);
2021-04-30 10:43:44 +02:00
2021-05-01 11:37:52 +02:00
u32 desc1=(tss_addr&0xFFFF)<<16|(limit&0xFFFF);
2021-05-02 14:46:18 +02:00
tss_addr>>=16;
2021-05-01 11:37:52 +02:00
u32 desc2=(tss_addr&0xFF);
2021-05-02 14:46:18 +02:00
desc2|=0b11101001<<8; // Type, Permission, present
tss_addr>>=8;
desc2|=tss_addr<<24;
tss_addr>>=8;
u32 desc3=tss_addr;
2021-04-30 10:43:44 +02:00
// Configure GDT
u32 *gdt_entry=(u32*)&gdt64_tss;
gdt_entry[0]=desc1;
2021-05-01 11:37:52 +02:00
gdt_entry[1]=desc2;
2021-04-30 10:43:44 +02:00
gdt_entry[2]=desc3;
gdt_entry[3]=0;
// Configure segment
kvar_tss.rsp0=(u64)VIRT(kvar_stack_pma);
2021-05-02 14:46:18 +02:00
kvar_tss.iomap_address=0;
2021-05-01 11:37:52 +02:00
asm(
"mov $0x28, %ax \n\t"
"ltr %ax"
);
}
void task1(){
2021-05-02 14:46:18 +02:00
while(1){
asm("mov $1, %rdi;int $0x30");
for(u32 delay=0;delay<=TASK_DELAY;delay++){}
2021-05-02 14:46:18 +02:00
}
}
void task2(){
while(1){
asm("mov $2, %rdi;int $0x30");
for(u32 delay=0;delay<=TASK_DELAY;delay++){}
}
}
void task3(){
while(1){
asm("mov $3, %rdi;int $0x30");
for(u32 delay=0;delay<=TASK_DELAY;delay++){}
2021-05-02 14:46:18 +02:00
}
2021-04-30 10:43:44 +02:00
}
2021-04-29 08:49:41 +02:00
2021-04-28 10:46:36 +02:00
extern "C" void boucane(u64 mb_info){
// Init linker variables
2021-04-28 15:07:12 +02:00
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));
asm volatile ("movq $res_binary_res_logo_bmp_start, %0":"=m"(kvar_logo_bmp_start));
asm volatile ("movq $res_binary_res_logo_bmp_end, %0":"=m"(kvar_logo_bmp_end));
2021-04-21 12:23:54 +02:00
2021-04-27 19:02:17 +02:00
// Init data structures
2021-04-28 15:07:12 +02:00
asm volatile ("call load_gdt");
2021-05-01 11:37:52 +02:00
configure_tss();
paging_enable();
2021-04-29 08:49:41 +02:00
memtext_init();
2021-04-27 19:02:17 +02:00
idt_enable_interrupt();
2021-05-01 11:37:52 +02:00
apic_enable();
2021-04-28 10:46:36 +02:00
// Looking for framebuffer
FRAMEBUFFER fb_info;
2021-04-28 10:46:36 +02:00
if(mb2_find_framebuffer((u32*)mb_info, &fb_info)){
if(fb_info.bpp>16){
FB_CFG conf;
conf.depth=fb_info.bpp;
conf.location=fb_info.addr;
conf.pitch=fb_info.pitch;
conf.width=fb_info.width;
conf.height=fb_info.height;
framebuffer_init(conf);
2021-04-29 08:49:41 +02:00
psftext_init((void*)kvar_terminus_psf_start);
__putchar=psftext_putchar;
}
else{
vgatext_init();
__putchar=vgatext_putchar;
}
}
2021-04-28 10:46:36 +02:00
// Booting!
printk("Booting Boucane v%d.%d.%d\n",VERSION_MAJOR,VERSION_MINOR, VERSION_PATH);
2021-05-02 14:46:18 +02:00
printk("System informations -- ");
char bootloader[20];
if(mb2_find_bootloader_name((u32*)mb_info,bootloader)){
2021-05-02 14:46:18 +02:00
printk("BOOT:%s ", bootloader);
}
MEM_INFO mem_infos;
if(mb2_find_mem((u32*)mb_info,&mem_infos)){
u64 mem=mem_infos.mem_upper-mem_infos.mem_lower;
mem/=1024;
printk("RAM:%dMB\n", mem);
}
printk("Launching processes... ");
create_task((void*)task1, 100);
create_task((void*)task2, 100);
create_task((void*)task3, 100);
2021-05-02 14:46:18 +02:00
scheduler_start();
2021-04-21 12:23:54 +02:00
while(1);
}