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