From patchwork Mon Jul 19 16:03:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 12386215 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0611C07E95 for ; Mon, 19 Jul 2021 16:22:31 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 752D261415 for ; Mon, 19 Jul 2021 16:22:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 752D261415 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=WEGh4iEeDdFl/4FS11dbZUKb6Ux/GOw/l+HImjwLOWU=; b=079RGTbjt479d8effF3Grct/KS 3sfNpMOQGHIDNa3q3/SUsFGaHIVL8uC3fZ1ZJ4bfU6FUmTi2T6RbMiAKK/gy5KyYdEq00G2dpc40i ZWjtcc4l7/OhM1hkNHmbjaEM7uKCsgRapY7OWiTxHUD4WMfbGOg3QBMIXtWWtqF6KHaWCOG2OOf7q O1nNvIKIZXMQLlerSdTy8S11uOKP34XmqW3Zg+uDogsLaOxj1fTQXSKuXZczTmcaeaRP2/+ENN8KH zTdFKV+ebOBE87Ka0u3exv/CFqp9RUpnfXlca9vv+Z88rgaKStEKQX2uEUPWKfblohrupf7vqTVdT yVrYXFew==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5W0P-00ARuD-EB; Mon, 19 Jul 2021 16:20:34 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5Vkj-00AKpI-Em for linux-arm-kernel@lists.infradead.org; Mon, 19 Jul 2021 16:04:23 +0000 Received: by mail-wr1-x449.google.com with SMTP id a4-20020adffb840000b02901304c660e75so8865765wrr.19 for ; Mon, 19 Jul 2021 09:04:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=wr4M5V9fqDifu4W3yqELoDlyD/QsVKGAaDZe0uH90bk=; b=h91JnRYTdlcPoijWgxxAfjtdJIrm6H6BOcmFAhwd9pay0PJJ4cC6n1M9vbvnZ6znbT nS/1gN8idExqyOnrE/Bmz/LLcTPnhqFfeS+t1mMiO1wdPBzQPExNqThRPzw01XZUA+H7 gdwMr0REM1mgF7ETdQ3FRIuAdUu2tv277ykNrgB5DnSPRjpGP76y9D0y5p/veJSbXPaC 4CPTxA7GU2+sLTqJORcgVZ7s3n6pyJ/1grd2vbUGVhIssCG9VpF9EJsTsBHAfdzO6UJm 4rntEOyEpDDnjgVG6RdAynoK+s7ZurB1veuHFd8Ap9cmHCr6tOSp+897JO54o+IABZLh QPwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=wr4M5V9fqDifu4W3yqELoDlyD/QsVKGAaDZe0uH90bk=; b=N3TRw5ub20QENM8OAMuf2lkEtQb2Z+vRhOXKOsw5jNcOsAhfyILN9Qfj+8KCALIY+b TeiVREzC3+upi8sv1iahEwLjtZBX39rNhaoJyUNDBQ2s9PlAZNoL4jLqx4zktOtdhFbr lg7WDwcZ2IfmIXIHnjvKNb8C5+jv9C2RHEeBILDTYX2pGhBS7hEa3sSoXSb+tK01xq35 6BXbMJ6TIFkV1NUx50Rm+TVWHMJsYYVA6un9TNS4PZ4i4a1y9XVNDzie3JL0tAZhVAVS b0g99ZSsexwQ0ecAj7UFPulBmFQK9E8L7xjHrWh4pzOuIMv12FW+do6C8dNJo4b4zasc fBjw== X-Gm-Message-State: AOAM530FhqX79UksIcyku9Eq1dvkxNLzbPAnUlOgaMmaNip9I0Y0JFzn WD4lS4fEO+U6kd+vtICDBgg6G21USg== X-Google-Smtp-Source: ABdhPJzzYFHkSS+rg5cJ0eIx8EjkgLZ9WyOnX41eM+jzRisgCK/ewgy3OonzA0LHza8N9WIoUOHe3/73yw== X-Received: from tabba.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:482]) (user=tabba job=sendgmr) by 2002:a5d:530c:: with SMTP id e12mr29810469wrv.130.1626710659125; Mon, 19 Jul 2021 09:04:19 -0700 (PDT) Date: Mon, 19 Jul 2021 17:03:46 +0100 In-Reply-To: <20210719160346.609914-1-tabba@google.com> Message-Id: <20210719160346.609914-16-tabba@google.com> Mime-Version: 1.0 References: <20210719160346.609914-1-tabba@google.com> X-Mailer: git-send-email 2.32.0.402.g57bb445576-goog Subject: [PATCH v3 15/15] KVM: arm64: Restrict protected VM capabilities From: Fuad Tabba To: kvmarm@lists.cs.columbia.edu Cc: maz@kernel.org, will@kernel.org, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, mark.rutland@arm.com, christoffer.dall@arm.com, pbonzini@redhat.com, drjones@redhat.com, qperret@google.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel-team@android.com, tabba@google.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210719_090421_598570_519E95AA X-CRM114-Status: GOOD ( 17.28 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Restrict protected VM capabilities based on the fixed-configuration for protected VMs. No functional change intended in current KVM-supported modes (nVHE, VHE). Signed-off-by: Fuad Tabba --- arch/arm64/include/asm/kvm_fixed_config.h | 10 ++++ arch/arm64/kvm/arm.c | 63 ++++++++++++++++++++++- arch/arm64/kvm/pkvm.c | 30 +++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/kvm_fixed_config.h b/arch/arm64/include/asm/kvm_fixed_config.h index b39a5de2c4b9..14310b035bf7 100644 --- a/arch/arm64/include/asm/kvm_fixed_config.h +++ b/arch/arm64/include/asm/kvm_fixed_config.h @@ -175,4 +175,14 @@ */ #define PVM_ID_AA64ISAR1_ALLOW (~0ULL) +/* + * Returns the maximum number of breakpoints supported for protected VMs. + */ +int kvm_arm_pkvm_get_max_brps(void); + +/* + * Returns the maximum number of watchpoints supported for protected VMs. + */ +int kvm_arm_pkvm_get_max_wrps(void); + #endif /* __ARM64_KVM_FIXED_CONFIG_H__ */ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 3f28549aff0d..bc41e3b71fab 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -188,9 +189,10 @@ void kvm_arch_destroy_vm(struct kvm *kvm) atomic_set(&kvm->online_vcpus, 0); } -int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) +static int kvm_check_extension(struct kvm *kvm, long ext) { int r; + switch (ext) { case KVM_CAP_IRQCHIP: r = vgic_present; @@ -281,6 +283,65 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) return r; } +static int pkvm_check_extension(struct kvm *kvm, long ext, int kvm_cap) +{ + int r; + + switch (ext) { + case KVM_CAP_ARM_PSCI: + case KVM_CAP_ARM_PSCI_0_2: + case KVM_CAP_NR_VCPUS: + case KVM_CAP_MAX_VCPUS: + case KVM_CAP_MAX_VCPU_ID: + r = kvm_cap; + break; + case KVM_CAP_ARM_EL1_32BIT: + r = kvm_cap && + (FIELD_GET(FEATURE(ID_AA64PFR0_EL1), PVM_ID_AA64PFR0_ALLOW) >= + ID_AA64PFR0_ELx_32BIT_64BIT); + break; + case KVM_CAP_GUEST_DEBUG_HW_BPS: + r = min(kvm_cap, kvm_arm_pkvm_get_max_brps()); + break; + case KVM_CAP_GUEST_DEBUG_HW_WPS: + r = min(kvm_cap, kvm_arm_pkvm_get_max_wrps()); + break; + case KVM_CAP_ARM_PMU_V3: + r = kvm_cap && + FIELD_GET(FEATURE(ID_AA64DFR0_PMUVER), PVM_ID_AA64DFR0_ALLOW); + break; + case KVM_CAP_ARM_SVE: + r = kvm_cap && + FIELD_GET(FEATURE(ID_AA64PFR0_SVE), PVM_ID_AA64PFR0_ALLOW); + break; + case KVM_CAP_ARM_PTRAUTH_ADDRESS: + r = kvm_cap && + FIELD_GET(FEATURE(ID_AA64ISAR1_API), PVM_ID_AA64ISAR1_ALLOW) && + FIELD_GET(FEATURE(ID_AA64ISAR1_APA), PVM_ID_AA64ISAR1_ALLOW); + break; + case KVM_CAP_ARM_PTRAUTH_GENERIC: + r = kvm_cap && + FIELD_GET(FEATURE(ID_AA64ISAR1_GPI), PVM_ID_AA64ISAR1_ALLOW) && + FIELD_GET(FEATURE(ID_AA64ISAR1_GPA), PVM_ID_AA64ISAR1_ALLOW); + break; + default: + r = 0; + break; + } + + return r; +} + +int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) +{ + int r = kvm_check_extension(kvm, ext); + + if (unlikely(kvm && kvm_vm_is_protected(kvm))) + r = pkvm_check_extension(kvm, ext, r); + + return r; +} + long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c index b8430b3d97af..d41553594d08 100644 --- a/arch/arm64/kvm/pkvm.c +++ b/arch/arm64/kvm/pkvm.c @@ -181,3 +181,33 @@ void kvm_init_protected_traps(struct kvm_vcpu *vcpu) pvm_init_traps_aa64mmfr0(vcpu); pvm_init_traps_aa64mmfr1(vcpu); } + +int kvm_arm_pkvm_get_max_brps(void) +{ + int num = FIELD_GET(FEATURE(ID_AA64DFR0_BRPS), PVM_ID_AA64DFR0_ALLOW); + + /* + * If breakpoints are supported, the maximum number is 1 + the field. + * Otherwise, return 0, which is not compliant with the architecture, + * but is reserved and is used here to indicate no debug support. + */ + if (num) + return 1 + num; + else + return 0; +} + +int kvm_arm_pkvm_get_max_wrps(void) +{ + int num = FIELD_GET(FEATURE(ID_AA64DFR0_WRPS), PVM_ID_AA64DFR0_ALLOW); + + /* + * If breakpoints are supported, the maximum number is 1 + the field. + * Otherwise, return 0, which is not compliant with the architecture, + * but is reserved and is used here to indicate no debug support. + */ + if (num) + return 1 + num; + else + return 0; +}