Problems reloading the 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.
User avatar
iansjack
Member
Member
Posts: 4761
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problems reloading the GDT in long mode?

Post by iansjack »

The sections of the manual describing each instruction tells you exactly what each one does.
rpio
Member
Member
Posts: 92
Joined: Sat Feb 20, 2021 3:11 pm

Re: Problems reloading the GDT in long mode?

Post by rpio »

...
Last edited by rpio on Tue Aug 13, 2024 12:21 pm, edited 1 time in total.
nullplan
Member
Member
Posts: 1870
Joined: Wed Aug 30, 2017 8:24 am

Re: Problems reloading the GDT in long mode?

Post by nullplan »

ngx wrote:Oh, if an inter segment jump is a far jump then I can't use it as I am in long mode where it is unavailable.
That is false. However, the immediate form is not available. You can just do the jump indirectly:

Code: Select all

reload: dq label
dw 8
[...]
  jmp far qword [reload]
label:
Or you do it with a far return.
ngx wrote:There is iretq in manual, but not retq or retfq
The instruction is "RET (far)". The Q is there to set the operand size to qword. Not sure if that is even needed in 64-bit mode. In AT&T syntax, suffixes are already used like that normally, and apparently for instructions with implicit operands, Intel decided to adopt the syntax. It used to be "o64 ret" in NASM. I haven't followed those discussions as I tend to just use GAS, so I'm not quite sure when that became RETQ.
Carpe diem!
rpio
Member
Member
Posts: 92
Joined: Sat Feb 20, 2021 3:11 pm

Re: Problems reloading the GDT in long mode?

Post by rpio »

...
Last edited by rpio on Tue Aug 13, 2024 12:20 pm, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5758
Joined: Mon Mar 25, 2013 7:01 pm

Re: Problems reloading the GDT in long mode?

Post by Octocontrabass »

nullplan wrote:You can just do the jump indirectly:

Code: Select all

reload: dq label
dw 8
[...]
  jmp far qword [reload]
label:
...But that will only work on Intel CPUs, not AMD. If your destination address fits in 32 bits, you can make it work for both:

Code: Select all

reload: dd label
dw 8
[...]
  jmp far dword [reload]
label:
If you need all 64 bits, your only choice is RETFQ.
ngx wrote:You told about retq, but what about retfq - as I understood with my tests it needs a segment pushed on to the stack for it to work so it is a far return(which like far jump needs a segment), but why is the retfq not described in intel or nasm manuals, and if it far jumps then retq doesn't or does it?
Which NASM manual are you looking at? It's definitely there in the latest version. Intel doesn't mention it directly, since Intel uses slightly different syntax from NASM. NASM uses "n" and "f" suffixes to indicate near and far returns, and uses "w", "d", and "q" suffixes to indicate word, dword, or qword operand sizes. That means if you want to find information about RETFQ in the Intel manuals, you'll have to look for RET far, 64-bit operand size.
ngx wrote:And also why there needs to be a retq if it is not mentioned in intel manual(only ret and iretq)?
When the "n" or "f" suffix isn't specified, NASM defaults to a near return, so RETQ is a near return with 64-bit operand size.
rpio
Member
Member
Posts: 92
Joined: Sat Feb 20, 2021 3:11 pm

Re: Problems reloading the GDT in long mode?

Post by rpio »

...
Last edited by rpio on Tue Aug 13, 2024 12:20 pm, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5758
Joined: Mon Mar 25, 2013 7:01 pm

Re: Problems reloading the GDT in long mode?

Post by Octocontrabass »

You can find that part in the Intel or AMD manuals. I've already explained where to look.
rpio
Member
Member
Posts: 92
Joined: Sat Feb 20, 2021 3:11 pm

Re: Problems reloading the GDT in long mode?

Post by rpio »

...
Last edited by rpio on Tue Aug 13, 2024 12:19 pm, edited 1 time in total.
nullplan
Member
Member
Posts: 1870
Joined: Wed Aug 30, 2017 8:24 am

Re: Problems reloading the GDT in long mode?

Post by nullplan »

Octocontrabass wrote:...But that will only work on Intel CPUs, not AMD.
Well, I wanted to complain about this, but then I had a more constructive idea: Is there a list of programmer-visible differences between Intel and AMD CPUs? If not, would someone be interested in starting one? Because reading the description of each instruction twice is not my idea of a fun afternoon. I can't, I have to go change my startup code.
Carpe diem!
Octocontrabass
Member
Member
Posts: 5758
Joined: Mon Mar 25, 2013 7:01 pm

Re: Problems reloading the GDT in long mode?

Post by Octocontrabass »

nullplan wrote:Is there a list of programmer-visible differences between Intel and AMD CPUs?
I don't know of any complete lists, but this reference covers most of the instructions that behave differently.
Post Reply