Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp3082034rdb; Mon, 4 Dec 2023 16:49:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IGO0L9+vb6etqTGqht9vtpQh6L5NTeqPP4tFgjr3oBpzgcVTwMK61EOj8Xs9k6xoIMwqEln X-Received: by 2002:a17:90b:17d2:b0:286:8f8f:5e3d with SMTP id me18-20020a17090b17d200b002868f8f5e3dmr370256pjb.30.1701737368014; Mon, 04 Dec 2023 16:49:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701737368; cv=none; d=google.com; s=arc-20160816; b=vgX8wvaupoSYc8tbGkuipdH7eYT3EeCfH2tpRxieyxruydJ2Dchb6nvwfK6XVIs5k4 yzDWvfl75DE1EALD9mS/qeiA1F12PTuWpC4TLqkUbNsCR7LAGdA43K9ZRJQpCQ88drTP GPiyqzIIFKvE1+C1Xi97WWg28oc8VPXcG0eWMouD0xN1g0a9py49YDoGum25VUXSPLyG vZQl6isO+gHE0JisZFjWTnitVJimC4DgZteIOEpgpFF8jWknHwGm3ymZ6B9xzZFqYNau dCHzjg117Wvx9/l6P7zyw9usbOUi77NFVIGMljg9CSuI16HZDqBqNmOJJfW87J+r60LS xvPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id :dkim-signature; bh=dwaGPPds1gqq/hmbR4RYqUAO7HZf4M93D51HxV2a6Rc=; fh=5d9u4qpgWBmGDmpkZnNoReK+SL//bU6ZL/DWWaAPURE=; b=g/KX1GCAXkzgnex08KPJHQHhvE3slqmHTJx/Bv3ue0pNPiKa9/kdMrMdFGyjOHQgtP UXrdkSO65jPrW62G495akifztqhmuCbkipjrh1659XBvllu7ZYhwmD85OpwGq1pP9M+c 0frIlxG0A9bgL31r/MC83XkCr+nhUeE8D2N7GBmDyzFT1XKgPYCeiTEXoF1xhX+exwSj fLLHdeu99z2pb86h3JyassOifjHLLNXzWbajxZ9vfJRNi/QG7tGu3cSSpqkyI2uXbwTB HrErPOlEeDTr4iSRaJV2qjR/RcrS63QUotvXNqwgNumsS72KElhSf/p6xj6vhIbvKVLn Rp8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=diARr6Bd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id n16-20020a170903111000b001cfb1dae607si8761176plh.146.2023.12.04.16.49.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 16:49:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=diARr6Bd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 471C68098491; Mon, 4 Dec 2023 16:49:25 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234865AbjLEAst (ORCPT + 99 others); Mon, 4 Dec 2023 19:48:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231567AbjLEAsp (ORCPT ); Mon, 4 Dec 2023 19:48:45 -0500 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.13]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17CF6C4; Mon, 4 Dec 2023 16:48:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701737331; x=1733273331; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=9beogKZrnue2jWeZInSxedei5tVeH9HIT36azUrOOZg=; b=diARr6Bdl2X5Wtmus3r+OjGXC6zfYoIvfgQeB2EDtPOfZa+zrAHRec+J VYoVgywWgjTv+dEnS0GGvZwrlCYL/1hhu6pO+jceLWlLOXxX55Q9MQdN3 nS6HeZgp9WTxC0KC9iny6NMiUs8P6KLfPIH8wjgTxd5QEvM0vBnz1hR95 /3DUMY8jV5N1ESoE6d8D3mfHWTOK9br9bbBUqcrSQxZVOswm4262itEmJ 2DIFCoMVU426KCI3Y4MN6aLIfPpYUVWi6wJd0ZGjDf0O67/VMWWsml6Eo qf+f+oF0Dl94zcgh1IDYMgyn6Zg4eyxD+zSOyLab2CflxVEsH9s+7LCCM w==; X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="867650" X-IronPort-AV: E=Sophos;i="6.04,251,1695711600"; d="scan'208";a="867650" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2023 16:48:51 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="799777470" X-IronPort-AV: E=Sophos;i="6.04,251,1695711600"; d="scan'208";a="799777470" Received: from binbinwu-mobl.ccr.corp.intel.com (HELO [10.238.10.126]) ([10.238.10.126]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2023 16:48:46 -0800 Message-ID: <0fa7580f-a8e9-408e-b6f5-a71326eedb84@linux.intel.com> Date: Tue, 5 Dec 2023 08:48:44 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v17 001/116] KVM: VMX: Move out vmx_x86_ops to 'main.c' to wrap VMX and TDX To: isaku.yamahata@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, 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 , Xiaoyao Li References: <167bc6ff99c28ace90f7e9873734a3b5502eddda.1699368322.git.isaku.yamahata@intel.com> From: Binbin Wu In-Reply-To: <167bc6ff99c28ace90f7e9873734a3b5502eddda.1699368322.git.isaku.yamahata@intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 04 Dec 2023 16:49:25 -0800 (PST) On 11/7/2023 10:55 PM, isaku.yamahata@intel.com wrote: > From: Sean Christopherson > > KVM accesses Virtual Machine Control Structure (VMCS) with VMX instructions > to operate on VM. TDX doesn't allow VMM to operate VMCS directly. > Instead, TDX has its own data structures, and TDX SEAMCALL APIs for VMM to > indirectly operate those data structures. This means we must have a TDX > version of kvm_x86_ops. > > The existing global struct kvm_x86_ops already defines an interface which > fits with TDX. But kvm_x86_ops is system-wide, not per-VM structure. To > allow VMX to coexist with TDs, the kvm_x86_ops callbacks will have wrappers > "if (tdx) tdx_op() else vmx_op()" to switch VMX or TDX at run time. > > To split the runtime switch, the VMX implementation, and the TDX > implementation, add main.c, and move out the vmx_x86_ops hooks in > preparation for adding TDX, which can coexist with VMX, i.e. KVM can run > both VMs and TDs. Use 'vt' for the naming scheme as a nod to VT-x and as a > concatenation of VmxTdx. > > The current code looks as follows. > In vmx.c > static vmx_op() { ... } > static struct kvm_x86_ops vmx_x86_ops = { > .op = vmx_op, > initialization code > > The eventually converted code will look like > In vmx.c, keep the VMX operations. > vmx_op() { ... } > VMX initialization > In tdx.c, define the TDX operations. > tdx_op() { ... } > TDX initialization > In x86_ops.h, declare the VMX and TDX operations. > vmx_op(); > tdx_op(); > In main.c, define common wrappers for VMX and TDX. > static vt_ops() { if (tdx) tdx_ops() else vmx_ops() } > static struct kvm_x86_ops vt_x86_ops = { > .op = vt_op, > initialization to call VMX and TDX initialization > > Opportunistically, fix the name inconsistency from vmx_create_vcpu() and > vmx_free_vcpu() to vmx_vcpu_create() and vxm_vcpu_free(). Nit: 3 indentation alignment below. Reviewed-by: Binbin Wu > > Co-developed-by: Xiaoyao Li > Signed-off-by: Xiaoyao Li > Signed-off-by: Sean Christopherson > Signed-off-by: Isaku Yamahata > Reviewed-by: Paolo Bonzini > --- > arch/x86/kvm/Makefile | 2 +- > arch/x86/kvm/vmx/main.c | 167 +++++++++++++++++ > arch/x86/kvm/vmx/vmx.c | 374 ++++++++++--------------------------- > arch/x86/kvm/vmx/x86_ops.h | 125 +++++++++++++ > 4 files changed, 395 insertions(+), 273 deletions(-) > create mode 100644 arch/x86/kvm/vmx/main.c > create mode 100644 arch/x86/kvm/vmx/x86_ops.h > > diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile > index 80e3fe184d17..0e894ae23cbc 100644 > --- a/arch/x86/kvm/Makefile > +++ b/arch/x86/kvm/Makefile > @@ -23,7 +23,7 @@ kvm-$(CONFIG_KVM_XEN) += xen.o > kvm-$(CONFIG_KVM_SMM) += smm.o > > kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ > - vmx/hyperv.o vmx/nested.o vmx/posted_intr.o > + vmx/hyperv.o vmx/nested.o vmx/posted_intr.o vmx/main.o > kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o > > kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o \ > diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c [...] > > -static int vmx_check_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type, > +int vmx_check_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type, > void *insn, int insn_len) Indentaion alignment > { > /* > @@ -1742,7 +1740,7 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu) > * Recognizes a pending MTF VM-exit and records the nested state for later > * delivery. > */ > -static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu) > +void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu) > { > struct vmcs12 *vmcs12 = get_vmcs12(vcpu); > struct vcpu_vmx *vmx = to_vmx(vcpu); > @@ -1773,7 +1771,7 @@ static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu) > } > } > [...] > +u8 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio); > +void vmx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, > + u64 *info1, u64 *info2, u32 *intr_info, u32 *error_code); Indentation alignment > +u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu); > +u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu); > +void vmx_write_tsc_offset(struct kvm_vcpu *vcpu); > +void vmx_write_tsc_multiplier(struct kvm_vcpu *vcpu); > +void vmx_request_immediate_exit(struct kvm_vcpu *vcpu); > +void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu); > +void vmx_update_cpu_dirty_logging(struct kvm_vcpu *vcpu); > +#ifdef CONFIG_X86_64 > +int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc, > + bool *expired); Indentation alignment > +void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu); > +#endif > +void vmx_setup_mce(struct kvm_vcpu *vcpu); > + > +#endif /* __KVM_X86_VMX_X86_OPS_H */