SMT Protection (SMTP) protects an SEV-SNP guest from sibling side channels by requiring that, while one of its vCPUs is in guest mode, every SMT sibling thread on that physical core is HLT idle in host mode. Add an "smtp" property to request and use it to advertize this feature for KVM to write it into the launch-time VMSAs. Sample command-line: -object sev-snp-guest,id=sev0,policy=0xb0000,cbitpos=51,reduced-phys-bits=1,smtp=on Signed-off-by: Pratik R. Sampat --- qapi/qom.json | 9 ++++++++- target/i386/sev.c | 15 +++++++++++++++ target/i386/sev.h | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/qapi/qom.json b/qapi/qom.json index b8237a94be01..3e270895a6d6 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -1101,6 +1101,12 @@ # @tsc-frequency: set secure TSC frequency. Only valid if Secure TSC # is enabled (default: zero) (since 11.1) # +# @smtp: Enable SMT Protection support for the guest. Adds +# corresponding VMSA feature bit to KVM_SEV_INIT2 vmsa_features so +# the host applies it to each vCPU's VMSA. Opt-in feature as it +# provides additional protection but with runtime overhead. +# (default: false) +# # Features: # # @confidential-guest-reset: If present, the hypervisor supports @@ -1119,7 +1125,8 @@ '*host-data': 'str', '*vcek-disabled': 'bool', '*secure-tsc': 'bool', - '*tsc-frequency': 'uint32' }, + '*tsc-frequency': 'uint32', + '*smtp': 'bool' }, 'features': [ 'confidential-guest-reset' ] } ## diff --git a/target/i386/sev.c b/target/i386/sev.c index 604c2255ac04..845f0b1f37f2 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -3316,6 +3316,18 @@ sev_snp_guest_set_tsc_frequency(Object *obj, Visitor *v, const char *name, SEV_SNP_GUEST(obj)->tsc_khz = value / 1000; } +static bool +sev_snp_guest_get_smtp(Object *obj, Error **errp) +{ + return is_sev_feature_set(SEV_COMMON(obj), SVM_SEV_FEAT_SMTP); +} + +static void +sev_snp_guest_set_smtp(Object *obj, bool value, Error **errp) +{ + sev_set_feature(SEV_COMMON(obj), SVM_SEV_FEAT_SMTP, value); +} + static void sev_snp_guest_class_init(ObjectClass *oc, const void *data) { @@ -3357,6 +3369,9 @@ sev_snp_guest_class_init(ObjectClass *oc, const void *data) object_class_property_add(oc, "tsc-frequency", "uint32", &uint64_type_info, sev_snp_guest_get_tsc_frequency, sev_snp_guest_set_tsc_frequency, NULL, NULL); + object_class_property_add_bool(oc, "smtp", + sev_snp_guest_get_smtp, + sev_snp_guest_set_smtp); } static void diff --git a/target/i386/sev.h b/target/i386/sev.h index 7725f92e1959..2c16a73d9a61 100644 --- a/target/i386/sev.h +++ b/target/i386/sev.h @@ -49,6 +49,7 @@ bool sev_snp_enabled(void); #define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) #define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) #define SVM_SEV_FEAT_SECURE_TSC BIT(9) +#define SVM_SEV_FEAT_SMTP BIT(15) typedef struct SevKernelLoaderContext { char *setup_data; -- 2.43.0 Enhanced SMT Protection (ESMTP) protects an SEV-SNP guest from sibling side channels by requiring that, while a vCPU is in guest mode, its SMT sibling thread is either idle in host mode or executing a vCPU the guest has declared to be a legal sibling. Add an "esmtp" property to request and use it to advertize this feature for KVM to write it into the launch-time VMSAs. Sample command-line: -object sev-snp-guest,id=sev0,policy=0xb0000,cbitpos=51,reduced-phys-bits=1,esmtp=on Signed-off-by: Pratik R. Sampat --- qapi/qom.json | 9 ++++++++- target/i386/sev.c | 21 +++++++++++++++++++++ target/i386/sev.h | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/qapi/qom.json b/qapi/qom.json index 3e270895a6d6..d60936a56be1 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -1107,6 +1107,12 @@ # provides additional protection but with runtime overhead. # (default: false) # +# @esmtp: Enable Enhanced SMT Protection support for the guest. Adds +# corresponding VMSA feature bit to KVM_SEV_INIT2 vmsa_features so +# the host applies it to each vCPU's VMSA. Opt-in feature as it +# provides additional protection but with runtime overhead. +# (default: false) (since 11.2) +# # Features: # # @confidential-guest-reset: If present, the hypervisor supports @@ -1126,7 +1132,8 @@ '*vcek-disabled': 'bool', '*secure-tsc': 'bool', '*tsc-frequency': 'uint32', - '*smtp': 'bool' }, + '*smtp': 'bool', + '*esmtp': 'bool' }, 'features': [ 'confidential-guest-reset' ] } ## diff --git a/target/i386/sev.c b/target/i386/sev.c index 845f0b1f37f2..1b5b833974cc 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -545,6 +545,12 @@ static int check_sev_features(SevCommonState *sev_common, uint64_t sev_features, "SEV: TSC frequency can only be set if Secure TSC is enabled"); return -1; } + if ((sev_features & SVM_SEV_FEAT_ESMTP) && + (sev_features & SVM_SEV_FEAT_SMTP)) { + error_setg(errp, + "SEV: SMT Protection and Enhanced SMT Protection are mutually exclusive"); + return -1; + } } else { if (sev_features && sev_es_enabled()) { error_setg(errp, @@ -3328,6 +3334,18 @@ sev_snp_guest_set_smtp(Object *obj, bool value, Error **errp) sev_set_feature(SEV_COMMON(obj), SVM_SEV_FEAT_SMTP, value); } +static bool +sev_snp_guest_get_esmtp(Object *obj, Error **errp) +{ + return is_sev_feature_set(SEV_COMMON(obj), SVM_SEV_FEAT_ESMTP); +} + +static void +sev_snp_guest_set_esmtp(Object *obj, bool value, Error **errp) +{ + sev_set_feature(SEV_COMMON(obj), SVM_SEV_FEAT_ESMTP, value); +} + static void sev_snp_guest_class_init(ObjectClass *oc, const void *data) { @@ -3372,6 +3390,9 @@ sev_snp_guest_class_init(ObjectClass *oc, const void *data) object_class_property_add_bool(oc, "smtp", sev_snp_guest_get_smtp, sev_snp_guest_set_smtp); + object_class_property_add_bool(oc, "esmtp", + sev_snp_guest_get_esmtp, + sev_snp_guest_set_esmtp); } static void diff --git a/target/i386/sev.h b/target/i386/sev.h index 2c16a73d9a61..566155417de8 100644 --- a/target/i386/sev.h +++ b/target/i386/sev.h @@ -50,6 +50,7 @@ bool sev_snp_enabled(void); #define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) #define SVM_SEV_FEAT_SECURE_TSC BIT(9) #define SVM_SEV_FEAT_SMTP BIT(15) +#define SVM_SEV_FEAT_ESMTP BIT(17) typedef struct SevKernelLoaderContext { char *setup_data; -- 2.43.0 Mint a new cookie for each vCPU thread. A vCPU with a unique cookie is never scheduled concurrently on the sibling thread of the same core. So, while one CPU thread runs, the scheduler forces the sibling to idle. The core scheduling mechanism cannot affect isolation. Hardware enforces the requirement that while a vCPU is in the guest mode, the sibling thread is in idle. It does decide whether the guest makes progress even when the vCPU threads are scheduled or pinned on the same core without being stalled in a KVM VMEXIT_IDLE_REQUIRED retry loop. Signed-off-by: Pratik R. Sampat --- target/i386/sev.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/target/i386/sev.c b/target/i386/sev.c index 1b5b833974cc..711d28780983 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -18,6 +18,7 @@ #include #include +#include #include "qapi/error.h" #include "qapi/qapi-type-infos-common.h" @@ -2040,6 +2041,31 @@ static int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) return 0; } +static void smtp_core_sched_setup(CPUState *leader, run_on_cpu_data arg) +{ + CPUState *cpu; + int ret; + + CPU_FOREACH(cpu) { + ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, cpu->thread_id, + PR_SCHED_CORE_SCOPE_THREAD, 0); + if (ret < 0) { + error_report("vCPU %lu: failed to create core-sched cookie: %s", + kvm_arch_vcpu_id(cpu), strerror(errno)); + return; + } + } +} + +static void smtp_core_sched_notify(Notifier *notifier, void *data) +{ + run_on_cpu(first_cpu, smtp_core_sched_setup, RUN_ON_CPU_NULL); +} + +static Notifier sev_snp_smtp_notify = { + .notify = smtp_core_sched_notify, +}; + static int sev_snp_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) { MachineState *ms = MACHINE(qdev_get_machine()); @@ -2058,6 +2084,11 @@ static int sev_snp_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) g_free(sev_snp_guest->kernel_hashes_data); sev_snp_guest->kernel_hashes_data = NULL; + /* Setup core scheduling cookies for SMTP after the vCPUs are all up */ + if (is_sev_feature_set(sev_common, SVM_SEV_FEAT_SMTP)) { + qemu_add_machine_init_done_notifier(&sev_snp_smtp_notify); + } + return 0; } -- 2.43.0 Group the vCPU threads for core scheduling such that trusted siblings share cookies and can be co-scheduled on the same core. The grouping cannot affect isolation. Hardware checks each pairing against the mask in the VMSA, which only the guest writes, so nothing chosen here can bring about a pairing hardware would have refused. It does decide whether the guest makes progress. Co-scheduling two vCPUs whose masks disagree makes every VMRUN exit with VMEXIT_ILLSIB, which KVM can only answer by retrying, and that never converges. Signed-off-by: Pratik R. Sampat --- target/i386/sev.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/target/i386/sev.c b/target/i386/sev.c index 711d28780983..b10c0f24cea9 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -2041,6 +2041,47 @@ static int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) return 0; } +/* + * Mint a cookie for the first vCPU and copy it to all the others, so that + * every vCPU thread of this guest ends up in one core-scheduling group. + */ +static void esmtp_core_sched_setup(CPUState *leader, run_on_cpu_data arg) +{ + CPUState *cpu; + int ret; + + ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, leader->thread_id, + PR_SCHED_CORE_SCOPE_THREAD, 0); + if (ret < 0) { + error_report("vCPU %lu: failed to create core-sched cookie: %s", + kvm_arch_vcpu_id(leader), strerror(errno)); + return; + } + + CPU_FOREACH(cpu) { + if (cpu == leader) { + continue; + } + + ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, cpu->thread_id, + PR_SCHED_CORE_SCOPE_THREAD, 0); + if (ret < 0) { + error_report("vCPU %lu: failed to share core-sched cookie: %s", + kvm_arch_vcpu_id(cpu), strerror(errno)); + return; + } + } +} + +static void esmtp_core_sched_notify(Notifier *notifier, void *data) +{ + run_on_cpu(first_cpu, esmtp_core_sched_setup, RUN_ON_CPU_NULL); +} + +static Notifier sev_snp_esmtp_notify = { + .notify = esmtp_core_sched_notify, +}; + static void smtp_core_sched_setup(CPUState *leader, run_on_cpu_data arg) { CPUState *cpu; @@ -2089,6 +2130,11 @@ static int sev_snp_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) qemu_add_machine_init_done_notifier(&sev_snp_smtp_notify); } + /* Setup core scheduling cookies for ESMTP after the vCPUs are all up */ + if (is_sev_feature_set(sev_common, SVM_SEV_FEAT_ESMTP)) { + qemu_add_machine_init_done_notifier(&sev_snp_esmtp_notify); + } + return 0; } -- 2.43.0