From patchwork Mon Jul 8 09:21:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Binbin Wu X-Patchwork-Id: 13726313 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (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 B9E835466B; Mon, 8 Jul 2024 09:20:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720430436; cv=none; b=GVoDFxPM4+eOahtKC67DUt/pOTUQT3DbAz2axWSMM7M4jcyHAFg7OXvxnGLvRSY4F94GrKgHGNZZvhVjEGDU8gyqoilC3qKx0XourqyILTxIiopTVlwc6uyrT1qA1hnSpfte3EaXprIyOUC5OSqX2jy9+m3gb0sofd2nshjERbE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720430436; c=relaxed/simple; bh=0i13OMgZrt/MzQQEsSgZlvpUvxqa2W/rOTQGrfLEHxU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rl94eDpsYwszbNfVSSeG5iwRTfzgBT+Ryu4ibR+oDPZSs854pOvtZuFXVzHnnsW8pExMLc8swkhBFxVolvJp5mdkqNM0nCgSBGUpzIWXxXsJbE3J1aEVEOshzcR4ElZ6MeQUtIA1jCfVx0qh/gmvD2aKdv56npIGZ048cMHhL1E= 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=AuGM71Ow; arc=none smtp.client-ip=198.175.65.21 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="AuGM71Ow" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1720430435; x=1751966435; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0i13OMgZrt/MzQQEsSgZlvpUvxqa2W/rOTQGrfLEHxU=; b=AuGM71Ow7pu/QY9CiPwZwS4Dp8ijaYtKzz3tjyVkNieMaYpsH3IqPkkj GtR+en9qthzhSEUFzk6NFbVrBzG8CnEesZOAtLnDvHsWIAHmY5TQnmx75 +0cHaTWHLUB0LLnQjWvCwdgRiXux9JpII+2KT0yVSGmYfMW/9uWSOjYRB s6mHRq/ApC0b3lPDfofw69GdeVvNIqncpVRVDvJExNFxm8jpSd2swZtrA /5Uh0zVXegleZsWrJGiKncPLhrpAmKf6H914LXXdB4NrYxDBtZr5JURUs Kom8Af4k8wJZHCSVG8MJ3KHbRT9AG4GVk2ZjkWFu7KnxvGJdea618Zcoh A==; X-CSE-ConnectionGUID: AsFpkOe4SxCEjvy27p89Ug== X-CSE-MsgGUID: Z3Z9e4pJSgmU6jov4cUlmw== X-IronPort-AV: E=McAfee;i="6700,10204,11126"; a="17577720" X-IronPort-AV: E=Sophos;i="6.09,191,1716274800"; d="scan'208";a="17577720" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jul 2024 02:20:35 -0700 X-CSE-ConnectionGUID: /RgI+kNlRxK6zzi3wt70iA== X-CSE-MsgGUID: 4VJLYcR3RcCag40YbAQ5mw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,191,1716274800"; d="scan'208";a="51866626" Received: from unknown (HELO litbin-desktop.sh.intel.com) ([10.239.156.93]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jul 2024 02:20:32 -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, michael.roth@amd.com, binbin.wu@linux.intel.com Subject: [PATCH 1/2] KVM: x86: Check hypercall's exit to userspace generically Date: Mon, 8 Jul 2024 17:21:49 +0800 Message-ID: <20240708092150.1799371-2-binbin.wu@linux.intel.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240708092150.1799371-1-binbin.wu@linux.intel.com> References: <20240708092150.1799371-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 --- arch/x86/kvm/x86.c | 4 ++-- arch/x86/kvm/x86.h | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 994743266480..f84c1f263e9b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10223,8 +10223,8 @@ 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. */ + if (is_kvm_hc_exit_enabled(vcpu->kvm, nr) && !ret) + /* 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 50596f6f8320..02809a915d72 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -547,4 +547,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 is_kvm_hc_exit_enabled(struct kvm *kvm, unsigned long hc_nr) +{ + return kvm->arch.hypercall_exit_enabled & (1 << hc_nr); +} #endif From patchwork Mon Jul 8 09:21:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Binbin Wu X-Patchwork-Id: 13726314 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (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 78E236F2E9; Mon, 8 Jul 2024 09:20:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720430437; cv=none; b=kX4koq1RgrQ7XTZKM+3m3g0ORvKaEbLdz57jTcmM09E64mmW8v551+Z8mc6Ts0zOf2gjHWug6Uzra2t67eUgNiqn4wmaGPrbeltKbK7bW9V0oRyTYJQaQcIdmwPiZXZIX1y3BXUWj31wlI9eraR73DqUT97vBIpzyERmgnqjEbc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720430437; c=relaxed/simple; bh=gjQUODsu+gw87w2W9bA7NO+17xGjpExlTBwcwJx/TSc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DE2hHCWziTD/lHlMoKbitKvhop46bj50lsQOaeJ+OLljDLjtJKj9SxMdmV51zbFMlkS0TQqpKsfEP8HaApX17ldkS6hd2GhuMq7yhoxDfWvxC0wvLBZZTCvYx5m7En6jkBKyR9OpZX6IFY7D36TCNMUfJyEju8H86fDmqLuy0Cs= 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=g5xk6fUs; arc=none smtp.client-ip=198.175.65.21 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="g5xk6fUs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1720430437; x=1751966437; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gjQUODsu+gw87w2W9bA7NO+17xGjpExlTBwcwJx/TSc=; b=g5xk6fUsYE6tD+ytApm014u0gnHViIqkOPOMZcksax4P6nI2WkZGeQfa WeHD54zz3jbmQs0/x8ur20CPVaRufxmJ6F+7vRX2hswDexH7O++7eCgXK +a5yr1fZObTmCozWvYaZ2kVDviq4T+PzwcCMSrCh5VR4vN33aKmphebm1 aIo4M2lqTNt5tX0rLSBrdgaS6ryyctBv+dam3NDBo9AizQ9JUR67cmco7 cDOMg4r3aqiKvDYR+PvxmjRLusSKLa9J8xxSgjbh6GEhyP5Iq/jkdSOwb nMKvgI5A3oYd0jFDyopSIChTbxqHsRq1dSZPAnDtHbE2etoKWZdlXFo7Z A==; X-CSE-ConnectionGUID: 6vfRm9XURNST3biiyWVuWg== X-CSE-MsgGUID: 6tDBcvzJQBec4V/DlxEFUQ== X-IronPort-AV: E=McAfee;i="6700,10204,11126"; a="17577727" X-IronPort-AV: E=Sophos;i="6.09,191,1716274800"; d="scan'208";a="17577727" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jul 2024 02:20:37 -0700 X-CSE-ConnectionGUID: 55muiDxcRyifFniJIJ/xIw== X-CSE-MsgGUID: 2ockUX/5THOYx3iIzwhPOA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,191,1716274800"; d="scan'208";a="51866645" Received: from unknown (HELO litbin-desktop.sh.intel.com) ([10.239.156.93]) by fmviesa005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jul 2024 02:20:34 -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, michael.roth@amd.com, binbin.wu@linux.intel.com Subject: [PATCH 2/2] KVM: x86: Use is_kvm_hc_exit_enabled() instead of opencode Date: Mon, 8 Jul 2024 17:21:50 +0800 Message-ID: <20240708092150.1799371-3-binbin.wu@linux.intel.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240708092150.1799371-1-binbin.wu@linux.intel.com> References: <20240708092150.1799371-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 is_kvm_hc_exit_enabled() instead of opencode. No functional change intended. Signed-off-by: Binbin Wu --- 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 43a450fb01fd..ed55ff5d1ed5 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3548,7 +3548,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 (!is_kvm_hc_exit_enabled(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) { set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR); return 1; /* resume guest */ } @@ -3631,7 +3631,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 (!is_kvm_hc_exit_enabled(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 f84c1f263e9b..ec9ebc258bf3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10168,7 +10168,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 (!is_kvm_hc_exit_enabled(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) break; if (!PAGE_ALIGNED(gpa) || !npages ||