kern_time.c revision 1.54.2.14 1 /* $NetBSD: kern_time.c,v 1.54.2.14 2002/07/12 01:40:20 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.14 2002/07/12 01:40:20 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 realtimerupcall(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 deltas. Virtual time timers
506 * are processed in the hardclock() routine of kern_clock.c. The real
507 * time timer is processed by a callout routine, called from the
508 * softclock() routine. Since a callout may be delayed in real time
509 * due to interrupt processing in the system, it is possible for the
510 * real time timeout routine (realtimeexpire, given below), to be
511 * delayed in real time past when it is supposed to occur. It does
512 * not suffice, therefore, to reload the real timer .it_value from the
513 * real time timers .it_interval. Rather, we compute the next time in
514 * absolute time the timer should go off.
515 */
516
517 /* Allocate a POSIX realtime timer. */
518 int
519 sys_timer_create(struct lwp *l, void *v, register_t *retval)
520 {
521 struct sys_timer_create_args /* {
522 syscallarg(clockid_t) clock_id;
523 syscallarg(struct sigevent *) evp;
524 syscallarg(timer_t *) timerid;
525 } */ *uap = v;
526 struct proc *p = l->l_proc;
527 clockid_t id;
528 struct sigevent *evp;
529 struct ptimer *pt;
530 int timerid, error;
531
532 id = SCARG(uap, clock_id);
533 if (id != CLOCK_REALTIME)
534 return (EINVAL);
535
536 if (p->p_timers == NULL)
537 timers_alloc(p);
538
539 for (timerid = 3; timerid < TIMER_MAX; timerid++)
540 if (p->p_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 pt->pt_ev.sigev_signo = SIGALRM;
559 pt->pt_ev.sigev_value.sival_int = timerid;
560 }
561 pt->pt_info.si_signo = pt->pt_ev.sigev_signo;
562 pt->pt_info.si_errno = 0;
563 pt->pt_info.si_code = 0;
564 pt->pt_info.si_pid = p->p_pid;
565 pt->pt_info.si_uid = p->p_cred->p_ruid;
566 pt->pt_info.si_addr = NULL;
567 pt->pt_info.si_status = 0;
568 pt->pt_info.si_value = pt->pt_ev.sigev_value;
569
570 callout_init(&pt->pt_ch);
571 pt->pt_type = CLOCK_REALTIME;
572 pt->pt_proc = p;
573 pt->pt_overruns = 0;
574
575 p->p_timers[timerid] = pt;
576
577 return copyout(&timerid, SCARG(uap, timerid), sizeof(timerid));
578 }
579
580
581 /* Delete a POSIX realtime timer */
582 int
583 sys_timer_delete(struct lwp *l, void *v, register_t *retval)
584 {
585 struct sys_timer_delete_args /* {
586 syscallarg(timer_t) timerid;
587 } */ *uap = v;
588 struct proc *p = l->l_proc;
589 int timerid;
590 struct ptimer *pt;
591
592 timerid = SCARG(uap, timerid);
593
594 if ((p->p_timers == NULL) ||
595 (timerid < 2) || (timerid >= TIMER_MAX) ||
596 ((pt = p->p_timers[timerid]) == NULL))
597 return (EINVAL);
598
599 callout_stop(&pt->pt_ch);
600 p->p_timers[timerid] = NULL;
601 pool_put(&ptimer_pool, pt);
602
603 return (0);
604 }
605
606 /* Set and arm a POSIX realtime timer */
607 int
608 sys_timer_settime(struct lwp *l, void *v, register_t *retval)
609 {
610 struct sys_timer_settime_args /* {
611 syscallarg(timer_t) timerid;
612 syscallarg(int) flags;
613 syscallarg(const struct itimerspec *) value;
614 syscallarg(struct itimerspec *) ovalue;
615 } */ *uap = v;
616 struct proc *p = l->l_proc;
617 int error, s, timerid;
618 struct itimerval val, oval;
619 struct itimerspec value, ovalue;
620 struct ptimer *pt;
621
622 timerid = SCARG(uap, timerid);
623
624 if ((p->p_timers == NULL) ||
625 (timerid < 2) || (timerid >= TIMER_MAX) ||
626 ((pt = p->p_timers[timerid]) == NULL))
627 return (EINVAL);
628
629 if ((error = copyin(SCARG(uap, value), &value,
630 sizeof(struct itimerspec))) != 0)
631 return (error);
632
633 TIMESPEC_TO_TIMEVAL(&val.it_value, &value.it_value);
634 TIMESPEC_TO_TIMEVAL(&val.it_interval, &value.it_interval);
635 if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
636 return (EINVAL);
637
638 oval = pt->pt_time;
639 pt->pt_time = val;
640
641 s = splclock();
642 callout_stop(&pt->pt_ch);
643 if (timerisset(&pt->pt_time.it_value)) {
644 if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0)
645 timeradd(&pt->pt_time.it_value, &time,
646 &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 splx(s);
655
656 if (SCARG(uap, ovalue)) {
657 TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue.it_value);
658 TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue.it_interval);
659 return copyout(&ovalue, SCARG(uap, ovalue),
660 sizeof(struct itimerspec));
661 }
662
663 return (0);
664 }
665
666 /* Return the time remaining until a POSIX timer fires. */
667 int
668 sys_timer_gettime(struct lwp *l, void *v, register_t *retval)
669 {
670 struct sys_timer_gettime_args /* {
671 syscallarg(timer_t) timerid;
672 syscallarg(struct itimerspec *) value;
673 } */ *uap = v;
674 struct itimerval aitv;
675 struct itimerspec its;
676 struct proc *p = l->l_proc;
677 int timerid;
678 struct ptimer *pt;
679
680 timerid = SCARG(uap, timerid);
681
682 if ((p->p_timers == NULL) ||
683 (timerid < 2) || (timerid >= TIMER_MAX) ||
684 ((pt = p->p_timers[timerid]) == NULL))
685 return (EINVAL);
686
687 aitv = pt->pt_time;
688
689 /*
690 * Real-time timers are kept in absolute time, but this interface
691 * is supposed to return a relative time.
692 */
693 if (timerisset(&aitv.it_value)) {
694 if (timercmp(&aitv.it_value, &time, <))
695 timerclear(&aitv.it_value);
696 else
697 timersub(&aitv.it_value, &time, &aitv.it_value);
698 }
699
700 TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its.it_interval);
701 TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its.it_value);
702
703 return copyout(&its, SCARG(uap, value), sizeof(its));
704 }
705
706 /*
707 * Return the count of the number of times a periodic timer expired
708 * while a notification was already pending. The counter is reset when
709 * a timer expires and a notification can be posted.
710 */
711 int
712 sys_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
713 {
714 struct sys_timer_getoverrun_args /* {
715 syscallarg(timer_t) timerid;
716 } */ *uap = v;
717 struct proc *p = l->l_proc;
718 int timerid;
719 struct ptimer *pt;
720
721 timerid = SCARG(uap, timerid);
722
723 if ((p->p_timers == NULL) ||
724 (timerid < 2) || (timerid >= TIMER_MAX) ||
725 ((pt = p->p_timers[timerid]) == NULL))
726 return (EINVAL);
727
728 *retval = pt->pt_overruns;
729
730 return (0);
731 }
732
733 /* Glue function that triggers an upcall; called from userret(). */
734 static void
735 realtimerupcall(struct lwp *l, void *arg)
736 {
737 struct ptimer *pt;
738
739 /* The LWP that is running doesn't change, so we don't need
740 * to touch sa_vp.
741 */
742 pt = (struct ptimer *)arg;
743 sa_upcall(l, SA_UPCALL_SIGEV, NULL, l, sizeof(siginfo_t),
744 &pt->pt_info);
745
746 /* The upcall should only be generated once. */
747 l->l_proc->p_userret = NULL;
748 }
749
750
751 /*
752 * Real interval timer expired:
753 * send process whose timer expired an alarm signal.
754 * If time is not set up to reload, then just return.
755 * Else compute next time timer should go off which is > current time.
756 * This is where delay in processing this timeout causes multiple
757 * SIGALRM calls to be compressed into one.
758 */
759 void
760 realtimerexpire(void *arg)
761 {
762 struct ptimer *pt;
763 struct proc *p;
764 int s;
765
766 pt = (struct ptimer *)arg;
767 p = pt->pt_proc;
768 if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
769 /*
770 * No RT signal infrastructure exists at this time;
771 * just post the signal number and throw away the
772 * value.
773 */
774 if (sigismember(&p->p_sigctx.ps_siglist, pt->pt_ev.sigev_signo))
775 pt->pt_overruns++;
776 else {
777 pt->pt_overruns = 0;
778 psignal(p, pt->pt_ev.sigev_signo);
779 }
780 } else if (pt->pt_ev.sigev_notify == SIGEV_SA && (p->p_flag & P_SA)) {
781 int notified = 0;
782 /* Cause the process to generate an upcall when it returns. */
783
784 if (p->p_nrlwps == 0) {
785 struct sadata_upcall *sd;
786 struct sadata *sa = p->p_sa;
787 struct lwp *l2;
788 int s, ret;
789
790 SCHED_LOCK(s);
791 l2 = sa_getcachelwp(p);
792 if (l2 != NULL) {
793 sd = sadata_upcall_alloc(0);
794 cpu_setfunc(l2, sa_switchcall, NULL);
795 ret = sa_upcall0(l2, SA_UPCALL_SIGEV,
796 NULL, NULL, sizeof(siginfo_t),
797 &pt->pt_info, sd);
798 if (ret == 0) {
799 l2->l_priority = l2->l_usrpri;
800 PRELE(l2);
801 KDASSERT(sa->sa_vp == NULL);
802 sa->sa_vp = l2;
803 setrunnable(l2);
804 notified = 1;
805 } else
806 sa_putcachelwp(p, l2);
807 }
808 SCHED_UNLOCK(s);
809 } else if (p->p_userret == NULL) {
810 pt->pt_overruns = 0;
811 p->p_userret = realtimerupcall;
812 p->p_userret_arg = pt;
813 notified = 1;
814 }
815 if (notified == 0)
816 pt->pt_overruns++;
817 }
818 if (!timerisset(&pt->pt_time.it_interval)) {
819 timerclear(&pt->pt_time.it_value);
820 return;
821 }
822 for (;;) {
823 s = splclock();
824 timeradd(&pt->pt_time.it_value,
825 &pt->pt_time.it_interval, &pt->pt_time.it_value);
826 if (timercmp(&pt->pt_time.it_value, &time, >)) {
827 /*
828 * Don't need to check hzto() return value, here.
829 * callout_reset() does it for us.
830 */
831 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
832 realtimerexpire, pt);
833 splx(s);
834 return;
835 }
836 splx(s);
837 pt->pt_overruns++;
838 }
839 }
840
841 /* BSD routine to get the value of an interval timer. */
842 /* ARGSUSED */
843 int
844 sys_getitimer(struct lwp *l, void *v, register_t *retval)
845 {
846 struct sys_getitimer_args /* {
847 syscallarg(int) which;
848 syscallarg(struct itimerval *) itv;
849 } */ *uap = v;
850 struct proc *p = l->l_proc;
851 struct itimerval aitv;
852 int s, which;
853
854 which = SCARG(uap, which);
855
856 if ((u_int)which > ITIMER_PROF)
857 return (EINVAL);
858
859 if ((p->p_timers == NULL) || (p->p_timers[which] == NULL)) {
860 timerclear(&aitv.it_value);
861 timerclear(&aitv.it_interval);
862 } else {
863 s = splclock();
864 if (which == ITIMER_REAL) {
865 /*
866 * Convert from absolute to relative time in
867 * .it_value part of real time timer. If time
868 * for real time timer has passed return 0,
869 * else return difference between current time
870 * and time for the timer to go off.
871 */
872 aitv = p->p_timers[ITIMER_REAL]->pt_time;
873 if (timerisset(&aitv.it_value)) {
874 if (timercmp(&aitv.it_value, &time, <))
875 timerclear(&aitv.it_value);
876 else
877 timersub(&aitv.it_value, &time, &aitv.it_value);
878 }
879 } else
880 aitv = p->p_timers[which]->pt_time;
881 splx(s);
882 }
883
884 return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
885
886 }
887
888 /* BSD routine to set/arm an interval timer. */
889 /* ARGSUSED */
890 int
891 sys_setitimer(struct lwp *l, void *v, register_t *retval)
892 {
893 struct sys_setitimer_args /* {
894 syscallarg(int) which;
895 syscallarg(const struct itimerval *) itv;
896 syscallarg(struct itimerval *) oitv;
897 } */ *uap = v;
898 struct proc *p = l->l_proc;
899 int which = SCARG(uap, which);
900 struct sys_getitimer_args getargs;
901 struct itimerval aitv;
902 const struct itimerval *itvp;
903 struct ptimer *pt;
904 int s, error;
905
906 if ((u_int)which > ITIMER_PROF)
907 return (EINVAL);
908 itvp = SCARG(uap, itv);
909 if (itvp &&
910 (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
911 return (error);
912 if (SCARG(uap, oitv) != NULL) {
913 SCARG(&getargs, which) = which;
914 SCARG(&getargs, itv) = SCARG(uap, oitv);
915 if ((error = sys_getitimer(l, &getargs, retval)) != 0)
916 return (error);
917 }
918 if (itvp == 0)
919 return (0);
920 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
921 return (EINVAL);
922
923 /*
924 * Don't bother allocating data structures if the process just
925 * wants to clear the timer.
926 */
927 if (!timerisset(&aitv.it_value) &&
928 ((p->p_timers == NULL) || (p->p_timers[which] == NULL)))
929 return (0);
930
931 if (p->p_timers == NULL)
932 timers_alloc(p);
933 if (p->p_timers[which] == NULL) {
934 pt = pool_get(&ptimer_pool, PR_WAITOK);
935 callout_init(&pt->pt_ch);
936 pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
937 pt->pt_overruns = 0;
938 pt->pt_proc = p;
939 pt->pt_type = which;
940 switch (which) {
941 case ITIMER_REAL:
942 pt->pt_ev.sigev_signo = SIGALRM;
943 break;
944 case ITIMER_VIRTUAL:
945 pt->pt_ev.sigev_signo = SIGVTALRM;
946 break;
947 case ITIMER_PROF:
948 pt->pt_ev.sigev_signo = SIGPROF;
949 break;
950 }
951 } else
952 pt = p->p_timers[which];
953
954 pt->pt_time = aitv;
955 p->p_timers[which] = pt;
956 if (which == ITIMER_REAL) {
957 s = splclock();
958 callout_stop(&pt->pt_ch);
959 if (timerisset(&pt->pt_time.it_value)) {
960 timeradd(&pt->pt_time.it_value, &time,
961 &pt->pt_time.it_value);
962 /*
963 * Don't need to check hzto() return value, here.
964 * callout_reset() does it for us.
965 */
966 callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
967 realtimerexpire, pt);
968 }
969 splx(s);
970 }
971
972 return (0);
973 }
974
975 /* Utility routines to manage the array of pointers to timers. */
976 void
977 timers_alloc(struct proc *p)
978 {
979 int i;
980 struct ptimer **pts;
981
982 pts = malloc(TIMER_MAX * sizeof(struct timer *), M_SUBPROC, 0);
983 for (i = 0; i < TIMER_MAX; i++)
984 pts[i] = NULL;
985 p->p_timers = pts;
986 }
987
988 void
989 timers_free(struct proc *p)
990 {
991 int i;
992 struct ptimer *pt, **pts;
993
994 if (p->p_timers) {
995 pts = p->p_timers;
996 p->p_timers = NULL;
997 for (i = 0; i < TIMER_MAX; i++)
998 if ((pt = pts[i]) != NULL) {
999 if (pt->pt_type == CLOCK_REALTIME)
1000 callout_stop(&pt->pt_ch);
1001 pool_put(&ptimer_pool, pt);
1002 }
1003 free(pts, M_SUBPROC);
1004 }
1005 }
1006
1007 /*
1008 * Check that a proposed value to load into the .it_value or
1009 * .it_interval part of an interval timer is acceptable, and
1010 * fix it to have at least minimal value (i.e. if it is less
1011 * than the resolution of the clock, round it up.)
1012 */
1013 int
1014 itimerfix(struct timeval *tv)
1015 {
1016
1017 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
1018 return (EINVAL);
1019 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
1020 tv->tv_usec = tick;
1021 return (0);
1022 }
1023
1024 /*
1025 * Decrement an interval timer by a specified number
1026 * of microseconds, which must be less than a second,
1027 * i.e. < 1000000. If the timer expires, then reload
1028 * it. In this case, carry over (usec - old value) to
1029 * reduce the value reloaded into the timer so that
1030 * the timer does not drift. This routine assumes
1031 * that it is called in a context where the timers
1032 * on which it is operating cannot change in value.
1033 */
1034 int
1035 itimerdecr(struct itimerval *itp, int usec)
1036 {
1037
1038 if (itp->it_value.tv_usec < usec) {
1039 if (itp->it_value.tv_sec == 0) {
1040 /* expired, and already in next interval */
1041 usec -= itp->it_value.tv_usec;
1042 goto expire;
1043 }
1044 itp->it_value.tv_usec += 1000000;
1045 itp->it_value.tv_sec--;
1046 }
1047 itp->it_value.tv_usec -= usec;
1048 usec = 0;
1049 if (timerisset(&itp->it_value))
1050 return (1);
1051 /* expired, exactly at end of interval */
1052 expire:
1053 if (timerisset(&itp->it_interval)) {
1054 itp->it_value = itp->it_interval;
1055 itp->it_value.tv_usec -= usec;
1056 if (itp->it_value.tv_usec < 0) {
1057 itp->it_value.tv_usec += 1000000;
1058 itp->it_value.tv_sec--;
1059 }
1060 } else
1061 itp->it_value.tv_usec = 0; /* sec is already 0 */
1062 return (0);
1063 }
1064
1065 /*
1066 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
1067 * for usage and rationale.
1068 */
1069 int
1070 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1071 {
1072 struct timeval tv, delta;
1073 int s, rv = 0;
1074
1075 s = splclock();
1076 tv = mono_time;
1077 splx(s);
1078
1079 timersub(&tv, lasttime, &delta);
1080
1081 /*
1082 * check for 0,0 is so that the message will be seen at least once,
1083 * even if interval is huge.
1084 */
1085 if (timercmp(&delta, mininterval, >=) ||
1086 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1087 *lasttime = tv;
1088 rv = 1;
1089 }
1090
1091 return (rv);
1092 }
1093
1094 /*
1095 * ppsratecheck(): packets (or events) per second limitation.
1096 */
1097 int
1098 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1099 {
1100 struct timeval tv, delta;
1101 int s, rv;
1102
1103 s = splclock();
1104 tv = mono_time;
1105 splx(s);
1106
1107 timersub(&tv, lasttime, &delta);
1108
1109 /*
1110 * check for 0,0 is so that the message will be seen at least once.
1111 * if more than one second have passed since the last update of
1112 * lasttime, reset the counter.
1113 *
1114 * we do increment *curpps even in *curpps < maxpps case, as some may
1115 * try to use *curpps for stat purposes as well.
1116 */
1117 if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1118 delta.tv_sec >= 1) {
1119 *lasttime = tv;
1120 *curpps = 0;
1121 rv = 1;
1122 } else if (maxpps < 0)
1123 rv = 1;
1124 else if (*curpps < maxpps)
1125 rv = 1;
1126 else
1127 rv = 0;
1128
1129 #if 1 /*DIAGNOSTIC?*/
1130 /* be careful about wrap-around */
1131 if (*curpps + 1 > *curpps)
1132 *curpps = *curpps + 1;
1133 #else
1134 /*
1135 * assume that there's not too many calls to this function.
1136 * not sure if the assumption holds, as it depends on *caller's*
1137 * behavior, not the behavior of this function.
1138 * IMHO it is wrong to make assumption on the caller's behavior,
1139 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1140 */
1141 *curpps = *curpps + 1;
1142 #endif
1143
1144 return (rv);
1145 }
1146