From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94AFFC433F5 for ; Fri, 27 May 2022 08:04:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348447AbiE0IEI (ORCPT ); Fri, 27 May 2022 04:04:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348457AbiE0ID5 (ORCPT ); Fri, 27 May 2022 04:03:57 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B1B94FD37A for ; Fri, 27 May 2022 01:03:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OoqvQ0dyHR02Jth+XgYVmPsapMfC+8OL3HcKdN8T8MA=; b=ep96kAvQdSkvjws8DbVLngPZJ8GmcEZbuR6oz66KjbXfxlHrlwOKZl6/Fzo+H5HaNNMCBL S4fFPtA0ktiPnCvu6OWCNlDqG45iBguK8Iv21lZOXFVgXccZMx3KuJ2myFT2sFlgcCxUkY /Tqo0v5hszKEkGUbg/QcTjUB5DznEQc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-518-6qWTa95VMhCdNiYpbaFZFg-1; Fri, 27 May 2022 04:03:52 -0400 X-MC-Unique: 6qWTa95VMhCdNiYpbaFZFg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A21661C161AD; Fri, 27 May 2022 08:03:51 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 622412026D64; Fri, 27 May 2022 08:03:42 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 01/22] KVM: arm64: Extend smccc_get_argx() Date: Fri, 27 May 2022 16:02:32 +0800 Message-Id: <20220527080253.1562538-2-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently, there are 3 inline functions to retrieve SMCCC arguments, but the number of arguments is limited to 3. We need to retrieve more SMCCC arguments when SDEI virtualization is supported. This introduces smccc_get_arg(), which accepts @index to indicate the SMCCC argument to be retrieved. Besides, smccc_get_function() also calls into this newly introduced helper. Further more, we also mechanically replace smccc_get_{arg1, arg2, arg3}() using the newly introduced helper. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/hypercalls.c | 4 ++-- arch/arm64/kvm/psci.c | 14 +++++++------- arch/arm64/kvm/pvtime.c | 2 +- arch/arm64/kvm/trng.c | 4 ++-- include/kvm/arm_hypercalls.h | 19 +++++-------------- 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index c9f401fa01a9..69f437f0d7c7 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -41,7 +41,7 @@ static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *= val) * (virtual or physical) with the first argument of the SMCCC * call. In case the identifier is not supported, error out. */ - feature =3D smccc_get_arg1(vcpu); + feature =3D smccc_get_arg(vcpu, 1); switch (feature) { case KVM_PTP_VIRT_COUNTER: cycles =3D systime_snapshot.cycles - vcpu_read_sys_reg(vcpu, CNTVOFF_EL2= ); @@ -137,7 +137,7 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) val[0] =3D ARM_SMCCC_VERSION_1_1; break; case ARM_SMCCC_ARCH_FEATURES_FUNC_ID: - feature =3D smccc_get_arg1(vcpu); + feature =3D smccc_get_arg(vcpu, 1); switch (feature) { case ARM_SMCCC_ARCH_WORKAROUND_1: switch (arm64_get_spectre_v2_state()) { diff --git a/arch/arm64/kvm/psci.c b/arch/arm64/kvm/psci.c index 7fbc4c1b9df0..5394b8a69e10 100644 --- a/arch/arm64/kvm/psci.c +++ b/arch/arm64/kvm/psci.c @@ -64,7 +64,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *so= urce_vcpu) struct kvm_vcpu *vcpu =3D NULL; unsigned long cpu_id; =20 - cpu_id =3D smccc_get_arg1(source_vcpu); + cpu_id =3D smccc_get_arg(source_vcpu, 1); if (!kvm_psci_valid_affinity(source_vcpu, cpu_id)) return PSCI_RET_INVALID_PARAMS; =20 @@ -85,7 +85,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *so= urce_vcpu) =20 reset_state =3D &vcpu->arch.reset_state; =20 - reset_state->pc =3D smccc_get_arg2(source_vcpu); + reset_state->pc =3D smccc_get_arg(source_vcpu, 2); =20 /* Propagate caller endianness */ reset_state->be =3D kvm_vcpu_is_be(source_vcpu); @@ -94,7 +94,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *so= urce_vcpu) * NOTE: We always update r0 (or x0) because for PSCI v0.1 * the general purpose registers are undefined upon CPU_ON. */ - reset_state->r0 =3D smccc_get_arg3(source_vcpu); + reset_state->r0 =3D smccc_get_arg(source_vcpu, 3); =20 WRITE_ONCE(reset_state->reset, true); kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); @@ -121,8 +121,8 @@ static unsigned long kvm_psci_vcpu_affinity_info(struct= kvm_vcpu *vcpu) struct kvm *kvm =3D vcpu->kvm; struct kvm_vcpu *tmp; =20 - target_affinity =3D smccc_get_arg1(vcpu); - lowest_affinity_level =3D smccc_get_arg2(vcpu); + target_affinity =3D smccc_get_arg(vcpu, 1); + lowest_affinity_level =3D smccc_get_arg(vcpu, 2); =20 if (!kvm_psci_valid_affinity(vcpu, target_affinity)) return PSCI_RET_INVALID_PARAMS; @@ -318,7 +318,7 @@ static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32= minor) val =3D minor =3D=3D 0 ? KVM_ARM_PSCI_1_0 : KVM_ARM_PSCI_1_1; break; case PSCI_1_0_FN_PSCI_FEATURES: - arg =3D smccc_get_arg1(vcpu); + arg =3D smccc_get_arg(vcpu, 1); val =3D kvm_psci_check_allowed_function(vcpu, arg); if (val) break; @@ -372,7 +372,7 @@ static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32= minor) fallthrough; case PSCI_1_1_FN64_SYSTEM_RESET2: if (minor >=3D 1) { - arg =3D smccc_get_arg1(vcpu); + arg =3D smccc_get_arg(vcpu, 1); =20 if (arg <=3D PSCI_1_1_RESET_TYPE_SYSTEM_WARM_RESET || arg >=3D PSCI_1_1_RESET_TYPE_VENDOR_START) { diff --git a/arch/arm64/kvm/pvtime.c b/arch/arm64/kvm/pvtime.c index 78a09f7a6637..05e775fc9e8b 100644 --- a/arch/arm64/kvm/pvtime.c +++ b/arch/arm64/kvm/pvtime.c @@ -34,7 +34,7 @@ void kvm_update_stolen_time(struct kvm_vcpu *vcpu) =20 long kvm_hypercall_pv_features(struct kvm_vcpu *vcpu) { - u32 feature =3D smccc_get_arg1(vcpu); + u32 feature =3D smccc_get_arg(vcpu, 1); long val =3D SMCCC_RET_NOT_SUPPORTED; =20 switch (feature) { diff --git a/arch/arm64/kvm/trng.c b/arch/arm64/kvm/trng.c index 99bdd7103c9c..89911b724a26 100644 --- a/arch/arm64/kvm/trng.c +++ b/arch/arm64/kvm/trng.c @@ -24,7 +24,7 @@ static const uuid_t arm_smc_trng_uuid __aligned(4) =3D UU= ID_INIT( static int kvm_trng_do_rnd(struct kvm_vcpu *vcpu, int size) { DECLARE_BITMAP(bits, TRNG_MAX_BITS64); - u32 num_bits =3D smccc_get_arg1(vcpu); + u32 num_bits =3D smccc_get_arg(vcpu, 1); int i; =20 if (num_bits > 3 * size) { @@ -60,7 +60,7 @@ int kvm_trng_call(struct kvm_vcpu *vcpu) val =3D ARM_SMCCC_TRNG_VERSION_1_0; break; case ARM_SMCCC_TRNG_FEATURES: - switch (smccc_get_arg1(vcpu)) { + switch (smccc_get_arg(vcpu, 1)) { case ARM_SMCCC_TRNG_VERSION: case ARM_SMCCC_TRNG_FEATURES: case ARM_SMCCC_TRNG_GET_UUID: diff --git a/include/kvm/arm_hypercalls.h b/include/kvm/arm_hypercalls.h index 1188f116cf4e..ffd06387dfdf 100644 --- a/include/kvm/arm_hypercalls.h +++ b/include/kvm/arm_hypercalls.h @@ -8,24 +8,15 @@ =20 int kvm_hvc_call_handler(struct kvm_vcpu *vcpu); =20 -static inline u32 smccc_get_function(struct kvm_vcpu *vcpu) -{ - return vcpu_get_reg(vcpu, 0); -} - -static inline unsigned long smccc_get_arg1(struct kvm_vcpu *vcpu) +static inline unsigned long smccc_get_arg(struct kvm_vcpu *vcpu, + unsigned char index) { - return vcpu_get_reg(vcpu, 1); + return vcpu_get_reg(vcpu, index); } =20 -static inline unsigned long smccc_get_arg2(struct kvm_vcpu *vcpu) -{ - return vcpu_get_reg(vcpu, 2); -} - -static inline unsigned long smccc_get_arg3(struct kvm_vcpu *vcpu) +static inline u32 smccc_get_function(struct kvm_vcpu *vcpu) { - return vcpu_get_reg(vcpu, 3); + return smccc_get_arg(vcpu, 0); } =20 static inline void smccc_set_retval(struct kvm_vcpu *vcpu, --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A24B7C433EF for ; Fri, 27 May 2022 08:04:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348493AbiE0IEK (ORCPT ); Fri, 27 May 2022 04:04:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348498AbiE0IEI (ORCPT ); Fri, 27 May 2022 04:04:08 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C52E0FF5A7 for ; Fri, 27 May 2022 01:04:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638645; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=X2EjlOHg/Gg2exhClHLrFxLFWlJoGviO0As2qeORgCM=; b=iWZA9vONgn9mR4mx8qdffOV1QTIyYjr685kmZ1luALkdJyTSUFmTGmjRdomcUA2QxNeRoc GdhQool3bNWBOrrdeYlXKJspihplJ5Lfid/iGilLPSUd2c6YTZyHujiS7OH8f4M+iEE6A1 A6waJki2Djl/fOs+YCIsHmVjYWfzuik= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-106-Sp3k1RAaOLm6GTK6n8vYIQ-1; Fri, 27 May 2022 04:04:00 -0400 X-MC-Unique: Sp3k1RAaOLm6GTK6n8vYIQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C43D8858EFE; Fri, 27 May 2022 08:03:59 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9A6B72026D64; Fri, 27 May 2022 08:03:52 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 02/22] KVM: arm64: Route hypercalls based on their owner Date: Fri, 27 May 2022 16:02:33 +0800 Message-Id: <20220527080253.1562538-3-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" kvm_hvc_call_handler() directly handles the incoming hypercall, or routes it based on its (function) ID. kvm_psci_call() becomes the gate keeper to handle the hypercall that can't be handled by any one else. It makes kvm_hvc_call_handler() a bit messy. This reorgnizes the code to route hypercalls to the corresponding handler based on their owners. A hypercall may be handled directly inside the owner's handler or routed to the associated functionality for further handling. By the way, @func_id is renamed to @func, consistent with smccc_get_function(). The only exception is 0.1 PSCI hypercalls. Their owners aren't Standard Secure Service and can't be routed by their owners. So those 0.1 PSCI hypercalls are filtered and routed to kvm_psci_call() in kvm_hvc_call_handler(). Suggested-by: Oliver Upton Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/hypercalls.c | 116 ++++++++++++++++++++++++++---------- include/linux/arm-smccc.h | 5 ++ 2 files changed, 88 insertions(+), 33 deletions(-) diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index 69f437f0d7c7..d66f464e0eb5 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -65,9 +65,9 @@ static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *= val) val[3] =3D lower_32_bits(cycles); } =20 -static bool kvm_hvc_call_default_allowed(u32 func_id) +static bool kvm_hvc_call_default_allowed(u32 func) { - switch (func_id) { + switch (func) { /* * List of function-ids that are not gated with the bitmapped * feature firmware registers, and are to be allowed for @@ -78,26 +78,26 @@ static bool kvm_hvc_call_default_allowed(u32 func_id) return true; default: /* PSCI 0.2 and up is in the 0:0x1f range */ - if (ARM_SMCCC_OWNER_NUM(func_id) =3D=3D ARM_SMCCC_OWNER_STANDARD && - ARM_SMCCC_FUNC_NUM(func_id) <=3D 0x1f) + if (ARM_SMCCC_OWNER_NUM(func) =3D=3D ARM_SMCCC_OWNER_STANDARD && + ARM_SMCCC_FUNC_NUM(func) <=3D 0x1f) return true; =20 /* * KVM's PSCI 0.1 doesn't comply with SMCCC, and has * its own function-id base and range */ - if (func_id >=3D KVM_PSCI_FN(0) && func_id <=3D KVM_PSCI_FN(3)) + if (func >=3D KVM_PSCI_FN(0) && func <=3D KVM_PSCI_FN(3)) return true; =20 return false; } } =20 -static bool kvm_hvc_call_allowed(struct kvm_vcpu *vcpu, u32 func_id) +static bool kvm_hvc_call_allowed(struct kvm_vcpu *vcpu, u32 func) { struct kvm_smccc_features *smccc_feat =3D &vcpu->kvm->arch.smccc_feat; =20 - switch (func_id) { + switch (func) { case ARM_SMCCC_TRNG_VERSION: case ARM_SMCCC_TRNG_FEATURES: case ARM_SMCCC_TRNG_GET_UUID: @@ -117,24 +117,19 @@ static bool kvm_hvc_call_allowed(struct kvm_vcpu *vcp= u, u32 func_id) return test_bit(KVM_REG_ARM_VENDOR_HYP_BIT_PTP, &smccc_feat->vendor_hyp_bmap); default: - return kvm_hvc_call_default_allowed(func_id); + return kvm_hvc_call_default_allowed(func); } } =20 -int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) +static int kvm_hvc_arch(struct kvm_vcpu *vcpu, u32 func) { struct kvm_smccc_features *smccc_feat =3D &vcpu->kvm->arch.smccc_feat; - u32 func_id =3D smccc_get_function(vcpu); - u64 val[4] =3D {SMCCC_RET_NOT_SUPPORTED}; + u64 val =3D SMCCC_RET_NOT_SUPPORTED; u32 feature; - gpa_t gpa; - - if (!kvm_hvc_call_allowed(vcpu, func_id)) - goto out; =20 - switch (func_id) { + switch (func) { case ARM_SMCCC_VERSION_FUNC_ID: - val[0] =3D ARM_SMCCC_VERSION_1_1; + val =3D ARM_SMCCC_VERSION_1_1; break; case ARM_SMCCC_ARCH_FEATURES_FUNC_ID: feature =3D smccc_get_arg(vcpu, 1); @@ -144,10 +139,10 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) case SPECTRE_VULNERABLE: break; case SPECTRE_MITIGATED: - val[0] =3D SMCCC_RET_SUCCESS; + val =3D SMCCC_RET_SUCCESS; break; case SPECTRE_UNAFFECTED: - val[0] =3D SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED; + val =3D SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED; break; } break; @@ -170,7 +165,7 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) break; fallthrough; case SPECTRE_UNAFFECTED: - val[0] =3D SMCCC_RET_NOT_REQUIRED; + val =3D SMCCC_RET_NOT_REQUIRED; break; } break; @@ -179,28 +174,65 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) case SPECTRE_VULNERABLE: break; case SPECTRE_MITIGATED: - val[0] =3D SMCCC_RET_SUCCESS; + val =3D SMCCC_RET_SUCCESS; break; case SPECTRE_UNAFFECTED: - val[0] =3D SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED; + val =3D SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED; break; } break; case ARM_SMCCC_HV_PV_TIME_FEATURES: if (test_bit(KVM_REG_ARM_STD_HYP_BIT_PV_TIME, &smccc_feat->std_hyp_bmap)) - val[0] =3D SMCCC_RET_SUCCESS; + val =3D SMCCC_RET_SUCCESS; break; } break; + } + + smccc_set_retval(vcpu, val, 0, 0, 0); + return 1; +} + +static int kvm_hvc_standard(struct kvm_vcpu *vcpu, u32 func) +{ + switch (ARM_SMCCC_FUNC_NUM(func)) { + case ARM_SMCCC_STANDARD_PSCI_START ... ARM_SMCCC_STANDARD_PSCI_END: + return kvm_psci_call(vcpu); + case ARM_SMCCC_STANDARD_TRNG_START ... ARM_SMCCC_STANDARD_TRNG_END: + return kvm_trng_call(vcpu); + } + + smccc_set_retval(vcpu, SMCCC_RET_NOT_SUPPORTED, 0, 0, 0); + return 1; +} + +static int kvm_hvc_standard_hyp(struct kvm_vcpu *vcpu, u32 func) +{ + u64 val =3D SMCCC_RET_NOT_SUPPORTED; + gpa_t gpa; + + switch (func) { case ARM_SMCCC_HV_PV_TIME_FEATURES: - val[0] =3D kvm_hypercall_pv_features(vcpu); + val =3D kvm_hypercall_pv_features(vcpu); break; case ARM_SMCCC_HV_PV_TIME_ST: gpa =3D kvm_init_stolen_time(vcpu); if (gpa !=3D GPA_INVALID) - val[0] =3D gpa; + val =3D gpa; break; + } + + smccc_set_retval(vcpu, val, 0, 0, 0); + return 1; +} + +static int kvm_hvc_vendor_hyp(struct kvm_vcpu *vcpu, u32 func) +{ + struct kvm_smccc_features *smccc_feat =3D &vcpu->kvm->arch.smccc_feat; + u64 val[4] =3D { SMCCC_RET_NOT_SUPPORTED }; + + switch (func) { case ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID: val[0] =3D ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0; val[1] =3D ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1; @@ -213,18 +245,36 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID: kvm_ptp_get_time(vcpu, val); break; - case ARM_SMCCC_TRNG_VERSION: - case ARM_SMCCC_TRNG_FEATURES: - case ARM_SMCCC_TRNG_GET_UUID: - case ARM_SMCCC_TRNG_RND32: - case ARM_SMCCC_TRNG_RND64: - return kvm_trng_call(vcpu); - default: + } + + smccc_set_retval(vcpu, val[0], val[1], val[2], val[3]); + return 1; +} + +int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) +{ + u32 func =3D smccc_get_function(vcpu); + + if (!kvm_hvc_call_allowed(vcpu, func)) + goto out; + + /* Filter these calls that aren't documented in the specification */ + if (func >=3D KVM_PSCI_FN_CPU_SUSPEND && func <=3D KVM_PSCI_FN_MIGRATE) return kvm_psci_call(vcpu); + + switch (ARM_SMCCC_OWNER_NUM(func)) { + case ARM_SMCCC_OWNER_ARCH: + return kvm_hvc_arch(vcpu, func); + case ARM_SMCCC_OWNER_STANDARD: + return kvm_hvc_standard(vcpu, func); + case ARM_SMCCC_OWNER_STANDARD_HYP: + return kvm_hvc_standard_hyp(vcpu, func); + case ARM_SMCCC_OWNER_VENDOR_HYP: + return kvm_hvc_vendor_hyp(vcpu, func); } =20 out: - smccc_set_retval(vcpu, val[0], val[1], val[2], val[3]); + smccc_set_retval(vcpu, SMCCC_RET_NOT_SUPPORTED, 0, 0, 0); return 1; } =20 diff --git a/include/linux/arm-smccc.h b/include/linux/arm-smccc.h index 220c8c60e021..834fb963a689 100644 --- a/include/linux/arm-smccc.h +++ b/include/linux/arm-smccc.h @@ -55,6 +55,11 @@ #define ARM_SMCCC_OWNER_TRUSTED_OS 50 #define ARM_SMCCC_OWNER_TRUSTED_OS_END 63 =20 +#define ARM_SMCCC_STANDARD_PSCI_START 0x0000 +#define ARM_SMCCC_STANDARD_PSCI_END 0x001f +#define ARM_SMCCC_STANDARD_TRNG_START 0x0050 +#define ARM_SMCCC_STANDARD_TRNG_END 0x005f + #define ARM_SMCCC_FUNC_QUERY_CALL_UID 0xff01 =20 #define ARM_SMCCC_QUIRK_NONE 0 --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CFE3C433F5 for ; Fri, 27 May 2022 08:04:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348644AbiE0IEY (ORCPT ); Fri, 27 May 2022 04:04:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348540AbiE0IEQ (ORCPT ); Fri, 27 May 2022 04:04:16 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E28D5102762 for ; Fri, 27 May 2022 01:04:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638652; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dRMSD2W8voEL7eRWyVgzB/wWa0yFOUK9oL5qAtEgNaU=; b=DEBP0NM6HerXYOFHudiHA3EDDbCCuyXbvn9zVoGeGBymAYEvOUTf2EklGLac3E3yDF5GpF CwqyJnzsQEg3tdLi9rhVaRL+7sL0kVSGleUSMFlW0eaCO/3Kf/cXNqxqy6XYT7Ay1zSNaW 9pkCQKCrZXROhEj/vzx08BELdI0yEiA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-630-OFBKgQpaPbW8NeH7NpvF0Q-1; Fri, 27 May 2022 04:04:07 -0400 X-MC-Unique: OFBKgQpaPbW8NeH7NpvF0Q-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ED8FB185A7B2; Fri, 27 May 2022 08:04:06 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 941352026985; Fri, 27 May 2022 08:04:00 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 03/22] KVM: arm64: Add SDEI virtualization infrastructure Date: Fri, 27 May 2022 16:02:34 +0800 Message-Id: <20220527080253.1562538-4-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Software Delegated Exception Interface (SDEI) provides a mechanism for registering and servicing system events, as defined by ARM DEN0054C specification. One of these events will be used by Asynchronous Page Fault (Async PF) to deliver notifications from host to guest. The events are classified into shared and private ones according to their scopes. The shared events are system or VM scoped, but the private events are CPU or VCPU scoped. The shared events can be registered, enabled, unregistered and reset through hypercalls issued from any VCPU. However, the private events are registered, enabled, unregistered and reset on the calling VCPU through hypercalls. Besides, the events are also classified into critical and normal events according their priority. During event delivery and handling, the normal event can be preempted by another critical event, but not in reverse way. The critical event can't be preempted by another normal event. In the implementation, both shared event and critical event aren't supported. It means all supported events are private and normal. There are several objects introduced to manage events and their states. * kvm_sdei_event_handler SDEI event handler, which is provided through EVENT_REGISTER hypercall, is called when the SDEI event is delivered from host to guest. * kvm_sdei_event_context The saved (preempted) context when SDEI event is delivered for handling. * kvm_sdei_vcpu SDEI events and their states. The flag KVM_ARM64_SDEI_MASKED is set to mask vCPU from SDEI events by default. Besides, SDEI_NOT_SUPPORTED is returned for all hypercalls at present. The hypercalls will be supported one by one in subsequent patches. Link: Link: https://developer.arm.com/documentation/den0054/latest Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/include/asm/kvm_host.h | 2 + arch/arm64/include/asm/kvm_sdei.h | 70 +++++++++++++++++++++++++++++++ arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 4 ++ arch/arm64/kvm/hypercalls.c | 3 ++ arch/arm64/kvm/sdei.c | 59 ++++++++++++++++++++++++++ include/linux/arm-smccc.h | 2 + 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/kvm_sdei.h create mode 100644 arch/arm64/kvm/sdei.c diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm= _host.h index 47a1e25e25bb..c9d053c2569e 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -366,6 +366,7 @@ struct kvm_vcpu_arch { * Anything that is not used directly from assembly code goes * here. */ + struct kvm_sdei_vcpu *sdei; =20 /* * Guest registers we preserve during guest debugging. @@ -466,6 +467,7 @@ struct kvm_vcpu_arch { #define KVM_ARM64_ON_UNSUPPORTED_CPU (1 << 15) /* Physical CPU not in supp= orted_cpus */ #define KVM_ARM64_HOST_SME_ENABLED (1 << 16) /* SME enabled for EL0 */ #define KVM_ARM64_WFIT (1 << 17) /* WFIT instruction trapped */ +#define KVM_ARM64_SDEI_MASKED (1 << 18) /* SDEI is masked */ =20 #define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \ KVM_GUESTDBG_USE_SW_BP | \ diff --git a/arch/arm64/include/asm/kvm_sdei.h b/arch/arm64/include/asm/kvm= _sdei.h new file mode 100644 index 000000000000..609338b17478 --- /dev/null +++ b/arch/arm64/include/asm/kvm_sdei.h @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Definitions of various KVM SDEI events. + * + * Copyright (C) 2022 Red Hat, Inc. + * + * Author(s): Gavin Shan + */ + +#ifndef __ARM64_KVM_SDEI_H__ +#define __ARM64_KVM_SDEI_H__ + +#include +#include +#include + +enum { + KVM_NR_SDEI_EVENTS, +}; + +/** + * struct kvm_sdei_event_handler - SDEI event handler + * + * @ep_addr: Address of SDEI event handler + * @ep_arg: Argument passed to SDEI event handler + */ +struct kvm_sdei_event_handler { + unsigned long ep_addr; + unsigned long ep_arg; +}; + +/** + * struct kvm_sdei_event_context - Saved context during SDEI event delivery + * + * @pc: PC of the saved context + * @pstate: PSTATE of the saved context + * @regs: x0 to x17 of the saved context + */ +struct kvm_sdei_event_context { + unsigned long pc; + unsigned long pstate; + unsigned long regs[18]; +}; + +/** + * struct kvm_sdei_vcpu - SDEI events and their sates + * + * @registered: Bitmap of registration states for SDEI events + * @enabled: Bitmap of enablement states for SDEI events + * @running: Bitmap of running states for SDEI events + * @pending: Bitmap of pending states for SDEI events + * @handlers: Array of SDEI event handlers + * @ctxt: Saved context during SDEI event delivery + */ +struct kvm_sdei_vcpu { + unsigned long registered; + unsigned long enabled; + unsigned long running; + unsigned long pending; + + struct kvm_sdei_event_handler handlers[KVM_NR_SDEI_EVENTS]; + struct kvm_sdei_event_context ctxt; +}; + +/* APIs */ +int kvm_sdei_call(struct kvm_vcpu *vcpu); +void kvm_sdei_create_vcpu(struct kvm_vcpu *vcpu); +void kvm_sdei_destroy_vcpu(struct kvm_vcpu *vcpu); + +#endif /* __ARM64_KVM_SDEI_H__ */ diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index aa127ae9f675..26e9d62875a8 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -14,7 +14,7 @@ kvm-y +=3D arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.= o \ inject_fault.o va_layout.o handle_exit.o \ guest.o debug.o reset.o sys_regs.o \ vgic-sys-reg-v3.o fpsimd.o pkvm.o \ - arch_timer.o trng.o vmid.o \ + arch_timer.o trng.o vmid.o sdei.o \ vgic/vgic.o vgic/vgic-init.o \ vgic/vgic-irqfd.o vgic/vgic-v2.o \ vgic/vgic-v3.o vgic/vgic-v4.o \ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 400bb0fe2745..e9516f951e7b 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -38,6 +38,7 @@ #include #include #include +#include #include =20 #include @@ -337,6 +338,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) =20 kvm_arm_pvtime_vcpu_init(&vcpu->arch); =20 + kvm_sdei_create_vcpu(vcpu); + vcpu->arch.hw_mmu =3D &vcpu->kvm->arch.mmu; =20 err =3D kvm_vgic_vcpu_init(vcpu); @@ -358,6 +361,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); kvm_timer_vcpu_terminate(vcpu); kvm_pmu_vcpu_destroy(vcpu); + kvm_sdei_destroy_vcpu(vcpu); =20 kvm_arm_vcpu_destroy(vcpu); } diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index d66f464e0eb5..88561ac47301 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -5,6 +5,7 @@ #include =20 #include +#include =20 #include #include @@ -199,6 +200,8 @@ static int kvm_hvc_standard(struct kvm_vcpu *vcpu, u32 = func) switch (ARM_SMCCC_FUNC_NUM(func)) { case ARM_SMCCC_STANDARD_PSCI_START ... ARM_SMCCC_STANDARD_PSCI_END: return kvm_psci_call(vcpu); + case ARM_SMCCC_STANDARD_SDEI_START ... ARM_SMCCC_STANDARD_SDEI_END: + return kvm_sdei_call(vcpu); case ARM_SMCCC_STANDARD_TRNG_START ... ARM_SMCCC_STANDARD_TRNG_END: return kvm_trng_call(vcpu); } diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c new file mode 100644 index 000000000000..0c4415fcad16 --- /dev/null +++ b/arch/arm64/kvm/sdei.c @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * SDEI virtualization support. + * + * Copyright (C) 2022 Red Hat, Inc. + * + * Author(s): Gavin Shan + */ + +#include +#include +#include +#include +#include + +int kvm_sdei_call(struct kvm_vcpu *vcpu) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + u32 func =3D smccc_get_function(vcpu); + bool has_result =3D true; + unsigned long ret; + + /* No return value for COMPLETE or COMPLETE_AND_RESUME */ + if (func =3D=3D SDEI_1_0_FN_SDEI_EVENT_COMPLETE || + func =3D=3D SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME) + has_result =3D false; + + if (!vsdei) { + ret =3D SDEI_NOT_SUPPORTED; + goto out; + } + + switch (func) { + default: + ret =3D SDEI_NOT_SUPPORTED; + } + +out: + if (has_result) + smccc_set_retval(vcpu, ret, 0, 0, 0); + + return 1; +} + +void kvm_sdei_create_vcpu(struct kvm_vcpu *vcpu) +{ + struct kvm_sdei_vcpu *vsdei; + + vcpu->arch.sdei =3D kzalloc(sizeof(*vsdei), GFP_KERNEL_ACCOUNT); + if (vcpu->arch.sdei) + vcpu->arch.flags |=3D KVM_ARM64_SDEI_MASKED; +} + +void kvm_sdei_destroy_vcpu(struct kvm_vcpu *vcpu) +{ + vcpu->arch.flags &=3D ~KVM_ARM64_SDEI_MASKED; + kfree(vcpu->arch.sdei); + vcpu->arch.sdei =3D NULL; +} diff --git a/include/linux/arm-smccc.h b/include/linux/arm-smccc.h index 834fb963a689..18159a9c6703 100644 --- a/include/linux/arm-smccc.h +++ b/include/linux/arm-smccc.h @@ -57,6 +57,8 @@ =20 #define ARM_SMCCC_STANDARD_PSCI_START 0x0000 #define ARM_SMCCC_STANDARD_PSCI_END 0x001f +#define ARM_SMCCC_STANDARD_SDEI_START 0x0020 +#define ARM_SMCCC_STANDARD_SDEI_END 0x003f #define ARM_SMCCC_STANDARD_TRNG_START 0x0050 #define ARM_SMCCC_STANDARD_TRNG_END 0x005f =20 --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA244C433F5 for ; Fri, 27 May 2022 08:04:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230489AbiE0IEc (ORCPT ); Fri, 27 May 2022 04:04:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348519AbiE0IEX (ORCPT ); Fri, 27 May 2022 04:04:23 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AC573FF5B2 for ; Fri, 27 May 2022 01:04:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638658; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=P4jpm8XSirJ1238sxZZHFV9+DDVhOPJcfnDADqydjXI=; b=NpxKsnCvkGm1GG+2dOkHIGJ5qexVpAk5pFDWQHNKMy1nO7Qbs0m6H6iSpkS/xumr9fIqnG hgHTuPxoCI/y5A3Ak2bk6DU9fQj+ctgS02GoVVyqhGd5iyzmPe9KYyWuT3YDqOH5QgCAFt 2ykSTvLgcMXkmJQyiVwigN67TNgEh24= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-5-M6zTRbdCOiKQyGV6iliAuA-1; Fri, 27 May 2022 04:04:14 -0400 X-MC-Unique: M6zTRbdCOiKQyGV6iliAuA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 04D042949BAE; Fri, 27 May 2022 08:04:14 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A90A42026D64; Fri, 27 May 2022 08:04:07 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 04/22] KVM: arm64: Support EVENT_REGISTER hypercall Date: Fri, 27 May 2022 16:02:35 +0800 Message-Id: <20220527080253.1562538-5-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports EVENT_REGISTER hypercall, which is used by guest to register event. The event won't be raised until it's registered and enabled. There are several cases where the request is injected: * The event isn't supported by KVM * The event has been registered or pending for unregistration * The reserved bits or relative mode are set in @flags argument Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/sdei.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index 0c4415fcad16..40c2d585470d 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -13,6 +13,34 @@ #include #include =20 +static unsigned long event_register(struct kvm_vcpu *vcpu) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + unsigned int num =3D smccc_get_arg(vcpu, 1); + unsigned long flags =3D smccc_get_arg(vcpu, 4); + + if (num >=3D KVM_NR_SDEI_EVENTS) + return SDEI_INVALID_PARAMETERS; + + /* Reject if the reserved bits or relative mode are set */ + if (flags & ~0x1UL) + return SDEI_INVALID_PARAMETERS; + + /* + * Reject if the event has been registered or pending for + * unregistration. + */ + if (test_bit(num, &vsdei->registered) || + test_bit(num, &vsdei->running)) + return SDEI_DENIED; + + vsdei->handlers[num].ep_addr =3D smccc_get_arg(vcpu, 2); + vsdei->handlers[num].ep_arg =3D smccc_get_arg(vcpu, 3); + set_bit(num, &vsdei->registered); + + return SDEI_SUCCESS; +} + int kvm_sdei_call(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -31,6 +59,9 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) } =20 switch (func) { + case SDEI_1_0_FN_SDEI_EVENT_REGISTER: + ret =3D event_register(vcpu); + break; default: ret =3D SDEI_NOT_SUPPORTED; } --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F164C433FE for ; Fri, 27 May 2022 08:05:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348673AbiE0IEz (ORCPT ); Fri, 27 May 2022 04:04:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348803AbiE0IEo (ORCPT ); Fri, 27 May 2022 04:04:44 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EDFA1FD37A for ; Fri, 27 May 2022 01:04:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638681; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tSmHChhkeGhveeNkT0xXj48o/ZM3uoGgnCvlBhi+Jhs=; b=d1qOsHqUhIPPTLPRx02nn35nb+HaeqBhC0XSRGZ5xCgsG15tWQO2pRCzyZpC3DQgjepPG4 WVvuK7NL0H+mSYrflKNdVN1NXKFtJoqMKpBuUUMJBKovn6Ulz+zpOLLoooodFF3/08Gr8N GPwNaH/tFx7c97oM1VwnMg+QWHCbBQg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-255-0jWaW6A2OoOHPARON6hnyQ-1; Fri, 27 May 2022 04:04:25 -0400 X-MC-Unique: 0jWaW6A2OoOHPARON6hnyQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F1E26858F0C; Fri, 27 May 2022 08:04:24 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C3DA62026D64; Fri, 27 May 2022 08:04:14 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 05/22] KVM: arm64: Support EVENT_{ENABLE, DISABLE} hypercall Date: Fri, 27 May 2022 16:02:36 +0800 Message-Id: <20220527080253.1562538-6-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports EVENT_{ENABLE, DISABLE} hypercall. The event can be pending, but never be delivered to guest until it's enabled. * The request is rejected if the event isn't registered or pending for unregistration. * When the event is enabled, KVM_REQ_SDEI is set to so that the event can be delivered to guest, if it has been pending. The delivery logic will be implemented in subsequent patches. For now, it's nothing other than a flag. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/sdei.c | 33 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm= _host.h index c9d053c2569e..79a0043a908c 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -47,6 +47,7 @@ #define KVM_REQ_RELOAD_GICv4 KVM_ARCH_REQ(4) #define KVM_REQ_RELOAD_PMU KVM_ARCH_REQ(5) #define KVM_REQ_SUSPEND KVM_ARCH_REQ(6) +#define KVM_REQ_SDEI KVM_ARCH_REQ(7) =20 #define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |= \ KVM_DIRTY_LOG_INITIALLY_SET) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index 40c2d585470d..a4046e7b21d8 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -41,6 +41,33 @@ static unsigned long event_register(struct kvm_vcpu *vcp= u) return SDEI_SUCCESS; } =20 +static unsigned long event_enable(struct kvm_vcpu *vcpu, bool enable) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + unsigned int num =3D smccc_get_arg(vcpu, 1); + + if (num >=3D KVM_NR_SDEI_EVENTS) + return SDEI_INVALID_PARAMETERS; + + /* + * Reject if the event hasn't been registered or pending + * for unregistration. + */ + if (!test_bit(num, &vsdei->registered)) + return SDEI_DENIED; + + if (enable) { + set_bit(num, &vsdei->enabled); + if (!(vcpu->arch.flags & KVM_ARM64_SDEI_MASKED) && + test_bit(num, &vsdei->pending)) + kvm_make_request(KVM_REQ_SDEI, vcpu); + } else { + clear_bit(num, &vsdei->enabled); + } + + return SDEI_SUCCESS; +} + int kvm_sdei_call(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -62,6 +89,12 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) case SDEI_1_0_FN_SDEI_EVENT_REGISTER: ret =3D event_register(vcpu); break; + case SDEI_1_0_FN_SDEI_EVENT_ENABLE: + ret =3D event_enable(vcpu, true); + break; + case SDEI_1_0_FN_SDEI_EVENT_DISABLE: + ret =3D event_enable(vcpu, false); + break; default: ret =3D SDEI_NOT_SUPPORTED; } --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C04EFC433EF for ; Fri, 27 May 2022 08:04:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348233AbiE0IEs (ORCPT ); Fri, 27 May 2022 04:04:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348793AbiE0IEn (ORCPT ); Fri, 27 May 2022 04:04:43 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C621910274A for ; Fri, 27 May 2022 01:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638679; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kcK/t9iW1JR5lhmsZM8idJt1ZlBlpeTLocByfzKuNnU=; b=VN0KBmPT1IVEUIL6fWSzIlu1do9NZL1l+Li5sOKAjhxAckcgd4QrgE38e6lprqmmoEsMnt 85cJvAHWKg5Diwtx+3MBGoKw5GYLvXoOIjsbruAPS0Izz96RRWj4ukHcMOZ+v50pifYwg/ 2+yovn46II/LX/ltSva4Rz+3lkrU8VM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-125-RzrZnFTLOkWOtiHCvt0Maw-1; Fri, 27 May 2022 04:04:34 -0400 X-MC-Unique: RzrZnFTLOkWOtiHCvt0Maw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CFC77187506C; Fri, 27 May 2022 08:04:33 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C23382024CBB; Fri, 27 May 2022 08:04:24 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 06/22] KVM: arm64: Support EVENT_CONTEXT hypercall Date: Fri, 27 May 2022 16:02:37 +0800 Message-Id: <20220527080253.1562538-7-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports EVENT_CONTEXT hypercall. It's called inside the event handler to retrieve the specified register in the saved or preempted context. * The request is rejected if no running event handler exists or the parameter ID is out of range. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/sdei.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index a4046e7b21d8..2fec2dcd02b0 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -44,7 +44,7 @@ static unsigned long event_register(struct kvm_vcpu *vcpu) static unsigned long event_enable(struct kvm_vcpu *vcpu, bool enable) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; - unsigned int num =3D smccc_get_arg(vcpu, 1); + int num =3D smccc_get_arg(vcpu, 1); =20 if (num >=3D KVM_NR_SDEI_EVENTS) return SDEI_INVALID_PARAMETERS; @@ -68,6 +68,23 @@ static unsigned long event_enable(struct kvm_vcpu *vcpu,= bool enable) return SDEI_SUCCESS; } =20 +static unsigned long event_context(struct kvm_vcpu *vcpu) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + struct kvm_sdei_event_context *ctxt =3D &vsdei->ctxt; + unsigned int param_id =3D smccc_get_arg(vcpu, 1); + + /* Reject if event handler isn't running */ + if (!vsdei->running) + return SDEI_DENIED; + + /* Reject if the parameter ID is out of range */ + if (param_id >=3D ARRAY_SIZE(ctxt->regs)) + return SDEI_INVALID_PARAMETERS; + + return ctxt->regs[param_id]; +} + int kvm_sdei_call(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -95,6 +112,9 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) case SDEI_1_0_FN_SDEI_EVENT_DISABLE: ret =3D event_enable(vcpu, false); break; + case SDEI_1_0_FN_SDEI_EVENT_CONTEXT: + ret =3D event_context(vcpu); + break; default: ret =3D SDEI_NOT_SUPPORTED; } --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6195FC433EF for ; Fri, 27 May 2022 08:05:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349053AbiE0IFE (ORCPT ); Fri, 27 May 2022 04:05:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244703AbiE0IEs (ORCPT ); Fri, 27 May 2022 04:04:48 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A3417FF592 for ; Fri, 27 May 2022 01:04:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638686; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=329kwVYlUvTHO0LeA/plITFrudtBaaBxKIDFkDJlGH8=; b=FkVvh/Cz+klKl0bBbWL+d8VwyMJLgHG+2q8s68T6StDD4BqxjS3Eaf/NMrgsc0V3eFzhdJ uwjvQ83cOOJp90xZjJ7+xOPcW4BfV8+NA1x6Os9wFfe/ElrMS/0R1IxGpiRexIOOFOwrD6 btkPEgZW2FF4d+n6uhe4odQI+UKlpV0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-216-dMEdTzJWOrCpZMOLi9hD9A-1; Fri, 27 May 2022 04:04:41 -0400 X-MC-Unique: dMEdTzJWOrCpZMOLi9hD9A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D2FAD811E76; Fri, 27 May 2022 08:04:40 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 87E762026D64; Fri, 27 May 2022 08:04:34 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 07/22] KVM: arm64: Support EVENT_UNREGISTER hypercall Date: Fri, 27 May 2022 16:02:38 +0800 Message-Id: <20220527080253.1562538-8-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports EVENT_UNREGISTER hypercall. No event will be delivered to guest after it's unregistered. * Reject if the event hasn't been registered. * Return SDEI_PENDING if the event handler is running. * The event is disabled automatically on unregistration. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/sdei.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index 2fec2dcd02b0..b44ab302732d 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -85,6 +85,39 @@ static unsigned long event_context(struct kvm_vcpu *vcpu) return ctxt->regs[param_id]; } =20 +static unsigned long event_unregister(struct kvm_vcpu *vcpu) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + unsigned int num =3D smccc_get_arg(vcpu, 1); + + if (num >=3D KVM_NR_SDEI_EVENTS) + return SDEI_INVALID_PARAMETERS; + + /* + * Reject if the event isn't registered. It's allowed to + * unregister event which has been pending for that. + */ + if (!test_bit(num, &vsdei->registered)) { + if (test_bit(num, &vsdei->running)) + return SDEI_PENDING; + else + return SDEI_DENIED; + } + + /* + * The event is disabled automatically on unregistration, even + * pending for that. + */ + clear_bit(num, &vsdei->enabled); + clear_bit(num, &vsdei->registered); + + /* Pending for unreigstration if the event handler is running */ + if (test_bit(num, &vsdei->running)) + return SDEI_PENDING; + + return SDEI_SUCCESS; +} + int kvm_sdei_call(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -115,6 +148,9 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) case SDEI_1_0_FN_SDEI_EVENT_CONTEXT: ret =3D event_context(vcpu); break; + case SDEI_1_0_FN_SDEI_EVENT_UNREGISTER: + ret =3D event_unregister(vcpu); + break; default: ret =3D SDEI_NOT_SUPPORTED; } --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF45BC433F5 for ; Fri, 27 May 2022 08:05:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349033AbiE0IFi (ORCPT ); Fri, 27 May 2022 04:05:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240023AbiE0IE4 (ORCPT ); Fri, 27 May 2022 04:04:56 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5367D10275F for ; Fri, 27 May 2022 01:04:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638693; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Two26LWqm11I3Z4UV+vVjj4R+s2XokxqwlwfmiyoUok=; b=ALK2nO4gTesCV0WW7g9YkrWjD/qQUICeSDJ0jDJizi8r9+WEo54YXTqntYyXdtOmOZ9G2r y+sfi6XuEzbWw73GZtprBEYe9j8l9z4vbiuf8cwyOJzqvpUFWh+hndumpH0y9a33ITm2Ta fhqHPwOYYcbRVbxmAJ4Wr7Q4jk1orU0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-122-3mnuTZQHPOCrMuMea_WNNw-1; Fri, 27 May 2022 04:04:49 -0400 X-MC-Unique: 3mnuTZQHPOCrMuMea_WNNw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 530E780A0B5; Fri, 27 May 2022 08:04:48 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A35432026D64; Fri, 27 May 2022 08:04:41 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 08/22] KVM: arm64: Support EVENT_STATUS hypercall Date: Fri, 27 May 2022 16:02:39 +0800 Message-Id: <20220527080253.1562538-9-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports EVENT_STATUS hypercall. It's used to retrieve the status about the specified event. A bitmap is returned to represent the event status, which includes state of registration, enablement, and running. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/sdei.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index b44ab302732d..377341f229da 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -118,6 +118,25 @@ static unsigned long event_unregister(struct kvm_vcpu = *vcpu) return SDEI_SUCCESS; } =20 +static unsigned long event_status(struct kvm_vcpu *vcpu) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + unsigned int num =3D smccc_get_arg(vcpu, 1); + unsigned long ret =3D 0; + + if (num >=3D KVM_NR_SDEI_EVENTS) + return SDEI_INVALID_PARAMETERS; + + if (test_bit(num, &vsdei->registered)) + ret |=3D (1UL << SDEI_EVENT_STATUS_REGISTERED); + if (test_bit(num, &vsdei->enabled)) + ret |=3D (1UL << SDEI_EVENT_STATUS_ENABLED); + if (test_bit(num, &vsdei->running)) + ret |=3D (1UL << SDEI_EVENT_STATUS_RUNNING); + + return ret; +} + int kvm_sdei_call(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -151,6 +170,9 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) case SDEI_1_0_FN_SDEI_EVENT_UNREGISTER: ret =3D event_unregister(vcpu); break; + case SDEI_1_0_FN_SDEI_EVENT_STATUS: + ret =3D event_status(vcpu); + break; default: ret =3D SDEI_NOT_SUPPORTED; } --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59A2BC433F5 for ; Fri, 27 May 2022 08:05:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348710AbiE0IFf (ORCPT ); Fri, 27 May 2022 04:05:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349033AbiE0IFE (ORCPT ); Fri, 27 May 2022 04:05:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 52FC5FF583 for ; Fri, 27 May 2022 01:05:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638702; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F48pBQPoVsgMuE4olcTfKdFglqqUe1/4WW/F05DCMx0=; b=WGtgt3cIJhY38P09t7V9AoKTBg1AQ88u+ifJGOY3UADMgOuozHm5jMMPFpDRI5WuGWHuTe AytS+vKQ7/lHEJVDZSD7LWO8HIU1Xc3V0B9AKC+ciWV816CKTTaM0J5IRTpO/Qor0OG4mT azLM7Rv5jEOPsrd7okxkmFcWKROlZmE= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-392-D069rGygNjaL8Jre3L4_gg-1; Fri, 27 May 2022 04:04:57 -0400 X-MC-Unique: D069rGygNjaL8Jre3L4_gg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 75C7E3C0ED5D; Fri, 27 May 2022 08:04:56 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 239ED202699A; Fri, 27 May 2022 08:04:48 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 09/22] KVM: arm64: Support EVENT_GET_INFO hypercall Date: Fri, 27 May 2022 16:02:40 +0800 Message-Id: <20220527080253.1562538-10-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports EVENT_GET_INFO hypercall. It's used to retrieve information about the event: type, signaled, priority, routing mode and affinity. All supported events are private and have normal priority. Besides, all supported events can be signaled from software. It means we just return fixed value for type/signaled/priority. As the routing mode and affinity are only available for the shared event, which is unsupported. So SDEI_INVALID_PARAMETERS is returned when they're requested. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/sdei.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index 377341f229da..cea523418c75 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -137,6 +137,38 @@ static unsigned long event_status(struct kvm_vcpu *vcp= u) return ret; } =20 +static unsigned long event_info(struct kvm_vcpu *vcpu) +{ + unsigned int num =3D smccc_get_arg(vcpu, 1); + unsigned int info =3D smccc_get_arg(vcpu, 2); + unsigned long ret =3D 0; + + if (num >=3D KVM_NR_SDEI_EVENTS) + return SDEI_INVALID_PARAMETERS; + + /* + * All supported events are private and have normal priority. + * Besides, all supported events can be signaled by software + */ + switch (info) { + case SDEI_EVENT_INFO_EV_TYPE: + ret =3D SDEI_EVENT_TYPE_PRIVATE; + break; + case SDEI_EVENT_INFO_EV_SIGNALED: + ret =3D 1; + break; + case SDEI_EVENT_INFO_EV_PRIORITY: + ret =3D SDEI_EVENT_PRIORITY_NORMAL; + break; + case SDEI_EVENT_INFO_EV_ROUTING_MODE: + case SDEI_EVENT_INFO_EV_ROUTING_AFF: + default: + ret =3D SDEI_INVALID_PARAMETERS; + } + + return ret; +} + int kvm_sdei_call(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -173,6 +205,9 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) case SDEI_1_0_FN_SDEI_EVENT_STATUS: ret =3D event_status(vcpu); break; + case SDEI_1_0_FN_SDEI_EVENT_GET_INFO: + ret =3D event_info(vcpu); + break; default: ret =3D SDEI_NOT_SUPPORTED; } --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8006C433F5 for ; Fri, 27 May 2022 08:05:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344493AbiE0IFm (ORCPT ); Fri, 27 May 2022 04:05:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349272AbiE0IFL (ORCPT ); Fri, 27 May 2022 04:05:11 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7C510FD359 for ; Fri, 27 May 2022 01:05:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638709; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0BNXaqWA1otDLXqlPZtOcrW0E3FZ4yYhwe+GYceYi8I=; b=bG71kPMmuBxipT/6fQIisVtCDxwpToSEuzKEkF7t0pEO7p4y1u4sUMc4KHXzPuIaZLHzl8 xY7G3jyTHyyo5HGO2vNai2ntfJIZmKvIkeuXAL/Nvw1zrANCzIxsH3ZhEHYvjqmKZhAGjE rsyUraHmeKzfgUqQdPP+PRsVO8pEEmM= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-37-NQFO-vD3OKS2WgVUIQ3FNg-1; Fri, 27 May 2022 04:05:04 -0400 X-MC-Unique: NQFO-vD3OKS2WgVUIQ3FNg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 211233804506; Fri, 27 May 2022 08:05:04 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 391E42026D64; Fri, 27 May 2022 08:04:56 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 10/22] KVM: arm64: Support PE_{MASK, UNMASK} hypercall Date: Fri, 27 May 2022 16:02:41 +0800 Message-Id: <20220527080253.1562538-11-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports PE_{MASK, UNMASK} hypercall. When PE_UNMASK is called, KVM_REQ_SDEI request becomes pending if there are any events need to be delivered. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/sdei.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index cea523418c75..6afa49b4f3c7 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -169,6 +169,21 @@ static unsigned long event_info(struct kvm_vcpu *vcpu) return ret; } =20 +static unsigned long pe_mask(struct kvm_vcpu *vcpu, bool mask) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + + if (mask) { + vcpu->arch.flags |=3D KVM_ARM64_SDEI_MASKED; + } else { + vcpu->arch.flags &=3D ~KVM_ARM64_SDEI_MASKED; + if (vsdei->pending) + kvm_make_request(KVM_REQ_SDEI, vcpu); + } + + return SDEI_SUCCESS; +} + int kvm_sdei_call(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -208,6 +223,12 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) case SDEI_1_0_FN_SDEI_EVENT_GET_INFO: ret =3D event_info(vcpu); break; + case SDEI_1_0_FN_SDEI_PE_MASK: + ret =3D pe_mask(vcpu, true); + break; + case SDEI_1_0_FN_SDEI_PE_UNMASK: + ret =3D pe_mask(vcpu, false); + break; default: ret =3D SDEI_NOT_SUPPORTED; } --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF5FEC433EF for ; Fri, 27 May 2022 08:05:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349252AbiE0IFt (ORCPT ); Fri, 27 May 2022 04:05:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349538AbiE0IFV (ORCPT ); Fri, 27 May 2022 04:05:21 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AA36B43EDE for ; Fri, 27 May 2022 01:05:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=990zi+sfu+1KWMZVgqplhrYTyhVc+VOJOjIr/irisRU=; b=H3xob5f9duKrdRRSYSSjLDG2P55A9+isW/I/Ch1SG1tUNIlkCXQzGrFsj89wHWIWg1gLlh miyNmPmvYutRAdxbi3fEaxAaqdXj2+D1s1XsrXF3Pf3jcy1e6UamUIESsVRvVckALD8xaB A0+4A+rKFLtsgjGYm8MgUMaoeOiCDfY= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-12-TbYShRdBOM6hGLgfZR7QJg-1; Fri, 27 May 2022 04:05:12 -0400 X-MC-Unique: TbYShRdBOM6hGLgfZR7QJg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C116B1C161AE; Fri, 27 May 2022 08:05:11 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E4D2A2026D64; Fri, 27 May 2022 08:05:04 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 11/22] KVM: arm64: Support {PRIVATE, SHARED}_RESET hypercall Date: Fri, 27 May 2022 16:02:42 +0800 Message-Id: <20220527080253.1562538-12-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports {PRIVATE, SHARED}_RESET hypercall. There is nothing to do if we're going to reset the shared events, which are not supported. For the private events, their registered and enabled state are cleared. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/sdei.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index 6afa49b4f3c7..42ba6f97b168 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -184,6 +184,26 @@ static unsigned long pe_mask(struct kvm_vcpu *vcpu, bo= ol mask) return SDEI_SUCCESS; } =20 +static unsigned long event_reset(struct kvm_vcpu *vcpu, bool private) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + unsigned int num; + + /* + * Nothing to do if we're going to reset the shared events, + * which are unsupported. + */ + if (!private) + return SDEI_SUCCESS; + + for (num =3D 0; num < KVM_NR_SDEI_EVENTS; num++) { + clear_bit(num, &vsdei->registered); + clear_bit(num, &vsdei->enabled); + } + + return SDEI_SUCCESS; +} + int kvm_sdei_call(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -229,6 +249,12 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) case SDEI_1_0_FN_SDEI_PE_UNMASK: ret =3D pe_mask(vcpu, false); break; + case SDEI_1_0_FN_SDEI_PRIVATE_RESET: + ret =3D event_reset(vcpu, true); + break; + case SDEI_1_0_FN_SDEI_SHARED_RESET: + ret =3D event_reset(vcpu, false); + break; default: ret =3D SDEI_NOT_SUPPORTED; } --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DEE2FC433FE for ; Fri, 27 May 2022 08:05:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349109AbiE0IFz (ORCPT ); Fri, 27 May 2022 04:05:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346376AbiE0IF1 (ORCPT ); Fri, 27 May 2022 04:05:27 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 56D71FF592 for ; Fri, 27 May 2022 01:05:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638724; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M1IZs3klzI4SDOe9ylEC0ku9fFW+/8u54cvhU+BDFz0=; b=QWVsj9HZWrKZK8g/mLGJdYII9looSEYsOrv0W05nc9nQ8pN2N6Iijx1h2eytAZQEHWa0x3 OLKWKeyJ+PTSPgvrjWflhNns4TWD8wZ2Zw9cmeyN4ev6UabwlX0+z7wgscekb8cU9L+JEq vitbzRjbBaFNq987L9UnX/A17ugh7Jw= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-282-1QRIi7dSPv-mlP2huYbBAw-1; Fri, 27 May 2022 04:05:19 -0400 X-MC-Unique: 1QRIi7dSPv-mlP2huYbBAw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 411E03804506; Fri, 27 May 2022 08:05:19 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 90A1C2026985; Fri, 27 May 2022 08:05:12 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 12/22] KVM: arm64: Support event injection and delivery Date: Fri, 27 May 2022 16:02:43 +0800 Message-Id: <20220527080253.1562538-13-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports event injection, delivery and cancellation. The event is injected and cancelled by kvm_sdei_{inject, cancel}_event(). For event delivery, kvm_sdei_deliver_event() is added to accommodate KVM_REQ_SDEI request. The KVM_REQ_SDEI request can be raised in several situation: * PE is unmasked * Event is enabled * Completion of currently running event or handler on receiving EVENT_COMPLETE or EVENT_COMPLETE_AND_RESUME hypercall, which will be supported in the subsequent patch. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/include/asm/kvm_sdei.h | 4 + arch/arm64/kvm/arm.c | 3 + arch/arm64/kvm/sdei.c | 123 ++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) diff --git a/arch/arm64/include/asm/kvm_sdei.h b/arch/arm64/include/asm/kvm= _sdei.h index 609338b17478..735d9ac1a5a2 100644 --- a/arch/arm64/include/asm/kvm_sdei.h +++ b/arch/arm64/include/asm/kvm_sdei.h @@ -64,6 +64,10 @@ struct kvm_sdei_vcpu { =20 /* APIs */ int kvm_sdei_call(struct kvm_vcpu *vcpu); +int kvm_sdei_inject_event(struct kvm_vcpu *vcpu, + unsigned int num, bool immediate); +int kvm_sdei_cancel_event(struct kvm_vcpu *vcpu, unsigned int num); +void kvm_sdei_deliver_event(struct kvm_vcpu *vcpu); void kvm_sdei_create_vcpu(struct kvm_vcpu *vcpu); void kvm_sdei_destroy_vcpu(struct kvm_vcpu *vcpu); =20 diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index e9516f951e7b..06cb5e38634e 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -720,6 +720,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) kvm_reset_vcpu(vcpu); =20 + if (kvm_check_request(KVM_REQ_SDEI, vcpu)) + kvm_sdei_deliver_event(vcpu); + /* * Clear IRQ_PENDING requests that were made to guarantee * that a VCPU sees new virtual interrupts. diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index 42ba6f97b168..36a72c1750fc 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -266,6 +266,129 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) return 1; } =20 +int kvm_sdei_inject_event(struct kvm_vcpu *vcpu, + unsigned int num, + bool immediate) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + + if (!vsdei) + return -EPERM; + + if (num >=3D KVM_NR_SDEI_EVENTS || !test_bit(num, &vsdei->registered)) + return -ENOENT; + + /* + * The event may be expected to be delivered immediately. There + * are several cases we can't do this: + * + * (1) The PE has been masked from any events. + * (2) The event isn't enabled yet. + * (3) There are any pending or running events. + */ + if (immediate && + ((vcpu->arch.flags & KVM_ARM64_SDEI_MASKED) || + !test_bit(num, &vsdei->enabled) || + vsdei->pending || vsdei->running)) + return -EBUSY; + + set_bit(num, &vsdei->pending); + if (!(vcpu->arch.flags & KVM_ARM64_SDEI_MASKED) && + test_bit(num, &vsdei->enabled)) + kvm_make_request(KVM_REQ_SDEI, vcpu); + + return 0; +} + +int kvm_sdei_cancel_event(struct kvm_vcpu *vcpu, unsigned int num) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + + if (!vsdei) + return -EPERM; + + if (num >=3D KVM_NR_SDEI_EVENTS || !test_bit(num, &vsdei->registered)) + return -ENOENT; + + if (test_bit(num, &vsdei->running)) + return -EBUSY; + + clear_bit(num, &vsdei->pending); + + return 0; +} + +void kvm_sdei_deliver_event(struct kvm_vcpu *vcpu) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + struct kvm_sdei_event_context *ctxt =3D &vsdei->ctxt; + unsigned int num, i; + unsigned long pstate; + + if (!vsdei || (vcpu->arch.flags & KVM_ARM64_SDEI_MASKED)) + return; + + /* + * All supported events have normal priority. So the currently + * running event can't be preempted by any one else. + */ + if (vsdei->running) + return; + + /* Select next pending event to be delivered */ + num =3D 0; + while (num < KVM_NR_SDEI_EVENTS) { + num =3D find_next_bit(&vsdei->pending, KVM_NR_SDEI_EVENTS, num); + if (test_bit(num, &vsdei->enabled)) + break; + } + + if (num >=3D KVM_NR_SDEI_EVENTS) + return; + + /* + * Save the interrupted context. We might have pending request + * to adjust PC. Lets adjust it now so that the resume address + * is correct when COMPLETE or COMPLETE_AND_RESUME hypercall + * is handled. + */ + __kvm_adjust_pc(vcpu); + ctxt->pc =3D *vcpu_pc(vcpu); + ctxt->pstate =3D *vcpu_cpsr(vcpu); + for (i =3D 0; i < ARRAY_SIZE(ctxt->regs); i++) + ctxt->regs[i] =3D vcpu_get_reg(vcpu, i); + + /* + * Inject event. The following registers are modified according + * to the specification. + * + * x0: event number + * x1: argument specified when the event is registered + * x2: PC of the interrupted context + * x3: PSTATE of the interrupted context + * PC: event handler + * PSTATE: Cleared nRW bit, but D/A/I/F bits are set + */ + for (i =3D 0; i < ARRAY_SIZE(ctxt->regs); i++) + vcpu_set_reg(vcpu, i, 0); + + vcpu_set_reg(vcpu, 0, num); + vcpu_set_reg(vcpu, 1, vsdei->handlers[num].ep_arg); + vcpu_set_reg(vcpu, 2, ctxt->pc); + vcpu_set_reg(vcpu, 3, ctxt->pstate); + + pstate =3D ctxt->pstate; + pstate &=3D ~(PSR_MODE32_BIT | PSR_MODE_MASK); + pstate |=3D (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT | PSR_MODE_EL1= h); + + *vcpu_cpsr(vcpu) =3D pstate; + *vcpu_pc(vcpu) =3D vsdei->handlers[num].ep_addr; + + /* Update event states */ + clear_bit(num, &vsdei->pending); + set_bit(num, &vsdei->running); +} + void kvm_sdei_create_vcpu(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei; --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 26781C433F5 for ; Fri, 27 May 2022 08:06:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347076AbiE0IF7 (ORCPT ); Fri, 27 May 2022 04:05:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349063AbiE0IFj (ORCPT ); Fri, 27 May 2022 04:05:39 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C239B10277F for ; Fri, 27 May 2022 01:05:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638735; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n/sMtf3ycYV6mAOWnm/6XSJenHeTbOjDcLvp1nt3Cvw=; b=gHHgcFtpvEyvPvj5/apjxDawphog82G1q+Uv2pgAcTcUlrHx7Ziau/ZQs4ZiFHBUXitWCM TOsZU25LljKM352TZGxeBrIEdHCBUwzTm2tnJzE8Y6eLKU8ZpeYEU7rCahrmJOv1/5nPsX s6Y4FgD9DOelvxVY1oo7p+S7HsmXapU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-568-LBUecK2GMdK7gJWsKVcz4A-1; Fri, 27 May 2022 04:05:30 -0400 X-MC-Unique: LBUecK2GMdK7gJWsKVcz4A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DFBC71C161AE; Fri, 27 May 2022 08:05:29 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F1D972026D64; Fri, 27 May 2022 08:05:19 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 13/22] KVM: arm64: Support EVENT_{COMPLETE, COMPLETE_AND_RESUME} hypercall Date: Fri, 27 May 2022 16:02:44 +0800 Message-Id: <20220527080253.1562538-14-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports EVENT_{COMPLETE, COMPLETE_AND_RESUME} hypercall. The execution resumes from previously interrupted context when EVENT_COMPLETE hypercall is received. However, the execution resumes from the specified address when EVENT_COMPLETE_AND_RESUME is received. In this case, context switches like below. * x0 to x17 are restored from the interrupted context. * SPSR_EL1 is set to PSTATE of the interrupted context. * ELR_EL1 is set to PC of the interrupted context. * PSTATE has nRW cleared, but D/A/I/F set. * PC is set to the resume address, specified in the first argument of EVENT_COMPLETE_AND_RESUME hypercall. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/sdei.c | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index 36a72c1750fc..0bea1b2f9452 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -85,6 +85,54 @@ static unsigned long event_context(struct kvm_vcpu *vcpu) return ctxt->regs[param_id]; } =20 +static void event_complete(struct kvm_vcpu *vcpu, bool resume) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + struct kvm_sdei_event_context *ctxt =3D &vsdei->ctxt; + unsigned long pstate, resume_addr =3D smccc_get_arg(vcpu, 1); + unsigned int num, i; + + num =3D find_next_bit(&vsdei->running, KVM_NR_SDEI_EVENTS, 0); + if (num >=3D KVM_NR_SDEI_EVENTS) + return; + + /* Restore registers: x0 -> x17 */ + for (i =3D 0; i < ARRAY_SIZE(ctxt->regs); i++) + vcpu_set_reg(vcpu, i, ctxt->regs[i]); + + /* + * The registers are modified accordingly if the execution resumes + * from the specified address. + * + * SPSR_EL1: PSTATE of the interrupted context + * ELR_EL1: PC of the interrupted context + * PSTATE: cleared nRW bit, but D/A/I/F bits are set + * PC: the resume address + */ + if (resume) { + if (has_vhe()) { + write_sysreg_el1(ctxt->pstate, SYS_SPSR); + write_sysreg_s(ctxt->pc, SYS_ELR_EL12); + } else { + __vcpu_sys_reg(vcpu, SPSR_EL1) =3D ctxt->pstate; + __vcpu_sys_reg(vcpu, ELR_EL1) =3D ctxt->pc; + } + + pstate =3D ctxt->pstate; + pstate &=3D ~(PSR_MODE32_BIT | PSR_MODE_MASK); + pstate |=3D (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | + PSR_F_BIT | PSR_MODE_EL1h); + *vcpu_cpsr(vcpu) =3D pstate; + *vcpu_pc(vcpu) =3D resume_addr; + } else { + *vcpu_cpsr(vcpu) =3D ctxt->pstate; + *vcpu_pc(vcpu) =3D ctxt->pc; + } + + /* Update event state */ + clear_bit(num, &vsdei->running); +} + static unsigned long event_unregister(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -234,6 +282,12 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) case SDEI_1_0_FN_SDEI_EVENT_CONTEXT: ret =3D event_context(vcpu); break; + case SDEI_1_0_FN_SDEI_EVENT_COMPLETE: + event_complete(vcpu, false); + break; + case SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME: + event_complete(vcpu, true); + break; case SDEI_1_0_FN_SDEI_EVENT_UNREGISTER: ret =3D event_unregister(vcpu); break; --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0A53C433EF for ; Fri, 27 May 2022 08:06:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229799AbiE0IG1 (ORCPT ); Fri, 27 May 2022 04:06:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349538AbiE0IFz (ORCPT ); Fri, 27 May 2022 04:05:55 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 38C5E104CB3 for ; Fri, 27 May 2022 01:05:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638748; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9CfhejkXuPa7WByuloODhCqAxTblZ1S6BbTJI/JlxSk=; b=H4TxWfSTloVWg5uz34FyZyE4oq/Xei6kkoW0QEGnOW5bSLUrkbAIZsv8GWjcmQjjWQa1Ac GG9LoUogh2grYaYKFPJzr4K7tAkJf/yzMNDsQGE3nhxs0mKk6hkdvEY0deIlVO2TdvppyX VbcQcSzfaGug5LVCUjXkHqIB1i7XcDo= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-287-9XtA_8I5PWyJEkWbwAHphQ-1; Fri, 27 May 2022 04:05:41 -0400 X-MC-Unique: 9XtA_8I5PWyJEkWbwAHphQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2B34B1C161B3; Fri, 27 May 2022 08:05:41 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A7D442026D64; Fri, 27 May 2022 08:05:30 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 14/22] KVM: arm64: Support EVENT_SIGNAL hypercall Date: Fri, 27 May 2022 16:02:45 +0800 Message-Id: <20220527080253.1562538-15-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports EVENT_SIGNAL hypercall. It's used to inject the software signaled event, whose number is zero. The KVM_REQ_SDEI request is raised if the PE is unmasked and the event has been enabled. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/sdei.c | 28 ++++++++++++++++++++++++++++ include/uapi/linux/arm_sdei.h | 3 +++ 2 files changed, 31 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index 0bea1b2f9452..a55797fce06a 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -232,6 +232,31 @@ static unsigned long pe_mask(struct kvm_vcpu *vcpu, bo= ol mask) return SDEI_SUCCESS; } =20 +static unsigned long event_signal(struct kvm_vcpu *vcpu) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + unsigned int num =3D smccc_get_arg(vcpu, 1); + + if (num >=3D KVM_NR_SDEI_EVENTS) + return SDEI_INVALID_PARAMETERS; + + /* + * The event must be software signaled event, whose + * number is zero. + */ + if (num !=3D SDEI_SW_SIGNALED_EVENT || + !test_bit(num, &vsdei->registered)) + return SDEI_INVALID_PARAMETERS; + + if (!test_and_set_bit(num, &vsdei->pending)) { + if (!(vcpu->arch.flags & KVM_ARM64_SDEI_MASKED) && + test_bit(num, &vsdei->enabled)) + kvm_make_request(KVM_REQ_SDEI, vcpu); + } + + return SDEI_SUCCESS; +} + static unsigned long event_reset(struct kvm_vcpu *vcpu, bool private) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -303,6 +328,9 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) case SDEI_1_0_FN_SDEI_PE_UNMASK: ret =3D pe_mask(vcpu, false); break; + case SDEI_1_1_FN_SDEI_EVENT_SIGNAL: + ret =3D event_signal(vcpu); + break; case SDEI_1_0_FN_SDEI_PRIVATE_RESET: ret =3D event_reset(vcpu, true); break; diff --git a/include/uapi/linux/arm_sdei.h b/include/uapi/linux/arm_sdei.h index af0630ba5437..72b07114bbfa 100644 --- a/include/uapi/linux/arm_sdei.h +++ b/include/uapi/linux/arm_sdei.h @@ -22,9 +22,12 @@ #define SDEI_1_0_FN_SDEI_PE_UNMASK SDEI_1_0_FN(0x0C) #define SDEI_1_0_FN_SDEI_INTERRUPT_BIND SDEI_1_0_FN(0x0D) #define SDEI_1_0_FN_SDEI_INTERRUPT_RELEASE SDEI_1_0_FN(0x0E) +#define SDEI_1_1_FN_SDEI_EVENT_SIGNAL SDEI_1_0_FN(0x0F) #define SDEI_1_0_FN_SDEI_PRIVATE_RESET SDEI_1_0_FN(0x11) #define SDEI_1_0_FN_SDEI_SHARED_RESET SDEI_1_0_FN(0x12) =20 +#define SDEI_SW_SIGNALED_EVENT 0 + #define SDEI_VERSION_MAJOR_SHIFT 48 #define SDEI_VERSION_MAJOR_MASK 0x7fff #define SDEI_VERSION_MINOR_SHIFT 32 --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DB20C433EF for ; Fri, 27 May 2022 08:06:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242348AbiE0IGd (ORCPT ); Fri, 27 May 2022 04:06:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349535AbiE0IGV (ORCPT ); Fri, 27 May 2022 04:06:21 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id ED5EA1053F4 for ; Fri, 27 May 2022 01:06:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638761; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k7pKbXrqucn/sAmVex4c4gIiaSEBPmDw6Y4/7YpC/zI=; b=ZkIKNXEia24jGWMVGmqRv6Lnr4mDbWV6zjWlcrESE+IA982gOLHaUTx966fHImuBFQwPSx 6S4EgLJq9Kj5X8xJJ1QQ+BmTVBaEMNiVdRDNQNHC4OzztEuQddlaUyCuEh8om5t1FjeHDr sRYxbbsSRBXLx1VGknLoIO/opu4YpEo= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-571-F2akXvRUPUS7TCab-24vaQ-1; Fri, 27 May 2022 04:05:55 -0400 X-MC-Unique: F2akXvRUPUS7TCab-24vaQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EB6403804509; Fri, 27 May 2022 08:05:49 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 27ABD2026D64; Fri, 27 May 2022 08:05:41 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 15/22] KVM: arm64: Support SDEI_FEATURES hypercall Date: Fri, 27 May 2022 16:02:46 +0800 Message-Id: <20220527080253.1562538-16-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports SDEI_FEATURES hypercall. It's used to retrieve the supported features, including number of slots for the interrupt binding events and relative mode for event handler. Currently, we simply return zero, meaning none of them is supported. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/sdei.c | 16 ++++++++++++++++ include/uapi/linux/arm_sdei.h | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index a55797fce06a..d295f84f6940 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -277,6 +277,19 @@ static unsigned long event_reset(struct kvm_vcpu *vcpu= , bool private) return SDEI_SUCCESS; } =20 +static unsigned long sdei_features(struct kvm_vcpu *vcpu) +{ + unsigned int feature =3D smccc_get_arg(vcpu, 1); + + switch (feature) { + case SDEI_FEATURE_BIND_SLOTS: + case SDEI_FEATURE_RELATIVE_MODE: + return 0; + } + + return SDEI_INVALID_PARAMETERS; +} + int kvm_sdei_call(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -337,6 +350,9 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) case SDEI_1_0_FN_SDEI_SHARED_RESET: ret =3D event_reset(vcpu, false); break; + case SDEI_1_1_FN_SDEI_FEATURES: + ret =3D sdei_features(vcpu); + break; default: ret =3D SDEI_NOT_SUPPORTED; } diff --git a/include/uapi/linux/arm_sdei.h b/include/uapi/linux/arm_sdei.h index 72b07114bbfa..5b783cc64617 100644 --- a/include/uapi/linux/arm_sdei.h +++ b/include/uapi/linux/arm_sdei.h @@ -23,6 +23,7 @@ #define SDEI_1_0_FN_SDEI_INTERRUPT_BIND SDEI_1_0_FN(0x0D) #define SDEI_1_0_FN_SDEI_INTERRUPT_RELEASE SDEI_1_0_FN(0x0E) #define SDEI_1_1_FN_SDEI_EVENT_SIGNAL SDEI_1_0_FN(0x0F) +#define SDEI_1_1_FN_SDEI_FEATURES SDEI_1_0_FN(0x10) #define SDEI_1_0_FN_SDEI_PRIVATE_RESET SDEI_1_0_FN(0x11) #define SDEI_1_0_FN_SDEI_SHARED_RESET SDEI_1_0_FN(0x12) =20 @@ -73,4 +74,8 @@ #define SDEI_EVENT_PRIORITY_NORMAL 0 #define SDEI_EVENT_PRIORITY_CRITICAL 1 =20 +/* SDEI_FEATURES */ +#define SDEI_FEATURE_BIND_SLOTS 0 +#define SDEI_FEATURE_RELATIVE_MODE 1 + #endif /* _UAPI_LINUX_ARM_SDEI_H */ --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0CACAC433EF for ; Fri, 27 May 2022 08:06:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244703AbiE0IGk (ORCPT ); Fri, 27 May 2022 04:06:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349542AbiE0IGX (ORCPT ); Fri, 27 May 2022 04:06:23 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 75BA51059CB for ; Fri, 27 May 2022 01:06:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638763; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g1oE6zW708ThGzA9gQNSKRACZxEySOeeS7EX47PV2eQ=; b=NVbmhFZS7DusoOmiB19RK8U/0orpvUVjFhskHH2bvBZbB/q+ZYyDPQHiN+dXToQBHmq1vd zKPxQeNqvvaq90FpcNM61plAxOq9LVMEDkiHNa89uMzzpZqwpN8DE0g5IKxYCjp3v5AH8u /16v8NA+V8oBzXfbLcoWGNGiUEBqibc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-423-JYmtOss3NKWCf39IZ3uN1w-1; Fri, 27 May 2022 04:05:58 -0400 X-MC-Unique: JYmtOss3NKWCf39IZ3uN1w-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6C53E2949BA6; Fri, 27 May 2022 08:05:57 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B4D7C2026D07; Fri, 27 May 2022 08:05:50 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 16/22] KVM: arm64: Support SDEI_VERSION hypercall Date: Fri, 27 May 2022 16:02:47 +0800 Message-Id: <20220527080253.1562538-17-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports SDEI_VERSION hypercall by returning v1.1, which is the specification version we're following. The vendor is set to one of the values returned from ARM_SMCCC_VENDOR_HYP_CALL_UID hypercall. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/include/asm/kvm_sdei.h | 3 +++ arch/arm64/kvm/sdei.c | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/arch/arm64/include/asm/kvm_sdei.h b/arch/arm64/include/asm/kvm= _sdei.h index 735d9ac1a5a2..a1e960943515 100644 --- a/arch/arm64/include/asm/kvm_sdei.h +++ b/arch/arm64/include/asm/kvm_sdei.h @@ -62,6 +62,9 @@ struct kvm_sdei_vcpu { struct kvm_sdei_event_context ctxt; }; =20 +/* Returned as vendor through SDEI_VERSION hypercall */ +#define KVM_SDEI_VENDOR ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2 + /* APIs */ int kvm_sdei_call(struct kvm_vcpu *vcpu); int kvm_sdei_inject_event(struct kvm_vcpu *vcpu, diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index d295f84f6940..f95b9bcce13c 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -13,6 +13,14 @@ #include #include =20 +static unsigned long sdei_version(struct kvm_vcpu *vcpu) +{ + /* v1.1 and vendor ID */ + return (1UL << SDEI_VERSION_MAJOR_SHIFT) | + (1UL << SDEI_VERSION_MINOR_SHIFT) | + KVM_SDEI_VENDOR; +} + static unsigned long event_register(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; @@ -308,6 +316,9 @@ int kvm_sdei_call(struct kvm_vcpu *vcpu) } =20 switch (func) { + case SDEI_1_0_FN_SDEI_VERSION: + ret =3D sdei_version(vcpu); + break; case SDEI_1_0_FN_SDEI_EVENT_REGISTER: ret =3D event_register(vcpu); break; --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C529BC433F5 for ; Fri, 27 May 2022 08:06:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346128AbiE0IGq (ORCPT ); Fri, 27 May 2022 04:06:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349397AbiE0IG2 (ORCPT ); Fri, 27 May 2022 04:06:28 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C5314106544 for ; Fri, 27 May 2022 01:06:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638768; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=twZ0Ps5QlQoExjMaYnRuFi/0acFN9mberGquMbJnVlw=; b=VV8mrSTQ/ux+kZcJ9Irozp2D8/P4skWVDK/cdaFpc1VH989VNvNTic5DdPKmhqY/oe5klz MyfZlCapAuoAwGjIyMG6NbLrTYSgTvUgdghgp1Ln0lMi3FlIJCZbGY0RVVlJsqNRHfjZvA FAL7IQ3z1g7peB7CEPl1rcd7xOvWML8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-44-dHQJq2LSP2WXZWUR_i_Jmw-1; Fri, 27 May 2022 04:06:05 -0400 X-MC-Unique: dHQJq2LSP2WXZWUR_i_Jmw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ACF59858EEE; Fri, 27 May 2022 08:06:04 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 279CC2026D64; Fri, 27 May 2022 08:05:57 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 17/22] KVM: arm64: Expose SDEI capbility and service Date: Fri, 27 May 2022 16:02:48 +0800 Message-Id: <20220527080253.1562538-18-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The SDEI functionality is ready and lets exposes the capability and service. * KVM_CAP_ARM_SDEI * KVM_REG_ARM_STD_BIT_SDEI in pseudo-firmware bitmap register KVM_REG_ARM_STD_BMAP * The events for software signaled one (0x0) and Async PF (0x1) are exposed. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- Documentation/virt/kvm/api.rst | 11 +++++++++++ Documentation/virt/kvm/arm/hypercalls.rst | 4 ++++ arch/arm64/include/asm/kvm_sdei.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/hypercalls.c | 4 ++++ include/uapi/linux/kvm.h | 1 + 7 files changed, 24 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 11e00a46c610..011ec0987d71 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7956,6 +7956,17 @@ should adjust CPUID leaf 0xA to reflect that the PMU= is disabled. When enabled, KVM will exit to userspace with KVM_EXIT_SYSTEM_EVENT of type KVM_SYSTEM_EVENT_SUSPEND to process the guest suspend request. =20 +8.37 KVM_CAP_ARM_SDEI +--------------------- + +:Capability: KVM_CAP_ARM_SDEI +:Architectures: arm64 +:Type: vm + +This capability indicates that the SDEI virtual service is supported +in the host. A VMM can check whether the service is available to enable +it. + 9. Known KVM API problems =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D =20 diff --git a/Documentation/virt/kvm/arm/hypercalls.rst b/Documentation/virt= /kvm/arm/hypercalls.rst index 3e23084644ba..8b8159988934 100644 --- a/Documentation/virt/kvm/arm/hypercalls.rst +++ b/Documentation/virt/kvm/arm/hypercalls.rst @@ -106,6 +106,10 @@ The pseudo-firmware bitmap register are as follows: The bit represents the services offered under v1.0 of ARM True Random Number Generator (TRNG) specification, ARM DEN0098. =20 + Bit-1: KVM_REG_ARM_STD_BIT_SDEI: + The bit represents the services offered under v1.1 of ARM Software + Delegated Exception Interface (SDEI) specification, ARM DEN0054C. + * KVM_REG_ARM_STD_HYP_BMAP: Controls the bitmap of the ARM Standard Hypervisor Service Calls. =20 diff --git a/arch/arm64/include/asm/kvm_sdei.h b/arch/arm64/include/asm/kvm= _sdei.h index a1e960943515..a68d40137a88 100644 --- a/arch/arm64/include/asm/kvm_sdei.h +++ b/arch/arm64/include/asm/kvm_sdei.h @@ -15,6 +15,8 @@ #include =20 enum { + KVM_SDEI_EVENT_SW_SIGNALED =3D 0, + KVM_SDEI_EVENT_ASYNC_PF, KVM_NR_SDEI_EVENTS, }; =20 diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/as= m/kvm.h index 3bb134355874..cc3251381960 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -344,6 +344,7 @@ struct kvm_arm_copy_mte_tags { =20 enum { KVM_REG_ARM_STD_BIT_TRNG_V1_0 =3D 0, + KVM_REG_ARM_STD_BIT_SDEI, #ifdef __KERNEL__ KVM_REG_ARM_STD_BMAP_BIT_COUNT, #endif diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 06cb5e38634e..15ead92ad26d 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -217,6 +217,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long = ext) case KVM_CAP_VCPU_ATTRIBUTES: case KVM_CAP_PTP_KVM: case KVM_CAP_ARM_SYSTEM_SUSPEND: + case KVM_CAP_ARM_SDEI: r =3D 1; break; case KVM_CAP_SET_GUEST_DEBUG2: diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index 88561ac47301..aaa5197ffe83 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -99,6 +99,10 @@ static bool kvm_hvc_call_allowed(struct kvm_vcpu *vcpu, = u32 func) struct kvm_smccc_features *smccc_feat =3D &vcpu->kvm->arch.smccc_feat; =20 switch (func) { + case SDEI_1_0_FN(ARM_SMCCC_STANDARD_SDEI_START - 0x20) ... + SDEI_1_0_FN(ARM_SMCCC_STANDARD_SDEI_END - 0x20): + return test_bit(KVM_REG_ARM_STD_BIT_SDEI, + &smccc_feat->std_bmap); case ARM_SMCCC_TRNG_VERSION: case ARM_SMCCC_TRNG_FEATURES: case ARM_SMCCC_TRNG_GET_UUID: diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 5088bd9f1922..65622bcaeec3 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1157,6 +1157,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_VM_TSC_CONTROL 214 #define KVM_CAP_SYSTEM_EVENT_DATA 215 #define KVM_CAP_ARM_SYSTEM_SUSPEND 216 +#define KVM_CAP_ARM_SDEI 217 =20 #ifdef KVM_CAP_IRQ_ROUTING =20 --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E768C433F5 for ; Fri, 27 May 2022 08:07:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349453AbiE0IHf (ORCPT ); Fri, 27 May 2022 04:07:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349649AbiE0IGh (ORCPT ); Fri, 27 May 2022 04:06:37 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5EA3D28992 for ; Fri, 27 May 2022 01:06:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638777; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JmejXcqy3igKfzXfj854zdtX6ccYIqInqbAINc4o8Ro=; b=axBVPih2HEsB7qqTA3BZFfPkuK9iPlUOMHlSQGv593m2Dd1yVwtn5KodXPHyitEsaMWpyB sun1H+WHSck8ADgZiv4SlYmGid8EO1Sca3GG6EHhh0CJEHeT5fsmmOnGPKN8F7D+jiw/S3 uJwi1owrVaSRGYDpYaR9iMhp2gbo2HY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-267-quDHT6L0NL646pHc2VDpBw-1; Fri, 27 May 2022 04:06:14 -0400 X-MC-Unique: quDHT6L0NL646pHc2VDpBw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BD19480418C; Fri, 27 May 2022 08:06:13 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 822C72026D64; Fri, 27 May 2022 08:06:05 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 18/22] KVM: arm64: Allow large sized pseudo firmware registers Date: Fri, 27 May 2022 16:02:49 +0800 Message-Id: <20220527080253.1562538-19-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently, the sizes of all pseudo firmware registers are 64 bits. The value from user space is copied to a local 64-bits variable, which is passed on in kvm_arm_set_fw_reg(). It works perfectly until large sized pseudo firmware registers are added for SDEI event handlers, states and context. This copies the user space's value to local 64-bits variable and passes the local variable on, only if the pseudo firmware register is 64-bits in size. No functional change intended. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/kvm/hypercalls.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index aaa5197ffe83..8e0df54d1422 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -406,14 +406,20 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const s= truct kvm_one_reg *reg) return 0; } =20 -static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 = val) +static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) { int ret =3D 0; struct kvm *kvm =3D vcpu->kvm; struct kvm_smccc_features *smccc_feat =3D &kvm->arch.smccc_feat; unsigned long *fw_reg_bmap, fw_reg_features; + void __user *uaddr =3D (void __user *)(long)reg->addr; + u64 val; =20 - switch (reg_id) { + if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + switch (reg->id) { case KVM_REG_ARM_STD_BMAP: fw_reg_bmap =3D &smccc_feat->std_bmap; fw_reg_features =3D KVM_ARM_SMCCC_STD_FEATURES; @@ -454,15 +460,14 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const s= truct kvm_one_reg *reg) u64 val; int wa_level; =20 - if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id))) - return -EFAULT; - switch (reg->id) { case KVM_REG_ARM_PSCI_VERSION: { bool wants_02; =20 wants_02 =3D test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features); + if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id))) + return -EFAULT; =20 switch (val) { case KVM_ARM_PSCI_0_1: @@ -483,6 +488,9 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const str= uct kvm_one_reg *reg) =20 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1: case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3: + if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id))) + return -EFAULT; + if (val & ~KVM_REG_FEATURE_LEVEL_MASK) return -EINVAL; =20 @@ -492,6 +500,9 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const str= uct kvm_one_reg *reg) return 0; =20 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2: + if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id))) + return -EFAULT; + if (val & ~(KVM_REG_FEATURE_LEVEL_MASK | KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED)) return -EINVAL; @@ -529,7 +540,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const str= uct kvm_one_reg *reg) case KVM_REG_ARM_STD_BMAP: case KVM_REG_ARM_STD_HYP_BMAP: case KVM_REG_ARM_VENDOR_HYP_BMAP: - return kvm_arm_set_fw_reg_bmap(vcpu, reg->id, val); + return kvm_arm_set_fw_reg_bmap(vcpu, reg); default: return -ENOENT; } --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7E87C433F5 for ; Fri, 27 May 2022 08:07:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234624AbiE0IHV (ORCPT ); Fri, 27 May 2022 04:07:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349695AbiE0IGn (ORCPT ); Fri, 27 May 2022 04:06:43 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7E462C3D18 for ; Fri, 27 May 2022 01:06:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638785; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Fyjs17cQuU2U21OwKMwjdN3jGGY3Oxumv8wAG92m2EA=; b=AitsRHhd7u2erCEHwJ4hYBM750m4tFdKXJGPhrCjN+yDEi42UwqROsn2AgeVUN0/nV1nI3 vJewSJOHhYQWVZ9EIILGD9OquK9CPoeb0L4seTpSAV50/Bf58ruz/uVu0RZZOMDuisXswG JShG2yw6JJ9IJoXUCC/r8jR95GdnuPw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-505-PsGwo5UTOaOLMy8tj08cCw-1; Fri, 27 May 2022 04:06:22 -0400 X-MC-Unique: PsGwo5UTOaOLMy8tj08cCw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4F110185A79C; Fri, 27 May 2022 08:06:21 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8B3662026D64; Fri, 27 May 2022 08:06:14 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 19/22] KVM: arm64: Support SDEI event migration Date: Fri, 27 May 2022 16:02:50 +0800 Message-Id: <20220527080253.1562538-20-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This supports migration for SDEI event handlers, states and context. Several pseudo firmware registers are added to assist the migration work. * KVM_REG_ARM_SDEI_EVENT_HANDLER_0 KVM_REG_ARM_SDEI_EVENT_HANDLER_1 KVM_REG_ARM_SDEI_EVENT_HANDLER_2 KVM_REG_ARM_SDEI_EVENT_HANDLER_3 128-bits in length. They're mapped to the handler's address and argument for Software Signaled event and Async PF event. Additinal two events are reserved for future needs without too much considerations to the compatible issue. Not too much bandwidth needed to migrate those two additional registers. * KVM_REG_ARM_SDEI_EVENT_REGISTERED KVM_REG_ARM_SDEI_EVENT_ENABLED KVM_REG_ARM_SDEI_EVENT_RUNNING KVM_REG_ARM_SDEI_EVENT_PENDING 64-bits in length. They're mapped to registered, enabled, running and pending bitmap. * KVM_REG_ARM_SDEI_EVENT_CONTEXT 2048-bits in length. It's mapped to saved or interrupted context. * KVM_REG_ARM_SDEI_PE_STATE 64-bits in length. It's mapped to PE's states, which is masked or unmasked. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- arch/arm64/include/asm/kvm_sdei.h | 2 + arch/arm64/include/uapi/asm/kvm.h | 17 ++++ arch/arm64/kvm/hypercalls.c | 32 ++++++ arch/arm64/kvm/sdei.c | 161 ++++++++++++++++++++++++++++++ 4 files changed, 212 insertions(+) diff --git a/arch/arm64/include/asm/kvm_sdei.h b/arch/arm64/include/asm/kvm= _sdei.h index a68d40137a88..d11964b88b58 100644 --- a/arch/arm64/include/asm/kvm_sdei.h +++ b/arch/arm64/include/asm/kvm_sdei.h @@ -73,6 +73,8 @@ int kvm_sdei_inject_event(struct kvm_vcpu *vcpu, unsigned int num, bool immediate); int kvm_sdei_cancel_event(struct kvm_vcpu *vcpu, unsigned int num); void kvm_sdei_deliver_event(struct kvm_vcpu *vcpu); +int kvm_sdei_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); +int kvm_sdei_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); void kvm_sdei_create_vcpu(struct kvm_vcpu *vcpu); void kvm_sdei_destroy_vcpu(struct kvm_vcpu *vcpu); =20 diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/as= m/kvm.h index cc3251381960..90247ce8de59 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -262,6 +262,11 @@ struct kvm_arm_copy_mte_tags { #define KVM_REG_ARM_FW (0x0014 << KVM_REG_ARM_COPROC_SHIFT) #define KVM_REG_ARM_FW_REG(r) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \ KVM_REG_ARM_FW | ((r) & 0xffff)) +#define KVM_REG_ARM_FW_REG_128(r) (KVM_REG_ARM64 | KVM_REG_SIZE_U128 | \ + KVM_REG_ARM_FW | ((r) & 0xffff)) +#define KVM_REG_ARM_FW_REG_2048(r) (KVM_REG_ARM64 | KVM_REG_SIZE_U2048 | \ + KVM_REG_ARM_FW | ((r) & 0xffff)) + #define KVM_REG_ARM_PSCI_VERSION KVM_REG_ARM_FW_REG(0) #define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 KVM_REG_ARM_FW_REG(1) #define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL 0 @@ -288,6 +293,18 @@ struct kvm_arm_copy_mte_tags { #define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_AVAIL 1 #define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_REQUIRED 2 =20 +/* SDEI registers */ +#define KVM_REG_ARM_SDEI_EVENT_HANDLER_0 KVM_REG_ARM_FW_REG_128(4) +#define KVM_REG_ARM_SDEI_EVENT_HANDLER_1 KVM_REG_ARM_FW_REG_128(5) +#define KVM_REG_ARM_SDEI_EVENT_HANDLER_2 KVM_REG_ARM_FW_REG_128(6) +#define KVM_REG_ARM_SDEI_EVENT_HANDLER_3 KVM_REG_ARM_FW_REG_128(7) +#define KVM_REG_ARM_SDEI_EVENT_REGISTERED KVM_REG_ARM_FW_REG(8) +#define KVM_REG_ARM_SDEI_EVENT_ENABLED KVM_REG_ARM_FW_REG(9) +#define KVM_REG_ARM_SDEI_EVENT_RUNNING KVM_REG_ARM_FW_REG(10) +#define KVM_REG_ARM_SDEI_EVENT_PENDING KVM_REG_ARM_FW_REG(11) +#define KVM_REG_ARM_SDEI_EVENT_CONTEXT KVM_REG_ARM_FW_REG_2048(12) +#define KVM_REG_ARM_SDEI_PE_STATE KVM_REG_ARM_FW_REG(13) + /* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT) =20 diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index 8e0df54d1422..d330aee968a9 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -290,6 +290,16 @@ static const u64 kvm_arm_fw_reg_ids[] =3D { KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3, + KVM_REG_ARM_SDEI_EVENT_HANDLER_0, + KVM_REG_ARM_SDEI_EVENT_HANDLER_1, + KVM_REG_ARM_SDEI_EVENT_HANDLER_2, + KVM_REG_ARM_SDEI_EVENT_HANDLER_3, + KVM_REG_ARM_SDEI_EVENT_REGISTERED, + KVM_REG_ARM_SDEI_EVENT_ENABLED, + KVM_REG_ARM_SDEI_EVENT_RUNNING, + KVM_REG_ARM_SDEI_EVENT_PENDING, + KVM_REG_ARM_SDEI_EVENT_CONTEXT, + KVM_REG_ARM_SDEI_PE_STATE, KVM_REG_ARM_STD_BMAP, KVM_REG_ARM_STD_HYP_BMAP, KVM_REG_ARM_VENDOR_HYP_BMAP, @@ -387,6 +397,17 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const st= ruct kvm_one_reg *reg) case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3: val =3D get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK; break; + case KVM_REG_ARM_SDEI_EVENT_HANDLER_0: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_1: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_2: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_3: + case KVM_REG_ARM_SDEI_EVENT_REGISTERED: + case KVM_REG_ARM_SDEI_EVENT_ENABLED: + case KVM_REG_ARM_SDEI_EVENT_RUNNING: + case KVM_REG_ARM_SDEI_EVENT_PENDING: + case KVM_REG_ARM_SDEI_EVENT_CONTEXT: + case KVM_REG_ARM_SDEI_PE_STATE: + return kvm_sdei_get_reg(vcpu, reg); case KVM_REG_ARM_STD_BMAP: val =3D READ_ONCE(smccc_feat->std_bmap); break; @@ -537,6 +558,17 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const st= ruct kvm_one_reg *reg) return -EINVAL; =20 return 0; + case KVM_REG_ARM_SDEI_EVENT_HANDLER_0: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_1: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_2: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_3: + case KVM_REG_ARM_SDEI_EVENT_REGISTERED: + case KVM_REG_ARM_SDEI_EVENT_ENABLED: + case KVM_REG_ARM_SDEI_EVENT_RUNNING: + case KVM_REG_ARM_SDEI_EVENT_PENDING: + case KVM_REG_ARM_SDEI_EVENT_CONTEXT: + case KVM_REG_ARM_SDEI_PE_STATE: + return kvm_sdei_set_reg(vcpu, reg); case KVM_REG_ARM_STD_BMAP: case KVM_REG_ARM_STD_HYP_BMAP: case KVM_REG_ARM_VENDOR_HYP_BMAP: diff --git a/arch/arm64/kvm/sdei.c b/arch/arm64/kvm/sdei.c index f95b9bcce13c..c14532de48f5 100644 --- a/arch/arm64/kvm/sdei.c +++ b/arch/arm64/kvm/sdei.c @@ -498,6 +498,167 @@ void kvm_sdei_deliver_event(struct kvm_vcpu *vcpu) set_bit(num, &vsdei->running); } =20 +int kvm_sdei_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + struct kvm_sdei_event_context *ctxt =3D &vsdei->ctxt; + struct kvm_sdei_event_handler handler; + void __user *uaddr =3D (void __user *)(long)reg->addr; + unsigned int num, i; + unsigned long val, *pstate =3D NULL; + + if (!vsdei) + return -EPERM; + + switch (reg->id) { + case KVM_REG_ARM_SDEI_EVENT_HANDLER_0: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_1: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_2: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_3: + num =3D (reg->id & 0xffff) - + (KVM_REG_ARM_SDEI_EVENT_HANDLER_0 & 0xffff); + + if (num < KVM_NR_SDEI_EVENTS) + handler =3D vsdei->handlers[num]; + else + memset(&handler, 0, sizeof(handler)); + + if (copy_to_user(uaddr, &handler, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + break; + case KVM_REG_ARM_SDEI_EVENT_REGISTERED: + pstate =3D &vsdei->registered; + fallthrough; + case KVM_REG_ARM_SDEI_EVENT_ENABLED: + pstate =3D pstate ? : &vsdei->enabled; + fallthrough; + case KVM_REG_ARM_SDEI_EVENT_RUNNING: + pstate =3D pstate ? : &vsdei->running; + fallthrough; + case KVM_REG_ARM_SDEI_EVENT_PENDING: + pstate =3D pstate ? : &vsdei->pending; + if (copy_to_user(uaddr, pstate, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + break; + case KVM_REG_ARM_SDEI_EVENT_CONTEXT: + if (copy_to_user(uaddr, &ctxt->pc, sizeof(ctxt->pc))) + return -EFAULT; + + uaddr +=3D sizeof(ctxt->pc); + if (copy_to_user(uaddr, &ctxt->pstate, sizeof(ctxt->pstate))) + return -EFAULT; + + uaddr +=3D sizeof(ctxt->pstate); + for (i =3D 0; i < ARRAY_SIZE(ctxt->regs); i++) { + if (copy_to_user(uaddr, &ctxt->regs[i], + sizeof(ctxt->regs[i]))) + return -EFAULT; + + uaddr +=3D sizeof(ctxt->regs[i]); + } + + break; + case KVM_REG_ARM_SDEI_PE_STATE: + val =3D (vcpu->arch.flags & KVM_ARM64_SDEI_MASKED) ? 1 : 0; + if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + break; + default: + return -ENOENT; + } + + return 0; +} + +int kvm_sdei_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) +{ + struct kvm_sdei_vcpu *vsdei =3D vcpu->arch.sdei; + struct kvm_sdei_event_context *ctxt =3D &vsdei->ctxt; + void __user *uaddr =3D (void __user *)(long)reg->addr; + unsigned int num, i; + unsigned long val, *pstate =3D NULL; + + if (!vsdei) + return -EPERM; + + switch (reg->id) { + case KVM_REG_ARM_SDEI_EVENT_HANDLER_0: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_1: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_2: + case KVM_REG_ARM_SDEI_EVENT_HANDLER_3: + num =3D (reg->id & 0xffff) - + (KVM_REG_ARM_SDEI_EVENT_HANDLER_0 & 0xffff); + if (num >=3D KVM_NR_SDEI_EVENTS) + break; + + if (copy_from_user(&vsdei->handlers[num], uaddr, + sizeof(vsdei->handlers[num]))) + return -EFAULT; + + break; + case KVM_REG_ARM_SDEI_EVENT_REGISTERED: + pstate =3D &vsdei->registered; + fallthrough; + case KVM_REG_ARM_SDEI_EVENT_ENABLED: + pstate =3D pstate ? : &vsdei->enabled; + fallthrough; + case KVM_REG_ARM_SDEI_EVENT_RUNNING: + pstate =3D pstate ? : &vsdei->running; + fallthrough; + case KVM_REG_ARM_SDEI_EVENT_PENDING: + pstate =3D pstate ? : &vsdei->pending; + if (copy_from_user(&val, uaddr, sizeof(val))) + return -EFAULT; + + *pstate =3D (val & GENMASK(KVM_NR_SDEI_EVENTS - 1, 0)); + if (!(vcpu->arch.flags & KVM_ARM64_SDEI_MASKED) && + pstate =3D=3D &vsdei->pending && + vsdei->pending) + kvm_make_request(KVM_REQ_SDEI, vcpu); + + break; + case KVM_REG_ARM_SDEI_EVENT_CONTEXT: + if (copy_from_user(&ctxt->pc, uaddr, sizeof(ctxt->pc))) + return -EFAULT; + + uaddr +=3D sizeof(ctxt->pc); + if (copy_from_user(&ctxt->pstate, uaddr, sizeof(ctxt->pstate))) + return -EFAULT; + + uaddr +=3D sizeof(ctxt->pstate); + for (i =3D 0; i < ARRAY_SIZE(ctxt->regs); i++) { + if (copy_from_user(&ctxt->regs[i], uaddr, + sizeof(ctxt->regs[i]))) + return -EFAULT; + + uaddr +=3D sizeof(ctxt->regs[i]); + } + + break; + case KVM_REG_ARM_SDEI_PE_STATE: + if (copy_from_user(&val, uaddr, sizeof(val))) + return -EFAULT; + + if (val) { + vcpu->arch.flags |=3D KVM_ARM64_SDEI_MASKED; + } else { + vcpu->arch.flags &=3D ~KVM_ARM64_SDEI_MASKED; + if (vsdei->pending) + kvm_make_request(KVM_REQ_SDEI, vcpu); + } + + break; + default: + return -ENOENT; + } + + return 0; + +} + void kvm_sdei_create_vcpu(struct kvm_vcpu *vcpu) { struct kvm_sdei_vcpu *vsdei; --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 981ABC433F5 for ; Fri, 27 May 2022 08:07:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343845AbiE0IH1 (ORCPT ); Fri, 27 May 2022 04:07:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348794AbiE0IHG (ORCPT ); Fri, 27 May 2022 04:07:06 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 18762FF583 for ; Fri, 27 May 2022 01:06:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638795; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Au77dj9BaZM3OWaXsQH00UFj+i5VHc/ijY2zkDDcEiA=; b=PWqAC5U9qp4+DKRFLCrgMVnrQZcCsRFShu8PJXjXzmyxeRwLraF70YPl38ZWL/GezbcjkL FM7q+1vcuynkvLznnnhScqIIqhpSZDaRvbjG6ek3cMOOdeKo2oUDDL4bYpNieUL88riVpG YL/PaGmGFZZmh0SuiYrcdscfLgY6Rpo= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-456-qa_RcJv4PtiKr0mqIkal9A-1; Fri, 27 May 2022 04:06:32 -0400 X-MC-Unique: qa_RcJv4PtiKr0mqIkal9A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 99C0A2949BB5; Fri, 27 May 2022 08:06:31 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 08B782026D64; Fri, 27 May 2022 08:06:21 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 20/22] KVM: arm64: Add SDEI document Date: Fri, 27 May 2022 16:02:51 +0800 Message-Id: <20220527080253.1562538-21-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This adds SDEI document, to explain what has been missed from the specification and the pesudo firmware registers used for migration. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- Documentation/virt/kvm/arm/sdei.rst | 64 +++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Documentation/virt/kvm/arm/sdei.rst diff --git a/Documentation/virt/kvm/arm/sdei.rst b/Documentation/virt/kvm/a= rm/sdei.rst new file mode 100644 index 000000000000..ba1539978b2c --- /dev/null +++ b/Documentation/virt/kvm/arm/sdei.rst @@ -0,0 +1,64 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +SDEI Virtualization Support for ARM64 +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +ARM specification DEN0054/C defines Software Delegated Exception Interface +(SDEI). It provides a mechanism for registering and servicing system events +from system firmware. The interface is offered by a higher exception level +to a lower exception level, in other words by a secure platform firmware +to hypervisor or hypervisor to OS or both. + +https://developer.arm.com/documentation/den0054/c + +KVM/arm64 implementation follows the specification to support the defined +hypercalls so that the system events can be registered and serviced from +KVM hypervisor to the guest OS. However, some of specified functionalities +are missed by the implementation. + + * The shared event is not supported. It means all supported events are + private. They need to be registered, enabled, disabled, unregistered + and reset from one particular PE (vCPU). + + * The critical priority is not supported. It means all supported events + have normal priority. So there is no preemption between two events + in the critical and normal priorities. One event can be running at + once on one particular PE (vCPU). + + * Interrupt binding event is not supported. It means all supported + events triggered by software. + + * Relative mode for the event handler's address is not supported. + The event handler address is always an absolute address. + +The event handlers, states and context need to be migrated. Several pseudo +firmware registers are added for this. + + * KVM_REG_ARM_SDEI_EVENT_HANDLER_0 + KVM_REG_ARM_SDEI_EVENT_HANDLER_1 + KVM_REG_ARM_SDEI_EVENT_HANDLER_2 + KVM_REG_ARM_SDEI_EVENT_HANDLER_3 + + The event handler's address and argument for events, whose number + are 0, 1, 2 and 3 respectively. Currently, there are only two + supported events, which are Software Signaled Event (0) and Async + PF Event (1). So the first two registers are only used. zeroes are + returned on reading KVM_REG_ARM_SDEI_EVENT_HANDLER_{2, 3}, and the + values written to them are ignored. + + * KVM_REG_ARM_SDEI_EVENT_REGISTERED + KVM_REG_ARM_SDEI_EVENT_ENABLED + KVM_REG_ARM_SDEI_EVENT_RUNNING + KVM_REG_ARM_SDEI_EVENT_PENDING + + They are mapped to the registered, enabled, running and pending + bitmap respectively. + + * KVM_REG_ARM_SDEI_EVENT_CONTEXT + + The interrupted context. + + * KVM_REG_ARM_SDEI_PE_STATE + + Relect the SDEI masked state on the PE (vCPU). --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2867C433F5 for ; Fri, 27 May 2022 08:07:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346425AbiE0IHk (ORCPT ); Fri, 27 May 2022 04:07:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349467AbiE0IHM (ORCPT ); Fri, 27 May 2022 04:07:12 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 200CA104CB4 for ; Fri, 27 May 2022 01:06:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638803; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=U6Q2dD3bxV4gDjNkhQvVENuOaAWqLSTexB84HcYuZeM=; b=Gvv4J0g9amyqqVcet1NVRSYP+Xm7LabTDLOMr2pp1EfEJFsIKnAdVd9BGGy9zNzBVc/Zqv BX/fIv/6hFu4elcM+Qol6n7ZQLBna9I7f4Jcczjf0DQlaiLMHhwTz4WdtEFhAENOI9435i vCTOrgl2oCX09d6X+8KW33KYii/uFZQ= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-29-G0fPdKk8Maqn9_4RZMunmA-1; Fri, 27 May 2022 04:06:39 -0400 X-MC-Unique: G0fPdKk8Maqn9_4RZMunmA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CB6AA1C3E9AB; Fri, 27 May 2022 08:06:38 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 66449202699A; Fri, 27 May 2022 08:06:30 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 21/22] selftests: KVM: aarch64: Add SDEI case in hypercall tests Date: Fri, 27 May 2022 16:02:52 +0800 Message-Id: <20220527080253.1562538-22-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This adds SDEI case in hypercall tests. The added hypercalls are issued to mimic what guest kernel does: reset the shared and private events, unmask PE, register/enable/disable/unregister event, and then mask PE. Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- tools/testing/selftests/kvm/aarch64/hypercalls.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/aarch64/hypercalls.c b/tools/testi= ng/selftests/kvm/aarch64/hypercalls.c index 41e0210b7a5e..1377d2ec8d4b 100644 --- a/tools/testing/selftests/kvm/aarch64/hypercalls.c +++ b/tools/testing/selftests/kvm/aarch64/hypercalls.c @@ -11,6 +11,7 @@ =20 #include #include +#include #include #include =20 @@ -19,7 +20,7 @@ #define FW_REG_ULIMIT_VAL(max_feat_bit) (GENMASK(max_feat_bit, 0)) =20 /* Last valid bits of the bitmapped firmware registers */ -#define KVM_REG_ARM_STD_BMAP_BIT_MAX 0 +#define KVM_REG_ARM_STD_BMAP_BIT_MAX 1 #define KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX 0 #define KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX 1 =20 @@ -68,6 +69,14 @@ static const struct test_hvc_info hvc_info[] =3D { TEST_HVC_INFO(ARM_SMCCC_TRNG_GET_UUID, 0), TEST_HVC_INFO(ARM_SMCCC_TRNG_RND32, 0), TEST_HVC_INFO(ARM_SMCCC_TRNG_RND64, 0), + TEST_HVC_INFO(SDEI_1_0_FN_SDEI_SHARED_RESET, 0), + TEST_HVC_INFO(SDEI_1_0_FN_SDEI_PRIVATE_RESET, 0), + TEST_HVC_INFO(SDEI_1_0_FN_SDEI_PE_UNMASK, 0), + TEST_HVC_INFO(SDEI_1_0_FN_SDEI_EVENT_REGISTER, SDEI_SW_SIGNALED_EVENT), + TEST_HVC_INFO(SDEI_1_0_FN_SDEI_EVENT_ENABLE, SDEI_SW_SIGNALED_EVENT), + TEST_HVC_INFO(SDEI_1_0_FN_SDEI_EVENT_DISABLE, SDEI_SW_SIGNALED_EVENT), + TEST_HVC_INFO(SDEI_1_0_FN_SDEI_EVENT_UNREGISTER, SDEI_SW_SIGNALED_EVENT), + TEST_HVC_INFO(SDEI_1_0_FN_SDEI_PE_MASK, 0), =20 /* KVM_REG_ARM_STD_HYP_BMAP */ TEST_HVC_INFO(ARM_SMCCC_ARCH_FEATURES_FUNC_ID, ARM_SMCCC_HV_PV_TIME_FEATU= RES), --=20 2.23.0 From nobody Tue Apr 28 23:19:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE7E4C433EF for ; Fri, 27 May 2022 08:07:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348818AbiE0IHp (ORCPT ); Fri, 27 May 2022 04:07:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348760AbiE0IHO (ORCPT ); Fri, 27 May 2022 04:07:14 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 181DD13CEF for ; Fri, 27 May 2022 01:06:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653638813; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gwt9QcPDJhVzAwJQz388VeKwOFQslaYIR/LV9tRZHE0=; b=QiZ8ER8ZBV/Omj9KSPM/pvoaIqDYhdEspg935vQiovrcS0VkqkD6GSrM6fKJ9+P1Nxy5nS DwyeJe412D37mG4+/mzl5H/pEvhTPzljnkQDPqqepVWRD0woPCtogVU/NinIwbv3JNduBS LBwjmCTyIdODye8fLUenWZJRnhnGaxQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-255-vVHNWWkYNUqCbb-_vFVP8Q-1; Fri, 27 May 2022 04:06:48 -0400 X-MC-Unique: vVHNWWkYNUqCbb-_vFVP8Q-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8424A811E81; Fri, 27 May 2022 08:06:47 +0000 (UTC) Received: from gshan.redhat.com (ovpn-12-91.pek2.redhat.com [10.72.12.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8FB8020296A2; Fri, 27 May 2022 08:06:39 +0000 (UTC) From: Gavin Shan To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, linux-kernel@vger.kernel.org, eauger@redhat.com, oupton@google.com, Jonathan.Cameron@huawei.com, pbonzini@redhat.com, vkuznets@redhat.com, will@kernel.org, james.morse@arm.com, mark.rutland@arm.com, shannon.zhaosl@gmail.com, shijie@amperemail.onmicrosoft.com, shan.gavin@gmail.com Subject: [PATCH v7 22/22] selftests: KVM: aarch64: Add SDEI test case Date: Fri, 27 May 2022 16:02:53 +0800 Message-Id: <20220527080253.1562538-23-gshan@redhat.com> In-Reply-To: <20220527080253.1562538-1-gshan@redhat.com> References: <20220527080253.1562538-1-gshan@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This adds SDEI selftest case where the various hypercalls are issued to the software signaled event, whose number is zero. The event is private, signaled and in normal priority. By default, two vCPUs are started and the following ioctl commands or hypercalls are sent to them in sequence, to simulate how they are used in VMM and the linux guest: kvm_check_cap(KVM_CAP_ARM_SDEI) (Available functionality) SDEI_1_0_FN_SDEI_VERSION SDEI_1_1_FN_SDEI_FEATURES (SDEI capability probing) SDEI_1_0_FN_SDEI_SHARED_RESET (restart SDEI) SDEI_1_0_FN_SDEI_PE_UNMASK (CPU online) SDEI_1_0_FN_SDEI_EVENT_GET_INFO SDEI_1_0_FN_SDEI_EVENT_REGISTER (register event) SDEI_1_0_FN_SDEI_EVENT_ENABLE (enable event) SDEI_1_1_FN_SDEI_EVENT_SIGNAL (event injection) SDEI_1_0_FN_SDEI_EVENT_DISABLE (disable event) SDEI_1_0_FN_SDEI_EVENT_UNREGISTER (unregister event) SDEI_1_0_FN_SDEI_PE_MASK (CPU offline) Signed-off-by: Gavin Shan Tested-by: Huang Shijie tested-by if respin is needed. Thank you for your time on this. --- tools/testing/selftests/kvm/Makefile | 1 + tools/testing/selftests/kvm/aarch64/sdei.c | 450 +++++++++++++++++++++ 2 files changed, 451 insertions(+) create mode 100644 tools/testing/selftests/kvm/aarch64/sdei.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests= /kvm/Makefile index 81470a99ed1c..39b976a6568f 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -109,6 +109,7 @@ TEST_GEN_PROGS_aarch64 +=3D aarch64/debug-exceptions TEST_GEN_PROGS_aarch64 +=3D aarch64/get-reg-list TEST_GEN_PROGS_aarch64 +=3D aarch64/hypercalls TEST_GEN_PROGS_aarch64 +=3D aarch64/psci_test +TEST_GEN_PROGS_aarch64 +=3D aarch64/sdei TEST_GEN_PROGS_aarch64 +=3D aarch64/vcpu_width_config TEST_GEN_PROGS_aarch64 +=3D aarch64/vgic_init TEST_GEN_PROGS_aarch64 +=3D aarch64/vgic_irq diff --git a/tools/testing/selftests/kvm/aarch64/sdei.c b/tools/testing/sel= ftests/kvm/aarch64/sdei.c new file mode 100644 index 000000000000..7a034d486e83 --- /dev/null +++ b/tools/testing/selftests/kvm/aarch64/sdei.c @@ -0,0 +1,450 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * ARM64 SDEI test + * + * Copyright (C) 2022 Red Hat, Inc. + * + * Author(s): Gavin Shan + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include +#include +#include +#include "processor.h" + +#define NR_VCPUS 2 +#define SDEI_TEST_EVENT_NUM SDEI_SW_SIGNALED_EVENT + +#define VCPU_COMMAND_IDLE 0 +#define VCPU_COMMAND_EXIT 1 + +struct vcpu_command { + const char *name; + uint64_t command; +}; + +struct sdei_feature { + uint16_t shared_slots; + uint16_t private_slots; + uint8_t relative_mode; +}; + +struct sdei_event_info { + uint8_t type; + uint8_t priority; + uint8_t signaled; +}; + +struct sdei_event_signal { + uint8_t handled; + uint8_t irq; + uint64_t status; + uint64_t pc; + uint64_t pstate; + uint64_t regs[18]; +}; + +struct sdei_state { + uint64_t command; + uint64_t num; + uint64_t status; + union { + uint64_t version; + struct sdei_feature feature; + struct sdei_event_info info; + struct sdei_event_signal signal; + }; + + uint8_t command_completed; +}; + +struct vcpu_state { + struct kvm_vm *vm; + uint32_t vcpu_id; + pthread_t thread; + struct sdei_state state; +}; + +extern char vectors; /* VBAR_EL1 */ +static struct vcpu_state vcpu_states[NR_VCPUS]; +static struct vcpu_command vcpu_commands[] =3D { + { "VERSION", SDEI_1_0_FN_SDEI_VERSION }, + { "FEATURES", SDEI_1_1_FN_SDEI_FEATURES }, + { "SHARED_RESET", SDEI_1_0_FN_SDEI_SHARED_RESET }, + { "PRIVATE_RESET", SDEI_1_0_FN_SDEI_PRIVATE_RESET }, + { "PE_UNMASK", SDEI_1_0_FN_SDEI_PE_UNMASK }, + { "EVENT_GET_INFO", SDEI_1_0_FN_SDEI_EVENT_GET_INFO }, + { "EVENT_REGISTER", SDEI_1_0_FN_SDEI_EVENT_REGISTER }, + { "EVENT_ENABLE", SDEI_1_0_FN_SDEI_EVENT_ENABLE }, + { "EVENT_SIGNAL", SDEI_1_1_FN_SDEI_EVENT_SIGNAL }, + { "PE_MASK", SDEI_1_0_FN_SDEI_PE_MASK }, + { "EVENT_DISABLE", SDEI_1_0_FN_SDEI_EVENT_DISABLE }, + { "EVENT_UNREGISTER", SDEI_1_0_FN_SDEI_EVENT_UNREGISTER }, +}; + +static inline bool is_error(int64_t status) +{ + if (status =3D=3D SDEI_NOT_SUPPORTED || + status =3D=3D SDEI_INVALID_PARAMETERS || + status =3D=3D SDEI_DENIED || + status =3D=3D SDEI_PENDING || + status =3D=3D SDEI_OUT_OF_RESOURCE) + return true; + + return false; +} + +static void guest_irq_handler(struct ex_regs *regs) +{ + int vcpu_id =3D guest_get_vcpuid(); + struct sdei_state *state =3D &vcpu_states[vcpu_id].state; + + WRITE_ONCE(state->signal.irq, true); +} + +static void sdei_event_handler(uint64_t num, uint64_t arg, + uint64_t pc, uint64_t pstate) +{ + struct sdei_state *state =3D (struct sdei_state *)arg; + struct arm_smccc_res res; + + smccc_hvc(SDEI_1_0_FN_SDEI_EVENT_STATUS, num, 0, 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->signal.status, res.a0); + + WRITE_ONCE(state->signal.pc, pc); + WRITE_ONCE(state->signal.pstate, pstate); + + smccc_hvc(SDEI_1_0_FN_SDEI_EVENT_CONTEXT, 0, 0, 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->signal.regs[0], res.a0); + smccc_hvc(SDEI_1_0_FN_SDEI_EVENT_CONTEXT, 1, 0, 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->signal.regs[1], res.a0); + smccc_hvc(SDEI_1_0_FN_SDEI_EVENT_CONTEXT, 2, 0, 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->signal.regs[2], res.a0); + smccc_hvc(SDEI_1_0_FN_SDEI_EVENT_CONTEXT, 3, 0, 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->signal.regs[3], res.a0); + + WRITE_ONCE(state->signal.handled, true); + smccc_hvc(SDEI_1_0_FN_SDEI_EVENT_COMPLETE_AND_RESUME, + (uint64_t)&vectors + 0x280, 0, 0, 0, 0, 0, 0, &res); +} + +static void guest_code(int vcpu_id) +{ + struct sdei_state *state; + struct arm_smccc_res res; + uint64_t command, last_command =3D -1UL, num; + + state =3D &vcpu_states[vcpu_id].state; + + while (1) { + command =3D READ_ONCE(state->command); + if (command =3D=3D last_command) + continue; + + num =3D READ_ONCE(state->num); + switch (command) { + case VCPU_COMMAND_IDLE: + WRITE_ONCE(state->status, SDEI_SUCCESS); + break; + case SDEI_1_0_FN_SDEI_VERSION: + smccc_hvc(command, 0, 0, 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->status, res.a0); + if (is_error(res.a0)) + break; + + WRITE_ONCE(state->version, res.a0); + break; + case SDEI_1_0_FN_SDEI_PRIVATE_RESET: + case SDEI_1_0_FN_SDEI_SHARED_RESET: + case SDEI_1_0_FN_SDEI_PE_UNMASK: + case SDEI_1_0_FN_SDEI_PE_MASK: + smccc_hvc(command, 0, 0, 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->status, res.a0); + break; + case SDEI_1_1_FN_SDEI_FEATURES: + smccc_hvc(command, SDEI_FEATURE_BIND_SLOTS, + 0, 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->status, res.a0); + if (is_error(res.a0)) + break; + + WRITE_ONCE(state->feature.shared_slots, + (res.a0 & 0xffff0000) >> 16); + WRITE_ONCE(state->feature.private_slots, + (res.a0 & 0x0000ffff)); + smccc_hvc(command, SDEI_FEATURE_RELATIVE_MODE, + 0, 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->status, res.a0); + if (is_error(res.a0)) + break; + + WRITE_ONCE(state->feature.relative_mode, res.a0); + break; + case SDEI_1_0_FN_SDEI_EVENT_GET_INFO: + smccc_hvc(command, num, SDEI_EVENT_INFO_EV_TYPE, + 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->status, res.a0); + if (is_error(res.a0)) + break; + + WRITE_ONCE(state->info.type, res.a0); + smccc_hvc(command, num, SDEI_EVENT_INFO_EV_PRIORITY, + 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->status, res.a0); + if (is_error(res.a0)) + break; + + WRITE_ONCE(state->info.priority, res.a0); + smccc_hvc(command, num, SDEI_EVENT_INFO_EV_SIGNALED, + 0, 0, 0, 0, 0, &res); + if (is_error(res.a0)) + break; + + WRITE_ONCE(state->info.signaled, res.a0); + break; + case SDEI_1_0_FN_SDEI_EVENT_REGISTER: + smccc_hvc(command, num, (uint64_t)sdei_event_handler, + (uint64_t)state, SDEI_EVENT_REGISTER_RM_ANY, + 0, 0, 0, &res); + WRITE_ONCE(state->status, res.a0); + break; + case SDEI_1_0_FN_SDEI_EVENT_ENABLE: + case SDEI_1_0_FN_SDEI_EVENT_DISABLE: + case SDEI_1_0_FN_SDEI_EVENT_UNREGISTER: + smccc_hvc(command, num, 0, 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->status, res.a0); + break; + case SDEI_1_1_FN_SDEI_EVENT_SIGNAL: + /* + * The injected event should be handled and delivered + * immediately in KVM. + */ + smccc_hvc(command, num, (uint64_t)state, + 0, 0, 0, 0, 0, &res); + WRITE_ONCE(state->status, res.a0); + break; + case VCPU_COMMAND_EXIT: + WRITE_ONCE(state->status, SDEI_SUCCESS); + GUEST_DONE(); + break; + default: + WRITE_ONCE(state->status, SDEI_INVALID_PARAMETERS); + } + + last_command =3D command; + WRITE_ONCE(state->command_completed, true); + } +} + +static void *vcpu_thread(void *arg) +{ + struct vcpu_state *state =3D arg; + + vcpu_run(state->vm, state->vcpu_id); + + return NULL; +} + +static bool vcpu_wait(struct kvm_vm *vm, int timeout_in_seconds) +{ + unsigned long count, limit; + int i; + + count =3D 0; + limit =3D (timeout_in_seconds * 1000000) / 50; + while (1) { + for (i =3D 0; i < NR_VCPUS; i++) { + sync_global_from_guest(vm, vcpu_states[i].state); + if (!vcpu_states[i].state.command_completed) + break; + } + + if (i >=3D NR_VCPUS) + return true; + + if (++count > limit) + return false; + + usleep(50); + } + + return false; +} + +static void vcpu_send_command(struct kvm_vm *vm, uint64_t command) +{ + int i; + + for (i =3D 0; i < NR_VCPUS; i++) { + memset(&vcpu_states[i].state, 0, + sizeof(vcpu_states[0].state)); + vcpu_states[i].state.num =3D SDEI_TEST_EVENT_NUM; + vcpu_states[i].state.status =3D SDEI_SUCCESS; + vcpu_states[i].state.command =3D command; + vcpu_states[i].state.command_completed =3D false; + + sync_global_to_guest(vm, vcpu_states[i].state); + } +} + +static bool vcpu_check_state(struct kvm_vm *vm) +{ + int i, j, ret; + + for (i =3D 0; i < NR_VCPUS; i++) + sync_global_from_guest(vm, vcpu_states[i].state); + + for (i =3D 0; i < NR_VCPUS; i++) { + if (is_error(vcpu_states[i].state.status)) + return false; + + for (j =3D 0; j < NR_VCPUS; j++) { + ret =3D memcmp(&vcpu_states[i].state, + &vcpu_states[j].state, + sizeof(vcpu_states[0].state)); + if (ret) + return false; + } + } + + return true; +} + +static void vcpu_dump_state(int index) +{ + struct sdei_state *state =3D &vcpu_states[0].state; + + pr_info("--- %s\n", vcpu_commands[index].name); + switch (state->command) { + case SDEI_1_0_FN_SDEI_VERSION: + pr_info(" Version: %ld.%ld (vendor: 0x%lx)\n", + SDEI_VERSION_MAJOR(state->version), + SDEI_VERSION_MINOR(state->version), + SDEI_VERSION_VENDOR(state->version)); + break; + case SDEI_1_1_FN_SDEI_FEATURES: + pr_info(" Shared event slots: %d\n", + state->feature.shared_slots); + pr_info(" Private event slots: %d\n", + state->feature.private_slots); + pr_info(" Relative mode: %s\n", + state->feature.relative_mode ? "Yes" : "No"); + break; + case SDEI_1_0_FN_SDEI_EVENT_GET_INFO: + pr_info(" Type: %s\n", + state->info.type =3D=3D SDEI_EVENT_TYPE_SHARED ? + "Shared" : "Private"); + pr_info(" Priority: %s\n", + state->info.priority =3D=3D SDEI_EVENT_PRIORITY_NORMAL ? + "Normal" : "Critical"); + pr_info(" Signaled: %s\n", + state->info.signaled ? "Yes" : "No"); + break; + case SDEI_1_1_FN_SDEI_EVENT_SIGNAL: + pr_info(" Handled: %s\n", + state->signal.handled ? "Yes" : "No"); + pr_info(" IRQ: %s\n", + state->signal.irq ? "Yes" : "No"); + pr_info(" Status: %s-%s-%s\n", + state->signal.status & (1 << SDEI_EVENT_STATUS_REGISTERED) ? + "Registered" : "x", + state->signal.status & (1 << SDEI_EVENT_STATUS_ENABLED) ? + "Enabled" : "x", + state->signal.status & (1 << SDEI_EVENT_STATUS_RUNNING) ? + "Running" : "x"); + pr_info(" PC/PSTATE: %016lx %016lx\n", + state->signal.pc, state->signal.pstate); + pr_info(" Regs: %016lx %016lx %016lx %016lx\n", + state->signal.regs[0], state->signal.regs[1], + state->signal.regs[2], state->signal.regs[3]); + break; + } + + if (index =3D=3D ARRAY_SIZE(vcpu_commands)) + pr_info("\n"); +} + +int main(int argc, char **argv) +{ + struct kvm_vm *vm; + uint32_t vcpu_ids[NR_VCPUS]; + int i, ret; + + if (!kvm_check_cap(KVM_CAP_ARM_SDEI)) { + pr_info("SDEI not supported\n"); + return 0; + } + + /* Create VM */ + for (i =3D 0; i < NR_VCPUS; i++) { + vcpu_states[i].vcpu_id =3D i; + vcpu_ids[i] =3D i; + } + + vm =3D vm_create_default_with_vcpus(NR_VCPUS, 0, 0, + guest_code, vcpu_ids); + vm_init_descriptor_tables(vm); + vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, + guest_irq_handler); + ucall_init(vm, NULL); + + /* Start the vCPUs */ + vcpu_send_command(vm, VCPU_COMMAND_IDLE); + for (i =3D 0; i < NR_VCPUS; i++) { + vcpu_states[i].vm =3D vm; + vcpu_args_set(vm, i, 1, i); + vcpu_init_descriptor_tables(vm, i); + ret =3D pthread_create(&vcpu_states[i].thread, NULL, + vcpu_thread, &vcpu_states[i]); + TEST_ASSERT(!ret, "Failed to create vCPU-%d pthread\n", i); + } + + /* Wait the idle command to complete */ + ret =3D vcpu_wait(vm, 5); + TEST_ASSERT(ret, "Timeout to execute IDLE command\n"); + + /* Start the tests */ + pr_info("\n"); + pr_info(" NR_VCPUS: %d SDEI Event: 0x%08x\n\n", + NR_VCPUS, SDEI_TEST_EVENT_NUM); + for (i =3D 0; i < ARRAY_SIZE(vcpu_commands); i++) { + /* + * We depends on SDEI_EVENT_SIGNAL hypercall to inject SDEI + * event. The number of the injected event must be zero. So + * we have to skip the corresponding test if the SDEI event + * number isn't zero. + */ + if (SDEI_TEST_EVENT_NUM !=3D SDEI_SW_SIGNALED_EVENT && + vcpu_commands[i].command =3D=3D SDEI_1_1_FN_SDEI_EVENT_SIGNAL) + continue; + + vcpu_send_command(vm, vcpu_commands[i].command); + ret =3D vcpu_wait(vm, 5); + if (!ret) { + pr_info("%s: Timeout\n", vcpu_commands[i].name); + return -1; + } + + ret =3D vcpu_check_state(vm); + if (!ret) { + pr_info("%s: Fail\n", vcpu_commands[i].name); + return -1; + } + + vcpu_dump_state(i); + } + + /* Terminate the guests */ + pr_info("\n Result: OK\n\n"); + vcpu_send_command(vm, VCPU_COMMAND_EXIT); + sleep(1); + + return 0; +} --=20 2.23.0