From nobody Mon Nov 25 04:57:03 2024 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCBCD21FD87; Wed, 30 Oct 2024 19:01:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730314877; cv=none; b=Ht7kHwQoTkkZH12GWo5gGmWHRovWXEPrHcusseBbLl8mzc09X/QplX/ZeN7HsWsuX93aNMAGIYlIi5fj7poBzlnZ3kSD2RdJveLGZPbEq2r3s5ZRDICjhYf9c4FDNPGiXjo2x/r557+cXUQsBLw/fqWJIuD5Ll9on68LfU4tM4A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730314877; c=relaxed/simple; bh=eulfXokD5/qZzy03Oms7b48P4y09C+Abv8jAdEYUHSk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V5z4DApHfyvxETruU+WbUQmL1+SPBwUYsNPlllS3Unk4SIXX75rp2RZOSLZr5P9vNJ9S6GoNbmMTeFqEwZozlk2NGDyyGC0cVibFm/EiPZBVd6ZyYMR7onEIFUDsAk8qdUnmshrf9W7zyvY100b3JbAs/n8IczoVJq8JkdMRH6E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=d6hyuXjZ; arc=none smtp.client-ip=192.198.163.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="d6hyuXjZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730314874; x=1761850874; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eulfXokD5/qZzy03Oms7b48P4y09C+Abv8jAdEYUHSk=; b=d6hyuXjZJbM8iO8nVGkLZ5Si0LGevBFPaLOXatw6nuvMk/KnvvSTGzYH YGEfZtj5FgZHfsuOW62p8hyFicQontCCLcC/kwqiFQg9Y8yrUi8VFn+D0 gzP7zyPD+7CY2KIzwpASdMrRFzpw20kjuRlO0tQ5Mmqo5bDUK3Qb9mea9 PnYodtnaDk0eqAk2J3AwoxFLc/FdfqNN+Jhdjk9fhkcjmOwgy5Y1RQL86 2WUEsF6FD8SEx73YiXVWLYisgLLi0Ph/ZxmLKTjKdnS2Nc7ZE2eJlGYiX CkXsmAdpktc8zVKNrUHw7/xDW9VU9swadd3EBCNTNxiz950ELRPFLO0Qg Q==; X-CSE-ConnectionGUID: R69U6LmiR/mb2l2mOR6seA== X-CSE-MsgGUID: K2MTfBKrR4qHHcCEubzatw== X-IronPort-AV: E=McAfee;i="6700,10204,11241"; a="17678821" X-IronPort-AV: E=Sophos;i="6.11,245,1725346800"; d="scan'208";a="17678821" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2024 12:01:05 -0700 X-CSE-ConnectionGUID: mpyYMRYmRZ6Nk1XKNE8sKA== X-CSE-MsgGUID: zs4cth0sSy+ItWgwmeRccA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,245,1725346800"; d="scan'208";a="82499437" Received: from sramkris-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4..) ([10.124.223.186]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2024 12:01:05 -0700 From: Rick Edgecombe To: pbonzini@redhat.com, seanjc@google.com Cc: rick.p.edgecombe@intel.com, yan.y.zhao@intel.com, isaku.yamahata@gmail.com, kai.huang@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, tony.lindgren@linux.intel.com, xiaoyao.li@intel.com, reinette.chatre@intel.com, Isaku Yamahata Subject: [PATCH v2 19/25] KVM: TDX: initialize VM with TDX specific parameters Date: Wed, 30 Oct 2024 12:00:32 -0700 Message-ID: <20241030190039.77971-20-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241030190039.77971-1-rick.p.edgecombe@intel.com> References: <20241030190039.77971-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Isaku Yamahata After the crypto-protection key has been configured, TDX requires a VM-scope initialization as a step of creating the TDX guest. This "per-VM" TDX initialization does the global configurations/features that the TDX guest can support, such as guest's CPUIDs (emulated by the TDX module), the maximum number of vcpus etc. This "per-VM" TDX initialization must be done before any "vcpu-scope" TDX initialization. To match this better, require the KVM_TDX_INIT_VM IOCTL() to be done before KVM creates any vcpus. Co-developed-by: Xiaoyao Li Signed-off-by: Xiaoyao Li Signed-off-by: Isaku Yamahata Signed-off-by: Rick Edgecombe --- uAPI breakout v2: - Move setting of gfn_direct_bits into MMU part 2 series (Yan) - Enable NO_RBP_MOD after removing workaround in later pactches (Fengwei) - Use TDX 1.5 naming of config_flags instead of exec_controls (Xiaoyao) - fixup SEAMCALL call sites due to function parameter changes to SEAMCALL wrappers (Kai) - Reject leaves that are not in tdx caps (Rick) - Add TD state handling (Tony) - Generate data directly from td_conf (Tony) - Fold in guest physical address to configure EPT level (Xiaoyao) - Use helpers for phys_addr_bits and add comments (Paolo) uAPI breakout v1: - Drop TDX_TD_XFAM_CET and use XFEATURE_MASK_CET_{USER, KERNEL}. - Update for the wrapper functions for SEAMCALLs. (Sean) - Move gfn_shared_mask settings into this patch due to MMU section move - Fix bisectability issues in headers (Kai) - Updates from seamcall overhaul (Kai) - Allow userspace configure xfam directly - Check if user sets non-configurable bits in CPUIDs - Rename error->hw_error - Move code change to tdx_module_setup() to __tdx_bringup() due to initializing is done in post hardware_setup() now and tdx_module_setup() is removed. Remove the code to use API to read global metadata but use exported 'struct tdx_sysinfo' pointer. - Replace 'tdx_info->nr_tdcs_pages' with a wrapper tdx_sysinfo_nr_tdcs_pages() because the 'struct tdx_sysinfo' doesn't have nr_tdcs_pages directly. - Replace tdx_info->max_vcpus_per_td with the new exported pointer in tdx_vm_init(). - Decrease the reserved space for struct kvm_tdx_init_vm (Kai) - Use sizeof_field() for struct kvm_tdx_init_vm cpuids (Tony) - No need to init init_vm, it gets copied over in tdx_td_init() (Chao) - Use kmalloc() instead of () kzalloc for init_vm in tdx_td_init() (Chao) - Add more line breaks to tdx_td_init() to make code easier to read (Tony) - Clarify patch description (Kai) v19: - Check NO_RBP_MOD of feature0 and set it - Update the comment for PT and CET v18: - remove the change of tools/arch/x86/include/uapi/asm/kvm.h - typo in comment. sha348 =3D> sha384 - updated comment in setup_tdparams_xfam() - fix setup_tdparams_xfam() to use init_vm instead of td_params --- arch/x86/include/uapi/asm/kvm.h | 24 +++ arch/x86/kvm/cpuid.c | 7 + arch/x86/kvm/cpuid.h | 2 + arch/x86/kvm/vmx/tdx.c | 259 ++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 24 +++ 5 files changed, 306 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kv= m.h index 0630530af334..892e16bd7430 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -929,6 +929,7 @@ struct kvm_hyperv_eventfd { /* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id { KVM_TDX_CAPABILITIES =3D 0, + KVM_TDX_INIT_VM, =20 KVM_TDX_CMD_NR_MAX, }; @@ -959,4 +960,27 @@ struct kvm_tdx_capabilities { struct kvm_cpuid2 cpuid; }; =20 +struct kvm_tdx_init_vm { + __u64 attributes; + __u64 xfam; + __u64 mrconfigid[6]; /* sha384 digest */ + __u64 mrowner[6]; /* sha384 digest */ + __u64 mrownerconfig[6]; /* sha384 digest */ + + /* The total space for TD_PARAMS before the CPUIDs is 256 bytes */ + __u64 reserved[12]; + + /* + * Call KVM_TDX_INIT_VM before vcpu creation, thus before + * KVM_SET_CPUID2. + * This configuration supersedes KVM_SET_CPUID2s for VCPUs because the + * TDX module directly virtualizes those CPUIDs without VMM. The user + * space VMM, e.g. qemu, should make KVM_SET_CPUID2 consistent with + * those values. If it doesn't, KVM may have wrong idea of vCPUIDs of + * the guest, and KVM may wrongly emulate CPUIDs or MSRs that the TDX + * module doesn't virtualize. + */ + struct kvm_cpuid2 cpuid; +}; + #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 41786b834b16..14be20e003f4 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -1513,6 +1513,13 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid, return r; } =20 +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2( + struct kvm_cpuid_entry2 *entries, int nent, u32 function, u64 index) +{ + return cpuid_entry2_find(entries, nent, function, index); +} +EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry2); + struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index(struct kvm_vcpu *vcpu, u32 function, u32 index) { diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h index 41697cca354e..00570227e2ae 100644 --- a/arch/x86/kvm/cpuid.h +++ b/arch/x86/kvm/cpuid.h @@ -13,6 +13,8 @@ void kvm_set_cpu_caps(void); =20 void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu); void kvm_update_pv_runtime(struct kvm_vcpu *vcpu); +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2(struct kvm_cpuid_entry2 *en= tries, + int nent, u32 function, u64 index); struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index(struct kvm_vcpu *vcpu, u32 function, u32 index); struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu, diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index c9093b003c13..ac224d79ba1e 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -5,6 +5,7 @@ #include "x86_ops.h" #include "tdx.h" =20 + #undef pr_fmt #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 @@ -63,6 +64,11 @@ static u64 tdx_get_supported_xfam(const struct tdx_sys_i= nfo_td_conf *td_conf) return val; } =20 +static int tdx_get_guest_phys_addr_bits(const u32 eax) +{ + return (eax & GENMASK(23, 16)) >> 16; +} + static u32 tdx_set_guest_phys_addr_bits(const u32 eax, int addr_bits) { return (eax & ~GENMASK(23, 16)) | (addr_bits & 0xff) << 16; @@ -360,7 +366,11 @@ static void tdx_reclaim_td_control_pages(struct kvm *k= vm) =20 void tdx_vm_free(struct kvm *kvm) { + struct kvm_tdx *kvm_tdx =3D to_kvm_tdx(kvm); + tdx_reclaim_td_control_pages(kvm); + + kvm_tdx->state =3D TD_STATE_UNINITIALIZED; } =20 static int tdx_do_tdh_mng_key_config(void *param) @@ -379,10 +389,10 @@ static int tdx_do_tdh_mng_key_config(void *param) return 0; } =20 -static int __tdx_td_init(struct kvm *kvm); - int tdx_vm_init(struct kvm *kvm) { + struct kvm_tdx *kvm_tdx =3D to_kvm_tdx(kvm); + kvm->arch.has_private_mem =3D true; =20 /* @@ -398,8 +408,9 @@ int tdx_vm_init(struct kvm *kvm) */ kvm->max_vcpus =3D min_t(int, kvm->max_vcpus, num_present_cpus()); =20 - /* Place holder for TDX specific logic. */ - return __tdx_td_init(kvm); + kvm_tdx->state =3D TD_STATE_UNINITIALIZED; + + return 0; } =20 static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd) @@ -450,7 +461,142 @@ static int tdx_get_capabilities(struct kvm_tdx_cmd *c= md) return ret; } =20 -static int __tdx_td_init(struct kvm *kvm) +/* + * KVM reports guest physical address in CPUID.0x800000008.EAX[23:16], whi= ch is + * similar to TDX's GPAW. Use this field as the interface for userspace to + * configure the GPAW and EPT level for TDs. + * + * Only values 48 and 52 are supported. Value 52 means GPAW-52 and EPT lev= el + * 5, Value 48 means GPAW-48 and EPT level 4. For value 48, GPAW-48 is alw= ays + * supported. Value 52 is only supported when the platform supports 5 level + * EPT. + */ +static int setup_tdparams_eptp_controls(struct kvm_cpuid2 *cpuid, + struct td_params *td_params) +{ + const struct kvm_cpuid_entry2 *entry; + int guest_pa; + + entry =3D kvm_find_cpuid_entry2(cpuid->entries, cpuid->nent, 0x80000008, = 0); + if (!entry) + return -EINVAL; + + guest_pa =3D tdx_get_guest_phys_addr_bits(entry->eax); + + if (guest_pa !=3D 48 && guest_pa !=3D 52) + return -EINVAL; + + if (guest_pa =3D=3D 52 && !cpu_has_vmx_ept_5levels()) + return -EINVAL; + + td_params->eptp_controls =3D VMX_EPTP_MT_WB; + if (guest_pa =3D=3D 52) { + td_params->eptp_controls |=3D VMX_EPTP_PWL_5; + td_params->config_flags |=3D TDX_CONFIG_FLAGS_MAX_GPAW; + } else { + td_params->eptp_controls |=3D VMX_EPTP_PWL_4; + } + + return 0; +} + +static int setup_tdparams_cpuids(struct kvm_cpuid2 *cpuid, + struct td_params *td_params) +{ + const struct tdx_sys_info_td_conf *td_conf =3D &tdx_sysinfo->td_conf; + const struct kvm_cpuid_entry2 *entry; + struct tdx_cpuid_value *value; + int i, copy_cnt =3D 0; + + /* + * td_params.cpuid_values: The number and the order of cpuid_value must + * be same to the one of struct tdsysinfo.{num_cpuid_config, cpuid_config= s} + * It's assumed that td_params was zeroed. + */ + for (i =3D 0; i < td_conf->num_cpuid_config; i++) { + struct kvm_cpuid_entry2 tmp; + + td_init_cpuid_entry2(&tmp, i); + + entry =3D kvm_find_cpuid_entry2(cpuid->entries, cpuid->nent, + tmp.function, tmp.index); + if (!entry) + continue; + + copy_cnt++; + + value =3D &td_params->cpuid_values[i]; + value->eax =3D entry->eax; + value->ebx =3D entry->ebx; + value->ecx =3D entry->ecx; + value->edx =3D entry->edx; + + /* + * TDX module does not accept nonzero bits 16..23 for the + * CPUID[0x80000008].EAX, see setup_tdparams_eptp_controls(). + */ + if (tmp.function =3D=3D 0x80000008) + value->eax =3D tdx_set_guest_phys_addr_bits(value->eax, 0); + } + + /* + * Rely on the TDX module to reject invalid configuration, but it can't + * check of leafs that don't have a proper slot in td_params->cpuid_values + * to stick then. So fail if there were entries that didn't get copied to + * td_params. + */ + if (copy_cnt !=3D cpuid->nent) + return -EINVAL; + + return 0; +} + +static int setup_tdparams(struct kvm *kvm, struct td_params *td_params, + struct kvm_tdx_init_vm *init_vm) +{ + const struct tdx_sys_info_td_conf *td_conf =3D &tdx_sysinfo->td_conf; + struct kvm_cpuid2 *cpuid =3D &init_vm->cpuid; + int ret; + + if (kvm->created_vcpus) + return -EBUSY; + + if (init_vm->attributes & ~tdx_get_supported_attrs(td_conf)) + return -EINVAL; + + if (init_vm->xfam & ~tdx_get_supported_xfam(td_conf)) + return -EINVAL; + + td_params->max_vcpus =3D kvm->max_vcpus; + td_params->attributes =3D init_vm->attributes | td_conf->attributes_fixed= 1; + td_params->xfam =3D init_vm->xfam | td_conf->xfam_fixed1; + + td_params->config_flags =3D TDX_CONFIG_FLAGS_NO_RBP_MOD; + td_params->tsc_frequency =3D TDX_TSC_KHZ_TO_25MHZ(kvm->arch.default_tsc_k= hz); + + ret =3D setup_tdparams_eptp_controls(cpuid, td_params); + if (ret) + return ret; + + ret =3D setup_tdparams_cpuids(cpuid, td_params); + if (ret) + return ret; + +#define MEMCPY_SAME_SIZE(dst, src) \ + do { \ + BUILD_BUG_ON(sizeof(dst) !=3D sizeof(src)); \ + memcpy((dst), (src), sizeof(dst)); \ + } while (0) + + MEMCPY_SAME_SIZE(td_params->mrconfigid, init_vm->mrconfigid); + MEMCPY_SAME_SIZE(td_params->mrowner, init_vm->mrowner); + MEMCPY_SAME_SIZE(td_params->mrownerconfig, init_vm->mrownerconfig); + + return 0; +} + +static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params, + u64 *seamcall_err) { struct kvm_tdx *kvm_tdx =3D to_kvm_tdx(kvm); cpumask_var_t packages; @@ -458,8 +604,9 @@ static int __tdx_td_init(struct kvm *kvm) unsigned long tdr_pa =3D 0; unsigned long va; int ret, i; - u64 err; + u64 err, rcx; =20 + *seamcall_err =3D 0; ret =3D tdx_guest_keyid_alloc(); if (ret < 0) return ret; @@ -573,10 +720,23 @@ static int __tdx_td_init(struct kvm *kvm) } } =20 - /* - * Note, TDH_MNG_INIT cannot be invoked here. TDH_MNG_INIT requires a de= dicated - * ioctl() to define the configure CPUID values for the TD. - */ + err =3D tdh_mng_init(kvm_tdx->tdr_pa, __pa(td_params), &rcx); + if ((err & TDX_SEAMCALL_STATUS_MASK) =3D=3D TDX_OPERAND_INVALID) { + /* + * Because a user gives operands, don't warn. + * Return a hint to the user because it's sometimes hard for the + * user to figure out which operand is invalid. SEAMCALL status + * code includes which operand caused invalid operand error. + */ + *seamcall_err =3D err; + ret =3D -EINVAL; + goto teardown; + } else if (WARN_ON_ONCE(err)) { + pr_tdx_error_1(TDH_MNG_INIT, err, rcx); + ret =3D -EIO; + goto teardown; + } + return 0; =20 /* @@ -624,6 +784,82 @@ static int __tdx_td_init(struct kvm *kvm) return ret; } =20 +static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd) +{ + struct kvm_tdx *kvm_tdx =3D to_kvm_tdx(kvm); + struct kvm_tdx_init_vm *init_vm; + struct td_params *td_params =3D NULL; + int ret; + + BUILD_BUG_ON(sizeof(*init_vm) !=3D 256 + sizeof_field(struct kvm_tdx_init= _vm, cpuid)); + BUILD_BUG_ON(sizeof(struct td_params) !=3D 1024); + + if (kvm_tdx->state !=3D TD_STATE_UNINITIALIZED) + return -EINVAL; + + if (cmd->flags) + return -EINVAL; + + init_vm =3D kmalloc(sizeof(*init_vm) + + sizeof(init_vm->cpuid.entries[0]) * KVM_MAX_CPUID_ENTRIES, + GFP_KERNEL); + if (!init_vm) + return -ENOMEM; + + if (copy_from_user(init_vm, u64_to_user_ptr(cmd->data), sizeof(*init_vm))= ) { + ret =3D -EFAULT; + goto out; + } + + if (init_vm->cpuid.nent > KVM_MAX_CPUID_ENTRIES) { + ret =3D -E2BIG; + goto out; + } + + if (copy_from_user(init_vm->cpuid.entries, + u64_to_user_ptr(cmd->data) + sizeof(*init_vm), + flex_array_size(init_vm, cpuid.entries, init_vm->cpuid.nent))) { + ret =3D -EFAULT; + goto out; + } + + if (memchr_inv(init_vm->reserved, 0, sizeof(init_vm->reserved))) { + ret =3D -EINVAL; + goto out; + } + + if (init_vm->cpuid.padding) { + ret =3D -EINVAL; + goto out; + } + + td_params =3D kzalloc(sizeof(struct td_params), GFP_KERNEL); + if (!td_params) { + ret =3D -ENOMEM; + goto out; + } + + ret =3D setup_tdparams(kvm, td_params, init_vm); + if (ret) + goto out; + + ret =3D __tdx_td_init(kvm, td_params, &cmd->hw_error); + if (ret) + goto out; + + kvm_tdx->tsc_offset =3D td_tdcs_exec_read64(kvm_tdx, TD_TDCS_EXEC_TSC_OFF= SET); + kvm_tdx->attributes =3D td_params->attributes; + kvm_tdx->xfam =3D td_params->xfam; + + kvm_tdx->state =3D TD_STATE_INITIALIZED; +out: + /* kfree() accepts NULL. */ + kfree(init_vm); + kfree(td_params); + + return ret; +} + int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_tdx_cmd tdx_cmd; @@ -645,6 +881,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) case KVM_TDX_CAPABILITIES: r =3D tdx_get_capabilities(&tdx_cmd); break; + case KVM_TDX_INIT_VM: + r =3D tdx_td_init(kvm, &tdx_cmd); + break; default: r =3D -EINVAL; goto out; diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h index e557a82bc882..1fcb7c1b078d 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -10,14 +10,27 @@ void tdx_cleanup(void); =20 extern bool enable_tdx; =20 +/* TDX module hardware states. These follow the TDX module OP_STATEs. */ +enum kvm_tdx_state { + TD_STATE_UNINITIALIZED =3D 0, + TD_STATE_INITIALIZED, + TD_STATE_RUNNABLE, +}; + struct kvm_tdx { struct kvm kvm; =20 unsigned long tdr_pa; unsigned long *tdcs_pa; =20 + u64 attributes; + u64 xfam; int hkid; u8 nr_tdcs_pages; + + u64 tsc_offset; + + enum kvm_tdx_state state; }; =20 struct vcpu_tdx { @@ -45,6 +58,17 @@ static __always_inline struct vcpu_tdx *to_tdx(struct kv= m_vcpu *vcpu) return container_of(vcpu, struct vcpu_tdx, vcpu); } =20 +static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u3= 2 field) +{ + u64 err, data; + + err =3D tdh_mng_rd(kvm_tdx->tdr_pa, TDCS_EXEC(field), &data); + if (unlikely(err)) { + pr_err("TDH_MNG_RD[EXEC.0x%x] failed: 0x%llx\n", field, err); + return 0; + } + return data; +} #else static inline void tdx_bringup(void) {} static inline void tdx_cleanup(void) {} --=20 2.47.0