ntp.c 11 KB

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