From nobody Wed Dec 31 06:41:16 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2FEE0C4332F for ; Tue, 7 Nov 2023 15:31:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344015AbjKGPbR (ORCPT ); Tue, 7 Nov 2023 10:31:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344037AbjKGPbF (ORCPT ); Tue, 7 Nov 2023 10:31:05 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93D0F61AB; Tue, 7 Nov 2023 07:02:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699369323; x=1730905323; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oYjyB0PKHYSQZ7RBGG3pOVL3nknQrbhawyzzlQAAUwo=; b=dnIti12upLw89vMfuSvQoHHittb4CtRzerfhEAcvmaxqbhkgoHbTwnEv y9uJ5v8jPa7SubCatn5njG8LHf8pMYGy4s+80xbus12IbBAQggLF5xLw9 dDEkCprPIVfHpaWoJ+ANomOo4YtvLtSa509lLyh7ftu/VT9ZtO0qsoUkY tW9TgIt+tVjYb3gd5Wy8XdfxVJeitUwlgYYojj9uuReIlrP7fZtlvrNnN xot0XaAXt5LcshvSrJJo62MMAovib3zgeS1JdozczikV5VQW0+QoN7qNV 2bol/20kd9YmoUIjC0PQP8mD0mqUgHp9WlU6b4ytEoTYzlWdUAnYEHR40 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10887"; a="2462697" X-IronPort-AV: E=Sophos;i="6.03,284,1694761200"; d="scan'208";a="2462697" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2023 06:58:30 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.03,284,1694761200"; d="scan'208";a="10851675" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Nov 2023 06:58:30 -0800 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v17 115/116] RFC: KVM: x86, TDX: Add check for KVM_SET_CPUID2 Date: Tue, 7 Nov 2023 06:57:21 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata Implement a hook of KVM_SET_CPUID2 for additional consistency check. Intel TDX or AMD SEV has a restriction on the value of cpuid. For example, some values must be the same between all vcpus. Check if the new values are consistent with the old values. The check is light because the cpuid consistency is very model specific and complicated. The user space VMM should set cpuid and MSRs consistently. Suggested-by: Sean Christopherson Link: https://lore.kernel.org/lkml/ZDiGpCkXOcCm074O@google.com/ Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/main.c | 10 ++++++ arch/x86/kvm/vmx/tdx.c | 72 ++++++++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/tdx.h | 7 ++++ arch/x86/kvm/vmx/x86_ops.h | 4 +++ 4 files changed, 87 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index ba7557b6dc57..4abfb0c74d5b 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -443,6 +443,15 @@ static void vt_vcpu_deliver_init(struct kvm_vcpu *vcpu) kvm_vcpu_deliver_init(vcpu); } =20 +static int vt_vcpu_check_cpuid(struct kvm_vcpu *vcpu, + struct kvm_cpuid_entry2 *e2, int nent) +{ + if (is_td_vcpu(vcpu)) + return tdx_vcpu_check_cpuid(vcpu, e2, nent); + + return 0; +} + static void vt_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) { if (is_td_vcpu(vcpu)) @@ -1103,6 +1112,7 @@ struct kvm_x86_ops vt_x86_ops __initdata =3D { =20 .get_exit_info =3D vt_get_exit_info, =20 + .vcpu_check_cpuid =3D vt_vcpu_check_cpuid, .vcpu_after_set_cpuid =3D vt_vcpu_after_set_cpuid, =20 .has_wbinvd_exit =3D cpu_has_vmx_wbinvd_exit, diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 8c0291535a78..8b58d91bda4e 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -514,6 +514,9 @@ void tdx_vm_free(struct kvm *kvm) =20 free_page((unsigned long)__va(kvm_tdx->tdr_pa)); kvm_tdx->tdr_pa =3D 0; + + kfree(kvm_tdx->cpuid); + kvm_tdx->cpuid =3D NULL; } =20 static int tdx_do_tdh_mng_key_config(void *param) @@ -635,6 +638,44 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) return 0; } =20 +int tdx_vcpu_check_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e= 2, int nent) +{ + struct kvm_tdx *kvm_tdx =3D to_kvm_tdx(vcpu->kvm); + const struct tdsysinfo_struct *tdsysinfo; + int i; + + tdsysinfo =3D tdx_get_sysinfo(); + if (!tdsysinfo) + return -EOPNOTSUPP; + + /* + * Simple check that new cpuid is consistent with created one. + * For simplicity, only trivial check. Don't try comprehensive checks + * with the cpuid virtualization table in the TDX module spec. + */ + for (i =3D 0; i < tdsysinfo->num_cpuid_config; i++) { + const struct tdx_cpuid_config *config =3D &tdsysinfo->cpuid_configs[i]; + u32 index =3D config->sub_leaf =3D=3D TDX_CPUID_NO_SUBLEAF ? 0 : config-= >sub_leaf; + const struct kvm_cpuid_entry2 *old =3D + kvm_find_cpuid_entry2(kvm_tdx->cpuid, kvm_tdx->cpuid_nent, + config->leaf, index); + const struct kvm_cpuid_entry2 *new =3D kvm_find_cpuid_entry2(e2, nent, + config->leaf, index); + + if (!!old !=3D !!new) + return -EINVAL; + if (!old && !new) + continue; + + if ((old->eax ^ new->eax) & config->eax || + (old->ebx ^ new->ebx) & config->ebx || + (old->ecx ^ new->ecx) & config->ecx || + (old->edx ^ new->edx) & config->edx) + return -EINVAL; + } + return 0; +} + void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { struct vcpu_tdx *tdx =3D to_tdx(vcpu); @@ -2196,10 +2237,12 @@ static int setup_tdparams_eptp_controls(struct kvm_= cpuid2 *cpuid, return 0; } =20 -static void setup_tdparams_cpuids(const struct tdsysinfo_struct *tdsysinfo, +static void setup_tdparams_cpuids(struct kvm *kvm, + const struct tdsysinfo_struct *tdsysinfo, struct kvm_cpuid2 *cpuid, struct td_params *td_params) { + struct kvm_tdx *kvm_tdx =3D to_kvm_tdx(kvm); int i; =20 /* @@ -2207,6 +2250,7 @@ static void setup_tdparams_cpuids(const struct tdsysi= nfo_struct *tdsysinfo, * be same to the one of struct tdsysinfo.{num_cpuid_config, cpuid_config= s} * It's assumed that td_params was zeroed. */ + kvm_tdx->cpuid_nent =3D 0; for (i =3D 0; i < tdsysinfo->num_cpuid_config; i++) { const struct tdx_cpuid_config *config =3D &tdsysinfo->cpuid_configs[i]; /* TDX_CPUID_NO_SUBLEAF in TDX CPUID_CONFIG means index =3D 0. */ @@ -2229,6 +2273,10 @@ static void setup_tdparams_cpuids(const struct tdsys= info_struct *tdsysinfo, value->ebx =3D entry->ebx & config->ebx; value->ecx =3D entry->ecx & config->ecx; value->edx =3D entry->edx & config->edx; + + /* Remember the setting to check for KVM_SET_CPUID2. */ + kvm_tdx->cpuid[kvm_tdx->cpuid_nent] =3D *entry; + kvm_tdx->cpuid_nent++; } } =20 @@ -2316,7 +2364,7 @@ static int setup_tdparams(struct kvm *kvm, struct td_= params *td_params, ret =3D setup_tdparams_eptp_controls(cpuid, td_params); if (ret) return ret; - setup_tdparams_cpuids(tdsysinfo, cpuid, td_params); + setup_tdparams_cpuids(kvm, tdsysinfo, cpuid, td_params); ret =3D setup_tdparams_xfam(cpuid, td_params); if (ret) return ret; @@ -2531,11 +2579,18 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_= tdx_cmd *cmd) if (cmd->flags) return -EINVAL; =20 - init_vm =3D kzalloc(sizeof(*init_vm) + - sizeof(init_vm->cpuid.entries[0]) * KVM_MAX_CPUID_ENTRIES, - GFP_KERNEL); - if (!init_vm) + WARN_ON_ONCE(kvm_tdx->cpuid); + kvm_tdx->cpuid =3D kzalloc(flex_array_size(init_vm, cpuid.entries, KVM_MA= X_CPUID_ENTRIES), + GFP_KERNEL); + if (!kvm_tdx->cpuid) return -ENOMEM; + + init_vm =3D kzalloc(struct_size(init_vm, cpuid.entries, KVM_MAX_CPUID_ENT= RIES), + GFP_KERNEL); + if (!init_vm) { + ret =3D -ENOMEM; + goto out; + } if (copy_from_user(init_vm, (void __user *)cmd->data, sizeof(*init_vm))) { ret =3D -EFAULT; goto out; @@ -2585,6 +2640,11 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_t= dx_cmd *cmd) =20 out: /* kfree() accepts NULL. */ + if (ret) { + kfree(kvm_tdx->cpuid); + kvm_tdx->cpuid =3D NULL; + kvm_tdx->cpuid_nent =3D 0; + } kfree(init_vm); kfree(td_params); return ret; diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h index 4ddcf804c0a4..54c3f6b83571 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -32,6 +32,13 @@ struct kvm_tdx { atomic_t tdh_mem_track; =20 u64 tsc_offset; + + /* + * For KVM_SET_CPUID to check consistency. Remember the one passed to + * TDH.MNG_INIT + */ + int cpuid_nent; + struct kvm_cpuid_entry2 *cpuid; }; =20 union tdx_exit_reason { diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h index b063424c916d..ae94bde4736d 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -162,6 +162,8 @@ u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bo= ol is_mmio); =20 void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode, int trig_mode, int vector); +int tdx_vcpu_check_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e= 2, + int nent); void tdx_inject_nmi(struct kvm_vcpu *vcpu); void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, u64 *info1, u64 *info2, u32 *intr_info, u32 *error_code); @@ -214,6 +216,8 @@ static inline u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu,= gfn_t gfn, bool is_mmio) =20 static inline void tdx_deliver_interrupt(struct kvm_lapic *apic, int deliv= ery_mode, int trig_mode, int vector) {} +static inline int tdx_vcpu_check_cpuid(struct kvm_vcpu *vcpu, struct kvm_c= puid_entry2 *e2, + int nent) { return -EOPNOTSUPP; } static inline void tdx_inject_nmi(struct kvm_vcpu *vcpu) {} static inline void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, u= 64 *info1, u64 *info2, u32 *intr_info, u32 *error_code) {} --=20 2.25.1