Page 1 of 1

Is there a need to load GDT in long mode?

Posted: Wed Aug 15, 2012 3:52 am
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?

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

Posted: Wed Aug 15, 2012 4:11 am
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.

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

Posted: Wed Aug 15, 2012 5:35 am
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.