Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2610295rdg; Mon, 16 Oct 2023 09:18:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFxEX4EFofcdh7ZKBzPW59ukDhnWY1nZdtccSQ0bWDY1kUSLzMiA4RS/rzspcq7u3LnccRo X-Received: by 2002:a17:90a:4584:b0:273:b0bd:343b with SMTP id v4-20020a17090a458400b00273b0bd343bmr28821597pjg.41.1697473130896; Mon, 16 Oct 2023 09:18:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697473130; cv=none; d=google.com; s=arc-20160816; b=SLIideGjQnvTViK26CR3eR22e4itj6A66LqoJrjWL2AroxyPVPoy6/cDJjaM2cbTOg GfdT7F5fnHd8Exrwc7ur+sqXZ+G6rS/GMxcqP+6124iM3WI5GUA9Z+iqjCbkJ4bodBIO PRzSjkP8SgUrOhbex1aGbxxcQj69oWIP5D74xn9dkne174ywvCDxr3HBjqvxeRBCohgT uoE3dW+EZEgXYBf5AcLR65a902BARmy7Bmdnei7HGcyTmldumXgoQNwoN1IZKmBhTgUC 97xfoR/HkwsPV+YAUk/Lq8OvdIm8ebPAGIu7AE9Qi2nz8VQ/r6QszC0Pf8WYgnvmuhb4 tcSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=eZ20UJhSjrRapn2+KTZiTxgpc45z+WbJmsEmpBbFmmo=; fh=lRdU2Q/1zx5DcPdZuWBjshA5VT5Oc9cEhB1tCFiV0Nw=; b=UzV6fgNR+rZUMYO6NP9jbonAYNFmNZ5KWxL3CS8UoQJlad5tVMXKvDcdYj5joc3xpw RGTrwdaIogUQXLwoBjKtRkdbHzAoRpvJPhOyNCUAllFKyCX53rWpreayEDAhTyp6xtJc gWScF7NMkFmT4nEI18UaKEqlsT/9gnCmbWVBL2enloE2Cy+TUcd3CtwjWF+IqETNaQdD 5sb+/KM86mDVbXlJCEDkVRNQOlAGgMxdCaPJmG0HcjfO9LBTr9n8WJ+5G834xw8g4LGC EgU1MyJA1Hfq2vhfTBNufAiO12prNyyTloOPQrEBdvjNH/4XxErBljySDs4WNDNykDl7 J4sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BvDjQbTP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id 30-20020a17090a001e00b00268e32e99f0si6266484pja.28.2023.10.16.09.18.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:18:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BvDjQbTP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 961018021584; Mon, 16 Oct 2023 09:18:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234160AbjJPQSj (ORCPT + 99 others); Mon, 16 Oct 2023 12:18:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233918AbjJPQSW (ORCPT ); Mon, 16 Oct 2023 12:18:22 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18D89D46; Mon, 16 Oct 2023 09:17:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473053; x=1729009053; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=b7riiPa5ae/x4ZfC4MHlD8SEswzGBdISqJKAID+goJg=; b=BvDjQbTPjDnGZUEtWLXIfbD0P054/l55MUzgKq8VUMspx2raXi5s58au PQF4StQKEYvU7u0+awuBzT4HFeG6/vl1VfD5k1zqY4D8ybjddZ9RHIsk8 NMjxNCPdxMQA1EutWXAEF5hIe08ctRFeiPxLsvSuq0T19dkWoAgloZblc 6j7bsQ+SxIBkrm+342Q6vXBnJn4BebtLibDJQ7sFExVl2yRrVmXD8VbDf 6D9snpwW7rFEkAt9oXcYWhENUcsi/WBlVnUo1ZTgYIvPxGLoBJOVOh3Qb pKZm3rcqSewaJjj1UoEsVXeReZFFxLL5ubfIKQ0tljqwG2mTOBmgLlu04 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="364921850" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="364921850" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="846448174" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="846448174" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:49 -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 Subject: [PATCH v16 060/116] KVM: TDX: Finalize VM initialization Date: Mon, 16 Oct 2023 09:14:12 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.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 (morse.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:18:47 -0700 (PDT) From: Isaku Yamahata To protect the initial contents of the guest TD, the TDX module measures the guest TD during the build process as SHA-384 measurement. The measurement of the guest TD contents needs to be completed to make the guest TD ready to run. Add a new subcommand, KVM_TDX_FINALIZE_VM, for VM-scoped KVM_MEMORY_ENCRYPT_OP to finalize the measurement and mark the TDX VM ready to run. Signed-off-by: Isaku Yamahata --- v14 -> v15: - removed unconditional tdx_track() by tdx_flush_tlb_current() that does tdx_track(). --- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 1 + 3 files changed, 23 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index a1815fcbb0be..1b4134247837 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -573,6 +573,7 @@ enum kvm_tdx_cmd_id { KVM_TDX_INIT_VM, KVM_TDX_INIT_VCPU, KVM_TDX_INIT_MEM_REGION, + KVM_TDX_FINALIZE_VM, KVM_TDX_CMD_NR_MAX, }; diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index dc17c212cb38..f4a8f57c2b35 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1423,6 +1423,24 @@ static int tdx_init_mem_region(struct kvm *kvm, struct kvm_tdx_cmd *cmd) return ret; } +static int tdx_td_finalizemr(struct kvm *kvm) +{ + struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); + u64 err; + + if (!is_hkid_assigned(kvm_tdx) || is_td_finalized(kvm_tdx)) + return -EINVAL; + + err = tdh_mr_finalize(kvm_tdx->tdr_pa); + if (WARN_ON_ONCE(err)) { + pr_tdx_error(TDH_MR_FINALIZE, err, NULL); + return -EIO; + } + + kvm_tdx->finalized = true; + return 0; +} + int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_tdx_cmd tdx_cmd; @@ -1445,6 +1463,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) case KVM_TDX_INIT_MEM_REGION: r = tdx_init_mem_region(kvm, &tdx_cmd); break; + case KVM_TDX_FINALIZE_VM: + r = tdx_td_finalizemr(kvm); + break; default: r = -EINVAL; goto out; diff --git a/tools/arch/x86/include/uapi/asm/kvm.h b/tools/arch/x86/include/uapi/asm/kvm.h index a3408f6e1124..4753a29a22ec 100644 --- a/tools/arch/x86/include/uapi/asm/kvm.h +++ b/tools/arch/x86/include/uapi/asm/kvm.h @@ -568,6 +568,7 @@ enum kvm_tdx_cmd_id { KVM_TDX_INIT_VM, KVM_TDX_INIT_VCPU, KVM_TDX_INIT_MEM_REGION, + KVM_TDX_FINALIZE_VM, KVM_TDX_CMD_NR_MAX, }; -- 2.25.1