Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp1113811imi; Fri, 22 Jul 2022 17:53:53 -0700 (PDT) X-Google-Smtp-Source: AGRyM1skY9SSINbuViwVewA61o2Yr3A3IDCMdro1x25kyxNuqu9vUOd+ZaJtlKO0ED9fttBneEeF X-Received: by 2002:a17:906:8475:b0:72b:12c7:effe with SMTP id hx21-20020a170906847500b0072b12c7effemr1848422ejc.337.1658537633010; Fri, 22 Jul 2022 17:53:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658537633; cv=none; d=google.com; s=arc-20160816; b=LuuvlUCJPdEZo5txtpNz9P5DnKogU032iI+en3+i04gf6NF4oWTnF/btW4H+h5HsyB mTEhU+Aa+d/0KYOTTu3tEX7om3cFfo9UhFcLhBEXhvDFfb28w+KrBjWSVr2Il8LU7kj9 YjD7ZrvtdfT3IeuBkaN08wbMuV/27RzOzsYG5QYgPXIRyhnwicRldGHqpWhFpWKbQkJS RVernFO5MQV0WY3Wioklr0FLPlmcOapOze/jVNf+CZOMl1sRpVVEfB12pX/fKxapoGAO rZhY8jFcel52xdG9243L2y67xxq23DP///LT5CDeTbGAM9eYHxtQlPEP6KBe17Q/FK45 m6Rg== 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=cAX1yeYYe1qjAZf7Suz3oA25ZI3D0KwAESBSQk1/8Uc=; b=o0MBrIA332qtZaLF/M6uK9/Wl1OSgRvoZKVGo1BogCxK9Sg3FvfVxv6qULvrrn5u2t uEzF5YmI5d4AH/kExn9j+NhNhqDrCR/nIkImSrT9cSnzy/tA6RF09VBikIpAUWCcTTwr 0dEh9vcpfIoe5EJdlmuphwZH9396upmxEETHsUMIWWlT0JMq8h4m/1QBlgqO8SQaTgW2 jnPEpvsDNemEUJu4eKzOIWXrVnQ3rZOwa4U+oMVR3poPNmR9M71G29BvDhR3CkPys8yL Gei16v1OAgveLWpI861e+hDh+zaLNS+sdmHLQrBlgHLA93O35HHgqmFWrZcxEz/12xx7 m2aA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=cOXrACnv; 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 gs42-20020a1709072d2a00b0072a47b13d67si9158441ejc.582.2022.07.22.17.53.28; Fri, 22 Jul 2022 17:53:53 -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=cOXrACnv; 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 S235822AbiGWAv6 (ORCPT + 99 others); Fri, 22 Jul 2022 20:51:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236149AbiGWAvs (ORCPT ); Fri, 22 Jul 2022 20:51:48 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B086B13D7A for ; Fri, 22 Jul 2022 17:51:47 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id z9-20020a170903018900b0016c39b2c1ffso3438370plg.12 for ; Fri, 22 Jul 2022 17:51:47 -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=cAX1yeYYe1qjAZf7Suz3oA25ZI3D0KwAESBSQk1/8Uc=; b=cOXrACnvJt1gAw6Hnq8VaDtjh0r3jmfmqe7BQWGe611UOjBM9A5zup37jEDgwM4vaL o5ZYdhY9UkfhzxJyITivmKwh3lx2nGpIihLMSl+Y9bWRIHbBShT6+Iyn6QcBh3JHH/+E afpoSxPwU8NjoH7kZoGFKflgeyNYocJHtgn/tUsNeJjxNWAluq2gG4qoYpLkXvraqcbM +IM9nfaz34Esk7x+4InPy7IqxEHeGRtuSqVGK5LBCRjKI3H24y4SOYZ9C/CxVRQnfYnC V2bG75ssa7q8DpPYLPg0+hUuoU8qYhU+55GHSflgDDcGPRcCWBbn9l04dWS6+fd02ef2 2czg== 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=cAX1yeYYe1qjAZf7Suz3oA25ZI3D0KwAESBSQk1/8Uc=; b=REGcJCOia8wlYH+tyOmiXjJGWbP/6Ynq7+s6HuE3/24g1CcWCVH5dIjL8efT9Q1Wfv +a9cH1jvDFsWN2EvIvQOOA/6e9h7tbBR8oXT1+Ho85632lx8cib3FbfWIRaDPwERkWdr ljIZS2I1wnwyjF4YfgvEhr+8d46wLzmvgpF9wne2Rc9B+7G9iJqZDC4VXPHe7tPM/WSN EnbHFHcdrCSiW5k/TJFhhjWfsIUL77SXcduSumGWwZIIdT52LSUcouI3JKcvJl86fXGc NmyyNcDBcOPt4pAdIsA6/xT2gsQFhhwZblkanPUCtOokB1jcJLIxxBcH/rwea24i4AeY ZAJw== X-Gm-Message-State: AJIora9hJ/xpP8SA0iRYgBDxtLt5cEitB6XUnh7xHS9BmjunfwY1sd4j 9/bfrWDMq4yTZbbuVrqCCmelR6A4HhQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ec8b:b0:16c:20d4:eb3 with SMTP id x11-20020a170902ec8b00b0016c20d40eb3mr2384465plg.40.1658537507307; Fri, 22 Jul 2022 17:51:47 -0700 (PDT) Reply-To: Sean Christopherson Date: Sat, 23 Jul 2022 00:51:16 +0000 In-Reply-To: <20220723005137.1649592-1-seanjc@google.com> Message-Id: <20220723005137.1649592-4-seanjc@google.com> Mime-Version: 1.0 References: <20220723005137.1649592-1-seanjc@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v4 03/24] KVM: x86: Don't check for code breakpoints when emulating on exception From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jim Mattson , Maxim Levitsky , 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,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 Reviewed-by: Maxim Levitsky --- 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 5366f884e9a7..566f9512b4a3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8523,8 +8523,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; @@ -8646,8 +8662,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.37.1.359.gd136c6c3e2-goog