6

I have a huge source code that works on PowerPC. I need to port it to ARM. But, ARM generates h/w exception on unaligned memory accesses. So, I want to find all the possible instances where the unaligned memory access exception could occur. I've considered the following options.

  1. Use -Wcast-align in gcc which would throw warnings for unaligned access.
  2. Make the PowerPC generate unaligned exception. For ARM, there is an option /proc/cpu/alignment by which the user can decide how to handle the exception. But, there is no such option for PowerPC.

My questions are,

  1. Is there a way to make the PowerPC generate unaligned memory access exception?
  2. Is there a better way to find out all occurences of unaligned memory access in the source code?
sigjuice
  • 28,661
  • 12
  • 68
  • 93
linuxfreak
  • 2,068
  • 3
  • 20
  • 29
  • way to make the PowerPC generate unaligned memory access exception - some options in kernel (CONFIG_ALIGNMENT_TRAP, etc)? – someuser Sep 11 '14 at 10:51
  • Also you can just port, catch and fix it. :) – someuser Sep 11 '14 at 10:53
  • PowerPC always generates alignment exceptions, apart from AltiVec loads/stores. Of course your OS may silently handle these exceptions, but that's another story. – Paul R Sep 11 '14 at 10:54
  • @someuser: I cannot port the code now as the hardware is still not ready. – linuxfreak Sep 11 '14 at 10:57
  • @PaulR: Alright. So, do I have to create a /proc/cpu/alignment entry and handle it in the kernel? – linuxfreak Sep 11 '14 at 10:59
  • Sorry - just realised I may be incorrect - different PowerPC CPUs may behave differently in this regard - I was basing this on experience with desktop/server G3/G4/G5 CPUs - embedded may be a different story. – Paul R Sep 11 '14 at 11:03
  • `prctl(PR_SET_UNALIGN, PR_UNALIGN_SIGBUS, 0, 0, 0)` may work. – ninjalj Sep 11 '14 at 18:47
  • Port it to ARM and wait for the crashes. IIRC some newer ARMs support unaligned accesses (if the right bit in some register is set), so this may be unnecessary; the biggest hurdle is more likely to be big-endian vs. little-endian (unless you're using little-endian PPC or big-endian ARM, which is Rather Rare). – tc. Feb 02 '15 at 02:09

2 Answers2

5
  1. It depends on your POWERPC processor. High end server processors like POWER8 will almost never generate alignment exceptions. That being said, often there is a HID SPR bit to make alignment exceptions occur more often. Either way, under Linux, the kernel will handle them and the user won't see it, other than a performance loss. You can set the prctl(PR_UNALIGN_SIGBUS) and this will make the kernel generate a SIGBUS, rather than handle them.

  2. In linux with perf events you can use the alignment-faults events. eg "perf stat -e alignment-faults testcase". Also if you turn on CONFIG_PPC_EMULATED_STATS you will get a debugfs entry called "emulated_instructions" which has a entry for unaligned accesses.

Mikey
  • 167
  • 4
  • Rather than doing all these, isn't it better to compile using -Wcast-align and fix all the issues? – linuxfreak Sep 12 '14 at 09:06
  • Yes, it's best to avoid these when possible. Sometimes it's not possible though and you might want to check. – Mikey Sep 14 '14 at 00:39
3
  1. Yes and no. PowerPC hardware has 32-bit unaligned access in hardware, which can't be overridden easily. But if you are running on "bare metal" as opposed to under an OS you could still force it to generate an exception by mapping a memory area as I/O space. This approach is complicated though, and you'd probably fare better reviewing the code by hand.

    For 64-bit accesses, most PowerPC already generate exceptions on unaligned access, if this is the case for your hardware you could trap it. Again this needs to happen in the kernel so if you're running under an OS you can't easily do it (the kernel would then handle the unaligned access in software without telling you about it).

  2. Not really, this can only really be determined at run-time especially if you are doing lots of pointer arithmetic.

Community
  • 1
  • 1
ThomasH
  • 1,085
  • 10
  • 12
  • 1) I'm running it on OS and not on "bare metal". Is it possible to create /proc/cpu/alignment entry and handle the exception in kernel? 2) Looks like there is no other way to do it at runtime. So, can we use a static analyzer tool to find out all the instances? – linuxfreak Sep 11 '14 at 11:04
  • 2
    It's always possible to handle this kind of exception in the kernel, by emulating the failed access. This is how it's done on MIPS as well. But there's a heavy performance penalty, so the best approach is to weed out as many as you can from the source code and have the emulation as a back-stop. You can have a counter for unaligned accesses exposed in the /proc fs, and you could also log the addresses where the accesses occur and then retrospectively fix them in the source code. I'm not aware of any static analysis tool that could catch all of these, and I don't think it's possible to do 100%. – ThomasH Sep 11 '14 at 11:10