Page 1 of 1

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

Posted: Fri Jul 16, 2010 8:34 am
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

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

Posted: Fri Jul 16, 2010 8:50 am
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

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

Posted: Fri Jul 16, 2010 10:08 am
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)?

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

Posted: Sat Jul 17, 2010 1:35 am
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;

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

Posted: Sat Jul 17, 2010 2:05 am
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

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

Posted: Sat Jul 17, 2010 2:12 am
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.

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

Posted: Sat Jul 17, 2010 2:22 am
by gerryg400
So the variables declaration order is important?
Vitally!

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

Posted: Sat Jul 17, 2010 4:49 am
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

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

Posted: Sat Jul 17, 2010 6:57 am
by Combuster
dukedevon wrote:So the variables declaration order is important?
As a C programmer, you should know that!