xref: /linux/kernel/time/ntp.c (revision ed3174d93c342b8b2eeba6bbd124707d55304a7b)
1 /*
2  * linux/kernel/time/ntp.c
3  *
4  * NTP state machine interfaces and logic.
5  *
6  * This code was mainly moved from kernel/timer.c and kernel/time.c
7  * Please see those files for relevant copyright info and historical
8  * changelogs.
9  */
10 
11 #include <linux/mm.h>
12 #include <linux/time.h>
13 #include <linux/timer.h>
14 #include <linux/timex.h>
15 #include <linux/jiffies.h>
16 #include <linux/hrtimer.h>
17 #include <linux/capability.h>
18 #include <asm/div64.h>
19 #include <asm/timex.h>
20 
21 /*
22  * Timekeeping variables
23  */
24 unsigned long tick_usec = TICK_USEC; 		/* USER_HZ period (usec) */
25 unsigned long tick_nsec;			/* ACTHZ period (nsec) */
26 static u64 tick_length, tick_length_base;
27 
28 #define MAX_TICKADJ		500		/* microsecs */
29 #define MAX_TICKADJ_SCALED	(((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
30 				  TICK_LENGTH_SHIFT) / NTP_INTERVAL_FREQ)
31 
32 /*
33  * phase-lock loop variables
34  */
35 /* TIME_ERROR prevents overwriting the CMOS clock */
36 static int time_state = TIME_OK;	/* clock synchronization status	*/
37 int time_status = STA_UNSYNC;		/* clock status bits		*/
38 static s64 time_offset;		/* time adjustment (ns)		*/
39 static long time_constant = 2;		/* pll time constant		*/
40 long time_maxerror = NTP_PHASE_LIMIT;	/* maximum error (us)		*/
41 long time_esterror = NTP_PHASE_LIMIT;	/* estimated error (us)		*/
42 long time_freq;				/* frequency offset (scaled ppm)*/
43 static long time_reftime;		/* time at last adjustment (s)	*/
44 long time_adjust;
45 
46 static void ntp_update_frequency(void)
47 {
48 	u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
49 				<< TICK_LENGTH_SHIFT;
50 	second_length += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT;
51 	second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
52 
53 	tick_length_base = second_length;
54 
55 	do_div(second_length, HZ);
56 	tick_nsec = second_length >> TICK_LENGTH_SHIFT;
57 
58 	do_div(tick_length_base, NTP_INTERVAL_FREQ);
59 }
60 
61 /**
62  * ntp_clear - Clears the NTP state variables
63  *
64  * Must be called while holding a write on the xtime_lock
65  */
66 void ntp_clear(void)
67 {
68 	time_adjust = 0;		/* stop active adjtime() */
69 	time_status |= STA_UNSYNC;
70 	time_maxerror = NTP_PHASE_LIMIT;
71 	time_esterror = NTP_PHASE_LIMIT;
72 
73 	ntp_update_frequency();
74 
75 	tick_length = tick_length_base;
76 	time_offset = 0;
77 }
78 
79 /*
80  * this routine handles the overflow of the microsecond field
81  *
82  * The tricky bits of code to handle the accurate clock support
83  * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
84  * They were originally developed for SUN and DEC kernels.
85  * All the kudos should go to Dave for this stuff.
86  */
87 void second_overflow(void)
88 {
89 	long time_adj;
90 
91 	/* Bump the maxerror field */
92 	time_maxerror += MAXFREQ >> SHIFT_USEC;
93 	if (time_maxerror > NTP_PHASE_LIMIT) {
94 		time_maxerror = NTP_PHASE_LIMIT;
95 		time_status |= STA_UNSYNC;
96 	}
97 
98 	/*
99 	 * Leap second processing. If in leap-insert state at the end of the
100 	 * day, the system clock is set back one second; if in leap-delete
101 	 * state, the system clock is set ahead one second. The microtime()
102 	 * routine or external clock driver will insure that reported time is
103 	 * always monotonic. The ugly divides should be replaced.
104 	 */
105 	switch (time_state) {
106 	case TIME_OK:
107 		if (time_status & STA_INS)
108 			time_state = TIME_INS;
109 		else if (time_status & STA_DEL)
110 			time_state = TIME_DEL;
111 		break;
112 	case TIME_INS:
113 		if (xtime.tv_sec % 86400 == 0) {
114 			xtime.tv_sec--;
115 			wall_to_monotonic.tv_sec++;
116 			time_state = TIME_OOP;
117 			printk(KERN_NOTICE "Clock: inserting leap second "
118 					"23:59:60 UTC\n");
119 		}
120 		break;
121 	case TIME_DEL:
122 		if ((xtime.tv_sec + 1) % 86400 == 0) {
123 			xtime.tv_sec++;
124 			wall_to_monotonic.tv_sec--;
125 			time_state = TIME_WAIT;
126 			printk(KERN_NOTICE "Clock: deleting leap second "
127 					"23:59:59 UTC\n");
128 		}
129 		break;
130 	case TIME_OOP:
131 		time_state = TIME_WAIT;
132 		break;
133 	case TIME_WAIT:
134 		if (!(time_status & (STA_INS | STA_DEL)))
135 		time_state = TIME_OK;
136 	}
137 
138 	/*
139 	 * Compute the phase adjustment for the next second. The offset is
140 	 * reduced by a fixed factor times the time constant.
141 	 */
142 	tick_length = tick_length_base;
143 	time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
144 	time_offset -= time_adj;
145 	tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
146 
147 	if (unlikely(time_adjust)) {
148 		if (time_adjust > MAX_TICKADJ) {
149 			time_adjust -= MAX_TICKADJ;
150 			tick_length += MAX_TICKADJ_SCALED;
151 		} else if (time_adjust < -MAX_TICKADJ) {
152 			time_adjust += MAX_TICKADJ;
153 			tick_length -= MAX_TICKADJ_SCALED;
154 		} else {
155 			tick_length += (s64)(time_adjust * NSEC_PER_USEC /
156 					NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
157 			time_adjust = 0;
158 		}
159 	}
160 }
161 
162 /*
163  * Return how long ticks are at the moment, that is, how much time
164  * update_wall_time_one_tick will add to xtime next time we call it
165  * (assuming no calls to do_adjtimex in the meantime).
166  * The return value is in fixed-point nanoseconds shifted by the
167  * specified number of bits to the right of the binary point.
168  * This function has no side-effects.
169  */
170 u64 current_tick_length(void)
171 {
172 	return tick_length;
173 }
174 
175 #ifdef CONFIG_GENERIC_CMOS_UPDATE
176 
177 /* Disable the cmos update - used by virtualization and embedded */
178 int no_sync_cmos_clock  __read_mostly;
179 
180 static void sync_cmos_clock(unsigned long dummy);
181 
182 static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
183 
184 static void sync_cmos_clock(unsigned long dummy)
185 {
186 	struct timespec now, next;
187 	int fail = 1;
188 
189 	/*
190 	 * If we have an externally synchronized Linux clock, then update
191 	 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
192 	 * called as close as possible to 500 ms before the new second starts.
193 	 * This code is run on a timer.  If the clock is set, that timer
194 	 * may not expire at the correct time.  Thus, we adjust...
195 	 */
196 	if (!ntp_synced())
197 		/*
198 		 * Not synced, exit, do not restart a timer (if one is
199 		 * running, let it run out).
200 		 */
201 		return;
202 
203 	getnstimeofday(&now);
204 	if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
205 		fail = update_persistent_clock(now);
206 
207 	next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
208 	if (next.tv_nsec <= 0)
209 		next.tv_nsec += NSEC_PER_SEC;
210 
211 	if (!fail)
212 		next.tv_sec = 659;
213 	else
214 		next.tv_sec = 0;
215 
216 	if (next.tv_nsec >= NSEC_PER_SEC) {
217 		next.tv_sec++;
218 		next.tv_nsec -= NSEC_PER_SEC;
219 	}
220 	mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next));
221 }
222 
223 static void notify_cmos_timer(void)
224 {
225 	if (!no_sync_cmos_clock)
226 		mod_timer(&sync_cmos_timer, jiffies + 1);
227 }
228 
229 #else
230 static inline void notify_cmos_timer(void) { }
231 #endif
232 
233 /* adjtimex mainly allows reading (and writing, if superuser) of
234  * kernel time-keeping variables. used by xntpd.
235  */
236 int do_adjtimex(struct timex *txc)
237 {
238 	long mtemp, save_adjust, rem;
239 	s64 freq_adj, temp64;
240 	int result;
241 
242 	/* In order to modify anything, you gotta be super-user! */
243 	if (txc->modes && !capable(CAP_SYS_TIME))
244 		return -EPERM;
245 
246 	/* Now we validate the data before disabling interrupts */
247 
248 	if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) {
249 	  /* singleshot must not be used with any other mode bits */
250 		if (txc->modes != ADJ_OFFSET_SINGLESHOT &&
251 					txc->modes != ADJ_OFFSET_SS_READ)
252 			return -EINVAL;
253 	}
254 
255 	if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
256 	  /* adjustment Offset limited to +- .512 seconds */
257 		if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
258 			return -EINVAL;
259 
260 	/* if the quartz is off by more than 10% something is VERY wrong ! */
261 	if (txc->modes & ADJ_TICK)
262 		if (txc->tick <  900000/USER_HZ ||
263 		    txc->tick > 1100000/USER_HZ)
264 			return -EINVAL;
265 
266 	write_seqlock_irq(&xtime_lock);
267 	result = time_state;	/* mostly `TIME_OK' */
268 
269 	/* Save for later - semantics of adjtime is to return old value */
270 	save_adjust = time_adjust;
271 
272 #if 0	/* STA_CLOCKERR is never set yet */
273 	time_status &= ~STA_CLOCKERR;		/* reset STA_CLOCKERR */
274 #endif
275 	/* If there are input parameters, then process them */
276 	if (txc->modes)
277 	{
278 	    if (txc->modes & ADJ_STATUS)	/* only set allowed bits */
279 		time_status =  (txc->status & ~STA_RONLY) |
280 			      (time_status & STA_RONLY);
281 
282 	    if (txc->modes & ADJ_FREQUENCY) {	/* p. 22 */
283 		if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
284 		    result = -EINVAL;
285 		    goto leave;
286 		}
287 		time_freq = ((s64)txc->freq * NSEC_PER_USEC)
288 				>> (SHIFT_USEC - SHIFT_NSEC);
289 	    }
290 
291 	    if (txc->modes & ADJ_MAXERROR) {
292 		if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
293 		    result = -EINVAL;
294 		    goto leave;
295 		}
296 		time_maxerror = txc->maxerror;
297 	    }
298 
299 	    if (txc->modes & ADJ_ESTERROR) {
300 		if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
301 		    result = -EINVAL;
302 		    goto leave;
303 		}
304 		time_esterror = txc->esterror;
305 	    }
306 
307 	    if (txc->modes & ADJ_TIMECONST) {	/* p. 24 */
308 		if (txc->constant < 0) {	/* NTP v4 uses values > 6 */
309 		    result = -EINVAL;
310 		    goto leave;
311 		}
312 		time_constant = min(txc->constant + 4, (long)MAXTC);
313 	    }
314 
315 	    if (txc->modes & ADJ_OFFSET) {	/* values checked earlier */
316 		if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
317 		    /* adjtime() is independent from ntp_adjtime() */
318 		    time_adjust = txc->offset;
319 		}
320 		else if (time_status & STA_PLL) {
321 		    time_offset = txc->offset * NSEC_PER_USEC;
322 
323 		    /*
324 		     * Scale the phase adjustment and
325 		     * clamp to the operating range.
326 		     */
327 		    time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC);
328 		    time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC);
329 
330 		    /*
331 		     * Select whether the frequency is to be controlled
332 		     * and in which mode (PLL or FLL). Clamp to the operating
333 		     * range. Ugly multiply/divide should be replaced someday.
334 		     */
335 
336 		    if (time_status & STA_FREQHOLD || time_reftime == 0)
337 		        time_reftime = xtime.tv_sec;
338 		    mtemp = xtime.tv_sec - time_reftime;
339 		    time_reftime = xtime.tv_sec;
340 
341 		    freq_adj = time_offset * mtemp;
342 		    freq_adj = shift_right(freq_adj, time_constant * 2 +
343 					   (SHIFT_PLL + 2) * 2 - SHIFT_NSEC);
344 		    if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
345 			temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL);
346 			if (time_offset < 0) {
347 			    temp64 = -temp64;
348 			    do_div(temp64, mtemp);
349 			    freq_adj -= temp64;
350 			} else {
351 			    do_div(temp64, mtemp);
352 			    freq_adj += temp64;
353 			}
354 		    }
355 		    freq_adj += time_freq;
356 		    freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
357 		    time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
358 		    time_offset = div_long_long_rem_signed(time_offset,
359 							   NTP_INTERVAL_FREQ,
360 							   &rem);
361 		    time_offset <<= SHIFT_UPDATE;
362 		} /* STA_PLL */
363 	    } /* txc->modes & ADJ_OFFSET */
364 	    if (txc->modes & ADJ_TICK)
365 		tick_usec = txc->tick;
366 
367 	    if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
368 		    ntp_update_frequency();
369 	} /* txc->modes */
370 leave:	if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
371 		result = TIME_ERROR;
372 
373 	if ((txc->modes == ADJ_OFFSET_SINGLESHOT) ||
374 			(txc->modes == ADJ_OFFSET_SS_READ))
375 		txc->offset = save_adjust;
376 	else
377 		txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) *
378 	    			NTP_INTERVAL_FREQ / 1000;
379 	txc->freq	   = (time_freq / NSEC_PER_USEC) <<
380 				(SHIFT_USEC - SHIFT_NSEC);
381 	txc->maxerror	   = time_maxerror;
382 	txc->esterror	   = time_esterror;
383 	txc->status	   = time_status;
384 	txc->constant	   = time_constant;
385 	txc->precision	   = 1;
386 	txc->tolerance	   = MAXFREQ;
387 	txc->tick	   = tick_usec;
388 
389 	/* PPS is not implemented, so these are zero */
390 	txc->ppsfreq	   = 0;
391 	txc->jitter	   = 0;
392 	txc->shift	   = 0;
393 	txc->stabil	   = 0;
394 	txc->jitcnt	   = 0;
395 	txc->calcnt	   = 0;
396 	txc->errcnt	   = 0;
397 	txc->stbcnt	   = 0;
398 	write_sequnlock_irq(&xtime_lock);
399 	do_gettimeofday(&txc->time);
400 	notify_cmos_timer();
401 	return(result);
402 }
403