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