kern_ntptime.c revision 2858
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/kernel.h>
49#include <sys/proc.h>
50#include <sys/timex.h>
51#include <sys/sysctl.h>
52
53/*
54 * The following variables are used by the hardclock() routine in the
55 * kern_clock.c module and are described in that module.
56 */
57extern struct timeval time;	/* kernel time variable */
58extern int time_state;		/* clock state */
59extern int time_status;		/* clock status bits */
60extern long time_offset;	/* time adjustment (us) */
61extern long time_freq;		/* frequency offset (scaled ppm) */
62extern long time_maxerror;	/* maximum error (us) */
63extern long time_esterror;	/* estimated error (us) */
64extern long time_constant;	/* pll time constant */
65extern long time_precision;	/* clock precision (us) */
66extern long time_tolerance;	/* frequency tolerance (scaled ppm) */
67
68#ifdef PPS_SYNC
69/*
70 * The following variables are used only if the PPS signal discipline
71 * is configured in the kernel.
72 */
73extern int pps_shift;		/* interval duration (s) (shift) */
74extern long pps_freq;		/* pps frequency offset (scaled ppm) */
75extern long pps_jitter;		/* pps jitter (us) */
76extern long pps_stabil;		/* pps stability (scaled ppm) */
77extern long pps_jitcnt;		/* jitter limit exceeded */
78extern long pps_calcnt;		/* calibration intervals */
79extern long pps_errcnt;		/* calibration errors */
80extern long pps_stbcnt;		/* stability limit exceeded */
81#endif /* PPS_SYNC */
82
83int
84ntp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
85	   void *newp, size_t newlen, struct proc *p)
86{
87	struct timeval atv;
88	struct ntptimeval ntv;
89	int s;
90
91	/* All names at this level are terminal. */
92	if (namelen != 1) {
93		return ENOTDIR;
94	}
95
96	if (name[0] != NTP_PLL_GETTIME) {
97		return EOPNOTSUPP;
98	}
99
100	s = splclock();
101#ifdef EXT_CLOCK
102	/*
103	 * The microtime() external clock routine returns a
104	 * status code. If less than zero, we declare an error
105	 * in the clock status word and return the kernel
106	 * (software) time variable. While there are other
107	 * places that call microtime(), this is the only place
108	 * that matters from an application point of view.
109	 */
110	if (microtime(&atv) < 0) {
111		time_status |= STA_CLOCKERR;
112		ntv.time = time;
113	} else {
114		time_status &= ~STA_CLOCKERR;
115	}
116#else /* EXT_CLOCK */
117	microtime(&atv);
118#endif /* EXT_CLOCK */
119	ntv.time = atv;
120	ntv.maxerror = time_maxerror;
121	ntv.esterror = time_esterror;
122	splx(s);
123
124	ntv.time_state = time_state;
125
126	/*
127	 * Status word error decode. If any of these conditions
128	 * occur, an error is returned, instead of the status
129	 * word. Most applications will care only about the fact
130	 * the system clock may not be trusted, not about the
131	 * details.
132	 *
133	 * Hardware or software error
134	 */
135	if (time_status & (STA_UNSYNC | STA_CLOCKERR)) {
136		ntv.time_state = TIME_ERROR;
137	}
138
139	/*
140	 * PPS signal lost when either time or frequency
141	 * synchronization requested
142	 */
143	if (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
144	    !(time_status & STA_PPSSIGNAL)) {
145		ntv.time_state = TIME_ERROR;
146	}
147
148	/*
149	 * PPS jitter exceeded when time synchronization
150	 * requested
151	 */
152	if (time_status & STA_PPSTIME &&
153	    time_status & STA_PPSJITTER) {
154		ntv.time_state = TIME_ERROR;
155	}
156
157	/*
158	 * PPS wander exceeded or calibration error when
159	 * frequency synchronization requested
160	 */
161	if (time_status & STA_PPSFREQ &&
162	    time_status & (STA_PPSWANDER | STA_PPSERROR)) {
163		ntv.time_state = TIME_ERROR;
164	}
165	return(sysctl_rdstruct(oldp, oldlenp, newp, &ntv, sizeof ntv));
166}
167
168/*
169 * ntp_adjtime() - NTP daemon application interface
170 */
171struct ntp_adjtime_args {
172  struct timex *tp;
173};
174
175int
176ntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap, int *retval)
177{
178	struct timex ntv;
179	int modes;
180	int s;
181	int error;
182
183	error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv));
184	if (error)
185		return error;
186
187	/*
188	 * Update selected clock variables - only the superuser can
189	 * change anything. Note that there is no error checking here on
190	 * the assumption the superuser should know what it is doing.
191	 */
192	modes = ntv.modes;
193	if ((modes != 0)
194	    && (error = suser(p->p_cred->pc_ucred, &p->p_acflag)))
195		return error;
196
197	s = splclock();
198	if (modes & MOD_FREQUENCY)
199#ifdef PPS_SYNC
200		time_freq = ntv.freq - pps_freq;
201#else /* PPS_SYNC */
202		time_freq = ntv.freq;
203#endif /* PPS_SYNC */
204	if (modes & MOD_MAXERROR)
205		time_maxerror = ntv.maxerror;
206	if (modes & MOD_ESTERROR)
207		time_esterror = ntv.esterror;
208	if (modes & MOD_STATUS) {
209		time_status &= STA_RONLY;
210		time_status |= ntv.status & ~STA_RONLY;
211	}
212	if (modes & MOD_TIMECONST)
213		time_constant = ntv.constant;
214	if (modes & MOD_OFFSET)
215		hardupdate(ntv.offset);
216
217	/*
218	 * Retrieve all clock variables
219	 */
220	if (time_offset < 0)
221		ntv.offset = -(-time_offset >> SHIFT_UPDATE);
222	else
223		ntv.offset = time_offset >> SHIFT_UPDATE;
224#ifdef PPS_SYNC
225	ntv.freq = time_freq + pps_freq;
226#else /* PPS_SYNC */
227	ntv.freq = time_freq;
228#endif /* PPS_SYNC */
229	ntv.maxerror = time_maxerror;
230	ntv.esterror = time_esterror;
231	ntv.status = time_status;
232	ntv.constant = time_constant;
233	ntv.precision = time_precision;
234	ntv.tolerance = time_tolerance;
235#ifdef PPS_SYNC
236	ntv.shift = pps_shift;
237	ntv.ppsfreq = pps_freq;
238	ntv.jitter = pps_jitter >> PPS_AVG;
239	ntv.stabil = pps_stabil;
240	ntv.calcnt = pps_calcnt;
241	ntv.errcnt = pps_errcnt;
242	ntv.jitcnt = pps_jitcnt;
243	ntv.stbcnt = pps_stbcnt;
244#endif /* PPS_SYNC */
245	(void)splx(s);
246
247	error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof(ntv));
248	if (!error) {
249		/*
250		 * Status word error decode. See comments in
251		 * ntp_gettime() routine.
252		 */
253		retval[0] = time_state;
254		if (time_status & (STA_UNSYNC | STA_CLOCKERR))
255			retval[0] = TIME_ERROR;
256		if (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
257		    !(time_status & STA_PPSSIGNAL))
258			retval[0] = TIME_ERROR;
259		if (time_status & STA_PPSTIME &&
260		    time_status & STA_PPSJITTER)
261			retval[0] = TIME_ERROR;
262		if (time_status & STA_PPSFREQ &&
263		    time_status & (STA_PPSWANDER | STA_PPSERROR))
264			retval[0] = TIME_ERROR;
265	}
266	return error;
267}
268
269
270