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