ntp.c 12 KB

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