From patchwork Mon Aug 26 02:22:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Binbin Wu X-Patchwork-Id: 13776961 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (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 E7E7AB652; Mon, 26 Aug 2024 02:21:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724638875; cv=none; b=BMZJWjIUgZnU7QQWNAQmi+iVlIMu+di4WeEB5OceFwPA+rHaoHpQ/zf1vXUsRavorovvG76gAseZplRrP0NJ6jZWj+P5B6MA9pw4DF2nGxzp5bOAoTOM1u6I0UuA3dsUoLiLLOxH95gkGqib+iuSCLtv79GTUxxReB6yD0Hv1/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724638875; c=relaxed/simple; bh=gDkIYgNA465raiQLMsxlKnOu34b4eN4oTZlq6jeFGMI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qMz1wgNIiK7YmrEiEWftCZ2mpHenAmdFYi/YcYr+Rk1wq5+j0nYSnbjrNBLr5+nYmuBfhVH7IhfOJB63qByzNUtIHXk+duLIU5X7KBHitVvtjs4PLNkGdO1hJF3iR3HO4pjotZuqznljSIe7e9h0bTtfrlYaT1yl2hnBPkIVNQY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=XZKfZsJ9; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="XZKfZsJ9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724638874; x=1756174874; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gDkIYgNA465raiQLMsxlKnOu34b4eN4oTZlq6jeFGMI=; b=XZKfZsJ9T8OLO2rPee0h/xaqeOlUSf4gjIh8Ex7n75OwY4G3w2x3zoFg phj4If6OpnY4yXC4sYL62UgU/syjJC4V8/KrxYnEwe9nz8TY7DVzWcd3z pyMt/zwcGeOJPuMEgdhkZ/VV+KkNAHTHXS6FxQXHNQn1gOVcZIVaLZ46z kYjW+zd5Q8NRquAkp/ALf1kSqZofctQmST+fxogmZfSvTZS/J+E+tnFF3 +Xu0xVCJUV1GZ5ORbo9Isv5SzF06a99sgR4yb1raLQNLXRWWkXlsXv5bL g9FssG6KRhgNZ0ICJ5K9oiEqakOnNRhWM297+p9RwmgObo8Kp0hUU8+qE Q==; X-CSE-ConnectionGUID: MfYrrEEXRUiPHqbamV8ZIQ== X-CSE-MsgGUID: aZPJFlIhRKmgFliypejUeg== X-IronPort-AV: E=McAfee;i="6700,10204,11175"; a="23207992" X-IronPort-AV: E=Sophos;i="6.10,176,1719903600"; d="scan'208";a="23207992" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Aug 2024 19:21:14 -0700 X-CSE-ConnectionGUID: dd60saCESimENrwD8sCldg== X-CSE-MsgGUID: tlpBx/czRo2YxGqo6gp2Dw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,176,1719903600"; d="scan'208";a="62336548" Received: from litbin-desktop.sh.intel.com ([10.239.156.93]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Aug 2024 19:21:11 -0700 From: Binbin Wu To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, isaku.yamahata@intel.com, rick.p.edgecombe@intel.com, kai.huang@intel.com, yuan.yao@linux.intel.com, xiaoyao.li@intel.com, binbin.wu@linux.intel.com Subject: [PATCH v3 1/2] KVM: x86: Check hypercall's exit to userspace generically Date: Mon, 26 Aug 2024 10:22:54 +0800 Message-ID: <20240826022255.361406-2-binbin.wu@linux.intel.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240826022255.361406-1-binbin.wu@linux.intel.com> References: <20240826022255.361406-1-binbin.wu@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Check whether a KVM hypercall needs to exit to userspace or not based on hypercall_exit_enabled field of struct kvm_arch. Userspace can request a hypercall to exit to userspace for handling by enable KVM_CAP_EXIT_HYPERCALL and the enabled hypercall will be set in hypercall_exit_enabled. Make the check code generic based on it. Signed-off-by: Binbin Wu Reviewed-by: Kai Huang --- arch/x86/kvm/x86.c | 5 +++-- arch/x86/kvm/x86.h | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 966fb301d44b..e521f14ad2b2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10220,8 +10220,9 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) cpl = kvm_x86_call(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. */ + /* Check !ret first to make sure nr is a valid KVM hypercall. */ + if (!ret && user_exit_on_hypercall(vcpu->kvm, nr)) + /* The hypercall is requested to exit to userspace. */ return 0; if (!op_64_bit) diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 6556a43f1915..bc1a9e080acb 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -561,4 +561,8 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, unsigned int port, void *data, unsigned int count, int in); +static inline bool user_exit_on_hypercall(struct kvm *kvm, unsigned long hc_nr) +{ + return kvm->arch.hypercall_exit_enabled & BIT(hc_nr); +} #endif From patchwork Mon Aug 26 02:22:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Binbin Wu X-Patchwork-Id: 13776962 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (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 8803B1119A; Mon, 26 Aug 2024 02:21:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724638878; cv=none; b=BbxESQQAh2XqgzoEY4dLDvEAoIQ6vNSD7EeGEBfPZ+hNLfHWo7fLRksZH+2CP/IxMh25yZ7URLvivr7wWsjPAIGNeLyLx0PauoBnXVGqPztCSHd49/ijj92Q1lKpPqh8fzWarPMwAj8dEfMWd9YhL1wwhbUrKly205P+op67ly4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724638878; c=relaxed/simple; bh=zwKP1TfpkDPqv5+F3ezCyw1T2c5N24g3EVi+MC5aFhw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UzfSjgQ3J/bsPFaay2kpFt0Z3wWJXOimQlins/BwGwKoGCdMiYEPx55djLSTvPs/bOBLJEoyltqAxgW6uBEDcR2aybk6dWPNhbqLh7Ig/1SB3FSsSe2t4bJ7JjikNSKSyRomKkByj46cpyLJTd1HK88r0qpNopyelO7RVPtplac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=WMT5VopW; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="WMT5VopW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724638876; x=1756174876; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zwKP1TfpkDPqv5+F3ezCyw1T2c5N24g3EVi+MC5aFhw=; b=WMT5VopWjZEBiI7Rc/hDGOEwYKOSe6sOKyDJIFyJ6Js1mYXoxv/Y9Kkd dw4f6ofdb03PtPCiXRswedAqj0AmeekTWvKmXhJ69JA8b6JnSg9QeuQjO drHmrtEQlBMYsjneSmThyA83+ljfUi8rlfxgcwFQ6hWjymleyg9OYj9L+ 84uxIsU1PD8deWmJdiVxlXZUKBs2zjn67aUv1fLpkmBdcuHpe8RtPS52n MpLLCNwUchwvNEgvZBWKn+02YCmJILyjKtkrnMtHqYjjeDfZ1RRquywoT gIpIhm+C3KI3h9v0mCocnf65V+USnizLERJRet0GejE9srt11oURG2hzt Q==; X-CSE-ConnectionGUID: JWE+HA4uQQezK1oA1w80mA== X-CSE-MsgGUID: u5bKLlrgRJ+ITFxuKoAbXQ== X-IronPort-AV: E=McAfee;i="6700,10204,11175"; a="23207997" X-IronPort-AV: E=Sophos;i="6.10,176,1719903600"; d="scan'208";a="23207997" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Aug 2024 19:21:16 -0700 X-CSE-ConnectionGUID: nPl52zwGTYWXV+WuFfQbYA== X-CSE-MsgGUID: J6r/5EVzTqCKsticZajTyA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,176,1719903600"; d="scan'208";a="62336553" Received: from litbin-desktop.sh.intel.com ([10.239.156.93]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Aug 2024 19:21:14 -0700 From: Binbin Wu To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, isaku.yamahata@intel.com, rick.p.edgecombe@intel.com, kai.huang@intel.com, yuan.yao@linux.intel.com, xiaoyao.li@intel.com, binbin.wu@linux.intel.com Subject: [PATCH v3 2/2] KVM: x86: Use user_exit_on_hypercall() instead of opencode Date: Mon, 26 Aug 2024 10:22:55 +0800 Message-ID: <20240826022255.361406-3-binbin.wu@linux.intel.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240826022255.361406-1-binbin.wu@linux.intel.com> References: <20240826022255.361406-1-binbin.wu@linux.intel.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use user_exit_on_hypercall() instead of opencode. No functional change intended. Signed-off-by: Binbin Wu Reviewed-by: Isaku Yamahata Reviewed-by: Kai Huang --- arch/x86/kvm/svm/sev.c | 4 ++-- arch/x86/kvm/x86.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 714c517dd4b7..9b3d55474922 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3637,7 +3637,7 @@ static int snp_begin_psc_msr(struct vcpu_svm *svm, u64 ghcb_msr) return 1; /* resume guest */ } - if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) { + if (!user_exit_on_hypercall(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) { set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); return 1; /* resume guest */ } @@ -3720,7 +3720,7 @@ static int snp_begin_psc(struct vcpu_svm *svm, struct psc_buffer *psc) bool huge; u64 gfn; - if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) { + if (!user_exit_on_hypercall(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) { snp_complete_psc(svm, VMGEXIT_PSC_ERROR_GENERIC); return 1; } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e521f14ad2b2..c41ba387cc8c 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10165,7 +10165,7 @@ unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr, u64 gpa = a0, npages = a1, attrs = a2; ret = -KVM_ENOSYS; - if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) + if (!user_exit_on_hypercall(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) break; if (!PAGE_ALIGNED(gpa) || !npages ||