Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1178984rbb; Mon, 26 Feb 2024 00:51:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVCrBLlm328fOOKmBHPFrWVCgFLacE70cXJ0diazPaMz3PJuEyvnWdCGdGw29xNOWXDSZoUe1VV5jsdnSzXeTV1CRto55dUnDmds/tKfA== X-Google-Smtp-Source: AGHT+IFJnVqCvy32d6C42mCxSsq9ABv8ck6BB1QvGfQz0IOuGTDALJNkzlX5GQj1CcmIgrycAKQ1 X-Received: by 2002:ac8:598e:0:b0:42e:7ea3:dcbd with SMTP id e14-20020ac8598e000000b0042e7ea3dcbdmr4790292qte.29.1708937506544; Mon, 26 Feb 2024 00:51:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708937506; cv=pass; d=google.com; s=arc-20160816; b=xIx1+t2i3CFYScISiaZvXruICjhuGaDcKmyBtuOkSrczg8uYAxaS0kkjdcRghEcU9S qSqpRiKC71c5plWFF1XlWm62D0npbtEeko7ki1XxtFFoaFUFS7l23IQGAJvokona6yss 3Rj+zWVUu7nLY9hMJGKW8ZzrMo0mZzHQI+CXL7vaU+yUld+l9pHXZM8iy+1UuOIJabtz CSk7xxsRfPEQRhPg5Jj6srAGZ4B/nUizFaGbwRjgkrV5dKc6vqDIWclKRhoocx7ptQ1D BjQPP1WK3NGw6qo8V5y/Rl4+FtvAAUlP8oSEeGVvEpm3ZCeCOQyzqEzQqBet779Tm8Vn Inbw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=/F+dgYxlyPDvQDjkRrulv8KA8qvi1hStxig25zpsctA=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=fI1PC+rtwI0MjyWyTBUs2uaaVSzIZLxVra7t5OmWcR79jIGQ5F8Q9TBpwCx0xkiGbs vlpIeJQv+OB63iTrMAFibtTAsDjuSR+IPW8Pus4B3kPPuaFu3KZ9c2Q4XymL+0dODXQH 8sU6q3oYfzbQFkvR6yKy2TmsULOsZ941CGMfpHRCdeIagLWLjYBbharLFfRoy/aFFSuZ p3TL3b3gs6rn3HWg7PBi7Wd7ojirP5R425s51SjYiVsQYGylOs8oMo23DdYcsCnxi4o9 qeFJxdncOGhCcblrPBmkLCKcVobuLv9iRiBi+uJytGFfaZJmLv5z3eTdqGcF9cVKULJL 1srQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Z8UzlLYl; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80836-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80836-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id x17-20020a05622a001100b0042e80bd33bcsi2386892qtw.631.2024.02.26.00.51.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:51:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80836-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Z8UzlLYl; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80836-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80836-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4DA491C21AB9 for ; Mon, 26 Feb 2024 08:51:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A614369E07; Mon, 26 Feb 2024 08:28:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Z8UzlLYl" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 622CC65BD8; Mon, 26 Feb 2024 08:28:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936114; cv=none; b=fi+AvL+LZDB63FCwiO+nfbcFP+wINKU3cLrKlgxIMYCjqbJOr68qlAJ/SxStKpSW9yiV8veMk9qsW9DSe+cHFWtLot89s34kIKZX8CR7HReZvzjINmOFQav7PleglOH/iNwl+Qq3rkMLKf7uiGJhH0RconR5UkMQJ15RHxduwno= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936114; c=relaxed/simple; bh=25C879RC4XCDozj4CupsIuo0NDO0r8Kx5QkcO4HDod8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NlaWEmBy7fRpg0oPZo7cvJOhBSvkewtueowc5/GzhvZqh3ya6+UrqihNKJyMnwzkxgxC5sC75WGUbBcyrtmxyqp2/N7z/ekKiomVnuvcpTASgaaRNw1wfMNYdSwacYsiaOZZ+wMnsp0kXZcaGkQ3qDoIXc6+BLu05d1zZg0bixw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Z8UzlLYl; arc=none smtp.client-ip=198.175.65.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708936113; x=1740472113; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=25C879RC4XCDozj4CupsIuo0NDO0r8Kx5QkcO4HDod8=; b=Z8UzlLYlxoCAbVxVkWufi7J+AJqDBmsxdqHiKphHcbZPKdBApgQ5JUlT iqONuLrbzvVL0BtTSqb8JLHPFbvhZ+qBrbQshiiGt8unPIaR+i4wD6zbF bht7QaaZRqAujTaXeaUWshtTQRHV4cCZdW/xScC0ZdZEQwl0mh1XWXZKN 5bqBLjGRrJCyg7a9gMRWHlIlJWNYEGnOxC13+LysGFnt8c/rz/mo4S7R2 T+3+MVmUx+UErx+KaNEPga4Ujyk82t32j4zKlVj4P8hAJJAzuNsbT2jMh T6YmohGkQbZBHPLdMUjVX9F2iiOCZc77ix3yWe+hY2u8kpMWVa7i40jzm g==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="3069484" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="3069484" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:32 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="11272466" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:32 -0800 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 , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v19 074/130] KVM: TDX: Create initial guest memory Date: Mon, 26 Feb 2024 00:26:16 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Isaku Yamahata Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API, tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback, set_private_spte, handles it. Implement the hooks for KVM_MEMORY_MAPPING, pre_memory_mapping() and post_memory_mapping() to handle it. Signed-off-by: Isaku Yamahata --- v19: - Switched to use KVM_MEMORY_MAPPING - Dropped measurement extension - updated commit message. private_page_add() => set_private_spte() --- arch/x86/kvm/vmx/main.c | 23 ++++++++++ arch/x86/kvm/vmx/tdx.c | 93 ++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 4 ++ arch/x86/kvm/vmx/x86_ops.h | 12 +++++ 4 files changed, 129 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index c5672909fdae..7258a6304b4b 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -252,6 +252,27 @@ static int vt_gmem_max_level(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, return 0; } +static int vt_pre_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping, + u64 *error_code, u8 *max_level) +{ + if (is_td_vcpu(vcpu)) + return tdx_pre_memory_mapping(vcpu, mapping, error_code, max_level); + + if (mapping->source) + return -EINVAL; + return 0; +} + +static void vt_post_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping) +{ + if (!is_td_vcpu(vcpu)) + return; + + tdx_post_memory_mapping(vcpu, mapping); +} + #define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLE)| \ BIT(APICV_INHIBIT_REASON_ABSENT) | \ @@ -415,6 +436,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_mem_enc_ioctl = vt_vcpu_mem_enc_ioctl, .gmem_max_level = vt_gmem_max_level, + .pre_memory_mapping = vt_pre_memory_mapping, + .post_memory_mapping = vt_post_memory_mapping, }; struct kvm_x86_init_ops vt_init_ops __initdata = { diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index e65fff43cb1b..8cf6e5dab3e9 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -390,6 +390,7 @@ int tdx_vm_init(struct kvm *kvm) */ kvm->max_vcpus = min(kvm->max_vcpus, TDX_MAX_VCPUS); + mutex_init(&to_kvm_tdx(kvm)->source_lock); return 0; } @@ -541,6 +542,51 @@ static int tdx_mem_page_aug(struct kvm *kvm, gfn_t gfn, return 0; } +static int tdx_mem_page_add(struct kvm *kvm, gfn_t gfn, + enum pg_level level, kvm_pfn_t pfn) +{ + struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); + hpa_t hpa = pfn_to_hpa(pfn); + gpa_t gpa = gfn_to_gpa(gfn); + struct tdx_module_args out; + hpa_t source_pa; + u64 err; + + lockdep_assert_held(&kvm_tdx->source_lock); + + /* + * KVM_MEMORY_MAPPING for TD supports only 4K page because + * tdh_mem_page_add() supports only 4K page. + */ + if (KVM_BUG_ON(level != PG_LEVEL_4K, kvm)) + return -EINVAL; + + if (KVM_BUG_ON(!kvm_tdx->source_page, kvm)) { + tdx_unpin(kvm, pfn); + return -EINVAL; + } + + source_pa = pfn_to_hpa(page_to_pfn(kvm_tdx->source_page)); + do { + err = tdh_mem_page_add(kvm_tdx->tdr_pa, gpa, hpa, source_pa, + &out); + /* + * This path is executed during populating initial guest memory + * image. i.e. before running any vcpu. Race is rare. + */ + } while (unlikely(err == TDX_ERROR_SEPT_BUSY)); + /* + * Don't warn: This is for KVM_MEMORY_MAPPING. So tdh_mem_page_add() can + * fail with parameters user provided. + */ + if (err) { + tdx_unpin(kvm, pfn); + return -EIO; + } + + return 0; +} + static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, enum pg_level level, kvm_pfn_t pfn) { @@ -563,9 +609,7 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, if (likely(is_td_finalized(kvm_tdx))) return tdx_mem_page_aug(kvm, gfn, level, pfn); - /* TODO: tdh_mem_page_add() comes here for the initial memory. */ - - return 0; + return tdx_mem_page_add(kvm, gfn, level, pfn); } static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn, @@ -1469,6 +1513,49 @@ int tdx_gmem_max_level(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, return 0; } +#define TDX_SEPT_PFERR (PFERR_WRITE_MASK | PFERR_GUEST_ENC_MASK) + +int tdx_pre_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping, + u64 *error_code, u8 *max_level) +{ + struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm); + struct page *page; + int r = 0; + + /* memory contents is needed for encryption. */ + if (!mapping->source) + return -EINVAL; + + /* Once TD is finalized, the initial guest memory is fixed. */ + if (is_td_finalized(to_kvm_tdx(vcpu->kvm))) + return -EINVAL; + + /* TDX supports only 4K to pre-populate. */ + *max_level = PG_LEVEL_4K; + *error_code = TDX_SEPT_PFERR; + + r = get_user_pages_fast(mapping->source, 1, 0, &page); + if (r < 0) + return r; + if (r != 1) + return -ENOMEM; + + mutex_lock(&kvm_tdx->source_lock); + kvm_tdx->source_page = page; + return 0; +} + +void tdx_post_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping) +{ + struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm); + + put_page(kvm_tdx->source_page); + kvm_tdx->source_page = NULL; + mutex_unlock(&kvm_tdx->source_lock); +} + #define TDX_MD_MAP(_fid, _ptr) \ { .fid = MD_FIELD_ID_##_fid, \ .ptr = (_ptr), } diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h index 75596b9dcf3f..d822e790e3e5 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -21,6 +21,10 @@ struct kvm_tdx { atomic_t tdh_mem_track; u64 tsc_offset; + + /* For KVM_MEMORY_MAPPING */ + struct mutex source_lock; + struct page *source_page; }; struct vcpu_tdx { diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h index 5335d35bc655..191f2964ec8e 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -160,6 +160,11 @@ int tdx_sept_flush_remote_tlbs(struct kvm *kvm); void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level); int tdx_gmem_max_level(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, bool is_private, u8 *max_level); +int tdx_pre_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping, + u64 *error_code, u8 *max_level); +void tdx_post_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping); #else static inline int tdx_hardware_setup(struct kvm_x86_ops *x86_ops) { return -EOPNOTSUPP; } static inline void tdx_hardware_unsetup(void) {} @@ -192,6 +197,13 @@ static inline int tdx_gmem_max_level(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, { return -EOPNOTSUPP; } +int tdx_pre_memory_mapping(struct kvm_vcpu *vcpu, + struct kvm_memory_mapping *mapping, + u64 *error_code, u8 *max_level) +{ + return -EOPNOTSUPP; +} +void tdx_post_memory_mapping(struct kvm_vcpu *vcpu, struct kvm_memory_mapping *mapping) {} #endif #endif /* __KVM_X86_VMX_X86_OPS_H */ -- 2.25.1