Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp4093050pxb; Mon, 4 Oct 2021 17:21:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbMYE5B90PDbO74mnebV6xTCfQ8Lkoofjy5Dt+GlHGK9RmmvsTGfLZ0wrSIHikZYgP+n9M X-Received: by 2002:a50:fc06:: with SMTP id i6mr6052917edr.370.1633393266117; Mon, 04 Oct 2021 17:21:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633393266; cv=none; d=google.com; s=arc-20160816; b=QWEs46WQSDpb0hboDlAZXLkQwbuxmSnPNTsh9n7YMLcQOQj83M/WjqpTGkQfw5VtVU 5h1oHv2zNGEh93NjdpewdFW2CC4LLh7GHY8MZFcy05LUx8uFQRfLagVDvCUm66VPapWN cXTNPWe4uuUZ1bWBMkb4sTiTCQ/UP8bY2GPzgg6t4eo8qn6zwjrpYJL3nzMIMTmJD2Ge WvRyvcnkrK3n3w0CmvWfWTQTBMEzRpCFsWd73nhdQhBkcfF0pIY1GOPqZQUxeEQBjeyZ wUgdwr+3jOPbt3vo/QoJk7fY8hJeD5BKujZad5vrH+tW0Sn+xbWimr7scJP6tLp6MGq1 9lYw== 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; bh=btpFVXG08NE6R5N7XBFvYFn+DR3oz23IwDpnvhRkRP0=; b=yTkQB6zpWhZx2MVbfhhKcwqmvwFL+onolrJFSswdyEZ8VoXmwuy5f0hHJi5LoKBlqE WQJLt3v60IZZ7j7n94nqItcUne1P4kn38YHPLB0Ry1KtnVRZA4rY9WgOLDIhbr6jNpVT yoMB+GYi//nwIqKtg2xy+ZltaXGGO1S1w1N/UP1nYMm9w+H6A0zMWtsNC2El4bzKhu4u wO0f+hc4J6s7rBNwQbZYCqhAn/6u/ODMCxROIBFLxGdygbB8+41OS0aCJ+Wp254A5KHF jINpVLXHM4IAov1jZb2bAWKK5J+MidQQMVpA6dmrCvpc6C9RYFdiWHMqshMt/joiBtXi gutA== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w8si25999981ede.307.2021.10.04.17.20.42; Mon, 04 Oct 2021 17:21:06 -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; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238355AbhJDRvT (ORCPT + 99 others); Mon, 4 Oct 2021 13:51:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:32924 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235335AbhJDRu4 (ORCPT ); Mon, 4 Oct 2021 13:50:56 -0400 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id F0DD96120C; Mon, 4 Oct 2021 17:49:03 +0000 (UTC) Received: from sofa.misterjones.org ([185.219.108.64] helo=why.lan) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1mXS5G-00EhBv-AV; Mon, 04 Oct 2021 18:49:02 +0100 From: Marc Zyngier To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: will@kernel.org, qperret@google.com, dbrazdil@google.com, Steven Price , Andrew Jones , Fuad Tabba , Srivatsa Vaddagiri , Shanker R Donthineni , James Morse , Suzuki K Poulose , Alexandru Elisei , kernel-team@android.com Subject: [PATCH v2 09/16] KVM: arm64: Advertise a capability for MMIO guard Date: Mon, 4 Oct 2021 18:48:42 +0100 Message-Id: <20211004174849.2831548-10-maz@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211004174849.2831548-1-maz@kernel.org> References: <20211004174849.2831548-1-maz@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, will@kernel.org, qperret@google.com, dbrazdil@google.com, steven.price@arm.com, drjones@redhat.com, tabba@google.com, vatsa@codeaurora.org, sdonthineni@nvidia.com, james.morse@arm.com, suzuki.poulose@arm.com, alexandru.elisei@arm.com, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In order for userspace to find out whether the MMIO guard is exposed to a guest, expose a capability that says so. We take this opportunity to make it incompatible with the NISV option, as that would be rather counter-productive! Signed-off-by: Marc Zyngier --- arch/arm64/kvm/arm.c | 29 ++++++++++++++++++----------- arch/arm64/kvm/hypercalls.c | 14 ++++++++++++-- include/uapi/linux/kvm.h | 1 + 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index ed9c89ec0b4f..1c9a7abe2728 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -81,32 +81,33 @@ int kvm_arch_check_processor_compat(void *opaque) int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) { - int r; + int r = -EINVAL; if (cap->flags) return -EINVAL; + mutex_lock(&kvm->lock); + switch (cap->cap) { case KVM_CAP_ARM_NISV_TO_USER: - r = 0; - set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER, - &kvm->arch.flags); + /* This is incompatible with MMIO guard */ + if (!test_bit(KVM_ARCH_FLAG_MMIO_GUARD, &kvm->arch.flags)) { + r = 0; + set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER, + &kvm->arch.flags); + } break; case KVM_CAP_ARM_MTE: - mutex_lock(&kvm->lock); - if (!system_supports_mte() || kvm->created_vcpus) { - r = -EINVAL; - } else { + if (system_supports_mte() && !kvm->created_vcpus) { r = 0; set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags); } - mutex_unlock(&kvm->lock); break; default: - r = -EINVAL; break; } + mutex_unlock(&kvm->lock); return r; } @@ -211,13 +212,19 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_IMMEDIATE_EXIT: case KVM_CAP_VCPU_EVENTS: case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2: - case KVM_CAP_ARM_NISV_TO_USER: case KVM_CAP_ARM_INJECT_EXT_DABT: case KVM_CAP_SET_GUEST_DEBUG: case KVM_CAP_VCPU_ATTRIBUTES: case KVM_CAP_PTP_KVM: r = 1; break; + case KVM_CAP_ARM_NISV_TO_USER: + r = !test_bit(KVM_ARCH_FLAG_MMIO_GUARD, &kvm->arch.flags); + break; + case KVM_CAP_ARM_MMIO_GUARD: + r = !test_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER, + &kvm->arch.flags); + break; case KVM_CAP_SET_GUEST_DEBUG2: return KVM_GUESTDBG_VALID_MASK; case KVM_CAP_ARM_SET_DEVICE_ADDR: diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index c39aab55ecae..e4fade6a96f6 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -59,6 +59,14 @@ static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) val[3] = lower_32_bits(cycles); } +static bool mmio_guard_allowed(struct kvm_vcpu *vcpu) +{ + return (!test_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER, + &vcpu->kvm->arch.flags) && + !vcpu_mode_is_32bit(vcpu)); + +} + int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) { u32 func_id = smccc_get_function(vcpu); @@ -131,7 +139,7 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) val[0] = BIT(ARM_SMCCC_KVM_FUNC_FEATURES); val[0] |= BIT(ARM_SMCCC_KVM_FUNC_PTP); /* Only advertise MMIO guard to 64bit guests */ - if (!vcpu_mode_is_32bit(vcpu)) { + if (mmio_guard_allowed(vcpu)) { val[0] |= BIT(ARM_SMCCC_KVM_FUNC_MMIO_GUARD_INFO); val[0] |= BIT(ARM_SMCCC_KVM_FUNC_MMIO_GUARD_ENROLL); val[0] |= BIT(ARM_SMCCC_KVM_FUNC_MMIO_GUARD_MAP); @@ -146,10 +154,12 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) val[0] = PAGE_SIZE; break; case ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_ENROLL_FUNC_ID: - if (!vcpu_mode_is_32bit(vcpu)) { + mutex_lock(&vcpu->kvm->lock); + if (mmio_guard_allowed(vcpu)) { set_bit(KVM_ARCH_FLAG_MMIO_GUARD, &vcpu->kvm->arch.flags); val[0] = SMCCC_RET_SUCCESS; } + mutex_unlock(&vcpu->kvm->lock); break; case ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID: if (!vcpu_mode_is_32bit(vcpu) && diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index a067410ebea5..ef171186e7be 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1112,6 +1112,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_BINARY_STATS_FD 203 #define KVM_CAP_EXIT_ON_EMULATION_FAILURE 204 #define KVM_CAP_ARM_MTE 205 +#define KVM_CAP_ARM_MMIO_GUARD 206 #ifdef KVM_CAP_IRQ_ROUTING -- 2.30.2