Q about stack alignment on PowerPC.

Joel Sherrill joel.sherrill at OARcorp.com
Fri Sep 22 17:23:49 UTC 2000


Sergei Organov wrote:
> 
> Hello,
> 
> Both SysV ABI and EABI for PowerPC specify stack pointer alignment
> restrictions (16 and 8 bytes, respectively). Where in RTEMS sources is this
> alignment assured? I can't find it. Neither for initial values of stack
> pointers for tasks nor for interrupt one :-(

Based on the PPC CPU model, PPC_STACK_ALIGNMENT is set. 
CPU_STACK_ALIGNMENT
gets set to this so the generic name follows the PPC CPU model as either 
8 or 16.

> Note that violation of the alignment requirements may result in very odd
> behavior using gcc versions 2.95 and newer. E.g., calling 'printf' with more
> than 8 double arguments will print 9th argument right when invoked from one
> task, and will print garbage when invoked from another. This is due to changes
> in 'va_arg' to align addresses of doubles passed on stack on 8 bytes
> boundary. Code generated by earlier versions of gcc used to work without any
> visible problems with stack pointers aligned on 4 bytes boundary.

The stack alignment factor SHOULD be being dealt with as part of 
context initialization.  Some of the other ports do in fact address
this.
But the PowerPC port does not seem to.  The code is in 
c/src/lib/libcpu/powerpc/*exception_processing/cpu.c in the routine
_CPU_Context_Initialize.  The code that caclulates the stack pointer
is:

 sp = (unsigned32)stack_base + size - CPU_MINIMUM_STACK_FRAME_SIZE;

I would think it should be something like this to be correct:

 sp = ((unsigned32) stack_base + size) & ~CPU_STACK_ALIGNMENT;
 sp -= CPU_MINIMUM_STACK_FRAME_SIZE;

CPU_STACK_ALIGNMENT has to be a power of 2.  The stack grows down
so the "&" is just going to lower the top of the stack 0-15 bytes.

If this works for you, please send a real patch back and let me know.
 
> Thanks in advance.
> 
> Sergei.

-- 
Joel Sherrill, Ph.D.             Director of Research & Development
joel at OARcorp.com                 On-Line Applications Research
Ask me about RTEMS: a free RTOS  Huntsville AL 35805
   Support Available             (256) 722-9985



More information about the users mailing list