ntp.c 10 KB

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