最近阅读kprobe的内核代码,目标指令被替换为brk指令;这个过程需要刷新cache,从代码中没能理解背后的原理。最近找到一篇文章给出的一些相关的解释,非常受用。
源文链接:https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/caches-and-self-modifying-code
The processor can only execute instructions that are in the I-cache and can only see data that are in the D-cache.
Generally, it cannot directly access memory. we cannot directly write into I-cache(or into memory).
CPU执行的指令来自I-cache,但是我们不能从I-cache读或者向其写数据;
当数据cache中指令被修改后,场景如下:
The solution
如何更新I-cache,只有以一种方法:
The data must move to the external memory and then into the I-cache from here.
In order to execute the new instruction, we need to tell the processor that the content of the I-cache are stale and need to be re-loaded from memory. We do this by invalidating the instructions in the I-cache. The result will look like this:
为了执行新指令,我们需要告诉处理器I-cache的内容已经过期,需要从内存中重新加载。我们通过使I-cache中的指令无效来实现这一点。结果如下所示:
If you now attempt to run the code you have written out to memory, the instruction fetch will miss in the I-cache and the processor will have to get the new version from memory. The result is that your newly-emitted code gets executed, as you intended.
如果您现在尝试运行已写入内存的代码,则I-cache中的指令取回将会丢失,处理器将不得不从内存中获取新版本。结果是执行了新发出的代码,正如您预期的那样。
阅读(696) | 评论(0) | 转发(0) |