kern_time.c revision 1.46
1/* $NetBSD: kern_time.c,v 1.46 2000/05/26 21:20:32 thorpej Exp $ */ 2 3/*- 4 * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christopher G. Demetriou. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39/* 40 * Copyright (c) 1982, 1986, 1989, 1993 41 * The Regents of the University of California. All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by the University of 54 * California, Berkeley and its contributors. 55 * 4. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95 72 */ 73 74#include "fs_nfs.h" 75#include "opt_nfsserver.h" 76 77#include <sys/param.h> 78#include <sys/resourcevar.h> 79#include <sys/kernel.h> 80#include <sys/systm.h> 81#include <sys/proc.h> 82#include <sys/vnode.h> 83#include <sys/signalvar.h> 84#include <sys/syslog.h> 85 86#include <sys/mount.h> 87#include <sys/syscallargs.h> 88 89#include <vm/vm.h> 90#include <uvm/uvm_extern.h> 91 92#if defined(NFS) || defined(NFSSERVER) 93#include <nfs/rpcv2.h> 94#include <nfs/nfsproto.h> 95#include <nfs/nfs_var.h> 96#endif 97 98#include <machine/cpu.h> 99 100/* 101 * Time of day and interval timer support. 102 * 103 * These routines provide the kernel entry points to get and set 104 * the time-of-day and per-process interval timers. Subroutines 105 * here provide support for adding and subtracting timeval structures 106 * and decrementing interval timers, optionally reloading the interval 107 * timers when they expire. 108 */ 109 110/* This function is used by clock_settime and settimeofday */ 111int 112settime(tv) 113 struct timeval *tv; 114{ 115 struct timeval delta; 116 struct schedstate_percpu *spc = &curcpu()->ci_schedstate; 117 int s; 118 119 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 120 s = splclock(); 121 timersub(tv, &time, &delta); 122 if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1) 123 return (EPERM); 124#ifdef notyet 125 if ((delta.tv_sec < 86400) && securelevel > 0) 126 return (EPERM); 127#endif 128 time = *tv; 129 (void) spllowersoftclock(); 130 timeradd(&boottime, &delta, &boottime); 131 timeradd(&spc->spc_runtime, &delta, &spc->spc_runtime); 132# if defined(NFS) || defined(NFSSERVER) 133 nqnfs_lease_updatetime(delta.tv_sec); 134# endif 135 splx(s); 136 resettodr(); 137 return (0); 138} 139 140/* ARGSUSED */ 141int 142sys_clock_gettime(p, v, retval) 143 struct proc *p; 144 void *v; 145 register_t *retval; 146{ 147 struct sys_clock_gettime_args /* { 148 syscallarg(clockid_t) clock_id; 149 syscallarg(struct timespec *) tp; 150 } */ *uap = v; 151 clockid_t clock_id; 152 struct timeval atv; 153 struct timespec ats; 154 155 clock_id = SCARG(uap, clock_id); 156 if (clock_id != CLOCK_REALTIME) 157 return (EINVAL); 158 159 microtime(&atv); 160 TIMEVAL_TO_TIMESPEC(&atv,&ats); 161 162 return copyout(&ats, SCARG(uap, tp), sizeof(ats)); 163} 164 165/* ARGSUSED */ 166int 167sys_clock_settime(p, v, retval) 168 struct proc *p; 169 void *v; 170 register_t *retval; 171{ 172 struct sys_clock_settime_args /* { 173 syscallarg(clockid_t) clock_id; 174 syscallarg(const struct timespec *) tp; 175 } */ *uap = v; 176 clockid_t clock_id; 177 struct timeval atv; 178 struct timespec ats; 179 int error; 180 181 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 182 return (error); 183 184 clock_id = SCARG(uap, clock_id); 185 if (clock_id != CLOCK_REALTIME) 186 return (EINVAL); 187 188 if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0) 189 return (error); 190 191 TIMESPEC_TO_TIMEVAL(&atv,&ats); 192 if ((error = settime(&atv))) 193 return (error); 194 195 return 0; 196} 197 198int 199sys_clock_getres(p, v, retval) 200 struct proc *p; 201 void *v; 202 register_t *retval; 203{ 204 struct sys_clock_getres_args /* { 205 syscallarg(clockid_t) clock_id; 206 syscallarg(struct timespec *) tp; 207 } */ *uap = v; 208 clockid_t clock_id; 209 struct timespec ts; 210 int error = 0; 211 212 clock_id = SCARG(uap, clock_id); 213 if (clock_id != CLOCK_REALTIME) 214 return (EINVAL); 215 216 if (SCARG(uap, tp)) { 217 ts.tv_sec = 0; 218 ts.tv_nsec = 1000000000 / hz; 219 220 error = copyout(&ts, SCARG(uap, tp), sizeof(ts)); 221 } 222 223 return error; 224} 225 226/* ARGSUSED */ 227int 228sys_nanosleep(p, v, retval) 229 struct proc *p; 230 void *v; 231 register_t *retval; 232{ 233 static int nanowait; 234 struct sys_nanosleep_args/* { 235 syscallarg(struct timespec *) rqtp; 236 syscallarg(struct timespec *) rmtp; 237 } */ *uap = v; 238 struct timespec rqt; 239 struct timespec rmt; 240 struct timeval atv, utv; 241 int error, s, timo; 242 243 error = copyin((caddr_t)SCARG(uap, rqtp), (caddr_t)&rqt, 244 sizeof(struct timespec)); 245 if (error) 246 return (error); 247 248 TIMESPEC_TO_TIMEVAL(&atv,&rqt) 249 if (itimerfix(&atv)) 250 return (EINVAL); 251 252 s = splclock(); 253 timeradd(&atv,&time,&atv); 254 timo = hzto(&atv); 255 /* 256 * Avoid inadvertantly sleeping forever 257 */ 258 if (timo == 0) 259 timo = 1; 260 splx(s); 261 262 error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo); 263 if (error == ERESTART) 264 error = EINTR; 265 if (error == EWOULDBLOCK) 266 error = 0; 267 268 if (SCARG(uap, rmtp)) { 269 int error; 270 271 s = splclock(); 272 utv = time; 273 splx(s); 274 275 timersub(&atv, &utv, &utv); 276 if (utv.tv_sec < 0) 277 timerclear(&utv); 278 279 TIMEVAL_TO_TIMESPEC(&utv,&rmt); 280 error = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp), 281 sizeof(rmt)); 282 if (error) 283 return (error); 284 } 285 286 return error; 287} 288 289/* ARGSUSED */ 290int 291sys_gettimeofday(p, v, retval) 292 struct proc *p; 293 void *v; 294 register_t *retval; 295{ 296 struct sys_gettimeofday_args /* { 297 syscallarg(struct timeval *) tp; 298 syscallarg(struct timezone *) tzp; 299 } */ *uap = v; 300 struct timeval atv; 301 int error = 0; 302 struct timezone tzfake; 303 304 if (SCARG(uap, tp)) { 305 microtime(&atv); 306 error = copyout(&atv, SCARG(uap, tp), sizeof(atv)); 307 if (error) 308 return (error); 309 } 310 if (SCARG(uap, tzp)) { 311 /* 312 * NetBSD has no kernel notion of time zone, so we just 313 * fake up a timezone struct and return it if demanded. 314 */ 315 tzfake.tz_minuteswest = 0; 316 tzfake.tz_dsttime = 0; 317 error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake)); 318 } 319 return (error); 320} 321 322/* ARGSUSED */ 323int 324sys_settimeofday(p, v, retval) 325 struct proc *p; 326 void *v; 327 register_t *retval; 328{ 329 struct sys_settimeofday_args /* { 330 syscallarg(const struct timeval *) tv; 331 syscallarg(const struct timezone *) tzp; 332 } */ *uap = v; 333 struct timeval atv; 334 struct timezone atz; 335 int error; 336 337 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 338 return (error); 339 /* Verify all parameters before changing time. */ 340 if (SCARG(uap, tv) && (error = copyin(SCARG(uap, tv), 341 &atv, sizeof(atv)))) 342 return (error); 343 /* XXX since we don't use tz, probably no point in doing copyin. */ 344 if (SCARG(uap, tzp) && (error = copyin(SCARG(uap, tzp), 345 &atz, sizeof(atz)))) 346 return (error); 347 if (SCARG(uap, tv)) 348 if ((error = settime(&atv))) 349 return (error); 350 /* 351 * NetBSD has no kernel notion of time zone, and only an 352 * obsolete program would try to set it, so we log a warning. 353 */ 354 if (SCARG(uap, tzp)) 355 log(LOG_WARNING, "pid %d attempted to set the " 356 "(obsolete) kernel time zone\n", p->p_pid); 357 return (0); 358} 359 360int tickdelta; /* current clock skew, us. per tick */ 361long timedelta; /* unapplied time correction, us. */ 362long bigadj = 1000000; /* use 10x skew above bigadj us. */ 363 364/* ARGSUSED */ 365int 366sys_adjtime(p, v, retval) 367 struct proc *p; 368 void *v; 369 register_t *retval; 370{ 371 struct sys_adjtime_args /* { 372 syscallarg(const struct timeval *) delta; 373 syscallarg(struct timeval *) olddelta; 374 } */ *uap = v; 375 struct timeval atv; 376 long ndelta, ntickdelta, odelta; 377 int s, error; 378 379 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 380 return (error); 381 382 error = copyin(SCARG(uap, delta), &atv, sizeof(struct timeval)); 383 if (error) 384 return (error); 385 if (SCARG(uap, olddelta) != NULL && 386 uvm_useracc((caddr_t)SCARG(uap, olddelta), sizeof(struct timeval), 387 B_WRITE) == FALSE) 388 return (EFAULT); 389 390 /* 391 * Compute the total correction and the rate at which to apply it. 392 * Round the adjustment down to a whole multiple of the per-tick 393 * delta, so that after some number of incremental changes in 394 * hardclock(), tickdelta will become zero, lest the correction 395 * overshoot and start taking us away from the desired final time. 396 */ 397 ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 398 if (ndelta > bigadj || ndelta < -bigadj) 399 ntickdelta = 10 * tickadj; 400 else 401 ntickdelta = tickadj; 402 if (ndelta % ntickdelta) 403 ndelta = ndelta / ntickdelta * ntickdelta; 404 405 /* 406 * To make hardclock()'s job easier, make the per-tick delta negative 407 * if we want time to run slower; then hardclock can simply compute 408 * tick + tickdelta, and subtract tickdelta from timedelta. 409 */ 410 if (ndelta < 0) 411 ntickdelta = -ntickdelta; 412 s = splclock(); 413 odelta = timedelta; 414 timedelta = ndelta; 415 tickdelta = ntickdelta; 416 splx(s); 417 418 if (SCARG(uap, olddelta)) { 419 atv.tv_sec = odelta / 1000000; 420 atv.tv_usec = odelta % 1000000; 421 (void) copyout(&atv, SCARG(uap, olddelta), 422 sizeof(struct timeval)); 423 } 424 return (0); 425} 426 427/* 428 * Get value of an interval timer. The process virtual and 429 * profiling virtual time timers are kept in the p_stats area, since 430 * they can be swapped out. These are kept internally in the 431 * way they are specified externally: in time until they expire. 432 * 433 * The real time interval timer is kept in the process table slot 434 * for the process, and its value (it_value) is kept as an 435 * absolute time rather than as a delta, so that it is easy to keep 436 * periodic real-time signals from drifting. 437 * 438 * Virtual time timers are processed in the hardclock() routine of 439 * kern_clock.c. The real time timer is processed by a timeout 440 * routine, called from the softclock() routine. Since a callout 441 * may be delayed in real time due to interrupt processing in the system, 442 * it is possible for the real time timeout routine (realitexpire, given below), 443 * to be delayed in real time past when it is supposed to occur. It 444 * does not suffice, therefore, to reload the real timer .it_value from the 445 * real time timers .it_interval. Rather, we compute the next time in 446 * absolute time the timer should go off. 447 */ 448/* ARGSUSED */ 449int 450sys_getitimer(p, v, retval) 451 struct proc *p; 452 void *v; 453 register_t *retval; 454{ 455 struct sys_getitimer_args /* { 456 syscallarg(int) which; 457 syscallarg(struct itimerval *) itv; 458 } */ *uap = v; 459 int which = SCARG(uap, which); 460 struct itimerval aitv; 461 int s; 462 463 if ((u_int)which > ITIMER_PROF) 464 return (EINVAL); 465 s = splclock(); 466 if (which == ITIMER_REAL) { 467 /* 468 * Convert from absolute to relative time in .it_value 469 * part of real time timer. If time for real time timer 470 * has passed return 0, else return difference between 471 * current time and time for the timer to go off. 472 */ 473 aitv = p->p_realtimer; 474 if (timerisset(&aitv.it_value)) { 475 if (timercmp(&aitv.it_value, &time, <)) 476 timerclear(&aitv.it_value); 477 else 478 timersub(&aitv.it_value, &time, &aitv.it_value); 479 } 480 } else 481 aitv = p->p_stats->p_timer[which]; 482 splx(s); 483 return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval))); 484} 485 486/* ARGSUSED */ 487int 488sys_setitimer(p, v, retval) 489 struct proc *p; 490 void *v; 491 register_t *retval; 492{ 493 struct sys_setitimer_args /* { 494 syscallarg(int) which; 495 syscallarg(const struct itimerval *) itv; 496 syscallarg(struct itimerval *) oitv; 497 } */ *uap = v; 498 int which = SCARG(uap, which); 499 struct sys_getitimer_args getargs; 500 struct itimerval aitv; 501 const struct itimerval *itvp; 502 int s, error; 503 504 if ((u_int)which > ITIMER_PROF) 505 return (EINVAL); 506 itvp = SCARG(uap, itv); 507 if (itvp && (error = copyin(itvp, &aitv, sizeof(struct itimerval)))) 508 return (error); 509 if (SCARG(uap, oitv) != NULL) { 510 SCARG(&getargs, which) = which; 511 SCARG(&getargs, itv) = SCARG(uap, oitv); 512 if ((error = sys_getitimer(p, &getargs, retval)) != 0) 513 return (error); 514 } 515 if (itvp == 0) 516 return (0); 517 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) 518 return (EINVAL); 519 s = splclock(); 520 if (which == ITIMER_REAL) { 521 callout_stop(&p->p_realit_ch); 522 if (timerisset(&aitv.it_value)) { 523 timeradd(&aitv.it_value, &time, &aitv.it_value); 524 callout_reset(&p->p_realit_ch, hzto(&aitv.it_value), 525 realitexpire, p); 526 } 527 p->p_realtimer = aitv; 528 } else 529 p->p_stats->p_timer[which] = aitv; 530 splx(s); 531 return (0); 532} 533 534/* 535 * Real interval timer expired: 536 * send process whose timer expired an alarm signal. 537 * If time is not set up to reload, then just return. 538 * Else compute next time timer should go off which is > current time. 539 * This is where delay in processing this timeout causes multiple 540 * SIGALRM calls to be compressed into one. 541 */ 542void 543realitexpire(arg) 544 void *arg; 545{ 546 struct proc *p; 547 int s; 548 549 p = (struct proc *)arg; 550 psignal(p, SIGALRM); 551 if (!timerisset(&p->p_realtimer.it_interval)) { 552 timerclear(&p->p_realtimer.it_value); 553 return; 554 } 555 for (;;) { 556 s = splclock(); 557 timeradd(&p->p_realtimer.it_value, 558 &p->p_realtimer.it_interval, &p->p_realtimer.it_value); 559 if (timercmp(&p->p_realtimer.it_value, &time, >)) { 560 callout_reset(&p->p_realit_ch, 561 hzto(&p->p_realtimer.it_value), realitexpire, p); 562 splx(s); 563 return; 564 } 565 splx(s); 566 } 567} 568 569/* 570 * Check that a proposed value to load into the .it_value or 571 * .it_interval part of an interval timer is acceptable, and 572 * fix it to have at least minimal value (i.e. if it is less 573 * than the resolution of the clock, round it up.) 574 */ 575int 576itimerfix(tv) 577 struct timeval *tv; 578{ 579 580 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 581 tv->tv_usec < 0 || tv->tv_usec >= 1000000) 582 return (EINVAL); 583 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 584 tv->tv_usec = tick; 585 return (0); 586} 587 588/* 589 * Decrement an interval timer by a specified number 590 * of microseconds, which must be less than a second, 591 * i.e. < 1000000. If the timer expires, then reload 592 * it. In this case, carry over (usec - old value) to 593 * reduce the value reloaded into the timer so that 594 * the timer does not drift. This routine assumes 595 * that it is called in a context where the timers 596 * on which it is operating cannot change in value. 597 */ 598int 599itimerdecr(itp, usec) 600 struct itimerval *itp; 601 int usec; 602{ 603 604 if (itp->it_value.tv_usec < usec) { 605 if (itp->it_value.tv_sec == 0) { 606 /* expired, and already in next interval */ 607 usec -= itp->it_value.tv_usec; 608 goto expire; 609 } 610 itp->it_value.tv_usec += 1000000; 611 itp->it_value.tv_sec--; 612 } 613 itp->it_value.tv_usec -= usec; 614 usec = 0; 615 if (timerisset(&itp->it_value)) 616 return (1); 617 /* expired, exactly at end of interval */ 618expire: 619 if (timerisset(&itp->it_interval)) { 620 itp->it_value = itp->it_interval; 621 itp->it_value.tv_usec -= usec; 622 if (itp->it_value.tv_usec < 0) { 623 itp->it_value.tv_usec += 1000000; 624 itp->it_value.tv_sec--; 625 } 626 } else 627 itp->it_value.tv_usec = 0; /* sec is already 0 */ 628 return (0); 629} 630 631/* 632 * ratecheck(): simple time-based rate-limit checking. see ratecheck(9) 633 * for usage and rationale. 634 */ 635int 636ratecheck(lasttime, mininterval) 637 struct timeval *lasttime; 638 const struct timeval *mininterval; 639{ 640 struct timeval delta; 641 int s, rv = 0; 642 643 s = splclock(); 644 timersub(&mono_time, lasttime, &delta); 645 646 /* 647 * check for 0,0 is so that the message will be seen at least once, 648 * even if interval is huge. 649 */ 650 if (timercmp(&delta, mininterval, >=) || 651 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { 652 *lasttime = mono_time; 653 rv = 1; 654 } 655 splx(s); 656 657 return (rv); 658} 659