ntp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 (ns) */
  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; /* frequency offset (scaled ppm)*/
  36. long time_reftime; /* time at last adjustment (s) */
  37. long time_adjust;
  38. long time_next_adjust;
  39. /**
  40. * ntp_clear - Clears the NTP state variables
  41. *
  42. * Must be called while holding a write on the xtime_lock
  43. */
  44. void ntp_clear(void)
  45. {
  46. time_adjust = 0; /* stop active adjtime() */
  47. time_status |= STA_UNSYNC;
  48. time_maxerror = NTP_PHASE_LIMIT;
  49. time_esterror = NTP_PHASE_LIMIT;
  50. ntp_update_frequency();
  51. tick_length = tick_length_base;
  52. time_offset = 0;
  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. tick_length_base += ((s64)time_freq * NSEC_PER_USEC) << (TICK_LENGTH_SHIFT - SHIFT_USEC);
  61. do_div(tick_length_base, HZ);
  62. tick_nsec = tick_length_base >> TICK_LENGTH_SHIFT;
  63. }
  64. /*
  65. * this routine handles the overflow of the microsecond field
  66. *
  67. * The tricky bits of code to handle the accurate clock support
  68. * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
  69. * They were originally developed for SUN and DEC kernels.
  70. * All the kudos should go to Dave for this stuff.
  71. */
  72. void second_overflow(void)
  73. {
  74. long time_adj;
  75. /* Bump the maxerror field */
  76. time_maxerror += time_tolerance >> SHIFT_USEC;
  77. if (time_maxerror > NTP_PHASE_LIMIT) {
  78. time_maxerror = NTP_PHASE_LIMIT;
  79. time_status |= STA_UNSYNC;
  80. }
  81. /*
  82. * Leap second processing. If in leap-insert state at the end of the
  83. * day, the system clock is set back one second; if in leap-delete
  84. * state, the system clock is set ahead one second. The microtime()
  85. * routine or external clock driver will insure that reported time is
  86. * always monotonic. The ugly divides should be replaced.
  87. */
  88. switch (time_state) {
  89. case TIME_OK:
  90. if (time_status & STA_INS)
  91. time_state = TIME_INS;
  92. else if (time_status & STA_DEL)
  93. time_state = TIME_DEL;
  94. break;
  95. case TIME_INS:
  96. if (xtime.tv_sec % 86400 == 0) {
  97. xtime.tv_sec--;
  98. wall_to_monotonic.tv_sec++;
  99. /*
  100. * The timer interpolator will make time change
  101. * gradually instead of an immediate jump by one second
  102. */
  103. time_interpolator_update(-NSEC_PER_SEC);
  104. time_state = TIME_OOP;
  105. clock_was_set();
  106. printk(KERN_NOTICE "Clock: inserting leap second "
  107. "23:59:60 UTC\n");
  108. }
  109. break;
  110. case TIME_DEL:
  111. if ((xtime.tv_sec + 1) % 86400 == 0) {
  112. xtime.tv_sec++;
  113. wall_to_monotonic.tv_sec--;
  114. /*
  115. * Use of time interpolator for a gradual change of
  116. * time
  117. */
  118. time_interpolator_update(NSEC_PER_SEC);
  119. time_state = TIME_WAIT;
  120. clock_was_set();
  121. printk(KERN_NOTICE "Clock: deleting leap second "
  122. "23:59:59 UTC\n");
  123. }
  124. break;
  125. case TIME_OOP:
  126. time_state = TIME_WAIT;
  127. break;
  128. case TIME_WAIT:
  129. if (!(time_status & (STA_INS | STA_DEL)))
  130. time_state = TIME_OK;
  131. }
  132. /*
  133. * Compute the phase adjustment for the next second. In PLL mode, the
  134. * offset is reduced by a fixed factor times the time constant. In FLL
  135. * mode the offset is used directly. In either mode, the maximum phase
  136. * adjustment for each second is clamped so as to spread the adjustment
  137. * over not more than the number of seconds between updates.
  138. */
  139. tick_length = tick_length_base;
  140. time_adj = time_offset;
  141. if (!(time_status & STA_FLL))
  142. time_adj = shift_right(time_adj, SHIFT_KG + time_constant);
  143. time_adj = min(time_adj, -((MAXPHASE / HZ) << SHIFT_UPDATE) / MINSEC);
  144. time_adj = max(time_adj, ((MAXPHASE / HZ) << SHIFT_UPDATE) / MINSEC);
  145. time_offset -= time_adj;
  146. tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
  147. }
  148. /*
  149. * Returns how many microseconds we need to add to xtime this tick
  150. * in doing an adjustment requested with adjtime.
  151. */
  152. static long adjtime_adjustment(void)
  153. {
  154. long time_adjust_step;
  155. time_adjust_step = time_adjust;
  156. if (time_adjust_step) {
  157. /*
  158. * We are doing an adjtime thing. Prepare time_adjust_step to
  159. * be within bounds. Note that a positive time_adjust means we
  160. * want the clock to run faster.
  161. *
  162. * Limit the amount of the step to be in the range
  163. * -tickadj .. +tickadj
  164. */
  165. time_adjust_step = min(time_adjust_step, (long)tickadj);
  166. time_adjust_step = max(time_adjust_step, (long)-tickadj);
  167. }
  168. return time_adjust_step;
  169. }
  170. /* in the NTP reference this is called "hardclock()" */
  171. void update_ntp_one_tick(void)
  172. {
  173. long time_adjust_step;
  174. time_adjust_step = adjtime_adjustment();
  175. if (time_adjust_step)
  176. /* Reduce by this step the amount of time left */
  177. time_adjust -= time_adjust_step;
  178. /* Changes by adjtime() do not take effect till next tick. */
  179. if (time_next_adjust != 0) {
  180. time_adjust = time_next_adjust;
  181. time_next_adjust = 0;
  182. }
  183. }
  184. /*
  185. * Return how long ticks are at the moment, that is, how much time
  186. * update_wall_time_one_tick will add to xtime next time we call it
  187. * (assuming no calls to do_adjtimex in the meantime).
  188. * The return value is in fixed-point nanoseconds shifted by the
  189. * specified number of bits to the right of the binary point.
  190. * This function has no side-effects.
  191. */
  192. u64 current_tick_length(void)
  193. {
  194. u64 ret;
  195. /* calculate the finest interval NTP will allow.
  196. */
  197. ret = tick_length;
  198. ret += (u64)(adjtime_adjustment() * 1000) << TICK_LENGTH_SHIFT;
  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. time_offset = min(ltemp, MAXPHASE);
  283. time_offset = max(time_offset, -MAXPHASE);
  284. /*
  285. * Select whether the frequency is to be controlled
  286. * and in which mode (PLL or FLL). Clamp to the operating
  287. * range. Ugly multiply/divide should be replaced someday.
  288. */
  289. if (time_status & STA_FREQHOLD || time_reftime == 0)
  290. time_reftime = xtime.tv_sec;
  291. mtemp = xtime.tv_sec - time_reftime;
  292. time_reftime = xtime.tv_sec;
  293. if (time_status & STA_FLL) {
  294. if (mtemp >= MINSEC) {
  295. ltemp = ((time_offset << 12) / mtemp) << (SHIFT_USEC - 12);
  296. time_freq += shift_right(ltemp, SHIFT_KH);
  297. } else /* calibration interval too short (p. 12) */
  298. result = TIME_ERROR;
  299. } else { /* PLL mode */
  300. if (mtemp < MAXSEC) {
  301. ltemp *= mtemp;
  302. time_freq += shift_right(ltemp,(time_constant +
  303. time_constant +
  304. SHIFT_KF - SHIFT_USEC));
  305. } else /* calibration interval too long (p. 12) */
  306. result = TIME_ERROR;
  307. }
  308. time_freq = min(time_freq, time_tolerance);
  309. time_freq = max(time_freq, -time_tolerance);
  310. time_offset = (time_offset * NSEC_PER_USEC / HZ) << SHIFT_UPDATE;
  311. } /* STA_PLL */
  312. } /* txc->modes & ADJ_OFFSET */
  313. if (txc->modes & ADJ_TICK)
  314. tick_usec = txc->tick;
  315. if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
  316. ntp_update_frequency();
  317. } /* txc->modes */
  318. leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
  319. result = TIME_ERROR;
  320. if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
  321. txc->offset = save_adjust;
  322. else
  323. txc->offset = shift_right(time_offset, SHIFT_UPDATE) * HZ / 1000;
  324. txc->freq = time_freq;
  325. txc->maxerror = time_maxerror;
  326. txc->esterror = time_esterror;
  327. txc->status = time_status;
  328. txc->constant = time_constant;
  329. txc->precision = time_precision;
  330. txc->tolerance = time_tolerance;
  331. txc->tick = tick_usec;
  332. /* PPS is not implemented, so these are zero */
  333. txc->ppsfreq = 0;
  334. txc->jitter = 0;
  335. txc->shift = 0;
  336. txc->stabil = 0;
  337. txc->jitcnt = 0;
  338. txc->calcnt = 0;
  339. txc->errcnt = 0;
  340. txc->stbcnt = 0;
  341. write_sequnlock_irq(&xtime_lock);
  342. do_gettimeofday(&txc->time);
  343. notify_arch_cmos_timer();
  344. return(result);
  345. }