Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/cc: Add cc_platform_set/_clear() helpers

Add functionality to set and/or clear different attributes of the
machine as a confidential computing platform. Add the first one too:
whether the machine is running as a host for SEV-SNP guests.

Fixes: 216d106c7ff7 ("x86/sev: Add SEV-SNP host initialization support")
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
Tested-by: Srikanth Aithal <sraithal@amd.com>
Link: https://lore.kernel.org/r/20240327154317.29909-5-bp@alien8.de

+64
+52
arch/x86/coco/core.c
··· 20 20 enum cc_vendor cc_vendor __ro_after_init = CC_VENDOR_NONE; 21 21 u64 cc_mask __ro_after_init; 22 22 23 + static struct cc_attr_flags { 24 + __u64 host_sev_snp : 1, 25 + __resv : 63; 26 + } cc_flags; 27 + 23 28 static bool noinstr intel_cc_platform_has(enum cc_attr attr) 24 29 { 25 30 switch (attr) { ··· 98 93 case CC_ATTR_GUEST_SEV_SNP: 99 94 return sev_status & MSR_AMD64_SEV_SNP_ENABLED; 100 95 96 + case CC_ATTR_HOST_SEV_SNP: 97 + return cc_flags.host_sev_snp; 98 + 101 99 default: 102 100 return false; 103 101 } ··· 160 152 } 161 153 } 162 154 EXPORT_SYMBOL_GPL(cc_mkdec); 155 + 156 + static void amd_cc_platform_clear(enum cc_attr attr) 157 + { 158 + switch (attr) { 159 + case CC_ATTR_HOST_SEV_SNP: 160 + cc_flags.host_sev_snp = 0; 161 + break; 162 + default: 163 + break; 164 + } 165 + } 166 + 167 + void cc_platform_clear(enum cc_attr attr) 168 + { 169 + switch (cc_vendor) { 170 + case CC_VENDOR_AMD: 171 + amd_cc_platform_clear(attr); 172 + break; 173 + default: 174 + break; 175 + } 176 + } 177 + 178 + static void amd_cc_platform_set(enum cc_attr attr) 179 + { 180 + switch (attr) { 181 + case CC_ATTR_HOST_SEV_SNP: 182 + cc_flags.host_sev_snp = 1; 183 + break; 184 + default: 185 + break; 186 + } 187 + } 188 + 189 + void cc_platform_set(enum cc_attr attr) 190 + { 191 + switch (cc_vendor) { 192 + case CC_VENDOR_AMD: 193 + amd_cc_platform_set(attr); 194 + break; 195 + default: 196 + break; 197 + } 198 + } 163 199 164 200 __init void cc_random_init(void) 165 201 {
+12
include/linux/cc_platform.h
··· 90 90 * Examples include TDX Guest. 91 91 */ 92 92 CC_ATTR_HOTPLUG_DISABLED, 93 + 94 + /** 95 + * @CC_ATTR_HOST_SEV_SNP: AMD SNP enabled on the host. 96 + * 97 + * The host kernel is running with the necessary features 98 + * enabled to run SEV-SNP guests. 99 + */ 100 + CC_ATTR_HOST_SEV_SNP, 93 101 }; 94 102 95 103 #ifdef CONFIG_ARCH_HAS_CC_PLATFORM ··· 115 107 * * FALSE - Specified Confidential Computing attribute is not active 116 108 */ 117 109 bool cc_platform_has(enum cc_attr attr); 110 + void cc_platform_set(enum cc_attr attr); 111 + void cc_platform_clear(enum cc_attr attr); 118 112 119 113 #else /* !CONFIG_ARCH_HAS_CC_PLATFORM */ 120 114 121 115 static inline bool cc_platform_has(enum cc_attr attr) { return false; } 116 + static inline void cc_platform_set(enum cc_attr attr) { } 117 + static inline void cc_platform_clear(enum cc_attr attr) { } 122 118 123 119 #endif /* CONFIG_ARCH_HAS_CC_PLATFORM */ 124 120