I have written a GDT in C for a 32-bit x86 system kernel, but I'm not sure it is correct. I have a GDT in the bootloader which is fine (in assembly) but I want one in the Kernel too, incase the bootloader is changed.
My structures are written like this:
Code: Select all
struct gdtstruct {
unsigned short seglimit;
unsigned short lowbase;
unsigned byte midbase;
unsigned byte access;
unsigned byte granularity;
unsigned byte highbase;
};
struct gdtrstruct {
unsigned short limit;
unsigned int base;
};
static struct gdtstruct gdt[2];
static struct gdtrstruct gdtr;
I then have functions to setup the GDT:
Code: Select all
void setgdt(int i, uword low, ubyte mid, ubyte high, uword limit, ubyte access, ubyte gran) {
gdt[i].lowbase = low;
gdt[i].midbase = mid;
gdt[i].highbase = high;
gdt[i].seglimit = limit;
gdt[i].access = access;
gdt[i].granularity = gran;
}
void installgdt() {
/* Add Descriptors to GDT */
setgdt(0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0); /* NULL Descriptor */
setgdt(1, 0x0, 0x0, 0x0, 0xFFFF, 0x9A, 0xCF); /* Code Descriptor */
setgdt(2, 0x0, 0x0, 0x0, 0xFFFF, 0x92, 0xCF); /* Data Descriptor */
/* Setup GDTR */
gdtr.base = (udword)&gdt[0]; /* Start of GDT */
gdtr.limit = (sizeof (struct gdtstruct) * 2) - 1; /* End of GDT */
/* Install GDT into GDTR */
__asm lgdt [gdtr];
}
Code: Select all
installgdt()
Does this look OK?