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 GdtEntry_t GdtEntry_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;
//----------------------------------------------------------------------------//

View File

@ -32,6 +32,7 @@
//----------------------------------------------------------------------------//
#define MINIMUM_RAM_SIZE 16 // Mio, the minimum RAM size.
#define IOMAP_SIZE (8 * 1024)
#define AVAILABLE_ZONE 1 // Fully usable RAM zone
#define RESERVED_ZONE 2 // Used by the firmware
@ -57,39 +58,55 @@ struct MemoryMap_t {
MapEntry_t entry[MAX_ENTRIES];
} __attribute__((__packed__));
// The gdt
// The gdt entry
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 flags;
uchar highBase; // last 8 bits
} __attribute__((__packed__));
struct TssDescriptor_t
{
ushort limitLow;
ushort base00;
uchar base16;
ushort lowLimit;
ushort lowBase;
uchar middleBase;
uchar access;
uchar size;
uchar base24;
uint base32;
unsigned middleLimit: 4;
unsigned flags: 4;
uchar highBase;
uint veryHighBase;
uint reserved;
} __attribute__ ((packed));
struct TssEntry_t
struct Tss_t
{
uint reserved0;
uint reserved1;
ulong privStackPointer[3]; // stack pointers for CPL 0-2
ulong intStackTable[8]; // /!\ [0] is reserved
// Stack pointers (RSP) for privilege levels 0-2
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));
@ -98,7 +115,7 @@ struct TssEntry_t
struct GdtPtr_t
{
uchar limit; // upper 16 bits
ushort base; // address of the first entry
ulong base; // address of the first entry
} __attribute__((__packed__));
//----------------------------------------------------------------------------//

View File

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

View File

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