Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp1300731rdb; Fri, 20 Oct 2023 14:43:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLjSjwiSmgvElUauqBfMJ2VQFIJ+3vNqHmGLi00jX+dwAY4Y7cYgMzkgmo/xHF7mnHMsyG X-Received: by 2002:a05:6358:70a:b0:166:de88:6148 with SMTP id e10-20020a056358070a00b00166de886148mr3389195rwj.1.1697838224584; Fri, 20 Oct 2023 14:43:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697838224; cv=none; d=google.com; s=arc-20160816; b=WtcUwG6yc0F46uKqBBCPmz+5KonioMNqEENjpju2prMpY7MEtl+orCmxAjhmDRx4Xv jK2VzD/DXhq1Gt+xnWIVXt5vPQhb2EsWQQQNEvYWHd7MsGkBxBvm5uojHNait+aoYKkm BPmP1/OK1JW5UPU1hpcr23Tzr3bmOP4IT03SRIFXSh1WsWGMDJqjGDyy8p55nzKPtyen /9PGe8hA30sOYpwRDlU8CNr1RQwUQQ3M5DJhI5pYh8wvPLw5nG9s3l19UwUYlHzwGoJm rCdndK1RZ6VDXTxRz483h7XZmVAAJskrEARX3l1ewe0zSrQp0XZzkurM6RtoxFwyG/ej V1TA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=OrkUb5e6f7M9UeNAstnZgH452akYrJ8pbLmUt7I8ZGI=; fh=nSGf6E5bqeZ4+WG1wtUl4dHMM2T+/oX4atvC7v39NTU=; b=QCL0L77FNXoexyANpDJ6wfjZ7ZQflGMOQicnqkQsbWmjsNJ5TW8wqGXo4JREaq8FSW Ow6N4wdlj8UdJI79JNY5R5RNHG5Qgg+4vRJNvu+fiVsR9e0ml0CqxmKzZn8C3uhCYCrW /biyxlsGHQ3guKlbNFzVN6+qTUkLpFmLmO93iU+Rhx9VZNsYIJnsVw75Scyj+xSTkkmZ ZDFBTRvSpaAqM5IjukqE4tuJALrI1jKbNOcn2OPhS43REsKWZQzvAB4DB4LWK2hMuPE+ +tHr8+iWeku2UN/1pt3k1aeS6IrkhsNTqfwk2UculajNlxnKj8nZz5JU+E0GBpVxXFtP svOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=m0LOjaqs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id b19-20020a6567d3000000b00578d0d070f7si2717285pgs.659.2023.10.20.14.43.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 14:43:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=m0LOjaqs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 3398282C4D41; Fri, 20 Oct 2023 14:43:33 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345575AbjJTVlu (ORCPT + 99 others); Fri, 20 Oct 2023 17:41:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345405AbjJTVlY (ORCPT ); Fri, 20 Oct 2023 17:41:24 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7893A10DE for ; Fri, 20 Oct 2023 14:41:10 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a7cc433782so17857687b3.3 for ; Fri, 20 Oct 2023 14:41:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697838069; x=1698442869; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OrkUb5e6f7M9UeNAstnZgH452akYrJ8pbLmUt7I8ZGI=; b=m0LOjaqsJG6WUA44qmHUQgL1iVXJNlpjFv5iS7kgiv5VRMueTW40/ijPGiGAa8YGf+ UwYIm/s/Zbs0JLSvKVKCtNBGZZfU9Aq8hLQnaTrafefCS9v+cv0jXAAhC+m+HStmp0vp 0lUg0FOQd1CfD5Ou9aLZw2IAAu71FcDwMeD8+VCQVjNP9EMZ1pudVomeVpSKj6d3qrBG aQpGIfhrO29hevkrrO/EJQcB4xXod8jy9MJOjETEDbcLBvIcT1DpAPLNlEDc8uUYuAoW Xu2GKPNllhFaG3WrwDsojg1uTmgnll2e3/qsr2uM5rA0w1dAsLfE2YNRzYRzMeSxdJr/ dsEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697838069; x=1698442869; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OrkUb5e6f7M9UeNAstnZgH452akYrJ8pbLmUt7I8ZGI=; b=mvqSCUUAli+XBX5adL59AROdE66sw+jTTwY7IC30sJey1zeHSMcjsmg29E4dI6wIoh pz7N2Y/5OEIEbxdXhkPOJITbbjSyRosFpJ9W1wUoEDh/0tEv/gE/5nlVfWuuNvRgYNLx g5Gaj0porfJkZmlCy2z7dmdgtJVxaHSNq831qymtuD9HZwsK7JX5wRiYMAQIGWNSU8ob yNF6wcokNA5J1z3DuY7OXOxiDEWoWD7Lm35Q7HV3fCy3UeXt7DA74Lsr0SBv0yHW6kbV PUsJh2cmRoqJrFQv+WnXdGkREsuN4NVU2k9mZkSKv6lZlNY+PIUuW1tpzl+QmH6x027F JVzA== X-Gm-Message-State: AOJu0YxBkKw/e6sAONQQqTL821Ur8SC1DxfckhhNWHZr9VyhujUuNnAX xbyGai33yoBrbVekX1EvDiGGWZEo1WYr X-Received: from rananta-linux.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:20a1]) (user=rananta job=sendgmr) by 2002:a81:6cc3:0:b0:5a7:b8d1:ef65 with SMTP id h186-20020a816cc3000000b005a7b8d1ef65mr73650ywc.3.1697838069760; Fri, 20 Oct 2023 14:41:09 -0700 (PDT) Date: Fri, 20 Oct 2023 21:40:53 +0000 In-Reply-To: <20231020214053.2144305-1-rananta@google.com> Mime-Version: 1.0 References: <20231020214053.2144305-1-rananta@google.com> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog Message-ID: <20231020214053.2144305-14-rananta@google.com> Subject: [PATCH v8 13/13] KVM: selftests: aarch64: vPMU test for immutability From: Raghavendra Rao Ananta To: Oliver Upton , Marc Zyngier Cc: Alexandru Elisei , James Morse , Suzuki K Poulose , Paolo Bonzini , Zenghui Yu , Shaoqin Huang , Jing Zhang , Reiji Watanabe , Colton Lewis , Raghavendra Rao Anata , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 20 Oct 2023 14:43:33 -0700 (PDT) KVM marks some of the vPMU registers as immutable to userspace once the vCPU has started running. Add a test scenario to check this behavior. Signed-off-by: Raghavendra Rao Ananta --- .../kvm/aarch64/vpmu_counter_access.c | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c b/tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c index 2b697b144e677..f87d76c614e8b 100644 --- a/tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c +++ b/tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c @@ -9,7 +9,8 @@ * those counters, and if the guest is prevented from accessing any * other counters. * It also checks if the userspace accesses to the PMU regsisters honor the - * PMCR.N value that's set for the guest. + * PMCR.N value that's set for the guest and if these accesses are immutable + * after KVM has run once. * This test runs only when KVM_CAP_ARM_PMU_V3 is supported on the host. */ #include @@ -648,6 +649,48 @@ static void run_error_test(uint64_t pmcr_n) destroy_vpmu_vm(); } +static uint64_t immutable_regs[] = { + SYS_PMCR_EL0, + SYS_PMCNTENSET_EL0, + SYS_PMCNTENCLR_EL0, + SYS_PMINTENSET_EL1, + SYS_PMINTENCLR_EL1, + SYS_PMOVSSET_EL0, + SYS_PMOVSCLR_EL0, +}; + +/* + * Create a guest with one vCPU, run it, and then make an attempt to update + * the registers in @immutable_regs[] (with their complements). KVM shouldn't + * allow updating these registers once vCPU starts running. Hence, the test + * fails if that's not the case. + */ +static void run_immutable_test(uint64_t pmcr_n) +{ + int i; + struct kvm_vcpu *vcpu; + uint64_t reg_id, reg_val, reg_val_orig; + + create_vpmu_vm(guest_code); + vcpu = vpmu_vm.vcpu; + + run_vcpu(vcpu, pmcr_n); + + for (i = 0; i < ARRAY_SIZE(immutable_regs); i++) { + reg_id = immutable_regs[i]; + + vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(reg_id), ®_val_orig); + vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(reg_id), ~reg_val_orig); + vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(reg_id), ®_val); + + TEST_ASSERT(reg_val == reg_val_orig, + "Register 0x%llx value updated after vCPU run: 0x%lx; expected: 0x%lx\n", + KVM_ARM64_SYS_REG(reg_id), reg_val, reg_val_orig); + } + + destroy_vpmu_vm(); +} + /* * Return the default number of implemented PMU event counters excluding * the cycle counter (i.e. PMCR_EL0.N value) for the guest. @@ -677,5 +720,7 @@ int main(void) for (i = pmcr_n + 1; i < ARMV8_PMU_MAX_COUNTERS; i++) run_error_test(i); + run_immutable_test(pmcr_n); + return 0; } -- 2.42.0.655.g421f12c284-goog