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.
GDT Tutorial "Long Mode" 64-bit table "User Mode" offsets are swapped compared to the 32-bit table
Re: GDT Tutorial "Long Mode" 64-bit table "User Mode" offsets are swapped compared to the 32-bit table
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!
- Demindiro
- Member

- Posts: 166
- Joined: Fri Jun 11, 2021 6:02 am
- Libera.chat IRC: demindiro
- Location: Belgium
- Contact:
Re: GDT Tutorial "Long Mode" 64-bit table "User Mode" offsets are swapped compared to the 32-bit table
It is indeed deliberate as it is required for syscall to work properly in long mode:
From https://wiki.osdev.org/SYSENTER#64_bit_mode_2
)
I suggest the GDT Tutorial is amended to include this information.
From https://wiki.osdev.org/SYSENTER#64_bit_mode_2
(someone even put it in bold. Easy to overlook evidentlyIn Long Mode, userland CS will be loaded from STAR 63:48 + 16 and userland SS from STAR 63:48 + 8 on SYSRET. You may need to modify your GDT accordingly.
I suggest the GDT Tutorial is amended to include this information.
