Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp1923057lqz; Tue, 2 Apr 2024 01:35:37 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVkhv9sjA+dW2X0rjWBeduc8Wp3p2pmeIMiS7z+NQmJVrzSQow/2Ah/uSsrOFhH4JsjCSX0dj1y7ea19vHQKdK9Gy4VhCkRKRErX0itug== X-Google-Smtp-Source: AGHT+IFgl9YHpkIo+q3lpxQApMI7HC2X/4Dt2vLxN57Pe7qSfGFt0qMCdOUpQG7Mk6zCe6Uqn9tJ X-Received: by 2002:a50:f60b:0:b0:56b:d9b6:1925 with SMTP id c11-20020a50f60b000000b0056bd9b61925mr8268020edn.22.1712046937010; Tue, 02 Apr 2024 01:35:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712046936; cv=pass; d=google.com; s=arc-20160816; b=v4OyfLSUMe2RQutq5kcqH2DPz++zvoHR9mN171LzGgF4AlBqRkdJxnMVoM1Mrrytxz +Src6TguwaU1tdN7EhcGTlDmaayY8UdvRgiULZR6ThP90oDOrAZ9F/D67mtRn2TMqZN7 woiQGW+ChlkHEEIQiwxa89U1hRYnlMI65pQ9OKbgs6yx20A/txdS0XJ9HGeBeOf+hNQn yYVHeO4f1OcYvzMShw+tsy2xVtZeyd44tRO+SeT30ht2Pt9WEcuxktAbli/q8gtHEBqs Z1MzYEUnm4Ws9omSTJFLTo46+EGnCq/MsqEaDYQ1gSyxJqHFYplF0/emyAcI1nR2xK1a ruyw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=43ImqbFDjqMCmjFGnMCs+yquSyjWrKaVgz6EdEbo8UY=; fh=R229OV7G6fHCkyUVj1y7OnKyRUrTaXkgThrQ97r0fro=; b=ScoDIXWs6Cg+zqg38KVC3XaKEnPyg+S7GWJwHhWcLsEvnv/phNYOcpojfhjIXxq8t0 P5qfcac2guRFYdxxm9yrr8uvEG9nLzb0zcE6Xjanb6wZvzInRHNgCCrjcwLxvOOWIlmT J+9yCFzemGhbwo9cl33UVtmPK3CMvEqBdLAo4T24o6VJJ+PDAyRJPod4GwQDF5cR9t+K xqR+2bHUlE1qznjs2To64A7FjrxIZ1v4elwgoDd9Wqn/AskqbwsM76U/tmK3jPAA5iV5 yXKSplR2/lXxzL9BJxHj0r72JpM2BQ2NEroGREh0iWCZyBiIuPc04+3+IoIxVzGOp/EL ixKQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=i+BGK+ip; arc=pass (i=1 spf=pass spfdomain=atishpatra.org dkim=pass dkdomain=atishpatra.org); spf=pass (google.com: domain of linux-kernel+bounces-127561-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-127561-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id z98-20020a509e6b000000b0056c420c4058si5210592ede.665.2024.04.02.01.35.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Apr 2024 01:35:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-127561-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=i+BGK+ip; arc=pass (i=1 spf=pass spfdomain=atishpatra.org dkim=pass dkdomain=atishpatra.org); spf=pass (google.com: domain of linux-kernel+bounces-127561-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-127561-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 8F92D1F22524 for ; Tue, 2 Apr 2024 08:35:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DFBBA4501C; Tue, 2 Apr 2024 08:35:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=atishpatra.org header.i=@atishpatra.org header.b="i+BGK+ip" Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D949D3E479 for ; Tue, 2 Apr 2024 08:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712046911; cv=none; b=LXaV8en+VbELJ6ctVlC5+q4ZAOwztDnEhP5eWnfQ85rIU0rOSrFQU7T9TS8yD+KYn+Xpxa7GQWZTWMpjC/m9JvyvraiHHIYA4lVHcbV63ZQYE84ixm25ugYqJyQA2sI+FEeLACdSF66k4AcVt3v76ZIBl4+ATIVOkg9xnLYYGSw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712046911; c=relaxed/simple; bh=mK373NT5o8w2V1WS0TRWuMqrXoNPT4Ng+C9HfRhs0EE=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=WuVdPvdzVVAssKevLp7AhdT3ciKWTh88aeoGp5pQz62D+tE1oNwsFdr0zSwZo0v1rvWATgTslTGYm/OgBnGzPfKKOpSbezpkrS9BeSS/wkdM9357lJJ282MIy66gWr0Jn22UMhlkRqJGgLEed7r6NSb0EN05mKhyYjfpRW5r7po= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=atishpatra.org; spf=pass smtp.mailfrom=atishpatra.org; dkim=pass (1024-bit key) header.d=atishpatra.org header.i=@atishpatra.org header.b=i+BGK+ip; arc=none smtp.client-ip=209.85.208.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=atishpatra.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=atishpatra.org Received: by mail-lj1-f182.google.com with SMTP id 38308e7fff4ca-2d476d7972aso70475821fa.1 for ; Tue, 02 Apr 2024 01:35:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atishpatra.org; s=google; t=1712046906; x=1712651706; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=43ImqbFDjqMCmjFGnMCs+yquSyjWrKaVgz6EdEbo8UY=; b=i+BGK+ipz9Vjr9ckuyk9fc0NugFX+tQQfJXeVT9dRjcdTxqdLkXc7GGxsBuahFnHCQ MNDPCuWcZRYGI0FZEz0CaeUrX0mTjR9FHQot1H/mvbvIfE/Ju6QWD6NthFleUtWKLzeH AMcbliDkJQgQVThkJXF5p6lTdbEEJiP90EDec= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712046906; x=1712651706; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=43ImqbFDjqMCmjFGnMCs+yquSyjWrKaVgz6EdEbo8UY=; b=te92m1br47Lpl7437nOybKu0/1I55H8YAEwW7ZcR55Q7IoI6IIMJkmkvic4cjiQvCe vanmKmyFLUZ6KmZCc+Blb3NuYQVffEk1XZQttlVkIH+4DGqQEyMYGM0YAB031UGknxA6 imoNelMpJH8cbGpf1xHpitqh1Uoi8m3Wy1u38hCpHV6f7bM5Yrx1hP8LpTplbM2/dr8r 0+wCBraByCiZJupQ59750tfbIR3A2Xz3gosApGc7QBoLmXL72frZPcYW0zMrlTWxCVXd Vh6QucNDRmsms0csR2p9TW5mPk/1fqX+WcGg0+mLu/ZpOJvWYYsm6CXiGeAjE08aWSGj 26Aw== X-Forwarded-Encrypted: i=1; AJvYcCVr3JPFiE95TCiENLmG+kaN9b29WNUTJrbrvPXGarQQWhOufrpABAPhSnRvuvrIzZ+uUqaOMXwryu8eIWVJpDTFXR99bVJ8M7sVbsgy X-Gm-Message-State: AOJu0YzplbQ4QWOY63PXYT4pwdx3VGaCbEPE37d2iFGOSahRc06xboUt iERER82U3aXq0WXY/BAs/n3uWtDq843NrRJqR0vCfHlbXvm6DIUPXRG8Y2Vp4F9mkcOnnLa4uP4 qJ3aYv4QKSH9EokhJHm+0pTXUezp9KwFI8/I+ X-Received: by 2002:a2e:918f:0:b0:2d2:7702:cb74 with SMTP id f15-20020a2e918f000000b002d27702cb74mr7938841ljg.20.1712046905708; Tue, 02 Apr 2024 01:35:05 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240229010130.1380926-1-atishp@rivosinc.com> <20240229010130.1380926-14-atishp@rivosinc.com> <20240302-ed6c516829dc0ed616f39a45@orel> In-Reply-To: <20240302-ed6c516829dc0ed616f39a45@orel> From: Atish Patra Date: Tue, 2 Apr 2024 01:34:54 -0700 Message-ID: Subject: Re: [PATCH v4 13/15] KVM: riscv: selftests: Add SBI PMU selftest To: Andrew Jones Cc: Atish Patra , linux-kernel@vger.kernel.org, Albert Ou , Alexandre Ghiti , Anup Patel , Conor Dooley , Guo Ren , Icenowy Zheng , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paolo Bonzini , Paul Walmsley , Shuah Khan , Will Deacon Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Mar 2, 2024 at 3:52=E2=80=AFAM Andrew Jones wrote: > > On Wed, Feb 28, 2024 at 05:01:28PM -0800, Atish Patra wrote: > > This test implements basic sanity test and cycle/instret event > > counting tests. > > > > Signed-off-by: Atish Patra > > --- > > tools/testing/selftests/kvm/Makefile | 1 + > > tools/testing/selftests/kvm/riscv/sbi_pmu.c | 340 ++++++++++++++++++++ > > 2 files changed, 341 insertions(+) > > create mode 100644 tools/testing/selftests/kvm/riscv/sbi_pmu.c > > > > diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selft= ests/kvm/Makefile > > index 426f85798aea..b2dce6843b9e 100644 > > --- a/tools/testing/selftests/kvm/Makefile > > +++ b/tools/testing/selftests/kvm/Makefile > > @@ -195,6 +195,7 @@ TEST_GEN_PROGS_riscv +=3D kvm_create_max_vcpus > > TEST_GEN_PROGS_riscv +=3D kvm_page_table_test > > TEST_GEN_PROGS_riscv +=3D set_memory_region_test > > TEST_GEN_PROGS_riscv +=3D steal_time > > +TEST_GEN_PROGS_riscv +=3D riscv/sbi_pmu > > We put the > > TEST_GEN_PROGS_riscv +=3D riscv/... > > lines at the top of the > > TEST_GEN_PROGS_riscv +=3D ... > Done. > set > > > > > SPLIT_TESTS +=3D arch_timer > > SPLIT_TESTS +=3D get-reg-list > > diff --git a/tools/testing/selftests/kvm/riscv/sbi_pmu.c b/tools/testin= g/selftests/kvm/riscv/sbi_pmu.c > > new file mode 100644 > > index 000000000000..fc1fc5eea99e > > --- /dev/null > > +++ b/tools/testing/selftests/kvm/riscv/sbi_pmu.c > > @@ -0,0 +1,340 @@ > > +// SPDX-License-Identifier: GPL-2.0-only > > +/* > > + * arch_timer.c - Tests the riscv64 sstc timer IRQ functionality > > + * > > + * The test validates the sstc timer IRQs using vstimecmp registers. > > + * It's ported from the aarch64 arch_timer test. > > The header (apparently borrowed from arch_timer.c) needs to be updated > to talk about the pmu instead of the timer. > Oops. Thanks for catching it. Fixed it. > > + * > > + * Copyright (c) 2024, Rivos Inc. > > + */ > > + > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include "kvm_util.h" > > +#include "test_util.h" > > +#include "processor.h" > > + > > +/* Maximum counters (firmware + hardware)*/ > ^ space > > > +#define RISCV_MAX_PMU_COUNTERS 64 > > +union sbi_pmu_ctr_info ctrinfo_arr[RISCV_MAX_PMU_COUNTERS]; > > + > > +/* Cache the available counters in a bitmask */ > > +static unsigned long counter_mask_available; > > + > > +unsigned long pmu_csr_read_num(int csr_num) > > +{ > > +#define switchcase_csr_read(__csr_num, __val) {\ > > + case __csr_num: \ > > + __val =3D csr_read(__csr_num); \ > > + break; } > > +#define switchcase_csr_read_2(__csr_num, __val) {\ > > + switchcase_csr_read(__csr_num + 0, __val) \ > > + switchcase_csr_read(__csr_num + 1, __val)} > > +#define switchcase_csr_read_4(__csr_num, __val) {\ > > + switchcase_csr_read_2(__csr_num + 0, __val) \ > > + switchcase_csr_read_2(__csr_num + 2, __val)} > > +#define switchcase_csr_read_8(__csr_num, __val) {\ > > + switchcase_csr_read_4(__csr_num + 0, __val) \ > > + switchcase_csr_read_4(__csr_num + 4, __val)} > > +#define switchcase_csr_read_16(__csr_num, __val) {\ > > + switchcase_csr_read_8(__csr_num + 0, __val) \ > > + switchcase_csr_read_8(__csr_num + 8, __val)} > > +#define switchcase_csr_read_32(__csr_num, __val) {\ > > + switchcase_csr_read_16(__csr_num + 0, __val) \ > > + switchcase_csr_read_16(__csr_num + 16, __val)} > > + > > + unsigned long ret =3D 0; > > + > > + switch (csr_num) { > > + switchcase_csr_read_32(CSR_CYCLE, ret) > > + switchcase_csr_read_32(CSR_CYCLEH, ret) > > + default : > > + break; > > + } > > + > > + return ret; > > +#undef switchcase_csr_read_32 > > +#undef switchcase_csr_read_16 > > +#undef switchcase_csr_read_8 > > +#undef switchcase_csr_read_4 > > +#undef switchcase_csr_read_2 > > +#undef switchcase_csr_read > > +} > > + > > +static inline void dummy_func_loop(int iter) > > +{ > > + int i =3D 0; > > + > > + while (i < iter) { > > + asm volatile("nop"); > > + i++; > > + } > > +} > > + > > +static void guest_illegal_exception_handler(struct ex_regs *regs) > > +{ > > + __GUEST_ASSERT(regs->cause =3D=3D EXC_INST_ILLEGAL, > > + "Unexpected exception handler %lx\n", regs->cause)= ; > > Shouldn't we be reporting somehow that we were here? We seem to be using > this handler to skip instructions which don't work, which is fine, if > we have some knowledge we skipped them and then do something else. > Otherwise I don't understand. > This is only used in test_vm_basic_test to validate that the guest will get an illegal exception if they try to access without configuring first. Any other test that validates the functionality will not use it. > > + > > + /* skip the trapping instruction */ > > + regs->epc +=3D 4; > > +} > > + > > +static unsigned long get_counter_index(unsigned long cbase, unsigned l= ong cmask, > > + unsigned long cflags, > > + unsigned long event) > > +{ > > + struct sbiret ret; > > + > > + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cba= se, cmask, > > + cflags, event, 0, 0); > > + __GUEST_ASSERT(ret.error =3D=3D 0, "config matching failed %ld\n"= , ret.error); > > + GUEST_ASSERT((ret.value < RISCV_MAX_PMU_COUNTERS) && > > + ((1UL << ret.value) & counter_mask_available)); > > I'd prefer to break these apart so it's more clear which one fails, if on= e > fails. > > GUEST_ASSERT(ret.value < RISCV_MAX_PMU_COUNTERS); > GUEST_ASSERT(BIT(ret.value) & counter_mask_available); > Done. > > + > > + return ret.value; > > +} > > + > > +static unsigned long get_num_counters(void) > > +{ > > + struct sbiret ret; > > + > > + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_NUM_COUNTERS, 0, 0, 0,= 0, 0, 0); > > + > > + __GUEST_ASSERT(ret.error =3D=3D 0, "Unable to retrieve number of = counters from SBI PMU"); > > + > > nit: drop this blank line > > > + __GUEST_ASSERT(ret.value < RISCV_MAX_PMU_COUNTERS, > > + "Invalid number of counters %ld\n", ret.value); > > + > > + return ret.value; > > +} > > + > > +static void update_counter_info(int num_counters) > > +{ > > + int i =3D 0; > > + struct sbiret ret; > > + > > + for (i =3D 0; i < num_counters; i++) { > > + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_GET_IN= FO, i, 0, 0, 0, 0, 0); > > + > > + /* There can be gaps in logical counter indicies*/ > > + if (!ret.error) > > + GUEST_ASSERT_NE(ret.value, 0); > > I guess this should be > > if (ret.error) > continue; > GUEST_ASSERT_NE(ret.value, 0); > Fixed it. > > + > > + ctrinfo_arr[i].value =3D ret.value; > > + counter_mask_available |=3D BIT(i); > > + } > > + > > + GUEST_ASSERT(counter_mask_available > 0); > > +} > > + > > +static unsigned long read_counter(int idx, union sbi_pmu_ctr_info ctri= nfo) > > +{ > > + unsigned long counter_val =3D 0; > > + struct sbiret ret; > > + > > + __GUEST_ASSERT(ctrinfo.type < 2, "Invalid counter type %d", ctrin= fo.type); > > + > > + if (ctrinfo.type =3D=3D SBI_PMU_CTR_TYPE_HW) { > > + counter_val =3D pmu_csr_read_num(ctrinfo.csr); > > + } else if (ctrinfo.type =3D=3D SBI_PMU_CTR_TYPE_FW) { > > + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_REA= D, idx, 0, 0, 0, 0, 0); > > + GUEST_ASSERT(ret.error =3D=3D 0); > > + counter_val =3D ret.value; > > + } > > + > > + return counter_val; > > +} > > + > > +static void start_counter(unsigned long counter, unsigned long start_f= lags, > > + unsigned long ival) > > +{ > > + struct sbiret ret; > > + > > + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, counter= , 1, start_flags, > > + ival, 0, 0); > > + __GUEST_ASSERT(ret.error =3D=3D 0, "Unable to start counter %ld\n= ", counter); > > +} > > + > > +static void stop_counter(unsigned long counter, unsigned long stop_fla= gs) > > +{ > > + struct sbiret ret; > > + > > + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, counter,= 1, stop_flags, > > + 0, 0, 0); > > + if (stop_flags & SBI_PMU_STOP_FLAG_RESET) > > + __GUEST_ASSERT(ret.error =3D=3D SBI_ERR_ALREADY_STOPPED, > > + "Unable to stop counter %ld\n", counter); > > This looks like we're abusing the SBI_PMU_STOP_FLAG_RESET flag to do the > already-stopped test. I'd rather helper functions work generally and do > stuff like this in test code with comments pointing it out. Or just > cleanly and separately set up an already-stopped test, so it's clear. > Doing it in test code adds redundancy. I will create two separate functions= . > > + else > > + __GUEST_ASSERT(ret.error =3D=3D 0, "Unable to stop counte= r %ld error %ld\n", > > + counter, ret.error); > > +} > > + > > +static void test_pmu_event(unsigned long event) > > +{ > > + unsigned long counter; > > + unsigned long counter_value_pre, counter_value_post; > > + unsigned long counter_init_value =3D 100; > > + > > + counter =3D get_counter_index(0, counter_mask_available, 0, event= ); > > + counter_value_pre =3D read_counter(counter, ctrinfo_arr[counter])= ; > > + > > + /* Do not set the initial value */ > > + start_counter(counter, 0, counter_init_value); > > + dummy_func_loop(10000); > > + > > nit: I'd remove this blank line so we have start/dummy/stop all together > in a group. Same comment below. > Fixed it. > > + stop_counter(counter, 0); > > + > > + counter_value_post =3D read_counter(counter, ctrinfo_arr[counter]= ); > > + __GUEST_ASSERT(counter_value_post > counter_value_pre, > > + "counter_value_post %lx counter_value_pre %lx\n", > > + counter_value_post, counter_value_pre); > > + > > + /* Now set the initial value and compare */ > > + start_counter(counter, SBI_PMU_START_FLAG_SET_INIT_VALUE, counter= _init_value); > > We should try to confirm that we reset the counter, otherwise the check > below only proves that the value we read is greater than 100, which it > is possible even if the reset doesn't work. > Hmm. There is no way to just update the counter value without starting it. Reading it without stopping is not reliable. Maybe we can do this. 1. Reset it to 100. Stop it immediately after and read it. Let's say the value is X 2. Now reset it to counter X + 1000. 3. Do the validation with the above reset value in #2. Wdyt ? > > + dummy_func_loop(10000); > > + > > + stop_counter(counter, 0); > > + > > + counter_value_post =3D read_counter(counter, ctrinfo_arr[counter]= ); > > + __GUEST_ASSERT(counter_value_post > counter_init_value, > > + "counter_value_post %lx counter_init_value %lx\n", > > + counter_value_post, counter_init_value); > > + > > + stop_counter(counter, SBI_PMU_STOP_FLAG_RESET); > > +} > > + > > +static void test_invalid_event(void) > > +{ > > + struct sbiret ret; > > + unsigned long event =3D 0x1234; /* A random event */ > > + > > + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, 0, > > + counter_mask_available, 0, event, 0, 0); > > + GUEST_ASSERT_EQ(ret.error, SBI_ERR_NOT_SUPPORTED); > > +} > > + > > +static void test_pmu_events(int cpu) > > cpu is unused so the parameter list can be void. Same comment for > test_pmu_basic_sanity() > Fixed. > > +{ > > + int num_counters =3D 0; > > + > > + /* Get the counter details */ > > + num_counters =3D get_num_counters(); > > + update_counter_info(num_counters); > > + > > + /* Sanity testing for any random invalid event */ > > + test_invalid_event(); > > + > > + /* Only these two events are guranteed to be present */ > > guaranteed > > > + test_pmu_event(SBI_PMU_HW_CPU_CYCLES); > > + test_pmu_event(SBI_PMU_HW_INSTRUCTIONS); > > + > > + GUEST_DONE(); > > +} > > + > > +static void test_pmu_basic_sanity(int cpu) > > +{ > > + long out_val =3D 0; > > + bool probe; > > + struct sbiret ret; > > + int num_counters =3D 0, i; > > + unsigned long counter_val =3D -1; > > + union sbi_pmu_ctr_info ctrinfo; > > + > > + probe =3D guest_sbi_probe_extension(SBI_EXT_PMU, &out_val); > > + GUEST_ASSERT(probe && out_val =3D=3D 1); > > + > > + num_counters =3D get_num_counters(); > > + > > + for (i =3D 0; i < num_counters; i++) { > > + ret =3D sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_GET_IN= FO, i, > > + 0, 0, 0, 0, 0); > > + > > + /* There can be gaps in logical counter indicies*/ > > + if (!ret.error) > > + GUEST_ASSERT_NE(ret.value, 0); > > + else > > + continue; > > nit: > > if (ret.error) > continue; > GUEST_ASSERT_NE(ret.value, 0); > Done. > > + > > + ctrinfo.value =3D ret.value; > > + > > + /* Accesibility check of hardware and read capability of = firmware counters */ > > Accessibility > Fixed. > > + counter_val =3D read_counter(i, ctrinfo); > > + /* The spec doesn't mandate any initial value. Verify if = a sane value */ > > + GUEST_ASSERT_NE(counter_val, -1); > > Hmm, does -1 have any special meaning? Otherwise it's a member of the set > of 'any', so there's nothing we can test. Or, maybe we can test that bits > higher than the ctrinfo bitwidth are zero. Although those bits might also > be unspecified, which means there's nothing we can test. > Yeah. I have removed the validation with a clarification. > > + } > > + > > + GUEST_DONE(); > > +} > > + > > +static void run_vcpu(struct kvm_vcpu *vcpu) > > +{ > > + struct ucall uc; > > + > > + vcpu_run(vcpu); > > + switch (get_ucall(vcpu, &uc)) { > > + case UCALL_ABORT: > > + REPORT_GUEST_ASSERT(uc); > > + break; > > + case UCALL_DONE: > > + case UCALL_SYNC: > > + break; > > + default: > > + TEST_FAIL("Unknown ucall %lu", uc.cmd); > > + break; > > + } > > +} > > + > > +void test_vm_destroy(struct kvm_vm *vm) > > +{ > > + memset(ctrinfo_arr, 0, sizeof(union sbi_pmu_ctr_info) * RISCV_MAX= _PMU_COUNTERS); > > + counter_mask_available =3D 0; > > + kvm_vm_free(vm); > > +} > > + > > +static void test_vm_basic_test(void *guest_code) > > +{ > > + struct kvm_vm *vm; > > + struct kvm_vcpu *vcpu; > > + > > + vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); > > + __TEST_REQUIRE(__vcpu_has_ext(vcpu, RISCV_ISA_EXT_REG(KVM_RISCV_S= BI_EXT_PMU)), > > Shouldn't this be checking RISCV_SBI_EXT_REG(KVM_RISCV_SBI_EXT_PMU)? > Oops. Fat fingers. Fixed it. > We should probably create two more helpers > > bool __vcpu_has_isa_ext(struct kvm_vcpu *vcpu, uint64_t isa_ext) > { > return __vcpu_has_ext(vcpu, RISCV_ISA_EXT_REG(isa_ext)); > } > bool __vcpu_has_sbi_ext(struct kvm_vcpu *vcpu, uint64_t sbi_ext) > { > return __vcpu_has_ext(vcpu, RISCV_SBI_EXT_REG(sbi_ext)); > } > > to make the extension checks less verbose and error prone. > Good idea. Added the patch. > > + "SBI PMU not available, skipping test"= ); > > + vm_init_vector_tables(vm); > > + /* Illegal instruction handler is required to verify read access = without configuration */ > > + vm_install_exception_handler(vm, EXC_INST_ILLEGAL, guest_illegal_= exception_handler); > > + > > + vcpu_init_vector_tables(vcpu); > > + vcpu_args_set(vcpu, 1, 0); > > We don't use the arguments in the guest code functions so we don't need > this call to vcpu_args_set() > Done. > > + run_vcpu(vcpu); > > + > > + test_vm_destroy(vm); > > +} > > + > > +static void test_vm_events_test(void *guest_code) > > +{ > > + struct kvm_vm *vm =3D NULL; > > + struct kvm_vcpu *vcpu =3D NULL; > > + > > + vm =3D vm_create_with_one_vcpu(&vcpu, guest_code); > > + __TEST_REQUIRE(__vcpu_has_ext(vcpu, RISCV_ISA_EXT_REG(KVM_RISCV_S= BI_EXT_PMU)), > > Same comment as above. > > > + "SBI PMU not available, skipping test"= ); > > + vcpu_args_set(vcpu, 1, 0); > > Same comment as above. > > > + run_vcpu(vcpu); > > + > > + test_vm_destroy(vm); > > +} > > + > > +int main(void) > > +{ > > + test_vm_basic_test(test_pmu_basic_sanity); > > + pr_info("SBI PMU basic test : PASS\n"); > > + > > + test_vm_events_test(test_pmu_events); > > + pr_info("SBI PMU event verification test : PASS\n"); > > + > > + return 0; > > +} > > -- > > 2.34.1 > > > > Thanks, > drew -- Regards, Atish