Annotation of src/sys/kern/kern_time.c, Revision 1.62
1.62 ! simonb 1: /* $NetBSD: kern_time.c,v 1.61 2002/01/31 00:13:08 simonb Exp $ */
1.42 cgd 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: */
1.9 cgd 38:
1.1 cgd 39: /*
1.8 cgd 40: * Copyright (c) 1982, 1986, 1989, 1993
41: * The Regents of the University of California. All rights reserved.
1.1 cgd 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: *
1.33 fvdl 71: * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
1.1 cgd 72: */
1.58 lukem 73:
74: #include <sys/cdefs.h>
1.62 ! simonb 75: __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.61 2002/01/31 00:13:08 simonb Exp $");
1.31 thorpej 76:
77: #include "fs_nfs.h"
1.54 bjh21 78: #include "opt_nfs.h"
1.34 thorpej 79: #include "opt_nfsserver.h"
1.1 cgd 80:
1.5 mycroft 81: #include <sys/param.h>
82: #include <sys/resourcevar.h>
83: #include <sys/kernel.h>
1.8 cgd 84: #include <sys/systm.h>
1.5 mycroft 85: #include <sys/proc.h>
1.8 cgd 86: #include <sys/vnode.h>
1.17 christos 87: #include <sys/signalvar.h>
1.25 perry 88: #include <sys/syslog.h>
1.1 cgd 89:
1.11 cgd 90: #include <sys/mount.h>
91: #include <sys/syscallargs.h>
1.19 christos 92:
1.37 thorpej 93: #include <uvm/uvm_extern.h>
94:
1.26 thorpej 95: #if defined(NFS) || defined(NFSSERVER)
1.20 fvdl 96: #include <nfs/rpcv2.h>
97: #include <nfs/nfsproto.h>
1.19 christos 98: #include <nfs/nfs_var.h>
99: #endif
1.17 christos 100:
1.5 mycroft 101: #include <machine/cpu.h>
1.23 cgd 102:
103: /*
1.1 cgd 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:
1.22 jtc 113: /* This function is used by clock_settime and settimeofday */
1.39 tron 114: int
1.22 jtc 115: settime(tv)
116: struct timeval *tv;
117: {
118: struct timeval delta;
1.47 thorpej 119: struct cpu_info *ci;
1.22 jtc 120: int s;
121:
122: /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
123: s = splclock();
124: timersub(tv, &time, &delta);
1.55 tron 125: if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1) {
126: splx(s);
1.29 tls 127: return (EPERM);
1.55 tron 128: }
1.29 tls 129: #ifdef notyet
1.55 tron 130: if ((delta.tv_sec < 86400) && securelevel > 0) {
131: splx(s);
1.29 tls 132: return (EPERM);
1.55 tron 133: }
1.29 tls 134: #endif
1.22 jtc 135: time = *tv;
1.38 thorpej 136: (void) spllowersoftclock();
1.22 jtc 137: timeradd(&boottime, &delta, &boottime);
1.47 thorpej 138: /*
139: * XXXSMP
140: * This is wrong. We should traverse a list of all
141: * CPUs and add the delta to the runtime of those
142: * CPUs which have a process on them.
143: */
144: ci = curcpu();
145: timeradd(&ci->ci_schedstate.spc_runtime, &delta,
146: &ci->ci_schedstate.spc_runtime);
1.54 bjh21 147: # if (defined(NFS) && !defined (NFS_V2_ONLY)) || defined(NFSSERVER)
1.22 jtc 148: nqnfs_lease_updatetime(delta.tv_sec);
149: # endif
150: splx(s);
151: resettodr();
1.29 tls 152: return (0);
1.22 jtc 153: }
154:
155: /* ARGSUSED */
156: int
157: sys_clock_gettime(p, v, retval)
158: struct proc *p;
159: void *v;
160: register_t *retval;
161: {
1.45 augustss 162: struct sys_clock_gettime_args /* {
1.22 jtc 163: syscallarg(clockid_t) clock_id;
1.23 cgd 164: syscallarg(struct timespec *) tp;
165: } */ *uap = v;
1.22 jtc 166: clockid_t clock_id;
167: struct timeval atv;
168: struct timespec ats;
1.61 simonb 169: int s;
1.22 jtc 170:
171: clock_id = SCARG(uap, clock_id);
1.61 simonb 172: switch (clock_id) {
173: case CLOCK_REALTIME:
174: microtime(&atv);
175: TIMEVAL_TO_TIMESPEC(&atv,&ats);
176: break;
177: case CLOCK_MONOTONIC:
178: /* XXX "hz" granularity */
179: s = splclock();
180: atv = mono_time;
181: splx(s);
182: TIMEVAL_TO_TIMESPEC(&atv,&ats);
183: break;
184: default:
1.22 jtc 185: return (EINVAL);
1.61 simonb 186: }
1.22 jtc 187:
1.24 cgd 188: return copyout(&ats, SCARG(uap, tp), sizeof(ats));
1.22 jtc 189: }
190:
191: /* ARGSUSED */
192: int
193: sys_clock_settime(p, v, retval)
194: struct proc *p;
195: void *v;
196: register_t *retval;
197: {
1.45 augustss 198: struct sys_clock_settime_args /* {
1.22 jtc 199: syscallarg(clockid_t) clock_id;
1.23 cgd 200: syscallarg(const struct timespec *) tp;
201: } */ *uap = v;
1.22 jtc 202: int error;
203:
204: if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
205: return (error);
206:
1.60 manu 207: return (clock_settime1(SCARG(uap, clock_id), SCARG(uap, tp)));
1.56 manu 208: }
209:
210:
211: int
1.60 manu 212: clock_settime1(clock_id, tp)
1.56 manu 213: clockid_t clock_id;
1.60 manu 214: const struct timespec *tp;
1.56 manu 215: {
1.60 manu 216: struct timespec ats;
1.56 manu 217: struct timeval atv;
218: int error;
219:
1.60 manu 220: if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
221: return (error);
222:
1.61 simonb 223: switch (clock_id) {
224: case CLOCK_REALTIME:
225: TIMESPEC_TO_TIMEVAL(&atv, &ats);
226: if ((error = settime(&atv)) != 0)
227: return (error);
228: break;
229: case CLOCK_MONOTONIC:
230: return (EINVAL); /* read-only clock */
231: default:
1.56 manu 232: return (EINVAL);
1.61 simonb 233: }
1.22 jtc 234:
235: return 0;
236: }
237:
238: int
239: sys_clock_getres(p, v, retval)
240: struct proc *p;
241: void *v;
242: register_t *retval;
243: {
1.45 augustss 244: struct sys_clock_getres_args /* {
1.22 jtc 245: syscallarg(clockid_t) clock_id;
1.23 cgd 246: syscallarg(struct timespec *) tp;
247: } */ *uap = v;
1.22 jtc 248: clockid_t clock_id;
249: struct timespec ts;
250: int error = 0;
251:
252: clock_id = SCARG(uap, clock_id);
1.61 simonb 253: switch (clock_id) {
254: case CLOCK_REALTIME:
255: case CLOCK_MONOTONIC:
1.22 jtc 256: ts.tv_sec = 0;
257: ts.tv_nsec = 1000000000 / hz;
1.61 simonb 258: break;
259: default:
260: return (EINVAL);
261: }
1.22 jtc 262:
1.61 simonb 263: if (SCARG(uap, tp))
1.35 perry 264: error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
1.22 jtc 265:
266: return error;
267: }
268:
1.27 jtc 269: /* ARGSUSED */
270: int
271: sys_nanosleep(p, v, retval)
272: struct proc *p;
273: void *v;
274: register_t *retval;
275: {
276: static int nanowait;
1.45 augustss 277: struct sys_nanosleep_args/* {
1.27 jtc 278: syscallarg(struct timespec *) rqtp;
279: syscallarg(struct timespec *) rmtp;
280: } */ *uap = v;
281: struct timespec rqt;
282: struct timespec rmt;
283: struct timeval atv, utv;
284: int error, s, timo;
285:
286: error = copyin((caddr_t)SCARG(uap, rqtp), (caddr_t)&rqt,
287: sizeof(struct timespec));
288: if (error)
289: return (error);
290:
291: TIMESPEC_TO_TIMEVAL(&atv,&rqt)
1.59 christos 292: if (itimerfix(&atv) || atv.tv_sec > 1000000000)
1.27 jtc 293: return (EINVAL);
294:
295: s = splclock();
296: timeradd(&atv,&time,&atv);
297: timo = hzto(&atv);
298: /*
299: * Avoid inadvertantly sleeping forever
300: */
301: if (timo == 0)
302: timo = 1;
303: splx(s);
304:
305: error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
306: if (error == ERESTART)
307: error = EINTR;
308: if (error == EWOULDBLOCK)
309: error = 0;
310:
311: if (SCARG(uap, rmtp)) {
1.28 jtc 312: int error;
313:
1.27 jtc 314: s = splclock();
315: utv = time;
316: splx(s);
317:
318: timersub(&atv, &utv, &utv);
319: if (utv.tv_sec < 0)
320: timerclear(&utv);
321:
322: TIMEVAL_TO_TIMESPEC(&utv,&rmt);
323: error = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
1.28 jtc 324: sizeof(rmt));
325: if (error)
326: return (error);
1.27 jtc 327: }
328:
329: return error;
330: }
1.22 jtc 331:
1.1 cgd 332: /* ARGSUSED */
1.3 andrew 333: int
1.16 mycroft 334: sys_gettimeofday(p, v, retval)
1.1 cgd 335: struct proc *p;
1.15 thorpej 336: void *v;
337: register_t *retval;
338: {
1.45 augustss 339: struct sys_gettimeofday_args /* {
1.11 cgd 340: syscallarg(struct timeval *) tp;
341: syscallarg(struct timezone *) tzp;
1.15 thorpej 342: } */ *uap = v;
1.1 cgd 343: struct timeval atv;
344: int error = 0;
1.25 perry 345: struct timezone tzfake;
1.1 cgd 346:
1.11 cgd 347: if (SCARG(uap, tp)) {
1.1 cgd 348: microtime(&atv);
1.35 perry 349: error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
1.17 christos 350: if (error)
1.1 cgd 351: return (error);
352: }
1.25 perry 353: if (SCARG(uap, tzp)) {
354: /*
1.32 mycroft 355: * NetBSD has no kernel notion of time zone, so we just
1.25 perry 356: * fake up a timezone struct and return it if demanded.
357: */
358: tzfake.tz_minuteswest = 0;
359: tzfake.tz_dsttime = 0;
1.35 perry 360: error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
1.25 perry 361: }
1.1 cgd 362: return (error);
363: }
364:
365: /* ARGSUSED */
1.3 andrew 366: int
1.16 mycroft 367: sys_settimeofday(p, v, retval)
1.1 cgd 368: struct proc *p;
1.15 thorpej 369: void *v;
370: register_t *retval;
371: {
1.16 mycroft 372: struct sys_settimeofday_args /* {
1.24 cgd 373: syscallarg(const struct timeval *) tv;
374: syscallarg(const struct timezone *) tzp;
1.15 thorpej 375: } */ *uap = v;
1.60 manu 376: int error;
377:
378: if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
379: return (error);
380:
381: return settimeofday1(SCARG(uap, tv), SCARG(uap, tzp), p);
382: }
383:
384: int
385: settimeofday1(utv, utzp, p)
386: const struct timeval *utv;
387: const struct timezone *utzp;
388: struct proc *p;
389: {
1.22 jtc 390: struct timeval atv;
1.1 cgd 391: struct timezone atz;
1.56 manu 392: struct timeval *tv = NULL;
393: struct timezone *tzp = NULL;
1.22 jtc 394: int error;
1.1 cgd 395:
1.8 cgd 396: /* Verify all parameters before changing time. */
1.60 manu 397: if (utv) {
398: if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
1.56 manu 399: return (error);
400: tv = &atv;
401: }
1.25 perry 402: /* XXX since we don't use tz, probably no point in doing copyin. */
1.60 manu 403: if (utzp) {
404: if ((error = copyin(utzp, &atz, sizeof(atz))) != 0)
1.56 manu 405: return (error);
406: tzp = &atz;
407: }
408:
409: if (tv)
410: if ((error = settime(tv)) != 0)
1.29 tls 411: return (error);
1.25 perry 412: /*
1.32 mycroft 413: * NetBSD has no kernel notion of time zone, and only an
1.25 perry 414: * obsolete program would try to set it, so we log a warning.
415: */
1.56 manu 416: if (tzp)
1.25 perry 417: log(LOG_WARNING, "pid %d attempted to set the "
1.32 mycroft 418: "(obsolete) kernel time zone\n", p->p_pid);
1.8 cgd 419: return (0);
1.1 cgd 420: }
421:
422: int tickdelta; /* current clock skew, us. per tick */
423: long timedelta; /* unapplied time correction, us. */
424: long bigadj = 1000000; /* use 10x skew above bigadj us. */
425:
426: /* ARGSUSED */
1.3 andrew 427: int
1.16 mycroft 428: sys_adjtime(p, v, retval)
1.1 cgd 429: struct proc *p;
1.15 thorpej 430: void *v;
431: register_t *retval;
432: {
1.45 augustss 433: struct sys_adjtime_args /* {
1.24 cgd 434: syscallarg(const struct timeval *) delta;
1.11 cgd 435: syscallarg(struct timeval *) olddelta;
1.15 thorpej 436: } */ *uap = v;
1.56 manu 437: int error;
1.1 cgd 438:
1.17 christos 439: if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1.1 cgd 440: return (error);
1.17 christos 441:
1.60 manu 442: return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), p);
1.56 manu 443: }
444:
445: int
446: adjtime1(delta, olddelta, p)
1.60 manu 447: const struct timeval *delta;
1.56 manu 448: struct timeval *olddelta;
449: struct proc *p;
450: {
1.60 manu 451: struct timeval atv;
1.56 manu 452: long ndelta, ntickdelta, odelta;
1.60 manu 453: int error;
1.56 manu 454: int s;
1.8 cgd 455:
1.60 manu 456: error = copyin(delta, &atv, sizeof(struct timeval));
457: if (error)
458: return (error);
459:
460: if (olddelta != NULL) {
461: if (uvm_useracc((caddr_t)olddelta,
462: sizeof(struct timeval), B_WRITE) == FALSE)
463: return (EFAULT);
464: }
465:
1.8 cgd 466: /*
467: * Compute the total correction and the rate at which to apply it.
468: * Round the adjustment down to a whole multiple of the per-tick
469: * delta, so that after some number of incremental changes in
470: * hardclock(), tickdelta will become zero, lest the correction
471: * overshoot and start taking us away from the desired final time.
472: */
1.60 manu 473: ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
1.41 hwr 474: if (ndelta > bigadj || ndelta < -bigadj)
1.8 cgd 475: ntickdelta = 10 * tickadj;
476: else
477: ntickdelta = tickadj;
478: if (ndelta % ntickdelta)
479: ndelta = ndelta / ntickdelta * ntickdelta;
480:
481: /*
482: * To make hardclock()'s job easier, make the per-tick delta negative
483: * if we want time to run slower; then hardclock can simply compute
484: * tick + tickdelta, and subtract tickdelta from timedelta.
485: */
486: if (ndelta < 0)
487: ntickdelta = -ntickdelta;
1.1 cgd 488: s = splclock();
1.8 cgd 489: odelta = timedelta;
1.1 cgd 490: timedelta = ndelta;
1.8 cgd 491: tickdelta = ntickdelta;
1.1 cgd 492: splx(s);
493:
1.56 manu 494: if (olddelta) {
1.60 manu 495: atv.tv_sec = odelta / 1000000;
496: atv.tv_usec = odelta % 1000000;
497: (void) copyout(&atv, olddelta, sizeof(struct timeval));
1.8 cgd 498: }
1.1 cgd 499: return (0);
500: }
501:
502: /*
503: * Get value of an interval timer. The process virtual and
504: * profiling virtual time timers are kept in the p_stats area, since
505: * they can be swapped out. These are kept internally in the
506: * way they are specified externally: in time until they expire.
507: *
508: * The real time interval timer is kept in the process table slot
509: * for the process, and its value (it_value) is kept as an
510: * absolute time rather than as a delta, so that it is easy to keep
511: * periodic real-time signals from drifting.
512: *
513: * Virtual time timers are processed in the hardclock() routine of
514: * kern_clock.c. The real time timer is processed by a timeout
515: * routine, called from the softclock() routine. Since a callout
516: * may be delayed in real time due to interrupt processing in the system,
517: * it is possible for the real time timeout routine (realitexpire, given below),
518: * to be delayed in real time past when it is supposed to occur. It
519: * does not suffice, therefore, to reload the real timer .it_value from the
520: * real time timers .it_interval. Rather, we compute the next time in
521: * absolute time the timer should go off.
522: */
523: /* ARGSUSED */
1.3 andrew 524: int
1.16 mycroft 525: sys_getitimer(p, v, retval)
1.1 cgd 526: struct proc *p;
1.15 thorpej 527: void *v;
528: register_t *retval;
529: {
1.45 augustss 530: struct sys_getitimer_args /* {
1.30 mycroft 531: syscallarg(int) which;
1.11 cgd 532: syscallarg(struct itimerval *) itv;
1.15 thorpej 533: } */ *uap = v;
1.30 mycroft 534: int which = SCARG(uap, which);
1.1 cgd 535: struct itimerval aitv;
536: int s;
537:
1.30 mycroft 538: if ((u_int)which > ITIMER_PROF)
1.1 cgd 539: return (EINVAL);
540: s = splclock();
1.30 mycroft 541: if (which == ITIMER_REAL) {
1.1 cgd 542: /*
1.12 mycroft 543: * Convert from absolute to relative time in .it_value
1.1 cgd 544: * part of real time timer. If time for real time timer
545: * has passed return 0, else return difference between
546: * current time and time for the timer to go off.
547: */
548: aitv = p->p_realtimer;
1.36 thorpej 549: if (timerisset(&aitv.it_value)) {
1.1 cgd 550: if (timercmp(&aitv.it_value, &time, <))
551: timerclear(&aitv.it_value);
552: else
1.14 mycroft 553: timersub(&aitv.it_value, &time, &aitv.it_value);
1.36 thorpej 554: }
1.1 cgd 555: } else
1.30 mycroft 556: aitv = p->p_stats->p_timer[which];
1.1 cgd 557: splx(s);
1.35 perry 558: return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
1.1 cgd 559: }
560:
561: /* ARGSUSED */
1.3 andrew 562: int
1.16 mycroft 563: sys_setitimer(p, v, retval)
1.1 cgd 564: struct proc *p;
1.45 augustss 565: void *v;
1.15 thorpej 566: register_t *retval;
567: {
1.45 augustss 568: struct sys_setitimer_args /* {
1.30 mycroft 569: syscallarg(int) which;
1.24 cgd 570: syscallarg(const struct itimerval *) itv;
1.11 cgd 571: syscallarg(struct itimerval *) oitv;
1.15 thorpej 572: } */ *uap = v;
1.30 mycroft 573: int which = SCARG(uap, which);
1.21 cgd 574: struct sys_getitimer_args getargs;
1.1 cgd 575: struct itimerval aitv;
1.45 augustss 576: const struct itimerval *itvp;
1.1 cgd 577: int s, error;
578:
1.30 mycroft 579: if ((u_int)which > ITIMER_PROF)
1.1 cgd 580: return (EINVAL);
1.11 cgd 581: itvp = SCARG(uap, itv);
1.56 manu 582: if (itvp &&
583: (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
1.1 cgd 584: return (error);
1.21 cgd 585: if (SCARG(uap, oitv) != NULL) {
1.30 mycroft 586: SCARG(&getargs, which) = which;
1.21 cgd 587: SCARG(&getargs, itv) = SCARG(uap, oitv);
1.23 cgd 588: if ((error = sys_getitimer(p, &getargs, retval)) != 0)
1.21 cgd 589: return (error);
590: }
1.1 cgd 591: if (itvp == 0)
592: return (0);
593: if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
594: return (EINVAL);
595: s = splclock();
1.30 mycroft 596: if (which == ITIMER_REAL) {
1.44 thorpej 597: callout_stop(&p->p_realit_ch);
1.1 cgd 598: if (timerisset(&aitv.it_value)) {
1.52 thorpej 599: /*
600: * Don't need to check hzto() return value, here.
601: * callout_reset() does it for us.
602: */
1.14 mycroft 603: timeradd(&aitv.it_value, &time, &aitv.it_value);
1.44 thorpej 604: callout_reset(&p->p_realit_ch, hzto(&aitv.it_value),
605: realitexpire, p);
1.1 cgd 606: }
607: p->p_realtimer = aitv;
608: } else
1.30 mycroft 609: p->p_stats->p_timer[which] = aitv;
1.1 cgd 610: splx(s);
611: return (0);
612: }
613:
614: /*
615: * Real interval timer expired:
616: * send process whose timer expired an alarm signal.
617: * If time is not set up to reload, then just return.
618: * Else compute next time timer should go off which is > current time.
619: * This is where delay in processing this timeout causes multiple
620: * SIGALRM calls to be compressed into one.
621: */
1.3 andrew 622: void
1.6 cgd 623: realitexpire(arg)
624: void *arg;
625: {
1.45 augustss 626: struct proc *p;
1.1 cgd 627: int s;
628:
1.6 cgd 629: p = (struct proc *)arg;
1.1 cgd 630: psignal(p, SIGALRM);
631: if (!timerisset(&p->p_realtimer.it_interval)) {
632: timerclear(&p->p_realtimer.it_value);
633: return;
634: }
635: for (;;) {
636: s = splclock();
1.14 mycroft 637: timeradd(&p->p_realtimer.it_value,
638: &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
1.1 cgd 639: if (timercmp(&p->p_realtimer.it_value, &time, >)) {
1.52 thorpej 640: /*
641: * Don't need to check hzto() return value, here.
642: * callout_reset() does it for us.
643: */
1.44 thorpej 644: callout_reset(&p->p_realit_ch,
645: hzto(&p->p_realtimer.it_value), realitexpire, p);
1.1 cgd 646: splx(s);
647: return;
648: }
649: splx(s);
650: }
651: }
652:
653: /*
654: * Check that a proposed value to load into the .it_value or
655: * .it_interval part of an interval timer is acceptable, and
656: * fix it to have at least minimal value (i.e. if it is less
657: * than the resolution of the clock, round it up.)
658: */
1.3 andrew 659: int
1.1 cgd 660: itimerfix(tv)
661: struct timeval *tv;
662: {
663:
1.59 christos 664: if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
1.1 cgd 665: return (EINVAL);
666: if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
667: tv->tv_usec = tick;
668: return (0);
669: }
670:
671: /*
672: * Decrement an interval timer by a specified number
673: * of microseconds, which must be less than a second,
674: * i.e. < 1000000. If the timer expires, then reload
675: * it. In this case, carry over (usec - old value) to
1.8 cgd 676: * reduce the value reloaded into the timer so that
1.1 cgd 677: * the timer does not drift. This routine assumes
678: * that it is called in a context where the timers
679: * on which it is operating cannot change in value.
680: */
1.3 andrew 681: int
1.1 cgd 682: itimerdecr(itp, usec)
1.45 augustss 683: struct itimerval *itp;
1.1 cgd 684: int usec;
685: {
686:
687: if (itp->it_value.tv_usec < usec) {
688: if (itp->it_value.tv_sec == 0) {
689: /* expired, and already in next interval */
690: usec -= itp->it_value.tv_usec;
691: goto expire;
692: }
693: itp->it_value.tv_usec += 1000000;
694: itp->it_value.tv_sec--;
695: }
696: itp->it_value.tv_usec -= usec;
697: usec = 0;
698: if (timerisset(&itp->it_value))
699: return (1);
700: /* expired, exactly at end of interval */
701: expire:
702: if (timerisset(&itp->it_interval)) {
703: itp->it_value = itp->it_interval;
704: itp->it_value.tv_usec -= usec;
705: if (itp->it_value.tv_usec < 0) {
706: itp->it_value.tv_usec += 1000000;
707: itp->it_value.tv_sec--;
708: }
709: } else
710: itp->it_value.tv_usec = 0; /* sec is already 0 */
711: return (0);
1.42 cgd 712: }
713:
714: /*
715: * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
716: * for usage and rationale.
717: */
718: int
719: ratecheck(lasttime, mininterval)
720: struct timeval *lasttime;
721: const struct timeval *mininterval;
722: {
1.49 itojun 723: struct timeval tv, delta;
1.42 cgd 724: int s, rv = 0;
725:
726: s = splclock();
1.49 itojun 727: tv = mono_time;
728: splx(s);
729:
730: timersub(&tv, lasttime, &delta);
1.42 cgd 731:
732: /*
733: * check for 0,0 is so that the message will be seen at least once,
734: * even if interval is huge.
735: */
736: if (timercmp(&delta, mininterval, >=) ||
737: (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1.49 itojun 738: *lasttime = tv;
1.42 cgd 739: rv = 1;
740: }
1.50 itojun 741:
742: return (rv);
743: }
744:
745: /*
746: * ppsratecheck(): packets (or events) per second limitation.
747: */
748: int
749: ppsratecheck(lasttime, curpps, maxpps)
750: struct timeval *lasttime;
751: int *curpps;
752: int maxpps; /* maximum pps allowed */
753: {
754: struct timeval tv, delta;
755: int s, rv;
756:
757: s = splclock();
758: tv = mono_time;
759: splx(s);
760:
761: timersub(&tv, lasttime, &delta);
762:
763: /*
764: * check for 0,0 is so that the message will be seen at least once.
765: * if more than one second have passed since the last update of
766: * lasttime, reset the counter.
767: *
768: * we do increment *curpps even in *curpps < maxpps case, as some may
769: * try to use *curpps for stat purposes as well.
770: */
771: if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
772: delta.tv_sec >= 1) {
773: *lasttime = tv;
774: *curpps = 0;
775: rv = 1;
1.53 itojun 776: } else if (maxpps < 0)
777: rv = 1;
778: else if (*curpps < maxpps)
1.50 itojun 779: rv = 1;
780: else
781: rv = 0;
782:
1.51 jhawk 783: #if 1 /*DIAGNOSTIC?*/
1.50 itojun 784: /* be careful about wrap-around */
785: if (*curpps + 1 > *curpps)
786: *curpps = *curpps + 1;
787: #else
788: /*
789: * assume that there's not too many calls to this function.
790: * not sure if the assumption holds, as it depends on *caller's*
791: * behavior, not the behavior of this function.
792: * IMHO it is wrong to make assumption on the caller's behavior,
1.51 jhawk 793: * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1.50 itojun 794: */
795: *curpps = *curpps + 1;
796: #endif
1.42 cgd 797:
798: return (rv);
1.1 cgd 799: }
CVSweb <webmaster@jp.NetBSD.org>