Intel 处理器可以延迟 TLB 失效吗?

发布于 2024-11-26 13:25:33 字数 544 浏览 3 评论 0原文

参考 Intel 的软件开发人员手册(订购号:325384-039US,2011 年 5 月),第 4.10.4.4 节“延迟失效”描述了 TLB 条目失效的潜在延迟,这可能会在访问其分页结构条目的内存时导致不可预测的结果已被更改。

手册说... “在某些情况下,所需的失效可能会延迟。软件开发- 操作员应该明白,在分页结构条目的修改之间 和执行第 4.10.4.2 节中建议的无效指令, 处理器可以使用基于旧值或新值的转换 分页结构条目。以下项目描述了一些潜在的后果 延迟失效的顺序: 如果修改分页结构条目以将 R/W 标志从 0 更改为 1,则写入 访问由该条目控制转换的线性地址可能或 可能不会导致页错误异常。

让我们假设一个简单的情况,其中页结构条目被修改为线性地址(r/w 标志从 0 翻转到 1),然后相应的TBL 失效指令被立即调用,我的问题是——作为 TLB 延迟失效的结果,即使在调用 TLB 失效之后,也可能对相关线性地址进行写访问。 没有错误(页错误)?

还是只有在页结构发生变化的线性地址没有发出“无效”指令时,“延迟无效”才会导致不可预测的结果?

This in reference to InteI's Software Developer’s Manual (Order Number: 325384-039US May 2011), the section 4.10.4.4 "Delayed Invalidation" describes a potential delay in invalidation of TLB entries which can cause unpredictable results while accessing memory whose paging-structure entry has been changed.

The manual says ...
"Required invalidations may be delayed under some circumstances. Software devel-
opers should understand that, between the modification of a paging-structure entry
and execution of the invalidation instruction recommended in Section 4.10.4.2, the
processor may use translations based on either the old value or the new value of the
paging-structure entry. The following items describe some of the potential conse-
quences of delayed invalidation:
If a paging-structure entry is modified to change the R/W flag from 0 to 1, write
accesses to linear addresses whose translation is controlled by this entry may or
may not cause a page-fault exception.
"

Let us suppose a simple case, where a page-strucure entry is modified (r/w flag is flipped from 0 to 1) for a linear address and after that the corresponding TBL invalidation instruction is called immediately. My question is--as a consiquence of delayed invalidation of TLB s it possible that even after calling invalidation of TLB a write access to the linear address in question doesn't fault (page fault)?

Or is the "delayed invalidation" can only cause unpredictable results when "invalidate" instruction for the linear address whose page-structure has changed has not been issued?

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

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

发布评论

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

评论(1

知足的幸福 2024-12-03 13:25:33

TLB 显然不会因 CR3 更改而取消缓存。 TLB 条目用地址空间的唯一标识符进行标记,并保留在 TLB 中,直到它们被错误的进程触及(在这种情况下,TLB 条目被丢弃)或地址空间被恢复(在这种情况下, TLB 在地址空间更改时得以保留)。

这一切对于 CPU 来说都是透明发生的。您的程序(或操作系统)不应该能够区分这种行为与 TLB 实际上因 TLB 失效而失效之间的区别,除非通过:

  • A)计时 - 即 TLB 乐观地不取消缓存更快(这就是他们这样做的原因)
  • B) 在某些边缘情况下,此行为有些未定义。如果您修改了您所在的代码页或触摸了您刚刚更改的内存,则 TLB 的旧值可能仍然存在(即使发生了 CR3 更改)。

解决方案是:

  • 1) 通过 invlpg 指令强制 TLB 更新。这会清除 TLB 条目,并在下次触摸页面时触发 TLB 读入。
  • 2) 通过 CR0 寄存器禁用和重新启用分页。
  • 3) 通过 CR0 中或 TLB 的所有页面上的缓存禁用位将所有页面标记为不可缓存(标记为不可缓存的 TLB 条目在使用后会自动清除)。
  • 4) 改变代码段的模式。

请注意,这确实是未定义的行为。转换到 SMM 可能会使 TLB 失效,也可能不会,从而导致竞争条件。不要依赖这种行为。

TLBs are transparently optimisitically not uncached by CR3 changes. TLBs entries are marked with a unique identifier for the address-space and are left in the TLB until they are either touched by the wrong process (in which case the TLB entry is trashed) or the address-space is restored (in which case the TLB was preserved over the address-space changing).

This all happens transparently to the CPU. Your program (or OS) shouldn't be able to tell the difference between this behaviour and the TLB being actually invalidated by a TLB invalidation except via:

  • A) Timing - i.e. TLB optimisticly not uncaching is faster (which is why they do it)
  • B) There are edge cases where this behaviour is somewhat undefined. If you modify the code page on which you're sitting or touch memory you've just changed, the old value of the TLB might still be there (even across a CR3 change).

The solution to this is to either:

  • 1) force a TLB update via a invlpg instruction. This purges the TLB entry, triggering a TLB read-in on the next touch of the page.
  • 2) disable and re-enable paging via the CR0 register.
  • 3) mark all pages as un-cachable via the cache-disable bit in CR0 or on all of the pages of the TLB (TLB entries marked uncachable are auto-purged after use).
  • 4) Change the mode of the code-segment.

Note that this is genuinely undefined behaviour. Transitioning to SMM can invalidate the TLB, or might not, leaving this open to a race-condition. Don't depend on this behaviour.

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