Florian Castellane Florian Castellane - 9 months ago 61
Linux Question

Logging and debugging unaligned accesses on Linux / aarch64

How can I log unaligned memory accesses on Linux / aarch64 (Cortex-a57)?

I understand there are two different things involved here:

  1. Choosing to raise an interrupt from the cpu on an unaligned access (ie. interrupts for unaligned memory accesses that would otherwise be supported by the cpu at a performance cost)

  2. Choosing how to handle these interrupts in Linux (log them / fire a SIGBUS / soft emulate unaligned access)

My problem is that first, I do not know how to manage the cpu's control registers from my program (nor if I should actually do it in my userspace application), and second, the
interface for managing the unaligned accesses in Linux seems to be gone (I am using a 4.4.0 kernel), see link below.

Managing unaligned accesses from the kernel:
https://www.kernel.org/doc/Documentation/arm/mem_alignment (likely out-of-date)

Does AArch64 support unaligned access?

Answer Source

You can't do this. Not with Linux, anyway.

Alignment faults for EL0 are governed by the SCTLR_EL1.A bit, but that also affects EL1. Thus even if you wrote a hacky kernel module to enable it (you obviously can't touch privileged system control registers directly from userspace), you're pretty much guaranteed that the kernel's going to panic as soon as the next network packet arrives. The arm64 kernel port relies on having the unaligned access capability provided by AArch64. It doesn't have the ARM port's /proc/cpu/alignment handler, because it doesn't have the legacy of pre-ARMv6 CPUs that didn't support unaligned access at all (well, in any usable fashion at least).

What you can do, though, is use perf tools to monitor any or all of Cortex-A57's microarchitectural PMU events 0x68, 0x69 or 0x6a, to count the unaligned-access-related events which your program triggers. There's no means to trap or debug individual accesses as there might be with the blunt instrument of alignment faults, but otherwise it's arguably more useful since it'll only count events attributable to your program.