From nobody Fri Nov 22 03:41:47 2024 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=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1727447008; cv=none; d=zohomail.com; s=zohoarc; b=a2uIBP/CAdtQDuQMyu4Ilsg01c8phRzjlPDVxvGdrlxg1ArQ6GxyYgxe2gL563xnWUJV3kr9spLf8GVmUuQt5DI/q3N7tOGKKa+wHqFI2MJ2HPGEyR/YlB1IB73GvrBSPB5S5zXvvT//h8t7MdbYfJ0iMk7r/uyiYDD5MZlH30c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1727447008; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0tKwV8v86DGxTWdmmkUI/rzcYb/3puBp5xnTtQzsiWQ=; b=RMOHFhEpmGcF6nnDTMQg2H+n+x3cmV+r11sK2Aalb+QvLB5/3JZhQJ8dVnayXogPAEZxN8Tvr8/RyxP1nvvumWt8Pe4i3V4zoky7Ql08B9nBwdjFjBeSWOKrqEPAlwvwC4YyRhXL+z3oMxE1zzNyRNnbGFrfBBEt9Sgsj2Qjedg= 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= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1727447008736277.1457668285851; Fri, 27 Sep 2024 07:23:28 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.806323.1217644 (Exim 4.92) (envelope-from ) id 1suBsF-0007sl-P6; Fri, 27 Sep 2024 14:23:11 +0000 Received: by outflank-mailman (output) from mailman id 806323.1217644; Fri, 27 Sep 2024 14:23:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1suBsF-0007se-Mf; Fri, 27 Sep 2024 14:23:11 +0000 Received: by outflank-mailman (input) for mailman id 806323; Fri, 27 Sep 2024 14:23:10 +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 ) id 1suBsE-0007sV-B4 for xen-devel@lists.xenproject.org; Fri, 27 Sep 2024 14:23:10 +0000 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [2a00:1450:4864:20::62b]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 05190d9f-7cdc-11ef-a0ba-8be0dac302b0; Fri, 27 Sep 2024 16:23:09 +0200 (CEST) Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-a8d2b24b7a8so609058766b.1 for ; Fri, 27 Sep 2024 07:23:09 -0700 (PDT) Received: from mewpvdipd1023.corp.cloud.com ([52.166.251.127]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a93c27c590asm140402866b.71.2024.09.27.07.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 07:23:07 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 05190d9f-7cdc-11ef-a0ba-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1727446988; x=1728051788; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=0tKwV8v86DGxTWdmmkUI/rzcYb/3puBp5xnTtQzsiWQ=; b=bgWbSKqTsEG1tkamyDwJSTYGBh+tGSFW7oUWRbbJLjn/MQzWTyhXhu4s3SribgP+qc 5hqtPgWR4YyTJr0cTJGthCycrH66DUmd9Eo7KfoKnkSruU1el8G3h8UoYbabBdWftice FPIm0zQKeZRX5DW+p5l5sujgJm+quxgmu7lRg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727446988; x=1728051788; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0tKwV8v86DGxTWdmmkUI/rzcYb/3puBp5xnTtQzsiWQ=; b=MK1WrEnMO0sN4/Ekgx5AWUPJKu2bD7Vy+ndZbgIJk7sI8+ZcmO7S9oVMQqMP4hGFJp vXnjx5uQtKQ2WC4Z5pPLCs//5yRkPM8SPxk9oq8GF3ms+kR3mLr+LUzFuIo//RMj0rEo cLhyGsGT58v7xe6aYjTnPeYGuccouxcgaqswmqPw82PSLP1oDXf3okwh3sp7tvPy9+qL QW1QQzl1KKjb+B4bNNoKN6ichAnddZCE810ydi5vfic+/S99GCpcG1p8Uq5Po8U1TRx+ QOWxVtG7HTCRxxcn47xWpepSxzXSFMOtFxkpCwButANqiRon1o6kIwKtd9pleG90p5Ze QTxw== X-Gm-Message-State: AOJu0YwQw+AxLe5Cq2buIc14B27cf50mtWef1eEnbHf9dAjQI4PKrP62 ueEwv/r7cvWjnI7Nqmvwu/27uj31W/z5VP2z2+kQBhYoZxJwJXtdMyNCEXsAtd+FQ0408AnoR0o 90Sg= X-Google-Smtp-Source: AGHT+IFm3zP20vgq0icB6D00PYRUPBSniANVZWnKMdG2zNNwHHMfkkUAIYKaSK4sOdmU2CM3H1Ojqg== X-Received: by 2002:a17:907:7f8a:b0:a86:9fac:6939 with SMTP id a640c23a62f3a-a93c320f668mr369611366b.30.1727446988245; Fri, 27 Sep 2024 07:23:08 -0700 (PDT) From: Alejandro Vallejo To: Xen-devel Cc: Alejandro Vallejo , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH v3] x86/traps: Re-enable interrupts after reading cr2 in the #PF handler Date: Fri, 27 Sep 2024 15:23:05 +0100 Message-ID: <20240927142305.299841-1-alejandro.vallejo@cloud.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1727447009152116600 Hitting a page fault clobbers %cr2, so if a page fault is handled while handling a previous page fault then %cr2 will hold the address of the latter fault rather than the former. This patch makes the page fault path delay re-enabling IRQs until %cr2 has been read in order to ensure it stays consistent. A similar argument holds in additional cases, but they happen to be safe: * %dr6 inside #DB: Safe because IST exceptions don't re-enable IRQs. * MSR_XFD_ERR inside #NM: Safe because AMX isn't used in #NM handler. While in the area, remove redundant q suffix to a movq in entry.S and add space after the comma. Fixes: a4cd20a19073 ("[XEN] 'd' key dumps both host and guest state.") Signed-off-by: Alejandro Vallejo Acked-by: Roger Pau Monn=C3=A9 --- v3: * s/dispatch_handlers/dispatch_exceptions/ * Updated commit message, spelling out the state of #DB and #NM, and state an existing race with debug keys. --- xen/arch/x86/traps.c | 8 ++++++++ xen/arch/x86/x86_64/entry.S | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/xen/arch/x86/traps.c b/xen/arch/x86/traps.c index 708136f62558..a9c2c607eb08 100644 --- a/xen/arch/x86/traps.c +++ b/xen/arch/x86/traps.c @@ -1600,6 +1600,14 @@ void asmlinkage do_page_fault(struct cpu_user_regs *= regs) =20 addr =3D read_cr2(); =20 + /* + * Don't re-enable interrupts if we were running an IRQ-off region when + * we hit the page fault, or we'll break that code. + */ + ASSERT(!local_irq_is_enabled()); + if ( regs->flags & X86_EFLAGS_IF ) + local_irq_enable(); + /* fixup_page_fault() might change regs->error_code, so cache it here.= */ error_code =3D regs->error_code; =20 diff --git a/xen/arch/x86/x86_64/entry.S b/xen/arch/x86/x86_64/entry.S index b8482de8ee5b..9b0cdb76408b 100644 --- a/xen/arch/x86/x86_64/entry.S +++ b/xen/arch/x86/x86_64/entry.S @@ -844,9 +844,9 @@ handle_exception_saved: #elif !defined(CONFIG_PV) ASSERT_CONTEXT_IS_XEN #endif /* CONFIG_PV */ - sti -1: movq %rsp,%rdi - movzbl UREGS_entry_vector(%rsp),%eax +.Ldispatch_exceptions: + mov %rsp, %rdi + movzbl UREGS_entry_vector(%rsp), %eax #ifdef CONFIG_PERF_COUNTERS lea per_cpu__perfcounters(%rip), %rcx add STACK_CPUINFO_FIELD(per_cpu_offset)(%r14), %rcx @@ -866,7 +866,19 @@ handle_exception_saved: jmp .L_exn_dispatch_done; \ .L_ ## vec ## _done: =20 + /* + * IRQs kept off to derisk being hit by a nested interrupt before + * reading %cr2. Otherwise a page fault in the nested interrupt ha= ndler + * would corrupt %cr2. + */ DISPATCH(X86_EXC_PF, do_page_fault) + + /* Only re-enable IRQs if they were active before taking the fault= */ + testb $X86_EFLAGS_IF >> 8, UREGS_eflags + 1(%rsp) + jz 1f + sti +1: + DISPATCH(X86_EXC_GP, do_general_protection) DISPATCH(X86_EXC_UD, do_invalid_op) DISPATCH(X86_EXC_NM, do_device_not_available) @@ -911,7 +923,7 @@ exception_with_ints_disabled: movq %rsp,%rdi call search_pre_exception_table testq %rax,%rax # no fixup code for faulting EIP? - jz 1b + jz .Ldispatch_exceptions movq %rax,UREGS_rip(%rsp) # fixup regular stack =20 #ifdef CONFIG_XEN_SHSTK --=20 2.46.0