ARM 未对齐内存访问解决方法 [英] ARM unaligned memory access workaround

查看:33
本文介绍了ARM 未对齐内存访问解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将源代码从运行 Linux 的 ARM 平台移植.不幸的是,我遇到了未对齐的内存访问问题.源使用指针强制转换并大量访问.

像下面这样的代码像病毒一样在代码库中传播.由于 gcc -Wcast-align 命令行选项,我可以查明有问题的位置,但有一千多个实例需要处理.

u = (IEC_BOOL);(((*(IEC_LINT*)pSP).H  > u);*(IEC_DWORD OS_SPTR *)pSP =(IEC_DWORD)(*(IEC_DWORD OS_SPTR *)pSP << -u);u = (IEC_BYTE)((*(IEC_DINT*)pSP != b) ? 1 : 0);*(IEC_DWORD*)pSP = (IEC_DWORD)(*(IEC_DWORD*)pSP & w);(*(IEC_ULINT*)pSP).H += u.H;(((*(IEC_ULINT OS_SPTR *)pSP).H == b.H)&&((*(IEC_ULINT OS_SPTR *)pSP).L > b.L))) ?1 : 0);u = (IEC_BYTE)((*(IEC_REAL*)pSP >= b) ? 1 : 0);

使用 echo 2 >/proc/cpu/alignment on 使 Linux Kernel 修复问题,但应用程序的性能下降到无法接受的程度.

我在网上搜索了 GCC (v4.4.1) 编译器的类似 __unaligned__packed 关键字之类的东西,但到目前为止还没有找到.

我认为很多有问题的代码行都可以用或多或少复杂的正则表达式/替换来修复,但现在,在这样做了一段时间后我看到,这种方法也将需要大量繁琐的工作.

你们对如何完成这项工作有什么建议吗?我认为 gcc 4.5 编译器插件会有点矫枉过正,但有什么比正则表达式更好的吗?你还能提出什么其他建议?不一定所有问题实例都必须修复,因为我仍然可以依靠内核处理一些罕见的情况.

解决方案

__attribute__((__packed__)) 在某些情况下可能会有所帮助,但我真的认为应该清理这段代码迟早要迟到,因为与一劳永逸地解决问题相比,您花在解决问题上的时间可能更多.

I have to port source code from to an ARM platform that runs Linux. Unfortunately I have run into unaligned memory access problems. The source uses pointer casts and access heavily.

Code like the one below has spread over the codebase like a virus. I can pinpoint the problematic locations thanks to the gcc -Wcast-align command line option but there are over a thousand instances to go through.

u = (IEC_BOOL);
(((*(IEC_LINT*)pSP).H < b.H) 
   || (((*(IEC_LINT*)pSP).H == b.H) && ((*(IEC_LINT*)pSP).L < b.L) )) ? 1 : 0);
*(IEC_DWORD OS_SPTR *)pSP = 
    (IEC_DWORD)(*(IEC_DWORD OS_SPTR *)pSP >> u);  
*(IEC_DWORD OS_SPTR *)pSP = 
    (IEC_DWORD)(*(IEC_DWORD OS_SPTR *)pSP << -u);  
u = (IEC_BYTE)((*(IEC_DINT*)pSP != b) ? 1 : 0);  
*(IEC_DWORD*)pSP = (IEC_DWORD)(*(IEC_DWORD*)pSP & w);  
(*(IEC_ULINT*)pSP).H += u.H;   
(((*(IEC_ULINT OS_SPTR *)pSP).H == b.H) 
    && ((*(IEC_ULINT OS_SPTR *)pSP).L > b.L))) ? 1 : 0);
u = (IEC_BYTE)((*(IEC_REAL*)pSP >= b) ? 1 : 0);

Using echo 2 > /proc/cpu/alignment on makes the Linux Kernel fix the problems but the performance of the application is degraded to a point that is not acceptable any more.

I searched the net for something like a __unaligned or __packed keyword for the GCC (v4.4.1) compiler but as of yet came up empty.

I thought a lot of the poblematic code lines could be fixed with a more or less complex regexp/replace but now, after doing that for a while I see, that also this approach will take enourmous amounts of tedious work.

Do you guys have any suggestion how to get this job done? I think a gcc 4.5 compiler plugin would be overkill but is there something better than regular expressions? what other suggestions can you come up with? Not necessarily all problem instances have to be fixed since I can still rely on the Kernel for a few rarer cases.

解决方案

There is __attribute__((__packed__)) which might help in some instances, but I really think this code should be cleaned up rather sooner than later, because it is likely that you will spend more time working around problems than it would take to fix it once and for all.

这篇关于ARM 未对齐内存访问解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