make sure that the pseudo_set isn't empty.
[unix-history] / sys / kern / kern_ntptime.c
index ee86506..9e50a42 100644 (file)
  *                                                                            *
  ******************************************************************************/
 
  *                                                                            *
  ******************************************************************************/
 
-/*
- * $Id$
- */
-
 /*
  * Modification history kern_ntptime.c
  *
 /*
  * Modification history kern_ntptime.c
  *
+ * 24 Mar 94   David L. Mills
+ *     Revised syscall interface to include new variables for PPS
+ *     time discipline.
+ *
  * 14 Feb 94   David L. Mills
  *     Added code for external clock
  *
  * 14 Feb 94   David L. Mills
  *     Added code for external clock
  *
@@ -31,7 +31,8 @@
  *     Created file
  */
 /*
  *     Created file
  */
 /*
- * ntp_gettime(), ntp_adjtime() - precision time interface
+ * ntp_gettime(), ntp_adjtime() - precision time interface for SunOS
+ * 4.1.1 and 4.1.3
  *
  * These routines consitute the Network Time Protocol (NTP) interfaces
  * for user and daemon application programs. The ntp_gettime() routine
  *
  * These routines consitute the Network Time Protocol (NTP) interfaces
  * for user and daemon application programs. The ntp_gettime() routine
 #include "param.h"
 #include "systm.h"
 #include "kernel.h"
 #include "param.h"
 #include "systm.h"
 #include "kernel.h"
-#include "timex.h"
 #include "proc.h"
 #include "proc.h"
+#include "timex.h"
 
 
-struct timex ntp_pll;          /* has to be declared somewhere... */
+/*
+ * The following variables are used by the hardclock() routine in the
+ * kern_clock.c module and are described in that module. 
+ */
+extern struct timeval time;    /* kernel time variable */
+extern int time_state;         /* clock state */
+extern int time_status;                /* clock status bits */
+extern long time_offset;       /* time adjustment (us) */
+extern long time_freq;         /* frequency offset (scaled ppm) */
+extern long time_maxerror;     /* maximum error (us) */
+extern long time_esterror;     /* estimated error (us) */
+extern long time_constant;     /* pll time constant */
+extern long time_precision;    /* clock precision (us) */
+extern long time_tolerance;    /* frequency tolerance (scaled ppm) */
 
 
+#ifdef PPS_SYNC
+/*
+ * The following variables are used only if the PPS signal discipline
+ * is configured in the kernel.
+ */
+extern int pps_shift;          /* interval duration (s) (shift) */
+extern long pps_freq;          /* pps frequency offset (scaled ppm) */
+extern long pps_jitter;                /* pps jitter (us) */
+extern long pps_stabil;                /* pps stability (scaled ppm) */
+extern long pps_jitcnt;                /* jitter limit exceeded */
+extern long pps_calcnt;                /* calibration intervals */
+extern long pps_errcnt;                /* calibration errors */
+extern long pps_stbcnt;                /* stability limit exceeded */
+#endif /* PPS_SYNC */
 
 /*
  * ntp_gettime() - NTP user application interface
  */
 
 /*
  * ntp_gettime() - NTP user application interface
  */
-
 struct ntp_gettime_args {
 struct ntp_gettime_args {
-       struct ntptimeval *tp;
+  struct ntptimeval *tp;
 };
 
 int
 };
 
 int
