Received: by 2002:ac2:464d:0:0:0:0:0 with SMTP id s13csp2001880lfo; Sat, 28 May 2022 13:25:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJze/1FsZip0Rpdo6YCoQftxcrYBaGGnksokTvk2bc3BUlyVWPBA2jowzZmd3D+RS8GuH4Mz X-Received: by 2002:a17:90b:4b90:b0:1e0:1b0:1a1 with SMTP id lr16-20020a17090b4b9000b001e001b001a1mr15044254pjb.70.1653769517325; Sat, 28 May 2022 13:25:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653769517; cv=none; d=google.com; s=arc-20160816; b=H3H4WY82wDiuxFkrnp0PlwPVFMpMDzhA85p9gyV57JadA9r8GchkEnhMplHnjFI0i+ V16Uw/oyLrhcDalWxFXam26l2jjJKhGJgJ+zOmBzTX1kJdAgZZhoexK0JNgnvlmDNpIG ipZB9XYFgv2K6mDEbiij1a5CIt0KNDnnvUR7oovYHMNxgWMC6OHW8N1Y0EiA4VIMvymF 1bJZK0hOOj6H91KwWAP1JDnEyUzGB01F0/N9xkPVcNFZCPNF07M4Te+xHU3SMV1Xhg6W SfSCNh7rGjxb9x08/93BMv7ZFNUUDB6/ZnNiUUV/z3W4o4M+IIYkZoKWbIpnKqdq7f77 Rn8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=M1IZs3klzI4SDOe9ylEC0ku9fFW+/8u54cvhU+BDFz0=; b=N1/1lmdHV1vwth496s6HsgFc6SJ+WfmzUgjQHsuNgzVSvORFm97afM6xHLAFF2iIge 3WxUi2I5MbWfwprFdtD5ts2eeFlRGfvtoj0XtKI1Pptdfv0GlmOzL01MKMvyDIRzOJwS RjW2aCWa7jF7cHf2Skc5VhBTz4502gseDXTAW8uJA7+kOgeJnQCthe5metxT2SfJiRu3 f8KcBWSHzO2A42eFYb94vLENWGN2YwQpLfXePGXN3ruUPlzU7XanpVZo/d18RgPLdtt/ 0XqREcqdWzDclyBFPnj+sku6MigKZei8c3EcgUWoi2FaowPWaXLZoXGtEHrT62G1jk/3 5lZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QWVsj9HZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id o18-20020a63fb12000000b003fbd727a15asi3542539pgh.670.2022.05.28.13.25.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 May 2022 13:25:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QWVsj9HZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9946515A38; Sat, 28 May 2022 12:32:39 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238361AbiE0IFx (ORCPT + 99 others); Fri, 27 May 2022 04:05:53 -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-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 { /* 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); 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); + 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; } +int kvm_sdei_inject_event(struct kvm_vcpu *vcpu, + unsigned int num, + bool immediate) +{ + struct kvm_sdei_vcpu *vsdei = vcpu->arch.sdei; + + if (!vsdei) + return -EPERM; + + if (num >= 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 = vcpu->arch.sdei; + + if (!vsdei) + return -EPERM; + + if (num >= 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 = vcpu->arch.sdei; + struct kvm_sdei_event_context *ctxt = &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 = 0; + while (num < KVM_NR_SDEI_EVENTS) { + num = find_next_bit(&vsdei->pending, KVM_NR_SDEI_EVENTS, num); + if (test_bit(num, &vsdei->enabled)) + break; + } + + if (num >= 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 = *vcpu_pc(vcpu); + ctxt->pstate = *vcpu_cpsr(vcpu); + for (i = 0; i < ARRAY_SIZE(ctxt->regs); i++) + ctxt->regs[i] = 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 = 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 = ctxt->pstate; + pstate &= ~(PSR_MODE32_BIT | PSR_MODE_MASK); + pstate |= (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT | PSR_MODE_EL1h); + + *vcpu_cpsr(vcpu) = pstate; + *vcpu_pc(vcpu) = 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; -- 2.23.0