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