[BACK]Return to kern_time.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/kern_time.c, Revision 1.105.4.2

1.105.4.2! ad          1: /*     $NetBSD: kern_time.c,v 1.105.4.1 2006/09/11 18:07:25 ad Exp $   */
1.42      cgd         2:
                      3: /*-
1.88      mycroft     4:  * Copyright (c) 2000, 2004, 2005 The NetBSD Foundation, Inc.
1.42      cgd         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.
1.72      agc        51:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        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:  *
1.33      fvdl       67:  *     @(#)kern_time.c 8.4 (Berkeley) 5/26/95
1.1       cgd        68:  */
1.58      lukem      69:
                     70: #include <sys/cdefs.h>
1.105.4.2! ad         71: __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.105.4.1 2006/09/11 18:07:25 ad Exp $");
1.31      thorpej    72:
                     73: #include "fs_nfs.h"
1.54      bjh21      74: #include "opt_nfs.h"
1.34      thorpej    75: #include "opt_nfsserver.h"
1.1       cgd        76:
1.5       mycroft    77: #include <sys/param.h>
                     78: #include <sys/resourcevar.h>
                     79: #include <sys/kernel.h>
1.8       cgd        80: #include <sys/systm.h>
1.5       mycroft    81: #include <sys/proc.h>
1.63      thorpej    82: #include <sys/sa.h>
                     83: #include <sys/savar.h>
1.8       cgd        84: #include <sys/vnode.h>
1.17      christos   85: #include <sys/signalvar.h>
1.25      perry      86: #include <sys/syslog.h>
1.101     kardel     87: #ifdef __HAVE_TIMECOUNTER
                     88: #include <sys/timetc.h>
                     89: #else /* !__HAVE_TIMECOUNTER */
1.95      cube       90: #include <sys/timevar.h>
1.101     kardel     91: #endif /* !__HAVE_TIMECOUNTER */
1.99      elad       92: #include <sys/kauth.h>
1.1       cgd        93:
1.11      cgd        94: #include <sys/mount.h>
                     95: #include <sys/syscallargs.h>
1.19      christos   96:
1.37      thorpej    97: #include <uvm/uvm_extern.h>
                     98:
1.26      thorpej    99: #if defined(NFS) || defined(NFSSERVER)
1.20      fvdl      100: #include <nfs/rpcv2.h>
                    101: #include <nfs/nfsproto.h>
1.93      jmmv      102: #include <nfs/nfs.h>
1.19      christos  103: #include <nfs/nfs_var.h>
                    104: #endif
1.17      christos  105:
1.5       mycroft   106: #include <machine/cpu.h>
1.23      cgd       107:
1.97      simonb    108: POOL_INIT(ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
                    109:     &pool_allocator_nointr);
                    110: POOL_INIT(ptimers_pool, sizeof(struct ptimers), 0, 0, 0, "ptimerspl",
                    111:     &pool_allocator_nointr);
                    112:
1.63      thorpej   113: static void timerupcall(struct lwp *, void *);
1.101     kardel    114: #ifdef __HAVE_TIMECOUNTER
                    115: static int itimespecfix(struct timespec *);            /* XXX move itimerfix to timespecs */
                    116: #endif /* __HAVE_TIMECOUNTER */
1.63      thorpej   117:
                    118: /* Time of day and interval timer support.
1.1       cgd       119:  *
                    120:  * These routines provide the kernel entry points to get and set
                    121:  * the time-of-day and per-process interval timers.  Subroutines
                    122:  * here provide support for adding and subtracting timeval structures
                    123:  * and decrementing interval timers, optionally reloading the interval
                    124:  * timers when they expire.
                    125:  */
                    126:
1.22      jtc       127: /* This function is used by clock_settime and settimeofday */
1.39      tron      128: int
1.98      christos  129: settime(struct proc *p, struct timespec *ts)
1.22      jtc       130: {
1.98      christos  131:        struct timeval delta, tv;
1.101     kardel    132: #ifdef __HAVE_TIMECOUNTER
                    133:        struct timeval now;
                    134:        struct timespec ts1;
                    135: #endif /* !__HAVE_TIMECOUNTER */
1.47      thorpej   136:        struct cpu_info *ci;
1.22      jtc       137:        int s;
                    138:
1.98      christos  139:        /*
                    140:         * Don't allow the time to be set forward so far it will wrap
                    141:         * and become negative, thus allowing an attacker to bypass
                    142:         * the next check below.  The cutoff is 1 year before rollover
                    143:         * occurs, so even if the attacker uses adjtime(2) to move
                    144:         * the time past the cutoff, it will take a very long time
                    145:         * to get to the wrap point.
                    146:         *
                    147:         * XXX: we check against INT_MAX since on 64-bit
                    148:         *      platforms, sizeof(int) != sizeof(long) and
                    149:         *      time_t is 32 bits even when atv.tv_sec is 64 bits.
                    150:         */
                    151:        if (ts->tv_sec > INT_MAX - 365*24*60*60) {
1.105.4.1  ad        152:                struct proc *pp;
                    153:
                    154:                rw_enter(&proclist_lock, RW_READER);
                    155:                pp = p->p_pptr;
                    156:                mutex_enter(&pp->p_crmutex);
1.98      christos  157:                log(LOG_WARNING, "pid %d (%s) "
                    158:                    "invoked by uid %d ppid %d (%s) "
                    159:                    "tried to set clock forward to %ld\n",
1.99      elad      160:                    p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
1.98      christos  161:                    pp->p_pid, pp->p_comm, (long)ts->tv_sec);
1.105.4.1  ad        162:                mutex_exit(&pp->p_crmutex);
                    163:                rw_exit(&proclist_lock);
1.98      christos  164:                return (EPERM);
                    165:        }
                    166:        TIMESPEC_TO_TIMEVAL(&tv, ts);
                    167:
1.22      jtc       168:        /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
                    169:        s = splclock();
1.101     kardel    170: #ifdef __HAVE_TIMECOUNTER
                    171:        microtime(&now);
                    172:        timersub(&tv, &now, &delta);
                    173: #else /* !__HAVE_TIMECOUNTER */
1.98      christos  174:        timersub(&tv, &time, &delta);
1.101     kardel    175: #endif /* !__HAVE_TIMECOUNTER */
1.55      tron      176:        if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1) {
                    177:                splx(s);
1.29      tls       178:                return (EPERM);
1.55      tron      179:        }
1.29      tls       180: #ifdef notyet
1.55      tron      181:        if ((delta.tv_sec < 86400) && securelevel > 0) {
                    182:                splx(s);
1.29      tls       183:                return (EPERM);
1.55      tron      184:        }
1.29      tls       185: #endif
1.103     kardel    186:
1.101     kardel    187: #ifdef __HAVE_TIMECOUNTER
1.103     kardel    188:        TIMEVAL_TO_TIMESPEC(&tv, &ts1);
1.101     kardel    189:        tc_setclock(&ts1);
                    190: #else /* !__HAVE_TIMECOUNTER */
1.98      christos  191:        time = tv;
1.103     kardel    192: #endif /* !__HAVE_TIMECOUNTER */
                    193:
1.38      thorpej   194:        (void) spllowersoftclock();
1.103     kardel    195:
1.22      jtc       196:        timeradd(&boottime, &delta, &boottime);
1.103     kardel    197:
1.47      thorpej   198:        /*
                    199:         * XXXSMP
                    200:         * This is wrong.  We should traverse a list of all
                    201:         * CPUs and add the delta to the runtime of those
                    202:         * CPUs which have a process on them.
                    203:         */
                    204:        ci = curcpu();
                    205:        timeradd(&ci->ci_schedstate.spc_runtime, &delta,
                    206:            &ci->ci_schedstate.spc_runtime);
1.101     kardel    207: #if (defined(NFS) && !defined (NFS_V2_ONLY)) || defined(NFSSERVER)
                    208:        nqnfs_lease_updatetime(delta.tv_sec);
                    209: #endif
1.22      jtc       210:        splx(s);
                    211:        resettodr();
1.29      tls       212:        return (0);
1.22      jtc       213: }
                    214:
                    215: /* ARGSUSED */
                    216: int
