ntp.c 10 KB

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