From patchwork Tue Sep 19 17:50:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jing Zhang X-Patchwork-Id: 13391748 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 4EFA9CE79AA for ; Tue, 19 Sep 2023 17:51:56 +0000 (UTC) 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:Message-ID: References:Mime-Version: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=KmlSJBvTpssh7W61NBgUtURCGKhPmp3bVQyCys79nWs=; b=dZkUAwDxkOl9esJeN+89QaAo2j ky12uq2qZuVs1uckMfU11GYObVrllFRjZyBvgvT+tpX3W0k+fgc8XgfMgSH7k1ZgGe7I/zbYT5mvX 6/W/q2NvcFkiCJ3vcrplQPKz0y/QdWMkOHuJrhHXUUH553lxpuE4gmuWDzN+XcqytgzKNJJz1eRw9 JREjtM7QYq7PngrJlx5uU4x5jHxK3ekrNtIHKpHnxy/Bfn50mNc8AJEgfV2L6epohWRgC6CYFUrTD lwDUb32UaOT57RHCYYSZYL9Ksbagdin/uhwIRZ2ynO/tidRaYbp0AazaQt2eAwKm+JS0rHxU35ADb Do5I8WSg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qiesi-000zvk-21; Tue, 19 Sep 2023 17:51:28 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qierr-000zMo-1c for linux-arm-kernel@lists.infradead.org; Tue, 19 Sep 2023 17:50:37 +0000 Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-274b736caf3so2660687a91.2 for ; Tue, 19 Sep 2023 10:50:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695145832; x=1695750632; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WDqS9gCK2hWJbMjalAxBpmnQXFt//tHeeybGxVMKHYk=; b=W50nmnNuP+zh5JGpB1xuupTFxpE3UCkuX3XLUE0PCClRqJ5LNX0awjYMYuK1Fts/iA LOJpKBPD/QFJawCVNnYrUbPH7ERMcEDjIiymElit13U/FnX6DT+pvMKfCjjBHhIcZEVy j/vBJ2tmGEMDx+NtBJO2SRpnxv1Xwp5yIkaCe0hFwg9QQYKUagGoXtxeRONRVZjUH6o0 yQOWdnzVV/FgFkJuYDfdzhJzJlrWq4F5r8AndtyjfOgq/bI+DN8KcbbKL8lxcMa2pTjp 1YjtkZNNqZbQ4JpagcgmFbvNQ5OHNSNWeajuFxaOXZirnfYQ0eb0qzShcqw3jiXhbKa/ E9Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695145832; x=1695750632; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WDqS9gCK2hWJbMjalAxBpmnQXFt//tHeeybGxVMKHYk=; b=F4cziUSTKECk7QBbuvq1PNw89evR2fkUyYlhKUli1rLfbvTUG+ww6aYZgcyfFfJ7tJ e5y++my9/dDN2T0l2NJ98KYH52fRNQYCIk4copC0PwfHkmgbtIATUI23R40EQ9VVLPCG iLM6aJhYM5kM4WONtxDO3d9TTD2jwm9OsGH+R93/AQYvC5s19KB9RM2jOJYRpoDWEJPB Lc7bxxmk2NUmu08cfrEWE8UU1hLgxHtrqfRbmzGOkJSrwFd5O2M7ze6UwnM2rVkXoaXi XJ9hfWoBjnmI1LECGndHXaC8GLCMbeonqtig/yXSa98axY8RFJU1aKHI+gaxaBMbkeQN P1vA== X-Gm-Message-State: AOJu0YzJW0BVlTEWX3xsAfCuPSTqxwxYKwrV2Y5FZ7UvPjm7ep5yumrW cMhflMeYMJ+2MjO/wGOgWP+Iu7Nq29sIzYn4Tw== X-Google-Smtp-Source: AGHT+IFN34HCsWwHcm0VPaYd7vZlKFFWGc9Vo3Vmh+MXi2TsekzYPzoQupklZxM0iu6EWzoGRPjs/bXawpslssLVYQ== X-Received: from jgzg.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1acf]) (user=jingzhangos job=sendgmr) by 2002:a17:90a:fa12:b0:268:1d63:b9ae with SMTP id cm18-20020a17090afa1200b002681d63b9aemr8189pjb.3.1695145831933; Tue, 19 Sep 2023 10:50:31 -0700 (PDT) Date: Tue, 19 Sep 2023 10:50:16 -0700 In-Reply-To: <20230919175017.538312-1-jingzhangos@google.com> Mime-Version: 1.0 References: <20230919175017.538312-1-jingzhangos@google.com> X-Mailer: git-send-email 2.42.0.459.ge4e396fd5e-goog Message-ID: <20230919175017.538312-5-jingzhangos@google.com> Subject: [PATCH v1 4/4] KVM: arm64: Reject attempts to set invalid debug arch version From: Jing Zhang To: KVM , KVMARM , ARMLinux , Marc Zyngier , Oliver Upton Cc: Will Deacon , Paolo Bonzini , James Morse , Alexandru Elisei , Suzuki K Poulose , Fuad Tabba , Suraj Jitindar Singh , Cornelia Huck , Shaoqin Huang , Jing Zhang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230919_105035_576065_EA172509 X-CRM114-Status: GOOD ( 16.55 ) 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 From: Oliver Upton The debug architecture is mandatory in ARMv8, so KVM should not allow userspace to configure a vCPU with less than that. Of course, this isn't handled elegantly by the generic ID register plumbing, as the respective ID register fields have a nonzero starting value. Add an explicit check for debug versions less than v8 of the architecture. Signed-off-by: Oliver Upton Signed-off-by: Jing Zhang --- arch/arm64/kvm/sys_regs.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 4dcc9272fbb8..fdebd9d042c3 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1216,8 +1216,14 @@ static s64 kvm_arm64_ftr_safe_value(u32 id, const struct arm64_ftr_bits *ftrp, /* Some features have different safe value type in KVM than host features */ switch (id) { case SYS_ID_AA64DFR0_EL1: - if (kvm_ftr.shift == ID_AA64DFR0_EL1_PMUVer_SHIFT) + switch (kvm_ftr.shift) { + case ID_AA64DFR0_EL1_PMUVer_SHIFT: kvm_ftr.type = FTR_LOWER_SAFE; + break; + case ID_AA64DFR0_EL1_DebugVer_SHIFT: + kvm_ftr.type = FTR_LOWER_SAFE; + break; + } break; case SYS_ID_DFR0_EL1: if (kvm_ftr.shift == ID_DFR0_EL1_PerfMon_SHIFT) @@ -1476,14 +1482,22 @@ static u64 read_sanitised_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, return val; } +#define ID_REG_LIMIT_FIELD_ENUM(val, reg, field, limit) \ +({ \ + u64 __f_val = FIELD_GET(reg##_##field##_MASK, val); \ + (val) &= ~reg##_##field##_MASK; \ + (val) |= FIELD_PREP(reg##_##field##_MASK, \ + min(__f_val, (u64)reg##_##field##_##limit)); \ + (val); \ +}) + static u64 read_sanitised_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd) { u64 val = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1); /* Limit debug to ARMv8.0 */ - val &= ~ID_AA64DFR0_EL1_DebugVer_MASK; - val |= SYS_FIELD_PREP_ENUM(ID_AA64DFR0_EL1, DebugVer, IMP); + val = ID_REG_LIMIT_FIELD_ENUM(val, ID_AA64DFR0_EL1, DebugVer, IMP); /* * Only initialize the PMU version if the vCPU was configured with one. @@ -1503,6 +1517,7 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, u64 val) { + u8 debugver = SYS_FIELD_GET(ID_AA64DFR0_EL1, DebugVer, val); u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, val); /* @@ -1522,6 +1537,13 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; + /* + * ID_AA64DFR0_EL1.DebugVer is one of those awkward fields with a + * nonzero minimum safe value. + */ + if (debugver < ID_AA64DFR0_EL1_DebugVer_IMP) + return -EINVAL; + return set_id_reg(vcpu, rd, val); } @@ -1543,6 +1565,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, u64 val) { u8 perfmon = SYS_FIELD_GET(ID_DFR0_EL1, PerfMon, val); + u8 copdbg = SYS_FIELD_GET(ID_DFR0_EL1, CopDbg, val); if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) { val &= ~ID_DFR0_EL1_PerfMon_MASK; @@ -1558,6 +1581,9 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, if (perfmon != 0 && perfmon < ID_DFR0_EL1_PerfMon_PMUv3) return -EINVAL; + if (copdbg < ID_DFR0_EL1_CopDbg_Armv8) + return -EINVAL; + return set_id_reg(vcpu, rd, val); }