ntp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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) / NTP_INTERVAL_FREQ)
  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 s64 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. u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
  43. << TICK_LENGTH_SHIFT;
  44. second_length += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT;
  45. second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
  46. tick_length_base = second_length;
  47. do_div(second_length, HZ);
  48. tick_nsec = second_length >> TICK_LENGTH_SHIFT;
  49. do_div(tick_length_base, NTP_INTERVAL_FREQ);
  50. }
  51. /**
  52. * ntp_clear - Clears the NTP state variables
  53. *
  54. * Must be called while holding a write on the xtime_lock
  55. */
  56. void ntp_clear(void)
  57. {
  58. time_adjust = 0; /* stop active adjtime() */
  59. time_status |= STA_UNSYNC;
  60. time_maxerror = NTP_PHASE_LIMIT;
  61. time_esterror = NTP_PHASE_LIMIT;
  62. ntp_update_frequency();
  63. tick_length = tick_length_base;
  64. time_offset = 0;
  65. }
  66. /*
  67. * this routine handles the overflow of the microsecond field
  68. *
  69. * The tricky bits of code to handle the accurate clock support
  70. * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
  71. * They were originally developed for SUN and DEC kernels.
  72. * All the kudos should go to Dave for this stuff.
  73. */
  74. void second_overflow(void)
  75. {
  76. long time_adj;
  77. /* Bump the maxerror field */
  78. time_maxerror += MAXFREQ >> SHIFT_USEC;
  79. if (time_maxerror > NTP_PHASE_LIMIT) {
  80. time_maxerror = NTP_PHASE_LIMIT;
  81. time_status |= STA_UNSYNC;
  82. }
  83. /*
  84. * Leap second processing. If in leap-insert state at the end of the
  85. * day, the system clock is set back one second; if in leap-delete
  86. * state, the system clock is set ahead one second. The microtime()
  87. * routine or external clock driver will insure that reported time is
  88. * always monotonic. The ugly divides should be replaced.
  89. */
  90. switch (time_state) {
  91. case TIME_OK:
  92. if (time_status & STA_INS)
  93. time_state = TIME_INS;
  94. else if (time_status & STA_DEL)
  95. time_state = TIME_DEL;
  96. break;
  97. case TIME_INS:
  98. if (xtime.tv_sec % 86400 == 0) {
  99. xtime.tv_sec--;
  100. wall_to_monotonic.tv_sec++;
  101. /*
  102. * The timer interpolator will make time change
  103. * gradually instead of an immediate jump by one second
  104. */
  105. time_interpolator_update(-NSEC_PER_SEC);
  106. time_state = TIME_OOP;
  107. clock_was_set();
  108. printk(KERN_NOTICE "Clock: inserting leap second "
  109. "23:59:60 UTC\n");
  110. }
  111. break;
  112. case TIME_DEL:
  113. if ((xtime.tv_sec + 1) % 86400 == 0) {
  114. xtime.tv_sec++;
  115. wall_to_monotonic.tv_sec--;
  116. /*
  117. * Use of time interpolator for a gradual change of
  118. * time
  119. */
  120. time_interpolator_update(NSEC_PER_SEC);
  121. time_state = TIME_WAIT;
  122. clock_was_set();
  123. printk(KERN_NOTICE "Clock: deleting leap second "
  124. "23:59:59 UTC\n");
  125. }
  126. break;
  127. case TIME_OOP:
  128. time_state = TIME_WAIT;
  129. break;
  130. case TIME_WAIT:
  131. if (!(time_status & (STA_INS | STA_DEL)))
  132. time_state = TIME_OK;
  133. }
  134. /*
  135. * Compute the phase adjustment for the next second. The offset is
  136. * reduced by a fixed factor times the time constant.
  137. */
  138. tick_length = tick_length_base;
  139. time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
  140. time_offset -= time_adj;
  141. tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
  142. if (unlikely(time_adjust)) {
  143. if (time_adjust > MAX_TICKADJ) {
  144. time_adjust -= MAX_TICKADJ;
  145. tick_length += MAX_TICKADJ_SCALED;
  146. } else if (time_adjust < -MAX_TICKADJ) {
  147. time_adjust += MAX_TICKADJ;
  148. tick_length -= MAX_TICKADJ_SCALED;
  149. } else {
  150. tick_length += (s64)(time_adjust * NSEC_PER_USEC /
  151. NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
  152. time_adjust = 0;
  153. }
  154. }
  155. }
  156. /*
  157. * Return how long ticks are at the moment, that is, how much time
  158. * update_wall_time_one_tick will add to xtime next time we call it
  159. * (assuming no calls to do_adjtimex in the meantime).
  160. * The return value is in fixed-point nanoseconds shifted by the
  161. * specified number of bits to the right of the binary point.
  162. * This function has no side-effects.
  163. */
  164. u64 current_tick_length(void)
  165. {
  166. return tick_length;
  167. }
  168. void __attribute__ ((weak)) notify_arch_cmos_timer(void)
  169. {
  170. return;
  171. }
  172. /* adjtimex mainly allows reading (and writing, if superuser) of
  173. * kernel time-keeping variables. used by xntpd.
  174. */
  175. int do_adjtimex(struct timex *txc)
  176. {
  177. long mtemp, save_adjust, rem;
  178. s64 freq_adj, temp64;
  179. int result;
  180. /* In order to modify anything, you gotta be super-user! */
  181. if (txc->modes && !capable(CAP_SYS_TIME))
  182. return -EPERM;
  183. /* Now we validate the data before disabling interrupts */
  184. if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
  185. /* singleshot must not be used with any other mode bits */
  186. if (txc->modes != ADJ_OFFSET_SINGLESHOT)
  187. return -EINVAL;
  188. if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
  189. /* adjustment Offset limited to +- .512 seconds */
  190. if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
  191. return -EINVAL;
  192. /* if the quartz is off by more than 10% something is VERY wrong ! */
  193. if (txc->modes & ADJ_TICK)
  194. if (txc->tick < 900000/USER_HZ ||
  195. txc->tick > 1100000/USER_HZ)
  196. return -EINVAL;
  197. write_seqlock_irq(&xtime_lock);
  198. result = time_state; /* mostly `TIME_OK' */
  199. /* Save for later - semantics of adjtime is to return old value */
  200. save_adjust = time_adjust;
  201. #if 0 /* STA_CLOCKERR is never set yet */
  202. time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
  203. #endif
  204. /* If there are input parameters, then process them */
  205. if (txc->modes)
  206. {
  207. if (txc->modes & ADJ_STATUS) /* only set allowed bits */
  208. time_status = (txc->status & ~STA_RONLY) |
  209. (time_status & STA_RONLY);
  210. if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
  211. if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
  212. result = -EINVAL;
  213. goto leave;
  214. }
  215. time_freq = ((s64)txc->freq * NSEC_PER_USEC)
  216. >> (SHIFT_USEC - SHIFT_NSEC);
  217. }
  218. if (txc->modes & ADJ_MAXERROR) {
  219. if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
  220. result = -EINVAL;
  221. goto leave;
  222. }
  223. time_maxerror = txc->maxerror;
  224. }
  225. if (txc->modes & ADJ_ESTERROR) {
  226. if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
  227. result = -EINVAL;
  228. goto leave;
  229. }
  230. time_esterror = txc->esterror;
  231. }
  232. if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
  233. if (txc->constant < 0) { /* NTP v4 uses values > 6 */
  234. result = -EINVAL;
  235. goto leave;
  236. }
  237. time_constant = min(txc->constant + 4, (long)MAXTC);
  238. }
  239. if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
  240. if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
  241. /* adjtime() is independent from ntp_adjtime() */
  242. time_adjust = txc->offset;
  243. }
  244. else if (time_status & STA_PLL) {
  245. time_offset = txc->offset * NSEC_PER_USEC;
  246. /*
  247. * Scale the phase adjustment and
  248. * clamp to the operating range.
  249. */
  250. time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC);
  251. time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC);
  252. /*
  253. * Select whether the frequency is to be controlled
  254. * and in which mode (PLL or FLL). Clamp to the operating
  255. * range. Ugly multiply/divide should be replaced someday.
  256. */
  257. if (time_status & STA_FREQHOLD || time_reftime == 0)
  258. time_reftime = xtime.tv_sec;
  259. mtemp = xtime.tv_sec - time_reftime;
  260. time_reftime = xtime.tv_sec;
  261. freq_adj = time_offset * mtemp;
  262. freq_adj = shift_right(freq_adj, time_constant * 2 +
  263. (SHIFT_PLL + 2) * 2 - SHIFT_NSEC);
  264. if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
  265. temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL);
  266. if (time_offset < 0) {
  267. temp64 = -temp64;
  268. do_div(temp64, mtemp);
  269. freq_adj -= temp64;
  270. } else {
  271. do_div(temp64, mtemp);
  272. freq_adj += temp64;
  273. }
  274. }
  275. freq_adj += time_freq;
  276. freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
  277. time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
  278. time_offset = div_long_long_rem_signed(time_offset,
  279. NTP_INTERVAL_FREQ,
  280. &rem);
  281. time_offset <<= 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 = ((long)shift_right(time_offset, SHIFT_UPDATE)) *
  295. NTP_INTERVAL_FREQ / 1000;
  296. txc->freq = (time_freq / NSEC_PER_USEC) <<
  297. (SHIFT_USEC - SHIFT_NSEC);
  298. txc->maxerror = time_maxerror;
  299. txc->esterror = time_esterror;
  300. txc->status = time_status;
  301. txc->constant = time_constant;
  302. txc->precision = 1;
  303. txc->tolerance = MAXFREQ;
  304. txc->tick = tick_usec;
  305. /* PPS is not implemented, so these are zero */
  306. txc->ppsfreq = 0;
  307. txc->jitter = 0;
  308. txc->shift = 0;
  309. txc->stabil = 0;
  310. txc->jitcnt = 0;
  311. txc->calcnt = 0;
  312. txc->errcnt = 0;
  313. txc->stbcnt = 0;
  314. write_sequnlock_irq(&xtime_lock);
  315. do_gettimeofday(&txc->time);
  316. notify_arch_cmos_timer();
  317. return(result);
  318. }