]>
Commit | Line | Data |
---|---|---|
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 |
50 | uint32_t hz_tick_interval = 1; |
51 | ||
2d21ac55 | 52 | |
6d2010ae | 53 | decl_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 | 74 | static 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 | */ | |
95 | static struct unlocked_clock_calend { | |
96 | struct clock_calend calend; /* copy of calendar */ | |
97 | uint32_t gen; /* generation count */ | |
98 | } flipflop[ 2]; | |
b0d623f7 A |
99 | |
100 | static 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 |
111 | static int64_t calend_adjtotal; /* Nanosecond remaining total adjustment */ |
112 | static uint64_t calend_adjdeadline; /* Absolute time value for next adjustment period */ | |
113 | static uint32_t calend_adjinterval; /* Absolute time interval of adjustment period */ | |
114 | ||
115 | static timer_call_data_t calend_adjcall; | |
116 | static uint32_t calend_adjactive; | |
117 | ||
0c530ab8 | 118 | static uint32_t calend_set_adjustment( |
b0d623f7 A |
119 | long *secs, |
120 | int *microsecs); | |
0c530ab8 A |
121 | |
122 | static void calend_adjust_call(void); | |
123 | static uint32_t calend_adjust(void); | |
9bccf70c | 124 | |
55e303ae A |
125 | static thread_call_data_t calend_wakecall; |
126 | ||
0c530ab8 | 127 | extern void IOKitResetTime(void); |
5d5c5d0d | 128 | |
316670eb A |
129 | void _clock_delay_until_deadline(uint64_t interval, |
130 | uint64_t deadline); | |
131 | ||
0c530ab8 | 132 | static uint64_t clock_boottime; /* Seconds boottime epoch */ |
4452a7af | 133 | |
0c530ab8 A |
134 | #define TIME_ADD(rsecs, secs, rfrac, frac, unit) \ |
135 | MACRO_BEGIN \ | |
136 | if (((rfrac) += (frac)) >= (unit)) { \ | |
137 | (rfrac) -= (unit); \ | |
138 | (rsecs) += 1; \ | |
139 | } \ | |
140 | (rsecs) += (secs); \ | |
141 | MACRO_END | |
142 | ||
143 | #define TIME_SUB(rsecs, secs, rfrac, frac, unit) \ | |
144 | MACRO_BEGIN \ | |
b0d623f7 | 145 | if ((int)((rfrac) -= (frac)) < 0) { \ |
0c530ab8 A |
146 | (rfrac) += (unit); \ |
147 | (rsecs) -= 1; \ | |
148 | } \ | |
149 | (rsecs) -= (secs); \ | |
150 | MACRO_END | |
1c79356b A |
151 | |
152 | /* | |
91447636 A |
153 | * clock_config: |
154 | * | |
155 | * Called once at boot to configure the clock subsystem. | |
1c79356b A |
156 | */ |
157 | void | |
158 | clock_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 | */ |
173 | void | |
174 | clock_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 | */ |
187 | void | |
188 | clock_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 | 206 | kern_return_t |
0c530ab8 A |
207 | mach_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 |
230 | void |
231 | clock_get_calendar_microtime( | |
b0d623f7 A |
232 | clock_sec_t *secs, |
233 | clock_usec_t *microsecs) | |
6601e61a | 234 | { |
0c530ab8 A |
235 | uint64_t now; |
236 | spl_t s; | |
4452a7af | 237 | |
0c530ab8 | 238 | s = splclock(); |
b0d623f7 | 239 | clock_lock(); |
4452a7af | 240 | |
0c530ab8 | 241 | now = mach_absolute_time(); |
4452a7af | 242 | |
2d21ac55 | 243 | if (clock_calend.adjdelta < 0) { |
0c530ab8 | 244 | uint32_t t32; |
4452a7af | 245 | |
6d2010ae A |
246 | /* |
247 | * Since offset is decremented during a negative adjustment, | |
248 | * ensure that time increases monotonically without going | |
249 | * temporarily backwards. | |
250 | * If the delta has not yet passed, now is set to the start | |
251 | * of the current adjustment period; otherwise, we're between | |
252 | * the expiry of the delta and the next call to calend_adjust(), | |
253 | * and we offset accordingly. | |
254 | */ | |
2d21ac55 | 255 | if (now > clock_calend.adjstart) { |
b0d623f7 | 256 | t32 = (uint32_t)(now - clock_calend.adjstart); |
0c530ab8 | 257 | |
2d21ac55 A |
258 | if (t32 > clock_calend.adjoffset) |
259 | now -= clock_calend.adjoffset; | |
0c530ab8 | 260 | else |
2d21ac55 | 261 | now = clock_calend.adjstart; |
0c530ab8 A |
262 | } |
263 | } | |
264 | ||
265 | now += clock_calend.offset; | |
266 | ||
267 | absolutetime_to_microtime(now, secs, microsecs); | |
268 | ||
b0d623f7 | 269 | *secs += (clock_sec_t)clock_calend.epoch; |
0c530ab8 | 270 | |
b0d623f7 | 271 | clock_unlock(); |
0c530ab8 | 272 | splx(s); |
21362eb3 | 273 | } |
89b3af67 | 274 | |
21362eb3 | 275 | /* |
0c530ab8 A |
276 | * clock_get_calendar_nanotime: |
277 | * | |
278 | * Returns the current calendar value, | |
279 | * nanoseconds as the fraction. | |
280 | * | |
281 | * Since we do not have an interface to | |
282 | * set the calendar with resolution greater | |
283 | * than a microsecond, we honor that here. | |
21362eb3 | 284 | */ |
0c530ab8 A |
285 | void |
286 | clock_get_calendar_nanotime( | |
b0d623f7 A |
287 | clock_sec_t *secs, |
288 | clock_nsec_t *nanosecs) | |
21362eb3 | 289 | { |
0c530ab8 A |
290 | uint64_t now; |
291 | spl_t s; | |
292 | ||
293 | s = splclock(); | |
b0d623f7 | 294 | clock_lock(); |
0c530ab8 A |
295 | |
296 | now = mach_absolute_time(); | |
297 | ||
2d21ac55 | 298 | if (clock_calend.adjdelta < 0) { |
0c530ab8 A |
299 | uint32_t t32; |
300 | ||
2d21ac55 | 301 | if (now > clock_calend.adjstart) { |
b0d623f7 | 302 | t32 = (uint32_t)(now - clock_calend.adjstart); |
0c530ab8 | 303 | |
2d21ac55 A |
304 | if (t32 > clock_calend.adjoffset) |
305 | now -= clock_calend.adjoffset; | |
0c530ab8 | 306 | else |
2d21ac55 | 307 | now = clock_calend.adjstart; |
0c530ab8 A |
308 | } |
309 | } | |
310 | ||
311 | now += clock_calend.offset; | |
312 | ||
313 | absolutetime_to_microtime(now, secs, nanosecs); | |
6d2010ae | 314 | |
0c530ab8 A |
315 | *nanosecs *= NSEC_PER_USEC; |
316 | ||
b0d623f7 | 317 | *secs += (clock_sec_t)clock_calend.epoch; |
0c530ab8 | 318 | |
b0d623f7 | 319 | clock_unlock(); |
0c530ab8 | 320 | splx(s); |
6601e61a | 321 | } |
4452a7af | 322 | |
6601e61a | 323 | /* |
0c530ab8 A |
324 | * clock_gettimeofday: |
325 | * | |
326 | * Kernel interface for commpage implementation of | |
327 | * gettimeofday() syscall. | |
328 | * | |
329 | * Returns the current calendar value, and updates the | |
330 | * commpage info as appropriate. Because most calls to | |
331 | * gettimeofday() are handled in user mode by the commpage, | |
332 | * this routine should be used infrequently. | |
6601e61a | 333 | */ |
0c530ab8 A |
334 | void |
335 | clock_gettimeofday( | |
b0d623f7 A |
336 | clock_sec_t *secs, |
337 | clock_usec_t *microsecs) | |
6601e61a | 338 | { |
0c530ab8 A |
339 | uint64_t now; |
340 | spl_t s; | |
4452a7af | 341 | |
0c530ab8 | 342 | s = splclock(); |
b0d623f7 | 343 | clock_lock(); |
0c530ab8 A |
344 | |
345 | now = mach_absolute_time(); | |
346 | ||
2d21ac55 | 347 | if (clock_calend.adjdelta >= 0) { |
0c530ab8 | 348 | clock_gettimeofday_set_commpage(now, clock_calend.epoch, clock_calend.offset, secs, microsecs); |
1c79356b | 349 | } |
0c530ab8 A |
350 | else { |
351 | uint32_t t32; | |
4452a7af | 352 | |
2d21ac55 | 353 | if (now > clock_calend.adjstart) { |
b0d623f7 | 354 | t32 = (uint32_t)(now - clock_calend.adjstart); |
0c530ab8 | 355 | |
2d21ac55 A |
356 | if (t32 > clock_calend.adjoffset) |
357 | now -= clock_calend.adjoffset; | |
0c530ab8 | 358 | else |
2d21ac55 | 359 | now = clock_calend.adjstart; |
0c530ab8 A |
360 | } |
361 | ||
362 | now += clock_calend.offset; | |
4452a7af | 363 | |
0c530ab8 A |
364 | absolutetime_to_microtime(now, secs, microsecs); |
365 | ||
b0d623f7 | 366 | *secs += (clock_sec_t)clock_calend.epoch; |
1c79356b | 367 | } |
1c79356b | 368 | |
b0d623f7 | 369 | clock_unlock(); |
0c530ab8 | 370 | splx(s); |
1c79356b A |
371 | } |
372 | ||
373 | /* | |
0c530ab8 A |
374 | * clock_set_calendar_microtime: |
375 | * | |
376 | * Sets the current calendar value by | |
377 | * recalculating the epoch and offset | |
378 | * from the system clock. | |
379 | * | |
380 | * Also adjusts the boottime to keep the | |
381 | * value consistent, writes the new | |
382 | * calendar value to the platform clock, | |
383 | * and sends calendar change notifications. | |
1c79356b | 384 | */ |
0c530ab8 A |
385 | void |
386 | clock_set_calendar_microtime( | |
b0d623f7 A |
387 | clock_sec_t secs, |
388 | clock_usec_t microsecs) | |
1c79356b | 389 | { |
b0d623f7 A |
390 | clock_sec_t sys; |
391 | clock_usec_t microsys; | |
392 | clock_sec_t newsecs; | |
393 | spl_t s; | |
8ad349bb | 394 | |
b0d623f7 | 395 | newsecs = (microsecs < 500*USEC_PER_SEC)? secs: secs + 1; |
0c530ab8 A |
396 | |
397 | s = splclock(); | |
b0d623f7 | 398 | clock_lock(); |
8ad349bb | 399 | |
2d21ac55 | 400 | commpage_disable_timestamp(); |
8f6c56a5 | 401 | |
89b3af67 | 402 | /* |
0c530ab8 A |
403 | * Calculate the new calendar epoch based on |
404 | * the new value and the system clock. | |
89b3af67 | 405 | */ |
0c530ab8 A |
406 | clock_get_system_microtime(&sys, µsys); |
407 | TIME_SUB(secs, sys, microsecs, microsys, USEC_PER_SEC); | |
8f6c56a5 | 408 | |
4452a7af | 409 | /* |
0c530ab8 | 410 | * Adjust the boottime based on the delta. |
4452a7af | 411 | */ |
0c530ab8 | 412 | clock_boottime += secs - clock_calend.epoch; |
21362eb3 | 413 | |
4452a7af | 414 | /* |
0c530ab8 | 415 | * Set the new calendar epoch. |
4452a7af | 416 | */ |
0c530ab8 | 417 | clock_calend.epoch = secs; |
6d2010ae | 418 | |
0c530ab8 | 419 | nanoseconds_to_absolutetime((uint64_t)microsecs * NSEC_PER_USEC, &clock_calend.offset); |
21362eb3 | 420 | |
0c530ab8 A |
421 | /* |
422 | * Cancel any adjustment in progress. | |
423 | */ | |
b0d623f7 | 424 | calend_adjtotal = clock_calend.adjdelta = 0; |
21362eb3 | 425 | |
b0d623f7 | 426 | clock_unlock(); |
6601e61a | 427 | |
0c530ab8 A |
428 | /* |
429 | * Set the new value for the platform clock. | |
430 | */ | |
431 | PESetGMTTimeOfDay(newsecs); | |
6601e61a | 432 | |
0c530ab8 | 433 | splx(s); |
6601e61a | 434 | |
0c530ab8 A |
435 | /* |
436 | * Send host notifications. | |
437 | */ | |
438 | host_notify_calendar_change(); | |
2d21ac55 A |
439 | |
440 | #if CONFIG_DTRACE | |
441 | clock_track_calend_nowait(); | |
442 | #endif | |
1c79356b A |
443 | } |
444 | ||
445 | /* | |
0c530ab8 A |
446 | * clock_initialize_calendar: |
447 | * | |
448 | * Set the calendar and related clocks | |
449 | * from the platform clock at boot or | |
450 | * wake event. | |
451 | * | |
452 | * Also sends host notifications. | |
1c79356b A |
453 | */ |
454 | void | |
0c530ab8 | 455 | clock_initialize_calendar(void) |
1c79356b | 456 | { |
b0d623f7 A |
457 | clock_sec_t sys, secs = PEGetGMTTimeOfDay(); |
458 | clock_usec_t microsys, microsecs = 0; | |
459 | spl_t s; | |
1c79356b | 460 | |
0c530ab8 | 461 | s = splclock(); |
b0d623f7 | 462 | clock_lock(); |
1c79356b | 463 | |
2d21ac55 | 464 | commpage_disable_timestamp(); |
1c79356b | 465 | |
b0d623f7 | 466 | if ((long)secs >= (long)clock_boottime) { |
0c530ab8 A |
467 | /* |
468 | * Initialize the boot time based on the platform clock. | |
469 | */ | |
470 | if (clock_boottime == 0) | |
471 | clock_boottime = secs; | |
1c79356b A |
472 | |
473 | /* | |
0c530ab8 A |
474 | * Calculate the new calendar epoch based on |
475 | * the platform clock and the system clock. | |
476 | */ | |
477 | clock_get_system_microtime(&sys, µsys); | |
478 | TIME_SUB(secs, sys, microsecs, microsys, USEC_PER_SEC); | |
1c79356b A |
479 | |
480 | /* | |
0c530ab8 | 481 | * Set the new calendar epoch. |
1c79356b | 482 | */ |
0c530ab8 | 483 | clock_calend.epoch = secs; |
6d2010ae | 484 | |
0c530ab8 | 485 | nanoseconds_to_absolutetime((uint64_t)microsecs * NSEC_PER_USEC, &clock_calend.offset); |
1c79356b | 486 | |
0c530ab8 A |
487 | /* |
488 | * Cancel any adjustment in progress. | |
1c79356b | 489 | */ |
b0d623f7 | 490 | calend_adjtotal = clock_calend.adjdelta = 0; |
1c79356b A |
491 | } |
492 | ||
b0d623f7 | 493 | clock_unlock(); |
0c530ab8 A |
494 | splx(s); |
495 | ||
1c79356b | 496 | /* |
0c530ab8 | 497 | * Send host notifications. |
1c79356b | 498 | */ |
0c530ab8 | 499 | host_notify_calendar_change(); |
2d21ac55 A |
500 | |
501 | #if CONFIG_DTRACE | |
502 | clock_track_calend_nowait(); | |
503 | #endif | |
1c79356b A |
504 | } |
505 | ||
506 | /* | |
0c530ab8 A |
507 | * clock_get_boottime_nanotime: |
508 | * | |
509 | * Return the boottime, used by sysctl. | |
1c79356b | 510 | */ |
0c530ab8 A |
511 | void |
512 | clock_get_boottime_nanotime( | |
b0d623f7 A |
513 | clock_sec_t *secs, |
514 | clock_nsec_t *nanosecs) | |
1c79356b | 515 | { |
b0d623f7 A |
516 | spl_t s; |
517 | ||
518 | s = splclock(); | |
519 | clock_lock(); | |
520 | ||
521 | *secs = (clock_sec_t)clock_boottime; | |
0c530ab8 | 522 | *nanosecs = 0; |
b0d623f7 A |
523 | |
524 | clock_unlock(); | |
525 | splx(s); | |
1c79356b A |
526 | } |
527 | ||
528 | /* | |
0c530ab8 A |
529 | * clock_adjtime: |
530 | * | |
531 | * Interface to adjtime() syscall. | |
532 | * | |
533 | * Calculates adjustment variables and | |
534 | * initiates adjustment. | |
6601e61a | 535 | */ |
1c79356b | 536 | void |
0c530ab8 | 537 | clock_adjtime( |
b0d623f7 A |
538 | long *secs, |
539 | int *microsecs) | |
1c79356b | 540 | { |
0c530ab8 A |
541 | uint32_t interval; |
542 | spl_t s; | |
1c79356b | 543 | |
0c530ab8 | 544 | s = splclock(); |
b0d623f7 | 545 | clock_lock(); |
1c79356b | 546 | |
0c530ab8 A |
547 | interval = calend_set_adjustment(secs, microsecs); |
548 | if (interval != 0) { | |
b0d623f7 | 549 | calend_adjdeadline = mach_absolute_time() + interval; |
6d2010ae | 550 | if (!timer_call_enter(&calend_adjcall, calend_adjdeadline, TIMER_CALL_CRITICAL)) |
b0d623f7 | 551 | calend_adjactive++; |
1c79356b | 552 | } |
0c530ab8 | 553 | else |
b0d623f7 A |
554 | if (timer_call_cancel(&calend_adjcall)) |
555 | calend_adjactive--; | |
0c530ab8 | 556 | |
b0d623f7 | 557 | clock_unlock(); |
0c530ab8 | 558 | splx(s); |
1c79356b A |
559 | } |
560 | ||
0c530ab8 A |
561 | static uint32_t |
562 | calend_set_adjustment( | |
b0d623f7 A |
563 | long *secs, |
564 | int *microsecs) | |
1c79356b | 565 | { |
0c530ab8 A |
566 | uint64_t now, t64; |
567 | int64_t total, ototal; | |
568 | uint32_t interval = 0; | |
1c79356b | 569 | |
6d2010ae A |
570 | /* |
571 | * Compute the total adjustment time in nanoseconds. | |
572 | */ | |
0c530ab8 | 573 | total = (int64_t)*secs * NSEC_PER_SEC + *microsecs * NSEC_PER_USEC; |
1c79356b | 574 | |
6d2010ae A |
575 | /* |
576 | * Disable commpage gettimeofday(). | |
577 | */ | |
2d21ac55 | 578 | commpage_disable_timestamp(); |
1c79356b | 579 | |
6d2010ae A |
580 | /* |
581 | * Get current absolute time. | |
582 | */ | |
0c530ab8 | 583 | now = mach_absolute_time(); |
1c79356b | 584 | |
6d2010ae A |
585 | /* |
586 | * Save the old adjustment total for later return. | |
587 | */ | |
b0d623f7 | 588 | ototal = calend_adjtotal; |
1c79356b | 589 | |
6d2010ae A |
590 | /* |
591 | * Is a new correction specified? | |
592 | */ | |
0c530ab8 | 593 | if (total != 0) { |
6d2010ae A |
594 | /* |
595 | * Set delta to the standard, small, adjustment skew. | |
596 | */ | |
0c530ab8 | 597 | int32_t delta = calend_adjskew; |
1c79356b | 598 | |
0c530ab8 | 599 | if (total > 0) { |
6d2010ae A |
600 | /* |
601 | * Positive adjustment. If greater than the preset 'big' | |
602 | * threshold, slew at a faster rate, capping if necessary. | |
603 | */ | |
0c530ab8 A |
604 | if (total > calend_adjbig) |
605 | delta *= 10; | |
606 | if (delta > total) | |
b0d623f7 | 607 | delta = (int32_t)total; |
c0fea474 | 608 | |
6d2010ae A |
609 | /* |
610 | * Convert the delta back from ns to absolute time and store in adjoffset. | |
611 | */ | |
0c530ab8 | 612 | nanoseconds_to_absolutetime((uint64_t)delta, &t64); |
b0d623f7 | 613 | clock_calend.adjoffset = (uint32_t)t64; |
0c530ab8 A |
614 | } |
615 | else { | |
6d2010ae A |
616 | /* |
617 | * Negative adjustment; therefore, negate the delta. If | |
618 | * greater than the preset 'big' threshold, slew at a faster | |
619 | * rate, capping if necessary. | |
620 | */ | |
0c530ab8 A |
621 | if (total < -calend_adjbig) |
622 | delta *= 10; | |
623 | delta = -delta; | |
624 | if (delta < total) | |
b0d623f7 | 625 | delta = (int32_t)total; |
5d5c5d0d | 626 | |
6d2010ae A |
627 | /* |
628 | * Save the current absolute time. Subsequent time operations occuring | |
629 | * during this negative correction can make use of this value to ensure | |
630 | * that time increases monotonically. | |
631 | */ | |
2d21ac55 | 632 | clock_calend.adjstart = now; |
89b3af67 | 633 | |
6d2010ae A |
634 | /* |
635 | * Convert the delta back from ns to absolute time and store in adjoffset. | |
636 | */ | |
0c530ab8 | 637 | nanoseconds_to_absolutetime((uint64_t)-delta, &t64); |
b0d623f7 | 638 | clock_calend.adjoffset = (uint32_t)t64; |
0c530ab8 | 639 | } |
4452a7af | 640 | |
6d2010ae A |
641 | /* |
642 | * Store the total adjustment time in ns. | |
643 | */ | |
b0d623f7 | 644 | calend_adjtotal = total; |
6d2010ae A |
645 | |
646 | /* | |
647 | * Store the delta for this adjustment period in ns. | |
648 | */ | |
2d21ac55 | 649 | clock_calend.adjdelta = delta; |
0c530ab8 | 650 | |
6d2010ae A |
651 | /* |
652 | * Set the interval in absolute time for later return. | |
653 | */ | |
b0d623f7 | 654 | interval = calend_adjinterval; |
0c530ab8 | 655 | } |
6d2010ae A |
656 | else { |
657 | /* | |
658 | * No change; clear any prior adjustment. | |
659 | */ | |
b0d623f7 | 660 | calend_adjtotal = clock_calend.adjdelta = 0; |
6d2010ae | 661 | } |
1c79356b | 662 | |
6d2010ae A |
663 | /* |
664 | * If an prior correction was in progress, return the | |
665 | * remaining uncorrected time from it. | |
666 | */ | |
0c530ab8 | 667 | if (ototal != 0) { |
b0d623f7 A |
668 | *secs = (long)(ototal / NSEC_PER_SEC); |
669 | *microsecs = (int)((ototal % NSEC_PER_SEC) / NSEC_PER_USEC); | |
0c530ab8 A |
670 | } |
671 | else | |
672 | *secs = *microsecs = 0; | |
1c79356b | 673 | |
2d21ac55 A |
674 | #if CONFIG_DTRACE |
675 | clock_track_calend_nowait(); | |
676 | #endif | |
677 | ||
0c530ab8 | 678 | return (interval); |
1c79356b A |
679 | } |
680 | ||
0c530ab8 A |
681 | static void |
682 | calend_adjust_call(void) | |
1c79356b | 683 | { |
0c530ab8 A |
684 | uint32_t interval; |
685 | spl_t s; | |
1c79356b | 686 | |
0c530ab8 | 687 | s = splclock(); |
b0d623f7 | 688 | clock_lock(); |
1c79356b | 689 | |
b0d623f7 | 690 | if (--calend_adjactive == 0) { |
0c530ab8 A |
691 | interval = calend_adjust(); |
692 | if (interval != 0) { | |
b0d623f7 | 693 | clock_deadline_for_periodic_event(interval, mach_absolute_time(), &calend_adjdeadline); |
1c79356b | 694 | |
6d2010ae | 695 | if (!timer_call_enter(&calend_adjcall, calend_adjdeadline, TIMER_CALL_CRITICAL)) |
b0d623f7 | 696 | calend_adjactive++; |
0c530ab8 | 697 | } |
1c79356b | 698 | } |
0c530ab8 | 699 | |
b0d623f7 | 700 | clock_unlock(); |
0c530ab8 | 701 | splx(s); |
1c79356b A |
702 | } |
703 | ||
0c530ab8 A |
704 | static uint32_t |
705 | calend_adjust(void) | |
1c79356b | 706 | { |
0c530ab8 A |
707 | uint64_t now, t64; |
708 | int32_t delta; | |
709 | uint32_t interval = 0; | |
89b3af67 | 710 | |
2d21ac55 | 711 | commpage_disable_timestamp(); |
89b3af67 | 712 | |
0c530ab8 | 713 | now = mach_absolute_time(); |
89b3af67 | 714 | |
2d21ac55 | 715 | delta = clock_calend.adjdelta; |
89b3af67 | 716 | |
0c530ab8 | 717 | if (delta > 0) { |
2d21ac55 | 718 | clock_calend.offset += clock_calend.adjoffset; |
4452a7af | 719 | |
b0d623f7 A |
720 | calend_adjtotal -= delta; |
721 | if (delta > calend_adjtotal) { | |
722 | clock_calend.adjdelta = delta = (int32_t)calend_adjtotal; | |
4452a7af | 723 | |
0c530ab8 | 724 | nanoseconds_to_absolutetime((uint64_t)delta, &t64); |
b0d623f7 | 725 | clock_calend.adjoffset = (uint32_t)t64; |
0c530ab8 A |
726 | } |
727 | } | |
728 | else | |
6d2010ae A |
729 | if (delta < 0) { |
730 | clock_calend.offset -= clock_calend.adjoffset; | |
4452a7af | 731 | |
6d2010ae A |
732 | calend_adjtotal -= delta; |
733 | if (delta < calend_adjtotal) { | |
734 | clock_calend.adjdelta = delta = (int32_t)calend_adjtotal; | |
4452a7af | 735 | |
6d2010ae A |
736 | nanoseconds_to_absolutetime((uint64_t)-delta, &t64); |
737 | clock_calend.adjoffset = (uint32_t)t64; | |
738 | } | |
739 | ||
740 | if (clock_calend.adjdelta != 0) | |
741 | clock_calend.adjstart = now; | |
0c530ab8 A |
742 | } |
743 | ||
2d21ac55 | 744 | if (clock_calend.adjdelta != 0) |
b0d623f7 | 745 | interval = calend_adjinterval; |
0c530ab8 | 746 | |
2d21ac55 A |
747 | #if CONFIG_DTRACE |
748 | clock_track_calend_nowait(); | |
749 | #endif | |
0c530ab8 A |
750 | |
751 | return (interval); | |
752 | } | |
753 | ||
754 | /* | |
755 | * clock_wakeup_calendar: | |
756 | * | |
757 | * Interface to power management, used | |
758 | * to initiate the reset of the calendar | |
759 | * on wake from sleep event. | |
760 | */ | |
761 | void | |
762 | clock_wakeup_calendar(void) | |
763 | { | |
764 | thread_call_enter(&calend_wakecall); | |
1c79356b A |
765 | } |
766 | ||
0c530ab8 A |
767 | /* |
768 | * Wait / delay routines. | |
769 | */ | |
91447636 A |
770 | static void |
771 | mach_wait_until_continue( | |
772 | __unused void *parameter, | |
773 | wait_result_t wresult) | |
774 | { | |
775 | thread_syscall_return((wresult == THREAD_INTERRUPTED)? KERN_ABORTED: KERN_SUCCESS); | |
776 | /*NOTREACHED*/ | |
777 | } | |
778 | ||
316670eb A |
779 | /* |
780 | * mach_wait_until_trap: Suspend execution of calling thread until the specified time has passed | |
781 | * | |
782 | * Parameters: args->deadline Amount of time to wait | |
783 | * | |
784 | * Returns: 0 Success | |
785 | * !0 Not success | |
786 | * | |
787 | */ | |
1c79356b | 788 | kern_return_t |
91447636 A |
789 | mach_wait_until_trap( |
790 | struct mach_wait_until_trap_args *args) | |
791 | { | |
792 | uint64_t deadline = args->deadline; | |
793 | wait_result_t wresult; | |
794 | ||
795 | wresult = assert_wait_deadline((event_t)mach_wait_until_trap, THREAD_ABORTSAFE, deadline); | |
796 | if (wresult == THREAD_WAITING) | |
797 | wresult = thread_block(mach_wait_until_continue); | |
798 | ||
799 | return ((wresult == THREAD_INTERRUPTED)? KERN_ABORTED: KERN_SUCCESS); | |
800 | } | |
801 | ||
91447636 A |
802 | void |
803 | clock_delay_until( | |
1c79356b A |
804 | uint64_t deadline) |
805 | { | |
91447636 A |
806 | uint64_t now = mach_absolute_time(); |
807 | ||
808 | if (now >= deadline) | |
809 | return; | |
1c79356b | 810 | |
316670eb A |
811 | _clock_delay_until_deadline(deadline - now, deadline); |
812 | } | |
813 | ||
814 | /* | |
815 | * Preserve the original precise interval that the client | |
816 | * requested for comparison to the spin threshold. | |
817 | */ | |
818 | void | |
819 | _clock_delay_until_deadline( | |
820 | uint64_t interval, | |
821 | uint64_t deadline) | |
822 | { | |
823 | ||
824 | if (interval == 0) | |
825 | return; | |
826 | ||
827 | if ( ml_delay_should_spin(interval) || | |
91447636 | 828 | get_preemption_level() != 0 || |
316670eb | 829 | ml_get_interrupts_enabled() == FALSE ) { |
bd504ef0 | 830 | machine_delay_until(interval, deadline); |
316670eb A |
831 | } else { |
832 | assert_wait_deadline((event_t)clock_delay_until, THREAD_UNINT, deadline); | |
91447636 A |
833 | |
834 | thread_block(THREAD_CONTINUE_NULL); | |
9bccf70c | 835 | } |
91447636 | 836 | } |
1c79356b | 837 | |
316670eb | 838 | |
91447636 A |
839 | void |
840 | delay_for_interval( | |
841 | uint32_t interval, | |
842 | uint32_t scale_factor) | |
843 | { | |
316670eb | 844 | uint64_t abstime; |
91447636 | 845 | |
316670eb | 846 | clock_interval_to_absolutetime_interval(interval, scale_factor, &abstime); |
91447636 | 847 | |
316670eb | 848 | _clock_delay_until_deadline(abstime, mach_absolute_time() + abstime); |
91447636 A |
849 | } |
850 | ||
851 | void | |
852 | delay( | |
853 | int usec) | |
854 | { | |
855 | delay_for_interval((usec < 0)? -usec: usec, NSEC_PER_USEC); | |
1c79356b | 856 | } |
9bccf70c | 857 | |
0c530ab8 A |
858 | /* |
859 | * Miscellaneous routines. | |
860 | */ | |
55e303ae | 861 | void |
0c530ab8 A |
862 | clock_interval_to_deadline( |
863 | uint32_t interval, | |
864 | uint32_t scale_factor, | |
865 | uint64_t *result) | |
9bccf70c | 866 | { |
0c530ab8 | 867 | uint64_t abstime; |
c0fea474 | 868 | |
0c530ab8 | 869 | clock_interval_to_absolutetime_interval(interval, scale_factor, &abstime); |
6601e61a | 870 | |
0c530ab8 | 871 | *result = mach_absolute_time() + abstime; |
8f6c56a5 | 872 | } |
5d5c5d0d | 873 | |
0c530ab8 A |
874 | void |
875 | clock_absolutetime_interval_to_deadline( | |
876 | uint64_t abstime, | |
877 | uint64_t *result) | |
8f6c56a5 | 878 | { |
0c530ab8 | 879 | *result = mach_absolute_time() + abstime; |
21362eb3 | 880 | } |
89b3af67 | 881 | |
4452a7af | 882 | void |
0c530ab8 A |
883 | clock_get_uptime( |
884 | uint64_t *result) | |
21362eb3 | 885 | { |
0c530ab8 | 886 | *result = mach_absolute_time(); |
6601e61a | 887 | } |
4452a7af | 888 | |
0c530ab8 A |
889 | void |
890 | clock_deadline_for_periodic_event( | |
891 | uint64_t interval, | |
892 | uint64_t abstime, | |
893 | uint64_t *deadline) | |
6601e61a | 894 | { |
0c530ab8 A |
895 | assert(interval != 0); |
896 | ||
897 | *deadline += interval; | |
898 | ||
899 | if (*deadline <= abstime) { | |
900 | *deadline = abstime + interval; | |
901 | abstime = mach_absolute_time(); | |
55e303ae | 902 | |
0c530ab8 A |
903 | if (*deadline <= abstime) |
904 | *deadline = abstime + interval; | |
905 | } | |
55e303ae | 906 | } |
2d21ac55 | 907 | |
b0d623f7 | 908 | #if CONFIG_DTRACE |
2d21ac55 A |
909 | |
910 | /* | |
911 | * clock_get_calendar_nanotime_nowait | |
912 | * | |
913 | * Description: Non-blocking version of clock_get_calendar_nanotime() | |
914 | * | |
915 | * Notes: This function operates by separately tracking calendar time | |
916 | * updates using a two element structure to copy the calendar | |
917 | * state, which may be asynchronously modified. It utilizes | |
918 | * barrier instructions in the tracking process and in the local | |
919 | * stable snapshot process in order to ensure that a consistent | |
920 | * snapshot is used to perform the calculation. | |
921 | */ | |
922 | void | |
923 | clock_get_calendar_nanotime_nowait( | |
b0d623f7 A |
924 | clock_sec_t *secs, |
925 | clock_nsec_t *nanosecs) | |
2d21ac55 A |
926 | { |
927 | int i = 0; | |
928 | uint64_t now; | |
929 | struct unlocked_clock_calend stable; | |
930 | ||
931 | for (;;) { | |
932 | stable = flipflop[i]; /* take snapshot */ | |
933 | ||
934 | /* | |
935 | * Use a barrier instructions to ensure atomicity. We AND | |
936 | * off the "in progress" bit to get the current generation | |
937 | * count. | |
938 | */ | |
939 | (void)hw_atomic_and(&stable.gen, ~(uint32_t)1); | |
940 | ||
941 | /* | |
942 | * If an update _is_ in progress, the generation count will be | |
943 | * off by one, if it _was_ in progress, it will be off by two, | |
944 | * and if we caught it at a good time, it will be equal (and | |
945 | * our snapshot is threfore stable). | |
946 | */ | |
947 | if (flipflop[i].gen == stable.gen) | |
948 | break; | |
949 | ||
950 | /* Switch to the oher element of the flipflop, and try again. */ | |
951 | i ^= 1; | |
952 | } | |
953 | ||
954 | now = mach_absolute_time(); | |
955 | ||
956 | if (stable.calend.adjdelta < 0) { | |
957 | uint32_t t32; | |
958 | ||
959 | if (now > stable.calend.adjstart) { | |
b0d623f7 | 960 | t32 = (uint32_t)(now - stable.calend.adjstart); |
2d21ac55 A |
961 | |
962 | if (t32 > stable.calend.adjoffset) | |
963 | now -= stable.calend.adjoffset; | |
964 | else | |
965 | now = stable.calend.adjstart; | |
966 | } | |
967 | } | |
968 | ||
969 | now += stable.calend.offset; | |
970 | ||
971 | absolutetime_to_microtime(now, secs, nanosecs); | |
972 | *nanosecs *= NSEC_PER_USEC; | |
973 | ||
b0d623f7 | 974 | *secs += (clock_sec_t)stable.calend.epoch; |
2d21ac55 A |
975 | } |
976 | ||
977 | static void | |
978 | clock_track_calend_nowait(void) | |
979 | { | |
980 | int i; | |
981 | ||
982 | for (i = 0; i < 2; i++) { | |
983 | struct clock_calend tmp = clock_calend; | |
984 | ||
985 | /* | |
986 | * Set the low bit if the generation count; since we use a | |
987 | * barrier instruction to do this, we are guaranteed that this | |
988 | * will flag an update in progress to an async caller trying | |
989 | * to examine the contents. | |
990 | */ | |
991 | (void)hw_atomic_or(&flipflop[i].gen, 1); | |
992 | ||
993 | flipflop[i].calend = tmp; | |
994 | ||
995 | /* | |
996 | * Increment the generation count to clear the low bit to | |
997 | * signal completion. If a caller compares the generation | |
998 | * count after taking a copy while in progress, the count | |
999 | * will be off by two. | |
1000 | */ | |
1001 | (void)hw_atomic_add(&flipflop[i].gen, 1); | |
1002 | } | |
1003 | } | |
b0d623f7 A |
1004 | |
1005 | #endif /* CONFIG_DTRACE */ |