禁止在x86/x86_64上进行未对齐的内存访问 [英] Prohibit unaligned memory accesses on x86/x86_64
问题描述
我想用x86/x86_64上禁止的未对齐内存访问来模拟系统. 有一些调试工具或特殊模式可以做到这一点吗?
I want to emulate the system with prohibited unaligned memory accesses on the x86/x86_64. Is there some debugging tool or special mode to do this?
在使用专为SPARC或其他类似CPU设计的软件(C/C ++)时,我想在几台x86/x86_64 PC上运行许多(CPU密集型)测试.但是我对Sparc的访问受到限制.
I want to run many (CPU-intensive) tests on the several x86/x86_64 PCs when working with software (C/C++) designed for SPARC or some other similar CPU. But my access to Sparc is limited.
据我所知,Sparc总是检查内存读写的对齐方式是否自然(从任何地址读取一个字节,但仅当地址被4整除时才允许读取4字节的字).
As I know, Sparc always checks alignment in memory reads and writes to be natural (reading a byte from any address, but reading a 4-byte word only allowed when address is divisible by 4).
可能是Valgrind或PIN具有这种模式?还是特殊模式的编译器? 我正在搜索Linux非商业工具,但Windows工具也允许.
May be Valgrind or PIN has such mode? Or special mode of compiler? I'm searching for Linux non-commercial tool, but windows tools allowed too.
或者EFLAGS中可能有秘密的CPU标志吗?
or may be there is secret CPU flag in EFLAGS?
推荐答案
这很棘手,我还没有亲自完成,但是我认为您可以通过以下方式实现:
It's tricky and I haven't done it personally, but I think you can do it in the following way:
x86_64 CPU(具体来说,我已经检查过Intel Corei7,但我想其他人也有)具有一个性能计数器MISALIGN_MEM_REF,用于计数器未对齐的内存引用.
x86_64 CPUs (specifically I've checked Intel Corei7 but I guess others as well) have a performance counter MISALIGN_MEM_REF which counter misaligned memory references.
因此,首先,您可以运行程序并在Linux下使用性能"工具来计算代码已完成的未对齐访问次数.
So first of all, you can run your program and use "perf" tool under Linux to get a count of the number of misaligned access your code has done.
一个更棘手和有趣的技巧是编写一个内核模块,该模块对性能计数器进行编程以在溢出时生成中断,并使它在第一个未对齐的加载/存储中溢出.在内核模块中响应此中断,但向您的进程发送信号.
A more tricky and interesting hack would be to write a kernel module that programs the performance counter to generate an interrupt on overflow and get it to overflow the first unaligned load/store. Respond to this interrupt in your kernel module but sending a signal to your process.
实际上,这会将x86_64变成不支持不对齐访问的内核.
This will, in effect, turn the x86_64 into a core that doesn't support unaligned access.
这不会很简单-除了代码之外,系统库还使用未对齐的访问,因此将它们与您自己的代码分开将很棘手.
This wont be simple though - beside your code, the system libraries also use unaligned accesses, so it will be tricky to separate them from your own code.
这篇关于禁止在x86/x86_64上进行未对齐的内存访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!