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