Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
c34daa85 KB |
2 | * Copyright (c) 1982, 1986, 1989, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
da7c5cc6 | 4 | * |
dbf0c423 | 5 | * %sccs.include.redist.c% |
9cb30eb6 | 6 | * |
c34daa85 | 7 | * @(#)kern_time.c 8.1 (Berkeley) %G% |
da7c5cc6 | 8 | */ |
961945a8 | 9 | |
38a01dbe KB |
10 | #include <sys/param.h> |
11 | #include <sys/resourcevar.h> | |
12 | #include <sys/kernel.h> | |
13 | #include <sys/systm.h> | |
14 | #include <sys/proc.h> | |
15 | #include <sys/vnode.h> | |
b6f30e0a | 16 | |
fb1db32c | 17 | |
1edb1cf8 BJ |
18 | /* |
19 | * Time of day and interval timer support. | |
aa261505 BJ |
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. | |
1edb1cf8 BJ |
26 | */ |
27 | ||
9e97623a CT |
28 | struct gettimeofday_args { |
29 | struct timeval *tp; | |
30 | struct timezone *tzp; | |
31 | }; | |
fc4a5eac MK |
32 | /* ARGSUSED */ |
33 | gettimeofday(p, uap, retval) | |
34 | struct proc *p; | |
9e97623a | 35 | register struct gettimeofday_args *uap; |
fc4a5eac MK |
36 | int *retval; |
37 | { | |
b6f30e0a | 38 | struct timeval atv; |
fc4a5eac | 39 | int error = 0; |
4147b3f6 | 40 | |
2b6a7e0f KB |
41 | if (uap->tp) { |
42 | microtime(&atv); | |
fc4a5eac MK |
43 | if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp, |
44 | sizeof (atv))) | |
d9c2f47f | 45 | return (error); |
2b6a7e0f KB |
46 | } |
47 | if (uap->tzp) | |
fc4a5eac MK |
48 | error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, |
49 | sizeof (tz)); | |
d9c2f47f | 50 | return (error); |
4147b3f6 BJ |
51 | } |
52 | ||
9e97623a CT |
53 | struct settimeofday_args { |
54 | struct timeval *tv; | |
55 | struct timezone *tzp; | |
56 | }; | |
cd23cb29 | 57 | /* ARGSUSED */ |
fc4a5eac MK |
58 | settimeofday(p, uap, retval) |
59 | struct proc *p; | |
9e97623a | 60 | struct settimeofday_args *uap; |
fc4a5eac MK |
61 | int *retval; |
62 | { | |
c3a8186d | 63 | struct timeval atv, delta; |
b6f30e0a | 64 | struct timezone atz; |
fc4a5eac | 65 | int error, s; |
4147b3f6 | 66 | |
8429d022 | 67 | if (error = suser(p->p_ucred, &p->p_acflag)) |
d9c2f47f | 68 | return (error); |
c3a8186d CT |
69 | /* Verify all parameters before changing time. */ |
70 | if (uap->tv && | |
71 | (error = copyin((caddr_t)uap->tv, (caddr_t)&atv, sizeof(atv)))) | |
72 | return (error); | |
73 | if (uap->tzp && | |
74 | (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz)))) | |
75 | return (error); | |
2b6a7e0f | 76 | if (uap->tv) { |
9cb30eb6 | 77 | /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ |
c3a8186d CT |
78 | s = splclock(); |
79 | /* nb. delta.tv_usec may be < 0, but this is OK here */ | |
80 | delta.tv_sec = atv.tv_sec - time.tv_sec; | |
81 | delta.tv_usec = atv.tv_usec - time.tv_usec; | |
82 | time = atv; | |
83 | (void) splsoftclock(); | |
84 | timevaladd(&boottime, &delta); | |
85 | timevalfix(&boottime); | |
86 | timevaladd(&runtime, &delta); | |
87 | timevalfix(&runtime); | |
88 | LEASE_UPDATETIME(delta.tv_sec); | |
89 | splx(s); | |
9cb30eb6 | 90 | resettodr(); |
2b6a7e0f | 91 | } |
c3a8186d | 92 | if (uap->tzp) |
849cbd39 | 93 | tz = atz; |
c3a8186d | 94 | return (0); |
4147b3f6 BJ |
95 | } |
96 | ||
4ca0d0d6 MK |
97 | extern int tickadj; /* "standard" clock skew, us./tick */ |
98 | int tickdelta; /* current clock skew, us. per tick */ | |
99 | long timedelta; /* unapplied time correction, us. */ | |
100 | long bigadj = 1000000; /* use 10x skew above bigadj us. */ | |
99e47f6b | 101 | |
9e97623a CT |
102 | struct adjtime_args { |
103 | struct timeval *delta; | |
104 | struct timeval *olddelta; | |
105 | }; | |
fc4a5eac MK |
106 | /* ARGSUSED */ |
107 | adjtime(p, uap, retval) | |
108 | struct proc *p; | |
9e97623a | 109 | register struct adjtime_args *uap; |
fc4a5eac MK |
110 | int *retval; |
111 | { | |
83619c4d CT |
112 | struct timeval atv; |
113 | register long ndelta, ntickdelta, odelta; | |
fc4a5eac | 114 | int s, error; |
99e47f6b | 115 | |
8429d022 | 116 | if (error = suser(p->p_ucred, &p->p_acflag)) |
d9c2f47f | 117 | return (error); |
fc4a5eac | 118 | if (error = |
83619c4d | 119 | copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))) |
d9c2f47f | 120 | return (error); |
83619c4d CT |
121 | |
122 | /* | |
123 | * Compute the total correction and the rate at which to apply it. | |
124 | * Round the adjustment down to a whole multiple of the per-tick | |
125 | * delta, so that after some number of incremental changes in | |
126 | * hardclock(), tickdelta will become zero, lest the correction | |
127 | * overshoot and start taking us away from the desired final time. | |
128 | */ | |
4ca0d0d6 | 129 | ndelta = atv.tv_sec * 1000000 + atv.tv_usec; |
83619c4d CT |
130 | if (ndelta > bigadj) |
131 | ntickdelta = 10 * tickadj; | |
132 | else | |
133 | ntickdelta = tickadj; | |
134 | if (ndelta % ntickdelta) | |
135 | ndelta = ndelta / ntickdelta * ntickdelta; | |
4ca0d0d6 | 136 | |
83619c4d CT |
137 | /* |
138 | * To make hardclock()'s job easier, make the per-tick delta negative | |
139 | * if we want time to run slower; then hardclock can simply compute | |
140 | * tick + tickdelta, and subtract tickdelta from timedelta. | |
141 | */ | |
142 | if (ndelta < 0) | |
143 | ntickdelta = -ntickdelta; | |
8efc019f | 144 | s = splclock(); |
83619c4d | 145 | odelta = timedelta; |
4ca0d0d6 | 146 | timedelta = ndelta; |
83619c4d | 147 | tickdelta = ntickdelta; |
8efc019f | 148 | splx(s); |
4ca0d0d6 | 149 | |
83619c4d CT |
150 | if (uap->olddelta) { |
151 | atv.tv_sec = odelta / 1000000; | |
152 | atv.tv_usec = odelta % 1000000; | |
153 | (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta, | |
154 | sizeof(struct timeval)); | |
155 | } | |
d9c2f47f | 156 | return (0); |
99e47f6b MK |
157 | } |
158 | ||
aa261505 BJ |
159 | /* |
160 | * Get value of an interval timer. The process virtual and | |
8429d022 | 161 | * profiling virtual time timers are kept in the p_stats area, since |
aa261505 BJ |
162 | * they can be swapped out. These are kept internally in the |
163 | * way they are specified externally: in time until they expire. | |
164 | * | |
165 | * The real time interval timer is kept in the process table slot | |
166 | * for the process, and its value (it_value) is kept as an | |
167 | * absolute time rather than as a delta, so that it is easy to keep | |
168 | * periodic real-time signals from drifting. | |
169 | * | |
170 | * Virtual time timers are processed in the hardclock() routine of | |
171 | * kern_clock.c. The real time timer is processed by a timeout | |
172 | * routine, called from the softclock() routine. Since a callout | |
173 | * may be delayed in real time due to interrupt processing in the system, | |
174 | * it is possible for the real time timeout routine (realitexpire, given below), | |
175 | * to be delayed in real time past when it is supposed to occur. It | |
176 | * does not suffice, therefore, to reload the real timer .it_value from the | |
177 | * real time timers .it_interval. Rather, we compute the next time in | |
178 | * absolute time the timer should go off. | |
179 | */ | |
9e97623a CT |
180 | struct getitimer_args { |
181 | u_int which; | |
182 | struct itimerval *itv; | |
183 | }; | |
fc4a5eac MK |
184 | /* ARGSUSED */ |
185 | getitimer(p, uap, retval) | |
186 | struct proc *p; | |
9e97623a | 187 | register struct getitimer_args *uap; |
fc4a5eac MK |
188 | int *retval; |
189 | { | |
d01b68d6 | 190 | struct itimerval aitv; |
b6f30e0a | 191 | int s; |
aac7ea5b | 192 | |
fc4a5eac | 193 | if (uap->which > ITIMER_PROF) |
d9c2f47f | 194 | return (EINVAL); |
fa5e5ab4 | 195 | s = splclock(); |
d01b68d6 | 196 | if (uap->which == ITIMER_REAL) { |
aa261505 BJ |
197 | /* |
198 | * Convert from absoulte to relative time in .it_value | |
199 | * part of real time timer. If time for real time timer | |
200 | * has passed return 0, else return difference between | |
201 | * current time and time for the timer to go off. | |
202 | */ | |
fc4a5eac | 203 | aitv = p->p_realtimer; |
d01b68d6 BJ |
204 | if (timerisset(&aitv.it_value)) |
205 | if (timercmp(&aitv.it_value, &time, <)) | |
206 | timerclear(&aitv.it_value); | |
207 | else | |
c3a8186d CT |
208 | timevalsub(&aitv.it_value, |
209 | (struct timeval *)&time); | |
d01b68d6 | 210 | } else |
8429d022 | 211 | aitv = p->p_stats->p_timer[uap->which]; |
d01b68d6 | 212 | splx(s); |
d9c2f47f | 213 | return (copyout((caddr_t)&aitv, (caddr_t)uap->itv, |
fc4a5eac | 214 | sizeof (struct itimerval))); |
aac7ea5b BJ |
215 | } |
216 | ||
9e97623a CT |
217 | struct setitimer_args { |
218 | u_int which; | |
219 | struct itimerval *itv, *oitv; | |
220 | }; | |
fc4a5eac MK |
221 | /* ARGSUSED */ |
222 | setitimer(p, uap, retval) | |
223 | struct proc *p; | |
9e97623a | 224 | register struct setitimer_args *uap; |
fc4a5eac MK |
225 | int *retval; |
226 | { | |
c4bbb24f KB |
227 | struct itimerval aitv; |
228 | register struct itimerval *itvp; | |
fc4a5eac | 229 | int s, error; |
aac7ea5b | 230 | |
fc4a5eac | 231 | if (uap->which > ITIMER_PROF) |
d9c2f47f | 232 | return (EINVAL); |
c4bbb24f | 233 | itvp = uap->itv; |
fc4a5eac | 234 | if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv, |
c4bbb24f | 235 | sizeof(struct itimerval)))) |
d9c2f47f | 236 | return (error); |
fc4a5eac | 237 | if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval))) |
d9c2f47f | 238 | return (error); |
c4bbb24f | 239 | if (itvp == 0) |
fc4a5eac MK |
240 | return (0); |
241 | if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) | |
d9c2f47f | 242 | return (EINVAL); |
fa5e5ab4 | 243 | s = splclock(); |
d01b68d6 | 244 | if (uap->which == ITIMER_REAL) { |
b32450f4 | 245 | untimeout(realitexpire, (caddr_t)p); |
d01b68d6 | 246 | if (timerisset(&aitv.it_value)) { |
c3a8186d | 247 | timevaladd(&aitv.it_value, (struct timeval *)&time); |
b32450f4 | 248 | timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value)); |
d01b68d6 BJ |
249 | } |
250 | p->p_realtimer = aitv; | |
251 | } else | |
8429d022 | 252 | p->p_stats->p_timer[uap->which] = aitv; |
b6f30e0a | 253 | splx(s); |
d9c2f47f | 254 | return (0); |
b6f30e0a BJ |
255 | } |
256 | ||
aa261505 BJ |
257 | /* |
258 | * Real interval timer expired: | |
259 | * send process whose timer expired an alarm signal. | |
260 | * If time is not set up to reload, then just return. | |
261 | * Else compute next time timer should go off which is > current time. | |
262 | * This is where delay in processing this timeout causes multiple | |
263 | * SIGALRM calls to be compressed into one. | |
264 | */ | |
c3a8186d CT |
265 | void |
266 | realitexpire(arg) | |
267 | void *arg; | |
d01b68d6 | 268 | { |
c3a8186d | 269 | register struct proc *p; |
d01b68d6 BJ |
270 | int s; |
271 | ||
c3a8186d | 272 | p = (struct proc *)arg; |
d01b68d6 BJ |
273 | psignal(p, SIGALRM); |
274 | if (!timerisset(&p->p_realtimer.it_interval)) { | |
275 | timerclear(&p->p_realtimer.it_value); | |
276 | return; | |
277 | } | |
278 | for (;;) { | |
fa5e5ab4 | 279 | s = splclock(); |
d01b68d6 BJ |
280 | timevaladd(&p->p_realtimer.it_value, |
281 | &p->p_realtimer.it_interval); | |
282 | if (timercmp(&p->p_realtimer.it_value, &time, >)) { | |
b32450f4 BJ |
283 | timeout(realitexpire, (caddr_t)p, |
284 | hzto(&p->p_realtimer.it_value)); | |
d01b68d6 BJ |
285 | splx(s); |
286 | return; | |
287 | } | |
288 | splx(s); | |
289 | } | |
290 | } | |
291 | ||
aa261505 BJ |
292 | /* |
293 | * Check that a proposed value to load into the .it_value or | |
294 | * .it_interval part of an interval timer is acceptable, and | |
295 | * fix it to have at least minimal value (i.e. if it is less | |
296 | * than the resolution of the clock, round it up.) | |
297 | */ | |
1edb1cf8 BJ |
298 | itimerfix(tv) |
299 | struct timeval *tv; | |
b6f30e0a | 300 | { |
b6f30e0a | 301 | |
d01b68d6 BJ |
302 | if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || |
303 | tv->tv_usec < 0 || tv->tv_usec >= 1000000) | |
1edb1cf8 | 304 | return (EINVAL); |
c45fcba6 | 305 | if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick) |
1edb1cf8 BJ |
306 | tv->tv_usec = tick; |
307 | return (0); | |
b6f30e0a BJ |
308 | } |
309 | ||
aa261505 BJ |
310 | /* |
311 | * Decrement an interval timer by a specified number | |
312 | * of microseconds, which must be less than a second, | |
313 | * i.e. < 1000000. If the timer expires, then reload | |
314 | * it. In this case, carry over (usec - old value) to | |
c3a8186d | 315 | * reduce the value reloaded into the timer so that |
aa261505 BJ |
316 | * the timer does not drift. This routine assumes |
317 | * that it is called in a context where the timers | |
318 | * on which it is operating cannot change in value. | |
319 | */ | |
b6f30e0a BJ |
320 | itimerdecr(itp, usec) |
321 | register struct itimerval *itp; | |
322 | int usec; | |
323 | { | |
324 | ||
1edb1cf8 BJ |
325 | if (itp->it_value.tv_usec < usec) { |
326 | if (itp->it_value.tv_sec == 0) { | |
aa261505 | 327 | /* expired, and already in next interval */ |
1edb1cf8 | 328 | usec -= itp->it_value.tv_usec; |
b6f30e0a | 329 | goto expire; |
1edb1cf8 BJ |
330 | } |
331 | itp->it_value.tv_usec += 1000000; | |
332 | itp->it_value.tv_sec--; | |
aac7ea5b | 333 | } |
1edb1cf8 BJ |
334 | itp->it_value.tv_usec -= usec; |
335 | usec = 0; | |
336 | if (timerisset(&itp->it_value)) | |
b6f30e0a | 337 | return (1); |
aa261505 | 338 | /* expired, exactly at end of interval */ |
b6f30e0a | 339 | expire: |
1edb1cf8 BJ |
340 | if (timerisset(&itp->it_interval)) { |
341 | itp->it_value = itp->it_interval; | |
342 | itp->it_value.tv_usec -= usec; | |
343 | if (itp->it_value.tv_usec < 0) { | |
344 | itp->it_value.tv_usec += 1000000; | |
345 | itp->it_value.tv_sec--; | |
346 | } | |
347 | } else | |
aa261505 | 348 | itp->it_value.tv_usec = 0; /* sec is already 0 */ |
b6f30e0a | 349 | return (0); |
aac7ea5b BJ |
350 | } |
351 | ||
aa261505 BJ |
352 | /* |
353 | * Add and subtract routines for timevals. | |
354 | * N.B.: subtract routine doesn't deal with | |
355 | * results which are before the beginning, | |
356 | * it just gets very confused in this case. | |
357 | * Caveat emptor. | |
358 | */ | |
359 | timevaladd(t1, t2) | |
360 | struct timeval *t1, *t2; | |
361 | { | |
362 | ||
363 | t1->tv_sec += t2->tv_sec; | |
364 | t1->tv_usec += t2->tv_usec; | |
365 | timevalfix(t1); | |
366 | } | |
367 | ||
368 | timevalsub(t1, t2) | |
369 | struct timeval *t1, *t2; | |
370 | { | |
371 | ||
372 | t1->tv_sec -= t2->tv_sec; | |
373 | t1->tv_usec -= t2->tv_usec; | |
374 | timevalfix(t1); | |
375 | } | |
376 | ||
377 | timevalfix(t1) | |
378 | struct timeval *t1; | |
379 | { | |
380 | ||
381 | if (t1->tv_usec < 0) { | |
382 | t1->tv_sec--; | |
383 | t1->tv_usec += 1000000; | |
384 | } | |
385 | if (t1->tv_usec >= 1000000) { | |
386 | t1->tv_sec++; | |
387 | t1->tv_usec -= 1000000; | |
388 | } | |
389 | } |