kern_time.c revision 1.136 1 /* $NetBSD: kern_time.c,v 1.136 2007/12/22 00:35:32 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2000, 2004, 2005, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christopher G. Demetriou.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1982, 1986, 1989, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.136 2007/12/22 00:35:32 yamt Exp $");
72
73 #include <sys/param.h>
74 #include <sys/resourcevar.h>
75 #include <sys/kernel.h>
76 #include <sys/systm.h>
77 #include <sys/proc.h>
78 #include <sys/vnode.h>
79 #include <sys/signalvar.h>
80 #include <sys/syslog.h>
81 #include <sys/timetc.h>
82 #include <sys/kauth.h>
83
84 #include <sys/mount.h>
85 #include <sys/syscallargs.h>
86
87 #include <uvm/uvm_extern.h>
88
89 #include <sys/cpu.h>
90
91 kmutex_t time_lock;
92
93 POOL_INIT(ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
94 &pool_allocator_nointr, IPL_NONE);
95 POOL_INIT(ptimers_pool, sizeof(struct ptimers), 0, 0, 0, "ptimerspl",
96 &pool_allocator_nointr, IPL_NONE);
97
98 /*
99 * Initialize timekeeping.
100 */
101 void
102 time_init(void)
103 {
104
105 mutex_init(&time_lock, MUTEX_DEFAULT, IPL_NONE);
106 }
107
108 /* Time of day and interval timer support.
109 *
110 * These routines provide the kernel entry points to get and set
111 * the time-of-day and per-process interval timers. Subroutines
112 * here provide support for adding and subtracting timeval structures
113 * and decrementing interval timers, optionally reloading the interval
114 * timers when they expire.
115 */
116
117 /* This function is used by clock_settime and settimeofday */
118 static int
119 settime1(struct proc *p, struct timespec *ts, bool check_kauth)
120 {
121 struct timeval delta, tv;
122 struct timeval now;
123 struct timespec ts1;
124 lwp_t *l;
125 int s;
126
127 TIMESPEC_TO_TIMEVAL(&tv, ts);
128
129 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
130 s = splclock();
131 microtime(&now);
132 timersub(&tv, &now, &delta);
133
134 if (check_kauth && kauth_authorize_system(kauth_cred_get(),
135 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, ts, &delta,
136 KAUTH_ARG(check_kauth ? false : true)) != 0) {
137 splx(s);
138 return (EPERM);
139 }
140
141 #ifdef notyet
142 if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
143 splx(s);
144 return (EPERM);
145 }
146 #endif
147
148 TIMEVAL_TO_TIMESPEC(&tv, &ts1);
149 tc_setclock(&ts1);
150
151 timeradd(&boottime, &delta, &boottime);
152
153 /*
154 * XXXSMP: There is a short race between setting the time above
155 * and adjusting LWP's run times. Fixing this properly means
156 * pausing all CPUs while we adjust the clock.
157 */
158 mutex_enter(&proclist_lock);
159 LIST_FOREACH(l, &alllwp, l_list) {
160 lwp_lock(l);
161 timeradd(&l->l_stime, &delta, &l->l_stime);
162 lwp_unlock(l);
163 }
164 mutex_exit(&proclist_lock);
165 resettodr();
166 splx(s);
167
168 return (0);
169 }
170
171 int
172 settime(struct proc *p, struct timespec *ts)
173 {
174 return (settime1(p, ts, true));
175 }
176
177 /* ARGSUSED */
178 int
179 sys_clock_gettime(struct lwp *l, const struct sys_clock_gettime_args *uap, register_t *retval)
180 {
181 /* {
182 syscallarg(clockid_t) clock_id;
183 syscallarg(struct timespec *) tp;
184 } */
185 clockid_t clock_id;
186 struct timespec ats;
187
188 clock_id = SCARG(uap, clock_id);
189 switch (clock_id) {
190 case CLOCK_REALTIME:
191 nanotime(&ats);
192 break;
193 case CLOCK_MONOTONIC:
194 nanouptime(&ats);
195 break;
196 default:
197 return (EINVAL);
198 }
199
200 return copyout(&ats, SCARG(uap, tp), sizeof(ats));
201 }
202
203 /* ARGSUSED */
204 int
205 sys_clock_settime(struct lwp *l, const struct sys_clock_settime_args *uap, register_t *retval)
206 {
207 /* {
208 syscallarg(clockid_t) clock_id;
209 syscallarg(const struct timespec *) tp;
210 } */
211
212 return clock_settime1(l->l_proc, SCARG(uap, clock_id), SCARG(uap, tp),
213 true);
214 }
215
216
217 int
218 clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
219 bool check_kauth)
220 {
221 struct timespec ats;
222 int error;
223
224 if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
225 return (error);
226
227 switch (clock_id) {
228 case CLOCK_REALTIME:
229 if ((error = settime1(p, &ats, check_kauth)) != 0)
230 return (error);
231 break;
232 case CLOCK_MONOTONIC:
233 return (EINVAL); /* read-only clock */
234 default:
235 return (EINVAL);
236 }
237
238 return 0;
239 }
240
241 int
242 sys_clock_getres(struct lwp *l, const struct sys_clock_getres_args *uap, register_t *retval)
243 {
244 /* {
245 syscallarg(clockid_t) clock_id;
246 syscallarg(struct timespec *) tp;
247 } */
248 clockid_t clock_id;
249 struct timespec ts;
250 int error = 0;
251
252 clock_id = SCARG(uap, clock_id);
253 switch (clock_id) {
254 case CLOCK_REALTIME:
255 case CLOCK_MONOTONIC:
256 ts.tv_sec = 0;
257 if (tc_getfrequency() > 1000000000)
258 ts.tv_nsec = 1;
259 else
260 ts.tv_nsec = 1000000000 / tc_getfrequency();
261 break;
262 default:
263 return (EINVAL);
264 }
265
266 if (SCARG(uap, tp))
267 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
268
269 return error;
270 }
271
272 /* ARGSUSED */
273 int
274 sys_nanosleep(struct lwp *l, const struct sys_nanosleep_args *uap, register_t *retval)
275 {
276 /* {
277 syscallarg(struct timespec *) rqtp;
278 syscallarg(struct timespec *) rmtp;
279 } */
280 struct timespec rmt, rqt;
281 int error, error1;
282
283 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
284 if (error)
285 return (error);
286
287 error = nanosleep1(l, &rqt, SCARG(uap, rmtp) ? &rmt : NULL);
288 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
289 return error;
290
291 error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
292 return error1 ? error1 : error;
293 }
294
295 int
296 nanosleep1(struct lwp *l, struct timespec *rqt, struct timespec *rmt)
297 {
298 int error, timo;
299
300 if (itimespecfix(rqt))
301 return (EINVAL);
302
303 timo = tstohz(rqt);
304 /*
305 * Avoid inadvertantly sleeping forever
306 */
307 if (timo == 0)
308 timo = 1;
309
310 if (rmt != NULL)
311 getnanouptime(rmt);
312
313 error = kpause("nanoslp", true, timo, NULL);
314 if (error == ERESTART)
315 error = EINTR;
316 if (error == EWOULDBLOCK)
317 error = 0;
318
319 if (rmt!= NULL) {
320 struct timespec rmtend;
321
322 getnanouptime(&rmtend);
323
324 timespecsub(&rmtend, rmt, rmt);
325 timespecsub(rqt, rmt, rmt);
326 if (rmt->tv_sec < 0)
327 timespecclear(rmt);
328 }
329
330 return error;
331 }
332
333 /* ARGSUSED */
334 int
335 sys_gettimeofday(struct lwp *l, const struct sys_gettimeofday_args *uap, register_t *retval)
336 {
337 /* {
338 syscallarg(struct timeval *) tp;
339 syscallarg(void *) tzp; really "struct timezone *";
340 } */
341 struct timeval atv;
342 int error = 0;
343 struct timezone tzfake;
344
345 if (SCARG(uap, tp)) {
346 microtime(&atv);
347 error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
348 if (error)
349 return (error);
350 }
351 if (SCARG(uap, tzp)) {
352 /*
353 * NetBSD has no kernel notion of time zone, so we just
354 * fake up a timezone struct and return it if demanded.
355 */
356 tzfake.tz_minuteswest = 0;
357 tzfake.tz_dsttime = 0;
358 error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
359 }
360 return (error);
361 }
362
363 /* ARGSUSED */
364 int
365 sys_settimeofday(struct lwp *l, const struct sys_settimeofday_args *uap, register_t *retval)
366 {
367 /* {
368 syscallarg(const struct timeval *) tv;
369 syscallarg(const void *) tzp; really "const struct timezone *";
370 } */
371
372 return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
373 }
374
375 int
376 settimeofday1(const struct timeval *utv, bool userspace,
377 const void *utzp, struct lwp *l, bool check_kauth)
378 {
379 struct timeval atv;
380 struct timespec ts;
381 int error;
382
383 /* Verify all parameters before changing time. */
384
385 /*
386 * NetBSD has no kernel notion of time zone, and only an
387 * obsolete program would try to set it, so we log a warning.
388 */
389 if (utzp)
390 log(LOG_WARNING, "pid %d attempted to set the "
391 "(obsolete) kernel time zone\n", l->l_proc->p_pid);
392
393 if (utv == NULL)
394 return 0;
395
396 if (userspace) {
397 if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
398 return error;
399 utv = &atv;
400 }
401
402 TIMEVAL_TO_TIMESPEC(utv, &ts);
403 return settime1(l->l_proc, &ts, check_kauth);
404 }
405
406 #ifndef __HAVE_TIMECOUNTER
407 int tickdelta; /* current clock skew, us. per tick */
408 long timedelta; /* unapplied time correction, us. */
409 long bigadj = 1000000; /* use 10x skew above bigadj us. */
410 #endif
411
412 int time_adjusted; /* set if an adjustment is made */
413
414 /* ARGSUSED */
415 int
416 sys_adjtime(struct lwp *l, const struct sys_adjtime_args *uap, register_t *retval)
417 {
418 /* {
419 syscallarg(const struct timeval *) delta;
420 syscallarg(struct timeval *) olddelta;
421 } */
422 int error;
423
424 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
425 KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
426 return (error);
427
428 return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), l->l_proc);
429 }
430
431 int
432 adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
433 {
434 struct timeval atv;
435 int error = 0;
436
437 #ifdef __HAVE_TIMECOUNTER
438 extern int64_t time_adjtime; /* in kern_ntptime.c */
439 #else /* !__HAVE_TIMECOUNTER */
440 long ndelta, ntickdelta, odelta;
441 int s;
442 #endif /* !__HAVE_TIMECOUNTER */
443
444 #ifdef __HAVE_TIMECOUNTER
445 if (olddelta) {
446 atv.tv_sec = time_adjtime / 1000000;
447 atv.tv_usec = time_adjtime % 1000000;
448 if (atv.tv_usec < 0) {
449 atv.tv_usec += 1000000;
450 atv.tv_sec--;
451 }
452 error = copyout(&atv, olddelta, sizeof(struct timeval));
453 if (error)
454 return (error);
455 }
456
457 if (delta) {
458 error = copyin(delta, &atv, sizeof(struct timeval));
459 if (error)
460 return (error);
461
462 time_adjtime = (int64_t)atv.tv_sec * 1000000 +
463 atv.tv_usec;
464
465 if (time_adjtime)
466 /* We need to save the system time during shutdown */
467 time_adjusted |= 1;
468 }
469 #else /* !__HAVE_TIMECOUNTER */
470 error = copyin(delta, &atv, sizeof(struct timeval));
471 if (error)
472 return (error);
473
474 /*
475 * Compute the total correction and the rate at which to apply it.
476 * Round the adjustment down to a whole multiple of the per-tick
477 * delta, so that after some number of incremental changes in
478 * hardclock(), tickdelta will become zero, lest the correction
479 * overshoot and start taking us away from the desired final time.
480 */
481 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
482 if (ndelta > bigadj || ndelta < -bigadj)
483 ntickdelta = 10 * tickadj;
484 else
485 ntickdelta = tickadj;
486 if (ndelta % ntickdelta)
487 ndelta = ndelta / ntickdelta * ntickdelta;
488
489 /*
490 * To make hardclock()'s job easier, make the per-tick delta negative
491 * if we want time to run slower; then hardclock can simply compute
492 * tick + tickdelta, and subtract tickdelta from timedelta.
493 */
494 if (ndelta < 0)
495 ntickdelta = -ntickdelta;
496 if (ndelta != 0)
497 /* We need to save the system clock time during shutdown */
498 time_adjusted |= 1;
499 s = splclock();
500 odelta = timedelta;
501 timedelta = ndelta;
502 tickdelta = ntickdelta;
503 splx(s);
504
505 if (olddelta) {
506 atv.tv_sec = odelta / 1000000;
507 atv.tv_usec = odelta % 1000000;
508 error = copyout(&atv, olddelta, sizeof(struct timeval));
509 }
510 #endif /* __HAVE_TIMECOUNTER */
511
512 return error;
513 }
514
515 /*
516 * Interval timer support. Both the BSD getitimer() family and the POSIX
517 * timer_*() family of routines are supported.
518 *
519 * All timers are kept in an array pointed to by p_timers, which is
520 * allocated on demand - many processes don't use timers at all. The
521 * first three elements in this array are reserved for the BSD timers:
522 * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
523 * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
524 * syscall.
525 *
526 * Realtime timers are kept in the ptimer structure as an absolute
527 * time; virtual time timers are kept as a linked list of deltas.
528 * Virtual time timers are processed in the hardclock() routine of
529 * kern_clock.c. The real time timer is processed by a callout
530 * routine, called from the softclock() routine. Since a callout may
531 * be delayed in real time due to interrupt processing in the system,
532 * it is possible for the real time timeout routine (realtimeexpire,
533 * given below), to be delayed in real time past when it is supposed
534 * to occur. It does not suffice, therefore, to reload the real timer
535 * .it_value from the real time timers .it_interval. Rather, we
536 * compute the next time in absolute time the timer should go off. */
537
538 /* Allocate a POSIX realtime timer. */
539 int
540 sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap, register_t *retval)
541 {
542 /* {
543 syscallarg(clockid_t) clock_id;
544 syscallarg(struct sigevent *) evp;
545 syscallarg(timer_t *) timerid;
546 } */
547
548 return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
549 SCARG(uap, evp), copyin, l);
550 }
551
552 int
553 timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
554 copyin_t fetch_event, struct lwp *l)
555 {
556 int error;
557 timer_t timerid;
558 struct ptimer *pt;
559 struct proc *p;
560
561 p = l->l_proc;
562
563 if (id < CLOCK_REALTIME ||
564 id > CLOCK_PROF)
565 return (EINVAL);
566
567 if (p->p_timers == NULL)
568 timers_alloc(p);
569
570 /* Find a free timer slot, skipping those reserved for setitimer(). */
571 for (timerid = 3; timerid < TIMER_MAX; timerid++)
572 if (p->p_timers->pts_timers[timerid] == NULL)
573 break;
574
575 if (timerid == TIMER_MAX)
576 return EAGAIN;
577
578 pt = pool_get(&ptimer_pool, PR_WAITOK);
579 if (evp) {
580 if (((error =
581 (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
582 ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
583 (pt->pt_ev.sigev_notify > SIGEV_SA))) {
584 pool_put(&ptimer_pool, pt);
585 return (error ? error : EINVAL);
586 }
587 } else {
588 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
589 switch (id) {
590 case CLOCK_REALTIME:
591 pt->pt_ev.sigev_signo = SIGALRM;
592 break;
593 case CLOCK_VIRTUAL:
594 pt->pt_ev.sigev_signo = SIGVTALRM;
595 break;
596 case CLOCK_PROF:
597 pt->pt_ev.sigev_signo = SIGPROF;
598 break;
599 }
600 pt->pt_ev.sigev_value.sival_int = timerid;
601 }
602 pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
603 pt->pt_info.ksi_errno = 0;
604 pt->pt_info.ksi_code = 0;
605 pt->pt_info.ksi_pid = p->p_pid;
606 pt->pt_info.ksi_uid = kauth_cred_getuid(l->l_cred);
607 pt->pt_info.ksi_value = pt->pt_ev.sigev_value;
608
609 pt->pt_type = id;
610 pt->pt_proc = p;
611 pt->pt_overruns = 0;
612 pt->pt_poverruns = 0;
613 pt->pt_entry = timerid;
614 timerclear(&pt->pt_time.it_value);
615 if (id == CLOCK_REALTIME)
616 callout_init(&pt->pt_ch, 0);
617 else
618 pt->pt_active = 0;
619
620 p->p_timers->pts_timers[timerid] = pt;
621
622 return copyout(&timerid, tid, sizeof(timerid));
623 }
624
625 /* Delete a POSIX realtime timer */
626 int
627 sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap, register_t *retval)
628 {
629 /* {
630 syscallarg(timer_t) timerid;
631 } */
632 struct proc *p = l->l_proc;
633 timer_t timerid;
634 struct ptimer *pt, *ptn;
635 int s;
636
637 timerid = SCARG(uap, timerid);
638
639 if ((p->p_timers == NULL) ||
640 (timerid < 2) || (timerid >= TIMER_MAX) ||
641 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
642 return (EINVAL);
643
644 if (pt->pt_type == CLOCK_REALTIME) {
645 callout_stop(&pt->pt_ch);
646 callout_destroy(&pt->pt_ch);
647 } else if (pt->pt_active) {
648 s = splclock();
649 ptn = LIST_NEXT(pt, pt_list);
650 LIST_REMOVE(pt, pt_list);
651 for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
652 timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
653 &ptn->pt_time.it_value);
654 splx(s);
655 }
656
657 p->p_timers->pts_timers[timerid] = NULL;
658 pool_put(&ptimer_pool, pt);
659
660 return (0);
661 }
662
663 /*
664 * Set up the given timer. The value in pt->pt_time.it_value is taken
665 * to be an absolute time for CLOCK_REALTIME timers and a relative
666 * time for virtual timers.
667 * Must be called at splclock().
668 */
669 void
670 timer_settime(struct ptimer *pt)
671 {
672 struct ptimer *ptn, *pptn;
673 struct ptlist *ptl;
674
675 if (pt->pt_type == CLOCK_REALTIME) {
676 callout_stop(&pt->pt_ch);
677 if (timerisset(&pt->pt_time.it_value)) {
678 /*
679 * Don't need to check hzto() return value, here.
680 * callout_reset() does it for us.
681 */
682 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
683 realtimerexpire, pt);
684 }
685 } else {
686 if (pt->pt_active) {
687 ptn = LIST_NEXT(pt, pt_list);
688 LIST_REMOVE(pt, pt_list);
689 for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
690 timeradd(&pt->pt_time.it_value,
691 &ptn->pt_time.it_value,
692 &ptn->pt_time.it_value);
693 }
694 if (timerisset(&pt->pt_time.it_value)) {
695 if (pt->pt_type == CLOCK_VIRTUAL)
696 ptl = &pt->pt_proc->p_timers->pts_virtual;
697 else
698 ptl = &pt->pt_proc->p_timers->pts_prof;
699
700 for (ptn = LIST_FIRST(ptl), pptn = NULL;
701 ptn && timercmp(&pt->pt_time.it_value,
702 &ptn->pt_time.it_value, >);
703 pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
704 timersub(&pt->pt_time.it_value,
705 &ptn->pt_time.it_value,
706 &pt->pt_time.it_value);
707
708 if (pptn)
709 LIST_INSERT_AFTER(pptn, pt, pt_list);
710 else
711 LIST_INSERT_HEAD(ptl, pt, pt_list);
712
713 for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
714 timersub(&ptn->pt_time.it_value,
715 &pt->pt_time.it_value,
716 &ptn->pt_time.it_value);
717
718 pt->pt_active = 1;
719 } else
720 pt->pt_active = 0;
721 }
722 }
723
724 void
725 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
726 {
727 struct timeval now;
728 struct ptimer *ptn;
729
730 *aitv = pt->pt_time;
731 if (pt->pt_type == CLOCK_REALTIME) {
732 /*
733 * Convert from absolute to relative time in .it_value
734 * part of real time timer. If time for real time
735 * timer has passed return 0, else return difference
736 * between current time and time for the timer to go
737 * off.
738 */
739 if (timerisset(&aitv->it_value)) {
740 getmicrotime(&now);
741 if (timercmp(&aitv->it_value, &now, <))
742 timerclear(&aitv->it_value);
743 else
744 timersub(&aitv->it_value, &now,
745 &aitv->it_value);
746 }
747 } else if (pt->pt_active) {
748 if (pt->pt_type == CLOCK_VIRTUAL)
749 ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
750 else
751 ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
752 for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
753 timeradd(&aitv->it_value,
754 &ptn->pt_time.it_value, &aitv->it_value);
755 KASSERT(ptn != NULL); /* pt should be findable on the list */
756 } else
757 timerclear(&aitv->it_value);
758 }
759
760
761
762 /* Set and arm a POSIX realtime timer */
763 int
764 sys_timer_settime(struct lwp *l, const struct sys_timer_settime_args *uap, register_t *retval)
765 {
766 /* {
767 syscallarg(timer_t) timerid;
768 syscallarg(int) flags;
769 syscallarg(const struct itimerspec *) value;
770 syscallarg(struct itimerspec *) ovalue;
771 } */
772 int error;
773 struct itimerspec value, ovalue, *ovp = NULL;
774
775 if ((error = copyin(SCARG(uap, value), &value,
776 sizeof(struct itimerspec))) != 0)
777 return (error);
778
779 if (SCARG(uap, ovalue))
780 ovp = &ovalue;
781
782 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
783 SCARG(uap, flags), l->l_proc)) != 0)
784 return error;
785
786 if (ovp)
787 return copyout(&ovalue, SCARG(uap, ovalue),
788 sizeof(struct itimerspec));
789 return 0;
790 }
791
792 int
793 dotimer_settime(int timerid, struct itimerspec *value,
794 struct itimerspec *ovalue, int flags, struct proc *p)
795 {
796 struct timeval now;
797 struct itimerval val, oval;
798 struct ptimer *pt;
799 int s;
800
801 if ((p->p_timers == NULL) ||
802 (timerid < 2) || (timerid >= TIMER_MAX) ||
803 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
804 return (EINVAL);
805
806 TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value);
807 TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval);
808 if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
809 return (EINVAL);
810
811 oval = pt->pt_time;
812 pt->pt_time = val;
813
814 s = splclock();
815 /*
816 * If we've been passed a relative time for a realtime timer,
817 * convert it to absolute; if an absolute time for a virtual
818 * timer, convert it to relative and make sure we don't set it
819 * to zero, which would cancel the timer, or let it go
820 * negative, which would confuse the comparison tests.
821 */
822 if (timerisset(&pt->pt_time.it_value)) {
823 if (pt->pt_type == CLOCK_REALTIME) {
824 if ((flags & TIMER_ABSTIME) == 0) {
825 getmicrotime(&now);
826 timeradd(&pt->pt_time.it_value, &now,
827 &pt->pt_time.it_value);
828 }
829 } else {
830 if ((flags & TIMER_ABSTIME) != 0) {
831 getmicrotime(&now);
832 timersub(&pt->pt_time.it_value, &now,
833 &pt->pt_time.it_value);
834 if (!timerisset(&pt->pt_time.it_value) ||
835 pt->pt_time.it_value.tv_sec < 0) {
836 pt->pt_time.it_value.tv_sec = 0;
837 pt->pt_time.it_value.tv_usec = 1;
838 }
839 }
840 }
841 }
842
843 timer_settime(pt);
844 splx(s);
845
846 if (ovalue) {
847 TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue->it_value);
848 TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue->it_interval);
849 }
850
851 return (0);
852 }
853
854 /* Return the time remaining until a POSIX timer fires. */
855 int
856 sys_timer_gettime(struct lwp *l, const struct sys_timer_gettime_args *uap, register_t *retval)
857 {
858 /* {
859 syscallarg(timer_t) timerid;
860 syscallarg(struct itimerspec *) value;
861 } */
862 struct itimerspec its;
863 int error;
864
865 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
866 &its)) != 0)
867 return error;
868
869 return copyout(&its, SCARG(uap, value), sizeof(its));
870 }
871
872 int
873 dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
874 {
875 int s;
876 struct ptimer *pt;
877 struct itimerval aitv;
878
879 if ((p->p_timers == NULL) ||
880 (timerid < 2) || (timerid >= TIMER_MAX) ||
881 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
882 return (EINVAL);
883
884 s = splclock();
885 timer_gettime(pt, &aitv);
886 splx(s);
887
888 TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its->it_interval);
889 TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its->it_value);
890
891 return 0;
892 }
893
894 /*
895 * Return the count of the number of times a periodic timer expired
896 * while a notification was already pending. The counter is reset when
897 * a timer expires and a notification can be posted.
898 */
899 int
900 sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap, register_t *retval)
901 {
902 /* {
903 syscallarg(timer_t) timerid;
904 } */
905 struct proc *p = l->l_proc;
906 int timerid;
907 struct ptimer *pt;
908
909 timerid = SCARG(uap, timerid);
910
911 if ((p->p_timers == NULL) ||
912 (timerid < 2) || (timerid >= TIMER_MAX) ||
913 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
914 return (EINVAL);
915
916 *retval = pt->pt_poverruns;
917
918 return (0);
919 }
920
921 /*
922 * Real interval timer expired:
923 * send process whose timer expired an alarm signal.
924 * If time is not set up to reload, then just return.
925 * Else compute next time timer should go off which is > current time.
926 * This is where delay in processing this timeout causes multiple
927 * SIGALRM calls to be compressed into one.
928 */
929 void
930 realtimerexpire(void *arg)
931 {
932 struct timeval now;
933 struct ptimer *pt;
934 int s;
935
936 pt = (struct ptimer *)arg;
937
938 itimerfire(pt);
939
940 if (!timerisset(&pt->pt_time.it_interval)) {
941 timerclear(&pt->pt_time.it_value);
942 return;
943 }
944 for (;;) {
945 s = splclock(); /* XXX need spl now? */
946 timeradd(&pt->pt_time.it_value,
947 &pt->pt_time.it_interval, &pt->pt_time.it_value);
948 getmicrotime(&now);
949 if (timercmp(&pt->pt_time.it_value, &now, >)) {
950 /*
951 * Don't need to check hzto() return value, here.
952 * callout_reset() does it for us.
953 */
954 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
955 realtimerexpire, pt);
956 splx(s);
957 return;
958 }
959 splx(s);
960 pt->pt_overruns++;
961 }
962 }
963
964 /* BSD routine to get the value of an interval timer. */
965 /* ARGSUSED */
966 int
967 sys_getitimer(struct lwp *l, const struct sys_getitimer_args *uap, register_t *retval)
968 {
969 /* {
970 syscallarg(int) which;
971 syscallarg(struct itimerval *) itv;
972 } */
973 struct proc *p = l->l_proc;
974 struct itimerval aitv;
975 int error;
976
977 error = dogetitimer(p, SCARG(uap, which), &aitv);
978 if (error)
979 return error;
980 return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
981 }
982
983 int
984 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
985 {
986 int s;
987
988 if ((u_int)which > ITIMER_PROF)
989 return (EINVAL);
990
991 if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
992 timerclear(&itvp->it_value);
993 timerclear(&itvp->it_interval);
994 } else {
995 s = splclock();
996 timer_gettime(p->p_timers->pts_timers[which], itvp);
997 splx(s);
998 }
999
1000 return 0;
1001 }
1002
1003 /* BSD routine to set/arm an interval timer. */
1004 /* ARGSUSED */
1005 int
1006 sys_setitimer(struct lwp *l, const struct sys_setitimer_args *uap, register_t *retval)
1007 {
1008 /* {
1009 syscallarg(int) which;
1010 syscallarg(const struct itimerval *) itv;
1011 syscallarg(struct itimerval *) oitv;
1012 } */
1013 struct proc *p = l->l_proc;
1014 int which = SCARG(uap, which);
1015 struct sys_getitimer_args getargs;
1016 const struct itimerval *itvp;
1017 struct itimerval aitv;
1018 int error;
1019
1020 if ((u_int)which > ITIMER_PROF)
1021 return (EINVAL);
1022 itvp = SCARG(uap, itv);
1023 if (itvp &&
1024 (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
1025 return (error);
1026 if (SCARG(uap, oitv) != NULL) {
1027 SCARG(&getargs, which) = which;
1028 SCARG(&getargs, itv) = SCARG(uap, oitv);
1029 if ((error = sys_getitimer(l, &getargs, retval)) != 0)
1030 return (error);
1031 }
1032 if (itvp == 0)
1033 return (0);
1034
1035 return dosetitimer(p, which, &aitv);
1036 }
1037
1038 int
1039 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1040 {
1041 struct timeval now;
1042 struct ptimer *pt;
1043 int s;
1044
1045 if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1046 return (EINVAL);
1047
1048 /*
1049 * Don't bother allocating data structures if the process just
1050 * wants to clear the timer.
1051 */
1052 if (!timerisset(&itvp->it_value) &&
1053 ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
1054 return (0);
1055
1056 if (p->p_timers == NULL)
1057 timers_alloc(p);
1058 if (p->p_timers->pts_timers[which] == NULL) {
1059 pt = pool_get(&ptimer_pool, PR_WAITOK);
1060 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1061 pt->pt_ev.sigev_value.sival_int = which;
1062 pt->pt_overruns = 0;
1063 pt->pt_proc = p;
1064 pt->pt_type = which;
1065 pt->pt_entry = which;
1066 switch (which) {
1067 case ITIMER_REAL:
1068 callout_init(&pt->pt_ch, 0);
1069 pt->pt_ev.sigev_signo = SIGALRM;
1070 break;
1071 case ITIMER_VIRTUAL:
1072 pt->pt_active = 0;
1073 pt->pt_ev.sigev_signo = SIGVTALRM;
1074 break;
1075 case ITIMER_PROF:
1076 pt->pt_active = 0;
1077 pt->pt_ev.sigev_signo = SIGPROF;
1078 break;
1079 }
1080 } else
1081 pt = p->p_timers->pts_timers[which];
1082
1083 pt->pt_time = *itvp;
1084 p->p_timers->pts_timers[which] = pt;
1085
1086 s = splclock();
1087 if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
1088 /* Convert to absolute time */
1089 /* XXX need to wrap in splclock for timecounters case? */
1090 getmicrotime(&now);
1091 timeradd(&pt->pt_time.it_value, &now, &pt->pt_time.it_value);
1092 }
1093 timer_settime(pt);
1094 splx(s);
1095
1096 return (0);
1097 }
1098
1099 /* Utility routines to manage the array of pointers to timers. */
1100 void
1101 timers_alloc(struct proc *p)
1102 {
1103 int i;
1104 struct ptimers *pts;
1105
1106 pts = pool_get(&ptimers_pool, PR_WAITOK);
1107 LIST_INIT(&pts->pts_virtual);
1108 LIST_INIT(&pts->pts_prof);
1109 for (i = 0; i < TIMER_MAX; i++)
1110 pts->pts_timers[i] = NULL;
1111 pts->pts_fired = 0;
1112 p->p_timers = pts;
1113 }
1114
1115 /*
1116 * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1117 * then clean up all timers and free all the data structures. If
1118 * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1119 * by timer_create(), not the BSD setitimer() timers, and only free the
1120 * structure if none of those remain.
1121 */
1122 void
1123 timers_free(struct proc *p, int which)
1124 {
1125 int i, s;
1126 struct ptimers *pts;
1127 struct ptimer *pt, *ptn;
1128 struct timeval tv;
1129
1130 if (p->p_timers) {
1131 pts = p->p_timers;
1132 if (which == TIMERS_ALL)
1133 i = 0;
1134 else {
1135 s = splclock();
1136 timerclear(&tv);
1137 for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
1138 ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1139 ptn = LIST_NEXT(ptn, pt_list))
1140 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1141 LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
1142 if (ptn) {
1143 timeradd(&tv, &ptn->pt_time.it_value,
1144 &ptn->pt_time.it_value);
1145 LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
1146 ptn, pt_list);
1147 }
1148
1149 timerclear(&tv);
1150 for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
1151 ptn && ptn != pts->pts_timers[ITIMER_PROF];
1152 ptn = LIST_NEXT(ptn, pt_list))
1153 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1154 LIST_FIRST(&p->p_timers->pts_prof) = NULL;
1155 if (ptn) {
1156 timeradd(&tv, &ptn->pt_time.it_value,
1157 &ptn->pt_time.it_value);
1158 LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
1159 pt_list);
1160 }
1161 splx(s);
1162 i = 3;
1163 }
1164 for ( ; i < TIMER_MAX; i++)
1165 if ((pt = pts->pts_timers[i]) != NULL) {
1166 if (pt->pt_type == CLOCK_REALTIME) {
1167 callout_stop(&pt->pt_ch);
1168 callout_destroy(&pt->pt_ch);
1169 }
1170 pts->pts_timers[i] = NULL;
1171 pool_put(&ptimer_pool, pt);
1172 }
1173 if ((pts->pts_timers[0] == NULL) &&
1174 (pts->pts_timers[1] == NULL) &&
1175 (pts->pts_timers[2] == NULL)) {
1176 p->p_timers = NULL;
1177 pool_put(&ptimers_pool, pts);
1178 }
1179 }
1180 }
1181
1182 /*
1183 * Decrement an interval timer by a specified number
1184 * of microseconds, which must be less than a second,
1185 * i.e. < 1000000. If the timer expires, then reload
1186 * it. In this case, carry over (usec - old value) to
1187 * reduce the value reloaded into the timer so that
1188 * the timer does not drift. This routine assumes
1189 * that it is called in a context where the timers
1190 * on which it is operating cannot change in value.
1191 */
1192 int
1193 itimerdecr(struct ptimer *pt, int usec)
1194 {
1195 struct itimerval *itp;
1196
1197 itp = &pt->pt_time;
1198 if (itp->it_value.tv_usec < usec) {
1199 if (itp->it_value.tv_sec == 0) {
1200 /* expired, and already in next interval */
1201 usec -= itp->it_value.tv_usec;
1202 goto expire;
1203 }
1204 itp->it_value.tv_usec += 1000000;
1205 itp->it_value.tv_sec--;
1206 }
1207 itp->it_value.tv_usec -= usec;
1208 usec = 0;
1209 if (timerisset(&itp->it_value))
1210 return (1);
1211 /* expired, exactly at end of interval */
1212 expire:
1213 if (timerisset(&itp->it_interval)) {
1214 itp->it_value = itp->it_interval;
1215 itp->it_value.tv_usec -= usec;
1216 if (itp->it_value.tv_usec < 0) {
1217 itp->it_value.tv_usec += 1000000;
1218 itp->it_value.tv_sec--;
1219 }
1220 timer_settime(pt);
1221 } else
1222 itp->it_value.tv_usec = 0; /* sec is already 0 */
1223 return (0);
1224 }
1225
1226 void
1227 itimerfire(struct ptimer *pt)
1228 {
1229 struct proc *p = pt->pt_proc;
1230
1231 if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
1232 /*
1233 * No RT signal infrastructure exists at this time;
1234 * just post the signal number and throw away the
1235 * value.
1236 */
1237 if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo))
1238 pt->pt_overruns++;
1239 else {
1240 ksiginfo_t ksi;
1241 KSI_INIT(&ksi);
1242 ksi.ksi_signo = pt->pt_ev.sigev_signo;
1243 ksi.ksi_code = SI_TIMER;
1244 ksi.ksi_value = pt->pt_ev.sigev_value;
1245 pt->pt_poverruns = pt->pt_overruns;
1246 pt->pt_overruns = 0;
1247 mutex_enter(&proclist_mutex);
1248 kpsignal(p, &ksi, NULL);
1249 mutex_exit(&proclist_mutex);
1250 }
1251 }
1252 }
1253
1254 /*
1255 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
1256 * for usage and rationale.
1257 */
1258 int
1259 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1260 {
1261 struct timeval tv, delta;
1262 int rv = 0;
1263
1264 getmicrouptime(&tv);
1265 timersub(&tv, lasttime, &delta);
1266
1267 /*
1268 * check for 0,0 is so that the message will be seen at least once,
1269 * even if interval is huge.
1270 */
1271 if (timercmp(&delta, mininterval, >=) ||
1272 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1273 *lasttime = tv;
1274 rv = 1;
1275 }
1276
1277 return (rv);
1278 }
1279
1280 /*
1281 * ppsratecheck(): packets (or events) per second limitation.
1282 */
1283 int
1284 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1285 {
1286 struct timeval tv, delta;
1287 int rv;
1288
1289 getmicrouptime(&tv);
1290 timersub(&tv, lasttime, &delta);
1291
1292 /*
1293 * check for 0,0 is so that the message will be seen at least once.
1294 * if more than one second have passed since the last update of
1295 * lasttime, reset the counter.
1296 *
1297 * we do increment *curpps even in *curpps < maxpps case, as some may
1298 * try to use *curpps for stat purposes as well.
1299 */
1300 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1301 delta.tv_sec >= 1) {
1302 *lasttime = tv;
1303 *curpps = 0;
1304 }
1305 if (maxpps < 0)
1306 rv = 1;
1307 else if (*curpps < maxpps)
1308 rv = 1;
1309 else
1310 rv = 0;
1311
1312 #if 1 /*DIAGNOSTIC?*/
1313 /* be careful about wrap-around */
1314 if (*curpps + 1 > *curpps)
1315 *curpps = *curpps + 1;
1316 #else
1317 /*
1318 * assume that there's not too many calls to this function.
1319 * not sure if the assumption holds, as it depends on *caller's*
1320 * behavior, not the behavior of this function.
1321 * IMHO it is wrong to make assumption on the caller's behavior,
1322 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1323 */
1324 *curpps = *curpps + 1;
1325 #endif
1326
1327 return (rv);
1328 }
1329