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