mprotect 是否会刷新 ARM Linux 上的指令缓存?

发布于 2024-08-31 15:27:26 字数 189 浏览 1 评论 0原文

我正在 ARM Linux 上编写一个 JIT,它执行包含自修改代码的指令集。该指令集没有任何缓存刷新指令(在这方面与 x86 类似)。

如果我向某个页面写入一些代码,然后在该页面上调用 mprotect,这是否足以使指令缓存失效?或者我还需要在这些页面上使用 cacheflush 系统调用吗?

I am writing a JIT on ARM Linux that executes an instruction set that contains self-modifying code. The instruction set does not have any cache flush instructions (similar to x86 in that respect).

If I write out some code to a page and then call mprotect on that page, is that sufficient to invalidate the instruction cache? Or do I also need to use the cacheflush syscall on those pages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够运 2024-09-07 15:27:26

您希望 mmap/mprotect 系统调用将建立立即更新的映射,并且不需要进一步交互即可使用指定的内存范围。我看到内核确实刷新了 mprotect 上的缓存。在这种情况下,不需要刷新缓存。

但是,我还发现某些版本的 libc 确实在 mprotect 之后调用 cacheflush,这意味着某些环境需要刷新缓存(或者以前有过)。我猜测这是一个错误的解决方法。

您始终可以添加对cacheflush的调用;虽然它是额外的代码,但它不应该是有害的——最坏的情况是,缓存已经被刷新了。您总是可以编写一个快速测试,看看会发生什么......

You'd expect that the mmap/mprotect syscalls would establish mappings that are updated immediately, and need no further interaction to use the memory ranges as specified. I see that the kernel does indeed flush caches on mprotect. In that case, no cache flush would be required.

However, I also see that some versions of libc do call cacheflush after mprotect, which would imply that some environments would need the caches flushed (or have previously). I'd take a guess that this is a workaround to a bug.

You could always add the call to cacheflush; although it's extra code, it shouldn't be to harmful - at worst, the caches will already be flushed. You could always write a quick test and see what happens...

居里长安 2024-09-07 15:27:26

特别是在 Linux 中,mprotect 至少从 2.6.39 版本(甚至更早版本)开始对所有缓存进行缓存刷新。你可以在代码中看到:
https://elixir.bootlin.com/ linux/v2.6.39.4/source/mm/mprotect.c#L122

如果您正在编写 POSIX 可移植代码,我会调用 cacheflush,因为标准 C 库不要求内核或实现执行此类行为。

编辑:您还应该小心并检查 flush_cache_range 在您正在实现的特定体系结构中执行的操作,因为在某些体系结构(如 ARM64)中该函数不执行任何操作...

In Linux specifically, mprotect DOES cacheflush all caches since at least version 2.6.39 (and even before that for sure). You can see that in the code:
https://elixir.bootlin.com/linux/v2.6.39.4/source/mm/mprotect.c#L122 .

If you are writing a POSIX portable code, I would call cacheflush as the standard C library is not demanding such behavior from the kernel, nor from the implementation.

Edit: You should also be carefull and check what flush_cache_range does in the specific architecture you are implementing for, as in some architecture (like ARM64) this function does nothing...

狼性发作 2024-09-07 15:27:26

我相信您不必显式刷新缓存。

这是哪个处理器? ARMv5? ARMv7?

I believe you do not have to explicitly flush the cache.

Which processor is this? ARMv5? ARMv7?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文