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