From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) (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 BC0E840B0ED for ; Thu, 23 Jul 2026 08:44:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.20 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784796260; cv=none; b=VRSZVY9ZphVaWMikt/20bkdbFig/TQNeGM7AIkWmmg8GMB83yxo3qSpZ5lsnAoTrQqLCfELYDZ1SSvKOo5hBs8FeNcTS+cr882IJiTFLiUrncc5GznMwvyZnt8wzdr0FjGPiF3Azw+9yyzw0uPVDHXL/yRlQjlxolsmPiyJbql8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784796260; c=relaxed/simple; bh=ri2R10jI/KZqksjq56uANWgNDteZi/WxIAXPivMhEeI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=YrsA+wlY2Qv7m/FBV/sbopbfwCfth6Vh7bvZTh6pUh6fG+xI5e898f1+hiC7ylHwUgZ+F+BZI7+Incg9uekA6qOYoy8s0A5EPVu/0gRyWJuLYE6u7iSX/fclylUhGVgk5PLl15mxD1F/knfoc/R5I0STfyywz0XtcJVE4YxVXD4= 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=nLojhgQv; arc=none smtp.client-ip=198.175.65.20 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="nLojhgQv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1784796257; x=1816332257; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=ri2R10jI/KZqksjq56uANWgNDteZi/WxIAXPivMhEeI=; b=nLojhgQvQq2djj8CHkWxubVNPcHJcCGTN+Fqo11cf9gJ0UAd0pxxWlf8 1IdAq24GPmzAtamyHK8M32CBiIaRSc3JQXd3K4W++8gbpGRHDAUq86+D8 x7rovCMUvGNck5cFDJuFn9o1Uu4xmym6hglNBL1sgC9a0c62ntJqFg7No J/dnVoqtkKhzIwNhXmQxlNTEQq/ymkcPOZTL/C9VvnOuusLRQrHfR1aLi 3AKAVzh6TvltxIkEcs/pQF9L5W6EVnKkv5psRFY/SE8MMwJhZHHYnSqnh FCDqGef9Phs654RSQ6esEZBzegkNSIH90WpqIoUrpiO3n4QfxH57BlKH1 g==; X-CSE-ConnectionGUID: r1vlSzFFSvm+jL3hmTt69A== X-CSE-MsgGUID: FEO8Jv7+TU2uPwWYTPjM5g== X-IronPort-AV: E=McAfee;i="6800,10657,11854"; a="85206207" X-IronPort-AV: E=Sophos;i="6.25,180,1779174000"; d="scan'208";a="85206207" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jul 2026 01:44:16 -0700 X-CSE-ConnectionGUID: Fgaspw/+Sm6TCmIKzFvJuA== X-CSE-MsgGUID: G+r80SPPRl+y3k/WeL64WQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,180,1779174000"; d="scan'208";a="253996784" Received: from unknown (HELO [10.238.208.132]) ([10.238.208.132]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jul 2026 01:44:10 -0700 Message-ID: Date: Thu, 23 Jul 2026 16:44:08 +0800 Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v14 03/22] KVM: selftests: Initialize the TDX VM To: Lisa Wang , Andrew Jones , Ackerley Tng , Binbin Wu , Chao Gao , Chenyi Qiang , Dave Hansen , Erdem Aktas , Kiryl Shutsemau , linux-kselftest@vger.kernel.org, Paolo Bonzini , "Pratik R. Sampat" , Reinette Chatre , Rick Edgecombe , Roger Wang , Ryan Afranji , Sagi Shahar , Sean Christopherson , Shuah Khan , Oliver Upton Cc: Jeremiah McReynolds , kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, x86@kernel.org References: <20260722-tdx-selftests-v14-0-15ad654a50db@google.com> <20260722-tdx-selftests-v14-3-15ad654a50db@google.com> Content-Language: en-US From: Xiaoyao Li In-Reply-To: <20260722-tdx-selftests-v14-3-15ad654a50db@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 7/23/2026 7:13 AM, Lisa Wang wrote: > From: Sagi Shahar > > Add tdx_init_vm() to handle the mandatory VM-level initialization > sequence required for Intel TDX. > > For TDX, the guest's CPUID configuration must be "sealed" during > KVM_TDX_INIT_VM before any vCPUs are created. This is necessary because > the TDX hardware directly virtualizes CPUID and includes the > configuration in the guest's initial security measurement. > > The helper calculates the required CPUID values by filtering the host- > supported bits (kvm_get_supported_cpuid) against the "directly > configurable" bits reported by KVM_TDX_CAPABILITIES, ensuring > compliance with the strict requirements of the TDH.MNG.INIT SEAMCALL. > +/* > + * TDX ioctls > + * Use underscores to avoid collisions with struct member names. > + */ > +#define __tdx_vm_ioctl(vm, cmd, _flags, arg) \ sev uses the name __vm_sev_ioctl, I think we need to keep them consistent. > +({ \ > + u64 r; \ > + \ > + union { \ > + struct kvm_tdx_cmd c; \ > + unsigned long raw; \ > + } tdx_cmd = { .c = { \ > + .id = (cmd), \ > + .flags = (u32)(_flags), \ > + .data = (u64)(arg), \ > + } }; \ > + \ > + r = __vm_ioctl(vm, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd.raw); \ > + r ?: tdx_cmd.c.hw_error; \ I know it takes the same handling from __vm_sev_ioctl(). But I think the handling for hw_error is not correct, at least for TDX (I didn't check for SEV). the hw_error is the additional info, to tell the SEAMCALL return code, when the IOCTL fails. KVM requires hw_error to be in the input, and KVM puts the SEAMCALL return code into hw_error when the IOCTL fails due to SEAMCALL failure. That means, when r == 0, the hw_error is always 0. I think we need to provide hw_error along with r to the caller so that caller can print them together. > +}) > + > +#define tdx_vm_ioctl(vm, cmd, flags, arg) \ > +({ \ > + u64 ret = __tdx_vm_ioctl(vm, cmd, flags, arg); \ > + \ > + if (ret) { \ > + TEST_ASSERT(!ret, \ > + "%s failed, rc: 0x%llx errno: %i (%s)", \ > + #cmd, (unsigned long long)ret, \ > + errno, strerror(errno)); \ The if() looks silly. Why add it? And why change it from __TEST_ASSERT_VM_VCPU_IOCTL() in the v13? Considering the suggestion of hw_error above, I think we need to introduce the TEST_ASSERT_TDX_VM_VCPU_IOCTL() which accepts additional hw_error? > diff --git a/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c > new file mode 100644 > index 000000000000..e1ffb67a106c > --- /dev/null > +++ b/tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c > @@ -0,0 +1,120 @@ > +// SPDX-License-Identifier: GPL-2.0-only > + > +#include "processor.h" > +#include "tdx/tdx_util.h" > + > +static struct kvm_tdx_capabilities *tdx_read_capabilities(struct kvm_vm *vm) make it const, is better. > + > +void tdx_init_vm(struct kvm_vm *vm, u64 attributes) > +{ > + struct kvm_tdx_init_vm *init_vm; > + const struct kvm_cpuid2 *tmp; > + struct kvm_cpuid2 *cpuid; > + > + tmp = kvm_get_supported_cpuid(); > + > + cpuid = allocate_kvm_cpuid2(tmp->nent); > + memcpy(cpuid, tmp, kvm_cpuid2_size(tmp->nent)); > + tdx_filter_cpuid(vm, cpuid); > + > + init_vm = calloc(1, sizeof(*init_vm) + > + sizeof(init_vm->cpuid.entries[0]) * cpuid->nent); > + TEST_ASSERT(init_vm, "init_vm allocation failed"); > + > + memcpy(&init_vm->cpuid, cpuid, kvm_cpuid2_size(cpuid->nent)); > + free(cpuid); > + > + init_vm->attributes = attributes; Besides CPUID, it only allows attributes to be configure but leave XFAM as 0. I think the changelog needs to explain why we need to configure attributes. The rest of the patch looks good to me. > + > + tdx_vm_ioctl(vm, KVM_TDX_INIT_VM, 0, init_vm); > + > + free(init_vm); > +} >