ntp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. u64 tick_length;
  25. static u64 tick_length_base;
  26. #define MAX_TICKADJ 500 /* microsecs */
  27. #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
  28. NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
  29. /*
  30. * phase-lock loop variables
  31. */
  32. /* TIME_ERROR prevents overwriting the CMOS clock */
  33. static int time_state = TIME_OK; /* clock synchronization status */
  34. int time_status = STA_UNSYNC; /* clock status bits */
  35. static long time_tai; /* TAI offset (s) */
  36. static s64 time_offset; /* time adjustment (ns) */
  37. static long time_constant = 2; /* pll time constant */
  38. long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
  39. long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
  40. static s64 time_freq; /* frequency offset (scaled ns/s)*/
  41. static long time_reftime; /* time at last adjustment (s) */
  42. long time_adjust;
  43. static long ntp_tick_adj;
  44. static void ntp_update_frequency(void)
  45. {
  46. u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
  47. << NTP_SCALE_SHIFT;
  48. second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
  49. second_length += time_freq;
  50. tick_length_base = second_length;
  51. tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
  52. tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
  53. }
  54. static void ntp_update_offset(long offset)
  55. {
  56. long mtemp;
  57. s64 freq_adj;
  58. if (!(time_status & STA_PLL))
  59. return;
  60. if (!(time_status & STA_NANO))
  61. offset *= NSEC_PER_USEC;
  62. /*
  63. * Scale the phase adjustment and
  64. * clamp to the operating range.
  65. */
  66. offset = min(offset, MAXPHASE);
  67. offset = max(offset, -MAXPHASE);
  68. /*
  69. * Select how the frequency is to be controlled
  70. * and in which mode (PLL or FLL).
  71. */
  72. if (time_status & STA_FREQHOLD || time_reftime == 0)
  73. time_reftime = xtime.tv_sec;
  74. mtemp = xtime.tv_sec - time_reftime;
  75. time_reftime = xtime.tv_sec;
  76. freq_adj = (s64)offset * mtemp;
  77. freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
  78. time_status &= ~STA_MODE;
  79. if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
  80. freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
  81. mtemp);
  82. time_status |= STA_MODE;
  83. }
  84. freq_adj += time_freq;
  85. freq_adj = min(freq_adj, MAXFREQ_SCALED);
  86. time_freq = max(freq_adj, -MAXFREQ_SCALED);
  87. time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
  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. s64 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. time_tai--;
  148. wall_to_monotonic.tv_sec--;
  149. time_state = TIME_WAIT;
  150. printk(KERN_NOTICE "Clock: deleting leap second "
  151. "23:59:59 UTC\n");
  152. }
  153. break;
  154. case TIME_OOP:
  155. time_tai++;
  156. time_state = TIME_WAIT;
  157. break;
  158. case TIME_WAIT:
  159. if (!(time_status & (STA_INS | STA_DEL)))
  160. time_state = TIME_OK;
  161. }
  162. /*
  163. * Compute the phase adjustment for the next second. The offset is
  164. * reduced by a fixed factor times the time constant.
  165. */
  166. tick_length = tick_length_base;
  167. time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
  168. time_offset -= time_adj;
  169. tick_length += time_adj;
  170. if (unlikely(time_adjust)) {
  171. if (time_adjust > MAX_TICKADJ) {
  172. time_adjust -= MAX_TICKADJ;
  173. tick_length += MAX_TICKADJ_SCALED;
  174. } else if (time_adjust < -MAX_TICKADJ) {
  175. time_adjust += MAX_TICKADJ;
  176. tick_length -= MAX_TICKADJ_SCALED;
  177. } else {
  178. tick_length += (s64)(time_adjust * NSEC_PER_USEC /
  179. NTP_INTERVAL_FREQ) << NTP_SCALE_SHIFT;
  180. time_adjust = 0;
  181. }
  182. }
  183. }
  184. #ifdef CONFIG_GENERIC_CMOS_UPDATE
  185. /* Disable the cmos update - used by virtualization and embedded */
  186. int no_sync_cmos_clock __read_mostly;
  187. static void sync_cmos_clock(unsigned long dummy);
  188. static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
  189. static void sync_cmos_clock(unsigned long dummy)
  190. {
  191. struct timespec now, next;
  192. int fail = 1;
  193. /*
  194. * If we have an externally synchronized Linux clock, then update
  195. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  196. * called as close as possible to 500 ms before the new second starts.
  197. * This code is run on a timer. If the clock is set, that timer
  198. * may not expire at the correct time. Thus, we adjust...
  199. */
  200. if (!ntp_synced())
  201. /*
  202. * Not synced, exit, do not restart a timer (if one is
  203. * running, let it run out).
  204. */
  205. return;
  206. getnstimeofday(&now);
  207. if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
  208. fail = update_persistent_clock(now);
  209. next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
  210. if (next.tv_nsec <= 0)
  211. next.tv_nsec += NSEC_PER_SEC;
  212. if (!fail)
  213. next.tv_sec = 659;
  214. else
  215. next.tv_sec = 0;
  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. static void notify_cmos_timer(void)
  223. {
  224. if (!no_sync_cmos_clock)
  225. mod_timer(&sync_cmos_timer, jiffies + 1);
  226. }
  227. #else
  228. static inline void notify_cmos_timer(void) { }
  229. #endif
  230. /* adjtimex mainly allows reading (and writing, if superuser) of
  231. * kernel time-keeping variables. used by xntpd.
  232. */
  233. int do_adjtimex(struct timex *txc)
  234. {
  235. struct timespec ts;
  236. long save_adjust;
  237. int result;
  238. /* In order to modify anything, you gotta be super-user! */
  239. if (txc->modes && !capable(CAP_SYS_TIME))
  240. return -EPERM;
  241. /* Now we validate the data before disabling interrupts */
  242. if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) {
  243. /* singleshot must not be used with any other mode bits */
  244. if (txc->modes & ~ADJ_OFFSET_SS_READ)
  245. return -EINVAL;
  246. }
  247. /* if the quartz is off by more than 10% something is VERY wrong ! */
  248. if (txc->modes & ADJ_TICK)
  249. if (txc->tick < 900000/USER_HZ ||
  250. txc->tick > 1100000/USER_HZ)
  251. return -EINVAL;
  252. write_seqlock_irq(&xtime_lock);
  253. /* Save for later - semantics of adjtime is to return old value */
  254. save_adjust = time_adjust;
  255. /* If there are input parameters, then process them */
  256. if (txc->modes) {
  257. if (txc->modes & ADJ_STATUS) {
  258. if ((time_status & STA_PLL) &&
  259. !(txc->status & STA_PLL)) {
  260. time_state = TIME_OK;
  261. time_status = STA_UNSYNC;
  262. }
  263. /* only set allowed bits */
  264. time_status &= STA_RONLY;
  265. time_status |= txc->status & ~STA_RONLY;
  266. }
  267. if (txc->modes & ADJ_NANO)
  268. time_status |= STA_NANO;
  269. if (txc->modes & ADJ_MICRO)
  270. time_status &= ~STA_NANO;
  271. if (txc->modes & ADJ_FREQUENCY) {
  272. time_freq = (s64)txc->freq * PPM_SCALE;
  273. time_freq = min(time_freq, MAXFREQ_SCALED);
  274. time_freq = max(time_freq, -MAXFREQ_SCALED);
  275. }
  276. if (txc->modes & ADJ_MAXERROR)
  277. time_maxerror = txc->maxerror;
  278. if (txc->modes & ADJ_ESTERROR)
  279. time_esterror = txc->esterror;
  280. if (txc->modes & ADJ_TIMECONST) {
  281. time_constant = txc->constant;
  282. if (!(time_status & STA_NANO))
  283. time_constant += 4;
  284. time_constant = min(time_constant, (long)MAXTC);
  285. time_constant = max(time_constant, 0l);
  286. }
  287. if (txc->modes & ADJ_TAI && txc->constant > 0)
  288. time_tai = txc->constant;
  289. if (txc->modes & ADJ_OFFSET) {
  290. if (txc->modes == ADJ_OFFSET_SINGLESHOT)
  291. /* adjtime() is independent from ntp_adjtime() */
  292. time_adjust = txc->offset;
  293. else
  294. ntp_update_offset(txc->offset);
  295. }
  296. if (txc->modes & ADJ_TICK)
  297. tick_usec = txc->tick;
  298. if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
  299. ntp_update_frequency();
  300. }
  301. result = time_state; /* mostly `TIME_OK' */
  302. if (time_status & (STA_UNSYNC|STA_CLOCKERR))
  303. result = TIME_ERROR;
  304. if ((txc->modes == ADJ_OFFSET_SINGLESHOT) ||
  305. (txc->modes == ADJ_OFFSET_SS_READ))
  306. txc->offset = save_adjust;
  307. else {
  308. txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
  309. NTP_SCALE_SHIFT);
  310. if (!(time_status & STA_NANO))
  311. txc->offset /= NSEC_PER_USEC;
  312. }
  313. txc->freq = shift_right((s32)(time_freq >> PPM_SCALE_INV_SHIFT) *
  314. (s64)PPM_SCALE_INV,
  315. NTP_SCALE_SHIFT);
  316. txc->maxerror = time_maxerror;
  317. txc->esterror = time_esterror;
  318. txc->status = time_status;
  319. txc->constant = time_constant;
  320. txc->precision = 1;
  321. txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
  322. txc->tick = tick_usec;
  323. txc->tai = time_tai;
  324. /* PPS is not implemented, so these are zero */
  325. txc->ppsfreq = 0;
  326. txc->jitter = 0;
  327. txc->shift = 0;
  328. txc->stabil = 0;
  329. txc->jitcnt = 0;
  330. txc->calcnt = 0;
  331. txc->errcnt = 0;
  332. txc->stbcnt = 0;
  333. write_sequnlock_irq(&xtime_lock);
  334. getnstimeofday(&ts);
  335. txc->time.tv_sec = ts.tv_sec;
  336. txc->time.tv_usec = ts.tv_nsec;
  337. if (!(time_status & STA_NANO))
  338. txc->time.tv_usec /= NSEC_PER_USEC;
  339. notify_cmos_timer();
  340. return result;
  341. }
  342. static int __init ntp_tick_adj_setup(char *str)
  343. {
  344. ntp_tick_adj = simple_strtol(str, NULL, 0);
  345. return 1;
  346. }
  347. __setup("ntp_tick_adj=", ntp_tick_adj_setup);