]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_time.c
xnu-792.1.5.tar.gz
[apple/xnu.git] / bsd / kern / kern_time.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
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 *
e5568f75
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,
e5568f75
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
86int gettimeofday(struct proc *p,
87#ifdef __ppc__
88 struct ppc_gettimeofday_args *uap,
89#else
90 struct gettimeofday_args *uap,
91#endif
92 register_t *retval);
93
1c79356b
A
94/*
95 * Time of day and interval timer support.
96 *
97 * These routines provide the kernel entry points to get and set
98 * the time-of-day and per-process interval timers. Subroutines
99 * here provide support for adding and subtracting timeval structures
100 * and decrementing interval timers, optionally reloading the interval
101 * timers when they expire.
91447636
A
102 *
103 * XXX Y2038 bug because of clock_get_calendar_microtime() first argument
1c79356b 104 */
1c79356b
A
105/* ARGSUSED */
106int
91447636
A
107gettimeofday(__unused struct proc *p,
108#ifdef __ppc__
109 register struct ppc_gettimeofday_args *uap,
110#else
111 register struct gettimeofday_args *uap,
112#endif
113 __unused register_t *retval)
1c79356b
A
114{
115 struct timeval atv;
116 int error = 0;
9bccf70c
A
117 struct timezone ltz; /* local copy */
118
119/* NOTE THIS implementation is for non ppc architectures only */
1c79356b
A
120
121 if (uap->tp) {
91447636
A
122 clock_get_calendar_microtime((uint32_t *)&atv.tv_sec, &atv.tv_usec);
123 if (IS_64BIT_PROCESS(p)) {
124 struct user_timeval user_atv;
125 user_atv.tv_sec = atv.tv_sec;
126 user_atv.tv_usec = atv.tv_usec;
127 /*
128 * This cast is not necessary for PPC, but is
129 * mostly harmless.
130 */
131 error = copyout(&user_atv, CAST_USER_ADDR_T(uap->tp), sizeof(struct user_timeval));
132 } else {
133 error = copyout(&atv, CAST_USER_ADDR_T(uap->tp), sizeof(struct timeval));
134 }
135 if (error)
1c79356b
A
136 return(error);
137 }
138
9bccf70c 139 if (uap->tzp) {
91447636 140 lck_spin_lock(tz_slock);
9bccf70c 141 ltz = tz;
91447636
A
142 lck_spin_unlock(tz_slock);
143 error = copyout((caddr_t)&ltz, CAST_USER_ADDR_T(uap->tzp),
1c79356b 144 sizeof (tz));
9bccf70c 145 }
1c79356b
A
146
147 return(error);
148}
149
91447636
A
150/*
151 * XXX Y2038 bug because of setthetime() argument
152 */
1c79356b
A
153/* ARGSUSED */
154int
91447636 155settimeofday(struct proc *p, struct settimeofday_args *uap, __unused register_t *retval)
1c79356b
A
156{
157 struct timeval atv;
158 struct timezone atz;
91447636 159 int error;
1c79356b 160
91447636 161 if ((error = suser(kauth_cred_get(), &p->p_acflag)))
1c79356b 162 return (error);
91447636
A
163 /* Verify all parameters before changing time */
164 if (uap->tv) {
165 if (IS_64BIT_PROCESS(p)) {
166 struct user_timeval user_atv;
167 error = copyin(uap->tv, &user_atv, sizeof(struct user_timeval));
168 atv.tv_sec = user_atv.tv_sec;
169 atv.tv_usec = user_atv.tv_usec;
170 } else {
171 error = copyin(uap->tv, &atv, sizeof(struct timeval));
172 }
173 if (error)
174 return (error);
175 }
176 if (uap->tzp && (error = copyin(uap->tzp, (caddr_t)&atz, sizeof(atz))))
1c79356b 177 return (error);
91447636
A
178 if (uap->tv) {
179 timevalfix(&atv);
180 if (atv.tv_sec < 0 || (atv.tv_sec == 0 && atv.tv_usec < 0))
181 return (EPERM);
1c79356b 182 setthetime(&atv);
91447636 183 }
9bccf70c 184 if (uap->tzp) {
91447636 185 lck_spin_lock(tz_slock);
1c79356b 186 tz = atz;
91447636 187 lck_spin_unlock(tz_slock);
9bccf70c 188 }
1c79356b
A
189 return (0);
190}
191
91447636
A
192static void
193setthetime(
194 struct timeval *tv)
1c79356b 195{
55e303ae 196 clock_set_calendar_microtime(tv->tv_sec, tv->tv_usec);
1c79356b
A
197}
198
91447636
A
199/*
200 * XXX Y2038 bug because of clock_adjtime() first argument
201 */
1c79356b
A
202/* ARGSUSED */
203int
91447636 204adjtime(struct proc *p, register struct adjtime_args *uap, __unused register_t *retval)
1c79356b 205{
9bccf70c 206 struct timeval atv;
9bccf70c 207 int error;
1c79356b 208
91447636 209 if ((error = suser(kauth_cred_get(), &p->p_acflag)))
1c79356b 210 return (error);
91447636
A
211 if (IS_64BIT_PROCESS(p)) {
212 struct user_timeval user_atv;
213 error = copyin(uap->delta, &user_atv, sizeof(struct user_timeval));
214 atv.tv_sec = user_atv.tv_sec;
215 atv.tv_usec = user_atv.tv_usec;
216 } else {
217 error = copyin(uap->delta, &atv, sizeof(struct timeval));
218 }
219 if (error)
9bccf70c 220 return (error);
1c79356b 221
91447636
A
222 /*
223 * Compute the total correction and the rate at which to apply it.
224 */
225 clock_adjtime((int32_t *)&atv.tv_sec, &atv.tv_usec);
1c79356b 226
1c79356b 227 if (uap->olddelta) {
91447636
A
228 if (IS_64BIT_PROCESS(p)) {
229 struct user_timeval user_atv;
230 user_atv.tv_sec = atv.tv_sec;
231 user_atv.tv_usec = atv.tv_usec;
232 error = copyout(&user_atv, uap->olddelta, sizeof(struct user_timeval));
233 } else {
234 error = copyout(&atv, uap->olddelta, sizeof(struct timeval));
235 }
1c79356b 236 }
1c79356b 237
9bccf70c 238 return (0);
1c79356b
A
239}
240
1c79356b 241/*
91447636
A
242 * Verify the calendar value. If negative,
243 * reset to zero (the epoch).
1c79356b
A
244 */
245void
91447636
A
246inittodr(
247 __unused time_t base)
1c79356b 248{
55e303ae
A
249 struct timeval tv;
250
1c79356b 251 /*
0b4e3aa0
A
252 * Assertion:
253 * The calendar has already been
91447636 254 * set up from the platform clock.
0b4e3aa0 255 *
1c79356b
A
256 * The value returned by microtime()
257 * is gotten from the calendar.
258 */
55e303ae 259 microtime(&tv);
1c79356b 260
91447636 261 if (tv.tv_sec < 0 || tv.tv_usec < 0) {
1c79356b 262 printf ("WARNING: preposterous time in Real Time Clock");
91447636
A
263 tv.tv_sec = 0; /* the UNIX epoch */
264 tv.tv_usec = 0;
265 setthetime(&tv);
1c79356b
A
266 printf(" -- CHECK AND RESET THE DATE!\n");
267 }
1c79356b
A
268}
269
91447636
A
270time_t
271boottime_sec(void)
272{
273 uint32_t sec, nanosec;
274 clock_get_boottime_nanotime(&sec, &nanosec);
275 return (sec);
276}
9bccf70c 277
91447636 278uint64_t tvtoabstime(struct timeval *tvp);
9bccf70c 279
1c79356b
A
280/*
281 * Get value of an interval timer. The process virtual and
9bccf70c 282 * profiling virtual time timers are kept internally in the
1c79356b
A
283 * way they are specified externally: in time until they expire.
284 *
9bccf70c
A
285 * The real time interval timer expiration time (p_rtime)
286 * is kept as an absolute time rather than as a delta, so that
287 * it is easy to keep periodic real-time signals from drifting.
1c79356b
A
288 *
289 * Virtual time timers are processed in the hardclock() routine of
9bccf70c
A
290 * kern_clock.c. The real time timer is processed by a callout
291 * routine. Since a callout may be delayed in real time due to
292 * other processing in the system, it is possible for the real
293 * time callout routine (realitexpire, given below), to be delayed
294 * in real time past when it is supposed to occur. It does not
295 * suffice, therefore, to reload the real time .it_value from the
296 * real time .it_interval. Rather, we compute the next time in
297 * absolute time when the timer should go off.
1c79356b
A
298 */
299
1c79356b
A
300/* ARGSUSED */
301int
91447636 302getitimer(struct proc *p, register struct getitimer_args *uap, __unused register_t *retval)
1c79356b
A
303{
304 struct itimerval aitv;
1c79356b
A
305
306 if (uap->which > ITIMER_PROF)
307 return(EINVAL);
1c79356b
A
308 if (uap->which == ITIMER_REAL) {
309 /*
9bccf70c
A
310 * If time for real time timer has passed return 0,
311 * else return difference between current time and
312 * time for the timer to go off.
1c79356b
A
313 */
314 aitv = p->p_realtimer;
9bccf70c
A
315 if (timerisset(&p->p_rtime)) {
316 struct timeval now;
317
318 microuptime(&now);
319 if (timercmp(&p->p_rtime, &now, <))
1c79356b 320 timerclear(&aitv.it_value);
9bccf70c
A
321 else {
322 aitv.it_value = p->p_rtime;
323 timevalsub(&aitv.it_value, &now);
324 }
325 }
326 else
327 timerclear(&aitv.it_value);
328 }
329 else
330 aitv = p->p_stats->p_timer[uap->which];
331
91447636
A
332 if (IS_64BIT_PROCESS(p)) {
333 struct user_itimerval user_itv;
334 user_itv.it_interval.tv_sec = aitv.it_interval.tv_sec;
335 user_itv.it_interval.tv_usec = aitv.it_interval.tv_usec;
336 user_itv.it_value.tv_sec = aitv.it_value.tv_sec;
337 user_itv.it_value.tv_usec = aitv.it_value.tv_usec;
338 return (copyout((caddr_t)&user_itv, uap->itv, sizeof (struct user_itimerval)));
339 } else {
340 return (copyout((caddr_t)&aitv, uap->itv, sizeof (struct itimerval)));
341 }
1c79356b
A
342}
343
1c79356b
A
344/* ARGSUSED */
345int
346setitimer(p, uap, retval)
347 struct proc *p;
348 register struct setitimer_args *uap;
349 register_t *retval;
350{
351 struct itimerval aitv;
91447636 352 user_addr_t itvp;
9bccf70c 353 int error;
1c79356b
A
354
355 if (uap->which > ITIMER_PROF)
9bccf70c 356 return (EINVAL);
91447636
A
357 if ((itvp = uap->itv)) {
358 if (IS_64BIT_PROCESS(p)) {
359 struct user_itimerval user_itv;
360 if ((error = copyin(itvp, (caddr_t)&user_itv, sizeof (struct user_itimerval))))
361 return (error);
362 aitv.it_interval.tv_sec = user_itv.it_interval.tv_sec;
363 aitv.it_interval.tv_usec = user_itv.it_interval.tv_usec;
364 aitv.it_value.tv_sec = user_itv.it_value.tv_sec;
365 aitv.it_value.tv_usec = user_itv.it_value.tv_usec;
366 } else {
367 if ((error = copyin(itvp, (caddr_t)&aitv, sizeof (struct itimerval))))
368 return (error);
369 }
370 }
371 if ((uap->itv = uap->oitv) && (error = getitimer(p, (struct getitimer_args *)uap, retval)))
1c79356b
A
372 return (error);
373 if (itvp == 0)
374 return (0);
375 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
376 return (EINVAL);
1c79356b 377 if (uap->which == ITIMER_REAL) {
91447636 378 thread_call_func_cancel((thread_call_func_t)realitexpire, (void *)p->p_pid, FALSE);
1c79356b 379 if (timerisset(&aitv.it_value)) {
9bccf70c
A
380 microuptime(&p->p_rtime);
381 timevaladd(&p->p_rtime, &aitv.it_value);
382 thread_call_func_delayed(
91447636 383 (thread_call_func_t)realitexpire, (void *)p->p_pid,
9bccf70c 384 tvtoabstime(&p->p_rtime));
1c79356b 385 }
9bccf70c
A
386 else
387 timerclear(&p->p_rtime);
388
1c79356b 389 p->p_realtimer = aitv;
9bccf70c
A
390 }
391 else
1c79356b 392 p->p_stats->p_timer[uap->which] = aitv;
9bccf70c
A
393
394 return (0);
1c79356b
A
395}
396
397/*
398 * Real interval timer expired:
399 * send process whose timer expired an alarm signal.
400 * If time is not set up to reload, then just return.
401 * Else compute next time timer should go off which is > current time.
402 * This is where delay in processing this timeout causes multiple
403 * SIGALRM calls to be compressed into one.
404 */
405void
9bccf70c
A
406realitexpire(
407 void *pid)
1c79356b
A
408{
409 register struct proc *p;
9bccf70c 410 struct timeval now;
91447636 411 boolean_t funnel_state;
1c79356b 412
91447636 413 funnel_state = thread_funnel_set(kernel_flock, TRUE);
9bccf70c
A
414 p = pfind((pid_t)pid);
415 if (p == NULL) {
416 (void) thread_funnel_set(kernel_flock, FALSE);
1c79356b
A
417 return;
418 }
9bccf70c
A
419
420 if (!timerisset(&p->p_realtimer.it_interval)) {
421 timerclear(&p->p_rtime);
422 psignal(p, SIGALRM);
423
424 (void) thread_funnel_set(kernel_flock, FALSE);
1c79356b 425 return;
1c79356b 426 }
9bccf70c
A
427
428 microuptime(&now);
429 timevaladd(&p->p_rtime, &p->p_realtimer.it_interval);
430 if (timercmp(&p->p_rtime, &now, <=)) {
431 if ((p->p_rtime.tv_sec + 2) >= now.tv_sec) {
432 for (;;) {
433 timevaladd(&p->p_rtime, &p->p_realtimer.it_interval);
434 if (timercmp(&p->p_rtime, &now, >))
435 break;
436 }
437 }
438 else {
439 p->p_rtime = p->p_realtimer.it_interval;
440 timevaladd(&p->p_rtime, &now);
1c79356b 441 }
1c79356b 442 }
9bccf70c 443
9bccf70c
A
444 psignal(p, SIGALRM);
445
91447636 446 thread_call_func_delayed((thread_call_func_t)realitexpire, pid, tvtoabstime(&p->p_rtime));
55e303ae 447
1c79356b
A
448 (void) thread_funnel_set(kernel_flock, FALSE);
449}
450
451/*
452 * Check that a proposed value to load into the .it_value or
453 * .it_interval part of an interval timer is acceptable, and
454 * fix it to have at least minimal value (i.e. if it is less
455 * than the resolution of the clock, round it up.)
456 */
457int
458itimerfix(tv)
459 struct timeval *tv;
460{
461
462 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
463 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
464 return (EINVAL);
465 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
466 tv->tv_usec = tick;
467 return (0);
468}
469
470/*
471 * Decrement an interval timer by a specified number
472 * of microseconds, which must be less than a second,
473 * i.e. < 1000000. If the timer expires, then reload
474 * it. In this case, carry over (usec - old value) to
475 * reducint the value reloaded into the timer so that
476 * the timer does not drift. This routine assumes
477 * that it is called in a context where the timers
478 * on which it is operating cannot change in value.
479 */
480int
481itimerdecr(itp, usec)
482 register struct itimerval *itp;
483 int usec;
484{
485
486 if (itp->it_value.tv_usec < usec) {
487 if (itp->it_value.tv_sec == 0) {
488 /* expired, and already in next interval */
489 usec -= itp->it_value.tv_usec;
490 goto expire;
491 }
492 itp->it_value.tv_usec += 1000000;
493 itp->it_value.tv_sec--;
494 }
495 itp->it_value.tv_usec -= usec;
496 usec = 0;
497 if (timerisset(&itp->it_value))
498 return (1);
499 /* expired, exactly at end of interval */
500expire:
501 if (timerisset(&itp->it_interval)) {
502 itp->it_value = itp->it_interval;
503 itp->it_value.tv_usec -= usec;
504 if (itp->it_value.tv_usec < 0) {
505 itp->it_value.tv_usec += 1000000;
506 itp->it_value.tv_sec--;
507 }
508 } else
509 itp->it_value.tv_usec = 0; /* sec is already 0 */
510 return (0);
511}
512
513/*
514 * Add and subtract routines for timevals.
515 * N.B.: subtract routine doesn't deal with
516 * results which are before the beginning,
517 * it just gets very confused in this case.
518 * Caveat emptor.
519 */
520void
9bccf70c
A
521timevaladd(
522 struct timeval *t1,
523 struct timeval *t2)
1c79356b
A
524{
525
526 t1->tv_sec += t2->tv_sec;
527 t1->tv_usec += t2->tv_usec;
528 timevalfix(t1);
529}
530void
9bccf70c
A
531timevalsub(
532 struct timeval *t1,
533 struct timeval *t2)
1c79356b
A
534{
535
536 t1->tv_sec -= t2->tv_sec;
537 t1->tv_usec -= t2->tv_usec;
538 timevalfix(t1);
539}
540void
9bccf70c
A
541timevalfix(
542 struct timeval *t1)
1c79356b
A
543{
544
545 if (t1->tv_usec < 0) {
546 t1->tv_sec--;
547 t1->tv_usec += 1000000;
548 }
549 if (t1->tv_usec >= 1000000) {
550 t1->tv_sec++;
551 t1->tv_usec -= 1000000;
552 }
553}
554
555/*
556 * Return the best possible estimate of the time in the timeval
557 * to which tvp points.
558 */
559void
9bccf70c
A
560microtime(
561 struct timeval *tvp)
1c79356b 562{
91447636 563 clock_get_calendar_microtime((uint32_t *)&tvp->tv_sec, &tvp->tv_usec);
1c79356b 564}
9bccf70c
A
565
566void
567microuptime(
568 struct timeval *tvp)
569{
91447636 570 clock_get_system_microtime((uint32_t *)&tvp->tv_sec, &tvp->tv_usec);
9bccf70c
A
571}
572
573/*
574 * Ditto for timespec.
575 */
576void
577nanotime(
578 struct timespec *tsp)
579{
91447636 580 clock_get_calendar_nanotime((uint32_t *)&tsp->tv_sec, (uint32_t *)&tsp->tv_nsec);
9bccf70c
A
581}
582
583void
584nanouptime(
585 struct timespec *tsp)
586{
91447636 587 clock_get_system_nanotime((uint32_t *)&tsp->tv_sec, (uint32_t *)&tsp->tv_nsec);
9bccf70c
A
588}
589
590uint64_t
591tvtoabstime(
592 struct timeval *tvp)
593{
594 uint64_t result, usresult;
595
596 clock_interval_to_absolutetime_interval(
597 tvp->tv_sec, NSEC_PER_SEC, &result);
598 clock_interval_to_absolutetime_interval(
599 tvp->tv_usec, NSEC_PER_USEC, &usresult);
600
601 return (result + usresult);
602}
603void
604time_zone_slock_init(void)
605{
91447636
A
606 /* allocate lock group attribute and group */
607 tz_slock_grp_attr = lck_grp_attr_alloc_init();
608 lck_grp_attr_setstat(tz_slock_grp_attr);
9bccf70c 609
91447636 610 tz_slock_grp = lck_grp_alloc_init("tzlock", tz_slock_grp_attr);
9bccf70c 611
91447636
A
612 /* Allocate lock attribute */
613 tz_slock_attr = lck_attr_alloc_init();
614 //lck_attr_setdebug(tz_slock_attr);
9bccf70c 615
91447636
A
616 /* Allocate the spin lock */
617 tz_slock = lck_spin_alloc_init(tz_slock_grp, tz_slock_attr);
9bccf70c 618}
91447636 619