__builtin___clear_cache如何工作? [英] How does __builtin___clear_cache work?

查看:479
本文介绍了__builtin___clear_cache如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看gcc文档时,我偶然发现了内置函数 __ builtin ___ clear_cache

Going through the gcc documentation, I stumbled into the builtin function __builtin___clear_cache.


—内置函数:void __builtin ___ clear_cache(char *开始,char
* end)此函数用于刷新处理器的指令高速缓存,以存储介于开始包含和结束排除之间的内存区域。
一些目标要求在
修改包含代码的内存之后,刷新指令缓存,以便获得确定性的
行为。

— Built-in Function: void __builtin___clear_cache (char *begin, char *end) This function is used to flush the processor's instruction cache for the region of memory between begin inclusive and end exclusive. Some targets require that the instruction cache be flushed, after modifying memory containing code, in order to obtain deterministic behavior.

如果目标不需要刷新指令高速缓存,则
__builtin___clear_cache无效。否则,要么以内联方式发出指令以清除指令缓存,要么调用libgcc中的
__clear_cache函数。

If the target does not require instruction cache flushes, __builtin___clear_cache has no effect. Otherwise either instructions are emitted in-line to clear the instruction cache or a call to the __clear_cache function in libgcc is made.



<我觉得这很有趣,但令人惊讶。在许多情况下,当前堆栈的大量指令存储在L1缓存(指令缓存)中。因此,乍一看,此内置函数会使其擦掉堆栈上的下一条指令,从而可能严重破坏程序的流程。

I find this interesting, but surprising. In many cases, a large number of the instructions for the current stack is stored in the L1 cache (instruction cache). So it would seem at first glance that this builtin could corrupt significantly the flow of our program, by making it wipe out the next instructions on the stack.

此指令还会执行吗?重新填充L1缓存中的堆栈部分?

Does this instruction also repopulates the part of the stack that was in the L1 cache?

这似乎不太可能。如果没有,那么我认为用户有责任使用正确的 begin end 参数,因此以免破坏我们的流程。在实践中,如何找到正确的开始 end 使用的东西?

This seems unlikely. If it does not, then I suppose the onus is on the user to use the right begin and end arguments, so as to not corrupt our process. In practice, how could one find what the right begin and end to use?

推荐答案

它只是在需要它们的目标处理器上发出一些奇怪的机器指令(x86不需要)。

It is just emitting some weird machine instruction[s] on target processors requiring them (x86 don't need that).

__ builtin ___ clear_cache 认为是便携式(对于GCC和兼容的编译器)刷新指令缓存的方式(例如,在某些 JIT 库)。

Think of __builtin___clear_cache as a "portable" (to GCC and compatible compilers) way to flush the instruction cache (e.g. in some JIT library).


在实践中,如何找到合适的开始和结束方式?

In practice, how could one find what the right begin and end to use?

为了安全起见,我会使用在某个页面范围内(例如,通过 sysconf(_SC_PAGESIZE) ....获得的),因此通常为4KB对齐的内存范围(4KB的倍数)。否则,您需要特定于目标的技巧来查找缓存行的宽度...

To be safe, I would use that on some page range (e.g. obtained with sysconf(_SC_PAGESIZE)....), so usually a 4Kbyte aligned memory range (multiple of 4Kbyte). Otherwise, you want some target specific trick to find the cache line width...

在Linux上,您可能会阅读 / proc / cpuinfo 并使用 cache_alignment & cache_size 行以获得更精确的缓存行大小和对齐方式。

On Linux, you might read /proc/cpuinfo and use the cache_alignment & cache_size lines to get a more precise cache line size and alignment.

BTW,使用<$ c $的代码c> __ builtin__clear_cache 很可能是(出于其他原因)目标计算机特定的,因此它具有或知道一些计算机参数(并且应包括高速缓存大小和对齐方式)。

BTW, a code using __builtin__clear_cache is very likely to be (for other reasons) target machine specific, so it has or knows some machine parameters (and that should include cache size & alignment).

这篇关于__builtin___clear_cache如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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