Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp1300310rdb; Fri, 20 Oct 2023 14:42:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGiMSMZs+bVJpAUM8hBNURdjSe3tyxBg7/Hmgk7igSIdnJp1wyIAm6dCQR90QQmS9ABgJiy X-Received: by 2002:a17:902:ead2:b0:1ca:7f91:aa5d with SMTP id p18-20020a170902ead200b001ca7f91aa5dmr2960675pld.16.1697838148395; Fri, 20 Oct 2023 14:42:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697838148; cv=none; d=google.com; s=arc-20160816; b=aofFw50uMdrLfO02lj6dH4HUN2PxUNnnnA1haOXoOgvRVlpJFaUaHp+Qm5KCBSjTrE Ui6beGwJUH0Enhmu+Cxbn6FpjR1jUp12cq9E13/S9xU3QeJoqPW2wwm9cWH2jBKu9rr6 qCFhqYWJPAJIAUYcFPzownUZUgQeKNfuOjp6Fg6i8B0snG7AdgJchY/u23EwmQn/Dc6b Y6/QsNiS+S2xLcMOZQX3wIe6GXhjgEWIa7HAewSMxTDP6HHKwnrDJyfv3CQKSYsWxjQq 6Vsb1F6jVPyWSQJHGbxbeY4RekDXpdlS33o+qghD7rX4rXyjgHzl4bWOIS+djcR8hNuh u5ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=eWANe28YHA3Ik3VAhdhi3PR1V7Sp1rEPcSjU6me6VYQ=; fh=nSGf6E5bqeZ4+WG1wtUl4dHMM2T+/oX4atvC7v39NTU=; b=KTZlxiawFHq/22RzTozWC7jN0YNUATe3uqIQIOvsgT1R+zGXVyNq3Yel1GocMoWAzf m5U1W6/wYILUl4wdHaCtVeRTeO5eI8wtWZr27zApEezKcR8zjzmv5KKnP7jHS8CHchGh hAQ1oKRFf7N7oSSAo55QzvW+T4u/jEVYZy1GRFqc7VnbJt6ciY6QvxFWLCFg5GJCIIka oZ6rpfkfLNAvbF2CTXWu6+wYuqDOS7gFjEd0KuQA2AhQmmuEz2+CAF5I2fY0oIpHMuDF IKm/6i8VRD4RVRhL33UAWT3JEyVcqqri9WjHgEYZJ+u03sNZzP9x79F5WQthyV9C8joW krbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="a3L9/DSr"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id d8-20020a170902654800b001c9bdaa81e3si2299168pln.633.2023.10.20.14.42.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 14:42:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="a3L9/DSr"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id C712F804E890; Fri, 20 Oct 2023 14:41:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345335AbjJTVlS (ORCPT + 99 others); Fri, 20 Oct 2023 17:41:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233197AbjJTVlP (ORCPT ); Fri, 20 Oct 2023 17:41:15 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B70F7D76 for ; Fri, 20 Oct 2023 14:41:04 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a839b31a0dso23806457b3.0 for ; Fri, 20 Oct 2023 14:41:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697838064; x=1698442864; darn=vger.kernel.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=eWANe28YHA3Ik3VAhdhi3PR1V7Sp1rEPcSjU6me6VYQ=; b=a3L9/DSrY46+hFW6OWnAMjHUl4Pp3cU/gldU1GxusgQ6LJZAVnFAKj8P7cV8PiRBoH QO3CafgU6K9E/fmFMzorI7Upx/9HslLzXUbJ9TLf/M658DNiO2TugLhdGkfXdukXqfLi 4dbKUFX1XitYSuG6FOioOvb90SHtxW/0XTu68g+nNw+GHlnW8JrZzu4b2ElItAqqm7m7 F6271x05jMLSN8+IRRFzkMsZ81ltdgHty7NoFJblpLXrSx9yCy4BAW6KVW/t7xjGNlyk q/KjOENjWK/QYiIllmoBIUflDvTVXRXvGxeyZz4KU1kA2c7/k/HKMJy0nQN/9NusqyqK iulQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697838064; x=1698442864; 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=eWANe28YHA3Ik3VAhdhi3PR1V7Sp1rEPcSjU6me6VYQ=; b=dUKq38QQWfCMaXbCR8zCiJboAR7K49xXkxuad0spsFArfm0IrwUcvKGC/f3iqa1lP9 ZquuryRqKqYIhFdJi7U2AMLXiyu1Va1O1E3RorV3SoispfYRabfAflyvuUPNbIoT6BAw heo/DI6D3/ZIjJ7E0Lx/q03sQOVbvVT4TY1P6gh1fTx77n0ASiYe8+Cm2gRk5ElfD8Hd fV52K2gzcNptwHd/j5I4it3m+fjEoLiciUq8LTvI1Ah1UXzTXSllPHcN32VXhQEmDJgr nqTFxzB2VmmeGWqpDIHzpgv/3/csRO7s9P23VIKDpAxvC7MEMTeIw/iUOaer9/QVsfP4 r36w== X-Gm-Message-State: AOJu0Yx8Gpfc36W4QA9raBJkMM3+LgaOogU+wUZmMLsFDYys0AEWW1XH ia7nV0u4quRr32DGgp9HFwxaTnktZG2s X-Received: from rananta-linux.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:20a1]) (user=rananta job=sendgmr) by 2002:a05:6902:1083:b0:d9a:c946:c18c with SMTP id v3-20020a056902108300b00d9ac946c18cmr92087ybu.6.1697838063913; Fri, 20 Oct 2023 14:41:03 -0700 (PDT) Date: Fri, 20 Oct 2023 21:40:47 +0000 In-Reply-To: <20231020214053.2144305-1-rananta@google.com> Mime-Version: 1.0 References: <20231020214053.2144305-1-rananta@google.com> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog Message-ID: <20231020214053.2144305-8-rananta@google.com> Subject: [PATCH v8 07/13] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest From: Raghavendra Rao Ananta To: Oliver Upton , Marc Zyngier Cc: Alexandru Elisei , James Morse , Suzuki K Poulose , Paolo Bonzini , Zenghui Yu , Shaoqin Huang , Jing Zhang , Reiji Watanabe , Colton Lewis , Raghavendra Rao Anata , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 20 Oct 2023 14:41:33 -0700 (PDT) From: Reiji Watanabe KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by userspace). Add support userspace limiting PMCR_EL0.N. Disallow userspace to set PMCR_EL0.N to a value that is greater than the host value as KVM doesn't support more event counters than what the host HW implements. Also, make this register immutable after the VM has started running. To maintain the existing expectations, instead of returning an error, KVM returns a success for these two cases. Finally, ignore writes to read-only bits that are cleared on vCPU reset, and RES{0,1} bits (including writable bits that KVM doesn't support yet), as those bits shouldn't be modified (at least with the current KVM). Signed-off-by: Reiji Watanabe Signed-off-by: Raghavendra Rao Ananta --- arch/arm64/kvm/sys_regs.c | 57 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 2e5d497596ef8..a2c5f210b3d6b 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1176,6 +1176,59 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, return 0; } +static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, + u64 val) +{ + struct kvm *kvm = vcpu->kvm; + u64 new_n, mutable_mask; + + mutex_lock(&kvm->arch.config_lock); + + /* + * Make PMCR immutable once the VM has started running, but + * do not return an error to meet the existing expectations. + */ + if (kvm_vm_has_ran_once(vcpu->kvm)) { + mutex_unlock(&kvm->arch.config_lock); + return 0; + } + + new_n = (val >> ARMV8_PMU_PMCR_N_SHIFT) & ARMV8_PMU_PMCR_N_MASK; + if (new_n != kvm->arch.pmcr_n) { + u8 pmcr_n_limit = kvm_arm_pmu_get_max_counters(kvm); + + /* + * The vCPU can't have more counters than the PMU hardware + * implements. Ignore this error to maintain compatibility + * with the existing KVM behavior. + */ + if (new_n <= pmcr_n_limit) + kvm->arch.pmcr_n = new_n; + } + mutex_unlock(&kvm->arch.config_lock); + + /* + * Ignore writes to RES0 bits, read only bits that are cleared on + * vCPU reset, and writable bits that KVM doesn't support yet. + * (i.e. only PMCR.N and bits [7:0] are mutable from userspace) + * The LP bit is RES0 when FEAT_PMUv3p5 is not supported on the vCPU. + * But, we leave the bit as it is here, as the vCPU's PMUver might + * be changed later (NOTE: the bit will be cleared on first vCPU run + * if necessary). + */ + mutable_mask = (ARMV8_PMU_PMCR_MASK | + (ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHIFT)); + val &= mutable_mask; + val |= (__vcpu_sys_reg(vcpu, r->reg) & ~mutable_mask); + + /* The LC bit is RES1 when AArch32 is not supported */ + if (!kvm_supports_32bit_el0()) + val |= ARMV8_PMU_PMCR_LC; + + __vcpu_sys_reg(vcpu, r->reg) = val; + return 0; +} + /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */ #define DBG_BCR_BVR_WCR_WVR_EL1(n) \ { SYS_DESC(SYS_DBGBVRn_EL1(n)), \ @@ -2309,8 +2362,8 @@ static const struct sys_reg_desc sys_reg_descs[] = { { SYS_DESC(SYS_CTR_EL0), access_ctr }, { SYS_DESC(SYS_SVCR), undef_access }, - { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, - .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr }, + { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr, + .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr }, { PMU_SYS_REG(PMCNTENSET_EL0), .access = access_pmcnten, .reg = PMCNTENSET_EL0, .get_user = get_pmcnten, .set_user = set_pmcnten }, -- 2.42.0.655.g421f12c284-goog