From patchwork Sun Feb 5 01:15:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 52848 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1578336wrn; Sat, 4 Feb 2023 17:23:08 -0800 (PST) X-Google-Smtp-Source: AK7set9RGzwy1QF3NUnzsoW3Trk7yq1O7nHWH2WxsDAtSVyMtxcGP7zAGvGbumrtpztOrxa4f63/ X-Received: by 2002:a05:6402:321e:b0:4a3:43c1:8434 with SMTP id g30-20020a056402321e00b004a343c18434mr9717283eda.8.1675560188330; Sat, 04 Feb 2023 17:23:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675560188; cv=none; d=google.com; s=arc-20160816; b=S9S8mHFSKdAWNB+zX4mH1lXCc76uS8OZ54/yTeQGGYaE5um1bH/08cnnce/us43kF1 6kLtQdVHP3rhKzpV7kwJAdc329BH0Y9W0hoLt+DOCukDkbUeLOA4Ft3Yoe+572+wXXoZ OmpdFbMglw9c+1AFPlUK/KZKedYaeWBD5vsdCl2afcIa/h2oXQOOd99SHBi3nOk1TcK/ lfvbJT3ECwg4DX81DShbsyNNmh40wr8zlMguZPUs7hOLt/jp2hVGVguw850bGHPnEyLR JhuHer7uwkMd2ALsRpKSnUWInJa+4YFw1VO1EuITZxto71s6miRyYOw0rqnh7x+WXGR8 Cofw== 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 :dkim-signature; bh=U56D9hJIIHchDDVPpFN73Ll8vi8yIOPIpQvQo3TH8jM=; b=KDOhIXLPX1+T9+NOyHxJA8Q7GijgH21TYlkwNQgdmG4bkQhBkXkBbqaKl3YbhQwXBy 43U/fkUGyi9Vq3KrDK6ZBKSZcyR//+fLaFKhSwGJgisvZIvE9jxVLckQUZFTvY0cZItn AzENOcvKUZzYE0iAsFn6GHk301uIVmWSa4VKOaFdVze65GJTAScVqrpNX7bdItF3LlU/ mVC9rV6caot9p6/chG5JOapd1WkVBLBS2XCGmYZVNc6/mACBR665MEuUF/bUNDtICym8 NldG634LbuUS0FrU/NVG+yTt41t55Ah5EnmF3brloHwsd/ikQTC3CKy5F2rk7bwjOLfX VFLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=i6LWUTJv; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m10-20020a056402430a00b004854e1d2682si9167315edc.249.2023.02.04.17.22.40; Sat, 04 Feb 2023 17:23:08 -0800 (PST) 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; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=i6LWUTJv; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232605AbjBEBQT (ORCPT + 99 others); Sat, 4 Feb 2023 20:16:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232254AbjBEBPo (ORCPT ); Sat, 4 Feb 2023 20:15:44 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D06027492 for ; Sat, 4 Feb 2023 17:15:33 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id j1so2675302pjd.0 for ; Sat, 04 Feb 2023 17:15:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=U56D9hJIIHchDDVPpFN73Ll8vi8yIOPIpQvQo3TH8jM=; b=i6LWUTJvWI0dc/A0LVN6lTUTLMzjjQ6p2D2q854wTpFsn/PpfZ4Ni0FxOT7l7PDwfj Bw2MbOZYFSWQceC/UN0zUQW8lwOLMJ1Hh0WFERaoUczhd87i4eUgueIat4nCIl9AraU6 n5OJfeqrPIDovcfUjZDD8a3YsWBrv3iWYsW7BS/HsjHIdf/if/3Cm2DYKqcjKP6eykDr hl7dwJkjUxwuxLQwWedT97MVQWFZu88vMy538eYLVUmVLRNc6BDR1oeeGJPjf5lRZXeF gzouB0IezaAq1dEsXx6d426cTHqvOe5NQ6TO24coYRVvWMydAsGhSFn2+AbVEAKVulkH bSlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U56D9hJIIHchDDVPpFN73Ll8vi8yIOPIpQvQo3TH8jM=; b=Tnx7c0S4SfYkH9HLfQvo04B1Dw++5+kwleElRzgctq6yWh6Fv7w0qEIlBGAePdAa6c RvvSPYSxPZhLZwm6mmm61/FQedoWk7w70f4BFftMRYRdOxcftfY9b5eKuVfnJTwIkSVq p/u/5BI9pHbuQcA6uJ2n1EvyuRrk2cyFicYxInQ0FqH4KO0y6PxeF3XkWhZH38P/+6U8 feH9HZ2uACgH/9Z7G8Zkyz0Xi2OsvXelHmxIl0YD8InTMA3+4siwBSL8SY6ParMyQRns Myd+glbHH8KLIrWcUgX7JWy5zxbWs32LAW0hBjKQiV3zvCqSmhLysCrWLhBffDZXltBL T4Ww== X-Gm-Message-State: AO0yUKWmXYqjKI+T89uw4u3GodZ8NrAU+T5NSbr2sQbhl3qB9FOqNxP+ 6DAn4P05vnDOcEOdSvfxOcuVMVeVnB4Xh2ji X-Received: by 2002:a05:6a20:12cd:b0:b8:a56f:7612 with SMTP id v13-20020a056a2012cd00b000b8a56f7612mr19679734pzg.55.1675559732776; Sat, 04 Feb 2023 17:15:32 -0800 (PST) Received: from atishp.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id c7-20020a17090a020700b0023080c4c3bcsm2721917pjc.31.2023.02.04.17.15.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Feb 2023 17:15:32 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Andrew Jones , Albert Ou , Anup Patel , Atish Patra , Guo Ren , Heiko Stuebner , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paul Walmsley , Will Deacon Subject: [PATCH v5 11/14] RISC-V: KVM: Implement trap & emulate for hpmcounters Date: Sat, 4 Feb 2023 17:15:12 -0800 Message-Id: <20230205011515.1284674-12-atishp@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230205011515.1284674-1-atishp@rivosinc.com> References: <20230205011515.1284674-1-atishp@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756952199645604708?= X-GMAIL-MSGID: =?utf-8?q?1756952199645604708?= As the KVM guests only see the virtual PMU counters, all hpmcounter access should trap and KVM emulates the read access on behalf of guests. Reviewed-by: Andrew Jones Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_vcpu_pmu.h | 16 ++++++++ arch/riscv/kvm/vcpu_insn.c | 4 +- arch/riscv/kvm/vcpu_pmu.c | 59 ++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h index 40905db..344a3ad 100644 --- a/arch/riscv/include/asm/kvm_vcpu_pmu.h +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h @@ -48,6 +48,19 @@ struct kvm_pmu { #define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu_context) #define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu_context)) +#if defined(CONFIG_32BIT) +#define KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS \ +{.base = CSR_CYCLEH, .count = 31, .func = kvm_riscv_vcpu_pmu_read_hpm }, \ +{.base = CSR_CYCLE, .count = 31, .func = kvm_riscv_vcpu_pmu_read_hpm }, +#else +#define KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS \ +{.base = CSR_CYCLE, .count = 31, .func = kvm_riscv_vcpu_pmu_read_hpm }, +#endif + +int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu, unsigned int csr_num, + unsigned long *val, unsigned long new_val, + unsigned long wr_mask); + int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_return *retdata); int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, struct kvm_vcpu_sbi_return *retdata); @@ -71,6 +84,9 @@ void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); struct kvm_pmu { }; +#define KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS \ +{ .base = 0, .count = 0, .func = NULL }, + static inline void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) {} static inline void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu) {} static inline void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) {} diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c index 0bb5276..f689337 100644 --- a/arch/riscv/kvm/vcpu_insn.c +++ b/arch/riscv/kvm/vcpu_insn.c @@ -213,7 +213,9 @@ struct csr_func { unsigned long wr_mask); }; -static const struct csr_func csr_funcs[] = { }; +static const struct csr_func csr_funcs[] = { + KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS +}; /** * kvm_riscv_vcpu_csr_return -- Handle CSR read/write after user space diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index 6d09a6f..fe9db221 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -17,6 +17,58 @@ #define kvm_pmu_num_counters(pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs) +static int pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, + unsigned long *out_val) +{ + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); + struct kvm_pmc *pmc; + u64 enabled, running; + + pmc = &kvpmu->pmc[cidx]; + if (!pmc->perf_event) + return -EINVAL; + + pmc->counter_val += perf_event_read_value(pmc->perf_event, &enabled, &running); + *out_val = pmc->counter_val; + + return 0; +} + +int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu, unsigned int csr_num, + unsigned long *val, unsigned long new_val, + unsigned long wr_mask) +{ + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); + int cidx, ret = KVM_INSN_CONTINUE_NEXT_SEPC; + + if (!kvpmu || !kvpmu->init_done) { + /* + * In absence of sscofpmf in the platform, the guest OS may use + * the legacy PMU driver to read cycle/instret. In that case, + * just return 0 to avoid any illegal trap. However, any other + * hpmcounter access should result in illegal trap as they must + * be access through SBI PMU only. + */ + if (csr_num == CSR_CYCLE || csr_num == CSR_INSTRET) { + *val = 0; + return ret; + } else { + return KVM_INSN_ILLEGAL_TRAP; + } + } + + /* The counter CSR are read only. Thus, any write should result in illegal traps */ + if (wr_mask) + return KVM_INSN_ILLEGAL_TRAP; + + cidx = csr_num - CSR_CYCLE; + + if (pmu_ctr_read(vcpu, cidx, val) < 0) + return KVM_INSN_ILLEGAL_TRAP; + + return ret; +} + int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, struct kvm_vcpu_sbi_return *retdata) { struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); @@ -69,7 +121,12 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, struct kvm_vcpu_sbi_return *retdata) { - /* TODO */ + int ret; + + ret = pmu_ctr_read(vcpu, cidx, &retdata->out_val); + if (ret == -EINVAL) + retdata->err_val = SBI_ERR_INVALID_PARAM; + return 0; }