Merge tag 'hyperv-next-signed-20220114' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / arch / x86 / kernel / cc_platform.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Confidential Computing Platform Capability checks
4  *
5  * Copyright (C) 2021 Advanced Micro Devices, Inc.
6  *
7  * Author: Tom Lendacky <thomas.lendacky@amd.com>
8  */
9
10 #include <linux/export.h>
11 #include <linux/cc_platform.h>
12 #include <linux/mem_encrypt.h>
13
14 #include <asm/mshyperv.h>
15 #include <asm/processor.h>
16
17 static bool __maybe_unused intel_cc_platform_has(enum cc_attr attr)
18 {
19 #ifdef CONFIG_INTEL_TDX_GUEST
20         return false;
21 #else
22         return false;
23 #endif
24 }
25
26 /*
27  * SME and SEV are very similar but they are not the same, so there are
28  * times that the kernel will need to distinguish between SME and SEV. The
29  * cc_platform_has() function is used for this.  When a distinction isn't
30  * needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
31  *
32  * The trampoline code is a good example for this requirement.  Before
33  * paging is activated, SME will access all memory as decrypted, but SEV
34  * will access all memory as encrypted.  So, when APs are being brought
35  * up under SME the trampoline area cannot be encrypted, whereas under SEV
36  * the trampoline area must be encrypted.
37  */
38 static bool amd_cc_platform_has(enum cc_attr attr)
39 {
40 #ifdef CONFIG_AMD_MEM_ENCRYPT
41         switch (attr) {
42         case CC_ATTR_MEM_ENCRYPT:
43                 return sme_me_mask;
44
45         case CC_ATTR_HOST_MEM_ENCRYPT:
46                 return sme_me_mask && !(sev_status & MSR_AMD64_SEV_ENABLED);
47
48         case CC_ATTR_GUEST_MEM_ENCRYPT:
49                 return sev_status & MSR_AMD64_SEV_ENABLED;
50
51         case CC_ATTR_GUEST_STATE_ENCRYPT:
52                 return sev_status & MSR_AMD64_SEV_ES_ENABLED;
53
54         /*
55          * With SEV, the rep string I/O instructions need to be unrolled
56          * but SEV-ES supports them through the #VC handler.
57          */
58         case CC_ATTR_GUEST_UNROLL_STRING_IO:
59                 return (sev_status & MSR_AMD64_SEV_ENABLED) &&
60                         !(sev_status & MSR_AMD64_SEV_ES_ENABLED);
61
62         default:
63                 return false;
64         }
65 #else
66         return false;
67 #endif
68 }
69
70 static bool hyperv_cc_platform_has(enum cc_attr attr)
71 {
72         return attr == CC_ATTR_GUEST_MEM_ENCRYPT;
73 }
74
75 bool cc_platform_has(enum cc_attr attr)
76 {
77         if (sme_me_mask)
78                 return amd_cc_platform_has(attr);
79
80         if (hv_is_isolation_supported())
81                 return hyperv_cc_platform_has(attr);
82
83         return false;
84 }
85 EXPORT_SYMBOL_GPL(cc_platform_has);