@@ -64,36 +91,92 @@ ntp_gettime(struct proc *p, struct ntp_gettime_args *uap, int *retval)
 {
        struct timeval atv;
        struct ntptimeval ntv;
 {
        struct timeval atv;
        struct ntptimeval ntv;
-       int error = 0;
        int s;
        int s;
+       int error = 0;
 
        if (uap->tp) {
                s = splclock();
 
        if (uap->tp) {
                s = splclock();
-               microtime(&ntv.time);
-               ntv.maxerror = ntp_pll.maxerror;
-               ntv.esterror = ntp_pll.esterror;
+#ifdef EXT_CLOCK
+               /*
+                * The microtime() external clock routine returns a
+                * status code. If less than zero, we declare an error
+                * in the clock status word and return the kernel
+                * (software) time variable. While there are other
+                * places that call microtime(), this is the only place
+                * that matters from an application point of view.
+                */
+               if (microtime(&atv) < 0) {
+                       time_status |= STA_CLOCKERR;
+                       ntv.time = time;
+               } else
+                       time_status &= ~STA_CLOCKERR;
+#else /* EXT_CLOCK */
+               microtime(&atv);
+#endif /* EXT_CLOCK */
+               ntv.time = atv;
+               ntv.maxerror = time_maxerror;
+               ntv.esterror = time_esterror;
                (void) splx(s);
 
                (void) splx(s);
 
-               error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof (ntv));
+               error = copyout((caddr_t)&ntv, (caddr_t)uap->tp,
+                   sizeof (ntv));
        }
        }
-       if (!error)
-               retval[0] = ntp_pll.status;
-       return error;
-}
+       if (!error) {
+               *retval = time_state;
 
 
-struct ntp_adjtime_args {
-       struct timex *tp;
-};
+               /*
+                * Status word error decode. If any of these conditions
+                * occur, an error is returned, instead of the status
+                * word. Most applications will care only about the fact
+                * the system clock may not be trusted, not about the
+                * details.
+                *
+                * Hardware or software error
+                */
+               if (time_status & (STA_UNSYNC | STA_CLOCKERR))
+                       *retval = TIME_ERROR;
+
+               /*
+                * PPS signal lost when either time or frequency
+                * synchronization requested
+                */
+               if (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
+                   !(time_status & STA_PPSSIGNAL))
+                       *retval = TIME_ERROR;
 
 
-extern void hardupdate(long);
+               /*
+                * PPS jitter exceeded when time synchronization
+                * requested
+                */
+               if (time_status & STA_PPSTIME &&
+                   time_status & STA_PPSJITTER)
+                       *retval = TIME_ERROR;
+
+               /*
+                * PPS wander exceeded or calibration error when
+                * frequency synchronization requested
+                */
+               if (time_status & STA_PPSFREQ &&
+                   time_status & (STA_PPSWANDER | STA_PPSERROR))
+                       *retval = TIME_ERROR;
+       }
+       return error;
+}
 
 /*
  * ntp_adjtime() - NTP daemon application interface
  */
 
 /*
  * ntp_adjtime() - NTP daemon application interface
  */
+struct ntp_adjtime_args {
+  struct timex *tp;
+};
+
 int
 int
-ntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap, int *retval) {
+ntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap, int *retval)
+{
        struct timex ntv;
        struct timex ntv;
-       int s, error;
+       int modes;
+       int s;
+       int error;
 
        error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv));
        if (error)
 
        error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv));
        if (error)
