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