From 87956a41a3b143a099d2e03266ada0c714760f09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Hord=C3=A9?= Date: Tue, 18 Sep 2018 15:11:50 +0200 Subject: [PATCH] feat: gestion TSS/TSR en cours --- include/asm.h | 2 ++ include/gdt.h | 35 ++++++++++++++++++++--------------- lib/gdt.c | 21 ++++++++++++++++++++- lib/syscall.c | 13 +++++++++++++ system/system.c | 4 ++++ 5 files changed, 59 insertions(+), 16 deletions(-) diff --git a/include/asm.h b/include/asm.h index 4364d8b..ec0a920 100755 --- a/include/asm.h +++ b/include/asm.h @@ -36,6 +36,8 @@ #define rdmsr(reg,low,high) asm volatile ("rdmsr" :: "=a" (low), "=d" (high) : "c" (reg) ) +#define ltr(tss) asm volatile ("ltr %%ax":: "a" (tss)) + /******************************************************************************/ #define outb(port,value) \ diff --git a/include/gdt.h b/include/gdt.h index 4e041af..f7afc17 100755 --- a/include/gdt.h +++ b/include/gdt.h @@ -8,12 +8,13 @@ #define SEL_KERNEL_DATA 0x28 /* selecteur data du kernel */ #define SEL_USER_DATA 0x30 /* selecteur data utilisateur */ +#define SEL_TSS 0x38 /* selecteur TSR */ + #define STACK_OFFSET 0xFFFF /* adresse de la pile du kernel */ -#define SIZEGDT 0x7 /* nombre de descripteurs */ +#define SIZEGDT 0x8 /* nombre de descripteurs */ #define BASEGDT 0x00000800 /* addr de la GDT */ - typedef struct gdtdes { u16 lim0_15; u16 base0_15; @@ -29,22 +30,26 @@ struct gdtr { u32 base; } __attribute__ ((packed)); -struct tss { - u16 previous_task, __previous_task_unused; +typedef struct tss { + u16 prevtask, reserved00; u32 esp0; - u16 ss0, __ss0_unused; + u16 ss0, reserved0; u32 esp1; - u16 ss1, __ss1_unused; + u16 ss1, reserved1; u32 esp2; - u16 ss2, __ss2_unused; + u16 ss2, reserved2; u32 cr3; u32 eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi; - u16 es, __es_unused; - u16 cs, __cs_unused; - u16 ss, __ss_unused; - u16 ds, __ds_unused; - u16 fs, __fs_unused; - u16 gs, __gs_unused; - u16 ldt_selector, __ldt_sel_unused; - u16 debug_flag, io_map; + u16 es, reserved3; + u16 cs, reserved4; + u16 ss, reserved5; + u16 ds, reserved6; + u16 fs, reserved7; + u16 gs, reserved8; + u16 ldt_selector, reserved9; + u16 trapflag, iomap; } __attribute__ ((packed)); + +void inittr(void); +void initgdt(u32 offset); +void makegdtdes(u32 base, u32 limite, u8 acces, u8 flags, gdtdes *desc); diff --git a/lib/gdt.c b/lib/gdt.c index 2e9ef8d..780226b 100755 --- a/lib/gdt.c +++ b/lib/gdt.c @@ -9,13 +9,16 @@ static struct gdtr gdtreg; /* table de GDT */ static gdtdes gdt[SIZEGDT]; +/* TSS */ +static struct tss tss0; + /*******************************************************************************/ /* Initialise la GDT */ void initgdt(u32 offset) { - makegdtdes(0x0, 0x00000, 0x00, 0x00, &gdt[0]); /* selecteur nul */ + makegdtdes(0x0, 0x00000, 0x00, 0x00, &gdt[0]); /* descripteur nul */ makegdtdes(0x0, 0xFFFFF, 0x9B, 0x0D, &gdt[1]); /* code -> SEL_KERNEL_CODE */ makegdtdes(0x0, 0x00000, 0x97, 0x0D, &gdt[2]); /* pile -> SEL_KERNEL_STACK */ makegdtdes(0x0, 0xFFFFF, 0xFF, 0x0D, &gdt[3]); /* code -> SEL_USER_CODE */ @@ -23,6 +26,13 @@ void initgdt(u32 offset) makegdtdes(0x0, 0xFFFFF, 0x93, 0x0D, &gdt[5]); /* data -> SEL_KERNEL_DATA */ makegdtdes(0x0, 0xFFFFF, 0xF3, 0x0D, &gdt[6]); /* data -> SEL_USER_DATA */ + tss0.trapflag = 0x00; + tss0.iomap = 0x00; + tss0.esp0 = 0x1FFF0; + tss0.ss0 = SEL_TSS; + + makegdtdes(&tss0, 0x67, 0xE9, 0x00, &gdt[7]); /* descripteur de tss */ + /* initialise le registre gdt */ gdtreg.limite = SIZEGDT * 8; gdtreg.base = BASEGDT; @@ -36,6 +46,15 @@ void initgdt(u32 offset) /*******************************************************************************/ +/* Initialise le registre de tâche (TSR) */ + +void inittr(void) +{ + ltr(SEL_TSS); +} + +/*******************************************************************************/ + /* Initialise les selecteurs avec la GDT */ void initselectors(u32 executingoffset) diff --git a/lib/syscall.c b/lib/syscall.c index a10a720..66b17a3 100644 --- a/lib/syscall.c +++ b/lib/syscall.c @@ -2,6 +2,19 @@ #include #include + /* 32bit SYSENTER instruction entry. + * + * Arguments: + * %eax System call number. + * %ebx Arg1 + * %ecx Arg2 + * %edx Arg3 + * %esi Arg4 + * %edi Arg5 + * %ebp user stack + * 0(%ebp) Arg6*/ + + /*******************************************************************************/ /* EntrĂ©e pour les appels système SYSENTER */ diff --git a/system/system.c b/system/system.c index 572307e..0e59404 100755 --- a/system/system.c +++ b/system/system.c @@ -56,6 +56,10 @@ int main(void) next: ok(); + print("\033[37m\033[0m -Initilisation des taches (TSR)\000"); + inittr(); + ok(); + print("\033[37m\033[0m -Initilisation des interruptions (IDT/PIC)\000"); initidt(); initpic();