1.63      thorpej   217: sys_clock_gettime(struct lwp *l, void *v, register_t *retval)
1.22      jtc       218: {
1.45      augustss  219:        struct sys_clock_gettime_args /* {
1.22      jtc       220:                syscallarg(clockid_t) clock_id;
1.23      cgd       221:                syscallarg(struct timespec *) tp;
                    222:        } */ *uap = v;
1.22      jtc       223:        clockid_t clock_id;
                    224:        struct timespec ats;
                    225:
                    226:        clock_id = SCARG(uap, clock_id);
1.61      simonb    227:        switch (clock_id) {
                    228:        case CLOCK_REALTIME:
1.96      simonb    229:                nanotime(&ats);
1.61      simonb    230:                break;
                    231:        case CLOCK_MONOTONIC:
1.101     kardel    232: #ifdef __HAVE_TIMECOUNTER
                    233:                nanouptime(&ats);
                    234: #else /* !__HAVE_TIMECOUNTER */
                    235:                {
                    236:                int s;
                    237:
1.61      simonb    238:                /* XXX "hz" granularity */
1.63      thorpej   239:                s = splclock();
1.101     kardel    240:                TIMEVAL_TO_TIMESPEC(&mono_time,&ats);
1.61      simonb    241:                splx(s);
1.101     kardel    242:                }
                    243: #endif /* !__HAVE_TIMECOUNTER */
1.61      simonb    244:                break;
                    245:        default:
1.22      jtc       246:                return (EINVAL);
1.61      simonb    247:        }
1.22      jtc       248:
1.24      cgd       249:        return copyout(&ats, SCARG(uap, tp), sizeof(ats));
1.22      jtc       250: }
                    251:
                    252: /* ARGSUSED */
                    253: int
1.90      thorpej   254: sys_clock_settime(struct lwp *l, void *v, register_t *retval)
1.22      jtc       255: {
1.45      augustss  256:        struct sys_clock_settime_args /* {
1.22      jtc       257:                syscallarg(clockid_t) clock_id;
1.23      cgd       258:                syscallarg(const struct timespec *) tp;
                    259:        } */ *uap = v;
1.22      jtc       260:        int error;
                    261:
1.105     ad        262:        if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
                    263:            &l->l_acflag)) != 0)
1.22      jtc       264:                return (error);
                    265:
1.105     ad        266:        return clock_settime1(l->l_proc, SCARG(uap, clock_id), SCARG(uap, tp));
1.56      manu      267: }
                    268:
                    269:
                    270: int
1.98      christos  271: clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp)
1.56      manu      272: {
1.60      manu      273:        struct timespec ats;
1.56      manu      274:        int error;
                    275:
1.60      manu      276:        if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
                    277:                return (error);
                    278:
1.61      simonb    279:        switch (clock_id) {
                    280:        case CLOCK_REALTIME:
1.98      christos  281:                if ((error = settime(p, &ats)) != 0)
1.61      simonb    282:                        return (error);
                    283:                break;
                    284:        case CLOCK_MONOTONIC:
                    285:                return (EINVAL);        /* read-only clock */
                    286:        default:
1.56      manu      287:                return (EINVAL);
1.61      simonb    288:        }
1.22      jtc       289:
                    290:        return 0;
                    291: }
                    292:
                    293: int
1.63      thorpej   294: sys_clock_getres(struct lwp *l, void *v, register_t *retval)
1.22      jtc       295: {
1.45      augustss  296:        struct sys_clock_getres_args /* {
1.22      jtc       297:                syscallarg(clockid_t) clock_id;
1.23      cgd       298:                syscallarg(struct timespec *) tp;
                    299:        } */ *uap = v;
1.22      jtc       300:        clockid_t clock_id;
                    301:        struct timespec ts;
                    302:        int error = 0;
                    303:
                    304:        clock_id = SCARG(uap, clock_id);
1.61      simonb    305:        switch (clock_id) {
                    306:        case CLOCK_REALTIME:
                    307:        case CLOCK_MONOTONIC:
1.22      jtc       308:                ts.tv_sec = 0;
1.102     kardel    309: #ifdef __HAVE_TIMECOUNTER
                    310:                if (tc_getfrequency() > 1000000000)
                    311:                        ts.tv_nsec = 1;
                    312:                else
                    313:                        ts.tv_nsec = 1000000000 / tc_getfrequency();
                    314: #else /* !__HAVE_TIMECOUNTER */
1.22      jtc       315:                ts.tv_nsec = 1000000000 / hz;
1.102     kardel    316: #endif /* !__HAVE_TIMECOUNTER */
1.61      simonb    317:                break;
                    318:        default:
                    319:                return (EINVAL);
                    320:        }
1.22      jtc       321:
1.61      simonb    322:        if (SCARG(uap, tp))
1.35      perry     323:                error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
1.22      jtc       324:
                    325:        return error;
                    326: }
                    327:
1.27      jtc       328: /* ARGSUSED */
                    329: int
1.63      thorpej   330: sys_nanosleep(struct lwp *l, void *v, register_t *retval)
1.27      jtc       331: {
1.101     kardel    332: #ifdef __HAVE_TIMECOUNTER
                    333:        static int nanowait;
                    334:        struct sys_nanosleep_args/* {
                    335:                syscallarg(struct timespec *) rqtp;
                    336:                syscallarg(struct timespec *) rmtp;
                    337:        } */ *uap = v;
                    338:        struct timespec rmt, rqt;
                    339:        int error, timo;
                    340:
                    341:        error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
                    342:        if (error)
                    343:                return (error);
                    344:
                    345:        if (itimespecfix(&rqt))
                    346:                return (EINVAL);
                    347:
                    348:        timo = tstohz(&rqt);
                    349:        /*
                    350:         * Avoid inadvertantly sleeping forever
                    351:         */
                    352:        if (timo == 0)
                    353:                timo = 1;
                    354:
1.104     kardel    355:        getnanouptime(&rmt);
                    356:
1.101     kardel    357:        error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
                    358:        if (error == ERESTART)
                    359:                error = EINTR;
                    360:        if (error == EWOULDBLOCK)
                    361:                error = 0;
                    362:
                    363:        if (SCARG(uap, rmtp)) {
                    364:                int error1;
1.104     kardel    365:                struct timespec rmtend;
1.101     kardel    366:
1.104     kardel    367:                getnanouptime(&rmtend);
1.101     kardel    368:
1.104     kardel    369:                timespecsub(&rmtend, &rmt, &rmt);
1.101     kardel    370:                timespecsub(&rqt, &rmt, &rmt);
                    371:                if (rmt.tv_sec < 0)
                    372:                        timespecclear(&rmt);
                    373:
                    374:                error1 = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
                    375:                        sizeof(rmt));
                    376:                if (error1)
                    377:                        return (error1);
                    378:        }
                    379:
                    380:        return error;
                    381: #else /* !__HAVE_TIMECOUNTER */
1.27      jtc       382:        static int nanowait;
1.45      augustss  383:        struct sys_nanosleep_args/* {
1.27      jtc       384:                syscallarg(struct timespec *) rqtp;
                    385:                syscallarg(struct timespec *) rmtp;
                    386:        } */ *uap = v;
                    387:        struct timespec rqt;
                    388:        struct timespec rmt;
                    389:        struct timeval atv, utv;
                    390:        int error, s, timo;
                    391:
1.89      christos  392:        error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
1.27      jtc       393:        if (error)
                    394:                return (error);
                    395:
1.85      atatat    396:        TIMESPEC_TO_TIMEVAL(&atv,&rqt);
1.80      christos  397:        if (itimerfix(&atv))
1.27      jtc       398:                return (EINVAL);
                    399:
                    400:        s = splclock();
                    401:        timeradd(&atv,&time,&atv);
                    402:        timo = hzto(&atv);
1.63      thorpej   403:        /*
1.27      jtc       404:         * Avoid inadvertantly sleeping forever
                    405:         */
                    406:        if (timo == 0)
                    407:                timo = 1;
                    408:        splx(s);
                    409:
                    410:        error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
                    411:        if (error == ERESTART)
                    412:                error = EINTR;
                    413:        if (error == EWOULDBLOCK)
                    414:                error = 0;
                    415:
                    416:        if (SCARG(uap, rmtp)) {
1.89      christos  417:                int error1;
1.28      jtc       418:
1.27      jtc       419:                s = splclock();
                    420:                utv = time;
                    421:                splx(s);
                    422:
                    423:                timersub(&atv, &utv, &utv);
                    424:                if (utv.tv_sec < 0)
                    425:                        timerclear(&utv);
                    426:
                    427:                TIMEVAL_TO_TIMESPEC(&utv,&rmt);
1.89      christos  428:                error1 = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
1.28      jtc       429:                        sizeof(rmt));
1.89      christos  430:                if (error1)
                    431:                        return (error1);
1.27      jtc       432:        }
                    433:
                    434:        return error;
1.101     kardel    435: #endif /* !__HAVE_TIMECOUNTER */
1.27      jtc       436: }
1.22      jtc       437:
1.1       cgd       438: /* ARGSUSED */
1.3       andrew    439: int
1.63      thorpej   440: sys_gettimeofday(struct lwp *l, void *v, register_t *retval)
1.15      thorpej   441: {
1.45      augustss  442:        struct sys_gettimeofday_args /* {
1.11      cgd       443:                syscallarg(struct timeval *) tp;
1.84      simonb    444:                syscallarg(void *) tzp;         really "struct timezone *"
1.15      thorpej   445:        } */ *uap = v;
1.1       cgd       446:        struct timeval atv;
                    447:        int error = 0;
1.25      perry     448:        struct timezone tzfake;
1.1       cgd       449:
1.11      cgd       450:        if (SCARG(uap, tp)) {
1.1       cgd       451:                microtime(&atv);
1.35      perry     452:                error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
1.17      christos  453:                if (error)
1.1       cgd       454:                        return (error);
                    455:        }
1.25      perry     456:        if (SCARG(uap, tzp)) {
                    457:                /*
1.32      mycroft   458:                 * NetBSD has no kernel notion of time zone, so we just
1.25      perry     459:                 * fake up a timezone struct and return it if demanded.
                    460:                 */
                    461:                tzfake.tz_minuteswest = 0;
                    462:                tzfake.tz_dsttime = 0;
1.35      perry     463:                error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
1.25      perry     464:        }
1.1       cgd       465:        return (error);
                    466: }
                    467:
                    468: /* ARGSUSED */
