ntp.c 11 KB

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