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