ntp.c 13 KB

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