1 /* $OpenBSD: kern_tc.c,v 1.45 2019/05/10 18:53:13 cheloha Exp $ */ 2 3 /* 4 * Copyright (c) 2000 Poul-Henning Kamp <phk@FreeBSD.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * If we meet some day, and you think this stuff is worth it, you 21 * can buy me a beer in return. Poul-Henning Kamp 22 */ 23 24 #include <sys/param.h> 25 #include <sys/atomic.h> 26 #include <sys/kernel.h> 27 #include <sys/mutex.h> 28 #include <sys/rwlock.h> 29 #include <sys/stdint.h> 30 #include <sys/timeout.h> 31 #include <sys/sysctl.h> 32 #include <sys/syslog.h> 33 #include <sys/systm.h> 34 #include <sys/timetc.h> 35 #include <sys/malloc.h> 36 #include <dev/rndvar.h> 37 38 /* 39 * A large step happens on boot. This constant detects such steps. 40 * It is relatively small so that ntp_update_second gets called enough 41 * in the typical 'missed a couple of seconds' case, but doesn't loop 42 * forever when the time step is large. 43 */ 44 #define LARGE_STEP 200 45 46 u_int dummy_get_timecount(struct timecounter *); 47 48 int sysctl_tc_hardware(void *, size_t *, void *, size_t); 49 int sysctl_tc_choice(void *, size_t *, void *, size_t); 50 51 /* 52 * Implement a dummy timecounter which we can use until we get a real one 53 * in the air. This allows the console and other early stuff to use 54 * time services. 55 */ 56 57 u_int 58 dummy_get_timecount(struct timecounter *tc) 59 { 60 static u_int now; 61 62 return (++now); 63 } 64 65 static struct timecounter dummy_timecounter = { 66 dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000 67 }; 68 69 /* 70 * Locks used to protect struct members, global variables in this file: 71 * I immutable after initialization 72 * t tc_lock 73 * w windup_mtx 74 */ 75 76 struct timehands { 77 /* These fields must be initialized by the driver. */ 78 struct timecounter *th_counter; /* [w] */ 79 int64_t th_adjtimedelta; /* [tw] */ 80 int64_t th_adjustment; /* [w] */ 81 u_int64_t th_scale; /* [w] */ 82 u_int th_offset_count; /* [w] */ 83 struct bintime th_boottime; /* [tw] */ 84 struct bintime th_offset; /* [w] */ 85 struct timeval th_microtime; /* [w] */ 86 struct timespec th_nanotime; /* [w] */ 87 /* Fields not to be copied in tc_windup start with th_generation. */ 88 volatile u_int th_generation; /* [w] */ 89 struct timehands *th_next; /* [I] */ 90 }; 91 92 static struct timehands th0; 93 static struct timehands th1 = { 94 .th_next = &th0 95 }; 96 static struct timehands th0 = { 97 .th_counter = &dummy_timecounter, 98 .th_scale = UINT64_MAX / 1000000, 99 .th_offset = { .sec = 1, .frac = 0 }, 100 .th_generation = 1, 101 .th_next = &th1 102 }; 103 104 struct rwlock tc_lock = RWLOCK_INITIALIZER("tc_lock"); 105 106 /* 107 * tc_windup() must be called before leaving this mutex. 108 */ 109 struct mutex windup_mtx = MUTEX_INITIALIZER(IPL_CLOCK); 110 111 static struct timehands *volatile timehands = &th0; /* [w] */ 112 struct timecounter *timecounter = &dummy_timecounter; /* [t] */ 113 static struct timecounter *timecounters = &dummy_timecounter; 114 115 volatile time_t time_second = 1; 116 volatile time_t time_uptime = 0; 117 118 struct bintime naptime; 119 static int timestepwarnings; 120 121 void ntp_update_second(struct timehands *); 122 void tc_windup(struct bintime *, struct bintime *, int64_t *); 123 124 /* 125 * Return the difference between the timehands' counter value now and what 126 * was when we copied it to the timehands' offset_count. 127 */ 128 static __inline u_int 129 tc_delta(struct timehands *th) 130 { 131 struct timecounter *tc; 132 133 tc = th->th_counter; 134 return ((tc->tc_get_timecount(tc) - th->th_offset_count) & 135 tc->tc_counter_mask); 136 } 137 138 /* 139 * Functions for reading the time. We have to loop until we are sure that 140 * the timehands that we operated on was not updated under our feet. See 141 * the comment in <sys/time.h> for a description of these functions. 142 */ 143 144 void 145 binboottime(struct bintime *bt) 146 { 147 struct timehands *th; 148 u_int gen; 149 150 do { 151 th = timehands; 152 gen = th->th_generation; 153 membar_consumer(); 154 *bt = th->th_boottime; 155 membar_consumer(); 156 } while (gen == 0 || gen != th->th_generation); 157 } 158 159 void 160 microboottime(struct timeval *tvp) 161 { 162 struct bintime bt; 163 164 binboottime(&bt); 165 bintime2timeval(&bt, tvp); 166 } 167 168 void 169 binuptime(struct bintime *bt) 170 { 171 struct timehands *th; 172 u_int gen; 173 174 do { 175 th = timehands; 176 gen = th->th_generation; 177 membar_consumer(); 178 *bt = th->th_offset; 179 bintime_addx(bt, th->th_scale * tc_delta(th)); 180 membar_consumer(); 181 } while (gen == 0 || gen != th->th_generation); 182 } 183 184 void 185 nanouptime(struct timespec *tsp) 186 { 187 struct bintime bt; 188 189 binuptime(&bt); 190 bintime2timespec(&bt, tsp); 191 } 192 193 void 194 microuptime(struct timeval *tvp) 195 { 196 struct bintime bt; 197 198 binuptime(&bt); 199 bintime2timeval(&bt, tvp); 200 } 201 202 void 203 bintime(struct bintime *bt) 204 { 205 struct timehands *th; 206 u_int gen; 207 208 do { 209 th = timehands; 210 gen = th->th_generation; 211 membar_consumer(); 212 *bt = th->th_offset; 213 bintime_addx(bt, th->th_scale * tc_delta(th)); 214 bintime_add(bt, &th->th_boottime); 215 membar_consumer(); 216 } while (gen == 0 || gen != th->th_generation); 217 } 218 219 void 220 nanotime(struct timespec *tsp) 221 { 222 struct bintime bt; 223 224 bintime(&bt); 225 bintime2timespec(&bt, tsp); 226 } 227 228 void 229 microtime(struct timeval *tvp) 230 { 231 struct bintime bt; 232 233 bintime(&bt); 234 bintime2timeval(&bt, tvp); 235 } 236 237 void 238 getnanouptime(struct timespec *tsp) 239 { 240 struct timehands *th; 241 u_int gen; 242 243 do { 244 th = timehands; 245 gen = th->th_generation; 246 membar_consumer(); 247 bintime2timespec(&th->th_offset, tsp); 248 membar_consumer(); 249 } while (gen == 0 || gen != th->th_generation); 250 } 251 252 void 253 getmicrouptime(struct timeval *tvp) 254 { 255 struct timehands *th; 256 u_int gen; 257 258 do { 259 th = timehands; 260 gen = th->th_generation; 261 membar_consumer(); 262 bintime2timeval(&th->th_offset, tvp); 263 membar_consumer(); 264 } while (gen == 0 || gen != th->th_generation); 265 } 266 267 void 268 getnanotime(struct timespec *tsp) 269 { 270 struct timehands *th; 271 u_int gen; 272 273 do { 274 th = timehands; 275 gen = th->th_generation; 276 membar_consumer(); 277 *tsp = th->th_nanotime; 278 membar_consumer(); 279 } while (gen == 0 || gen != th->th_generation); 280 } 281 282 void 283 getmicrotime(struct timeval *tvp) 284 { 285 struct timehands *th; 286 u_int gen; 287 288 do { 289 th = timehands; 290 gen = th->th_generation; 291 membar_consumer(); 292 *tvp = th->th_microtime; 293 membar_consumer(); 294 } while (gen == 0 || gen != th->th_generation); 295 } 296 297 /* 298 * Initialize a new timecounter and possibly use it. 299 */ 300 void 301 tc_init(struct timecounter *tc) 302 { 303 u_int u; 304 305 u = tc->tc_frequency / tc->tc_counter_mask; 306 /* XXX: We need some margin here, 10% is a guess */ 307 u *= 11; 308 u /= 10; 309 if (tc->tc_quality >= 0) { 310 if (u > hz) { 311 tc->tc_quality = -2000; 312 printf("Timecounter \"%s\" frequency %lu Hz", 313 tc->tc_name, (unsigned long)tc->tc_frequency); 314 printf(" -- Insufficient hz, needs at least %u\n", u); 315 } 316 } 317 318 tc->tc_next = timecounters; 319 timecounters = tc; 320 /* 321 * Never automatically use a timecounter with negative quality. 322 * Even though we run on the dummy counter, switching here may be 323 * worse since this timecounter may not be monotonic. 324 */ 325 if (tc->tc_quality < 0) 326 return; 327 if (tc->tc_quality < timecounter->tc_quality) 328 return; 329 if (tc->tc_quality == timecounter->tc_quality && 330 tc->tc_frequency < timecounter->tc_frequency) 331 return; 332 (void)tc->tc_get_timecount(tc); 333 enqueue_randomness(tc->tc_get_timecount(tc)); 334 335 timecounter = tc; 336 } 337 338 /* Report the frequency of the current timecounter. */ 339 u_int64_t 340 tc_getfrequency(void) 341 { 342 343 return (timehands->th_counter->tc_frequency); 344 } 345 346 /* 347 * Step our concept of UTC, aka the realtime clock. 348 * This is done by modifying our estimate of when we booted. 349 * 350 * Any ongoing adjustment is meaningless after a clock jump, 351 * so we zero adjtimedelta here as well. 352 */ 353 void 354 tc_setrealtimeclock(const struct timespec *ts) 355 { 356 struct timespec ts2; 357 struct bintime bt, bt2; 358 int64_t zero = 0; 359 360 rw_enter_write(&tc_lock); 361 mtx_enter(&windup_mtx); 362 binuptime(&bt2); 363 timespec2bintime(ts, &bt); 364 bintime_sub(&bt, &bt2); 365 bintime_add(&bt2, &timehands->th_boottime); 366 367 /* XXX fiddle all the little crinkly bits around the fiords... */ 368 tc_windup(&bt, NULL, &zero); 369 mtx_leave(&windup_mtx); 370 rw_exit_write(&tc_lock); 371 372 enqueue_randomness(ts->tv_sec); 373 374 if (timestepwarnings) { 375 bintime2timespec(&bt2, &ts2); 376 log(LOG_INFO, "Time stepped from %lld.%09ld to %lld.%09ld\n", 377 (long long)ts2.tv_sec, ts2.tv_nsec, 378 (long long)ts->tv_sec, ts->tv_nsec); 379 } 380 } 381 382 /* 383 * Step the monotonic and realtime clocks, triggering any timeouts that 384 * should have occurred across the interval. 385 */ 386 void 387 tc_setclock(const struct timespec *ts) 388 { 389 struct bintime bt, bt2; 390 struct timespec earlier; 391 static int first = 1; 392 int rewind = 0; 393 #ifndef SMALL_KERNEL 394 long long adj_ticks; 395 #endif 396 397 /* 398 * When we're called for the first time, during boot when 399 * the root partition is mounted, we need to set boottime. 400 */ 401 if (first) { 402 tc_setrealtimeclock(ts); 403 first = 0; 404 return; 405 } 406 407 enqueue_randomness(ts->tv_sec); 408 409 mtx_enter(&windup_mtx); 410 timespec2bintime(ts, &bt); 411 bintime_sub(&bt, &timehands->th_boottime); 412 413 /* 414 * Don't rewind the offset. 415 */ 416 if (bt.sec < timehands->th_offset.sec || 417 (bt.sec == timehands->th_offset.sec && 418 bt.frac < timehands->th_offset.frac)) 419 rewind = 1; 420 421 bt2 = timehands->th_offset; 422 423 /* XXX fiddle all the little crinkly bits around the fiords... */ 424 tc_windup(NULL, rewind ? NULL : &bt, NULL); 425 mtx_leave(&windup_mtx); 426 427 if (rewind) { 428 bintime2timespec(&bt, &earlier); 429 printf("%s: cannot rewind uptime to %lld.%09ld\n", 430 __func__, (long long)earlier.tv_sec, earlier.tv_nsec); 431 return; 432 } 433 434 #ifndef SMALL_KERNEL 435 /* convert the bintime to ticks */ 436 bintime_sub(&bt, &bt2); 437 bintime_add(&naptime, &bt); 438 adj_ticks = (uint64_t)hz * bt.sec + 439 (((uint64_t)1000000 * (uint32_t)(bt.frac >> 32)) >> 32) / tick; 440 if (adj_ticks > 0) { 441 if (adj_ticks > INT_MAX) 442 adj_ticks = INT_MAX; 443 timeout_adjust_ticks(adj_ticks); 444 } 445 #endif 446 } 447 448 /* 449 * Initialize the next struct timehands in the ring and make 450 * it the active timehands. Along the way we might switch to a different 451 * timecounter and/or do seconds processing in NTP. Slightly magic. 452 */ 453 void 454 tc_windup(struct bintime *new_boottime, struct bintime *new_offset, 455 int64_t *new_adjtimedelta) 456 { 457 struct bintime bt; 458 struct timecounter *active_tc; 459 struct timehands *th, *tho; 460 u_int64_t scale; 461 u_int delta, ncount, ogen; 462 int i; 463 464 if (new_boottime != NULL || new_adjtimedelta != NULL) 465 rw_assert_wrlock(&tc_lock); 466 MUTEX_ASSERT_LOCKED(&windup_mtx); 467 468 active_tc = timecounter; 469 470 /* 471 * Make the next timehands a copy of the current one, but do not 472 * overwrite the generation or next pointer. While we update 473 * the contents, the generation must be zero. 474 */ 475 tho = timehands; 476 th = tho->th_next; 477 ogen = th->th_generation; 478 th->th_generation = 0; 479 membar_producer(); 480 memcpy(th, tho, offsetof(struct timehands, th_generation)); 481 482 /* 483 * If changing the boot offset, do so before updating the 484 * offset fields. 485 */ 486 if (new_offset != NULL) 487 th->th_offset = *new_offset; 488 489 /* 490 * Capture a timecounter delta on the current timecounter and if 491 * changing timecounters, a counter value from the new timecounter. 492 * Update the offset fields accordingly. 493 */ 494 delta = tc_delta(th); 495 if (th->th_counter != active_tc) 496 ncount = active_tc->tc_get_timecount(active_tc); 497 else 498 ncount = 0; 499 th->th_offset_count += delta; 500 th->th_offset_count &= th->th_counter->tc_counter_mask; 501 bintime_addx(&th->th_offset, th->th_scale * delta); 502 503 #ifdef notyet 504 /* 505 * Hardware latching timecounters may not generate interrupts on 506 * PPS events, so instead we poll them. There is a finite risk that 507 * the hardware might capture a count which is later than the one we 508 * got above, and therefore possibly in the next NTP second which might 509 * have a different rate than the current NTP second. It doesn't 510 * matter in practice. 511 */ 512 if (tho->th_counter->tc_poll_pps) 513 tho->th_counter->tc_poll_pps(tho->th_counter); 514 #endif 515 516 /* 517 * If changing the boot time or clock adjustment, do so before 518 * NTP processing. 519 */ 520 if (new_boottime != NULL) 521 th->th_boottime = *new_boottime; 522 if (new_adjtimedelta != NULL) 523 th->th_adjtimedelta = *new_adjtimedelta; 524 525 /* 526 * Deal with NTP second processing. The for loop normally 527 * iterates at most once, but in extreme situations it might 528 * keep NTP sane if timeouts are not run for several seconds. 529 * At boot, the time step can be large when the TOD hardware 530 * has been read, so on really large steps, we call 531 * ntp_update_second only twice. We need to call it twice in 532 * case we missed a leap second. 533 */ 534 bt = th->th_offset; 535 bintime_add(&bt, &th->th_boottime); 536 i = bt.sec - tho->th_microtime.tv_sec; 537 if (i > LARGE_STEP) 538 i = 2; 539 for (; i > 0; i--) 540 ntp_update_second(th); 541 542 /* Update the UTC timestamps used by the get*() functions. */ 543 /* XXX shouldn't do this here. Should force non-`get' versions. */ 544 bintime2timeval(&bt, &th->th_microtime); 545 bintime2timespec(&bt, &th->th_nanotime); 546 547 /* Now is a good time to change timecounters. */ 548 if (th->th_counter != active_tc) { 549 th->th_counter = active_tc; 550 th->th_offset_count = ncount; 551 } 552 553 /*- 554 * Recalculate the scaling factor. We want the number of 1/2^64 555 * fractions of a second per period of the hardware counter, taking 556 * into account the th_adjustment factor which the NTP PLL/adjtime(2) 557 * processing provides us with. 558 * 559 * The th_adjustment is nanoseconds per second with 32 bit binary 560 * fraction and we want 64 bit binary fraction of second: 561 * 562 * x = a * 2^32 / 10^9 = a * 4.294967296 563 * 564 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int 565 * we can only multiply by about 850 without overflowing, but that 566 * leaves suitably precise fractions for multiply before divide. 567 * 568 * Divide before multiply with a fraction of 2199/512 results in a 569 * systematic undercompensation of 10PPM of th_adjustment. On a 570 * 5000PPM adjustment this is a 0.05PPM error. This is acceptable. 571 * 572 * We happily sacrifice the lowest of the 64 bits of our result 573 * to the goddess of code clarity. 574 * 575 */ 576 scale = (u_int64_t)1 << 63; 577 scale += (th->th_adjustment / 1024) * 2199; 578 scale /= th->th_counter->tc_frequency; 579 th->th_scale = scale * 2; 580 581 /* 582 * Now that the struct timehands is again consistent, set the new 583 * generation number, making sure to not make it zero. 584 */ 585 if (++ogen == 0) 586 ogen = 1; 587 membar_producer(); 588 th->th_generation = ogen; 589 590 /* Go live with the new struct timehands. */ 591 time_second = th->th_microtime.tv_sec; 592 time_uptime = th->th_offset.sec; 593 membar_producer(); 594 timehands = th; 595 } 596 597 /* Report or change the active timecounter hardware. */ 598 int 599 sysctl_tc_hardware(void *oldp, size_t *oldlenp, void *newp, size_t newlen) 600 { 601 char newname[32]; 602 struct timecounter *newtc, *tc; 603 int error; 604 605 tc = timecounter; 606 strlcpy(newname, tc->tc_name, sizeof(newname)); 607 608 error = sysctl_string(oldp, oldlenp, newp, newlen, newname, sizeof(newname)); 609 if (error != 0 || strcmp(newname, tc->tc_name) == 0) 610 return (error); 611 for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) { 612 if (strcmp(newname, newtc->tc_name) != 0) 613 continue; 614 615 /* Warm up new timecounter. */ 616 (void)newtc->tc_get_timecount(newtc); 617 (void)newtc->tc_get_timecount(newtc); 618 619 rw_enter_write(&tc_lock); 620 timecounter = newtc; 621 rw_exit_write(&tc_lock); 622 623 return (0); 624 } 625 return (EINVAL); 626 } 627 628 /* Report or change the active timecounter hardware. */ 629 int 630 sysctl_tc_choice(void *oldp, size_t *oldlenp, void *newp, size_t newlen) 631 { 632 char buf[32], *spc, *choices; 633 struct timecounter *tc; 634 int error, maxlen; 635 636 spc = ""; 637 maxlen = 0; 638 for (tc = timecounters; tc != NULL; tc = tc->tc_next) 639 maxlen += sizeof(buf); 640 choices = malloc(maxlen, M_TEMP, M_WAITOK); 641 *choices = '\0'; 642 for (tc = timecounters; tc != NULL; tc = tc->tc_next) { 643 snprintf(buf, sizeof(buf), "%s%s(%d)", 644 spc, tc->tc_name, tc->tc_quality); 645 spc = " "; 646 strlcat(choices, buf, maxlen); 647 } 648 error = sysctl_rdstring(oldp, oldlenp, newp, choices); 649 free(choices, M_TEMP, maxlen); 650 return (error); 651 } 652 653 /* 654 * Timecounters need to be updated every so often to prevent the hardware 655 * counter from overflowing. Updating also recalculates the cached values 656 * used by the get*() family of functions, so their precision depends on 657 * the update frequency. 658 */ 659 static int tc_tick; 660 661 void 662 tc_ticktock(void) 663 { 664 static int count; 665 666 if (++count < tc_tick) 667 return; 668 if (!mtx_enter_try(&windup_mtx)) 669 return; 670 count = 0; 671 tc_windup(NULL, NULL, NULL); 672 mtx_leave(&windup_mtx); 673 } 674 675 void 676 inittimecounter(void) 677 { 678 #ifdef DEBUG 679 u_int p; 680 #endif 681 682 /* 683 * Set the initial timeout to 684 * max(1, <approx. number of hardclock ticks in a millisecond>). 685 * People should probably not use the sysctl to set the timeout 686 * to smaller than its initial value, since that value is the 687 * smallest reasonable one. If they want better timestamps they 688 * should use the non-"get"* functions. 689 */ 690 if (hz > 1000) 691 tc_tick = (hz + 500) / 1000; 692 else 693 tc_tick = 1; 694 #ifdef DEBUG 695 p = (tc_tick * 1000000) / hz; 696 printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000); 697 #endif 698 699 /* warm up new timecounter (again) and get rolling. */ 700 (void)timecounter->tc_get_timecount(timecounter); 701 (void)timecounter->tc_get_timecount(timecounter); 702 } 703 704 /* 705 * Return timecounter-related information. 706 */ 707 int 708 sysctl_tc(int *name, u_int namelen, void *oldp, size_t *oldlenp, 709 void *newp, size_t newlen) 710 { 711 if (namelen != 1) 712 return (ENOTDIR); 713 714 switch (name[0]) { 715 case KERN_TIMECOUNTER_TICK: 716 return (sysctl_rdint(oldp, oldlenp, newp, tc_tick)); 717 case KERN_TIMECOUNTER_TIMESTEPWARNINGS: 718 return (sysctl_int(oldp, oldlenp, newp, newlen, 719 ×tepwarnings)); 720 case KERN_TIMECOUNTER_HARDWARE: 721 return (sysctl_tc_hardware(oldp, oldlenp, newp, newlen)); 722 case KERN_TIMECOUNTER_CHOICE: 723 return (sysctl_tc_choice(oldp, oldlenp, newp, newlen)); 724 default: 725 return (EOPNOTSUPP); 726 } 727 /* NOTREACHED */ 728 } 729 730 /* 731 * Skew the timehands according to any adjfreq(2)/adjtime(2) adjustments. 732 */ 733 void 734 ntp_update_second(struct timehands *th) 735 { 736 int64_t adj; 737 738 MUTEX_ASSERT_LOCKED(&windup_mtx); 739 740 if (th->th_adjtimedelta > 0) 741 adj = MIN(5000, th->th_adjtimedelta); 742 else 743 adj = MAX(-5000, th->th_adjtimedelta); 744 th->th_adjtimedelta -= adj; 745 th->th_adjustment = (adj * 1000) << 32; 746 th->th_adjustment += th->th_counter->tc_freq_adj; 747 } 748 749 void 750 tc_adjfreq(int64_t *old, int64_t *new) 751 { 752 if (old != NULL) { 753 rw_assert_anylock(&tc_lock); 754 *old = timecounter->tc_freq_adj; 755 } 756 if (new != NULL) { 757 rw_assert_wrlock(&tc_lock); 758 mtx_enter(&windup_mtx); 759 timecounter->tc_freq_adj = *new; 760 tc_windup(NULL, NULL, NULL); 761 mtx_leave(&windup_mtx); 762 } 763 } 764 765 void 766 tc_adjtime(int64_t *old, int64_t *new) 767 { 768 struct timehands *th; 769 u_int gen; 770 771 if (old != NULL) { 772 do { 773 th = timehands; 774 gen = th->th_generation; 775 membar_consumer(); 776 *old = th->th_adjtimedelta; 777 membar_consumer(); 778 } while (gen == 0 || gen != th->th_generation); 779 } 780 if (new != NULL) { 781 rw_assert_wrlock(&tc_lock); 782 mtx_enter(&windup_mtx); 783 tc_windup(NULL, NULL, new); 784 mtx_leave(&windup_mtx); 785 } 786 } 787