GDT in Long Mode

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
os.hacker64
Member
Member
Posts: 149
Joined: Mon Feb 11, 2008 4:43 pm
Location: Limbo City,Afterlife

GDT in Long Mode

Post 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
exkor
Member
Member
Posts: 111
Joined: Wed May 23, 2007 9:38 pm

Post 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
Last edited by exkor on Wed Feb 20, 2008 5:09 pm, edited 1 time in total.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Post 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.
Post Reply