Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1405960rdb; Fri, 1 Dec 2023 16:06:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IGUcGp8yoE5t8k/bJ8cYwg61GmNaansstqoLEe85z/pmvbmwy48rxPLZIaviXMUkmmYOGFV X-Received: by 2002:a05:6808:f08:b0:3b8:b063:5d6c with SMTP id m8-20020a0568080f0800b003b8b0635d6cmr407822oiw.83.1701475618598; Fri, 01 Dec 2023 16:06:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701475618; cv=none; d=google.com; s=arc-20160816; b=wiJxc7ucqCC+cTw0UpLMwOLJUTiCLUhcIwOSHpRu1hHtI+YIjZoCXHVi0leLIERbTI Zt9M0NLctQ6Iztyj0DK10+Ub4GK+kzA/Wo/4L3+2tRyuR9md4paPDEH5J8TafrSvysqd 6SROiloXAsOSa/CZnDpq5hvJgt323uqfNu1NC/KuututJjw2MCNGln0T/4NZ6/Awj54S Md6wfn35y211P+gmNwtGvYo3F1FKEi1c0XARWoH1mCB4fr/1LhQaD8hcfHC6ygS5D6At E4sON5AK1CcZ31rFeN2kNrZ/erGIm0LM4mQf5gOYJb1U4Byd+FPvCqTMUudTJ9v6gtbS lDYA== 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=1np6+7PRvXQlpdzzc08QPCWEiLYS28PyG2M8UTLh7eY=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=JD+SthEoYaTGff6MjrLD3GilcNc93A2sOEg9k7xsm7tRmEvtMDxXO9ecBFl4Bv7AWD p9g2BWsc1tB65v3SDg4CuoKCAh51rQpxnJBiYv09cMyS6kjA/85tX49LMjL91HT5C7oB 8tMY3ExG+GkoOS9drebFrXB0tqd7F7v9wF/REvpWruJYQ6G+y2omsSGdXQ9s9R5gQYdY rUg7uI9i+zjAgjCWO5UZPL7Rph5SAFFmxuhPqNjWeU+2Wz+/sVs4Cm4IwYlFrLF44XYq fH1EexATK5TUbJ3FRBJCZghdZ1ioa508ot+BVtUF8hLAfIdszF7s/cI7JIPKo0jhcvaj v+gA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=A5Mro3EI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id k25-20020a63ba19000000b005c65fe4373fsi74601pgf.315.2023.12.01.16.06.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 16:06:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=A5Mro3EI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (Postfix) with ESMTP id 73AAE83037C7; Fri, 1 Dec 2023 16:06:50 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235322AbjLBAGV (ORCPT + 99 others); Fri, 1 Dec 2023 19:06:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442054AbjLBAF3 (ORCPT ); Fri, 1 Dec 2023 19:05:29 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD80E2109 for ; Fri, 1 Dec 2023 16:05:05 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-286472a0c6dso1918182a91.1 for ; Fri, 01 Dec 2023 16:05:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701475505; x=1702080305; darn=vger.kernel.org; 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=1np6+7PRvXQlpdzzc08QPCWEiLYS28PyG2M8UTLh7eY=; b=A5Mro3EI+QYZWxtxK2ilJuraL8g3qepKA9pjc1wlLtI3lDwKyCjU6yEz4wR3JN8TCT Sqi59oNPP2FySRo8UnV9dP/ZmlXLTRPy121LuVWNdvxat78rL4SOElvOcjStttsBI55Y PyG/yIANK4qQye350KSsyt+9fRlM++kKK8AZMYcnlHXvHtXVBwon5o6ilPpvDjtia6JR D8ZIIS/vKK8oKoJW9YNnH1gXLIszQG9WNOdjhn77e/Yp2Iongc06C75YNwGflKp9rs3d B8FTHW8DfkV9IMZyeSBWxHQCzzVl2ZIfagDHOLydS88gu+WxZRjov4Z98OSyK+nmLKMW 7jUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701475505; x=1702080305; 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=1np6+7PRvXQlpdzzc08QPCWEiLYS28PyG2M8UTLh7eY=; b=TuHLuHolj0PpROaQs8D6/rNrG/l3SjFKr4r6q4GgwskjTbJpkdM9El+/jINgRO3u0w RFUFVlrQjdKAGgq5wQdVKEr9F742T1qvBjcEhhAqcLXszvccZn7fjHQGA+4uhK8MdfRm tBR/RUtZrDSRwCxEjJENisprqjljIhQR/ri5uK+TvLTFa9036l+ckTOUohFLkoh1lK1h E60sgwWfBUsNDrUi59+aXfzTVeJyi18MpF1Moj5iyjKYFyt3WpkCQW6vkyqN0jyM1C3Y mzYunJlICxH7iWYh5h91IIpuKgQy76xSIz0nSJ/XtRJqwofJ1E8sY+RoBa1dJNWcTZmG BPqQ== X-Gm-Message-State: AOJu0YyP1+0K3zzsgCQaQRvdwQaJFL1GBAGoJFOxbBjHhvu2l/f+0Hj4 G65Rc2TIE+z4E9IRvINQM6nPrwhOpfc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:cb97:b0:286:5a99:f834 with SMTP id a23-20020a17090acb9700b002865a99f834mr120336pju.3.1701475505056; Fri, 01 Dec 2023 16:05:05 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 1 Dec 2023 16:04:12 -0800 In-Reply-To: <20231202000417.922113-1-seanjc@google.com> Mime-Version: 1.0 References: <20231202000417.922113-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231202000417.922113-24-seanjc@google.com> Subject: [PATCH v9 23/28] KVM: selftests: Query module param to detect FEP in MSR filtering test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Dapeng Mi , Jim Mattson , Jinrong Liang , Aaron Lewis , Like Xu 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,T_SCC_BODY_TEXT_LINE, 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 groat.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 (groat.vger.email [0.0.0.0]); Fri, 01 Dec 2023 16:06:50 -0800 (PST) Add a helper to detect KVM support for forced emulation by querying the module param, and use the helper to detect support for the MSR filtering test instead of throwing a noodle/NOP at KVM to see if it sticks. Cc: Aaron Lewis Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 5 ++++ .../kvm/x86_64/userspace_msr_exit_test.c | 27 +++++++------------ 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index ee082ae58f40..d211cea188be 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -1222,6 +1222,11 @@ static inline bool kvm_is_pmu_enabled(void) return get_kvm_param_bool("enable_pmu"); } +static inline bool kvm_is_forced_emulation_enabled(void) +{ + return !!get_kvm_param_integer("force_emulation_prefix"); +} + uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr, int *level); uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr); diff --git a/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c b/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c index 3533dc2fbfee..9e12dbc47a72 100644 --- a/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c +++ b/tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c @@ -14,8 +14,7 @@ /* Forced emulation prefix, used to invoke the emulator unconditionally. */ #define KVM_FEP "ud2; .byte 'k', 'v', 'm';" -#define KVM_FEP_LENGTH 5 -static int fep_available = 1; +static bool fep_available; #define MSR_NON_EXISTENT 0x474f4f00 @@ -260,13 +259,6 @@ static void guest_code_filter_allow(void) GUEST_ASSERT(data == 2); GUEST_ASSERT(guest_exception_count == 0); - /* - * Test to see if the instruction emulator is available (ie: the module - * parameter 'kvm.force_emulation_prefix=1' is set). This instruction - * will #UD if it isn't available. - */ - __asm__ __volatile__(KVM_FEP "nop"); - if (fep_available) { /* Let userspace know we aren't done. */ GUEST_SYNC(0); @@ -388,12 +380,6 @@ static void guest_fep_gp_handler(struct ex_regs *regs) &em_wrmsr_start, &em_wrmsr_end); } -static void guest_ud_handler(struct ex_regs *regs) -{ - fep_available = 0; - regs->rip += KVM_FEP_LENGTH; -} - static void check_for_guest_assert(struct kvm_vcpu *vcpu) { struct ucall uc; @@ -531,9 +517,11 @@ static void test_msr_filter_allow(void) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; + uint64_t cmd; int rc; vm = vm_create_with_one_vcpu(&vcpu, guest_code_filter_allow); + sync_global_to_guest(vm, fep_available); rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR); TEST_ASSERT(rc, "KVM_CAP_X86_USER_SPACE_MSR is available"); @@ -561,11 +549,11 @@ static void test_msr_filter_allow(void) run_guest_then_process_wrmsr(vcpu, MSR_NON_EXISTENT); run_guest_then_process_rdmsr(vcpu, MSR_NON_EXISTENT); - vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler); vcpu_run(vcpu); - vm_install_exception_handler(vm, UD_VECTOR, NULL); + cmd = process_ucall(vcpu); - if (process_ucall(vcpu) != UCALL_DONE) { + if (fep_available) { + TEST_ASSERT_EQ(cmd, UCALL_SYNC); vm_install_exception_handler(vm, GP_VECTOR, guest_fep_gp_handler); /* Process emulated rdmsr and wrmsr instructions. */ @@ -583,6 +571,7 @@ static void test_msr_filter_allow(void) /* Confirm the guest completed without issues. */ run_guest_then_process_ucall_done(vcpu); } else { + TEST_ASSERT_EQ(cmd, UCALL_DONE); printf("To run the instruction emulated tests set the module parameter 'kvm.force_emulation_prefix=1'\n"); } @@ -804,6 +793,8 @@ static void test_user_exit_msr_flags(void) int main(int argc, char *argv[]) { + fep_available = kvm_is_forced_emulation_enabled(); + test_msr_filter_allow(); test_msr_filter_deny(); -- 2.43.0.rc2.451.g8631bc7472-goog