ntp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #define CLOCK_TICK_OVERFLOW (LATCH * HZ - CLOCK_TICK_RATE)
  42. #define CLOCK_TICK_ADJUST (((s64)CLOCK_TICK_OVERFLOW * NSEC_PER_SEC) / \
  43. (s64)CLOCK_TICK_RATE)
  44. static void ntp_update_frequency(void)
  45. {
  46. u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
  47. << TICK_LENGTH_SHIFT;
  48. second_length += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT;
  49. second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
  50. tick_length_base = second_length;
  51. do_div(second_length, HZ);
  52. tick_nsec = second_length >> TICK_LENGTH_SHIFT;
  53. do_div(tick_length_base, NTP_INTERVAL_FREQ);
  54. }
  55. /**
  56. * ntp_clear - Clears the NTP state variables
  57. *
  58. * Must be called while holding a write on the xtime_lock
  59. */
  60. void ntp_clear(void)
  61. {
  62. time_adjust = 0; /* stop active adjtime() */
  63. time_status |= STA_UNSYNC;
  64. time_maxerror = NTP_PHASE_LIMIT;
  65. time_esterror = NTP_PHASE_LIMIT;
  66. ntp_update_frequency();
  67. tick_length = tick_length_base;
  68. time_offset = 0;
  69. }
  70. /*
  71. * this routine handles the overflow of the microsecond field
  72. *
  73. * The tricky bits of code to handle the accurate clock support
  74. * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
  75. * They were originally developed for SUN and DEC kernels.
  76. * All the kudos should go to Dave for this stuff.
  77. */
  78. void second_overflow(void)
  79. {
  80. long time_adj;
  81. /* Bump the maxerror field */
  82. time_maxerror += MAXFREQ >> SHIFT_USEC;
  83. if (time_maxerror > NTP_PHASE_LIMIT) {
  84. time_maxerror = NTP_PHASE_LIMIT;
  85. time_status |= STA_UNSYNC;
  86. }
  87. /*
  88. * Leap second processing. If in leap-insert state at the end of the
  89. * day, the system clock is set back one second; if in leap-delete
  90. * state, the system clock is set ahead one second. The microtime()
  91. * routine or external clock driver will insure that reported time is
  92. * always monotonic. The ugly divides should be replaced.
  93. */
  94. switch (time_state) {
  95. case TIME_OK:
  96. if (time_status & STA_INS)
  97. time_state = TIME_INS;
  98. else if (time_status & STA_DEL)
  99. time_state = TIME_DEL;
  100. break;
  101. case TIME_INS:
  102. if (xtime.tv_sec % 86400 == 0) {
  103. xtime.tv_sec--;
  104. wall_to_monotonic.tv_sec++;
  105. time_state = TIME_OOP;
  106. printk(KERN_NOTICE "Clock: inserting leap second "
  107. "23:59:60 UTC\n");
  108. }
  109. break;
  110. case TIME_DEL:
  111. if ((xtime.tv_sec + 1) % 86400 == 0) {
  112. xtime.tv_sec++;
  113. wall_to_monotonic.tv_sec--;
  114. time_state = TIME_WAIT;
  115. printk(KERN_NOTICE "Clock: deleting leap second "
  116. "23:59:59 UTC\n");
  117. }
  118. break;
  119. case TIME_OOP:
  120. time_state = TIME_WAIT;
  121. break;
  122. case TIME_WAIT:
  123. if (!(time_status & (STA_INS | STA_DEL)))
  124. time_state = TIME_OK;
  125. }
  126. /*
  127. * Compute the phase adjustment for the next second. The offset is
  128. * reduced by a fixed factor times the time constant.
  129. */
  130. tick_length = tick_length_base;
  131. time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
  132. time_offset -= time_adj;
  133. tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
  134. if (unlikely(time_adjust)) {
  135. if (time_adjust > MAX_TICKADJ) {
  136. time_adjust -= MAX_TICKADJ;
  137. tick_length += MAX_TICKADJ_SCALED;
  138. } else if (time_adjust < -MAX_TICKADJ) {
  139. time_adjust += MAX_TICKADJ;
  140. tick_length -= MAX_TICKADJ_SCALED;
  141. } else {
  142. tick_length += (s64)(time_adjust * NSEC_PER_USEC /
  143. NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
  144. time_adjust = 0;
  145. }
  146. }
  147. }
  148. /*
  149. * Return how long ticks are at the moment, that is, how much time
  150. * update_wall_time_one_tick will add to xtime next time we call it
  151. * (assuming no calls to do_adjtimex in the meantime).
  152. * The return value is in fixed-point nanoseconds shifted by the
  153. * specified number of bits to the right of the binary point.
  154. * This function has no side-effects.
  155. */
  156. u64 current_tick_length(void)
  157. {
  158. return tick_length;
  159. }
  160. #ifdef CONFIG_GENERIC_CMOS_UPDATE
  161. /* Disable the cmos update - used by virtualization and embedded */
  162. int no_sync_cmos_clock __read_mostly;
  163. static void sync_cmos_clock(unsigned long dummy);
  164. static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
  165. static void sync_cmos_clock(unsigned long dummy)
  166. {
  167. struct timespec now, next;
  168. int fail = 1;
  169. /*
  170. * If we have an externally synchronized Linux clock, then update
  171. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  172. * called as close as possible to 500 ms before the new second starts.
  173. * This code is run on a timer. If the clock is set, that timer
  174. * may not expire at the correct time. Thus, we adjust...
  175. */
  176. if (!ntp_synced())
  177. /*
  178. * Not synced, exit, do not restart a timer (if one is
  179. * running, let it run out).
  180. */
  181. return;
  182. getnstimeofday(&now);
  183. if (abs(xtime.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
  184. fail = update_persistent_clock(now);
  185. next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
  186. if (next.tv_nsec <= 0)
  187. next.tv_nsec += NSEC_PER_SEC;
  188. if (!fail)
  189. next.tv_sec = 659;
  190. else
  191. next.tv_sec = 0;
  192. if (next.tv_nsec >= NSEC_PER_SEC) {
  193. next.tv_sec++;
  194. next.tv_nsec -= NSEC_PER_SEC;
  195. }
  196. mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next));
  197. }
  198. static void notify_cmos_timer(void)
  199. {
  200. if (!no_sync_cmos_clock)
  201. mod_timer(&sync_cmos_timer, jiffies + 1);
  202. }
  203. #else
  204. static inline void notify_cmos_timer(void) { }
  205. #endif
  206. /* adjtimex mainly allows reading (and writing, if superuser) of
  207. * kernel time-keeping variables. used by xntpd.
  208. */
  209. int do_adjtimex(struct timex *txc)
  210. {
  211. long mtemp, save_adjust, rem;
  212. s64 freq_adj, temp64;
  213. int result;
  214. /* In order to modify anything, you gotta be super-user! */
  215. if (txc->modes && !capable(CAP_SYS_TIME))
  216. return -EPERM;
  217. /* Now we validate the data before disabling interrupts */
  218. if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
  219. /* singleshot must not be used with any other mode bits */
  220. if (txc->modes != ADJ_OFFSET_SINGLESHOT)
  221. return -EINVAL;
  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. temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL);
  300. if (time_offset < 0) {
  301. temp64 = -temp64;
  302. do_div(temp64, mtemp);
  303. freq_adj -= temp64;
  304. } else {
  305. do_div(temp64, mtemp);
  306. freq_adj += temp64;
  307. }
  308. }
  309. freq_adj += time_freq;
  310. freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
  311. time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
  312. time_offset = div_long_long_rem_signed(time_offset,
  313. NTP_INTERVAL_FREQ,
  314. &rem);
  315. time_offset <<= SHIFT_UPDATE;
  316. } /* STA_PLL */
  317. } /* txc->modes & ADJ_OFFSET */
  318. if (txc->modes & ADJ_TICK)
  319. tick_usec = txc->tick;
  320. if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
  321. ntp_update_frequency();
  322. } /* txc->modes */
  323. leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
  324. result = TIME_ERROR;
  325. if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
  326. txc->offset = save_adjust;
  327. else
  328. txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) *
  329. NTP_INTERVAL_FREQ / 1000;
  330. txc->freq = (time_freq / NSEC_PER_USEC) <<
  331. (SHIFT_USEC - SHIFT_NSEC);
  332. txc->maxerror = time_maxerror;
  333. txc->esterror = time_esterror;
  334. txc->status = time_status;
  335. txc->constant = time_constant;
  336. txc->precision = 1;
  337. txc->tolerance = MAXFREQ;
  338. txc->tick = tick_usec;
  339. /* PPS is not implemented, so these are zero */
  340. txc->ppsfreq = 0;
  341. txc->jitter = 0;
  342. txc->shift = 0;
  343. txc->stabil = 0;
  344. txc->jitcnt = 0;
  345. txc->calcnt = 0;
  346. txc->errcnt = 0;
  347. txc->stbcnt = 0;
  348. write_sequnlock_irq(&xtime_lock);
  349. do_gettimeofday(&txc->time);
  350. notify_cmos_timer();
  351. return(result);
  352. }