kern_time.c revision 1.12
1/* $NetBSD: kern_time.c,v 1.12 1994/12/11 18:06:10 mycroft 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 45#include <sys/mount.h> 46#include <sys/syscallargs.h> 47 48#include <machine/cpu.h> 49 50/* 51 * Time of day and interval timer support. 52 * 53 * These routines provide the kernel entry points to get and set 54 * the time-of-day and per-process interval timers. Subroutines 55 * here provide support for adding and subtracting timeval structures 56 * and decrementing interval timers, optionally reloading the interval 57 * timers when they expire. 58 */ 59 60/* ARGSUSED */ 61int 62gettimeofday(p, uap, retval) 63 struct proc *p; 64 register struct gettimeofday_args /* { 65 syscallarg(struct timeval *) tp; 66 syscallarg(struct timezone *) tzp; 67 } */ *uap; 68 register_t *retval; 69{ 70 struct timeval atv; 71 int error = 0; 72 73 if (SCARG(uap, tp)) { 74 microtime(&atv); 75 if (error = copyout((caddr_t)&atv, (caddr_t)SCARG(uap, tp), 76 sizeof (atv))) 77 return (error); 78 } 79 if (SCARG(uap, tzp)) 80 error = copyout((caddr_t)&tz, (caddr_t)SCARG(uap, tzp), 81 sizeof (tz)); 82 return (error); 83} 84 85/* ARGSUSED */ 86int 87settimeofday(p, uap, retval) 88 struct proc *p; 89 struct settimeofday_args /* { 90 syscallarg(struct timeval *) tv; 91 syscallarg(struct timezone *) tzp; 92 } */ *uap; 93 register_t *retval; 94{ 95 struct timeval atv, delta; 96 struct timezone atz; 97 int error, s; 98 99 if (error = suser(p->p_ucred, &p->p_acflag)) 100 return (error); 101 /* Verify all parameters before changing time. */ 102 if (SCARG(uap, tv) && (error = copyin((caddr_t)SCARG(uap, tv), 103 (caddr_t)&atv, sizeof(atv)))) 104 return (error); 105 if (SCARG(uap, tzp) && (error = copyin((caddr_t)SCARG(uap, tzp), 106 (caddr_t)&atz, sizeof(atz)))) 107 return (error); 108 if (SCARG(uap, tv)) { 109 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ 110 delta = atv; 111 s = splclock(); 112 __timersub(&delta, &time); 113 time = atv; 114 (void) splsoftclock(); 115 __timeradd(&boottime, &delta); 116 __timeradd(&runtime, &delta); 117 LEASE_UPDATETIME(delta.tv_sec); 118 splx(s); 119 resettodr(); 120 } 121 if (SCARG(uap, tzp)) 122 tz = atz; 123 return (0); 124} 125 126int tickdelta; /* current clock skew, us. per tick */ 127long timedelta; /* unapplied time correction, us. */ 128long bigadj = 1000000; /* use 10x skew above bigadj us. */ 129 130/* ARGSUSED */ 131int 132adjtime(p, uap, retval) 133 struct proc *p; 134 register struct adjtime_args /* { 135 syscallarg(struct timeval *) delta; 136 syscallarg(struct timeval *) olddelta; 137 } */ *uap; 138 register_t *retval; 139{ 140 struct timeval atv; 141 register long ndelta, ntickdelta, odelta; 142 int s, error; 143 144 if (error = suser(p->p_ucred, &p->p_acflag)) 145 return (error); 146 if (error = copyin((caddr_t)SCARG(uap, delta), (caddr_t)&atv, 147 sizeof(struct timeval))) 148 return (error); 149 150 /* 151 * Compute the total correction and the rate at which to apply it. 152 * Round the adjustment down to a whole multiple of the per-tick 153 * delta, so that after some number of incremental changes in 154 * hardclock(), tickdelta will become zero, lest the correction 155 * overshoot and start taking us away from the desired final time. 156 */ 157 ndelta = atv.tv_sec * 1000000 + atv.tv_usec; 158 if (ndelta > bigadj) 159 ntickdelta = 10 * tickadj; 160 else 161 ntickdelta = tickadj; 162 if (ndelta % ntickdelta) 163 ndelta = ndelta / ntickdelta * ntickdelta; 164 165 /* 166 * To make hardclock()'s job easier, make the per-tick delta negative 167 * if we want time to run slower; then hardclock can simply compute 168 * tick + tickdelta, and subtract tickdelta from timedelta. 169 */ 170 if (ndelta < 0) 171 ntickdelta = -ntickdelta; 172 s = splclock(); 173 odelta = timedelta; 174 timedelta = ndelta; 175 tickdelta = ntickdelta; 176 splx(s); 177 178 if (SCARG(uap, olddelta)) { 179 atv.tv_sec = odelta / 1000000; 180 atv.tv_usec = odelta % 1000000; 181 (void) copyout((caddr_t)&atv, (caddr_t)SCARG(uap, olddelta), 182 sizeof(struct timeval)); 183 } 184 return (0); 185} 186 187/* 188 * Get value of an interval timer. The process virtual and 189 * profiling virtual time timers are kept in the p_stats area, since 190 * they can be swapped out. These are kept internally in the 191 * way they are specified externally: in time until they expire. 192 * 193 * The real time interval timer is kept in the process table slot 194 * for the process, and its value (it_value) is kept as an 195 * absolute time rather than as a delta, so that it is easy to keep 196 * periodic real-time signals from drifting. 197 * 198 * Virtual time timers are processed in the hardclock() routine of 199 * kern_clock.c. The real time timer is processed by a timeout 200 * routine, called from the softclock() routine. Since a callout 201 * may be delayed in real time due to interrupt processing in the system, 202 * it is possible for the real time timeout routine (realitexpire, given below), 203 * to be delayed in real time past when it is supposed to occur. It 204 * does not suffice, therefore, to reload the real timer .it_value from the 205 * real time timers .it_interval. Rather, we compute the next time in 206 * absolute time the timer should go off. 207 */ 208/* ARGSUSED */ 209int 210getitimer(p, uap, retval) 211 struct proc *p; 212 register struct getitimer_args /* { 213 syscallarg(u_int) which; 214 syscallarg(struct itimerval *) itv; 215 } */ *uap; 216 register_t *retval; 217{ 218 struct itimerval aitv; 219 int s; 220 221 if (SCARG(uap, which) > ITIMER_PROF) 222 return (EINVAL); 223 s = splclock(); 224 if (SCARG(uap, which) == ITIMER_REAL) { 225 /* 226 * Convert from absolute to relative time in .it_value 227 * part of real time timer. If time for real time timer 228 * has passed return 0, else return difference between 229 * current time and time for the timer to go off. 230 */ 231 aitv = p->p_realtimer; 232 if (timerisset(&aitv.it_value)) 233 if (timercmp(&aitv.it_value, &time, <)) 234 timerclear(&aitv.it_value); 235 else 236 __timersub(&aitv.it_value, &time); 237 } else 238 aitv = p->p_stats->p_timer[SCARG(uap, which)]; 239 splx(s); 240 return (copyout((caddr_t)&aitv, (caddr_t)SCARG(uap, itv), 241 sizeof (struct itimerval))); 242} 243 244/* ARGSUSED */ 245int 246setitimer(p, uap, retval) 247 struct proc *p; 248 register struct setitimer_args /* { 249 syscallarg(u_int) which; 250 syscallarg(struct itimerval *) itv; 251 syscallarg(struct itimerval *) oitv; 252 } */ *uap; 253 register_t *retval; 254{ 255 struct itimerval aitv; 256 register struct itimerval *itvp; 257 int s, error; 258 259 if (SCARG(uap, which) > ITIMER_PROF) 260 return (EINVAL); 261 itvp = SCARG(uap, itv); 262 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv, 263 sizeof(struct itimerval)))) 264 return (error); 265 if ((SCARG(uap, itv) = SCARG(uap, oitv)) && 266 (error = getitimer(p, uap, retval))) 267 return (error); 268 if (itvp == 0) 269 return (0); 270 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) 271 return (EINVAL); 272 s = splclock(); 273 if (SCARG(uap, which) == ITIMER_REAL) { 274 untimeout(realitexpire, p); 275 if (timerisset(&aitv.it_value)) { 276 __timeradd(&aitv.it_value, &time); 277 timeout(realitexpire, p, hzto(&aitv.it_value)); 278 } 279 p->p_realtimer = aitv; 280 } else 281 p->p_stats->p_timer[SCARG(uap, which)] = aitv; 282 splx(s); 283 return (0); 284} 285 286/* 287 * Real interval timer expired: 288 * send process whose timer expired an alarm signal. 289 * If time is not set up to reload, then just return. 290 * Else compute next time timer should go off which is > current time. 291 * This is where delay in processing this timeout causes multiple 292 * SIGALRM calls to be compressed into one. 293 */ 294void 295realitexpire(arg) 296 void *arg; 297{ 298 register struct proc *p; 299 int s; 300 301 p = (struct proc *)arg; 302 psignal(p, SIGALRM); 303 if (!timerisset(&p->p_realtimer.it_interval)) { 304 timerclear(&p->p_realtimer.it_value); 305 return; 306 } 307 for (;;) { 308 s = splclock(); 309 __timeradd(&p->p_realtimer.it_value, 310 &p->p_realtimer.it_interval); 311 if (timercmp(&p->p_realtimer.it_value, &time, >)) { 312 timeout(realitexpire, p, 313 hzto(&p->p_realtimer.it_value)); 314 splx(s); 315 return; 316 } 317 splx(s); 318 } 319} 320 321/* 322 * Check that a proposed value to load into the .it_value or 323 * .it_interval part of an interval timer is acceptable, and 324 * fix it to have at least minimal value (i.e. if it is less 325 * than the resolution of the clock, round it up.) 326 */ 327int 328itimerfix(tv) 329 struct timeval *tv; 330{ 331 332 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 333 tv->tv_usec < 0 || tv->tv_usec >= 1000000) 334 return (EINVAL); 335 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 336 tv->tv_usec = tick; 337 return (0); 338} 339 340/* 341 * Decrement an interval timer by a specified number 342 * of microseconds, which must be less than a second, 343 * i.e. < 1000000. If the timer expires, then reload 344 * it. In this case, carry over (usec - old value) to 345 * reduce the value reloaded into the timer so that 346 * the timer does not drift. This routine assumes 347 * that it is called in a context where the timers 348 * on which it is operating cannot change in value. 349 */ 350int 351itimerdecr(itp, usec) 352 register struct itimerval *itp; 353 int usec; 354{ 355 356 if (itp->it_value.tv_usec < usec) { 357 if (itp->it_value.tv_sec == 0) { 358 /* expired, and already in next interval */ 359 usec -= itp->it_value.tv_usec; 360 goto expire; 361 } 362 itp->it_value.tv_usec += 1000000; 363 itp->it_value.tv_sec--; 364 } 365 itp->it_value.tv_usec -= usec; 366 usec = 0; 367 if (timerisset(&itp->it_value)) 368 return (1); 369 /* expired, exactly at end of interval */ 370expire: 371 if (timerisset(&itp->it_interval)) { 372 itp->it_value = itp->it_interval; 373 itp->it_value.tv_usec -= usec; 374 if (itp->it_value.tv_usec < 0) { 375 itp->it_value.tv_usec += 1000000; 376 itp->it_value.tv_sec--; 377 } 378 } else 379 itp->it_value.tv_usec = 0; /* sec is already 0 */ 380 return (0); 381} 382