Bug 75898

Summary: Exception and reboot: Loader and kernel use SSE2 instructions before they get enabled
Product: Base System Reporter: freebsd
Component: i386Assignee: freebsd-i386 (Nobody) <i386>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 5.3-STABLE   
Hardware: Any   
OS: Any   

Description freebsd 2005-01-06 20:40:31 UTC
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.

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.
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
Comment 1 Hiroo Ono 2005-06-09 15:10:49 UTC
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?
Comment 2 Mark Linimon freebsd_committer freebsd_triage 2007-07-08 07:27:07 UTC
State Changed
From-To: open->patched

obrien did the MFC, but it has not yet been merged back to RELENG_5.
Comment 3 Remko Lodder freebsd_committer freebsd_triage 2008-02-09 13:11:21 UTC
State Changed
From-To: patched->closed

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