| 1 | /* |
| 2 | * Copyright (c) 1982, 1986, 1989 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * %sccs.include.redist.c% |
| 6 | * |
| 7 | * @(#)kern_time.c 7.14 (Berkeley) %G% |
| 8 | */ |
| 9 | |
| 10 | #include "param.h" |
| 11 | #include "user.h" |
| 12 | #include "kernel.h" |
| 13 | #include "proc.h" |
| 14 | |
| 15 | |
| 16 | /* |
| 17 | * Time of day and interval timer support. |
| 18 | * |
| 19 | * These routines provide the kernel entry points to get and set |
| 20 | * the time-of-day and per-process interval timers. Subroutines |
| 21 | * here provide support for adding and subtracting timeval structures |
| 22 | * and decrementing interval timers, optionally reloading the interval |
| 23 | * timers when they expire. |
| 24 | */ |
| 25 | |
| 26 | /* ARGSUSED */ |
| 27 | gettimeofday(p, uap, retval) |
| 28 | struct proc *p; |
| 29 | register struct args { |
| 30 | struct timeval *tp; |
| 31 | struct timezone *tzp; |
| 32 | } *uap; |
| 33 | int *retval; |
| 34 | { |
| 35 | struct timeval atv; |
| 36 | int error = 0; |
| 37 | |
| 38 | if (uap->tp) { |
| 39 | microtime(&atv); |
| 40 | if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp, |
| 41 | sizeof (atv))) |
| 42 | return (error); |
| 43 | } |
| 44 | if (uap->tzp) |
| 45 | error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, |
| 46 | sizeof (tz)); |
| 47 | return (error); |
| 48 | } |
| 49 | |
| 50 | /* ARGSUSED */ |
| 51 | settimeofday(p, uap, retval) |
| 52 | struct proc *p; |
| 53 | struct args { |
| 54 | struct timeval *tv; |
| 55 | struct timezone *tzp; |
| 56 | } *uap; |
| 57 | int *retval; |
| 58 | { |
| 59 | struct timeval atv; |
| 60 | struct timezone atz; |
| 61 | int error, s; |
| 62 | |
| 63 | if (error = suser(u.u_cred, &u.u_acflag)) |
| 64 | return (error); |
| 65 | if (uap->tv) { |
| 66 | if (error = copyin((caddr_t)uap->tv, (caddr_t)&atv, |
| 67 | sizeof (struct timeval))) |
| 68 | return (error); |
| 69 | /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ |
| 70 | boottime.tv_sec += atv.tv_sec - time.tv_sec; |
| 71 | s = splhigh(); time = atv; splx(s); |
| 72 | resettodr(); |
| 73 | } |
| 74 | if (uap->tzp && (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, |
| 75 | sizeof (atz))) == 0) |
| 76 | tz = atz; |
| 77 | return (error); |
| 78 | } |
| 79 | |
| 80 | extern int tickadj; /* "standard" clock skew, us./tick */ |
| 81 | int tickdelta; /* current clock skew, us. per tick */ |
| 82 | long timedelta; /* unapplied time correction, us. */ |
| 83 | long bigadj = 1000000; /* use 10x skew above bigadj us. */ |
| 84 | |
| 85 | /* ARGSUSED */ |
| 86 | adjtime(p, uap, retval) |
| 87 | struct proc *p; |
| 88 | register struct args { |
| 89 | struct timeval *delta; |
| 90 | struct timeval *olddelta; |
| 91 | } *uap; |
| 92 | int *retval; |
| 93 | { |
| 94 | struct timeval atv, oatv; |
| 95 | register long ndelta; |
| 96 | int s, error; |
| 97 | |
| 98 | if (error = suser(u.u_cred, &u.u_acflag)) |
| 99 | return (error); |
| 100 | if (error = |
| 101 | copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval))) |
| 102 | return (error); |
| 103 | ndelta = atv.tv_sec * 1000000 + atv.tv_usec; |
| 104 | if (timedelta == 0) |
| 105 | if (ndelta > bigadj) |
| 106 | tickdelta = 10 * tickadj; |
| 107 | else |
| 108 | tickdelta = tickadj; |
| 109 | if (ndelta % tickdelta) |
| 110 | ndelta = ndelta / tickadj * tickadj; |
| 111 | |
| 112 | s = splclock(); |
| 113 | if (uap->olddelta) { |
| 114 | oatv.tv_sec = timedelta / 1000000; |
| 115 | oatv.tv_usec = timedelta % 1000000; |
| 116 | } |
| 117 | timedelta = ndelta; |
| 118 | splx(s); |
| 119 | |
| 120 | if (uap->olddelta) |
| 121 | (void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta, |
| 122 | sizeof (struct timeval)); |
| 123 | return (0); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Get value of an interval timer. The process virtual and |
| 128 | * profiling virtual time timers are kept in the u. area, since |
| 129 | * they can be swapped out. These are kept internally in the |
| 130 | * way they are specified externally: in time until they expire. |
| 131 | * |
| 132 | * The real time interval timer is kept in the process table slot |
| 133 | * for the process, and its value (it_value) is kept as an |
| 134 | * absolute time rather than as a delta, so that it is easy to keep |
| 135 | * periodic real-time signals from drifting. |
| 136 | * |
| 137 | * Virtual time timers are processed in the hardclock() routine of |
| 138 | * kern_clock.c. The real time timer is processed by a timeout |
| 139 | * routine, called from the softclock() routine. Since a callout |
| 140 | * may be delayed in real time due to interrupt processing in the system, |
| 141 | * it is possible for the real time timeout routine (realitexpire, given below), |
| 142 | * to be delayed in real time past when it is supposed to occur. It |
| 143 | * does not suffice, therefore, to reload the real timer .it_value from the |
| 144 | * real time timers .it_interval. Rather, we compute the next time in |
| 145 | * absolute time the timer should go off. |
| 146 | */ |
| 147 | /* ARGSUSED */ |
| 148 | getitimer(p, uap, retval) |
| 149 | struct proc *p; |
| 150 | register struct args { |
| 151 | u_int which; |
| 152 | struct itimerval *itv; |
| 153 | } *uap; |
| 154 | int *retval; |
| 155 | { |
| 156 | struct itimerval aitv; |
| 157 | int s; |
| 158 | |
| 159 | if (uap->which > ITIMER_PROF) |
| 160 | return (EINVAL); |
| 161 | s = splclock(); |
| 162 | if (uap->which == ITIMER_REAL) { |
| 163 | /* |
| 164 | * Convert from absoulte to relative time in .it_value |
| 165 | * part of real time timer. If time for real time timer |
| 166 | * has passed return 0, else return difference between |
| 167 | * current time and time for the timer to go off. |
| 168 | */ |
| 169 | aitv = p->p_realtimer; |
| 170 | if (timerisset(&aitv.it_value)) |
| 171 | if (timercmp(&aitv.it_value, &time, <)) |
| 172 | timerclear(&aitv.it_value); |
| 173 | else |
| 174 | timevalsub(&aitv.it_value, &time); |
| 175 | } else |
| 176 | aitv = u.u_timer[uap->which]; |
| 177 | splx(s); |
| 178 | return (copyout((caddr_t)&aitv, (caddr_t)uap->itv, |
| 179 | sizeof (struct itimerval))); |
| 180 | } |
| 181 | |
| 182 | /* ARGSUSED */ |
| 183 | setitimer(p, uap, retval) |
| 184 | struct proc *p; |
| 185 | register struct args { |
| 186 | u_int which; |
| 187 | struct itimerval *itv, *oitv; |
| 188 | } *uap; |
| 189 | int *retval; |
| 190 | { |
| 191 | struct itimerval aitv; |
| 192 | register struct itimerval *itvp; |
| 193 | int s, error; |
| 194 | |
| 195 | if (uap->which > ITIMER_PROF) |
| 196 | return (EINVAL); |
| 197 | itvp = uap->itv; |
| 198 | if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv, |
| 199 | sizeof(struct itimerval)))) |
| 200 | return (error); |
| 201 | if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval))) |
| 202 | return (error); |
| 203 | if (itvp == 0) |
| 204 | return (0); |
| 205 | if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) |
| 206 | return (EINVAL); |
| 207 | s = splclock(); |
| 208 | if (uap->which == ITIMER_REAL) { |
| 209 | untimeout(realitexpire, (caddr_t)p); |
| 210 | if (timerisset(&aitv.it_value)) { |
| 211 | timevaladd(&aitv.it_value, &time); |
| 212 | timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value)); |
| 213 | } |
| 214 | p->p_realtimer = aitv; |
| 215 | } else |
| 216 | u.u_timer[uap->which] = aitv; |
| 217 | splx(s); |
| 218 | return (0); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Real interval timer expired: |
| 223 | * send process whose timer expired an alarm signal. |
| 224 | * If time is not set up to reload, then just return. |
| 225 | * Else compute next time timer should go off which is > current time. |
| 226 | * This is where delay in processing this timeout causes multiple |
| 227 | * SIGALRM calls to be compressed into one. |
| 228 | */ |
| 229 | realitexpire(p) |
| 230 | register struct proc *p; |
| 231 | { |
| 232 | int s; |
| 233 | |
| 234 | psignal(p, SIGALRM); |
| 235 | if (!timerisset(&p->p_realtimer.it_interval)) { |
| 236 | timerclear(&p->p_realtimer.it_value); |
| 237 | return; |
| 238 | } |
| 239 | for (;;) { |
| 240 | s = splclock(); |
| 241 | timevaladd(&p->p_realtimer.it_value, |
| 242 | &p->p_realtimer.it_interval); |
| 243 | if (timercmp(&p->p_realtimer.it_value, &time, >)) { |
| 244 | timeout(realitexpire, (caddr_t)p, |
| 245 | hzto(&p->p_realtimer.it_value)); |
| 246 | splx(s); |
| 247 | return; |
| 248 | } |
| 249 | splx(s); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Check that a proposed value to load into the .it_value or |
| 255 | * .it_interval part of an interval timer is acceptable, and |
| 256 | * fix it to have at least minimal value (i.e. if it is less |
| 257 | * than the resolution of the clock, round it up.) |
| 258 | */ |
| 259 | itimerfix(tv) |
| 260 | struct timeval *tv; |
| 261 | { |
| 262 | |
| 263 | if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || |
| 264 | tv->tv_usec < 0 || tv->tv_usec >= 1000000) |
| 265 | return (EINVAL); |
| 266 | if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) |
| 267 | tv->tv_usec = tick; |
| 268 | return (0); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Decrement an interval timer by a specified number |
| 273 | * of microseconds, which must be less than a second, |
| 274 | * i.e. < 1000000. If the timer expires, then reload |
| 275 | * it. In this case, carry over (usec - old value) to |
| 276 | * reducint the value reloaded into the timer so that |
| 277 | * the timer does not drift. This routine assumes |
| 278 | * that it is called in a context where the timers |
| 279 | * on which it is operating cannot change in value. |
| 280 | */ |
| 281 | itimerdecr(itp, usec) |
| 282 | register struct itimerval *itp; |
| 283 | int usec; |
| 284 | { |
| 285 | |
| 286 | if (itp->it_value.tv_usec < usec) { |
| 287 | if (itp->it_value.tv_sec == 0) { |
| 288 | /* expired, and already in next interval */ |
| 289 | usec -= itp->it_value.tv_usec; |
| 290 | goto expire; |
| 291 | } |
| 292 | itp->it_value.tv_usec += 1000000; |
| 293 | itp->it_value.tv_sec--; |
| 294 | } |
| 295 | itp->it_value.tv_usec -= usec; |
| 296 | usec = 0; |
| 297 | if (timerisset(&itp->it_value)) |
| 298 | return (1); |
| 299 | /* expired, exactly at end of interval */ |
| 300 | expire: |
| 301 | if (timerisset(&itp->it_interval)) { |
| 302 | itp->it_value = itp->it_interval; |
| 303 | itp->it_value.tv_usec -= usec; |
| 304 | if (itp->it_value.tv_usec < 0) { |
| 305 | itp->it_value.tv_usec += 1000000; |
| 306 | itp->it_value.tv_sec--; |
| 307 | } |
| 308 | } else |
| 309 | itp->it_value.tv_usec = 0; /* sec is already 0 */ |
| 310 | return (0); |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Add and subtract routines for timevals. |
| 315 | * N.B.: subtract routine doesn't deal with |
| 316 | * results which are before the beginning, |
| 317 | * it just gets very confused in this case. |
| 318 | * Caveat emptor. |
| 319 | */ |
| 320 | timevaladd(t1, t2) |
| 321 | struct timeval *t1, *t2; |
| 322 | { |
| 323 | |
| 324 | t1->tv_sec += t2->tv_sec; |
| 325 | t1->tv_usec += t2->tv_usec; |
| 326 | timevalfix(t1); |
| 327 | } |
| 328 | |
| 329 | timevalsub(t1, t2) |
| 330 | struct timeval *t1, *t2; |
| 331 | { |
| 332 | |
| 333 | t1->tv_sec -= t2->tv_sec; |
| 334 | t1->tv_usec -= t2->tv_usec; |
| 335 | timevalfix(t1); |
| 336 | } |
| 337 | |
| 338 | timevalfix(t1) |
| 339 | struct timeval *t1; |
| 340 | { |
| 341 | |
| 342 | if (t1->tv_usec < 0) { |
| 343 | t1->tv_sec--; |
| 344 | t1->tv_usec += 1000000; |
| 345 | } |
| 346 | if (t1->tv_usec >= 1000000) { |
| 347 | t1->tv_sec++; |
| 348 | t1->tv_usec -= 1000000; |
| 349 | } |
| 350 | } |