Move the GDT
Posted: Sat Jul 24, 2004 8:15 am
I?ve been coding an OS for a while and today I thought it would be good to move the GDT and GDTD to a "safer" location. I dont know why my code doesn?t work. The kernel starts but as soon as I try to read the keyboard, the OS reboots. It doesnt reboot when not trying to move the GDT. This is my code. Note that Im already in PM, and just wanna move the GDT. k_malloc is a memory function for the kernel, used the same way as the regual malloc function. I got three segments; the NULL-segment, a code and a data (just like when I entered PM in the bootloader)
struct GDT_descriptor {
unsigned short limit0;
unsigned short base0;
unsigned char base1;
unsigned char attr1;
unsigned char attr2;
unsigned char base2;
}__attribute__((packed));
typedef struct GDT_descriptor GDT_descriptor;
GDT_descriptor *GDT; // The GDT
struct GDT_descriptor2 {
unsigned short size;
unsigned long GDT;
}__attribute__((packed));
typedef struct GDT_descriptor2 GDT_descriptor2;
GDT_descriptor2 *GDTD; // The GDT descriptor
void move_gdt() {
GDT = (GDT_descriptor *)k_malloc(3*sizeof(GDT_descriptor));
GDT[0].limit0 = GDT[0].base0 = GDT[0].base1 = GDT[0].attr1 = GDT[0].attr2 = GDT[0].base2 = 0; // reserved by Intel
GDT[1].limit0 = 0x0FFFF;
GDT[1].base0 = 0;
GDT[1].base1 = 0;
GDT[1].attr1 = 0x9A;
GDT[1].attr2 = 0xCF;
GDT[1].base2 = 0;
GDT[2].limit0 = 0x0FFFF;
GDT[2].base0 = 0;
GDT[2].base1 = 0;
GDT[2].attr1 = 0x92;
GDT[2].attr2 = 0xCF;
GDT[2].base2 = 0;
GDTD = (GDT_descriptor2 *)k_malloc(sizeof(GDT_descriptor2));
GDTD->size = 24;
GDTD->GDT = GDT;
asm("XOR %AX, %AX");
asm("MOV %DS, %AX");
asm volatile("LGDT (%0) ": :"p" (GDT));
asm("MOV %AX, 8");
asm("MOV %DS, %AX");
asm("MOV %SS, %AX");
}
struct GDT_descriptor {
unsigned short limit0;
unsigned short base0;
unsigned char base1;
unsigned char attr1;
unsigned char attr2;
unsigned char base2;
}__attribute__((packed));
typedef struct GDT_descriptor GDT_descriptor;
GDT_descriptor *GDT; // The GDT
struct GDT_descriptor2 {
unsigned short size;
unsigned long GDT;
}__attribute__((packed));
typedef struct GDT_descriptor2 GDT_descriptor2;
GDT_descriptor2 *GDTD; // The GDT descriptor
void move_gdt() {
GDT = (GDT_descriptor *)k_malloc(3*sizeof(GDT_descriptor));
GDT[0].limit0 = GDT[0].base0 = GDT[0].base1 = GDT[0].attr1 = GDT[0].attr2 = GDT[0].base2 = 0; // reserved by Intel
GDT[1].limit0 = 0x0FFFF;
GDT[1].base0 = 0;
GDT[1].base1 = 0;
GDT[1].attr1 = 0x9A;
GDT[1].attr2 = 0xCF;
GDT[1].base2 = 0;
GDT[2].limit0 = 0x0FFFF;
GDT[2].base0 = 0;
GDT[2].base1 = 0;
GDT[2].attr1 = 0x92;
GDT[2].attr2 = 0xCF;
GDT[2].base2 = 0;
GDTD = (GDT_descriptor2 *)k_malloc(sizeof(GDT_descriptor2));
GDTD->size = 24;
GDTD->GDT = GDT;
asm("XOR %AX, %AX");
asm("MOV %DS, %AX");
asm volatile("LGDT (%0) ": :"p" (GDT));
asm("MOV %AX, 8");
asm("MOV %DS, %AX");
asm("MOV %SS, %AX");
}