Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A105C43334 for ; Tue, 14 Jun 2022 20:48:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344020AbiFNUr6 (ORCPT ); Tue, 14 Jun 2022 16:47:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240980AbiFNUrs (ORCPT ); Tue, 14 Jun 2022 16:47:48 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 476491F2F0 for ; Tue, 14 Jun 2022 13:47:47 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id p9-20020a17090a0e4900b001eaa92b5b95so11546pja.4 for ; Tue, 14 Jun 2022 13:47: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=XW7Auazi+rtg3tCq25tBoxDOd9/9r9yYTc2MrH9AoHM=; b=bXvLxBX2KPymYQ40OMfTjXomGxbuh8lHVnDQPbxkSwAl3S59t2Wl1GDpothuPKQtyd eme9UwcDH7MJFgoGtVLF/E0PqoLwqdDyrtwQTjIFcKUfRSFu94tYSGURDo4vsWSEMYts RWhh7Rl9y3nmr4rzF2DQVWhU72luEfNdetI/EDU1A2/rLyIUcGNCV4Ay/esg5zF69Gw9 mMcHYHB2+7bzH1n6d2i96MN1K7x3G/yzLjqZhF4A0681hKxwszk4mxa18vWdy0q/focX +V6BcTNnesQ9Tya1WmuaJYnXBuK/g3rwrCpBBQ1aGU8AFLhODPzPyqYZR+yDr/kQuaWN N1yQ== 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=XW7Auazi+rtg3tCq25tBoxDOd9/9r9yYTc2MrH9AoHM=; b=wLg9mADlW7VbbsOjGQmQdIQHBvmFQPqYwVQvJROBhaptz6cTTO8p0lMWON/zFAFBLW 7s0oSVgBwqcK64xe/FUADORR5wfhkuCKdSUOxbXu+IXD1XKODXvqrMC8Uqg0LPFRPsE9 98qWn8awih2GDVT5S/j4fNARPmtB09V7zc8NeGc7mZEGsLldhWFNGYmCI2hy8g9wBtv1 YdzbuK69JeuoN7TGYkc6GmlI3wB1lkcdwVizG1KDN9BjaVMsgTJLsb/uf8RcOXASU8FL Gl4o2DDTUAFtnT8nTLjTMs6LqaVtZoruq1UQ7rJJ0Y8NN53LeLiv/ROIekHsQJZnAwLG j5vA== X-Gm-Message-State: AOAM532M8qQxpkM00iIh5JULlB82mLXvRTNcRN5eCd0t5SSnqWlkHP5d Bd6u8GLN+fDUto44AIxY1qzMnmiDIKg= X-Google-Smtp-Source: ABdhPJzwe6tqyJNiixz/BgrttsvdBlhkPOKbT8YqQt1OF34QAa4Drg67jpXCCwJLCyZTGn+T/CGMYaCHVOk= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1344:b0:51b:e3b5:54e2 with SMTP id k4-20020a056a00134400b0051be3b554e2mr6406748pfu.6.1655239666700; Tue, 14 Jun 2022 13:47:46 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 14 Jun 2022 20:47:15 +0000 In-Reply-To: <20220614204730.3359543-1-seanjc@google.com> Message-Id: <20220614204730.3359543-7-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 06/21] KVM: x86: Treat #DBs from the emulator as fault-like (code and DR7.GD=1) 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" Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a dedicated "exception type" for #DBs, as #DBs can be fault-like or trap-like depending the sub-type of #DB, and effectively defer the decision of what to do with the #DB to the caller. For the emulator's two calls to exception_type(), treat the #DB as fault-like, as the emulator handles only code breakpoint and general detect #DBs, both of which are fault-like. For event injection, which uses exception_type() to determine whether to set EFLAGS.RF=1 on the stack, keep the current behavior of not setting RF=1 for #DBs. Intel and AMD explicitly state RF isn't set on code #DBs, so exempting by failing the "== EXCPT_FAULT" check is correct. The only other fault-like #DB is General Detect, and despite Intel and AMD both strongly implying (through omission) that General Detect #DBs should set RF=1, hardware (multiple generations of both Intel and AMD), in fact does not. Through insider knowledge, extreme foresight, sheer dumb luck, or some combination thereof, KVM correctly handled RF for General Detect #DBs. Fixes: 38827dbd3fb8 ("KVM: x86: Do not update EFLAGS on faulting emulation") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c5db31b4bd6f..7c3ce601bdcc 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -529,6 +529,7 @@ static int exception_class(int vector) #define EXCPT_TRAP 1 #define EXCPT_ABORT 2 #define EXCPT_INTERRUPT 3 +#define EXCPT_DB 4 static int exception_type(int vector) { @@ -539,8 +540,14 @@ static int exception_type(int vector) mask = 1 << vector; - /* #DB is trap, as instruction watchpoints are handled elsewhere */ - if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR))) + /* + * #DBs can be trap-like or fault-like, the caller must check other CPU + * state, e.g. DR6, to determine whether a #DB is a trap or fault. + */ + if (mask & (1 << DB_VECTOR)) + return EXCPT_DB; + + if (mask & ((1 << BP_VECTOR) | (1 << OF_VECTOR))) return EXCPT_TRAP; if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR))) @@ -8632,6 +8639,12 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, unsigned long rflags = static_call(kvm_x86_get_rflags)(vcpu); toggle_interruptibility(vcpu, ctxt->interruptibility); vcpu->arch.emulate_regs_need_sync_to_vcpu = false; + + /* + * Note, EXCPT_DB is assumed to be fault-like as the emulator + * only supports code breakpoints and general detect #DB, both + * of which are fault-like. + */ if (!ctxt->have_exception || exception_type(ctxt->exception.vector) == EXCPT_TRAP) { kvm_pmu_trigger_event(vcpu, PERF_COUNT_HW_INSTRUCTIONS); @@ -9546,6 +9559,16 @@ static int inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit) /* try to inject new event if pending */ if (vcpu->arch.exception.pending) { + /* + * Fault-class exceptions, except #DBs, set RF=1 in the RFLAGS + * value pushed on the stack. Trap-like exception and all #DBs + * leave RF as-is (KVM follows Intel's behavior in this regard; + * AMD states that code breakpoint #DBs excplitly clear RF=0). + * + * Note, most versions of Intel's SDM and AMD's APM incorrectly + * describe the behavior of General Detect #DBs, which are + * fault-like. They do _not_ set RF, a la code breakpoints. + */ if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT) __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) | X86_EFLAGS_RF); -- 2.36.1.476.g0c4daa206d-goog