Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp639042rdd; Tue, 9 Jan 2024 15:19:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEMGuBiq+DPQGL4QAcovgIik28gfT7MjTdVKGHxUDdWfhgf0teVnLtyFYndNsM8prVkWwep X-Received: by 2002:a17:902:c94f:b0:1d3:5b35:543a with SMTP id i15-20020a170902c94f00b001d35b35543amr156054pla.109.1704842368665; Tue, 09 Jan 2024 15:19:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704842368; cv=none; d=google.com; s=arc-20160816; b=YF5poq8M6TpvdFNUV02KPPImCRoeLKceKda57DH40+chvMcWoEMMoZZZR7cMp6/JwP m75NAdjFi7hPylreUQMclzjXj/RHXNUkqPhkQ6iELV03jQtOahA/TMuYQbVFIHNIB/QT vQSOyL1iaKYojJwum3us3qI4ECmNjIAfXZsmVOyGyrmf8XxmAZE1+2lAH1NkG3w0Uibm bTAprqbE5CSi+XxfWLIbaNfHDxFdoCsJaafoAXzK1oe7QntFm2nmYbXIBu1jaV1KBjyH HIaClTk3prulgRbCUQOQPM+exiEF0Qm4WND0B4FpsEHkMQCKJqY7eXu6khC7SbGy0F0k zZpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=0dnLmZ28wujiU5GE+YLfWBhmje+8bXhZoFPGUCSCmRo=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=Jk8cpOFKiEXL7XAdKerCwEaF0lt/2iOjkLqmIh5kMbEHpu4U2g0PiCB1h7clFHpI4o QysHCEefUsf/SA7WjJ7zdbK1Yj3yEVoLQoA2/stTDR9+m1eFVBwHI9qRHJG/m+A1eqz2 4Yk+BsuJ9IvFRjFNg1SnGsMF5AfEi2rtXrb8GfHKpqckl3nysqVMnX8ddeAI8tWvZ8pM 5mcNcrYlhrY2zdBzO+prKtokES+iOuhdHBqB7hRuvyXZCaooe4BxzD6FjceGUU2G0RRV TLs5z60Jj1GFMILL/Ywz6MeF99qSISI+by2GavNsAJkTcnVBFPvZBt3SVI3JkFE9vO18 4+Qw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=H3+sxRIK; spf=pass (google.com: domain of linux-kernel+bounces-21513-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21513-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id j4-20020a170902758400b001cf5d537eb6si2325621pll.190.2024.01.09.15.19.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 15:19:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21513-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=H3+sxRIK; spf=pass (google.com: domain of linux-kernel+bounces-21513-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21513-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 743A1B27458 for ; Tue, 9 Jan 2024 23:12:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D51B34C3C6; Tue, 9 Jan 2024 23:03:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="H3+sxRIK" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 A2E044BA92 for ; Tue, 9 Jan 2024 23:03:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dbed6083a91so4434141276.3 for ; Tue, 09 Jan 2024 15:03:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704841421; x=1705446221; 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=0dnLmZ28wujiU5GE+YLfWBhmje+8bXhZoFPGUCSCmRo=; b=H3+sxRIKHI1ey7MUf/Ss+NDH52XKvngO4fKh9F5ZOZ8X77VkbhDIbOBLIe5sE4nJIv p88u938gxdyupydhMk68JLJeYeGHiWuXL2xEoYzzMaPdySnAWqZIa0ohmAjZbDADwm4Q dvck0DftwuD34/OYib/UWI+vORmJkICDLt3fdCzZQ+iknPNABmj4Tm4RjPx3u3IVKBM7 Xl4IzGPupcy5eFx+7LrTRkR+3ZIXvKAqx2Nh/QZ2irB1hShXAPyOLT/ineVQ/kRPb34S 2rt3WGAWNNMZlQHrO5Lt8Vmb3hQn2VJZIP0lc1w8V7sJeOuLBTeg5NRmBRNsSlJmJ4iP ADWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704841421; x=1705446221; 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=0dnLmZ28wujiU5GE+YLfWBhmje+8bXhZoFPGUCSCmRo=; b=XOCoc3qkgUe8D3VjYIbN12ZEne6KvV0AJO+1t1GE0fmdDSkcISY1cR+8BgNrZ9pD6R ILRFK2m9vJk617vKjxsRRZM5yyozxp+hX8NJm/B8yms3Fsyz/isjM35J8R83Tkz9f05T U5qEoDj76VFaq0CEVJgAH6qZvegTeGlwZHXeVK5iNnXAHSlMbKPiJLAhqbTTBwZwULoU oJ9Wp0mtyu+1416Xa3FuKE8TiBjW6IW8NIU62rNg8kUC76G0PJOsjDl23rIs7dH66Yl9 nrNcdFLZoTobsXKMnuXpVNb2vAEjN6mWX7eYiyiIPzpzA/aT0HXZ3ETr9S9l7h/0tKBO HP2A== X-Gm-Message-State: AOJu0YyBIK4qigeIp6uydcWlDWPY1kzfnR8uwYJC0RIJrXbWxSwxI3C1 5tev8aXHpDRwjpLgt0N69VggDbHw6kvtfwiTHA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1348:b0:dbc:c697:63bd with SMTP id g8-20020a056902134800b00dbcc69763bdmr43795ybu.0.1704841420785; Tue, 09 Jan 2024 15:03:40 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 15:02:44 -0800 In-Reply-To: <20240109230250.424295-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240109230250.424295-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20240109230250.424295-25-seanjc@google.com> Subject: [PATCH v10 24/29] 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" 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.472.g3155946c3a-goog