1 // SPDX-License-Identifier: GPL-2.0-only
3 * Confidential Computing Platform Capability checks
5 * Copyright (C) 2021 Advanced Micro Devices, Inc.
6 * Copyright (C) 2024 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
8 * Author: Tom Lendacky <thomas.lendacky@amd.com>
11 #include <linux/export.h>
12 #include <linux/cc_platform.h>
13 #include <linux/string.h>
14 #include <linux/random.h>
16 #include <asm/archrandom.h>
18 #include <asm/processor.h>
20 enum cc_vendor cc_vendor __ro_after_init = CC_VENDOR_NONE;
21 u64 cc_mask __ro_after_init;
23 static bool intel_cc_platform_has(enum cc_attr attr)
26 case CC_ATTR_GUEST_UNROLL_STRING_IO:
27 case CC_ATTR_HOTPLUG_DISABLED:
28 case CC_ATTR_GUEST_MEM_ENCRYPT:
29 case CC_ATTR_MEM_ENCRYPT:
37 * SME and SEV are very similar but they are not the same, so there are
38 * times that the kernel will need to distinguish between SME and SEV. The
39 * cc_platform_has() function is used for this. When a distinction isn't
40 * needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
42 * The trampoline code is a good example for this requirement. Before
43 * paging is activated, SME will access all memory as decrypted, but SEV
44 * will access all memory as encrypted. So, when APs are being brought
45 * up under SME the trampoline area cannot be encrypted, whereas under SEV
46 * the trampoline area must be encrypted.
48 static bool amd_cc_platform_has(enum cc_attr attr)
50 #ifdef CONFIG_AMD_MEM_ENCRYPT
52 case CC_ATTR_MEM_ENCRYPT:
55 case CC_ATTR_HOST_MEM_ENCRYPT:
56 return sme_me_mask && !(sev_status & MSR_AMD64_SEV_ENABLED);
58 case CC_ATTR_GUEST_MEM_ENCRYPT:
59 return sev_status & MSR_AMD64_SEV_ENABLED;
61 case CC_ATTR_GUEST_STATE_ENCRYPT:
62 return sev_status & MSR_AMD64_SEV_ES_ENABLED;
65 * With SEV, the rep string I/O instructions need to be unrolled
66 * but SEV-ES supports them through the #VC handler.
68 case CC_ATTR_GUEST_UNROLL_STRING_IO:
69 return (sev_status & MSR_AMD64_SEV_ENABLED) &&
70 !(sev_status & MSR_AMD64_SEV_ES_ENABLED);
72 case CC_ATTR_GUEST_SEV_SNP:
73 return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
83 static bool hyperv_cc_platform_has(enum cc_attr attr)
85 return attr == CC_ATTR_GUEST_MEM_ENCRYPT;
88 bool cc_platform_has(enum cc_attr attr)
92 return amd_cc_platform_has(attr);
94 return intel_cc_platform_has(attr);
95 case CC_VENDOR_HYPERV:
96 return hyperv_cc_platform_has(attr);
101 EXPORT_SYMBOL_GPL(cc_platform_has);
103 u64 cc_mkenc(u64 val)
106 * Both AMD and Intel use a bit in the page table to indicate
107 * encryption status of the page.
109 * - for AMD, bit *set* means the page is encrypted
110 * - for Intel *clear* means encrypted.
114 return val | cc_mask;
115 case CC_VENDOR_INTEL:
116 return val & ~cc_mask;
122 u64 cc_mkdec(u64 val)
124 /* See comment in cc_mkenc() */
127 return val & ~cc_mask;
128 case CC_VENDOR_INTEL:
129 return val | cc_mask;
134 EXPORT_SYMBOL_GPL(cc_mkdec);
136 __init void cc_random_init(void)
139 * The seed is 32 bytes (in units of longs), which is 256 bits, which
140 * is the security level that the RNG is targeting.
142 unsigned long rng_seed[32 / sizeof(long)];
145 if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
149 * Since the CoCo threat model includes the host, the only reliable
150 * source of entropy that can be neither observed nor manipulated is
151 * RDRAND. Usually, RDRAND failure is considered tolerable, but since
152 * CoCo guests have no other unobservable source of entropy, it's
153 * important to at least ensure the RNG gets some initial random seeds.
155 for (i = 0; i < ARRAY_SIZE(rng_seed); i += longs) {
156 longs = arch_get_random_longs(&rng_seed[i], ARRAY_SIZE(rng_seed) - i);
159 * A zero return value means that the guest doesn't have RDRAND
160 * or the CPU is physically broken, and in both cases that
161 * means most crypto inside of the CoCo instance will be
162 * broken, defeating the purpose of CoCo in the first place. So
163 * just panic here because it's absolutely unsafe to continue
167 panic("RDRAND is defective.");
169 add_device_randomness(rng_seed, sizeof(rng_seed));
170 memzero_explicit(rng_seed, sizeof(rng_seed));