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