From patchwork Mon Feb 14 06:57:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 12745048 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 BDB55C433F5 for ; Mon, 14 Feb 2022 07:05:38 +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: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=VF6C5YCOGsX4/RPkqYryZyBEfmATOAyjHzkA7YwsphE=; b=eM3/tSkqdbHHL8oMI+3qg3TuT+ BSgXsuRsiBfxmhm7s1PRkfX7Wl1dE/jJ1y6JdX1JFEHGvYswBTfiWR1adk4odHF9mYyx8csc5cxtP 7eB2sadKA4DSh4F0pdozLA6f9KyarieEDXWgIeMTpZKRP+o2x172wliH8CwAyhkrLf/CUcX0XNqpD e57kHl5tIsweq5G5fgPsHX63HyWEg5m7I20yZiX5suawPyASlODUonnb/G/z3PK4+y1PKP6lV94Zb vbpRusKyQXSONSlQNykM+r9FIxeuBqfG/SaTcC9892OZBrcOcVkO2RfgvWRNdSKKG4rGcLYzrykaY lJSo6ASw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nJVP3-00DYfs-DM; Mon, 14 Feb 2022 07:04:05 +0000 Received: from mail-pj1-x104a.google.com ([2607:f8b0:4864:20::104a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nJVLR-00DWwp-U8 for linux-arm-kernel@lists.infradead.org; Mon, 14 Feb 2022 07:00:23 +0000 Received: by mail-pj1-x104a.google.com with SMTP id h16-20020a17090ac39000b001b8d02b2efaso11517091pjt.5 for ; Sun, 13 Feb 2022 23:00:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=3YXTeyXBEmNCdPDjiReBLqzjLxovi4NAJDqchxZh9cs=; b=hqjIH2MDABDU/OeLEKZWo+DdKFtbNKPzc8DzAsmSoewDb0TQvvmNYfZoqr4LRPXuCu 3/MsTvJul6Ij9zh2P/rEwbxSoWxJwP9B3mBPhm7La4N4RoGaWILY7KjdW5Ujj+uLPPvm M8Rne1sT9cGZvK0KeTNOmjBlWuqyGQ7lIqjcUAy21IrRnsS/u00py3MfG7ueSilWBRKe SG+Y7xvtyl4UaohTa/VoGlSW5XHwO4NES1aGnE+cCOEbkyF3DSgDWWRVfIcu/KgBuFdE 5GTsbRB0iQ3R9rrjRdgHrJVZ7GoLfXXoxQVXuhy5kcR6limdJhuizj5foMhZAqnFY7pX 7jPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=3YXTeyXBEmNCdPDjiReBLqzjLxovi4NAJDqchxZh9cs=; b=Gmxggoc+4AbY4fG5hCoUF3qZbfpyIoHinYy6Rj4JGhNCH6LkyItgnQj62SC+1ThpU9 05lQo8dQzA2wSDZIw28l9Td9/bb52/LRVu9CacWXhY9JH6aLdi+ZvYj2a4tkfbdbRtlf Bp8UfdsNXTcXif5XWo/+JbKkdxcbpFbBPxSnspE0gTW2Cw4SO2Tz2EL4HvZOmme3blnm zZqGT2oUnGCpyZW23McQr+6WxWfQs4ePROz7OnD8YgyRAD1pAM1hDlWIi89TTg8ngbA8 21CYXWndzWqTXhgHdZyzlGpJoOFJU3GVHgPxCUP5ksYnqkUjPctvib5322sxGF1JiHYF XMiA== X-Gm-Message-State: AOAM5335+8CZNYzVSroajYomGPu6hL49f6fT7ZmYG7wIR3yR2uL30DW6 bQa9XslW9FBmJrXYEJTbhvQqfEFWe2A= X-Google-Smtp-Source: ABdhPJyq9BwnHRiqYEmw3Jv6ypDLwMjFgJah6jMEDc6CbrOMpkBYC8Pq1R+X3qit7EcVAKLFGgrEBsgt/Rk= X-Received: from reiji-vws-sp.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3d59]) (user=reijiw job=sendgmr) by 2002:a63:368f:: with SMTP id d137mr10488057pga.475.1644822020701; Sun, 13 Feb 2022 23:00:20 -0800 (PST) Date: Sun, 13 Feb 2022 22:57:31 -0800 In-Reply-To: <20220214065746.1230608-1-reijiw@google.com> Message-Id: <20220214065746.1230608-13-reijiw@google.com> Mime-Version: 1.0 References: <20220214065746.1230608-1-reijiw@google.com> X-Mailer: git-send-email 2.35.1.265.g69c8d7142f-goog Subject: [PATCH v5 12/27] KVM: arm64: Make ID_DFR0_EL1 writable From: Reiji Watanabe To: Marc Zyngier , kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, James Morse , Alexandru Elisei , Suzuki K Poulose , Paolo Bonzini , Will Deacon , Andrew Jones , Fuad Tabba , Peng Liang , Peter Shier , Ricardo Koller , Oliver Upton , Jing Zhang , Raghavendra Rao Anata , Reiji Watanabe X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220213_230022_053279_496DBF6B X-CRM114-Status: GOOD ( 16.40 ) 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 This patch adds id_reg_info for ID_DFR0_EL1 to make it writable by userspace. Return an error if userspace tries to set PerfMon field of the register to a value that conflicts with the PMU configuration. Signed-off-by: Reiji Watanabe --- arch/arm64/kvm/sys_regs.c | 55 ++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index ae379755fa26..90e6a85d4e31 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -633,6 +633,27 @@ static int validate_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, return 0; } +static int validate_id_dfr0_el1(struct kvm_vcpu *vcpu, + const struct id_reg_info *id_reg, u64 val) +{ + bool vcpu_pmu, dfr0_pmu; + unsigned int perfmon; + + perfmon = cpuid_feature_extract_unsigned_field(val, ID_DFR0_PERFMON_SHIFT); + if (perfmon == 1 || perfmon == 2) + /* PMUv1 or PMUv2 is not allowed on ARMv8. */ + return -EINVAL; + + vcpu_pmu = kvm_vcpu_has_pmu(vcpu); + dfr0_pmu = id_reg_has_pmu(val, ID_DFR0_PERFMON_SHIFT, ID_DFR0_PERFMON_8_0); + + /* Check if there is a conflict with a request via KVM_ARM_VCPU_INIT */ + if (vcpu_pmu ^ dfr0_pmu) + return -EPERM; + + return 0; +} + static void init_id_aa64pfr0_el1_info(struct id_reg_info *id_reg) { u64 limit = id_reg->vcpu_limit_val; @@ -693,8 +714,17 @@ static void init_id_aa64dfr0_el1_info(struct id_reg_info *id_reg) id_reg->vcpu_limit_val = limit; } +static void init_id_dfr0_el1_info(struct id_reg_info *id_reg) +{ + /* Limit guests to PMUv3 for ARMv8.4 */ + id_reg->vcpu_limit_val = + cpuid_feature_cap_perfmon_field(id_reg->vcpu_limit_val, + ID_DFR0_PERFMON_SHIFT, + ID_DFR0_PERFMON_8_4); +} + static u64 vcpu_mask_id_aa64pfr0_el1(const struct kvm_vcpu *vcpu, - const struct id_reg_info *idr) + const struct id_reg_info *idr) { return vcpu_has_sve(vcpu) ? 0 : ARM64_FEATURE_MASK(ID_AA64PFR0_SVE); } @@ -717,6 +747,12 @@ static u64 vcpu_mask_id_aa64dfr0_el1(const struct kvm_vcpu *vcpu, return kvm_vcpu_has_pmu(vcpu) ? 0 : ARM64_FEATURE_MASK(ID_AA64DFR0_PMUVER); } +static u64 vcpu_mask_id_dfr0_el1(const struct kvm_vcpu *vcpu, + const struct id_reg_info *idr) +{ + return kvm_vcpu_has_pmu(vcpu) ? 0 : ARM64_FEATURE_MASK(ID_DFR0_PERFMON); +} + static struct id_reg_info id_aa64pfr0_el1_info = { .sys_reg = SYS_ID_AA64PFR0_EL1, .ignore_mask = ARM64_FEATURE_MASK(ID_AA64PFR0_GIC), @@ -773,6 +809,13 @@ static struct id_reg_info id_aa64dfr0_el1_info = { .vcpu_mask = vcpu_mask_id_aa64dfr0_el1, }; +static struct id_reg_info id_dfr0_el1_info = { + .sys_reg = SYS_ID_DFR0_EL1, + .init = init_id_dfr0_el1_info, + .validate = validate_id_dfr0_el1, + .vcpu_mask = vcpu_mask_id_dfr0_el1, +}; + /* * An ID register that needs special handling to control the value for the * guest must have its own id_reg_info in id_reg_info_table. @@ -782,6 +825,7 @@ static struct id_reg_info id_aa64dfr0_el1_info = { */ #define GET_ID_REG_INFO(id) (id_reg_info_table[IDREG_IDX(id)]) static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM] = { + [IDREG_IDX(SYS_ID_DFR0_EL1)] = &id_dfr0_el1_info, [IDREG_IDX(SYS_ID_AA64PFR0_EL1)] = &id_aa64pfr0_el1_info, [IDREG_IDX(SYS_ID_AA64PFR1_EL1)] = &id_aa64pfr1_el1_info, [IDREG_IDX(SYS_ID_AA64DFR0_EL1)] = &id_aa64dfr0_el1_info, @@ -1712,15 +1756,6 @@ static u64 __read_id_reg(const struct kvm_vcpu *vcpu, u32 id) /* Clear fields for opt-in features that are not configured. */ val &= ~(id_reg->vcpu_mask(vcpu, id_reg)); - switch (id) { - case SYS_ID_DFR0_EL1: - /* Limit guests to PMUv3 for ARMv8.4 */ - val = cpuid_feature_cap_perfmon_field(val, - ID_DFR0_PERFMON_SHIFT, - kvm_vcpu_has_pmu(vcpu) ? ID_DFR0_PERFMON_8_4 : 0); - break; - } - return val; }