Page 1 of 1

GDT Tutorial "Long Mode" 64-bit table "User Mode" offsets are swapped compared to the 32-bit table

Posted: Fri Aug 14, 2026 8:57 am
by jhosdev
https://wiki.osdev.org/GDT_Tutorial#Fla ... Mode_Setup

The 32-bit table shows rows:
Offset 0x18: "User Mode Code Segment"
Offset 0x20: "User Mode Data Segment"

while the 64-bit table shows these:
Offset 0x18: "User Mode Data Segment"
Offset 0x20: "User Mode Code Segment"

To rephrase, in the 64-bit table, the Data row is first with the Code row second, instead of vice-versa like the 32-bit table.

Questions:
* Is this deliberate?
* Is it common for 32-bit and 64-bit to have this difference in offsets?
* If it is common, why?

Suggestion: If it is an oversight, I can switch the 64-bit user rows. If it is deliberate, an explanation would be helpful.

Re: GDT Tutorial "Long Mode" 64-bit table "User Mode" offsets are swapped compared to the 32-bit table

Posted: Fri Aug 14, 2026 9:04 am
by nullplan
jhosdev wrote: Fri Aug 14, 2026 8:57 am * Is this deliberate?
* Is it common for 32-bit and 64-bit to have this difference in offsets?
* If it is common, why?
This is probably deliberate to have the setup right for the syscall instruction. That instruction makes assumptions about the layout of the GDT. Writing this from memory, but I think it requires that the kernel code segment precede the kernel data segment, and that the 32-bit user code segment precede the user data segment, which in turn precedes the 64-bit user code segment. If you don't use a 32-bit user code segment, you can leave it out, but you still need the rest to work. Therefore, for a 64-bit OS with no legacy mode, it is necessary to swap the user code and data segment around.