From faber@ylum.lunabase.org  Mon Apr  2 00:28:22 2007
Return-Path: <faber@ylum.lunabase.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 56F1616A402
	for <FreeBSD-gnats-submit@freebsd.org>; Mon,  2 Apr 2007 00:28:22 +0000 (UTC)
	(envelope-from faber@ylum.lunabase.org)
Received: from lunabase.org (adsl-63-200-244-106.dsl.lsan03.pacbell.net [63.200.244.106])
	by mx1.freebsd.org (Postfix) with ESMTP id D843613C458
	for <FreeBSD-gnats-submit@freebsd.org>; Mon,  2 Apr 2007 00:28:21 +0000 (UTC)
	(envelope-from faber@ylum.lunabase.org)
Received: from ylum.lunabase.org (localhost [127.0.0.1])
	by lunabase.org (8.13.8/8.13.8) with ESMTP id l31NqLiE024645;
	Sun, 1 Apr 2007 16:52:22 -0700 (PDT)
	(envelope-from faber@ylum.lunabase.org)
Received: (from faber@localhost)
	by ylum.lunabase.org (8.13.8/8.13.8/Submit) id l31NqLCs024644;
	Sun, 1 Apr 2007 16:52:21 -0700 (PDT)
	(envelope-from faber)
Message-Id: <200704012352.l31NqLCs024644@ylum.lunabase.org>
Date: Sun, 1 Apr 2007 16:52:21 -0700 (PDT)
From: Ted Faber <faber@lunabase.org>
Reply-To: Ted Faber <faber@lunabase.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc: faber@lunabase.org
Subject: kernel clock unset if RTC day of week wrong (patch included)
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         111117
>Category:       i386
>Synopsis:       [kernel] [patch] kernel clock unset if RTC day of week wrong
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Apr 02 00:30:04 GMT 2007
>Closed-Date:    Fri Sep 21 20:09:10 GMT 2007
>Last-Modified:  Tue Jul 10 03:39:33 UTC 2012
>Originator:     Ted Faber
>Release:        FreeBSD 7.0-CURRENT i386
>Organization:
None
>Environment:
System: FreeBSD ylum.lunabase.org 7.0-CURRENT FreeBSD 7.0-CURRENT #5: Thu Mar 29 22:26:12 PDT 2007 root@ylum.lunabase.org:/usr/obj/usr/src/sys/YLUM i386


>Description:
	clock_ct_to_ts() in sys/kern/subr_clock.c leaves the stuct
	timespec it's initializing unchanged if the day of the week
	passed in is inconsistent with the other values.  The dow is not
	used in the initialization of the struct.  The routine does
	properly set an error code (EINVAL) to let the caller know that
	the struct is uninitialized, and there is a "don't care" value
	(-1) for day of the week that overrides the check.  However,
	sys/i386/isa/clock.c does not check the error code, so i386
	systems that have a wrongly set or calculated day of the week in
	their RTC have their time silently set to a random value.

	Of course this happened to me.

	The patch changed sys/i386/isa/clock.c to check the error code
	and retry the call to clock_ct_to_ts() with the day of the week
	set to -1 if the call failed and the day of the week was not -1.
	I put a couple diagnostics in so the admin is aware that
	something's fishy, even if the second call succeeds.

	The patch is relative to /usr/src against a recent CURRENT.

>How-To-Repeat:
	Get a buggy RTC on an i386.  Alternatively, if your BIOS allows
	setting the day of the week independently, you could do that.
	My buggy BIOS is on an EPIA ME6000 board.

>Fix:

	Patch attached.


--- sys/i386/isa/clock.c.orig	Sun Apr  1 16:20:43 2007
+++ sys/i386/isa/clock.c	Sun Apr  1 16:34:37 2007
@@ -665,6 +665,7 @@
 	int s;
 	struct timespec ts;
 	struct clocktime ct;
+	int conv_error = 0;
 
 	if (base) {
 		printf("base %d\n", base);
@@ -702,7 +703,25 @@
 #else
 	ct.year += 2000;
 #endif
-	clock_ct_to_ts(&ct, &ts);
+	if ( clock_ct_to_ts(&ct, &ts) ) {
+		if ( ct.dow != -1 ) {
+			/* Retry in case only the day of the week was wrong */
+			ct.dow = -1;
+			if ( clock_ct_to_ts(&ct, &ts)) conv_error = 1;
+			else {
+				printf("Real time clock day of week is "
+					"wrong\n");
+				printf("Kernel time should be correct.\n");
+			}
+		}
+		else conv_error = 1;
+	}
+
+	if ( conv_error ) {
+		printf("Real time clock values fail sanity check\n");
+		printf("Kernel time of day is probably wrong\n");
+	}
+	
 	ts.tv_sec += utc_offset();
 	tc_setclock(&ts);
 }
>Release-Note:
>Audit-Trail:

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: i386/111117: commit references a PR
Date: Fri, 27 Jul 2007 09:34:48 +0000 (UTC)

 dwmalone    2007-07-27 09:34:43 UTC
 
   FreeBSD src repository
 
   Modified files:
     sys/i386/isa         clock.c 
   Log:
   It seems that some i386 mothermoards either do not implement the
   day of week field correctly, or they remember bad values that are
   written into the day of week field. For this reason, ignore the day
   of week field when reading the clock on i386 rather than bailing if
   it is set incorrectly.
   
   Problems were seen on a number of platforms, including VMWare, qemu,
   EPIA ME6000, Epox-3PTA and ABIT-SL30T.
   
   This is a slightly different fix to that proposed by Ted in his PR,
   but the same basic idea.
   
   PR:             111117
   Submitted by:   Ted Faber <faber@lunabase.org>
   Approved by:    re (rwatson)
   MFC after:      3 weeks
   
   Revision  Changes    Path
   1.239     +2 -1      src/sys/i386/isa/clock.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
Responsible-Changed-From-To: freebsd-i386->dwmalone 
Responsible-Changed-By: dwmalone 
Responsible-Changed-When: Fri Jul 27 10:54:26 UTC 2007 
Responsible-Changed-Why:  
Problem should be fixed in -current. I will MFC in a couple of weeks. 

David. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=111117 
State-Changed-From-To: open->closed 
State-Changed-By: dwmalone 
State-Changed-When: Fri Sep 21 20:08:12 UTC 2007 
State-Changed-Why:  
The code to set the clock in RELENG_6 is different from -current, so 
this shouldn't actually be a problem here. 

David. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=111117 
Responsible-Changed-From-To: dwmalone->freebsd-bugs 
Responsible-Changed-By: eadler 
Responsible-Changed-When: Tue Jul 10 03:39:32 UTC 2012 
Responsible-Changed-Why:  
over to the pool (approved by bugmeister) 

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