1.3       andrew    469: int
1.63      thorpej   470: sys_settimeofday(struct lwp *l, void *v, register_t *retval)
1.15      thorpej   471: {
1.16      mycroft   472:        struct sys_settimeofday_args /* {
1.24      cgd       473:                syscallarg(const struct timeval *) tv;
1.84      simonb    474:                syscallarg(const void *) tzp;   really "const struct timezone *"
1.15      thorpej   475:        } */ *uap = v;
1.60      manu      476:        int error;
                    477:
1.105     ad        478:        if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
                    479:            &l->l_acflag)) != 0)
1.60      manu      480:                return (error);
                    481:
1.105     ad        482:        return settimeofday1(SCARG(uap, tv), SCARG(uap, tzp), l->l_proc);
1.60      manu      483: }
                    484:
                    485: int
1.90      thorpej   486: settimeofday1(const struct timeval *utv, const struct timezone *utzp,
                    487:     struct proc *p)
1.60      manu      488: {
1.22      jtc       489:        struct timeval atv;
1.98      christos  490:        struct timespec ts;
1.22      jtc       491:        int error;
1.1       cgd       492:
1.8       cgd       493:        /* Verify all parameters before changing time. */
1.25      perry     494:        /*
1.32      mycroft   495:         * NetBSD has no kernel notion of time zone, and only an
1.25      perry     496:         * obsolete program would try to set it, so we log a warning.
                    497:         */
1.98      christos  498:        if (utzp)
1.25      perry     499:                log(LOG_WARNING, "pid %d attempted to set the "
1.63      thorpej   500:                    "(obsolete) kernel time zone\n", p->p_pid);
1.98      christos  501:
                    502:        if (utv == NULL)
                    503:                return 0;
                    504:
                    505:        if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
                    506:                return error;
                    507:        TIMEVAL_TO_TIMESPEC(&atv, &ts);
                    508:        return settime(p, &ts);
1.1       cgd       509: }
                    510:
1.101     kardel    511: #ifndef __HAVE_TIMECOUNTER
1.1       cgd       512: int    tickdelta;                      /* current clock skew, us. per tick */
                    513: long   timedelta;                      /* unapplied time correction, us. */
                    514: long   bigadj = 1000000;               /* use 10x skew above bigadj us. */
1.101     kardel    515: #endif
                    516:
1.68      dsl       517: int    time_adjusted;                  /* set if an adjustment is made */
1.1       cgd       518:
                    519: /* ARGSUSED */
1.3       andrew    520: int
1.63      thorpej   521: sys_adjtime(struct lwp *l, void *v, register_t *retval)
1.15      thorpej   522: {
1.45      augustss  523:        struct sys_adjtime_args /* {
1.24      cgd       524:                syscallarg(const struct timeval *) delta;
1.11      cgd       525:                syscallarg(struct timeval *) olddelta;
1.15      thorpej   526:        } */ *uap = v;
1.56      manu      527:        int error;
1.1       cgd       528:
1.105     ad        529:        if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
                    530:            &l->l_acflag)) != 0)
1.1       cgd       531:                return (error);
1.17      christos  532:
1.105     ad        533:        return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), l->l_proc);
1.56      manu      534: }
                    535:
                    536: int
1.90      thorpej   537: adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
1.56      manu      538: {
1.60      manu      539:        struct timeval atv;
1.101     kardel    540:        int error = 0;
                    541:
                    542: #ifdef __HAVE_TIMECOUNTER
                    543:        extern int64_t time_adjtime;  /* in kern_ntptime.c */
                    544: #else /* !__HAVE_TIMECOUNTER */
1.56      manu      545:        long ndelta, ntickdelta, odelta;
                    546:        int s;
1.101     kardel    547: #endif /* !__HAVE_TIMECOUNTER */
                    548:
                    549: #ifdef __HAVE_TIMECOUNTER
                    550:        if (olddelta) {
                    551:                atv.tv_sec = time_adjtime / 1000000;
                    552:                atv.tv_usec = time_adjtime % 1000000;
                    553:                if (atv.tv_usec < 0) {
                    554:                        atv.tv_usec += 1000000;
                    555:                        atv.tv_sec--;
                    556:                }
                    557:                error = copyout(&atv, olddelta, sizeof(struct timeval));
                    558:                if (error)
                    559:                        return (error);
                    560:        }
                    561:
                    562:        if (delta) {
                    563:                error = copyin(delta, &atv, sizeof(struct timeval));
                    564:                if (error)
                    565:                        return (error);
                    566:
                    567:                time_adjtime = (int64_t)atv.tv_sec * 1000000 +
                    568:                        atv.tv_usec;
1.8       cgd       569:
1.101     kardel    570:                if (time_adjtime)
                    571:                        /* We need to save the system time during shutdown */
                    572:                        time_adjusted |= 1;
                    573:        }
                    574: #else /* !__HAVE_TIMECOUNTER */
1.60      manu      575:        error = copyin(delta, &atv, sizeof(struct timeval));
                    576:        if (error)
                    577:                return (error);
                    578:
1.8       cgd       579:        /*
                    580:         * Compute the total correction and the rate at which to apply it.
                    581:         * Round the adjustment down to a whole multiple of the per-tick
                    582:         * delta, so that after some number of incremental changes in
                    583:         * hardclock(), tickdelta will become zero, lest the correction
                    584:         * overshoot and start taking us away from the desired final time.
                    585:         */
1.60      manu      586:        ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
1.41      hwr       587:        if (ndelta > bigadj || ndelta < -bigadj)
1.8       cgd       588:                ntickdelta = 10 * tickadj;
                    589:        else
                    590:                ntickdelta = tickadj;
                    591:        if (ndelta % ntickdelta)
                    592:                ndelta = ndelta / ntickdelta * ntickdelta;
                    593:
                    594:        /*
                    595:         * To make hardclock()'s job easier, make the per-tick delta negative
                    596:         * if we want time to run slower; then hardclock can simply compute
                    597:         * tick + tickdelta, and subtract tickdelta from timedelta.
                    598:         */
                    599:        if (ndelta < 0)
                    600:                ntickdelta = -ntickdelta;
1.68      dsl       601:        if (ndelta != 0)
                    602:                /* We need to save the system clock time during shutdown */
                    603:                time_adjusted |= 1;
1.1       cgd       604:        s = splclock();
1.8       cgd       605:        odelta = timedelta;
1.1       cgd       606:        timedelta = ndelta;
1.8       cgd       607:        tickdelta = ntickdelta;
1.1       cgd       608:        splx(s);
                    609:
1.56      manu      610:        if (olddelta) {
1.60      manu      611:                atv.tv_sec = odelta / 1000000;
                    612:                atv.tv_usec = odelta % 1000000;
1.79      chs       613:                error = copyout(&atv, olddelta, sizeof(struct timeval));
1.8       cgd       614:        }
1.101     kardel    615: #endif /* __HAVE_TIMECOUNTER */
                    616:
1.79      chs       617:        return error;
1.1       cgd       618: }
                    619:
                    620: /*
1.63      thorpej   621:  * Interval timer support. Both the BSD getitimer() family and the POSIX
                    622:  * timer_*() family of routines are supported.
1.1       cgd       623:  *
1.63      thorpej   624:  * All timers are kept in an array pointed to by p_timers, which is
                    625:  * allocated on demand - many processes don't use timers at all. The
                    626:  * first three elements in this array are reserved for the BSD timers:
                    627:  * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
                    628:  * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
                    629:  * syscall.
1.1       cgd       630:  *
1.63      thorpej   631:  * Realtime timers are kept in the ptimer structure as an absolute
                    632:  * time; virtual time timers are kept as a linked list of deltas.
1.1       cgd       633:  * Virtual time timers are processed in the hardclock() routine of
1.63      thorpej   634:  * kern_clock.c.  The real time timer is processed by a callout
                    635:  * routine, called from the softclock() routine.  Since a callout may
                    636:  * be delayed in real time due to interrupt processing in the system,
                    637:  * it is possible for the real time timeout routine (realtimeexpire,
                    638:  * given below), to be delayed in real time past when it is supposed
                    639:  * to occur.  It does not suffice, therefore, to reload the real timer
                    640:  * .it_value from the real time timers .it_interval.  Rather, we
                    641:  * compute the next time in absolute time the timer should go off.  */
                    642:
                    643: /* Allocate a POSIX realtime timer. */
                    644: int
                    645: sys_timer_create(struct lwp *l, void *v, register_t *retval)
                    646: {
                    647:        struct sys_timer_create_args /* {
                    648:                syscallarg(clockid_t) clock_id;
                    649:                syscallarg(struct sigevent *) evp;
                    650:                syscallarg(timer_t *) timerid;
                    651:        } */ *uap = v;
1.92      cube      652:
                    653:        return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
1.105     ad        654:            SCARG(uap, evp), copyin, l);
1.92      cube      655: }
                    656:
                    657: int
                    658: timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
