Home | History | Annotate | Line # | Download | only in kern
kern_ntptime.c revision 1.13.2.1
      1 /*	$NetBSD: kern_ntptime.c,v 1.13.2.1 2001/03/05 22:49:41 nathanw 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 "opt_ntp.h"
     52 
     53 #include <sys/param.h>
     54 #include <sys/resourcevar.h>
     55 #include <sys/systm.h>
     56 #include <sys/kernel.h>
     57 #include <sys/lwp.h>
     58 #include <sys/proc.h>
     59 #include <sys/timex.h>
     60 #include <sys/vnode.h>
     61 
     62 #include <sys/mount.h>
     63 #include <sys/syscallargs.h>
     64 
     65 #include <machine/cpu.h>
     66 
     67 #include <uvm/uvm_extern.h>
     68 #include <sys/sysctl.h>
     69 
     70 #ifdef NTP
     71 
     72 /*
     73  * The following variables are used by the hardclock() routine in the
     74  * kern_clock.c module and are described in that module.
     75  */
     76 extern int time_state;		/* clock state */
     77 extern int time_status;		/* clock status bits */
     78 extern long time_offset;	/* time adjustment (us) */
     79 extern long time_freq;		/* frequency offset (scaled ppm) */
     80 extern long time_maxerror;	/* maximum error (us) */
     81 extern long time_esterror;	/* estimated error (us) */
     82 extern long time_constant;	/* pll time constant */
     83 extern long time_precision;	/* clock precision (us) */
     84 extern long time_tolerance;	/* frequency tolerance (scaled ppm) */
     85 
     86 #ifdef PPS_SYNC
     87 /*
     88  * The following variables are used only if the PPS signal discipline
     89  * is configured in the kernel.
     90  */
     91 extern int pps_shift;		/* interval duration (s) (shift) */
     92 extern long pps_freq;		/* pps frequency offset (scaled ppm) */
     93 extern long pps_jitter;		/* pps jitter (us) */
     94 extern long pps_stabil;		/* pps stability (scaled ppm) */
     95 extern long pps_jitcnt;		/* jitter limit exceeded */
     96 extern long pps_calcnt;		/* calibration intervals */
     97 extern long pps_errcnt;		/* calibration errors */
     98 extern long pps_stbcnt;		/* stability limit exceeded */
     99 #endif /* PPS_SYNC */
    100 
    101 
    102 
    103 /*ARGSUSED*/
    104 /*
    105  * ntp_gettime() - NTP user application interface
    106  */
    107 int
    108 sys_ntp_gettime(l, v, retval)
    109 	struct lwp *l;
    110 	void *v;
    111 	register_t *retval;
    112 
    113 {
    114 	struct sys_ntp_gettime_args /* {
    115 		syscallarg(struct ntptimeval *) ntvp;
    116 	} */ *uap = v;
    117 	struct timeval atv;
    118 	struct ntptimeval ntv;
    119 	int error = 0;
    120 	int s;
    121 
    122 	if (SCARG(uap, ntvp)) {
    123 		s = splclock();
    124 #ifdef EXT_CLOCK
    125 		/*
    126 		 * The microtime() external clock routine returns a
    127 		 * status code. If less than zero, we declare an error
    128 		 * in the clock status word and return the kernel
    129 		 * (software) time variable. While there are other
    130 		 * places that call microtime(), this is the only place
    131 		 * that matters from an application point of view.
    132 		 */
    133 		if (microtime(&atv) < 0) {
    134 			time_status |= STA_CLOCKERR;
    135 			ntv.time = time;
    136 		} else
    137 			time_status &= ~STA_CLOCKERR;
    138 #else /* EXT_CLOCK */
    139 		microtime(&atv);
    140 #endif /* EXT_CLOCK */
    141 		ntv.time = atv;
    142 		ntv.maxerror = time_maxerror;
    143 		ntv.esterror = time_esterror;
    144 		(void) splx(s);
    145 
    146 		error = copyout((caddr_t)&ntv, (caddr_t)SCARG(uap, ntvp),
    147 		    sizeof(ntv));
    148 	}
    149 	if (!error) {
    150 
    151 		/*
    152 		 * Status word error decode. If any of these conditions
    153 		 * occur, an error is returned, instead of the status
    154 		 * word. Most applications will care only about the fact
    155 		 * the system clock may not be trusted, not about the
    156 		 * details.
    157 		 *
    158 		 * Hardware or software error
    159 		 */
    160 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
    161 
    162 		/*
    163 		 * PPS signal lost when either time or frequency
    164 		 * synchronization requested
    165 		 */
    166 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
    167 		    !(time_status & STA_PPSSIGNAL)) ||
    168 
    169 		/*
    170 		 * PPS jitter exceeded when time synchronization
    171 		 * requested
    172 		 */
    173 		    (time_status & STA_PPSTIME &&
    174 		    time_status & STA_PPSJITTER) ||
    175 
    176 		/*
    177 		 * PPS wander exceeded or calibration error when
    178 		 * frequency synchronization requested
    179 		 */
    180 		    (time_status & STA_PPSFREQ &&
    181 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
    182 			*retval = TIME_ERROR;
    183 		else
    184 			*retval = (register_t)time_state;
    185 	}
    186 	return(error);
    187 }
    188 
    189 
    190 /* ARGSUSED */
    191 /*
    192  * ntp_adjtime() - NTP daemon application interface
    193  */
    194 int
    195 sys_ntp_adjtime(l, v, retval)
    196 	struct lwp *l;
    197 	void *v;
    198 	register_t *retval;
    199 {
    200 	struct sys_ntp_adjtime_args /* {
    201 		syscallarg(struct timex *) tp;
    202 	} */ *uap = v;
    203 	struct proc *p = l->l_proc;
    204 	struct timex ntv;
    205 	int error = 0;
    206 	int modes;
    207 	int s;
    208 
    209 	if ((error = copyin((caddr_t)SCARG(uap, tp), (caddr_t)&ntv,
    210 			sizeof(ntv))))
    211 		return (error);
    212 
    213 	/*
    214 	 * Update selected clock variables - only the superuser can
    215 	 * change anything. Note that there is no error checking here on
    216 	 * the assumption the superuser should know what it is doing.
    217 	 */
    218 	modes = ntv.modes;
    219 	if (modes != 0 && (error = suser(p->p_ucred, &p->p_acflag)))
    220 		return (error);
    221 
    222 	s = splclock();
    223 	if (modes & MOD_FREQUENCY)
    224 #ifdef PPS_SYNC
    225 		time_freq = ntv.freq - pps_freq;
    226 #else /* PPS_SYNC */
    227 		time_freq = ntv.freq;
    228 #endif /* PPS_SYNC */
    229 	if (modes & MOD_MAXERROR)
    230 		time_maxerror = ntv.maxerror;
    231 	if (modes & MOD_ESTERROR)
    232 		time_esterror = ntv.esterror;
    233 	if (modes & MOD_STATUS) {
    234 		time_status &= STA_RONLY;
    235 		time_status |= ntv.status & ~STA_RONLY;
    236 	}
    237 	if (modes & MOD_TIMECONST)
    238 		time_constant = ntv.constant;
    239 	if (modes & MOD_OFFSET)
    240 		hardupdate(ntv.offset);
    241 
    242 	/*
    243 	 * Retrieve all clock variables
    244 	 */
    245 	if (time_offset < 0)
    246 		ntv.offset = -(-time_offset >> SHIFT_UPDATE);
    247 	else
    248 		ntv.offset = time_offset >> SHIFT_UPDATE;
    249 #ifdef PPS_SYNC
    250 	ntv.freq = time_freq + pps_freq;
    251 #else /* PPS_SYNC */
    252 	ntv.freq = time_freq;
    253 #endif /* PPS_SYNC */
    254 	ntv.maxerror = time_maxerror;
    255 	ntv.esterror = time_esterror;
    256 	ntv.status = time_status;
    257 	ntv.constant = time_constant;
    258 	ntv.precision = time_precision;
    259 	ntv.tolerance = time_tolerance;
    260 #ifdef PPS_SYNC
    261 	ntv.shift = pps_shift;
    262 	ntv.ppsfreq = pps_freq;
    263 	ntv.jitter = pps_jitter >> PPS_AVG;
    264 	ntv.stabil = pps_stabil;
    265 	ntv.calcnt = pps_calcnt;
    266 	ntv.errcnt = pps_errcnt;
    267 	ntv.jitcnt = pps_jitcnt;
    268 	ntv.stbcnt = pps_stbcnt;
    269 #endif /* PPS_SYNC */
    270 	(void)splx(s);
    271 
    272 	error = copyout((caddr_t)&ntv, (caddr_t)SCARG(uap, tp), sizeof(ntv));
    273 	if (!error) {
    274 
    275 		/*
    276 		 * Status word error decode. See comments in
    277 		 * ntp_gettime() routine.
    278 		 */
    279 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
    280 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
    281 		    !(time_status & STA_PPSSIGNAL)) ||
    282 		    (time_status & STA_PPSTIME &&
    283 		    time_status & STA_PPSJITTER) ||
    284 		    (time_status & STA_PPSFREQ &&
    285 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
    286 			*retval = TIME_ERROR;
    287 		else
    288 			*retval = (register_t)time_state;
    289 	}
    290 	return error;
    291 }
    292 
    293 
    294 
    295 /*
    296  * return information about kernel precision timekeeping
    297  */
    298 int
    299 sysctl_ntptime(where, sizep)
    300 	void *where;
    301 	size_t *sizep;
    302 {
    303 	struct timeval atv;
    304 	struct ntptimeval ntv;
    305 	int s;
    306 
    307 	/*
    308 	 * Construct ntp_timeval.
    309 	 */
    310 
    311 	s = splclock();
    312 #ifdef EXT_CLOCK
    313 	/*
    314 	 * The microtime() external clock routine returns a
    315 	 * status code. If less than zero, we declare an error
    316 	 * in the clock status word and return the kernel
    317 	 * (software) time variable. While there are other
    318 	 * places that call microtime(), this is the only place
    319 	 * that matters from an application point of view.
    320 	 */
    321 	if (microtime(&atv) < 0) {
    322 		time_status |= STA_CLOCKERR;
    323 		ntv.time = time;
    324 	} else {
    325 		time_status &= ~STA_CLOCKERR;
    326 	}
    327 #else /* EXT_CLOCK */
    328 	microtime(&atv);
    329 #endif /* EXT_CLOCK */
    330 	ntv.time = atv;
    331 	ntv.maxerror = time_maxerror;
    332 	ntv.esterror = time_esterror;
    333 	splx(s);
    334 
    335 #ifdef notyet
    336 	/*
    337 	 * Status word error decode. If any of these conditions
    338 	 * occur, an error is returned, instead of the status
    339 	 * word. Most applications will care only about the fact
    340 	 * the system clock may not be trusted, not about the
    341 	 * details.
    342 	 *
    343 	 * Hardware or software error
    344 	 */
    345 	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
    346 		ntv.time_state = TIME_ERROR;
    347 
    348 	/*
    349 	 * PPS signal lost when either time or frequency
    350 	 * synchronization requested
    351 	 */
    352 	   (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
    353 	    !(time_status & STA_PPSSIGNAL)) ||
    354 
    355 	/*
    356 	 * PPS jitter exceeded when time synchronization
    357 	 * requested
    358 	 */
    359 	   (time_status & STA_PPSTIME &&
    360 	    time_status & STA_PPSJITTER) ||
    361 
    362 	/*
    363 	 * PPS wander exceeded or calibration error when
    364 	 * frequency synchronization requested
    365 	 */
    366 	   (time_status & STA_PPSFREQ &&
    367 	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
    368 		ntv.time_state = TIME_ERROR;
    369 	else
    370 		ntv.time_state = time_state;
    371 #endif /* notyet */
    372 	return (sysctl_rdstruct(where, sizep, NULL, &ntv, sizeof(ntv)));
    373 }
    374 
    375 #else /* !NTP */
    376 
    377 /* For some reason, raising SIGSYS (as sys_nosys would) is problematic. */
    378 
    379 int
    380 sys_ntp_gettime(l, v, retval)
    381 	struct lwp *l;
    382 	void *v;
    383 	register_t *retval;
    384 {
    385 	return(ENOSYS);
    386 }
    387 
    388 #endif /* !NTP */
    389