x86_64 interrupt stack 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
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

x86_64 interrupt stack alignment

Post by gerryg400 »

Has anyone else been caught out by the fact that x86_64 interrupt stack alignment must be 16 bytes ? And that the processor modifies the stack pointer to enforce this ? I've been getting weird crashes for days. My kmalloc was occasionally allocating thread structures on an 8 byte boundary, and the stack frame within those structures was subsequently misaligned and sometimes thngs got overwritten.

Caveat developor.

- gerryg400
If a trainstation is where trains stop, what is a workstation ?
User avatar
AndreaOrru
Member
Member
Posts: 50
Joined: Fri Apr 25, 2008 2:50 pm
Location: New York

Re: x86_64 interrupt stack alignment

Post by AndreaOrru »

I had the same problem, it really made me crazy.
Close the world, txEn eht nepO
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: x86_64 interrupt stack alignment

Post by qw »

Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 3A wrote: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.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: x86_64 interrupt stack alignment

Post by gerryg400 »

Hmmm thanks... yes. I know. I just didn't know that my kmalloc was broken. It is supposed to return 16byte aligned memory but during the 64bit port things changed a little. :(
If a trainstation is where trains stop, what is a workstation ?
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: x86_64 interrupt stack alignment

Post by qw »

I didn't, I've never programmed in 64 bits mode.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: x86_64 interrupt stack alignment

Post by Owen »

gerryg400 wrote:Hmmm thanks... yes. I know. I just didn't know that my kmalloc was broken. It is supposed to return 16byte aligned memory but during the 64bit port things changed a little. :(
To be standard conformant you must return 16-byte aligned memory on 32-bit too (And, with AVX being introduced, that is now increasing to 32-byte alignment)
Post Reply