1.105     ad        659:     copyin_t fetch_event, struct lwp *l)
1.92      cube      660: {
                    661:        int error;
                    662:        timer_t timerid;
1.63      thorpej   663:        struct ptimer *pt;
1.105     ad        664:        struct proc *p;
                    665:
                    666:        p = l->l_proc;
1.63      thorpej   667:
                    668:        if (id < CLOCK_REALTIME ||
                    669:            id > CLOCK_PROF)
                    670:                return (EINVAL);
                    671:
                    672:        if (p->p_timers == NULL)
                    673:                timers_alloc(p);
                    674:
                    675:        /* Find a free timer slot, skipping those reserved for setitimer(). */
                    676:        for (timerid = 3; timerid < TIMER_MAX; timerid++)
                    677:                if (p->p_timers->pts_timers[timerid] == NULL)
                    678:                        break;
                    679:
                    680:        if (timerid == TIMER_MAX)
                    681:                return EAGAIN;
                    682:
                    683:        pt = pool_get(&ptimer_pool, PR_WAITOK);
                    684:        if (evp) {
                    685:                if (((error =
1.92      cube      686:                    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
1.63      thorpej   687:                    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
                    688:                        (pt->pt_ev.sigev_notify > SIGEV_SA))) {
                    689:                        pool_put(&ptimer_pool, pt);
                    690:                        return (error ? error : EINVAL);
                    691:                }
                    692:        } else {
                    693:                pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
                    694:                switch (id) {
                    695:                case CLOCK_REALTIME:
                    696:                        pt->pt_ev.sigev_signo = SIGALRM;
                    697:                        break;
                    698:                case CLOCK_VIRTUAL:
                    699:                        pt->pt_ev.sigev_signo = SIGVTALRM;
                    700:                        break;
                    701:                case CLOCK_PROF:
                    702:                        pt->pt_ev.sigev_signo = SIGPROF;
                    703:                        break;
                    704:                }
                    705:                pt->pt_ev.sigev_value.sival_int = timerid;
                    706:        }
1.73      christos  707:        pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
                    708:        pt->pt_info.ksi_errno = 0;
                    709:        pt->pt_info.ksi_code = 0;
                    710:        pt->pt_info.ksi_pid = p->p_pid;
1.105     ad        711:        pt->pt_info.ksi_uid = kauth_cred_getuid(l->l_cred);
1.73      christos  712:        pt->pt_info.ksi_sigval = pt->pt_ev.sigev_value;
1.63      thorpej   713:
                    714:        pt->pt_type = id;
                    715:        pt->pt_proc = p;
                    716:        pt->pt_overruns = 0;
                    717:        pt->pt_poverruns = 0;
1.64      nathanw   718:        pt->pt_entry = timerid;
1.63      thorpej   719:        timerclear(&pt->pt_time.it_value);
                    720:        if (id == CLOCK_REALTIME)
                    721:                callout_init(&pt->pt_ch);
                    722:        else
                    723:                pt->pt_active = 0;
                    724:
                    725:        p->p_timers->pts_timers[timerid] = pt;
                    726:
1.92      cube      727:        return copyout(&timerid, tid, sizeof(timerid));
1.63      thorpej   728: }
                    729:
                    730: /* Delete a POSIX realtime timer */
1.3       andrew    731: int
1.63      thorpej   732: sys_timer_delete(struct lwp *l, void *v, register_t *retval)
1.15      thorpej   733: {
1.63      thorpej   734:        struct sys_timer_delete_args /*  {
                    735:                syscallarg(timer_t) timerid;
1.15      thorpej   736:        } */ *uap = v;
1.63      thorpej   737:        struct proc *p = l->l_proc;
1.65      jdolecek  738:        timer_t timerid;
1.63      thorpej   739:        struct ptimer *pt, *ptn;
1.1       cgd       740:        int s;
                    741:
1.63      thorpej   742:        timerid = SCARG(uap, timerid);
                    743:
                    744:        if ((p->p_timers == NULL) ||
                    745:            (timerid < 2) || (timerid >= TIMER_MAX) ||
                    746:            ((pt = p->p_timers->pts_timers[timerid]) == NULL))
1.1       cgd       747:                return (EINVAL);
1.63      thorpej   748:
                    749:        if (pt->pt_type == CLOCK_REALTIME)
                    750:                callout_stop(&pt->pt_ch);
                    751:        else if (pt->pt_active) {
                    752:                s = splclock();
                    753:                ptn = LIST_NEXT(pt, pt_list);
                    754:                LIST_REMOVE(pt, pt_list);
                    755:                for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
                    756:                        timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
                    757:                            &ptn->pt_time.it_value);
                    758:                splx(s);
                    759:        }
                    760:
                    761:        p->p_timers->pts_timers[timerid] = NULL;
                    762:        pool_put(&ptimer_pool, pt);
                    763:
                    764:        return (0);
                    765: }
                    766:
                    767: /*
1.67      nathanw   768:  * Set up the given timer. The value in pt->pt_time.it_value is taken
                    769:  * to be an absolute time for CLOCK_REALTIME timers and a relative
                    770:  * time for virtual timers.
1.63      thorpej   771:  * Must be called at splclock().
                    772:  */
                    773: void
                    774: timer_settime(struct ptimer *pt)
                    775: {
                    776:        struct ptimer *ptn, *pptn;
                    777:        struct ptlist *ptl;
                    778:
                    779:        if (pt->pt_type == CLOCK_REALTIME) {
                    780:                callout_stop(&pt->pt_ch);
                    781:                if (timerisset(&pt->pt_time.it_value)) {
                    782:                        /*
                    783:                         * Don't need to check hzto() return value, here.
                    784:                         * callout_reset() does it for us.
                    785:                         */
                    786:                        callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
                    787:                            realtimerexpire, pt);
                    788:                }
                    789:        } else {
                    790:                if (pt->pt_active) {
                    791:                        ptn = LIST_NEXT(pt, pt_list);
                    792:                        LIST_REMOVE(pt, pt_list);
                    793:                        for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
                    794:                                timeradd(&pt->pt_time.it_value,
                    795:                                    &ptn->pt_time.it_value,
                    796:                                    &ptn->pt_time.it_value);
                    797:                }
                    798:                if (timerisset(&pt->pt_time.it_value)) {
                    799:                        if (pt->pt_type == CLOCK_VIRTUAL)
                    800:                                ptl = &pt->pt_proc->p_timers->pts_virtual;
                    801:                        else
                    802:                                ptl = &pt->pt_proc->p_timers->pts_prof;
                    803:
                    804:                        for (ptn = LIST_FIRST(ptl), pptn = NULL;
                    805:                             ptn && timercmp(&pt->pt_time.it_value,
                    806:                                 &ptn->pt_time.it_value, >);
                    807:                             pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
                    808:                                timersub(&pt->pt_time.it_value,
                    809:                                    &ptn->pt_time.it_value,
                    810:                                    &pt->pt_time.it_value);
                    811:
                    812:                        if (pptn)
                    813:                                LIST_INSERT_AFTER(pptn, pt, pt_list);
                    814:                        else
                    815:                                LIST_INSERT_HEAD(ptl, pt, pt_list);
                    816:
                    817:                        for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
                    818:                                timersub(&ptn->pt_time.it_value,
                    819:                                    &pt->pt_time.it_value,
                    820:                                    &ptn->pt_time.it_value);
                    821:
                    822:                        pt->pt_active = 1;
                    823:                } else
                    824:                        pt->pt_active = 0;
                    825:        }
                    826: }
                    827:
                    828: void
                    829: timer_gettime(struct ptimer *pt, struct itimerval *aitv)
                    830: {
1.101     kardel    831: #ifdef __HAVE_TIMECOUNTER
                    832:        struct timeval now;
                    833: #endif
1.63      thorpej   834:        struct ptimer *ptn;
                    835:
                    836:        *aitv = pt->pt_time;
                    837:        if (pt->pt_type == CLOCK_REALTIME) {
1.1       cgd       838:                /*
1.12      mycroft   839:                 * Convert from absolute to relative time in .it_value
1.63      thorpej   840:                 * part of real time timer.  If time for real time
                    841:                 * timer has passed return 0, else return difference
                    842:                 * between current time and time for the timer to go
                    843:                 * off.
1.1       cgd       844:                 */
1.63      thorpej   845:                if (timerisset(&aitv->it_value)) {
1.101     kardel    846: #ifdef __HAVE_TIMECOUNTER
                    847:                        getmicrotime(&now);
                    848:                        if (timercmp(&aitv->it_value, &now, <))
                    849:                                timerclear(&aitv->it_value);
                    850:                        else
                    851:                                timersub(&aitv->it_value, &now,
                    852:                                    &aitv->it_value);
                    853: #else /* !__HAVE_TIMECOUNTER */
1.63      thorpej   854:                        if (timercmp(&aitv->it_value, &time, <))
                    855:                                timerclear(&aitv->it_value);
1.1       cgd       856:                        else
1.63      thorpej   857:                                timersub(&aitv->it_value, &time,
                    858:                                    &aitv->it_value);
1.101     kardel    859: #endif /* !__HAVE_TIMECOUNTER */
1.36      thorpej   860:                }
1.63      thorpej   861:        } else if (pt->pt_active) {
                    862:                if (pt->pt_type == CLOCK_VIRTUAL)
                    863:                        ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
                    864:                else
                    865:                        ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
                    866:                for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
                    867:                        timeradd(&aitv->it_value,
                    868:                            &ptn->pt_time.it_value, &aitv->it_value);
                    869:                KASSERT(ptn != NULL); /* pt should be findable on the list */
1.1       cgd       870:        } else
1.63      thorpej   871:                timerclear(&aitv->it_value);
                    872: }
                    873:
                    874:
                    875:
                    876: /* Set and arm a POSIX realtime timer */
                    877: int
                    878: sys_timer_settime(struct lwp *l, void *v, register_t *retval)
                    879: {
                    880:        struct sys_timer_settime_args /* {
                    881:                syscallarg(timer_t) timerid;
                    882:                syscallarg(int) flags;
                    883:                syscallarg(const struct itimerspec *) value;
                    884:                syscallarg(struct itimerspec *) ovalue;
                    885:        } */ *uap = v;
1.92      cube      886:        int error;
                    887:        struct itimerspec value, ovalue, *ovp = NULL;
                    888:
                    889:        if ((error = copyin(SCARG(uap, value), &value,
                    890:            sizeof(struct itimerspec))) != 0)
                    891:                return (error);
                    892:
                    893:        if (SCARG(uap, ovalue))
                    894:                ovp = &ovalue;
                    895:
                    896:        if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
                    897:            SCARG(uap, flags), l->l_proc)) != 0)
                    898:                return error;
                    899:
                    900:        if (ovp)
                    901:                return copyout(&ovalue, SCARG(uap, ovalue),
                    902:                    sizeof(struct itimerspec));
                    903:        return 0;
                    904: }
                    905:
                    906: int
                    907: dotimer_settime(int timerid, struct itimerspec *value,
                    908:     struct itimerspec *ovalue, int flags, struct proc *p)
                    909: {
1.101     kardel    910: #ifdef __HAVE_TIMECOUNTER
                    911:        struct timeval now;
                    912: #endif
1.63      thorpej   913:        struct itimerval val, oval;
                    914:        struct ptimer *pt;
1.101     kardel    915:        int s;
1.63      thorpej   916:
                    917:        if ((p->p_timers == NULL) ||
                    918:            (timerid < 2) || (timerid >= TIMER_MAX) ||
                    919:            ((pt = p->p_timers->pts_timers[timerid]) == NULL))
                    920:                return (EINVAL);
                    921:
1.92      cube      922:        TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value);
                    923:        TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval);
