ntp.c 11 KB

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