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