Skip to content

Commit 0236ebd

Browse files
hanliyangopsiff
authored andcommitted
x86/cpu: Detect memory encryption features on Hygon CPUs
hygon inclusion category: feature CVE: NA --------------------------- Hygon SME is identified by CPUID 0x8000001f, but requires BIOS support to enable it (set bit 23 of MSR_AMD64_SYSCFG). Hygon CSV and CSV2 are identified by CPUID 0x8000001f, but requires BIOS support to enable it (set bit 23 of MSR_AMD64_SYSCFG and set bit 0 of MSR_K7_HWCR). Only show the SME, CSV, CSV2 features as available if reported by CPUID and enabled by BIOS. Signed-off-by: hanliyang <hanliyang@hygon.cn> Link: deepin-community#350 (cherry picked from commit 54f0805) Signed-off-by: Wentao Guan <guanwentao@uniontech.com> Conflicts: arch/x86/kernel/cpu/hygon.c
1 parent d167d99 commit 0236ebd

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

arch/x86/kernel/cpu/hygon.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,50 @@ static void bsp_init_hygon(struct cpuinfo_x86 *c)
122122
resctrl_cpu_detect(c);
123123
}
124124

125+
static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)
126+
{
127+
u64 msr;
128+
u32 eax;
129+
130+
eax = cpuid_eax(0x8000001f);
131+
132+
/* Check whether SME or CSV is supported */
133+
if (!(eax & (BIT(0) | BIT(1))))
134+
return;
135+
136+
/* If BIOS has not enabled SME then don't advertise the SME feature. */
137+
rdmsrl(MSR_AMD64_SYSCFG, msr);
138+
if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
139+
goto clear_all;
140+
141+
/*
142+
* Always adjust physical address bits. Even though this will be a
143+
* value above 32-bits this is still done for CONFIG_X86_32 so that
144+
* accurate values are reported.
145+
*/
146+
c->x86_phys_bits -= (cpuid_ebx(0x8000001f) >> 6) & 0x3f;
147+
148+
/* Don't advertise SME and CSV features under CONFIG_X86_32. */
149+
if (IS_ENABLED(CONFIG_X86_32))
150+
goto clear_all;
151+
152+
/*
153+
* If BIOS has not enabled CSV then don't advertise the CSV and CSV2
154+
* feature.
155+
*/
156+
rdmsrl(MSR_K7_HWCR, msr);
157+
if (!(msr & MSR_K7_HWCR_SMMLOCK))
158+
goto clear_csv;
159+
160+
return;
161+
162+
clear_all:
163+
setup_clear_cpu_cap(X86_FEATURE_SME);
164+
clear_csv:
165+
setup_clear_cpu_cap(X86_FEATURE_SEV);
166+
setup_clear_cpu_cap(X86_FEATURE_SEV_ES);
167+
}
168+
125169
static void early_init_hygon(struct cpuinfo_x86 *c)
126170
{
127171
u32 dummy;
@@ -166,6 +210,8 @@ static void early_init_hygon(struct cpuinfo_x86 *c)
166210
* we can set it unconditionally.
167211
*/
168212
set_cpu_cap(c, X86_FEATURE_VMMCALL);
213+
214+
early_detect_mem_encrypt(c);
169215
}
170216

171217
static void init_hygon(struct cpuinfo_x86 *c)

arch/x86/kernel/cpu/proc.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,14 @@ static int show_cpuinfo(struct seq_file *m, void *v)
103103

104104
seq_puts(m, "flags\t\t:");
105105
for (i = 0; i < 32*NCAPINTS; i++)
106-
if (cpu_has(c, i) && x86_cap_flags[i] != NULL)
107-
seq_printf(m, " %s", x86_cap_flags[i]);
106+
if (cpu_has(c, i) && x86_cap_flags[i] != NULL) {
107+
if (c->x86_vendor == X86_VENDOR_HYGON)
108+
seq_printf(m, " %s", i == X86_FEATURE_SEV ? "csv" :
109+
(i == X86_FEATURE_SEV_ES ? "csv2" :
110+
x86_cap_flags[i]));
111+
else
112+
seq_printf(m, " %s", x86_cap_flags[i]);
113+
}
108114

109115
#ifdef CONFIG_X86_VMX_FEATURE_NAMES
110116
if (cpu_has(c, X86_FEATURE_VMX) && c->vmx_capability[0]) {

0 commit comments

Comments
 (0)