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