Subject: Re: Data Abort Exceptions
To: Jay Monkman <jtm@smoothsmoothie.com>
From: Richard Earnshaw <rearnsha@arm.com>
List: port-arm
Date: 07/22/2002 17:48:56
> I'm trying to figure out how NetBSD handles misaligned data accesses
> on the ARM, and from what I can see, it doesn't. There'a a comment in 
> arm32/fault.c : data_abort_handler():
>         case FAULT_ALIGN_0:              /* Alignment Fault */
>         case FAULT_ALIGN_1:              /* Alignment Fault */
>                 /*
>                  * Really this should just kill the process.
>                  * Alignment faults are turned off in the kernel
>                  * in order to get better performance from shorts with
>                  * GCC so an alignment fault means somebody has played
>                  * with the control register in the CPU. Might as well
>                  * panic as the kernel was not compiled for aligned
> 		 accesses.
>                  */
> 
> How can this work? What if someone wants to compile an application 
> something like the following:
>         typedef struct {
>              char c  __attribute__ ((packed));
>              long l  __attribute__ ((packed));
>         } foo_t;
> 
> 
>         void func1(void);
>         void func2(long *p);
> 
>         void func1(void)
>         {
>              foo_t foo;
> 
>              foo.c = 'a';
>              foo.l = 0x12345678;
> 
>              func2(&foo.l);
>         }
> 
>         void func2(long *p)
>         {
>              printf("%p : %x\n", p, *p);
>         }
> 
>         int main(void)
>         {
>              func1();
>         }
> 
> Isn't this guaranteed to cause a data abort? Does NetBSD just kill the 
> process that does it?
> 

You need to read the ARM ARM (or any other standard book about the ARM 
architecture) to understand what will happen.

Current releases of NetBSD are configured not to abort the above, but to 
use a load-and-rotate type of operation, so in the above func2 would print 
the value contained by loading (*p & ~3) rotated by 8 bits.

On a.out format builds of NetBSD we rely on this to make half-word 
operations work.

On ELF builds we've outlawed this, and on processors that support faulting 
unaligned accesses we will probably make the CPU abort the access (though 
I don't believe we do so yet).

R.