1.63      thorpej   924:        if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
                    925:                return (EINVAL);
                    926:
                    927:        oval = pt->pt_time;
                    928:        pt->pt_time = val;
                    929:
                    930:        s = splclock();
1.67      nathanw   931:        /*
                    932:         * If we've been passed a relative time for a realtime timer,
                    933:         * convert it to absolute; if an absolute time for a virtual
                    934:         * timer, convert it to relative and make sure we don't set it
                    935:         * to zero, which would cancel the timer, or let it go
                    936:         * negative, which would confuse the comparison tests.
                    937:         */
                    938:        if (timerisset(&pt->pt_time.it_value)) {
                    939:                if (pt->pt_type == CLOCK_REALTIME) {
1.101     kardel    940: #ifdef __HAVE_TIMECOUNTER
                    941:                        if ((flags & TIMER_ABSTIME) == 0) {
                    942:                                getmicrotime(&now);
                    943:                                timeradd(&pt->pt_time.it_value, &now,
                    944:                                    &pt->pt_time.it_value);
                    945:                        }
                    946: #else /* !__HAVE_TIMECOUNTER */
1.92      cube      947:                        if ((flags & TIMER_ABSTIME) == 0)
1.67      nathanw   948:                                timeradd(&pt->pt_time.it_value, &time,
                    949:                                    &pt->pt_time.it_value);
1.101     kardel    950: #endif /* !__HAVE_TIMECOUNTER */
1.67      nathanw   951:                } else {
1.92      cube      952:                        if ((flags & TIMER_ABSTIME) != 0) {
1.101     kardel    953: #ifdef __HAVE_TIMECOUNTER
                    954:                                getmicrotime(&now);
                    955:                                timersub(&pt->pt_time.it_value, &now,
                    956:                                    &pt->pt_time.it_value);
                    957: #else /* !__HAVE_TIMECOUNTER */
1.67      nathanw   958:                                timersub(&pt->pt_time.it_value, &time,
                    959:                                    &pt->pt_time.it_value);
1.101     kardel    960: #endif /* !__HAVE_TIMECOUNTER */
1.67      nathanw   961:                                if (!timerisset(&pt->pt_time.it_value) ||
                    962:                                    pt->pt_time.it_value.tv_sec < 0) {
                    963:                                        pt->pt_time.it_value.tv_sec = 0;
                    964:                                        pt->pt_time.it_value.tv_usec = 1;
                    965:                                }
                    966:                        }
                    967:                }
                    968:        }
                    969:
1.63      thorpej   970:        timer_settime(pt);
                    971:        splx(s);
                    972:
1.92      cube      973:        if (ovalue) {
                    974:                TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue->it_value);
                    975:                TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue->it_interval);
1.63      thorpej   976:        }
                    977:
                    978:        return (0);
                    979: }
                    980:
                    981: /* Return the time remaining until a POSIX timer fires. */
                    982: int
                    983: sys_timer_gettime(struct lwp *l, void *v, register_t *retval)
                    984: {
                    985:        struct sys_timer_gettime_args /* {
                    986:                syscallarg(timer_t) timerid;
                    987:                syscallarg(struct itimerspec *) value;
                    988:        } */ *uap = v;
                    989:        struct itimerspec its;
1.92      cube      990:        int error;
                    991:
                    992:        if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
                    993:            &its)) != 0)
                    994:                return error;
                    995:
                    996:        return copyout(&its, SCARG(uap, value), sizeof(its));
                    997: }
                    998:
                    999: int
                   1000: dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
                   1001: {
                   1002:        int s;
1.63      thorpej  1003:        struct ptimer *pt;
1.92      cube     1004:        struct itimerval aitv;
1.63      thorpej  1005:
                   1006:        if ((p->p_timers == NULL) ||
                   1007:            (timerid < 2) || (timerid >= TIMER_MAX) ||
                   1008:            ((pt = p->p_timers->pts_timers[timerid]) == NULL))
                   1009:                return (EINVAL);
                   1010:
                   1011:        s = splclock();
                   1012:        timer_gettime(pt, &aitv);
1.1       cgd      1013:        splx(s);
1.63      thorpej  1014:
1.92      cube     1015:        TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its->it_interval);
                   1016:        TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its->it_value);
