From patchwork Sat Mar 11 00:46:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 67916 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp60098wrd; Fri, 10 Mar 2023 17:14:45 -0800 (PST) X-Google-Smtp-Source: AK7set8Svnnr6SyamjihPNT/jRyX7BaYLT65loljvv1eJ7j6tmSYg8B7SvPkHvxuxGsIRZKcvMlB X-Received: by 2002:a17:903:18d:b0:19a:eb93:6165 with SMTP id z13-20020a170903018d00b0019aeb936165mr36889673plg.22.1678497284994; Fri, 10 Mar 2023 17:14:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678497284; cv=none; d=google.com; s=arc-20160816; b=Dq+vD8po1g2MnhvVQo8A4uRFa7+BCZN1J0rz4++aAjR8FXpbTigg7uKPssYF+6L13Q 8ZjLPpSm62I/BhpNv8+tDjNJgPwyz/6oIuea7VyVWrAE/0bJ8I9oPRFoQ9MCkL6/5Fnx mt6UVlInEm2o0hgzyUmLbPT6yiX3f86tvpP8lYH+gqAnsO1ChNEG4qwdJSZ4RfxAxXlo SRXBDJTjNRdrQ0YV45TU7OCTPXOZcxQbbEULXgtwc9Fy9fmbz5oEg6OwiT3cVk6tnhgW wrZZLBLWZhtwm5fB7Myqeg1to7RZ+pS44p1fflCTxngjqOBIuiG/4vySPHVawyNUBOpK NfvQ== 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:reply-to:dkim-signature; bh=IhTBia6bsmV3yZc9hMtuCJ7+iIO3gStCyZPlx0VMvQQ=; b=AsVwxHbj4m96N13ddT4fMOeSDPzMHxSghhKShm4Hf/yqVUyRpKXj0quZ1YrsDUom9p 3dWE0JKbA8OE8CZom/Lb9lTZzZ4Uz7bXwlWmb/fxV2V9rUEFMl5Set5YN+8foAlP3HQe T9OzYFogAjcMqbVHskZ8afEcjUrs72nhvCP27FoSMsSirVX9milLqNtGR0xZ2ITDcsDN PfcKf3NOaabEfWrkJf9RWR/L0/ZbdisBALRGeQSwSKBgOdEeTq7KbJXEm/X1BaH91x7X G3QiUkQ4fRdiKcIphkhcvS1lhuccGpnHT0WWr5shiSPdxKN+LbKZIcQAflZpemAmMr3E fMjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="Gvu/bH0w"; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o4-20020a170902bcc400b0019e8ffc177asi1175802pls.367.2023.03.10.17.14.29; Fri, 10 Mar 2023 17:14:44 -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=@google.com header.s=20210112 header.b="Gvu/bH0w"; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231360AbjCKAtF (ORCPT + 99 others); Fri, 10 Mar 2023 19:49:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230415AbjCKAsH (ORCPT ); Fri, 10 Mar 2023 19:48:07 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF79913EB92 for ; Fri, 10 Mar 2023 16:47:16 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id a9-20020a170902b58900b0019e2eafafddso3625078pls.7 for ; Fri, 10 Mar 2023 16:47:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678495611; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=IhTBia6bsmV3yZc9hMtuCJ7+iIO3gStCyZPlx0VMvQQ=; b=Gvu/bH0wLcYdNxbtY2pIVUnwdv5diml4fm36b2o7zImi6GqKbDgWBNLkjS6TOYB0rW Kd6UijLo1FAp0KYUbGLQn3/3nXbBu5K1L+Uh75ozfpJCSu3tw3PS4q7BHxcpOQ3pEj/7 1ju2iac7nsa5DhCdFGjlIFAo/Hndm/HTw/1VN667M51a2tGSZYktQp87TnLCzy3YnUcZ JeqVy7LmSj/zOM79hXzJjqsIjoIoHg7F7ltFynS7AA0/mD+HSLBGQk/D+34VWb42TNJr aMR8mhVhyUsUK32gscNSeEGxG6faDkskat7GMJ9ZbqtQuz9tDnJdByQ3u0wQ+BMfRMSB zkGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678495611; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IhTBia6bsmV3yZc9hMtuCJ7+iIO3gStCyZPlx0VMvQQ=; b=662JgemML6FWflwpxMMKw7ZAEsKDVcInxQS7tBjlim1Lum/oKTxRiKTIhBFvvJUysW WgFLsDPhLqExxMh+Oms+xYL3txtoVIYKf/HHmJHPnBuls+T0cgaTRHc5ZZPccipUVm9H QnXgRCJmwxMurFBq3x2qe+AElriClW5K+qxPlxFiOFchEChoksJG5aVz6tZWXc29bJtL mjMeP8z5u5Cfw4RUMd2NAPPxbWbDqk+7ZQ1kwAkXz9yBHw5lAqacm6bS07rXjG4Da9FF 58wWB/N2YBMPI6nXXzn0SSy7HIl7mjOw5n4tXihr9wNLbdXlampdpVSGCTpTmsw2mNrt CHlA== X-Gm-Message-State: AO0yUKUMS+97t+WaFD3tXV50MM11g+rab0n9zO/5mDo52TFoKHDUiSlG xBGDTuA5QguSA4FDTVQegj9Ax5V4T7w= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:f705:0:b0:503:7cc9:3f8d with SMTP id x5-20020a63f705000000b005037cc93f8dmr9669146pgh.9.1678495611683; Fri, 10 Mar 2023 16:46:51 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Mar 2023 16:46:13 -0800 In-Reply-To: <20230311004618.920745-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311004618.920745-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311004618.920745-17-seanjc@google.com> Subject: [PATCH v3 16/21] KVM: selftests: Expand negative testing of guest writes to PERF_CAPABILITIES From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu , Yu Zhang X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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 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?1760031969177731748?= X-GMAIL-MSGID: =?utf-8?q?1760031969177731748?= Test that the guest can't write 0 to PERF_CAPABILITIES, can't write the current value, and can't toggle _any_ bits. There is no reason to special case the LBR format. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 61 ++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c index d91bf44a2e39..44fc6101a547 100644 --- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c @@ -54,9 +54,59 @@ static const union perf_capabilities format_caps = { .pebs_format = -1, }; -static void guest_code(void) +static void guest_code(uint64_t current_val) { - wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT); + uint8_t vector; + int i; + + vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, current_val); + GUEST_ASSERT_2(vector == GP_VECTOR, current_val, vector); + + vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, 0); + GUEST_ASSERT_2(vector == GP_VECTOR, 0, vector); + + for (i = 0; i < 64; i++) { + vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, + current_val ^ BIT_ULL(i)); + GUEST_ASSERT_2(vector == GP_VECTOR, + current_val ^ BIT_ULL(i), vector); + } + + GUEST_DONE(); +} + +/* + * Verify that guest WRMSRs to PERF_CAPABILITIES #GP regardless of the value + * written, that the guest always sees the userspace controlled value, and that + * PERF_CAPABILITIES is immutable after KVM_RUN. + */ +static void test_guest_wrmsr_perf_capabilities(union perf_capabilities host_cap) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, guest_code); + struct ucall uc; + + vm_init_descriptor_tables(vm); + vcpu_init_descriptor_tables(vcpu); + + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities); + + vcpu_args_set(vcpu, 1, host_cap.capabilities); + vcpu_run(vcpu); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT_2(uc, "val = 0x%lx, vector = %lu"); + break; + case UCALL_DONE: + break; + default: + TEST_FAIL("Unexpected ucall: %lu", uc.cmd); + } + + ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabilities); + + kvm_vm_free(vm); } /* @@ -79,7 +129,7 @@ static void test_fungible_perf_capabilities(union perf_capabilities host_cap) const uint64_t fungible_caps = host_cap.capabilities & ~immutable_caps.capabilities; struct kvm_vcpu *vcpu; - struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, guest_code); + struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL); int bit; for_each_set_bit(bit, &fungible_caps, 64) { @@ -89,10 +139,6 @@ static void test_fungible_perf_capabilities(union perf_capabilities host_cap) } vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities); - /* check whatever we write with KVM_SET_MSR is _not_ modified */ - vcpu_run(vcpu); - ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabilities); - kvm_vm_free(vm); } @@ -153,6 +199,7 @@ int main(int argc, char *argv[]) test_basic_perf_capabilities(host_cap); test_fungible_perf_capabilities(host_cap); test_immutable_perf_capabilities(host_cap); + test_guest_wrmsr_perf_capabilities(host_cap); printf("Completed perf capability tests.\n"); }