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