2 * ntp_loopfilter.c - implements the NTP loop filter algorithm
4 * ATTENTION: Get approval from Dave Mills on all changes to this file!
13 #include "ntp_unixtime.h"
14 #include "ntp_stdlib.h"
22 #if defined(VMS) && defined(VMS_LOCALUNIT) /*wjm*/
23 #include "ntp_refclock.h"
27 #include "ntp_syscall.h"
28 #endif /* KERNEL_PLL */
31 * This is an implementation of the clock discipline algorithm described
32 * in UDel TR 97-4-3, as amended. It operates as an adaptive parameter,
33 * hybrid phase/frequency-lock loop. A number of sanity checks are
34 * included to protect against timewarps, timespikes and general mayhem.
35 * All units are in s and s/s, unless noted otherwise.
37 #define CLOCK_MAX .128 /* default step threshold (s) */
38 #define CLOCK_MINSTEP 900. /* default stepout threshold (s) */
39 #define CLOCK_PANIC 1000. /* default panic threshold (s) */
40 #define CLOCK_PHI 15e-6 /* max frequency error (s/s) */
41 #define CLOCK_PLL 16. /* PLL loop gain */
42 #define CLOCK_FLL 8. /* FLL loop gain */
43 #define CLOCK_AVG 4. /* parameter averaging constant */
44 #define CLOCK_ALLAN 1500. /* compromise Allan intercept (s) */
45 #define CLOCK_DAY 86400. /* one day in seconds (s) */
46 #define CLOCK_LIMIT 30 /* poll-adjust threshold */
47 #define CLOCK_PGATE 4. /* poll-adjust gate */
48 #define PPS_MAXAGE 120 /* kernel pps signal timeout (s) */
51 * Clock discipline state machine. This is used to control the
52 * synchronization behavior during initialization and following a
55 * State < max > max Comments
56 * ====================================================
57 * NSET FREQ FREQ no ntp.drift
59 * FSET TSET if (allow) TSET, ntp.drift
62 * TSET SYNC FREQ time set
64 * FREQ SYNC if (mu < 900) FREQ calculate frequency
65 * else if (allow) TSET
68 * SYNC SYNC if (mu < 900) SYNC normal state
71 * SPIK SYNC if (allow) TSET spike detector
74 #define S_NSET 0 /* clock never set */
75 #define S_FSET 1 /* frequency set from the drift file */
76 #define S_TSET 2 /* time set */
77 #define S_FREQ 3 /* frequency mode */
78 #define S_SYNC 4 /* clock synchronized */
79 #define S_SPIK 5 /* spike detected */
82 * Kernel PLL/PPS state machine. This is used with the kernel PLL
83 * modifications described in the README.kernel file.
85 * If kernel support for the ntp_adjtime() system call is available, the
86 * ntp_control flag is set. The ntp_enable and kern_enable flags can be
87 * set at configuration time or run time using ntpdc. If ntp_enable is
88 * false, the discipline loop is unlocked and no correctios of any kind
89 * are made. If both ntp_control and kern_enable are set, the kernel
90 * support is used as described above; if false, the kernel is bypassed
91 * entirely and the daemon PLL used instead.
93 * Each update to a prefer peer sets pps_stratum if it survives the
94 * intersection algorithm and its time is within range. The PPS time
95 * discipline is enabled (STA_PPSTIME bit set in the status word) when
96 * pps_stratum is true and the PPS frequency discipline is enabled. If
97 * the PPS time discipline is enabled and the kernel reports a PPS
98 * signal is present, the pps_control variable is set to the current
99 * time. If the current time is later than pps_control by PPS_MAXAGE
100 * (120 s), this variable is set to zero.
102 * If an external clock is present, the clock driver sets STA_CLK in the
103 * status word. When the local clock driver sees this bit, it updates
104 * via this routine, which then calls ntp_adjtime() with the STA_PLL bit
105 * set to zero, in which case the system clock is not adjusted. This is
106 * also a signal for the external clock driver to discipline the system
110 * Program variables that can be tinkered.
112 double clock_max = CLOCK_MAX; /* step threshold (s) */
113 double clock_minstep = CLOCK_MINSTEP; /* stepout threshold (s) */
114 double clock_panic = CLOCK_PANIC; /* panic threshold (s) */
115 double clock_phi = CLOCK_PHI; /* dispersion rate (s/s) */
116 double allan_xpt = CLOCK_ALLAN; /* Allan intercept (s) */
121 static double clock_offset; /* clock offset adjustment (s) */
122 double drift_comp; /* clock frequency (s/s) */
123 double clock_stability; /* clock stability (s/s) */
124 u_long pps_control; /* last pps sample time */
125 static void rstclock P((int, u_long, double)); /* transition function */
128 struct timex ntv; /* kernel API parameters */
129 int pll_status; /* status bits for kernel pll */
130 int pll_nano; /* nanosecond kernel switch */
131 #endif /* KERNEL_PLL */
134 * Clock state machine control flags
136 int ntp_enable; /* clock discipline enabled */
137 int pll_control; /* kernel support available */
138 int kern_enable; /* kernel support enabled */
139 int pps_enable; /* kernel PPS discipline enabled */
140 int ext_enable; /* external clock enabled */
141 int pps_stratum; /* pps stratum */
142 int allow_panic = FALSE; /* allow panic correction */
143 int mode_ntpdate = FALSE; /* exit on first clock set */
146 * Clock state machine variables
148 u_char sys_poll = NTP_MINDPOLL; /* system poll interval (log2 s) */
149 int state; /* clock discipline state */
150 int tc_counter; /* hysteresis counter */
151 u_long last_time; /* time of last clock update (s) */
152 double last_offset; /* last clock offset (s) */
153 double sys_jitter; /* system RMS jitter (s) */
156 * Huff-n'-puff filter variables
158 static double *sys_huffpuff; /* huff-n'-puff filter */
159 static int sys_hufflen; /* huff-n'-puff filter stages */
160 static int sys_huffptr; /* huff-n'-puff filter pointer */
161 static double sys_mindly; /* huff-n'-puff filter min delay */
163 #if defined(KERNEL_PLL)
164 /* Emacs cc-mode goes nuts if we split the next line... */
165 #define MOD_BITS (MOD_OFFSET | MOD_MAXERROR | MOD_ESTERROR | \
166 MOD_STATUS | MOD_TIMECONST)
168 static void pll_trap P((int)); /* configuration trap */
169 static struct sigaction sigsys; /* current sigaction status */
170 static struct sigaction newsigsys; /* new sigaction status */
171 static sigjmp_buf env; /* environment var. for pll_trap() */
173 #endif /* KERNEL_PLL */
176 * init_loopfilter - initialize loop filter data
179 init_loopfilter(void)
182 * Initialize state variables. Initially, we expect no drift
183 * file, so set the state to S_NSET.
185 rstclock(S_NSET, current_time, 0);
189 * local_clock - the NTP logical clock loop filter. Returns 1 if the
190 * clock was stepped, 0 if it was slewed and -1 if it is hopeless.
192 * LOCKCLOCK: The only thing this routine does is set the
193 * sys_rootdispersion variable equal to the peer dispersion.
197 struct peer *peer, /* synch source peer structure */
198 double fp_offset, /* clock offset (s) */
199 double epsil /* jittter (square s*s) */
202 u_long mu; /* interval since last update (s) */
203 double oerror; /* previous error estimate */
204 double flladj; /* FLL frequency adjustment (ppm) */
205 double plladj; /* PLL frequency adjustment (ppm) */
206 double clock_frequency; /* clock frequency adjustment (ppm) */
207 double dtemp, etemp; /* double temps */
208 int retval; /* return value */
211 * If the loop is opened, monitor and record the offsets
212 * anyway in order to determine the open-loop response.
217 "local_clock: assocID %d offset %.9f jitter %.9f state %d\n",
218 peer->associd, fp_offset, SQRT(epsil), state);
221 sys_rootdispersion = peer->rootdispersion;
224 #else /* LOCKCLOCK */
226 record_loop_stats(fp_offset, drift_comp, SQRT(epsil),
227 clock_stability, sys_poll);
232 * If the clock is way off, panic is declared. The clock_panic
233 * defaults to 1000 s; if set to zero, the panic will never
234 * occur. The allow_panic defaults to FALSE, so the first panic
235 * will exit. It can be set TRUE by a command line option, in
236 * which case the clock will be set anyway and time marches on.
237 * But, allow_panic will be set it FALSE when the update is
238 * within the step range; so, subsequent panics will exit.
240 if (fabs(fp_offset) > clock_panic && clock_panic > 0 &&
243 "time correction of %.0f seconds exceeds sanity limit (%.0f); set clock manually to the correct UTC time.",
244 fp_offset, clock_panic);
249 * If simulating ntpdate, set the clock directly, rather than
250 * using the discipline. The clock_max defines the step
251 * threshold, above which the clock will be stepped instead of
252 * slewed. The value defaults to 128 ms, but can be set to even
253 * unreasonable values. If set to zero, the clock will never be
256 * Note that if ntpdate is active, the terminal does not detach,
257 * so the termination comments print directly to the console.
260 if (fabs(fp_offset) > clock_max && clock_max > 0) {
261 step_systime(fp_offset);
262 msyslog(LOG_NOTICE, "time reset %+.6f s",
264 printf("ntpd: time set %+.6fs\n", fp_offset);
266 adj_systime(fp_offset);
267 msyslog(LOG_NOTICE, "time slew %+.6f s",
269 printf("ntpd: time slew %+.6fs\n", fp_offset);
271 record_loop_stats(fp_offset, drift_comp, SQRT(epsil),
272 clock_stability, sys_poll);
277 * If the clock has never been set, set it and initialize the
278 * discipline parameters. We then switch to frequency mode to
279 * speed the inital convergence process. If lucky, after an hour
280 * the ntp.drift file is created and initialized and we don't
283 if (state == S_NSET) {
284 if (fabs(fp_offset) > clock_max && clock_max > 0) {
285 step_systime(fp_offset);
286 msyslog(LOG_NOTICE, "time reset %+.6f s",
290 rstclock(S_FREQ, peer->epoch, 0);
295 * Update the jitter estimate.
298 dtemp = SQUARE(sys_jitter);
299 sys_jitter = SQRT(dtemp + (epsil - dtemp) / CLOCK_AVG);
302 * The huff-n'-puff filter finds the lowest delay in the recent
303 * interval. This is used to correct the offset by one-half the
304 * difference between the sample delay and minimum delay. This
305 * is most effective if the delays are highly assymetric and
306 * clockhopping is avoided and the clock frequency wander is
309 if (sys_huffpuff != NULL) {
310 if (peer->delay < sys_huffpuff[sys_huffptr])
311 sys_huffpuff[sys_huffptr] = peer->delay;
312 if (peer->delay < sys_mindly)
313 sys_mindly = peer->delay;
315 dtemp = -(peer->delay - sys_mindly) / 2;
317 dtemp = (peer->delay - sys_mindly) / 2;
322 "local_clock: size %d mindly %.6f huffpuff %.6f\n",
323 sys_hufflen, sys_mindly, dtemp);
328 * Clock state machine transition function. This is where the
329 * action is and defines how the system reacts to large phase
330 * and frequency errors. There are two main regimes: when the
331 * offset exceeds the step threshold and when it does not.
332 * However, if the step threshold is set to zero, a step will
333 * never occur. See the instruction manual for the details how
334 * these actions interact with the command line options.
337 if (sys_poll > peer->maxpoll)
338 sys_poll = peer->maxpoll;
339 else if (sys_poll < peer->minpoll)
340 sys_poll = peer->minpoll;
341 clock_frequency = flladj = plladj = 0;
342 mu = peer->epoch - last_time;
343 if (fabs(fp_offset) > clock_max && clock_max > 0) {
347 * In S_TSET state the time has been set at the last
348 * valid update and the offset at that time set to zero.
349 * If following that we cruise outside the capture
350 * range, assume a really bad frequency error and switch
358 * In S_SYNC state we ignore outlyers. At the first
359 * outlyer after the stepout threshold, switch to S_SPIK
363 if (mu < clock_minstep)
369 * In S_FREQ state we ignore outlyers. At the first
370 * outlyer after 900 s, compute the apparent phase and
371 * frequency correction.
374 if (mu < clock_minstep)
376 /* fall through to S_SPIK */
379 * In S_SPIK state a large correction is necessary.
380 * Since the outlyer may be due to a large frequency
381 * error, compute the apparent frequency correction.
384 clock_frequency = (fp_offset - clock_offset) /
386 /* fall through to default */
389 * We get here directly in S_FSET state and indirectly
390 * from S_FREQ and S_SPIK states. The clock is either
391 * reset or shaken, but never stirred.
394 step_systime(fp_offset);
395 msyslog(LOG_NOTICE, "time reset %+.6f s",
398 rstclock(S_TSET, peer->epoch, 0);
406 * In S_FSET state this is the first update. Adjust the
407 * phase, but don't adjust the frequency until the next
411 rstclock(S_TSET, peer->epoch, fp_offset);
415 * In S_FREQ state ignore updates until the stepout
416 * threshold. After that, correct the phase and
417 * frequency and switch to S_SYNC state.
420 if (mu < clock_minstep)
422 clock_frequency = (fp_offset - clock_offset) /
424 rstclock(S_SYNC, peer->epoch, fp_offset);
428 * Either the clock has just been set or the previous
429 * update was a spike and ignored. Since this update is
430 * not an outlyer, fold the tent and resume life.
435 /* fall through to default */
438 * We come here in the normal case for linear phase and
439 * frequency adjustments. If the difference between the
440 * last offset and the current one exceeds the jitter by
441 * CLOCK_SGATE and the interval since the last update is
442 * less than twice the system poll interval, consider
443 * the update a popcorn spike and ignore it..
447 dtemp = fabs(fp_offset - last_offset);
449 if (dtemp > CLOCK_SGATE * oerror && mu <
450 (u_long) ULOGTOD(sys_poll + 1)) {
454 "local_clock: popcorn %.6f %.6f\n",
457 last_offset = fp_offset;
463 * The FLL and PLL frequency gain constants
464 * depend on the poll interval and Allan
465 * intercept. The PLL constant is calculated
466 * throughout the poll interval range, but the
467 * update interval is clamped so as not to
468 * exceed the poll interval. The FLL gain is
469 * zero below one-half the Allan intercept and
470 * unity at MAXPOLL. It decreases as 1 /
471 * (MAXPOLL + 1 - poll interval) in a feeble
472 * effort to match the loop stiffness to the
473 * Allan wobble. Particularly for the PLL, these
474 * measures allow oversampling, but not
475 * undersampling and insure stability even when
476 * the rules of fair engagement are broken.
478 if (ULOGTOD(sys_poll) > allan_xpt / 2) {
479 dtemp = NTP_MAXPOLL + 1 - sys_poll;
480 flladj = (fp_offset - clock_offset) /
481 (max(mu, allan_xpt) * dtemp);
483 etemp = min(mu, (u_long)ULOGTOD(sys_poll));
484 dtemp = 4 * CLOCK_PLL * ULOGTOD(sys_poll);
485 plladj = fp_offset * etemp / (dtemp * dtemp);
486 last_time = peer->epoch;
487 last_offset = clock_offset = fp_offset;
494 * This code segment works when clock adjustments are made using
495 * precision time kernel support and the ntp_adjtime() system
496 * call. This support is available in Solaris 2.6 and later,
497 * Digital Unix 4.0 and later, FreeBSD, Linux and specially
498 * modified kernels for HP-UX 9 and Ultrix 4. In the case of the
499 * DECstation 5000/240 and Alpha AXP, additional kernel
500 * modifications provide a true microsecond clock and nanosecond
501 * clock, respectively.
503 if (pll_control && kern_enable) {
506 * We initialize the structure for the ntp_adjtime()
507 * system call. We have to convert everything to
508 * microseconds or nanoseconds first. Do not update the
509 * system variables if the ext_enable flag is set. In
510 * this case, the external clock driver will update the
511 * variables, which will be read later by the local
512 * clock driver. Afterwards, remember the time and
513 * frequency offsets for jitter and stability values and
514 * to update the drift file.
516 memset(&ntv, 0, sizeof(ntv));
518 ntv.modes = MOD_STATUS;
520 ntv.modes = MOD_BITS;
521 if (clock_offset < 0)
526 ntv.offset = (int32)(clock_offset *
528 ntv.constant = sys_poll;
530 ntv.offset = (int32)(clock_offset *
532 ntv.constant = sys_poll - 4;
534 if (clock_frequency != 0) {
535 ntv.modes |= MOD_FREQUENCY;
536 ntv.freq = (int32)((clock_frequency +
537 drift_comp) * 65536e6);
539 ntv.esterror = (u_int32)(sys_jitter * 1e6);
540 ntv.maxerror = (u_int32)((sys_rootdelay / 2 +
541 sys_rootdispersion) * 1e6);
542 ntv.status = STA_PLL;
545 * Set the leap bits in the status word.
547 if (sys_leap == LEAP_NOTINSYNC) {
548 ntv.status |= STA_UNSYNC;
549 } else if (calleapwhen(sys_reftime.l_ui) <
551 if (sys_leap & LEAP_ADDSECOND)
552 ntv.status |= STA_INS;
553 else if (sys_leap & LEAP_DELSECOND)
554 ntv.status |= STA_DEL;
558 * Switch to FLL mode if the poll interval is
559 * greater than MAXDPOLL, so that the kernel
560 * loop behaves as the daemon loop; viz.,
561 * selects the FLL when necessary, etc. For
564 if (sys_poll > NTP_MAXDPOLL)
565 ntv.status |= STA_FLL;
568 * If the PPS signal is up and enabled, light
569 * the frequency bit. If the PPS driver is
570 * working, light the phase bit as well. If not,
571 * douse the lights, since somebody else may
572 * have left the switch on.
574 if (pps_enable && pll_status & STA_PPSSIGNAL) {
575 ntv.status |= STA_PPSFREQ;
576 if (pps_stratum < STRATUM_UNSPEC)
577 ntv.status |= STA_PPSTIME;
579 ntv.status &= ~(STA_PPSFREQ |
585 * Pass the stuff to the kernel. If it squeals, turn off
586 * the pigs. In any case, fetch the kernel offset and
587 * frequency and pretend we did it here.
589 if (ntp_adjtime(&ntv) == TIME_ERROR) {
590 if (ntv.status != pll_status)
591 NLOG(NLOG_SYNCEVENT | NLOG_SYSEVENT)
593 "kernel time sync disabled %04x",
595 ntv.status &= ~(STA_PPSFREQ | STA_PPSTIME);
597 if (ntv.status != pll_status)
598 NLOG(NLOG_SYNCEVENT | NLOG_SYSEVENT)
600 "kernel time sync enabled %04x",
603 pll_status = ntv.status;
605 clock_offset = ntv.offset / 1e9;
607 clock_offset = ntv.offset / 1e6;
608 clock_frequency = ntv.freq / 65536e6 - drift_comp;
612 * If the kernel PPS is lit, monitor its performance.
614 if (ntv.status & STA_PPSTIME) {
615 pps_control = current_time;
617 sys_jitter = ntv.jitter / 1e9;
619 sys_jitter = ntv.jitter / 1e6;
622 #endif /* KERNEL_PLL */
625 * Adjust the clock frequency and calculate the stability. If
626 * kernel support is available, we use the results of the kernel
627 * discipline instead of the PLL/FLL discipline. In this case,
628 * drift_comp is a sham and used only for updating the drift
629 * file and for billboard eye candy.
631 dtemp = clock_frequency + flladj + plladj;
632 etemp = drift_comp + dtemp;
633 if (etemp > NTP_MAXFREQ)
634 drift_comp = NTP_MAXFREQ;
635 else if (etemp <= -NTP_MAXFREQ)
636 drift_comp = -NTP_MAXFREQ;
639 if (fabs(etemp) > NTP_MAXFREQ)
640 NLOG(NLOG_SYNCEVENT | NLOG_SYSEVENT)
642 "frequency error %.0f PPM exceeds tolerance %.0f PPM",
643 etemp * 1e6, NTP_MAXFREQ * 1e6);
645 etemp = SQUARE(clock_stability);
646 dtemp = SQUARE(dtemp);
647 clock_stability = SQRT(etemp + (dtemp - etemp) / CLOCK_AVG);
650 * In SYNC state, adjust the poll interval. The trick here is to
651 * compare the apparent frequency change induced by the system
652 * jitter over the poll interval, or fritter, to the frequency
653 * stability. If the fritter is greater than the stability,
654 * phase noise predominates and the averaging interval is
655 * increased; otherwise, it is decreased. A bit of hysteresis
656 * helps calm the dance. Works best using burst mode.
658 if (state == S_SYNC) {
659 if (sys_jitter > ULOGTOD(sys_poll) * clock_stability &&
660 fabs(clock_offset) < CLOCK_PGATE * sys_jitter) {
661 tc_counter += sys_poll;
662 if (tc_counter > CLOCK_LIMIT) {
663 tc_counter = CLOCK_LIMIT;
664 if (sys_poll < peer->maxpoll) {
670 tc_counter -= sys_poll << 1;
671 if (tc_counter < -CLOCK_LIMIT) {
672 tc_counter = -CLOCK_LIMIT;
673 if (sys_poll > peer->minpoll) {
682 * Update the system time variables.
684 dtemp = peer->disp + (current_time - peer->epoch) * clock_phi +
685 sys_jitter + fabs(last_offset);
686 if (!(peer->flags & FLAG_REFCLOCK) && dtemp < MINDISPERSE)
688 sys_rootdispersion = peer->rootdispersion + dtemp;
689 record_loop_stats(last_offset, drift_comp, sys_jitter,
690 clock_stability, sys_poll);
695 "local_clock: mu %lu rootjit %.6f stab %.3f poll %d count %d\n",
696 mu, dtemp, clock_stability * 1e6, sys_poll,
700 #endif /* LOCKCLOCK */
705 * adj_host_clock - Called once every second to update the local clock.
707 * LOCKCLOCK: The only thing this routine does is increment the
708 * sys_rootdispersion variable.
718 * Update the dispersion since the last update. In contrast to
719 * NTPv3, NTPv4 does not declare unsynchronized after one day,
720 * since the dispersion check serves this function. Also,
721 * since the poll interval can exceed one day, the old test
722 * would be counterproductive. Note we do this even with
723 * external clocks, since the clock driver will recompute the
724 * maximum error and the local clock driver will pick it up and
725 * pass to the common refclock routines. Very elegant.
727 sys_rootdispersion += clock_phi;
731 * Declare PPS kernel unsync if the pps signal has not been
732 * heard for a few minutes.
734 if (pps_control && current_time - pps_control > PPS_MAXAGE) {
736 NLOG(NLOG_SYNCEVENT | NLOG_SYSEVENT)
737 msyslog(LOG_NOTICE, "pps sync disabled");
742 * If NTP is disabled or ntpdate mode enabled or the kernel
743 * discipline enabled, we have no business going further.
745 if (!ntp_enable || mode_ntpdate || (pll_control && kern_enable))
749 * Intricate wrinkle for legacy only. If the local clock driver
750 * is in use and selected for synchronization, somebody else may
751 * tinker the adjtime() syscall. If this is the case, the driver
752 * is marked prefer and we have to avoid calling adjtime(),
753 * since that may truncate the other guy's requests.
756 if (sys_peer->refclktype == REFCLK_LOCALCLOCK &&
757 sys_peer->flags & FLAG_PREFER)
762 * Implement the phase and frequency adjustments. Note the
763 * black art formerly practiced here has been whitewashed.
765 adjustment = clock_offset / (CLOCK_PLL * ULOGTOD(sys_poll));
766 clock_offset -= adjustment;
767 adj_systime(adjustment + drift_comp);
768 #endif /* LOCKCLOCK */
773 * Clock state machine. Enter new state and set state variables.
777 int trans, /* new state */
778 u_long epoch, /* last time */
779 double offset /* last offset */
783 sys_poll = NTP_MINPOLL;
786 last_offset = clock_offset = offset;
789 printf("local_clock: at %lu state %d\n", last_time,
796 * huff-n'-puff filter
803 if (sys_huffpuff == NULL)
805 sys_huffptr = (sys_huffptr + 1) % sys_hufflen;
806 sys_huffpuff[sys_huffptr] = 1e9;
808 for (i = 0; i < sys_hufflen; i++) {
809 if (sys_huffpuff[i] < sys_mindly)
810 sys_mindly = sys_huffpuff[i];
816 * loop_config - configure the loop filter
818 * LOCKCLOCK: The LOOP_DRIFTINIT and LOOP_DRIFTCOMP cases are no-ops.
835 * Assume the kernel supports the ntp_adjtime() syscall.
836 * If that syscall works, initialize the kernel
837 * variables. Otherwise, continue leaving no harm
838 * behind. While at it, ask to set nanosecond mode. If
839 * the kernel agrees, rejoice; othewise, it does only
842 * Call out the safety patrol. If ntpdate mode or if the
843 * step threshold has been changed by the -x option or
844 * tinker command, kernel discipline is unsafe, so don't
845 * do any of this stuff.
847 if (mode_ntpdate || clock_max != CLOCK_MAX)
851 memset(&ntv, 0, sizeof(ntv));
853 ntv.modes = MOD_BITS | MOD_NANO;
855 ntv.modes = MOD_BITS;
856 #endif /* STA_NANO */
857 ntv.maxerror = MAXDISPERSE;
858 ntv.esterror = MAXDISPERSE;
859 ntv.status = STA_UNSYNC;
862 * Use sigsetjmp() to save state and then call
863 * ntp_adjtime(); if it fails, then siglongjmp() is used
866 newsigsys.sa_handler = pll_trap;
867 newsigsys.sa_flags = 0;
868 if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
870 "sigaction() fails to save SIGSYS trap: %m");
873 if (sigsetjmp(env, 1) == 0)
875 if ((sigaction(SIGSYS, &sigsys,
876 (struct sigaction *)NULL))) {
878 "sigaction() fails to restore SIGSYS trap: %m");
884 pll_status = ntv.status;
887 if (pll_status & STA_NANO)
889 if (pll_status & STA_CLK)
891 #endif /* STA_NANO */
892 NLOG(NLOG_SYNCEVENT | NLOG_SYSEVENT)
894 "kernel time sync status %04x",
897 #endif /* KERNEL_PLL */
898 #endif /* LOCKCLOCK */
905 * If the frequency value is reasonable, set the initial
906 * frequency to the given value and the state to S_FSET.
907 * Otherwise, the drift file may be missing or broken,
908 * so set the frequency to zero. This erases past
909 * history should somebody break something.
911 if (freq <= NTP_MAXFREQ && freq >= -NTP_MAXFREQ) {
913 rstclock(S_FSET, current_time, 0);
920 * Sanity check. If the kernel is enabled, load the
921 * frequency and light up the loop. If not, set the
922 * kernel frequency to zero and leave the loop dark. In
923 * either case set the time to zero to cancel any
927 memset((char *)&ntv, 0, sizeof(ntv));
928 ntv.modes = MOD_OFFSET | MOD_FREQUENCY;
930 ntv.modes |= MOD_STATUS;
931 ntv.status = STA_PLL;
932 ntv.freq = (int32)(drift_comp *
935 (void)ntp_adjtime(&ntv);
937 #endif /* KERNEL_PLL */
938 #endif /* LOCKCLOCK */
942 * Special tinker variables for Ulrich Windl. Very dangerous.
944 case LOOP_MAX: /* step threshold */
948 case LOOP_PANIC: /* panic threshold */
952 case LOOP_PHI: /* dispersion rate */
956 case LOOP_MINSTEP: /* watchdog bark */
957 clock_minstep = freq;
960 case LOOP_ALLAN: /* Allan intercept */
964 case LOOP_HUFFPUFF: /* huff-n'-puff filter length */
967 sys_hufflen = (int)(freq / HUFFPUFF);
968 sys_huffpuff = (double *)emalloc(sizeof(double) *
970 for (i = 0; i < sys_hufflen; i++)
971 sys_huffpuff[i] = 1e9;
975 case LOOP_FREQ: /* initial frequency */
976 drift_comp = freq / 1e6;
977 rstclock(S_FSET, current_time, 0);
983 #if defined(KERNEL_PLL) && defined(SIGSYS)
985 * _trap - trap processor for undefined syscalls
987 * This nugget is called by the kernel when the SYS_ntp_adjtime()
988 * syscall bombs because the silly thing has not been implemented in
989 * the kernel. In this case the phase-lock loop is emulated by
990 * the stock adjtime() syscall and a lot of indelicate abuse.
1000 #endif /* KERNEL_PLL && SIGSYS */