From nobody Sun Sep 7 12:21:37 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 224F6CDB465 for ; Mon, 16 Oct 2023 16:31:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234079AbjJPQbO (ORCPT ); Mon, 16 Oct 2023 12:31:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232345AbjJPQaz (ORCPT ); Mon, 16 Oct 2023 12:30:55 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09FAE6EB7; Mon, 16 Oct 2023 09:22:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473333; x=1729009333; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=N4eRJibuYOThT14apMCxsBiRf7iBG8VzekPRdjglBbA=; b=NyFV70oCW/iIxZXY1gguT8ZHs0DJRjHr3iBmkHXo/UIICT2ZLT5myv/O tPXXrAuU3uffb23+PyHzKheOvokAkzKL5685EGi0YwsPNZRsj13wbH5Ys UxQiqxsVMaZjIMf+ko9W0doSKzakYc22YFuScj45BI6FMigNCiec6U5bT GHJ7p0f48UxVPGA1MLqPqXH3Mc05aQP1nGwvV19am07Sr2mAKJvYyDWxZ w0E4RTVXxhXff6Cjc+Gbq14XnMQIjtSxxcf9HqoGUKNkxEnJu5rFqeWIX 4Um/e2QF8NQaTIp19ni7EUN0ig5IPVaE4omLS6dv1RgxxSdq8/2NmWDyy A==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="365825947" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="365825947" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="1087126035" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="1087126035" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:28 -0700 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, Sean Christopherson Subject: [PATCH v16 018/116] KVM: TDX: x86: Add ioctl to get TDX systemwide parameters Date: Mon, 16 Oct 2023 09:13:30 -0700 Message-Id: <3e2abc9abe194057e00bbea4320c94aa03b0ac0c.1697471314.git.isaku.yamahata@intel.com> 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: Sean Christopherson Implement an ioctl to get system-wide parameters for TDX. Although the function is systemwide, vm scoped mem_enc ioctl works for userspace VMM like qemu and device scoped version is not define, re-use vm scoped mem_enc. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- v14 -> v15: - ABI change: added supported_gpaw and reserved area, --- arch/x86/include/uapi/asm/kvm.h | 24 ++++++++++ arch/x86/kvm/vmx/tdx.c | 64 +++++++++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 52 ++++++++++++++++++++++ 3 files changed, 140 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kv= m.h index 615fb60b3717..3fbd43d5177b 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -593,4 +593,28 @@ struct kvm_tdx_cmd { __u64 error; }; =20 +struct kvm_tdx_cpuid_config { + __u32 leaf; + __u32 sub_leaf; + __u32 eax; + __u32 ebx; + __u32 ecx; + __u32 edx; +}; + +struct kvm_tdx_capabilities { + __u64 attrs_fixed0; + __u64 attrs_fixed1; + __u64 xfam_fixed0; + __u64 xfam_fixed1; +#define TDX_CAP_GPAW_48 (1 << 0) +#define TDX_CAP_GPAW_52 (1 << 1) + __u32 supported_gpaw; + __u32 padding; + __u64 reserved[251]; + + __u32 nr_cpuid_configs; + struct kvm_tdx_cpuid_config cpuid_configs[]; +}; + #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index ead229e34813..f9e80582865d 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -6,6 +6,7 @@ #include "capabilities.h" #include "x86_ops.h" #include "x86.h" +#include "mmu.h" #include "tdx.h" =20 #undef pr_fmt @@ -16,6 +17,66 @@ offsetof(struct tdsysinfo_struct, cpuid_configs)) \ / sizeof(struct tdx_cpuid_config)) =20 +static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd) +{ + struct kvm_tdx_capabilities __user *user_caps; + const struct tdsysinfo_struct *tdsysinfo; + struct kvm_tdx_capabilities *caps =3D NULL; + int ret =3D 0; + + BUILD_BUG_ON(sizeof(struct kvm_tdx_cpuid_config) !=3D + sizeof(struct tdx_cpuid_config)); + + if (cmd->flags) + return -EINVAL; + + tdsysinfo =3D tdx_get_sysinfo(); + if (!tdsysinfo) + return -EOPNOTSUPP; + + caps =3D kmalloc(sizeof(*caps), GFP_KERNEL); + if (!caps) + return -ENOMEM; + + user_caps =3D (void __user *)cmd->data; + if (copy_from_user(caps, user_caps, sizeof(*caps))) { + ret =3D -EFAULT; + goto out; + } + + if (caps->nr_cpuid_configs < tdsysinfo->num_cpuid_config) { + ret =3D -E2BIG; + goto out; + } + + *caps =3D (struct kvm_tdx_capabilities) { + .attrs_fixed0 =3D tdsysinfo->attributes_fixed0, + .attrs_fixed1 =3D tdsysinfo->attributes_fixed1, + .xfam_fixed0 =3D tdsysinfo->xfam_fixed0, + .xfam_fixed1 =3D tdsysinfo->xfam_fixed1, + .supported_gpaw =3D TDX_CAP_GPAW_48 | + (kvm_get_shadow_phys_bits() >=3D 52 && + cpu_has_vmx_ept_5levels()) ? TDX_CAP_GPAW_52 : 0, + .nr_cpuid_configs =3D tdsysinfo->num_cpuid_config, + .padding =3D 0, + }; + + if (copy_to_user(user_caps, caps, sizeof(*caps))) { + ret =3D -EFAULT; + goto out; + } + if (copy_to_user(user_caps->cpuid_configs, &tdsysinfo->cpuid_configs, + tdsysinfo->num_cpuid_config * + sizeof(struct tdx_cpuid_config))) { + ret =3D -EFAULT; + } + +out: + /* kfree() accepts NULL. */ + kfree(caps); + return ret; +} + int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_tdx_cmd tdx_cmd; @@ -29,6 +90,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) mutex_lock(&kvm->lock); =20 switch (tdx_cmd.id) { + case KVM_TDX_CAPABILITIES: + r =3D tdx_get_capabilities(&tdx_cmd); + break; default: r =3D -EINVAL; goto out; diff --git a/tools/arch/x86/include/uapi/asm/kvm.h b/tools/arch/x86/include= /uapi/asm/kvm.h index 1a6a1f987949..7a08723e99e2 100644 --- a/tools/arch/x86/include/uapi/asm/kvm.h +++ b/tools/arch/x86/include/uapi/asm/kvm.h @@ -562,4 +562,56 @@ struct kvm_pmu_event_filter { /* x86-specific KVM_EXIT_HYPERCALL flags. */ #define KVM_EXIT_HYPERCALL_LONG_MODE BIT(0) =20 +/* Trust Domain eXtension sub-ioctl() commands. */ +enum kvm_tdx_cmd_id { + KVM_TDX_CAPABILITIES =3D 0, + + KVM_TDX_CMD_NR_MAX, +}; + +struct kvm_tdx_cmd { + /* enum kvm_tdx_cmd_id */ + __u32 id; + /* flags for sub-commend. If sub-command doesn't use this, set zero. */ + __u32 flags; + /* + * data for each sub-command. An immediate or a pointer to the actual + * data in process virtual address. If sub-command doesn't use it, + * set zero. + */ + __u64 data; + /* + * Auxiliary error code. The sub-command may return TDX SEAMCALL + * status code in addition to -Exxx. + * Defined for consistency with struct kvm_sev_cmd. + */ + __u64 error; + /* Reserved: Defined for consistency with struct kvm_sev_cmd. */ + __u64 unused; +}; + +struct kvm_tdx_cpuid_config { + __u32 leaf; + __u32 sub_leaf; + __u32 eax; + __u32 ebx; + __u32 ecx; + __u32 edx; +}; + +struct kvm_tdx_capabilities { + __u64 attrs_fixed0; + __u64 attrs_fixed1; + __u64 xfam_fixed0; + __u64 xfam_fixed1; +#define TDX_CAP_GPAW_48 (1 << 0) +#define TDX_CAP_GPAW_52 (1 << 1) + __u32 supported_gpaw; + __u32 padding; + __u64 reserved[251]; + + __u32 nr_cpuid_configs; + struct kvm_tdx_cpuid_config cpuid_configs[]; +}; + #endif /* _ASM_X86_KVM_H */ --=20 2.25.1