1.63      thorpej  1017:
1.92      cube     1018:        return 0;
1.63      thorpej  1019: }
                   1020:
                   1021: /*
                   1022:  * Return the count of the number of times a periodic timer expired
                   1023:  * while a notification was already pending. The counter is reset when
                   1024:  * a timer expires and a notification can be posted.
                   1025:  */
                   1026: int
                   1027: sys_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
                   1028: {
                   1029:        struct sys_timer_getoverrun_args /* {
                   1030:                syscallarg(timer_t) timerid;
                   1031:        } */ *uap = v;
                   1032:        struct proc *p = l->l_proc;
                   1033:        int timerid;
                   1034:        struct ptimer *pt;
                   1035:
                   1036:        timerid = SCARG(uap, timerid);
                   1037:
                   1038:        if ((p->p_timers == NULL) ||
                   1039:            (timerid < 2) || (timerid >= TIMER_MAX) ||
                   1040:            ((pt = p->p_timers->pts_timers[timerid]) == NULL))
                   1041:                return (EINVAL);
                   1042:
                   1043:        *retval = pt->pt_poverruns;
                   1044:
                   1045:        return (0);
                   1046: }
                   1047:
                   1048: /* Glue function that triggers an upcall; called from userret(). */
                   1049: static void
                   1050: timerupcall(struct lwp *l, void *arg)
                   1051: {
1.64      nathanw  1052:        struct ptimers *pt = (struct ptimers *)arg;
                   1053:        unsigned int i, fired, done;
1.74      cl       1054:
1.81      cl       1055:        KDASSERT(l->l_proc->p_sa);
                   1056:        /* Bail out if we do not own the virtual processor */
1.82      cl       1057:        if (l->l_savp->savp_lwp != l)
1.81      cl       1058:                return ;
1.87      perry    1059:
1.63      thorpej  1060:        KERNEL_PROC_LOCK(l);
1.71      fvdl     1061:
1.64      nathanw  1062:        fired = pt->pts_fired;
                   1063:        done = 0;
                   1064:        while ((i = ffs(fired)) != 0) {
1.74      cl       1065:                siginfo_t *si;
1.73      christos 1066:                int mask = 1 << --i;
1.74      cl       1067:                int f;
1.73      christos 1068:
1.74      cl       1069:                f = l->l_flag & L_SA;
                   1070:                l->l_flag &= ~L_SA;
1.94      chs      1071:                si = siginfo_alloc(PR_WAITOK);
1.77      thorpej  1072:                si->_info = pt->pts_timers[i]->pt_info.ksi_info;
1.64      nathanw  1073:                if (sa_upcall(l, SA_UPCALL_SIGEV | SA_UPCALL_DEFER, NULL, l,
1.94      chs      1074:                    sizeof(*si), si, siginfo_free) != 0) {
                   1075:                        siginfo_free(si);
1.86      mycroft  1076:                        /* XXX What do we do here?? */
                   1077:                } else
1.73      christos 1078:                        done |= mask;
                   1079:                fired &= ~mask;
1.74      cl       1080:                l->l_flag |= f;
1.64      nathanw  1081:        }
                   1082:        pt->pts_fired &= ~done;
                   1083:        if (pt->pts_fired == 0)
1.63      thorpej  1084:                l->l_proc->p_userret = NULL;
                   1085:
                   1086:        KERNEL_PROC_UNLOCK(l);
                   1087: }
                   1088:
                   1089: /*
                   1090:  * Real interval timer expired:
                   1091:  * send process whose timer expired an alarm signal.
                   1092:  * If time is not set up to reload, then just return.
                   1093:  * Else compute next time timer should go off which is > current time.
                   1094:  * This is where delay in processing this timeout causes multiple
                   1095:  * SIGALRM calls to be compressed into one.
                   1096:  */
                   1097: void
                   1098: realtimerexpire(void *arg)
                   1099: {
1.101     kardel   1100: #ifdef __HAVE_TIMECOUNTER
                   1101:        struct timeval now;
                   1102: #endif
1.63      thorpej  1103:        struct ptimer *pt;
                   1104:        int s;
                   1105:
                   1106:        pt = (struct ptimer *)arg;
                   1107:
                   1108:        itimerfire(pt);
                   1109:
                   1110:        if (!timerisset(&pt->pt_time.it_interval)) {
                   1111:                timerclear(&pt->pt_time.it_value);
                   1112:                return;
                   1113:        }
1.101     kardel   1114: #ifdef __HAVE_TIMECOUNTER
                   1115:        for (;;) {
                   1116:                s = splclock(); /* XXX need spl now? */
                   1117:                timeradd(&pt->pt_time.it_value,
                   1118:                    &pt->pt_time.it_interval, &pt->pt_time.it_value);
                   1119:                getmicrotime(&now);
                   1120:                if (timercmp(&pt->pt_time.it_value, &now, >)) {
                   1121:                        /*
                   1122:                         * Don't need to check hzto() return value, here.
                   1123:                         * callout_reset() does it for us.
                   1124:                         */
                   1125:                        callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
                   1126:                            realtimerexpire, pt);
                   1127:                        splx(s);
                   1128:                        return;
                   1129:                }
                   1130:                splx(s);
                   1131:                pt->pt_overruns++;
                   1132:        }
                   1133: #else /* !__HAVE_TIMECOUNTER */
1.63      thorpej  1134:        for (;;) {
                   1135:                s = splclock();
                   1136:                timeradd(&pt->pt_time.it_value,
                   1137:                    &pt->pt_time.it_interval, &pt->pt_time.it_value);
                   1138:                if (timercmp(&pt->pt_time.it_value, &time, >)) {
                   1139:                        /*
                   1140:                         * Don't need to check hzto() return value, here.
                   1141:                         * callout_reset() does it for us.
                   1142:                         */
                   1143:                        callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
                   1144:                            realtimerexpire, pt);
                   1145:                        splx(s);
                   1146:                        return;
                   1147:                }
                   1148:                splx(s);
                   1149:                pt->pt_overruns++;
                   1150:        }
1.101     kardel   1151: #endif /* !__HAVE_TIMECOUNTER */
1.63      thorpej  1152: }
                   1153:
                   1154: /* BSD routine to get the value of an interval timer. */
                   1155: /* ARGSUSED */
                   1156: int
                   1157: sys_getitimer(struct lwp *l, void *v, register_t *retval)
                   1158: {
                   1159:        struct sys_getitimer_args /* {
                   1160:                syscallarg(int) which;
                   1161:                syscallarg(struct itimerval *) itv;
                   1162:        } */ *uap = v;
                   1163:        struct proc *p = l->l_proc;
                   1164:        struct itimerval aitv;
1.91      cube     1165:        int error;
                   1166:
                   1167:        error = dogetitimer(p, SCARG(uap, which), &aitv);
                   1168:        if (error)
                   1169:                return error;
                   1170:        return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
                   1171: }
1.63      thorpej  1172:
1.91      cube     1173: int
                   1174: dogetitimer(struct proc *p, int which, struct itimerval *itvp)
                   1175: {
                   1176:        int s;
1.63      thorpej  1177:
                   1178:        if ((u_int)which > ITIMER_PROF)
                   1179:                return (EINVAL);
                   1180:
                   1181:        if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
1.91      cube     1182:                timerclear(&itvp->it_value);
                   1183:                timerclear(&itvp->it_interval);
1.63      thorpej  1184:        } else {
                   1185:                s = splclock();
1.91      cube     1186:                timer_gettime(p->p_timers->pts_timers[which], itvp);
1.63      thorpej  1187:                splx(s);
                   1188:        }
                   1189:
1.91      cube     1190:        return 0;
1.1       cgd      1191: }
                   1192:
