]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_time.c
c31bbb507750444a277a35cf041d2bec4ddfbc75
[apple/xnu.git] / bsd / kern / kern_time.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*
27 * Copyright (c) 1982, 1986, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
59 */
60
61 #include <sys/param.h>
62 #include <sys/resourcevar.h>
63 #include <sys/kernel.h>
64 #include <sys/systm.h>
65 #include <sys/proc.h>
66 #include <sys/vnode.h>
67
68 #include <sys/mount.h>
69
70 #include <kern/clock.h>
71
72 #define HZ 100 /* XXX */
73
74 volatile struct timeval time;
75 /* simple lock used to access timezone, tz structure */
76 decl_simple_lock_data(, tz_slock);
77 /*
78 * Time of day and interval timer support.
79 *
80 * These routines provide the kernel entry points to get and set
81 * the time-of-day and per-process interval timers. Subroutines
82 * here provide support for adding and subtracting timeval structures
83 * and decrementing interval timers, optionally reloading the interval
84 * timers when they expire.
85 */
86 struct gettimeofday_args{
87 struct timeval *tp;
88 struct timezone *tzp;
89 };
90 /* ARGSUSED */
91 int
92 gettimeofday(p, uap, retval)
93 struct proc *p;
94 register struct gettimeofday_args *uap;
95 register_t *retval;
96 {
97 struct timeval atv;
98 int error = 0;
99 extern simple_lock_data_t tz_slock;
100 struct timezone ltz; /* local copy */
101
102 /* NOTE THIS implementation is for non ppc architectures only */
103
104 if (uap->tp) {
105 microtime(&atv);
106 if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
107 sizeof (atv)))
108 return(error);
109 }
110
111 if (uap->tzp) {
112 usimple_lock(&tz_slock);
113 ltz = tz;
114 usimple_unlock(&tz_slock);
115 error = copyout((caddr_t)&ltz, (caddr_t)uap->tzp,
116 sizeof (tz));
117 }
118
119 return(error);
120 }
121
122 struct settimeofday_args {
123 struct timeval *tv;
124 struct timezone *tzp;
125 };
126 /* ARGSUSED */
127 int
128 settimeofday(p, uap, retval)
129 struct proc *p;
130 struct settimeofday_args *uap;
131 register_t *retval;
132 {
133 struct timeval atv;
134 struct timezone atz;
135 int error, s;
136 extern simple_lock_data_t tz_slock;
137
138 if (error = suser(p->p_ucred, &p->p_acflag))
139 return (error);
140 /* Verify all parameters before changing time. */
141 if (uap->tv && (error = copyin((caddr_t)uap->tv,
142 (caddr_t)&atv, sizeof(atv))))
143 return (error);
144 if (uap->tzp && (error = copyin((caddr_t)uap->tzp,
145 (caddr_t)&atz, sizeof(atz))))
146 return (error);
147 if (uap->tv)
148 setthetime(&atv);
149 if (uap->tzp) {
150 usimple_lock(&tz_slock);
151 tz = atz;
152 usimple_unlock(&tz_slock);
153 }
154 return (0);
155 }
156
157 setthetime(tv)
158 struct timeval *tv;
159 {
160 long delta = tv->tv_sec - time.tv_sec;
161 mach_timespec_t now;
162
163 now.tv_sec = tv->tv_sec;
164 now.tv_nsec = tv->tv_usec * NSEC_PER_USEC;
165
166 clock_set_calendar_value(now);
167 boottime.tv_sec += delta;
168 #if NFSCLIENT || NFSSERVER
169 lease_updatetime(delta);
170 #endif
171 }
172
173 #define tickadj (40 * NSEC_PER_USEC) /* "standard" skew, ns / 10 ms */
174 #define bigadj (1 * NSEC_PER_SEC) /* use 10x skew above bigadj ns */
175
176 struct adjtime_args {
177 struct timeval *delta;
178 struct timeval *olddelta;
179 };
180 /* ARGSUSED */
181 int
182 adjtime(p, uap, retval)
183 struct proc *p;
184 register struct adjtime_args *uap;
185 register_t *retval;
186 {
187 struct timeval atv;
188 int64_t total;
189 uint32_t delta;
190 int error;
191
192 if (error = suser(p->p_ucred, &p->p_acflag))
193 return (error);
194 if (error = copyin((caddr_t)uap->delta,
195 (caddr_t)&atv, sizeof (struct timeval)))
196 return (error);
197
198 /*
199 * Compute the total correction and the rate at which to apply it.
200 */
201 total = (int64_t)atv.tv_sec * NSEC_PER_SEC + atv.tv_usec * NSEC_PER_USEC;
202 if (total > bigadj || total < -bigadj)
203 delta = 10 * tickadj;
204 else
205 delta = tickadj;
206
207 total = clock_set_calendar_adjtime(total, delta);
208
209 if (uap->olddelta) {
210 atv.tv_sec = total / NSEC_PER_SEC;
211 atv.tv_usec = (total / NSEC_PER_USEC) % USEC_PER_SEC;
212 (void) copyout((caddr_t)&atv,
213 (caddr_t)uap->olddelta, sizeof (struct timeval));
214 }
215
216 return (0);
217 }
218
219 /*
220 * Initialze the time of day register.
221 * Trust the RTC except for the case where it is set before
222 * the UNIX epoch. In that case use the the UNIX epoch.
223 * The argument passed in is ignored.
224 */
225 void
226 inittodr(base)
227 time_t base;
228 {
229 /*
230 * Assertion:
231 * The calendar has already been
232 * set up from the battery clock.
233 *
234 * The value returned by microtime()
235 * is gotten from the calendar.
236 */
237 microtime(&time);
238
239 /*
240 * This variable still exists to keep
241 * 'w' happy. It should only be considered
242 * an approximation.
243 */
244 boottime.tv_sec = time.tv_sec;
245 boottime.tv_usec = 0;
246
247 /*
248 * If the RTC does not have acceptable value, i.e. time before
249 * the UNIX epoch, set it to the UNIX epoch
250 */
251 if (time.tv_sec < 0) {
252 printf ("WARNING: preposterous time in Real Time Clock");
253 time.tv_sec = 0; /* the UNIX epoch */
254 time.tv_usec = 0;
255 setthetime(&time);
256 boottime = time;
257 printf(" -- CHECK AND RESET THE DATE!\n");
258 }
259
260 return;
261 }
262
263 void timevaladd(
264 struct timeval *t1,
265 struct timeval *t2);
266 void timevalsub(
267 struct timeval *t1,
268 struct timeval *t2);
269 void timevalfix(
270 struct timeval *t1);
271
272 uint64_t
273 tvtoabstime(
274 struct timeval *tvp);
275
276 /*
277 * Get value of an interval timer. The process virtual and
278 * profiling virtual time timers are kept internally in the
279 * way they are specified externally: in time until they expire.
280 *
281 * The real time interval timer expiration time (p_rtime)
282 * is kept as an absolute time rather than as a delta, so that
283 * it is easy to keep periodic real-time signals from drifting.
284 *
285 * Virtual time timers are processed in the hardclock() routine of
286 * kern_clock.c. The real time timer is processed by a callout
287 * routine. Since a callout may be delayed in real time due to
288 * other processing in the system, it is possible for the real
289 * time callout routine (realitexpire, given below), to be delayed
290 * in real time past when it is supposed to occur. It does not
291 * suffice, therefore, to reload the real time .it_value from the
292 * real time .it_interval. Rather, we compute the next time in
293 * absolute time when the timer should go off.
294 */
295
296 struct getitimer_args {
297 u_int which;
298 struct itimerval *itv;
299 };
300 /* ARGSUSED */
301 int
302 getitimer(p, uap, retval)
303 struct proc *p;
304 register struct getitimer_args *uap;
305 register_t *retval;
306 {
307 struct itimerval aitv;
308
309 if (uap->which > ITIMER_PROF)
310 return(EINVAL);
311 if (uap->which == ITIMER_REAL) {
312 /*
313 * If time for real time timer has passed return 0,
314 * else return difference between current time and
315 * time for the timer to go off.
316 */
317 aitv = p->p_realtimer;
318 if (timerisset(&p->p_rtime)) {
319 struct timeval now;
320
321 microuptime(&now);
322 if (timercmp(&p->p_rtime, &now, <))
323 timerclear(&aitv.it_value);
324 else {
325 aitv.it_value = p->p_rtime;
326 timevalsub(&aitv.it_value, &now);
327 }
328 }
329 else
330 timerclear(&aitv.it_value);
331 }
332 else
333 aitv = p->p_stats->p_timer[uap->which];
334
335 return (copyout((caddr_t)&aitv,
336 (caddr_t)uap->itv, sizeof (struct itimerval)));
337 }
338
339 struct setitimer_args {
340 u_int which;
341 struct itimerval *itv;
342 struct itimerval *oitv;
343 };
344 /* ARGSUSED */
345 int
346 setitimer(p, uap, retval)
347 struct proc *p;
348 register struct setitimer_args *uap;
349 register_t *retval;
350 {
351 struct itimerval aitv;
352 register struct itimerval *itvp;
353 int error;
354
355 if (uap->which > ITIMER_PROF)
356 return (EINVAL);
357 if ((itvp = uap->itv) &&
358 (error = copyin((caddr_t)itvp,
359 (caddr_t)&aitv, sizeof (struct itimerval))))
360 return (error);
361 if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
362 return (error);
363 if (itvp == 0)
364 return (0);
365 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
366 return (EINVAL);
367 if (uap->which == ITIMER_REAL) {
368 thread_call_func_cancel(realitexpire, (void *)p->p_pid, FALSE);
369 if (timerisset(&aitv.it_value)) {
370 microuptime(&p->p_rtime);
371 timevaladd(&p->p_rtime, &aitv.it_value);
372 thread_call_func_delayed(
373 realitexpire, (void *)p->p_pid,
374 tvtoabstime(&p->p_rtime));
375 }
376 else
377 timerclear(&p->p_rtime);
378
379 p->p_realtimer = aitv;
380 }
381 else
382 p->p_stats->p_timer[uap->which] = aitv;
383
384 return (0);
385 }
386
387 /*
388 * Real interval timer expired:
389 * send process whose timer expired an alarm signal.
390 * If time is not set up to reload, then just return.
391 * Else compute next time timer should go off which is > current time.
392 * This is where delay in processing this timeout causes multiple
393 * SIGALRM calls to be compressed into one.
394 */
395 void
396 realitexpire(
397 void *pid)
398 {
399 register struct proc *p;
400 struct timeval now;
401 boolean_t funnel_state = thread_funnel_set(kernel_flock, TRUE);
402
403 p = pfind((pid_t)pid);
404 if (p == NULL) {
405 (void) thread_funnel_set(kernel_flock, FALSE);
406 return;
407 }
408
409 if (!timerisset(&p->p_realtimer.it_interval)) {
410 timerclear(&p->p_rtime);
411 psignal(p, SIGALRM);
412
413 (void) thread_funnel_set(kernel_flock, FALSE);
414 return;
415 }
416
417 microuptime(&now);
418 timevaladd(&p->p_rtime, &p->p_realtimer.it_interval);
419 if (timercmp(&p->p_rtime, &now, <=)) {
420 if ((p->p_rtime.tv_sec + 2) >= now.tv_sec) {
421 for (;;) {
422 timevaladd(&p->p_rtime, &p->p_realtimer.it_interval);
423 if (timercmp(&p->p_rtime, &now, >))
424 break;
425 }
426 }
427 else {
428 p->p_rtime = p->p_realtimer.it_interval;
429 timevaladd(&p->p_rtime, &now);
430 }
431 }
432
433 thread_call_func_delayed(realitexpire, pid, tvtoabstime(&p->p_rtime));
434
435 psignal(p, SIGALRM);
436
437 (void) thread_funnel_set(kernel_flock, FALSE);
438 }
439
440 /*
441 * Check that a proposed value to load into the .it_value or
442 * .it_interval part of an interval timer is acceptable, and
443 * fix it to have at least minimal value (i.e. if it is less
444 * than the resolution of the clock, round it up.)
445 */
446 int
447 itimerfix(tv)
448 struct timeval *tv;
449 {
450
451 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
452 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
453 return (EINVAL);
454 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
455 tv->tv_usec = tick;
456 return (0);
457 }
458
459 /*
460 * Decrement an interval timer by a specified number
461 * of microseconds, which must be less than a second,
462 * i.e. < 1000000. If the timer expires, then reload
463 * it. In this case, carry over (usec - old value) to
464 * reducint the value reloaded into the timer so that
465 * the timer does not drift. This routine assumes
466 * that it is called in a context where the timers
467 * on which it is operating cannot change in value.
468 */
469 int
470 itimerdecr(itp, usec)
471 register struct itimerval *itp;
472 int usec;
473 {
474
475 if (itp->it_value.tv_usec < usec) {
476 if (itp->it_value.tv_sec == 0) {
477 /* expired, and already in next interval */
478 usec -= itp->it_value.tv_usec;
479 goto expire;
480 }
481 itp->it_value.tv_usec += 1000000;
482 itp->it_value.tv_sec--;
483 }
484 itp->it_value.tv_usec -= usec;
485 usec = 0;
486 if (timerisset(&itp->it_value))
487 return (1);
488 /* expired, exactly at end of interval */
489 expire:
490 if (timerisset(&itp->it_interval)) {
491 itp->it_value = itp->it_interval;
492 itp->it_value.tv_usec -= usec;
493 if (itp->it_value.tv_usec < 0) {
494 itp->it_value.tv_usec += 1000000;
495 itp->it_value.tv_sec--;
496 }
497 } else
498 itp->it_value.tv_usec = 0; /* sec is already 0 */
499 return (0);
500 }
501
502 /*
503 * Add and subtract routines for timevals.
504 * N.B.: subtract routine doesn't deal with
505 * results which are before the beginning,
506 * it just gets very confused in this case.
507 * Caveat emptor.
508 */
509 void
510 timevaladd(
511 struct timeval *t1,
512 struct timeval *t2)
513 {
514
515 t1->tv_sec += t2->tv_sec;
516 t1->tv_usec += t2->tv_usec;
517 timevalfix(t1);
518 }
519 void
520 timevalsub(
521 struct timeval *t1,
522 struct timeval *t2)
523 {
524
525 t1->tv_sec -= t2->tv_sec;
526 t1->tv_usec -= t2->tv_usec;
527 timevalfix(t1);
528 }
529 void
530 timevalfix(
531 struct timeval *t1)
532 {
533
534 if (t1->tv_usec < 0) {
535 t1->tv_sec--;
536 t1->tv_usec += 1000000;
537 }
538 if (t1->tv_usec >= 1000000) {
539 t1->tv_sec++;
540 t1->tv_usec -= 1000000;
541 }
542 }
543
544 /*
545 * Return the best possible estimate of the time in the timeval
546 * to which tvp points.
547 */
548 void
549 microtime(
550 struct timeval *tvp)
551 {
552 mach_timespec_t now = clock_get_calendar_value();
553
554 tvp->tv_sec = now.tv_sec;
555 tvp->tv_usec = now.tv_nsec / NSEC_PER_USEC;
556 }
557
558 void
559 microuptime(
560 struct timeval *tvp)
561 {
562 mach_timespec_t now = clock_get_system_value();
563
564 tvp->tv_sec = now.tv_sec;
565 tvp->tv_usec = now.tv_nsec / NSEC_PER_USEC;
566 }
567
568 /*
569 * Ditto for timespec.
570 */
571 void
572 nanotime(
573 struct timespec *tsp)
574 {
575 mach_timespec_t now = clock_get_calendar_value();
576
577 tsp->tv_sec = now.tv_sec;
578 tsp->tv_nsec = now.tv_nsec;
579 }
580
581 void
582 nanouptime(
583 struct timespec *tsp)
584 {
585 mach_timespec_t now = clock_get_system_value();
586
587 tsp->tv_sec = now.tv_sec;
588 tsp->tv_nsec = now.tv_nsec;
589 }
590
591 uint64_t
592 tvtoabstime(
593 struct timeval *tvp)
594 {
595 uint64_t result, usresult;
596
597 clock_interval_to_absolutetime_interval(
598 tvp->tv_sec, NSEC_PER_SEC, &result);
599 clock_interval_to_absolutetime_interval(
600 tvp->tv_usec, NSEC_PER_USEC, &usresult);
601
602 return (result + usresult);
603 }
604 void
605 time_zone_slock_init(void)
606 {
607 extern simple_lock_data_t tz_slock;
608
609 simple_lock_init(&tz_slock);
610
611
612 }