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