From cf86c19096ce362cb0406fc808540fa82c1626e7 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 13 May 2019 18:07:45 +0200 Subject: [PATCH] working on TSS --- Makefile | 9 ++++++++ include/kernel/base.h | 2 ++ include/kernel/mm.h | 46 ++++++++++++++++++++++++++++++++------ kaleid/kernel/cpu/cpuf.asm | 2 ++ kaleid/kernel/cpu/isr.asm | 2 ++ kaleid/kernel/init/init.c | 2 +- kaleid/kernel/mm/gdt.c | 44 +++++++++++++++++++++++++++--------- 7 files changed, 88 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 6e0aff8..d5b7de2 100644 --- a/Makefile +++ b/Makefile @@ -162,6 +162,15 @@ $(KOBJDIR)/kernel/cpu/idt.o: $(KALEIDDIR)/kernel/cpu/idt.c \ @rm -f $@.1 $@.2 @echo ${CL2}[$@] ${CL}Compiled.${CL3} +$(KOBJDIR)/kernel/mm/gdt.o: $(KALEIDDIR)/kernel/mm/gdt.c \ + $(KALEIDDIR)/kernel/mm/gdt.asm | $(KOBJDIR) + @mkdir -p $(shell dirname $@) + @$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/mm/gdt.asm -o $@.1 + @$(KCC) $< -o $@.2 + @$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@ + @rm -f $@.1 $@.2 + @echo ${CL2}[$@] ${CL}Compiled.${CL3} + $(KOBJDIR)/kernel/cpu/cpuid.o: $(KALEIDDIR)/kernel/cpu/cpuid.c \ $(KALEIDDIR)/kernel/cpu/cpuf.asm | $(KOBJDIR) @mkdir -p $(shell dirname $@) diff --git a/include/kernel/base.h b/include/kernel/base.h index bccb425..113c711 100644 --- a/include/kernel/base.h +++ b/include/kernel/base.h @@ -58,6 +58,8 @@ typedef struct MemoryMap_t MemoryMap_t; typedef struct MapEntry_t MapEntry_t; typedef struct GdtEntry_t GdtEntry_t; typedef struct GdtPtr_t GdtPtr_t; +typedef struct TssEntry_t TssEntry_t; +typedef struct TssDescriptor_t TssDescriptor_t; //----------------------------------------------------------------------------// diff --git a/include/kernel/mm.h b/include/kernel/mm.h index ec1ce6a..cc2949c 100644 --- a/include/kernel/mm.h +++ b/include/kernel/mm.h @@ -60,14 +60,40 @@ struct MemoryMap_t { // The gdt struct GdtEntry_t { - ushort lowLimit; // lower 16 bits - ushort lowBase; // lower 16 bits - uchar middleBase; // next 8 bits - uchar access; // determine what ring this segment can be used in - uchar granularity; - uchar highBase; // last 8 bits + ushort lowLimit; // lower 16 bits + ushort lowBase; // lower 16 bits + uchar middleBase; // next 8 bits + uchar access; // determine what ring this segment can be used in + uchar granularity; + uchar highBase; // last 8 bits } __attribute__((__packed__)); +struct TssDescriptor_t +{ + ushort limitLow; + ushort base00; + uchar base16; + uchar access; + uchar size; + uchar base24; + uint base32; + uint reserved; +} __attribute__ ((packed)); + +struct TssEntry_t +{ + uint reserved0; + + ulong privStackPointer[3]; // stack pointers for CPL 0-2 + ulong intStackTable[8]; // /!\ [0] is reserved + + ulong reserved1; + ushort reserved2; + ushort ioMapOffset; +} __attribute__ ((packed)); + + + // The gdt pointer struct GdtPtr_t { @@ -106,7 +132,13 @@ void MmInitGdt(void); // // Loads the descriptor table // -extern void MmLoadGdt(GdtPtr_t *GdtPtr); +extern void MmLoadGdt(ulong ds, ulong cs, ulong tr); + + +// +// Stores the descriptor table +// +extern void MmStoreGdt(void); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/cpu/cpuf.asm b/kaleid/kernel/cpu/cpuf.asm index 98abe26..ecffec7 100644 --- a/kaleid/kernel/cpu/cpuf.asm +++ b/kaleid/kernel/cpu/cpuf.asm @@ -22,5 +22,7 @@ ; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; +[BITS 64] + %include "kaleid/kernel/cpu/cpuf.inc" diff --git a/kaleid/kernel/cpu/isr.asm b/kaleid/kernel/cpu/isr.asm index 76b05e9..14b0523 100644 --- a/kaleid/kernel/cpu/isr.asm +++ b/kaleid/kernel/cpu/isr.asm @@ -22,6 +22,8 @@ ; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; +[BITS 64] + %include "kaleid/kernel/cpu/cpuf.inc" global IdtInit diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 8e90088..7b2a540 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -44,7 +44,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) // Sanity checks BtDoSanityChecks(mbMagic); - // Memory & scheduler + // Memory MmInitMemoryMap(); // Interrupts launching diff --git a/kaleid/kernel/mm/gdt.c b/kaleid/kernel/mm/gdt.c index f21d63a..aeb752c 100644 --- a/kaleid/kernel/mm/gdt.c +++ b/kaleid/kernel/mm/gdt.c @@ -24,13 +24,11 @@ #include -// The Gdt -GdtEntry_t gdtEntries[5]; +GdtEntry_t gdtEntries[5] = { 0 }; +TssEntry_t tssEntry = { 0 }; GdtPtr_t gdtPtr; -#if 0 -static void SetGdtEntry(int index, uint base, uint limit, uchar access, - uchar granularity) +static void SetGdtEntry(int index, uint base, uint limit, uchar access) { gdtEntries[index].lowBase = (base & 0xFFFF); gdtEntries[index].middleBase = (base >> 16) & 0xFF; @@ -39,10 +37,30 @@ static void SetGdtEntry(int index, uint base, uint limit, uchar access, gdtEntries[index].lowLimit = (limit & 0xFFFF); gdtEntries[index].granularity = (limit >> 16) & 0x0F; - gdtEntries[index].granularity |= granularity & 0xF0; - gdtEntries[index].access = access; + gdtEntries[index].granularity |= 0xA0; + + // 0x10 is system + // 0x80 is present + gdtEntries[index].access = access | 0x10 | 0x80; +} + +static void SetTssEntry(uchar index, ulong base, ulong limit) +{ + TssDescriptor_t tssDesc = { 0 }; + + tssDesc.limitLow = limit & 0xffff; + tssDesc.size = (limit >> 16) & 0xf; + + tssDesc.base00 = base & 0xffff; + tssDesc.base16 = (base >> 16) & 0xff; + tssDesc.base24 = (base >> 24) & 0xff; + tssDesc.base32 = (base >> 32) & 0xffffffff; + tssDesc.reserved = 0; + + tssDesc.access = 0x01 | 0x08 | 0x10 | 0x80; + + memmove(&gdtEntries[index], &tssDesc, sizeof(TssDescriptor_t)); } -#endif void MmInitGdt(void) @@ -50,11 +68,15 @@ void MmInitGdt(void) gdtPtr.limit = (sizeof(GdtEntry_t) * 5) - 1; gdtPtr.base = (uint)(ullong)&gdtEntries; - //SetGdtEntry(0,0,0,0,0); + MmStoreGdt(); - /* XXX set TSS register */ + /* SetGdtEntry(1, 0x0, 0xfffff, 0x02 | 0x08); */ - //MmLoadGdt(&gdtPtr); + /* tssEntry.ioMapOffset = sizeof(TssEntry_t); */ + + /* SetTssEntry(2, (ulong)&tssEntry, sizeof(TssEntry_t)); */ + + /* MmLoadGdt(0x8, 0x0, 2 << 3); */ }