From nobody Fri Mar 14 08:06:51 2025
Delivered-To: importer@patchew.org
Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates
 192.237.175.120 as permitted sender) client-ip=192.237.175.120;
 envelope-from=xen-devel-bounces@lists.xenproject.org;
 helo=lists.xenproject.org;
Authentication-Results: mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of lists.xenproject.org designates
 192.237.175.120 as permitted sender)
  smtp.mailfrom=xen-devel-bounces@lists.xenproject.org;
	dmarc=pass(p=reject dis=none)  header.from=citrix.com
ARC-Seal: i=1; a=rsa-sha256; t=1694810242; cv=none;
	d=zohomail.com; s=zohoarc;
	b=h0O++CefFGSLE9g+8uD8Oop2J0fpaF4BdkleEuo7KbhXu+E1UIJBxGSkB1hETYMQa3m6mEUrl3WbP6dQ2sOMCjE8lWGzuTWht7emw3esiAG8i2Wy8N4S8duiMrhV86ySHohM32cpe4mbMkqtUE7uA3cdw5nZ/idgze0YfqbEqMg=
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com;
 s=zohoarc;
	t=1694810242;
 h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To;
	bh=tjWOoTHFXvp7tAuMVuZ2rN9aFbpCxtvhN4m+eRmOyRw=;
	b=mXYeRCMqTcyCcs3KKF7jsAmTVf8a7hpzD6c9d1V5IVeRRCIC2vWE8Tvw3do9/30MX/BMpBUIcpJ7YsCKq6Ic8aUJVip9Gdd5sUAnhqOOBl2Q2/wMds8rDRR50iKHG+PqR499z8gruFFL72dTK4t2Avo1VfnB6LnG5w/8juxplq0=
ARC-Authentication-Results: i=1; mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of lists.xenproject.org designates
 192.237.175.120 as permitted sender)
  smtp.mailfrom=xen-devel-bounces@lists.xenproject.org;
	dmarc=pass header.from=<andrew.cooper3@citrix.com> (p=reject dis=none)
Return-Path: <xen-devel-bounces@lists.xenproject.org>
Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120])
 by mx.zohomail.com
	with SMTPS id 1694810242515955.6503701388225;
 Fri, 15 Sep 2023 13:37:22 -0700 (PDT)
Received: from list by lists.xenproject.org with
 outflank-mailman.603365.940372 (Exim 4.92)
	(envelope-from <xen-devel-bounces@lists.xenproject.org>)
	id 1qhFYe-0002Px-1O; Fri, 15 Sep 2023 20:36:56 +0000
Received: by outflank-mailman (output) from mailman id 603365.940372;
 Fri, 15 Sep 2023 20:36:56 +0000
Received: from localhost ([127.0.0.1] helo=lists.xenproject.org)
	by lists.xenproject.org with esmtp (Exim 4.92)
	(envelope-from <xen-devel-bounces@lists.xenproject.org>)
	id 1qhFYd-0002Pj-UT; Fri, 15 Sep 2023 20:36:55 +0000
Received: by outflank-mailman (input) for mailman id 603365;
 Fri, 15 Sep 2023 20:36:54 +0000
Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254]
 helo=se1-gles-sth1.inumbo.com)
 by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from
 <SRS0=Zf2R=E7=citrix.com=prvs=615989724=Andrew.Cooper3@srs-se1.protection.inumbo.net>)
 id 1qhFYc-0001T8-Dn
 for xen-devel@lists.xenproject.org; Fri, 15 Sep 2023 20:36:54 +0000
