OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Aug 17, 2017 9:26 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Loading my GDT causes a triple fault
PostPosted: Sun Mar 05, 2017 1:09 am 
Offline
User avatar

Joined: Mon Feb 20, 2017 1:01 am
Posts: 13
Location: The Moon
I am reading some guides on how to write a simple GDT. After reading 50 different guides, I came up with this:
Code:
//In gdtc.h
#include <stdint.h>

typedef struct {
   uint16_t           lLimit;
   uint16_t           lBase;
   uint8_t            mBase;
   uint8_t            access;
   uint8_t            granularity;
   uint8_t            hBase;
} gdtEntry_t;

struct {
   uint16_t limit;
   uint32_t base;
} gdtPointer;

extern void loadGdt();
void setupGdt();


//In gdtc.c
#include "gdtc.h"

gdtEntry_t gdt[3];


void gdtSetGate(int num, uint64_t base, uint64_t limit, uint8_t access,
   uint8_t gran){
   gdt[num].lBase      = (base & 0xFFFF);
   gdt[num].mBase      = (base >> 16) & 0xFF;
   gdt[num].hBase      = (base >> 24) & 0xFF;

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

   gdt[num].granularity   |= (gran & 0xF0);
   gdt[num].access      = access;
}

void setupGdt(){
   gdtPointer.limit   = (sizeof(gdtEntry_t) * 3) - 1;
   gdtPointer.base      = &gdt;

   gdtSetGate(0, 0, 0, 0, 0);
   gdtSetGate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
   gdtSetGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);

   loadGdt();
}


//And in gdtaglobal loadGdt
extern gdtPointer

loadGdt:
   lgdt[gdtPointer]
   mov ax, 0x10
   mov ds, ax
   mov es, ax
   mov fs, ax
   mov gs, ax
   mov ss, ax
   jmp 0x08:fload
fload:
   ret.asm


When I compile I get this warning:
Code:
gdtc.c:21:19: warning: assignment makes integer from pointer without a cast
  gdtPointer.base  = &gdt;

I tried to load it in my emulator anyway, but of course it triple faults.
Then I changed that line to:
Code:
gdtPointer.base = (uint32_t*) *gdt;

And got
Code:
gdtc.c:21:2: error: cannot convert to a pointer type
  gdtPointer.base  = (uint32_t*) *gdt;


So what am I doing wrong?

_________________
Developing an OS that is so early in development, it can't do anything because stupid me can't figure out interrupts
Image


Top
 Profile  
 
 Post subject: Re: Loading my GDT causes a triple fault
PostPosted: Sun Mar 05, 2017 1:47 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 796
beauhefley wrote:
Code:
struct {
   uint16_t limit;
   uint32_t base;
} gdtPointer;



The structure is not tightly packed, base must be at offset of 2, immediately after limit.

beauhefley wrote:
Code:
void gdtSetGate(int num, uint64_t base, uint64_t limit, uint8_t access,
   uint8_t gran){



Just a nit: you don't need those uint64_t's. uint32_t is enough.


Top
 Profile  
 
 Post subject: Re: Loading my GDT causes a triple fault
PostPosted: Sun Mar 05, 2017 2:24 am 
Offline
Member
Member

Joined: Fri Aug 07, 2015 6:13 am
Posts: 821
Oh! Memories :cry:
You reminded me of the moment when the exact same thing happened to me.
After looking for days trying to figure out what was wrong, nothing was wrong with my code except I forgot:
typedef struct gdt_entry_t
{
...
} __attribute__((packed)) gdt_entry_t;

typedef struct gdt_pointer_t
{
...
} __attribute__((packed)) gdt_pointer_t;

Also as alexfru mentioned uint32_t is enough. Before posting "Loading my IDT causes a triple fault", check if your structs are aligned. :wink:

_________________
OS: Basic OS, Monolithic Kernel, Written in C++
Current Task: Physical Address Space Multi-functional Manager


Top
 Profile  
 
 Post subject: Re: Loading my GDT causes a triple fault
PostPosted: Sun Mar 05, 2017 10:58 am 
Offline
Member
Member
User avatar

Joined: Sat Dec 27, 2014 9:11 am
Posts: 845
Location: Maadi, Cairo, Egypt
Maybe this is a little off-topic to this specific problem, but some references indicate that some CPU implementations require that the GDT and IDT be aligned on a uint64_t boundary. Just to be safe, you should probably do this.

_________________
Byte me; OS site, source code.
You know your OS is advanced when you stop using the Intel programming guide as a reference.


Top
 Profile  
 
 Post subject: Re: Loading my GDT causes a triple fault
PostPosted: Sun Mar 05, 2017 12:51 pm 
Offline
User avatar

Joined: Mon Feb 20, 2017 1:01 am
Posts: 13
Location: The Moon
octacone wrote:
Oh! Memories :cry:
You reminded me of the moment when the exact same thing happened to me.
After looking for days trying to figure out what was wrong, nothing was wrong with my code except I forgot:
typedef struct gdt_entry_t
{
...
} __attribute__((packed)) gdt_entry_t;

typedef struct gdt_pointer_t
{
...
} __attribute__((packed)) gdt_pointer_t;

Also as alexfru mentioned uint32_t is enough. Before posting "Loading my IDT causes a triple fault", check if your structs are aligned. :wink:


Ah OK. I'll try both of those when I get home. I'm currently on my phone. Thank you!

_________________
Developing an OS that is so early in development, it can't do anything because stupid me can't figure out interrupts
Image


Top
 Profile  
 
 Post subject: Re: Loading my GDT causes a triple fault
PostPosted: Sun Mar 05, 2017 1:39 pm 
Offline
Member
Member

Joined: Sat Nov 21, 2009 5:11 pm
Posts: 646
omarrx024 wrote:
Maybe this is a little off-topic to this specific problem, but some references indicate that some CPU implementations require that the GDT and IDT be aligned on a uint64_t boundary. Just to be safe, you should probably do this.

Hold on, what kind of references?


Top
 Profile  
 
 Post subject: Re: Loading my GDT causes a triple fault
PostPosted: Mon Mar 06, 2017 11:17 am 
Offline
Member
Member
User avatar

Joined: Sat Dec 27, 2014 9:11 am
Posts: 845
Location: Maadi, Cairo, Egypt
Gigasoft wrote:
omarrx024 wrote:
Maybe this is a little off-topic to this specific problem, but some references indicate that some CPU implementations require that the GDT and IDT be aligned on a uint64_t boundary. Just to be safe, you should probably do this.

Hold on, what kind of references?

TBH, I don't remember where I read this. Anyway, it wouldn't hurt doing it.

_________________
Byte me; OS site, source code.
You know your OS is advanced when you stop using the Intel programming guide as a reference.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 17 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