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