Chinaunix首页 | 论坛 | 博客
  • 博客访问: 526267
  • 博文数量: 51
  • 博客积分: 345
  • 博客等级: 民兵
  • 技术积分: 534
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-21 12:02
个人简介

文章分类

全部博文(51)

文章存档

2023年(2)

2022年(1)

2021年(7)

2020年(10)

2019年(2)

2016年(20)

2015年(5)

2014年(1)

2011年(3)

我的朋友

分类: LINUX

2021-01-06 21:22:12

最近阅读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中指令被修改后,场景如下:
Cache after new code written

The solution
如何更新I-cache,只有以一种方法:
The data must move to the external memory and then into the I-cache from here.

cleaned data in cache result

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中的指令无效来实现这一点。结果如下所示:
Invalidating instructions in I-cache esults

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中的指令取回将会丢失,处理器将不得不从内存中获取新版本。结果是执行了新发出的代码,正如您预期的那样。




阅读(626) | 评论(0) | 转发(0) |
0

上一篇:perf probe实例

下一篇:ebpf程序实例

给主人留下些什么吧!~~