Long Mode Alignment

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.
Post Reply
johnsa
Member
Member
Posts: 321
Joined: Mon Oct 15, 2007 3:04 pm

Long Mode Alignment

Post by johnsa »

Quick question,

Do procedures/call destinations need to be 16byte aligned in long mode? For some reason if I don't align 16 before procedure call labels things go very badly wrong... even on an empty function like

MyFunction:
ret
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Long Mode Alignment

Post by Combuster »

No you don't need to. Something else is wrong with your setup.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
johnsa
Member
Member
Posts: 321
Joined: Mon Oct 15, 2007 3:04 pm

Re: Long Mode Alignment

Post by johnsa »

Its bizarre.. it's almost as if theres a bug in FASM, where macro expansion isn't working properly... like the target addresses are going wonky when it assembles over macros... before i had any macros i had no issues, now with macros in the code things have gone pear-shaped
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Long Mode Alignment

Post by Combuster »

If you want to blame FASM, prove it. Until then, the compiler is always right.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Long Mode Alignment

Post by AJ »

Hi,

If it's with macro's, look at the expansion - is it what you would expect? If FASM does not have an option to show you the expanded macro, try assembling and then disassembling to see if the output was as expected.

Cheers,
Adam
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Long Mode Alignment

Post by 01000101 »

I agree. Check out the resulting code before you blame the FASM.

But, in case of some rare unknown bug, check to see if you've enabled alignment checking as that would probably cause havoc with unaligned calls/memory operations.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Long Mode Alignment

Post by Combuster »

If its the AC mechanism, it means the AC bit in the flags got set (maybe), AM bit in CR0 got set (very unlikely, even by accident!), and code's running in ring 3 (given the problem description, also unlikely), and the code's doing unaligned accesses.

So I personally wouldn't bet on that.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
stlw
Member
Member
Posts: 359
Joined: Fri Apr 04, 2008 6:43 am
Contact:

Re: Long Mode Alignment

Post by stlw »

johnsa wrote:Quick question,

Do procedures/call destinations need to be 16byte aligned in long mode? For some reason if I don't align 16 before procedure call labels things go very badly wrong... even on an empty function like

MyFunction:
ret
Just a guess, might be this could be related. Anyway, this not supposed to cause a problems alone ...

Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 3A: System Programming Guide, Part 1
5.14.2 64-Bit Mode Stack Frame

In legacy mode, the size of an IDT entry (16 bits or 32 bits) determines the size of interrupt-stack-frame pushes. SS:ESP is pushed only on a CPL change. In 64-bit mode, the size of interrupt stack-frame pushes is fixed at eight bytes. This is because only 64-bit mode gates can be referenced. 64-bit mode also pushes SS:RSP unconditionally, rather than only on a CPL change.
Aside from error codes, pushing SS:RSP unconditionally presents operating systems with a consistent interrupt-stackframe size across all interrupts. Interrupt service-routine entry points that handle interrupts generated by the INTn instruction or external INTR# signal can push an additional error code place-holder to maintain consistency.
In legacy mode, the stack pointer may be at any alignment when an interrupt or exception causes a stack frame to be pushed. This causes the stack frame and succeeding pushes done by an interrupt handler to be at arbitrary alignments. In IA-32e mode, the RSP is aligned to a 16-byte boundary before pushing the stack frame. The stack frame itself is aligned on a 16-byte boundary when the interrupt handler is called. The processor can arbitrarily realign the new RSP on interrupts because the previous (possibly unaligned) RSP is unconditionally saved on the newly aligned stack. The previous RSP will be automatically restored by a subsequent IRET.
Aligning the stack permits exception and interrupt frames to be aligned on a 16-byte boundary before interrupts are re-enabled. This allows the stack to be formatted for optimal storage of 16-byte XMM registers, which enables the interrupt handler to use faster 16-byte aligned loads and stores (MOVAPS rather than MOVUPS) to save and restore XMM registers.
Although the RSP alignment is always performed when LMA = 1, it is only of consequence for the kernel-mode case where there is no stack switch or IST used. For a stack switch or IST, the OS would have presumably put suitably aligned RSP values in the TSS.
johnsa
Member
Member
Posts: 321
Joined: Mon Oct 15, 2007 3:04 pm

Re: Long Mode Alignment

Post by johnsa »

Eventually found the issue... normally in FASM macros have to be declared before use.. for some reason it can't pick them up with a forward reference , which it can do with procs/labels/vars etc.. for some reason when i assembled i had one macro which was a forward reference and it didn't complain about it.. i moved it up before the code that uses it (right at the beginning where i define equates etc) and it came right.. its a bit fishy, but it fixed it...
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Long Mode Alignment

Post by ru2aqare »

johnsa wrote:Eventually found the issue... normally in FASM macros have to be declared before use.. for some reason it can't pick them up with a forward reference , which it can do with procs/labels/vars etc.. for some reason when i assembled i had one macro which was a forward reference and it didn't complain about it.. i moved it up before the code that uses it (right at the beginning where i define equates etc) and it came right.. its a bit fishy, but it fixed it...
It's the same, be it the C preprocessor's macros or MASM's macros... Macros usually have to be defined before use, however undefined macros are usually flagged by the compiler/assembler as undefined identifiers.
johnsa
Member
Member
Posts: 321
Joined: Mon Oct 15, 2007 3:04 pm

Re: Long Mode Alignment

Post by johnsa »

i can forward declare macros in masm no prob.. do it all the time, its a bit of pain not being able too.. it means i have to have one include with code, and then a helper include with macros at the start..
Post Reply