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