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