From f63afbb3b1c6ab8e8bd81042e4d53776ca171855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Sat, 26 Nov 2016 13:20:57 +0100 Subject: [PATCH] [USER] TSS --- kernel/arch/gdt.c | 8 ++++++-- kernel/arch/registers.S | 6 ++++++ kernel/boot/kmain.c | 13 ------------- kernel/cpu/cpu.c | 5 ++--- kernel/include/cpu.h | 3 ++- kernel/include/gdt.h | 26 ++++++++++++++++++++++++++ 6 files changed, 42 insertions(+), 19 deletions(-) diff --git a/kernel/arch/gdt.c b/kernel/arch/gdt.c index c7729a4..1024565 100644 --- a/kernel/arch/gdt.c +++ b/kernel/arch/gdt.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -10,16 +11,19 @@ void gdt_init() { cpu_t *cpu = get_cpu(); + cpu->tss.io_mba = sizeof(tss_t); GDT[0] = 0; GDT[SEG_KCODE/8] = (uint64_t)(GDT_PRESENT | GDT_CODEDATA | GDT_WRITE | GDT_EXECUTE | GDT_64BIT); GDT[SEG_KDATA/8] = (GDT_PRESENT | GDT_CODEDATA | GDT_WRITE); GDT[SEG_UCODE/8] = (GDT_PRESENT | GDT_CODEDATA | GDT_WRITE | GDT_EXECUTE | GDT_64BIT | GDT_RING3); GDT[SEG_UDATA/8] = (GDT_PRESENT | GDT_CODEDATA | GDT_WRITE | GDT_RING3); + GDT[SEG_TSS/8] = (GDT_PRESENT | GDT_TSS | TSS_BLO((uint64_t)&cpu->tss) | TSS_LIM(sizeof(tss_t))); + GDT[SEG_TSS/8 + 1] = (TSS_BHI((uint64_t)&cpu->tss)); - GDTP.len = 5*8-1; + GDTP.len = 7*8-1; GDTP.addr = (uint64_t)&GDT[0]; load_gdt(&GDTP); - + load_tr(SEG_TSS); } diff --git a/kernel/arch/registers.S b/kernel/arch/registers.S index ca3fa1e..9480b66 100644 --- a/kernel/arch/registers.S +++ b/kernel/arch/registers.S @@ -1,3 +1,4 @@ +#include .intel_syntax noprefix #include @@ -41,3 +42,8 @@ load_gdt: .load_gdt: ret +.global load_tr +load_tr: + mov rax, rdi + ltr ax + ret diff --git a/kernel/boot/kmain.c b/kernel/boot/kmain.c index 0ac14c7..c31875f 100644 --- a/kernel/boot/kmain.c +++ b/kernel/boot/kmain.c @@ -30,22 +30,9 @@ int kmain(uint64_t multiboot_magic, void *multiboot_data) pit_init(); process_t *p1 = process_spawn(0); - thread_t *t1 = new_thread(thread_function); process_attach(p1, t1); - scheduler_insert(t1); - t1 = new_thread(thread_function); - process_attach(p1, t1); - scheduler_insert(t1); - t1 = new_thread(thread_function); - process_attach(p1, t1); - scheduler_insert(t1); - t1 = new_thread(thread_function); - process_attach(p1, t1); - scheduler_insert(t1); - t1 = new_thread(thread_function); - process_attach(p1, t1); scheduler_insert(t1); asm("sti"); diff --git a/kernel/cpu/cpu.c b/kernel/cpu/cpu.c index 0e7bf34..ea451e8 100644 --- a/kernel/cpu/cpu.c +++ b/kernel/cpu/cpu.c @@ -62,9 +62,8 @@ void cpu_init() all_ap_started = 1; vmm_set_page(0, TRAMPOLINE_ADDR, 0, 0); - // Keep the GDT mapped for now - /* vmm_set_page(0, TRAMPOLINE_GDT, 0, 0); */ - /* pmm_free(page); */ + vmm_set_page(0, TRAMPOLINE_GDT, 0, 0); + pmm_free(page); debug_info("CPU - Status\n"); for(unsigned int i = 0; i < num_cpu; i++) diff --git a/kernel/include/cpu.h b/kernel/include/cpu.h index ca17aa7..ad68aca 100644 --- a/kernel/include/cpu.h +++ b/kernel/include/cpu.h @@ -34,9 +34,10 @@ typedef struct cpu_t thread_t *current_thread; thread_t *last_thread; process_t *current_process; - uint64_t gdt[5]; + uint64_t gdt[7]; struct gdtp_st gdt_p; thread_t *scheduler; + tss_t tss; }__attribute__((packed)) cpu_t; extern cpu_t cpus[]; diff --git a/kernel/include/gdt.h b/kernel/include/gdt.h index 8db6099..ab60c53 100644 --- a/kernel/include/gdt.h +++ b/kernel/include/gdt.h @@ -4,6 +4,7 @@ #define SEG_KDATA 0x10 #define SEG_UDATA 0x18 #define SEG_UCODE 0x20 +#define SEG_TSS 0x28 #ifdef __ASSEMBLER__ #define GDT_WRITE (1<<41) @@ -20,6 +21,10 @@ #define GDT_RING3 (3LL<<45) #define GDT_TSS (9LL<<40) +#define TSS_BLO(base) ((((base)&0xFFFF)<<16) | ((((base)>>16)&0xFF)<<32) | ((((base)>>24)&0xFF)<<56)) +#define TSS_BHI(base) (((base)>>32)&0xFFFFFFFF) +#define TSS_LIM(lim) (((lim)&0xFFFF) | ((((lim)>>16)&0xF)<<48)) + #include extern uint64_t BootGDT; @@ -32,6 +37,27 @@ struct gdtp_st uint32_t pad2; }__attribute__((packed)); +typedef struct +{ + uint32_t r1; + uint64_t rsp0; + uint64_t rsp1; + uint64_t rsp2; + uint64_t r2; + uint64_t ist1; + uint64_t ist2; + uint64_t ist3; + uint64_t ist4; + uint64_t ist5; + uint64_t ist6; + uint64_t ist7; + uint64_t r3; + uint16_t r4; + uint16_t io_mba; +}__attribute__((packed)) tss_t; + void load_gdt(struct gdtp_st *); void gdt_init(); + +void load_tr(uint32_t segment); #endif