OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 2:44 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Triple fault when loading GDT
PostPosted: Tue Dec 07, 2021 11:50 am 
Offline

Joined: Tue Dec 07, 2021 11:46 am
Posts: 1
I am trying to load a GDT but keep getting a triple fault. I've been trying to do this for far too long so any help would be hugely apppreciated

These are my GDT structs

Code:
struct GlobalDescriptorTableEntry
    {
        unsigned short limit_low;  /* The lower 16 bits of the limit */
        unsigned short base_low;   /* The lower 16 bits of the base */
        unsigned char base_mid;    /* The next 8 bits of the base */
        unsigned char access;      /* Contains access flags */
        unsigned char granularity; /* Specify granularity, and 4 bits of limit */
        unsigned char base_high;   /* The last 8 bits of the base; */
    } __attribute__((packed));     /* It needs to be packed like this, 64 bits */

    struct GlobalDescriptorTablePointer
    {
        unsigned short limit; /* Size of gdt table in bytes*/
        unsigned int base;    /* Address to the first gdt entry */
    } __attribute__((packed));


This is the assembly function to load the gdt
Code:
/* Build GDT pointer and load it */
.global gdt_load_and_set
gdt_load_and_set:
   mov 4(%esp),%eax
   lgdt (%eax)

   mov $0x10, %ax
   mov %ax, %ds
   mov %ax, %es
   mov %ax, %fs
   mov %ax, %gs
   mov %ax, %ss
   jmp  $0x08,$flush

flush:
   ret


And this is the code that setupds the GDT
Code:
GlobalDescriptorTable::GlobalDescriptorTable()
    {
   
        GlobalDescriptorTablePointer gtdPointer;
        gtdPointer.limit = (sizeof(GlobalDescriptorTableEntry) * GDT_NUM_ENTRIES) - 1 ;
        gtdPointer.base = (unsigned int)&gdt_entries;

        CreateEntry(0, 0, 0, 0, 0);                // Null segment
        CreateEntry(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); // Code segment
        CreateEntry(2, 0, 0xFFFFFFFF, 0x92, 0xCF); // Data segment
        // CreateEntry(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); // User mode code segment
        // CreateEntry(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); // User mode data segment
        gdt_load_and_set((unsigned int)&gtdPointer);
    }

    void GlobalDescriptorTable::CreateEntry(int num, unsigned int base, unsigned int limit, unsigned char access, unsigned char gran)
    {
        gdt_entries[num].base_low = (base & 0xFFFF);
        gdt_entries[num].base_mid = (base >> 16) & 0xFF;
        gdt_entries[num].base_high = (base >> 24) & 0xFF;

        gdt_entries[num].limit_low = (limit & 0xFFFF);
        gdt_entries[num].granularity = (limit >> 16) & 0x0F;

        gdt_entries[num].granularity |= gran & 0xF0;
        gdt_entries[num].access = access;
    }


Top
 Profile  
 
 Post subject: Re: Triple fault when loading GDT
PostPosted: Wed Dec 08, 2021 11:20 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
And what is the instruction that is failing? How is the GDT declared (that is, what is gdt_entries)? Is it possibly an alignment issue? The way you declare the GDT structure, the GDT entries have an alignment of 2, but they ought to have one of 8. Is the GDT pointer correct? Have you tried exhausting the debugging capabilities of whatever emulator you are using?

In short, what steps have you taken to investigate or rectify the issue?

There is nothing obviously wrong from what I can see here, though. So the issue may be elsewhere.

_________________
Carpe diem!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot], MichaelPetch and 72 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group