如何在 Linux 2.6.35 上从用户模式清除和使 ARM v7 处理器缓存无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6046716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:08:38  来源:igfitidea点击:

How clear and invalidate ARM v7 processor cache from User Mode on Linux 2.6.35

linuxarm

提问by Alex Kolesnyk

I tried clear and invalidate ARM v7 processor cache for instruction line, because instruction codes can change in execution.

我尝试清除指令行的 ARM v7 处理器缓存并使之无效,因为指令代码可以在执行中更改。

For reaching the effect, I tried 2 variants. Here they are:

为了达到效果,我尝试了 2 个变体。他们来了:

  1. I used GCC __clear_cache()function but it didn't give a required result. Instruction codes in cache didn't change.

  2. I looked for a source codes for GCC and found the uclinux-eabi.h filewhere 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));           \
    }
    
  1. 我使用了 GCC __clear_cache()函数,但它没有给出所需的结果。缓存中的指令代码没有改变。

  2. 我寻找 GCC 的源代码并找到了uclinux-eabi.h 文件,我在其中找到了下一个清除缓存的代码:

    /* 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 ?

也许有人知道我做错了什么?

回答by Rune

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.

我自己也有类似的问题。__clear_cache() 有效,但前提是有问题的内存区域是使用带有 PROT_EXEC 设置的 mmap() 分配的。如果您为它提供来自常规 malloc() 内存的内存范围,Linux 将不会刷新指令缓存,即使处理器似乎很乐​​意从 malloc() 内存执行代码。

See https://community.arm.com/groups/processors/blog/2010/02/17/caches-and-self-modifying-codefor example code on how to do this.

有关如何执行此操作的示例代码,请参阅https://community.arm.com/groups/processors/blog/2010/02/17/caches-and-self-modifying-code