timeout/untimeout ==> callout_*
[dragonfly.git] / sys / kern / kern_time.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/kern/kern_time.c,v 1.68.2.1 2002/10/01 08:00:41 bde Exp $
1fa2b4b4 35 * $DragonFly: src/sys/kern/kern_time.c,v 1.16 2004/06/04 20:35:36 dillon Exp $
984263bc
MD
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/buf.h>
41#include <sys/sysproto.h>
42#include <sys/resourcevar.h>
43#include <sys/signalvar.h>
44#include <sys/kernel.h>
45#include <sys/systm.h>
46#include <sys/sysent.h>
df2244e3 47#include <sys/sysunion.h>
984263bc
MD
48#include <sys/proc.h>
49#include <sys/time.h>
50#include <sys/vnode.h>
a94976ad 51#include <sys/sysctl.h>
984263bc
MD
52#include <vm/vm.h>
53#include <vm/vm_extern.h>
245e4f17 54#include <sys/msgport2.h>
88c4d2f6 55#include <sys/thread2.h>
984263bc
MD
56
57struct timezone tz;
58
59/*
60 * Time of day and interval timer support.
61 *
62 * These routines provide the kernel entry points to get and set
63 * the time-of-day and per-process interval timers. Subroutines
64 * here provide support for adding and subtracting timeval structures
65 * and decrementing interval timers, optionally reloading the interval
66 * timers when they expire.
67 */
68
402ed7e1
RG
69static int nanosleep1 (struct timespec *rqt,
70 struct timespec *rmt);
71static int settime (struct timeval *);
72static void timevalfix (struct timeval *);
73static void no_lease_updatetime (int);
984263bc 74
88c4d2f6
MD
75static int sleep_hard_us = 100;
76SYSCTL_INT(_kern, OID_AUTO, sleep_hard_us, CTLFLAG_RW, &sleep_hard_us, 0, "")
a94976ad 77
984263bc
MD
78static void
79no_lease_updatetime(deltat)
80 int deltat;
81{
82}
83
402ed7e1 84void (*lease_updatetime) (int) = no_lease_updatetime;
984263bc
MD
85
86static int
87settime(tv)
88 struct timeval *tv;
89{
90 struct timeval delta, tv1, tv2;
91 static struct timeval maxtime, laststep;
92 struct timespec ts;
984263bc 93
88c4d2f6 94 crit_enter();
984263bc
MD
95 microtime(&tv1);
96 delta = *tv;
97 timevalsub(&delta, &tv1);
98
99 /*
100 * If the system is secure, we do not allow the time to be
101 * set to a value earlier than 1 second less than the highest
102 * time we have yet seen. The worst a miscreant can do in
103 * this circumstance is "freeze" time. He couldn't go
104 * back to the past.
105 *
106 * We similarly do not allow the clock to be stepped more
107 * than one second, nor more than once per second. This allows
108 * a miscreant to make the clock march double-time, but no worse.
109 */
110 if (securelevel > 1) {
111 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
112 /*
113 * Update maxtime to latest time we've seen.
114 */
115 if (tv1.tv_sec > maxtime.tv_sec)
116 maxtime = tv1;
117 tv2 = *tv;
118 timevalsub(&tv2, &maxtime);
119 if (tv2.tv_sec < -1) {
120 tv->tv_sec = maxtime.tv_sec - 1;
121 printf("Time adjustment clamped to -1 second\n");
122 }
123 } else {
124 if (tv1.tv_sec == laststep.tv_sec) {
88c4d2f6 125 crit_exit();
984263bc
MD
126 return (EPERM);
127 }
128 if (delta.tv_sec > 1) {
129 tv->tv_sec = tv1.tv_sec + 1;
130 printf("Time adjustment clamped to +1 second\n");
131 }
132 laststep = *tv;
133 }
134 }
135
136 ts.tv_sec = tv->tv_sec;
137 ts.tv_nsec = tv->tv_usec * 1000;
88c4d2f6 138 set_timeofday(&ts);
984263bc 139 lease_updatetime(delta.tv_sec);
88c4d2f6 140 crit_exit();
984263bc
MD
141 resettodr();
142 return (0);
143}
144
984263bc
MD
145/* ARGSUSED */
146int
41c20dac 147clock_gettime(struct clock_gettime_args *uap)
984263bc
MD
148{
149 struct timespec ats;
150
151 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
152 return (EINVAL);
153 nanotime(&ats);
154 return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
155}
156
984263bc
MD
157/* ARGSUSED */
158int
41c20dac 159clock_settime(struct clock_settime_args *uap)
984263bc 160{
dadab5e9 161 struct thread *td = curthread;
984263bc
MD
162 struct timeval atv;
163 struct timespec ats;
164 int error;
165
dadab5e9 166 if ((error = suser(td)) != 0)
984263bc
MD
167 return (error);
168 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
169 return (EINVAL);
170 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
171 return (error);
172 if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
173 return (EINVAL);
174 /* XXX Don't convert nsec->usec and back */
175 TIMESPEC_TO_TIMEVAL(&atv, &ats);
176 if ((error = settime(&atv)))
177 return (error);
178 return (0);
179}
180
984263bc 181int
41c20dac 182clock_getres(struct clock_getres_args *uap)
984263bc
MD
183{
184 struct timespec ts;
185 int error;
186
187 if (SCARG(uap, clock_id) != CLOCK_REALTIME)
188 return (EINVAL);
189 error = 0;
190 if (SCARG(uap, tp)) {
191 ts.tv_sec = 0;
192 /*
193 * Round up the result of the division cheaply by adding 1.
194 * Rounding up is especially important if rounding down
195 * would give 0. Perfect rounding is unimportant.
196 */
88c4d2f6 197 ts.tv_nsec = 1000000000 / cputimer_freq + 1;
984263bc
MD
198 error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
199 }
200 return (error);
201}
202
88c4d2f6
MD
203/*
204 * nanosleep1()
205 *
206 * This is a general helper function for nanosleep() (aka sleep() aka
207 * usleep()).
208 *
209 * If there is less then one tick's worth of time left and
210 * we haven't done a yield, or the remaining microseconds is
211 * ridiculously low, do a yield. This avoids having
212 * to deal with systimer overheads when the system is under
213 * heavy loads. If we have done a yield already then use
214 * a systimer and an uninterruptable thread wait.
215 *
216 * If there is more then a tick's worth of time left,
217 * calculate the baseline ticks and use an interruptable
218 * tsleep, then handle the fine-grained delay on the next
219 * loop. This usually results in two sleeps occuring, a long one
220 * and a short one.
221 */
222static void
223ns1_systimer(systimer_t info)
224{
225 lwkt_schedule(info->data);
226}
984263bc
MD
227
228static int
41c20dac 229nanosleep1(struct timespec *rqt, struct timespec *rmt)
984263bc 230{
88c4d2f6 231 static int nanowait;
984263bc
MD
232 struct timespec ts, ts2, ts3;
233 struct timeval tv;
234 int error;
88c4d2f6 235 int tried_yield;
984263bc
MD
236
237 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
238 return (EINVAL);
239 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
240 return (0);
a94976ad
MD
241 nanouptime(&ts);
242 timespecadd(&ts, rqt); /* ts = target timestamp compare */
243 TIMESPEC_TO_TIMEVAL(&tv, rqt); /* tv = sleep interval */
88c4d2f6
MD
244 tried_yield = 0;
245
984263bc 246 for (;;) {
88c4d2f6
MD
247 int ticks;
248 struct systimer info;
249
250 ticks = tv.tv_usec / tick; /* approximate */
a94976ad 251
88c4d2f6 252 if (tv.tv_sec == 0 && ticks == 0) {
37af14fe 253 thread_t td = curthread;
88c4d2f6
MD
254 if (tried_yield || tv.tv_usec < sleep_hard_us) {
255 tried_yield = 0;
a94976ad 256 uio_yield();
a94976ad 257 } else {
37af14fe 258 crit_enter_quick(td);
88c4d2f6 259 systimer_init_oneshot(&info, ns1_systimer,
37af14fe
MD
260 td, tv.tv_usec);
261 lwkt_deschedule_self(td);
262 crit_exit_quick(td);
88c4d2f6
MD
263 lwkt_switch();
264 systimer_del(&info); /* make sure it's gone */
a94976ad 265 }
37af14fe 266 error = iscaught(td->td_proc);
88c4d2f6
MD
267 } else if (tv.tv_sec == 0) {
268 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
a94976ad 269 } else {
88c4d2f6
MD
270 ticks = tvtohz_low(&tv); /* also handles overflow */
271 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
a94976ad
MD
272 }
273 nanouptime(&ts2);
88c4d2f6 274 if (error && error != EWOULDBLOCK) {
984263bc
MD
275 if (error == ERESTART)
276 error = EINTR;
277 if (rmt != NULL) {
278 timespecsub(&ts, &ts2);
279 if (ts.tv_sec < 0)
280 timespecclear(&ts);
281 *rmt = ts;
282 }
283 return (error);
284 }
285 if (timespeccmp(&ts2, &ts, >=))
286 return (0);
287 ts3 = ts;
288 timespecsub(&ts3, &ts2);
289 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
290 }
291}
292
245e4f17 293static void nanosleep_done(void *arg);
df2244e3 294static void nanosleep_copyout(union sysunion *sysun);
245e4f17 295
984263bc
MD
296/* ARGSUSED */
297int
41c20dac 298nanosleep(struct nanosleep_args *uap)
984263bc 299{
245e4f17 300 int error;
df2244e3 301 struct sysmsg_sleep *smsleep = &uap->sysmsg.sm.sleep;
984263bc 302
df2244e3 303 error = copyin(uap->rqtp, &smsleep->rqt, sizeof(smsleep->rqt));
984263bc
MD
304 if (error)
305 return (error);
245e4f17
MD
306 /*
307 * YYY clean this up to always use the callout, note that an abort
308 * implementation should record the residual in the async case.
309 */
df2244e3 310 if (uap->sysmsg.lmsg.ms_flags & MSGF_ASYNC) {
245e4f17
MD
311 quad_t ticks;
312
df2244e3
MD
313 ticks = (quad_t)smsleep->rqt.tv_nsec * hz / 1000000000LL;
314 if (smsleep->rqt.tv_sec)
315 ticks += (quad_t)smsleep->rqt.tv_sec * hz;
245e4f17
MD
316 if (ticks <= 0) {
317 if (ticks == 0)
318 error = 0;
319 else
320 error = EINVAL;
321 } else {
df2244e3 322 uap->sysmsg.copyout = nanosleep_copyout;
1fa2b4b4 323 uap->sysmsg.lmsg.ms_flags &= ~MSGF_DONE;
df2244e3
MD
324 callout_init(&smsleep->timer);
325 callout_reset(&smsleep->timer, ticks, nanosleep_done, uap);
245e4f17
MD
326 error = EASYNC;
327 }
328 } else {
329 /*
330 * Old synchronous sleep code, copyout the residual if
331 * nanosleep was interrupted.
332 */
df2244e3 333 error = nanosleep1(&smsleep->rqt, &smsleep->rmt);
245e4f17 334 if (error && SCARG(uap, rmtp))
df2244e3 335 error = copyout(&smsleep->rmt, SCARG(uap, rmtp), sizeof(smsleep->rmt));
984263bc
MD
336 }
337 return (error);
338}
339
245e4f17
MD
340/*
341 * Asynch completion for the nanosleep() syscall. This function may be
342 * called from any context and cannot legally access the originating
343 * thread, proc, or its user space.
344 *
345 * YYY change the callout interface API so we can simply assign the replymsg
346 * function to it directly.
347 */
348static void
349nanosleep_done(void *arg)
350{
351 struct nanosleep_args *uap = arg;
1fa2b4b4 352 lwkt_msg_t msg = &uap->sysmsg.lmsg;
245e4f17 353
1fa2b4b4 354 lwkt_replymsg(msg, 0);
245e4f17
MD
355}
356
357/*
358 * Asynch return for the nanosleep() syscall, called in the context of the
359 * originating thread when it pulls the message off the reply port. This
360 * function is responsible for any copyouts to userland. Kernel threads
361 * which do their own internal system calls will not usually call the return
362 * function.
363 */
364static void
df2244e3 365nanosleep_copyout(union sysunion *sysun)
245e4f17 366{
df2244e3
MD
367 struct nanosleep_args *uap = &sysun->nanosleep;
368 struct sysmsg_sleep *smsleep = &uap->sysmsg.sm.sleep;
245e4f17 369
df2244e3
MD
370 if (sysun->lmsg.ms_error && uap->rmtp) {
371 sysun->lmsg.ms_error =
372 copyout(&smsleep->rmt, uap->rmtp, sizeof(smsleep->rmt));
245e4f17
MD
373 }
374}
375
984263bc
MD
376/* ARGSUSED */
377int
41c20dac 378gettimeofday(struct gettimeofday_args *uap)
984263bc
MD
379{
380 struct timeval atv;
381 int error = 0;
382
383 if (uap->tp) {
384 microtime(&atv);
385 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
386 sizeof (atv))))
387 return (error);
388 }
389 if (uap->tzp)
390 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
391 sizeof (tz));
392 return (error);
393}
394
984263bc
MD
395/* ARGSUSED */
396int
41c20dac 397settimeofday(struct settimeofday_args *uap)
984263bc 398{
dadab5e9 399 struct thread *td = curthread;
984263bc
MD
400 struct timeval atv;
401 struct timezone atz;
402 int error;
403
dadab5e9 404 if ((error = suser(td)))
984263bc
MD
405 return (error);
406 /* Verify all parameters before changing time. */
407 if (uap->tv) {
408 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
409 sizeof(atv))))
410 return (error);
411 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
412 return (EINVAL);
413 }
414 if (uap->tzp &&
415 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
416 return (error);
417 if (uap->tv && (error = settime(&atv)))
418 return (error);
419 if (uap->tzp)
420 tz = atz;
421 return (0);
422}
423
424int tickdelta; /* current clock skew, us. per tick */
425long timedelta; /* unapplied time correction, us. */
426static long bigadj = 1000000; /* use 10x skew above bigadj us. */
427
984263bc
MD
428/* ARGSUSED */
429int
41c20dac 430adjtime(struct adjtime_args *uap)
984263bc 431{
dadab5e9 432 struct thread *td = curthread;
984263bc 433 struct timeval atv;
dadab5e9 434 long ndelta, ntickdelta, odelta;
88c4d2f6 435 int error;
984263bc 436
dadab5e9 437 if ((error = suser(td)))
984263bc
MD
438 return (error);
439 if ((error =
440 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
441 return (error);
442
443 /*
444 * Compute the total correction and the rate at which to apply it.
445 * Round the adjustment down to a whole multiple of the per-tick
446 * delta, so that after some number of incremental changes in
447 * hardclock(), tickdelta will become zero, lest the correction
448 * overshoot and start taking us away from the desired final time.
449 */
450 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
451 if (ndelta > bigadj || ndelta < -bigadj)
452 ntickdelta = 10 * tickadj;
453 else
454 ntickdelta = tickadj;
455 if (ndelta % ntickdelta)
456 ndelta = ndelta / ntickdelta * ntickdelta;
457
458 /*
459 * To make hardclock()'s job easier, make the per-tick delta negative
460 * if we want time to run slower; then hardclock can simply compute
461 * tick + tickdelta, and subtract tickdelta from timedelta.
462 */
463 if (ndelta < 0)
464 ntickdelta = -ntickdelta;
88c4d2f6
MD
465 /*
466 * XXX not MP safe , but will probably work anyway.
467 */
468 crit_enter();
984263bc
MD
469 odelta = timedelta;
470 timedelta = ndelta;
471 tickdelta = ntickdelta;
88c4d2f6 472 crit_exit();
984263bc
MD
473
474 if (uap->olddelta) {
475 atv.tv_sec = odelta / 1000000;
476 atv.tv_usec = odelta % 1000000;
477 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
478 sizeof(struct timeval));
479 }
480 return (0);
481}
482
483/*
484 * Get value of an interval timer. The process virtual and
485 * profiling virtual time timers are kept in the p_stats area, since
486 * they can be swapped out. These are kept internally in the
487 * way they are specified externally: in time until they expire.
488 *
489 * The real time interval timer is kept in the process table slot
490 * for the process, and its value (it_value) is kept as an
491 * absolute time rather than as a delta, so that it is easy to keep
492 * periodic real-time signals from drifting.
493 *
494 * Virtual time timers are processed in the hardclock() routine of
495 * kern_clock.c. The real time timer is processed by a timeout
496 * routine, called from the softclock() routine. Since a callout
497 * may be delayed in real time due to interrupt processing in the system,
498 * it is possible for the real time timeout routine (realitexpire, given below),
499 * to be delayed in real time past when it is supposed to occur. It
500 * does not suffice, therefore, to reload the real timer .it_value from the
501 * real time timers .it_interval. Rather, we compute the next time in
502 * absolute time the timer should go off.
503 */
984263bc
MD
504/* ARGSUSED */
505int
41c20dac 506getitimer(struct getitimer_args *uap)
984263bc 507{
41c20dac 508 struct proc *p = curproc;
984263bc
MD
509 struct timeval ctv;
510 struct itimerval aitv;
984263bc
MD
511
512 if (uap->which > ITIMER_PROF)
513 return (EINVAL);
88c4d2f6 514 crit_enter();
984263bc
MD
515 if (uap->which == ITIMER_REAL) {
516 /*
517 * Convert from absolute to relative time in .it_value
518 * part of real time timer. If time for real time timer
519 * has passed return 0, else return difference between
520 * current time and time for the timer to go off.
521 */
522 aitv = p->p_realtimer;
523 if (timevalisset(&aitv.it_value)) {
524 getmicrouptime(&ctv);
525 if (timevalcmp(&aitv.it_value, &ctv, <))
526 timevalclear(&aitv.it_value);
527 else
528 timevalsub(&aitv.it_value, &ctv);
529 }
88c4d2f6 530 } else {
984263bc 531 aitv = p->p_stats->p_timer[uap->which];
88c4d2f6
MD
532 }
533 crit_exit();
984263bc
MD
534 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
535 sizeof (struct itimerval)));
536}
537
984263bc
MD
538/* ARGSUSED */
539int
41c20dac 540setitimer(struct setitimer_args *uap)
984263bc
MD
541{
542 struct itimerval aitv;
543 struct timeval ctv;
41c20dac
MD
544 struct itimerval *itvp;
545 struct proc *p = curproc;
88c4d2f6 546 int error;
984263bc
MD
547
548 if (uap->which > ITIMER_PROF)
549 return (EINVAL);
550 itvp = uap->itv;
551 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
552 sizeof(struct itimerval))))
553 return (error);
554 if ((uap->itv = uap->oitv) &&
41c20dac 555 (error = getitimer((struct getitimer_args *)uap)))
984263bc
MD
556 return (error);
557 if (itvp == 0)
558 return (0);
559 if (itimerfix(&aitv.it_value))
560 return (EINVAL);
561 if (!timevalisset(&aitv.it_value))
562 timevalclear(&aitv.it_interval);
563 else if (itimerfix(&aitv.it_interval))
564 return (EINVAL);
88c4d2f6 565 crit_enter();
984263bc
MD
566 if (uap->which == ITIMER_REAL) {
567 if (timevalisset(&p->p_realtimer.it_value))
568 untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
569 if (timevalisset(&aitv.it_value))
570 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
a94976ad 571 tvtohz_high(&aitv.it_value));
984263bc
MD
572 getmicrouptime(&ctv);
573 timevaladd(&aitv.it_value, &ctv);
574 p->p_realtimer = aitv;
88c4d2f6 575 } else {
984263bc 576 p->p_stats->p_timer[uap->which] = aitv;
88c4d2f6
MD
577 }
578 crit_exit();
984263bc
MD
579 return (0);
580}
581
582/*
583 * Real interval timer expired:
584 * send process whose timer expired an alarm signal.
585 * If time is not set up to reload, then just return.
586 * Else compute next time timer should go off which is > current time.
587 * This is where delay in processing this timeout causes multiple
588 * SIGALRM calls to be compressed into one.
a94976ad 589 * tvtohz_high() always adds 1 to allow for the time until the next clock
984263bc
MD
590 * interrupt being strictly less than 1 clock tick, but we don't want
591 * that here since we want to appear to be in sync with the clock
592 * interrupt even when we're delayed.
593 */
594void
595realitexpire(arg)
596 void *arg;
597{
1fd87d54 598 struct proc *p;
984263bc 599 struct timeval ctv, ntv;
984263bc
MD
600
601 p = (struct proc *)arg;
602 psignal(p, SIGALRM);
603 if (!timevalisset(&p->p_realtimer.it_interval)) {
604 timevalclear(&p->p_realtimer.it_value);
605 return;
606 }
607 for (;;) {
88c4d2f6 608 crit_enter();
984263bc
MD
609 timevaladd(&p->p_realtimer.it_value,
610 &p->p_realtimer.it_interval);
611 getmicrouptime(&ctv);
612 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
613 ntv = p->p_realtimer.it_value;
614 timevalsub(&ntv, &ctv);
615 p->p_ithandle = timeout(realitexpire, (caddr_t)p,
a94976ad 616 tvtohz_low(&ntv));
88c4d2f6 617 crit_exit();
984263bc
MD
618 return;
619 }
88c4d2f6 620 crit_exit();
984263bc
MD
621 }
622}
623
624/*
625 * Check that a proposed value to load into the .it_value or
626 * .it_interval part of an interval timer is acceptable, and
627 * fix it to have at least minimal value (i.e. if it is less
628 * than the resolution of the clock, round it up.)
629 */
630int
631itimerfix(tv)
632 struct timeval *tv;
633{
634
635 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
636 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
637 return (EINVAL);
638 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
639 tv->tv_usec = tick;
640 return (0);
641}
642
643/*
644 * Decrement an interval timer by a specified number
645 * of microseconds, which must be less than a second,
646 * i.e. < 1000000. If the timer expires, then reload
647 * it. In this case, carry over (usec - old value) to
648 * reduce the value reloaded into the timer so that
649 * the timer does not drift. This routine assumes
650 * that it is called in a context where the timers
651 * on which it is operating cannot change in value.
652 */
653int
654itimerdecr(itp, usec)
1fd87d54 655 struct itimerval *itp;
984263bc
MD
656 int usec;
657{
658
659 if (itp->it_value.tv_usec < usec) {
660 if (itp->it_value.tv_sec == 0) {
661 /* expired, and already in next interval */
662 usec -= itp->it_value.tv_usec;
663 goto expire;
664 }
665 itp->it_value.tv_usec += 1000000;
666 itp->it_value.tv_sec--;
667 }
668 itp->it_value.tv_usec -= usec;
669 usec = 0;
670 if (timevalisset(&itp->it_value))
671 return (1);
672 /* expired, exactly at end of interval */
673expire:
674 if (timevalisset(&itp->it_interval)) {
675 itp->it_value = itp->it_interval;
676 itp->it_value.tv_usec -= usec;
677 if (itp->it_value.tv_usec < 0) {
678 itp->it_value.tv_usec += 1000000;
679 itp->it_value.tv_sec--;
680 }
681 } else
682 itp->it_value.tv_usec = 0; /* sec is already 0 */
683 return (0);
684}
685
686/*
687 * Add and subtract routines for timevals.
688 * N.B.: subtract routine doesn't deal with
689 * results which are before the beginning,
690 * it just gets very confused in this case.
691 * Caveat emptor.
692 */
693void
694timevaladd(t1, t2)
695 struct timeval *t1, *t2;
696{
697
698 t1->tv_sec += t2->tv_sec;
699 t1->tv_usec += t2->tv_usec;
700 timevalfix(t1);
701}
702
703void
704timevalsub(t1, t2)
705 struct timeval *t1, *t2;
706{
707
708 t1->tv_sec -= t2->tv_sec;
709 t1->tv_usec -= t2->tv_usec;
710 timevalfix(t1);
711}
712
713static void
714timevalfix(t1)
715 struct timeval *t1;
716{
717
718 if (t1->tv_usec < 0) {
719 t1->tv_sec--;
720 t1->tv_usec += 1000000;
721 }
722 if (t1->tv_usec >= 1000000) {
723 t1->tv_sec++;
724 t1->tv_usec -= 1000000;
725 }
726}
cea4446f
HP
727
728/*
729 * ratecheck(): simple time-based rate-limit checking.
730 */
731int
732ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
733{
734 struct timeval tv, delta;
735 int rv = 0;
736
737 getmicrouptime(&tv); /* NB: 10ms precision */
738 delta = tv;
739 timevalsub(&delta, lasttime);
740
741 /*
742 * check for 0,0 is so that the message will be seen at least once,
743 * even if interval is huge.
744 */
745 if (timevalcmp(&delta, mininterval, >=) ||
746 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
747 *lasttime = tv;
748 rv = 1;
749 }
750
751 return (rv);
752}
753
754/*
755 * ppsratecheck(): packets (or events) per second limitation.
756 *
757 * Return 0 if the limit is to be enforced (e.g. the caller
758 * should drop a packet because of the rate limitation).
759 *
760 * maxpps of 0 always causes zero to be returned. maxpps of -1
761 * always causes 1 to be returned; this effectively defeats rate
762 * limiting.
763 *
764 * Note that we maintain the struct timeval for compatibility
765 * with other bsd systems. We reuse the storage and just monitor
766 * clock ticks for minimal overhead.
767 */
768int
769ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
770{
771 int now;
772
773 /*
774 * Reset the last time and counter if this is the first call
775 * or more than a second has passed since the last update of
776 * lasttime.
777 */
778 now = ticks;
779 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
780 lasttime->tv_sec = now;
781 *curpps = 1;
782 return (maxpps != 0);
783 } else {
784 (*curpps)++; /* NB: ignore potential overflow */
785 return (maxpps < 0 || *curpps < maxpps);
786 }
787}
788