Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1939436rdb; Tue, 5 Sep 2023 09:19:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGQKXyKF1k8Q6VcSQ38LH5lSq3+2LZXEGBEOWBy+yFre7dKuOuNTv79vg5tx6DS+aPjNdDD X-Received: by 2002:a05:6512:280d:b0:4ff:8d9e:d36f with SMTP id cf13-20020a056512280d00b004ff8d9ed36fmr300977lfb.39.1693930773977; Tue, 05 Sep 2023 09:19:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693930773; cv=none; d=google.com; s=arc-20160816; b=gt1uz5N4MvFo1YvaFYYZM5+bNTQ7UW/YagbypUHifvbCh2da992CcPPclc+T6f3uUF PSDcOI0GJYBjeNLeb3FNj35q0PYvyWreWBw6ETSG/0zhP4j0hXEXtKXOWz2j9C9kzsvd Hm3NfRkBgwVzBQ3/aaxrIfYE70j5Y3u+pJSQEorAAKV7sN1s9AzbHcgNUqHLOrSXfowg yBfBoG8FMNVBY6T/OYTsohtUvQIvnDJbQg9df4j2bDcid36mIuaIrBZM6fbZD9rRYNIK Hb3L0IEI9s0HsBCZKGwkxqjZqrsMuZlkL3TH9uQRh6RY/DJz27gM+mw3b8Y/6QS2k3sY ZEBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=u5U6gKXJ6HlzyfpHiF6jcREUenyukueUhTiWrtfoJjo=; fh=CZ8D47g5ajU/lYMMT5yJI50pbG977oQ2Qy9mgmfgiEI=; b=Sr+G9or72iaY6WCB3Qr4hIjsxxaUMXU9HbtEHGjRyWcCbjoPtpRQ0aMdVb5AoTqmPH qX9AIGHFUySBfBjgTArwyzP2DJxQQOUXzghrlrVIaZADPE35s57NcxBONw2oUUigSY0+ EXJRjekEa80qgRtQN+EsyuwfLLTxjZjRs6/t7Q53tKNLOmcHEmNZoVexSVdeSEc5plcp VqPVmABANahoUm1w1Iovr1ZAso1QGAipBHFzsatN+OKBeYdzf7p4QW4qIq7NSLkIr9EV oAxR2veJm5t7qPJyvOpcC2xa320oF0EUMfCW2Bb3xRAGO2e+oNL7K2ZcZc6Wx2aXnY+O gFPg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w8-20020aa7dcc8000000b005234684a7e2si8200915edu.119.2023.09.05.09.19.24; Tue, 05 Sep 2023 09:19:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353014AbjIDOHe (ORCPT + 20 others); Mon, 4 Sep 2023 10:07:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351897AbjIDOHb (ORCPT ); Mon, 4 Sep 2023 10:07:31 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 446DDC6 for ; Mon, 4 Sep 2023 07:07:27 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 08FEF16F3; Mon, 4 Sep 2023 07:08:05 -0700 (PDT) Received: from localhost.localdomain (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 860FC3F766; Mon, 4 Sep 2023 07:07:24 -0700 (PDT) From: James Clark To: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, broonie@kernel.org Cc: maz@kernel.org, James Clark , Catalin Marinas , Will Deacon , Suzuki K Poulose , Mike Leach , Leo Yan , Alexander Shishkin , James Morse , Kristina Martsenko , Anshuman Khandual , Rob Herring , Jintack Lim , Joey Gouly , linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] coresight: Allow guests to be traced when FEAT_TRF and VHE are present Date: Mon, 4 Sep 2023 15:07:03 +0100 Message-Id: <20230904140705.1620708-3-james.clark@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230904140705.1620708-1-james.clark@arm.com> References: <20230904140705.1620708-1-james.clark@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 Currently the userspace and kernel filters for guests are never set, so no trace will be generated for them. Add it by writing to the guest filters when exclude_guest isn't set. By writing either E1TRE or E0TRE, filtering on either guest kernel or guest userspace is also supported. Since TRFCR_EL1 access is trapped, this can't be modified by the guest. This change also brings exclude_host support which is difficult to add as a separate commit without excess churn and resulting in no trace at all. Testing ======= The addresses were counted with the following: $ perf report -D | grep -Eo 'EL2|EL1|EL0' | sort | uniq -c Guest kernel only: $ perf record -e cs_etm//Gk -a -- true 535 EL1 1 EL2 Guest user only (0 addresses expected because the guest OS hasn't reached userspace yet): $ perf record -e cs_etm//Gu -a -- true Host kernel only: $ perf record -e cs_etm//Hk -a -- true 3501 EL2 Host userspace only: $ perf record -e cs_etm//Hu -a -- true 408 EL0 1 EL2 Reviewed-by: Mark Brown (sysreg) Signed-off-by: James Clark --- arch/arm64/tools/sysreg | 4 ++ .../coresight/coresight-etm4x-core.c | 51 ++++++++++++++++--- drivers/hwtracing/coresight/coresight-etm4x.h | 2 +- drivers/hwtracing/coresight/coresight-priv.h | 3 ++ 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/arch/arm64/tools/sysreg b/arch/arm64/tools/sysreg index 6ca7db69d6c9..cae9139b6c05 100644 --- a/arch/arm64/tools/sysreg +++ b/arch/arm64/tools/sysreg @@ -2531,3 +2531,7 @@ EndSysreg Sysreg TRFCR_EL2 3 4 1 2 1 Fields TRFCR_EL2 EndSysreg + +Sysreg TRFCR_EL12 3 5 1 2 1 +Fields TRFCR_ELx +EndSysreg diff --git a/drivers/hwtracing/coresight/coresight-etm4x-core.c b/drivers/hwtracing/coresight/coresight-etm4x-core.c index 77b0271ce6eb..6c16a14d6fbe 100644 --- a/drivers/hwtracing/coresight/coresight-etm4x-core.c +++ b/drivers/hwtracing/coresight/coresight-etm4x-core.c @@ -274,6 +274,18 @@ static void etm4x_prohibit_trace(struct etmv4_drvdata *drvdata) cpu_prohibit_trace(); } +static u64 etm4x_get_kern_user_filter(struct etmv4_drvdata *drvdata) +{ + u64 trfcr = drvdata->trfcr; + + if (drvdata->config.mode & ETM_MODE_EXCL_KERN) + trfcr &= ~TRFCR_ELx_ExTRE; + if (drvdata->config.mode & ETM_MODE_EXCL_USER) + trfcr &= ~TRFCR_ELx_E0TRE; + + return trfcr; +} + /* * etm4x_allow_trace - Allow CPU tracing in the respective ELs, * as configured by the drvdata->config.mode for the current @@ -286,18 +298,39 @@ static void etm4x_prohibit_trace(struct etmv4_drvdata *drvdata) */ static void etm4x_allow_trace(struct etmv4_drvdata *drvdata) { - u64 trfcr = drvdata->trfcr; + u64 trfcr; /* If the CPU doesn't support FEAT_TRF, nothing to do */ - if (!trfcr) + if (!drvdata->trfcr) return; - if (drvdata->config.mode & ETM_MODE_EXCL_KERN) - trfcr &= ~TRFCR_ELx_ExTRE; - if (drvdata->config.mode & ETM_MODE_EXCL_USER) - trfcr &= ~TRFCR_ELx_E0TRE; + if (drvdata->config.mode & ETM_MODE_EXCL_HOST) + trfcr = drvdata->trfcr & ~(TRFCR_ELx_ExTRE | TRFCR_ELx_E0TRE); + else + trfcr = etm4x_get_kern_user_filter(drvdata); write_trfcr(trfcr); + + /* + * Filters for EL1 and EL0 (when running a guest) are stored in + * TRFCR_EL1 so write it there for VHE. For nVHE, the filters in + * have to be re-applied when switching to the guest instead. + */ + if (!is_kernel_in_hyp_mode()) + return; + + if (drvdata->config.mode & ETM_MODE_EXCL_GUEST) + trfcr = drvdata->trfcr & ~(TRFCR_ELx_ExTRE | TRFCR_ELx_E0TRE); + else + trfcr = etm4x_get_kern_user_filter(drvdata); + + /* + * TRFCR_EL1 doesn't have CX and TRFCR_EL1.TS has no effect when TS is + * set in EL2 so mask them out. + */ + trfcr &= ~(TRFCR_ELx_TS_MASK | TRFCR_EL2_CX); + + write_sysreg_s(trfcr, SYS_TRFCR_EL12); } #ifdef CONFIG_ETM4X_IMPDEF_FEATURE @@ -655,6 +688,12 @@ static int etm4_parse_event_config(struct coresight_device *csdev, if (attr->exclude_user) config->mode = ETM_MODE_EXCL_USER; + if (attr->exclude_host) + config->mode |= ETM_MODE_EXCL_HOST; + + if (attr->exclude_guest) + config->mode |= ETM_MODE_EXCL_GUEST; + /* Always start from the default config */ etm4_set_default_config(config); diff --git a/drivers/hwtracing/coresight/coresight-etm4x.h b/drivers/hwtracing/coresight/coresight-etm4x.h index 20e2e4cb7614..3f170599822f 100644 --- a/drivers/hwtracing/coresight/coresight-etm4x.h +++ b/drivers/hwtracing/coresight/coresight-etm4x.h @@ -841,7 +841,7 @@ enum etm_impdef_type { * @s_ex_level: Secure ELs where tracing is supported. */ struct etmv4_config { - u32 mode; + u64 mode; u32 pe_sel; u32 cfg; u32 eventctrl0; diff --git a/drivers/hwtracing/coresight/coresight-priv.h b/drivers/hwtracing/coresight/coresight-priv.h index 767076e07970..727dd27ba800 100644 --- a/drivers/hwtracing/coresight/coresight-priv.h +++ b/drivers/hwtracing/coresight/coresight-priv.h @@ -39,6 +39,9 @@ #define ETM_MODE_EXCL_KERN BIT(30) #define ETM_MODE_EXCL_USER BIT(31) +#define ETM_MODE_EXCL_HOST BIT(32) +#define ETM_MODE_EXCL_GUEST BIT(33) + struct cs_pair_attribute { struct device_attribute attr; u32 lo_off; -- 2.34.1