Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4660196iob; Sun, 8 May 2022 20:59:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxWlGLjAfTJI84LFuXBcmOzQn8mbO7jfS5N44gehfnyLGbuCH9y2Dh526EadlARr9c+x40G X-Received: by 2002:a17:903:40cb:b0:15f:c1f:5f87 with SMTP id t11-20020a17090340cb00b0015f0c1f5f87mr4458996pld.83.1652068775024; Sun, 08 May 2022 20:59:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652068775; cv=none; d=google.com; s=arc-20160816; b=Az+x2lw5qI0sKQNTGBfn91oEDYSRi1GPr4e/AXlzGqYYXKulek2lnjeEsiiVXStM7q mj+rEQNx2OURQg5Rwh+KrhPokmoFb3PE2nJG3jWU4yRllbvAv7VPeSRrGgPUYTP4yHqp v5++7azw4xr/vBIP5HXRba8wamVcjI5Xy5NvkwrECBo3eYUri0SUyAbezheA6keOcBVM AYmekAqhxlOv5uiC29K5ppDr+/tqOPow4eLyPLOcuc0D4Ii1i+ApRoNBMt0SsaDe4s0O UNKhTq1ltcBWwT27vr5KmjAlbMHUPqg1rGRLUhQqNAPB7Z1yMcXIGPAyXoG5w7LdaZYc TKjA== 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=Mu9SDU/T8w7JYyIIemEXWLjwGlVa6CJWLmJnHlraS2E=; b=kA91w8282nJNkTnip0UtHhJeZ/hErzUDcqtoBnXrm6bpxywVDW1oTfyiuHZ3+BcdFX /YlMmeutTD7tOJVm5Mb85zdI19/CTUZSiJm+u2l56YmkCGDAiXHwaeb/sJGPFX0etSNu Z+ka4uUwEe8QptB0bAuhQXn50Sssy99fKBpzVuwdAgofU+4cPPuufYVkwQaQ2w/6jVIh VxP7H5padlqBX2nRWPd+1dF9wLJyOR78cVzgRe5rVfVN2pjbF23Wb/ARBjjJchPxjq92 8fCif+pBD+2NZzi77cmwSl6YreUPDAfF+F+QLBWwUZwQ0ieGeSyK6BkY+Mpw7PesO21z O3XQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="fm/4zcFI"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id j3-20020a170902da8300b00153b2d1650bsi11595077plx.275.2022.05.08.20.59.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 20:59:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="fm/4zcFI"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 09E95A621B; Sun, 8 May 2022 20:58:27 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384866AbiEESYp (ORCPT + 99 others); Thu, 5 May 2022 14:24:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383279AbiEESTo (ORCPT ); Thu, 5 May 2022 14:19:44 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DF8B5DA77; Thu, 5 May 2022 11:15:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1651774559; x=1683310559; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9xf32HW6EXdJYgTdtxGZYvnAhMF+g9xyTE0/Qzb0fQY=; b=fm/4zcFIiAb2tOb1iky810vaJDmoUXk7fUmQ7gDrQrIxGopAm54959wI sMEnpJpmPmNOODxTPvN+KUJVkX07jlihUoGQu4WzvqX5KpNmo1RqKYCnX 9PB85MsDkiIugc+glyro5snmgAgmji8buFJuDyCncpkFnxne9hklOQUnF 7BCYLFb6sPBJeYB3tqeQU/+RjywQeF90Lf6LcAN6ZiGzeOKg3UXgy5o+P 6tce8ahBwbCrfzmTgQ1JHdigQyNAZT0cZJ5m3UEyEJo3QC9Nak1MDZ1rl pOIwvAykvmsM3ZONiSWNSxVfig/yGXugOsOOVgS/w4NvNwP/CxkrtUrCP g==; X-IronPort-AV: E=McAfee;i="6400,9594,10338"; a="268097120" X-IronPort-AV: E=Sophos;i="5.91,202,1647327600"; d="scan'208";a="268097120" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2022 11:15:55 -0700 X-IronPort-AV: E=Sophos;i="5.91,202,1647327600"; d="scan'208";a="665083494" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2022 11:15:55 -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 Subject: [RFC PATCH v6 097/104] KVM: TDX: Handle TDX PV map_gpa hypercall Date: Thu, 5 May 2022 11:15:31 -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=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Isaku Yamahata Wire up TDX PV map_gpa hypercall to the kvm/mmu backend. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/tdx.c | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index ee83539d5228..d5bb5f1cbd21 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1221,6 +1221,64 @@ static int tdx_report_fatal_error(struct kvm_vcpu *vcpu) return 0; } +static int tdx_map_gpa(struct kvm_vcpu *vcpu) +{ + struct kvm *kvm = vcpu->kvm; + gpa_t gpa = tdvmcall_a0_read(vcpu); + gpa_t size = tdvmcall_a1_read(vcpu); + gpa_t end = gpa + size; + bool allow_private = kvm_is_private_gpa(kvm, gpa); + + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_INVALID_OPERAND); + if (!IS_ALIGNED(gpa, 4096) || !IS_ALIGNED(size, 4096) || + end < gpa || + end > kvm_gfn_shared_mask(kvm) << (PAGE_SHIFT + 1) || + kvm_is_private_gpa(kvm, gpa) != kvm_is_private_gpa(kvm, end)) + return 1; + + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS); + +#define TDX_MAP_GPA_SIZE_MAX (16 * 1024 * 1024) + while (gpa < end) { + gfn_t s = gpa_to_gfn(gpa); + gfn_t e = gpa_to_gfn( + min(roundup(gpa + 1, TDX_MAP_GPA_SIZE_MAX), end)); + int ret = kvm_mmu_map_gpa(vcpu, &s, e, allow_private); + + if (ret == -EAGAIN) + e = s; + else if (ret) { + tdvmcall_set_return_code(vcpu, + TDG_VP_VMCALL_INVALID_OPERAND); + break; + } + + gpa = gfn_to_gpa(e); + + /* + * TODO: + * Interrupt this hypercall invocation to return remaining + * region to the guest and let the guest to resume the + * hypercall. + * + * The TDX Guest-Hypervisor Communication Interface(GHCI) + * specification and guest implementation need to be updated. + * + * if (gpa < end && need_resched()) { + * size = end - gpa; + * tdvmcall_a0_write(vcpu, gpa); + * tdvmcall_a1_write(vcpu, size); + * tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_INTERRUPTED_RESUME); + * break; + * } + */ + if (gpa < end && need_resched()) + cond_resched(); + } + + return 1; +} + static int handle_tdvmcall(struct kvm_vcpu *vcpu) { if (tdvmcall_exit_type(vcpu)) @@ -1241,6 +1299,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu) return tdx_emulate_wrmsr(vcpu); case TDG_VP_VMCALL_REPORT_FATAL_ERROR: return tdx_report_fatal_error(vcpu); + case TDG_VP_VMCALL_MAP_GPA: + return tdx_map_gpa(vcpu); default: break; } -- 2.25.1