Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp2689405pxf; Sun, 4 Apr 2021 10:26:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyuYDlgk7Myr4K5G2Nx9xTWeNxyUVPaPsD40d80h17Ya3ONT2fNMgmlbI3GDi4z4VWlayCR X-Received: by 2002:a92:8e4f:: with SMTP id k15mr2815991ilh.16.1617557205490; Sun, 04 Apr 2021 10:26:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617557205; cv=none; d=google.com; s=arc-20160816; b=Ik4artOy/6dlFLHXXGfA/PrScsAxu2yH8GMWutekhhsJ+RUbdftIKk8/AZgzHmRwjt xiQ3pBTUMmvCFvhhCPVl+57yaIgLNBsID0rilypWvBu9vRB/n5XcIUlqDe9NvBQZTr8/ F6fXy7PR7F/9L/h4wffghjNneT73YExSP3C5fuuU0oob2ugJLgIXKeZJ7I6f6AtzkhtM J8iq6KWBDsj0IGz7Wom4n1K9dFjzNjsUI27w34dmAany2h/aBqHzEnhm+MjCxfQNo1Oi z3UxAeHhdBZ2S4jcr1OKV+ZV0+pN7Dm++TQN66cVTwcRs8tge5FL//PPaO+WCOt4/Ugo tdoQ== 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=eOYpukDLmGfOzd0Aol5byEkjqVTZ4sRQkcES7s56wrE=; b=P1h51HzIffJaoI6N+yIMc1c2UUYQ4fx/6MIms3IzsN3Phb4xGFOD4K+mN+vrjR00sj N//yA8GR2Mqv2s+6n309cyy+pZfKri1CwVHM1UgsY48psyp8ashj8I9YFxkgvAL/pG6d 8q5s5qGL/z+j5n01h+Sqiy3vSqyvUbbI7lCP+uCjmymar7z7IDsQ005Es6Bp86r3UWFO ga5y+0NDmD9tpdYasWbFaF+3BsMZK2n9MwyctdqB2RfMSE0EYSzvWqldbXQavFo02H1J K+xu4A24IpUQNI9LyCopZXNwME9gZ7IMPabuvuSH3D6FR9lUduI8UnL39xxO+03rFbHv ntiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="LQ0Bp/jJ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y10si13793684iow.59.2021.04.04.10.26.32; Sun, 04 Apr 2021 10:26:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="LQ0Bp/jJ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231362AbhDDRY4 (ORCPT + 99 others); Sun, 4 Apr 2021 13:24:56 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:45607 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231469AbhDDRX0 (ORCPT ); Sun, 4 Apr 2021 13:23:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617557001; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eOYpukDLmGfOzd0Aol5byEkjqVTZ4sRQkcES7s56wrE=; b=LQ0Bp/jJ4UWQtG1rFfaCfrCdK8TdVK2Y8PQ/FhaMF5boMfj0MTJ0ElAA76boosMyf600Tl VJJ8SSgUNELGFUtkSzqGDwVur/vJ+1wj1B7dm0E3U1kXD+EtHOjLK5VnwFaVwkt3uzw5Rx HmPMtIpfsF2F2c/+iOShnSD87FrDaVI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-14-B1LXOZf1PRyB_4VxhHNF5g-1; Sun, 04 Apr 2021 13:23:19 -0400 X-MC-Unique: B1LXOZf1PRyB_4VxhHNF5g-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 55469189CD04; Sun, 4 Apr 2021 17:23:18 +0000 (UTC) Received: from laptop.redhat.com (ovpn-112-13.ams2.redhat.com [10.36.112.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id 36D2710027C4; Sun, 4 Apr 2021 17:23:14 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, maz@kernel.org, drjones@redhat.com, alexandru.elisei@arm.com Cc: james.morse@arm.com, suzuki.poulose@arm.com, shuah@kernel.org, pbonzini@redhat.com Subject: [PATCH v5 7/8] KVM: arm64: vgic-v3: Expose GICR_TYPER.Last for userspace Date: Sun, 4 Apr 2021 19:22:42 +0200 Message-Id: <20210404172243.504309-8-eric.auger@redhat.com> In-Reply-To: <20210404172243.504309-1-eric.auger@redhat.com> References: <20210404172243.504309-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit 23bde34771f1 ("KVM: arm64: vgic-v3: Drop the reporting of GICR_TYPER.Last for userspace") temporarily fixed a bug identified when attempting to access the GICR_TYPER register before the redistributor region setting, but dropped the support of the LAST bit. Emulating the GICR_TYPER.Last bit still makes sense for architecture compliance though. This patch restores its support (if the redistributor region was set) while keeping the code safe. We introduce a new helper, vgic_mmio_vcpu_rdist_is_last() which computes whether a redistributor is the highest one of a series of redistributor contributor pages. With this new implementation we do not need to have a uaccess read accessor anymore. Signed-off-by: Eric Auger --- v4 -> v5: - redist region list now is sorted by @base - change the implementation according to Marc's understanding of the spec --- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 58 +++++++++++++++++------------- include/kvm/arm_vgic.h | 1 + 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/arch/arm64/kvm/vgic/vgic-mmio-v3.c b/arch/arm64/kvm/vgic/vgic-mmio-v3.c index e1ed0c5a8eaa..03a253785700 100644 --- a/arch/arm64/kvm/vgic/vgic-mmio-v3.c +++ b/arch/arm64/kvm/vgic/vgic-mmio-v3.c @@ -251,45 +251,52 @@ static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu, vgic_enable_lpis(vcpu); } +static bool vgic_mmio_vcpu_rdist_is_last(struct kvm_vcpu *vcpu) +{ + struct vgic_dist *vgic = &vcpu->kvm->arch.vgic; + struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + struct vgic_redist_region *iter, *rdreg = vgic_cpu->rdreg; + + if (!rdreg) + return false; + + if (vgic_cpu->rdreg_index < rdreg->free_index - 1) { + return false; + } else if (rdreg->count && vgic_cpu->rdreg_index == (rdreg->count - 1)) { + struct list_head *rd_regions = &vgic->rd_regions; + gpa_t end = rdreg->base + rdreg->count * KVM_VGIC_V3_REDIST_SIZE; + + /* + * the rdist is the last one of the redist region, + * check whether there is no other contiguous rdist region + */ + list_for_each_entry(iter, rd_regions, list) { + if (iter->base == end && iter->free_index > 0) + return false; + } + } + return true; +} + static unsigned long vgic_mmio_read_v3r_typer(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len) { unsigned long mpidr = kvm_vcpu_get_mpidr_aff(vcpu); - struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; - struct vgic_redist_region *rdreg = vgic_cpu->rdreg; int target_vcpu_id = vcpu->vcpu_id; - gpa_t last_rdist_typer = rdreg->base + GICR_TYPER + - (rdreg->free_index - 1) * KVM_VGIC_V3_REDIST_SIZE; u64 value; value = (u64)(mpidr & GENMASK(23, 0)) << 32; value |= ((target_vcpu_id & 0xffff) << 8); - if (addr == last_rdist_typer) + if (vgic_has_its(vcpu->kvm)) + value |= GICR_TYPER_PLPIS; + + if (vgic_mmio_vcpu_rdist_is_last(vcpu)) value |= GICR_TYPER_LAST; - if (vgic_has_its(vcpu->kvm)) - value |= GICR_TYPER_PLPIS; return extract_bytes(value, addr & 7, len); } -static unsigned long vgic_uaccess_read_v3r_typer(struct kvm_vcpu *vcpu, - gpa_t addr, unsigned int len) -{ - unsigned long mpidr = kvm_vcpu_get_mpidr_aff(vcpu); - int target_vcpu_id = vcpu->vcpu_id; - u64 value; - - value = (u64)(mpidr & GENMASK(23, 0)) << 32; - value |= ((target_vcpu_id & 0xffff) << 8); - - if (vgic_has_its(vcpu->kvm)) - value |= GICR_TYPER_PLPIS; - - /* reporting of the Last bit is not supported for userspace */ - return extract_bytes(value, addr & 7, len); -} - static unsigned long vgic_mmio_read_v3r_iidr(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len) { @@ -612,7 +619,7 @@ static const struct vgic_register_region vgic_v3_rd_registers[] = { VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_TYPER, vgic_mmio_read_v3r_typer, vgic_mmio_write_wi, - vgic_uaccess_read_v3r_typer, vgic_mmio_uaccess_write_wi, 8, + NULL, vgic_mmio_uaccess_write_wi, 8, VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH(GICR_WAKER, vgic_mmio_read_raz, vgic_mmio_write_wi, 4, @@ -714,6 +721,7 @@ int vgic_register_redist_iodev(struct kvm_vcpu *vcpu) return -EINVAL; vgic_cpu->rdreg = rdreg; + vgic_cpu->rdreg_index = rdreg->free_index; rd_base = rdreg->base + rdreg->free_index * KVM_VGIC_V3_REDIST_SIZE; diff --git a/include/kvm/arm_vgic.h b/include/kvm/arm_vgic.h index 3d74f1060bd1..ec621180ef09 100644 --- a/include/kvm/arm_vgic.h +++ b/include/kvm/arm_vgic.h @@ -322,6 +322,7 @@ struct vgic_cpu { */ struct vgic_io_device rd_iodev; struct vgic_redist_region *rdreg; + u32 rdreg_index; /* Contains the attributes and gpa of the LPI pending tables. */ u64 pendbaser; -- 2.26.3