From patchwork Tue Oct 12 04:35:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reiji Watanabe X-Patchwork-Id: 12551443 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 1E9F9C433F5 for ; Tue, 12 Oct 2021 04:42:18 +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 DC75C60C4A for ; Tue, 12 Oct 2021 04:42:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DC75C60C4A 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=hh+otI2WhqCcTx87sp1DGaKTxqJyFDbUb5JnSw9hYbM=; b=CfUKZOOYIpzbYkW+NAuo8vsu63 ZlqE4riNwEFZMkTIsfgaXCanzbyK5Ex+bAKLkmD/43uY9HuU1H4brP94TLuycw5dQfBtXSQl1rfbo yZcWb+J/7VmStknq6IrQU1wSI1Pr1DhR1mScWswqVsQrVB2OIssDiBo9yVNKp3odynCpdsuP9ZmDh JEaI+hV1sCHYklKcLVW6flStiRPuCgo4M2qMJbE3ugM7L8gBO2qIVt3fl1WYoGVglW1XIqC4Y/K4n 0vc1ClzGSmwZsVsOfZARYaasuTru8uf57rCak30tjYPfjE4JEDrcrrUhJT2LWXy9Ht6XlwZ9v4iWQ ZC2uvChg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ma9Zy-00BRw2-D2; Tue, 12 Oct 2021 04:39:54 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ma9Wl-00BQQL-Kk for linux-arm-kernel@lists.infradead.org; Tue, 12 Oct 2021 04:36:37 +0000 Received: by mail-pl1-x64a.google.com with SMTP id p5-20020a170902a405b029012cbb4fcc03so8368388plq.19 for ; Mon, 11 Oct 2021 21:36:34 -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=KkXFZ5Fme7Kzy6UMAllI6J6cKTiNLiuGaTgPl7tFhoc=; b=gNWTCyHt0F+Tgvg9IjH1MQsh4TR7Z/ZVpwe1d0n6s1PxESO4F3WckfgA0IFJR+4tn1 BrtW2efC0MMgaI4/C1g8YwITxHGdB4kUZ8wLio9087ilyqc9MkdkTM5VcR8GGZVdnnTi C4dix4cTMWXtL1S55G+HZOYWpktnCDcr0foHyFoJu3MczBgMQQVGsAhhUogHjuB6MlUM 1GuIC4m5jLesSIHV4l7obvirl8Dh/035/DnD5RnDlOgtzH4urkuhgGTc1/+if+XBxq4g KjJDD0rV93rZv/LytfXCBIvF3/Ei+xCOLf7Mii0kfNbDKXYS8fASBPTK9gv1TluP05hb 0nmg== 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=KkXFZ5Fme7Kzy6UMAllI6J6cKTiNLiuGaTgPl7tFhoc=; b=GgGUcrn4LffOtDRWsSmmSBZlDrca7zUflhCCDTYaVEyDMmFABZ68FGMCx5L7Qk9v68 UkZFkzfwcJXj6h6lcj/cuFTg8WncdhR+4zkZxx4t9i+k7PQ9f12XraTrT/AGfR8e4j2f /HqhiZBQdk8ZgkZ71SwS3fMMhm8WtS6ETuczif9k7QcTZpmK6XVO2ZLCDzAO6Om4pj8P 2gVwZsKulY7SZDzhbqskoCff6F4plwrAdxZxPe7HOMJIrAZmQx5NTbZfQCsekdeLXNFa iKsfY3Bctvpt1YHMlbTag9Do4kvgnaAMK99IxxGuSRXt1Y1cEk5v0iflRc+xuZgb7haf t+vA== X-Gm-Message-State: AOAM530deb9rheyWeR0pK1Q+zHAFNT7YtxLmPffLkFUwuLSsjgySOFcs S8TiwZjg3404ua/plxGfXU0DiKpfEHY= X-Google-Smtp-Source: ABdhPJyzBf51TEcN/HoLmrN431l4sJxIHytNAgq2tIPPD+LnRTXbAwEq84O2Hau4z9V+wwNwWLecscNAgN0= 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 z13mr296118pjn.0.1634013393949; Mon, 11 Oct 2021 21:36:33 -0700 (PDT) Date: Mon, 11 Oct 2021 21:35:20 -0700 In-Reply-To: <20211012043535.500493-1-reijiw@google.com> Message-Id: <20211012043535.500493-11-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 10/25] KVM: arm64: Make ID_AA64DFR0_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_213635_715703_75E29568 X-CRM114-Status: GOOD ( 20.67 ) 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_AA64DFR0_EL1 to make it writable by userspace. Return an error if userspace tries to set PMUVER field of the register to a value that conflicts with the PMU configuration. Since number of context-aware breakpoints must be no more than number of supported breakpoints according to Arm ARM, return an error if userspace tries to set CTX_CMPS field to such value. Signed-off-by: Reiji Watanabe --- arch/arm64/kvm/sys_regs.c | 108 ++++++++++++++++++++++++++++++++++---- 1 file changed, 97 insertions(+), 11 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 7819a07ee248..61e61f4bb81c 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -402,6 +402,46 @@ static int validate_id_aa64isar1_el1(struct kvm_vcpu *vcpu, u64 val) return 0; } +static bool id_reg_has_pmu(u64 val, u64 shift, u64 min) +{ + u64 pmu = ((val >> shift) & 0xf); + + /* + * Treat IMPLEMENTATION DEFINED functionality as unimplemented for + * ID_AA64DFR0_EL1.PMUVer/ID_DFR0_EL1.PerfMon. + */ + if (pmu == 0xf) + pmu = 0; + + return (pmu >= min); +} + +static int validate_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, u64 val) +{ + unsigned int brps, ctx_cmps; + bool vcpu_pmu = kvm_vcpu_has_pmu(vcpu); + bool dfr0_pmu = id_reg_has_pmu(val, ID_AA64DFR0_PMUVER_SHIFT, + ID_AA64DFR0_PMUVER_8_0); + + brps = cpuid_feature_extract_unsigned_field(val, + ID_AA64DFR0_BRPS_SHIFT); + ctx_cmps = cpuid_feature_extract_unsigned_field(val, + ID_AA64DFR0_CTX_CMPS_SHIFT); + + /* + * Number of context-aware breakpoints can be no more than number of + * supported breakpoints. + */ + if (ctx_cmps > brps) + 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; @@ -445,6 +485,47 @@ static void init_id_aa64isar1_el1_info(struct id_reg_info *id_reg) (id_reg->sys_val & ~PTRAUTH_MASK); } +/* + * ID_AA64DFR0_EL1.PMUVer/ID_DFR0_EL1.PerfMon == 0xf indicates + * IMPLEMENTATION DEFINED form of performance monitors supported, + * PMUv3 not supported (NOTE: 0x0 indicates PMU is not supported). + * This function is to cap a value of those two fields with the + * given 'cap_val' treating 0xf in the fields as 0. + */ +static u64 id_reg_cap_pmu(u64 val, u64 shift, u64 cap_val) +{ + u64 mask = 0xf; + u64 pmu; + + pmu = (val >> shift) & mask; + pmu = (pmu == 0xf) ? 0 : pmu; + pmu = min(pmu, cap_val); + + val &= ~(0xfULL << shift); + val |= (pmu & 0xf) << shift; + return val; +} + +static void init_id_aa64dfr0_el1_info(struct id_reg_info *id_reg) +{ + u64 limit; + + id_reg->sys_val = read_sanitised_ftr_reg(id_reg->sys_reg); + limit = id_reg->sys_val; + + /* Limit guests to PMUv3 for ARMv8.4 */ + limit = id_reg_cap_pmu(limit, ID_AA64DFR0_PMUVER_SHIFT, + ID_AA64DFR0_PMUVER_8_4); + /* Limit debug to ARMv8.0 */ + limit &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_DEBUGVER); + limit |= (FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_DEBUGVER), 6)); + + /* Hide SPE from guests */ + limit &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_PMSVER); + + id_reg->vcpu_limit_val = limit; +} + static u64 get_reset_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, struct id_reg_info *idr) { @@ -468,6 +549,14 @@ static u64 get_reset_id_aa64isar1_el1(struct kvm_vcpu *vcpu, idr->vcpu_limit_val : (idr->vcpu_limit_val & ~PTRAUTH_MASK); } +static u64 get_reset_id_aa64dfr0_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_AA64DFR0_PMUVER))); +} + static struct id_reg_info id_aa64pfr0_el1_info = { .sys_reg = SYS_ID_AA64PFR0_EL1, .init = init_id_aa64pfr0_el1_info, @@ -497,6 +586,13 @@ static struct id_reg_info id_aa64isar1_el1_info = { .get_reset_val = get_reset_id_aa64isar1_el1, }; +static struct id_reg_info id_aa64dfr0_el1_info = { + .sys_reg = SYS_ID_AA64DFR0_EL1, + .init = init_id_aa64dfr0_el1_info, + .validate = validate_id_aa64dfr0_el1, + .get_reset_val = get_reset_id_aa64dfr0_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. @@ -508,6 +604,7 @@ static struct id_reg_info id_aa64isar1_el1_info = { static struct id_reg_info *id_reg_info_table[KVM_ARM_ID_REG_MAX_NUM] = { [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, [IDREG_IDX(SYS_ID_AA64ISAR0_EL1)] = &id_aa64isar0_el1_info, [IDREG_IDX(SYS_ID_AA64ISAR1_EL1)] = &id_aa64isar1_el1_info, }; @@ -1346,17 +1443,6 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu, u64 val = raz ? 0 : __vcpu_sys_reg(vcpu, IDREG_SYS_IDX(id)); switch (id) { - case SYS_ID_AA64DFR0_EL1: - /* Limit debug to ARMv8.0 */ - val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_DEBUGVER); - val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_DEBUGVER), 6); - /* Limit guests to PMUv3 for ARMv8.4 */ - val = cpuid_feature_cap_perfmon_field(val, - ID_AA64DFR0_PMUVER_SHIFT, - kvm_vcpu_has_pmu(vcpu) ? ID_AA64DFR0_PMUVER_8_4 : 0); - /* Hide SPE from guests */ - val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_PMSVER); - break; case SYS_ID_DFR0_EL1: /* Limit guests to PMUv3 for ARMv8.4 */ val = cpuid_feature_cap_perfmon_field(val,