Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp4118371rwb; Sun, 7 Aug 2022 15:34:30 -0700 (PDT) X-Google-Smtp-Source: AA6agR4j2iwmSTsQMqRcE5iarcmIUQa9ksUgyh2zl5EdXSo+3bQ/ZQMkkMaNH/4HjmzWbz2WbswC X-Received: by 2002:a05:6a00:a06:b0:52d:f9fa:8fc9 with SMTP id p6-20020a056a000a0600b0052df9fa8fc9mr16395272pfh.15.1659911669821; Sun, 07 Aug 2022 15:34:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659911669; cv=none; d=google.com; s=arc-20160816; b=odv1C/rD4Hjh3RgK1M912T12+bxNKRmmirN4+une7mPSpJBZkTxaGBIVyLy+IfDum6 eEAVvJBorh0P33V5WmyIYjYUyvCmd4ItzbaV+J5pd3mVUj3jmdxMM7J+S2W9TbD+Nwyp OxtMbrwysWP9qEE9TGBrCkN8XnCjw70HKIFbqrwYlSBAWc/WbphDjfDGiFcoIJxAe+Vw F6/HS3/XF9TyXkLfsBoAOkXaTewBsMUHNqCf5SlA9cyakv6GPJE3NdtxyGqteF/yEa2N Fpegmg5D1z3b/v7cZC+LRPZHbA0NU3ZD1eOPTJPPJDcAsLf/WdONOxB5GlXIWUXNx59K 8otA== 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=cKTJBad5eMEjUnfoeTo5IekoL44+9AQxFNSWKsyY614=; b=zJwdOOK8ucL8dl9hBw2EB5n0lTnp2AKaOqjjcpnJ9QYSQP5cjpxeiV31jwpwvbcrkl eAycZmxp5/5cj+UTkkUMXElF1OYmdTUMhphLM59TIkZuQXAdI/6aUixTijH91q1V7Qn4 pITGGNiGTszZWuHKlEc2qVe+zQ4VJQn/8Ksn0JAMGduIFKBE5c4nbuVfC9m4yTgs4mGa wxe0gS6No+q9+QS/BbYp5kaDsgT9OePAMfUtcJ7KfRIodFdBsoxb0dIkYOiQlh/v3Eqb x82R9GFhxlVy5zNFVrt18bZsBTavH+jCKkfZXEWmwAbOmc6H+XB7JOS0Az5Ps1/fjiz3 vTLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=U4ttQ2Ok; 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 h5-20020a170902f70500b001708b184ff9si5546009plo.213.2022.08.07.15.34.15; Sun, 07 Aug 2022 15:34:29 -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=U4ttQ2Ok; 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 S237294AbiHGWJ3 (ORCPT + 99 others); Sun, 7 Aug 2022 18:09:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237078AbiHGWFM (ORCPT ); Sun, 7 Aug 2022 18:05:12 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65DB8B7DF; Sun, 7 Aug 2022 15:03:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1659909783; x=1691445783; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2IKpMD/5mOlSohbGo23im3gthlxB9WFfnDJ1NWi5/PI=; b=U4ttQ2OkO8Ljj7iwXr17+YoBaOe5YBrQ76OuHWQLQkhrj7OrBjJto/bE LfS9ZY0tR9nEH1thzdBFIefURo3xOb8yAjsyDzvf5AErYP7QK5gCwA8FR QXdO8ocpWgALiFaIzCFeGyddU+dgJgtFJjnhUIMVOA9NUT2n+7lXseLNR taWjO0hQZ9wPZDmLC8mLZfTLs2Zfr51K0wHnR5aG4AqXIP9tn4ISVUbIG bQlIHXfUZwAwhGxwsOUYZ/HfXpwtWVeayyltbel1Tws2n/KSL27yE5YFc La3zXw4v/+TPg/QIctaRHQB4UjYlIRIY99YXj+D7Y9pnNfygNt2cZnYxA g==; X-IronPort-AV: E=McAfee;i="6400,9594,10432"; a="270240385" X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="270240385" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:42 -0700 X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="663682715" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:42 -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: [PATCH v8 082/103] KVM: x86: Split core of hypercall emulation to helper function Date: Sun, 7 Aug 2022 15:02:07 -0700 Message-Id: <3a91ab66dfef4c23faa13b50e0296630d562e95d.1659854791.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=-5.0 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_PASS,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: 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 ef04220527b2..df0b98e09cc9 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1995,6 +1995,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 bc3b657e9795..01a320dd20ab 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9495,26 +9495,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; @@ -9523,11 +9512,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) { @@ -9586,6 +9570,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