1.63      thorpej  1193: /* BSD routine to set/arm an interval timer. */
1.1       cgd      1194: /* ARGSUSED */
1.3       andrew   1195: int
1.63      thorpej  1196: sys_setitimer(struct lwp *l, void *v, register_t *retval)
1.15      thorpej  1197: {
1.45      augustss 1198:        struct sys_setitimer_args /* {
1.30      mycroft  1199:                syscallarg(int) which;
1.24      cgd      1200:                syscallarg(const struct itimerval *) itv;
1.11      cgd      1201:                syscallarg(struct itimerval *) oitv;
1.15      thorpej  1202:        } */ *uap = v;
1.63      thorpej  1203:        struct proc *p = l->l_proc;
1.30      mycroft  1204:        int which = SCARG(uap, which);
1.21      cgd      1205:        struct sys_getitimer_args getargs;
1.91      cube     1206:        const struct itimerval *itvp;
1.1       cgd      1207:        struct itimerval aitv;
1.91      cube     1208:        int error;
1.1       cgd      1209:
1.30      mycroft  1210:        if ((u_int)which > ITIMER_PROF)
1.1       cgd      1211:                return (EINVAL);
1.11      cgd      1212:        itvp = SCARG(uap, itv);
1.63      thorpej  1213:        if (itvp &&
1.56      manu     1214:            (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
1.1       cgd      1215:                return (error);
1.21      cgd      1216:        if (SCARG(uap, oitv) != NULL) {
1.30      mycroft  1217:                SCARG(&getargs, which) = which;
1.21      cgd      1218:                SCARG(&getargs, itv) = SCARG(uap, oitv);
1.63      thorpej  1219:                if ((error = sys_getitimer(l, &getargs, retval)) != 0)
1.21      cgd      1220:                        return (error);
                   1221:        }
1.1       cgd      1222:        if (itvp == 0)
                   1223:                return (0);
1.91      cube     1224:
                   1225:        return dosetitimer(p, which, &aitv);
                   1226: }
                   1227:
                   1228: int
                   1229: dosetitimer(struct proc *p, int which, struct itimerval *itvp)
                   1230: {
1.101     kardel   1231: #ifdef __HAVE_TIMECOUNTER
                   1232:        struct timeval now;
                   1233: #endif
1.91      cube     1234:        struct ptimer *pt;
                   1235:        int s;
                   1236:
                   1237:        if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1.1       cgd      1238:                return (EINVAL);
1.63      thorpej  1239:
                   1240:        /*
                   1241:         * Don't bother allocating data structures if the process just
                   1242:         * wants to clear the timer.
                   1243:         */
1.91      cube     1244:        if (!timerisset(&itvp->it_value) &&
1.63      thorpej  1245:            ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
                   1246:                return (0);
                   1247:
                   1248:        if (p->p_timers == NULL)
                   1249:                timers_alloc(p);
                   1250:        if (p->p_timers->pts_timers[which] == NULL) {
                   1251:                pt = pool_get(&ptimer_pool, PR_WAITOK);
                   1252:                pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1.76      christos 1253:                pt->pt_ev.sigev_value.sival_int = which;
1.63      thorpej  1254:                pt->pt_overruns = 0;
                   1255:                pt->pt_proc = p;
                   1256:                pt->pt_type = which;
1.64      nathanw  1257:                pt->pt_entry = which;
1.63      thorpej  1258:                switch (which) {
                   1259:                case ITIMER_REAL:
                   1260:                        callout_init(&pt->pt_ch);
                   1261:                        pt->pt_ev.sigev_signo = SIGALRM;
                   1262:                        break;
                   1263:                case ITIMER_VIRTUAL:
                   1264:                        pt->pt_active = 0;
                   1265:                        pt->pt_ev.sigev_signo = SIGVTALRM;
                   1266:                        break;
                   1267:                case ITIMER_PROF:
                   1268:                        pt->pt_active = 0;
                   1269:                        pt->pt_ev.sigev_signo = SIGPROF;
                   1270:                        break;
1.1       cgd      1271:                }
                   1272:        } else
1.63      thorpej  1273:                pt = p->p_timers->pts_timers[which];
                   1274:
1.91      cube     1275:        pt->pt_time = *itvp;
1.63      thorpej  1276:        p->p_timers->pts_timers[which] = pt;
                   1277:
                   1278:        s = splclock();
1.67      nathanw  1279:        if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
                   1280:                /* Convert to absolute time */
1.101     kardel   1281: #ifdef __HAVE_TIMECOUNTER
                   1282:                /* XXX need to wrap in splclock for timecounters case? */
                   1283:                getmicrotime(&now);
                   1284:                timeradd(&pt->pt_time.it_value, &now, &pt->pt_time.it_value);
                   1285: #else /* !__HAVE_TIMECOUNTER */
1.67      nathanw  1286:                timeradd(&pt->pt_time.it_value, &time, &pt->pt_time.it_value);
1.101     kardel   1287: #endif /* !__HAVE_TIMECOUNTER */
1.67      nathanw  1288:        }
1.63      thorpej  1289:        timer_settime(pt);
1.1       cgd      1290:        splx(s);
1.63      thorpej  1291:
1.1       cgd      1292:        return (0);
                   1293: }
                   1294:
1.63      thorpej  1295: /* Utility routines to manage the array of pointers to timers. */
                   1296: void
                   1297: timers_alloc(struct proc *p)
                   1298: {
                   1299:        int i;
                   1300:        struct ptimers *pts;
                   1301:
1.100     yamt     1302:        pts = pool_get(&ptimers_pool, PR_WAITOK);
1.63      thorpej  1303:        LIST_INIT(&pts->pts_virtual);
                   1304:        LIST_INIT(&pts->pts_prof);
                   1305:        for (i = 0; i < TIMER_MAX; i++)
                   1306:                pts->pts_timers[i] = NULL;
1.64      nathanw  1307:        pts->pts_fired = 0;
1.63      thorpej  1308:        p->p_timers = pts;
                   1309: }
                   1310:
1.1       cgd      1311: /*
1.63      thorpej  1312:  * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
                   1313:  * then clean up all timers and free all the data structures. If
                   1314:  * "which" is set to TIMERS_POSIX, only clean up the timers allocated
                   1315:  * by timer_create(), not the BSD setitimer() timers, and only free the
                   1316:  * structure if none of those remain.
1.1       cgd      1317:  */
1.3       andrew   1318: void
1.63      thorpej  1319: timers_free(struct proc *p, int which)
1.6       cgd      1320: {
1.63      thorpej  1321:        int i, s;
                   1322:        struct ptimers *pts;
                   1323:        struct ptimer *pt, *ptn;
                   1324:        struct timeval tv;
                   1325:
                   1326:        if (p->p_timers) {
                   1327:                pts = p->p_timers;
                   1328:                if (which == TIMERS_ALL)
                   1329:                        i = 0;
                   1330:                else {
                   1331:                        s = splclock();
                   1332:                        timerclear(&tv);
                   1333:                        for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
                   1334:                             ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
                   1335:                             ptn = LIST_NEXT(ptn, pt_list))
                   1336:                                timeradd(&tv, &ptn->pt_time.it_value, &tv);
                   1337:                        LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
                   1338:                        if (ptn) {
                   1339:                                timeradd(&tv, &ptn->pt_time.it_value,
                   1340:                                    &ptn->pt_time.it_value);
                   1341:                                LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
                   1342:                                    ptn, pt_list);
                   1343:                        }
                   1344:
                   1345:                        timerclear(&tv);
                   1346:                        for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
                   1347:                             ptn && ptn != pts->pts_timers[ITIMER_PROF];
                   1348:                             ptn = LIST_NEXT(ptn, pt_list))
                   1349:                                timeradd(&tv, &ptn->pt_time.it_value, &tv);
                   1350:                        LIST_FIRST(&p->p_timers->pts_prof) = NULL;
                   1351:                        if (ptn) {
                   1352:                                timeradd(&tv, &ptn->pt_time.it_value,
                   1353:                                    &ptn->pt_time.it_value);
                   1354:                                LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
                   1355:                                    pt_list);
                   1356:                        }
1.1       cgd      1357:                        splx(s);
1.63      thorpej  1358:                        i = 3;
                   1359:                }
                   1360:                for ( ; i < TIMER_MAX; i++)
                   1361:                        if ((pt = pts->pts_timers[i]) != NULL) {
                   1362:                                if (pt->pt_type == CLOCK_REALTIME)
                   1363:                                        callout_stop(&pt->pt_ch);
                   1364:                                pts->pts_timers[i] = NULL;
                   1365:                                pool_put(&ptimer_pool, pt);
                   1366:                        }
                   1367:                if ((pts->pts_timers[0] == NULL) &&
                   1368:                    (pts->pts_timers[1] == NULL) &&
                   1369:                    (pts->pts_timers[2] == NULL)) {
                   1370:                        p->p_timers = NULL;
1.97      simonb   1371:                        pool_put(&ptimers_pool, pts);
1.1       cgd      1372:                }
                   1373:        }
                   1374: }
                   1375:
                   1376: /*
                   1377:  * Check that a proposed value to load into the .it_value or
                   1378:  * .it_interval part of an interval timer is acceptable, and
                   1379:  * fix it to have at least minimal value (i.e. if it is less
                   1380:  * than the resolution of the clock, round it up.)
                   1381:  */
1.3       andrew   1382: int
1.63      thorpej  1383: itimerfix(struct timeval *tv)
1.1       cgd      1384: {
                   1385:
1.59      christos 1386:        if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
1.1       cgd      1387:                return (EINVAL);
                   1388:        if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
                   1389:                tv->tv_usec = tick;
                   1390:        return (0);
                   1391: }
                   1392:
1.101     kardel   1393: #ifdef __HAVE_TIMECOUNTER
                   1394: int
                   1395: itimespecfix(struct timespec *ts)
                   1396: {
                   1397:
                   1398:        if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
                   1399:                return (EINVAL);
                   1400:        if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
                   1401:                ts->tv_nsec = tick * 1000;
                   1402:        return (0);
                   1403: }
                   1404: #endif /* __HAVE_TIMECOUNTER */
                   1405:
1.1       cgd      1406: /*
                   1407:  * Decrement an interval timer by a specified number
                   1408:  * of microseconds, which must be less than a second,
                   1409:  * i.e. < 1000000.  If the timer expires, then reload
                   1410:  * it.  In this case, carry over (usec - old value) to
1.8       cgd      1411:  * reduce the value reloaded into the timer so that
1.1       cgd      1412:  * the timer does not drift.  This routine assumes
                   1413:  * that it is called in a context where the timers
                   1414:  * on which it is operating cannot change in value.
                   1415:  */
1.3       andrew   1416: int
1.63      thorpej  1417: itimerdecr(struct ptimer *pt, int usec)
                   1418: {
1.45      augustss 1419:        struct itimerval *itp;
1.1       cgd      1420:
1.63      thorpej  1421:        itp = &pt->pt_time;
1.1       cgd      1422:        if (itp->it_value.tv_usec < usec) {
                   1423:                if (itp->it_value.tv_sec == 0) {
                   1424:                        /* expired, and already in next interval */
                   1425:                        usec -= itp->it_value.tv_usec;
                   1426:                        goto expire;
                   1427:                }
                   1428:                itp->it_value.tv_usec += 1000000;
                   1429:                itp->it_value.tv_sec--;
                   1430:        }
                   1431:        itp->it_value.tv_usec -= usec;
                   1432:        usec = 0;
                   1433:        if (timerisset(&itp->it_value))
                   1434:                return (1);
                   1435:        /* expired, exactly at end of interval */
                   1436: expire:
                   1437:        if (timerisset(&itp->it_interval)) {
                   1438:                itp->it_value = itp->it_interval;
                   1439:                itp->it_value.tv_usec -= usec;
                   1440:                if (itp->it_value.tv_usec < 0) {
                   1441:                        itp->it_value.tv_usec += 1000000;
                   1442:                        itp->it_value.tv_sec--;
                   1443:                }
1.63      thorpej  1444:                timer_settime(pt);
1.1       cgd      1445:        } else
                   1446:                itp->it_value.tv_usec = 0;              /* sec is already 0 */
                   1447:        return (0);
1.42      cgd      1448: }
                   1449:
