working on gdt

This commit is contained in:
Adrien Bourmault 2019-05-19 23:33:34 +02:00
parent d1a46d7acd
commit 6ccc6157bc
4 changed files with 75 additions and 57 deletions

View File

@ -59,7 +59,7 @@ typedef struct MemoryMap_t MemoryMap_t;
typedef struct MapEntry_t MapEntry_t; typedef struct MapEntry_t MapEntry_t;
typedef struct GdtEntry_t GdtEntry_t; typedef struct GdtEntry_t GdtEntry_t;
typedef struct GdtPtr_t GdtPtr_t; typedef struct GdtPtr_t GdtPtr_t;
typedef struct TssEntry_t TssEntry_t; typedef struct Tss_t Tss_t;
typedef struct TssDescriptor_t TssDescriptor_t; typedef struct TssDescriptor_t TssDescriptor_t;
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//

View File

@ -32,6 +32,7 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#define MINIMUM_RAM_SIZE 16 // Mio, the minimum RAM size. #define MINIMUM_RAM_SIZE 16 // Mio, the minimum RAM size.
#define IOMAP_SIZE (8 * 1024)
#define AVAILABLE_ZONE 1 // Fully usable RAM zone #define AVAILABLE_ZONE 1 // Fully usable RAM zone
#define RESERVED_ZONE 2 // Used by the firmware #define RESERVED_ZONE 2 // Used by the firmware
@ -57,39 +58,55 @@ struct MemoryMap_t {
MapEntry_t entry[MAX_ENTRIES]; MapEntry_t entry[MAX_ENTRIES];
} __attribute__((__packed__)); } __attribute__((__packed__));
// The gdt // The gdt entry
struct GdtEntry_t struct GdtEntry_t
{ {
ushort lowLimit; // lower 16 bits ushort lowLimit; // lower 16 bits
ushort lowBase; // lower 16 bits ushort lowBase; // lower 16 bits
uchar middleBase; // next 8 bits uchar middleBase; // next 8 bits
uchar access; // determine what ring this segment can be used in uchar access; // determine what ring this segment can be used in
uchar granularity; uchar flags;
uchar highBase; // last 8 bits uchar highBase; // last 8 bits
} __attribute__((__packed__)); } __attribute__((__packed__));
struct TssDescriptor_t struct TssDescriptor_t
{ {
ushort limitLow; ushort lowLimit;
ushort base00; ushort lowBase;
uchar base16; uchar middleBase;
uchar access; uchar access;
uchar size; unsigned middleLimit: 4;
uchar base24; unsigned flags: 4;
uint base32; uchar highBase;
uint reserved; uint veryHighBase;
uint reserved;
} __attribute__ ((packed)); } __attribute__ ((packed));
struct TssEntry_t struct Tss_t
{ {
uint reserved0; uint reserved1;
ulong privStackPointer[3]; // stack pointers for CPL 0-2 // Stack pointers (RSP) for privilege levels 0-2
ulong intStackTable[8]; // /!\ [0] is reserved ulong rsp0;
ulong rsp1;
ulong rsp2;
ulong reserved2;
// Interrupt stack table pointers
ulong ist1;
ulong ist2;
ulong ist3;
ulong ist4;
ulong ist5;
ulong ist6;
ulong ist7;
ulong reserved3;
ushort reserved4;
// Offset to the I/O permission bit map from the 64-bit TSS base
ushort iomap_base;
uchar iomap[IOMAP_SIZE];
ulong reserved1;
ushort reserved2;
ushort ioMapOffset;
} __attribute__ ((packed)); } __attribute__ ((packed));
@ -98,7 +115,7 @@ struct TssEntry_t
struct GdtPtr_t struct GdtPtr_t
{ {
uchar limit; // upper 16 bits uchar limit; // upper 16 bits
ushort base; // address of the first entry ulong base; // address of the first entry
} __attribute__((__packed__)); } __attribute__((__packed__));
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//

View File

