x86: Enumerate TME and PCONFIG, add MKTME_KEY_PROG helper
v2 cover x86: Enumerate TME and PCONFIG, add MKTME_KEY_PROG helper
The patchset does some ground work for MKTME enabling:
- Adds two new cpufeatures: TME and PCONFIG;
- Detects if BIOS enabled TME and MKTME;
- Enumerates what PCONFIG targets are supported;
- Provides helper to program encryption keys into CPU;
As part of TME enumeration we check of how many bits from physical address are claimed for encryption key ID. This may be critical as we or guest VM must not use these bits for physical address.
Please review and consider applying.
v2:
- Fixes for TME enumeration;
- Add PCONFIG CPUID leaf support;
- Add MKTME_KEY_PROG helper;
Kirill A. Shutemov (5):
x86/cpufeatures: Add Intel Total Memory Encryption cpufeature x86/tme: Detect if TME and MKTME is activated by BIOS x86/cpufeatures: Add Intel PCONFIG cpufeature x86/pconfig: Detect PCONFIG targets
x86/pconfig: Provide defines and helper to run MKTME_KEY_PROG leaf
arch/x86/include/asm/cpufeatures.h | 2 + arch/x86/include/asm/intel_pconfig.h | 65 +++++++++++++++++++++++++ arch/x86/kernel/cpu/Makefile | 2 +- arch/x86/kernel/cpu/intel.c | 94 ++++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/intel_pconfig.c | 82 +++++++++++++++++++++++++++++++ 5 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/intel_pconfig.h create mode 100644 arch/x86/kernel/cpu/intel_pconfig.c
Signature · 1 line
2.15.1
PATCH v2 1/5 x86/cpufeatures: Add Intel Total Memory Encryption cpufeature
CPUID.0x7.0x0:ECX[13] indicates whether CPU supports Intel Total Memory Encryption.
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -315,6 +315,7 @@#define X86_FEATURE_VPCLMULQDQ (16*32+10)/* Carry-Less Multiplication Double Quadword */ #define X86_FEATURE_AVX512_VNNI (16*32+11)/* Vector Neural Network Instructions */ #define X86_FEATURE_AVX512_BITALG (16*32+12)/* Support for VPOPCNT[B,W] and VPSHUF-BITQMB instructions */ +#define X86_FEATURE_TME (16*32+13)/* Intel Total Memory Encryption */ #define X86_FEATURE_AVX512_VPOPCNTDQ (16*32+14)/* POPCNT for vectors of DW/QW */ #define X86_FEATURE_LA57 (16*32+16)/* 5-level page tables */ #define X86_FEATURE_RDPID (16*32+22)/* RDPID instruction */
Signature · 1 line
2.15.1
PATCH v2 2/5 x86/tme: Detect if TME and MKTME is activated by BIOS
In reply to Kirill A. ShutemovIA32_TME_ACTIVATE MSR (0x982) can be used to check if BIOS has enabled TME and MKTME. It includes which encryption policy/algorithm is selected for TME or available for MKTME. For MKTME, the MSR also enumerates how many KeyIDs are available.
--- arch/x86/kernel/cpu/intel.c | 94 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+)
arch/x86/kernel/cpu/intel.c +94 -0
@@ -506,6 +506,97 @@ static void detect_vmx_virtcap(struct cpuinfo_x86 *c) } } +#define MSR_IA32_TME_ACTIVATE 0x982 + +#define TME_ACTIVATE_LOCKED (x) (x & 0x1) +#define TME_ACTIVATE_ENABLED (x) (x & 0x2) + +#define TME_ACTIVATE_POLICY (x) ((x >> 4) & 0xf)/* Bits 7:4 */ +#define TME_ACTIVATE_POLICY_AES_XTS_128 0 + +#define TME_ACTIVATE_KEYID_BITS (x) ((x >> 32) & 0xf)/* Bits 35:32 */ + +#define TME_ACTIVATE_CRYPTO_ALGS (x) ((x >> 48) & 0xffff)/* Bits 63:48 */ +#define TME_ACTIVATE_CRYPTO_AES_XTS_128 1 + +#define MKTME_ENABLED 0 +#define MKTME_DISABLED 1 +#define MKTME_UNINITIALIZED 2 +static int mktme_status =MKTME_UNINITIALIZED ; + +static void detect_keyid_bits (struct cpuinfo_x86 *c,u64 tme_activate) +{ +int keyid_bits =0 , nr_keyids =0 ; + + keyid_bits =TME_ACTIVATE_KEYID_BITS (tme_activate); + nr_keyids = (1UL << keyid_bits) -1 ; +if (nr_keyids) { +pr_info_once ("x86/mktme: enabled by BIOS\n" ); +pr_info_once ("x86/mktme: %d KeyIDs available\n" , nr_keyids); + }else { +pr_info_once ("x86/mktme: disabled by BIOS\n" ); + } + +if (mktme_status ==MKTME_UNINITIALIZED ) { +/* MKTME is usable */ + mktme_status =MKTME_ENABLED ; + } + +/* +* Exclude KeyID bits from physical address bits. +* +* We have to do this even if we are not going to use KeyID bits +* ourself. VM guests still have to know that these bits are not usable +* for physical address. +*/ + c->x86_phys_bits -= keyid_bits; +} + +static void detect_tme (struct cpuinfo_x86 *c) +{ +u64 tme_activate, tme_policy, tme_crypto_algs; +static u64 tme_activate_cpu0 =0 ; + +rdmsrl (MSR_IA32_TME_ACTIVATE , tme_activate); + +if (mktme_status !=MKTME_UNINITIALIZED ) { +if (tme_activate != tme_activate_cpu0) { +/* Broken BIOS? */ +pr_err_once ("x86/tme: configuation is inconsistent between CPUs\n" ); +pr_err_once ("x86/tme: MKTME is not usable\n" ); + mktme_status =MKTME_DISABLED ; + +/* Proceed. We may need to exclude bits from x86_phys_bits. */ + } + }else { + tme_activate_cpu0 = tme_activate; + } + +if (!TME_ACTIVATE_LOCKED (tme_activate) || !TME_ACTIVATE_ENABLED (tme_activate)) { +pr_info_once ("x86/tme: not enabled by BIOS\n" ); + mktme_status =MKTME_DISABLED ; +return ; + } + +if (mktme_status !=MKTME_UNINITIALIZED ) +return detect_keyid_bits (c, tme_activate); + +pr_info ("x86/tme: enabled by BIOS\n" ); + + tme_policy =TME_ACTIVATE_POLICY (tme_activate); +if (tme_policy !=TME_ACTIVATE_POLICY_AES_XTS_128 ) +pr_warn ("x86/tme: Unknown policy is active: %#llx\n" , tme_policy); + + tme_crypto_algs =TME_ACTIVATE_CRYPTO_ALGS (tme_activate); +if (!(tme_crypto_algs &TME_ACTIVATE_CRYPTO_AES_XTS_128 )) { +pr_err ("x86/mktme: No known encryption algorithm is supported: %#llx\n" , + tme_crypto_algs); + mktme_status =MKTME_DISABLED ; + } + +detect_keyid_bits (c, tme_activate); +} +static void init_intel_energy_perf (struct cpuinfo_x86 *c) {u64 epb;
@@ -676,6 +767,9 @@ static void init_intel(struct cpuinfo_x86 *c)if (cpu_has (c,X86_FEATURE_VMX ))detect_vmx_virtcap (c); +if (cpu_has (c,X86_FEATURE_TME )) +detect_tme (c); +init_intel_energy_perf (c);init_intel_misc_features (c);
Signature · 1 line
2.15.1
Re: [PATCHv2 2/5] x86/tme: Detect if TME and MKTME is activated by BIOS
Quoted 4 lines · Kirill A. Shutemov
IA32_TME_ACTIVATEMSR (0x982) can be used to check if BIOS has enabled TME and MKTME. It includes which encryption policy/algorithm is selected for TME or available for MKTME. For MKTME, the MSR also enumerates how many KeyIDs are available.
The hacking of the phys_addr_bits is a pretty important part of this. Are you sure it's not worth calling out in the description?
Quoted 6 lines
arch/x86/kernel/cpu/intel.c +6
-0+#define MSR_IA32_TME_ACTIVATE 0x982 + +#define TME_ACTIVATE_LOCKED (x) (x & 0x1) +#define TME_ACTIVATE_ENABLED (x) (x & 0x2) + +#define TME_ACTIVATE_POLICY (x) ((x >> 4) & 0xf)/* Bits 7:4 */
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 524
Quoted 10 lines
arch/x86/kernel/cpu/intel.c +10
-0+#define TME_ACTIVATE_POLICY_AES_XTS_128 0 + +#define TME_ACTIVATE_KEYID_BITS (x) ((x >> 32) & 0xf)/* Bits 35:32 */ + +#define TME_ACTIVATE_CRYPTO_ALGS (x) ((x >> 48) & 0xffff)/* Bits 63:48 */ +#define TME_ACTIVATE_CRYPTO_AES_XTS_128 1 + +#define MKTME_ENABLED 0 +#define MKTME_DISABLED 1 +#define MKTME_UNINITIALIZED 2
The indentation there looks a bit wonky. Might want to double-check it.
Also, can you clearly spell out which of these things are software constructs vs. hardware ones? MKTME_* look like software constructs.
Quoted 4 lines
arch/x86/kernel/cpu/intel.c +4
-0+static int mktme_status =MKTME_UNINITIALIZED ; + +static void detect_keyid_bits (struct cpuinfo_x86 *c,u64 tme_activate) +{
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 538
Quoted 10 lines
arch/x86/kernel/cpu/intel.c +10
-0+int keyid_bits =0 , nr_keyids =0 ; + + keyid_bits =TME_ACTIVATE_KEYID_BITS (tme_activate); + nr_keyids = (1UL << keyid_bits) -1 ; +if (nr_keyids) { +pr_info_once ("x86/mktme: enabled by BIOS\n" ); +pr_info_once ("x86/mktme: %d KeyIDs available\n" , nr_keyids); + }else { +pr_info_once ("x86/mktme: disabled by BIOS\n" ); + }
Just curious, but how do you know that this indicates the BIOS disabling MKTME?
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 543
Quoted 4 lines
arch/x86/kernel/cpu/intel.c +4
-0+if (mktme_status ==MKTME_UNINITIALIZED ) { +/* MKTME is usable */ + mktme_status =MKTME_ENABLED ; + }
To me, it's a little bit odd that we "enable" MKTME down in the keyid detection code. I wonder if you could just return the resulting number of keyids and then actually do the mktme_status munging in one place (detect_tme()).
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 553
Quoted 9 lines
arch/x86/kernel/cpu/intel.c +9
-0+/* +* Exclude KeyID bits from physical address bits. +* +* We have to do this even if we are not going to use KeyID bits +* ourself. VM guests still have to know that these bits are not usable +* for physical address. +*/ + c->x86_phys_bits -= keyid_bits; +}
How do we tell guests about this? kvm_set_mmio_spte_mask()?
Quoted 18 lines
arch/x86/kernel/cpu/intel.c +18
-0+static void detect_tme (struct cpuinfo_x86 *c) +{ +u64 tme_activate, tme_policy, tme_crypto_algs; +static u64 tme_activate_cpu0 =0 ; + +rdmsrl (MSR_IA32_TME_ACTIVATE , tme_activate); + +if (mktme_status !=MKTME_UNINITIALIZED ) { +if (tme_activate != tme_activate_cpu0) { +/* Broken BIOS? */ +pr_err_once ("x86/tme: configuation is inconsistent between CPUs\n" ); +pr_err_once ("x86/tme: MKTME is not usable\n" ); + mktme_status =MKTME_DISABLED ; + +/* Proceed. We may need to exclude bits from x86_phys_bits. */ + } + }else { + tme_activate_cpu0 = tme_activate;
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 582
Quoted 10 lines
arch/x86/kernel/cpu/intel.c +10
-0+ } + +if (!TME_ACTIVATE_LOCKED (tme_activate) || !TME_ACTIVATE_ENABLED (tme_activate)) { +pr_info_once ("x86/tme: not enabled by BIOS\n" ); + mktme_status =MKTME_DISABLED ; +return ; + } + +if (mktme_status !=MKTME_UNINITIALIZED ) +return detect_keyid_bits (c, tme_activate);
Returning the result of a void function is a bit odd-looking. Would it look nicer to just have a label and some gotos to the detection?
Quoted 5 lines
arch/x86/kernel/cpu/intel.c +5
-0+pr_info ("x86/tme: enabled by BIOS\n" ); + + tme_policy =TME_ACTIVATE_POLICY (tme_activate); +if (tme_policy !=TME_ACTIVATE_POLICY_AES_XTS_128 ) +pr_warn ("x86/tme: Unknown policy is active: %#llx\n" , tme_policy);
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 598
Quoted 10 lines
arch/x86/kernel/cpu/intel.c +10
-0+ + tme_crypto_algs =TME_ACTIVATE_CRYPTO_ALGS (tme_activate); +if (!(tme_crypto_algs &TME_ACTIVATE_CRYPTO_AES_XTS_128 )) { +pr_err ("x86/mktme: No known encryption algorithm is supported: %#llx\n" , + tme_crypto_algs); + mktme_status =MKTME_DISABLED ; + } + +detect_keyid_bits (c, tme_activate); +}
I noticed that this code is not optional, other than by disabling CPU_SUP_INTEL. Was that intentional? What were your thoughts behind that?
Re: [PATCHv2 2/5] x86/tme: Detect if TME and MKTME is activated by BIOS
Quoted 70 lines · Dave Hansen
Quoted 7 lines · Kirill A. Shutemov
IA32_TME_ACTIVATEMSR (0x982) can be used to check if BIOS has enabled
TME and MKTME. It includes which encryption policy/algorithm is selected
for TME or available for MKTME. For MKTME, the MSR also enumerates how
many KeyIDs are available.The hacking of the
phys_addr_bitsis a pretty important part of this. Are you sure it's not worth calling out in the description?Quoted 14 lines
arch/x86/kernel/cpu/intel.c +6
-0+#define MSR_IA32_TME_ACTIVATE 0x982 + +#define TME_ACTIVATE_LOCKED (x) (x & 0x1) +#define TME_ACTIVATE_ENABLED (x) (x & 0x2) + +#define TME_ACTIVATE_POLICY (x) ((x >> 4) & 0xf)/* Bits 7:4 */
arch/x86/kernel/cpu/intel.c +3
-0+#define TME_ACTIVATE_POLICY_AES_XTS_128 0 + +#define TME_ACTIVATE_KEYID_BITS (x) ((x >> 32) & 0xf)/* Bits 35:32 */
(quoted diff) +2
-0+ +#define TME_ACTIVATE_CRYPTO_ALGS(x) ((x >> 48) & 0xffff)/* Bits 63:48 */
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 524
Quoted 5 lines
arch/x86/kernel/cpu/intel.c +5
-0+#define TME_ACTIVATE_CRYPTO_AES_XTS_128 1 + +#define MKTME_ENABLED 0 +#define MKTME_DISABLED 1 +#define MKTME_UNINITIALIZED 2The indentation there looks a bit wonky. Might want to double-check it.
Also, can you clearly spell out which of these things are software constructs vs. hardware ones? MKTME_* look like software constructs.
Quoted 13 lines
arch/x86/kernel/cpu/intel.c +3
-0+static int mktme_status =MKTME_UNINITIALIZED ; + +static void detect_keyid_bits(struct cpuinfo_x86 *c, u64
tme_activate)arch/x86/kernel/cpu/intel.c +8
-0+{ +int keyid_bits =0 , nr_keyids =0 ; + + keyid_bits =TME_ACTIVATE_KEYID_BITS (tme_activate); + nr_keyids = (1UL << keyid_bits) -1 ; +if (nr_keyids) { +pr_info_once ("x86/mktme: enabled by BIOS\n" ); + pr_info_once("x86/mktme: %d KeyIDs available\n",
nr_keyids);arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 538
Quoted 3 lines
arch/x86/kernel/cpu/intel.c +3
-0+ }else { +pr_info_once ("x86/mktme: disabled by BIOS\n" ); + }Just curious, but how do you know that this indicates the BIOS disabling
MKTME?arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 543
Quoted 4 lines
arch/x86/kernel/cpu/intel.c +4
-0+if (mktme_status ==MKTME_UNINITIALIZED ) { +/* MKTME is usable */ + mktme_status =MKTME_ENABLED ; + }To me, it's a little bit odd that we "enable" MKTME down in the keyid detection code. I wonder if you could just return the resulting number
of keyids and then actually do themktme_statusmunging in one place (detect_tme()).
Quoted 13 lines
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 553
Quoted 11 lines
arch/x86/kernel/cpu/intel.c +4
-0+/* +* Exclude KeyID bits from physical address bits. +* +* We have to do this even if we are not going to use KeyID bits
(quoted diff) +1
-0+ * ourself. VM guests still have to know that these bitsare not usable
arch/x86/kernel/cpu/intel.c +4
-0+* for physical address. +*/ + c->x86_phys_bits -= keyid_bits; +}How do we tell guests about this?
kvm_set_mmio_spte_mask()?
Hi Dave,
KVM tells guest physical bits info in CPUID emulating from guest. Currently KVM uses native CPUID to get physical bits info, and report it to guest in CPUID emulating. KVM is not consulting c->x86_phys_bits in CPUID emulation now, but for MK-TME I think it should be reasonable for KVM to do that.
The kvm_set_mmio_spte_mask() you mentioned is used to setup pte mask to cause page fault for guest's MMIO pages (in shadow MMU mode only, for EPT we have different function). It is using boot_cpu_data.x86_phys_bits (for which we need to do code update for MK-TME), but this function is not related to reporting physical bits info to guest.
Thanks,
-Kai
Quoted 60 lines
Quoted 26 lines
arch/x86/kernel/cpu/intel.c +11
-0+static void detect_tme (struct cpuinfo_x86 *c) +{ +u64 tme_activate, tme_policy, tme_crypto_algs; +static u64 tme_activate_cpu0 =0 ; + +rdmsrl (MSR_IA32_TME_ACTIVATE , tme_activate); + +if (mktme_status !=MKTME_UNINITIALIZED ) { +if (tme_activate != tme_activate_cpu0) { +/* Broken BIOS? */ + pr_err_once("x86/tme: configuation isinconsistent between CPUs\n");
(quoted diff) +1
-0+ pr_err_once("x86/tme: MKTME is notusable\n");
arch/x86/kernel/cpu/intel.c +3
-0+ mktme_status =MKTME_DISABLED ; + +/* Proceed. We may need to exclude bits from
x86_phys_bits. */arch/x86/kernel/cpu/intel.c +6
-0+ } + }else { + tme_activate_cpu0 = tme_activate; + } + + if (!TME_ACTIVATE_LOCKED(tme_activate) ||!TME_ACTIVATE_ENABLED(tme_activate)) {
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 582
Quoted 7 lines
arch/x86/kernel/cpu/intel.c +7
-0+pr_info_once ("x86/tme: not enabled by BIOS\n" ); + mktme_status =MKTME_DISABLED ; +return ; + } + +if (mktme_status !=MKTME_UNINITIALIZED ) +return detect_keyid_bits (c, tme_activate);Returning the result of a void function is a bit odd-looking. Would it
look nicer to just have a label and some gotos to the detection?Quoted 12 lines
arch/x86/kernel/cpu/intel.c +5
-0+pr_info ("x86/tme: enabled by BIOS\n" ); + + tme_policy =TME_ACTIVATE_POLICY (tme_activate); +if (tme_policy !=TME_ACTIVATE_POLICY_AES_XTS_128 ) + pr_warn("x86/tme: Unknown policy is active:%#llx\n",
tme_policy);arch/x86/kernel/cpu/intel.c +3
-0+ + tme_crypto_algs =TME_ACTIVATE_CRYPTO_ALGS (tme_activate); +if (!(tme_crypto_algs &TME_ACTIVATE_CRYPTO_AES_XTS_128 )){
(quoted diff) +1
-0+ pr_err("x86/mktme: No known encryption algorithmis supported: %#llx\n",
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 598
Quoted 6 lines
arch/x86/kernel/cpu/intel.c +6
-0+ tme_crypto_algs); + mktme_status =MKTME_DISABLED ; + } + +detect_keyid_bits (c, tme_activate); +}I noticed that this code is not optional, other than by disabling
CPU_SUP_INTEL. Was that intentional? What were your thoughts behind that?
Re: [PATCHv2 2/5] x86/tme: Detect if TME and MKTME is activated by BIOS
In reply to Dave HansenQuoted 8 lines · Dave Hansen
Quoted 4 lines · Kirill A. Shutemov
IA32_TME_ACTIVATEMSR (0x982) can be used to check if BIOS has enabled TME and MKTME. It includes which encryption policy/algorithm is selected for TME or available for MKTME. For MKTME, the MSR also enumerates how many KeyIDs are available.The hacking of the
phys_addr_bitsis a pretty important part of this. Are you sure it's not worth calling out in the description?
Okay, will do on the next revision.
Quoted 10 lines
Quoted 10 lines
arch/x86/kernel/cpu/intel.c +10
-0+#define MSR_IA32_TME_ACTIVATE 0x982 + +#define TME_ACTIVATE_LOCKED (x) (x & 0x1) +#define TME_ACTIVATE_ENABLED (x) (x & 0x2) + +#define TME_ACTIVATE_POLICY (x) ((x >> 4) & 0xf)/* Bits 7:4 */ +#define TME_ACTIVATE_POLICY_AES_XTS_128 0 + +#define TME_ACTIVATE_KEYID_BITS (x) ((x >> 32) & 0xf)/* Bits 35:32 */ +
Quoted 8 lines
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 524
Quoted 6 lines
arch/x86/kernel/cpu/intel.c +6
-0+#define TME_ACTIVATE_CRYPTO_ALGS (x) ((x >> 48) & 0xffff)/* Bits 63:48 */ +#define TME_ACTIVATE_CRYPTO_AES_XTS_128 1 + +#define MKTME_ENABLED 0 +#define MKTME_DISABLED 1 +#define MKTME_UNINITIALIZED 2The indentation there looks a bit wonky. Might want to double-check it.
Do you mean that MKTME_* is indented differently than the rest?
I'll fix that.
Quoted 2 lines
Also, can you clearly spell out which of these things are software constructs vs. hardware ones? MKTME_* look like software constructs.
Yes, MKTME_* is software. I'll call it out.
Quoted 9 lines
Quoted 9 lines
arch/x86/kernel/cpu/intel.c +9
-0+static int mktme_status =MKTME_UNINITIALIZED ; + +static void detect_keyid_bits (struct cpuinfo_x86 *c,u64 tme_activate) +{ +int keyid_bits =0 , nr_keyids =0 ; + + keyid_bits =TME_ACTIVATE_KEYID_BITS (tme_activate); + nr_keyids = (1UL << keyid_bits) -1 ; +if (nr_keyids) {
Quoted 8 lines
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 538
Quoted 5 lines
arch/x86/kernel/cpu/intel.c +5
-0+pr_info_once ("x86/mktme: enabled by BIOS\n" ); +pr_info_once ("x86/mktme: %d KeyIDs available\n" , nr_keyids); + }else { +pr_info_once ("x86/mktme: disabled by BIOS\n" ); + }Just curious, but how do you know that this indicates the BIOS disabling MKTME?
0 bits for KeyID means we don't have MKTME. Only TME.
Quoted 10 lines
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 543
Quoted 5 lines
arch/x86/kernel/cpu/intel.c +4
-0+if (mktme_status ==MKTME_UNINITIALIZED ) { +/* MKTME is usable */ + mktme_status =MKTME_ENABLED ; + }To me, it's a little bit odd that we "enable" MKTME down in the keyid detection code. I wonder if you could just return the resulting number of keyids and then actually do the
mktme_statusmunging in one place (detect_tme()).
Makes sense.
Quoted 11 lines
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 553
Quoted 9 lines
arch/x86/kernel/cpu/intel.c +9
-0+/* +* Exclude KeyID bits from physical address bits. +* +* We have to do this even if we are not going to use KeyID bits +* ourself. VM guests still have to know that these bits are not usable +* for physical address. +*/ + c->x86_phys_bits -= keyid_bits; +}How do we tell guests about this?
kvm_set_mmio_spte_mask()?
Has Kai answered this for you?
Quoted 23 lines
Quoted 23 lines
arch/x86/kernel/cpu/intel.c +23
-0+static void detect_tme (struct cpuinfo_x86 *c) +{ +u64 tme_activate, tme_policy, tme_crypto_algs; +static u64 tme_activate_cpu0 =0 ; + +rdmsrl (MSR_IA32_TME_ACTIVATE , tme_activate); + +if (mktme_status !=MKTME_UNINITIALIZED ) { +if (tme_activate != tme_activate_cpu0) { +/* Broken BIOS? */ +pr_err_once ("x86/tme: configuation is inconsistent between CPUs\n" ); +pr_err_once ("x86/tme: MKTME is not usable\n" ); + mktme_status =MKTME_DISABLED ; + +/* Proceed. We may need to exclude bits from x86_phys_bits. */ + } + }else { + tme_activate_cpu0 = tme_activate; + } + +if (!TME_ACTIVATE_LOCKED (tme_activate) || !TME_ACTIVATE_ENABLED (tme_activate)) { +pr_info_once ("x86/tme: not enabled by BIOS\n" ); + mktme_status =MKTME_DISABLED ;
Quoted 8 lines
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 582
Quoted 5 lines
arch/x86/kernel/cpu/intel.c +5
-0+return ; + } + +if (mktme_status !=MKTME_UNINITIALIZED ) +return detect_keyid_bits (c, tme_activate);Returning the result of a void function is a bit odd-looking. Would it look nicer to just have a label and some gotos to the detection?
Okay. Either way fine to me.
Quoted 10 lines
Quoted 10 lines
arch/x86/kernel/cpu/intel.c +10
-0+pr_info ("x86/tme: enabled by BIOS\n" ); + + tme_policy =TME_ACTIVATE_POLICY (tme_activate); +if (tme_policy !=TME_ACTIVATE_POLICY_AES_XTS_128 ) +pr_warn ("x86/tme: Unknown policy is active: %#llx\n" , tme_policy); + + tme_crypto_algs =TME_ACTIVATE_CRYPTO_ALGS (tme_activate); +if (!(tme_crypto_algs &TME_ACTIVATE_CRYPTO_AES_XTS_128 )) { +pr_err ("x86/mktme: No known encryption algorithm is supported: %#llx\n" , + tme_crypto_algs);
Quoted 8 lines
arch/x86/kernel/cpu/intel.c static void detect_vmx_virtcap(struct cpuinfo_x86 *c) · line 598
Quoted 5 lines
arch/x86/kernel/cpu/intel.c +5
-0+ mktme_status =MKTME_DISABLED ; + } + +detect_keyid_bits (c, tme_activate); +}I noticed that this code is not optional, other than by disabling
CPU_SUP_INTEL. Was that intentional? What were your thoughts behind that?
We need to mask out bits for KeyID even if we don't use them ourself, so I think we should do this unconditionally.
I need to re-check this with 32-bit kernel, though.
Signature · 1 line
Kirill A. Shutemov
PATCH v2 3/5 x86/cpufeatures: Add Intel PCONFIG cpufeature
In reply to Kirill A. ShutemovCPUID.0x7.0x0:EDX[18] indicates whether Intel CPU support PCONFIG instruction.
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -328,6 +328,7 @@/* Intel-defined CPU features, CPUID level 0x00000007:0 (EDX), word 18 */ #define X86_FEATURE_AVX512_4VNNIW (18*32+ 2)/* AVX-512 Neural Network Instructions */ #define X86_FEATURE_AVX512_4FMAPS (18*32+ 3)/* AVX-512 Multiply Accumulation Single precision */ +#define X86_FEATURE_PCONFIG (18*32+18)/* Intel PCONFIG */ #define X86_FEATURE_SPEC_CTRL (18*32+26)/* "" Speculation Control (IBRS + IBPB) */ #define X86_FEATURE_INTEL_STIBP (18*32+27)/* "" Single Thread Indirect Branch Predictors */ #define X86_FEATURE_ARCH_CAPABILITIES (18*32+29)/* IA32_ARCH_CAPABILITIES MSR (Intel) */
Signature · 1 line
2.15.1
PATCH v2 4/5 x86/pconfig: Detect PCONFIG targets
In reply to Kirill A. ShutemovIntel PCONFIG targets are enumerated via new CPUID leaf 0x1b. This patch detects all supported targets of PCONFIG and implements helper to check if the target is supported.
--- arch/x86/include/asm/intel_pconfig.h | 15 +++++++ arch/x86/kernel/cpu/Makefile | 2 +- arch/x86/kernel/cpu/intel_pconfig.c | 82 ++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/intel_pconfig.h create mode 100644 arch/x86/kernel/cpu/intel_pconfig.c
arch/x86/include/asm/intel_pconfig.h new file +15 -0
@@ -0,0 +1,15 @@ +#ifndef _ASM_X86_INTEL_PCONFIG_H +#define _ASM_X86_INTEL_PCONFIG_H + +#include <asm/asm.h> +#include <asm/processor.h> + +enum pconfig_target { +INVALID_TARGET =0 , +MKTME_TARGET =1 , +PCONFIG_TARGET_NR +}; + +int pconfig_target_supported (enum pconfig_target target); + +#endif /* _ASM_X86_INTEL_PCONFIG_H */
arch/x86/kernel/cpu/Makefile +1 -1
@@ -28,7 +28,7 @@ obj-y += cpuid-deps.o obj-$(CONFIG_PROC_FS) += proc.o obj-$(CONFIG_X86_FEATURE_NAMES) += capflags.o powerflags.o -obj-$(CONFIG_CPU_SUP_INTEL) += intel.o +obj-$(CONFIG_CPU_SUP_INTEL) += intel.o intel_pconfig.o obj-$(CONFIG_CPU_SUP_AMD) += amd.o obj-$(CONFIG_CPU_SUP_CYRIX_32) += cyrix.o obj-$(CONFIG_CPU_SUP_CENTAUR) += centaur.o
arch/x86/kernel/cpu/intel_pconfig.c new file +82 -0
82 lines not on this page · raw message
Signature · 1 line
2.15.1
PATCH v2 5/5 x86/pconfig: Provide defines and helper to run MKTME_KEY_PROG leaf
In reply to Kirill A. ShutemovMKTME_KEY_PROG allows to manipulate MKTME keys in the CPU.
--- arch/x86/include/asm/intel_pconfig.h | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+)
arch/x86/include/asm/intel_pconfig.h +50 -0
@@ -12,4 +12,54 @@ enum pconfig_target {int pconfig_target_supported (enum pconfig_target target); +enum pconfig_leaf { +MKTME_KEY_PROGRAM =0 , +PCONFIG_LEAF_INVALID , +}; + +#define PCONFIG ".byte 0x0f, 0x01, 0xc5" + +/* Defines and structure for MKTME_KEY_PROGRAM of PCONFIG instruction */ + +/* mktme_key_program::keyid_ctrl COMMAND, bits [7:0] */ +#define MKTME_KEYID_SET_KEY_DIRECT 0 +#define MKTME_KEYID_SET_KEY_RANDOM 1 +#define MKTME_KEYID_CLEAR_KEY 2 +#define MKTME_KEYID_NO_ENCRYPT 3 + +/* mktme_key_program::keyid_ctrl ENC_ALG, bits [23:8] */ +#define MKTME_AES_XTS_128 (1 << 8) + +/* Return codes from the PCONFIG MKTME_KEY_PROGRAM */ +#define MKTME_PROG_SUCCESS 0 +#define MKTME_INVALID_PROG_CMD 1 +#define MKTME_ENTROPY_ERROR 2 +#define MKTME_INVALID_KEYID 3 +#define MKTME_INVALID_ENC_ALG 4 +#define MKTME_DEVICE_BUSY 5 + +/* Hardware requires the structure to be 256 byte alinged. Otherwise #GP(0). */ +struct mktme_key_program { +u16 keyid; +u32 keyid_ctrl; +u8 __rsvd[58 ]; +u8 key_field_1[64 ]; +u8 key_field_2[64 ]; +} __packed__aligned(256) ; + +static inline int mktme_key_program (struct mktme_key_program *key_program) +{ +unsigned long rax =MKTME_KEY_PROGRAM ; + +if (!pconfig_target_supported (MKTME_TARGET )) +return -ENXIO ; + + asm volatile(PCONFIG + : "=a" (rax), "=b" (key_program) + : "0" (rax), "1" (key_program) + : "memory", "cc"); + +return rax; +} +#endif /* _ASM_X86_INTEL_PCONFIG_H */
Signature · 1 line
2.15.1
Quoted 9 lines · Kirill A. Shutemov
The patchset does some ground work for MKTME enabling:
- Adds two new cpufeatures: TME and PCONFIG;
- Detects if BIOS enabled TME and MKTME;
- Enumerates what PCONFIG targets are supported;
- Provides helper to program encryption keys into CPU;As part of TME enumeration we check of how many bits from physical address are claimed for encryption key ID. This may be critical as we or guest VM must not use these bits for physical address.
For this kind of stuff, I'd really appreciate if you included some high-level descriptions. I'd assume that a reviewer has no idea what PCONFIG or MKTME is.
It would also be really nice to say which hardware will implement this. Is it in particular CPUs today, for instance?