gpt4 book ai didi

assembly - 访问跨 MMU 页边界的变量

转载 作者:行者123 更新时间:2023-12-05 02:28:52 26 4
gpt4 key购买 nike

我正在学习Windows下的X64汇编语言和MASM64,就是看最新版的《64位汇编语言的艺术》一书。
我有一个关于书中引用的问题:

You do have to worry about MMU page organization in memory in one situation. Sometimes it is convenient to access (read) data beyond the end of a data structure in memory. However, if that data structure is aligned with the end of an MMU page, accessing the next page in memory could be problematic. Some pages in memory are inaccessible; the MMU does not allow reading, writing, or execution to occur on that page.Attempting to do so will generate an x86-64 general protection (segmentation) fault and abort the normal execution of your program. If you have a data access that crosses a page boundary, and the next page in memory is inaccessible, this will crash your program. For example, consider a word access to a byte object at the very end of an MMU page, asshown in Figure 3-2.Figure 3-2: Word access at the end of an MMU page

As a general rule, you should never read data beyond the end of a data structure. If for some reason you need to do so, you should ensure that it is legal to access the next page in memory (alas, there is no instruction on modern x86-64 CPUs to allow this; the only way to be sure that access is legal is to make sure there is valid data after the data structure you are accessing).

所以我的问题是:假设我有那个确切的案例。数据段末尾的字变量。如何防止异常?通过手动填充 00h 单元格?正确地将每个变量与其大小对齐?如果我对齐所有内容,如果最后一个变量是跨越 4k 边界的 qword 会发生什么?如何预防?
MASM 会自动分配另一个顺序数据段来容纳它吗?

最佳答案

读取页面中已知包含任何有效字节的任何位置是安全的,例如在具有未对齐的 foo: dq 1 的静态存储中。如果你有它,mov rax, [foo] 总是安全的。

您的汇编器 + 链接器将确保 .data.rdata.bss 中的所有存储实际上都由有效页面支持操作系统会让你触摸。


你的书的意思是你可能有一个 3 字节结构的数组,例如 RGB 像素。 x86 没有 3 字节加载,因此使用 mov eax, [rcx] 加载整个像素结构实际上会加载 4 个字节,包括您不关心的 1 个字节。

通常这很好,除非 [rcx+3] 位于未映射的页面中。 (例如,缓冲区的最后一个像素,在页面末尾结束,下一页未映射)。跨入另一个不需要数据的缓存行对性能来说不是很好,因此这是与 2 或 3 个单独加载(如 movzx eax, word ptr [rcx]/movzx)的权衡edx, byte ptr [rcx+2]

这在 SIMD 中更常见,您可以在加载它们后在寄存器中一次更多地使用多个元素。像 movdqu xmm0, [rcx] 加载 16 个字节,包括 5 个完整像素和我们不打算在此向量中处理的另一个像素的 1 个字节。

(平面 RGB 没有这个问题,其中所有 R 分量都是连续的。或者一般来说,AoS 与 SoA = 数组结构对 SIMD 有好处。如果展开,你也不会有这个问题你的循环是 3 或什么,所以 3x 16 字节向量 = 48 字节覆盖 16x 3 字节像素,如果需要,可能会做一些改组或者有 3 个不同的向量常量,如果你需要不同的常量来排列你的结构的不同组件或像素或其他。)

如果遍历一个数组,你在最后一次迭代中会遇到同样的问题。如果数组大于 1 个 SIMD 向量(XMM 或 YMM),而不是最后一个 n % 4 元素的标量,您有时可以安排在数组末尾结束的 SIMD 加载,因此它与先前的完整向量部分重叠。 (为了减少分支,保留 1..4 个清理元素而不是 0..3,所以如果 n 是向量宽度的倍数,那么“清理”是另一个完整向量。)这有效非常适合制作 ASCII 字符串的小写副本:重做任何给定字节的工作很好,而且你没有就地存储,所以你甚至没有存储转发停顿,因为你不会'负载与以前的商店重叠。对数组求和(您需要避免重复计算)或就地工作不太容易。


另见 Is it safe to read past the end of a buffer within the same page on x86 and x64?

这对 strlen 来说是一个挑战,您知道您被允许读取的数据是否延伸到下一页。 (除非您一次只读取 1 个字节,这比使用 SSE2 慢 16 倍。)


AVX-512 通过故障抑制屏蔽了加载/存储,因此 k1=0x7F 的 vmovdqu8 xmm0{k1}{z}, [rcx] 将有效地加载 15 字节,而不是即使第 16 个字节(掩码为零)扩展到未映射的页面,也会出现错误。与 AVX vmaskmovps 等相同。但是 AMD 上的商店版本很慢。

另见 Vectorizing with unaligned buffers: using VMASKMOVPS: generating a mask from a misalignment count? Or not using that insn at all


Attempting to do so will generate an x86-64 general protection (segmentation) fault

实际上是一个 #PF 页面错误,用于访问未映射或权限被拒绝的页面。但是,是的,同样的区别。

关于assembly - 访问跨 MMU 页边界的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72479456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com