From nobody@FreeBSD.org  Thu Jan  6 20:39:57 2005
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 99A8716A4CE
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  6 Jan 2005 20:39:57 +0000 (GMT)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 5F38343D3F
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  6 Jan 2005 20:39:57 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id j06Kdvto016949
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 6 Jan 2005 20:39:57 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id j06KdvU7016948;
	Thu, 6 Jan 2005 20:39:57 GMT
	(envelope-from nobody)
Message-Id: <200501062039.j06KdvU7016948@www.freebsd.org>
Date: Thu, 6 Jan 2005 20:39:57 GMT
From: Bartosz Fabianowski <freebsd@chillt.de>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Exception and reboot: Loader and kernel use SSE2 instructions before they get enabled
X-Send-Pr-Version: www-2.3

>Number:         75898
>Category:       i386
>Synopsis:       Exception and reboot: Loader and kernel use SSE2 instructions before they get enabled
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-i386
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jan 06 20:40:31 GMT 2005
>Closed-Date:    Sat Feb 09 13:11:22 UTC 2008
>Last-Modified:  Sat Feb 09 13:11:22 UTC 2008
>Originator:     Bartosz Fabianowski
>Release:        5.3-STABLE
>Organization:
>Environment:
FreeBSD takahe.local 5.3-STABLE FreeBSD 5.3-STABLE #0: Wed Dec 29 17:52:52 CET 2004     root@takahe.local:/usr/obj/usr/src/sys/TAKAHE  i386
>Description:
bsd.cpu.mk as of version 1.42 on 6.0-CURRENT and 1.40.2.1 on 5.3-STABLE recognizes the setting CPUTYPE=pentium-m in /etc/make.conf, which enables code generation and optimization for the Pentium M family. One of the features enabled by this switch is SSE2. This works fine once the systems is up and running, but breaks the boot process.

The problem is that for SSE and SSE2 instructions to be available, the OSFXSR bit in CR4 must be set. This has to be done by the operating system, as the bit is off by default. At system startup, the kernel sets the bit at some point. However, the code that runs before this point in time already tries to use SSE2 instructions and fails miserably with an UD exception (invalid opcode), which in turn leads to an instant reboot.

The code that runs before the OSFXSR bit is set is the entire loader  and part of the kernel.

To confirm that the problems lies in SSE2 instructions being used too early, I recompiled with CPUTYPE=pentium3 and the system started up as expected. Others have reported the same issue with CPUTYPE=athlon-xp, which also has SSE support. It might be that some motherboards silently set OSFXSR, in which case this problem would not appear on the respective systems and therefore is a bit hard to chase.
>How-To-Repeat:
* Build world and / or kernel on a machine with above mentioned version of bsd.cpu.mk and CPUTYPE=pentium-m
* Reboot
* If the BIOS does not set OSFXSR in CR4, watch the UD exception and the instant reboot
>Fix:
One way of fixing this would be to specifically disable SSE and SSE2 support in the Makefiles of those parts of the system that run before the OSFXSR bit is set. However, it might be hard to find all the Makefiles and to maintain those fixes over a long time.

Another way would be to enable the OSFXSR bit earlier on in the boot process. NOTE however that this bit also indicates that XMM registers are saved on context switch, so with setting this bit comes the obligation to always used FXSAVE and FXRSTOR.

>Release-Note:
>Audit-Trail:

From: Hiroo Ono <hiroo@oikumene.gcd.org>
To: bug-followup@FreeBSD.org, obrien@FreeBSD.org
Cc: freebsd@chillt.de
Subject: Re: i386/75898: Exception and reboot: Loader and kernel use SSE2 instructions before they get enabled
Date: Thu, 09 Jun 2005 23:10:49 +0900

 Hello,
 
 > bsd.cpu.mk as of version 1.42 on 6.0-CURRENT and 1.40.2.1 on
 > 5.3-STABLE recognizes the setting CPUTYPE=pentium-m in
 > /etc/make.conf, which enables code generation and optimization for
 > the Pentium M family. One of the features enabled by this switch is
 > SSE2.
 
 > One way of fixing this would be to specifically disable SSE and SSE2
 > support in the Makefiles of those parts of the system that run
 > before the OSFXSR bit is set
 
 Mr.Obrien committed the fix in
 src/sys/boot/i386/Makefile.inc	rev.1.10 (HEAD) and 1.9.2.1 (RELENG_5),
 src/sys/boot/ficl/Makefile	rev.1.41 (HEAD) (not MFC'ed though).
 This seems to have fixed the problem in 6-CURRENT. The problem
 described in PR/ still remains.
 
 The PR i386/81111 (/boot/loader causes reboot due to CFLAGS+= -msse3)
 may relate to this PR.
 Also, i386/81694 may be related.
 
 The latter is not MFC'ed and thus RELENG_5 is still affected (I ran
 into this problem with 5.4-RELEASE-p1). Would you please MFC it?
State-Changed-From-To: open->patched 
State-Changed-By: linimon 
State-Changed-When: Sun Jul 8 06:27:07 UTC 2007 
State-Changed-Why:  
obrien did the MFC, but it has not yet been merged back to RELENG_5. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=75898 
State-Changed-From-To: patched->closed 
State-Changed-By: remko 
State-Changed-When: Sat Feb 9 13:11:21 UTC 2008 
State-Changed-Why:  
This needs MFC to RELENG_5, though I would like people facing this to 
encourage them to use FreeBSD-6 and/or FreeBSD-7 (preferred) since in 
the not too long future REL_5 will no longer be supported. Close the 
ticket for those reasons 

http://www.freebsd.org/cgi/query-pr.cgi?pr=75898 
>Unformatted:
