kern_time.c revision 26335
1/* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93 34 * $Id: kern_time.c,v 1.26 1997/05/10 12:00:03 peter Exp $ 35 */ 36 37#include <sys/param.h> 38#include <sys/sysproto.h> 39#include <sys/resourcevar.h> 40#include <sys/signalvar.h> 41#include <sys/kernel.h> 42#include <sys/systm.h> 43#include <sys/sysent.h> 44#include <sys/proc.h> 45#include <sys/signal.h> 46#include <sys/time.h> 47#include <sys/vnode.h> 48#include <vm/vm.h> 49#include <vm/vm_param.h> 50#include <vm/vm_extern.h> 51 52struct timezone tz; 53 54/* 55 * Time of day and interval timer support. 56 * 57 * These routines provide the kernel entry points to get and set 58 * the time-of-day and per-process interval timers. Subroutines 59 * here provide support for adding and subtracting timeval structures 60 * and decrementing interval timers, optionally reloading the interval 61 * timers when they expire. 62 */ 63 64static int settime __P((struct timeval *)); 65static void timevalfix __P((struct timeval *)); 66static int nanosleep1 __P((struct proc *p, struct timespec *rqt, 67 struct timespec *rmt)); 68 69static int 70settime(tv) 71 struct timeval *tv; 72{ 73 struct timeval delta; 74 struct proc *p; 75 int s; 76 77 /* 78 * Must not set clock backwards in highly secure mode. 79 */ 80 s = splclock(); 81 delta.tv_sec = tv->tv_sec - time.tv_sec; 82 delta.tv_usec = tv->tv_usec - time.tv_usec; 83 splx(s); 84 timevalfix(&delta); 85 if (delta.tv_sec < 0 && securelevel > 1) 86 return (EPERM); 87 88 s = splclock(); 89 /* 90 * Recalculate delta directly to minimize clock interrupt 91 * latency. Fix it after the ipl has been lowered. 92 */ 93 delta.tv_sec = tv->tv_sec - time.tv_sec; 94 delta.tv_usec = tv->tv_usec - time.tv_usec; 95 time = *tv; 96 /* 97 * XXX should arrange for microtime() to agree with *tv if 98 * it is called now. As it is, it may add up to about 99 * `tick' unwanted usec. 100 * Another problem is that clock interrupts may occur at 101 * other than multiples of `tick'. It's not worth fixing 102 * this here, since the problem is also caused by tick 103 * adjustments. 104 */ 105 (void) splsoftclock(); 106 timevalfix(&delta); 107 timevaladd(&boottime, &delta); 108 timevaladd(&runtime, &delta); 109 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 110 if (timerisset(&p->p_realtimer.it_value)) 111 timevaladd(&p->p_realtimer.it_value, &delta); 112 if (p->p_sleepend) 113 timevaladd(p->p_sleepend, &delta); 114 } 115#ifdef NFS 116 lease_updatetime(delta.tv_sec); 117#endif 118 splx(s); 119 resettodr(); 120 return (0); 121} 122 123#ifndef _SYS_SYSPROTO_H_ 124struct clock_gettime_args { 125 clockid_t clock_id; 126 struct timespec *tp; 127}; 128#endif 129 130/* ARGSUSED */ 131int 132clock_gettime(p, uap, retval) 133 struct proc *p; 134 struct clock_gettime_args *uap; 135 register_t *retval; 136{ 137 struct timeval atv; 138 struct timespec ats; 139 140 if (SCARG(uap, clock_id) != CLOCK_REALTIME) 141 return (EINVAL); 142 microtime(&atv); 143 TIMEVAL_TO_TIMESPEC(&atv, &ats); 144 return (copyout(&ats, SCARG(uap, tp), sizeof(ats))); 145} 146 147#ifndef _SYS_SYSPROTO_H_ 148struct clock_settime_args { 149 clockid_t clock_id; 150 const struct timespec *tp; 151}; 152#endif 153 154/* ARGSUSED */ 155int 156clock_settime(p, uap, retval) 157 struct proc *p; 158 struct clock_settime_args *uap; 159 register_t *retval; 160{ 161 struct timeval atv; 162 struct timespec ats; 163 int error; 164 165 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 166 return (error); 167 if (SCARG(uap, clock_id) != CLOCK_REALTIME) 168 return (EINVAL); 169 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0) 170 return (error); 171 if (atv.tv_usec < 0 || ats.tv_nsec >= 1000000000) 172 return (EINVAL); 173 TIMESPEC_TO_TIMEVAL(&atv, &ats); 174 if ((error = settime(&atv))) 175 return (error); 176 return (0); 177} 178 179#ifndef _SYS_SYSPROTO_H_ 180struct clock_getres_args { 181 clockid_t clock_id; 182 struct timespec *tp; 183}; 184#endif 185 186int 187clock_getres(p, uap, retval) 188 struct proc *p; 189 struct clock_getres_args *uap; 190 register_t *retval; 191{ 192 struct timespec ts; 193 int error; 194 195 if (SCARG(uap, clock_id) != CLOCK_REALTIME) 196 return (EINVAL); 197 error = 0; 198 if (SCARG(uap, tp)) { 199 ts.tv_sec = 0; 200 ts.tv_nsec = 1000000000 / hz; 201 error = copyout(&ts, SCARG(uap, tp), sizeof(ts)); 202 } 203 return (error); 204} 205 206static int nanowait; 207 208static int 209nanosleep1(p, rqt, rmt) 210 struct proc *p; 211 struct timespec *rqt, *rmt; 212{ 213 struct timeval atv, utv; 214 int error, s, timo; 215 216 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000) 217 return (EINVAL); 218 TIMESPEC_TO_TIMEVAL(&atv, rqt) 219 if (itimerfix(&atv)) 220 return (EINVAL); 221 222 /* 223 * XXX this is not as careful as settimeofday() about minimising 224 * interrupt latency. The hzto() interface is inconvenient as usual. 225 */ 226 s = splclock(); 227 timevaladd(&atv, &time); 228 timo = hzto(&atv); 229 splx(s); 230 231 p->p_sleepend = &atv; 232 error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp", timo); 233 p->p_sleepend = NULL; 234 235 if (error == ERESTART) 236 error = EINTR; 237 if (error == EWOULDBLOCK) 238 error = 0; 239 if (rmt != NULL) { 240 /*- 241 * XXX this is unnecessary and possibly wrong if the timeout 242 * expired. Then the remaining time should be zero. If the 243 * calculation gives a nonzero value, then we have a bug. 244 * (1) if settimeofday() was called, then the calculation is 245 * probably wrong, since `time' has probably become 246 * inconsistent with the ending time `atv'. 247 * XXX (1) should be fixed now with p->p_sleepend; 248 * (2) otherwise, our calculation of `timo' was wrong, perhaps 249 * due to `tick' being wrong when hzto() was called or 250 * changing afterwards (it can be wrong or change due to 251 * hzto() not knowing about adjtime(2) or tickadj(8)). 252 * Then we should be sleeping again instead instead of 253 * returning. Rounding up in hzto() probably fixes this 254 * problem for small timeouts, but the absolute error may 255 * be large for large timeouts. 256 */ 257 s = splclock(); 258 utv = time; 259 splx(s); 260 timevalsub(&atv, &utv); 261 if (atv.tv_sec < 0) 262 timerclear(&atv); 263 TIMEVAL_TO_TIMESPEC(&atv, rmt); 264 } 265 return (error); 266} 267 268#ifndef _SYS_SYSPROTO_H_ 269struct nanosleep_args { 270 struct timespec *rqtp; 271 struct timespec *rmtp; 272}; 273#endif 274 275/* ARGSUSED */ 276int 277nanosleep(p, uap, retval) 278 struct proc *p; 279 struct nanosleep_args *uap; 280 register_t *retval; 281{ 282 struct timespec rmt, rqt; 283 int error, error2; 284 285 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt)); 286 if (error) 287 return (error); 288 if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE)) 289 return (EFAULT); 290 291 error = nanosleep1(p, &rqt, &rmt); 292 293 if (SCARG(uap, rmtp)) { 294 error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt)); 295 if (error2) /* XXX shouldn't happen, did useracc() above */ 296 return (error2); 297 } 298 return (error); 299} 300 301#ifndef _SYS_SYSPROTO_H_ 302struct signanosleep_args { 303 struct timespec *rqtp; 304 struct timespec *rmtp; 305 sigset_t *mask; 306}; 307#endif 308 309/* ARGSUSED */ 310int 311signanosleep(p, uap, retval) 312 struct proc *p; 313 struct signanosleep_args *uap; 314 register_t *retval; 315{ 316 struct timespec rmt, rqt; 317 int error, error2; 318 struct sigacts *ps = p->p_sigacts; 319 sigset_t mask; 320 321 error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt)); 322 if (error) 323 return (error); 324 if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE)) 325 return (EFAULT); 326 error = copyin(SCARG(uap, mask), &mask, sizeof(mask)); 327 if (error) 328 return (error); 329 330 /* See kern_sig.c:sigsuspend() for explanation */ 331 ps->ps_oldmask = p->p_sigmask; 332 ps->ps_flags |= SAS_OLDMASK; 333 p->p_sigmask = mask &~ sigcantmask; 334 335 error = nanosleep1(p, &rqt, &rmt); 336 337 p->p_sigmask = ps->ps_oldmask; /* in case timeout rather than sig */ 338 ps->ps_flags &= ~SAS_OLDMASK; 339 340 if (SCARG(uap, rmtp)) { 341 error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt)); 342 if (error2) /* XXX shouldn't happen, did useracc() above */ 343 return (error2); 344 } 345 return (error); 346 347} 348 349#ifndef _SYS_SYSPROTO_H_ 350struct gettimeofday_args { 351 struct timeval *tp; 352 struct timezone *tzp; 353}; 354#endif 355/* ARGSUSED */ 356int 357gettimeofday(p, uap, retval) 358 struct proc *p; 359 register struct gettimeofday_args *uap; 360 int *retval; 361{ 362 struct timeval atv; 363 int error = 0; 364 365 if (uap->tp) { 366 microtime(&atv); 367 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp, 368 sizeof (atv)))) 369 return (error); 370 } 371 if (uap->tzp) 372 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, 373 sizeof (tz)); 374 return (error); 375} 376 377#ifndef _SYS_SYSPROTO_H_ 378struct settimeofday_args { 379 struct timeval *tv; 380 struct timezone *tzp; 381}; 382#endif 383/* ARGSUSED */ 384int 385settimeofday(p, uap, retval) 386 struct proc *p; 387 struct settimeofday_args *uap; 388 int *retval; 389{ 390 struct timeval atv; 391 struct timezone atz; 392 int error; 393 394 if ((error = suser(p->p_ucred, &p->p_acflag))) 395 return (error); 396 /* Verify all parameters before changing time. */ 397 if (uap->tv) { 398 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv, 399 sizeof(atv)))) 400 return (error); 401 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000) 402 return (EINVAL); 403 } 404 if (uap->tzp && 405 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz)))) 406 return (error); 407 if (uap->tv && (error = settime(&atv))) 408 return (error); 409 if (uap->tzp) 410 tz = atz; 411 return (0); 412} 413 414extern int tickadj; /* "standard" clock skew, us./tick */ 415int tickdelta; /* current clock skew, us. per tick */ 416long timedelta; /* unapplied time correction, us. */ 417static long bigadj = 1000000; /* use 10x skew above bigadj us. */ 418 419#ifndef _SYS_SYSPROTO_H_ 420struct adjtime_args { 421 struct timeval *delta; 422 struct timeval *olddelta; 423}; 424#endif 425/* ARGSUSED */ 426int 427adjtime(p, uap, retval) 428 struct proc *p; 429 register struct adjtime_args *uap; 430 int *retval; 431{ 432 struct timeval atv; 433 register long ndelta, ntickdelta, odelta; 434 int s, error; 435 436 if ((error = suser(p->p_ucred, &p->p_acflag))) 437 return (error); 438 if ((error = 439 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval)))) 440 return (error); 441 442 /* 443 * Compute the total correction and the rate at which to apply it. 444 * Round the adjustment down to a whole multiple of the per-tick 445 * delta, so that after some number of incremental changes in 446 * hardclock(), tickdelta will become zero, lest the correction 447 * overshoot and start taking us away from the desired final time. 448 */ 449 ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 450 if (ndelta > bigadj || ndelta < -bigadj) 451 ntickdelta = 10 * tickadj; 452 else 453 ntickdelta = tickadj; 454 if (ndelta % ntickdelta) 455 ndelta = ndelta / ntickdelta * ntickdelta; 456 457 /* 458 * To make hardclock()'s job easier, make the per-tick delta negative 459 * if we want time to run slower; then hardclock can simply compute 460 * tick + tickdelta, and subtract tickdelta from timedelta. 461 */ 462 if (ndelta < 0) 463 ntickdelta = -ntickdelta; 464 s = splclock(); 465 odelta = timedelta; 466 timedelta = ndelta; 467 tickdelta = ntickdelta; 468 splx(s); 469 470 if (uap->olddelta) { 471 atv.tv_sec = odelta / 1000000; 472 atv.tv_usec = odelta % 1000000; 473 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta, 474 sizeof(struct timeval)); 475 } 476 return (0); 477} 478 479/* 480 * Get value of an interval timer. The process virtual and 481 * profiling virtual time timers are kept in the p_stats area, since 482 * they can be swapped out. These are kept internally in the 483 * way they are specified externally: in time until they expire. 484 * 485 * The real time interval timer is kept in the process table slot 486 * for the process, and its value (it_value) is kept as an 487 * absolute time rather than as a delta, so that it is easy to keep 488 * periodic real-time signals from drifting. 489 * 490 * Virtual time timers are processed in the hardclock() routine of 491 * kern_clock.c. The real time timer is processed by a timeout 492 * routine, called from the softclock() routine. Since a callout 493 * may be delayed in real time due to interrupt processing in the system, 494 * it is possible for the real time timeout routine (realitexpire, given below), 495 * to be delayed in real time past when it is supposed to occur. It 496 * does not suffice, therefore, to reload the real timer .it_value from the 497 * real time timers .it_interval. Rather, we compute the next time in 498 * absolute time the timer should go off. 499 */ 500#ifndef _SYS_SYSPROTO_H_ 501struct getitimer_args { 502 u_int which; 503 struct itimerval *itv; 504}; 505#endif 506/* ARGSUSED */ 507int 508getitimer(p, uap, retval) 509 struct proc *p; 510 register struct getitimer_args *uap; 511 int *retval; 512{ 513 struct itimerval aitv; 514 int s; 515 516 if (uap->which > ITIMER_PROF) 517 return (EINVAL); 518 s = splclock(); 519 if (uap->which == ITIMER_REAL) { 520 /* 521 * Convert from absoulte to relative time in .it_value 522 * part of real time timer. If time for real time timer 523 * has passed return 0, else return difference between 524 * current time and time for the timer to go off. 525 */ 526 aitv = p->p_realtimer; 527 if (timerisset(&aitv.it_value)) 528 if (timercmp(&aitv.it_value, &time, <)) 529 timerclear(&aitv.it_value); 530 else 531 timevalsub(&aitv.it_value, &time); 532 } else 533 aitv = p->p_stats->p_timer[uap->which]; 534 splx(s); 535 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv, 536 sizeof (struct itimerval))); 537} 538 539#ifndef _SYS_SYSPROTO_H_ 540struct setitimer_args { 541 u_int which; 542 struct itimerval *itv, *oitv; 543}; 544#endif 545/* ARGSUSED */ 546int 547setitimer(p, uap, retval) 548 struct proc *p; 549 register struct setitimer_args *uap; 550 int *retval; 551{ 552 struct itimerval aitv; 553 register struct itimerval *itvp; 554 int s, error; 555 556 if (uap->which > ITIMER_PROF) 557 return (EINVAL); 558 itvp = uap->itv; 559 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv, 560 sizeof(struct itimerval)))) 561 return (error); 562 if ((uap->itv = uap->oitv) && 563 (error = getitimer(p, (struct getitimer_args *)uap, retval))) 564 return (error); 565 if (itvp == 0) 566 return (0); 567 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) 568 return (EINVAL); 569 s = splclock(); 570 if (uap->which == ITIMER_REAL) { 571 untimeout(realitexpire, (caddr_t)p); 572 if (timerisset(&aitv.it_value)) { 573 timevaladd(&aitv.it_value, &time); 574 timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value)); 575 } 576 p->p_realtimer = aitv; 577 } else 578 p->p_stats->p_timer[uap->which] = aitv; 579 splx(s); 580 return (0); 581} 582 583/* 584 * Real interval timer expired: 585 * send process whose timer expired an alarm signal. 586 * If time is not set up to reload, then just return. 587 * Else compute next time timer should go off which is > current time. 588 * This is where delay in processing this timeout causes multiple 589 * SIGALRM calls to be compressed into one. 590 * hzto() always adds 1 to allow for the time until the next clock 591 * interrupt being strictly less than 1 clock tick, but we don't want 592 * that here since we want to appear to be in sync with the clock 593 * interrupt even when we're delayed. 594 */ 595void 596realitexpire(arg) 597 void *arg; 598{ 599 register struct proc *p; 600 int s; 601 602 p = (struct proc *)arg; 603 psignal(p, SIGALRM); 604 if (!timerisset(&p->p_realtimer.it_interval)) { 605 timerclear(&p->p_realtimer.it_value); 606 return; 607 } 608 for (;;) { 609 s = splclock(); 610 timevaladd(&p->p_realtimer.it_value, 611 &p->p_realtimer.it_interval); 612 if (timercmp(&p->p_realtimer.it_value, &time, >)) { 613 timeout(realitexpire, (caddr_t)p, 614 hzto(&p->p_realtimer.it_value) - 1); 615 splx(s); 616 return; 617 } 618 splx(s); 619 } 620} 621 622/* 623 * Check that a proposed value to load into the .it_value or 624 * .it_interval part of an interval timer is acceptable, and 625 * fix it to have at least minimal value (i.e. if it is less 626 * than the resolution of the clock, round it up.) 627 */ 628int 629itimerfix(tv) 630 struct timeval *tv; 631{ 632 633 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 634 tv->tv_usec < 0 || tv->tv_usec >= 1000000) 635 return (EINVAL); 636 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 637 tv->tv_usec = tick; 638 return (0); 639} 640 641/* 642 * Decrement an interval timer by a specified number 643 * of microseconds, which must be less than a second, 644 * i.e. < 1000000. If the timer expires, then reload 645 * it. In this case, carry over (usec - old value) to 646 * reduce the value reloaded into the timer so that 647 * the timer does not drift. This routine assumes 648 * that it is called in a context where the timers 649 * on which it is operating cannot change in value. 650 */ 651int 652itimerdecr(itp, usec) 653 register struct itimerval *itp; 654 int usec; 655{ 656 657 if (itp->it_value.tv_usec < usec) { 658 if (itp->it_value.tv_sec == 0) { 659 /* expired, and already in next interval */ 660 usec -= itp->it_value.tv_usec; 661 goto expire; 662 } 663 itp->it_value.tv_usec += 1000000; 664 itp->it_value.tv_sec--; 665 } 666 itp->it_value.tv_usec -= usec; 667 usec = 0; 668 if (timerisset(&itp->it_value)) 669 return (1); 670 /* expired, exactly at end of interval */ 671expire: 672 if (timerisset(&itp->it_interval)) { 673 itp->it_value = itp->it_interval; 674 itp->it_value.tv_usec -= usec; 675 if (itp->it_value.tv_usec < 0) { 676 itp->it_value.tv_usec += 1000000; 677 itp->it_value.tv_sec--; 678 } 679 } else 680 itp->it_value.tv_usec = 0; /* sec is already 0 */ 681 return (0); 682} 683 684/* 685 * Add and subtract routines for timevals. 686 * N.B.: subtract routine doesn't deal with 687 * results which are before the beginning, 688 * it just gets very confused in this case. 689 * Caveat emptor. 690 */ 691void 692timevaladd(t1, t2) 693 struct timeval *t1, *t2; 694{ 695 696 t1->tv_sec += t2->tv_sec; 697 t1->tv_usec += t2->tv_usec; 698 timevalfix(t1); 699} 700 701void 702timevalsub(t1, t2) 703 struct timeval *t1, *t2; 704{ 705 706 t1->tv_sec -= t2->tv_sec; 707 t1->tv_usec -= t2->tv_usec; 708 timevalfix(t1); 709} 710 711static void 712timevalfix(t1) 713 struct timeval *t1; 714{ 715 716 if (t1->tv_usec < 0) { 717 t1->tv_sec--; 718 t1->tv_usec += 1000000; 719 } 720 if (t1->tv_usec >= 1000000) { 721 t1->tv_sec++; 722 t1->tv_usec -= 1000000; 723 } 724} 725