ntp.c 13 KB

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