Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2680519rwd; Sun, 28 May 2023 22:04:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7ZelMezPZhH3NFn/ft4YvVP0rbhjAfQw2vffpUE9X493PpFJ8UnS0QHzax+IIRoCnNYYsm X-Received: by 2002:a17:902:b096:b0:1ab:d89:5ef6 with SMTP id p22-20020a170902b09600b001ab0d895ef6mr8464074plr.68.1685336682203; Sun, 28 May 2023 22:04:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685336682; cv=none; d=google.com; s=arc-20160816; b=Xtqib3JFEHhCoxH877YHZ+bImxK+phG4NwO8Fy/Eage1LgHrMKTnbPeVNCCdfMhhKr 2IR4xriBv1uChjXHNpXnFHN5OJDxOk9H++ezgnGVAOeo+ht3dGnb6yTbq69JSj2YlNQ/ lubzq/r/d+UiMsSeMLdeWIIal7liFxfpCJp9McliapluBY1LZI7xr1ssE7WSuMjpLmdD RhaJY9uqUfaqqSD9zYHWw18SMchRcOoLqnDnRqYy/Unvh05w0LT46t1VyEwgfWAzFbAk /0pgwtl+u8v4+vykB9P65m4u0UEdQ53ZBshJ33vvrA042gitEtQEi6SL54G0NrQiNzK5 Fm5w== 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=Q0rRmeg/mvN1pHXCvwotPhjtDWHUVz7ncf0F8bngXrk=; b=aWktRKtwRO0Ho3P3fRG2VMHtP+BMYhMVGJjPZisnW+xf9BDKXJHphPKpMp1uba8qdS SbpMrTeZMEC6E5hN3MnMdZ8uw6l+lbOR7A6AK5uEqMi9sfjWUdCJSn8uOMhf4i2c2CJr /7CrvexJ7n46Q5qWOhDYUkQp49gO/iFfqbHNRcO/4qfvzI+KogtqZUdS+qm8vkGkkMgI 3mLunzUXIGaYMjaWO9f55B5MkRhONEbA/MIfSor4gNyhcMy8SfzLv4I7Vf5SKkxiQ/Gb NhXbbD8MBt/UpwuhcNdDDf4GuEGjqbwx1elOiGOf6RMAd1+4gjTvU/9/tfGE+vguY24V 93ew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=co3See2i; 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 z8-20020a1709028f8800b001a6f0eab385si9018550plo.55.2023.05.28.22.04.30; Sun, 28 May 2023 22:04:42 -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=co3See2i; 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 S231614AbjE2E1o (ORCPT + 99 others); Mon, 29 May 2023 00:27:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231996AbjE2E06 (ORCPT ); Mon, 29 May 2023 00:26:58 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 412C82103; Sun, 28 May 2023 21:24:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685334256; x=1716870256; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ojaP+SeqfBDurYjLYk+X4XCKLI50S1CKYZ6aq9fSIY8=; b=co3See2iMheMxlFTxXJPJlGuMGQsf47PQjS2euuHoL10rCmdBVfRAf+U vpUJ7qXt9e1YldS5L1EnaiLK9KH8Ntb5+U6VIL45Dhwk+l2y2ijteQU/T M92MMfyo+gLE+/Mpye2YSWQcih9h2evWMft27cbmSG0IUADe1aLXeYXde XJxT8L/sZvQqFOBnrQy9i2NpIfPUITp6So518NtrF3kgpPbGZipBJWP3F SG8/r2I4HxtkIhXSTkCwXn4kjc+jsBND/mFQh/7amj2EKxFQYTFLW3hjQ JgKF44rbR9wD7/qsM2dhdutIYDiRY2+qGQ1jKs9WcDEEsAZemR1+nzeJw A==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="334966088" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="334966088" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:22 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="775784383" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="775784383" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:22 -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 Subject: [PATCH v14 060/113] KVM: TDX: Finalize VM initialization Date: Sun, 28 May 2023 21:19:42 -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=-4.6 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 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 --- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 31 +++++++++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 1 + 3 files changed, 33 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 415c0a94a1a5..371d8ffd9121 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -571,6 +571,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 7c6e02cb66e4..00a7e400c1e0 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1295,6 +1295,34 @@ 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; + } + + /* + * Blindly do TDH_MEM_TRACK after finalizing the measurement to handle + * the case where SEPT entries were zapped/blocked, e.g. from failed + * NUMA balancing, after they were added to the TD via + * tdx_init_mem_region(). TDX module doesn't allow TDH_MEM_TRACK prior + * to TDH.MR.FINALIZE, and conversely requires TDH.MEM.TRACK for entries + * that were TDH.MEM.RANGE.BLOCK'd prior to TDH.MR.FINALIZE. + */ + (void)tdh_mem_track(to_kvm_tdx(kvm)->tdr_pa); + + kvm_tdx->finalized = true; + return 0; +} + int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_tdx_cmd tdx_cmd; @@ -1317,6 +1345,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 254419d4dfb4..41666bfa3bcb 100644 --- a/tools/arch/x86/include/uapi/asm/kvm.h +++ b/tools/arch/x86/include/uapi/asm/kvm.h @@ -571,6 +571,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