From 7d5a9792e57b4088cce5cc97837eb04016b57a4d Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 31 Oct 2019 00:49:00 +0100 Subject: Implemented basic syscalls and user mode Doesn't completely work right now --- src/kernel/gdt/gdt.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-) (limited to 'src/kernel/gdt/gdt.c') diff --git a/src/kernel/gdt/gdt.c b/src/kernel/gdt/gdt.c index a4d6a57..b933950 100644 --- a/src/kernel/gdt/gdt.c +++ b/src/kernel/gdt/gdt.c @@ -1,4 +1,8 @@ +#include +#include #include +#include +#include struct gdt_entry { unsigned short limit_low; @@ -14,12 +18,46 @@ struct gdt_ptr { void *base; } __attribute__((packed)); -struct gdt_entry gdt[3]; +struct gdt_entry gdt[6]; struct gdt_ptr gp; +struct tss_entry_struct { + uint32_t prev_tss; + uint32_t esp0; + uint32_t ss0; + uint32_t esp1; + uint32_t ss1; + uint32_t esp2; + uint32_t ss2; + uint32_t cr3; + uint32_t eip; + uint32_t eflags; + uint32_t eax; + uint32_t ecx; + uint32_t edx; + uint32_t ebx; + uint32_t esp; + uint32_t ebp; + uint32_t esi; + uint32_t edi; + uint32_t es; + uint32_t cs; + uint32_t ss; + uint32_t ds; + uint32_t fs; + uint32_t gs; + uint32_t ldt; + uint16_t trap; + uint16_t iomap_base; +} __attribute__((packed)); + +struct tss_entry_struct tss_entry; + extern void gdt_flush(); -void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran) { +extern void tss_flush(); + +void gdt_set_gate(int32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) { // Set descriptor base address gdt[num].base_low = (base & 0xFFFF); gdt[num].base_middle = (base >> 16) & 0xFF; @@ -36,7 +74,7 @@ void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned cha void gdt_install() { // Set GDT pointer and limit - gp.limit = (sizeof(struct gdt_entry) * 3) - 1; + gp.limit = (sizeof(struct gdt_entry) * 6) - 1; gp.base = &gdt; // NULL descriptor @@ -48,8 +86,37 @@ void gdt_install() { // Data segment gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); + // User mode code segment + gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); + + // User mode data segment + gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); + + // Write TSS + tss_write(5, 0x10, 0x0); + // Remove old GDT and install the new changes! gdt_flush(); + tss_flush(); // FAILS vga_log("Installed Global Descriptor Table", 2); -} \ No newline at end of file +} + +void tss_write(int32_t num, uint16_t ss0, uint32_t esp0) { + uint32_t base = (uint32_t) &tss_entry; + uint32_t limit = base + sizeof(tss_entry); + + gdt_set_gate(num, base, limit, 0xE9, 0x00); + + memset(&tss_entry, 0, sizeof(tss_entry)); + + tss_entry.ss0 = ss0; + tss_entry.esp0 = esp0; + + tss_entry.cs = 0x0b; + tss_entry.ss = tss_entry.ds = tss_entry.es = tss_entry.fs = tss_entry.gs = 0x13; +} + +void set_kernel_stack(uint32_t stack) { + tss_entry.esp0 = stack; +} -- cgit v1.2.3