/******************************************************************************
* Copyright (c) David L. Mills 1993, 1994 *
* Permission to use, copy, modify, and distribute this software and its *
* documentation for any purpose and without fee is hereby granted, provided *
* that the above copyright notice appears in all copies and that both the *
* copyright notice and this permission notice appear in supporting *
* documentation, and that the name University of Delaware not be used in *
* advertising or publicity pertaining to distribution of the software *
* without specific, written prior permission. The University of Delaware *
* makes no representations about the suitability this software for any *
* purpose. It is provided "as is" without express or implied warranty. *
******************************************************************************/
* Modification history kern_ntptime.c
* 24 Mar 94 David L. Mills
* Revised syscall interface to include new variables for PPS
* 14 Feb 94 David L. Mills
* Added code for external clock
* 28 Nov 93 David L. Mills
* Revised frequency scaling to conform with adjusted parameters
* 17 Sep 93 David L. Mills
* ntp_gettime(), ntp_adjtime() - precision time interface for SunOS
* These routines consitute the Network Time Protocol (NTP) interfaces
* for user and daemon application programs. The ntp_gettime() routine
* provides the time, maximum error (synch distance) and estimated error
* (dispersion) to client user application programs. The ntp_adjtime()
* routine is used by the NTP daemon to adjust the system clock to an
* externally derived time. The time offset and related variables set by
* this routine are used by hardclock() to adjust the phase and
* frequency of the phase-lock loop which controls the system clock.
* The following variables are used by the hardclock() routine in the
* kern_clock.c module and are described in that module.
extern struct timeval time
; /* kernel time variable */
extern int time_state
; /* clock state */
extern int time_status
; /* clock status bits */
extern long time_offset
; /* time adjustment (us) */
extern long time_freq
; /* frequency offset (scaled ppm) */
extern long time_maxerror
; /* maximum error (us) */
extern long time_esterror
; /* estimated error (us) */
extern long time_constant
; /* pll time constant */
extern long time_precision
; /* clock precision (us) */
extern long time_tolerance
; /* frequency tolerance (scaled ppm) */
* The following variables are used only if the PPS signal discipline
* is configured in the kernel.
extern int pps_shift
; /* interval duration (s) (shift) */
extern long pps_freq
; /* pps frequency offset (scaled ppm) */
extern long pps_jitter
; /* pps jitter (us) */
extern long pps_stabil
; /* pps stability (scaled ppm) */
extern long pps_jitcnt
; /* jitter limit exceeded */
extern long pps_calcnt
; /* calibration intervals */
extern long pps_errcnt
; /* calibration errors */
extern long pps_stbcnt
; /* stability limit exceeded */
* ntp_gettime() - NTP user application interface
struct ntp_gettime_args
{
ntp_gettime(struct proc
*p
, struct ntp_gettime_args
*uap
, int *retval
)
* The microtime() external clock routine returns a
* status code. If less than zero, we declare an error
* in the clock status word and return the kernel
* (software) time variable. While there are other
* places that call microtime(), this is the only place
* that matters from an application point of view.
if (microtime(&atv
) < 0) {
time_status
|= STA_CLOCKERR
;
time_status
&= ~STA_CLOCKERR
;
ntv
.maxerror
= time_maxerror
;
ntv
.esterror
= time_esterror
;
error
= copyout((caddr_t
)&ntv
, (caddr_t
)uap
->tp
,
* Status word error decode. If any of these conditions
* occur, an error is returned, instead of the status
* word. Most applications will care only about the fact
* the system clock may not be trusted, not about the
* Hardware or software error
if (time_status
& (STA_UNSYNC
| STA_CLOCKERR
))
* PPS signal lost when either time or frequency
* synchronization requested
if (time_status
& (STA_PPSFREQ
| STA_PPSTIME
) &&
!(time_status
& STA_PPSSIGNAL
))
* PPS jitter exceeded when time synchronization
if (time_status
& STA_PPSTIME
&&
time_status
& STA_PPSJITTER
)
* PPS wander exceeded or calibration error when
* frequency synchronization requested
if (time_status
& STA_PPSFREQ
&&
time_status
& (STA_PPSWANDER
| STA_PPSERROR
))
* ntp_adjtime() - NTP daemon application interface
struct ntp_adjtime_args
{
ntp_adjtime(struct proc
*p
, struct ntp_adjtime_args
*uap
, int *retval
)
error
= copyin((caddr_t
)uap
->tp
, (caddr_t
)&ntv
, sizeof(ntv
));
* Update selected clock variables - only the superuser can
* change anything. Note that there is no error checking here on
* the assumption the superuser should know what it is doing.
&& (error
= suser(p
->p_cred
->pc_ucred
, &p
->p_acflag
)))
if (modes
& MOD_FREQUENCY
)
time_freq
= ntv
.freq
- pps_freq
;
if (modes
& MOD_MAXERROR
)
time_maxerror
= ntv
.maxerror
;
if (modes
& MOD_ESTERROR
)
time_esterror
= ntv
.esterror
;
if (modes
& MOD_STATUS
) {
time_status
&= STA_RONLY
;
time_status
|= ntv
.status
& ~STA_RONLY
;
if (modes
& MOD_TIMECONST
)
time_constant
= ntv
.constant
;
* Retrieve all clock variables
ntv
.offset
= -(-time_offset
>> SHIFT_UPDATE
);
ntv
.offset
= time_offset
>> SHIFT_UPDATE
;
ntv
.freq
= time_freq
+ pps_freq
;
ntv
.maxerror
= time_maxerror
;
ntv
.esterror
= time_esterror
;
ntv
.status
= time_status
;
ntv
.constant
= time_constant
;
ntv
.precision
= time_precision
;
ntv
.tolerance
= time_tolerance
;
ntv
.jitter
= pps_jitter
>> PPS_AVG
;
error
= copyout((caddr_t
)&ntv
, (caddr_t
)uap
->tp
, sizeof(ntv
));
* Status word error decode. See comments in
if (time_status
& (STA_UNSYNC
| STA_CLOCKERR
))
if (time_status
& (STA_PPSFREQ
| STA_PPSTIME
) &&
!(time_status
& STA_PPSSIGNAL
))
if (time_status
& STA_PPSTIME
&&
time_status
& STA_PPSJITTER
)
if (time_status
& STA_PPSFREQ
&&
time_status
& (STA_PPSWANDER
| STA_PPSERROR
))