Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp819328rdf; Fri, 3 Nov 2023 17:04:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEfhWSzeoPCnhGhZSIxmvk3NpT+4WC2eOHMquamjbOvTa41b+Icg1LSbxpgt0tgYHudKWBR X-Received: by 2002:a17:90a:4b07:b0:27f:f260:ceaf with SMTP id g7-20020a17090a4b0700b0027ff260ceafmr18926579pjh.10.1699056263965; Fri, 03 Nov 2023 17:04:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699056263; cv=none; d=google.com; s=arc-20160816; b=xkR3VDjY0AzBejwiSgTjXBV/DIF1rS4j2pLA11OE+dF1z2nHfndnPGzvQohJKRar6N uHCIgf0KPw4UXxXmc/4h5y+4vX01eVjOJDRVyvC8/tneFc8Hm5JDc75Of/bWbxF/j/wR ffNNzkMnmSq1PUncndHBDprY5GQhI4su5SUjuWMSJirwEcx/AyKPA1Sw8KFf3sXW12rZ NzFlhqxpSaouHUfuweLqwY38//EttdRcblMr6Q3oR17x9V8CTtCC5f3VsM75t2hwSR4L fZlcDvdX1JT3Q6gmAZ+FFSA3OuS3jTa8VFGkQsTisFvVWFN8jg80LyN3J3G8xuWRxc3F lO6w== 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=HFthXLKtlL9b7mBubOTPYiyOU6v8Gj2JZ4woVFum6XM=; fh=z/UQYx9FUD9Bz9JyYrmi2ijAScp29gktKr8lejSUuXU=; b=izOM1b+KS+My2iZTT21ckBbMUxoL96QlGV0DXoMy1mJ7UG4TsAP7kfx29JbnjfbrZL AgHoEffLWVhG1hSh+TdS+481GnUTnKMIsgyDWs1w7z6/LZRSYuDhyDJcJy5JD009fUcp 9TZJUilxbQSaVCpWPNR9DIb8PrP00hXu1pQa0nIsmrqdO0QPSWYiusoahh0i7ZYxSSEU T/0s6YlNQNIO9RnBbTW4wwC/IZDxpjCtKj13FgrmyW6oVg6aybRgaqs7KbA4h0Hp+LgZ XBXv+X1vd+HVwqkxMJGF8SzIBKT8ZPUL75leS6x+4o7C0c0rIN2SRs2b1RLbVVL4Jz3w wnfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=by8pITx+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id bx23-20020a17090af49700b002791e648ce3si2560646pjb.174.2023.11.03.17.04.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 17:04:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=by8pITx+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 0930E81D7720; Fri, 3 Nov 2023 17:04:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232530AbjKDAEO (ORCPT + 99 others); Fri, 3 Nov 2023 20:04:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231972AbjKDADj (ORCPT ); Fri, 3 Nov 2023 20:03:39 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E0D0171D for ; Fri, 3 Nov 2023 17:03:16 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da307fb7752so3211570276.0 for ; Fri, 03 Nov 2023 17:03:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699056195; x=1699660995; 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=HFthXLKtlL9b7mBubOTPYiyOU6v8Gj2JZ4woVFum6XM=; b=by8pITx+EMCBczP6kzbbixDrUhSQj30BM182/RyuTP30JojaQnB5of+wXTDoq2mM2d Hjl5axfvpPvFnbmimGYj+arya80VxKz0I4TznJV+EgKB6pT8me0eKDCwKedOtUefRgel 3hZWvPtoCkYCP+EAWFq7Cq7iuflkh3qTMHjm2vRomMXHJdbU0zgRurelQhRFnVwly7Os he+xobM8XxYyiDiRFwB5v/acWTPaH6iERdmX2aG5KJAmYFaCVN0xbCD92s8JQ5Z4F4P1 R2Tq3JrsouHuLWLn58miaWIeg5YyAbLc8F8kZRWx2L2QtW3ZL+rOAH1iGAqkIcUgw/2j wKzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699056195; x=1699660995; 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=HFthXLKtlL9b7mBubOTPYiyOU6v8Gj2JZ4woVFum6XM=; b=JGuwyP2tu2hdBYzLFNdTYbXLptjVeehiA9VXHT8592BYv4xyIo158A0doxJ76a7PK0 X1XaUXujHm1SbLhVxTXH4T6sev08ziG35oqdla/IlY/m1hicLTJ10W/HPIkNfxe+NEDF s8Vd7sUCvWcIduaTOwCPVTSqgYzEATQs0/bpvTF5LvYtVPqxL8bX9/jeuHBmTY8K4+sw PpWP4fwZ0IExgyjyQjCqDtzC8/pGMOedi8BDNWaffS5LwZVpwN7YqHeNOvdOBrGALz5Y o1nILfsylHzVvsIKhXg7NQLSxcGk5K17GwnfBocJXxbyGbqmMhXLbnXOGZWY3gUsmN/i B1NA== X-Gm-Message-State: AOJu0YydAfoKWuMgEzOFx5UeUkaCDU8HSl7O5uvHKEumOvkDTWRVxxO9 z58+KSF1xXIG7WJNoHhJEJOlWkqphQ4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:549:b0:da3:b96c:6c48 with SMTP id z9-20020a056902054900b00da3b96c6c48mr187684ybs.9.1699056195235; Fri, 03 Nov 2023 17:03:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 3 Nov 2023 17:02:36 -0700 In-Reply-To: <20231104000239.367005-1-seanjc@google.com> Mime-Version: 1.0 References: <20231104000239.367005-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231104000239.367005-19-seanjc@google.com> Subject: [PATCH v6 18/20] 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 , Jinrong Liang , Like Xu , Jim Mattson , Aaron Lewis Content-Type: text/plain; charset="UTF-8" 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_BLOCKED,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 lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Fri, 03 Nov 2023 17:04:23 -0700 (PDT) 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 1885e758eb4d..47612742968d 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -1219,6 +1219,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.42.0.869.gea05f2083d-goog