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