Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp168431rdb; Mon, 22 Jan 2024 16:09:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IEmwVqmcDzWo5tlMhU8kQoGzkwghdCzZy/kfC/ABN81/eC2XKUao3XAOtahsBuI9aQRY/oc X-Received: by 2002:a05:620a:298a:b0:783:a2f5:6297 with SMTP id r10-20020a05620a298a00b00783a2f56297mr1891606qkp.44.1705968585427; Mon, 22 Jan 2024 16:09:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705968585; cv=pass; d=google.com; s=arc-20160816; b=zPsQi8KG+dQXOt7R2ecQsJlet8SrVt6zzuWpxYT08S7Xt348lLhZbi0S4ngv4wuczE QlDSFGa4Aof/RstgzDN9Wsf16otF8G7r8EP14mvMnMyBlnHlTnxQLDTpJgVOoE1sM/XQ VpoPS5QGR3R4Ul4PsKIBWpt1E/fNVhU7FYKO6E3WdkZ625Ki5GyCQ7zHrliV2/AxGc9v FWjFQpl/81YUSn2OQodoZB3caG/RxBcApaI+hgUHTpc90KM4IdJqpPEesmaBUsMCP7ZU T6oxFQ5pJ4CsUpKbi7fPde5lolUpyluyPVFw1yNw2ekWE6+y+4gih29AFFGT5YECNPWs nY/w== 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=PCX4HfAwJ6v8IrU+341Yz0OK7651fStvb0KAqt0yur4=; fh=ka1BjeomumTXa1Vhgkzg2yK2f13tB8VguFI2e9zPJ3c=; b=uzuz/BFXBpWYBo7KUMEGJ4qY+dfqVpNpe2bPd/IRJDdMPEZeZrFNjwaKvotaAEV8FW DEwTtNIgyPwUEvzjuSffDlBq4YvyURlgY7SYNZof21mD56Bn1GQNC7ZetbDGDZfKCqFE VHvxAlZ7iyygh0Z2zU/CrW8genlwGvqxZrCWDQE4TE+xgRCLqBbu9YOCitxbJBsv6B32 xenVNrUowjufCc92pV9pWW5hgtKdaW2u1yI5Ij677EuijrI8unlJWTseqsoGXqJhN5g9 QeOD4M7FH8boY29inUiSznSxoHKc6rYqY3ftSOtWUG4XPcqj890P5CWNg/fJ6jPP5ELN mNTw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="WxlRvc/6"; 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-34388-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34388-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 s2-20020a05620a080200b0078396394489si5552286qks.439.2024.01.22.16.09.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 16:09:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34388-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="WxlRvc/6"; 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-34388-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34388-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 1CEF71C25256 for ; Tue, 23 Jan 2024 00:09:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0EE716A01C; Mon, 22 Jan 2024 23:55:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="WxlRvc/6" Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) (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 3F777664BB; Mon, 22 Jan 2024 23:55:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.55.52.120 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967756; cv=none; b=WlZ1CgeD5RYDhi50DUL3okJtLjyPDIdI0QldX7aEmJOSsc7omqC8vMyZ6pxEa1Zlye/oDWi58z6Uv95dDxLBPxexC3hHrMyoyE0JCpY55/RhihqFT7waTHWjSHwUHnA/1VQXKXIMWbtIaweof2+vfIl8EEIOE9SoZ+r36bjg+fI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967756; c=relaxed/simple; bh=BZwHMVdSD6ggIN71+32BhKJKrjuOPwgmLUIXnM3p5bk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TAZE9n/7mJ/MLlqPGs75xZWKeYW/XXM9cgwxrg7YHiMAGfSYKnfiXF1asDzfii5ENjCwW6fzHDnKqJ4hHaApu4JUsGIWLygVyh4XAojYeJuF9MRAK+e/GncHPMwC+KaxWtDwXcwef5JDFhhi+GbnA9SiY3BoT2rJDPGj3G/GGmg= 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=WxlRvc/6; arc=none smtp.client-ip=192.55.52.120 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=1705967754; x=1737503754; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BZwHMVdSD6ggIN71+32BhKJKrjuOPwgmLUIXnM3p5bk=; b=WxlRvc/6WLkPPJoCjUb9mYNJUOeN2srXzU+66BXr4e0DuUk9zOvJBhh0 MOILK1+VAc/2YxpAG+4DPTBkLScZPQGiqijk37GsBStug6D8sdrptdphi MkbygWmq3D9x/HRvje5HircyiANrbPagwbIW9zBEYinSPIy5ZuQRQ8pat QQkVL+UHd7MhOqJkF8Knnj/fyzMPN3JhGvcWOuH8LBv/TzYCNpRZ31UBh QwS26jaRnm6SBxS7epCrkvZyotALBVRDqxnJXtoYLTosRiog02DxWPLbX E2DWGeqSjfdoRRStMOOBtvw+3qjgD3ob6fAcLzipVyY5Oi6qjoof6wSjP g==; X-IronPort-AV: E=McAfee;i="6600,9927,10961"; a="400217813" X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="400217813" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 15:55:46 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="27817937" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 15:55:46 -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, Sean Christopherson Subject: [PATCH v18 086/121] KVM: x86: Split core of hypercall emulation to helper function Date: Mon, 22 Jan 2024 15:54:02 -0800 Message-Id: <5134caa55ac3dec33fb2addb5545b52b3b52db02.1705965635.git.isaku.yamahata@intel.com> 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: 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 b1bf9e9b77e8..c29f822dc360 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2186,6 +2186,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 e252372bb633..614d0bccfde2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10033,26 +10033,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; @@ -10061,7 +10050,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; } @@ -10122,18 +10111,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