Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1802198iog; Tue, 14 Jun 2022 13:49:21 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vI6v1tYjyGukkUsm//zs4iOCLSrKSDNIR4QcI28cL52rETAqF3Slwi92iOFVz23B26K93h X-Received: by 2002:a17:902:ca03:b0:168:ed8e:fce7 with SMTP id w3-20020a170902ca0300b00168ed8efce7mr4781707pld.155.1655239760960; Tue, 14 Jun 2022 13:49:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655239760; cv=none; d=google.com; s=arc-20160816; b=GaPyJtc3y9OC1Z4FKFVjvVNDLOwit2FmzmG1eriXppzf6civpPDkR6cWlUJ9mAjYwC Sk3zBqUfBVF8DImLChNc/v66Lw0qWsuNa9DF3Lv8e8uvprhY31QMN4rCw8kt5x1IuDjJ 5SSS2PTaWVgLdQ8OHpg/IltzzXsOmSqPCNPQ5zrVBzz6nazLYLnFrAiWbJABNiFafzTn 5bGzQMf4ydoiM1+Fpl+2uz1UqK7oDEY7OYx4kSBhLL3X/DzSmNyyZfOtAqH4YoisOw1p 4n2sugBfXvY4nqb3VJMLYkvGqE3FBkFfnLSVCs7kDHia4Y2vldIHFwXDMv7cuzK/G7iN oyRQ== 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:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=S6MVCft9LaHiNhWyF7FD9LCOGxdFEPDWsG4/kVl+FmI=; b=pvhCyB605CqnM1ugfx3bEQVTQ5maIhD6hO6pvr5ACb/kXY3mF+iRm0b7lEU32k/1Az kAHVzt/DGllmP7uKvWVN01X06KchrsYQ8HebAeKF5eP61cs5dpuXZZ3nyPjywlNLvMnI xxTtWXRAngEzDyVwDKpTmslJOHfmXLWfW2U4nNmw+zJPtED6GZNmQs8LxVSWQRQ//o8R ZpnIneNUf8q4YGNz+8lQ9r2ywMlTN0WIuJK8d2GaadjIi7oiNbc/eYcVUqR3rcSS8l7T C1ndliN+kay5ZgWh9eajiqL0I3kfRtLmZ4bwOjnAUUNhI9AvSAcZtUDzzsqAGLtqt7yT jMcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=TgQLUbxB; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e7-20020a170902ef4700b0015ea33c252esi13561600plx.234.2022.06.14.13.48.46; Tue, 14 Jun 2022 13:49:20 -0700 (PDT) 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=TgQLUbxB; 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 S1344730AbiFNUr4 (ORCPT + 99 others); Tue, 14 Jun 2022 16:47:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235649AbiFNUrn (ORCPT ); Tue, 14 Jun 2022 16:47:43 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C92431D322 for ; Tue, 14 Jun 2022 13:47:41 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id r10-20020a632b0a000000b003fcb4af0273so5503414pgr.1 for ; Tue, 14 Jun 2022 13:47:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=S6MVCft9LaHiNhWyF7FD9LCOGxdFEPDWsG4/kVl+FmI=; b=TgQLUbxB7n/ualcy2OzKA3Sa77C9pngWE2rlTiSyuzTdOsHwnty1/vgyvf3DV7YlVY kab7xWrs9PBf5+e9/qAMHIr4auL77u7ojkiUt5G82LV/fZxn0Fwc62jb0xTyg1RKtnUq b750KanEC+sRBPDpRSbiE5rTYbpof3xPOq66zx14nxOw1xOPGOkPtNjjnTi/9FKecIfs Zxwyp3qwtiDBM2UgvleviuXl+cfNAmh0QwyfGMN4faQoOvZIykOHL1IaEThJh2o9j/9u cwvrduyq1j0Wxt2sbjXNnP3qmILsXznpKPoVxHnrCX750+lLuRZd2NVci2wyKyJY4A1K Rzsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=S6MVCft9LaHiNhWyF7FD9LCOGxdFEPDWsG4/kVl+FmI=; b=eCXMO36UNYvxYxWdP0Eeuax7QMnlX5zTEthpUVMamuEAeZlPSlaZw1FRyiCiuoPfyU sJb3dSuRxC8q2Rcx1iAh/X7NVMIqrzPc7YCJaiakMV0VUVbDwnxLzsvkvscrTLv6RLCx 3PWuHyl5eD5DlS7+TYv7cnibeFtlWQ8fTVKNb3e9Fx6viHm/y86knt5WHkFQCsaOxoQS 6NUCsnUAfcguL/zZNusyA9QejCtSqr/R7b8WCtmGYyqX/h83ypxw24Qnhpf8UQkk99pb x1cGmArqs8jgm4urkiz++6+Uo5ik6IHw8ol8RBBkmwzJI+b2DOepC0OrkdbQEwEPG7WI pkrQ== X-Gm-Message-State: AJIora+21UzUgfH26Tcp+ECQIJn1WVfJnDrfDv442NoCvEhWMa3Q+2tm 4YQC76GPAFshF0Ih1SaP8GE8R4BB6wM= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a17:90a:cb8c:b0:1e6:715f:ed28 with SMTP id a12-20020a17090acb8c00b001e6715fed28mr6401949pju.69.1655239661300; Tue, 14 Jun 2022 13:47:41 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 14 Jun 2022 20:47:12 +0000 In-Reply-To: <20220614204730.3359543-1-seanjc@google.com> Message-Id: <20220614204730.3359543-4-seanjc@google.com> Mime-Version: 1.0 References: <20220614204730.3359543-1-seanjc@google.com> X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog Subject: [PATCH v2 03/21] KVM: x86: Don't check for code breakpoints when emulating on exception From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Oliver Upton , Peter Shier 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_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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 Don't check for code breakpoints during instruction emulation if the emulation was triggered by exception interception. Code breakpoints are the highest priority fault-like exception, and KVM only emulates on exceptions that are fault-like. Thus, if hardware signaled a different exception, then the vCPU is already passed the stage of checking for hardware breakpoints. This is likely a glorified nop in terms of functionality, and is more for clarification and is technically an optimization. Intel's SDM explicitly states vmcs.GUEST_RFLAGS.RF on exception interception is the same as the value that would have been saved on the stack had the exception not been intercepted, i.e. will be '1' due to all fault-like exceptions setting RF to '1'. AMD says "guest state saved ... is the processor state as of the moment the intercept triggers", but that begs the question, "when does the intercept trigger?". Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2318a99139fa..c5db31b4bd6f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8364,8 +8364,24 @@ int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction); -static bool kvm_vcpu_check_code_breakpoint(struct kvm_vcpu *vcpu, int *r) +static bool kvm_vcpu_check_code_breakpoint(struct kvm_vcpu *vcpu, + int emulation_type, int *r) { + WARN_ON_ONCE(emulation_type & EMULTYPE_NO_DECODE); + + /* + * Do not check for code breakpoints if hardware has already done the + * checks, as inferred from the emulation type. On NO_DECODE and SKIP, + * the instruction has passed all exception checks, and all intercepted + * exceptions that trigger emulation have lower priority than code + * breakpoints, i.e. the fact that the intercepted exception occurred + * means any code breakpoints have already been serviced. + */ + if (emulation_type & (EMULTYPE_NO_DECODE | EMULTYPE_SKIP | + EMULTYPE_TRAP_UD | EMULTYPE_TRAP_UD_FORCED | + EMULTYPE_VMWARE_GP | EMULTYPE_PF)) + return false; + if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { struct kvm_run *kvm_run = vcpu->run; @@ -8487,8 +8503,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, * are fault-like and are higher priority than any faults on * the code fetch itself. */ - if (!(emulation_type & EMULTYPE_SKIP) && - kvm_vcpu_check_code_breakpoint(vcpu, &r)) + if (kvm_vcpu_check_code_breakpoint(vcpu, emulation_type, &r)) return r; r = x86_decode_emulated_instruction(vcpu, emulation_type, -- 2.36.1.476.g0c4daa206d-goog