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

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
Post Reply
jhosdev
Posts: 1
Joined: Fri Aug 14, 2026 8:24 am

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

Post 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.
nullplan
Member
Member
Posts: 2033
Joined: Wed Aug 30, 2017 8:24 am

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

Post 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.
Carpe diem!
Post Reply