]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/clock.c
xnu-2422.115.4.tar.gz
[apple/xnu.git] / osfmk / kern / clock.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
1c79356b
A
32 */
33
91447636 34#include <mach/mach_types.h>
91447636 35
1c79356b 36#include <kern/lock.h>
1c79356b 37#include <kern/spl.h>
55e303ae 38#include <kern/sched_prim.h>
1c79356b 39#include <kern/thread.h>
1c79356b 40#include <kern/clock.h>
0c530ab8
A
41#include <kern/host_notify.h>
42
43#include <IOKit/IOPlatformExpert.h>
c0fea474 44
0c530ab8 45#include <machine/commpage.h>
1c79356b 46
91447636 47#include <mach/mach_traps.h>
1c79356b
A
48#include <mach/mach_time.h>
49
2d21ac55
A
50uint32_t hz_tick_interval = 1;
51
2d21ac55 52
6d2010ae 53decl_simple_lock_data(,clock_lock)
91447636 54
b0d623f7
A
55#define clock_lock() \
56 simple_lock(&clock_lock)
57
58#define clock_unlock() \
59 simple_unlock(&clock_lock)
60
61#define clock_lock_init() \
62 simple_lock_init(&clock_lock, 0)
63
64
1c79356b 65/*
0c530ab8
A
66 * Time of day (calendar) variables.
67 *
68 * Algorithm:
69 *
70 * TOD <- (seconds + epoch, fraction) <- CONV(current absolute time + offset)
71 *
72 * where CONV converts absolute time units into seconds and a fraction.
1c79356b 73 */
0c530ab8 74static struct clock_calend {
2d21ac55
A
75 uint64_t epoch;
76 uint64_t offset;
b0d623f7 77
2d21ac55
A
78 int32_t adjdelta; /* Nanosecond time delta for this adjustment period */
79 uint64_t adjstart; /* Absolute time value for start of this adjustment period */
80 uint32_t adjoffset; /* Absolute time offset for this adjustment period as absolute value */
2d21ac55
A
81} clock_calend;
82
b0d623f7
A
83#if CONFIG_DTRACE
84
2d21ac55
A
85/*
86 * Unlocked calendar flipflop; this is used to track a clock_calend such
87 * that we can safely access a snapshot of a valid clock_calend structure
88 * without needing to take any locks to do it.
89 *
90 * The trick is to use a generation count and set the low bit when it is
91 * being updated/read; by doing this, we guarantee, through use of the
92 * hw_atomic functions, that the generation is incremented when the bit
93 * is cleared atomically (by using a 1 bit add).
94 */
95static struct unlocked_clock_calend {
96 struct clock_calend calend; /* copy of calendar */
97 uint32_t gen; /* generation count */
98} flipflop[ 2];
b0d623f7
A
99
100static void clock_track_calend_nowait(void);
101
2d21ac55 102#endif
1c79356b 103
0c530ab8
A
104/*
105 * Calendar adjustment variables and values.
106 */
107#define calend_adjperiod (NSEC_PER_SEC / 100) /* adjustment period, ns */
108#define calend_adjskew (40 * NSEC_PER_USEC) /* "standard" skew, ns / period */
109#define calend_adjbig (NSEC_PER_SEC) /* use 10x skew above adjbig ns */
110
b0d623f7
A
111static int64_t calend_adjtotal; /* Nanosecond remaining total adjustment */
112static uint64_t calend_adjdeadline; /* Absolute time value for next adjustment period */
113static uint32_t calend_adjinterval; /* Absolute time interval of adjustment period */
114
115static timer_call_data_t calend_adjcall;
116static uint32_t calend_adjactive;
117
0c530ab8 118static uint32_t calend_set_adjustment(
b0d623f7
A
119 long *secs,
120 int *microsecs);
0c530ab8
A
121
122static void calend_adjust_call(void);
123static uint32_t calend_adjust(void);
9bccf70c 124
55e303ae
A
125static thread_call_data_t calend_wakecall;
126
0c530ab8 127extern void IOKitResetTime(void);
5d5c5d0d 128
316670eb
A
129void _clock_delay_until_deadline(uint64_t interval,
130 uint64_t deadline);
131
0c530ab8 132static uint64_t clock_boottime; /* Seconds boottime epoch */
4452a7af 133
0c530ab8
A
134#define TIME_ADD(rsecs, secs, rfrac, frac, unit) \
135MACRO_BEGIN \
136 if (((rfrac) += (frac)) >= (unit)) { \
137 (rfrac) -= (unit); \
138 (rsecs) += 1; \
139 } \
140 (rsecs) += (secs); \
141MACRO_END
142
143#define TIME_SUB(rsecs, secs, rfrac, frac, unit) \
144MACRO_BEGIN \
b0d623f7 145 if ((int)((rfrac) -= (frac)) < 0) { \
0c530ab8
A
146 (rfrac) += (unit); \
147 (rsecs) -= 1; \
148 } \
149 (rsecs) -= (secs); \
150MACRO_END
1c79356b
A
151
152/*
91447636
A
153 * clock_config:
154 *
155 * Called once at boot to configure the clock subsystem.
1c79356b
A
156 */
157void
158clock_config(void)
159{
b0d623f7 160 clock_lock_init();
8f6c56a5 161
b0d623f7 162 timer_call_setup(&calend_adjcall, (timer_call_func_t)calend_adjust_call, NULL);
0c530ab8 163 thread_call_setup(&calend_wakecall, (thread_call_func_t)IOKitResetTime, NULL);
6601e61a 164
0c530ab8 165 clock_oldconfig();
1c79356b
A
166}
167
168/*
91447636
A
169 * clock_init:
170 *
171 * Called on a processor each time started.
1c79356b
A
172 */
173void
174clock_init(void)
175{
0c530ab8 176 clock_oldinit();
1c79356b
A
177}
178
55e303ae 179/*
0c530ab8
A
180 * clock_timebase_init:
181 *
182 * Called by machine dependent code
183 * to initialize areas dependent on the
184 * timebase value. May be called multiple
185 * times during start up.
55e303ae
A
186 */
187void
188clock_timebase_init(void)
189{
0c530ab8 190 uint64_t abstime;
5d5c5d0d 191
0c530ab8 192 nanoseconds_to_absolutetime(calend_adjperiod, &abstime);
b0d623f7 193 calend_adjinterval = (uint32_t)abstime;
2d21ac55
A
194
195 nanoseconds_to_absolutetime(NSEC_PER_SEC / 100, &abstime);
b0d623f7 196 hz_tick_interval = (uint32_t)abstime;
89b3af67 197
0c530ab8 198 sched_timebase_init();
8ad349bb 199}
c0fea474 200
8ad349bb 201/*
0c530ab8
A
202 * mach_timebase_info_trap:
203 *
204 * User trap returns timebase constant.
8ad349bb 205 */
6601e61a 206kern_return_t
0c530ab8
A
207mach_timebase_info_trap(
208 struct mach_timebase_info_trap_args *args)
6601e61a 209{
0c530ab8
A
210 mach_vm_address_t out_info_addr = args->info;
211 mach_timebase_info_data_t info;
6601e61a 212
0c530ab8 213 clock_timebase_info(&info);
89b3af67 214
0c530ab8 215 copyout((void *)&info, out_info_addr, sizeof (info));
4452a7af 216
6601e61a 217 return (KERN_SUCCESS);
8f6c56a5 218}
5d5c5d0d 219
8f6c56a5 220/*
0c530ab8 221 * Calendar routines.
8f6c56a5 222 */
4452a7af 223
6601e61a 224/*
0c530ab8
A
225 * clock_get_calendar_microtime:
226 *
227 * Returns the current calendar value,
228 * microseconds as the fraction.
6601e61a 229 */
0c530ab8
A
230void
231clock_get_calendar_microtime(
b0d623f7
A
232 clock_sec_t *secs,
233 clock_usec_t *microsecs)
39236c6e
A
234{
235 clock_get_calendar_absolute_and_microtime(secs, microsecs, NULL);
236}
237
238/*
239 * clock_get_calendar_absolute_and_microtime:
240 *
241 * Returns the current calendar value,
242 * microseconds as the fraction. Also
243 * returns mach_absolute_time if abstime
244 * is not NULL.
245 */
246void
247clock_get_calendar_absolute_and_microtime(
248 clock_sec_t *secs,
249 clock_usec_t *microsecs,
250 uint64_t *abstime)
6601e61a 251{
0c530ab8
A
252 uint64_t now;
253 spl_t s;
4452a7af 254
0c530ab8 255 s = splclock();
b0d623f7 256 clock_lock();
4452a7af 257
0c530ab8 258 now = mach_absolute_time();
39236c6e
A
259 if (abstime)
260 *abstime = now;
4452a7af 261
2d21ac55 262 if (clock_calend.adjdelta < 0) {
0c530ab8 263 uint32_t t32;
4452a7af 264
6d2010ae
A
265 /*
266 * Since offset is decremented during a negative adjustment,
267 * ensure that time increases monotonically without going
268 * temporarily backwards.
269 * If the delta has not yet passed, now is set to the start
270 * of the current adjustment period; otherwise, we're between
271 * the expiry of the delta and the next call to calend_adjust(),
272 * and we offset accordingly.
273 */
2d21ac55 274 if (now > clock_calend.adjstart) {
b0d623f7 275 t32 = (uint32_t)(now - clock_calend.adjstart);
0c530ab8 276
2d21ac55
A
277 if (t32 > clock_calend.adjoffset)
278 now -= clock_calend.adjoffset;
0c530ab8 279 else
2d21ac55 280 now = clock_calend.adjstart;
0c530ab8
A
281 }
282 }
283
284 now += clock_calend.offset;
285
286 absolutetime_to_microtime(now, secs, microsecs);
287
b0d623f7 288 *secs += (clock_sec_t)clock_calend.epoch;
0c530ab8 289
b0d623f7 290 clock_unlock();
0c530ab8 291 splx(s);
21362eb3 292}
89b3af67 293
21362eb3 294/*
0c530ab8
A
295 * clock_get_calendar_nanotime:
296 *
297 * Returns the current calendar value,
298 * nanoseconds as the fraction.
299 *
300 * Since we do not have an interface to
301 * set the calendar with resolution greater
302 * than a microsecond, we honor that here.
21362eb3 303 */
0c530ab8
A
304void
305clock_get_calendar_nanotime(
b0d623f7
A
306 clock_sec_t *secs,
307 clock_nsec_t *nanosecs)
21362eb3 308{
0c530ab8
A
309 uint64_t now;
310 spl_t s;
311
312 s = splclock();
b0d623f7 313 clock_lock();
0c530ab8
A
314
315 now = mach_absolute_time();
316
2d21ac55 317 if (clock_calend.adjdelta < 0) {
0c530ab8
A
318 uint32_t t32;
319
2d21ac55 320 if (now > clock_calend.adjstart) {
b0d623f7 321 t32 = (uint32_t)(now - clock_calend.adjstart);
0c530ab8 322
2d21ac55
A
323 if (t32 > clock_calend.adjoffset)
324 now -= clock_calend.adjoffset;
0c530ab8 325 else
2d21ac55 326 now = clock_calend.adjstart;
0c530ab8
A
327 }
328 }
329
330 now += clock_calend.offset;
331
332 absolutetime_to_microtime(now, secs, nanosecs);
6d2010ae 333
0c530ab8
A
334 *nanosecs *= NSEC_PER_USEC;
335
b0d623f7 336 *secs += (clock_sec_t)clock_calend.epoch;
0c530ab8 337
b0d623f7 338 clock_unlock();
0c530ab8 339 splx(s);
6601e61a 340}
4452a7af 341
6601e61a 342/*
0c530ab8
A
343 * clock_gettimeofday:
344 *
345 * Kernel interface for commpage implementation of
346 * gettimeofday() syscall.
347 *
348 * Returns the current calendar value, and updates the
349 * commpage info as appropriate. Because most calls to
350 * gettimeofday() are handled in user mode by the commpage,
351 * this routine should be used infrequently.
6601e61a 352 */
0c530ab8
A
353void
354clock_gettimeofday(
b0d623f7
A
355 clock_sec_t *secs,
356 clock_usec_t *microsecs)
6601e61a 357{
0c530ab8
A
358 uint64_t now;
359 spl_t s;
4452a7af 360
0c530ab8 361 s = splclock();
b0d623f7 362 clock_lock();
0c530ab8
A
363
364 now = mach_absolute_time();
365
2d21ac55 366 if (clock_calend.adjdelta >= 0) {
0c530ab8 367 clock_gettimeofday_set_commpage(now, clock_calend.epoch, clock_calend.offset, secs, microsecs);
1c79356b 368 }
0c530ab8
A
369 else {
370 uint32_t t32;
4452a7af 371
2d21ac55 372 if (now > clock_calend.adjstart) {
b0d623f7 373 t32 = (uint32_t)(now - clock_calend.adjstart);
0c530ab8 374
2d21ac55
A
375 if (t32 > clock_calend.adjoffset)
376 now -= clock_calend.adjoffset;
0c530ab8 377 else
2d21ac55 378 now = clock_calend.adjstart;
0c530ab8
A
379 }
380
381 now += clock_calend.offset;
4452a7af 382
0c530ab8
A
383 absolutetime_to_microtime(now, secs, microsecs);
384
b0d623f7 385 *secs += (clock_sec_t)clock_calend.epoch;
1c79356b 386 }
1c79356b 387
b0d623f7 388 clock_unlock();
0c530ab8 389 splx(s);
1c79356b
A
390}
391
392/*
0c530ab8
A
393 * clock_set_calendar_microtime:
394 *
395 * Sets the current calendar value by
396 * recalculating the epoch and offset
397 * from the system clock.
398 *
399 * Also adjusts the boottime to keep the
400 * value consistent, writes the new
401 * calendar value to the platform clock,
402 * and sends calendar change notifications.
1c79356b 403 */
0c530ab8
A
404void
405clock_set_calendar_microtime(
b0d623f7
A
406 clock_sec_t secs,
407 clock_usec_t microsecs)
1c79356b 408{
b0d623f7
A
409 clock_sec_t sys;
410 clock_usec_t microsys;
411 clock_sec_t newsecs;
412 spl_t s;
8ad349bb 413
b0d623f7 414 newsecs = (microsecs < 500*USEC_PER_SEC)? secs: secs + 1;
0c530ab8
A
415
416 s = splclock();
b0d623f7 417 clock_lock();
8ad349bb 418
2d21ac55 419 commpage_disable_timestamp();
8f6c56a5 420
89b3af67 421 /*
0c530ab8
A
422 * Calculate the new calendar epoch based on
423 * the new value and the system clock.
89b3af67 424 */
0c530ab8
A
425 clock_get_system_microtime(&sys, &microsys);
426 TIME_SUB(secs, sys, microsecs, microsys, USEC_PER_SEC);
8f6c56a5 427
4452a7af 428 /*
0c530ab8 429 * Adjust the boottime based on the delta.
4452a7af 430 */
0c530ab8 431 clock_boottime += secs - clock_calend.epoch;
21362eb3 432
4452a7af 433 /*
0c530ab8 434 * Set the new calendar epoch.
4452a7af 435 */
0c530ab8 436 clock_calend.epoch = secs;
6d2010ae 437
0c530ab8 438 nanoseconds_to_absolutetime((uint64_t)microsecs * NSEC_PER_USEC, &clock_calend.offset);
21362eb3 439
0c530ab8
A
440 /*
441 * Cancel any adjustment in progress.
442 */
b0d623f7 443 calend_adjtotal = clock_calend.adjdelta = 0;
21362eb3 444
b0d623f7 445 clock_unlock();
6601e61a 446
0c530ab8
A
447 /*
448 * Set the new value for the platform clock.
449 */
450 PESetGMTTimeOfDay(newsecs);
6601e61a 451
0c530ab8 452 splx(s);
6601e61a 453
0c530ab8
A
454 /*
455 * Send host notifications.
456 */
457 host_notify_calendar_change();
2d21ac55
A
458
459#if CONFIG_DTRACE
460 clock_track_calend_nowait();
461#endif
1c79356b
A
462}
463
464/*
0c530ab8
A
465 * clock_initialize_calendar:
466 *
467 * Set the calendar and related clocks
468 * from the platform clock at boot or
469 * wake event.
470 *
471 * Also sends host notifications.
1c79356b
A
472 */
473void
0c530ab8 474clock_initialize_calendar(void)
1c79356b 475{
b0d623f7
A
476 clock_sec_t sys, secs = PEGetGMTTimeOfDay();
477 clock_usec_t microsys, microsecs = 0;
478 spl_t s;
1c79356b 479
0c530ab8 480 s = splclock();
b0d623f7 481 clock_lock();
1c79356b 482
2d21ac55 483 commpage_disable_timestamp();
1c79356b 484
b0d623f7 485 if ((long)secs >= (long)clock_boottime) {
0c530ab8
A
486 /*
487 * Initialize the boot time based on the platform clock.
488 */
489 if (clock_boottime == 0)
490 clock_boottime = secs;
1c79356b
A
491
492 /*
0c530ab8
A
493 * Calculate the new calendar epoch based on
494 * the platform clock and the system clock.
495 */
496 clock_get_system_microtime(&sys, &microsys);
497 TIME_SUB(secs, sys, microsecs, microsys, USEC_PER_SEC);
1c79356b
A
498
499 /*
0c530ab8 500 * Set the new calendar epoch.
1c79356b 501 */
0c530ab8 502 clock_calend.epoch = secs;
6d2010ae 503
0c530ab8 504 nanoseconds_to_absolutetime((uint64_t)microsecs * NSEC_PER_USEC, &clock_calend.offset);
1c79356b 505
0c530ab8
A
506 /*
507 * Cancel any adjustment in progress.
1c79356b 508 */
b0d623f7 509 calend_adjtotal = clock_calend.adjdelta = 0;
1c79356b
A
510 }
511
b0d623f7 512 clock_unlock();
0c530ab8
A
513 splx(s);
514
1c79356b 515 /*
0c530ab8 516 * Send host notifications.
1c79356b 517 */
0c530ab8 518 host_notify_calendar_change();
2d21ac55
A
519
520#if CONFIG_DTRACE
521 clock_track_calend_nowait();
522#endif
1c79356b
A
523}
524
525/*
0c530ab8
A
526 * clock_get_boottime_nanotime:
527 *
528 * Return the boottime, used by sysctl.
1c79356b 529 */
0c530ab8
A
530void
531clock_get_boottime_nanotime(
b0d623f7
A
532 clock_sec_t *secs,
533 clock_nsec_t *nanosecs)
1c79356b 534{
b0d623f7
A
535 spl_t s;
536
537 s = splclock();
538 clock_lock();
539
540 *secs = (clock_sec_t)clock_boottime;
0c530ab8 541 *nanosecs = 0;
b0d623f7
A
542
543 clock_unlock();
544 splx(s);
1c79356b
A
545}
546
547/*
0c530ab8
A
548 * clock_adjtime:
549 *
550 * Interface to adjtime() syscall.
551 *
552 * Calculates adjustment variables and
553 * initiates adjustment.
6601e61a 554 */
1c79356b 555void
0c530ab8 556clock_adjtime(
b0d623f7
A
557 long *secs,
558 int *microsecs)
1c79356b 559{
0c530ab8
A
560 uint32_t interval;
561 spl_t s;
1c79356b 562
0c530ab8 563 s = splclock();
b0d623f7 564 clock_lock();
1c79356b 565
0c530ab8
A
566 interval = calend_set_adjustment(secs, microsecs);
567 if (interval != 0) {
b0d623f7 568 calend_adjdeadline = mach_absolute_time() + interval;
39236c6e 569 if (!timer_call_enter(&calend_adjcall, calend_adjdeadline, TIMER_CALL_SYS_CRITICAL))
b0d623f7 570 calend_adjactive++;
1c79356b 571 }
0c530ab8 572 else
b0d623f7
A
573 if (timer_call_cancel(&calend_adjcall))
574 calend_adjactive--;
0c530ab8 575
b0d623f7 576 clock_unlock();
0c530ab8 577 splx(s);
1c79356b
A
578}
579
0c530ab8
A
580static uint32_t
581calend_set_adjustment(
b0d623f7
A
582 long *secs,
583 int *microsecs)
1c79356b 584{
0c530ab8
A
585 uint64_t now, t64;
586 int64_t total, ototal;
587 uint32_t interval = 0;
1c79356b 588
6d2010ae
A
589 /*
590 * Compute the total adjustment time in nanoseconds.
591 */
39236c6e 592 total = ((int64_t)*secs * (int64_t)NSEC_PER_SEC) + (*microsecs * (int64_t)NSEC_PER_USEC);
1c79356b 593
6d2010ae
A
594 /*
595 * Disable commpage gettimeofday().
596 */
2d21ac55 597 commpage_disable_timestamp();
1c79356b 598
6d2010ae
A
599 /*
600 * Get current absolute time.
601 */
0c530ab8 602 now = mach_absolute_time();
1c79356b 603
6d2010ae
A
604 /*
605 * Save the old adjustment total for later return.
606 */
b0d623f7 607 ototal = calend_adjtotal;
1c79356b 608
6d2010ae
A
609 /*
610 * Is a new correction specified?
611 */
0c530ab8 612 if (total != 0) {
6d2010ae
A
613 /*
614 * Set delta to the standard, small, adjustment skew.
615 */
0c530ab8 616 int32_t delta = calend_adjskew;
1c79356b 617
0c530ab8 618 if (total > 0) {
6d2010ae
A
619 /*
620 * Positive adjustment. If greater than the preset 'big'
621 * threshold, slew at a faster rate, capping if necessary.
622 */
39236c6e 623 if (total > (int64_t) calend_adjbig)
0c530ab8
A
624 delta *= 10;
625 if (delta > total)
b0d623f7 626 delta = (int32_t)total;
c0fea474 627
6d2010ae
A
628 /*
629 * Convert the delta back from ns to absolute time and store in adjoffset.
630 */
0c530ab8 631 nanoseconds_to_absolutetime((uint64_t)delta, &t64);
b0d623f7 632 clock_calend.adjoffset = (uint32_t)t64;
0c530ab8
A
633 }
634 else {
6d2010ae
A
635 /*
636 * Negative adjustment; therefore, negate the delta. If
637 * greater than the preset 'big' threshold, slew at a faster
638 * rate, capping if necessary.
639 */
39236c6e 640 if (total < (int64_t) -calend_adjbig)
0c530ab8
A
641 delta *= 10;
642 delta = -delta;
643 if (delta < total)
b0d623f7 644 delta = (int32_t)total;
5d5c5d0d 645
6d2010ae
A
646 /*
647 * Save the current absolute time. Subsequent time operations occuring
648 * during this negative correction can make use of this value to ensure
649 * that time increases monotonically.
650 */
2d21ac55 651 clock_calend.adjstart = now;
89b3af67 652
6d2010ae
A
653 /*
654 * Convert the delta back from ns to absolute time and store in adjoffset.
655 */
0c530ab8 656 nanoseconds_to_absolutetime((uint64_t)-delta, &t64);
b0d623f7 657 clock_calend.adjoffset = (uint32_t)t64;
0c530ab8 658 }
4452a7af 659
6d2010ae
A
660 /*
661 * Store the total adjustment time in ns.
662 */
b0d623f7 663 calend_adjtotal = total;
6d2010ae
A
664
665 /*
666 * Store the delta for this adjustment period in ns.
667 */
2d21ac55 668 clock_calend.adjdelta = delta;
0c530ab8 669
6d2010ae
A
670 /*
671 * Set the interval in absolute time for later return.
672 */
b0d623f7 673 interval = calend_adjinterval;
0c530ab8 674 }
6d2010ae
A
675 else {
676 /*
677 * No change; clear any prior adjustment.
678 */
b0d623f7 679 calend_adjtotal = clock_calend.adjdelta = 0;
6d2010ae 680 }
1c79356b 681
6d2010ae
A
682 /*
683 * If an prior correction was in progress, return the
684 * remaining uncorrected time from it.
685 */
0c530ab8 686 if (ototal != 0) {
39236c6e
A
687 *secs = (long)(ototal / (long)NSEC_PER_SEC);
688 *microsecs = (int)((ototal % (int)NSEC_PER_SEC) / (int)NSEC_PER_USEC);
0c530ab8
A
689 }
690 else
691 *secs = *microsecs = 0;
1c79356b 692
2d21ac55
A
693#if CONFIG_DTRACE
694 clock_track_calend_nowait();
695#endif
696
0c530ab8 697 return (interval);
1c79356b
A
698}
699
0c530ab8
A
700static void
701calend_adjust_call(void)
1c79356b 702{
0c530ab8
A
703 uint32_t interval;
704 spl_t s;
1c79356b 705
0c530ab8 706 s = splclock();
b0d623f7 707 clock_lock();
1c79356b 708
b0d623f7 709 if (--calend_adjactive == 0) {
0c530ab8
A
710 interval = calend_adjust();
711 if (interval != 0) {
b0d623f7 712 clock_deadline_for_periodic_event(interval, mach_absolute_time(), &calend_adjdeadline);
1c79356b 713
39236c6e 714 if (!timer_call_enter(&calend_adjcall, calend_adjdeadline, TIMER_CALL_SYS_CRITICAL))
b0d623f7 715 calend_adjactive++;
0c530ab8 716 }
1c79356b 717 }
0c530ab8 718
b0d623f7 719 clock_unlock();
0c530ab8 720 splx(s);
1c79356b
A
721}
722
0c530ab8
A
723static uint32_t
724calend_adjust(void)
1c79356b 725{
0c530ab8
A
726 uint64_t now, t64;
727 int32_t delta;
728 uint32_t interval = 0;
89b3af67 729
2d21ac55 730 commpage_disable_timestamp();
89b3af67 731
0c530ab8 732 now = mach_absolute_time();
89b3af67 733
2d21ac55 734 delta = clock_calend.adjdelta;
89b3af67 735
0c530ab8 736 if (delta > 0) {
2d21ac55 737 clock_calend.offset += clock_calend.adjoffset;
4452a7af 738
b0d623f7
A
739 calend_adjtotal -= delta;
740 if (delta > calend_adjtotal) {
741 clock_calend.adjdelta = delta = (int32_t)calend_adjtotal;
4452a7af 742
0c530ab8 743 nanoseconds_to_absolutetime((uint64_t)delta, &t64);
b0d623f7 744 clock_calend.adjoffset = (uint32_t)t64;
0c530ab8
A
745 }
746 }
747 else
6d2010ae
A
748 if (delta < 0) {
749 clock_calend.offset -= clock_calend.adjoffset;
4452a7af 750
6d2010ae
A
751 calend_adjtotal -= delta;
752 if (delta < calend_adjtotal) {
753 clock_calend.adjdelta = delta = (int32_t)calend_adjtotal;
4452a7af 754
6d2010ae
A
755 nanoseconds_to_absolutetime((uint64_t)-delta, &t64);
756 clock_calend.adjoffset = (uint32_t)t64;
757 }
758
759 if (clock_calend.adjdelta != 0)
760 clock_calend.adjstart = now;
0c530ab8
A
761 }
762
2d21ac55 763 if (clock_calend.adjdelta != 0)
b0d623f7 764 interval = calend_adjinterval;
0c530ab8 765
2d21ac55
A
766#if CONFIG_DTRACE
767 clock_track_calend_nowait();
768#endif
0c530ab8
A
769
770 return (interval);
771}
772
773/*
774 * clock_wakeup_calendar:
775 *
776 * Interface to power management, used
777 * to initiate the reset of the calendar
778 * on wake from sleep event.
779 */
780void
781clock_wakeup_calendar(void)
782{
783 thread_call_enter(&calend_wakecall);
1c79356b
A
784}
785
0c530ab8
A
786/*
787 * Wait / delay routines.
788 */
91447636
A
789static void
790mach_wait_until_continue(
791 __unused void *parameter,
792 wait_result_t wresult)
793{
794 thread_syscall_return((wresult == THREAD_INTERRUPTED)? KERN_ABORTED: KERN_SUCCESS);
795 /*NOTREACHED*/
796}
797
316670eb
A
798/*
799 * mach_wait_until_trap: Suspend execution of calling thread until the specified time has passed
800 *
801 * Parameters: args->deadline Amount of time to wait
802 *
803 * Returns: 0 Success
804 * !0 Not success
805 *
806 */
1c79356b 807kern_return_t
91447636
A
808mach_wait_until_trap(
809 struct mach_wait_until_trap_args *args)
810{
811 uint64_t deadline = args->deadline;
812 wait_result_t wresult;
813
39236c6e
A
814 wresult = assert_wait_deadline_with_leeway((event_t)mach_wait_until_trap, THREAD_ABORTSAFE,
815 TIMEOUT_URGENCY_USER_NORMAL, deadline, 0);
91447636
A
816 if (wresult == THREAD_WAITING)
817 wresult = thread_block(mach_wait_until_continue);
818
819 return ((wresult == THREAD_INTERRUPTED)? KERN_ABORTED: KERN_SUCCESS);
820}
821
91447636
A
822void
823clock_delay_until(
1c79356b
A
824 uint64_t deadline)
825{
91447636
A
826 uint64_t now = mach_absolute_time();
827
828 if (now >= deadline)
829 return;
1c79356b 830
316670eb
A
831 _clock_delay_until_deadline(deadline - now, deadline);
832}
833
834/*
835 * Preserve the original precise interval that the client
836 * requested for comparison to the spin threshold.
837 */
838void
839_clock_delay_until_deadline(
840 uint64_t interval,
841 uint64_t deadline)
842{
843
844 if (interval == 0)
845 return;
846
847 if ( ml_delay_should_spin(interval) ||
91447636 848 get_preemption_level() != 0 ||
316670eb 849 ml_get_interrupts_enabled() == FALSE ) {
bd504ef0 850 machine_delay_until(interval, deadline);
316670eb
A
851 } else {
852 assert_wait_deadline((event_t)clock_delay_until, THREAD_UNINT, deadline);
91447636
A
853
854 thread_block(THREAD_CONTINUE_NULL);
9bccf70c 855 }
91447636 856}
1c79356b 857
316670eb 858
91447636
A
859void
860delay_for_interval(
861 uint32_t interval,
862 uint32_t scale_factor)
863{
316670eb 864 uint64_t abstime;
91447636 865
316670eb 866 clock_interval_to_absolutetime_interval(interval, scale_factor, &abstime);
91447636 867
316670eb 868 _clock_delay_until_deadline(abstime, mach_absolute_time() + abstime);
91447636
A
869}
870
871void
872delay(
873 int usec)
874{
875 delay_for_interval((usec < 0)? -usec: usec, NSEC_PER_USEC);
1c79356b 876}
9bccf70c 877
0c530ab8
A
878/*
879 * Miscellaneous routines.
880 */
55e303ae 881void
0c530ab8
A
882clock_interval_to_deadline(
883 uint32_t interval,
884 uint32_t scale_factor,
885 uint64_t *result)
9bccf70c 886{
0c530ab8 887 uint64_t abstime;
c0fea474 888
0c530ab8 889 clock_interval_to_absolutetime_interval(interval, scale_factor, &abstime);
6601e61a 890
0c530ab8 891 *result = mach_absolute_time() + abstime;
8f6c56a5 892}
5d5c5d0d 893
0c530ab8
A
894void
895clock_absolutetime_interval_to_deadline(
896 uint64_t abstime,
897 uint64_t *result)
8f6c56a5 898{
0c530ab8 899 *result = mach_absolute_time() + abstime;
21362eb3 900}
89b3af67 901
4452a7af 902void
0c530ab8
A
903clock_get_uptime(
904 uint64_t *result)
21362eb3 905{
0c530ab8 906 *result = mach_absolute_time();
6601e61a 907}
4452a7af 908
0c530ab8
A
909void
910clock_deadline_for_periodic_event(
911 uint64_t interval,
912 uint64_t abstime,
913 uint64_t *deadline)
6601e61a 914{
0c530ab8
A
915 assert(interval != 0);
916
917 *deadline += interval;
918
919 if (*deadline <= abstime) {
920 *deadline = abstime + interval;
921 abstime = mach_absolute_time();
55e303ae 922
0c530ab8
A
923 if (*deadline <= abstime)
924 *deadline = abstime + interval;
925 }
55e303ae 926}
2d21ac55 927
b0d623f7 928#if CONFIG_DTRACE
2d21ac55
A
929
930/*
931 * clock_get_calendar_nanotime_nowait
932 *
933 * Description: Non-blocking version of clock_get_calendar_nanotime()
934 *
935 * Notes: This function operates by separately tracking calendar time
936 * updates using a two element structure to copy the calendar
937 * state, which may be asynchronously modified. It utilizes
938 * barrier instructions in the tracking process and in the local
939 * stable snapshot process in order to ensure that a consistent
940 * snapshot is used to perform the calculation.
941 */
942void
943clock_get_calendar_nanotime_nowait(
b0d623f7
A
944 clock_sec_t *secs,
945 clock_nsec_t *nanosecs)
2d21ac55
A
946{
947 int i = 0;
948 uint64_t now;
949 struct unlocked_clock_calend stable;
950
951 for (;;) {
952 stable = flipflop[i]; /* take snapshot */
953
954 /*
955 * Use a barrier instructions to ensure atomicity. We AND
956 * off the "in progress" bit to get the current generation
957 * count.
958 */
959 (void)hw_atomic_and(&stable.gen, ~(uint32_t)1);
960
961 /*
962 * If an update _is_ in progress, the generation count will be
963 * off by one, if it _was_ in progress, it will be off by two,
964 * and if we caught it at a good time, it will be equal (and
965 * our snapshot is threfore stable).
966 */
967 if (flipflop[i].gen == stable.gen)
968 break;
969
970 /* Switch to the oher element of the flipflop, and try again. */
971 i ^= 1;
972 }
973
974 now = mach_absolute_time();
975
976 if (stable.calend.adjdelta < 0) {
977 uint32_t t32;
978
979 if (now > stable.calend.adjstart) {
b0d623f7 980 t32 = (uint32_t)(now - stable.calend.adjstart);
2d21ac55
A
981
982 if (t32 > stable.calend.adjoffset)
983 now -= stable.calend.adjoffset;
984 else
985 now = stable.calend.adjstart;
986 }
987 }
988
989 now += stable.calend.offset;
990
991 absolutetime_to_microtime(now, secs, nanosecs);
992 *nanosecs *= NSEC_PER_USEC;
993
b0d623f7 994 *secs += (clock_sec_t)stable.calend.epoch;
2d21ac55
A
995}
996
997static void
998clock_track_calend_nowait(void)
999{
1000 int i;
1001
1002 for (i = 0; i < 2; i++) {
1003 struct clock_calend tmp = clock_calend;
1004
1005 /*
1006 * Set the low bit if the generation count; since we use a
1007 * barrier instruction to do this, we are guaranteed that this
1008 * will flag an update in progress to an async caller trying
1009 * to examine the contents.
1010 */
1011 (void)hw_atomic_or(&flipflop[i].gen, 1);
1012
1013 flipflop[i].calend = tmp;
1014
1015 /*
1016 * Increment the generation count to clear the low bit to
1017 * signal completion. If a caller compares the generation
1018 * count after taking a copy while in progress, the count
1019 * will be off by two.
1020 */
1021 (void)hw_atomic_add(&flipflop[i].gen, 1);
1022 }
1023}
b0d623f7
A
1024
1025#endif /* CONFIG_DTRACE */