Search code examples
cgccembedded-linuxmemory-alignmentpowerpc

Generate unaligned memory access exception in PowerPC


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?

Solution

    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.