From patchwork Tue Oct 12 04:35:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 12551445 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C946FC433F5 for ; Tue, 12 Oct 2021 04:42:49 +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 9AB1B60EE2 for ; Tue, 12 Oct 2021 04:42:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 9AB1B60EE2 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=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=fSow2ojBlzNQTDElIMAdRrp9QTy7Sz0+aW2nKEe+PTA=; b=TADPkTBoB0Ho10Px+oOj7U3N+E uG8AY95GjZFj1cdDeIWcjMVmnchexfrpttM45Eoe09juN0e1546YoiL1Nn1LMszd90dSHymbQWbTI jFLjtdj1hxMLg8jU6JxBvfNInreQxAibNR4dgebYUtv4Gov7v7faDTQUbM2HZqwC0Uslw5pH4dMBA TReBulRLauY/PBpzuh40u0YgumD2KOE4IZ57+WV1YNDAiu3sOHDKBPyuVgXP4w1cwGnPmQo9sZE+b 5iWU4sYQaPju3SOvgHk7oK+cMjMBzPYAI9zaLnGUGLZQ/jRNu6d0MQZu4S7VXW6ti4zuDknPOSIbc y6uM74gA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ma9ag-00BSET-Nx; Tue, 12 Oct 2021 04:40:39 +0000 Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ma9Wn-00BQR6-P2 for linux-arm-kernel@lists.infradead.org; Tue, 12 Oct 2021 04:36:39 +0000 Received: by mail-pf1-x449.google.com with SMTP id j12-20020aa783cc000000b0044b702424b7so8617595pfn.6 for ; Mon, 11 Oct 2021 21:36:36 -0700 (PDT) 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=cwGN9Xe3XWxW643nb96+bXhhQ/Ir7xJT1HwtMeHM7cQ=; b=DBUBOXq/KtFBlinyQ1LBw4GMEMeHuJzh2MUTnjINtr2KUrltQAUA/+vj8wI0Fk5ST3 XiWsa2domVcF8x4i7LPxC0hj8GAwn/DPal9Zqj1qoljPzYPMatgwIia0NMQQqWKOxdYE LeLft6vpRTZbBcHr0bemhAlKRQCe5qJgGwKhn2+w35t4snNcehqQb2g0rsw1RIOdYFU9 bAiPujoV+AaVBXHt8l6+A821yQN76n/5eHRiIh9+1xoDs8dHcReaRoDiKfQOBKkSwGZP HVYmsNdv0YnE0MVnp1hIZAk+puV5zvweS0KhO09Xkis/O6oEEaP7NKkHSlBTNJZEgphI qdnA== 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=cwGN9Xe3XWxW643nb96+bXhhQ/Ir7xJT1HwtMeHM7cQ=; b=5G5CN057sTxK+xjR4geHLv7CZYTEfWwj38258BdY48Nuj2NYMOhxUSvxNlSz5lp0zz uemmYxqdkkktrD8BPeRG53bVk96fDpSskQWOmiuvQtDHs0e/RsFUZCPECtWeLby9Hzz5 v5YS6JizQ9ZgjQE6x+KOk6sYl5emt4onyGB8Go5RhE05mhDCzmofYvnWxuXWkwuAUDww 1BpH1PiJJGrE1Hp9+j9qZd3hKFhYkTE9EQFgKGcGUPvWQOVsSU/W6f2ffSxsDO+lXw6r oyMMhKpOJQNdW7DUbd6Z+cwhQ+QRHEgko31CkU9M+DKl0pIcQpUystOrK1aze8OABhim I3cw== X-Gm-Message-State: AOAM531EIDcmcrBfn12H9U0I/4xWZ59NVJuVfDWvFQ9aQihGg4PeSLLg nh2IgzRsqohkDf9jfetZDXBUAXj2E+U= X-Google-Smtp-Source: ABdhPJy8CGkD4X+vedywWNr5lgYqfM+G5c5VEXo+GN+CPYbQ23SjsxTScLeFIEMAadLh5UzZSK8h53NYvAg= X-Received: from reiji-vws.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:15a3]) (user=reijiw job=sendgmr) by 2002:a17:90a:8b8d:: with SMTP id z13mr296126pjn.0.1634013395790; Mon, 11 Oct 2021 21:36:35 -0700 (PDT) Date: Mon, 11 Oct 2021 21:35:21 -0700 In-Reply-To: <20211012043535.500493-1-reijiw@google.com> Message-Id: <20211012043535.500493-12-reijiw@google.com> Mime-Version: 1.0 References: <20211012043535.500493-1-reijiw@google.com> X-Mailer: git-send-email 2.33.0.882.g93a45727a2-goog Subject: [RFC PATCH 11/25] 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 , 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-20211011_213637_842220_F17C4F7F X-CRM114-Status: GOOD ( 17.30 ) 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 | 57 +++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 61e61f4bb81c..84c064dfc63a 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -442,6 +442,25 @@ static int validate_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, u64 val) return 0; } +static int validate_id_dfr0_el1(struct kvm_vcpu *vcpu, u64 val) +{ + bool vcpu_pmu = kvm_vcpu_has_pmu(vcpu); + unsigned int perfmon = cpuid_feature_extract_unsigned_field(val, + ID_DFR0_PERFMON_SHIFT); + bool dfr0_pmu = id_reg_has_pmu(val, ID_DFR0_PERFMON_SHIFT, + ID_DFR0_PERFMON_8_0); + + if (perfmon == 1 || perfmon == 2) + /* PMUv1 or PMUv2 is not allowed on ARMv8. */ + return -EINVAL; + + /* 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; @@ -526,6 +545,16 @@ 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) +{ + id_reg->sys_val = read_sanitised_ftr_reg(id_reg->sys_reg); + + /* Limit guests to PMUv3 for ARMv8.4 */ + id_reg->vcpu_limit_val = id_reg_cap_pmu(id_reg->sys_val, + ID_DFR0_PERFMON_SHIFT, + ID_DFR0_PERFMON_8_4); +} + static u64 get_reset_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, struct id_reg_info *idr) { @@ -557,6 +586,14 @@ static u64 get_reset_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, (idr->vcpu_limit_val & ~(ARM64_FEATURE_MASK(ID_AA64DFR0_PMUVER))); } +static u64 get_reset_id_dfr0_el1(struct kvm_vcpu *vcpu, + struct id_reg_info *idr) +{ + return kvm_vcpu_has_pmu(vcpu) ? + idr->vcpu_limit_val : + (idr->vcpu_limit_val & ~(ARM64_FEATURE_MASK(ID_DFR0_PERFMON))); +} + static struct id_reg_info id_aa64pfr0_el1_info = { .sys_reg = SYS_ID_AA64PFR0_EL1, .init = init_id_aa64pfr0_el1_info, @@ -593,6 +630,13 @@ static struct id_reg_info id_aa64dfr0_el1_info = { .get_reset_val = get_reset_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, + .get_reset_val = get_reset_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. @@ -602,6 +646,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, @@ -1440,18 +1485,8 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r, bool raz) { u32 id = reg_to_encoding(r); - u64 val = raz ? 0 : __vcpu_sys_reg(vcpu, IDREG_SYS_IDX(id)); - 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; + return raz ? 0 : __vcpu_sys_reg(vcpu, IDREG_SYS_IDX(id)); } static unsigned int id_visibility(const struct kvm_vcpu *vcpu,