Page 1 of 1

x86_64 interrupt stack alignment

Posted: Wed May 19, 2010 4:24 am
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

Re: x86_64 interrupt stack alignment

Posted: Wed May 19, 2010 6:46 am
by AndreaOrru
I had the same problem, it really made me crazy.

Re: x86_64 interrupt stack alignment

Posted: Wed May 19, 2010 7:01 am
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.

Re: x86_64 interrupt stack alignment

Posted: Wed May 19, 2010 7:07 am
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. :(

Re: x86_64 interrupt stack alignment

Posted: Wed May 19, 2010 7:13 am
by qw
I didn't, I've never programmed in 64 bits mode.

Re: x86_64 interrupt stack alignment

Posted: Wed May 19, 2010 9:11 am
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)