anyway, here is my class to setup things:
Code: Select all
//----------- GlobalDescriptorTable.h------------
[ Message was too long will be posted in follow up
//---------- GlobalDescriptorTable.cpp-----------
#include <GlobalDescriptorTable.h>
#include <iostream>
#include <memset.h>
#define GDT_EMPTY 0 //empty
#define GDT_FULL 1 //full
using std::cout;
using std::endl;
// Returns zero if there are no free descriptors
unsigned int GDT::findEmpty() {
unsigned int i;
for (i = 0; i < MAX_ELEM; i++) {
if (gdt[i] == GDT_EMPTY) {
gdt[i] = GDT_FULL;
return i;
}
}
return 0;
}
size_t GDT::setDesc(size_t selector, uint32 base, uint32 limit, uint32 opt) {
size_t i = selector / 8;
GDTable[i].base_low = base & 0xFFFF;
GDTable[i].base_med = (base >> 16) & 0xFF;
GDTable[i].base_high = base >> 24;
GDTable[i].limit_low = limit & 0xFFFF;
GDTable[i].limit_high = (limit >> 16) & 0x0F;
GDTable[i].access = (opt + D_PRESENT) >> 8;
GDTable[i].granularity = ((opt & 0xff) >> 4);
return i * 8;
}
size_t GDT::addDesc(uint32 base, uint32 limit, uint32 opt) {
size_t i = findEmpty(); //finds first free descriptor and validates it.
GDTable[i].base_low = base & 0xFFFF;
GDTable[i].base_med = (base >> 16) & 0xFF;
GDTable[i].base_high = base >> 24;
GDTable[i].limit_low = limit & 0xFFFF;
GDTable[i].limit_high = (limit >> 16) & 0x0F;
GDTable[i].access = (opt + D_PRESENT) >> 8;
GDTable[i].granularity = ((opt & 0xff) >> 4);
return i * 8;
}
void GDT::remDesc(uint32 selector) {
selector /= 8;
gdt[selector] = GDT_EMPTY; //Marks descriptor as free
memset(&GDTable[selector], 0, sizeof(x86_desc));
}
GDT::GDT() {
unsigned int i;
cout << "Loading GDT..." << endl;
gdt_desc.size = (sizeof(GDTable) - 1);
gdt_desc.offset = reinterpret_cast<uint32>(GDTable);
for (i = 0; i < MAX_ELEM; i++) {
gdt[i] = GDT_EMPTY; //Mark remaining entries as free
}
// The first element of the gdt is not usable and is made null
gdt[0] = GDT_FULL;
memset(&GDTable[0], 0, sizeof(x86_desc));
// kernel will occupy lower 1GB of address space, more than enough ;)
setDesc(KERNEL_CS, 0x00000000, 0x3fffffff, (D_CODE | D_READ | D_BIG | D_BIG_LIM));
setDesc(KERNEL_DS, 0x00000000, 0x3fffffff, (D_DATA | D_WRITE | D_BIG | D_BIG_LIM));
__asm__ __volatile__("lgdt %0" : "=m" (gdt_desc));
// BREAKS HERE
__asm__ __volatile__(
"xor %eax, %eax \n"
"movw $0x10, %ax \n"
"movw %ax, %ds \n"
"movw %ax, %es \n"
"movw %ax, %ss \n"
"movw %ax, %fs \n"
"movw $0x08, %ax \n"
"movw %ax, %cs \n"
);
}
Thanks
proxy