2021-09-14 15:34:14 +00:00
|
|
|
/******************************************************************
|
|
|
|
* gdt.c - GLOBAL DESCRIPTOR TABLE *
|
|
|
|
* Contains function prototypes for setting up the GDT *
|
|
|
|
******************************************************************/
|
2021-09-14 15:35:52 +00:00
|
|
|
#define MAX_DESCRIPTORS 5
|
2021-09-14 15:34:14 +00:00
|
|
|
#include "gdt.h"
|
2021-09-14 15:29:04 +00:00
|
|
|
|
|
|
|
/* Our GDT, with 3 entries, and finally our special GDT pointer */
|
2021-09-14 15:46:50 +00:00
|
|
|
struct GdtEntry gdt[MAX_DESCRIPTORS];
|
|
|
|
struct GdtPointer gp;
|
2021-09-14 15:29:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Setup a descriptor in the Global Descriptor Table */
|
2021-09-14 15:48:57 +00:00
|
|
|
void GdtSetGate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)
|
2021-09-14 15:29:04 +00:00
|
|
|
{
|
2021-09-14 15:46:50 +00:00
|
|
|
/* Sanity check */
|
|
|
|
if (num >= MAX_DESCRIPTORS) return;
|
|
|
|
|
2021-09-14 15:29:04 +00:00
|
|
|
/* Setup the descriptor base address */
|
|
|
|
gdt[num].base_low = (base & 0xFFFF);
|
|
|
|
gdt[num].base_middle = (base >> 16) & 0xFF;
|
|
|
|
gdt[num].base_high = (base >> 24) & 0xFF;
|
|
|
|
|
|
|
|
/* Setup the descriptor limits */
|
|
|
|
gdt[num].limit_low = (limit & 0xFFFF);
|
|
|
|
gdt[num].granularity = ((limit >> 16) & 0x0F);
|
|
|
|
|
|
|
|
/* Finally, set up the granularity and access flags */
|
|
|
|
gdt[num].granularity |= (gran & 0xF0);
|
|
|
|
gdt[num].access = access;
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:48:57 +00:00
|
|
|
struct GdtEntry* GdtGetGate(int num)
|
2021-09-14 15:34:14 +00:00
|
|
|
{
|
|
|
|
if (num>MAX_DESCRIPTORS) return 0;
|
|
|
|
return &gdt[num];
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
2021-09-14 15:34:14 +00:00
|
|
|
|
2021-09-14 15:29:04 +00:00
|
|
|
/* Should be called by main. This will setup the special GDT
|
|
|
|
* pointer, set up the first 3 entries in our GDT, and then
|
|
|
|
* finally call gdt_flush() in our assembler file in order
|
|
|
|
* to tell the processor where the new GDT is and update the
|
|
|
|
* new segment registers */
|
2021-09-14 15:48:57 +00:00
|
|
|
void GdtInstall()
|
2021-09-14 15:29:04 +00:00
|
|
|
{
|
|
|
|
/* Setup the GDT pointer and limit */
|
2021-09-14 15:46:50 +00:00
|
|
|
gp.limit = (sizeof(struct GdtEntry) * 3) - 1;
|
2021-09-14 15:34:14 +00:00
|
|
|
gp.base = (unsigned int)&gdt;
|
2021-09-14 15:29:04 +00:00
|
|
|
|
|
|
|
/* Our NULL descriptor */
|
2021-09-14 15:48:57 +00:00
|
|
|
GdtSetGate(0, 0, 0, 0, 0);
|
2021-09-14 15:29:04 +00:00
|
|
|
|
|
|
|
/* The second entry is our Code Segment. The base address
|
|
|
|
* is 0, the limit is 4GBytes, it uses 4KByte granularity,
|
|
|
|
* uses 32-bit opcodes, and is a Code Segment descriptor.
|
|
|
|
* Please check the table above in the tutorial in order
|
|
|
|
* to see exactly what each value means */
|
2021-09-14 15:48:57 +00:00
|
|
|
GdtSetGate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
|
2021-09-14 15:29:04 +00:00
|
|
|
|
|
|
|
/* The third entry is our Data Segment. It's EXACTLY the
|
|
|
|
* same as our code segment, but the descriptor type in
|
|
|
|
* this entry's access byte says it's a Data Segment */
|
2021-09-14 15:48:57 +00:00
|
|
|
GdtSetGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
|
2021-09-14 15:29:04 +00:00
|
|
|
|
2021-09-14 15:35:52 +00:00
|
|
|
/* User mode Code segment*/
|
2021-09-14 15:48:57 +00:00
|
|
|
GdtSetGate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
|
2021-09-14 15:35:52 +00:00
|
|
|
|
|
|
|
/* User mode data segment*/
|
2021-09-14 15:48:57 +00:00
|
|
|
GdtSetGate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);
|
2021-09-14 15:35:52 +00:00
|
|
|
|
2021-09-14 15:29:04 +00:00
|
|
|
/* Flush out the old GDT and install the new changes! */
|
2021-09-14 15:48:57 +00:00
|
|
|
GdtFlush();
|
2021-09-14 15:29:04 +00:00
|
|
|
}
|
2021-09-14 15:48:57 +00:00
|
|
|
|