1 /* $OpenBSD: kern_time.c,v 1.148 2020/10/15 16:31:11 cheloha Exp $ */ 2 /* $NetBSD: kern_time.c,v 1.20 1996/02/18 11:57:06 fvdl Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95 33 */ 34 35 #include <sys/param.h> 36 #include <sys/resourcevar.h> 37 #include <sys/kernel.h> 38 #include <sys/systm.h> 39 #include <sys/rwlock.h> 40 #include <sys/proc.h> 41 #include <sys/ktrace.h> 42 #include <sys/vnode.h> 43 #include <sys/signalvar.h> 44 #include <sys/stdint.h> 45 #include <sys/pledge.h> 46 #include <sys/task.h> 47 #include <sys/timeout.h> 48 #include <sys/timetc.h> 49 50 #include <sys/mount.h> 51 #include <sys/syscallargs.h> 52 53 #include <dev/clock_subr.h> 54 55 /* 56 * Time of day and interval timer support. 57 * 58 * These routines provide the kernel entry points to get and set 59 * the time-of-day and per-process interval timers. Subroutines 60 * here provide support for adding and subtracting timeval structures 61 * and decrementing interval timers, optionally reloading the interval 62 * timers when they expire. 63 */ 64 65 /* This function is used by clock_settime and settimeofday */ 66 int 67 settime(const struct timespec *ts) 68 { 69 struct timespec now; 70 71 /* 72 * Don't allow the time to be set forward so far it will wrap 73 * and become negative, thus allowing an attacker to bypass 74 * the next check below. The cutoff is 1 year before rollover 75 * occurs, so even if the attacker uses adjtime(2) to move 76 * the time past the cutoff, it will take a very long time 77 * to get to the wrap point. 78 * 79 * XXX: we check against UINT_MAX until we can figure out 80 * how to deal with the hardware RTCs. 81 */ 82 if (ts->tv_sec > UINT_MAX - 365*24*60*60) { 83 printf("denied attempt to set clock forward to %lld\n", 84 (long long)ts->tv_sec); 85 return (EPERM); 86 } 87 /* 88 * If the system is secure, we do not allow the time to be 89 * set to an earlier value (it may be slowed using adjtime, 90 * but not set back). This feature prevent interlopers from 91 * setting arbitrary time stamps on files. 92 */ 93 nanotime(&now); 94 if (securelevel > 1 && timespeccmp(ts, &now, <=)) { 95 printf("denied attempt to set clock back %lld seconds\n", 96 (long long)now.tv_sec - ts->tv_sec); 97 return (EPERM); 98 } 99 100 tc_setrealtimeclock(ts); 101 KERNEL_LOCK(); 102 resettodr(); 103 KERNEL_UNLOCK(); 104 105 return (0); 106 } 107 108 int 109 clock_gettime(struct proc *p, clockid_t clock_id, struct timespec *tp) 110 { 111 struct proc *q; 112 int error = 0; 113 114 switch (clock_id) { 115 case CLOCK_REALTIME: 116 nanotime(tp); 117 break; 118 case CLOCK_UPTIME: 119 nanoruntime(tp); 120 break; 121 case CLOCK_MONOTONIC: 122 case CLOCK_BOOTTIME: 123 nanouptime(tp); 124 break; 125 case CLOCK_PROCESS_CPUTIME_ID: 126 nanouptime(tp); 127 timespecsub(tp, &curcpu()->ci_schedstate.spc_runtime, tp); 128 timespecadd(tp, &p->p_p->ps_tu.tu_runtime, tp); 129 timespecadd(tp, &p->p_rtime, tp); 130 break; 131 case CLOCK_THREAD_CPUTIME_ID: 132 nanouptime(tp); 133 timespecsub(tp, &curcpu()->ci_schedstate.spc_runtime, tp); 134 timespecadd(tp, &p->p_tu.tu_runtime, tp); 135 timespecadd(tp, &p->p_rtime, tp); 136 break; 137 default: 138 /* check for clock from pthread_getcpuclockid() */ 139 if (__CLOCK_TYPE(clock_id) == CLOCK_THREAD_CPUTIME_ID) { 140 KERNEL_LOCK(); 141 q = tfind(__CLOCK_PTID(clock_id) - THREAD_PID_OFFSET); 142 if (q == NULL || q->p_p != p->p_p) 143 error = ESRCH; 144 else 145 *tp = q->p_tu.tu_runtime; 146 KERNEL_UNLOCK(); 147 } else 148 error = EINVAL; 149 break; 150 } 151 return (error); 152 } 153 154 int 155 sys_clock_gettime(struct proc *p, void *v, register_t *retval) 156 { 157 struct sys_clock_gettime_args /* { 158 syscallarg(clockid_t) clock_id; 159 syscallarg(struct timespec *) tp; 160 } */ *uap = v; 161 struct timespec ats; 162 int error; 163 164 memset(&ats, 0, sizeof(ats)); 165 if ((error = clock_gettime(p, SCARG(uap, clock_id), &ats)) != 0) 166 return (error); 167 168 error = copyout(&ats, SCARG(uap, tp), sizeof(ats)); 169 #ifdef KTRACE 170 if (error == 0 && KTRPOINT(p, KTR_STRUCT)) 171 ktrabstimespec(p, &ats); 172 #endif 173 return (error); 174 } 175 176 int 177 sys_clock_settime(struct proc *p, void *v, register_t *retval) 178 { 179 struct sys_clock_settime_args /* { 180 syscallarg(clockid_t) clock_id; 181 syscallarg(const struct timespec *) tp; 182 } */ *uap = v; 183 struct timespec ats; 184 clockid_t clock_id; 185 int error; 186 187 if ((error = suser(p)) != 0) 188 return (error); 189 190 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0) 191 return (error); 192 193 clock_id = SCARG(uap, clock_id); 194 switch (clock_id) { 195 case CLOCK_REALTIME: 196 if (!timespecisvalid(&ats)) 197 return (EINVAL); 198 if ((error = settime(&ats)) != 0) 199 return (error); 200 break; 201 default: /* Other clocks are read-only */ 202 return (EINVAL); 203 } 204 205 return (0); 206 } 207 208 int 209 sys_clock_getres(struct proc *p, void *v, register_t *retval) 210 { 211 struct sys_clock_getres_args /* { 212 syscallarg(clockid_t) clock_id; 213 syscallarg(struct timespec *) tp; 214 } */ *uap = v; 215 clockid_t clock_id; 216 struct bintime bt; 217 struct timespec ts; 218 struct proc *q; 219 u_int64_t scale; 220 int error = 0, realstathz; 221 222 memset(&ts, 0, sizeof(ts)); 223 realstathz = (stathz == 0) ? hz : stathz; 224 clock_id = SCARG(uap, clock_id); 225 226 switch (clock_id) { 227 case CLOCK_REALTIME: 228 case CLOCK_MONOTONIC: 229 case CLOCK_BOOTTIME: 230 case CLOCK_UPTIME: 231 memset(&bt, 0, sizeof(bt)); 232 rw_enter_read(&tc_lock); 233 scale = ((1ULL << 63) / tc_getfrequency()) * 2; 234 bt.frac = tc_getprecision() * scale; 235 rw_exit_read(&tc_lock); 236 BINTIME_TO_TIMESPEC(&bt, &ts); 237 break; 238 case CLOCK_PROCESS_CPUTIME_ID: 239 case CLOCK_THREAD_CPUTIME_ID: 240 ts.tv_nsec = 1000000000 / realstathz; 241 break; 242 default: 243 /* check for clock from pthread_getcpuclockid() */ 244 if (__CLOCK_TYPE(clock_id) == CLOCK_THREAD_CPUTIME_ID) { 245 KERNEL_LOCK(); 246 q = tfind(__CLOCK_PTID(clock_id) - THREAD_PID_OFFSET); 247 if (q == NULL || q->p_p != p->p_p) 248 error = ESRCH; 249 else 250 ts.tv_nsec = 1000000000 / realstathz; 251 KERNEL_UNLOCK(); 252 } else 253 error = EINVAL; 254 break; 255 } 256 257 if (error == 0 && SCARG(uap, tp)) { 258 ts.tv_nsec = MAX(ts.tv_nsec, 1); 259 error = copyout(&ts, SCARG(uap, tp), sizeof(ts)); 260 #ifdef KTRACE 261 if (error == 0 && KTRPOINT(p, KTR_STRUCT)) 262 ktrreltimespec(p, &ts); 263 #endif 264 } 265 266 return error; 267 } 268 269 int 270 sys_nanosleep(struct proc *p, void *v, register_t *retval) 271 { 272 static int chan; 273 struct sys_nanosleep_args/* { 274 syscallarg(const struct timespec *) rqtp; 275 syscallarg(struct timespec *) rmtp; 276 } */ *uap = v; 277 struct timespec elapsed, remainder, request, start, stop; 278 uint64_t nsecs; 279 struct timespec *rmtp; 280 int copyout_error, error; 281 282 rmtp = SCARG(uap, rmtp); 283 error = copyin(SCARG(uap, rqtp), &request, sizeof(request)); 284 if (error) 285 return (error); 286 #ifdef KTRACE 287 if (KTRPOINT(p, KTR_STRUCT)) 288 ktrreltimespec(p, &request); 289 #endif 290 291 if (request.tv_sec < 0 || !timespecisvalid(&request)) 292 return (EINVAL); 293 294 do { 295 getnanouptime(&start); 296 nsecs = MAX(1, MIN(TIMESPEC_TO_NSEC(&request), MAXTSLP)); 297 error = tsleep_nsec(&chan, PWAIT | PCATCH, "nanosleep", nsecs); 298 getnanouptime(&stop); 299 timespecsub(&stop, &start, &elapsed); 300 timespecsub(&request, &elapsed, &request); 301 if (request.tv_sec < 0) 302 timespecclear(&request); 303 if (error != EWOULDBLOCK) 304 break; 305 } while (timespecisset(&request)); 306 307 if (error == ERESTART) 308 error = EINTR; 309 if (error == EWOULDBLOCK) 310 error = 0; 311 312 if (rmtp) { 313 memset(&remainder, 0, sizeof(remainder)); 314 remainder = request; 315 copyout_error = copyout(&remainder, rmtp, sizeof(remainder)); 316 if (copyout_error) 317 error = copyout_error; 318 #ifdef KTRACE 319 if (copyout_error == 0 && KTRPOINT(p, KTR_STRUCT)) 320 ktrreltimespec(p, &remainder); 321 #endif 322 } 323 324 return error; 325 } 326 327 int 328 sys_gettimeofday(struct proc *p, void *v, register_t *retval) 329 { 330 struct sys_gettimeofday_args /* { 331 syscallarg(struct timeval *) tp; 332 syscallarg(struct timezone *) tzp; 333 } */ *uap = v; 334 struct timeval atv; 335 static const struct timezone zerotz = { 0, 0 }; 336 struct timeval *tp; 337 struct timezone *tzp; 338 int error = 0; 339 340 tp = SCARG(uap, tp); 341 tzp = SCARG(uap, tzp); 342 343 if (tp) { 344 memset(&atv, 0, sizeof(atv)); 345 microtime(&atv); 346 if ((error = copyout(&atv, tp, sizeof (atv)))) 347 return (error); 348 #ifdef KTRACE 349 if (KTRPOINT(p, KTR_STRUCT)) 350 ktrabstimeval(p, &atv); 351 #endif 352 } 353 if (tzp) 354 error = copyout(&zerotz, tzp, sizeof(zerotz)); 355 return (error); 356 } 357 358 int 359 sys_settimeofday(struct proc *p, void *v, register_t *retval) 360 { 361 struct sys_settimeofday_args /* { 362 syscallarg(const struct timeval *) tv; 363 syscallarg(const struct timezone *) tzp; 364 } */ *uap = v; 365 struct timezone atz; 366 struct timeval atv; 367 const struct timeval *tv; 368 const struct timezone *tzp; 369 int error; 370 371 tv = SCARG(uap, tv); 372 tzp = SCARG(uap, tzp); 373 374 if ((error = suser(p))) 375 return (error); 376 /* Verify all parameters before changing time. */ 377 if (tv && (error = copyin(tv, &atv, sizeof(atv)))) 378 return (error); 379 if (tzp && (error = copyin(tzp, &atz, sizeof(atz)))) 380 return (error); 381 if (tv) { 382 struct timespec ts; 383 384 #ifdef KTRACE 385 if (KTRPOINT(p, KTR_STRUCT)) 386 ktrabstimeval(p, &atv); 387 #endif 388 if (!timerisvalid(&atv)) 389 return (EINVAL); 390 TIMEVAL_TO_TIMESPEC(&atv, &ts); 391 if ((error = settime(&ts)) != 0) 392 return (error); 393 } 394 395 return (0); 396 } 397 398 #define ADJFREQ_MAX (500000000LL << 32) 399 #define ADJFREQ_MIN (-500000000LL << 32) 400 401 int 402 sys_adjfreq(struct proc *p, void *v, register_t *retval) 403 { 404 struct sys_adjfreq_args /* { 405 syscallarg(const int64_t *) freq; 406 syscallarg(int64_t *) oldfreq; 407 } */ *uap = v; 408 int error = 0; 409 int64_t f, oldf; 410 const int64_t *freq = SCARG(uap, freq); 411 int64_t *oldfreq = SCARG(uap, oldfreq); 412 413 if (freq) { 414 if ((error = suser(p))) 415 return (error); 416 if ((error = copyin(freq, &f, sizeof(f)))) 417 return (error); 418 if (f < ADJFREQ_MIN || f > ADJFREQ_MAX) 419 return (EINVAL); 420 } 421 422 rw_enter(&tc_lock, (freq == NULL) ? RW_READ : RW_WRITE); 423 if (oldfreq) { 424 tc_adjfreq(&oldf, NULL); 425 if ((error = copyout(&oldf, oldfreq, sizeof(oldf)))) 426 goto out; 427 } 428 if (freq) 429 tc_adjfreq(NULL, &f); 430 out: 431 rw_exit(&tc_lock); 432 return (error); 433 } 434 435 int 436 sys_adjtime(struct proc *p, void *v, register_t *retval) 437 { 438 struct sys_adjtime_args /* { 439 syscallarg(const struct timeval *) delta; 440 syscallarg(struct timeval *) olddelta; 441 } */ *uap = v; 442 struct timeval atv; 443 const struct timeval *delta = SCARG(uap, delta); 444 struct timeval *olddelta = SCARG(uap, olddelta); 445 int64_t adjustment, remaining; 446 int error; 447 448 error = pledge_adjtime(p, delta); 449 if (error) 450 return error; 451 452 if (delta) { 453 if ((error = suser(p))) 454 return (error); 455 if ((error = copyin(delta, &atv, sizeof(struct timeval)))) 456 return (error); 457 #ifdef KTRACE 458 if (KTRPOINT(p, KTR_STRUCT)) 459 ktrreltimeval(p, &atv); 460 #endif 461 if (!timerisvalid(&atv)) 462 return (EINVAL); 463 464 if (atv.tv_sec > INT64_MAX / 1000000) 465 return EINVAL; 466 if (atv.tv_sec < INT64_MIN / 1000000) 467 return EINVAL; 468 adjustment = atv.tv_sec * 1000000; 469 if (adjustment > INT64_MAX - atv.tv_usec) 470 return EINVAL; 471 adjustment += atv.tv_usec; 472 473 rw_enter_write(&tc_lock); 474 } 475 476 if (olddelta) { 477 tc_adjtime(&remaining, NULL); 478 memset(&atv, 0, sizeof(atv)); 479 atv.tv_sec = remaining / 1000000; 480 atv.tv_usec = remaining % 1000000; 481 if (atv.tv_usec < 0) { 482 atv.tv_usec += 1000000; 483 atv.tv_sec--; 484 } 485 486 if ((error = copyout(&atv, olddelta, sizeof(struct timeval)))) 487 goto out; 488 } 489 490 if (delta) 491 tc_adjtime(NULL, &adjustment); 492 out: 493 if (delta) 494 rw_exit_write(&tc_lock); 495 return (error); 496 } 497 498 499 struct mutex itimer_mtx = MUTEX_INITIALIZER(IPL_CLOCK); 500 501 /* 502 * Get or set value of an interval timer. The process virtual and 503 * profiling virtual time timers are kept internally in the 504 * way they are specified externally: in time until they expire. 505 * 506 * The real time interval timer's it_value, in contrast, is kept as an 507 * absolute time rather than as a delta, so that it is easy to keep 508 * periodic real-time signals from drifting. 509 * 510 * Virtual time timers are processed in the hardclock() routine of 511 * kern_clock.c. The real time timer is processed by a timeout 512 * routine, called from the softclock() routine. Since a callout 513 * may be delayed in real time due to interrupt processing in the system, 514 * it is possible for the real time timeout routine (realitexpire, given below), 515 * to be delayed in real time past when it is supposed to occur. It 516 * does not suffice, therefore, to reload the real timer .it_value from the 517 * real time timers .it_interval. Rather, we compute the next time in 518 * absolute time the timer should go off. 519 */ 520 void 521 setitimer(int which, const struct itimerval *itv, struct itimerval *olditv) 522 { 523 struct itimerspec its, oldits; 524 struct timespec now; 525 struct itimerspec *itimer; 526 struct process *pr; 527 int timo; 528 529 KASSERT(which >= ITIMER_REAL && which <= ITIMER_PROF); 530 531 pr = curproc->p_p; 532 itimer = &pr->ps_timer[which]; 533 534 if (itv != NULL) { 535 TIMEVAL_TO_TIMESPEC(&itv->it_value, &its.it_value); 536 TIMEVAL_TO_TIMESPEC(&itv->it_interval, &its.it_interval); 537 } 538 539 if (which != ITIMER_REAL) 540 mtx_enter(&itimer_mtx); 541 else 542 getnanouptime(&now); 543 544 if (olditv != NULL) 545 oldits = *itimer; 546 if (itv != NULL) { 547 if (which == ITIMER_REAL) { 548 if (timespecisset(&its.it_value)) { 549 timo = tstohz(&its.it_value); 550 timeout_add(&pr->ps_realit_to, timo); 551 timespecadd(&its.it_value, &now, &its.it_value); 552 } else 553 timeout_del(&pr->ps_realit_to); 554 } 555 *itimer = its; 556 } 557 558 if (which != ITIMER_REAL) 559 mtx_leave(&itimer_mtx); 560 561 if (olditv != NULL) { 562 if (which == ITIMER_REAL && timespecisset(&oldits.it_value)) { 563 if (timespeccmp(&oldits.it_value, &now, <)) 564 timespecclear(&oldits.it_value); 565 else { 566 timespecsub(&oldits.it_value, &now, 567 &oldits.it_value); 568 } 569 } 570 TIMESPEC_TO_TIMEVAL(&olditv->it_value, &oldits.it_value); 571 TIMESPEC_TO_TIMEVAL(&olditv->it_interval, &oldits.it_interval); 572 } 573 } 574 575 void 576 cancel_all_itimers(void) 577 { 578 struct itimerval itv; 579 int i; 580 581 timerclear(&itv.it_value); 582 timerclear(&itv.it_interval); 583 584 for (i = 0; i < nitems(curproc->p_p->ps_timer); i++) 585 setitimer(i, &itv, NULL); 586 } 587 588 int 589 sys_getitimer(struct proc *p, void *v, register_t *retval) 590 { 591 struct sys_getitimer_args /* { 592 syscallarg(int) which; 593 syscallarg(struct itimerval *) itv; 594 } */ *uap = v; 595 struct itimerval aitv; 596 int which; 597 598 which = SCARG(uap, which); 599 if (which < ITIMER_REAL || which > ITIMER_PROF) 600 return EINVAL; 601 602 memset(&aitv, 0, sizeof(aitv)); 603 604 setitimer(which, NULL, &aitv); 605 606 return copyout(&aitv, SCARG(uap, itv), sizeof(aitv)); 607 } 608 609 int 610 sys_setitimer(struct proc *p, void *v, register_t *retval) 611 { 612 struct sys_setitimer_args /* { 613 syscallarg(int) which; 614 syscallarg(const struct itimerval *) itv; 615 syscallarg(struct itimerval *) oitv; 616 } */ *uap = v; 617 struct itimerval aitv, olditv; 618 struct itimerval *newitvp, *olditvp; 619 int error, which; 620 621 which = SCARG(uap, which); 622 if (which < ITIMER_REAL || which > ITIMER_PROF) 623 return EINVAL; 624 625 newitvp = olditvp = NULL; 626 if (SCARG(uap, itv) != NULL) { 627 error = copyin(SCARG(uap, itv), &aitv, sizeof(aitv)); 628 if (error) 629 return error; 630 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) 631 return EINVAL; 632 if (!timerisset(&aitv.it_value)) 633 timerclear(&aitv.it_interval); 634 newitvp = &aitv; 635 } 636 if (SCARG(uap, oitv) != NULL) { 637 memset(&olditv, 0, sizeof(olditv)); 638 olditvp = &olditv; 639 } 640 if (newitvp == NULL && olditvp == NULL) 641 return 0; 642 643 setitimer(which, newitvp, olditvp); 644 645 if (SCARG(uap, oitv) != NULL) 646 return copyout(&olditv, SCARG(uap, oitv), sizeof(olditv)); 647 648 return 0; 649 } 650 651 /* 652 * Real interval timer expired: 653 * send process whose timer expired an alarm signal. 654 * If time is not set up to reload, then just return. 655 * Else compute next time timer should go off which is > current time. 656 * This is where delay in processing this timeout causes multiple 657 * SIGALRM calls to be compressed into one. 658 */ 659 void 660 realitexpire(void *arg) 661 { 662 struct timespec cts, nts; 663 struct process *pr = arg; 664 struct itimerspec *tp = &pr->ps_timer[ITIMER_REAL]; 665 int timo; 666 667 prsignal(pr, SIGALRM); 668 669 /* If it was a one-shot timer we're done. */ 670 if (!timespecisset(&tp->it_interval)) { 671 timespecclear(&tp->it_value); 672 return; 673 } 674 675 /* Find the nearest future expiration point and restart the timeout. */ 676 getnanouptime(&cts); 677 while (timespeccmp(&tp->it_value, &cts, <=)) 678 timespecadd(&tp->it_value, &tp->it_interval, &tp->it_value); 679 nts = tp->it_value; 680 timespecsub(&nts, &cts, &nts); 681 timo = tstohz(&nts) - 1; 682 if (timo <= 0) 683 timo = 1; 684 if ((pr->ps_flags & PS_EXITING) == 0) 685 timeout_add(&pr->ps_realit_to, timo); 686 } 687 688 /* 689 * Check that a proposed value to load into the .it_value or 690 * .it_interval part of an interval timer is acceptable. 691 */ 692 int 693 itimerfix(struct timeval *tv) 694 { 695 696 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 697 tv->tv_usec < 0 || tv->tv_usec >= 1000000) 698 return (EINVAL); 699 700 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 701 tv->tv_usec = tick; 702 703 return (0); 704 } 705 706 /* 707 * Decrement an interval timer by the given number of nanoseconds. 708 * If the timer expires and it is periodic then reload it. When reloading 709 * the timer we subtract any overrun from the next period so that the timer 710 * does not drift. 711 */ 712 int 713 itimerdecr(struct itimerspec *itp, long nsec) 714 { 715 struct timespec decrement; 716 717 NSEC_TO_TIMESPEC(nsec, &decrement); 718 719 mtx_enter(&itimer_mtx); 720 721 /* 722 * Double-check that the timer is enabled. A different thread 723 * in setitimer(2) may have disabled it while we were entering 724 * the mutex. 725 */ 726 if (!timespecisset(&itp->it_value)) { 727 mtx_leave(&itimer_mtx); 728 return (1); 729 } 730 731 /* 732 * The timer is enabled. Update and reload it as needed. 733 */ 734 timespecsub(&itp->it_value, &decrement, &itp->it_value); 735 if (itp->it_value.tv_sec >= 0 && timespecisset(&itp->it_value)) { 736 mtx_leave(&itimer_mtx); 737 return (1); 738 } 739 if (!timespecisset(&itp->it_interval)) { 740 timespecclear(&itp->it_value); 741 mtx_leave(&itimer_mtx); 742 return (0); 743 } 744 while (itp->it_value.tv_sec < 0 || !timespecisset(&itp->it_value)) 745 timespecadd(&itp->it_value, &itp->it_interval, &itp->it_value); 746 mtx_leave(&itimer_mtx); 747 return (0); 748 } 749 750 /* 751 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9) 752 * for usage and rationale. 753 */ 754 int 755 ratecheck(struct timeval *lasttime, const struct timeval *mininterval) 756 { 757 struct timeval tv, delta; 758 int rv = 0; 759 760 getmicrouptime(&tv); 761 762 timersub(&tv, lasttime, &delta); 763 764 /* 765 * check for 0,0 is so that the message will be seen at least once, 766 * even if interval is huge. 767 */ 768 if (timercmp(&delta, mininterval, >=) || 769 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 770 *lasttime = tv; 771 rv = 1; 772 } 773 774 return (rv); 775 } 776 777 /* 778 * ppsratecheck(): packets (or events) per second limitation. 779 */ 780 int 781 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) 782 { 783 struct timeval tv, delta; 784 int rv; 785 786 microuptime(&tv); 787 788 timersub(&tv, lasttime, &delta); 789 790 /* 791 * check for 0,0 is so that the message will be seen at least once. 792 * if more than one second have passed since the last update of 793 * lasttime, reset the counter. 794 * 795 * we do increment *curpps even in *curpps < maxpps case, as some may 796 * try to use *curpps for stat purposes as well. 797 */ 798 if (maxpps == 0) 799 rv = 0; 800 else if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || 801 delta.tv_sec >= 1) { 802 *lasttime = tv; 803 *curpps = 0; 804 rv = 1; 805 } else if (maxpps < 0) 806 rv = 1; 807 else if (*curpps < maxpps) 808 rv = 1; 809 else 810 rv = 0; 811 812 #if 1 /*DIAGNOSTIC?*/ 813 /* be careful about wrap-around */ 814 if (*curpps + 1 > *curpps) 815 *curpps = *curpps + 1; 816 #else 817 /* 818 * assume that there's not too many calls to this function. 819 * not sure if the assumption holds, as it depends on *caller's* 820 * behavior, not the behavior of this function. 821 * IMHO it is wrong to make assumption on the caller's behavior, 822 * so the above #if is #if 1, not #ifdef DIAGNOSTIC. 823 */ 824 *curpps = *curpps + 1; 825 #endif 826 827 return (rv); 828 } 829 830 todr_chip_handle_t todr_handle; 831 int inittodr_done; 832 833 #define MINYEAR ((OpenBSD / 100) - 1) /* minimum plausible year */ 834 835 /* 836 * inittodr: 837 * 838 * Initialize time from the time-of-day register. 839 */ 840 void 841 inittodr(time_t base) 842 { 843 time_t deltat; 844 struct timeval rtctime; 845 struct timespec ts; 846 int badbase; 847 848 inittodr_done = 1; 849 850 if (base < (MINYEAR - 1970) * SECYR) { 851 printf("WARNING: preposterous time in file system\n"); 852 /* read the system clock anyway */ 853 base = (MINYEAR - 1970) * SECYR; 854 badbase = 1; 855 } else 856 badbase = 0; 857 858 rtctime.tv_sec = base; 859 rtctime.tv_usec = 0; 860 861 if (todr_handle == NULL || 862 todr_gettime(todr_handle, &rtctime) != 0 || 863 rtctime.tv_sec < (MINYEAR - 1970) * SECYR) { 864 /* 865 * Believe the time in the file system for lack of 866 * anything better, resetting the TODR. 867 */ 868 rtctime.tv_sec = base; 869 rtctime.tv_usec = 0; 870 if (todr_handle != NULL && !badbase) 871 printf("WARNING: bad clock chip time\n"); 872 ts.tv_sec = rtctime.tv_sec; 873 ts.tv_nsec = rtctime.tv_usec * 1000; 874 tc_setclock(&ts); 875 goto bad; 876 } else { 877 ts.tv_sec = rtctime.tv_sec; 878 ts.tv_nsec = rtctime.tv_usec * 1000; 879 tc_setclock(&ts); 880 } 881 882 if (!badbase) { 883 /* 884 * See if we gained/lost two or more days; if 885 * so, assume something is amiss. 886 */ 887 deltat = rtctime.tv_sec - base; 888 if (deltat < 0) 889 deltat = -deltat; 890 if (deltat < 2 * SECDAY) 891 return; /* all is well */ 892 #ifndef SMALL_KERNEL 893 printf("WARNING: clock %s %lld days\n", 894 rtctime.tv_sec < base ? "lost" : "gained", 895 (long long)(deltat / SECDAY)); 896 #endif 897 } 898 bad: 899 printf("WARNING: CHECK AND RESET THE DATE!\n"); 900 } 901 902 /* 903 * resettodr: 904 * 905 * Reset the time-of-day register with the current time. 906 */ 907 void 908 resettodr(void) 909 { 910 struct timeval rtctime; 911 912 /* 913 * Skip writing the RTC if inittodr(9) never ran. We don't 914 * want to overwrite a reasonable value with a nonsense value. 915 */ 916 if (!inittodr_done) 917 return; 918 919 microtime(&rtctime); 920 921 if (todr_handle != NULL && 922 todr_settime(todr_handle, &rtctime) != 0) 923 printf("WARNING: can't update clock chip time\n"); 924 } 925 926 void 927 todr_attach(struct todr_chip_handle *todr) 928 { 929 todr_handle = todr; 930 } 931 932 #define RESETTODR_PERIOD 1800 933 934 void periodic_resettodr(void *); 935 void perform_resettodr(void *); 936 937 struct timeout resettodr_to = TIMEOUT_INITIALIZER(periodic_resettodr, NULL); 938 struct task resettodr_task = TASK_INITIALIZER(perform_resettodr, NULL); 939 940 void 941 periodic_resettodr(void *arg __unused) 942 { 943 task_add(systq, &resettodr_task); 944 } 945 946 void 947 perform_resettodr(void *arg __unused) 948 { 949 resettodr(); 950 timeout_add_sec(&resettodr_to, RESETTODR_PERIOD); 951 } 952 953 void 954 start_periodic_resettodr(void) 955 { 956 timeout_add_sec(&resettodr_to, RESETTODR_PERIOD); 957 } 958 959 void 960 stop_periodic_resettodr(void) 961 { 962 timeout_del(&resettodr_to); 963 task_del(systq, &resettodr_task); 964 } 965