Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.218.2.1
      1  1.218.2.1    martin /*	$NetBSD: kern_time.c,v 1.218.2.1 2023/02/22 19:54:30 martin Exp $	*/
      2       1.42       cgd 
      3       1.42       cgd /*-
      4      1.207   thorpej  * Copyright (c) 2000, 2004, 2005, 2007, 2008, 2009, 2020
      5      1.207   thorpej  *     The NetBSD Foundation, Inc.
      6       1.42       cgd  * All rights reserved.
      7       1.42       cgd  *
      8       1.42       cgd  * This code is derived from software contributed to The NetBSD Foundation
      9      1.207   thorpej  * by Christopher G. Demetriou, by Andrew Doran, and by Jason R. Thorpe.
     10       1.42       cgd  *
     11       1.42       cgd  * Redistribution and use in source and binary forms, with or without
     12       1.42       cgd  * modification, are permitted provided that the following conditions
     13       1.42       cgd  * are met:
     14       1.42       cgd  * 1. Redistributions of source code must retain the above copyright
     15       1.42       cgd  *    notice, this list of conditions and the following disclaimer.
     16       1.42       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.42       cgd  *    notice, this list of conditions and the following disclaimer in the
     18       1.42       cgd  *    documentation and/or other materials provided with the distribution.
     19       1.42       cgd  *
     20       1.42       cgd  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.42       cgd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.42       cgd  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.42       cgd  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.42       cgd  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.42       cgd  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.42       cgd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.42       cgd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.42       cgd  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.42       cgd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.42       cgd  * POSSIBILITY OF SUCH DAMAGE.
     31       1.42       cgd  */
     32        1.9       cgd 
     33        1.1       cgd /*
     34        1.8       cgd  * Copyright (c) 1982, 1986, 1989, 1993
     35        1.8       cgd  *	The Regents of the University of California.  All rights reserved.
     36        1.1       cgd  *
     37        1.1       cgd  * Redistribution and use in source and binary forms, with or without
     38        1.1       cgd  * modification, are permitted provided that the following conditions
     39        1.1       cgd  * are met:
     40        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     41        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     42        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     43        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     44        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     45       1.72       agc  * 3. Neither the name of the University nor the names of its contributors
     46        1.1       cgd  *    may be used to endorse or promote products derived from this software
     47        1.1       cgd  *    without specific prior written permission.
     48        1.1       cgd  *
     49        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59        1.1       cgd  * SUCH DAMAGE.
     60        1.1       cgd  *
     61       1.33      fvdl  *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
     62        1.1       cgd  */
     63       1.58     lukem 
     64       1.58     lukem #include <sys/cdefs.h>
     65  1.218.2.1    martin __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.218.2.1 2023/02/22 19:54:30 martin Exp $");
     66        1.1       cgd 
     67        1.5   mycroft #include <sys/param.h>
     68        1.5   mycroft #include <sys/resourcevar.h>
     69        1.5   mycroft #include <sys/kernel.h>
     70        1.8       cgd #include <sys/systm.h>
     71        1.5   mycroft #include <sys/proc.h>
     72        1.8       cgd #include <sys/vnode.h>
     73       1.17  christos #include <sys/signalvar.h>
     74       1.25     perry #include <sys/syslog.h>
     75      1.101    kardel #include <sys/timetc.h>
     76      1.218  riastrad #include <sys/timevar.h>
     77      1.143        ad #include <sys/timex.h>
     78       1.99      elad #include <sys/kauth.h>
     79       1.11       cgd #include <sys/mount.h>
     80       1.11       cgd #include <sys/syscallargs.h>
     81      1.143        ad #include <sys/cpu.h>
     82       1.19  christos 
     83      1.210   thorpej kmutex_t	itimer_mutex __cacheline_aligned;	/* XXX static */
     84      1.207   thorpej static struct itlist itimer_realtime_changed_notify;
     85      1.142        ad 
     86      1.207   thorpej static void	ptimer_intr(void *);
     87      1.207   thorpej static void	*ptimer_sih __read_mostly;
     88      1.210   thorpej static TAILQ_HEAD(, ptimer) ptimer_queue;
     89       1.97    simonb 
     90      1.168      yamt #define	CLOCK_VIRTUAL_P(clockid)	\
     91      1.168      yamt 	((clockid) == CLOCK_VIRTUAL || (clockid) == CLOCK_PROF)
     92      1.168      yamt 
     93      1.168      yamt CTASSERT(ITIMER_REAL == CLOCK_REALTIME);
     94      1.168      yamt CTASSERT(ITIMER_VIRTUAL == CLOCK_VIRTUAL);
     95      1.168      yamt CTASSERT(ITIMER_PROF == CLOCK_PROF);
     96      1.170  christos CTASSERT(ITIMER_MONOTONIC == CLOCK_MONOTONIC);
     97      1.168      yamt 
     98      1.187  christos #define	DELAYTIMER_MAX	32
     99      1.186  christos 
    100      1.131        ad /*
    101      1.131        ad  * Initialize timekeeping.
    102      1.131        ad  */
    103      1.131        ad void
    104      1.131        ad time_init(void)
    105      1.131        ad {
    106      1.131        ad 
    107      1.207   thorpej 	mutex_init(&itimer_mutex, MUTEX_DEFAULT, IPL_SCHED);
    108      1.207   thorpej 	LIST_INIT(&itimer_realtime_changed_notify);
    109      1.207   thorpej 
    110      1.207   thorpej 	TAILQ_INIT(&ptimer_queue);
    111      1.207   thorpej 	ptimer_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    112      1.207   thorpej 	    ptimer_intr, NULL);
    113      1.207   thorpej }
    114      1.207   thorpej 
    115      1.207   thorpej /*
    116      1.207   thorpej  * Check if the time will wrap if set to ts.
    117      1.207   thorpej  *
    118      1.207   thorpej  * ts - timespec describing the new time
    119      1.207   thorpej  * delta - the delta between the current time and ts
    120      1.207   thorpej  */
    121      1.207   thorpej bool
    122      1.207   thorpej time_wraps(struct timespec *ts, struct timespec *delta)
    123      1.207   thorpej {
    124      1.207   thorpej 
    125      1.207   thorpej 	/*
    126      1.207   thorpej 	 * Don't allow the time to be set forward so far it
    127      1.207   thorpej 	 * will wrap and become negative, thus allowing an
    128      1.207   thorpej 	 * attacker to bypass the next check below.  The
    129      1.207   thorpej 	 * cutoff is 1 year before rollover occurs, so even
    130      1.207   thorpej 	 * if the attacker uses adjtime(2) to move the time
    131      1.207   thorpej 	 * past the cutoff, it will take a very long time
    132      1.207   thorpej 	 * to get to the wrap point.
    133      1.207   thorpej 	 */
    134      1.207   thorpej 	if ((ts->tv_sec > LLONG_MAX - 365*24*60*60) ||
    135      1.207   thorpej 	    (delta->tv_sec < 0 || delta->tv_nsec < 0))
    136      1.207   thorpej 		return true;
    137      1.207   thorpej 
    138      1.207   thorpej 	return false;
    139      1.207   thorpej }
    140      1.207   thorpej 
    141      1.207   thorpej /*
    142      1.207   thorpej  * itimer_lock:
    143      1.207   thorpej  *
    144      1.207   thorpej  *	Acquire the interval timer data lock.
    145      1.207   thorpej  */
    146      1.207   thorpej void
    147      1.207   thorpej itimer_lock(void)
    148      1.207   thorpej {
    149      1.207   thorpej 	mutex_spin_enter(&itimer_mutex);
    150      1.131        ad }
    151      1.131        ad 
    152      1.207   thorpej /*
    153      1.207   thorpej  * itimer_unlock:
    154      1.207   thorpej  *
    155      1.207   thorpej  *	Release the interval timer data lock.
    156      1.207   thorpej  */
    157      1.142        ad void
    158      1.207   thorpej itimer_unlock(void)
    159      1.142        ad {
    160      1.207   thorpej 	mutex_spin_exit(&itimer_mutex);
    161      1.207   thorpej }
    162      1.142        ad 
    163      1.207   thorpej /*
    164      1.207   thorpej  * itimer_lock_held:
    165      1.207   thorpej  *
    166      1.207   thorpej  *	Check that the interval timer lock is held for diagnostic
    167      1.207   thorpej  *	assertions.
    168      1.207   thorpej  */
    169      1.210   thorpej inline bool __diagused
    170      1.207   thorpej itimer_lock_held(void)
    171      1.207   thorpej {
    172      1.207   thorpej 	return mutex_owned(&itimer_mutex);
    173      1.142        ad }
    174      1.142        ad 
    175      1.207   thorpej /*
    176      1.207   thorpej  * Time of day and interval timer support.
    177        1.1       cgd  *
    178        1.1       cgd  * These routines provide the kernel entry points to get and set
    179        1.1       cgd  * the time-of-day and per-process interval timers.  Subroutines
    180        1.1       cgd  * here provide support for adding and subtracting timeval structures
    181        1.1       cgd  * and decrementing interval timers, optionally reloading the interval
    182        1.1       cgd  * timers when they expire.
    183        1.1       cgd  */
    184        1.1       cgd 
    185       1.22       jtc /* This function is used by clock_settime and settimeofday */
    186      1.132      elad static int
    187      1.156  christos settime1(struct proc *p, const struct timespec *ts, bool check_kauth)
    188       1.22       jtc {
    189      1.156  christos 	struct timespec delta, now;
    190       1.22       jtc 
    191      1.206       nia 	/*
    192      1.206       nia 	 * The time being set to an unreasonable value will cause
    193      1.206       nia 	 * unreasonable system behaviour.
    194      1.206       nia 	 */
    195      1.206       nia 	if (ts->tv_sec < 0 || ts->tv_sec > (1LL << 36))
    196      1.217  riastrad 		return EINVAL;
    197      1.206       nia 
    198      1.156  christos 	nanotime(&now);
    199      1.156  christos 	timespecsub(ts, &now, &delta);
    200      1.132      elad 
    201      1.134      elad 	if (check_kauth && kauth_authorize_system(kauth_cred_get(),
    202      1.156  christos 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, __UNCONST(ts),
    203      1.156  christos 	    &delta, KAUTH_ARG(check_kauth ? false : true)) != 0) {
    204      1.217  riastrad 		return EPERM;
    205       1.55      tron 	}
    206      1.132      elad 
    207       1.29       tls #ifdef notyet
    208      1.109      elad 	if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
    209      1.217  riastrad 		return EPERM;
    210       1.55      tron 	}
    211       1.29       tls #endif
    212      1.103    kardel 
    213      1.156  christos 	tc_setclock(ts);
    214      1.103    kardel 
    215       1.22       jtc 	resettodr();
    216      1.129        ad 
    217      1.207   thorpej 	/*
    218      1.207   thorpej 	 * Notify pending CLOCK_REALTIME timers about the real time change.
    219      1.207   thorpej 	 * There may be inactive timers on this list, but this happens
    220      1.207   thorpej 	 * comparatively less often than timers firing, and so it's better
    221      1.207   thorpej 	 * to put the extra checks here than to complicate the other code
    222      1.207   thorpej 	 * path.
    223      1.207   thorpej 	 */
    224      1.207   thorpej 	struct itimer *it;
    225      1.207   thorpej 	itimer_lock();
    226      1.207   thorpej 	LIST_FOREACH(it, &itimer_realtime_changed_notify, it_rtchgq) {
    227      1.207   thorpej 		KASSERT(it->it_ops->ito_realtime_changed != NULL);
    228      1.207   thorpej 		if (timespecisset(&it->it_time.it_value)) {
    229      1.207   thorpej 			(*it->it_ops->ito_realtime_changed)(it);
    230      1.207   thorpej 		}
    231      1.207   thorpej 	}
    232      1.207   thorpej 	itimer_unlock();
    233      1.207   thorpej 
    234      1.217  riastrad 	return 0;
    235       1.22       jtc }
    236       1.22       jtc 
    237      1.132      elad int
    238      1.132      elad settime(struct proc *p, struct timespec *ts)
    239      1.132      elad {
    240      1.217  riastrad 	return settime1(p, ts, true);
    241      1.132      elad }
    242      1.132      elad 
    243       1.22       jtc /* ARGSUSED */
    244       1.22       jtc int
    245      1.156  christos sys___clock_gettime50(struct lwp *l,
    246      1.156  christos     const struct sys___clock_gettime50_args *uap, register_t *retval)
    247       1.22       jtc {
    248      1.135       dsl 	/* {
    249       1.22       jtc 		syscallarg(clockid_t) clock_id;
    250       1.23       cgd 		syscallarg(struct timespec *) tp;
    251      1.135       dsl 	} */
    252      1.165     njoly 	int error;
    253       1.22       jtc 	struct timespec ats;
    254       1.22       jtc 
    255      1.165     njoly 	error = clock_gettime1(SCARG(uap, clock_id), &ats);
    256      1.165     njoly 	if (error != 0)
    257      1.165     njoly 		return error;
    258      1.165     njoly 
    259      1.165     njoly 	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
    260      1.165     njoly }
    261      1.165     njoly 
    262       1.22       jtc /* ARGSUSED */
    263       1.22       jtc int
    264      1.156  christos sys___clock_settime50(struct lwp *l,
    265      1.156  christos     const struct sys___clock_settime50_args *uap, register_t *retval)
    266       1.22       jtc {
    267      1.135       dsl 	/* {
    268       1.22       jtc 		syscallarg(clockid_t) clock_id;
    269       1.23       cgd 		syscallarg(const struct timespec *) tp;
    270      1.135       dsl 	} */
    271      1.156  christos 	int error;
    272      1.156  christos 	struct timespec ats;
    273       1.22       jtc 
    274      1.156  christos 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
    275      1.156  christos 		return error;
    276      1.156  christos 
    277      1.156  christos 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
    278       1.56      manu }
    279       1.56      manu 
    280       1.56      manu 
    281       1.56      manu int
    282      1.132      elad clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
    283      1.132      elad     bool check_kauth)
    284       1.56      manu {
    285       1.56      manu 	int error;
    286       1.56      manu 
    287      1.201     kamil 	if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000L)
    288      1.201     kamil 		return EINVAL;
    289      1.201     kamil 
    290       1.61    simonb 	switch (clock_id) {
    291       1.61    simonb 	case CLOCK_REALTIME:
    292      1.156  christos 		if ((error = settime1(p, tp, check_kauth)) != 0)
    293      1.217  riastrad 			return error;
    294       1.61    simonb 		break;
    295       1.61    simonb 	case CLOCK_MONOTONIC:
    296      1.217  riastrad 		return EINVAL;	/* read-only clock */
    297       1.61    simonb 	default:
    298      1.217  riastrad 		return EINVAL;
    299       1.61    simonb 	}
    300       1.22       jtc 
    301       1.22       jtc 	return 0;
    302       1.22       jtc }
    303       1.22       jtc 
    304       1.22       jtc int
    305      1.156  christos sys___clock_getres50(struct lwp *l, const struct sys___clock_getres50_args *uap,
    306      1.140      yamt     register_t *retval)
    307       1.22       jtc {
    308      1.135       dsl 	/* {
    309       1.22       jtc 		syscallarg(clockid_t) clock_id;
    310       1.23       cgd 		syscallarg(struct timespec *) tp;
    311      1.135       dsl 	} */
    312       1.22       jtc 	struct timespec ts;
    313      1.180      maxv 	int error;
    314       1.22       jtc 
    315      1.164     njoly 	if ((error = clock_getres1(SCARG(uap, clock_id), &ts)) != 0)
    316      1.164     njoly 		return error;
    317      1.164     njoly 
    318      1.164     njoly 	if (SCARG(uap, tp))
    319      1.164     njoly 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
    320      1.164     njoly 
    321      1.164     njoly 	return error;
    322      1.164     njoly }
    323      1.164     njoly 
    324      1.164     njoly int
    325      1.164     njoly clock_getres1(clockid_t clock_id, struct timespec *ts)
    326      1.164     njoly {
    327      1.164     njoly 
    328       1.61    simonb 	switch (clock_id) {
    329       1.61    simonb 	case CLOCK_REALTIME:
    330       1.61    simonb 	case CLOCK_MONOTONIC:
    331      1.164     njoly 		ts->tv_sec = 0;
    332      1.102    kardel 		if (tc_getfrequency() > 1000000000)
    333      1.164     njoly 			ts->tv_nsec = 1;
    334      1.102    kardel 		else
    335      1.164     njoly 			ts->tv_nsec = 1000000000 / tc_getfrequency();
    336       1.61    simonb 		break;
    337       1.61    simonb 	default:
    338      1.164     njoly 		return EINVAL;
    339       1.61    simonb 	}
    340       1.22       jtc 
    341      1.164     njoly 	return 0;
    342       1.22       jtc }
    343       1.22       jtc 
    344       1.27       jtc /* ARGSUSED */
    345       1.27       jtc int
    346      1.156  christos sys___nanosleep50(struct lwp *l, const struct sys___nanosleep50_args *uap,
    347      1.140      yamt     register_t *retval)
    348       1.27       jtc {
    349      1.135       dsl 	/* {
    350      1.101    kardel 		syscallarg(struct timespec *) rqtp;
    351      1.101    kardel 		syscallarg(struct timespec *) rmtp;
    352      1.135       dsl 	} */
    353      1.101    kardel 	struct timespec rmt, rqt;
    354      1.120       dsl 	int error, error1;
    355      1.101    kardel 
    356      1.101    kardel 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
    357      1.101    kardel 	if (error)
    358      1.217  riastrad 		return error;
    359      1.101    kardel 
    360      1.175  christos 	error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
    361      1.175  christos 	    SCARG(uap, rmtp) ? &rmt : NULL);
    362      1.175  christos 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    363      1.175  christos 		return error;
    364      1.175  christos 
    365      1.175  christos 	error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
    366      1.175  christos 	return error1 ? error1 : error;
    367      1.175  christos }
    368      1.175  christos 
    369      1.175  christos /* ARGSUSED */
    370      1.175  christos int
    371      1.175  christos sys_clock_nanosleep(struct lwp *l, const struct sys_clock_nanosleep_args *uap,
    372      1.175  christos     register_t *retval)
    373      1.175  christos {
    374      1.175  christos 	/* {
    375      1.175  christos 		syscallarg(clockid_t) clock_id;
    376      1.175  christos 		syscallarg(int) flags;
    377      1.175  christos 		syscallarg(struct timespec *) rqtp;
    378      1.175  christos 		syscallarg(struct timespec *) rmtp;
    379      1.175  christos 	} */
    380      1.175  christos 	struct timespec rmt, rqt;
    381      1.175  christos 	int error, error1;
    382      1.175  christos 
    383      1.175  christos 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
    384      1.175  christos 	if (error)
    385      1.181  christos 		goto out;
    386      1.175  christos 
    387      1.175  christos 	error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags), &rqt,
    388      1.175  christos 	    SCARG(uap, rmtp) ? &rmt : NULL);
    389      1.120       dsl 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    390      1.181  christos 		goto out;
    391      1.120       dsl 
    392      1.189     njoly 	if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0 &&
    393      1.189     njoly 	    (error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt))) != 0)
    394      1.181  christos 		error = error1;
    395      1.181  christos out:
    396      1.181  christos 	*retval = error;
    397      1.181  christos 	return 0;
    398      1.120       dsl }
    399      1.120       dsl 
    400      1.120       dsl int
    401      1.175  christos nanosleep1(struct lwp *l, clockid_t clock_id, int flags, struct timespec *rqt,
    402      1.175  christos     struct timespec *rmt)
    403      1.120       dsl {
    404      1.141      yamt 	struct timespec rmtstart;
    405      1.120       dsl 	int error, timo;
    406      1.120       dsl 
    407      1.184       uwe 	if ((error = ts2timo(clock_id, flags, rqt, &timo, &rmtstart)) != 0) {
    408      1.184       uwe 		if (error == ETIMEDOUT) {
    409      1.184       uwe 			error = 0;
    410      1.184       uwe 			if (rmt != NULL)
    411      1.184       uwe 				rmt->tv_sec = rmt->tv_nsec = 0;
    412      1.184       uwe 		}
    413      1.184       uwe 		return error;
    414      1.184       uwe 	}
    415      1.101    kardel 
    416      1.101    kardel 	/*
    417      1.175  christos 	 * Avoid inadvertently sleeping forever
    418      1.101    kardel 	 */
    419      1.101    kardel 	if (timo == 0)
    420      1.101    kardel 		timo = 1;
    421      1.141      yamt again:
    422      1.141      yamt 	error = kpause("nanoslp", true, timo, NULL);
    423      1.197       kre 	if (error == EWOULDBLOCK)
    424      1.197       kre 		error = 0;
    425      1.141      yamt 	if (rmt != NULL || error == 0) {
    426      1.141      yamt 		struct timespec rmtend;
    427      1.141      yamt 		struct timespec t0;
    428      1.141      yamt 		struct timespec *t;
    429      1.204      maxv 		int err;
    430      1.204      maxv 
    431      1.204      maxv 		err = clock_gettime1(clock_id, &rmtend);
    432      1.204      maxv 		if (err != 0)
    433      1.204      maxv 			return err;
    434      1.101    kardel 
    435      1.141      yamt 		t = (rmt != NULL) ? rmt : &t0;
    436      1.179  christos 		if (flags & TIMER_ABSTIME) {
    437      1.179  christos 			timespecsub(rqt, &rmtend, t);
    438      1.179  christos 		} else {
    439      1.213  riastrad 			if (timespeccmp(&rmtend, &rmtstart, <))
    440      1.213  riastrad 				timespecclear(t); /* clock wound back */
    441      1.213  riastrad 			else
    442      1.213  riastrad 				timespecsub(&rmtend, &rmtstart, t);
    443      1.213  riastrad 			if (timespeccmp(rqt, t, <))
    444      1.213  riastrad 				timespecclear(t);
    445      1.213  riastrad 			else
    446      1.213  riastrad 				timespecsub(rqt, t, t);
    447      1.179  christos 		}
    448      1.141      yamt 		if (t->tv_sec < 0)
    449      1.141      yamt 			timespecclear(t);
    450      1.141      yamt 		if (error == 0) {
    451      1.141      yamt 			timo = tstohz(t);
    452      1.141      yamt 			if (timo > 0)
    453      1.141      yamt 				goto again;
    454      1.141      yamt 		}
    455      1.141      yamt 	}
    456      1.104    kardel 
    457      1.101    kardel 	if (error == ERESTART)
    458      1.101    kardel 		error = EINTR;
    459      1.101    kardel 
    460      1.101    kardel 	return error;
    461       1.27       jtc }
    462       1.22       jtc 
    463      1.186  christos int
    464      1.186  christos sys_clock_getcpuclockid2(struct lwp *l,
    465      1.186  christos     const struct sys_clock_getcpuclockid2_args *uap,
    466      1.186  christos     register_t *retval)
    467      1.186  christos {
    468      1.186  christos 	/* {
    469      1.186  christos 		syscallarg(idtype_t idtype;
    470      1.186  christos 		syscallarg(id_t id);
    471      1.186  christos 		syscallarg(clockid_t *)clock_id;
    472      1.186  christos 	} */
    473      1.186  christos 	pid_t pid;
    474      1.186  christos 	lwpid_t lid;
    475      1.186  christos 	clockid_t clock_id;
    476      1.186  christos 	id_t id = SCARG(uap, id);
    477      1.186  christos 
    478      1.186  christos 	switch (SCARG(uap, idtype)) {
    479      1.186  christos 	case P_PID:
    480      1.188   msaitoh 		pid = id == 0 ? l->l_proc->p_pid : id;
    481      1.186  christos 		clock_id = CLOCK_PROCESS_CPUTIME_ID | pid;
    482      1.186  christos 		break;
    483      1.186  christos 	case P_LWPID:
    484      1.186  christos 		lid = id == 0 ? l->l_lid : id;
    485      1.186  christos 		clock_id = CLOCK_THREAD_CPUTIME_ID | lid;
    486      1.186  christos 		break;
    487      1.186  christos 	default:
    488      1.186  christos 		return EINVAL;
    489      1.186  christos 	}
    490      1.186  christos 	return copyout(&clock_id, SCARG(uap, clock_id), sizeof(clock_id));
    491      1.186  christos }
    492      1.186  christos 
    493        1.1       cgd /* ARGSUSED */
    494        1.3    andrew int
    495      1.156  christos sys___gettimeofday50(struct lwp *l, const struct sys___gettimeofday50_args *uap,
    496      1.140      yamt     register_t *retval)
    497       1.15   thorpej {
    498      1.135       dsl 	/* {
    499       1.11       cgd 		syscallarg(struct timeval *) tp;
    500      1.135       dsl 		syscallarg(void *) tzp;		really "struct timezone *";
    501      1.135       dsl 	} */
    502        1.1       cgd 	struct timeval atv;
    503        1.1       cgd 	int error = 0;
    504       1.25     perry 	struct timezone tzfake;
    505        1.1       cgd 
    506       1.11       cgd 	if (SCARG(uap, tp)) {
    507      1.190      maxv 		memset(&atv, 0, sizeof(atv));
    508        1.1       cgd 		microtime(&atv);
    509       1.35     perry 		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
    510       1.17  christos 		if (error)
    511      1.217  riastrad 			return error;
    512        1.1       cgd 	}
    513       1.25     perry 	if (SCARG(uap, tzp)) {
    514       1.25     perry 		/*
    515       1.32   mycroft 		 * NetBSD has no kernel notion of time zone, so we just
    516       1.25     perry 		 * fake up a timezone struct and return it if demanded.
    517       1.25     perry 		 */
    518       1.25     perry 		tzfake.tz_minuteswest = 0;
    519       1.25     perry 		tzfake.tz_dsttime = 0;
    520       1.35     perry 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
    521       1.25     perry 	}
    522      1.217  riastrad 	return error;
    523        1.1       cgd }
    524        1.1       cgd 
    525        1.1       cgd /* ARGSUSED */
    526        1.3    andrew int
    527      1.156  christos sys___settimeofday50(struct lwp *l, const struct sys___settimeofday50_args *uap,
    528      1.140      yamt     register_t *retval)
    529       1.15   thorpej {
    530      1.135       dsl 	/* {
    531       1.24       cgd 		syscallarg(const struct timeval *) tv;
    532      1.140      yamt 		syscallarg(const void *) tzp; really "const struct timezone *";
    533      1.135       dsl 	} */
    534       1.60      manu 
    535      1.119       dsl 	return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
    536       1.60      manu }
    537       1.60      manu 
    538       1.60      manu int
    539      1.119       dsl settimeofday1(const struct timeval *utv, bool userspace,
    540      1.119       dsl     const void *utzp, struct lwp *l, bool check_kauth)
    541       1.60      manu {
    542       1.22       jtc 	struct timeval atv;
    543       1.98  christos 	struct timespec ts;
    544       1.22       jtc 	int error;
    545        1.1       cgd 
    546        1.8       cgd 	/* Verify all parameters before changing time. */
    547      1.119       dsl 
    548       1.25     perry 	/*
    549       1.32   mycroft 	 * NetBSD has no kernel notion of time zone, and only an
    550       1.25     perry 	 * obsolete program would try to set it, so we log a warning.
    551       1.25     perry 	 */
    552       1.98  christos 	if (utzp)
    553       1.25     perry 		log(LOG_WARNING, "pid %d attempted to set the "
    554      1.119       dsl 		    "(obsolete) kernel time zone\n", l->l_proc->p_pid);
    555       1.98  christos 
    556      1.217  riastrad 	if (utv == NULL)
    557       1.98  christos 		return 0;
    558       1.98  christos 
    559      1.119       dsl 	if (userspace) {
    560      1.119       dsl 		if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
    561      1.119       dsl 			return error;
    562      1.119       dsl 		utv = &atv;
    563      1.119       dsl 	}
    564      1.119       dsl 
    565      1.200     kamil 	if (utv->tv_usec < 0 || utv->tv_usec >= 1000000)
    566      1.200     kamil 		return EINVAL;
    567      1.200     kamil 
    568      1.119       dsl 	TIMEVAL_TO_TIMESPEC(utv, &ts);
    569      1.133      elad 	return settime1(l->l_proc, &ts, check_kauth);
    570        1.1       cgd }
    571        1.1       cgd 
    572       1.68       dsl int	time_adjusted;			/* set if an adjustment is made */
    573        1.1       cgd 
    574        1.1       cgd /* ARGSUSED */
    575        1.3    andrew int
    576      1.156  christos sys___adjtime50(struct lwp *l, const struct sys___adjtime50_args *uap,
    577      1.140      yamt     register_t *retval)
    578       1.15   thorpej {
    579      1.135       dsl 	/* {
    580       1.24       cgd 		syscallarg(const struct timeval *) delta;
    581       1.11       cgd 		syscallarg(struct timeval *) olddelta;
    582      1.135       dsl 	} */
    583      1.180      maxv 	int error;
    584      1.156  christos 	struct timeval atv, oldatv;
    585        1.1       cgd 
    586      1.106      elad 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
    587      1.106      elad 	    KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
    588      1.156  christos 		return error;
    589       1.17  christos 
    590      1.156  christos 	if (SCARG(uap, delta)) {
    591      1.156  christos 		error = copyin(SCARG(uap, delta), &atv,
    592      1.156  christos 		    sizeof(*SCARG(uap, delta)));
    593      1.156  christos 		if (error)
    594      1.217  riastrad 			return error;
    595      1.156  christos 	}
    596      1.156  christos 	adjtime1(SCARG(uap, delta) ? &atv : NULL,
    597      1.156  christos 	    SCARG(uap, olddelta) ? &oldatv : NULL, l->l_proc);
    598      1.156  christos 	if (SCARG(uap, olddelta))
    599      1.156  christos 		error = copyout(&oldatv, SCARG(uap, olddelta),
    600      1.156  christos 		    sizeof(*SCARG(uap, olddelta)));
    601      1.156  christos 	return error;
    602       1.56      manu }
    603       1.56      manu 
    604      1.156  christos void
    605      1.110      yamt adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
    606       1.56      manu {
    607      1.101    kardel 
    608      1.101    kardel 	if (olddelta) {
    609      1.194      maxv 		memset(olddelta, 0, sizeof(*olddelta));
    610      1.143        ad 		mutex_spin_enter(&timecounter_lock);
    611      1.156  christos 		olddelta->tv_sec = time_adjtime / 1000000;
    612      1.156  christos 		olddelta->tv_usec = time_adjtime % 1000000;
    613      1.156  christos 		if (olddelta->tv_usec < 0) {
    614      1.156  christos 			olddelta->tv_usec += 1000000;
    615      1.156  christos 			olddelta->tv_sec--;
    616      1.101    kardel 		}
    617      1.157  christos 		mutex_spin_exit(&timecounter_lock);
    618      1.101    kardel 	}
    619      1.212  riastrad 
    620      1.101    kardel 	if (delta) {
    621      1.156  christos 		mutex_spin_enter(&timecounter_lock);
    622      1.212  riastrad 		/*
    623      1.212  riastrad 		 * XXX This should maybe just report failure to
    624      1.212  riastrad 		 * userland for nonsense deltas.
    625      1.212  riastrad 		 */
    626      1.212  riastrad 		if (delta->tv_sec > INT64_MAX/1000000 - 1) {
    627      1.212  riastrad 			time_adjtime = INT64_MAX;
    628      1.214  riastrad 		} else if (delta->tv_sec < INT64_MIN/1000000 + 1) {
    629      1.214  riastrad 			time_adjtime = INT64_MIN;
    630      1.212  riastrad 		} else {
    631      1.214  riastrad 			time_adjtime = delta->tv_sec * 1000000
    632      1.214  riastrad 			    + MAX(-999999, MIN(999999, delta->tv_usec));
    633      1.212  riastrad 		}
    634      1.101    kardel 
    635      1.143        ad 		if (time_adjtime) {
    636      1.101    kardel 			/* We need to save the system time during shutdown */
    637      1.101    kardel 			time_adjusted |= 1;
    638      1.143        ad 		}
    639      1.143        ad 		mutex_spin_exit(&timecounter_lock);
    640      1.101    kardel 	}
    641        1.1       cgd }
    642        1.1       cgd 
    643        1.1       cgd /*
    644      1.207   thorpej  * Interval timer support.
    645      1.207   thorpej  *
    646      1.207   thorpej  * The itimer_*() routines provide generic support for interval timers,
    647      1.207   thorpej  * both real (CLOCK_REALTIME, CLOCK_MONOTIME), and virtual (CLOCK_VIRTUAL,
    648      1.207   thorpej  * CLOCK_PROF).
    649      1.207   thorpej  *
    650      1.207   thorpej  * Real timers keep their deadline as an absolute time, and are fired
    651      1.207   thorpej  * by a callout.  Virtual timers are kept as a linked-list of deltas,
    652      1.207   thorpej  * and are processed by hardclock().
    653        1.1       cgd  *
    654      1.207   thorpej  * Because the real time timer callout may be delayed in real time due
    655      1.207   thorpej  * to interrupt processing on the system, it is possible for the real
    656      1.207   thorpej  * time timeout routine (itimer_callout()) run past after its deadline.
    657      1.207   thorpej  * It does not suffice, therefore, to reload the real timer .it_value
    658      1.207   thorpej  * from the timer's .it_interval.  Rather, we compute the next deadline
    659      1.207   thorpej  * in absolute time based on the current time and the .it_interval value,
    660      1.207   thorpej  * and report any overruns.
    661        1.1       cgd  *
    662      1.207   thorpej  * Note that while the virtual timers are supported in a generic fashion
    663      1.207   thorpej  * here, they only (currently) make sense as per-process timers, and thus
    664      1.207   thorpej  * only really work for that case.
    665      1.207   thorpej  */
    666       1.63   thorpej 
    667      1.207   thorpej /*
    668      1.207   thorpej  * itimer_init:
    669      1.207   thorpej  *
    670      1.207   thorpej  *	Initialize the common data for an interval timer.
    671      1.207   thorpej  */
    672      1.210   thorpej void
    673      1.207   thorpej itimer_init(struct itimer * const it, const struct itimer_ops * const ops,
    674      1.207   thorpej     clockid_t const id, struct itlist * const itl)
    675       1.63   thorpej {
    676       1.92      cube 
    677      1.207   thorpej 	KASSERT(itimer_lock_held());
    678      1.207   thorpej 	KASSERT(ops != NULL);
    679      1.207   thorpej 
    680      1.207   thorpej 	timespecclear(&it->it_time.it_value);
    681      1.207   thorpej 	it->it_ops = ops;
    682      1.207   thorpej 	it->it_clockid = id;
    683      1.207   thorpej 	it->it_overruns = 0;
    684      1.207   thorpej 	it->it_dying = false;
    685      1.207   thorpej 	if (!CLOCK_VIRTUAL_P(id)) {
    686      1.207   thorpej 		KASSERT(itl == NULL);
    687      1.207   thorpej 		callout_init(&it->it_ch, CALLOUT_MPSAFE);
    688      1.207   thorpej 		if (id == CLOCK_REALTIME && ops->ito_realtime_changed != NULL) {
    689      1.207   thorpej 			LIST_INSERT_HEAD(&itimer_realtime_changed_notify,
    690      1.207   thorpej 			    it, it_rtchgq);
    691      1.207   thorpej 		}
    692      1.207   thorpej 	} else {
    693      1.207   thorpej 		KASSERT(itl != NULL);
    694      1.207   thorpej 		it->it_vlist = itl;
    695      1.207   thorpej 		it->it_active = false;
    696      1.207   thorpej 	}
    697       1.92      cube }
    698       1.92      cube 
    699      1.207   thorpej /*
    700      1.210   thorpej  * itimer_poison:
    701      1.207   thorpej  *
    702      1.210   thorpej  *	Poison an interval timer, preventing it from being scheduled
    703      1.210   thorpej  *	or processed, in preparation for freeing the timer.
    704      1.207   thorpej  */
    705      1.210   thorpej void
    706      1.210   thorpej itimer_poison(struct itimer * const it)
    707       1.92      cube {
    708      1.105        ad 
    709      1.207   thorpej 	KASSERT(itimer_lock_held());
    710       1.63   thorpej 
    711      1.207   thorpej 	it->it_dying = true;
    712       1.63   thorpej 
    713      1.207   thorpej 	/*
    714      1.207   thorpej 	 * For non-virtual timers, stop the callout, or wait for it to
    715      1.207   thorpej 	 * run if it has already fired.  It cannot restart again after
    716      1.207   thorpej 	 * this point: the callout won't restart itself when dying, no
    717      1.207   thorpej 	 * other users holding the lock can restart it, and any other
    718      1.207   thorpej 	 * users waiting for callout_halt concurrently (itimer_settime)
    719      1.207   thorpej 	 * will restart from the top.
    720      1.207   thorpej 	 */
    721      1.207   thorpej 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
    722      1.207   thorpej 		callout_halt(&it->it_ch, &itimer_mutex);
    723      1.207   thorpej 		if (it->it_clockid == CLOCK_REALTIME &&
    724      1.207   thorpej 		    it->it_ops->ito_realtime_changed != NULL) {
    725      1.207   thorpej 			LIST_REMOVE(it, it_rtchgq);
    726      1.207   thorpej 		}
    727      1.207   thorpej 	}
    728      1.210   thorpej }
    729      1.210   thorpej 
    730      1.210   thorpej /*
    731      1.210   thorpej  * itimer_fini:
    732      1.210   thorpej  *
    733      1.210   thorpej  *	Release resources used by an interval timer.
    734      1.210   thorpej  *
    735      1.210   thorpej  *	N.B. itimer_lock must be held on entry, and is released on exit.
    736      1.210   thorpej  */
    737      1.210   thorpej void
    738      1.210   thorpej itimer_fini(struct itimer * const it)
    739      1.210   thorpej {
    740       1.63   thorpej 
    741      1.210   thorpej 	KASSERT(itimer_lock_held());
    742      1.142        ad 
    743      1.210   thorpej 	/* All done with the global state. */
    744      1.207   thorpej 	itimer_unlock();
    745      1.207   thorpej 
    746      1.207   thorpej 	/* Destroy the callout, if needed. */
    747      1.207   thorpej 	if (!CLOCK_VIRTUAL_P(it->it_clockid))
    748      1.207   thorpej 		callout_destroy(&it->it_ch);
    749      1.207   thorpej }
    750      1.207   thorpej 
    751      1.207   thorpej /*
    752      1.207   thorpej  * itimer_decr:
    753      1.207   thorpej  *
    754      1.207   thorpej  *	Decrement an interval timer by a specified number of nanoseconds,
    755      1.207   thorpej  *	which must be less than a second, i.e. < 1000000000.  If the timer
    756      1.207   thorpej  *	expires, then reload it.  In this case, carry over (nsec - old value)
    757      1.207   thorpej  *	to reduce the value reloaded into the timer so that the timer does
    758      1.207   thorpej  *	not drift.  This routine assumes that it is called in a context where
    759      1.207   thorpej  *	the timers on which it is operating cannot change in value.
    760      1.207   thorpej  *
    761      1.207   thorpej  *	Returns true if the timer has expired.
    762      1.207   thorpej  */
    763      1.207   thorpej static bool
    764      1.207   thorpej itimer_decr(struct itimer *it, int nsec)
    765      1.207   thorpej {
    766      1.207   thorpej 	struct itimerspec *itp;
    767      1.207   thorpej 	int error __diagused;
    768      1.207   thorpej 
    769      1.207   thorpej 	KASSERT(itimer_lock_held());
    770      1.207   thorpej 	KASSERT(CLOCK_VIRTUAL_P(it->it_clockid));
    771      1.207   thorpej 
    772      1.207   thorpej 	itp = &it->it_time;
    773      1.207   thorpej 	if (itp->it_value.tv_nsec < nsec) {
    774      1.207   thorpej 		if (itp->it_value.tv_sec == 0) {
    775      1.207   thorpej 			/* expired, and already in next interval */
    776      1.207   thorpej 			nsec -= itp->it_value.tv_nsec;
    777      1.207   thorpej 			goto expire;
    778      1.207   thorpej 		}
    779      1.207   thorpej 		itp->it_value.tv_nsec += 1000000000;
    780      1.207   thorpej 		itp->it_value.tv_sec--;
    781      1.142        ad 	}
    782      1.207   thorpej 	itp->it_value.tv_nsec -= nsec;
    783      1.207   thorpej 	nsec = 0;
    784      1.207   thorpej 	if (timespecisset(&itp->it_value))
    785      1.207   thorpej 		return false;
    786      1.207   thorpej 	/* expired, exactly at end of interval */
    787      1.207   thorpej  expire:
    788      1.207   thorpej 	if (timespecisset(&itp->it_interval)) {
    789      1.207   thorpej 		itp->it_value = itp->it_interval;
    790      1.207   thorpej 		itp->it_value.tv_nsec -= nsec;
    791      1.207   thorpej 		if (itp->it_value.tv_nsec < 0) {
    792      1.207   thorpej 			itp->it_value.tv_nsec += 1000000000;
    793      1.207   thorpej 			itp->it_value.tv_sec--;
    794       1.63   thorpej 		}
    795      1.207   thorpej 		error = itimer_settime(it);
    796      1.207   thorpej 		KASSERT(error == 0); /* virtual, never fails */
    797      1.207   thorpej 	} else
    798      1.207   thorpej 		itp->it_value.tv_nsec = 0;		/* sec is already 0 */
    799      1.207   thorpej 	return true;
    800      1.207   thorpej }
    801      1.207   thorpej 
    802      1.207   thorpej static void itimer_callout(void *);
    803       1.63   thorpej 
    804      1.207   thorpej /*
    805      1.207   thorpej  * itimer_arm_real:
    806      1.207   thorpej  *
    807      1.207   thorpej  *	Arm a non-virtual timer.
    808      1.207   thorpej  */
    809      1.207   thorpej static void
    810      1.207   thorpej itimer_arm_real(struct itimer * const it)
    811      1.207   thorpej {
    812  1.218.2.1    martin 	KASSERT(!it->it_dying);
    813  1.218.2.1    martin 
    814      1.207   thorpej 	/*
    815      1.207   thorpej 	 * Don't need to check tshzto() return value, here.
    816      1.207   thorpej 	 * callout_reset() does it for us.
    817      1.207   thorpej 	 */
    818      1.207   thorpej 	callout_reset(&it->it_ch,
    819      1.207   thorpej 	    (it->it_clockid == CLOCK_MONOTONIC
    820      1.207   thorpej 		? tshztoup(&it->it_time.it_value)
    821      1.207   thorpej 		: tshzto(&it->it_time.it_value)),
    822      1.207   thorpej 	    itimer_callout, it);
    823       1.63   thorpej }
    824       1.63   thorpej 
    825      1.207   thorpej /*
    826      1.207   thorpej  * itimer_callout:
    827      1.207   thorpej  *
    828      1.207   thorpej  *	Callout to expire a non-virtual timer.  Queue it up for processing,
    829      1.207   thorpej  *	and then reload, if it is configured to do so.
    830      1.207   thorpej  *
    831      1.207   thorpej  *	N.B. A delay in processing this callout causes multiple
    832      1.207   thorpej  *	SIGALRM calls to be compressed into one.
    833      1.207   thorpej  */
    834      1.207   thorpej static void
    835      1.207   thorpej itimer_callout(void *arg)
    836       1.15   thorpej {
    837      1.207   thorpej 	uint64_t last_val, next_val, interval, now_ns;
    838      1.207   thorpej 	struct timespec now, next;
    839      1.207   thorpej 	struct itimer * const it = arg;
    840      1.207   thorpej 	int backwards;
    841        1.1       cgd 
    842      1.207   thorpej 	itimer_lock();
    843      1.207   thorpej 	(*it->it_ops->ito_fire)(it);
    844       1.63   thorpej 
    845      1.207   thorpej 	if (!timespecisset(&it->it_time.it_interval)) {
    846      1.207   thorpej 		timespecclear(&it->it_time.it_value);
    847      1.207   thorpej 		itimer_unlock();
    848      1.207   thorpej 		return;
    849      1.142        ad 	}
    850      1.207   thorpej 
    851      1.207   thorpej 	if (it->it_clockid == CLOCK_MONOTONIC) {
    852      1.207   thorpej 		getnanouptime(&now);
    853      1.207   thorpej 	} else {
    854      1.207   thorpej 		getnanotime(&now);
    855       1.63   thorpej 	}
    856      1.216  riastrad 
    857      1.207   thorpej 	backwards = (timespeccmp(&it->it_time.it_value, &now, >));
    858      1.216  riastrad 
    859      1.216  riastrad 	/* Nonnegative interval guaranteed by itimerfix.  */
    860      1.216  riastrad 	KASSERT(it->it_time.it_interval.tv_sec >= 0);
    861      1.216  riastrad 	KASSERT(it->it_time.it_interval.tv_nsec >= 0);
    862      1.216  riastrad 
    863      1.207   thorpej 	/* Handle the easy case of non-overflown timers first. */
    864      1.216  riastrad 	if (!backwards &&
    865      1.216  riastrad 	    timespecaddok(&it->it_time.it_value, &it->it_time.it_interval)) {
    866      1.216  riastrad 		timespecadd(&it->it_time.it_value, &it->it_time.it_interval,
    867      1.216  riastrad 		    &next);
    868      1.207   thorpej 		it->it_time.it_value = next;
    869      1.207   thorpej 	} else {
    870      1.207   thorpej 		now_ns = timespec2ns(&now);
    871      1.207   thorpej 		last_val = timespec2ns(&it->it_time.it_value);
    872      1.207   thorpej 		interval = timespec2ns(&it->it_time.it_interval);
    873      1.198  riastrad 
    874      1.207   thorpej 		next_val = now_ns +
    875      1.207   thorpej 		    (now_ns - last_val + interval - 1) % interval;
    876      1.207   thorpej 
    877      1.207   thorpej 		if (backwards)
    878      1.207   thorpej 			next_val += interval;
    879      1.207   thorpej 		else
    880      1.207   thorpej 			it->it_overruns += (now_ns - last_val) / interval;
    881      1.207   thorpej 
    882      1.207   thorpej 		it->it_time.it_value.tv_sec = next_val / 1000000000;
    883      1.207   thorpej 		it->it_time.it_value.tv_nsec = next_val % 1000000000;
    884      1.207   thorpej 	}
    885       1.63   thorpej 
    886      1.207   thorpej 	/*
    887      1.207   thorpej 	 * Reset the callout, if it's not going away.
    888      1.207   thorpej 	 */
    889      1.207   thorpej 	if (!it->it_dying)
    890      1.207   thorpej 		itimer_arm_real(it);
    891      1.207   thorpej 	itimer_unlock();
    892       1.63   thorpej }
    893       1.63   thorpej 
    894       1.63   thorpej /*
    895      1.207   thorpej  * itimer_settime:
    896      1.207   thorpej  *
    897      1.207   thorpej  *	Set up the given interval timer. The value in it->it_time.it_value
    898      1.207   thorpej  *	is taken to be an absolute time for CLOCK_REALTIME/CLOCK_MONOTONIC
    899      1.207   thorpej  *	timers and a relative time for CLOCK_VIRTUAL/CLOCK_PROF timers.
    900      1.198  riastrad  *
    901      1.207   thorpej  *	If the callout had already fired but not yet run, fails with
    902      1.207   thorpej  *	ERESTART -- caller must restart from the top to look up a timer.
    903       1.63   thorpej  */
    904      1.198  riastrad int
    905      1.207   thorpej itimer_settime(struct itimer *it)
    906       1.63   thorpej {
    907      1.207   thorpej 	struct itimer *itn, *pitn;
    908      1.207   thorpej 	struct itlist *itl;
    909       1.63   thorpej 
    910      1.207   thorpej 	KASSERT(itimer_lock_held());
    911      1.142        ad 
    912      1.207   thorpej 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
    913      1.198  riastrad 		/*
    914      1.198  riastrad 		 * Try to stop the callout.  However, if it had already
    915      1.198  riastrad 		 * fired, we have to drop the lock to wait for it, so
    916      1.198  riastrad 		 * the world may have changed and pt may not be there
    917      1.198  riastrad 		 * any more.  In that case, tell the caller to start
    918      1.198  riastrad 		 * over from the top.
    919      1.198  riastrad 		 */
    920      1.207   thorpej 		if (callout_halt(&it->it_ch, &itimer_mutex))
    921      1.198  riastrad 			return ERESTART;
    922      1.198  riastrad 
    923      1.207   thorpej 		/* Now we can touch it and start it up again. */
    924      1.207   thorpej 		if (timespecisset(&it->it_time.it_value))
    925      1.207   thorpej 			itimer_arm_real(it);
    926       1.63   thorpej 	} else {
    927      1.207   thorpej 		if (it->it_active) {
    928      1.207   thorpej 			itn = LIST_NEXT(it, it_list);
    929      1.207   thorpej 			LIST_REMOVE(it, it_list);
    930      1.207   thorpej 			for ( ; itn; itn = LIST_NEXT(itn, it_list))
    931      1.207   thorpej 				timespecadd(&it->it_time.it_value,
    932      1.207   thorpej 				    &itn->it_time.it_value,
    933      1.207   thorpej 				    &itn->it_time.it_value);
    934      1.207   thorpej 		}
    935      1.207   thorpej 		if (timespecisset(&it->it_time.it_value)) {
    936      1.207   thorpej 			itl = it->it_vlist;
    937      1.207   thorpej 			for (itn = LIST_FIRST(itl), pitn = NULL;
    938      1.207   thorpej 			     itn && timespeccmp(&it->it_time.it_value,
    939      1.207   thorpej 				 &itn->it_time.it_value, >);
    940      1.207   thorpej 			     pitn = itn, itn = LIST_NEXT(itn, it_list))
    941      1.207   thorpej 				timespecsub(&it->it_time.it_value,
    942      1.207   thorpej 				    &itn->it_time.it_value,
    943      1.207   thorpej 				    &it->it_time.it_value);
    944      1.207   thorpej 
    945      1.207   thorpej 			if (pitn)
    946      1.207   thorpej 				LIST_INSERT_AFTER(pitn, it, it_list);
    947       1.63   thorpej 			else
    948      1.207   thorpej 				LIST_INSERT_HEAD(itl, it, it_list);
    949       1.63   thorpej 
    950      1.207   thorpej 			for ( ; itn ; itn = LIST_NEXT(itn, it_list))
    951      1.207   thorpej 				timespecsub(&itn->it_time.it_value,
    952      1.207   thorpej 				    &it->it_time.it_value,
    953      1.207   thorpej 				    &itn->it_time.it_value);
    954       1.63   thorpej 
    955      1.207   thorpej 			it->it_active = true;
    956      1.207   thorpej 		} else {
    957      1.207   thorpej 			it->it_active = false;
    958      1.207   thorpej 		}
    959       1.63   thorpej 	}
    960      1.198  riastrad 
    961      1.198  riastrad 	/* Success!  */
    962      1.198  riastrad 	return 0;
    963       1.63   thorpej }
    964       1.63   thorpej 
    965      1.207   thorpej /*
    966      1.207   thorpej  * itimer_gettime:
    967      1.207   thorpej  *
    968      1.207   thorpej  *	Return the remaining time of an interval timer.
    969      1.207   thorpej  */
    970       1.63   thorpej void
    971      1.207   thorpej itimer_gettime(const struct itimer *it, struct itimerspec *aits)
    972       1.63   thorpej {
    973      1.150  christos 	struct timespec now;
    974      1.207   thorpej 	struct itimer *itn;
    975       1.63   thorpej 
    976      1.207   thorpej 	KASSERT(itimer_lock_held());
    977      1.142        ad 
    978      1.207   thorpej 	*aits = it->it_time;
    979      1.207   thorpej 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
    980        1.1       cgd 		/*
    981       1.12   mycroft 		 * Convert from absolute to relative time in .it_value
    982       1.63   thorpej 		 * part of real time timer.  If time for real time
    983       1.63   thorpej 		 * timer has passed return 0, else return difference
    984       1.63   thorpej 		 * between current time and time for the timer to go
    985       1.63   thorpej 		 * off.
    986        1.1       cgd 		 */
    987      1.150  christos 		if (timespecisset(&aits->it_value)) {
    988      1.207   thorpej 			if (it->it_clockid == CLOCK_REALTIME) {
    989      1.168      yamt 				getnanotime(&now);
    990      1.168      yamt 			} else { /* CLOCK_MONOTONIC */
    991      1.168      yamt 				getnanouptime(&now);
    992      1.168      yamt 			}
    993      1.150  christos 			if (timespeccmp(&aits->it_value, &now, <))
    994      1.150  christos 				timespecclear(&aits->it_value);
    995      1.101    kardel 			else
    996      1.150  christos 				timespecsub(&aits->it_value, &now,
    997      1.150  christos 				    &aits->it_value);
    998       1.36   thorpej 		}
    999      1.207   thorpej 	} else if (it->it_active) {
   1000      1.207   thorpej 		for (itn = LIST_FIRST(it->it_vlist); itn && itn != it;
   1001      1.207   thorpej 		     itn = LIST_NEXT(itn, it_list))
   1002      1.150  christos 			timespecadd(&aits->it_value,
   1003      1.207   thorpej 			    &itn->it_time.it_value, &aits->it_value);
   1004      1.207   thorpej 		KASSERT(itn != NULL); /* it should be findable on the list */
   1005        1.1       cgd 	} else
   1006      1.150  christos 		timespecclear(&aits->it_value);
   1007       1.63   thorpej }
   1008       1.63   thorpej 
   1009      1.207   thorpej /*
   1010      1.207   thorpej  * Per-process timer support.
   1011      1.207   thorpej  *
   1012      1.207   thorpej  * Both the BSD getitimer() family and the POSIX timer_*() family of
   1013      1.207   thorpej  * routines are supported.
   1014      1.207   thorpej  *
   1015      1.207   thorpej  * All timers are kept in an array pointed to by p_timers, which is
   1016      1.207   thorpej  * allocated on demand - many processes don't use timers at all. The
   1017      1.207   thorpej  * first four elements in this array are reserved for the BSD timers:
   1018      1.207   thorpej  * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, element
   1019      1.207   thorpej  * 2 is ITIMER_PROF, and element 3 is ITIMER_MONOTONIC. The rest may be
   1020      1.207   thorpej  * allocated by the timer_create() syscall.
   1021      1.207   thorpej  *
   1022      1.207   thorpej  * These timers are a "sub-class" of interval timer.
   1023      1.207   thorpej  */
   1024      1.207   thorpej 
   1025      1.207   thorpej /*
   1026      1.207   thorpej  * ptimer_free:
   1027      1.207   thorpej  *
   1028      1.207   thorpej  *	Free the per-process timer at the specified index.
   1029      1.207   thorpej  */
   1030      1.207   thorpej static void
   1031      1.207   thorpej ptimer_free(struct ptimers *pts, int index)
   1032      1.207   thorpej {
   1033      1.207   thorpej 	struct itimer *it;
   1034      1.207   thorpej 	struct ptimer *pt;
   1035      1.207   thorpej 
   1036      1.207   thorpej 	KASSERT(itimer_lock_held());
   1037      1.207   thorpej 
   1038      1.207   thorpej 	it = pts->pts_timers[index];
   1039      1.207   thorpej 	pt = container_of(it, struct ptimer, pt_itimer);
   1040      1.207   thorpej 	pts->pts_timers[index] = NULL;
   1041      1.210   thorpej 	itimer_poison(it);
   1042      1.210   thorpej 
   1043      1.210   thorpej 	/*
   1044      1.210   thorpej 	 * Remove it from the queue to be signalled.  Must be done
   1045      1.210   thorpej 	 * after itimer is poisoned, because we may have had to wait
   1046      1.210   thorpej 	 * for the callout to complete.
   1047      1.210   thorpej 	 */
   1048      1.210   thorpej 	if (pt->pt_queued) {
   1049      1.210   thorpej 		TAILQ_REMOVE(&ptimer_queue, pt, pt_chain);
   1050      1.210   thorpej 		pt->pt_queued = false;
   1051      1.210   thorpej 	}
   1052      1.210   thorpej 
   1053      1.207   thorpej 	itimer_fini(it);	/* releases itimer_lock */
   1054      1.207   thorpej 	kmem_free(pt, sizeof(*pt));
   1055      1.207   thorpej }
   1056      1.207   thorpej 
   1057      1.207   thorpej /*
   1058      1.207   thorpej  * ptimers_alloc:
   1059      1.207   thorpej  *
   1060      1.207   thorpej  *	Allocate a ptimers for the specified process.
   1061      1.207   thorpej  */
   1062      1.207   thorpej static struct ptimers *
   1063      1.207   thorpej ptimers_alloc(struct proc *p)
   1064      1.207   thorpej {
   1065      1.207   thorpej 	struct ptimers *pts;
   1066      1.207   thorpej 	int i;
   1067      1.207   thorpej 
   1068      1.207   thorpej 	pts = kmem_alloc(sizeof(*pts), KM_SLEEP);
   1069      1.207   thorpej 	LIST_INIT(&pts->pts_virtual);
   1070      1.207   thorpej 	LIST_INIT(&pts->pts_prof);
   1071      1.207   thorpej 	for (i = 0; i < TIMER_MAX; i++)
   1072      1.207   thorpej 		pts->pts_timers[i] = NULL;
   1073      1.207   thorpej 	itimer_lock();
   1074      1.207   thorpej 	if (p->p_timers == NULL) {
   1075      1.207   thorpej 		p->p_timers = pts;
   1076      1.207   thorpej 		itimer_unlock();
   1077      1.207   thorpej 		return pts;
   1078      1.207   thorpej 	}
   1079      1.207   thorpej 	itimer_unlock();
   1080      1.207   thorpej 	kmem_free(pts, sizeof(*pts));
   1081      1.207   thorpej 	return p->p_timers;
   1082      1.207   thorpej }
   1083      1.207   thorpej 
   1084      1.207   thorpej /*
   1085      1.207   thorpej  * ptimers_free:
   1086      1.207   thorpej  *
   1087      1.207   thorpej  *	Clean up the per-process timers. If "which" is set to TIMERS_ALL,
   1088      1.207   thorpej  *	then clean up all timers and free all the data structures. If
   1089      1.207   thorpej  *	"which" is set to TIMERS_POSIX, only clean up the timers allocated
   1090      1.207   thorpej  *	by timer_create(), not the BSD setitimer() timers, and only free the
   1091      1.207   thorpej  *	structure if none of those remain.
   1092      1.207   thorpej  *
   1093      1.207   thorpej  *	This function is exported because it is needed in the exec and
   1094      1.207   thorpej  *	exit code paths.
   1095      1.207   thorpej  */
   1096      1.207   thorpej void
   1097      1.207   thorpej ptimers_free(struct proc *p, int which)
   1098      1.207   thorpej {
   1099      1.207   thorpej 	struct ptimers *pts;
   1100      1.207   thorpej 	struct itimer *itn;
   1101      1.207   thorpej 	struct timespec ts;
   1102      1.207   thorpej 	int i;
   1103      1.207   thorpej 
   1104      1.207   thorpej 	if (p->p_timers == NULL)
   1105      1.207   thorpej 		return;
   1106      1.207   thorpej 
   1107      1.207   thorpej 	pts = p->p_timers;
   1108      1.207   thorpej 	itimer_lock();
   1109      1.207   thorpej 	if (which == TIMERS_ALL) {
   1110      1.207   thorpej 		p->p_timers = NULL;
   1111      1.207   thorpej 		i = 0;
   1112      1.207   thorpej 	} else {
   1113      1.207   thorpej 		timespecclear(&ts);
   1114      1.207   thorpej 		for (itn = LIST_FIRST(&pts->pts_virtual);
   1115      1.207   thorpej 		     itn && itn != pts->pts_timers[ITIMER_VIRTUAL];
   1116      1.207   thorpej 		     itn = LIST_NEXT(itn, it_list)) {
   1117      1.207   thorpej 			KASSERT(itn->it_clockid == CLOCK_VIRTUAL);
   1118      1.207   thorpej 			timespecadd(&ts, &itn->it_time.it_value, &ts);
   1119      1.207   thorpej 		}
   1120      1.207   thorpej 		LIST_FIRST(&pts->pts_virtual) = NULL;
   1121      1.207   thorpej 		if (itn) {
   1122      1.207   thorpej 			KASSERT(itn->it_clockid == CLOCK_VIRTUAL);
   1123      1.207   thorpej 			timespecadd(&ts, &itn->it_time.it_value,
   1124      1.207   thorpej 			    &itn->it_time.it_value);
   1125      1.207   thorpej 			LIST_INSERT_HEAD(&pts->pts_virtual, itn, it_list);
   1126      1.207   thorpej 		}
   1127      1.207   thorpej 		timespecclear(&ts);
   1128      1.207   thorpej 		for (itn = LIST_FIRST(&pts->pts_prof);
   1129      1.207   thorpej 		     itn && itn != pts->pts_timers[ITIMER_PROF];
   1130      1.207   thorpej 		     itn = LIST_NEXT(itn, it_list)) {
   1131      1.207   thorpej 			KASSERT(itn->it_clockid == CLOCK_PROF);
   1132      1.207   thorpej 			timespecadd(&ts, &itn->it_time.it_value, &ts);
   1133      1.207   thorpej 		}
   1134      1.207   thorpej 		LIST_FIRST(&pts->pts_prof) = NULL;
   1135      1.207   thorpej 		if (itn) {
   1136      1.207   thorpej 			KASSERT(itn->it_clockid == CLOCK_PROF);
   1137      1.207   thorpej 			timespecadd(&ts, &itn->it_time.it_value,
   1138      1.207   thorpej 			    &itn->it_time.it_value);
   1139      1.207   thorpej 			LIST_INSERT_HEAD(&pts->pts_prof, itn, it_list);
   1140      1.207   thorpej 		}
   1141      1.207   thorpej 		i = TIMER_MIN;
   1142      1.207   thorpej 	}
   1143      1.207   thorpej 	for ( ; i < TIMER_MAX; i++) {
   1144      1.207   thorpej 		if (pts->pts_timers[i] != NULL) {
   1145      1.207   thorpej 			/* Free the timer and release the lock.  */
   1146      1.207   thorpej 			ptimer_free(pts, i);
   1147      1.207   thorpej 			/* Reacquire the lock for the next one.  */
   1148      1.207   thorpej 			itimer_lock();
   1149      1.207   thorpej 		}
   1150      1.207   thorpej 	}
   1151      1.207   thorpej 	if (pts->pts_timers[0] == NULL && pts->pts_timers[1] == NULL &&
   1152      1.207   thorpej 	    pts->pts_timers[2] == NULL && pts->pts_timers[3] == NULL) {
   1153      1.207   thorpej 		p->p_timers = NULL;
   1154      1.207   thorpej 		itimer_unlock();
   1155      1.207   thorpej 		kmem_free(pts, sizeof(*pts));
   1156      1.207   thorpej 	} else
   1157      1.207   thorpej 		itimer_unlock();
   1158      1.207   thorpej }
   1159      1.207   thorpej 
   1160      1.207   thorpej /*
   1161      1.207   thorpej  * ptimer_fire:
   1162      1.207   thorpej  *
   1163      1.207   thorpej  *	Fire a per-process timer.
   1164      1.207   thorpej  */
   1165      1.207   thorpej static void
   1166      1.207   thorpej ptimer_fire(struct itimer *it)
   1167      1.207   thorpej {
   1168      1.207   thorpej 	struct ptimer *pt = container_of(it, struct ptimer, pt_itimer);
   1169      1.207   thorpej 
   1170      1.207   thorpej 	KASSERT(itimer_lock_held());
   1171      1.207   thorpej 
   1172      1.207   thorpej 	/*
   1173      1.207   thorpej 	 * XXX Can overrun, but we don't do signal queueing yet, anyway.
   1174      1.207   thorpej 	 * XXX Relying on the clock interrupt is stupid.
   1175      1.207   thorpej 	 */
   1176      1.207   thorpej 	if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL) {
   1177      1.207   thorpej 		return;
   1178      1.207   thorpej 	}
   1179      1.210   thorpej 
   1180      1.210   thorpej 	if (!pt->pt_queued) {
   1181      1.210   thorpej 		TAILQ_INSERT_TAIL(&ptimer_queue, pt, pt_chain);
   1182      1.210   thorpej 		pt->pt_queued = true;
   1183      1.210   thorpej 		softint_schedule(ptimer_sih);
   1184      1.210   thorpej 	}
   1185      1.207   thorpej }
   1186      1.207   thorpej 
   1187      1.207   thorpej /*
   1188      1.207   thorpej  * Operations vector for per-process timers (BSD and POSIX).
   1189      1.207   thorpej  */
   1190      1.207   thorpej static const struct itimer_ops ptimer_itimer_ops = {
   1191      1.210   thorpej 	.ito_fire = ptimer_fire,
   1192      1.207   thorpej };
   1193      1.207   thorpej 
   1194      1.207   thorpej /*
   1195      1.207   thorpej  * sys_timer_create:
   1196      1.207   thorpej  *
   1197      1.207   thorpej  *	System call to create a POSIX timer.
   1198      1.207   thorpej  */
   1199      1.207   thorpej int
   1200      1.207   thorpej sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap,
   1201      1.207   thorpej     register_t *retval)
   1202      1.207   thorpej {
   1203      1.207   thorpej 	/* {
   1204      1.207   thorpej 		syscallarg(clockid_t) clock_id;
   1205      1.207   thorpej 		syscallarg(struct sigevent *) evp;
   1206      1.207   thorpej 		syscallarg(timer_t *) timerid;
   1207      1.207   thorpej 	} */
   1208      1.207   thorpej 
   1209      1.207   thorpej 	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
   1210      1.207   thorpej 	    SCARG(uap, evp), copyin, l);
   1211      1.207   thorpej }
   1212      1.207   thorpej 
   1213      1.207   thorpej int
   1214      1.207   thorpej timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
   1215      1.207   thorpej     copyin_t fetch_event, struct lwp *l)
   1216      1.207   thorpej {
   1217      1.207   thorpej 	int error;
   1218      1.207   thorpej 	timer_t timerid;
   1219      1.207   thorpej 	struct itlist *itl;
   1220      1.207   thorpej 	struct ptimers *pts;
   1221      1.207   thorpej 	struct ptimer *pt;
   1222      1.207   thorpej 	struct proc *p;
   1223       1.63   thorpej 
   1224      1.207   thorpej 	p = l->l_proc;
   1225       1.63   thorpej 
   1226      1.207   thorpej 	if ((u_int)id > CLOCK_MONOTONIC)
   1227      1.217  riastrad 		return EINVAL;
   1228      1.207   thorpej 
   1229      1.207   thorpej 	if ((pts = p->p_timers) == NULL)
   1230      1.207   thorpej 		pts = ptimers_alloc(p);
   1231      1.207   thorpej 
   1232      1.207   thorpej 	pt = kmem_zalloc(sizeof(*pt), KM_SLEEP);
   1233      1.207   thorpej 	if (evp != NULL) {
   1234      1.207   thorpej 		if (((error =
   1235      1.207   thorpej 		    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
   1236      1.207   thorpej 		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
   1237      1.207   thorpej 			(pt->pt_ev.sigev_notify > SIGEV_SA)) ||
   1238      1.207   thorpej 			(pt->pt_ev.sigev_notify == SIGEV_SIGNAL &&
   1239      1.207   thorpej 			 (pt->pt_ev.sigev_signo <= 0 ||
   1240      1.207   thorpej 			  pt->pt_ev.sigev_signo >= NSIG))) {
   1241      1.207   thorpej 			kmem_free(pt, sizeof(*pt));
   1242      1.207   thorpej 			return (error ? error : EINVAL);
   1243      1.207   thorpej 		}
   1244      1.207   thorpej 	}
   1245      1.207   thorpej 
   1246      1.207   thorpej 	/* Find a free timer slot, skipping those reserved for setitimer(). */
   1247      1.207   thorpej 	itimer_lock();
   1248      1.207   thorpej 	for (timerid = TIMER_MIN; timerid < TIMER_MAX; timerid++)
   1249      1.207   thorpej 		if (pts->pts_timers[timerid] == NULL)
   1250      1.207   thorpej 			break;
   1251      1.207   thorpej 	if (timerid == TIMER_MAX) {
   1252      1.207   thorpej 		itimer_unlock();
   1253      1.207   thorpej 		kmem_free(pt, sizeof(*pt));
   1254      1.207   thorpej 		return EAGAIN;
   1255      1.207   thorpej 	}
   1256      1.207   thorpej 	if (evp == NULL) {
   1257      1.207   thorpej 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
   1258      1.207   thorpej 		switch (id) {
   1259      1.207   thorpej 		case CLOCK_REALTIME:
   1260      1.207   thorpej 		case CLOCK_MONOTONIC:
   1261      1.207   thorpej 			pt->pt_ev.sigev_signo = SIGALRM;
   1262      1.207   thorpej 			break;
   1263      1.207   thorpej 		case CLOCK_VIRTUAL:
   1264      1.207   thorpej 			pt->pt_ev.sigev_signo = SIGVTALRM;
   1265      1.207   thorpej 			break;
   1266      1.207   thorpej 		case CLOCK_PROF:
   1267      1.207   thorpej 			pt->pt_ev.sigev_signo = SIGPROF;
   1268      1.207   thorpej 			break;
   1269      1.207   thorpej 		}
   1270      1.207   thorpej 		pt->pt_ev.sigev_value.sival_int = timerid;
   1271      1.207   thorpej 	}
   1272      1.207   thorpej 
   1273      1.207   thorpej 	switch (id) {
   1274      1.207   thorpej 	case CLOCK_VIRTUAL:
   1275      1.208   thorpej 		itl = &pts->pts_virtual;
   1276      1.207   thorpej 		break;
   1277      1.207   thorpej 	case CLOCK_PROF:
   1278      1.208   thorpej 		itl = &pts->pts_prof;
   1279      1.207   thorpej 		break;
   1280      1.207   thorpej 	default:
   1281      1.207   thorpej 		itl = NULL;
   1282      1.207   thorpej 	}
   1283      1.207   thorpej 
   1284      1.207   thorpej 	itimer_init(&pt->pt_itimer, &ptimer_itimer_ops, id, itl);
   1285      1.207   thorpej 	pt->pt_proc = p;
   1286      1.207   thorpej 	pt->pt_poverruns = 0;
   1287      1.207   thorpej 	pt->pt_entry = timerid;
   1288      1.210   thorpej 	pt->pt_queued = false;
   1289      1.207   thorpej 
   1290      1.207   thorpej 	pts->pts_timers[timerid] = &pt->pt_itimer;
   1291      1.207   thorpej 	itimer_unlock();
   1292      1.207   thorpej 
   1293      1.207   thorpej 	return copyout(&timerid, tid, sizeof(timerid));
   1294      1.207   thorpej }
   1295      1.207   thorpej 
   1296      1.207   thorpej /*
   1297      1.207   thorpej  * sys_timer_delete:
   1298      1.207   thorpej  *
   1299      1.207   thorpej  *	System call to delete a POSIX timer.
   1300      1.207   thorpej  */
   1301      1.207   thorpej int
   1302      1.207   thorpej sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap,
   1303      1.207   thorpej     register_t *retval)
   1304      1.207   thorpej {
   1305      1.207   thorpej 	/* {
   1306      1.207   thorpej 		syscallarg(timer_t) timerid;
   1307      1.207   thorpej 	} */
   1308      1.207   thorpej 	struct proc *p = l->l_proc;
   1309      1.207   thorpej 	timer_t timerid;
   1310      1.207   thorpej 	struct ptimers *pts;
   1311      1.207   thorpej 	struct itimer *it, *itn;
   1312      1.207   thorpej 
   1313      1.207   thorpej 	timerid = SCARG(uap, timerid);
   1314      1.207   thorpej 	pts = p->p_timers;
   1315      1.217  riastrad 
   1316      1.207   thorpej 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
   1317      1.217  riastrad 		return EINVAL;
   1318      1.207   thorpej 
   1319      1.207   thorpej 	itimer_lock();
   1320      1.207   thorpej 	if ((it = pts->pts_timers[timerid]) == NULL) {
   1321      1.207   thorpej 		itimer_unlock();
   1322      1.217  riastrad 		return EINVAL;
   1323      1.207   thorpej 	}
   1324      1.207   thorpej 
   1325      1.207   thorpej 	if (CLOCK_VIRTUAL_P(it->it_clockid)) {
   1326      1.207   thorpej 		if (it->it_active) {
   1327      1.207   thorpej 			itn = LIST_NEXT(it, it_list);
   1328      1.207   thorpej 			LIST_REMOVE(it, it_list);
   1329      1.207   thorpej 			for ( ; itn; itn = LIST_NEXT(itn, it_list))
   1330      1.207   thorpej 				timespecadd(&it->it_time.it_value,
   1331      1.207   thorpej 				    &itn->it_time.it_value,
   1332      1.207   thorpej 				    &itn->it_time.it_value);
   1333      1.207   thorpej 			it->it_active = false;
   1334      1.207   thorpej 		}
   1335      1.207   thorpej 	}
   1336      1.207   thorpej 
   1337      1.207   thorpej 	/* Free the timer and release the lock.  */
   1338      1.207   thorpej 	ptimer_free(pts, timerid);
   1339      1.207   thorpej 
   1340      1.217  riastrad 	return 0;
   1341      1.207   thorpej }
   1342      1.207   thorpej 
   1343      1.207   thorpej /*
   1344      1.207   thorpej  * sys___timer_settime50:
   1345      1.207   thorpej  *
   1346      1.207   thorpej  *	System call to set/arm a POSIX timer.
   1347      1.207   thorpej  */
   1348       1.63   thorpej int
   1349      1.156  christos sys___timer_settime50(struct lwp *l,
   1350      1.156  christos     const struct sys___timer_settime50_args *uap,
   1351      1.140      yamt     register_t *retval)
   1352       1.63   thorpej {
   1353      1.135       dsl 	/* {
   1354       1.63   thorpej 		syscallarg(timer_t) timerid;
   1355       1.63   thorpej 		syscallarg(int) flags;
   1356       1.63   thorpej 		syscallarg(const struct itimerspec *) value;
   1357       1.63   thorpej 		syscallarg(struct itimerspec *) ovalue;
   1358      1.135       dsl 	} */
   1359       1.92      cube 	int error;
   1360       1.92      cube 	struct itimerspec value, ovalue, *ovp = NULL;
   1361       1.92      cube 
   1362       1.92      cube 	if ((error = copyin(SCARG(uap, value), &value,
   1363       1.92      cube 	    sizeof(struct itimerspec))) != 0)
   1364      1.217  riastrad 		return error;
   1365       1.92      cube 
   1366       1.92      cube 	if (SCARG(uap, ovalue))
   1367       1.92      cube 		ovp = &ovalue;
   1368       1.92      cube 
   1369       1.92      cube 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
   1370       1.92      cube 	    SCARG(uap, flags), l->l_proc)) != 0)
   1371       1.92      cube 		return error;
   1372       1.92      cube 
   1373       1.92      cube 	if (ovp)
   1374       1.92      cube 		return copyout(&ovalue, SCARG(uap, ovalue),
   1375       1.92      cube 		    sizeof(struct itimerspec));
   1376       1.92      cube 	return 0;
   1377       1.92      cube }
   1378       1.92      cube 
   1379       1.92      cube int
   1380       1.92      cube dotimer_settime(int timerid, struct itimerspec *value,
   1381       1.92      cube     struct itimerspec *ovalue, int flags, struct proc *p)
   1382       1.92      cube {
   1383      1.150  christos 	struct timespec now;
   1384      1.150  christos 	struct itimerspec val, oval;
   1385      1.142        ad 	struct ptimers *pts;
   1386      1.207   thorpej 	struct itimer *it;
   1387      1.160  christos 	int error;
   1388       1.63   thorpej 
   1389      1.142        ad 	pts = p->p_timers;
   1390       1.63   thorpej 
   1391      1.142        ad 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
   1392      1.142        ad 		return EINVAL;
   1393      1.150  christos 	val = *value;
   1394      1.160  christos 	if ((error = itimespecfix(&val.it_value)) != 0 ||
   1395      1.160  christos 	    (error = itimespecfix(&val.it_interval)) != 0)
   1396      1.160  christos 		return error;
   1397       1.63   thorpej 
   1398      1.207   thorpej 	itimer_lock();
   1399      1.207   thorpej  restart:
   1400      1.207   thorpej 	if ((it = pts->pts_timers[timerid]) == NULL) {
   1401      1.207   thorpej 		itimer_unlock();
   1402      1.150  christos 		return EINVAL;
   1403      1.142        ad 	}
   1404      1.142        ad 
   1405      1.207   thorpej 	oval = it->it_time;
   1406      1.207   thorpej 	it->it_time = val;
   1407       1.63   thorpej 
   1408       1.67   nathanw 	/*
   1409       1.67   nathanw 	 * If we've been passed a relative time for a realtime timer,
   1410       1.67   nathanw 	 * convert it to absolute; if an absolute time for a virtual
   1411       1.67   nathanw 	 * timer, convert it to relative and make sure we don't set it
   1412       1.67   nathanw 	 * to zero, which would cancel the timer, or let it go
   1413       1.67   nathanw 	 * negative, which would confuse the comparison tests.
   1414       1.67   nathanw 	 */
   1415      1.207   thorpej 	if (timespecisset(&it->it_time.it_value)) {
   1416      1.207   thorpej 		if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
   1417      1.101    kardel 			if ((flags & TIMER_ABSTIME) == 0) {
   1418      1.207   thorpej 				if (it->it_clockid == CLOCK_REALTIME) {
   1419      1.168      yamt 					getnanotime(&now);
   1420      1.168      yamt 				} else { /* CLOCK_MONOTONIC */
   1421      1.168      yamt 					getnanouptime(&now);
   1422      1.168      yamt 				}
   1423      1.207   thorpej 				timespecadd(&it->it_time.it_value, &now,
   1424      1.207   thorpej 				    &it->it_time.it_value);
   1425      1.101    kardel 			}
   1426       1.67   nathanw 		} else {
   1427       1.92      cube 			if ((flags & TIMER_ABSTIME) != 0) {
   1428      1.150  christos 				getnanotime(&now);
   1429      1.207   thorpej 				timespecsub(&it->it_time.it_value, &now,
   1430      1.207   thorpej 				    &it->it_time.it_value);
   1431      1.207   thorpej 				if (!timespecisset(&it->it_time.it_value) ||
   1432      1.207   thorpej 				    it->it_time.it_value.tv_sec < 0) {
   1433      1.207   thorpej 					it->it_time.it_value.tv_sec = 0;
   1434      1.207   thorpej 					it->it_time.it_value.tv_nsec = 1;
   1435       1.67   nathanw 				}
   1436       1.67   nathanw 			}
   1437       1.67   nathanw 		}
   1438       1.67   nathanw 	}
   1439       1.67   nathanw 
   1440      1.207   thorpej 	error = itimer_settime(it);
   1441      1.198  riastrad 	if (error == ERESTART) {
   1442      1.207   thorpej 		KASSERT(!CLOCK_VIRTUAL_P(it->it_clockid));
   1443      1.198  riastrad 		goto restart;
   1444      1.198  riastrad 	}
   1445      1.198  riastrad 	KASSERT(error == 0);
   1446      1.207   thorpej 	itimer_unlock();
   1447       1.63   thorpej 
   1448      1.150  christos 	if (ovalue)
   1449      1.150  christos 		*ovalue = oval;
   1450       1.63   thorpej 
   1451      1.217  riastrad 	return 0;
   1452       1.63   thorpej }
   1453       1.63   thorpej 
   1454      1.207   thorpej /*
   1455      1.207   thorpej  * sys___timer_gettime50:
   1456      1.207   thorpej  *
   1457      1.207   thorpej  *	System call to return the time remaining until a POSIX timer fires.
   1458      1.207   thorpej  */
   1459       1.63   thorpej int
   1460      1.156  christos sys___timer_gettime50(struct lwp *l,
   1461      1.156  christos     const struct sys___timer_gettime50_args *uap, register_t *retval)
   1462       1.63   thorpej {
   1463      1.135       dsl 	/* {
   1464       1.63   thorpej 		syscallarg(timer_t) timerid;
   1465       1.63   thorpej 		syscallarg(struct itimerspec *) value;
   1466      1.135       dsl 	} */
   1467       1.63   thorpej 	struct itimerspec its;
   1468       1.92      cube 	int error;
   1469       1.92      cube 
   1470       1.92      cube 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
   1471       1.92      cube 	    &its)) != 0)
   1472       1.92      cube 		return error;
   1473       1.92      cube 
   1474       1.92      cube 	return copyout(&its, SCARG(uap, value), sizeof(its));
   1475       1.92      cube }
   1476       1.92      cube 
   1477       1.92      cube int
   1478       1.92      cube dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
   1479       1.92      cube {
   1480      1.207   thorpej 	struct itimer *it;
   1481      1.142        ad 	struct ptimers *pts;
   1482       1.63   thorpej 
   1483      1.142        ad 	pts = p->p_timers;
   1484      1.142        ad 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
   1485      1.217  riastrad 		return EINVAL;
   1486      1.207   thorpej 	itimer_lock();
   1487      1.207   thorpej 	if ((it = pts->pts_timers[timerid]) == NULL) {
   1488      1.207   thorpej 		itimer_unlock();
   1489      1.217  riastrad 		return EINVAL;
   1490      1.142        ad 	}
   1491      1.207   thorpej 	itimer_gettime(it, its);
   1492      1.207   thorpej 	itimer_unlock();
   1493       1.63   thorpej 
   1494       1.92      cube 	return 0;
   1495       1.63   thorpej }
   1496       1.63   thorpej 
   1497       1.63   thorpej /*
   1498      1.207   thorpej  * sys_timer_getoverrun:
   1499      1.207   thorpej  *
   1500      1.207   thorpej  *	System call to return the number of times a POSIX timer has
   1501      1.207   thorpej  *	expired while a notification was already pending.  The counter
   1502      1.207   thorpej  *	is reset when a timer expires and a notification can be posted.
   1503       1.63   thorpej  */
   1504       1.63   thorpej int
   1505      1.140      yamt sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap,
   1506      1.140      yamt     register_t *retval)
   1507       1.63   thorpej {
   1508      1.135       dsl 	/* {
   1509       1.63   thorpej 		syscallarg(timer_t) timerid;
   1510      1.135       dsl 	} */
   1511       1.63   thorpej 	struct proc *p = l->l_proc;
   1512      1.142        ad 	struct ptimers *pts;
   1513       1.63   thorpej 	int timerid;
   1514      1.207   thorpej 	struct itimer *it;
   1515       1.63   thorpej 	struct ptimer *pt;
   1516       1.63   thorpej 
   1517       1.63   thorpej 	timerid = SCARG(uap, timerid);
   1518       1.63   thorpej 
   1519      1.142        ad 	pts = p->p_timers;
   1520      1.142        ad 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
   1521      1.217  riastrad 		return EINVAL;
   1522      1.207   thorpej 	itimer_lock();
   1523      1.207   thorpej 	if ((it = pts->pts_timers[timerid]) == NULL) {
   1524      1.207   thorpej 		itimer_unlock();
   1525      1.217  riastrad 		return EINVAL;
   1526      1.142        ad 	}
   1527      1.207   thorpej 	pt = container_of(it, struct ptimer, pt_itimer);
   1528       1.63   thorpej 	*retval = pt->pt_poverruns;
   1529      1.187  christos 	if (*retval >= DELAYTIMER_MAX)
   1530      1.187  christos 		*retval = DELAYTIMER_MAX;
   1531      1.207   thorpej 	itimer_unlock();
   1532       1.63   thorpej 
   1533      1.217  riastrad 	return 0;
   1534       1.63   thorpej }
   1535       1.63   thorpej 
   1536       1.63   thorpej /*
   1537      1.207   thorpej  * sys___getitimer50:
   1538      1.207   thorpej  *
   1539      1.207   thorpej  *	System call to get the time remaining before a BSD timer fires.
   1540       1.63   thorpej  */
   1541       1.63   thorpej int
   1542      1.156  christos sys___getitimer50(struct lwp *l, const struct sys___getitimer50_args *uap,
   1543      1.140      yamt     register_t *retval)
   1544       1.63   thorpej {
   1545      1.135       dsl 	/* {
   1546       1.63   thorpej 		syscallarg(int) which;
   1547       1.63   thorpej 		syscallarg(struct itimerval *) itv;
   1548      1.135       dsl 	} */
   1549       1.63   thorpej 	struct proc *p = l->l_proc;
   1550       1.63   thorpej 	struct itimerval aitv;
   1551       1.91      cube 	int error;
   1552       1.91      cube 
   1553      1.191      maxv 	memset(&aitv, 0, sizeof(aitv));
   1554       1.91      cube 	error = dogetitimer(p, SCARG(uap, which), &aitv);
   1555       1.91      cube 	if (error)
   1556       1.91      cube 		return error;
   1557      1.217  riastrad 	return copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval));
   1558       1.91      cube }
   1559       1.63   thorpej 
   1560       1.91      cube int
   1561       1.91      cube dogetitimer(struct proc *p, int which, struct itimerval *itvp)
   1562       1.91      cube {
   1563      1.142        ad 	struct ptimers *pts;
   1564      1.207   thorpej 	struct itimer *it;
   1565      1.150  christos 	struct itimerspec its;
   1566       1.63   thorpej 
   1567      1.170  christos 	if ((u_int)which > ITIMER_MONOTONIC)
   1568      1.217  riastrad 		return EINVAL;
   1569       1.63   thorpej 
   1570      1.207   thorpej 	itimer_lock();
   1571      1.142        ad 	pts = p->p_timers;
   1572      1.207   thorpej 	if (pts == NULL || (it = pts->pts_timers[which]) == NULL) {
   1573       1.91      cube 		timerclear(&itvp->it_value);
   1574       1.91      cube 		timerclear(&itvp->it_interval);
   1575      1.150  christos 	} else {
   1576      1.207   thorpej 		itimer_gettime(it, &its);
   1577      1.151  christos 		TIMESPEC_TO_TIMEVAL(&itvp->it_value, &its.it_value);
   1578      1.151  christos 		TIMESPEC_TO_TIMEVAL(&itvp->it_interval, &its.it_interval);
   1579      1.150  christos 	}
   1580      1.207   thorpej 	itimer_unlock();
   1581       1.63   thorpej 
   1582       1.91      cube 	return 0;
   1583        1.1       cgd }
   1584        1.1       cgd 
   1585      1.207   thorpej /*
   1586      1.207   thorpej  * sys___setitimer50:
   1587      1.207   thorpej  *
   1588      1.207   thorpej  *	System call to set/arm a BSD timer.
   1589      1.207   thorpej  */
   1590        1.3    andrew int
   1591      1.156  christos sys___setitimer50(struct lwp *l, const struct sys___setitimer50_args *uap,
   1592      1.140      yamt     register_t *retval)
   1593       1.15   thorpej {
   1594      1.135       dsl 	/* {
   1595       1.30   mycroft 		syscallarg(int) which;
   1596       1.24       cgd 		syscallarg(const struct itimerval *) itv;
   1597       1.11       cgd 		syscallarg(struct itimerval *) oitv;
   1598      1.135       dsl 	} */
   1599       1.63   thorpej 	struct proc *p = l->l_proc;
   1600       1.30   mycroft 	int which = SCARG(uap, which);
   1601      1.156  christos 	struct sys___getitimer50_args getargs;
   1602       1.91      cube 	const struct itimerval *itvp;
   1603        1.1       cgd 	struct itimerval aitv;
   1604       1.91      cube 	int error;
   1605        1.1       cgd 
   1606       1.11       cgd 	itvp = SCARG(uap, itv);
   1607       1.63   thorpej 	if (itvp &&
   1608      1.174  dholland 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval))) != 0)
   1609      1.217  riastrad 		return error;
   1610       1.21       cgd 	if (SCARG(uap, oitv) != NULL) {
   1611       1.30   mycroft 		SCARG(&getargs, which) = which;
   1612       1.21       cgd 		SCARG(&getargs, itv) = SCARG(uap, oitv);
   1613      1.156  christos 		if ((error = sys___getitimer50(l, &getargs, retval)) != 0)
   1614      1.217  riastrad 			return error;
   1615       1.21       cgd 	}
   1616        1.1       cgd 	if (itvp == 0)
   1617      1.217  riastrad 		return 0;
   1618       1.91      cube 
   1619       1.91      cube 	return dosetitimer(p, which, &aitv);
   1620       1.91      cube }
   1621       1.91      cube 
   1622       1.91      cube int
   1623       1.91      cube dosetitimer(struct proc *p, int which, struct itimerval *itvp)
   1624       1.91      cube {
   1625      1.150  christos 	struct timespec now;
   1626      1.142        ad 	struct ptimers *pts;
   1627      1.207   thorpej 	struct ptimer *spare;
   1628      1.207   thorpej 	struct itimer *it;
   1629      1.207   thorpej 	struct itlist *itl;
   1630      1.198  riastrad 	int error;
   1631       1.91      cube 
   1632      1.211    simonb 	if ((u_int)which > ITIMER_MONOTONIC)
   1633      1.217  riastrad 		return EINVAL;
   1634       1.91      cube 	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
   1635      1.217  riastrad 		return EINVAL;
   1636       1.63   thorpej 
   1637       1.63   thorpej 	/*
   1638       1.63   thorpej 	 * Don't bother allocating data structures if the process just
   1639       1.63   thorpej 	 * wants to clear the timer.
   1640       1.63   thorpej 	 */
   1641      1.142        ad 	spare = NULL;
   1642      1.142        ad 	pts = p->p_timers;
   1643      1.142        ad  retry:
   1644      1.142        ad 	if (!timerisset(&itvp->it_value) && (pts == NULL ||
   1645      1.142        ad 	    pts->pts_timers[which] == NULL))
   1646      1.217  riastrad 		return 0;
   1647      1.142        ad 	if (pts == NULL)
   1648      1.207   thorpej 		pts = ptimers_alloc(p);
   1649      1.207   thorpej 	itimer_lock();
   1650      1.207   thorpej  restart:
   1651      1.207   thorpej 	it = pts->pts_timers[which];
   1652      1.207   thorpej 	if (it == NULL) {
   1653      1.207   thorpej 		struct ptimer *pt;
   1654      1.207   thorpej 
   1655      1.142        ad 		if (spare == NULL) {
   1656      1.207   thorpej 			itimer_unlock();
   1657      1.207   thorpej 			spare = kmem_zalloc(sizeof(*spare), KM_SLEEP);
   1658      1.142        ad 			goto retry;
   1659      1.142        ad 		}
   1660      1.142        ad 		pt = spare;
   1661      1.142        ad 		spare = NULL;
   1662      1.207   thorpej 
   1663      1.207   thorpej 		it = &pt->pt_itimer;
   1664       1.63   thorpej 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
   1665       1.76  christos 		pt->pt_ev.sigev_value.sival_int = which;
   1666      1.149  christos 
   1667       1.63   thorpej 		switch (which) {
   1668       1.63   thorpej 		case ITIMER_REAL:
   1669      1.170  christos 		case ITIMER_MONOTONIC:
   1670      1.207   thorpej 			itl = NULL;
   1671       1.63   thorpej 			pt->pt_ev.sigev_signo = SIGALRM;
   1672       1.63   thorpej 			break;
   1673       1.63   thorpej 		case ITIMER_VIRTUAL:
   1674      1.208   thorpej 			itl = &pts->pts_virtual;
   1675       1.63   thorpej 			pt->pt_ev.sigev_signo = SIGVTALRM;
   1676       1.63   thorpej 			break;
   1677       1.63   thorpej 		case ITIMER_PROF:
   1678      1.208   thorpej 			itl = &pts->pts_prof;
   1679       1.63   thorpej 			pt->pt_ev.sigev_signo = SIGPROF;
   1680       1.63   thorpej 			break;
   1681      1.209  christos 		default:
   1682      1.209  christos 			panic("%s: can't happen %d", __func__, which);
   1683        1.1       cgd 		}
   1684      1.207   thorpej 		itimer_init(it, &ptimer_itimer_ops, which, itl);
   1685      1.207   thorpej 		pt->pt_proc = p;
   1686      1.207   thorpej 		pt->pt_entry = which;
   1687      1.207   thorpej 
   1688      1.207   thorpej 		pts->pts_timers[which] = it;
   1689      1.142        ad 	}
   1690       1.63   thorpej 
   1691      1.207   thorpej 	TIMEVAL_TO_TIMESPEC(&itvp->it_value, &it->it_time.it_value);
   1692      1.207   thorpej 	TIMEVAL_TO_TIMESPEC(&itvp->it_interval, &it->it_time.it_interval);
   1693      1.150  christos 
   1694      1.215  riastrad 	error = 0;
   1695      1.207   thorpej 	if (timespecisset(&it->it_time.it_value)) {
   1696       1.67   nathanw 		/* Convert to absolute time */
   1697      1.101    kardel 		/* XXX need to wrap in splclock for timecounters case? */
   1698      1.170  christos 		switch (which) {
   1699      1.170  christos 		case ITIMER_REAL:
   1700      1.170  christos 			getnanotime(&now);
   1701      1.215  riastrad 			if (!timespecaddok(&it->it_time.it_value, &now)) {
   1702      1.215  riastrad 				error = EINVAL;
   1703      1.215  riastrad 				goto out;
   1704      1.215  riastrad 			}
   1705      1.207   thorpej 			timespecadd(&it->it_time.it_value, &now,
   1706      1.207   thorpej 			    &it->it_time.it_value);
   1707      1.170  christos 			break;
   1708      1.170  christos 		case ITIMER_MONOTONIC:
   1709      1.170  christos 			getnanouptime(&now);
   1710      1.215  riastrad 			if (!timespecaddok(&it->it_time.it_value, &now)) {
   1711      1.215  riastrad 				error = EINVAL;
   1712      1.215  riastrad 				goto out;
   1713      1.215  riastrad 			}
   1714      1.207   thorpej 			timespecadd(&it->it_time.it_value, &now,
   1715      1.207   thorpej 			    &it->it_time.it_value);
   1716      1.170  christos 			break;
   1717      1.170  christos 		default:
   1718      1.170  christos 			break;
   1719      1.170  christos 		}
   1720       1.67   nathanw 	}
   1721      1.215  riastrad 
   1722      1.207   thorpej 	error = itimer_settime(it);
   1723      1.198  riastrad 	if (error == ERESTART) {
   1724      1.207   thorpej 		KASSERT(!CLOCK_VIRTUAL_P(it->it_clockid));
   1725      1.198  riastrad 		goto restart;
   1726      1.198  riastrad 	}
   1727      1.198  riastrad 	KASSERT(error == 0);
   1728      1.215  riastrad out:
   1729      1.207   thorpej 	itimer_unlock();
   1730      1.142        ad 	if (spare != NULL)
   1731      1.207   thorpej 		kmem_free(spare, sizeof(*spare));
   1732       1.63   thorpej 
   1733      1.215  riastrad 	return error;
   1734        1.1       cgd }
   1735        1.1       cgd 
   1736        1.1       cgd /*
   1737      1.207   thorpej  * ptimer_tick:
   1738      1.207   thorpej  *
   1739      1.207   thorpej  *	Called from hardclock() to decrement per-process virtual timers.
   1740        1.1       cgd  */
   1741        1.3    andrew void
   1742      1.207   thorpej ptimer_tick(lwp_t *l, bool user)
   1743        1.6       cgd {
   1744       1.63   thorpej 	struct ptimers *pts;
   1745      1.207   thorpej 	struct itimer *it;
   1746      1.142        ad 	proc_t *p;
   1747      1.142        ad 
   1748      1.142        ad 	p = l->l_proc;
   1749      1.142        ad 	if (p->p_timers == NULL)
   1750      1.142        ad 		return;
   1751      1.142        ad 
   1752      1.207   thorpej 	itimer_lock();
   1753      1.142        ad 	if ((pts = l->l_proc->p_timers) != NULL) {
   1754       1.63   thorpej 		/*
   1755      1.142        ad 		 * Run current process's virtual and profile time, as needed.
   1756       1.63   thorpej 		 */
   1757      1.207   thorpej 		if (user && (it = LIST_FIRST(&pts->pts_virtual)) != NULL)
   1758      1.207   thorpej 			if (itimer_decr(it, tick * 1000))
   1759      1.207   thorpej 				(*it->it_ops->ito_fire)(it);
   1760      1.207   thorpej 		if ((it = LIST_FIRST(&pts->pts_prof)) != NULL)
   1761      1.207   thorpej 			if (itimer_decr(it, tick * 1000))
   1762      1.207   thorpej 				(*it->it_ops->ito_fire)(it);
   1763      1.142        ad 	}
   1764      1.207   thorpej 	itimer_unlock();
   1765      1.142        ad }
   1766      1.142        ad 
   1767      1.207   thorpej /*
   1768      1.207   thorpej  * ptimer_intr:
   1769      1.207   thorpej  *
   1770      1.207   thorpej  *	Software interrupt handler for processing per-process
   1771      1.207   thorpej  *	timer expiration.
   1772      1.207   thorpej  */
   1773      1.142        ad static void
   1774      1.207   thorpej ptimer_intr(void *cookie)
   1775      1.142        ad {
   1776      1.142        ad 	ksiginfo_t ksi;
   1777      1.207   thorpej 	struct itimer *it;
   1778      1.142        ad 	struct ptimer *pt;
   1779      1.142        ad 	proc_t *p;
   1780      1.217  riastrad 
   1781      1.205        ad 	mutex_enter(&proc_lock);
   1782      1.207   thorpej 	itimer_lock();
   1783      1.210   thorpej 	while ((pt = TAILQ_FIRST(&ptimer_queue)) != NULL) {
   1784      1.210   thorpej 		it = &pt->pt_itimer;
   1785      1.142        ad 
   1786      1.210   thorpej 		TAILQ_REMOVE(&ptimer_queue, pt, pt_chain);
   1787      1.210   thorpej 		KASSERT(pt->pt_queued);
   1788      1.210   thorpej 		pt->pt_queued = false;
   1789      1.207   thorpej 
   1790      1.207   thorpej 		p = pt->pt_proc;
   1791      1.207   thorpej 		if (p->p_timers == NULL) {
   1792      1.154  wrstuden 			/* Process is dying. */
   1793      1.142        ad 			continue;
   1794      1.154  wrstuden 		}
   1795      1.172     rmind 		if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL) {
   1796      1.142        ad 			continue;
   1797      1.142        ad 		}
   1798      1.142        ad 		if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo)) {
   1799      1.207   thorpej 			it->it_overruns++;
   1800      1.142        ad 			continue;
   1801       1.64   nathanw 		}
   1802      1.142        ad 
   1803      1.142        ad 		KSI_INIT(&ksi);
   1804      1.142        ad 		ksi.ksi_signo = pt->pt_ev.sigev_signo;
   1805      1.142        ad 		ksi.ksi_code = SI_TIMER;
   1806      1.142        ad 		ksi.ksi_value = pt->pt_ev.sigev_value;
   1807      1.207   thorpej 		pt->pt_poverruns = it->it_overruns;
   1808      1.207   thorpej 		it->it_overruns = 0;
   1809      1.207   thorpej 		itimer_unlock();
   1810      1.142        ad 		kpsignal(p, &ksi, NULL);
   1811      1.207   thorpej 		itimer_lock();
   1812       1.63   thorpej 	}
   1813      1.207   thorpej 	itimer_unlock();
   1814      1.205        ad 	mutex_exit(&proc_lock);
   1815       1.63   thorpej }
   1816