From mboxrd@z Thu Jan 1 00:00:00 1970 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1D514C4167B for ; Thu, 7 Dec 2023 07:18:29 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rB8db-0006I4-B1; Thu, 07 Dec 2023 02:17:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rB8dY-0006HC-2I for qemu-devel@nongnu.org; Thu, 07 Dec 2023 02:17:32 -0500 Received: from mgamail.intel.com ([192.55.52.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rB8dU-0002iD-OH for qemu-devel@nongnu.org; Thu, 07 Dec 2023 02:17:31 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701933448; x=1733469448; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=BgCZPpQ50gmd2BMmvAZSRbHcwf16Fy5t1weLfxDv2Hs=; b=cRVb4AuAuOh7ZEfN+i0OfADr+DNL5lAh8wC+DFFmylO38Fznyx0/W/Yy Qye8vSR3Lalbj0iY9gtlLx+izBLrZhC1Z5a4YmZyY1QktWD3toTqYU4Vu /aVBH2i172Ta9RgK7OnUtDsAkQGZr4t/AKg8djF5ncAlVp1q3ypHzXkZV WlxGcZE8wRZRO/D2kCeriSJwvEyWZgwO4uukIBwBuaDNGj1+Pc2PsWT83 K31S9QnhcNCUAodSS80tI22a+Gld9Axs/UuQYyXljTmin5QJJjG/yZlSM fkL1drL4KitKJ8d2uI2/dHLjWQYp/fVJQxXinyPRpp2WULSCj5VK6Bikj Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10916"; a="480378939" X-IronPort-AV: E=Sophos;i="6.04,256,1695711600"; d="scan'208";a="480378939" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Dec 2023 23:17:14 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10916"; a="944933517" X-IronPort-AV: E=Sophos;i="6.04,256,1695711600"; d="scan'208";a="944933517" Received: from xiaoyaol-hp-g830.ccr.corp.intel.com (HELO [10.93.29.154]) ([10.93.29.154]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Dec 2023 23:17:05 -0800 Message-ID: Date: Thu, 7 Dec 2023 15:16:59 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 18/70] i386/tdx: Get tdx_capabilities via KVM_TDX_CAPABILITIES Content-Language: en-US To: Isaku Yamahata Cc: Paolo Bonzini , David Hildenbrand , Igor Mammedov , "Michael S . Tsirkin" , Marcel Apfelbaum , Richard Henderson , Peter Xu , =?UTF-8?Q?Philippe_Mathieu-Daud=C3=A9?= , Cornelia Huck , =?UTF-8?Q?Daniel_P=2E_Berrang=C3=A9?= , Eric Blake , Markus Armbruster , Marcelo Tosatti , qemu-devel@nongnu.org, kvm@vger.kernel.org, Michael Roth , Sean Christopherson , Claudio Fontana , Gerd Hoffmann , Isaku Yamahata , Chenyi Qiang , isaku.yamahata@intel.com References: <20231115071519.2864957-1-xiaoyao.li@intel.com> <20231115071519.2864957-19-xiaoyao.li@intel.com> <20231117211843.GA1648821@ls.amr.corp.intel.com> From: Xiaoyao Li In-Reply-To: <20231117211843.GA1648821@ls.amr.corp.intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=192.55.52.43; envelope-from=xiaoyao.li@intel.com; helo=mgamail.intel.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HK_RANDOM_ENVFROM=0.999, HK_RANDOM_FROM=0.999, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org On 11/18/2023 5:18 AM, Isaku Yamahata wrote: > On Wed, Nov 15, 2023 at 02:14:27AM -0500, > Xiaoyao Li wrote: > >> KVM provides TDX capabilities via sub command KVM_TDX_CAPABILITIES of >> IOCTL(KVM_MEMORY_ENCRYPT_OP). Get the capabilities when initializing >> TDX context. It will be used to validate user's setting later. >> >> Since there is no interface reporting how many cpuid configs contains in >> KVM_TDX_CAPABILITIES, QEMU chooses to try starting with a known number >> and abort when it exceeds KVM_MAX_CPUID_ENTRIES. >> >> Besides, introduce the interfaces to invoke TDX "ioctls" at different >> scope (KVM, VM and VCPU) in preparation. >> >> Signed-off-by: Xiaoyao Li >> --- >> Changes in v3: >> - rename __tdx_ioctl() to tdx_ioctl_internal() >> - Pass errp in get_tdx_capabilities(); >> >> changes in v2: >> - Make the error message more clear; >> >> changes in v1: >> - start from nr_cpuid_configs = 6 for the loop; >> - stop the loop when nr_cpuid_configs exceeds KVM_MAX_CPUID_ENTRIES; >> --- >> target/i386/kvm/kvm.c | 2 - >> target/i386/kvm/kvm_i386.h | 2 + >> target/i386/kvm/tdx.c | 102 ++++++++++++++++++++++++++++++++++++- >> 3 files changed, 103 insertions(+), 3 deletions(-) >> >> diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c >> index 7abcdebb1452..28e60c5ea4a7 100644 >> --- a/target/i386/kvm/kvm.c >> +++ b/target/i386/kvm/kvm.c >> @@ -1687,8 +1687,6 @@ static int hyperv_init_vcpu(X86CPU *cpu) >> >> static Error *invtsc_mig_blocker; >> >> -#define KVM_MAX_CPUID_ENTRIES 100 >> - >> static void kvm_init_xsave(CPUX86State *env) >> { >> if (has_xsave2) { >> diff --git a/target/i386/kvm/kvm_i386.h b/target/i386/kvm/kvm_i386.h >> index 55fb25fa8e2e..c3ef46a97a7b 100644 >> --- a/target/i386/kvm/kvm_i386.h >> +++ b/target/i386/kvm/kvm_i386.h >> @@ -13,6 +13,8 @@ >> >> #include "sysemu/kvm.h" >> >> +#define KVM_MAX_CPUID_ENTRIES 100 >> + >> #ifdef CONFIG_KVM >> >> #define kvm_pit_in_kernel() \ >> diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c >> index 621a05beeb4e..cb0040187b27 100644 >> --- a/target/i386/kvm/tdx.c >> +++ b/target/i386/kvm/tdx.c >> @@ -12,17 +12,117 @@ >> */ >> >> #include "qemu/osdep.h" >> +#include "qemu/error-report.h" >> #include "qapi/error.h" >> #include "qom/object_interfaces.h" >> +#include "sysemu/kvm.h" >> >> #include "hw/i386/x86.h" >> +#include "kvm_i386.h" >> #include "tdx.h" >> >> +static struct kvm_tdx_capabilities *tdx_caps; >> + >> +enum tdx_ioctl_level{ >> + TDX_PLATFORM_IOCTL, >> + TDX_VM_IOCTL, >> + TDX_VCPU_IOCTL, >> +}; >> + >> +static int tdx_ioctl_internal(void *state, enum tdx_ioctl_level level, int cmd_id, >> + __u32 flags, void *data) >> +{ >> + struct kvm_tdx_cmd tdx_cmd; >> + int r; >> + >> + memset(&tdx_cmd, 0x0, sizeof(tdx_cmd)); >> + >> + tdx_cmd.id = cmd_id; >> + tdx_cmd.flags = flags; >> + tdx_cmd.data = (__u64)(unsigned long)data; >> + >> + switch (level) { >> + case TDX_PLATFORM_IOCTL: >> + r = kvm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); >> + break; >> + case TDX_VM_IOCTL: >> + r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); >> + break; >> + case TDX_VCPU_IOCTL: >> + r = kvm_vcpu_ioctl(state, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); >> + break; >> + default: >> + error_report("Invalid tdx_ioctl_level %d", level); >> + exit(1); >> + } >> + >> + return r; >> +} >> + >> +static inline int tdx_platform_ioctl(int cmd_id, __u32 flags, void *data) >> +{ >> + return tdx_ioctl_internal(NULL, TDX_PLATFORM_IOCTL, cmd_id, flags, data); >> +} >> + >> +static inline int tdx_vm_ioctl(int cmd_id, __u32 flags, void *data) >> +{ >> + return tdx_ioctl_internal(NULL, TDX_VM_IOCTL, cmd_id, flags, data); >> +} >> + >> +static inline int tdx_vcpu_ioctl(void *vcpu_fd, int cmd_id, __u32 flags, >> + void *data) >> +{ >> + return tdx_ioctl_internal(vcpu_fd, TDX_VCPU_IOCTL, cmd_id, flags, data); >> +} > > As all of ioctl variants aren't used yet, we can split out them. No. tdx_vm_ioctl() is used right below. I can remove the tdx_platform_ioctl() because its sole user, KVM_TDX_CAPABILITIES, changed to vm scope. > An independent > patch to define ioctl functions. > > >> + >> +static int get_tdx_capabilities(Error **errp) >> +{ >> + struct kvm_tdx_capabilities *caps; >> + /* 1st generation of TDX reports 6 cpuid configs */ >> + int nr_cpuid_configs = 6; >> + size_t size; >> + int r; >> + >> + do { >> + size = sizeof(struct kvm_tdx_capabilities) + >> + nr_cpuid_configs * sizeof(struct kvm_tdx_cpuid_config); >> + caps = g_malloc0(size); >> + caps->nr_cpuid_configs = nr_cpuid_configs; >> + >> + r = tdx_vm_ioctl(KVM_TDX_CAPABILITIES, 0, caps); >> + if (r == -E2BIG) { >> + g_free(caps); >> + nr_cpuid_configs *= 2; > > g_realloc()? Maybe a matter of preference. I would like to keep the current code unless strong objection from maintainers. > Other than this, it looks good to me.