简体   繁体   中英

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?
  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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM