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