kern_time.c revision 1.141.2.1 1 /* $NetBSD: kern_time.c,v 1.141.2.1 2008/03/29 20:47:00 christos 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.141.2.1 2008/03/29 20:47:00 christos 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, const struct timespec *ts, bool check_kauth)
120 {
121 struct timespec delta, now;
122 struct bintime btdelta;
123 lwp_t *l;
124 int s;
125
126 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
127 s = splclock();
128 nanotime(&now);
129 timespecsub(ts, &now, &delta);
130
131 if (check_kauth && kauth_authorize_system(kauth_cred_get(),
132 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, __UNCONST(ts),
133 &delta, KAUTH_ARG(check_kauth ? false : true)) != 0) {
134 splx(s);
135 return (EPERM);
136 }
137
138 #ifdef notyet
139 if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
140 splx(s);
141 return (EPERM);
142 }
143 #endif
144
145 tc_setclock(ts);
146
147 timespecadd(&boottime, &delta, &boottime);
148
149 /*
150 * XXXSMP: There is a short race between setting the time above
151 * and adjusting LWP's run times. Fixing this properly means
152 * pausing all CPUs while we adjust the clock.
153 */
154 timespec2bintime(&delta, &btdelta);
155 mutex_enter(&proclist_lock);
156 LIST_FOREACH(l, &alllwp, l_list) {
157 lwp_lock(l);
158 bintime_add(&l->l_stime, &btdelta);
159 lwp_unlock(l);
160 }
161 mutex_exit(&proclist_lock);
162 resettodr();
163 splx(s);
164
165 return (0);
166 }
167
168 int
169 settime(struct proc *p, struct timespec *ts)
170 {
171 return (settime1(p, ts, true));
172 }
173
174 /* ARGSUSED */
175 int
176 sys___clock_gettime50(struct lwp *l,
177 const struct sys___clock_gettime50_args *uap, register_t *retval)
178 {
179 /* {
180 syscallarg(clockid_t) clock_id;
181 syscallarg(struct timespec *) tp;
182 } */
183 clockid_t clock_id;
184 struct timespec ats;
185
186 clock_id = SCARG(uap, clock_id);
187 switch (clock_id) {
188 case CLOCK_REALTIME:
189 nanotime(&ats);
190 break;
191 case CLOCK_MONOTONIC:
192 nanouptime(&ats);
193 break;
194 default:
195 return (EINVAL);
196 }
197
198 return copyout(&ats, SCARG(uap, tp), sizeof(ats));
199 }
200
201 /* ARGSUSED */
202 int
203 sys___clock_settime50(struct lwp *l,
204 const struct sys___clock_settime50_args *uap, register_t *retval)
205 {
206 /* {
207 syscallarg(clockid_t) clock_id;
208 syscallarg(const struct timespec *) tp;
209 } */
210 int error;
211 struct timespec ats;
212
213 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
214 return error;
215
216 return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
217 }
218
219
220 int
221 clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
222 bool check_kauth)
223 {
224 int error;
225
226 switch (clock_id) {
227 case CLOCK_REALTIME:
228 if ((error = settime1(p, tp, check_kauth)) != 0)
229 return (error);
230 break;
231 case CLOCK_MONOTONIC:
232 return (EINVAL); /* read-only clock */
233 default:
234 return (EINVAL);
235 }
236
237 return 0;
238 }
239
240 int
241 sys___clock_getres50(struct lwp *l, const struct sys___clock_getres50_args *uap,
242 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___nanosleep50(struct lwp *l, const struct sys___nanosleep50_args *uap,
275 register_t *retval)
276 {
277 /* {
278 syscallarg(struct timespec *) rqtp;
279 syscallarg(struct timespec *) rmtp;
280 } */
281 struct timespec rmt, rqt;
282 int error, error1;
283
284 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
285 if (error)
286 return (error);
287
288 error = nanosleep1(l, &rqt, SCARG(uap, rmtp) ? &rmt : NULL);
289 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
290 return error;
291
292 error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
293 return error1 ? error1 : error;
294 }
295
296 int
297 nanosleep1(struct lwp *l, struct timespec *rqt, struct timespec *rmt)
298 {
299 struct timespec rmtstart;
300 int error, timo;
301
302 if (itimespecfix(rqt))
303 return (EINVAL);
304
305 timo = tstohz(rqt);
306 /*
307 * Avoid inadvertantly sleeping forever
308 */
309 if (timo == 0)
310 timo = 1;
311 getnanouptime(&rmtstart);
312 again:
313 error = kpause("nanoslp", true, timo, NULL);
314 if (rmt != NULL || error == 0) {
315 struct timespec rmtend;
316 struct timespec t0;
317 struct timespec *t;
318
319 getnanouptime(&rmtend);
320 t = (rmt != NULL) ? rmt : &t0;
321 timespecsub(&rmtend, &rmtstart, t);
322 timespecsub(rqt, t, t);
323 if (t->tv_sec < 0)
324 timespecclear(t);
325 if (error == 0) {
326 timo = tstohz(t);
327 if (timo > 0)
328 goto again;
329 }
330 }
331
332 if (error == ERESTART)
333 error = EINTR;
334 if (error == EWOULDBLOCK)
335 error = 0;
336
337 return error;
338 }
339
340 /* ARGSUSED */
341 int
342 sys___gettimeofday50(struct lwp *l, const struct sys___gettimeofday50_args *uap,
343 register_t *retval)
344 {
345 /* {
346 syscallarg(struct timeval *) tp;
347 syscallarg(void *) tzp; really "struct timezone *";
348 } */
349 struct timeval atv;
350 int error = 0;
351 struct timezone tzfake;
352
353 if (SCARG(uap, tp)) {
354 microtime(&atv);
355 error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
356 if (error)
357 return (error);
358 }
359 if (SCARG(uap, tzp)) {
360 /*
361 * NetBSD has no kernel notion of time zone, so we just
362 * fake up a timezone struct and return it if demanded.
363 */
364 tzfake.tz_minuteswest = 0;
365 tzfake.tz_dsttime = 0;
366 error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
367 }
368 return (error);
369 }
370
371 /* ARGSUSED */
372 int
373 sys___settimeofday50(struct lwp *l, const struct sys___settimeofday50_args *uap,
374 register_t *retval)
375 {
376 /* {
377 syscallarg(const struct timeval *) tv;
378 syscallarg(const void *) tzp; really "const struct timezone *";
379 } */
380
381 return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
382 }
383
384 int
385 settimeofday1(const struct timeval *utv, bool userspace,
386 const void *utzp, struct lwp *l, bool check_kauth)
387 {
388 struct timeval atv;
389 struct timespec ts;
390 int error;
391
392 /* Verify all parameters before changing time. */
393
394 /*
395 * NetBSD has no kernel notion of time zone, and only an
396 * obsolete program would try to set it, so we log a warning.
397 */
398 if (utzp)
399 log(LOG_WARNING, "pid %d attempted to set the "
400 "(obsolete) kernel time zone\n", l->l_proc->p_pid);
401
402 if (utv == NULL)
403 return 0;
404
405 if (userspace) {
406 if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
407 return error;
408 utv = &atv;
409 }
410
411 TIMEVAL_TO_TIMESPEC(utv, &ts);
412 return settime1(l->l_proc, &ts, check_kauth);
413 }
414
415 int time_adjusted; /* set if an adjustment is made */
416
417 /* ARGSUSED */
418 int
419 sys___adjtime50(struct lwp *l, const struct sys___adjtime50_args *uap,
420 register_t *retval)
421 {
422 /* {
423 syscallarg(const struct timeval *) delta;
424 syscallarg(struct timeval *) olddelta;
425 } */
426 int error = 0;
427 struct timeval atv, oldatv;
428
429 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
430 KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
431 return error;
432
433 if (SCARG(uap, delta)) {
434 error = copyin(SCARG(uap, delta), &atv,
435 sizeof(*SCARG(uap, delta)));
436 if (error)
437 return (error);
438 }
439 adjtime1(SCARG(uap, delta) ? &atv : NULL,
440 SCARG(uap, olddelta) ? &oldatv : NULL, l->l_proc);
441 if (SCARG(uap, olddelta))
442 error = copyout(&oldatv, SCARG(uap, olddelta),
443 sizeof(*SCARG(uap, olddelta)));
444 return error;
445 }
446
447 void
448 adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
449 {
450 struct timeval atv;
451
452 extern int64_t time_adjtime; /* in kern_ntptime.c */
453
454 if (olddelta) {
455 olddelta->tv_sec = time_adjtime / 1000000;
456 olddelta->tv_usec = time_adjtime % 1000000;
457 if (olddelta->tv_usec < 0) {
458 olddelta->tv_usec += 1000000;
459 olddelta->tv_sec--;
460 }
461 }
462
463 if (delta) {
464 time_adjtime = delta->tv_sec * 1000000 + atv.tv_usec;
465
466 if (time_adjtime)
467 /* We need to save the system time during shutdown */
468 time_adjusted |= 1;
469 }
470 }
471
472 /*
473 * Interval timer support. Both the BSD getitimer() family and the POSIX
474 * timer_*() family of routines are supported.
475 *
476 * All timers are kept in an array pointed to by p_timers, which is
477 * allocated on demand - many processes don't use timers at all. The
478 * first three elements in this array are reserved for the BSD timers:
479 * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
480 * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
481 * syscall.
482 *
483 * Realtime timers are kept in the ptimer structure as an absolute
484 * time; virtual time timers are kept as a linked list of deltas.
485 * Virtual time timers are processed in the hardclock() routine of
486 * kern_clock.c. The real time timer is processed by a callout
487 * routine, called from the softclock() routine. Since a callout may
488 * be delayed in real time due to interrupt processing in the system,
489 * it is possible for the real time timeout routine (realtimeexpire,
490 * given below), to be delayed in real time past when it is supposed
491 * to occur. It does not suffice, therefore, to reload the real timer
492 * .it_value from the real time timers .it_interval. Rather, we
493 * compute the next time in absolute time the timer should go off. */
494
495 /* Allocate a POSIX realtime timer. */
496 int
497 sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap,
498 register_t *retval)
499 {
500 /* {
501 syscallarg(clockid_t) clock_id;
502 syscallarg(struct sigevent *) evp;
503 syscallarg(timer_t *) timerid;
504 } */
505
506 return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
507 SCARG(uap, evp), copyin, l);
508 }
509
510 int
511 timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
512 copyin_t fetch_event, struct lwp *l)
513 {
514 int error;
515 timer_t timerid;
516 struct ptimer *pt;
517 struct proc *p;
518
519 p = l->l_proc;
520
521 if (id < CLOCK_REALTIME ||
522 id > CLOCK_PROF)
523 return (EINVAL);
524
525 if (p->p_timers == NULL)
526 timers_alloc(p);
527
528 /* Find a free timer slot, skipping those reserved for setitimer(). */
529 for (timerid = 3; timerid < TIMER_MAX; timerid++)
530 if (p->p_timers->pts_timers[timerid] == NULL)
531 break;
532
533 if (timerid == TIMER_MAX)
534 return EAGAIN;
535
536 pt = pool_get(&ptimer_pool, PR_WAITOK);
537 if (evp) {
538 if (((error =
539 (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
540 ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
541 (pt->pt_ev.sigev_notify > SIGEV_SA))) {
542 pool_put(&ptimer_pool, pt);
543 return (error ? error : EINVAL);
544 }
545 } else {
546 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
547 switch (id) {
548 case CLOCK_REALTIME:
549 pt->pt_ev.sigev_signo = SIGALRM;
550 break;
551 case CLOCK_VIRTUAL:
552 pt->pt_ev.sigev_signo = SIGVTALRM;
553 break;
554 case CLOCK_PROF:
555 pt->pt_ev.sigev_signo = SIGPROF;
556 break;
557 }
558 pt->pt_ev.sigev_value.sival_int = timerid;
559 }
560 pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
561 pt->pt_info.ksi_errno = 0;
562 pt->pt_info.ksi_code = 0;
563 pt->pt_info.ksi_pid = p->p_pid;
564 pt->pt_info.ksi_uid = kauth_cred_getuid(l->l_cred);
565 pt->pt_info.ksi_value = pt->pt_ev.sigev_value;
566
567 pt->pt_type = id;
568 pt->pt_proc = p;
569 pt->pt_overruns = 0;
570 pt->pt_poverruns = 0;
571 pt->pt_entry = timerid;
572 timerclear(&pt->pt_time.it_value);
573 if (id == CLOCK_REALTIME)
574 callout_init(&pt->pt_ch, 0);
575 else
576 pt->pt_active = 0;
577
578 p->p_timers->pts_timers[timerid] = pt;
579
580 return copyout(&timerid, tid, sizeof(timerid));
581 }
582
583 /* Delete a POSIX realtime timer */
584 int
585 sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap,
586 register_t *retval)
587 {
588 /* {
589 syscallarg(timer_t) timerid;
590 } */
591 struct proc *p = l->l_proc;
592 timer_t timerid;
593 struct ptimer *pt, *ptn;
594 int s;
595
596 timerid = SCARG(uap, timerid);
597
598 if ((p->p_timers == NULL) ||
599 (timerid < 2) || (timerid >= TIMER_MAX) ||
600 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
601 return (EINVAL);
602
603 if (pt->pt_type == CLOCK_REALTIME) {
604 callout_stop(&pt->pt_ch);
605 callout_destroy(&pt->pt_ch);
606 } else if (pt->pt_active) {
607 s = splclock();
608 ptn = LIST_NEXT(pt, pt_list);
609 LIST_REMOVE(pt, pt_list);
610 for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
611 timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
612 &ptn->pt_time.it_value);
613 splx(s);
614 }
615
616 p->p_timers->pts_timers[timerid] = NULL;
617 pool_put(&ptimer_pool, pt);
618
619 return (0);
620 }
621
622 /*
623 * Set up the given timer. The value in pt->pt_time.it_value is taken
624 * to be an absolute time for CLOCK_REALTIME timers and a relative
625 * time for virtual timers.
626 * Must be called at splclock().
627 */
628 void
629 timer_settime(struct ptimer *pt)
630 {
631 struct ptimer *ptn, *pptn;
632 struct ptlist *ptl;
633
634 if (pt->pt_type == CLOCK_REALTIME) {
635 callout_stop(&pt->pt_ch);
636 if (timerisset(&pt->pt_time.it_value)) {
637 /*
638 * Don't need to check hzto() return value, here.
639 * callout_reset() does it for us.
640 */
641 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
642 realtimerexpire, pt);
643 }
644 } else {
645 if (pt->pt_active) {
646 ptn = LIST_NEXT(pt, pt_list);
647 LIST_REMOVE(pt, pt_list);
648 for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
649 timeradd(&pt->pt_time.it_value,
650 &ptn->pt_time.it_value,
651 &ptn->pt_time.it_value);
652 }
653 if (timerisset(&pt->pt_time.it_value)) {
654 if (pt->pt_type == CLOCK_VIRTUAL)
655 ptl = &pt->pt_proc->p_timers->pts_virtual;
656 else
657 ptl = &pt->pt_proc->p_timers->pts_prof;
658
659 for (ptn = LIST_FIRST(ptl), pptn = NULL;
660 ptn && timercmp(&pt->pt_time.it_value,
661 &ptn->pt_time.it_value, >);
662 pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
663 timersub(&pt->pt_time.it_value,
664 &ptn->pt_time.it_value,
665 &pt->pt_time.it_value);
666
667 if (pptn)
668 LIST_INSERT_AFTER(pptn, pt, pt_list);
669 else
670 LIST_INSERT_HEAD(ptl, pt, pt_list);
671
672 for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
673 timersub(&ptn->pt_time.it_value,
674 &pt->pt_time.it_value,
675 &ptn->pt_time.it_value);
676
677 pt->pt_active = 1;
678 } else
679 pt->pt_active = 0;
680 }
681 }
682
683 void
684 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
685 {
686 struct timeval now;
687 struct ptimer *ptn;
688
689 *aitv = pt->pt_time;
690 if (pt->pt_type == CLOCK_REALTIME) {
691 /*
692 * Convert from absolute to relative time in .it_value
693 * part of real time timer. If time for real time
694 * timer has passed return 0, else return difference
695 * between current time and time for the timer to go
696 * off.
697 */
698 if (timerisset(&aitv->it_value)) {
699 getmicrotime(&now);
700 if (timercmp(&aitv->it_value, &now, <))
701 timerclear(&aitv->it_value);
702 else
703 timersub(&aitv->it_value, &now,
704 &aitv->it_value);
705 }
706 } else if (pt->pt_active) {
707 if (pt->pt_type == CLOCK_VIRTUAL)
708 ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
709 else
710 ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
711 for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
712 timeradd(&aitv->it_value,
713 &ptn->pt_time.it_value, &aitv->it_value);
714 KASSERT(ptn != NULL); /* pt should be findable on the list */
715 } else
716 timerclear(&aitv->it_value);
717 }
718
719
720
721 /* Set and arm a POSIX realtime timer */
722 int
723 sys___timer_settime50(struct lwp *l,
724 const struct sys___timer_settime50_args *uap,
725 register_t *retval)
726 {
727 /* {
728 syscallarg(timer_t) timerid;
729 syscallarg(int) flags;
730 syscallarg(const struct itimerspec *) value;
731 syscallarg(struct itimerspec *) ovalue;
732 } */
733 int error;
734 struct itimerspec value, ovalue, *ovp = NULL;
735
736 if ((error = copyin(SCARG(uap, value), &value,
737 sizeof(struct itimerspec))) != 0)
738 return (error);
739
740 if (SCARG(uap, ovalue))
741 ovp = &ovalue;
742
743 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
744 SCARG(uap, flags), l->l_proc)) != 0)
745 return error;
746
747 if (ovp)
748 return copyout(&ovalue, SCARG(uap, ovalue),
749 sizeof(struct itimerspec));
750 return 0;
751 }
752
753 int
754 dotimer_settime(int timerid, struct itimerspec *value,
755 struct itimerspec *ovalue, int flags, struct proc *p)
756 {
757 struct timeval now;
758 struct itimerval val, oval;
759 struct ptimer *pt;
760 int s;
761
762 if ((p->p_timers == NULL) ||
763 (timerid < 2) || (timerid >= TIMER_MAX) ||
764 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
765 return (EINVAL);
766
767 TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value);
768 TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval);
769 if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
770 return (EINVAL);
771
772 oval = pt->pt_time;
773 pt->pt_time = val;
774
775 s = splclock();
776 /*
777 * If we've been passed a relative time for a realtime timer,
778 * convert it to absolute; if an absolute time for a virtual
779 * timer, convert it to relative and make sure we don't set it
780 * to zero, which would cancel the timer, or let it go
781 * negative, which would confuse the comparison tests.
782 */
783 if (timerisset(&pt->pt_time.it_value)) {
784 if (pt->pt_type == CLOCK_REALTIME) {
785 if ((flags & TIMER_ABSTIME) == 0) {
786 getmicrotime(&now);
787 timeradd(&pt->pt_time.it_value, &now,
788 &pt->pt_time.it_value);
789 }
790 } else {
791 if ((flags & TIMER_ABSTIME) != 0) {
792 getmicrotime(&now);
793 timersub(&pt->pt_time.it_value, &now,
794 &pt->pt_time.it_value);
795 if (!timerisset(&pt->pt_time.it_value) ||
796 pt->pt_time.it_value.tv_sec < 0) {
797 pt->pt_time.it_value.tv_sec = 0;
798 pt->pt_time.it_value.tv_usec = 1;
799 }
800 }
801 }
802 }
803
804 timer_settime(pt);
805 splx(s);
806
807 if (ovalue) {
808 TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue->it_value);
809 TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue->it_interval);
810 }
811
812 return (0);
813 }
814
815 /* Return the time remaining until a POSIX timer fires. */
816 int
817 sys___timer_gettime50(struct lwp *l,
818 const struct sys___timer_gettime50_args *uap, register_t *retval)
819 {
820 /* {
821 syscallarg(timer_t) timerid;
822 syscallarg(struct itimerspec *) value;
823 } */
824 struct itimerspec its;
825 int error;
826
827 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
828 &its)) != 0)
829 return error;
830
831 return copyout(&its, SCARG(uap, value), sizeof(its));
832 }
833
834 int
835 dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
836 {
837 int s;
838 struct ptimer *pt;
839 struct itimerval aitv;
840
841 if ((p->p_timers == NULL) ||
842 (timerid < 2) || (timerid >= TIMER_MAX) ||
843 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
844 return (EINVAL);
845
846 s = splclock();
847 timer_gettime(pt, &aitv);
848 splx(s);
849
850 TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its->it_interval);
851 TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its->it_value);
852
853 return 0;
854 }
855
856 /*
857 * Return the count of the number of times a periodic timer expired
858 * while a notification was already pending. The counter is reset when
859 * a timer expires and a notification can be posted.
860 */
861 int
862 sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap,
863 register_t *retval)
864 {
865 /* {
866 syscallarg(timer_t) timerid;
867 } */
868 struct proc *p = l->l_proc;
869 int timerid;
870 struct ptimer *pt;
871
872 timerid = SCARG(uap, timerid);
873
874 if ((p->p_timers == NULL) ||
875 (timerid < 2) || (timerid >= TIMER_MAX) ||
876 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
877 return (EINVAL);
878
879 *retval = pt->pt_poverruns;
880
881 return (0);
882 }
883
884 /*
885 * Real interval timer expired:
886 * send process whose timer expired an alarm signal.
887 * If time is not set up to reload, then just return.
888 * Else compute next time timer should go off which is > current time.
889 * This is where delay in processing this timeout causes multiple
890 * SIGALRM calls to be compressed into one.
891 */
892 void
893 realtimerexpire(void *arg)
894 {
895 struct timeval now;
896 struct ptimer *pt;
897 int s;
898
899 pt = (struct ptimer *)arg;
900
901 itimerfire(pt);
902
903 if (!timerisset(&pt->pt_time.it_interval)) {
904 timerclear(&pt->pt_time.it_value);
905 return;
906 }
907 for (;;) {
908 s = splclock(); /* XXX need spl now? */
909 timeradd(&pt->pt_time.it_value,
910 &pt->pt_time.it_interval, &pt->pt_time.it_value);
911 getmicrotime(&now);
912 if (timercmp(&pt->pt_time.it_value, &now, >)) {
913 /*
914 * Don't need to check hzto() return value, here.
915 * callout_reset() does it for us.
916 */
917 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
918 realtimerexpire, pt);
919 splx(s);
920 return;
921 }
922 splx(s);
923 pt->pt_overruns++;
924 }
925 }
926
927 /* BSD routine to get the value of an interval timer. */
928 /* ARGSUSED */
929 int
930 sys___getitimer50(struct lwp *l, const struct sys___getitimer50_args *uap,
931 register_t *retval)
932 {
933 /* {
934 syscallarg(int) which;
935 syscallarg(struct itimerval *) itv;
936 } */
937 struct proc *p = l->l_proc;
938 struct itimerval aitv;
939 int error;
940
941 error = dogetitimer(p, SCARG(uap, which), &aitv);
942 if (error)
943 return error;
944 return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
945 }
946
947 int
948 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
949 {
950 int s;
951
952 if ((u_int)which > ITIMER_PROF)
953 return (EINVAL);
954
955 if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
956 timerclear(&itvp->it_value);
957 timerclear(&itvp->it_interval);
958 } else {
959 s = splclock();
960 timer_gettime(p->p_timers->pts_timers[which], itvp);
961 splx(s);
962 }
963
964 return 0;
965 }
966
967 /* BSD routine to set/arm an interval timer. */
968 /* ARGSUSED */
969 int
970 sys___setitimer50(struct lwp *l, const struct sys___setitimer50_args *uap,
971 register_t *retval)
972 {
973 /* {
974 syscallarg(int) which;
975 syscallarg(const struct itimerval *) itv;
976 syscallarg(struct itimerval *) oitv;
977 } */
978 struct proc *p = l->l_proc;
979 int which = SCARG(uap, which);
980 struct sys___getitimer50_args getargs;
981 const struct itimerval *itvp;
982 struct itimerval aitv;
983 int error;
984
985 if ((u_int)which > ITIMER_PROF)
986 return (EINVAL);
987 itvp = SCARG(uap, itv);
988 if (itvp &&
989 (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
990 return (error);
991 if (SCARG(uap, oitv) != NULL) {
992 SCARG(&getargs, which) = which;
993 SCARG(&getargs, itv) = SCARG(uap, oitv);
994 if ((error = sys___getitimer50(l, &getargs, retval)) != 0)
995 return (error);
996 }
997 if (itvp == 0)
998 return (0);
999
1000 return dosetitimer(p, which, &aitv);
1001 }
1002
1003 int
1004 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1005 {
1006 struct timeval now;
1007 struct ptimer *pt;
1008 int s;
1009
1010 if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1011 return (EINVAL);
1012
1013 /*
1014 * Don't bother allocating data structures if the process just
1015 * wants to clear the timer.
1016 */
1017 if (!timerisset(&itvp->it_value) &&
1018 ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
1019 return (0);
1020
1021 if (p->p_timers == NULL)
1022 timers_alloc(p);
1023 if (p->p_timers->pts_timers[which] == NULL) {
1024 pt = pool_get(&ptimer_pool, PR_WAITOK);
1025 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1026 pt->pt_ev.sigev_value.sival_int = which;
1027 pt->pt_overruns = 0;
1028 pt->pt_proc = p;
1029 pt->pt_type = which;
1030 pt->pt_entry = which;
1031 switch (which) {
1032 case ITIMER_REAL:
1033 callout_init(&pt->pt_ch, 0);
1034 pt->pt_ev.sigev_signo = SIGALRM;
1035 break;
1036 case ITIMER_VIRTUAL:
1037 pt->pt_active = 0;
1038 pt->pt_ev.sigev_signo = SIGVTALRM;
1039 break;
1040 case ITIMER_PROF:
1041 pt->pt_active = 0;
1042 pt->pt_ev.sigev_signo = SIGPROF;
1043 break;
1044 }
1045 } else
1046 pt = p->p_timers->pts_timers[which];
1047
1048 pt->pt_time = *itvp;
1049 p->p_timers->pts_timers[which] = pt;
1050
1051 s = splclock();
1052 if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
1053 /* Convert to absolute time */
1054 /* XXX need to wrap in splclock for timecounters case? */
1055 getmicrotime(&now);
1056 timeradd(&pt->pt_time.it_value, &now, &pt->pt_time.it_value);
1057 }
1058 timer_settime(pt);
1059 splx(s);
1060
1061 return (0);
1062 }
1063
1064 /* Utility routines to manage the array of pointers to timers. */
1065 void
1066 timers_alloc(struct proc *p)
1067 {
1068 int i;
1069 struct ptimers *pts;
1070
1071 pts = pool_get(&ptimers_pool, PR_WAITOK);
1072 LIST_INIT(&pts->pts_virtual);
1073 LIST_INIT(&pts->pts_prof);
1074 for (i = 0; i < TIMER_MAX; i++)
1075 pts->pts_timers[i] = NULL;
1076 pts->pts_fired = 0;
1077 p->p_timers = pts;
1078 }
1079
1080 /*
1081 * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1082 * then clean up all timers and free all the data structures. If
1083 * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1084 * by timer_create(), not the BSD setitimer() timers, and only free the
1085 * structure if none of those remain.
1086 */
1087 void
1088 timers_free(struct proc *p, int which)
1089 {
1090 int i, s;
1091 struct ptimers *pts;
1092 struct ptimer *pt, *ptn;
1093 struct timeval tv;
1094
1095 if (p->p_timers) {
1096 pts = p->p_timers;
1097 if (which == TIMERS_ALL)
1098 i = 0;
1099 else {
1100 s = splclock();
1101 timerclear(&tv);
1102 for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
1103 ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1104 ptn = LIST_NEXT(ptn, pt_list))
1105 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1106 LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
1107 if (ptn) {
1108 timeradd(&tv, &ptn->pt_time.it_value,
1109 &ptn->pt_time.it_value);
1110 LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
1111 ptn, pt_list);
1112 }
1113
1114 timerclear(&tv);
1115 for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
1116 ptn && ptn != pts->pts_timers[ITIMER_PROF];
1117 ptn = LIST_NEXT(ptn, pt_list))
1118 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1119 LIST_FIRST(&p->p_timers->pts_prof) = NULL;
1120 if (ptn) {
1121 timeradd(&tv, &ptn->pt_time.it_value,
1122 &ptn->pt_time.it_value);
1123 LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
1124 pt_list);
1125 }
1126 splx(s);
1127 i = 3;
1128 }
1129 for ( ; i < TIMER_MAX; i++)
1130 if ((pt = pts->pts_timers[i]) != NULL) {
1131 if (pt->pt_type == CLOCK_REALTIME) {
1132 callout_stop(&pt->pt_ch);
1133 callout_destroy(&pt->pt_ch);
1134 }
1135 pts->pts_timers[i] = NULL;
1136 pool_put(&ptimer_pool, pt);
1137 }
1138 if ((pts->pts_timers[0] == NULL) &&
1139 (pts->pts_timers[1] == NULL) &&
1140 (pts->pts_timers[2] == NULL)) {
1141 p->p_timers = NULL;
1142 pool_put(&ptimers_pool, pts);
1143 }
1144 }
1145 }
1146
1147 /*
1148 * Decrement an interval timer by a specified number
1149 * of microseconds, which must be less than a second,
1150 * i.e. < 1000000. If the timer expires, then reload
1151 * it. In this case, carry over (usec - old value) to
1152 * reduce the value reloaded into the timer so that
1153 * the timer does not drift. This routine assumes
1154 * that it is called in a context where the timers
1155 * on which it is operating cannot change in value.
1156 */
1157 int
1158 itimerdecr(struct ptimer *pt, int usec)
1159 {
1160 struct itimerval *itp;
1161
1162 itp = &pt->pt_time;
1163 if (itp->it_value.tv_usec < usec) {
1164 if (itp->it_value.tv_sec == 0) {
1165 /* expired, and already in next interval */
1166 usec -= itp->it_value.tv_usec;
1167 goto expire;
1168 }
1169 itp->it_value.tv_usec += 1000000;
1170 itp->it_value.tv_sec--;
1171 }
1172 itp->it_value.tv_usec -= usec;
1173 usec = 0;
1174 if (timerisset(&itp->it_value))
1175 return (1);
1176 /* expired, exactly at end of interval */
1177 expire:
1178 if (timerisset(&itp->it_interval)) {
1179 itp->it_value = itp->it_interval;
1180 itp->it_value.tv_usec -= usec;
1181 if (itp->it_value.tv_usec < 0) {
1182 itp->it_value.tv_usec += 1000000;
1183 itp->it_value.tv_sec--;
1184 }
1185 timer_settime(pt);
1186 } else
1187 itp->it_value.tv_usec = 0; /* sec is already 0 */
1188 return (0);
1189 }
1190
1191 void
1192 itimerfire(struct ptimer *pt)
1193 {
1194 struct proc *p = pt->pt_proc;
1195
1196 if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
1197 /*
1198 * No RT signal infrastructure exists at this time;
1199 * just post the signal number and throw away the
1200 * value.
1201 */
1202 if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo))
1203 pt->pt_overruns++;
1204 else {
1205 ksiginfo_t ksi;
1206 KSI_INIT(&ksi);
1207 ksi.ksi_signo = pt->pt_ev.sigev_signo;
1208 ksi.ksi_code = SI_TIMER;
1209 ksi.ksi_value = pt->pt_ev.sigev_value;
1210 pt->pt_poverruns = pt->pt_overruns;
1211 pt->pt_overruns = 0;
1212 mutex_enter(&proclist_mutex);
1213 kpsignal(p, &ksi, NULL);
1214 mutex_exit(&proclist_mutex);
1215 }
1216 }
1217 }
1218
1219 /*
1220 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
1221 * for usage and rationale.
1222 */
1223 int
1224 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1225 {
1226 struct timeval tv, delta;
1227 int rv = 0;
1228
1229 getmicrouptime(&tv);
1230 timersub(&tv, lasttime, &delta);
1231
1232 /*
1233 * check for 0,0 is so that the message will be seen at least once,
1234 * even if interval is huge.
1235 */
1236 if (timercmp(&delta, mininterval, >=) ||
1237 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1238 *lasttime = tv;
1239 rv = 1;
1240 }
1241
1242 return (rv);
1243 }
1244
1245 /*
1246 * ppsratecheck(): packets (or events) per second limitation.
1247 */
1248 int
1249 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1250 {
1251 struct timeval tv, delta;
1252 int rv;
1253
1254 getmicrouptime(&tv);
1255 timersub(&tv, lasttime, &delta);
1256
1257 /*
1258 * check for 0,0 is so that the message will be seen at least once.
1259 * if more than one second have passed since the last update of
1260 * lasttime, reset the counter.
1261 *
1262 * we do increment *curpps even in *curpps < maxpps case, as some may
1263 * try to use *curpps for stat purposes as well.
1264 */
1265 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1266 delta.tv_sec >= 1) {
1267 *lasttime = tv;
1268 *curpps = 0;
1269 }
1270 if (maxpps < 0)
1271 rv = 1;
1272 else if (*curpps < maxpps)
1273 rv = 1;
1274 else
1275 rv = 0;
1276
1277 #if 1 /*DIAGNOSTIC?*/
1278 /* be careful about wrap-around */
1279 if (*curpps + 1 > *curpps)
1280 *curpps = *curpps + 1;
1281 #else
1282 /*
1283 * assume that there's not too many calls to this function.
1284 * not sure if the assumption holds, as it depends on *caller's*
1285 * behavior, not the behavior of this function.
1286 * IMHO it is wrong to make assumption on the caller's behavior,
1287 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1288 */
1289 *curpps = *curpps + 1;
1290 #endif
1291
1292 return (rv);
1293 }
1294