Home | History | Annotate | Line # | Download | only in kern
subr_time.c revision 1.35.4.2
      1  1.35.4.2    martin /*	$NetBSD: subr_time.c,v 1.35.4.2 2024/10/11 17:12:28 martin Exp $	*/
      2       1.1     pooka 
      3       1.1     pooka /*
      4       1.1     pooka  * Copyright (c) 1982, 1986, 1989, 1993
      5       1.1     pooka  *	The Regents of the University of California.  All rights reserved.
      6       1.1     pooka  *
      7       1.1     pooka  * Redistribution and use in source and binary forms, with or without
      8       1.1     pooka  * modification, are permitted provided that the following conditions
      9       1.1     pooka  * are met:
     10       1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     11       1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     12       1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     14       1.1     pooka  *    documentation and/or other materials provided with the distribution.
     15       1.1     pooka  * 3. Neither the name of the University nor the names of its contributors
     16       1.1     pooka  *    may be used to endorse or promote products derived from this software
     17       1.1     pooka  *    without specific prior written permission.
     18       1.1     pooka  *
     19       1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1     pooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1     pooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1     pooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1     pooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1     pooka  * SUCH DAMAGE.
     30       1.1     pooka  *
     31       1.1     pooka  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
     32       1.1     pooka  *	@(#)kern_time.c 8.4 (Berkeley) 5/26/95
     33       1.1     pooka  */
     34       1.1     pooka 
     35       1.1     pooka #include <sys/cdefs.h>
     36  1.35.4.2    martin __KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.35.4.2 2024/10/11 17:12:28 martin Exp $");
     37       1.1     pooka 
     38       1.1     pooka #include <sys/param.h>
     39       1.1     pooka #include <sys/kernel.h>
     40      1.18  christos #include <sys/proc.h>
     41      1.18  christos #include <sys/kauth.h>
     42      1.18  christos #include <sys/lwp.h>
     43       1.1     pooka #include <sys/timex.h>
     44       1.1     pooka #include <sys/time.h>
     45       1.1     pooka #include <sys/timetc.h>
     46       1.2        ad #include <sys/intr.h>
     47       1.1     pooka 
     48       1.1     pooka /*
     49       1.1     pooka  * Compute number of hz until specified time.  Used to compute second
     50       1.1     pooka  * argument to callout_reset() from an absolute time.
     51       1.1     pooka  */
     52       1.1     pooka int
     53       1.4  christos tvhzto(const struct timeval *tvp)
     54       1.1     pooka {
     55       1.1     pooka 	struct timeval now, tv;
     56       1.1     pooka 
     57       1.1     pooka 	tv = *tvp;	/* Don't modify original tvp. */
     58       1.1     pooka 	getmicrotime(&now);
     59       1.1     pooka 	timersub(&tv, &now, &tv);
     60       1.1     pooka 	return tvtohz(&tv);
     61       1.1     pooka }
     62       1.1     pooka 
     63       1.1     pooka /*
     64       1.1     pooka  * Compute number of ticks in the specified amount of time.
     65       1.1     pooka  */
     66       1.1     pooka int
     67       1.4  christos tvtohz(const struct timeval *tv)
     68       1.1     pooka {
     69       1.1     pooka 	unsigned long ticks;
     70       1.1     pooka 	long sec, usec;
     71       1.1     pooka 
     72       1.1     pooka 	/*
     73       1.1     pooka 	 * If the number of usecs in the whole seconds part of the time
     74       1.1     pooka 	 * difference fits in a long, then the total number of usecs will
     75       1.1     pooka 	 * fit in an unsigned long.  Compute the total and convert it to
     76       1.1     pooka 	 * ticks, rounding up and adding 1 to allow for the current tick
     77       1.1     pooka 	 * to expire.  Rounding also depends on unsigned long arithmetic
     78       1.1     pooka 	 * to avoid overflow.
     79       1.1     pooka 	 *
     80       1.1     pooka 	 * Otherwise, if the number of ticks in the whole seconds part of
     81       1.1     pooka 	 * the time difference fits in a long, then convert the parts to
     82       1.1     pooka 	 * ticks separately and add, using similar rounding methods and
     83       1.1     pooka 	 * overflow avoidance.  This method would work in the previous
     84       1.1     pooka 	 * case, but it is slightly slower and assumes that hz is integral.
     85       1.1     pooka 	 *
     86       1.1     pooka 	 * Otherwise, round the time difference down to the maximum
     87       1.1     pooka 	 * representable value.
     88       1.1     pooka 	 *
     89       1.1     pooka 	 * If ints are 32-bit, then the maximum value for any timeout in
     90       1.1     pooka 	 * 10ms ticks is 248 days.
     91       1.1     pooka 	 */
     92       1.1     pooka 	sec = tv->tv_sec;
     93       1.1     pooka 	usec = tv->tv_usec;
     94       1.1     pooka 
     95  1.35.4.2    martin 	KASSERT(usec >= 0);
     96  1.35.4.2    martin 	KASSERT(usec < 1000000);
     97       1.8  drochner 
     98       1.8  drochner 	/* catch overflows in conversion time_t->int */
     99       1.8  drochner 	if (tv->tv_sec > INT_MAX)
    100       1.8  drochner 		return INT_MAX;
    101       1.8  drochner 	if (tv->tv_sec < 0)
    102       1.8  drochner 		return 0;
    103       1.1     pooka 
    104       1.8  drochner 	if (sec < 0 || (sec == 0 && usec == 0)) {
    105       1.1     pooka 		/*
    106       1.1     pooka 		 * Would expire now or in the past.  Return 0 ticks.
    107       1.4  christos 		 * This is different from the legacy tvhzto() interface,
    108       1.1     pooka 		 * and callers need to check for it.
    109       1.1     pooka 		 */
    110       1.1     pooka 		ticks = 0;
    111       1.1     pooka 	} else if (sec <= (LONG_MAX / 1000000))
    112       1.1     pooka 		ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
    113       1.1     pooka 		    / tick) + 1;
    114       1.1     pooka 	else if (sec <= (LONG_MAX / hz))
    115       1.1     pooka 		ticks = (sec * hz) +
    116       1.1     pooka 		    (((unsigned long)usec + (tick - 1)) / tick) + 1;
    117       1.1     pooka 	else
    118       1.1     pooka 		ticks = LONG_MAX;
    119       1.1     pooka 
    120       1.1     pooka 	if (ticks > INT_MAX)
    121       1.1     pooka 		ticks = INT_MAX;
    122       1.1     pooka 
    123       1.1     pooka 	return ((int)ticks);
    124       1.1     pooka }
    125       1.1     pooka 
    126       1.4  christos int
    127       1.4  christos tshzto(const struct timespec *tsp)
    128       1.4  christos {
    129       1.4  christos 	struct timespec now, ts;
    130       1.4  christos 
    131       1.4  christos 	ts = *tsp;	/* Don't modify original tsp. */
    132       1.4  christos 	getnanotime(&now);
    133       1.4  christos 	timespecsub(&ts, &now, &ts);
    134       1.4  christos 	return tstohz(&ts);
    135       1.4  christos }
    136       1.9  christos 
    137       1.9  christos int
    138       1.9  christos tshztoup(const struct timespec *tsp)
    139       1.9  christos {
    140       1.9  christos 	struct timespec now, ts;
    141       1.9  christos 
    142       1.9  christos 	ts = *tsp;	/* Don't modify original tsp. */
    143       1.9  christos 	getnanouptime(&now);
    144       1.9  christos 	timespecsub(&ts, &now, &ts);
    145       1.9  christos 	return tstohz(&ts);
    146       1.9  christos }
    147       1.9  christos 
    148       1.1     pooka /*
    149       1.1     pooka  * Compute number of ticks in the specified amount of time.
    150       1.1     pooka  */
    151       1.1     pooka int
    152       1.4  christos tstohz(const struct timespec *ts)
    153       1.1     pooka {
    154       1.1     pooka 	struct timeval tv;
    155       1.1     pooka 
    156       1.1     pooka 	/*
    157       1.1     pooka 	 * usec has great enough resolution for hz, so convert to a
    158       1.1     pooka 	 * timeval and use tvtohz() above.
    159       1.1     pooka 	 */
    160       1.1     pooka 	TIMESPEC_TO_TIMEVAL(&tv, ts);
    161       1.1     pooka 	return tvtohz(&tv);
    162       1.1     pooka }
    163       1.1     pooka 
    164       1.1     pooka /*
    165       1.1     pooka  * Check that a proposed value to load into the .it_value or
    166       1.1     pooka  * .it_interval part of an interval timer is acceptable, and
    167       1.1     pooka  * fix it to have at least minimal value (i.e. if it is less
    168      1.15  christos  * than the resolution of the clock, round it up.). We don't
    169      1.15  christos  * timeout the 0,0 value because this means to disable the
    170      1.15  christos  * timer or the interval.
    171       1.1     pooka  */
    172       1.1     pooka int
    173       1.1     pooka itimerfix(struct timeval *tv)
    174       1.1     pooka {
    175       1.1     pooka 
    176      1.12  christos 	if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    177      1.12  christos 		return EINVAL;
    178      1.15  christos 	if (tv->tv_sec < 0)
    179      1.12  christos 		return ETIMEDOUT;
    180      1.15  christos 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    181       1.1     pooka 		tv->tv_usec = tick;
    182      1.12  christos 	return 0;
    183       1.1     pooka }
    184       1.1     pooka 
    185       1.1     pooka int
    186       1.1     pooka itimespecfix(struct timespec *ts)
    187       1.1     pooka {
    188       1.1     pooka 
    189      1.12  christos 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
    190      1.12  christos 		return EINVAL;
    191      1.15  christos 	if (ts->tv_sec < 0)
    192      1.12  christos 		return ETIMEDOUT;
    193      1.15  christos 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
    194       1.1     pooka 		ts->tv_nsec = tick * 1000;
    195      1.12  christos 	return 0;
    196       1.1     pooka }
    197       1.5     rmind 
    198       1.5     rmind int
    199       1.5     rmind inittimeleft(struct timespec *ts, struct timespec *sleepts)
    200       1.5     rmind {
    201       1.5     rmind 
    202       1.5     rmind 	if (itimespecfix(ts)) {
    203       1.5     rmind 		return -1;
    204       1.5     rmind 	}
    205      1.35  riastrad 	KASSERT(ts->tv_sec >= 0);
    206       1.5     rmind 	getnanouptime(sleepts);
    207       1.5     rmind 	return 0;
    208       1.5     rmind }
    209       1.5     rmind 
    210       1.5     rmind int
    211       1.5     rmind gettimeleft(struct timespec *ts, struct timespec *sleepts)
    212       1.5     rmind {
    213      1.35  riastrad 	struct timespec now, sleptts;
    214      1.35  riastrad 
    215      1.35  riastrad 	KASSERT(ts->tv_sec >= 0);
    216       1.5     rmind 
    217       1.5     rmind 	/*
    218       1.5     rmind 	 * Reduce ts by elapsed time based on monotonic time scale.
    219       1.5     rmind 	 */
    220      1.35  riastrad 	getnanouptime(&now);
    221      1.35  riastrad 	KASSERT(timespeccmp(sleepts, &now, <=));
    222      1.35  riastrad 	timespecsub(&now, sleepts, &sleptts);
    223      1.35  riastrad 	*sleepts = now;
    224      1.35  riastrad 
    225      1.35  riastrad 	if (timespeccmp(ts, &sleptts, <=)) { /* timed out */
    226      1.35  riastrad 		timespecclear(ts);
    227      1.35  riastrad 		return 0;
    228      1.35  riastrad 	}
    229       1.5     rmind 	timespecsub(ts, &sleptts, ts);
    230       1.5     rmind 
    231       1.5     rmind 	return tstohz(ts);
    232       1.5     rmind }
    233       1.5     rmind 
    234      1.20  christos void
    235      1.20  christos clock_timeleft(clockid_t clockid, struct timespec *ts, struct timespec *sleepts)
    236      1.20  christos {
    237      1.20  christos 	struct timespec sleptts;
    238      1.20  christos 
    239      1.20  christos 	clock_gettime1(clockid, &sleptts);
    240      1.20  christos 	timespecadd(ts, sleepts, ts);
    241      1.20  christos 	timespecsub(ts, &sleptts, ts);
    242      1.20  christos 	*sleepts = sleptts;
    243      1.20  christos }
    244      1.20  christos 
    245      1.11    martin int
    246      1.11    martin clock_gettime1(clockid_t clock_id, struct timespec *ts)
    247      1.11    martin {
    248      1.18  christos 	int error;
    249      1.18  christos 	struct proc *p;
    250      1.18  christos 
    251      1.18  christos #define CPUCLOCK_ID_MASK (~(CLOCK_THREAD_CPUTIME_ID|CLOCK_PROCESS_CPUTIME_ID))
    252      1.18  christos 	if (clock_id & CLOCK_PROCESS_CPUTIME_ID) {
    253      1.18  christos 		pid_t pid = clock_id & CPUCLOCK_ID_MASK;
    254  1.35.4.2    martin 		struct timeval cputime;
    255      1.18  christos 
    256      1.25        ad 		mutex_enter(&proc_lock);
    257      1.18  christos 		p = pid == 0 ? curproc : proc_find(pid);
    258      1.18  christos 		if (p == NULL) {
    259      1.25        ad 			mutex_exit(&proc_lock);
    260      1.18  christos 			return ESRCH;
    261      1.18  christos 		}
    262  1.35.4.2    martin 		mutex_enter(p->p_lock);
    263  1.35.4.2    martin 		calcru(p, /*usertime*/NULL, /*systime*/NULL, /*intrtime*/NULL,
    264  1.35.4.2    martin 		    &cputime);
    265  1.35.4.2    martin 		mutex_exit(p->p_lock);
    266      1.25        ad 		mutex_exit(&proc_lock);
    267      1.18  christos 
    268      1.18  christos 		// XXX: Perhaps create a special kauth type
    269      1.31  christos 		error = kauth_authorize_process(kauth_cred_get(),
    270      1.18  christos 		    KAUTH_PROCESS_PTRACE, p,
    271      1.18  christos 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    272      1.18  christos 		if (error)
    273      1.18  christos 			return error;
    274  1.35.4.2    martin 
    275  1.35.4.2    martin 		TIMEVAL_TO_TIMESPEC(&cputime, ts);
    276  1.35.4.2    martin 		return 0;
    277      1.18  christos 	} else if (clock_id & CLOCK_THREAD_CPUTIME_ID) {
    278      1.18  christos 		struct lwp *l;
    279      1.18  christos 		lwpid_t lid = clock_id & CPUCLOCK_ID_MASK;
    280  1.35.4.2    martin 		struct bintime tm = {0, 0};
    281  1.35.4.2    martin 
    282      1.18  christos 		p = curproc;
    283      1.18  christos 		mutex_enter(p->p_lock);
    284      1.18  christos 		l = lid == 0 ? curlwp : lwp_find(p, lid);
    285      1.18  christos 		if (l == NULL) {
    286      1.18  christos 			mutex_exit(p->p_lock);
    287      1.18  christos 			return ESRCH;
    288      1.18  christos 		}
    289  1.35.4.2    martin 		addrulwp(l, &tm);
    290      1.18  christos 		mutex_exit(p->p_lock);
    291      1.18  christos 
    292  1.35.4.2    martin 		bintime2timespec(&tm, ts);
    293      1.18  christos 		return 0;
    294      1.18  christos 	}
    295      1.11    martin 
    296      1.11    martin 	switch (clock_id) {
    297      1.11    martin 	case CLOCK_REALTIME:
    298      1.11    martin 		nanotime(ts);
    299      1.11    martin 		break;
    300      1.11    martin 	case CLOCK_MONOTONIC:
    301      1.11    martin 		nanouptime(ts);
    302      1.11    martin 		break;
    303      1.11    martin 	default:
    304      1.11    martin 		return EINVAL;
    305      1.11    martin 	}
    306      1.11    martin 
    307      1.11    martin 	return 0;
    308      1.11    martin }
    309      1.11    martin 
    310       1.5     rmind /*
    311       1.5     rmind  * Calculate delta and convert from struct timespec to the ticks.
    312       1.5     rmind  */
    313       1.5     rmind int
    314      1.10  christos ts2timo(clockid_t clock_id, int flags, struct timespec *ts,
    315      1.10  christos     int *timo, struct timespec *start)
    316       1.5     rmind {
    317      1.14  christos 	int error;
    318      1.28       nia 	struct timespec tsd;
    319       1.5     rmind 
    320      1.21     kamil 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000L)
    321      1.21     kamil 		return EINVAL;
    322      1.21     kamil 
    323      1.30       nia 	if ((flags & TIMER_ABSTIME) != 0 || start != NULL) {
    324      1.29       nia 		error = clock_gettime1(clock_id, &tsd);
    325      1.26       nia 		if (error != 0)
    326      1.17  christos 			return error;
    327      1.29       nia 		if (start != NULL)
    328      1.29       nia 			*start = tsd;
    329      1.26       nia 	}
    330      1.10  christos 
    331      1.30       nia 	if ((flags & TIMER_ABSTIME) != 0) {
    332      1.34  riastrad 		if (!timespecsubok(ts, &tsd))
    333      1.29       nia 			return EINVAL;
    334  1.35.4.1    martin 		timespecsub(ts, &tsd, &tsd);
    335  1.35.4.1    martin 		ts = &tsd;
    336      1.29       nia 	}
    337      1.10  christos 
    338      1.26       nia 	error = itimespecfix(ts);
    339      1.26       nia 	if (error != 0)
    340       1.5     rmind 		return error;
    341      1.10  christos 
    342      1.15  christos 	if (ts->tv_sec == 0 && ts->tv_nsec == 0)
    343      1.15  christos 		return ETIMEDOUT;
    344      1.15  christos 
    345      1.14  christos 	*timo = tstohz(ts);
    346      1.14  christos 	KASSERT(*timo > 0);
    347       1.5     rmind 
    348       1.5     rmind 	return 0;
    349       1.5     rmind }
    350      1.33  riastrad 
    351      1.33  riastrad bool
    352      1.33  riastrad timespecaddok(const struct timespec *tsp, const struct timespec *usp)
    353      1.33  riastrad {
    354      1.33  riastrad 	enum { TIME_MIN = __type_min(time_t), TIME_MAX = __type_max(time_t) };
    355      1.33  riastrad 	time_t a = tsp->tv_sec;
    356      1.33  riastrad 	time_t b = usp->tv_sec;
    357      1.33  riastrad 	bool carry;
    358      1.33  riastrad 
    359      1.33  riastrad 	/*
    360      1.33  riastrad 	 * Caller is responsible for guaranteeing valid timespec
    361      1.33  riastrad 	 * inputs.  Any user-controlled inputs must be validated or
    362      1.33  riastrad 	 * adjusted.
    363      1.33  riastrad 	 */
    364      1.33  riastrad 	KASSERT(tsp->tv_nsec >= 0);
    365      1.33  riastrad 	KASSERT(usp->tv_nsec >= 0);
    366      1.33  riastrad 	KASSERT(tsp->tv_nsec < 1000000000L);
    367      1.33  riastrad 	KASSERT(usp->tv_nsec < 1000000000L);
    368      1.33  riastrad 	CTASSERT(1000000000L <= __type_max(long) - 1000000000L);
    369      1.33  riastrad 
    370      1.33  riastrad 	/*
    371      1.33  riastrad 	 * Fail if a + b + carry overflows TIME_MAX, or if a + b
    372      1.33  riastrad 	 * overflows TIME_MIN because timespecadd adds the carry after
    373      1.33  riastrad 	 * computing a + b.
    374      1.33  riastrad 	 *
    375      1.33  riastrad 	 * Break it into two mutually exclusive and exhaustive cases:
    376      1.33  riastrad 	 * I. a >= 0
    377      1.33  riastrad 	 * II. a < 0
    378      1.33  riastrad 	 */
    379      1.33  riastrad 	carry = (tsp->tv_nsec + usp->tv_nsec >= 1000000000L);
    380      1.33  riastrad 	if (a >= 0) {
    381      1.33  riastrad 		/*
    382      1.33  riastrad 		 * Case I: a >= 0.  If b < 0, then b + 1 <= 0, so
    383      1.33  riastrad 		 *
    384      1.33  riastrad 		 *	a + b + 1 <= a + 0 <= TIME_MAX,
    385      1.33  riastrad 		 *
    386      1.33  riastrad 		 * and
    387      1.33  riastrad 		 *
    388      1.33  riastrad 		 *	a + b >= 0 + b = b >= TIME_MIN,
    389      1.33  riastrad 		 *
    390      1.33  riastrad 		 * so this can't overflow.
    391      1.33  riastrad 		 *
    392      1.33  riastrad 		 * If b >= 0, then a + b + carry >= a + b >= 0, so
    393      1.33  riastrad 		 * negative results and thus results below TIME_MIN are
    394      1.33  riastrad 		 * impossible; we need only avoid
    395      1.33  riastrad 		 *
    396      1.33  riastrad 		 *	a + b + carry > TIME_MAX,
    397      1.33  riastrad 		 *
    398      1.33  riastrad 		 * which we will do by rejecting if
    399      1.33  riastrad 		 *
    400      1.33  riastrad 		 *	b > TIME_MAX - a - carry,
    401      1.33  riastrad 		 *
    402      1.33  riastrad 		 * which in turn is incidentally always false if b < 0
    403      1.33  riastrad 		 * so we don't need extra logic to discriminate on the
    404      1.33  riastrad 		 * b >= 0 and b < 0 cases.
    405      1.33  riastrad 		 *
    406      1.33  riastrad 		 * Since 0 <= a <= TIME_MAX, we know
    407      1.33  riastrad 		 *
    408      1.33  riastrad 		 *	0 <= TIME_MAX - a <= TIME_MAX,
    409      1.33  riastrad 		 *
    410      1.33  riastrad 		 * and hence
    411      1.33  riastrad 		 *
    412      1.33  riastrad 		 *	-1 <= TIME_MAX - a - 1 < TIME_MAX.
    413      1.33  riastrad 		 *
    414      1.33  riastrad 		 * So we can compute TIME_MAX - a - carry (i.e., either
    415      1.33  riastrad 		 * TIME_MAX - a or TIME_MAX - a - 1) safely without
    416      1.33  riastrad 		 * overflow.
    417      1.33  riastrad 		 */
    418      1.33  riastrad 		if (b > TIME_MAX - a - carry)
    419      1.33  riastrad 			return false;
    420      1.33  riastrad 	} else {
    421      1.33  riastrad 		/*
    422      1.33  riastrad 		 * Case II: a < 0.  If b >= 0, then since a + 1 <= 0,
    423      1.33  riastrad 		 * we have
    424      1.33  riastrad 		 *
    425      1.33  riastrad 		 *	a + b + 1 <= b <= TIME_MAX,
    426      1.33  riastrad 		 *
    427      1.33  riastrad 		 * and
    428      1.33  riastrad 		 *
    429      1.33  riastrad 		 *	a + b >= a >= TIME_MIN,
    430      1.33  riastrad 		 *
    431      1.33  riastrad 		 * so this can't overflow.
    432      1.33  riastrad 		 *
    433      1.33  riastrad 		 * If b < 0, then the intermediate a + b is negative
    434      1.33  riastrad 		 * and the outcome a + b + 1 is nonpositive, so we need
    435      1.33  riastrad 		 * only avoid
    436      1.33  riastrad 		 *
    437      1.33  riastrad 		 *	a + b < TIME_MIN,
    438      1.33  riastrad 		 *
    439      1.33  riastrad 		 * which we will do by rejecting if
    440      1.33  riastrad 		 *
    441      1.33  riastrad 		 *	a < TIME_MIN - b.
    442      1.33  riastrad 		 *
    443      1.33  riastrad 		 * (Reminder: The carry is added afterward in
    444      1.33  riastrad 		 * timespecadd, so to avoid overflow it is not enough
    445      1.33  riastrad 		 * to merely reject a + b + carry < TIME_MIN.)
    446      1.33  riastrad 		 *
    447      1.33  riastrad 		 * It is safe to compute the difference TIME_MIN - b
    448      1.33  riastrad 		 * because b is negative, so the result lies in
    449      1.33  riastrad 		 * (TIME_MIN, 0].
    450      1.33  riastrad 		 */
    451      1.33  riastrad 		if (b < 0 && a < TIME_MIN - b)
    452      1.33  riastrad 			return false;
    453      1.33  riastrad 	}
    454      1.33  riastrad 
    455      1.33  riastrad 	return true;
    456      1.33  riastrad }
    457      1.33  riastrad 
    458      1.33  riastrad bool
    459      1.33  riastrad timespecsubok(const struct timespec *tsp, const struct timespec *usp)
    460      1.33  riastrad {
    461      1.33  riastrad 	enum { TIME_MIN = __type_min(time_t), TIME_MAX = __type_max(time_t) };
    462      1.33  riastrad 	time_t a = tsp->tv_sec, b = usp->tv_sec;
    463      1.33  riastrad 	bool borrow;
    464      1.33  riastrad 
    465      1.33  riastrad 	/*
    466      1.33  riastrad 	 * Caller is responsible for guaranteeing valid timespec
    467      1.33  riastrad 	 * inputs.  Any user-controlled inputs must be validated or
    468      1.33  riastrad 	 * adjusted.
    469      1.33  riastrad 	 */
    470      1.33  riastrad 	KASSERT(tsp->tv_nsec >= 0);
    471      1.33  riastrad 	KASSERT(usp->tv_nsec >= 0);
    472      1.33  riastrad 	KASSERT(tsp->tv_nsec < 1000000000L);
    473      1.33  riastrad 	KASSERT(usp->tv_nsec < 1000000000L);
    474      1.33  riastrad 	CTASSERT(1000000000L <= __type_max(long) - 1000000000L);
    475      1.33  riastrad 
    476      1.33  riastrad 	/*
    477      1.33  riastrad 	 * Fail if a - b - borrow overflows TIME_MIN, or if a - b
    478      1.33  riastrad 	 * overflows TIME_MAX because timespecsub subtracts the borrow
    479      1.33  riastrad 	 * after computing a - b.
    480      1.33  riastrad 	 *
    481      1.33  riastrad 	 * Break it into two mutually exclusive and exhaustive cases:
    482      1.33  riastrad 	 * I. a < 0
    483      1.33  riastrad 	 * II. a >= 0
    484      1.33  riastrad 	 */
    485      1.33  riastrad 	borrow = (tsp->tv_nsec - usp->tv_nsec < 0);
    486      1.33  riastrad 	if (a < 0) {
    487      1.33  riastrad 		/*
    488      1.33  riastrad 		 * Case I: a < 0.  If b < 0, then -b - 1 >= 0, so
    489      1.33  riastrad 		 *
    490      1.33  riastrad 		 *	a - b - 1 >= a + 0 >= TIME_MIN,
    491      1.33  riastrad 		 *
    492      1.33  riastrad 		 * and, since a <= -1, provided that TIME_MIN <=
    493      1.33  riastrad 		 * -TIME_MAX - 1 so that TIME_MAX <= -TIME_MIN - 1 (in
    494      1.33  riastrad 		 * fact, equality holds, under the assumption of
    495      1.33  riastrad 		 * two's-complement arithmetic),
    496      1.33  riastrad 		 *
    497      1.33  riastrad 		 *	a - b <= -1 - b = -b - 1 <= TIME_MAX,
    498      1.33  riastrad 		 *
    499      1.33  riastrad 		 * so this can't overflow.
    500      1.33  riastrad 		 */
    501      1.33  riastrad 		CTASSERT(TIME_MIN <= -TIME_MAX - 1);
    502      1.33  riastrad 
    503      1.33  riastrad 		/*
    504      1.33  riastrad 		 * If b >= 0, then a - b - borrow <= a - b < 0, so
    505      1.33  riastrad 		 * positive results and thus results above TIME_MAX are
    506      1.33  riastrad 		 * impossible; we need only avoid
    507      1.33  riastrad 		 *
    508      1.33  riastrad 		 *	a - b - borrow < TIME_MIN,
    509      1.33  riastrad 		 *
    510      1.33  riastrad 		 * which we will do by rejecting if
    511      1.33  riastrad 		 *
    512      1.33  riastrad 		 *	a < TIME_MIN + b + borrow.
    513      1.33  riastrad 		 *
    514      1.33  riastrad 		 * The right-hand side is safe to evaluate for any
    515      1.33  riastrad 		 * values of b and borrow as long as TIME_MIN +
    516      1.33  riastrad 		 * TIME_MAX + 1 <= TIME_MAX, i.e., TIME_MIN <= -1.
    517      1.33  riastrad 		 * (Note: If time_t were unsigned, this would fail!)
    518      1.33  riastrad 		 *
    519      1.33  riastrad 		 * Note: Unlike Case I in timespecaddok, this criterion
    520      1.33  riastrad 		 * does not work for b < 0, nor can the roles of a and
    521      1.33  riastrad 		 * b in the inequality be reversed (e.g., -b < TIME_MIN
    522      1.33  riastrad 		 * - a + borrow) without extra cases like checking for
    523      1.33  riastrad 		 * b = TEST_MIN.
    524      1.33  riastrad 		 */
    525      1.33  riastrad 		CTASSERT(TIME_MIN < -1);
    526      1.33  riastrad 		if (b >= 0 && a < TIME_MIN + b + borrow)
    527      1.33  riastrad 			return false;
    528      1.33  riastrad 	} else {
    529      1.33  riastrad 		/*
    530      1.33  riastrad 		 * Case II: a >= 0.  If b >= 0, then
    531      1.33  riastrad 		 *
    532      1.33  riastrad 		 *	a - b <= a <= TIME_MAX,
    533      1.33  riastrad 		 *
    534      1.33  riastrad 		 * and, provided TIME_MIN <= -TIME_MAX - 1 (in fact,
    535      1.33  riastrad 		 * equality holds, under the assumption of
    536      1.33  riastrad 		 * two's-complement arithmetic)
    537      1.33  riastrad 		 *
    538      1.33  riastrad 		 *	a - b - 1 >= -b - 1 >= -TIME_MAX - 1 >= TIME_MIN,
    539      1.33  riastrad 		 *
    540      1.33  riastrad 		 * so this can't overflow.
    541      1.33  riastrad 		 */
    542      1.33  riastrad 		CTASSERT(TIME_MIN <= -TIME_MAX - 1);
    543      1.33  riastrad 
    544      1.33  riastrad 		/*
    545      1.33  riastrad 		 * If b < 0, then a - b >= a >= 0, so negative results
    546      1.33  riastrad 		 * and thus results below TIME_MIN are impossible; we
    547      1.33  riastrad 		 * need only avoid
    548      1.33  riastrad 		 *
    549      1.33  riastrad 		 *	a - b > TIME_MAX,
    550      1.33  riastrad 		 *
    551      1.33  riastrad 		 * which we will do by rejecting if
    552      1.33  riastrad 		 *
    553      1.33  riastrad 		 *	a > TIME_MAX + b.
    554      1.33  riastrad 		 *
    555      1.33  riastrad 		 * (Reminder: The borrow is subtracted afterward in
    556      1.33  riastrad 		 * timespecsub, so to avoid overflow it is not enough
    557      1.33  riastrad 		 * to merely reject a - b - borrow > TIME_MAX.)
    558      1.33  riastrad 		 *
    559      1.33  riastrad 		 * It is safe to compute the sum TIME_MAX + b because b
    560      1.33  riastrad 		 * is negative, so the result lies in [0, TIME_MAX).
    561      1.33  riastrad 		 */
    562      1.33  riastrad 		if (b < 0 && a > TIME_MAX + b)
    563      1.33  riastrad 			return false;
    564      1.33  riastrad 	}
    565      1.33  riastrad 
    566      1.33  riastrad 	return true;
    567      1.33  riastrad }
    568