gpt4 book ai didi

x86 - LFENCE 是否在 AMD 处理器上进行序列化?

转载 作者:行者123 更新时间:2023-12-03 01:48:47 25 4
gpt4 key购买 nike

在最近的英特尔 ISA 文档中,lfence 指令已被定义为序列化指令流(防止跨指令流乱序执行)。特别是description该指令包括这一行:

Specifically, LFENCE does not execute until all prior instructions have completed locally, and no later instruction begins execution until LFENCE completes.

请注意,这适用于所有指令,而不仅仅是内存加载指令,使得lfence更多不仅仅是内存排序栅栏。

虽然这现在出现在 ISA 文档中,但尚不清楚它是否是“体系结构”(即所有 x86 实现都要遵守),还是英特尔特定的。特别是,AMD 处理器是否也将 lfence 视为序列化指令流?

最佳答案

AMD 始终在其手册中将其 LFENCE 的实现描述为加载序列化指令

Acts as a barrier to force strong memory ordering (serialization)between load instructions preceding the LFENCE and load instructionsthat follow the LFENCE.

LFENCE 的原始用例是订购 WC 内存类型加载。然而,在推测执行漏洞被发现后,AMD于2018年1月发布了一份题为“管理AMD处理器推测的软件技术”的文档。这是第一个也是唯一一个提到 MSR C001_1029[1] 的文档(一些 AMD 文档中讨论了 C001_1029 的其他位,但没有讨论位 1)。当 C001_1029[1] 设置为 1 时,LFENCE 表现为调度序列化指令(这比仅仅加载序列化更昂贵)。由于此 MSR 可在大多数较旧的 AMD 处理器上使用,因此它似乎几乎始终受到支持。也许是因为他们认为将来可能需要在 LFENCE 的行为方面保持与英特尔处理器的兼容性。

栅栏指令和序列化指令以及具有序列化属性的指令的排序规则有异常(exception)。 Intel 和 AMD 处理器之间的这些异常(exception)情况略有不同。我现在能想到的一个例子是CLFLUSH指令。因此,AMD 和 Intel 在谈论具有序列化属性的指令时,含义略有不同。

我不清楚的一件事是 document 引用的以下部分:

AMD family 0Fh/11h processors support LFENCE as serializing always butdo not support this MSR.

此声明含糊不清,因为它没有明确说明 AMD 系列 0Fh 和 11h 上的 LFENCE 是完全序列化(使用 AMD 术语)还是调度序列化(使用 AMD 术语)。但它很可能只是调度序列化。 AMD 系列特定手册未提及 LFENCE 或 MSR C001_1029。

<小时/>

自 Linux 内核 v4.15-rc8 起,使用 AMD 处理器上的 LFENCE 序列化属性。该更改包含两个提交 12 。以下宏在提交 1 中定义:

+#define MSR_F10H_DECFG         0xc0011029
+#define MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT 1

第一个宏指定 MSR 地址,第二个宏指定偏移量。以下代码已添加到提交 2 的 init_amd 中(部分注释是我的):

/* LFENCE always requires SSE2 */
if (cpu_has(c, X86_FEATURE_XMM2)) {
unsigned long long val;
int ret;

/* The AMD CPU supports LFENCE, but there are three cases to be considered:
* 1- MSR C001_1029[1] must be set to enable the dispatch
* serializing behavior of LFENCE. This can only be done
* if and only if the MSR is supported.
* 2- The MSR is not supported (AMD 0Fh/11h). LFENCE is by
* default at least dispatch serializing. Nothing needs to
* be done.
* 3- The MSR is supported, but we are running under a hypervisor
* that does not support writing that MSR (because perhaps
* the hypervisor has not been updated yet). In this case, resort
* to the slower MFENCE for serializing RDTSC and use a Spectre
* mitigation that does not require LFENCE (i.e., generic retpoline).


/*
* A serializing LFENCE has less overhead than MFENCE, so
* use it for execution serialization. On families which
* don't have that MSR, LFENCE is already serializing.
* msr_set_bit() uses the safe accessors, too, even if the MSR
* is not present.
*/
msr_set_bit(MSR_F10H_DECFG,
MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT);

/*
* Verify that the MSR write was successful (could be running
* under a hypervisor) and only then assume that LFENCE is
* serializing.
*/
ret = rdmsrl_safe(MSR_F10H_DECFG, &val);
if (!ret && (val & MSR_F10H_DECFG_LFENCE_SERIALIZE)) {
/* A serializing LFENCE stops RDTSC speculation */
set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
/* X86_FEATURE_LFENCE_RDTSC is used later to choose a Spectre
mitigation */
} else {
/* MFENCE stops RDTSC speculation */
set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC);
}
}

自v5.4-rc1起,删除了MSR写入验证码。所以代码就变成了:

    msr_set_bit(MSR_F10H_DECFG,
MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT);
set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);

commit message 中讨论了此更改背后的原因。 。 (总而言之,它大多不需要,而且可能不起作用。)

那个document还说:

All AMD family 10h/12h/14h/15h/16h/17h processors support this MSR.LFENCE support is indicated by CPUID function1 EDX bit 26, SSE2. AMDfamily 0Fh/11h processors support LFENCE as serializing always but donot support this MSR.

但 AMD 手册似乎尚未更新,未提及对 C001_1029[1] 的支持。

AMD 在该文件中说了以下内容:

AMD plans support for this MSR and access to this bit for all futureprocessors.

这意味着 C001_1029[1] 应被视为 future AMD 处理器上的架构(相对于 2018 年 1 月)。

关于x86 - LFENCE 是否在 AMD 处理器上进行序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51844886/

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