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