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