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