Home | History | Annotate | Line # | Download | only in kern
kern_ntptime.c revision 1.31
      1 /*	$NetBSD: kern_ntptime.c,v 1.31 2006/05/29 09:57:54 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.31 2006/05/29 09:57:54 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 static void ntp_gettime(struct ntptimeval *);
    109 static int ntp_timestatus(void);
    110 
    111 /*ARGSUSED*/
    112 /*
    113  * ntp_gettime() - NTP user application interface
    114  */
    115 static void
    116 ntp_gettime(ntvp)
    117 	struct ntptimeval *ntvp;
    118 {
    119 	struct timeval atv;
    120 	int s;
    121 
    122 	memset(ntvp, 0, sizeof(struct ntptimeval));
    123 
    124 	s = splclock();
    125 #ifdef EXT_CLOCK
    126 	/*
    127 	 * The microtime() external clock routine returns a
    128 	 * status code. If less than zero, we declare an error
    129 	 * in the clock status word and return the kernel
    130 	 * (software) time variable. While there are other
    131 	 * places that call microtime(), this is the only place
    132 	 * that matters from an application point of view.
    133 	 */
    134 	if (microtime(&atv) < 0) {
    135 		time_status |= STA_CLOCKERR;
    136 		ntvp->time = time;
    137 	} else
    138 		time_status &= ~STA_CLOCKERR;
    139 #else /* EXT_CLOCK */
    140 	microtime(&atv);
    141 #endif /* EXT_CLOCK */
    142 	ntvp->maxerror = time_maxerror;
    143 	ntvp->esterror = time_esterror;
    144 	(void) splx(s);
    145 	TIMEVAL_TO_TIMESPEC(&atv, &ntvp->time);
    146 }
    147 
    148 static int
    149 ntp_timestatus()
    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 		return (TIME_ERROR);
    183 	else
    184 		return ((register_t)time_state);
    185 }
    186 
    187 int
    188 sys___ntp_gettime30(l, v, retval)
    189 	struct lwp *l;
    190 	void *v;
    191 	register_t *retval;
    192 {
    193 	struct sys___ntp_gettime30_args /* {
    194 		syscallarg(struct ntptimeval *) ntvp;
    195 	} */ *uap = v;
    196 	struct ntptimeval ntv;
    197 	int error = 0;
    198 
    199 	if (SCARG(uap, ntvp)) {
    200 		ntp_gettime(&ntv);
    201 
    202 		error = copyout((caddr_t)&ntv, (caddr_t)SCARG(uap, ntvp),
    203 				sizeof(ntv));
    204 	}
    205 	if (!error)
    206 		*retval = ntp_timestatus();
    207 
    208 	return (error);
    209 }
    210 
    211 #ifdef COMPAT_30
    212 int
    213 compat_30_sys_ntp_gettime(l, v, retval)
    214 	struct lwp *l;
    215 	void *v;
    216 	register_t *retval;
    217 {
    218 	struct compat_30_sys_ntp_gettime_args /* {
    219 		syscallarg(struct ntptimeval30 *) ontvp;
    220 	} */ *uap = v;
    221 	struct ntptimeval ntv;
    222 	struct ntptimeval30 ontv;
    223 	int error = 0;
    224 
    225 	if (SCARG(uap, ntvp)) {
    226 		ntp_gettime(&ntv);
    227 
    228 		TIMESPEC_TO_TIMEVAL(&ontv.time, &ntv.time);
    229 		ontv.maxerror = ntv.maxerror;
    230 		ontv.esterror = ntv.esterror;
    231 
    232 		error = copyout((caddr_t)&ontv, (caddr_t)SCARG(uap, ntvp),
    233 				sizeof(ontv));
    234 	}
    235 	if (!error)
    236 		*retval = ntp_timestatus();
    237 
    238 	return (error);
    239 }
    240 #endif
    241 
    242 /* ARGSUSED */
    243 /*
    244  * ntp_adjtime() - NTP daemon application interface
    245  */
    246 int
    247 sys_ntp_adjtime(l, v, retval)
    248 	struct lwp *l;
    249 	void *v;
    250 	register_t *retval;
    251 {
    252 	struct sys_ntp_adjtime_args /* {
    253 		syscallarg(struct timex *) tp;
    254 	} */ *uap = v;
    255 	struct proc *p = l->l_proc;
    256 	struct timex ntv;
    257 	int error = 0;
    258 
    259 	if ((error = copyin((caddr_t)SCARG(uap, tp), (caddr_t)&ntv,
    260 			sizeof(ntv))) != 0)
    261 		return (error);
    262 
    263 	if (ntv.modes != 0 && (error = kauth_authorize_generic(p->p_cred,
    264 				KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
    265 		return (error);
    266 
    267 	return (ntp_adjtime1(&ntv, v, retval));
    268 }
    269 
    270 int
    271 ntp_adjtime1(ntv, v, retval)
    272 	struct timex *ntv;
    273 	void *v;
    274 	register_t	*retval;
    275 {
    276 	struct sys_ntp_adjtime_args /* {
    277 		syscallarg(struct timex *) tp;
    278 	} */ *uap = v;
    279 	int error = 0;
    280 	int modes;
    281 	int s;
    282 
    283 	/*
    284 	 * Update selected clock variables. Note that there is no error
    285 	 * checking here on the assumption the superuser should know
    286 	 * what it is doing.
    287 	 */
    288 	modes = ntv->modes;
    289 	if (modes != 0)
    290 		/* We need to save the system time during shutdown */
    291 		time_adjusted |= 2;
    292 	s = splclock();
    293 	if (modes & MOD_FREQUENCY)
    294 #ifdef PPS_SYNC
    295 		time_freq = ntv->freq - pps_freq;
    296 #else /* PPS_SYNC */
    297 		time_freq = ntv->freq;
    298 #endif /* PPS_SYNC */
    299 	if (modes & MOD_MAXERROR)
    300 		time_maxerror = ntv->maxerror;
    301 	if (modes & MOD_ESTERROR)
    302 		time_esterror = ntv->esterror;
    303 	if (modes & MOD_STATUS) {
    304 		time_status &= STA_RONLY;
    305 		time_status |= ntv->status & ~STA_RONLY;
    306 	}
    307 	if (modes & MOD_TIMECONST)
    308 		time_constant = ntv->constant;
    309 	if (modes & MOD_OFFSET)
    310 		hardupdate(ntv->offset);
    311 
    312 	/*
    313 	 * Retrieve all clock variables
    314 	 */
    315 	if (time_offset < 0)
    316 		ntv->offset = -(-time_offset >> SHIFT_UPDATE);
    317 	else
    318 		ntv->offset = time_offset >> SHIFT_UPDATE;
    319 #ifdef PPS_SYNC
    320 	ntv->freq = time_freq + pps_freq;
    321 #else /* PPS_SYNC */
    322 	ntv->freq = time_freq;
    323 #endif /* PPS_SYNC */
    324 	ntv->maxerror = time_maxerror;
    325 	ntv->esterror = time_esterror;
    326 	ntv->status = time_status;
    327 	ntv->constant = time_constant;
    328 	ntv->precision = time_precision;
    329 	ntv->tolerance = time_tolerance;
    330 #ifdef PPS_SYNC
    331 	ntv->shift = pps_shift;
    332 	ntv->ppsfreq = pps_freq;
    333 	ntv->jitter = pps_jitter >> PPS_AVG;
    334 	ntv->stabil = pps_stabil;
    335 	ntv->calcnt = pps_calcnt;
    336 	ntv->errcnt = pps_errcnt;
    337 	ntv->jitcnt = pps_jitcnt;
    338 	ntv->stbcnt = pps_stbcnt;
    339 #endif /* PPS_SYNC */
    340 	(void)splx(s);
    341 
    342 	error = copyout((caddr_t)ntv, (caddr_t)SCARG(uap, tp), sizeof(*ntv));
    343 	if (!error) {
    344 
    345 		/*
    346 		 * Status word error decode. See comments in
    347 		 * ntp_gettime() routine.
    348 		 */
    349 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
    350 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
    351 		    !(time_status & STA_PPSSIGNAL)) ||
    352 		    (time_status & STA_PPSTIME &&
    353 		    time_status & STA_PPSJITTER) ||
    354 		    (time_status & STA_PPSFREQ &&
    355 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
    356 			*retval = TIME_ERROR;
    357 		else
    358 			*retval = (register_t)time_state;
    359 	}
    360 	return error;
    361 }
    362 
    363 /*
    364  * return information about kernel precision timekeeping
    365  */
    366 static int
    367 sysctl_kern_ntptime(SYSCTLFN_ARGS)
    368 {
    369 	struct sysctlnode node;
    370 	struct ntptimeval ntv;
    371 
    372 	ntp_gettime(&ntv);
    373 
    374 	node = *rnode;
    375 	node.sysctl_data = &ntv;
    376 	node.sysctl_size = sizeof(ntv);
    377 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    378 }
    379 
    380 SYSCTL_SETUP(sysctl_kern_ntptime_setup, "sysctl kern.ntptime node setup")
    381 {
    382 
    383 	sysctl_createv(clog, 0, NULL, NULL,
    384 		       CTLFLAG_PERMANENT,
    385 		       CTLTYPE_NODE, "kern", NULL,
    386 		       NULL, 0, NULL, 0,
    387 		       CTL_KERN, CTL_EOL);
    388 
    389 	sysctl_createv(clog, 0, NULL, NULL,
    390 		       CTLFLAG_PERMANENT,
    391 		       CTLTYPE_STRUCT, "ntptime",
    392 		       SYSCTL_DESCR("Kernel clock values for NTP"),
    393 		       sysctl_kern_ntptime, 0, NULL,
    394 		       sizeof(struct ntptimeval),
    395 		       CTL_KERN, KERN_NTPTIME, CTL_EOL);
    396 }
    397 #else /* !NTP */
    398 /* For some reason, raising SIGSYS (as sys_nosys would) is problematic. */
    399 
    400 int
    401 sys___ntp_gettime30(l, v, retval)
    402 	struct lwp *l;
    403 	void *v;
    404 	register_t *retval;
    405 {
    406 
    407 	return(ENOSYS);
    408 }
    409 
    410 #ifdef COMPAT_30
    411 int
    412 compat_30_sys_ntp_gettime(l, v, retval)
    413 	struct lwp *l;
    414 	void *v;
    415 	register_t *retval;
    416 {
    417 
    418 	return(ENOSYS);
    419 }
    420 #endif
    421 #endif /* !NTP */
    422