How to trap unaligned memory access?

前端 未结 2 876
梦如初夏
梦如初夏 2020-11-30 09:55

I am working on a pet open-source project that implements some stream cipher algorithms and I am having trouble with a bug triggered only when I run it on an ARM processor.

相关标签:
2条回答
  • 2020-11-30 10:33

    Linux can do the fixup for you or warn about the access.

    You can enable the behavior in /proc/cpu/alignment, see http://www.mjmwired.net/kernel/Documentation/arm/mem_alignment for an explanation of the different values.

    0 - Do nothing (default behavior)
    1 - Warning in kernel-log with PC and Memory-Address printed.
    2 - Fixup error
    3 - Warn and Fixup
    4 - Send a SIGBUS to the process
    5 - Send SIGBUS and output Warning
    
    0 讨论(0)
  • 2020-11-30 10:43

    ARM Linux maintains a list of alignment handler exceptions,

    $ cat /proc/cpu/alignment 
    User:           0
    System:         0
    Skipped:        0
    Half:           0
    Word:           0
    DWord:          0
    Multi:          0
    User faults:    0 (ignored)
    

    It is only active with procfs, but it is hard to imagine a system without procfs. The specific code handling this is in alignment.c. You can use echo 3 > /proc/cpu/alignment to have Linux fixup the instruction and provide some dmesg output. Generally, handling un-aligned accesses through emulation is very in-efficient. It is better to correct the code. The signal option with a debugger attached should give some clue as to the source of the exception.

    Read the manual. ;-)

    0 讨论(0)
提交回复
热议问题