Problems creating a GDT, triple fault on mov ds, ax [solved]

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
dukedevon
Posts: 21
Joined: Thu Jul 08, 2010 10:02 am
Location: Karlsruhe, Germany
Contact:

Problems creating a GDT, triple fault on mov ds, ax [solved]

Post by dukedevon »

I'm trying to set up a gdt as shown in james molloys tutorial.
I have been trying to debug it for 3 days now and I'm getting a little frustrated.
I think my gdt_ptr is broken. Bochs outputs the following:

Code: Select all

00022021485e[CPU0 ] load_seg_reg(DS, 0x0010): invalid segment
00022021485e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x0d)
00022021485e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
00022021485i[CPU0 ] CPU is in protected mode (active)
00022021485i[CPU0 ] CS.d_b = 32 bit
00022021485i[CPU0 ] SS.d_b = 32 bit
00022021485i[CPU0 ] EFER   = 0x00000000
00022021485i[CPU0 ] | RAX=0000000000100010  RBX=0000000000002000
00022021485i[CPU0 ] | RCX=00000000000000c0  RDX=00000000000000f2
00022021485i[CPU0 ] | RSP=0000000007feff68  RBP=0000000007feff94
00022021485i[CPU0 ] | RSI=0000000000000000  RDI=0000000000000000
00022021485i[CPU0 ] |  R8=0000000000000000   R9=0000000000000000
00022021485i[CPU0 ] | R10=0000000000000000  R11=0000000000000000
00022021485i[CPU0 ] | R12=0000000000000000  R13=0000000000000000
00022021485i[CPU0 ] | R14=0000000000000000  R15=0000000000000000
00022021485i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf sf zf af PF cf
00022021485i[CPU0 ] | SEG selector     base    limit G D
00022021485i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00022021485i[CPU0 ] |  CS:0020( 0004| 0|  0) 00000000 ffffffff 1 1
00022021485i[CPU0 ] |  DS:0028( 0005| 0|  0) 00000000 ffffffff 1 1
00022021485i[CPU0 ] |  SS:0028( 0005| 0|  0) 00000000 ffffffff 1 1
00022021485i[CPU0 ] |  ES:0028( 0005| 0|  0) 00000000 ffffffff 1 1
00022021485i[CPU0 ] |  FS:0000( 0000| 0|  0) 00000000 00000000 0 0
00022021485i[CPU0 ] |  GS:0000( 0000| 0|  0) 00000000 00000000 0 0
00022021485i[CPU0 ] |  MSR_FS_BASE:0000000000000000
00022021485i[CPU0 ] |  MSR_GS_BASE:0000000000000000
00022021485i[CPU0 ] | RIP=0000000000100039 (0000000000100039)
00022021485i[CPU0 ] | CR0=0x60000011 CR2=0x0000000000000000
00022021485i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00022021485e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
info gdt 0 4:

Code: Select all

GDT[0x00]=??? descriptor hi=0x00000000, lo=0x00000000
GDT[0x01]=??? descriptor hi=0xcf9a0000, lo=0x0000ffff
GDT[0x02]=??? descriptor hi=0xcf920000, lo=0x0000ffff
GDT[0x03]=??? descriptor hi=0xcffa0000, lo=0x0000ffff
GDT[0x04]=??? descriptor hi=0xcff20000, lo=0x0000ffff
My gdt_flush:

Code: Select all

global gdt_flush
gdt_flush:
    mov eax, [esp+4]
    lgdt [eax]
    mov ax, 0x10
    mov ds, ax  <--- triple fault here
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:flush2
flush2:
    ret
and my gdt_installer

Code: Select all

void Gdt::install()
{
    gdt_ptr.limit = (sizeof(gdt_entry_t) * 5) - 1;
    gdt_ptr.base = (unsigned int) &gdt_entries;

    set_gate(0, 0, 0, 0, 0);
    set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
    set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
    set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
    set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);

    gdt_flush((unsigned int) &gdt_ptr);
}
I'm still a rookie when it comes to OS-Development so expect me to ask stupid questions ;-)

Thanks in advance
dukedevon
Last edited by dukedevon on Mon Jul 19, 2010 8:09 am, edited 1 time in total.
FlExOS --- Stay tuned ;-)
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Problems creating a GDT, triple fault on mov ds, ax

Post by gerryg400 »

Code: Select all

GDT[0x01]=??? descriptor hi=0xcf9a0000, lo=0x0000ffff
That's not right. Shouldn't it be ?

Code: Select all

GDT[0x01]=??? descriptor hi=0x00cf9800, lo=0x0000ffff
If a trainstation is where trains stop, what is a workstation ?
User avatar
blobmiester
Member
Member
Posts: 45
Joined: Fri Jul 16, 2010 9:49 am

Re: Problems creating a GDT, triple fault on mov ds, ax

Post by blobmiester »

My first guess is that there is something wrong with your GDT Descriptor structure. Can you post that (along with your GDT Pointer structure)?
User avatar
dukedevon
Posts: 21
Joined: Thu Jul 08, 2010 10:02 am
Location: Karlsruhe, Germany
Contact:

Re: Problems creating a GDT, triple fault on mov ds, ax

Post by dukedevon »

My structs look like this:

Code: Select all

struct gdt_entry_struct {
	unsigned short limit_low;
	unsigned short base_low;
	unsigned char base_middle;
	unsigned char base_high;
	unsigned char access;
	unsigned char granularity;
} __attribute__((packed));
typedef struct gdt_entry_struct gdt_entry_t;

struct gdt_ptr_struct {
	unsigned short limit;
	unsigned int base;
} __attribute__((packed));
typedef struct gdt_ptr_struct gdt_ptr_t;
FlExOS --- Stay tuned ;-)
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Problems creating a GDT, triple fault on mov ds, ax

Post by gerryg400 »

Should be like this

Code: Select all

struct gdt_entry_struct {
   unsigned short limit_low;
   unsigned short base_low;
   unsigned char base_middle;
   unsigned char access;
   unsigned char granularity;
   unsigned char base_high;
} __attribute__((packed));
typedef struct gdt_entry_struct gdt_entry_t;
which will give this, as I suggested before

Code: Select all

GDT[0x01]=??? descriptor hi=0x00cf9800, lo=0x0000ffff
If a trainstation is where trains stop, what is a workstation ?
User avatar
dukedevon
Posts: 21
Joined: Thu Jul 08, 2010 10:02 am
Location: Karlsruhe, Germany
Contact:

Re: Problems creating a GDT, triple fault on mov ds, ax

Post by dukedevon »

So the variables declaration order is important?
Thanks a lot.

Does the following GDT look correct?

Code: Select all

GDT[0x00]=??? descriptor hi=0x00000000, lo=0x00000000
GDT[0x01]=Code segment, base=0x00000000, limit=0xffffffff, Execute/Read, 32-bit
GDT[0x02]=Data segment, base=0x00000000, limit=0xffffffff, Read/Write
GDT[0x03]=Code segment, base=0x00000000, limit=0xffffffff, Execute/Read, 32-bit
GDT[0x04]=Data segment, base=0x00000000, limit=0xffffffff, Read/Write
I'm kinda worried about 0x00, because of the question marks.
FlExOS --- Stay tuned ;-)
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Problems creating a GDT, triple fault on mov ds, ax

Post by gerryg400 »

So the variables declaration order is important?
Vitally!
If a trainstation is where trains stop, what is a workstation ?
JwB
Posts: 5
Joined: Mon Jun 21, 2010 5:59 pm
Contact:

Re: Problems creating a GDT, triple fault on mov ds, ax

Post by JwB »

So the variables declaration order is important?
The CPU expects the data to be in a certain order in memory else it wont know which bit is what :P

0x0 looks correct, should be all 0s
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Problems creating a GDT, triple fault on mov ds, ax

Post by Combuster »

dukedevon wrote:So the variables declaration order is important?
As a C programmer, you should know that!
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply