3

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 /proc/cpu/alignment 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)

Related: Does AArch64 support unaligned access?

Community
  • 1
  • 1
Florian Castellane
  • 1,197
  • 2
  • 14
  • 38

1 Answers1

1

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.

Notlikethat
  • 20,095
  • 3
  • 40
  • 77
  • So it isn't possible for the kernel to set a different handling of alignment faults for itself and userspace? As you mentioned, profiling is the way to go here ; I was wondering in case one would need stricter memory checks (if that makes sense) – Florian Castellane Sep 22 '16 at 09:13
  • CPU allows to set different handling for EL1 (kernel) and EL0 (userspace). There are different exceptions. Simply made your own vector table and you are free to go :) – user3124812 Jan 14 '19 at 23:25
  • coming back here after a really long time (had a similar issue again) this would have helped me troubleshoot: `cat 3 > /proc/cpu/alignment` https://stackoverflow.com/a/16549366/2918853 – Florian Castellane Nov 10 '20 at 09:36