1*65195a4cSriastradh /* $NetBSD: subr_time_arith.c,v 1.1 2024/12/22 23:24:20 riastradh Exp $ */ 2*65195a4cSriastradh 3*65195a4cSriastradh /*- 4*65195a4cSriastradh * Copyright (c) 2000, 2004, 2005, 2007, 2008, 2009, 2020 5*65195a4cSriastradh * The NetBSD Foundation, Inc. 6*65195a4cSriastradh * All rights reserved. 7*65195a4cSriastradh * 8*65195a4cSriastradh * This code is derived from software contributed to The NetBSD Foundation 9*65195a4cSriastradh * by Christopher G. Demetriou, by Andrew Doran, and by Jason R. Thorpe. 10*65195a4cSriastradh * 11*65195a4cSriastradh * Redistribution and use in source and binary forms, with or without 12*65195a4cSriastradh * modification, are permitted provided that the following conditions 13*65195a4cSriastradh * are met: 14*65195a4cSriastradh * 1. Redistributions of source code must retain the above copyright 15*65195a4cSriastradh * notice, this list of conditions and the following disclaimer. 16*65195a4cSriastradh * 2. Redistributions in binary form must reproduce the above copyright 17*65195a4cSriastradh * notice, this list of conditions and the following disclaimer in the 18*65195a4cSriastradh * documentation and/or other materials provided with the distribution. 19*65195a4cSriastradh * 20*65195a4cSriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21*65195a4cSriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22*65195a4cSriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23*65195a4cSriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24*65195a4cSriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25*65195a4cSriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26*65195a4cSriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27*65195a4cSriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28*65195a4cSriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29*65195a4cSriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30*65195a4cSriastradh * POSSIBILITY OF SUCH DAMAGE. 31*65195a4cSriastradh */ 32*65195a4cSriastradh 33*65195a4cSriastradh /* 34*65195a4cSriastradh * Copyright (c) 1982, 1986, 1989, 1993 35*65195a4cSriastradh * The Regents of the University of California. All rights reserved. 36*65195a4cSriastradh * 37*65195a4cSriastradh * Redistribution and use in source and binary forms, with or without 38*65195a4cSriastradh * modification, are permitted provided that the following conditions 39*65195a4cSriastradh * are met: 40*65195a4cSriastradh * 1. Redistributions of source code must retain the above copyright 41*65195a4cSriastradh * notice, this list of conditions and the following disclaimer. 42*65195a4cSriastradh * 2. Redistributions in binary form must reproduce the above copyright 43*65195a4cSriastradh * notice, this list of conditions and the following disclaimer in the 44*65195a4cSriastradh * documentation and/or other materials provided with the distribution. 45*65195a4cSriastradh * 3. Neither the name of the University nor the names of its contributors 46*65195a4cSriastradh * may be used to endorse or promote products derived from this software 47*65195a4cSriastradh * without specific prior written permission. 48*65195a4cSriastradh * 49*65195a4cSriastradh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50*65195a4cSriastradh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51*65195a4cSriastradh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52*65195a4cSriastradh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53*65195a4cSriastradh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54*65195a4cSriastradh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55*65195a4cSriastradh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56*65195a4cSriastradh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57*65195a4cSriastradh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58*65195a4cSriastradh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59*65195a4cSriastradh * SUCH DAMAGE. 60*65195a4cSriastradh * 61*65195a4cSriastradh * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 62*65195a4cSriastradh * @(#)kern_time.c 8.4 (Berkeley) 5/26/95 63*65195a4cSriastradh */ 64*65195a4cSriastradh 65*65195a4cSriastradh #include <sys/cdefs.h> 66*65195a4cSriastradh __KERNEL_RCSID(0, "$NetBSD: subr_time_arith.c,v 1.1 2024/12/22 23:24:20 riastradh Exp $"); 67*65195a4cSriastradh 68*65195a4cSriastradh #include <sys/types.h> 69*65195a4cSriastradh 70*65195a4cSriastradh #include <sys/errno.h> 71*65195a4cSriastradh #include <sys/time.h> 72*65195a4cSriastradh #include <sys/timearith.h> 73*65195a4cSriastradh 74*65195a4cSriastradh #if defined(_KERNEL) 75*65195a4cSriastradh 76*65195a4cSriastradh #include <sys/kernel.h> 77*65195a4cSriastradh #include <sys/systm.h> 78*65195a4cSriastradh 79*65195a4cSriastradh #include <machine/limits.h> 80*65195a4cSriastradh 81*65195a4cSriastradh #elif defined(_TIME_TESTING) 82*65195a4cSriastradh 83*65195a4cSriastradh #include <assert.h> 84*65195a4cSriastradh #include <limits.h> 85*65195a4cSriastradh #include <stdbool.h> 86*65195a4cSriastradh 87*65195a4cSriastradh extern int hz; 88*65195a4cSriastradh extern long tick; 89*65195a4cSriastradh 90*65195a4cSriastradh #define KASSERT assert 91*65195a4cSriastradh 92*65195a4cSriastradh #endif 93*65195a4cSriastradh 94*65195a4cSriastradh /* 95*65195a4cSriastradh * Compute number of ticks in the specified amount of time. 96*65195a4cSriastradh */ 97*65195a4cSriastradh int 98*65195a4cSriastradh tvtohz(const struct timeval *tv) 99*65195a4cSriastradh { 100*65195a4cSriastradh unsigned long ticks; 101*65195a4cSriastradh long sec, usec; 102*65195a4cSriastradh 103*65195a4cSriastradh /* 104*65195a4cSriastradh * If the number of usecs in the whole seconds part of the time 105*65195a4cSriastradh * difference fits in a long, then the total number of usecs will 106*65195a4cSriastradh * fit in an unsigned long. Compute the total and convert it to 107*65195a4cSriastradh * ticks, rounding up and adding 1 to allow for the current tick 108*65195a4cSriastradh * to expire. Rounding also depends on unsigned long arithmetic 109*65195a4cSriastradh * to avoid overflow. 110*65195a4cSriastradh * 111*65195a4cSriastradh * Otherwise, if the number of ticks in the whole seconds part of 112*65195a4cSriastradh * the time difference fits in a long, then convert the parts to 113*65195a4cSriastradh * ticks separately and add, using similar rounding methods and 114*65195a4cSriastradh * overflow avoidance. This method would work in the previous 115*65195a4cSriastradh * case, but it is slightly slower and assumes that hz is integral. 116*65195a4cSriastradh * 117*65195a4cSriastradh * Otherwise, round the time difference down to the maximum 118*65195a4cSriastradh * representable value. 119*65195a4cSriastradh * 120*65195a4cSriastradh * If ints are 32-bit, then the maximum value for any timeout in 121*65195a4cSriastradh * 10ms ticks is 248 days. 122*65195a4cSriastradh */ 123*65195a4cSriastradh sec = tv->tv_sec; 124*65195a4cSriastradh usec = tv->tv_usec; 125*65195a4cSriastradh 126*65195a4cSriastradh KASSERT(usec >= 0); 127*65195a4cSriastradh KASSERT(usec < 1000000); 128*65195a4cSriastradh 129*65195a4cSriastradh /* catch overflows in conversion time_t->int */ 130*65195a4cSriastradh if (tv->tv_sec > INT_MAX) 131*65195a4cSriastradh return INT_MAX; 132*65195a4cSriastradh if (tv->tv_sec < 0) 133*65195a4cSriastradh return 0; 134*65195a4cSriastradh 135*65195a4cSriastradh if (sec < 0 || (sec == 0 && usec == 0)) { 136*65195a4cSriastradh /* 137*65195a4cSriastradh * Would expire now or in the past. Return 0 ticks. 138*65195a4cSriastradh * This is different from the legacy tvhzto() interface, 139*65195a4cSriastradh * and callers need to check for it. 140*65195a4cSriastradh */ 141*65195a4cSriastradh ticks = 0; 142*65195a4cSriastradh } else if (sec <= (LONG_MAX / 1000000)) 143*65195a4cSriastradh ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1)) 144*65195a4cSriastradh / tick) + 1; 145*65195a4cSriastradh else if (sec <= (LONG_MAX / hz)) 146*65195a4cSriastradh ticks = (sec * hz) + 147*65195a4cSriastradh (((unsigned long)usec + (tick - 1)) / tick) + 1; 148*65195a4cSriastradh else 149*65195a4cSriastradh ticks = LONG_MAX; 150*65195a4cSriastradh 151*65195a4cSriastradh if (ticks > INT_MAX) 152*65195a4cSriastradh ticks = INT_MAX; 153*65195a4cSriastradh 154*65195a4cSriastradh return ((int)ticks); 155*65195a4cSriastradh } 156*65195a4cSriastradh 157*65195a4cSriastradh /* 158*65195a4cSriastradh * Check that a proposed value to load into the .it_value or 159*65195a4cSriastradh * .it_interval part of an interval timer is acceptable, and 160*65195a4cSriastradh * fix it to have at least minimal value (i.e. if it is less 161*65195a4cSriastradh * than the resolution of the clock, round it up.). We don't 162*65195a4cSriastradh * timeout the 0,0 value because this means to disable the 163*65195a4cSriastradh * timer or the interval. 164*65195a4cSriastradh */ 165*65195a4cSriastradh int 166*65195a4cSriastradh itimerfix(struct timeval *tv) 167*65195a4cSriastradh { 168*65195a4cSriastradh 169*65195a4cSriastradh if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) 170*65195a4cSriastradh return EINVAL; 171*65195a4cSriastradh if (tv->tv_sec < 0) 172*65195a4cSriastradh return ETIMEDOUT; 173*65195a4cSriastradh if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 174*65195a4cSriastradh tv->tv_usec = tick; 175*65195a4cSriastradh return 0; 176*65195a4cSriastradh } 177*65195a4cSriastradh 178*65195a4cSriastradh int 179*65195a4cSriastradh itimespecfix(struct timespec *ts) 180*65195a4cSriastradh { 181*65195a4cSriastradh 182*65195a4cSriastradh if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000) 183*65195a4cSriastradh return EINVAL; 184*65195a4cSriastradh if (ts->tv_sec < 0) 185*65195a4cSriastradh return ETIMEDOUT; 186*65195a4cSriastradh if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000) 187*65195a4cSriastradh ts->tv_nsec = tick * 1000; 188*65195a4cSriastradh return 0; 189*65195a4cSriastradh } 190*65195a4cSriastradh 191*65195a4cSriastradh /* 192*65195a4cSriastradh * timespecaddok(tsp, usp) 193*65195a4cSriastradh * 194*65195a4cSriastradh * True if tsp + usp can be computed without overflow, i.e., if it 195*65195a4cSriastradh * is OK to do timespecadd(tsp, usp, ...). 196*65195a4cSriastradh */ 197*65195a4cSriastradh bool 198*65195a4cSriastradh timespecaddok(const struct timespec *tsp, const struct timespec *usp) 199*65195a4cSriastradh { 200*65195a4cSriastradh enum { TIME_MIN = __type_min(time_t), TIME_MAX = __type_max(time_t) }; 201*65195a4cSriastradh time_t a = tsp->tv_sec; 202*65195a4cSriastradh time_t b = usp->tv_sec; 203*65195a4cSriastradh bool carry; 204*65195a4cSriastradh 205*65195a4cSriastradh /* 206*65195a4cSriastradh * Caller is responsible for guaranteeing valid timespec 207*65195a4cSriastradh * inputs. Any user-controlled inputs must be validated or 208*65195a4cSriastradh * adjusted. 209*65195a4cSriastradh */ 210*65195a4cSriastradh KASSERT(tsp->tv_nsec >= 0); 211*65195a4cSriastradh KASSERT(usp->tv_nsec >= 0); 212*65195a4cSriastradh KASSERT(tsp->tv_nsec < 1000000000L); 213*65195a4cSriastradh KASSERT(usp->tv_nsec < 1000000000L); 214*65195a4cSriastradh __CTASSERT(1000000000L <= __type_max(long) - 1000000000L); 215*65195a4cSriastradh 216*65195a4cSriastradh /* 217*65195a4cSriastradh * Fail if a + b + carry overflows TIME_MAX, or if a + b 218*65195a4cSriastradh * overflows TIME_MIN because timespecadd adds the carry after 219*65195a4cSriastradh * computing a + b. 220*65195a4cSriastradh * 221*65195a4cSriastradh * Break it into two mutually exclusive and exhaustive cases: 222*65195a4cSriastradh * I. a >= 0 223*65195a4cSriastradh * II. a < 0 224*65195a4cSriastradh */ 225*65195a4cSriastradh carry = (tsp->tv_nsec + usp->tv_nsec >= 1000000000L); 226*65195a4cSriastradh if (a >= 0) { 227*65195a4cSriastradh /* 228*65195a4cSriastradh * Case I: a >= 0. If b < 0, then b + 1 <= 0, so 229*65195a4cSriastradh * 230*65195a4cSriastradh * a + b + 1 <= a + 0 <= TIME_MAX, 231*65195a4cSriastradh * 232*65195a4cSriastradh * and 233*65195a4cSriastradh * 234*65195a4cSriastradh * a + b >= 0 + b = b >= TIME_MIN, 235*65195a4cSriastradh * 236*65195a4cSriastradh * so this can't overflow. 237*65195a4cSriastradh * 238*65195a4cSriastradh * If b >= 0, then a + b + carry >= a + b >= 0, so 239*65195a4cSriastradh * negative results and thus results below TIME_MIN are 240*65195a4cSriastradh * impossible; we need only avoid 241*65195a4cSriastradh * 242*65195a4cSriastradh * a + b + carry > TIME_MAX, 243*65195a4cSriastradh * 244*65195a4cSriastradh * which we will do by rejecting if 245*65195a4cSriastradh * 246*65195a4cSriastradh * b > TIME_MAX - a - carry, 247*65195a4cSriastradh * 248*65195a4cSriastradh * which in turn is incidentally always false if b < 0 249*65195a4cSriastradh * so we don't need extra logic to discriminate on the 250*65195a4cSriastradh * b >= 0 and b < 0 cases. 251*65195a4cSriastradh * 252*65195a4cSriastradh * Since 0 <= a <= TIME_MAX, we know 253*65195a4cSriastradh * 254*65195a4cSriastradh * 0 <= TIME_MAX - a <= TIME_MAX, 255*65195a4cSriastradh * 256*65195a4cSriastradh * and hence 257*65195a4cSriastradh * 258*65195a4cSriastradh * -1 <= TIME_MAX - a - 1 < TIME_MAX. 259*65195a4cSriastradh * 260*65195a4cSriastradh * So we can compute TIME_MAX - a - carry (i.e., either 261*65195a4cSriastradh * TIME_MAX - a or TIME_MAX - a - 1) safely without 262*65195a4cSriastradh * overflow. 263*65195a4cSriastradh */ 264*65195a4cSriastradh if (b > TIME_MAX - a - carry) 265*65195a4cSriastradh return false; 266*65195a4cSriastradh } else { 267*65195a4cSriastradh /* 268*65195a4cSriastradh * Case II: a < 0. If b >= 0, then since a + 1 <= 0, 269*65195a4cSriastradh * we have 270*65195a4cSriastradh * 271*65195a4cSriastradh * a + b + 1 <= b <= TIME_MAX, 272*65195a4cSriastradh * 273*65195a4cSriastradh * and 274*65195a4cSriastradh * 275*65195a4cSriastradh * a + b >= a >= TIME_MIN, 276*65195a4cSriastradh * 277*65195a4cSriastradh * so this can't overflow. 278*65195a4cSriastradh * 279*65195a4cSriastradh * If b < 0, then the intermediate a + b is negative 280*65195a4cSriastradh * and the outcome a + b + 1 is nonpositive, so we need 281*65195a4cSriastradh * only avoid 282*65195a4cSriastradh * 283*65195a4cSriastradh * a + b < TIME_MIN, 284*65195a4cSriastradh * 285*65195a4cSriastradh * which we will do by rejecting if 286*65195a4cSriastradh * 287*65195a4cSriastradh * a < TIME_MIN - b. 288*65195a4cSriastradh * 289*65195a4cSriastradh * (Reminder: The carry is added afterward in 290*65195a4cSriastradh * timespecadd, so to avoid overflow it is not enough 291*65195a4cSriastradh * to merely reject a + b + carry < TIME_MIN.) 292*65195a4cSriastradh * 293*65195a4cSriastradh * It is safe to compute the difference TIME_MIN - b 294*65195a4cSriastradh * because b is negative, so the result lies in 295*65195a4cSriastradh * (TIME_MIN, 0]. 296*65195a4cSriastradh */ 297*65195a4cSriastradh if (b < 0 && a < TIME_MIN - b) 298*65195a4cSriastradh return false; 299*65195a4cSriastradh } 300*65195a4cSriastradh 301*65195a4cSriastradh return true; 302*65195a4cSriastradh } 303*65195a4cSriastradh 304*65195a4cSriastradh /* 305*65195a4cSriastradh * timespecsubok(tsp, usp) 306*65195a4cSriastradh * 307*65195a4cSriastradh * True if tsp - usp can be computed without overflow, i.e., if it 308*65195a4cSriastradh * is OK to do timespecsub(tsp, usp, ...). 309*65195a4cSriastradh */ 310*65195a4cSriastradh bool 311*65195a4cSriastradh timespecsubok(const struct timespec *tsp, const struct timespec *usp) 312*65195a4cSriastradh { 313*65195a4cSriastradh enum { TIME_MIN = __type_min(time_t), TIME_MAX = __type_max(time_t) }; 314*65195a4cSriastradh time_t a = tsp->tv_sec, b = usp->tv_sec; 315*65195a4cSriastradh bool borrow; 316*65195a4cSriastradh 317*65195a4cSriastradh /* 318*65195a4cSriastradh * Caller is responsible for guaranteeing valid timespec 319*65195a4cSriastradh * inputs. Any user-controlled inputs must be validated or 320*65195a4cSriastradh * adjusted. 321*65195a4cSriastradh */ 322*65195a4cSriastradh KASSERT(tsp->tv_nsec >= 0); 323*65195a4cSriastradh KASSERT(usp->tv_nsec >= 0); 324*65195a4cSriastradh KASSERT(tsp->tv_nsec < 1000000000L); 325*65195a4cSriastradh KASSERT(usp->tv_nsec < 1000000000L); 326*65195a4cSriastradh __CTASSERT(1000000000L <= __type_max(long) - 1000000000L); 327*65195a4cSriastradh 328*65195a4cSriastradh /* 329*65195a4cSriastradh * Fail if a - b - borrow overflows TIME_MIN, or if a - b 330*65195a4cSriastradh * overflows TIME_MAX because timespecsub subtracts the borrow 331*65195a4cSriastradh * after computing a - b. 332*65195a4cSriastradh * 333*65195a4cSriastradh * Break it into two mutually exclusive and exhaustive cases: 334*65195a4cSriastradh * I. a < 0 335*65195a4cSriastradh * II. a >= 0 336*65195a4cSriastradh */ 337*65195a4cSriastradh borrow = (tsp->tv_nsec - usp->tv_nsec < 0); 338*65195a4cSriastradh if (a < 0) { 339*65195a4cSriastradh /* 340*65195a4cSriastradh * Case I: a < 0. If b < 0, then -b - 1 >= 0, so 341*65195a4cSriastradh * 342*65195a4cSriastradh * a - b - 1 >= a + 0 >= TIME_MIN, 343*65195a4cSriastradh * 344*65195a4cSriastradh * and, since a <= -1, provided that TIME_MIN <= 345*65195a4cSriastradh * -TIME_MAX - 1 so that TIME_MAX <= -TIME_MIN - 1 (in 346*65195a4cSriastradh * fact, equality holds, under the assumption of 347*65195a4cSriastradh * two's-complement arithmetic), 348*65195a4cSriastradh * 349*65195a4cSriastradh * a - b <= -1 - b = -b - 1 <= TIME_MAX, 350*65195a4cSriastradh * 351*65195a4cSriastradh * so this can't overflow. 352*65195a4cSriastradh */ 353*65195a4cSriastradh __CTASSERT(TIME_MIN <= -TIME_MAX - 1); 354*65195a4cSriastradh 355*65195a4cSriastradh /* 356*65195a4cSriastradh * If b >= 0, then a - b - borrow <= a - b < 0, so 357*65195a4cSriastradh * positive results and thus results above TIME_MAX are 358*65195a4cSriastradh * impossible; we need only avoid 359*65195a4cSriastradh * 360*65195a4cSriastradh * a - b - borrow < TIME_MIN, 361*65195a4cSriastradh * 362*65195a4cSriastradh * which we will do by rejecting if 363*65195a4cSriastradh * 364*65195a4cSriastradh * a < TIME_MIN + b + borrow. 365*65195a4cSriastradh * 366*65195a4cSriastradh * The right-hand side is safe to evaluate for any 367*65195a4cSriastradh * values of b and borrow as long as TIME_MIN + 368*65195a4cSriastradh * TIME_MAX + 1 <= TIME_MAX, i.e., TIME_MIN <= -1. 369*65195a4cSriastradh * (Note: If time_t were unsigned, this would fail!) 370*65195a4cSriastradh * 371*65195a4cSriastradh * Note: Unlike Case I in timespecaddok, this criterion 372*65195a4cSriastradh * does not work for b < 0, nor can the roles of a and 373*65195a4cSriastradh * b in the inequality be reversed (e.g., -b < TIME_MIN 374*65195a4cSriastradh * - a + borrow) without extra cases like checking for 375*65195a4cSriastradh * b = TEST_MIN. 376*65195a4cSriastradh */ 377*65195a4cSriastradh __CTASSERT(TIME_MIN < -1); 378*65195a4cSriastradh if (b >= 0 && a < TIME_MIN + b + borrow) 379*65195a4cSriastradh return false; 380*65195a4cSriastradh } else { 381*65195a4cSriastradh /* 382*65195a4cSriastradh * Case II: a >= 0. If b >= 0, then 383*65195a4cSriastradh * 384*65195a4cSriastradh * a - b <= a <= TIME_MAX, 385*65195a4cSriastradh * 386*65195a4cSriastradh * and, provided TIME_MIN <= -TIME_MAX - 1 (in fact, 387*65195a4cSriastradh * equality holds, under the assumption of 388*65195a4cSriastradh * two's-complement arithmetic) 389*65195a4cSriastradh * 390*65195a4cSriastradh * a - b - 1 >= -b - 1 >= -TIME_MAX - 1 >= TIME_MIN, 391*65195a4cSriastradh * 392*65195a4cSriastradh * so this can't overflow. 393*65195a4cSriastradh */ 394*65195a4cSriastradh __CTASSERT(TIME_MIN <= -TIME_MAX - 1); 395*65195a4cSriastradh 396*65195a4cSriastradh /* 397*65195a4cSriastradh * If b < 0, then a - b >= a >= 0, so negative results 398*65195a4cSriastradh * and thus results below TIME_MIN are impossible; we 399*65195a4cSriastradh * need only avoid 400*65195a4cSriastradh * 401*65195a4cSriastradh * a - b > TIME_MAX, 402*65195a4cSriastradh * 403*65195a4cSriastradh * which we will do by rejecting if 404*65195a4cSriastradh * 405*65195a4cSriastradh * a > TIME_MAX + b. 406*65195a4cSriastradh * 407*65195a4cSriastradh * (Reminder: The borrow is subtracted afterward in 408*65195a4cSriastradh * timespecsub, so to avoid overflow it is not enough 409*65195a4cSriastradh * to merely reject a - b - borrow > TIME_MAX.) 410*65195a4cSriastradh * 411*65195a4cSriastradh * It is safe to compute the sum TIME_MAX + b because b 412*65195a4cSriastradh * is negative, so the result lies in [0, TIME_MAX). 413*65195a4cSriastradh */ 414*65195a4cSriastradh if (b < 0 && a > TIME_MAX + b) 415*65195a4cSriastradh return false; 416*65195a4cSriastradh } 417*65195a4cSriastradh 418*65195a4cSriastradh return true; 419*65195a4cSriastradh } 420*65195a4cSriastradh 421*65195a4cSriastradh /* 422*65195a4cSriastradh * itimer_transition(it, now, next, &overruns) 423*65195a4cSriastradh * 424*65195a4cSriastradh * Given: 425*65195a4cSriastradh * 426*65195a4cSriastradh * - it: the current state of an itimer (it_value = last expiry 427*65195a4cSriastradh * time, it_interval = periodic rescheduling interval), and 428*65195a4cSriastradh * 429*65195a4cSriastradh * - now: the current time on the itimer's clock; 430*65195a4cSriastradh * 431*65195a4cSriastradh * compute: 432*65195a4cSriastradh * 433*65195a4cSriastradh * - next: the next time the itimer should be scheduled for, and 434*65195a4cSriastradh * - overruns: the number of overruns if we're firing late. 435*65195a4cSriastradh * 436*65195a4cSriastradh * XXX This should maybe also say whether the itimer should expire 437*65195a4cSriastradh * at all. 438*65195a4cSriastradh */ 439*65195a4cSriastradh void 440*65195a4cSriastradh itimer_transition(const struct itimerspec *restrict it, 441*65195a4cSriastradh const struct timespec *restrict now, 442*65195a4cSriastradh struct timespec *restrict next, 443*65195a4cSriastradh int *restrict overrunsp) 444*65195a4cSriastradh { 445*65195a4cSriastradh uint64_t last_val, next_val, interval, now_ns; 446*65195a4cSriastradh int backwards; 447*65195a4cSriastradh 448*65195a4cSriastradh /* 449*65195a4cSriastradh * Zero the outputs so we can test assertions in userland 450*65195a4cSriastradh * without undefined behaviour. 451*65195a4cSriastradh */ 452*65195a4cSriastradh timespecclear(next); 453*65195a4cSriastradh *overrunsp = 0; 454*65195a4cSriastradh 455*65195a4cSriastradh /* 456*65195a4cSriastradh * Paranoia: Caller should guarantee this. 457*65195a4cSriastradh */ 458*65195a4cSriastradh if (!timespecisset(&it->it_interval)) { 459*65195a4cSriastradh timespecclear(next); 460*65195a4cSriastradh return; 461*65195a4cSriastradh } 462*65195a4cSriastradh 463*65195a4cSriastradh backwards = (timespeccmp(&it->it_value, now, >)); 464*65195a4cSriastradh 465*65195a4cSriastradh /* Nonnegative interval guaranteed by itimerfix. */ 466*65195a4cSriastradh KASSERT(it->it_interval.tv_sec >= 0); 467*65195a4cSriastradh KASSERT(it->it_interval.tv_nsec >= 0); 468*65195a4cSriastradh 469*65195a4cSriastradh /* Handle the easy case of non-overflown timers first. */ 470*65195a4cSriastradh if (!backwards && 471*65195a4cSriastradh timespecaddok(&it->it_value, &it->it_interval)) { 472*65195a4cSriastradh timespecadd(&it->it_value, &it->it_interval, 473*65195a4cSriastradh next); 474*65195a4cSriastradh } else { 475*65195a4cSriastradh now_ns = timespec2ns(now); 476*65195a4cSriastradh last_val = timespec2ns(&it->it_value); 477*65195a4cSriastradh interval = timespec2ns(&it->it_interval); 478*65195a4cSriastradh 479*65195a4cSriastradh next_val = now_ns + 480*65195a4cSriastradh (now_ns - last_val + interval - 1) % interval; 481*65195a4cSriastradh 482*65195a4cSriastradh if (backwards) 483*65195a4cSriastradh next_val += interval; 484*65195a4cSriastradh else 485*65195a4cSriastradh *overrunsp = (now_ns - last_val) / interval; 486*65195a4cSriastradh 487*65195a4cSriastradh next->tv_sec = next_val / 1000000000; 488*65195a4cSriastradh next->tv_nsec = next_val % 1000000000; 489*65195a4cSriastradh } 490*65195a4cSriastradh } 491