如何在 Linux 2.6.35 上从用户模式清除和无效 ARM v7 处理器缓存
我尝试清除指令行的 ARM v7 处理器缓存并使之无效,因为指令代码在执行中可能会发生变化。
为了达到效果,我尝试了两种变体。它们是:
我使用了 GCC __clear_cache() 函数,但它没有给出所需的结果。缓存中的指令代码没有改变。
我查找了 GCC 的源代码,并找到了 uclinux-eabi.h 文件,我在其中找到了下一个清除缓存的代码:
/* 清除从‘beg’到‘end’的指令缓存。这使得 对 SYS_cacheflush 的内联系统调用。 */ #undef CLEAR_INSN_CACHE #define CLEAR_INSN_CACHE(BEG, END) \ { \ 寄存器 unsigned long _beg __asm ("a1") = (unsigned long) (BEG); \ 寄存器 unsigned long _end __asm("a2") = (unsigned long) (END); \ 寄存器 unsigned long _flg __asm("a3") = 0; \ 寄存器无符号长_scno __asm("r7") = 0xf0002; \ __asm __易失性 \ (\ “swi 0x0@sys_cacheflush”\ :“=r”(_beg)\ :“0”(_beg)、“r”(_end)、“r”(_flg)、“r”(_scno)); \ }
此变体没有给出结果也。
也许有人知道我做错了什么?
I tried clear and invalidate ARM v7 processor cache for instruction line, because instruction codes can change in execution.
For reaching the effect, I tried 2 variants. Here they are:
I used GCC __clear_cache() function but it didn't give a required result. Instruction codes in cache didn't change.
I looked for a source codes for GCC and found the uclinux-eabi.h file where I found the next code for clearing cache:
/* Clear the instruction cache from `beg' to `end'. This makes an inline system call to SYS_cacheflush. */ #undef CLEAR_INSN_CACHE #define CLEAR_INSN_CACHE(BEG, END) \ { \ register unsigned long _beg __asm ("a1") = (unsigned long) (BEG); \ register unsigned long _end __asm ("a2") = (unsigned long) (END); \ register unsigned long _flg __asm ("a3") = 0; \ register unsigned long _scno __asm ("r7") = 0xf0002; \ __asm __volatile \ ( \ "swi 0x0 @ sys_cacheflush" \ : "=r" (_beg) \ : "0" (_beg), "r" (_end), "r" (_flg), "r" (_scno)); \ }
This variant didn't give the result too.
Maybe someone knows what I do wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己也有类似的问题。 __clear_cache() 有效,但前提是相关内存区域是使用 mmap() 并设置了 PROT_EXEC 来分配的。如果您向 Linux 提供来自常规 malloc()ed 内存的内存范围,Linux 将不会刷新指令缓存,即使处理器似乎很乐意从 malloc()ed 内存中执行代码。
请参阅 https://community。 arm.com/groups/processors/blog/2010/02/17/caches-and-self-modifying-code 有关如何执行此操作的示例代码。
I had a similar problem myself. __clear_cache() works, but only if the memory area in question was allocated using mmap() with PROT_EXEC set. Linux will not flush the instruction cache if you provide it with a memory range that comes from regular malloc()ed memory, even if the processor seems to be happy to execute code from malloc()ed memory.
See https://community.arm.com/groups/processors/blog/2010/02/17/caches-and-self-modifying-code for example code on how to do this.