ntp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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/timex.h>
  13. #include <asm/div64.h>
  14. #include <asm/timex.h>
  15. /*
  16. * Timekeeping variables
  17. */
  18. unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
  19. unsigned long tick_nsec; /* ACTHZ period (nsec) */
  20. static u64 tick_length, tick_length_base;
  21. /* Don't completely fail for HZ > 500. */
  22. int tickadj = 500/HZ ? : 1; /* microsecs */
  23. /*
  24. * phase-lock loop variables
  25. */
  26. /* TIME_ERROR prevents overwriting the CMOS clock */
  27. int time_state = TIME_OK; /* clock synchronization status */
  28. int time_status = STA_UNSYNC; /* clock status bits */
  29. long time_offset; /* time adjustment (us) */
  30. long time_constant = 2; /* pll time constant */
  31. long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
  32. long time_precision = 1; /* clock precision (us) */
  33. long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
  34. long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
  35. long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
  36. /* frequency offset (scaled ppm)*/
  37. long time_reftime; /* time at last adjustment (s) */
  38. long time_adjust;
  39. long time_next_adjust;
  40. /**
  41. * ntp_clear - Clears the NTP state variables
  42. *
  43. * Must be called while holding a write on the xtime_lock
  44. */
  45. void ntp_clear(void)
  46. {
  47. time_adjust = 0; /* stop active adjtime() */
  48. time_status |= STA_UNSYNC;
  49. time_maxerror = NTP_PHASE_LIMIT;
  50. time_esterror = NTP_PHASE_LIMIT;
  51. ntp_update_frequency();
  52. tick_length = tick_length_base;
  53. }
  54. #define CLOCK_TICK_OVERFLOW (LATCH * HZ - CLOCK_TICK_RATE)
  55. #define CLOCK_TICK_ADJUST (((s64)CLOCK_TICK_OVERFLOW * NSEC_PER_SEC) / (s64)CLOCK_TICK_RATE)
  56. void ntp_update_frequency(void)
  57. {
  58. tick_length_base = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) << TICK_LENGTH_SHIFT;
  59. tick_length_base += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT;
  60. do_div(tick_length_base, HZ);
  61. tick_nsec = tick_length_base >> TICK_LENGTH_SHIFT;
  62. }
  63. /*
  64. * this routine handles the overflow of the microsecond field
  65. *
  66. * The tricky bits of code to handle the accurate clock support
  67. * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
  68. * They were originally developed for SUN and DEC kernels.
  69. * All the kudos should go to Dave for this stuff.
  70. */
  71. void second_overflow(void)
  72. {
  73. long ltemp, time_adj;
  74. /* Bump the maxerror field */
  75. time_maxerror += time_tolerance >> SHIFT_USEC;
  76. if (time_maxerror > NTP_PHASE_LIMIT) {
  77. time_maxerror = NTP_PHASE_LIMIT;
  78. time_status |= STA_UNSYNC;
  79. }
  80. /*
  81. * Leap second processing. If in leap-insert state at the end of the
  82. * day, the system clock is set back one second; if in leap-delete
  83. * state, the system clock is set ahead one second. The microtime()
  84. * routine or external clock driver will insure that reported time is
  85. * always monotonic. The ugly divides should be replaced.
  86. */
  87. switch (time_state) {
  88. case TIME_OK:
  89. if (time_status & STA_INS)
  90. time_state = TIME_INS;
  91. else if (time_status & STA_DEL)
  92. time_state = TIME_DEL;
  93. break;
  94. case TIME_INS:
  95. if (xtime.tv_sec % 86400 == 0) {
  96. xtime.tv_sec--;
  97. wall_to_monotonic.tv_sec++;
  98. /*
  99. * The timer interpolator will make time change
  100. * gradually instead of an immediate jump by one second
  101. */
  102. time_interpolator_update(-NSEC_PER_SEC);
  103. time_state = TIME_OOP;
  104. clock_was_set();
  105. printk(KERN_NOTICE "Clock: inserting leap second "
  106. "23:59:60 UTC\n");
  107. }
  108. break;
  109. case TIME_DEL:
  110. if ((xtime.tv_sec + 1) % 86400 == 0) {
  111. xtime.tv_sec++;
  112. wall_to_monotonic.tv_sec--;
  113. /*
  114. * Use of time interpolator for a gradual change of
  115. * time
  116. */
  117. time_interpolator_update(NSEC_PER_SEC);
  118. time_state = TIME_WAIT;
  119. clock_was_set();
  120. printk(KERN_NOTICE "Clock: deleting leap second "
  121. "23:59:59 UTC\n");
  122. }
  123. break;
  124. case TIME_OOP:
  125. time_state = TIME_WAIT;
  126. break;
  127. case TIME_WAIT:
  128. if (!(time_status & (STA_INS | STA_DEL)))
  129. time_state = TIME_OK;
  130. }
  131. /*
  132. * Compute the phase adjustment for the next second. In PLL mode, the
  133. * offset is reduced by a fixed factor times the time constant. In FLL
  134. * mode the offset is used directly. In either mode, the maximum phase
  135. * adjustment for each second is clamped so as to spread the adjustment
  136. * over not more than the number of seconds between updates.
  137. */
  138. ltemp = time_offset;
  139. if (!(time_status & STA_FLL))
  140. ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
  141. ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
  142. ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
  143. time_offset -= ltemp;
  144. time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
  145. /*
  146. * Compute the frequency estimate and additional phase adjustment due
  147. * to frequency error for the next second.
  148. */
  149. ltemp = time_freq;
  150. time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
  151. #if HZ == 100
  152. /*
  153. * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
  154. * get 128.125; => only 0.125% error (p. 14)
  155. */
  156. time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
  157. #endif
  158. #if HZ == 250
  159. /*
  160. * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
  161. * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
  162. */
  163. time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
  164. #endif
  165. #if HZ == 1000
  166. /*
  167. * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
  168. * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
  169. */
  170. time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
  171. #endif
  172. tick_length = tick_length_base;
  173. tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - (SHIFT_SCALE - 10));
  174. }
  175. /*
  176. * Returns how many microseconds we need to add to xtime this tick
  177. * in doing an adjustment requested with adjtime.
  178. */
  179. static long adjtime_adjustment(void)
  180. {
  181. long time_adjust_step;
  182. time_adjust_step = time_adjust;
  183. if (time_adjust_step) {
  184. /*
  185. * We are doing an adjtime thing. Prepare time_adjust_step to
  186. * be within bounds. Note that a positive time_adjust means we
  187. * want the clock to run faster.
  188. *
  189. * Limit the amount of the step to be in the range
  190. * -tickadj .. +tickadj
  191. */
  192. time_adjust_step = min(time_adjust_step, (long)tickadj);
  193. time_adjust_step = max(time_adjust_step, (long)-tickadj);
  194. }
  195. return time_adjust_step;
  196. }
  197. /* in the NTP reference this is called "hardclock()" */
  198. void update_ntp_one_tick(void)
  199. {
  200. long time_adjust_step;
  201. time_adjust_step = adjtime_adjustment();
  202. if (time_adjust_step)
  203. /* Reduce by this step the amount of time left */
  204. time_adjust -= time_adjust_step;
  205. /* Changes by adjtime() do not take effect till next tick. */
  206. if (time_next_adjust != 0) {
  207. time_adjust = time_next_adjust;
  208. time_next_adjust = 0;
  209. }
  210. }
  211. /*
  212. * Return how long ticks are at the moment, that is, how much time
  213. * update_wall_time_one_tick will add to xtime next time we call it
  214. * (assuming no calls to do_adjtimex in the meantime).
  215. * The return value is in fixed-point nanoseconds shifted by the
  216. * specified number of bits to the right of the binary point.
  217. * This function has no side-effects.
  218. */
  219. u64 current_tick_length(void)
  220. {
  221. u64 ret;
  222. /* calculate the finest interval NTP will allow.
  223. */
  224. ret = tick_length;
  225. ret += (u64)(adjtime_adjustment() * 1000) << TICK_LENGTH_SHIFT;
  226. return ret;
  227. }
  228. void __attribute__ ((weak)) notify_arch_cmos_timer(void)
  229. {
  230. return;
  231. }
  232. /* adjtimex mainly allows reading (and writing, if superuser) of
  233. * kernel time-keeping variables. used by xntpd.
  234. */
  235. int do_adjtimex(struct timex *txc)
  236. {
  237. long ltemp, mtemp, save_adjust;
  238. int result;
  239. /* In order to modify anything, you gotta be super-user! */
  240. if (txc->modes && !capable(CAP_SYS_TIME))
  241. return -EPERM;
  242. /* Now we validate the data before disabling interrupts */
  243. if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
  244. /* singleshot must not be used with any other mode bits */
  245. if (txc->modes != ADJ_OFFSET_SINGLESHOT)
  246. return -EINVAL;
  247. if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
  248. /* adjustment Offset limited to +- .512 seconds */
  249. if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
  250. return -EINVAL;
  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. write_seqlock_irq(&xtime_lock);
  257. result = time_state; /* mostly `TIME_OK' */
  258. /* Save for later - semantics of adjtime is to return old value */
  259. save_adjust = time_next_adjust ? time_next_adjust : time_adjust;
  260. #if 0 /* STA_CLOCKERR is never set yet */
  261. time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
  262. #endif
  263. /* If there are input parameters, then process them */
  264. if (txc->modes)
  265. {
  266. if (txc->modes & ADJ_STATUS) /* only set allowed bits */
  267. time_status = (txc->status & ~STA_RONLY) |
  268. (time_status & STA_RONLY);
  269. if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
  270. if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
  271. result = -EINVAL;
  272. goto leave;
  273. }
  274. time_freq = txc->freq;
  275. }
  276. if (txc->modes & ADJ_MAXERROR) {
  277. if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
  278. result = -EINVAL;
  279. goto leave;
  280. }
  281. time_maxerror = txc->maxerror;
  282. }
  283. if (txc->modes & ADJ_ESTERROR) {
  284. if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
  285. result = -EINVAL;
  286. goto leave;
  287. }
  288. time_esterror = txc->esterror;
  289. }
  290. if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
  291. if (txc->constant < 0) { /* NTP v4 uses values > 6 */
  292. result = -EINVAL;
  293. goto leave;
  294. }
  295. time_constant = txc->constant;
  296. }
  297. if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
  298. if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
  299. /* adjtime() is independent from ntp_adjtime() */
  300. if ((time_next_adjust = txc->offset) == 0)
  301. time_adjust = 0;
  302. }
  303. else if (time_status & STA_PLL) {
  304. ltemp = txc->offset;
  305. /*
  306. * Scale the phase adjustment and
  307. * clamp to the operating range.
  308. */
  309. if (ltemp > MAXPHASE)
  310. time_offset = MAXPHASE << SHIFT_UPDATE;
  311. else if (ltemp < -MAXPHASE)
  312. time_offset = -(MAXPHASE << SHIFT_UPDATE);
  313. else
  314. time_offset = ltemp << SHIFT_UPDATE;
  315. /*
  316. * Select whether the frequency is to be controlled
  317. * and in which mode (PLL or FLL). Clamp to the operating
  318. * range. Ugly multiply/divide should be replaced someday.
  319. */
  320. if (time_status & STA_FREQHOLD || time_reftime == 0)
  321. time_reftime = xtime.tv_sec;
  322. mtemp = xtime.tv_sec - time_reftime;
  323. time_reftime = xtime.tv_sec;
  324. if (time_status & STA_FLL) {
  325. if (mtemp >= MINSEC) {
  326. ltemp = (time_offset / mtemp) << (SHIFT_USEC -
  327. SHIFT_UPDATE);
  328. time_freq += shift_right(ltemp, SHIFT_KH);
  329. } else /* calibration interval too short (p. 12) */
  330. result = TIME_ERROR;
  331. } else { /* PLL mode */
  332. if (mtemp < MAXSEC) {
  333. ltemp *= mtemp;
  334. time_freq += shift_right(ltemp,(time_constant +
  335. time_constant +
  336. SHIFT_KF - SHIFT_USEC));
  337. } else /* calibration interval too long (p. 12) */
  338. result = TIME_ERROR;
  339. }
  340. time_freq = min(time_freq, time_tolerance);
  341. time_freq = max(time_freq, -time_tolerance);
  342. } /* STA_PLL */
  343. } /* txc->modes & ADJ_OFFSET */
  344. if (txc->modes & ADJ_TICK)
  345. tick_usec = txc->tick;
  346. if (txc->modes & ADJ_TICK)
  347. ntp_update_frequency();
  348. } /* txc->modes */
  349. leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
  350. result = TIME_ERROR;
  351. if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
  352. txc->offset = save_adjust;
  353. else {
  354. txc->offset = shift_right(time_offset, SHIFT_UPDATE);
  355. }
  356. txc->freq = time_freq;
  357. txc->maxerror = time_maxerror;
  358. txc->esterror = time_esterror;
  359. txc->status = time_status;
  360. txc->constant = time_constant;
  361. txc->precision = time_precision;
  362. txc->tolerance = time_tolerance;
  363. txc->tick = tick_usec;
  364. /* PPS is not implemented, so these are zero */
  365. txc->ppsfreq = 0;
  366. txc->jitter = 0;
  367. txc->shift = 0;
  368. txc->stabil = 0;
  369. txc->jitcnt = 0;
  370. txc->calcnt = 0;
  371. txc->errcnt = 0;
  372. txc->stbcnt = 0;
  373. write_sequnlock_irq(&xtime_lock);
  374. do_gettimeofday(&txc->time);
  375. notify_arch_cmos_timer();
  376. return(result);
  377. }