ntp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #define MAX_TICKADJ 500 /* microsecs */
  22. #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
  23. TICK_LENGTH_SHIFT) / HZ)
  24. /*
  25. * phase-lock loop variables
  26. */
  27. /* TIME_ERROR prevents overwriting the CMOS clock */
  28. int time_state = TIME_OK; /* clock synchronization status */
  29. int time_status = STA_UNSYNC; /* clock status bits */
  30. long time_offset; /* time adjustment (ns) */
  31. long time_constant = 2; /* pll time constant */
  32. long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
  33. long time_precision = 1; /* clock precision (us) */
  34. long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
  35. long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
  36. long time_freq; /* frequency offset (scaled ppm)*/
  37. long time_reftime; /* time at last adjustment (s) */
  38. long time_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. if (unlikely(time_adjust)) {
  148. if (time_adjust > MAX_TICKADJ) {
  149. time_adjust -= MAX_TICKADJ;
  150. tick_length += MAX_TICKADJ_SCALED;
  151. } else if (time_adjust < -MAX_TICKADJ) {
  152. time_adjust += MAX_TICKADJ;
  153. tick_length -= MAX_TICKADJ_SCALED;
  154. } else {
  155. time_adjust = 0;
  156. tick_length += (s64)(time_adjust * NSEC_PER_USEC /
  157. HZ) << TICK_LENGTH_SHIFT;
  158. }
  159. }
  160. }
  161. /*
  162. * Return how long ticks are at the moment, that is, how much time
  163. * update_wall_time_one_tick will add to xtime next time we call it
  164. * (assuming no calls to do_adjtimex in the meantime).
  165. * The return value is in fixed-point nanoseconds shifted by the
  166. * specified number of bits to the right of the binary point.
  167. * This function has no side-effects.
  168. */
  169. u64 current_tick_length(void)
  170. {
  171. return tick_length;
  172. }
  173. void __attribute__ ((weak)) notify_arch_cmos_timer(void)
  174. {
  175. return;
  176. }
  177. /* adjtimex mainly allows reading (and writing, if superuser) of
  178. * kernel time-keeping variables. used by xntpd.
  179. */
  180. int do_adjtimex(struct timex *txc)
  181. {
  182. long ltemp, mtemp, save_adjust;
  183. int result;
  184. /* In order to modify anything, you gotta be super-user! */
  185. if (txc->modes && !capable(CAP_SYS_TIME))
  186. return -EPERM;
  187. /* Now we validate the data before disabling interrupts */
  188. if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
  189. /* singleshot must not be used with any other mode bits */
  190. if (txc->modes != ADJ_OFFSET_SINGLESHOT)
  191. return -EINVAL;
  192. if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
  193. /* adjustment Offset limited to +- .512 seconds */
  194. if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
  195. return -EINVAL;
  196. /* if the quartz is off by more than 10% something is VERY wrong ! */
  197. if (txc->modes & ADJ_TICK)
  198. if (txc->tick < 900000/USER_HZ ||
  199. txc->tick > 1100000/USER_HZ)
  200. return -EINVAL;
  201. write_seqlock_irq(&xtime_lock);
  202. result = time_state; /* mostly `TIME_OK' */
  203. /* Save for later - semantics of adjtime is to return old value */
  204. save_adjust = time_adjust;
  205. #if 0 /* STA_CLOCKERR is never set yet */
  206. time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
  207. #endif
  208. /* If there are input parameters, then process them */
  209. if (txc->modes)
  210. {
  211. if (txc->modes & ADJ_STATUS) /* only set allowed bits */
  212. time_status = (txc->status & ~STA_RONLY) |
  213. (time_status & STA_RONLY);
  214. if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
  215. if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
  216. result = -EINVAL;
  217. goto leave;
  218. }
  219. time_freq = txc->freq;
  220. }
  221. if (txc->modes & ADJ_MAXERROR) {
  222. if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
  223. result = -EINVAL;
  224. goto leave;
  225. }
  226. time_maxerror = txc->maxerror;
  227. }
  228. if (txc->modes & ADJ_ESTERROR) {
  229. if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
  230. result = -EINVAL;
  231. goto leave;
  232. }
  233. time_esterror = txc->esterror;
  234. }
  235. if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
  236. if (txc->constant < 0) { /* NTP v4 uses values > 6 */
  237. result = -EINVAL;
  238. goto leave;
  239. }
  240. time_constant = txc->constant;
  241. }
  242. if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
  243. if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
  244. /* adjtime() is independent from ntp_adjtime() */
  245. time_adjust = txc->offset;
  246. }
  247. else if (time_status & STA_PLL) {
  248. ltemp = txc->offset;
  249. /*
  250. * Scale the phase adjustment and
  251. * clamp to the operating range.
  252. */
  253. time_offset = min(ltemp, MAXPHASE);
  254. time_offset = max(time_offset, -MAXPHASE);
  255. /*
  256. * Select whether the frequency is to be controlled
  257. * and in which mode (PLL or FLL). Clamp to the operating
  258. * range. Ugly multiply/divide should be replaced someday.
  259. */
  260. if (time_status & STA_FREQHOLD || time_reftime == 0)
  261. time_reftime = xtime.tv_sec;
  262. mtemp = xtime.tv_sec - time_reftime;
  263. time_reftime = xtime.tv_sec;
  264. if (time_status & STA_FLL) {
  265. if (mtemp >= MINSEC) {
  266. ltemp = ((time_offset << 12) / mtemp) << (SHIFT_USEC - 12);
  267. time_freq += shift_right(ltemp, SHIFT_KH);
  268. } else /* calibration interval too short (p. 12) */
  269. result = TIME_ERROR;
  270. } else { /* PLL mode */
  271. if (mtemp < MAXSEC) {
  272. ltemp *= mtemp;
  273. time_freq += shift_right(ltemp,(time_constant +
  274. time_constant +
  275. SHIFT_KF - SHIFT_USEC));
  276. } else /* calibration interval too long (p. 12) */
  277. result = TIME_ERROR;
  278. }
  279. time_freq = min(time_freq, time_tolerance);
  280. time_freq = max(time_freq, -time_tolerance);
  281. time_offset = (time_offset * NSEC_PER_USEC / HZ) << SHIFT_UPDATE;
  282. } /* STA_PLL */
  283. } /* txc->modes & ADJ_OFFSET */
  284. if (txc->modes & ADJ_TICK)
  285. tick_usec = txc->tick;
  286. if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
  287. ntp_update_frequency();
  288. } /* txc->modes */
  289. leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
  290. result = TIME_ERROR;
  291. if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
  292. txc->offset = save_adjust;
  293. else
  294. txc->offset = shift_right(time_offset, SHIFT_UPDATE) * HZ / 1000;
  295. txc->freq = time_freq;
  296. txc->maxerror = time_maxerror;
  297. txc->esterror = time_esterror;
  298. txc->status = time_status;
  299. txc->constant = time_constant;
  300. txc->precision = time_precision;
  301. txc->tolerance = time_tolerance;
  302. txc->tick = tick_usec;
  303. /* PPS is not implemented, so these are zero */
  304. txc->ppsfreq = 0;
  305. txc->jitter = 0;
  306. txc->shift = 0;
  307. txc->stabil = 0;
  308. txc->jitcnt = 0;
  309. txc->calcnt = 0;
  310. txc->errcnt = 0;
  311. txc->stbcnt = 0;
  312. write_sequnlock_irq(&xtime_lock);
  313. do_gettimeofday(&txc->time);
  314. notify_arch_cmos_timer();
  315. return(result);
  316. }