NetBSD Problem Report #49174

From www@NetBSD.org  Wed Sep  3 14:43:07 2014
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [149.20.53.66])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(Client CN "mail.netbsd.org", Issuer "Postmaster NetBSD.org" (verified OK))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 14732B8032
	for <gnats-bugs@gnats.NetBSD.org>; Wed,  3 Sep 2014 14:43:07 +0000 (UTC)
Message-Id: <20140903144305.DEBE9B80DE@mollari.NetBSD.org>
Date: Wed,  3 Sep 2014 14:43:05 +0000 (UTC)
From: cherry@zyx.in
Reply-To: cherry@zyx.in
To: gnats-bugs@NetBSD.org
Subject: acpi resume doesn't restore xcr0
X-Send-Pr-Version: www-1.0

>Number:         49174
>Category:       port-amd64
>Synopsis:       acpi resume doesn't restore xcr0
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    port-amd64-maintainer
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Sep 03 14:45:00 +0000 2014
>Closed-Date:    Mon Jan 01 08:21:25 +0000 2018
>Last-Modified:  Fri May 04 16:10:01 +0000 2018
>Originator:     Cherry G. Mathew
>Release:        -current, -7
>Organization:
>Environment:
>Description:
The acpi resume path from wakeup doesn't restore the intel xcr0 register, which causes an fpu panic not much later.

PoC patch below, but the implementation should probably be higher level.

>How-To-Repeat:
suspend acpi laptop. Resume it. Panic ensues.
>Fix:
diff -r 9c5992049309 sys/arch/amd64/acpi/acpi_wakeup_low.S
--- a/sys/arch/amd64/acpi/acpi_wakeup_low.S	Tue Sep 02 19:34:11 2014 +0530
+++ b/sys/arch/amd64/acpi/acpi_wakeup_low.S	Wed Sep 03 20:11:21 2014 +0530
@@ -73,6 +73,15 @@
 	movq	%rax,%cr8
 	movq	ACPI_SUSPEND_CR4(%r8),%rax
 	movq	%rax,%cr4
+
+	andq	$CR4_OSXSAVE, %rax
+	jz	1f
+	xorq	%rcx, %rcx
+	movl	ACPI_SUSPEND_XCR0(%r8), %eax
+	movl	ACPI_SUSPEND_XCR0+4(%r8), %edx
+	xsetbv
+
+1:	
 	movq	ACPI_SUSPEND_CR3(%r8),%rax
 	movq	%rax,%cr3
 	movq	ACPI_SUSPEND_CR2(%r8),%rax
@@ -80,8 +89,8 @@
 	movq	ACPI_SUSPEND_CR0(%r8),%rax
 	movq	%rax,%cr0

-	jmp 1f
-1:
+	jmp 2f
+2:

 	movq	CPUVAR(GDT),%rax
 	movzwq	ACPI_SUSPEND_TR(%r8),%rdx
@@ -131,6 +140,16 @@
 	pushfq
 	popq	ACPI_SUSPEND_REG+(7*8)(%r8)

+	movq	%cr4,%rax
+	andq	$CR4_OSXSAVE, %rax
+	jz	1f
+
+	xorq	%rcx, %rcx
+	xgetbv
+	movl	%edx,ACPI_SUSPEND_XCR0+4(%r8)
+	movl	%eax,ACPI_SUSPEND_XCR0(%r8)
+	
+1:	
 	movq	%rsp,ACPI_SUSPEND_REG+(0*8)(%r8)

 	movl	$MSR_FSBASE,%ecx
diff -r 9c5992049309 sys/arch/amd64/amd64/genassym.cf
--- a/sys/arch/amd64/amd64/genassym.cf	Tue Sep 02 19:34:11 2014 +0530
+++ b/sys/arch/amd64/amd64/genassym.cf	Wed Sep 03 20:11:21 2014 +0530
@@ -265,6 +265,7 @@
 define	ACPI_SUSPEND_CR3	offsetof(struct cpu_info, ci_suspend_cr3)
 define	ACPI_SUSPEND_CR4	offsetof(struct cpu_info, ci_suspend_cr4)
 define	ACPI_SUSPEND_CR8	offsetof(struct cpu_info, ci_suspend_cr8)
+define	ACPI_SUSPEND_XCR0	offsetof(struct cpu_info, ci_suspend_xcr0)

 if NIOAPIC > 0
 define		PIC_IOAPIC		offsetof(struct pic, pic_ioapic)
diff -r 9c5992049309 sys/arch/x86/include/cpu.h
--- a/sys/arch/x86/include/cpu.h	Tue Sep 02 19:34:11 2014 +0530
+++ b/sys/arch/x86/include/cpu.h	Wed Sep 03 20:11:21 2014 +0530
@@ -235,6 +235,7 @@
 	uintptr_t	ci_suspend_cr3;
 	uintptr_t	ci_suspend_cr4;
 	uintptr_t	ci_suspend_cr8;
+	uintptr_t	ci_suspend_xcr0;

 	/* The following must be in a single cache line. */
 	int		ci_want_resched __aligned(64);

>Release-Note:

>Audit-Trail:
From: "Maxime Villard" <maxv@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/49174 CVS commit: src/sys/arch/x86/acpi
Date: Thu, 10 Aug 2017 13:13:03 +0000

 Module Name:	src
 Committed By:	maxv
 Date:		Thu Aug 10 13:13:03 UTC 2017

 Modified Files:
 	src/sys/arch/x86/acpi: acpi_wakeup.c

 Log Message:
 Save and restore xcr0 when doing ACPI sleeps. Should fix PR/49174.


 To generate a diff of this commit:
 cvs rdiff -u -r1.45 -r1.46 src/sys/arch/x86/acpi/acpi_wakeup.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->feedback
State-Changed-By: maxv@NetBSD.org
State-Changed-When: Thu, 10 Aug 2017 13:16:33 +0000
State-Changed-Why:
Could you test the patch?


State-Changed-From-To: feedback->closed
State-Changed-By: maxv@NetBSD.org
State-Changed-When: Mon, 01 Jan 2018 08:21:25 +0000
State-Changed-Why:
Close this PR, my patch was tested by Christoph Badura.


From: coypu@sdf.org
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: port-amd64/49174 (acpi resume doesn't restore xcr0)
Date: Mon, 1 Jan 2018 16:57:36 +0000

 Pullup to -8 please? :)

From: "Martin Husemann" <martin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/49174 CVS commit: [netbsd-8] src/sys/arch/x86/acpi
Date: Fri, 4 May 2018 16:07:59 +0000

 Module Name:	src
 Committed By:	martin
 Date:		Fri May  4 16:07:59 UTC 2018

 Modified Files:
 	src/sys/arch/x86/acpi [netbsd-8]: acpi_wakeup.c

 Log Message:
 Pull up following revision(s) (requested by maya in ticket #784):
 	sys/arch/x86/acpi/acpi_wakeup.c: revision 1.46
 Save and restore xcr0 when doing ACPI sleeps. Should fix PR/49174.


 To generate a diff of this commit:
 cvs rdiff -u -r1.45 -r1.45.8.1 src/sys/arch/x86/acpi/acpi_wakeup.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.43 2018/01/16 07:36:43 maya Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2017 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.