2 * ntp_loopfilter.c - implements the NTP loop filter algorithm
11 #include "ntp_unixtime.h"
12 #include "ntp_stdlib.h"
20 #if defined(VMS) && defined(VMS_LOCALUNIT) /*wjm*/
21 #include "ntp_refclock.h"
25 #include "ntp_syscall.h"
26 #endif /* KERNEL_PLL */
29 * This is an implementation of the clock discipline algorithm described
30 * in UDel TR 97-4-3, as amended. It operates as an adaptive parameter,
31 * hybrid phase/frequency-lock loop. A number of sanity checks are
32 * included to protect against timewarps, timespikes and general mayhem.
33 * All units are in s and s/s, unless noted otherwise.
35 #define CLOCK_MAX .128 /* default max offset (s) */
36 #define CLOCK_PANIC 1000. /* default panic offset (s) */
37 #define CLOCK_MAXSTAB 2e-6 /* max frequency stability (s/s) */
38 #define CLOCK_MAXERR 1e-2 /* max phase jitter (s) */
39 #define CLOCK_PHI 15e-6 /* max frequency error (s/s) */
40 #define SHIFT_PLL 4 /* PLL loop gain (shift) */
41 #define CLOCK_AVG 4. /* FLL loop gain */
42 #define CLOCK_MINSEC 256. /* min FLL update interval (s) */
43 #define CLOCK_MINSTEP 900. /* step-change timeout (s) */
44 #define CLOCK_DAY 86400. /* one day of seconds */
45 #define CLOCK_LIMIT 30 /* poll-adjust threshold */
46 #define CLOCK_PGATE 4. /* poll-adjust gate */
47 #define CLOCK_ALLAN 1024. /* min Allan intercept (s) */
48 #define CLOCK_ADF 1e11 /* Allan deviation factor */
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
109 #define PPS_MAXAGE 120 /* kernel pps signal timeout (s) */
112 * Program variables that can be tinkered.
114 double clock_max = CLOCK_MAX; /* max offset before step (s) */
115 double clock_panic = CLOCK_PANIC; /* max offset before panic (s) */
116 double clock_phi = CLOCK_PHI; /* dispersion rate (s/s) */
117 double clock_minstep = CLOCK_MINSTEP; /* step timeout (s) */
118 double allan_xpt = CLOCK_ALLAN; /* minimum Allan intercept (s) */
123 static double clock_offset; /* clock offset adjustment (s) */
124 double drift_comp; /* clock frequency (s/s) */
125 double clock_stability; /* clock stability (s/s) */
126 u_long pps_control; /* last pps sample time */
127 static void rstclock P((int, double, double)); /* transition function */
130 struct timex ntv; /* kernel API parameters */
131 int pll_status; /* status bits for kernel pll */
132 int pll_nano; /* nanosecond kernel switch */
133 #endif /* KERNEL_PLL */
136 * Clock state machine control flags
138 int ntp_enable; /* clock discipline enabled */
139 int pll_control; /* kernel support available */
140 int kern_enable; /* kernel support enabled */
141 int pps_enable; /* kernel PPS discipline enabled */
142 int ext_enable; /* external clock enabled */
143 int pps_stratum; /* pps stratum */
144 int allow_step = TRUE; /* allow step correction */
145 int allow_panic = FALSE; /* allow panic correction */
146 int mode_ntpdate = FALSE; /* exit on first clock set */
149 * Clock state machine variables
151 u_char sys_minpoll = NTP_MINDPOLL; /* min sys poll interval (log2 s) */
152 u_char sys_poll = NTP_MINDPOLL; /* system poll interval (log2 s) */
153 int state; /* clock discipline state */
154 int tc_counter; /* poll-adjust counter */
155 u_long last_time; /* time of last clock update (s) */
156 double last_offset; /* last clock offset (s) */
157 double sys_jitter; /* system RMS jitter (s) */
160 * Huff-n'-puff filter variables
162 static double *sys_huffpuff; /* huff-n'-puff filter */
163 static int sys_hufflen; /* huff-n'-puff filter stages */
164 static int sys_huffptr; /* huff-n'-puff filter pointer */
165 static double sys_mindly; /* huff-n'-puff filter min delay */
167 #if defined(KERNEL_PLL)
168 /* Emacs cc-mode goes nuts if we split the next line... */
169 #define MOD_BITS (MOD_OFFSET | MOD_MAXERROR | MOD_ESTERROR | \
170 MOD_STATUS | MOD_TIMECONST)
172 static void pll_trap P((int)); /* configuration trap */
173 static struct sigaction sigsys; /* current sigaction status */
174 static struct sigaction newsigsys; /* new sigaction status */
175 static sigjmp_buf env; /* environment var. for pll_trap() */
177 #endif /* KERNEL_PLL */
180 * init_loopfilter - initialize loop filter data
183 init_loopfilter(void)
186 * Initialize state variables. Initially, we expect no drift
187 * file, so set the state to S_NSET.
189 rstclock(S_NSET, current_time, 0);
193 * local_clock - the NTP logical clock loop filter. Returns 1 if the
194 * clock was stepped, 0 if it was slewed and -1 if it is hopeless.
198 struct peer *peer, /* synch source peer structure */
199 double fp_offset, /* clock offset (s) */
200 double epsil /* jittter (square s*s) */
203 double mu; /* interval since last update (s) */
204 double oerror; /* previous error estimate */
205 double flladj; /* FLL frequency adjustment (ppm) */
206 double plladj; /* PLL frequency adjustment (ppm) */
207 double clock_frequency; /* clock frequency adjustment (ppm) */
208 double dtemp, etemp; /* double temps */
209 int retval; /* return value */
212 * If the loop is opened, monitor and record the offsets
213 * anyway in order to determine the open-loop response.
218 "local_clock: assocID %d off %.6f jit %.6f sta %d\n",
219 peer->associd, fp_offset, SQRT(epsil), state);
222 record_loop_stats(fp_offset, drift_comp, SQRT(epsil),
223 clock_stability, sys_poll);
228 * If the clock is way off, panic is declared. The clock_panic
229 * defaults to 1000 s; if set to zero, the panic will never
230 * occur. The allow_panic defaults to FALSE, so the first panic
231 * will exit. It can be set TRUE by a command line option, in
232 * which case the clock will be set anyway and time marches on.
233 * But, allow_panic will be set it FALSE when the update is
234 * within the step range; so, subsequent panics will exit.
236 if (fabs(fp_offset) > clock_panic && clock_panic > 0 &&
239 "time correction of %.0f seconds exceeds sanity limit (%.0f); set clock manually to the correct UTC time.",
240 fp_offset, clock_panic);
245 * If simulating ntpdate, set the clock directly, rather than
246 * using the discipline. The clock_max defines the step
247 * threshold, above which the clock will be stepped instead of
248 * slewed. The value defaults to 128 ms, but can be set to even
249 * unreasonable values. If set to zero, the clock will never be
252 * Note that if ntpdate is active, the terminal does not detach,
253 * so the termination comments print directly to the console.
256 if (allow_step && fabs(fp_offset) > clock_max &&
258 step_systime(fp_offset);
259 NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
260 msyslog(LOG_NOTICE, "time reset %.6f s",
262 printf("ntpd: time reset %.6fs\n", fp_offset);
264 adj_systime(fp_offset);
265 NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
266 msyslog(LOG_NOTICE, "time slew %.6f s",
268 printf("ntpd: time slew %.6fs\n", fp_offset);
270 record_loop_stats(fp_offset, drift_comp, SQRT(epsil),
271 clock_stability, sys_poll);
276 * If the clock has never been set, set it and initialize the
277 * discipline parameters. We then switch to frequency mode to
278 * speed the inital convergence process. If lucky, after an hour
279 * the ntp.drift file is created and initialized and we don't
282 if (state == S_NSET) {
283 step_systime(fp_offset);
284 NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
285 msyslog(LOG_NOTICE, "time set %.6f s", fp_offset);
286 rstclock(S_FREQ, peer->epoch, fp_offset);
291 * Update the jitter estimate.
294 dtemp = SQUARE(sys_jitter);
295 sys_jitter = SQRT(dtemp + (epsil - dtemp) / CLOCK_AVG);
298 * The huff-n'-puff filter finds the lowest delay in the recent
299 * interval. This is used to correct the offset by one-half the
300 * difference between the sample delay and minimum delay. This
301 * is most effective if the delays are highly assymetric and
302 * clockhopping is avoided and the clock frequency wander is
305 if (sys_huffpuff != NULL) {
306 if (peer->delay < sys_huffpuff[sys_huffptr])
307 sys_huffpuff[sys_huffptr] = peer->delay;
308 if (peer->delay < sys_mindly)
309 sys_mindly = peer->delay;
311 dtemp = -(peer->delay - sys_mindly) / 2;
313 dtemp = (peer->delay - sys_mindly) / 2;
318 "local_clock: size %d mindly %.6f huffpuff %.6f\n",
319 sys_hufflen, sys_mindly, dtemp);
324 * Clock state machine transition function. This is where the
325 * action is and defines how the system reacts to large phase
326 * and frequency errors. There are two main regimes: when the
327 * offset exceeds the step threshold and when it does not.
328 * However, if the step threshold is set to zero, a step will
329 * never occur. See the instruction manual for the details how
330 * these actions interact with the command line options.
333 if (sys_poll > peer->maxpoll)
334 sys_poll = peer->maxpoll;
335 else if (sys_poll < peer->minpoll)
336 sys_poll = peer->minpoll;
337 clock_frequency = flladj = plladj = 0;
338 mu = peer->epoch - last_time;
339 if (fabs(fp_offset) > clock_max && clock_max > 0) {
343 * In S_TSET state the time has been set at the last
344 * valid update and the offset at that time set to zero.
345 * If following that we cruise outside the capture
346 * range, assume a really bad frequency error and switch
354 * In S_SYNC state we ignore outlyers. At the first
355 * outlyer after the stepout threshold, switch to S_SPIK
359 if (mu < clock_minstep)
365 * In S_FREQ state we ignore outlyers. At the first
366 * outlyer after 900 s, compute the apparent phase and
367 * frequency correction.
370 if (mu < clock_minstep)
372 /* fall through to S_SPIK */
375 * In S_SPIK state a large correction is necessary.
376 * Since the outlyer may be due to a large frequency
377 * error, compute the apparent frequency correction.
380 clock_frequency = (fp_offset - clock_offset) /
382 /* fall through to default */
385 * We get here directly in S_FSET state and indirectly
386 * from S_FREQ and S_SPIK states. The clock is either
387 * reset or shaken, but never stirred.
391 step_systime(fp_offset);
392 NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
393 msyslog(LOG_NOTICE, "time reset %.6f s",
395 rstclock(S_TSET, peer->epoch, 0);
398 NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
399 msyslog(LOG_NOTICE, "time slew %.6f s",
401 rstclock(S_FREQ, peer->epoch,
410 * In S_FSET state this is the first update. Adjust the
411 * phase, but don't adjust the frequency until the next
415 rstclock(S_TSET, peer->epoch, fp_offset);
419 * In S_FREQ state ignore updates until the stepout
420 * threshold. After that, correct the phase and
421 * frequency and switch to S_SYNC state.
424 if (mu < clock_minstep)
426 clock_frequency = (fp_offset - clock_offset) /
428 rstclock(S_SYNC, peer->epoch, fp_offset);
432 * Either the clock has just been set or the previous
433 * update was a spike and ignored. Since this update is
434 * not an outlyer, fold the tent and resume life.
439 /* fall through to default */
442 * We come here in the normal case for linear phase and
443 * frequency adjustments. If the offset exceeds the
444 * previous time error estimate by CLOCK_SGATE and the
445 * interval since the last update is less than twice the
446 * poll interval, consider the update a popcorn spike
451 if (fabs(fp_offset - last_offset) >
452 CLOCK_SGATE * oerror && mu <
453 ULOGTOD(sys_poll + 1)) {
457 "local_clock: popcorn %.6f %.6f\n",
459 last_offset), CLOCK_SGATE *
462 last_offset = fp_offset;
467 * Compute the FLL and PLL frequency adjustments
468 * conditioned on intricate weighting factors.
469 * For the FLL, the averaging interval is
470 * clamped to a minimum of 1024 s and the gain
471 * is decreased from unity for mu above 1024 s
472 * to zero below 256 s. For the PLL, the
473 * averaging interval is clamped not to exceed
474 * the sustem poll interval. No gain factor is
475 * necessary, since the frequency steering above
476 * 1024 s is negligible. Particularly for the
477 * PLL, these measures allow oversampling, but
478 * not undersampling and insure stability even
479 * when the rules of fair engagement are broken.
481 dtemp = max(mu, allan_xpt);
482 etemp = min(max(0, mu - CLOCK_MINSEC) /
484 flladj = fp_offset * etemp / (dtemp *
486 dtemp = ULOGTOD(SHIFT_PLL + 2 + sys_poll);
487 etemp = min(mu, ULOGTOD(sys_poll));
488 plladj = fp_offset * etemp / (dtemp * dtemp);
489 last_time = peer->epoch;
490 last_offset = clock_offset = fp_offset;
495 #if defined(KERNEL_PLL)
497 * This code segment works when clock adjustments are made using
498 * precision time kernel support and the ntp_adjtime() system
499 * call. This support is available in Solaris 2.6 and later,
500 * Digital Unix 4.0 and later, FreeBSD, Linux and specially
501 * modified kernels for HP-UX 9 and Ultrix 4. In the case of the
502 * DECstation 5000/240 and Alpha AXP, additional kernel
503 * modifications provide a true microsecond clock and nanosecond
504 * clock, respectively.
506 if (pll_control && kern_enable) {
509 * We initialize the structure for the ntp_adjtime()
510 * system call. We have to convert everything to
511 * microseconds or nanoseconds first. Do not update the
512 * system variables if the ext_enable flag is set. In
513 * this case, the external clock driver will update the
514 * variables, which will be read later by the local
515 * clock driver. Afterwards, remember the time and
516 * frequency offsets for jitter and stability values and
517 * to update the drift file.
519 memset(&ntv, 0, sizeof(ntv));
521 ntv.modes = MOD_STATUS;
523 ntv.modes = MOD_BITS;
524 if (clock_offset < 0)
529 ntv.offset = (int32)(clock_offset *
531 ntv.constant = sys_poll;
533 ntv.offset = (int32)(clock_offset *
535 ntv.constant = sys_poll - 4;
537 if (clock_frequency != 0) {
538 ntv.modes |= MOD_FREQUENCY;
539 ntv.freq = (int32)((clock_frequency +
540 drift_comp) * 65536e6);
542 ntv.esterror = (u_int32)(sys_jitter * 1e6);
543 ntv.maxerror = (u_int32)((sys_rootdelay / 2 +
544 sys_rootdispersion) * 1e6);
545 ntv.status = STA_PLL;
548 * Set the leap bits in the status word.
550 if (sys_leap == LEAP_NOTINSYNC) {
551 ntv.status |= STA_UNSYNC;
552 } else if (calleapwhen(sys_reftime.l_ui) <
554 if (sys_leap & LEAP_ADDSECOND)
555 ntv.status |= STA_INS;
556 else if (sys_leap & LEAP_DELSECOND)
557 ntv.status |= STA_DEL;
561 * Switch to FLL mode if the poll interval is
562 * greater than MAXDPOLL, so that the kernel
563 * loop behaves as the daemon loop; viz.,
564 * selects the FLL when necessary, etc. For
567 if (sys_poll > NTP_MAXDPOLL)
568 ntv.status |= STA_FLL;
571 * If the PPS signal is up and enabled, light
572 * the frequency bit. If the PPS driver is
573 * working, light the phase bit as well. If not,
574 * douse the lights, since somebody else may
575 * have left the switch on.
577 if (pps_enable && pll_status & STA_PPSSIGNAL) {
578 ntv.status |= STA_PPSFREQ;
579 if (pps_stratum < STRATUM_UNSPEC)
580 ntv.status |= STA_PPSTIME;
582 ntv.status &= ~(STA_PPSFREQ |
588 * Pass the stuff to the kernel. If it squeals, turn off
589 * the pigs. In any case, fetch the kernel offset and
590 * frequency and pretend we did it here.
592 if (ntp_adjtime(&ntv) == TIME_ERROR) {
593 if (ntv.status != pll_status)
595 "kernel time discipline status change %x",
597 ntv.status &= ~(STA_PPSFREQ | STA_PPSTIME);
599 pll_status = ntv.status;
601 clock_offset = ntv.offset / 1e9;
603 clock_offset = ntv.offset / 1e6;
604 clock_frequency = ntv.freq / 65536e6 - drift_comp;
608 * If the kernel PPS is lit, monitor its performance.
610 if (ntv.status & STA_PPSTIME) {
611 pps_control = current_time;
613 sys_jitter = ntv.jitter / 1e9;
615 sys_jitter = ntv.jitter / 1e6;
618 #endif /* KERNEL_PLL */
621 * Adjust the clock frequency and calculate the stability. If
622 * kernel support is available, we use the results of the kernel
623 * discipline instead of the PLL/FLL discipline. In this case,
624 * drift_comp is a sham and used only for updating the drift
625 * file and for billboard eye candy.
627 etemp = clock_frequency + flladj + plladj;
629 if (drift_comp > NTP_MAXFREQ)
630 drift_comp = NTP_MAXFREQ;
631 else if (drift_comp <= -NTP_MAXFREQ)
632 drift_comp = -NTP_MAXFREQ;
633 dtemp = SQUARE(clock_stability);
634 etemp = SQUARE(etemp) - dtemp;
635 clock_stability = SQRT(dtemp + etemp / CLOCK_AVG);
638 * In SYNC state, adjust the poll interval. The trick here is to
639 * compare the apparent frequency change induced by the system
640 * jitter over the poll interval, or fritter, to the frequency
641 * stability. If the fritter is greater than the stability,
642 * phase noise predominates and the averaging interval is
643 * increased; otherwise, it is decreased. A bit of hysteresis
644 * helps calm the dance. Works best using burst mode.
646 if (state == S_SYNC) {
647 if (sys_jitter / ULOGTOD(sys_poll) > clock_stability &&
648 fabs(clock_offset) < CLOCK_PGATE * sys_jitter) {
649 tc_counter += sys_poll;
650 if (tc_counter > CLOCK_LIMIT) {
651 tc_counter = CLOCK_LIMIT;
652 if (sys_poll < peer->maxpoll) {
658 tc_counter -= sys_poll << 1;
659 if (tc_counter < -CLOCK_LIMIT) {
660 tc_counter = -CLOCK_LIMIT;
661 if (sys_poll > peer->minpoll) {
670 * Update the system time variables.
672 dtemp = peer->disp + sys_jitter;
673 if ((peer->flags & FLAG_REFCLOCK) == 0 && dtemp < MINDISPERSE)
675 sys_rootdispersion = peer->rootdispersion + dtemp;
676 record_loop_stats(last_offset, drift_comp, sys_jitter,
677 clock_stability, sys_poll);
681 "local_clock: mu %.0f noi %.3f stb %.3f pol %d cnt %d\n",
682 mu, sys_jitter * 1e6 / mu, clock_stability * 1e6,
683 sys_poll, tc_counter);
690 * adj_host_clock - Called once every second to update the local clock.
700 * Update the dispersion since the last update. In contrast to
701 * NTPv3, NTPv4 does not declare unsynchronized after one day,
702 * since the dispersion check serves this function. Also,
703 * since the poll interval can exceed one day, the old test
704 * would be counterproductive. Note we do this even with
705 * external clocks, since the clock driver will recompute the
706 * maximum error and the local clock driver will pick it up and
707 * pass to the common refclock routines. Very elegant.
709 sys_rootdispersion += clock_phi;
712 * Declare PPS kernel unsync if the pps signal has not been
713 * heard for a few minutes.
715 if (pps_control && current_time - pps_control > PPS_MAXAGE) {
717 NLOG(NLOG_SYSEVENT) /* conditional if clause */
718 msyslog(LOG_INFO, "pps sync disabled");
725 * If the phase-lock loop is implemented in the kernel, we
726 * have no business going further.
728 if (pll_control && kern_enable)
732 * Intricate wrinkle for legacy only. If the local clock driver
733 * is in use and selected for synchronization, somebody else may
734 * tinker the adjtime() syscall. If this is the case, the driver
735 * is marked prefer and we have to avoid calling adjtime(),
736 * since that may truncate the other guy's requests.
739 if (sys_peer->refclktype == REFCLK_LOCALCLOCK &&
740 sys_peer->flags & FLAG_PREFER)
743 adjustment = clock_offset / ULOGTOD(SHIFT_PLL + sys_poll);
744 clock_offset -= adjustment;
745 adj_systime(adjustment + drift_comp);
750 * Clock state machine. Enter new state and set state variables.
754 int trans, /* new state */
755 double epoch, /* last time */
756 double offset /* last offset */
760 sys_poll = NTP_MINPOLL;
763 last_offset = clock_offset = offset;
768 * huff-n'-puff filter
775 if (sys_huffpuff == NULL)
777 sys_huffptr = (sys_huffptr + 1) % sys_hufflen;
778 sys_huffpuff[sys_huffptr] = 1e9;
780 for (i = 0; i < sys_hufflen; i++) {
781 if (sys_huffpuff[i] < sys_mindly)
782 sys_mindly = sys_huffpuff[i];
788 * loop_config - configure the loop filter
804 * Assume the kernel supports the ntp_adjtime() syscall.
805 * If that syscall works, initialize the kernel
806 * variables. Otherwise, continue leaving no harm
807 * behind. While at it, ask to set nanosecond mode. If
808 * the kernel agrees, rejoice; othewise, it does only
812 memset(&ntv, 0, sizeof(ntv));
814 ntv.modes = MOD_BITS | MOD_NANO;
816 ntv.modes = MOD_BITS;
817 #endif /* STA_NANO */
818 ntv.maxerror = MAXDISPERSE;
819 ntv.esterror = MAXDISPERSE;
820 ntv.status = STA_UNSYNC;
823 * Use sigsetjmp() to save state and then call
824 * ntp_adjtime(); if it fails, then siglongjmp() is used
827 newsigsys.sa_handler = pll_trap;
828 newsigsys.sa_flags = 0;
829 if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
831 "sigaction() fails to save SIGSYS trap: %m");
834 if (sigsetjmp(env, 1) == 0)
836 if ((sigaction(SIGSYS, &sigsys,
837 (struct sigaction *)NULL))) {
839 "sigaction() fails to restore SIGSYS trap: %m");
845 pll_status = ntv.status;
848 if (pll_status & STA_NANO)
850 if (pll_status & STA_CLK)
852 #endif /* STA_NANO */
854 "kernel time discipline status %04x",
857 #endif /* KERNEL_PLL */
863 * Initialize the kernel frequency and clamp to
864 * reasonable value. Also set the initial state to
865 * S_FSET to indicated the frequency has been
866 * initialized from the previously saved drift file.
868 rstclock(S_FSET, current_time, 0);
870 if (drift_comp > NTP_MAXFREQ)
871 drift_comp = NTP_MAXFREQ;
872 if (drift_comp < -NTP_MAXFREQ)
873 drift_comp = -NTP_MAXFREQ;
877 * Sanity check. If the kernel is enabled, load the
878 * frequency and light up the loop. If not, set the
879 * kernel frequency to zero and leave the loop dark. In
880 * either case set the time to zero to cancel any
884 memset((char *)&ntv, 0, sizeof(ntv));
885 ntv.modes = MOD_OFFSET | MOD_FREQUENCY;
887 ntv.modes |= MOD_STATUS;
888 ntv.status = STA_PLL;
889 ntv.freq = (int32)(drift_comp *
892 (void)ntp_adjtime(&ntv);
894 #endif /* KERNEL_PLL */
898 * Special tinker variables for Ulrich Windl. Very dangerous.
900 case LOOP_MAX: /* step threshold */
904 case LOOP_PANIC: /* panic exit threshold */
908 case LOOP_PHI: /* dispersion rate */
912 case LOOP_MINSTEP: /* watchdog bark */
913 clock_minstep = freq;
916 case LOOP_MINPOLL: /* ephemeral association poll */
917 if (freq < NTP_MINPOLL)
919 sys_minpoll = (u_char)freq;
922 case LOOP_ALLAN: /* minimum Allan intercept */
923 if (freq < CLOCK_ALLAN)
928 case LOOP_HUFFPUFF: /* huff-n'-puff filter length */
931 sys_hufflen = (int)(freq / HUFFPUFF);
932 sys_huffpuff = (double *)emalloc(sizeof(double) *
934 for (i = 0; i < sys_hufflen; i++)
935 sys_huffpuff[i] = 1e9;
942 #if defined(KERNEL_PLL) && defined(SIGSYS)
944 * _trap - trap processor for undefined syscalls
946 * This nugget is called by the kernel when the SYS_ntp_adjtime()
947 * syscall bombs because the silly thing has not been implemented in
948 * the kernel. In this case the phase-lock loop is emulated by
949 * the stock adjtime() syscall and a lot of indelicate abuse.
959 #endif /* KERNEL_PLL && SIGSYS */