Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp503982rwb; Thu, 12 Jan 2023 08:41:23 -0800 (PST) X-Google-Smtp-Source: AMrXdXvCkqBQVgbxF3Go47ZmQ3jyOP3HLohwrWGLJIOUfe5aDOtKpinnqeLFHDLCkHvdSCtPXgmq X-Received: by 2002:aa7:dd45:0:b0:494:fae3:c0df with SMTP id o5-20020aa7dd45000000b00494fae3c0dfmr20453808edw.12.1673541682960; Thu, 12 Jan 2023 08:41:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673541682; cv=none; d=google.com; s=arc-20160816; b=rL3O/QVsEF4nyEMrf07O30GAiltmJn76sj3FOCejsRCLXka15w3FHMk5yyIaHsuNH1 yf3X0+G612g04LewLHtZ0jR0YmiquPB/Apn0ZEbtgCHcMyQzslI/uZf7xzJYBC8oKSUq PBd9FGjoMY9XtvwytJ3v7gliiUae40BBekIrbmJ8WiXEOKmWDEEaDBLa4/pEWvJKwkhA w0rd3mRbPTSj65RwqQEtLA/kESvKaxUCN244jqaswUlUYqtc1bBiocGhzS09YODXb2BN pG/viixXPejKj69mXBROsArhuqhnr6a9pNwg6FoiaFxLU7ryCpzCiUQwmmCFMNpeWs0y C45A== 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=RvNAtcT+3k1iZdMrQ9jBzvwpUO+8czCH6QNLp/B9YeY=; b=GNpqxM7346BVxqwXcHEgkZGk/7/XoX8488f95WlqGIOYn5iG3DKTWIdGupsu21GMn/ 4rvkRXGiyRFKXOVyX8Ecafhccg8PEZa0Ystd7MZDgBg30P3ugVKhuTBCgjDOLCg+Idkj K2D0VWbbE9wPqI5ioe9skeMiEeKb/8bIwZ3FxRpZC0aiUEdruFEwih/oi8hJUXNaZOd3 0AZD8UXgdKrTX8dHNTOTrP0iaaPttk27mn/TkGYQEJVDyo/VVmgy6TFyoechUnpIdavx PEbMBcuMsetpWw8wTgRkDusKJI/bmjzZnrAKPK4g+vfA3xS4KjR0Uec5Le4FcQJFyo2j o0Gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VUR3eCsg; 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 i9-20020a05640242c900b00485fa68b06csi24734282edc.354.2023.01.12.08.41.10; Thu, 12 Jan 2023 08:41:22 -0800 (PST) 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=VUR3eCsg; 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 S233851AbjALQjB (ORCPT + 50 others); Thu, 12 Jan 2023 11:39:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232439AbjALQhU (ORCPT ); Thu, 12 Jan 2023 11:37:20 -0500 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 436B81182C; Thu, 12 Jan 2023 08:33:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673541222; x=1705077222; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GCt+pPvCoIeId6mkCXkmNQtyy5F8g00nHFpmxLAuMlc=; b=VUR3eCsgVf6jv4SiCk0XYZiVXb+KwRNSgCdauGDOx2VF+osJxSYLedUc x5Ca2nGwkbH5WlDo51/Ylz8oM88pOTxfIDAACsEsKGgq+T67/ODJAzyKS Hmo995espkJXAyt78FuD2q/s5Nx/9j/WiGunhhRWrJimtyfu5N5u1vLW1 0zS9MOo1tiaUC72SAw6jxFVLkS5a7dV2lQxafvqk/zlViYhOOgKM/FVDv T13oLw1K6pbrSJg5AamI8HzgCladrnRBFFA/g4SkRmJ8dTWycAW1z7sHJ d9q8uMXMLULPs3ibhhbikJNmMfI5nWzR8HxnAVDziLMJS6uSW5ykCwxPZ Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="386089672" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="386089672" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:34 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="726372522" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="726372522" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:34 -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 , David Matlack , Sean Christopherson Subject: [PATCH v11 083/113] KVM: x86: Split core of hypercall emulation to helper function Date: Thu, 12 Jan 2023 08:32:31 -0800 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.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 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: Sean Christopherson By necessity, TDX will use a different register ABI for hypercalls. Break out the core functionality so that it may be reused for TDX. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/include/asm/kvm_host.h | 4 +++ arch/x86/kvm/x86.c | 54 ++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index cead782a48b6..f93d271aba67 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2074,6 +2074,10 @@ static inline void kvm_clear_apicv_inhibit(struct kvm *kvm, kvm_set_or_clear_apicv_inhibit(kvm, reason, false); } +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr, + unsigned long a0, unsigned long a1, + unsigned long a2, unsigned long a3, + int op_64_bit); int kvm_emulate_hypercall(struct kvm_vcpu *vcpu); int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 384aa282c68f..3d1c854b1604 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9723,26 +9723,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu) return kvm_skip_emulated_instruction(vcpu); } -int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr, + unsigned long a0, unsigned long a1, + unsigned long a2, unsigned long a3, + int op_64_bit) { - unsigned long nr, a0, a1, a2, a3, ret; - int op_64_bit; - - if (kvm_xen_hypercall_enabled(vcpu->kvm)) - return kvm_xen_hypercall(vcpu); - - if (kvm_hv_hypercall_enabled(vcpu)) - return kvm_hv_hypercall(vcpu); - - nr = kvm_rax_read(vcpu); - a0 = kvm_rbx_read(vcpu); - a1 = kvm_rcx_read(vcpu); - a2 = kvm_rdx_read(vcpu); - a3 = kvm_rsi_read(vcpu); + unsigned long ret; trace_kvm_hypercall(nr, a0, a1, a2, a3); - op_64_bit = is_64_bit_hypercall(vcpu); if (!op_64_bit) { nr &= 0xFFFFFFFF; a0 &= 0xFFFFFFFF; @@ -9751,11 +9740,6 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) a3 &= 0xFFFFFFFF; } - if (static_call(kvm_x86_get_cpl)(vcpu) != 0) { - ret = -KVM_EPERM; - goto out; - } - ret = -KVM_ENOSYS; switch (nr) { @@ -9814,6 +9798,34 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) ret = -KVM_ENOSYS; break; } + return ret; +} +EXPORT_SYMBOL_GPL(__kvm_emulate_hypercall); + +int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) +{ + unsigned long nr, a0, a1, a2, a3, ret; + int op_64_bit; + + if (kvm_xen_hypercall_enabled(vcpu->kvm)) + return kvm_xen_hypercall(vcpu); + + if (kvm_hv_hypercall_enabled(vcpu)) + return kvm_hv_hypercall(vcpu); + + nr = kvm_rax_read(vcpu); + a0 = kvm_rbx_read(vcpu); + a1 = kvm_rcx_read(vcpu); + a2 = kvm_rdx_read(vcpu); + a3 = kvm_rsi_read(vcpu); + op_64_bit = is_64_bit_hypercall(vcpu); + + if (static_call(kvm_x86_get_cpl)(vcpu) != 0) { + ret = -KVM_EPERM; + goto out; + } + + ret = __kvm_emulate_hypercall(vcpu, nr, a0, a1, a2, a3, op_64_bit); out: if (!op_64_bit) ret = (u32)ret; -- 2.25.1