Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2619943rdg; Mon, 16 Oct 2023 09:34:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH8+4j4lH9QAnpJRcCgtVuK8Y9g1XkkTJM4NjSwAV69M+EzNciVx8mqJnp84KFBpSFe+KWB X-Received: by 2002:a05:6a20:4428:b0:161:4459:c62c with SMTP id ce40-20020a056a20442800b001614459c62cmr12599796pzb.18.1697474083535; Mon, 16 Oct 2023 09:34:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697474083; cv=none; d=google.com; s=arc-20160816; b=LR38lnhk/0Dw7OMQ2moDHFBgP1dpUN7CONYyZk/TqYei2JQbjwOsVlB/gsRbjQYoPk ffkE2qopwKS5TV2nbdwLe2snGdY0jwpKs353gxnkXqkz1nCj32sfyvZfm5NkP21Tfhja Ata5E/HCdpOwKQWid/k3kvs4A6bH0mtNlHOBLWfGBm/YFtHAqk3ZOPt6z80zMX2cZcOb gLlf9s77uP0RjtMXTGZzcFJ4N1pVpdwzAFa4wyw/KeKEuZ56cPXngOE98Raf7BUHO5Eg xWxuPSO1XGf7rIMMcst4udCFGEA4AUJJRF5LUYEIPSGNB7NYEWMJGXrI9G8sTER+pWaz MxCg== 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=IqXF3ZzBBDKYCWlyD+HoLFpfvYSoY5iYFzsrZdy8xx0=; fh=4OEEkzWp/q+dELKxtVy/jK+pEs8VYY1pIiC6FIGYmPg=; b=qiSrFd5IrVxTpJsDlP4vmOdZKdwc7hIKdGOcRPsunpySU/lMmJlFatLdVoPCmCGXWj U02tstxJC+k65O12RZe9S32RWqgvveQBWZMRa/FXQduQ/Y8CD/y0noJruxg8fgL1rEpr x1lnCug/J23W+Rql+w5oRJtb272R6L2aV6hXpUPVjVChM1qdS8tiTE2bYbbZwmM/fjel e6h+CKqL1112SUCiMWOtgvk0aPXHrOsMSvZBnoJ423kvIhEXE60ro6lVLuqmf9O1o1YP r9C+cSMXOXoZENfZhPGhOueTnQb78Wqpy2DFRRKWg5zrhMPbkfF82p7FVttXrREbGjmq Ya6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=W7CM2d2w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id t10-20020a63b70a000000b00565fb7714a5si3646915pgf.800.2023.10.16.09.34.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:34:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=W7CM2d2w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 5D54B80436B4; Mon, 16 Oct 2023 09:34:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234052AbjJPQeQ (ORCPT + 99 others); Mon, 16 Oct 2023 12:34:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234034AbjJPQdQ (ORCPT ); Mon, 16 Oct 2023 12:33:16 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 453123C35; Mon, 16 Oct 2023 09:19:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473179; x=1729009179; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1VFBC7qjqrsByPfc1wTI2JLlyVPTMmz3DKHE1+bpq8U=; b=W7CM2d2w4gbL5SHK/eo5Cfn2FuzQGI7NRuy698+QnrkHKj2dq8yAQzFC EQprwlV5DOfj7EkL4Wj0SQQ+GQFwinVE3O+7nm5v/X8AcKKHrxL7VUIgI zzPncrU7MS5taQPIdjKlRx+mDENpsN9G0AMdzBK292nbTS4lS+LNL/SSJ ITeICDIC2jOk+dXtxchJAOHdZfoFc+Ss2IvvNPerIylWFbIFN1Yc62L+O gAshYcpWY2IrAvZAGpMztwnLOgyJciltjo9uNfA1+/NAywoSiT7uGQBgy r/kLbSkQP3ukIhK2yoN0OWpFJ6fCpjBb6X7DLb8yLfVmj+lZcsaXRRAvT g==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="364921980" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="364921980" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:16:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="846448255" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="846448255" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:59 -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, hang.yuan@intel.com, tina.zhang@intel.com, Sean Christopherson Subject: [PATCH v16 081/116] KVM: x86: Split core of hypercall emulation to helper function Date: Mon, 16 Oct 2023 09:14:33 -0700 Message-Id: <8dab7e2f2e5a5e82ffe784a26978602c7a6a01eb.1697471314.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=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:34:40 -0700 (PDT) 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 | 56 ++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index fcd1e2d0dc92..641f769b30d1 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2135,6 +2135,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 cpl); 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 a75b0b9ce9bf..d68e99f4f5ea 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9848,26 +9848,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, int cpl) { - 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; @@ -9876,7 +9865,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) a3 &= 0xFFFFFFFF; } - if (static_call(kvm_x86_get_cpl)(vcpu) != 0) { + if (cpl) { ret = -KVM_EPERM; goto out; } @@ -9937,18 +9926,49 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ); vcpu->arch.complete_userspace_io = complete_hypercall_exit; + /* stat is incremented on completion. */ return 0; } default: ret = -KVM_ENOSYS; break; } + out: + ++vcpu->stat.hypercalls; + 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; + int cpl; + + 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); + cpl = static_call(kvm_x86_get_cpl)(vcpu); + + ret = __kvm_emulate_hypercall(vcpu, nr, a0, a1, a2, a3, op_64_bit, cpl); + if (nr == KVM_HC_MAP_GPA_RANGE && !ret) + /* MAP_GPA tosses the request to the user space. */ + return 0; + if (!op_64_bit) ret = (u32)ret; kvm_rax_write(vcpu, ret); - ++vcpu->stat.hypercalls; return kvm_skip_emulated_instruction(vcpu); } EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); -- 2.25.1