Page 1 of 1

GDT in Long Mode

Posted: Wed Feb 20, 2008 4:38 pm
by os.hacker64
I realize that there are changes in long mode when it comes to the GDT. I just want to make sure that all the descriptors are 4 bytes and there is a 4 byte null descriptor at the beginning of the GDT:

Code: Select all

GDTR64:;what is loaded into the GDTR when entering long mode
limit dw gdtend64-gdtbeg64;size of the GDT
offset dq gdtbeg64;start address of the 64 bit gdt


gdtbeg64:;beginning of 64 bit gdt
dd 0
;etc...


gdtend64:;end of 64 bit gdt

Posted: Wed Feb 20, 2008 5:06 pm
by exkor
it has always been 8 bytes, same as protected, but tss for instance is 16bytes
Nice pictures in Intel manual Vol 3a (May 2007), sections 3.4.5, 6.2.3

Posted: Wed Feb 20, 2008 5:08 pm
by 01000101
you need to have your 32-bit GDT descriptors, and then your 64-bit one as well. The descriptor for the 64-bit cs is almost identical to the 32-bit one except that it has the L flag set.

Code: Select all

GDTR:					; Global Descriptors Table Register
    dw 4*8-1				; limit of GDT (size minus one)
    dq GDT				; linear address of GDT

GDT:
    dw 0,0,0,0  			; null desciptor
    dw 0FFFFh,0,9200h,08Fh		; flat data desciptor
    dw 0FFFFh,0,9A00h,0CFh		; 32-bit code desciptor
    dw 0FFFFh,0,9A00h,0AFh		; 64-bit code desciptor
then the GDT table gets loaded with the lgdt command before entering PMode, and then can be re-used when enterring 64-bit mode because the 64-bit descriptor is already there.

Code: Select all

lgdt [cs:GDTR]
when you jump into 64-bit mode, just make sure you use the correct descriptor (0x18) for the far jump.