Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp332492rwb; Tue, 25 Jul 2023 16:54:43 -0700 (PDT) X-Google-Smtp-Source: APBJJlFWgYVuGSZ/IRLFCVq2F2frrWVpDD7yC9MW5tmjGQftde/0Brg+0BPABlW9L//OuWSLu0BQ X-Received: by 2002:a05:6a21:2706:b0:130:f6bc:9146 with SMTP id rm6-20020a056a21270600b00130f6bc9146mr452813pzb.14.1690329283205; Tue, 25 Jul 2023 16:54:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690329283; cv=none; d=google.com; s=arc-20160816; b=RGHC/r7Etvfw2Re+p39iGVVq11imYc52TWko7NwIZfrqGFDN5CQHUYhobzqx2jtveg dBzH9xYCGWlVitkYPrSHYk7fZNTngblXkszd2Kw/d/MWQZtqvXx7e0tL0Ej0xUq2Xuao rIN6ma9Erq+ebSdNpXDsy5a2nhAUnAQC4qlqcV/XxlX2wK81byOaOOjxkddJQhLNjALJ afx3EItLH1PYpea21uH/XtwHqV7tRLPIQMQieiBxM1KXtNqf0WwuYaKZblPBeDN70s3L W/rqrOYyk0k7rs0OvtlNJ6pJOCAJCum2+bV88fOklGVs6aYGr03WyRl5vTQv9HzqhfuK U9DQ== 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=I2hONYhI3dVhJ8oUuB6S6z/INBKz4h+iMSBGKjT2Lpw=; fh=juxwNcA6iKwLISFgUCNwdoIYC0NKLUseq3xZdq25RR4=; b=Ut0A6xxf1Wi66OS1wEPia4L9rBXCL4G2QqUuacWVFxtASjrxViDh8g5kNrXeeKG4Sd dwpaEJ5JckqYbR7Nm+wAo4OHFYRnSMyAlGTKL/6YnO9rSxHNf8GCwGuWCvxuL14fj21Y 6a/2G4PE8oC4omJHYAkbrwMOlqmBJJpErAkrBX6kCY1uepVN71kykmdG8XkdvG+7Nn5X QJf+qTF7mZK8zgtHYJtPHWt/FOdf8LS9NGiC1fRbj46WUJdySbuHDozy8oX30l+FUSLX ZedBhMwAPlvWHs/Sw97o4PbMG34QXE0tfIn8LTaW3sS6jStBO2Jp8ounHWtdDUZQceVR FZWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OfoSPGMJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y23-20020a63de57000000b00563a8cd68dcsi6198797pgi.789.2023.07.25.16.54.29; Tue, 25 Jul 2023 16:54:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OfoSPGMJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230194AbjGYWTE (ORCPT + 99 others); Tue, 25 Jul 2023 18:19:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231826AbjGYWRW (ORCPT ); Tue, 25 Jul 2023 18:17:22 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1BB63C11; Tue, 25 Jul 2023 15:16:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690323363; x=1721859363; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GMV9S+VuISPcx/3i9GUJTDTL+nqi66I0CevPooz+lD4=; b=OfoSPGMJnZlijcCHu7VG6Bdj91BQN5jPshA/T+m5UTp6DDww97r/YBO8 BB3CakXg1Co5Ok10drUXix57qQakUHsIdC75abfKlEIRXQlJq3HKiepAm MwmJVlpW8xxA5S28kb63MHugdZKq7vO9mzjdBHqL+4eV/tmzsJIrRBo2H G6Q4Vq9DN7JNmFzQeUOfVhLnrPYx5wWWX5f92UMzkNcl+jX/05ZRfVtz1 sWtRA2rWPD0u+w9J22bRJMqBAr0x/GqIakPRosPd8d3zQUTk2SctSrFt7 IJI6SI8Fm9r2st5otcusPNOZaOVvA0D8Ro7IbApw/0S0xTmZwm28TXGQm Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="367882521" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="367882521" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="840001780" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="840001780" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15: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 v15 060/115] KVM: TDX: Finalize VM initialization Date: Tue, 25 Jul 2023 15:14:11 -0700 Message-Id: <5b164b7f299ad4ed103f52cdb0d603100c9841b3.1690322424.git.isaku.yamahata@intel.com> 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=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(). Signed-off-by: Isaku Yamahata --- 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 32e84c29d35e..63f2b6dc4f27 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1360,6 +1360,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; @@ -1382,6 +1400,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