GDT Problem (C Code with Inline Assembly)

-1

I wrote simple C code about setting GDT, It works well when IDT is not set. but it's not working when I set IDT (works well when there is only an IDT.) I was google it about 2 weeks. plz help me.

code:

#include "gdt.h"

void initGdt(){
    struct GDT *entry;
    struct GDTR *gdtr;

gdtr = (GDTR*)0x100000;     
entry = (GDT*)(0x100000 + sizeof(GDTR));

gdtr->size = (sizeof(GDT) * 3) - 1;
gdtr->offset = (unsigned int)entry;

//Null Descriptor
setGDTEntry(&(entry[0]), 0, 0, 0, 0);

//Kernel Code Descriptor
setGDTEntry(&(entry[1]), 0x00000000, 0xFFFFFFFF, 0x9A, 0xCF);

//Kernel Data Descriptor 
setGDTEntry(&(entry[1]), 0x00000000, 0xFFFFFFFF, 0x92, 0xCF);

__asm__ __volatile("cli");
__asm__ __volatile("mov eax, %0"::"r"(gdtr));
__asm__ __volatile("lgdt [eax]");
__asm__ __volatile("sti");
}

void setGDTEntry(GDT *entry, unsigned int limitAddress, unsigned int baseAddress, unsigned short accessByte, unsigned short flag){
    entry->lowerLimitAddress = limitAddress & 0xFFFF;
    entry->lowerBaseAddress = baseAddress & 0xFFFF;
    entry->middleBaseAddress = (baseAddress >> 16) & 0xFF;
    entry->accessByte = accessByte;
    entry->upperLimitAddressAndFlags = ((limitAddress >> 16) & 0xF) | flag;
    entry->upperBaseAddress = (baseAddress >> 24) & 0xFF;
}

header file:

typedef struct GDT{
    unsigned short lowerLimitAddress;
    unsigned short lowerBaseAddress;
    unsigned char middleBaseAddress;
    unsigned char accessByte;
    unsigned char upperLimitAddressAndFlags;
    unsigned char upperBaseAddress;
}__attribute__((packed)) GDT;

typedef struct GDTR{
    unsigned short size;
    unsigned int offset;
}__attribute__((packed)) GDTR;

void initGdt();
void loadGdt();
void setGDTEntry(GDT *entry, unsigned int limitAddress, unsigned int baseAddress, unsigned short accessByte, unsigned short flag);

if you need idt code or anything about problem, plz leave some comment.

c
operating-system
gdt
asked on Stack Overflow Oct 1, 2018 by Parity

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0