kern_time.c revision 1.54.2.19 1 /* $NetBSD: kern_time.c,v 1.54.2.19 2002/10/03 23:49:31 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.54.2.19 2002/10/03 23:49:31 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 struct timeval *oatv = NULL;
442 long ndelta, ntickdelta, odelta;
443 int error;
444 int s;
445
446 error = copyin(delta, &atv, sizeof(struct timeval));
447 if (error)
448 return (error);
449
450 if (olddelta != NULL) {
451 if (uvm_useracc((caddr_t)olddelta,
452 sizeof(struct timeval), B_WRITE) == FALSE)
453 return (EFAULT);
454 oatv = olddelta;
455 }
456
457 /*
458 * Compute the total correction and the rate at which to apply it.
459 * Round the adjustment down to a whole multiple of the per-tick
460 * delta, so that after some number of incremental changes in
461 * hardclock(), tickdelta will become zero, lest the correction
462 * overshoot and start taking us away from the desired final time.
463 */
464 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
465 if (ndelta > bigadj || ndelta < -bigadj)
466 ntickdelta = 10 * tickadj;
467 else
468 ntickdelta = tickadj;
469 if (ndelta % ntickdelta)
470 ndelta = ndelta / ntickdelta * ntickdelta;
471
472 /*
473 * To make hardclock()'s job easier, make the per-tick delta negative
474 * if we want time to run slower; then hardclock can simply compute
475 * tick + tickdelta, and subtract tickdelta from timedelta.
476 */
477 if (ndelta < 0)
478 ntickdelta = -ntickdelta;
479 s = splclock();
480 odelta = timedelta;
481 timedelta = ndelta;
482 tickdelta = ntickdelta;
483 splx(s);
484
485 if (olddelta) {
486 atv.tv_sec = odelta / 1000000;
487 atv.tv_usec = odelta % 1000000;
488 (void) copyout(&atv, olddelta, sizeof(struct timeval));
489 }
490 return (0);
491 }
492
493 /*
494 * Interval timer support. Both the BSD getitimer() family and the POSIX
495 * timer_*() family of routines are supported.
496 *
497 * All timers are kept in an array pointed to by p_timers, which is
498 * allocated on demand - many processes don't use timers at all. The
499 * first three elements in this array are reserved for the BSD timers:
500 * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
501 * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
502 * syscall.
503 *
504 * Realtime timers are kept in the ptimer structure as an absolute
505 * time; virtual time timers are kept as a linked list of deltas.
506 * Virtual time timers are processed in the hardclock() routine of
507 * kern_clock.c. The real time timer is processed by a callout
508 * routine, called from the softclock() routine. Since a callout may
509 * be delayed in real time due to interrupt processing in the system,
510 * it is possible for the real time timeout routine (realtimeexpire,
511 * given below), to be delayed in real time past when it is supposed
512 * to occur. It does not suffice, therefore, to reload the real timer
513 * .it_value from the real time timers .it_interval. Rather, we
514 * compute the next time in absolute time the timer should go off. */
515
516 /* Allocate a POSIX realtime timer. */
517 int
518 sys_timer_create(struct lwp *l, void *v, register_t *retval)
519 {
520 struct sys_timer_create_args /* {
521 syscallarg(clockid_t) clock_id;
522 syscallarg(struct sigevent *) evp;
523 syscallarg(timer_t *) timerid;
524 } */ *uap = v;
525 struct proc *p = l->l_proc;
526 clockid_t id;
527 struct sigevent *evp;
528 struct ptimer *pt;
529 int timerid, error;
530
531 id = SCARG(uap, clock_id);
532 if (id < CLOCK_REALTIME ||
533 id > CLOCK_PROF)
534 return (EINVAL);
535
536 if (p->p_timers == NULL)
537 timers_alloc(p);
538
539 /* Find a free timer slot, skipping those reserved for setitimer(). */
540 for (timerid = 3; timerid < TIMER_MAX; timerid++)
541 if (p->p_timers->pts_timers[timerid] == NULL)
542 break;
543
544 if (timerid == TIMER_MAX)
545 return EAGAIN;
546
547 pt = pool_get(&ptimer_pool, PR_WAITOK);
548 evp = SCARG(uap, evp);
549 if (evp) {
550 if (((error =
551 copyin(evp, &pt->pt_ev, sizeof (pt->pt_ev))) != 0) ||
552 ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
553 (pt->pt_ev.sigev_notify > SIGEV_SA))) {
554 pool_put(&ptimer_pool, pt);
555 return (error ? error : EINVAL);
556 }
557 } else {
558 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
559 switch (id) {
560 case CLOCK_REALTIME:
561 pt->pt_ev.sigev_signo = SIGALRM;
562 break;
563 case CLOCK_VIRTUAL:
564 pt->pt_ev.sigev_signo = SIGVTALRM;
565 break;
566 case CLOCK_PROF:
567 pt->pt_ev.sigev_signo = SIGPROF;
568 break;
569 }
570 pt->pt_ev.sigev_value.sival_int = timerid;
571 }
572 pt->pt_info.si_signo = pt->pt_ev.sigev_signo;
573 pt->pt_info.si_errno = 0;
574 pt->pt_info.si_code = 0;
575 pt->pt_info.si_pid = p->p_pid;
576 pt->pt_info.si_uid = p->p_cred->p_ruid;
577 pt->pt_info.si_addr = NULL;
578 pt->pt_info.si_status = 0;
579 pt->pt_info.si_value = pt->pt_ev.sigev_value;
580
581 pt->pt_type = id;
582 pt->pt_proc = p;
583 pt->pt_overruns = 0;
584 timerclear(&pt->pt_time.it_value);
585 if (id == CLOCK_REALTIME)
586 callout_init(&pt->pt_ch);
587 else
588 pt->pt_active = 0;
589
590 p->p_timers->pts_timers[timerid] = pt;
591
592 return copyout(&timerid, SCARG(uap, timerid), sizeof(timerid));
593 }
594
595
596 /* Delete a POSIX realtime timer */
597 int
598 sys_timer_delete(struct lwp *l, void *v, register_t *retval)
599 {
600 struct sys_timer_delete_args /* {
601 syscallarg(timer_t) timerid;
602 } */ *uap = v;
603 struct proc *p = l->l_proc;
604 int timerid;
605 struct ptimer *pt, *ptn;
606 int s;
607
608 timerid = SCARG(uap, timerid);
609
610 if ((p->p_timers == NULL) ||
611 (timerid < 2) || (timerid >= TIMER_MAX) ||
612 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
613 return (EINVAL);
614
615 if (pt->pt_type == CLOCK_REALTIME)
616 callout_stop(&pt->pt_ch);
617 else if (pt->pt_active) {
618 s = splclock();
619 ptn = LIST_NEXT(pt, pt_list);
620 LIST_REMOVE(pt, pt_list);
621 for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
622 timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
623 &ptn->pt_time.it_value);
624 splx(s);
625 }
626
627 p->p_timers->pts_timers[timerid] = NULL;
628 pool_put(&ptimer_pool, pt);
629
630 return (0);
631 }
632
633 /*
634 * Set up the given timer. The value in pt->pt_time.it_value is taken to be
635 * relative to now.
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 timeradd(&pt->pt_time.it_value, &time,
648 &pt->pt_time.it_value);
649 /*
650 * Don't need to check hzto() return value, here.
651 * callout_reset() does it for us.
652 */
653 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
654 realtimerexpire, pt);
655 }
656 } else {
657 if (pt->pt_active) {
658 ptn = LIST_NEXT(pt, pt_list);
659 LIST_REMOVE(pt, pt_list);
660 for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
661 timeradd(&pt->pt_time.it_value,
662 &ptn->pt_time.it_value,
663 &ptn->pt_time.it_value);
664 }
665 if (timerisset(&pt->pt_time.it_value)) {
666 if (pt->pt_type == CLOCK_VIRTUAL)
667 ptl = &pt->pt_proc->p_timers->pts_virtual;
668 else
669 ptl = &pt->pt_proc->p_timers->pts_prof;
670
671 for (ptn = LIST_FIRST(ptl), pptn = NULL;
672 ptn && timercmp(&pt->pt_time.it_value,
673 &ptn->pt_time.it_value, >);
674 pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
675 timersub(&pt->pt_time.it_value,
676 &ptn->pt_time.it_value,
677 &pt->pt_time.it_value);
678
679 if (pptn)
680 LIST_INSERT_AFTER(pptn, pt, pt_list);
681 else
682 LIST_INSERT_HEAD(ptl, pt, pt_list);
683
684 for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
685 timersub(&ptn->pt_time.it_value,
686 &pt->pt_time.it_value,
687 &ptn->pt_time.it_value);
688
689 pt->pt_active = 1;
690 } else
691 pt->pt_active = 0;
692 }
693 }
694
695 void
696 timer_gettime(struct ptimer *pt, struct itimerval *aitv)
697 {
698 struct ptimer *ptn;
699
700 *aitv = pt->pt_time;
701 if (pt->pt_type == CLOCK_REALTIME) {
702 /*
703 * Convert from absolute to relative time in .it_value
704 * part of real time timer. If time for real time
705 * timer has passed return 0, else return difference
706 * between current time and time for the timer to go
707 * off.
708 */
709 if (timerisset(&aitv->it_value)) {
710 if (timercmp(&aitv->it_value, &time, <))
711 timerclear(&aitv->it_value);
712 else
713 timersub(&aitv->it_value, &time,
714 &aitv->it_value);
715 }
716 } else if (pt->pt_active) {
717 if (pt->pt_type == CLOCK_VIRTUAL)
718 ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
719 else
720 ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
721 for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
722 timeradd(&aitv->it_value,
723 &ptn->pt_time.it_value, &aitv->it_value);
724 KASSERT(ptn != NULL); /* pt should be findable on the list */
725 } else
726 timerclear(&aitv->it_value);
727 }
728
729
730
731 /* Set and arm a POSIX realtime timer */
732 int
733 sys_timer_settime(struct lwp *l, void *v, register_t *retval)
734 {
735 struct sys_timer_settime_args /* {
736 syscallarg(timer_t) timerid;
737 syscallarg(int) flags;
738 syscallarg(const struct itimerspec *) value;
739 syscallarg(struct itimerspec *) ovalue;
740 } */ *uap = v;
741 struct proc *p = l->l_proc;
742 int error, s, timerid;
743 struct itimerval val, oval;
744 struct itimerspec value, ovalue;
745 struct ptimer *pt;
746
747 timerid = SCARG(uap, timerid);
748
749 if ((p->p_timers == NULL) ||
750 (timerid < 2) || (timerid >= TIMER_MAX) ||
751 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
752 return (EINVAL);
753
754 if ((error = copyin(SCARG(uap, value), &value,
755 sizeof(struct itimerspec))) != 0)
756 return (error);
757
758 TIMESPEC_TO_TIMEVAL(&val.it_value, &value.it_value);
759 TIMESPEC_TO_TIMEVAL(&val.it_interval, &value.it_interval);
760 if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
761 return (EINVAL);
762
763 oval = pt->pt_time;
764 pt->pt_time = val;
765
766 s = splclock();
767 /* If we've been passed an absolute time, convert it to relative. */
768 if (timerisset(&pt->pt_time.it_value) &&
769 (SCARG(uap, flags) & TIMER_ABSTIME))
770 timersub(&pt->pt_time.it_value, &time,
771 &pt->pt_time.it_value);
772 timer_settime(pt);
773 splx(s);
774
775 if (SCARG(uap, ovalue)) {
776 TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue.it_value);
777 TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue.it_interval);
778 return copyout(&ovalue, SCARG(uap, ovalue),
779 sizeof(struct itimerspec));
780 }
781
782 return (0);
783 }
784
785 /* Return the time remaining until a POSIX timer fires. */
786 int
787 sys_timer_gettime(struct lwp *l, void *v, register_t *retval)
788 {
789 struct sys_timer_gettime_args /* {
790 syscallarg(timer_t) timerid;
791 syscallarg(struct itimerspec *) value;
792 } */ *uap = v;
793 struct itimerval aitv;
794 struct itimerspec its;
795 struct proc *p = l->l_proc;
796 int s, timerid;
797 struct ptimer *pt;
798
799 timerid = SCARG(uap, timerid);
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 s = splclock();
807 timer_gettime(pt, &aitv);
808 splx(s);
809
810 TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its.it_interval);
811 TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its.it_value);
812
813 return copyout(&its, SCARG(uap, value), sizeof(its));
814 }
815
816 /*
817 * Return the count of the number of times a periodic timer expired
818 * while a notification was already pending. The counter is reset when
819 * a timer expires and a notification can be posted.
820 */
821 int
822 sys_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
823 {
824 struct sys_timer_getoverrun_args /* {
825 syscallarg(timer_t) timerid;
826 } */ *uap = v;
827 struct proc *p = l->l_proc;
828 int timerid;
829 struct ptimer *pt;
830
831 timerid = SCARG(uap, timerid);
832
833 if ((p->p_timers == NULL) ||
834 (timerid < 2) || (timerid >= TIMER_MAX) ||
835 ((pt = p->p_timers->pts_timers[timerid]) == NULL))
836 return (EINVAL);
837
838 *retval = pt->pt_overruns;
839
840 return (0);
841 }
842
843 /* Glue function that triggers an upcall; called from userret(). */
844 static void
845 timerupcall(struct lwp *l, void *arg)
846 {
847 struct ptimer *pt = (struct ptimer *)arg;
848
849 /* The upcall should be generated exactly once. */
850 if (sa_upcall(l, SA_UPCALL_SIGEV | SA_UPCALL_DEFER, NULL, l,
851 sizeof(siginfo_t), &pt->pt_info) == 0)
852 l->l_proc->p_userret = NULL;
853 }
854
855
856 /*
857 * Real interval timer expired:
858 * send process whose timer expired an alarm signal.
859 * If time is not set up to reload, then just return.
860 * Else compute next time timer should go off which is > current time.
861 * This is where delay in processing this timeout causes multiple
862 * SIGALRM calls to be compressed into one.
863 */
864 void
865 realtimerexpire(void *arg)
866 {
867 struct ptimer *pt;
868 int s;
869
870 pt = (struct ptimer *)arg;
871
872 itimerfire(pt);
873
874 if (!timerisset(&pt->pt_time.it_interval)) {
875 timerclear(&pt->pt_time.it_value);
876 return;
877 }
878 for (;;) {
879 s = splclock();
880 timeradd(&pt->pt_time.it_value,
881 &pt->pt_time.it_interval, &pt->pt_time.it_value);
882 if (timercmp(&pt->pt_time.it_value, &time, >)) {
883 /*
884 * Don't need to check hzto() return value, here.
885 * callout_reset() does it for us.
886 */
887 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
888 realtimerexpire, pt);
889 splx(s);
890 return;
891 }
892 splx(s);
893 pt->pt_overruns++;
894 }
895 }
896
897 /* BSD routine to get the value of an interval timer. */
898 /* ARGSUSED */
899 int
900 sys_getitimer(struct lwp *l, void *v, register_t *retval)
901 {
902 struct sys_getitimer_args /* {
903 syscallarg(int) which;
904 syscallarg(struct itimerval *) itv;
905 } */ *uap = v;
906 struct proc *p = l->l_proc;
907 struct itimerval aitv;
908 int s, which;
909
910 which = SCARG(uap, which);
911
912 if ((u_int)which > ITIMER_PROF)
913 return (EINVAL);
914
915 if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
916 timerclear(&aitv.it_value);
917 timerclear(&aitv.it_interval);
918 } else {
919 s = splclock();
920 timer_gettime(p->p_timers->pts_timers[which], &aitv);
921 splx(s);
922 }
923
924 return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
925
926 }
927
928 /* BSD routine to set/arm an interval timer. */
929 /* ARGSUSED */
930 int
931 sys_setitimer(struct lwp *l, void *v, register_t *retval)
932 {
933 struct sys_setitimer_args /* {
934 syscallarg(int) which;
935 syscallarg(const struct itimerval *) itv;
936 syscallarg(struct itimerval *) oitv;
937 } */ *uap = v;
938 struct proc *p = l->l_proc;
939 int which = SCARG(uap, which);
940 struct sys_getitimer_args getargs;
941 struct itimerval aitv;
942 const struct itimerval *itvp;
943 struct ptimer *pt;
944 int s, error;
945
946 if ((u_int)which > ITIMER_PROF)
947 return (EINVAL);
948 itvp = SCARG(uap, itv);
949 if (itvp &&
950 (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
951 return (error);
952 if (SCARG(uap, oitv) != NULL) {
953 SCARG(&getargs, which) = which;
954 SCARG(&getargs, itv) = SCARG(uap, oitv);
955 if ((error = sys_getitimer(l, &getargs, retval)) != 0)
956 return (error);
957 }
958 if (itvp == 0)
959 return (0);
960 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
961 return (EINVAL);
962
963 /*
964 * Don't bother allocating data structures if the process just
965 * wants to clear the timer.
966 */
967 if (!timerisset(&aitv.it_value) &&
968 ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
969 return (0);
970
971 if (p->p_timers == NULL)
972 timers_alloc(p);
973 if (p->p_timers->pts_timers[which] == NULL) {
974 pt = pool_get(&ptimer_pool, PR_WAITOK);
975 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
976 pt->pt_overruns = 0;
977 pt->pt_proc = p;
978 pt->pt_type = which;
979 switch (which) {
980 case ITIMER_REAL:
981 callout_init(&pt->pt_ch);
982 pt->pt_ev.sigev_signo = SIGALRM;
983 break;
984 case ITIMER_VIRTUAL:
985 pt->pt_active = 0;
986 pt->pt_ev.sigev_signo = SIGVTALRM;
987 break;
988 case ITIMER_PROF:
989 pt->pt_active = 0;
990 pt->pt_ev.sigev_signo = SIGPROF;
991 break;
992 }
993 } else
994 pt = p->p_timers->pts_timers[which];
995
996 pt->pt_time = aitv;
997 p->p_timers->pts_timers[which] = pt;
998
999 s = splclock();
1000 timer_settime(pt);
1001 splx(s);
1002
1003 return (0);
1004 }
1005
1006 /* Utility routines to manage the array of pointers to timers. */
1007 void
1008 timers_alloc(struct proc *p)
1009 {
1010 int i;
1011 struct ptimers *pts;
1012
1013 pts = malloc(sizeof (struct ptimers), M_SUBPROC, 0);
1014 LIST_INIT(&pts->pts_virtual);
1015 LIST_INIT(&pts->pts_prof);
1016 for (i = 0; i < TIMER_MAX; i++)
1017 pts->pts_timers[i] = NULL;
1018 p->p_timers = pts;
1019 }
1020
1021 /*
1022 * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1023 * then clean up all timers and free all the data structures. If
1024 * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1025 * by timer_create(), not the BSD setitimer() timers, and only free the
1026 * structure if none of those remain.
1027 */
1028 void
1029 timers_free(struct proc *p, int which)
1030 {
1031 int i, s;
1032 struct ptimers *pts;
1033 struct ptimer *pt, *ptn;
1034 struct timeval tv;
1035
1036 if (p->p_timers) {
1037 pts = p->p_timers;
1038 if (which == TIMERS_ALL)
1039 i = 0;
1040 else {
1041 s = splclock();
1042 timerclear(&tv);
1043 for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
1044 ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1045 ptn = LIST_NEXT(ptn, pt_list))
1046 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1047 LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
1048 if (ptn) {
1049 timeradd(&tv, &ptn->pt_time.it_value,
1050 &ptn->pt_time.it_value);
1051 LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
1052 ptn, pt_list);
1053 }
1054
1055 timerclear(&tv);
1056 for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
1057 ptn && ptn != pts->pts_timers[ITIMER_PROF];
1058 ptn = LIST_NEXT(ptn, pt_list))
1059 timeradd(&tv, &ptn->pt_time.it_value, &tv);
1060 LIST_FIRST(&p->p_timers->pts_prof) = NULL;
1061 if (ptn) {
1062 timeradd(&tv, &ptn->pt_time.it_value,
1063 &ptn->pt_time.it_value);
1064 LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
1065 pt_list);
1066 }
1067 splx(s);
1068 i = 3;
1069 }
1070 for ( ; i < TIMER_MAX; i++)
1071 if ((pt = pts->pts_timers[i]) != NULL) {
1072 if (pt->pt_type == CLOCK_REALTIME)
1073 callout_stop(&pt->pt_ch);
1074 pts->pts_timers[i] = NULL;
1075 pool_put(&ptimer_pool, pt);
1076 }
1077 if ((pts->pts_timers[0] == NULL) &&
1078 (pts->pts_timers[1] == NULL) &&
1079 (pts->pts_timers[2] == NULL)) {
1080 p->p_timers = NULL;
1081 free(pts, M_SUBPROC);
1082 }
1083 }
1084 }
1085
1086 /*
1087 * Check that a proposed value to load into the .it_value or
1088 * .it_interval part of an interval timer is acceptable, and
1089 * fix it to have at least minimal value (i.e. if it is less
1090 * than the resolution of the clock, round it up.)
1091 */
1092 int
1093 itimerfix(struct timeval *tv)
1094 {
1095
1096 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
1097 return (EINVAL);
1098 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
1099 tv->tv_usec = tick;
1100 return (0);
1101 }
1102
1103 /*
1104 * Decrement an interval timer by a specified number
1105 * of microseconds, which must be less than a second,
1106 * i.e. < 1000000. If the timer expires, then reload
1107 * it. In this case, carry over (usec - old value) to
1108 * reduce the value reloaded into the timer so that
1109 * the timer does not drift. This routine assumes
1110 * that it is called in a context where the timers
1111 * on which it is operating cannot change in value.
1112 */
1113 int
1114 itimerdecr(struct ptimer *pt, int usec)
1115 {
1116 struct itimerval *itp;
1117
1118 itp = &pt->pt_time;
1119 if (itp->it_value.tv_usec < usec) {
1120 if (itp->it_value.tv_sec == 0) {
1121 /* expired, and already in next interval */
1122 usec -= itp->it_value.tv_usec;
1123 goto expire;
1124 }
1125 itp->it_value.tv_usec += 1000000;
1126 itp->it_value.tv_sec--;
1127 }
1128 itp->it_value.tv_usec -= usec;
1129 usec = 0;
1130 if (timerisset(&itp->it_value))
1131 return (1);
1132 /* expired, exactly at end of interval */
1133 expire:
1134 if (timerisset(&itp->it_interval)) {
1135 itp->it_value = itp->it_interval;
1136 itp->it_value.tv_usec -= usec;
1137 if (itp->it_value.tv_usec < 0) {
1138 itp->it_value.tv_usec += 1000000;
1139 itp->it_value.tv_sec--;
1140 }
1141 timer_settime(pt);
1142 } else
1143 itp->it_value.tv_usec = 0; /* sec is already 0 */
1144 return (0);
1145 }
1146
1147 void
1148 itimerfire(struct ptimer *pt)
1149 {
1150 struct proc *p = pt->pt_proc;
1151
1152 if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
1153 /*
1154 * No RT signal infrastructure exists at this time;
1155 * just post the signal number and throw away the
1156 * value.
1157 */
1158 if (sigismember(&p->p_sigctx.ps_siglist, pt->pt_ev.sigev_signo))
1159 pt->pt_overruns++;
1160 else {
1161 pt->pt_overruns = 0;
1162 psignal(p, pt->pt_ev.sigev_signo);
1163 }
1164 } else if (pt->pt_ev.sigev_notify == SIGEV_SA && (p->p_flag & P_SA)) {
1165 /* Cause the process to generate an upcall when it returns. */
1166 struct sadata *sa = p->p_sa;
1167
1168 if (p->p_userret == NULL) {
1169 if (sa->sa_idle)
1170 wakeup(p);
1171 pt->pt_overruns = 0;
1172 p->p_userret = timerupcall;
1173 p->p_userret_arg = pt;
1174 } else
1175 pt->pt_overruns++;
1176 }
1177
1178 }
1179
1180 /*
1181 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
1182 * for usage and rationale.
1183 */
1184 int
1185 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1186 {
1187 struct timeval tv, delta;
1188 int s, rv = 0;
1189
1190 s = splclock();
1191 tv = mono_time;
1192 splx(s);
1193
1194 timersub(&tv, lasttime, &delta);
1195
1196 /*
1197 * check for 0,0 is so that the message will be seen at least once,
1198 * even if interval is huge.
1199 */
1200 if (timercmp(&delta, mininterval, >=) ||
1201 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1202 *lasttime = tv;
1203 rv = 1;
1204 }
1205
1206 return (rv);
1207 }
1208
1209 /*
1210 * ppsratecheck(): packets (or events) per second limitation.
1211 */
1212 int
1213 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1214 {
1215 struct timeval tv, delta;
1216 int s, rv;
1217
1218 s = splclock();
1219 tv = mono_time;
1220 splx(s);
1221
1222 timersub(&tv, lasttime, &delta);
1223
1224 /*
1225 * check for 0,0 is so that the message will be seen at least once.
1226 * if more than one second have passed since the last update of
1227 * lasttime, reset the counter.
1228 *
1229 * we do increment *curpps even in *curpps < maxpps case, as some may
1230 * try to use *curpps for stat purposes as well.
1231 */
1232 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1233 delta.tv_sec >= 1) {
1234 *lasttime = tv;
1235 *curpps = 0;
1236 rv = 1;
1237 } else if (maxpps < 0)
1238 rv = 1;
1239 else if (*curpps < maxpps)
1240 rv = 1;
1241 else
1242 rv = 0;
1243
1244 #if 1 /*DIAGNOSTIC?*/
1245 /* be careful about wrap-around */
1246 if (*curpps + 1 > *curpps)
1247 *curpps = *curpps + 1;
1248 #else
1249 /*
1250 * assume that there's not too many calls to this function.
1251 * not sure if the assumption holds, as it depends on *caller's*
1252 * behavior, not the behavior of this function.
1253 * IMHO it is wrong to make assumption on the caller's behavior,
1254 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1255 */
1256 *curpps = *curpps + 1;
1257 #endif
1258
1259 return (rv);
1260 }
1261