ntp.c 12 KB

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