@@ -104,58 +187,81 @@ ntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap, int *retval) {
         * change anything. Note that there is no error checking here on
         * the assumption the superuser should know what it is doing.
         */
         * change anything. Note that there is no error checking here on
         * the assumption the superuser should know what it is doing.
         */
-       if(ntv.mode != 0 && !(error = suser(p->p_ucred, &p->p_acflag)))
-         return error ? error : EPERM;
+       modes = ntv.modes;
+       if ((modes != 0)
+           && (error = suser(p->p_cred->pc_ucred, &p->p_acflag)))
+               return error;
 
        s = splclock();
 
        s = splclock();
-       if (ntv.mode & ADJ_OFFSET)
-               hardupdate(ntv.offset);
-       if (ntv.mode & ADJ_FREQUENCY)
+       if (modes & MOD_FREQUENCY)
 #ifdef PPS_SYNC
 #ifdef PPS_SYNC
-               ntp_pll.frequency = ntv.frequency - ntp_pll.ybar;
+               time_freq = ntv.freq - pps_freq;
 #else /* PPS_SYNC */
 #else /* PPS_SYNC */
-               ntp_pll.frequency = ntv.frequency;
+               time_freq = ntv.freq;
 #endif /* PPS_SYNC */
 #endif /* PPS_SYNC */
-       if (ntv.mode & ADJ_MAXERROR)
-               ntp_pll.maxerror = ntv.maxerror;
-       if (ntv.mode & ADJ_ESTERROR)
-               ntp_pll.esterror = ntv.esterror;
-       if (ntv.mode & ADJ_STATUS)
-               if (ntp_pll.status == TIME_OK || ntv.status == TIME_BAD)
-                       ntp_pll.status = ntv.status;
-       if (ntv.mode & ADJ_TIMECONST)
-               ntp_pll.time_constant = ntv.time_constant;
+       if (modes & MOD_MAXERROR)
+               time_maxerror = ntv.maxerror;
+       if (modes & MOD_ESTERROR)
+               time_esterror = ntv.esterror;
+       if (modes & MOD_STATUS) {
+               time_status &= STA_RONLY;
+               time_status |= ntv.status & ~STA_RONLY;
+       }
+       if (modes & MOD_TIMECONST)
+               time_constant = ntv.constant;
+       if (modes & MOD_OFFSET)
+               hardupdate(ntv.offset);
 
        /*
         * Retrieve all clock variables
         */
 
        /*
         * Retrieve all clock variables
         */
-       if (ntp_pll.offset < 0)
-               ntv.offset = -(-ntp_pll.offset >> SHIFT_UPDATE);
+       if (time_offset < 0)
+               ntv.offset = -(-time_offset >> SHIFT_UPDATE);
        else
        else
-               ntv.offset = ntp_pll.offset >> SHIFT_UPDATE;
+               ntv.offset = time_offset >> SHIFT_UPDATE;
 #ifdef PPS_SYNC
 #ifdef PPS_SYNC
-       ntv.frequency = ntp_pll.frequency + ntp_pll.ybar;
+       ntv.freq = time_freq + pps_freq;
 #else /* PPS_SYNC */
 #else /* PPS_SYNC */
-       ntv.frequency = ntp_pll.frequency;
+       ntv.freq = time_freq;
 #endif /* PPS_SYNC */
 #endif /* PPS_SYNC */
-       ntv.maxerror = ntp_pll.maxerror;
-       ntv.esterror = ntp_pll.esterror;
-       ntv.status = ntp_pll.status;
-       ntv.time_constant = ntp_pll.time_constant;
-       ntv.precision = ntp_pll.precision;
-       ntv.tolerance = ntp_pll.tolerance;
+       ntv.maxerror = time_maxerror;
+       ntv.esterror = time_esterror;
+       ntv.status = time_status;
+       ntv.constant = time_constant;
+       ntv.precision = time_precision;
+       ntv.tolerance = time_tolerance;
 #ifdef PPS_SYNC
 #ifdef PPS_SYNC
-       ntv.ybar = ntp_pll.ybar;
-       ntv.disp = ntp_pll.disp;
-       ntv.shift = ntp_pll.shift;
-       ntv.calcnt = ntp_pll.calcnt;
-       ntv.jitcnt = ntp_pll.jitcnt;
-       ntv.discnt = ntp_pll.discnt;
+       ntv.shift = pps_shift;
+       ntv.ppsfreq = pps_freq;
+       ntv.jitter = pps_jitter >> PPS_AVG;
+       ntv.stabil = pps_stabil;
+       ntv.calcnt = pps_calcnt;
+       ntv.errcnt = pps_errcnt;
+       ntv.jitcnt = pps_jitcnt;
+       ntv.stbcnt = pps_stbcnt;
 #endif /* PPS_SYNC */
        (void)splx(s);
 
        error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof(ntv));
 #endif /* PPS_SYNC */
        (void)splx(s);
 
        error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof(ntv));
-       retval[0] = ntp_pll.status;
+       if (!error) {
+               /*
+                * Status word error decode. See comments in
+                * ntp_gettime() routine.
+                */
+               retval[0] = time_state;
+               if (time_status & (STA_UNSYNC | STA_CLOCKERR))
+                       retval[0] = TIME_ERROR;
+               if (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
+                   !(time_status & STA_PPSSIGNAL))
+                       retval[0] = TIME_ERROR;
+               if (time_status & STA_PPSTIME &&
+                   time_status & STA_PPSJITTER)
+                       retval[0] = TIME_ERROR;
+               if (time_status & STA_PPSFREQ &&
+                   time_status & (STA_PPSWANDER | STA_PPSERROR))
+                       retval[0] = TIME_ERROR;
+       }
        return error;
 }
 
        return error;
 }
 
+