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