Home | History | Annotate | Line # | Download | only in kern
kern_ntptime.c revision 1.12
      1  1.12       mrg /*	$NetBSD: kern_ntptime.c,v 1.12 2000/06/27 17:41:23 mrg Exp $	*/
      2   1.1  jonathan 
      3   1.1  jonathan /******************************************************************************
      4   1.1  jonathan  *                                                                            *
      5   1.1  jonathan  * Copyright (c) David L. Mills 1993, 1994                                    *
      6   1.1  jonathan  *                                                                            *
      7   1.1  jonathan  * Permission to use, copy, modify, and distribute this software and its      *
      8   1.1  jonathan  * documentation for any purpose and without fee is hereby granted, provided  *
      9   1.1  jonathan  * that the above copyright notice appears in all copies and that both the    *
     10   1.1  jonathan  * copyright notice and this permission notice appear in supporting           *
     11   1.1  jonathan  * documentation, and that the name University of Delaware not be used in     *
     12   1.1  jonathan  * advertising or publicity pertaining to distribution of the software        *
     13   1.1  jonathan  * without specific, written prior permission.  The University of Delaware    *
     14   1.1  jonathan  * makes no representations about the suitability this software for any       *
     15   1.1  jonathan  * purpose.  It is provided "as is" without express or implied warranty.      *
     16   1.1  jonathan  *                                                                            *
     17   1.1  jonathan  ******************************************************************************/
     18   1.1  jonathan 
     19   1.1  jonathan /*
     20   1.1  jonathan  * Modification history kern_ntptime.c
     21   1.1  jonathan  *
     22   1.1  jonathan  * 24 Sep 94	David L. Mills
     23   1.1  jonathan  *	Tightened code at exits.
     24   1.1  jonathan  *
     25   1.1  jonathan  * 24 Mar 94	David L. Mills
     26   1.1  jonathan  *	Revised syscall interface to include new variables for PPS
     27   1.1  jonathan  *	time discipline.
     28   1.1  jonathan  *
     29   1.1  jonathan  * 14 Feb 94	David L. Mills
     30   1.1  jonathan  *	Added code for external clock
     31   1.1  jonathan  *
     32   1.1  jonathan  * 28 Nov 93	David L. Mills
     33   1.1  jonathan  *	Revised frequency scaling to conform with adjusted parameters
     34   1.1  jonathan  *
     35   1.1  jonathan  * 17 Sep 93	David L. Mills
     36   1.1  jonathan  *	Created file
     37   1.1  jonathan  */
     38   1.1  jonathan /*
     39   1.1  jonathan  * ntp_gettime(), ntp_adjtime() - precision time interface for SunOS
     40   1.1  jonathan  * V4.1.1 and V4.1.3
     41   1.1  jonathan  *
     42   1.1  jonathan  * These routines consitute the Network Time Protocol (NTP) interfaces
     43   1.1  jonathan  * for user and daemon application programs. The ntp_gettime() routine
     44   1.1  jonathan  * provides the time, maximum error (synch distance) and estimated error
     45   1.1  jonathan  * (dispersion) to client user application programs. The ntp_adjtime()
     46   1.1  jonathan  * routine is used by the NTP daemon to adjust the system clock to an
     47   1.1  jonathan  * externally derived time. The time offset and related variables set by
     48   1.1  jonathan  * this routine are used by hardclock() to adjust the phase and
     49   1.1  jonathan  * frequency of the phase-lock loop which controls the system clock.
     50   1.1  jonathan  */
     51   1.6  jonathan #include "opt_ntp.h"
     52   1.6  jonathan 
     53   1.1  jonathan #include <sys/param.h>
     54   1.1  jonathan #include <sys/resourcevar.h>
     55   1.1  jonathan #include <sys/systm.h>
     56   1.1  jonathan #include <sys/kernel.h>
     57   1.1  jonathan #include <sys/proc.h>
     58   1.1  jonathan #include <sys/timex.h>
     59   1.1  jonathan #include <sys/vnode.h>
     60   1.1  jonathan 
     61   1.1  jonathan #include <sys/mount.h>
     62   1.1  jonathan #include <sys/syscallargs.h>
     63   1.1  jonathan 
     64   1.1  jonathan #include <machine/cpu.h>
     65   1.1  jonathan 
     66  1.12       mrg #include <uvm/uvm_extern.h>
     67   1.2  christos #include <sys/sysctl.h>
     68   1.2  christos 
     69   1.4   thorpej #ifdef NTP
     70   1.4   thorpej 
     71   1.1  jonathan /*
     72   1.1  jonathan  * The following variables are used by the hardclock() routine in the
     73   1.1  jonathan  * kern_clock.c module and are described in that module.
     74   1.1  jonathan  */
     75   1.1  jonathan extern int time_state;		/* clock state */
     76   1.1  jonathan extern int time_status;		/* clock status bits */
     77   1.1  jonathan extern long time_offset;	/* time adjustment (us) */
     78   1.1  jonathan extern long time_freq;		/* frequency offset (scaled ppm) */
     79   1.1  jonathan extern long time_maxerror;	/* maximum error (us) */
     80   1.1  jonathan extern long time_esterror;	/* estimated error (us) */
     81   1.1  jonathan extern long time_constant;	/* pll time constant */
     82   1.1  jonathan extern long time_precision;	/* clock precision (us) */
     83   1.1  jonathan extern long time_tolerance;	/* frequency tolerance (scaled ppm) */
     84   1.1  jonathan 
     85   1.1  jonathan #ifdef PPS_SYNC
     86   1.1  jonathan /*
     87   1.1  jonathan  * The following variables are used only if the PPS signal discipline
     88   1.1  jonathan  * is configured in the kernel.
     89   1.1  jonathan  */
     90   1.1  jonathan extern int pps_shift;		/* interval duration (s) (shift) */
     91   1.1  jonathan extern long pps_freq;		/* pps frequency offset (scaled ppm) */
     92   1.1  jonathan extern long pps_jitter;		/* pps jitter (us) */
     93   1.1  jonathan extern long pps_stabil;		/* pps stability (scaled ppm) */
     94   1.1  jonathan extern long pps_jitcnt;		/* jitter limit exceeded */
     95   1.1  jonathan extern long pps_calcnt;		/* calibration intervals */
     96   1.1  jonathan extern long pps_errcnt;		/* calibration errors */
     97   1.1  jonathan extern long pps_stbcnt;		/* stability limit exceeded */
     98   1.1  jonathan #endif /* PPS_SYNC */
     99   1.1  jonathan 
    100   1.1  jonathan 
    101   1.1  jonathan 
    102   1.1  jonathan /*ARGSUSED*/
    103   1.1  jonathan /*
    104   1.1  jonathan  * ntp_gettime() - NTP user application interface
    105   1.1  jonathan  */
    106   1.1  jonathan int
    107   1.3   thorpej sys_ntp_gettime(p, v, retval)
    108   1.1  jonathan 	struct proc *p;
    109   1.1  jonathan 	void *v;
    110   1.1  jonathan 	register_t *retval;
    111   1.1  jonathan 
    112   1.1  jonathan {
    113   1.3   thorpej 	struct sys_ntp_gettime_args /* {
    114   1.5       cgd 		syscallarg(struct ntptimeval *) ntvp;
    115   1.1  jonathan 	} */ *uap = v;
    116   1.1  jonathan 	struct timeval atv;
    117   1.1  jonathan 	struct ntptimeval ntv;
    118   1.1  jonathan 	int error = 0;
    119   1.1  jonathan 	int s;
    120   1.1  jonathan 
    121   1.5       cgd 	if (SCARG(uap, ntvp)) {
    122   1.1  jonathan 		s = splclock();
    123   1.1  jonathan #ifdef EXT_CLOCK
    124   1.1  jonathan 		/*
    125   1.1  jonathan 		 * The microtime() external clock routine returns a
    126   1.1  jonathan 		 * status code. If less than zero, we declare an error
    127   1.1  jonathan 		 * in the clock status word and return the kernel
    128   1.1  jonathan 		 * (software) time variable. While there are other
    129   1.1  jonathan 		 * places that call microtime(), this is the only place
    130   1.1  jonathan 		 * that matters from an application point of view.
    131   1.1  jonathan 		 */
    132   1.1  jonathan 		if (microtime(&atv) < 0) {
    133   1.1  jonathan 			time_status |= STA_CLOCKERR;
    134   1.1  jonathan 			ntv.time = time;
    135   1.1  jonathan 		} else
    136   1.1  jonathan 			time_status &= ~STA_CLOCKERR;
    137   1.1  jonathan #else /* EXT_CLOCK */
    138   1.1  jonathan 		microtime(&atv);
    139   1.1  jonathan #endif /* EXT_CLOCK */
    140   1.1  jonathan 		ntv.time = atv;
    141   1.1  jonathan 		ntv.maxerror = time_maxerror;
    142   1.1  jonathan 		ntv.esterror = time_esterror;
    143   1.1  jonathan 		(void) splx(s);
    144   1.1  jonathan 
    145   1.5       cgd 		error = copyout((caddr_t)&ntv, (caddr_t)SCARG(uap, ntvp),
    146   1.7     perry 		    sizeof(ntv));
    147   1.1  jonathan 	}
    148   1.1  jonathan 	if (!error) {
    149   1.1  jonathan 
    150   1.1  jonathan 		/*
    151   1.1  jonathan 		 * Status word error decode. If any of these conditions
    152   1.1  jonathan 		 * occur, an error is returned, instead of the status
    153   1.1  jonathan 		 * word. Most applications will care only about the fact
    154   1.1  jonathan 		 * the system clock may not be trusted, not about the
    155   1.1  jonathan 		 * details.
    156   1.1  jonathan 		 *
    157   1.1  jonathan 		 * Hardware or software error
    158   1.1  jonathan 		 */
    159   1.1  jonathan 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
    160   1.1  jonathan 
    161   1.1  jonathan 		/*
    162   1.1  jonathan 		 * PPS signal lost when either time or frequency
    163   1.1  jonathan 		 * synchronization requested
    164   1.1  jonathan 		 */
    165   1.1  jonathan 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
    166   1.1  jonathan 		    !(time_status & STA_PPSSIGNAL)) ||
    167   1.1  jonathan 
    168   1.1  jonathan 		/*
    169   1.1  jonathan 		 * PPS jitter exceeded when time synchronization
    170   1.1  jonathan 		 * requested
    171   1.1  jonathan 		 */
    172   1.1  jonathan 		    (time_status & STA_PPSTIME &&
    173   1.1  jonathan 		    time_status & STA_PPSJITTER) ||
    174   1.1  jonathan 
    175   1.1  jonathan 		/*
    176   1.1  jonathan 		 * PPS wander exceeded or calibration error when
    177   1.1  jonathan 		 * frequency synchronization requested
    178   1.1  jonathan 		 */
    179   1.1  jonathan 		    (time_status & STA_PPSFREQ &&
    180   1.1  jonathan 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
    181   1.1  jonathan 			*retval = TIME_ERROR;
    182   1.1  jonathan 		else
    183   1.1  jonathan 			*retval = (register_t)time_state;
    184   1.1  jonathan 	}
    185   1.1  jonathan 	return(error);
    186   1.1  jonathan }
    187   1.1  jonathan 
    188   1.1  jonathan 
    189   1.1  jonathan /* ARGSUSED */
    190   1.1  jonathan /*
    191   1.1  jonathan  * ntp_adjtime() - NTP daemon application interface
    192   1.1  jonathan  */
    193   1.1  jonathan int
    194   1.3   thorpej sys_ntp_adjtime(p, v, retval)
    195   1.1  jonathan 	struct proc *p;
    196   1.1  jonathan 	void *v;
    197   1.1  jonathan 	register_t *retval;
    198   1.1  jonathan {
    199   1.3   thorpej 	struct sys_ntp_adjtime_args /* {
    200   1.1  jonathan 		syscallarg(struct timex *) tp;
    201   1.1  jonathan 	} */ *uap = v;
    202   1.1  jonathan 	struct timex ntv;
    203   1.1  jonathan 	int error = 0;
    204   1.1  jonathan 	int modes;
    205   1.1  jonathan 	int s;
    206   1.1  jonathan 
    207   1.1  jonathan 	if ((error = copyin((caddr_t)SCARG(uap, tp), (caddr_t)&ntv,
    208   1.1  jonathan 			sizeof(ntv))))
    209   1.1  jonathan 		return (error);
    210   1.1  jonathan 
    211   1.1  jonathan 	/*
    212   1.1  jonathan 	 * Update selected clock variables - only the superuser can
    213   1.1  jonathan 	 * change anything. Note that there is no error checking here on
    214   1.1  jonathan 	 * the assumption the superuser should know what it is doing.
    215   1.1  jonathan 	 */
    216   1.1  jonathan 	modes = ntv.modes;
    217   1.1  jonathan 	if (modes != 0 && (error = suser(p->p_ucred, &p->p_acflag)))
    218   1.1  jonathan 		return (error);
    219   1.1  jonathan 
    220   1.1  jonathan 	s = splclock();
    221   1.1  jonathan 	if (modes & MOD_FREQUENCY)
    222   1.1  jonathan #ifdef PPS_SYNC
    223   1.1  jonathan 		time_freq = ntv.freq - pps_freq;
    224   1.1  jonathan #else /* PPS_SYNC */
    225   1.1  jonathan 		time_freq = ntv.freq;
    226   1.1  jonathan #endif /* PPS_SYNC */
    227   1.1  jonathan 	if (modes & MOD_MAXERROR)
    228   1.1  jonathan 		time_maxerror = ntv.maxerror;
    229   1.1  jonathan 	if (modes & MOD_ESTERROR)
    230   1.1  jonathan 		time_esterror = ntv.esterror;
    231   1.1  jonathan 	if (modes & MOD_STATUS) {
    232   1.1  jonathan 		time_status &= STA_RONLY;
    233   1.1  jonathan 		time_status |= ntv.status & ~STA_RONLY;
    234   1.1  jonathan 	}
    235   1.1  jonathan 	if (modes & MOD_TIMECONST)
    236   1.1  jonathan 		time_constant = ntv.constant;
    237   1.1  jonathan 	if (modes & MOD_OFFSET)
    238   1.1  jonathan 		hardupdate(ntv.offset);
    239   1.1  jonathan 
    240   1.1  jonathan 	/*
    241   1.1  jonathan 	 * Retrieve all clock variables
    242   1.1  jonathan 	 */
    243   1.1  jonathan 	if (time_offset < 0)
    244   1.1  jonathan 		ntv.offset = -(-time_offset >> SHIFT_UPDATE);
    245   1.1  jonathan 	else
    246   1.1  jonathan 		ntv.offset = time_offset >> SHIFT_UPDATE;
    247   1.1  jonathan #ifdef PPS_SYNC
    248   1.1  jonathan 	ntv.freq = time_freq + pps_freq;
    249   1.1  jonathan #else /* PPS_SYNC */
    250   1.1  jonathan 	ntv.freq = time_freq;
    251   1.1  jonathan #endif /* PPS_SYNC */
    252   1.1  jonathan 	ntv.maxerror = time_maxerror;
    253   1.1  jonathan 	ntv.esterror = time_esterror;
    254   1.1  jonathan 	ntv.status = time_status;
    255   1.1  jonathan 	ntv.constant = time_constant;
    256   1.1  jonathan 	ntv.precision = time_precision;
    257   1.1  jonathan 	ntv.tolerance = time_tolerance;
    258   1.1  jonathan #ifdef PPS_SYNC
    259   1.1  jonathan 	ntv.shift = pps_shift;
    260   1.1  jonathan 	ntv.ppsfreq = pps_freq;
    261   1.1  jonathan 	ntv.jitter = pps_jitter >> PPS_AVG;
    262   1.1  jonathan 	ntv.stabil = pps_stabil;
    263   1.1  jonathan 	ntv.calcnt = pps_calcnt;
    264   1.1  jonathan 	ntv.errcnt = pps_errcnt;
    265   1.1  jonathan 	ntv.jitcnt = pps_jitcnt;
    266   1.1  jonathan 	ntv.stbcnt = pps_stbcnt;
    267   1.1  jonathan #endif /* PPS_SYNC */
    268   1.1  jonathan 	(void)splx(s);
    269   1.1  jonathan 
    270   1.1  jonathan 	error = copyout((caddr_t)&ntv, (caddr_t)SCARG(uap, tp), sizeof(ntv));
    271   1.1  jonathan 	if (!error) {
    272   1.1  jonathan 
    273   1.1  jonathan 		/*
    274   1.1  jonathan 		 * Status word error decode. See comments in
    275   1.1  jonathan 		 * ntp_gettime() routine.
    276   1.1  jonathan 		 */
    277   1.1  jonathan 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
    278   1.1  jonathan 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
    279   1.1  jonathan 		    !(time_status & STA_PPSSIGNAL)) ||
    280   1.1  jonathan 		    (time_status & STA_PPSTIME &&
    281   1.1  jonathan 		    time_status & STA_PPSJITTER) ||
    282   1.1  jonathan 		    (time_status & STA_PPSFREQ &&
    283   1.1  jonathan 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
    284   1.1  jonathan 			*retval = TIME_ERROR;
    285   1.1  jonathan 		else
    286   1.1  jonathan 			*retval = (register_t)time_state;
    287   1.1  jonathan 	}
    288   1.1  jonathan 	return error;
    289   1.1  jonathan }
    290   1.1  jonathan 
    291   1.1  jonathan 
    292   1.1  jonathan 
    293   1.1  jonathan /*
    294   1.1  jonathan  * return information about kernel precision timekeeping
    295   1.1  jonathan  */
    296   1.1  jonathan int
    297   1.1  jonathan sysctl_ntptime(where, sizep)
    298  1.10    simonb 	void *where;
    299   1.1  jonathan 	size_t *sizep;
    300   1.1  jonathan {
    301   1.1  jonathan 	struct timeval atv;
    302   1.1  jonathan 	struct ntptimeval ntv;
    303   1.1  jonathan 	int s;
    304   1.1  jonathan 
    305   1.1  jonathan 	/*
    306   1.1  jonathan 	 * Construct ntp_timeval.
    307   1.1  jonathan 	 */
    308   1.1  jonathan 
    309   1.1  jonathan 	s = splclock();
    310   1.1  jonathan #ifdef EXT_CLOCK
    311   1.1  jonathan 	/*
    312   1.1  jonathan 	 * The microtime() external clock routine returns a
    313   1.1  jonathan 	 * status code. If less than zero, we declare an error
    314   1.1  jonathan 	 * in the clock status word and return the kernel
    315   1.1  jonathan 	 * (software) time variable. While there are other
    316   1.1  jonathan 	 * places that call microtime(), this is the only place
    317   1.1  jonathan 	 * that matters from an application point of view.
    318   1.1  jonathan 	 */
    319   1.1  jonathan 	if (microtime(&atv) < 0) {
    320   1.1  jonathan 		time_status |= STA_CLOCKERR;
    321   1.1  jonathan 		ntv.time = time;
    322   1.1  jonathan 	} else {
    323   1.1  jonathan 		time_status &= ~STA_CLOCKERR;
    324   1.1  jonathan 	}
    325   1.1  jonathan #else /* EXT_CLOCK */
    326   1.1  jonathan 	microtime(&atv);
    327   1.1  jonathan #endif /* EXT_CLOCK */
    328   1.1  jonathan 	ntv.time = atv;
    329   1.1  jonathan 	ntv.maxerror = time_maxerror;
    330   1.1  jonathan 	ntv.esterror = time_esterror;
    331   1.1  jonathan 	splx(s);
    332   1.1  jonathan 
    333   1.1  jonathan #ifdef notyet
    334   1.1  jonathan 	/*
    335   1.1  jonathan 	 * Status word error decode. If any of these conditions
    336   1.1  jonathan 	 * occur, an error is returned, instead of the status
    337   1.1  jonathan 	 * word. Most applications will care only about the fact
    338   1.1  jonathan 	 * the system clock may not be trusted, not about the
    339   1.1  jonathan 	 * details.
    340   1.1  jonathan 	 *
    341   1.1  jonathan 	 * Hardware or software error
    342   1.1  jonathan 	 */
    343   1.1  jonathan 	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
    344   1.1  jonathan 		ntv.time_state = TIME_ERROR;
    345   1.1  jonathan 
    346   1.1  jonathan 	/*
    347   1.1  jonathan 	 * PPS signal lost when either time or frequency
    348   1.1  jonathan 	 * synchronization requested
    349   1.1  jonathan 	 */
    350   1.1  jonathan 	   (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
    351   1.1  jonathan 	    !(time_status & STA_PPSSIGNAL)) ||
    352   1.1  jonathan 
    353   1.1  jonathan 	/*
    354   1.1  jonathan 	 * PPS jitter exceeded when time synchronization
    355   1.1  jonathan 	 * requested
    356   1.1  jonathan 	 */
    357   1.1  jonathan 	   (time_status & STA_PPSTIME &&
    358   1.1  jonathan 	    time_status & STA_PPSJITTER) ||
    359   1.1  jonathan 
    360   1.1  jonathan 	/*
    361   1.1  jonathan 	 * PPS wander exceeded or calibration error when
    362   1.1  jonathan 	 * frequency synchronization requested
    363   1.1  jonathan 	 */
    364   1.1  jonathan 	   (time_status & STA_PPSFREQ &&
    365   1.1  jonathan 	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
    366   1.1  jonathan 		ntv.time_state = TIME_ERROR;
    367   1.1  jonathan 	else
    368   1.1  jonathan 		ntv.time_state = time_state;
    369   1.1  jonathan #endif /* notyet */
    370   1.1  jonathan 	return (sysctl_rdstruct(where, sizep, NULL, &ntv, sizeof(ntv)));
    371   1.1  jonathan }
    372   1.4   thorpej 
    373   1.4   thorpej #else /* !NTP */
    374   1.4   thorpej 
    375   1.4   thorpej /*
    376   1.4   thorpej  * For kernels configured without the NTP option, emulate the behavior
    377   1.4   thorpej  * of a kernel with no NTP support (i.e., sys_nosys()). On systems
    378   1.4   thorpej  * where kernel  NTP support appears present when xntpd is compiled,
    379   1.4   thorpej  * (e.g., sys/timex.h is present),  xntpd relies on getting a SIGSYS
    380   1.4   thorpej  * signal in response to an ntp_adjtime() syscal, to inform xntpd that
    381   1.4   thorpej  * NTP support is not really present, and xntpd should fall back to
    382   1.4   thorpej  * using a user-level phase-locked loop to discipline the clock.
    383   1.4   thorpej  */
    384   1.4   thorpej int
    385   1.4   thorpej sys_ntp_gettime(p, v, retval)
    386   1.4   thorpej 	struct proc *p;
    387   1.4   thorpej 	void *v;
    388   1.4   thorpej 	register_t *retval;
    389   1.4   thorpej {
    390   1.4   thorpej 	return(ENOSYS);
    391   1.4   thorpej }
    392   1.4   thorpej 
    393   1.4   thorpej int
    394   1.4   thorpej sys_ntp_adjtime(p, v, retval)
    395   1.4   thorpej 	struct proc *p;
    396   1.4   thorpej 	void *v;
    397   1.4   thorpej 	register_t *retval;
    398   1.4   thorpej {
    399   1.4   thorpej 	return(sys_nosys(p, v, retval));
    400   1.4   thorpej }
    401   1.4   thorpej 
    402   1.4   thorpej int
    403   1.4   thorpej sysctl_ntptime(where, sizep)
    404  1.11       cgd 	void *where;
    405   1.4   thorpej 	size_t *sizep;
    406   1.4   thorpej {
    407   1.4   thorpej 	return (ENOSYS);
    408   1.4   thorpej }
    409   1.4   thorpej #endif /* NTP */
    410