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 */ 35 36 #include <sys/param.h> 37 #include <sys/resourcevar.h> 38 #include <sys/kernel.h> 39 #include <sys/systm.h> 40 #include <sys/proc.h> 41 #include <sys/vnode.h> 42 43 #include <machine/cpu.h> 44 45 void timevaladd __P((struct timeval *, struct timeval *)); 46 void timevalsub __P((struct timeval *, struct timeval *)); 47 void timevalfix __P((struct timeval *)); 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 */ 301 void 302 realitexpire(arg) 303 void *arg; 304 { 305 register struct proc *p; 306 int s; 307 308 p = (struct proc *)arg; 309 psignal(p, SIGALRM); 310 if (!timerisset(&p->p_realtimer.it_interval)) { 311 timerclear(&p->p_realtimer.it_value); 312 return; 313 } 314 for (;;) { 315 s = splclock(); 316 timevaladd(&p->p_realtimer.it_value, 317 &p->p_realtimer.it_interval); 318 if (timercmp(&p->p_realtimer.it_value, &time, >)) { 319 timeout(realitexpire, (caddr_t)p, 320 hzto(&p->p_realtimer.it_value)); 321 splx(s); 322 return; 323 } 324 splx(s); 325 } 326 } 327 328 /* 329 * Check that a proposed value to load into the .it_value or 330 * .it_interval part of an interval timer is acceptable, and 331 * fix it to have at least minimal value (i.e. if it is less 332 * than the resolution of the clock, round it up.) 333 */ 334 int 335 itimerfix(tv) 336 struct timeval *tv; 337 { 338 339 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 340 tv->tv_usec < 0 || tv->tv_usec >= 1000000) 341 return (EINVAL); 342 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) 343 tv->tv_usec = tick; 344 return (0); 345 } 346 347 /* 348 * Decrement an interval timer by a specified number 349 * of microseconds, which must be less than a second, 350 * i.e. < 1000000. If the timer expires, then reload 351 * it. In this case, carry over (usec - old value) to 352 * reduce the value reloaded into the timer so that 353 * the timer does not drift. This routine assumes 354 * that it is called in a context where the timers 355 * on which it is operating cannot change in value. 356 */ 357 int 358 itimerdecr(itp, usec) 359 register struct itimerval *itp; 360 int usec; 361 { 362 363 if (itp->it_value.tv_usec < usec) { 364 if (itp->it_value.tv_sec == 0) { 365 /* expired, and already in next interval */ 366 usec -= itp->it_value.tv_usec; 367 goto expire; 368 } 369 itp->it_value.tv_usec += 1000000; 370 itp->it_value.tv_sec--; 371 } 372 itp->it_value.tv_usec -= usec; 373 usec = 0; 374 if (timerisset(&itp->it_value)) 375 return (1); 376 /* expired, exactly at end of interval */ 377 expire: 378 if (timerisset(&itp->it_interval)) { 379 itp->it_value = itp->it_interval; 380 itp->it_value.tv_usec -= usec; 381 if (itp->it_value.tv_usec < 0) { 382 itp->it_value.tv_usec += 1000000; 383 itp->it_value.tv_sec--; 384 } 385 } else 386 itp->it_value.tv_usec = 0; /* sec is already 0 */ 387 return (0); 388 } 389 390 /* 391 * Add and subtract routines for timevals. 392 * N.B.: subtract routine doesn't deal with 393 * results which are before the beginning, 394 * it just gets very confused in this case. 395 * Caveat emptor. 396 */ 397 void 398 timevaladd(t1, t2) 399 struct timeval *t1, *t2; 400 { 401 402 t1->tv_sec += t2->tv_sec; 403 t1->tv_usec += t2->tv_usec; 404 timevalfix(t1); 405 } 406 407 void 408 timevalsub(t1, t2) 409 struct timeval *t1, *t2; 410 { 411 412 t1->tv_sec -= t2->tv_sec; 413 t1->tv_usec -= t2->tv_usec; 414 timevalfix(t1); 415 } 416 417 void 418 timevalfix(t1) 419 struct timeval *t1; 420 { 421 422 if (t1->tv_usec < 0) { 423 t1->tv_sec--; 424 t1->tv_usec += 1000000; 425 } 426 if (t1->tv_usec >= 1000000) { 427 t1->tv_sec++; 428 t1->tv_usec -= 1000000; 429 } 430 } 431