Received: from esa4.hc3370-68.iphmx.com (esa4.hc3370-68.iphmx.com
 [216.71.155.144]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS
 id 9abc83a9-5407-11ee-8788-cb3800f73035;
 Fri, 15 Sep 2023 22:36:53 +0200 (CEST)
X-Outflank-Mailman: Message body and most headers restored to incoming version
X-BeenThere: xen-devel@lists.xenproject.org
List-Id: Xen developer discussion <xen-devel.lists.xenproject.org>
List-Unsubscribe: <https://lists.xenproject.org/mailman/options/xen-devel>,
 <mailto:xen-devel-request@lists.xenproject.org?subject=unsubscribe>
List-Post: <mailto:xen-devel@lists.xenproject.org>
List-Help: <mailto:xen-devel-request@lists.xenproject.org?subject=help>
List-Subscribe: <https://lists.xenproject.org/mailman/listinfo/xen-devel>,
 <mailto:xen-devel-request@lists.xenproject.org?subject=subscribe>
Errors-To: xen-devel-bounces@lists.xenproject.org
Precedence: list
Sender: "Xen-devel" <xen-devel-bounces@lists.xenproject.org>
X-Inumbo-ID: 9abc83a9-5407-11ee-8788-cb3800f73035
DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple;
  d=citrix.com; s=securemail; t=1694810213;
  h=from:to:cc:subject:date:message-id:in-reply-to:
   references:mime-version:content-transfer-encoding;
  bh=e8vuaKEQBPD5qLqcwW195bMIeNLY5T3jMc0PDLaok4g=;
  b=IjrFoasuRDanrgL5Oph2CyRAC+dlNVERQgS57PDaLCdyCskKsBzkciii
   bZjZC0YmM4zwN7FHEK7/ad4sieLgKeMiKX3Nd2CD6ly9wyL/QxS8tgrr7
   +Dx0cE44yrSLFJ9XysDL4Yemx8fZ6cYQiiwKM+w58l2a5eCw++T5nvEwl
   U=;
X-CSE-ConnectionGUID: kAevLBhUSueOHby+POGfdA==
X-CSE-MsgGUID: f8gy8O7DSnychYXMSVaPnw==
Authentication-Results: esa4.hc3370-68.iphmx.com;
 dkim=none (message not signed) header.i=none
X-SBRS: 4.0
X-MesageID: 125538999
X-Ironport-Server: esa4.hc3370-68.iphmx.com
X-Remote-IP: 162.221.156.123
X-Policy: $RELAYED
X-ThreatScanner-Verdict: Negative
IronPort-Data: A9a23:wnwOZ6scWuH8o0LQ9AbWkFk3WufnVGBeMUV32f8akzHdYApBsoF/q
 tZmKT3UO/iNZDChfYt0YIuxpBwPu5bXzNU1SQJv+C41EXtD+JbJXdiXEBz9bniYRiHhoOCLz
 O1FM4Wdc5pkJpP4jk3wWlQ0hSAkjclkfpKlVaicfHg3HFc4IMsYoUoLs/YjhYJ1isSODQqIu
 Nfjy+XSI1bg0DNvWo4uw/vrRChH4rKq4lv0gnRkPaoQ5A6HyiFOZH4iDfrZw0XQE9E88tGSH
 44v/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Dtex/hHNtOkOws1NSZLW7bUQmTXeJfBOLZqlWKcUDDH6xmpM
 NdmsBFeaXN5DNB7PoSjjPWLz9Z+qjkzJyV
X-Talos-CUID: 9a23:pgvw72/1Sfm/CJ1SFzGVv009F94BWH3g92rdBG+yGCVRb5KbRXbFrQ==
X-Talos-MUID: =?us-ascii?q?9a23=3AZg/MLQzd50ihGUtVDSd1Bh2FlYqaqKr+B1Fdtso?=
 =?us-ascii?q?qgM+VPixBCxKmsj2PRJByfw=3D=3D?=
X-IronPort-AV: E=Sophos;i="6.02,150,1688443200";
   d="scan'208";a="125538999"
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
CC: Andrew Cooper <andrew.cooper3@citrix.com>, Jan Beulich
	<JBeulich@suse.com>, =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?=
	<roger.pau@citrix.com>, Wei Liu <wl@xen.org>, Jinoh Kang
	<jinoh.kang.kr@gmail.com>
Subject: [PATCH 2/7] x86/emul: Fix and extend #DB trap handling
Date: Fri, 15 Sep 2023 21:36:23 +0100
Message-ID: <20230915203628.837732-3-andrew.cooper3@citrix.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20230915203628.837732-1-andrew.cooper3@citrix.com>
References: <20230915203628.837732-1-andrew.cooper3@citrix.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
X-ZohoMail-DKIM: pass (identity @citrix.com)
X-ZM-MESSAGEID: 1694810242964100003

Lots of this is very very broken, but we need to start somewhere.

First, the bugfix.  Hooks which use X86EMUL_DONE to skip the general emulat=
ion
still need to evaluate singlestep as part of completing the instruction.
Defer the logic until X86EMUL_DONE has been converted to X86EMUL_OKAY.

Second, the improvement.  PENDING_DBG, INTERRUPTIBILITY and ACTIVITY are
internal pipeline state which Intel exposed to software in the VMCS, and AMD
exposed a subset of in the VMCB.  Importantly, bits set in PENDING_DBG can
survive across multiple instruction boundaries if e.g. delivery of #DB is
delayed by a MovSS.

For now, introduce a full pending_dbg field into the retire union.  This ke=
eps
the sh_page_fault() and init_context() paths working but in due course the
field will want to lose the "retire" infix.

In addition, set singlestep into pending_dbg as appropriate.  Leave the old
singlestep bitfield in place until we can adjust the callers to the new
scheme.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Roger Pau Monn=C3=A9 <roger.pau@citrix.com>
CC: Wei Liu <wl@xen.org>
CC: Jinoh Kang <jinoh.kang.kr@gmail.com>

v2:
 * Only evaluate singlestep on X86EMUL_OKAY, but do so after X86EMUL_DONE h=
as
   been adjusted to X86EMUL_OKAY.
 * Adjust comments in light of X86EMUL_DONE not getting back to callers.
---
 xen/arch/x86/x86_emulate/x86_emulate.c | 20 +++++++++++++-------
 xen/arch/x86/x86_emulate/x86_emulate.h | 14 +++++++++++---
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/xen/arch/x86/x86_emulate/x86_emulate.c b/xen/arch/x86/x86_emul=
ate/x86_emulate.c
index 94caec1d142c..de7f99500e3f 100644
--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -8379,13 +8379,6 @@ x86_emulate(
     if ( !mode_64bit() )
         _regs.r(ip) =3D (uint32_t)_regs.r(ip);
=20
-    /* Should a singlestep #DB be raised? */
-    if ( rc =3D=3D X86EMUL_OKAY && singlestep && !ctxt->retire.mov_ss )
-    {
-        ctxt->retire.singlestep =3D true;
-        ctxt->retire.sti =3D false;
-    }
-
     if ( rc !=3D X86EMUL_DONE )
         *ctxt->regs =3D _regs;
     else
@@ -8394,6 +8387,19 @@ x86_emulate(
         rc =3D X86EMUL_OKAY;
     }
=20
+    /* Should a singlestep #DB be raised? */
+    if ( rc =3D=3D X86EMUL_OKAY && singlestep )
+    {
+        ctxt->retire.pending_dbg |=3D X86_DR6_BS;
+
+        /* BROKEN - TODO, merge into pending_dbg. */
+        if ( !ctxt->retire.mov_ss )
+        {
+            ctxt->retire.singlestep =3D true;
+            ctxt->retire.sti =3D false;
+        }
+    }
+
     ctxt->regs->eflags &=3D ~X86_EFLAGS_RF;
=20
  done:
diff --git a/xen/arch/x86/x86_emulate/x86_emulate.h b/xen/arch/x86/x86_emul=
ate/x86_emulate.h
index 698750267a90..fbc023c37e34 100644
--- a/xen/arch/x86/x86_emulate/x86_emulate.h
+++ b/xen/arch/x86/x86_emulate/x86_emulate.h
@@ -588,15 +588,23 @@ struct x86_emulate_ctxt
     /* Canonical opcode (see below) (valid only on X86EMUL_OKAY). */
     unsigned int opcode;
=20
-    /* Retirement state, set by the emulator (valid only on X86EMUL_OKAY).=
 */
+    /*
+     * Retirement state, set by the emulator (valid only on X86EMUL_OKAY).
+     *
+     * TODO: all this state should be input/output from the VMCS PENDING_D=
BG,
+     * INTERRUPTIBILITY and ACTIVITIY fields.
+     */
     union {
-        uint8_t raw;
+        unsigned long raw;
         struct {
+            /* Accumulated %dr6 trap bits, positive polarity. */
+            unsigned int pending_dbg;
+
             bool hlt:1;          /* Instruction HLTed. */
             bool mov_ss:1;       /* Instruction sets MOV-SS irq shadow. */
             bool sti:1;          /* Instruction sets STI irq shadow. */
             bool unblock_nmi:1;  /* Instruction clears NMI blocking. */
-            bool singlestep:1;   /* Singlestepping was active. */
+            bool singlestep:1;   /* Singlestepping was active. (TODO, merg=
e into pending_dbg) */
         };
     } retire;
=20
--=20
2.30.2