kern_ntptime.c revision 12279
1/******************************************************************************
2 *                                                                            *
3 * Copyright (c) David L. Mills 1993, 1994                                    *
4 *                                                                            *
5 * Permission to use, copy, modify, and distribute this software and its      *
6 * documentation for any purpose and without fee is hereby granted, provided  *
7 * that the above copyright notice appears in all copies and that both the    *
8 * copyright notice and this permission notice appear in supporting           *
9 * documentation, and that the name University of Delaware not be used in     *
10 * advertising or publicity pertaining to distribution of the software        *
11 * without specific, written prior permission.  The University of Delaware    *
12 * makes no representations about the suitability this software for any       *
13 * purpose.  It is provided "as is" without express or implied warranty.      *
14 *                                                                            *
15 ******************************************************************************/
16
17/*
18 * Modification history kern_ntptime.c
19 *
20 * 24 Mar 94	David L. Mills
21 *	Revised syscall interface to include new variables for PPS
22 *	time discipline.
23 *
24 * 14 Feb 94	David L. Mills
25 *	Added code for external clock
26 *
27 * 28 Nov 93	David L. Mills
28 *	Revised frequency scaling to conform with adjusted parameters
29 *
30 * 17 Sep 93	David L. Mills
31 *	Created file
32 */
33/*
34 * ntp_gettime(), ntp_adjtime() - precision time interface for SunOS
35 * 4.1.1 and 4.1.3
36 *
37 * These routines consitute the Network Time Protocol (NTP) interfaces
38 * for user and daemon application programs. The ntp_gettime() routine
39 * provides the time, maximum error (synch distance) and estimated error
40 * (dispersion) to client user application programs. The ntp_adjtime()
41 * routine is used by the NTP daemon to adjust the system clock to an
42 * externally derived time. The time offset and related variables set by
43 * this routine are used by hardclock() to adjust the phase and
44 * frequency of the phase-lock loop which controls the system clock.
45 */
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/sysproto.h>
49#include <sys/kernel.h>
50#include <sys/proc.h>
51#include <sys/timex.h>
52#include <vm/vm.h>
53#include <sys/sysctl.h>
54
55/*
56 * The following variables are used by the hardclock() routine in the
57 * kern_clock.c module and are described in that module.
58 */
59extern int time_state;		/* clock state */
60extern int time_status;		/* clock status bits */
61extern long time_offset;	/* time adjustment (us) */
62extern long time_freq;		/* frequency offset (scaled ppm) */
63extern long time_maxerror;	/* maximum error (us) */
64extern long time_esterror;	/* estimated error (us) */
65extern long time_constant;	/* pll time constant */
66extern long time_precision;	/* clock precision (us) */
67extern long time_tolerance;	/* frequency tolerance (scaled ppm) */
68
69#ifdef PPS_SYNC
70/*
71 * The following variables are used only if the PPS signal discipline
72 * is configured in the kernel.
73 */
74extern int pps_shift;		/* interval duration (s) (shift) */
75extern long pps_freq;		/* pps frequency offset (scaled ppm) */
76extern long pps_jitter;		/* pps jitter (us) */
77extern long pps_stabil;		/* pps stability (scaled ppm) */
78extern long pps_jitcnt;		/* jitter limit exceeded */
79extern long pps_calcnt;		/* calibration intervals */
80extern long pps_errcnt;		/* calibration errors */
81extern long pps_stbcnt;		/* stability limit exceeded */
82#endif /* PPS_SYNC */
83
84static int
85ntp_sysctl SYSCTL_HANDLER_ARGS
86{
87	struct timeval atv;
88	struct ntptimeval ntv;
89	int s;
90
91	s = splclock();
92#ifdef EXT_CLOCK
93	/*
94	 * The microtime() external clock routine returns a
95	 * status code. If less than zero, we declare an error
96	 * in the clock status word and return the kernel
97	 * (software) time variable. While there are other
98	 * places that call microtime(), this is the only place
99	 * that matters from an application point of view.
100	 */
101	if (microtime(&atv) < 0) {
102		time_status |= STA_CLOCKERR;
103		ntv.time = time;
104	} else {
105		time_status &= ~STA_CLOCKERR;
106	}
107#else /* EXT_CLOCK */
108	microtime(&atv);
109#endif /* EXT_CLOCK */
110	ntv.time = atv;
111	ntv.maxerror = time_maxerror;
112	ntv.esterror = time_esterror;
113	splx(s);
114
115	ntv.time_state = time_state;
116
117	/*
118	 * Status word error decode. If any of these conditions
119	 * occur, an error is returned, instead of the status
120	 * word. Most applications will care only about the fact
121	 * the system clock may not be trusted, not about the
122	 * details.
123	 *
124	 * Hardware or software error
125	 */
126	if (time_status & (STA_UNSYNC | STA_CLOCKERR)) {
127		ntv.time_state = TIME_ERROR;
128	}
129
130	/*
131	 * PPS signal lost when either time or frequency
132	 * synchronization requested
133	 */
134	if (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
135	    !(time_status & STA_PPSSIGNAL)) {
136		ntv.time_state = TIME_ERROR;
137	}
138
139	/*
140	 * PPS jitter exceeded when time synchronization
141	 * requested
142	 */
143	if (time_status & STA_PPSTIME &&
144	    time_status & STA_PPSJITTER) {
145		ntv.time_state = TIME_ERROR;
146	}
147
148	/*
149	 * PPS wander exceeded or calibration error when
150	 * frequency synchronization requested
151	 */
152	if (time_status & STA_PPSFREQ &&
153	    time_status & (STA_PPSWANDER | STA_PPSERROR)) {
154		ntv.time_state = TIME_ERROR;
155	}
156	return (sysctl_handle_opaque(oidp, &ntv, sizeof ntv, req));
157}
158
159SYSCTL_NODE(_kern, KERN_NTP_PLL, ntp_pll, CTLFLAG_RW, 0,
160	"NTP kernel PLL related stuff");
161SYSCTL_PROC(_kern_ntp_pll, NTP_PLL_GETTIME, gettime, CTLTYPE_OPAQUE|CTLFLAG_RD,
162	0, sizeof(struct ntptimeval) , ntp_sysctl, "");
163
164/*
165 * ntp_adjtime() - NTP daemon application interface
166 */
167#ifndef _SYS_SYSPROTO_H_
168struct ntp_adjtime_args {
169  struct timex *tp;
170};
171#endif
172
173int
174ntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap, int *retval)
175{
176	struct timex ntv;
177	int modes;
178	int s;
179	int error;
180
181	error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv));
182	if (error)
183		return error;
184
185	/*
186	 * Update selected clock variables - only the superuser can
187	 * change anything. Note that there is no error checking here on
188	 * the assumption the superuser should know what it is doing.
189	 */
190	modes = ntv.modes;
191	if ((modes != 0)
192	    && (error = suser(p->p_cred->pc_ucred, &p->p_acflag)))
193		return error;
194
195	s = splclock();
196	if (modes & MOD_FREQUENCY)
197#ifdef PPS_SYNC
198		time_freq = ntv.freq - pps_freq;
199#else /* PPS_SYNC */
200		time_freq = ntv.freq;
201#endif /* PPS_SYNC */
202	if (modes & MOD_MAXERROR)
203		time_maxerror = ntv.maxerror;
204	if (modes & MOD_ESTERROR)
205		time_esterror = ntv.esterror;
206	if (modes & MOD_STATUS) {
207		time_status &= STA_RONLY;
208		time_status |= ntv.status & ~STA_RONLY;
209	}
210	if (modes & MOD_TIMECONST)
211		time_constant = ntv.constant;
212	if (modes & MOD_OFFSET)
213		hardupdate(ntv.offset);
214
215	/*
216	 * Retrieve all clock variables
217	 */
218	if (time_offset < 0)
219		ntv.offset = -(-time_offset >> SHIFT_UPDATE);
220	else
221		ntv.offset = time_offset >> SHIFT_UPDATE;
222#ifdef PPS_SYNC
223	ntv.freq = time_freq + pps_freq;
224#else /* PPS_SYNC */
225	ntv.freq = time_freq;
226#endif /* PPS_SYNC */
227	ntv.maxerror = time_maxerror;
228	ntv.esterror = time_esterror;
229	ntv.status = time_status;
230	ntv.constant = time_constant;
231	ntv.precision = time_precision;
232	ntv.tolerance = time_tolerance;
233#ifdef PPS_SYNC
234	ntv.shift = pps_shift;
235	ntv.ppsfreq = pps_freq;
236	ntv.jitter = pps_jitter >> PPS_AVG;
237	ntv.stabil = pps_stabil;
238	ntv.calcnt = pps_calcnt;
239	ntv.errcnt = pps_errcnt;
240	ntv.jitcnt = pps_jitcnt;
241	ntv.stbcnt = pps_stbcnt;
242#endif /* PPS_SYNC */
243	(void)splx(s);
244
245	error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof(ntv));
246	if (!error) {
247		/*
248		 * Status word error decode. See comments in
249		 * ntp_gettime() routine.
250		 */
251		retval[0] = time_state;
252		if (time_status & (STA_UNSYNC | STA_CLOCKERR))
253			retval[0] = TIME_ERROR;
254		if (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
255		    !(time_status & STA_PPSSIGNAL))
256			retval[0] = TIME_ERROR;
257		if (time_status & STA_PPSTIME &&
258		    time_status & STA_PPSJITTER)
259			retval[0] = TIME_ERROR;
260		if (time_status & STA_PPSFREQ &&
261		    time_status & (STA_PPSWANDER | STA_PPSERROR))
262			retval[0] = TIME_ERROR;
263	}
264	return error;
265}
266
267
268