FreeBSD arm EABI5 documentation?

Mark Millard marklmi at yahoo.com
Wed Jul 10 22:19:06 UTC 2019



On 2019-Jul-10, at 13:47, adr <adr at SDF.ORG> wrote:

> On Wed, 10 Jul 2019, Ian Lepore wrote:
> 
>> That's... odd.  The arm spec requires the stack to be 8-byte aligned at
>> any public interface.  It's hard to imagine how anything could work
>> properly if it were not, given that the toolchains will assume that 64-
>> bit values are aligned at 64-bit boundaries and will thus generate
>> instructions that require that alignment (require it even if strict
>> alignment checking for most instructions is disabled in the control
>> register).  If you could enter a function with the stack only 4-byte
>> aligned, how would the compiler know it's safe to use something like an
>> LDREXD instruction on a local variable allocated on the stack?
> 
> I have no idea. The only thing I can assure you is that in the code I'm
> talking about (a forth implementation using SDL2) I've never aligned the
> C stack when passing arguments to external functions. Until now!

Curious, I looked around in NetBSD code and found the likes of:

/* ARM-specific macro to align a stack pointer (downwards). */
#define STACK_ALIGNBYTES	(8 - 1)

in netbsd's src/sys/arch/arm/include/param.h .

In src/sys/arch/aarch64/include/param.h there is:

/* AARCH64-specific macro to align a stack pointer (downwards). */
#define STACK_ALIGNBYTES	(16 - 1)


So it appears that the kernel does have stack alignment:
src/sys/sys/param.h has . . .

#if defined(_KERNEL) || defined(__EXPOSE_STACK)
. . .
#ifndef STACK_ALIGNBYTES
#define STACK_ALIGNBYTES	__ALIGNBYTES
#endif
. . .
#define	STACK_ALIGN(sp, bytes)	\
 	((char *)(((unsigned long)(sp)) & ~(bytes)))
. . .
#define	STACK_LEN_ALIGN(len, bytes)	(((len) + (bytes)) & ~(bytes))

There is code for signal delivery:

void
sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
{
. . .	
	fp = getframe(l, sig, &onstack);
	
/* make room on the stack */
	fp--;
	
/* make the stack aligned */
	fp = (struct sigframe_siginfo *)STACK_ALIGN(fp, STACK_ALIGNBYTES);
. . .

AARCH64 even has for runing 32-bit code:

static void
netbsd32_sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
{
. . .
	fp = (struct netbsd32_sigframe_siginfo *)sp;
	fp = (struct netbsd32_sigframe_siginfo *)STACK_ALIGN(fp - 1, 8);
. . .

(But that last looks wrong: 8 should likely be 8-1, as in
STACK_ALIGNBYTES for 32-bit ARM.)

There is:

static size_t
calcstack(struct execve_data * restrict data, const size_t gaplen)
{
. . .
	/* make the stack "safely" aligned */
	return STACK_LEN_ALIGN(stacklen, STACK_ALIGNBYTES);
}

I saw other comments mentioning ' stack "safely" aligned' when
looking around.

So it basically matches up with Ian's comments at the kernel vs. user
space interface, despite the probable netbsd32_sendsig_siginfo error.

===
Mark Millard
marklmi at yahoo.com
( dsl-only.net went
away in early 2018-Mar)



More information about the freebsd-arm mailing list