Home | History | Annotate | Line # | Download | only in kern
kern_ntptime.c revision 1.13.2.2
      1 /*	$NetBSD: kern_ntptime.c,v 1.13.2.2 2001/09/21 22:36:25 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 
    207 	if ((error = copyin((caddr_t)SCARG(uap, tp), (caddr_t)&ntv,
    208 			sizeof(ntv))) != 0)
    209 		return (error);
    210 
    211 
    212 	if (ntv.modes != 0 && (error = suser(p->p_ucred, &p->p_acflag)) != 0)
    213 		return (error);
    214 
    215 	return (ntp_adjtime1(&ntv, v, retval));
    216 }
    217 
    218 int
    219 ntp_adjtime1(ntv, v, retval)
    220 	struct timex *ntv;
    221 	void *v;
    222 	register_t	*retval;
    223 {
    224 	struct sys_ntp_adjtime_args /* {
    225 		syscallarg(struct timex *) tp;
    226 	} */ *uap = v;
    227 	int error = 0;
    228 	int modes;
    229 	int s;
    230 
    231 	/*
    232 	 * Update selected clock variables. Note that there is no error
    233 	 * checking here on the assumption the superuser should know
    234 	 * what it is doing.
    235 	 */
    236 	modes = ntv->modes;
    237 	s = splclock();
    238 	if (modes & MOD_FREQUENCY)
    239 #ifdef PPS_SYNC
    240 		time_freq = ntv->freq - pps_freq;
    241 #else /* PPS_SYNC */
    242 		time_freq = ntv->freq;
    243 #endif /* PPS_SYNC */
    244 	if (modes & MOD_MAXERROR)
    245 		time_maxerror = ntv->maxerror;
    246 	if (modes & MOD_ESTERROR)
    247 		time_esterror = ntv->esterror;
    248 	if (modes & MOD_STATUS) {
    249 		time_status &= STA_RONLY;
    250 		time_status |= ntv->status & ~STA_RONLY;
    251 	}
    252 	if (modes & MOD_TIMECONST)
    253 		time_constant = ntv->constant;
    254 	if (modes & MOD_OFFSET)
    255 		hardupdate(ntv->offset);
    256 
    257 	/*
    258 	 * Retrieve all clock variables
    259 	 */
    260 	if (time_offset < 0)
    261 		ntv->offset = -(-time_offset >> SHIFT_UPDATE);
    262 	else
    263 		ntv->offset = time_offset >> SHIFT_UPDATE;
    264 #ifdef PPS_SYNC
    265 	ntv->freq = time_freq + pps_freq;
    266 #else /* PPS_SYNC */
    267 	ntv->freq = time_freq;
    268 #endif /* PPS_SYNC */
    269 	ntv->maxerror = time_maxerror;
    270 	ntv->esterror = time_esterror;
    271 	ntv->status = time_status;
    272 	ntv->constant = time_constant;
    273 	ntv->precision = time_precision;
    274 	ntv->tolerance = time_tolerance;
    275 #ifdef PPS_SYNC
    276 	ntv->shift = pps_shift;
    277 	ntv->ppsfreq = pps_freq;
    278 	ntv->jitter = pps_jitter >> PPS_AVG;
    279 	ntv->stabil = pps_stabil;
    280 	ntv->calcnt = pps_calcnt;
    281 	ntv->errcnt = pps_errcnt;
    282 	ntv->jitcnt = pps_jitcnt;
    283 	ntv->stbcnt = pps_stbcnt;
    284 #endif /* PPS_SYNC */
    285 	(void)splx(s);
    286 
    287 	error = copyout((caddr_t)ntv, (caddr_t)SCARG(uap, tp), sizeof(*ntv));
    288 	if (!error) {
    289 
    290 		/*
    291 		 * Status word error decode. See comments in
    292 		 * ntp_gettime() routine.
    293 		 */
    294 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
    295 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
    296 		    !(time_status & STA_PPSSIGNAL)) ||
    297 		    (time_status & STA_PPSTIME &&
    298 		    time_status & STA_PPSJITTER) ||
    299 		    (time_status & STA_PPSFREQ &&
    300 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
    301 			*retval = TIME_ERROR;
    302 		else
    303 			*retval = (register_t)time_state;
    304 	}
    305 	return error;
    306 }
    307 
    308 
    309 
    310 /*
    311  * return information about kernel precision timekeeping
    312  */
    313 int
    314 sysctl_ntptime(where, sizep)
    315 	void *where;
    316 	size_t *sizep;
    317 {
    318 	struct timeval atv;
    319 	struct ntptimeval ntv;
    320 	int s;
    321 
    322 	/*
    323 	 * Construct ntp_timeval.
    324 	 */
    325 
    326 	s = splclock();
    327 #ifdef EXT_CLOCK
    328 	/*
    329 	 * The microtime() external clock routine returns a
    330 	 * status code. If less than zero, we declare an error
    331 	 * in the clock status word and return the kernel
    332 	 * (software) time variable. While there are other
    333 	 * places that call microtime(), this is the only place
    334 	 * that matters from an application point of view.
    335 	 */
    336 	if (microtime(&atv) < 0) {
    337 		time_status |= STA_CLOCKERR;
    338 		ntv.time = time;
    339 	} else {
    340 		time_status &= ~STA_CLOCKERR;
    341 	}
    342 #else /* EXT_CLOCK */
    343 	microtime(&atv);
    344 #endif /* EXT_CLOCK */
    345 	ntv.time = atv;
    346 	ntv.maxerror = time_maxerror;
    347 	ntv.esterror = time_esterror;
    348 	splx(s);
    349 
    350 #ifdef notyet
    351 	/*
    352 	 * Status word error decode. If any of these conditions
    353 	 * occur, an error is returned, instead of the status
    354 	 * word. Most applications will care only about the fact
    355 	 * the system clock may not be trusted, not about the
    356 	 * details.
    357 	 *
    358 	 * Hardware or software error
    359 	 */
    360 	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
    361 		ntv.time_state = TIME_ERROR;
    362 
    363 	/*
    364 	 * PPS signal lost when either time or frequency
    365 	 * synchronization requested
    366 	 */
    367 	   (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
    368 	    !(time_status & STA_PPSSIGNAL)) ||
    369 
    370 	/*
    371 	 * PPS jitter exceeded when time synchronization
    372 	 * requested
    373 	 */
    374 	   (time_status & STA_PPSTIME &&
    375 	    time_status & STA_PPSJITTER) ||
    376 
    377 	/*
    378 	 * PPS wander exceeded or calibration error when
    379 	 * frequency synchronization requested
    380 	 */
    381 	   (time_status & STA_PPSFREQ &&
    382 	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
    383 		ntv.time_state = TIME_ERROR;
    384 	else
    385 		ntv.time_state = time_state;
    386 #endif /* notyet */
    387 	return (sysctl_rdstruct(where, sizep, NULL, &ntv, sizeof(ntv)));
    388 }
    389 
    390 #else /* !NTP */
    391 
    392 /* For some reason, raising SIGSYS (as sys_nosys would) is problematic. */
    393 
    394 int
    395 sys_ntp_gettime(l, v, retval)
    396 	struct lwp *l;
    397 	void *v;
    398 	register_t *retval;
    399 {
    400 	return(ENOSYS);
    401 }
    402 
    403 #endif /* !NTP */
    404