1.63      thorpej  1450: void
                   1451: itimerfire(struct ptimer *pt)
                   1452: {
                   1453:        struct proc *p = pt->pt_proc;
1.82      cl       1454:        struct sadata_vp *vp;
                   1455:        unsigned int i;
1.78      cl       1456:
1.63      thorpej  1457:        if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
                   1458:                /*
                   1459:                 * No RT signal infrastructure exists at this time;
                   1460:                 * just post the signal number and throw away the
                   1461:                 * value.
                   1462:                 */
1.105.4.2! ad       1463:                if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo)) {
        !          1464:                        /*
        !          1465:                         * XXXAD Timers need to become per-LWP.
        !          1466:                         */
1.63      thorpej  1467:                        pt->pt_overruns++;
1.105.4.2! ad       1468:                } else {
1.75      christos 1469:                        ksiginfo_t ksi;
                   1470:                        (void)memset(&ksi, 0, sizeof(ksi));
                   1471:                        ksi.ksi_signo = pt->pt_ev.sigev_signo;
                   1472:                        ksi.ksi_code = SI_TIMER;
                   1473:                        ksi.ksi_sigval = pt->pt_ev.sigev_value;
1.63      thorpej  1474:                        pt->pt_poverruns = pt->pt_overruns;
                   1475:                        pt->pt_overruns = 0;
1.105.4.2! ad       1476:                        mutex_enter(&proclist_mutex);
1.75      christos 1477:                        kpsignal(p, &ksi, NULL);
1.105.4.2! ad       1478:                        mutex_exit(&proclist_mutex);
1.63      thorpej  1479:                }
                   1480:        } else if (pt->pt_ev.sigev_notify == SIGEV_SA && (p->p_flag & P_SA)) {
                   1481:                /* Cause the process to generate an upcall when it returns. */
                   1482:
                   1483:                if (p->p_userret == NULL) {
1.70      nathanw  1484:                        /*
                   1485:                         * XXX stop signals can be processed inside tsleep,
                   1486:                         * which can be inside sa_yield's inner loop, which
                   1487:                         * makes testing for sa_idle alone insuffucent to
                   1488:                         * determine if we really should call setrunnable.
                   1489:                         */
1.63      thorpej  1490:                        pt->pt_poverruns = pt->pt_overruns;
                   1491:                        pt->pt_overruns = 0;
1.64      nathanw  1492:                        i = 1 << pt->pt_entry;
                   1493:                        p->p_timers->pts_fired = i;
1.63      thorpej  1494:                        p->p_userret = timerupcall;
1.64      nathanw  1495:                        p->p_userret_arg = p->p_timers;
1.87      perry    1496:
1.105.4.2! ad       1497:                        mutex_enter(&p->p_smutex);      /* XXXAD locking */
1.82      cl       1498:                        SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
                   1499:                                if (vp->savp_lwp->l_flag & L_SA_IDLE) {
                   1500:                                        vp->savp_lwp->l_flag &= ~L_SA_IDLE;
1.105.4.2! ad       1501:                                        /* XXXAD */
        !          1502:                                        wakeup(vp->savp_lwp);
1.82      cl       1503:                                        break;
                   1504:                                }
1.78      cl       1505:                        }
1.105.4.2! ad       1506:                        mutex_exit(&p->p_smutex);       /* XXXAD locking */
1.64      nathanw  1507:                } else if (p->p_userret == timerupcall) {
                   1508:                        i = 1 << pt->pt_entry;
                   1509:                        if ((p->p_timers->pts_fired & i) == 0) {
                   1510:                                pt->pt_poverruns = pt->pt_overruns;
                   1511:                                pt->pt_overruns = 0;
1.66      jdolecek 1512:                                p->p_timers->pts_fired |= i;
1.64      nathanw  1513:                        } else
                   1514:                                pt->pt_overruns++;
                   1515:                } else {
1.63      thorpej  1516:                        pt->pt_overruns++;
1.78      cl       1517:                        if ((p->p_flag & P_WEXIT) == 0)
                   1518:                                printf("itimerfire(%d): overrun %d on timer %x (userret is %p)\n",
                   1519:                                    p->p_pid, pt->pt_overruns,
                   1520:                                    pt->pt_ev.sigev_value.sival_int,
                   1521:                                    p->p_userret);
1.64      nathanw  1522:                }
1.63      thorpej  1523:        }
                   1524:
                   1525: }
                   1526:
1.42      cgd      1527: /*
                   1528:  * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
                   1529:  * for usage and rationale.
                   1530:  */
                   1531: int
1.63      thorpej  1532: ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1.42      cgd      1533: {
1.49      itojun   1534:        struct timeval tv, delta;
1.101     kardel   1535:        int rv = 0;
                   1536: #ifndef __HAVE_TIMECOUNTER
                   1537:        int s;
                   1538: #endif
1.42      cgd      1539:
1.101     kardel   1540: #ifdef __HAVE_TIMECOUNTER
                   1541:        getmicrouptime(&tv);
                   1542: #else /* !__HAVE_TIMECOUNTER */
1.63      thorpej  1543:        s = splclock();
1.49      itojun   1544:        tv = mono_time;
                   1545:        splx(s);
1.101     kardel   1546: #endif /* !__HAVE_TIMECOUNTER */
1.49      itojun   1547:        timersub(&tv, lasttime, &delta);
1.42      cgd      1548:
                   1549:        /*
                   1550:         * check for 0,0 is so that the message will be seen at least once,
                   1551:         * even if interval is huge.
                   1552:         */
                   1553:        if (timercmp(&delta, mininterval, >=) ||
                   1554:            (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1.49      itojun   1555:                *lasttime = tv;
1.42      cgd      1556:                rv = 1;
                   1557:        }
1.50      itojun   1558:
                   1559:        return (rv);
                   1560: }
                   1561:
                   1562: /*
                   1563:  * ppsratecheck(): packets (or events) per second limitation.
                   1564:  */
                   1565: int
1.63      thorpej  1566: ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1.50      itojun   1567: {
                   1568:        struct timeval tv, delta;
1.101     kardel   1569:        int rv;
                   1570: #ifndef __HAVE_TIMECOUNTER
                   1571:        int s;
                   1572: #endif
1.50      itojun   1573:
1.101     kardel   1574: #ifdef __HAVE_TIMECOUNTER
                   1575:        getmicrouptime(&tv);
                   1576: #else /* !__HAVE_TIMECOUNTER */
1.63      thorpej  1577:        s = splclock();
1.50      itojun   1578:        tv = mono_time;
                   1579:        splx(s);
1.101     kardel   1580: #endif /* !__HAVE_TIMECOUNTER */
1.50      itojun   1581:        timersub(&tv, lasttime, &delta);
                   1582:
                   1583:        /*
                   1584:         * check for 0,0 is so that the message will be seen at least once.
                   1585:         * if more than one second have passed since the last update of
                   1586:         * lasttime, reset the counter.
                   1587:         *
                   1588:         * we do increment *curpps even in *curpps < maxpps case, as some may
                   1589:         * try to use *curpps for stat purposes as well.
                   1590:         */
                   1591:        if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
                   1592:            delta.tv_sec >= 1) {
                   1593:                *lasttime = tv;
                   1594:                *curpps = 0;
1.69      dyoung   1595:        }
                   1596:        if (maxpps < 0)
1.53      itojun   1597:                rv = 1;
                   1598:        else if (*curpps < maxpps)
1.50      itojun   1599:                rv = 1;
                   1600:        else
                   1601:                rv = 0;
                   1602:
1.51      jhawk    1603: #if 1 /*DIAGNOSTIC?*/
1.50      itojun   1604:        /* be careful about wrap-around */
                   1605:        if (*curpps + 1 > *curpps)
                   1606:                *curpps = *curpps + 1;
                   1607: #else
                   1608:        /*
                   1609:         * assume that there's not too many calls to this function.
                   1610:         * not sure if the assumption holds, as it depends on *caller's*
                   1611:         * behavior, not the behavior of this function.
                   1612:         * IMHO it is wrong to make assumption on the caller's behavior,
1.51      jhawk    1613:         * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1.50      itojun   1614:         */
                   1615:        *curpps = *curpps + 1;
                   1616: #endif
1.42      cgd      1617:
                   1618:        return (rv);
1.1       cgd      1619: }

CVSweb <webmaster@jp.NetBSD.org>