Is there a need to load 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
rdos
Member
Member
Posts: 3307
Joined: Wed Oct 01, 2008 1:55 pm

Is there a need to load GDT in long mode?

Post by rdos »

It looks like the GDT is similar between long mode and protected mode. Assuming the GDT is always residing below 4G, is there a need to reload it in long mode in order to use 64-bit code segments?
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Is there a need to load GDT in long mode?

Post by iansjack »

You set up the GDT in protected mode and don't need to reload it when you change to long mode (as long as you enter valid 64-bit selectors in the table). But note that segments are very different in long mode; in essence you always use a flat memory mode.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Is there a need to load GDT in long mode?

Post by bluemoon »

I would suggest to reload it (if you abused GDT[0] for GDTR) since GDTR is slightly larger for long mode.

In my case, I enter long mode in boot loader so I reloaded it in my kernel after remapped into high address (Note, GDTR takes linear (zero-based logical) address, not physical address).

My GDT looks like this:

Code: Select all

align 16
gdtr	dw 8*8-1
	dq gdt
	dw 0
align 16
gdt	dd 0, 0
	dd 0x0000FFFF, 0x00AF9A00 ; 0x08 CODE64 DPL0
	dd 0x0000FFFF, 0x008F9200 ; 0x10 DATA64 DPL0
	dd 0x0000FFFF, 0x00CFFA00 ; 0x18 CODE32 DPL3
	dd 0x0000FFFF, 0x008FF200 ; 0x20 DATA64 DPL3
	dd 0x0000FFFF, 0x00AFFA00 ; 0x28 CODE64 DPL3
	dd 0, 0, 0, 0		  ; 0x30 TSS
Note that this seemingly strange order indeed fits for syscall.
Post Reply