Enable TSS segment

This commit is contained in:
Loic Guegan 2021-04-09 18:18:15 +02:00
parent deabd68158
commit c6aa00eea7
6 changed files with 263 additions and 17 deletions

View file

@ -67,6 +67,10 @@ movw $0x18, %ax
movw %ax, %ss
movl $0x50000,%esp
# Load
mov $0x38, %eax
ltr %ax
# Start kernel main function
call bringelle

View file

@ -20,11 +20,11 @@ char mb_load_tag(char **data, char type){
c_tag_type=c_tag_type+tag_size+4;
// Skip padding for 64 bits
int p=c_tag_type;
int p=(int)c_tag_type;
while((p & 0x7) != 0)
p++;
// Assign address after padding
c_tag_type=p;
c_tag_type=(char *)p;
c_tag_size=c_tag_type+4;
}

View file

@ -1,15 +1,28 @@
#include "libc/stdio.h"
#include "utils/pic.h"
#include "boot/multiboot.h"
#include "utils/mem.h"
char show_tics=0;
void utask(){
while(1);
}
void bringelle(){
clear();
printc("Booting Bringelle...",GREEN);
printc("Booting Bringelle...\n",GREEN);
// Kernel boot sequence
pic_enable_interrupt();
print("Interrupts enabled!\n");
printc(" done!\n",GREEN);
// Utask
memcpy((void*)utask,(void*)0x300000, 100); // 100 bytes seems reasonable to load utask
print("Kernel started ");
show_tics=1;
while(1);
}
@ -20,5 +33,9 @@ void clock(){
if(tic>=20){
tic=0;
sec++;
if(show_tics)
putchar('.');
}
}

View file

@ -2,11 +2,14 @@
#include "mem.h"
struct GDT_REGISTER GDTR = { 0, 0 };
GDT_TSS TSS;
void gdt_memcpy(){
GDTR.limit=8*4; // Each entry is 8 bytes and 4 entries
GDTR.limit=8*8; // Each entry is 8 bytes and 8 entries
GDTR.base=0x800;
gdt_write_entry((GDT_ENTRY){0,0,0,0},0); // First one must be null
// ----- Kernel Segments
GDT_ENTRY cs_desc;
cs_desc.base=0;
cs_desc.limit=0xFFFFF;
@ -26,13 +29,50 @@ void gdt_memcpy(){
ss_desc.access=GDT_PR|GDT_PRVL_0|GDT_S|GDT_RW|GDT_DC;
// Write GDT descriptors into memory
gdt_write_entry((GDT_ENTRY){0,0,0,0},GDTR.base); // First one must be null
gdt_write_entry(cs_desc, GDTR.base+8); // Each entry is 64 bits (8 bytes)
gdt_write_entry(ds_desc, GDTR.base+8*2);
gdt_write_entry(ss_desc, GDTR.base+8*3);
gdt_write_entry(cs_desc, 1);
gdt_write_entry(ds_desc, 2);
gdt_write_entry(ss_desc, 3);
// ----- User Segments
GDT_ENTRY ucs_desc;
ucs_desc.base=0x300000;
ucs_desc.limit=0xFFFFF;
ucs_desc.flags=GDT_SZ|GDT_GR;
ucs_desc.access=GDT_PR|GDT_PRVL_3|GDT_S|GDT_EXEC|GDT_RW|GDT_DC;
GDT_ENTRY uds_desc;
uds_desc.base=0x300000;
uds_desc.limit=0xFFFFF;
uds_desc.flags=GDT_SZ|GDT_GR;
uds_desc.access=GDT_PR|GDT_PRVL_3|GDT_S|GDT_RW;
GDT_ENTRY uss_desc;
uss_desc.base=0; // Not used in stack descriptor
uss_desc.limit=0x64; // Define how much entry it can contains
uss_desc.flags=GDT_SZ|GDT_GR;
uss_desc.access=GDT_PR|GDT_PRVL_3|GDT_S|GDT_RW|GDT_DC;
// Write GDT descriptors into memory
gdt_write_entry(ucs_desc, 4); // Each entry is 64 bits (8 bytes)
gdt_write_entry(uds_desc, 5);
gdt_write_entry(uss_desc, 6);
// Init TSS segment
TSS.t_reserved=0;
TSS.io_map=0;
TSS.ss0=0x18;
TSS.esp0=0x50000;
GDT_ENTRY tss_desc;
tss_desc.base=(u32)&TSS; // Not used in stack descriptor
tss_desc.limit=0x68; // Define how much entry it can contains
tss_desc.flags=0;
tss_desc.access=0x89 | GDT_PRVL_3; // Note that 0x89 is specific to TSS!
gdt_write_entry(tss_desc, 7);
}
void gdt_write_entry(GDT_ENTRY entry, u32 addr){
void gdt_write_entry(GDT_ENTRY entry, u32 id){
int descriptor[2];
// First row of the descriptor
@ -47,5 +87,5 @@ void gdt_write_entry(GDT_ENTRY entry, u32 addr){
descriptor[1]=base|access<<8|limit<<16|flags<<20|base2<<24;
// Copy descriptor into memory
memcpy(descriptor,(void*)addr,8);
memcpy(descriptor,(void*)GDTR.base+8*id,8); // Each entry is 64 bits (8 bytes)
}

View file

@ -31,6 +31,35 @@ struct GDT_REGISTER {
u32 base;
} __attribute__((packed));
typedef struct GDT_TSS {
u16 previous_task,previous_task_unused;
u32 esp0;
u16 ss0, ss0_unused;
u32 esp1;
u16 ss1, ss1_unused;
u32 esp2;
u16 ss2, ss2_unused;
u32 cr3;
u32 eip;
u32 eflags;
u32 eax;
u32 ecx;
u32 edx;
u32 ebx;
u32 esp;
u32 ebp;
u32 esi;
u32 edi;
u16 es, es_reserved;
u16 cs, cs_reserved;
u16 ss, ss_reserved;
u16 ds, ds_reserved;
u16 fs, fs_reserved;
u16 gs, gs_reserved;
u16 ldtss, ldtss_reserved;
u16 t_reserved, io_map;
} __attribute__((packed)) GDT_TSS;
/**
* Copy GDT in memory
*/
@ -39,6 +68,6 @@ void gdt_memcpy();
/**
* Write a GDT entry at address addr
*/
void gdt_write_entry(GDT_ENTRY entry, u32 addr);
void gdt_write_entry(GDT_ENTRY entry, u32 id);
#endif