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