FreeBSD arm EABI5 documentation?

Mark Millard marklmi at yahoo.com
Thu Jul 11 15:29:43 UTC 2019



On 2019-Jul-10, at 15:18, Mark Millard <marklmi at yahoo.com> wrote:


> 
> 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.

I had an exchange via Jared McNeill (who is active on NetBSD)
that involved NetBSD's criteria. Overallit went like this for
that aspect of the exchange:

QUOTE
From: Nick Hudson <nick.hudson at gmx.co.uk>
Subject: Re: netbsd32_sendsig_siginfo STACK_ALIGN use problem?
Date: July 11, 2019 at 03:59:52 PDT
To: Jared McNeill <jared.mcneill at live.ca>, Mark Millard <marklmi at yahoo.com>

On 11/07/2019 11:21, Jared McNeill wrote:
> Hi Mark —
> 
> This isn’t really an area I am familiar with. Nick Hudson (in copy), our resident arm expert, can likely help.
> 
> Cheers,
> Jared
> 
> 
>> On Jul 10, 2019, at 7:17 PM, Mark Millard <marklmi at yahoo.com> wrote:
>> 
>> Curious about 32-bit arm stack alignment requirements in netbsd
>> (based on a FreeBSD thread making claims that user space allows
>> 4-byte stack alignment), I went looking around some in NetBSD
>> code. . . .
>> 


8 byte stack alignment is a requirement of

http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042f/IHI0042F_aapcs.pdf

5.2.1.2 Stack constraints at a public interface

The stack must also conform to the following constraint at a public interface:
SP mod 8 = 0. The stack must be double-word aligned

I hope this helps.
END QUOTE

(I was really reporting a coding error in netbsd32_sendsig_siginfo
relative to meeting this criteria.)

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



More information about the freebsd-arm mailing list