From patchwork Wed Feb 1 23:12:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 51602 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp563336wrn; Wed, 1 Feb 2023 15:17:04 -0800 (PST) X-Google-Smtp-Source: AK7set9nzgWZTs7cVEP0H+NzhX3xlmU0g/ihVgpGFUAvL6+9PWeFieCF3ITfAxCMPospO2JU3H1K X-Received: by 2002:a05:6a20:d48a:b0:b8:72f9:8e5d with SMTP id im10-20020a056a20d48a00b000b872f98e5dmr4847558pzb.62.1675293423751; Wed, 01 Feb 2023 15:17:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675293423; cv=none; d=google.com; s=arc-20160816; b=w4HLNMfU9u917lLrL8KtbJFBWWDOBMSle3BxDKigLsLYTEugaTXIbXx+wVEukR040g g3O2i82Z6NOyBnCF2fzJ1/S4qPTKm7DPI3HByLIFpNMxXhwbJ0rT+nX2jf/12oOst0Lk v3V9FWBH0Pg7Y/p9kBccJFPhaLD4WsdooXZunBZN/pDs8c75K1REPYyxHFzkdXu8PTKu +0h9zL0IvJa4snxKmfr2TFiS7XgRWhr+00ptVJGYXcqISQgLcacqvT1PL3f7yNTBbppp MBFXOMVjTa/ZHNLt8E9RFic9tKpoKxe/Zjx6Wqc6p1f/DaF81/DcwbE4K6EEaWOuTCth HLRQ== 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=caynWJva1dOdOq57JXJiWjuHhrwg++AWRdrPSfdbeeE=; b=fG+jGpHa/3prhCMxL2qB8Rdg5OjZBrAtsoth/5pULSQRX62jMw6Gn9vKpL+4D9YyXQ 2b0L8yn3yAzjePjOmM2rt3+2MOsXx6iYbq2W7LymTloozVbFbI/nB27YX61px92xvZ/1 PcjBDxktc6ecuP2QxJoDD9G1uANT5IwabTNFeq5sRcJ+5NfD/7Y0zO6JgJ1/8jB1R9JL OzyrYpg4VBPS7LAyOO6Ns73D7bYuv8IO/G60H8P7zSIuXWFqTHJVmlBmI3MeMgKh8ALV 4ey5/cwdjthThXuiXkCH/5TdfEQJBaWYnyqqk3Qcfu1KvFVUmNdq5dQGSF9lLo8m/ZuI lTiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=xIjIgO2h; 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 v13-20020a63bf0d000000b004ad057fcf5bsi21347145pgf.728.2023.02.01.15.16.27; Wed, 01 Feb 2023 15:17:03 -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=xIjIgO2h; 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 S231935AbjBAXNj (ORCPT + 99 others); Wed, 1 Feb 2023 18:13:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231676AbjBAXNT (ORCPT ); Wed, 1 Feb 2023 18:13:19 -0500 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22E4B577F6 for ; Wed, 1 Feb 2023 15:13:03 -0800 (PST) Received: by mail-pj1-x1030.google.com with SMTP id rm7-20020a17090b3ec700b0022c05558d22so125154pjb.5 for ; Wed, 01 Feb 2023 15:13:03 -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=caynWJva1dOdOq57JXJiWjuHhrwg++AWRdrPSfdbeeE=; b=xIjIgO2h+i3GtNolEUJCCx5HjIfOoh9rZvsUz7IhhfFTBguPawX2wzl0pDyTBe2hC5 6HJIE1SOl4Fh5uRkRXmGb9tYtUL5xa63if+dNcjEwnzJ/GKUO/kumGJK9ZC2UGmfhkSP iLAmCsDZKo51n+nRPztNwLG++oKtoN3tzJLaoiIgZvDqLujl0VQVKHUpk1OTfoCtvafD W1xcnPkjd8FdQvTVRKOvlMEJhg9MLtE71z5GlMUiFSCO43ttvGw6LnuSH6LnUYyGfCg2 cVo2zmoJQRkN2C2AxFH8IgJgbrvhOuRrWEfIjvEptpTwO9Reqkv97qgZV7PSjmini0bz ZrwA== 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=caynWJva1dOdOq57JXJiWjuHhrwg++AWRdrPSfdbeeE=; b=DGVdx8eqxDqzzGQW+++B2Fu/jGYf7GTdFcFDcJrbfTeGTFNKsv1hG4w8erqwkDh/bo /MjmFMar/nTq62/qJLbGLtqmX6k554kr/uFBjOSe0fJO0pZMDg7XbsewQA1qdmxO5mXo Y4rCB2H9GtJVofnP9jDkJSNhgyHQl8PXJgn0NHali1uTol1K8vmbHU38PY1rmbi7Tws5 5tgPVX3WIZG1rcGVXeHjrmYeg+ZRmIE0VpvhDAkz2R0EU5h+1a0bq3lL6ejH86elt/6H 5ruZDbPRdykKD6z/nXCouYPdOxtuBmqTkC+jKCVmYSF1rlmV7xnuaN3sK8Zau/1TPPeX K6xA== X-Gm-Message-State: AO0yUKW0CJT3EInk6v+bwXo+d8oA3O8uD57SFqLxTiicDEWlrL1rYdhG JqX5oWmShsmWQFhkCViguWASB1kQZBtY2bR1 X-Received: by 2002:a17:90a:53:b0:22b:f834:3fac with SMTP id 19-20020a17090a005300b0022bf8343facmr4297815pjb.11.1675293182978; Wed, 01 Feb 2023 15:13:02 -0800 (PST) Received: from atishp.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id t3-20020a17090a510300b0022bf0b0e1b7sm1861774pjh.10.2023.02.01.15.13.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 15:13:02 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Andrew Jones , Albert Ou , Anup Patel , Atish Patra , Eric Lin , 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 v4 11/14] RISC-V: KVM: Implement trap & emulate for hpmcounters Date: Wed, 1 Feb 2023 15:12:47 -0800 Message-Id: <20230201231250.3806412-12-atishp@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230201231250.3806412-1-atishp@rivosinc.com> References: <20230201231250.3806412-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=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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756672476980996431?= X-GMAIL-MSGID: =?utf-8?q?1756672476980996431?= 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 | 15 +++++++ arch/riscv/kvm/vcpu_insn.c | 4 +- arch/riscv/kvm/vcpu_pmu.c | 59 ++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h index e2b4038..2afaaf5 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); @@ -70,6 +83,8 @@ void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); #else 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) {} 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 9a531fe..6fa0065 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; }