Received: by 2002:ac2:5a04:0:0:0:0:0 with SMTP id q4csp378766lfn; Wed, 2 Mar 2022 09:12:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJxSz0N1mP8X9iGLnTGRHoit3hacXt5tBQiqHqrCcOtHl/7ljWM5S4lkXYzB9zMibmleTdAW X-Received: by 2002:a05:6a00:1705:b0:4e1:5a1:c626 with SMTP id h5-20020a056a00170500b004e105a1c626mr34159227pfc.1.1646241127971; Wed, 02 Mar 2022 09:12:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646241127; cv=none; d=google.com; s=arc-20160816; b=gy4xZLY6fS9EPkXEuxww+jvYNtq75vhy1BcjUoCINowdi3WJ/kt6bc1Q/WSHvqMEwj UdFxrJHDGoSnvS0zkteSeRkP7Ks/Wjk80vXQxy7PFM5O0ey6e0UkPc5q0B4wxCnAfJeD 1w2+LrSD9545IJJW/6AehTDXEhDhWjRrYrTq1QL3siHHrvBsVAkYlVvtNRJOeouOQ19J Bkxj79bz3rr6BTXoxUVW8aotOOfHW/wkaUsxZir8N+I0PBA9DcLn4/kGg3Eg8lBRoK4E lwjR4dWH4j+neQto1Vqi3g9t5ITCYe/RN9oEYeKq2MzNGy5Q1O6G4atTa4QZ+kB9Z0cp wLog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=YTSE18DERbl9qM7GONTPjmit96VbYnQ2NwGZg9RYFgo=; b=tgadkvlAyhfgVdBdnyvpX2giB4mdye5N0FvejstgeCH+WRm/IMiel90cxEfXjBjpvx OYu6QzeRhLRXXupWWSN8BSPvRkK8YxotUCe+5TntOSD7AdXbRGW6U7KiXvVhv9NEpYHT Xa2lMrQiQKN7Oohl0ukAXBk0GDMj02RfbZoJDwwbt/uPnwmwuJOV0KbtHk/0/BBhO3LF t6UYybhT3gWRNO4AbjU9NES/82AvNsaW5aBQRKMG8lzbOKdaH17okG6NA69zThd3aAeu cZIlpWZkoR00mukFzuaxfyk1vUanAqgaDGrBDXebw+SkAM6uf2ck0QGWAO3RX39NxMYd 0lrA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q14-20020a17090311ce00b001517d6bd6f8si6570607plh.533.2022.03.02.09.11.48; Wed, 02 Mar 2022 09:12:07 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241280AbiCBNQE (ORCPT + 99 others); Wed, 2 Mar 2022 08:16:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232516AbiCBNQD (ORCPT ); Wed, 2 Mar 2022 08:16:03 -0500 Received: from out0-153.mail.aliyun.com (out0-153.mail.aliyun.com [140.205.0.153]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26A9BC3C0D; Wed, 2 Mar 2022 05:15:18 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047212;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---.MyREdp1_1646226915; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.MyREdp1_1646226915) by smtp.aliyun-inc.com(127.0.0.1); Wed, 02 Mar 2022 21:15:15 +0800 From: "Hou Wenlong" To: kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: [PATCH] KVM: x86/emulator: Emulate RDPID only if it is enabled in guest Date: Wed, 02 Mar 2022 21:15:14 +0800 Message-Id: <1dfd46ae5b76d3ed87bde3154d51c64ea64c99c1.1646226788.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham 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 When RDTSCP is supported but RDPID is not supported in host, RDPID emulation is available. However, __kvm_get_msr() would only fail when RDTSCP/RDPID both are disabled in guest, so the emulator wouldn't inject a #UD when RDPID is disabled but RDTSCP is enabled in guest. Fixes: fb6d4d340e05 ("KVM: x86: emulate RDPID") Signed-off-by: Hou Wenlong --- arch/x86/kvm/emulate.c | 4 +++- arch/x86/kvm/kvm_emulate.h | 1 + arch/x86/kvm/x86.c | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index b7990defca9c..817d28985645 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -3500,8 +3500,10 @@ static int em_rdpid(struct x86_emulate_ctxt *ctxt) { u64 tsc_aux = 0; - if (ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux)) + if (!ctxt->ops->guest_has_rdpid(ctxt)) return emulate_ud(ctxt); + + ctxt->ops->get_msr(ctxt, MSR_TSC_AUX, &tsc_aux); ctxt->dst.val = tsc_aux; return X86EMUL_CONTINUE; } diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index 39eded2426ff..a2a7654d8ace 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -226,6 +226,7 @@ struct x86_emulate_ops { bool (*guest_has_long_mode)(struct x86_emulate_ctxt *ctxt); bool (*guest_has_movbe)(struct x86_emulate_ctxt *ctxt); bool (*guest_has_fxsr)(struct x86_emulate_ctxt *ctxt); + bool (*guest_has_rdpid)(struct x86_emulate_ctxt *ctxt); void (*set_nmi_mask)(struct x86_emulate_ctxt *ctxt, bool masked); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c712c33c1521..6150d38de593 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7703,6 +7703,11 @@ static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt) return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR); } +static bool emulator_guest_has_rdpid(struct x86_emulate_ctxt *ctxt) +{ + return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_RDPID); +} + static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg) { return kvm_register_read_raw(emul_to_vcpu(ctxt), reg); @@ -7785,6 +7790,7 @@ static const struct x86_emulate_ops emulate_ops = { .guest_has_long_mode = emulator_guest_has_long_mode, .guest_has_movbe = emulator_guest_has_movbe, .guest_has_fxsr = emulator_guest_has_fxsr, + .guest_has_rdpid = emulator_guest_has_rdpid, .set_nmi_mask = emulator_set_nmi_mask, .get_hflags = emulator_get_hflags, .exiting_smm = emulator_exiting_smm, -- 2.31.1