@ -24,56 +24,57 @@
#include <mm/mm.h> #include <mm/mm.h>
GdtEntry_t gdtEntries[5] = { 0 }; ulong gdtEntries[2] =
TssEntry_t tssEntry = { 0 }; {
(0L), // NULL descriptor
((1L<<43) | (1L<<44) | (1L<<47) | (1L<<53)) // KERNEL CS 0x8
};
//Tss_t tssEntry = { 0 };
GdtPtr_t gdtPtr; GdtPtr_t gdtPtr;
static void SetGdtEntry(int index, uint base, uint limit, uchar access) /* static void SetGdtEntry(int index, uint base, uint limit, uchar access) */
{ /* { */
gdtEntries[index].lowBase = (base & 0xFFFF); /* gdtEntries[index].lowBase = (base & 0xFFFF); */
gdtEntries[index].middleBase = (base >> 16) & 0xFF; /* gdtEntries[index].middleBase = (base >> 16) & 0xFF; */
gdtEntries[index].highBase = (base >> 24) & 0xFF; /* gdtEntries[index].highBase = (base >> 24) & 0xFF; */
gdtEntries[index].lowLimit = (limit & 0xFFFF); /* gdtEntries[index].lowLimit = (limit & 0xFFFF); */
gdtEntries[index].granularity = (limit >> 16) & 0x0F; /* gdtEntries[index].granularity = (limit >> 16) & 0x0F; */
gdtEntries[index].granularity |= 0xA0; /* gdtEntries[index].granularity |= 0xA0; */
// 0x10 is system /* // 0x10 is system */
// 0x80 is present /* // 0x80 is present */
gdtEntries[index].access = access | 0x10 | 0x80; /* gdtEntries[index].access = access | 0x10 | 0x80; */
} /* } */
static void SetTssEntry(uchar index, ulong base, ulong limit) /* static void SetTssEntry(uchar index, ulong base, ulong limit) */
{ /* { */
TssDescriptor_t tssDesc = { 0 }; /* TssDescriptor_t tssDesc = { 0 }; */
tssDesc.limitLow = limit & 0xffff; /* tssDesc.limitLow = limit & 0xffff; */
tssDesc.size = (limit >> 16) & 0xf; /* tssDesc.size = (limit >> 16) & 0xf; */
tssDesc.base00 = base & 0xffff; /* tssDesc.base00 = base & 0xffff; */
tssDesc.base16 = (base >> 16) & 0xff; /* tssDesc.base16 = (base >> 16) & 0xff; */
tssDesc.base24 = (base >> 24) & 0xff; /* tssDesc.base24 = (base >> 24) & 0xff; */
tssDesc.base32 = (base >> 32) & 0xffffffff; /* tssDesc.base32 = (base >> 32) & 0xffffffff; */
tssDesc.reserved = 0; /* tssDesc.reserved = 0; */
tssDesc.access = 0x01 | 0x08 | 0x10 | 0x80; /* tssDesc.access = 0x01 | 0x08 | 0x10 | 0x80; */
memmove(&gdtEntries[index], &tssDesc, sizeof(TssDescriptor_t)); /* memmove(&gdtEntries[index], &tssDesc, sizeof(TssDescriptor_t)); */
} /* } */
void MmInitGdt(void) void MmInitGdt(void)
{ {
//MmStoreGdt(); gdtPtr.limit = sizeof(gdtEntries) - 1;
gdtPtr.base = (ulong)&gdtEntries;
/* SetGdtEntry(1, 0x0, 0xfffff, 0x02 | 0x08); */ MmLoadGdt();
/* tssEntry.ioMapOffset = sizeof(TssEntry_t); */
/* SetTssEntry(2, (ulong)&tssEntry, sizeof(TssEntry_t)); */
//MmLoadGdt();
} }

View File

@ -33,7 +33,7 @@ noreturn void PoShutdownQemu(void)
IoWriteWordOnPort(0x604, 0x2000); IoWriteWordOnPort(0x604, 0x2000);
KeCrashSystem(); KeStartPanic("Shutdown failed lol ;)");
__builtin_unreachable(); __builtin_unreachable();
} }
@ -45,7 +45,7 @@ noreturn void PoShutdownVirtualbox(void)
IoWriteWordOnPort(0x4004, 0x3400); IoWriteWordOnPort(0x4004, 0x3400);
KeCrashSystem(); KeStartPanic("Shutdown failed lol ;)");
__builtin_unreachable(); __builtin_unreachable();
} }
@ -57,7 +57,7 @@ noreturn void PoShutdownBochs(void)
IoWriteWordOnPort(0xB004, 0x2000); IoWriteWordOnPort(0xB004, 0x2000);
KeCrashSystem(); KeStartPanic("Shutdown failed lol ;)");
__builtin_unreachable(); __builtin_unreachable();
} }