kern_time.c revision 1.1.1.2 1 /*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
34 */
35
36 #include <sys/param.h>
37 #include <sys/resourcevar.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/vnode.h>
42
43 #include <machine/cpu.h>
44
45 /*
46 * Time of day and interval timer support.
47 *
48 * These routines provide the kernel entry points to get and set
49 * the time-of-day and per-process interval timers. Subroutines
50 * here provide support for adding and subtracting timeval structures
51 * and decrementing interval timers, optionally reloading the interval
52 * timers when they expire.
53 */
54
55 struct gettimeofday_args {
56 struct timeval *tp;
57 struct timezone *tzp;
58 };
59 /* ARGSUSED */
60 gettimeofday(p, uap, retval)
61 struct proc *p;
62 register struct gettimeofday_args *uap;
63 int *retval;
64 {
65 struct timeval atv;
66 int error = 0;
67
68 if (uap->tp) {
69 microtime(&atv);
70 if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
71 sizeof (atv)))
72 return (error);
73 }
74 if (uap->tzp)
75 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
76 sizeof (tz));
77 return (error);
78 }
79
80 struct settimeofday_args {
81 struct timeval *tv;
82 struct timezone *tzp;
83 };
84 /* ARGSUSED */
85 settimeofday(p, uap, retval)
86 struct proc *p;
87 struct settimeofday_args *uap;
88 int *retval;
89 {
90 struct timeval atv, delta;
91 struct timezone atz;
92 int error, s;
93
94 if (error = suser(p->p_ucred, &p->p_acflag))
95 return (error);
96 /* Verify all parameters before changing time. */
97 if (uap->tv &&
98 (error = copyin((caddr_t)uap->tv, (caddr_t)&atv, sizeof(atv))))
99 return (error);
100 if (uap->tzp &&
101 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
102 return (error);
103 if (uap->tv) {
104 /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
105 s = splclock();
106 /* nb. delta.tv_usec may be < 0, but this is OK here */
107 delta.tv_sec = atv.tv_sec - time.tv_sec;
108 delta.tv_usec = atv.tv_usec - time.tv_usec;
109 time = atv;
110 (void) splsoftclock();
111 timevaladd(&boottime, &delta);
112 timevalfix(&boottime);
113 timevaladd(&runtime, &delta);
114 timevalfix(&runtime);
115 LEASE_UPDATETIME(delta.tv_sec);
116 splx(s);
117 resettodr();
118 }
119 if (uap->tzp)
120 tz = atz;
121 return (0);
122 }
123
124 extern int tickadj; /* "standard" clock skew, us./tick */
125 int tickdelta; /* current clock skew, us. per tick */
126 long timedelta; /* unapplied time correction, us. */
127 long bigadj = 1000000; /* use 10x skew above bigadj us. */
128
129 struct adjtime_args {
130 struct timeval *delta;
131 struct timeval *olddelta;
132 };
133 /* ARGSUSED */
134 adjtime(p, uap, retval)
135 struct proc *p;
136 register struct adjtime_args *uap;
137 int *retval;
138 {
139 struct timeval atv;
140 register long ndelta, ntickdelta, odelta;
141 int s, error;
142
143 if (error = suser(p->p_ucred, &p->p_acflag))
144 return (error);
145 if (error =
146 copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval)))
147 return (error);
148
149 /*
150 * Compute the total correction and the rate at which to apply it.
151 * Round the adjustment down to a whole multiple of the per-tick
152 * delta, so that after some number of incremental changes in
153 * hardclock(), tickdelta will become zero, lest the correction
154 * overshoot and start taking us away from the desired final time.
155 */
156 ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
157 if (ndelta > bigadj)
158 ntickdelta = 10 * tickadj;
159 else
160 ntickdelta = tickadj;
161 if (ndelta % ntickdelta)
162 ndelta = ndelta / ntickdelta * ntickdelta;
163
164 /*
165 * To make hardclock()'s job easier, make the per-tick delta negative
166 * if we want time to run slower; then hardclock can simply compute
167 * tick + tickdelta, and subtract tickdelta from timedelta.
168 */
169 if (ndelta < 0)
170 ntickdelta = -ntickdelta;
171 s = splclock();
172 odelta = timedelta;
173 timedelta = ndelta;
174 tickdelta = ntickdelta;
175 splx(s);
176
177 if (uap->olddelta) {
178 atv.tv_sec = odelta / 1000000;
179 atv.tv_usec = odelta % 1000000;
180 (void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
181 sizeof(struct timeval));
182 }
183 return (0);
184 }
185
186 /*
187 * Get value of an interval timer. The process virtual and
188 * profiling virtual time timers are kept in the p_stats area, since
189 * they can be swapped out. These are kept internally in the
190 * way they are specified externally: in time until they expire.
191 *
192 * The real time interval timer is kept in the process table slot
193 * for the process, and its value (it_value) is kept as an
194 * absolute time rather than as a delta, so that it is easy to keep
195 * periodic real-time signals from drifting.
196 *
197 * Virtual time timers are processed in the hardclock() routine of
198 * kern_clock.c. The real time timer is processed by a timeout
199 * routine, called from the softclock() routine. Since a callout
200 * may be delayed in real time due to interrupt processing in the system,
201 * it is possible for the real time timeout routine (realitexpire, given below),
202 * to be delayed in real time past when it is supposed to occur. It
203 * does not suffice, therefore, to reload the real timer .it_value from the
204 * real time timers .it_interval. Rather, we compute the next time in
205 * absolute time the timer should go off.
206 */
207 struct getitimer_args {
208 u_int which;
209 struct itimerval *itv;
210 };
211 /* ARGSUSED */
212 getitimer(p, uap, retval)
213 struct proc *p;
214 register struct getitimer_args *uap;
215 int *retval;
216 {
217 struct itimerval aitv;
218 int s;
219
220 if (uap->which > ITIMER_PROF)
221 return (EINVAL);
222 s = splclock();
223 if (uap->which == ITIMER_REAL) {
224 /*
225 * Convert from absoulte to relative time in .it_value
226 * part of real time timer. If time for real time timer
227 * has passed return 0, else return difference between
228 * current time and time for the timer to go off.
229 */
230 aitv = p->p_realtimer;
231 if (timerisset(&aitv.it_value))
232 if (timercmp(&aitv.it_value, &time, <))
233 timerclear(&aitv.it_value);
234 else
235 timevalsub(&aitv.it_value,
236 (struct timeval *)&time);
237 } else
238 aitv = p->p_stats->p_timer[uap->which];
239 splx(s);
240 return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
241 sizeof (struct itimerval)));
242 }
243
244 struct setitimer_args {
245 u_int which;
246 struct itimerval *itv, *oitv;
247 };
248 /* ARGSUSED */
249 setitimer(p, uap, retval)
250 struct proc *p;
251 register struct setitimer_args *uap;
252 int *retval;
253 {
254 struct itimerval aitv;
255 register struct itimerval *itvp;
256 int s, error;
257
258 if (uap->which > ITIMER_PROF)
259 return (EINVAL);
260 itvp = uap->itv;
261 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
262 sizeof(struct itimerval))))
263 return (error);
264 if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
265 return (error);
266 if (itvp == 0)
267 return (0);
268 if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
269 return (EINVAL);
270 s = splclock();
271 if (uap->which == ITIMER_REAL) {
272 untimeout(realitexpire, (caddr_t)p);
273 if (timerisset(&aitv.it_value)) {
274 timevaladd(&aitv.it_value, (struct timeval *)&time);
275 timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
276 }
277 p->p_realtimer = aitv;
278 } else
279 p->p_stats->p_timer[uap->which] = aitv;
280 splx(s);
281 return (0);
282 }
283
284 /*
285 * Real interval timer expired:
286 * send process whose timer expired an alarm signal.
287 * If time is not set up to reload, then just return.
288 * Else compute next time timer should go off which is > current time.
289 * This is where delay in processing this timeout causes multiple
290 * SIGALRM calls to be compressed into one.
291 */
292 void
293 realitexpire(arg)
294 void *arg;
295 {
296 register struct proc *p;
297 int s;
298
299 p = (struct proc *)arg;
300 psignal(p, SIGALRM);
301 if (!timerisset(&p->p_realtimer.it_interval)) {
302 timerclear(&p->p_realtimer.it_value);
303 return;
304 }
305 for (;;) {
306 s = splclock();
307 timevaladd(&p->p_realtimer.it_value,
308 &p->p_realtimer.it_interval);
309 if (timercmp(&p->p_realtimer.it_value, &time, >)) {
310 timeout(realitexpire, (caddr_t)p,
311 hzto(&p->p_realtimer.it_value));
312 splx(s);
313 return;
314 }
315 splx(s);
316 }
317 }
318
319 /*
320 * Check that a proposed value to load into the .it_value or
321 * .it_interval part of an interval timer is acceptable, and
322 * fix it to have at least minimal value (i.e. if it is less
323 * than the resolution of the clock, round it up.)
324 */
325 itimerfix(tv)
326 struct timeval *tv;
327 {
328
329 if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
330 tv->tv_usec < 0 || tv->tv_usec >= 1000000)
331 return (EINVAL);
332 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
333 tv->tv_usec = tick;
334 return (0);
335 }
336
337 /*
338 * Decrement an interval timer by a specified number
339 * of microseconds, which must be less than a second,
340 * i.e. < 1000000. If the timer expires, then reload
341 * it. In this case, carry over (usec - old value) to
342 * reduce the value reloaded into the timer so that
343 * the timer does not drift. This routine assumes
344 * that it is called in a context where the timers
345 * on which it is operating cannot change in value.
346 */
347 itimerdecr(itp, usec)
348 register struct itimerval *itp;
349 int usec;
350 {
351
352 if (itp->it_value.tv_usec < usec) {
353 if (itp->it_value.tv_sec == 0) {
354 /* expired, and already in next interval */
355 usec -= itp->it_value.tv_usec;
356 goto expire;
357 }
358 itp->it_value.tv_usec += 1000000;
359 itp->it_value.tv_sec--;
360 }
361 itp->it_value.tv_usec -= usec;
362 usec = 0;
363 if (timerisset(&itp->it_value))
364 return (1);
365 /* expired, exactly at end of interval */
366 expire:
367 if (timerisset(&itp->it_interval)) {
368 itp->it_value = itp->it_interval;
369 itp->it_value.tv_usec -= usec;
370 if (itp->it_value.tv_usec < 0) {
371 itp->it_value.tv_usec += 1000000;
372 itp->it_value.tv_sec--;
373 }
374 } else
375 itp->it_value.tv_usec = 0; /* sec is already 0 */
376 return (0);
377 }
378
379 /*
380 * Add and subtract routines for timevals.
381 * N.B.: subtract routine doesn't deal with
382 * results which are before the beginning,
383 * it just gets very confused in this case.
384 * Caveat emptor.
385 */
386 timevaladd(t1, t2)
387 struct timeval *t1, *t2;
388 {
389
390 t1->tv_sec += t2->tv_sec;
391 t1->tv_usec += t2->tv_usec;
392 timevalfix(t1);
393 }
394
395 timevalsub(t1, t2)
396 struct timeval *t1, *t2;
397 {
398
399 t1->tv_sec -= t2->tv_sec;
400 t1->tv_usec -= t2->tv_usec;
401 timevalfix(t1);
402 }
403
404 timevalfix(t1)
405 struct timeval *t1;
406 {
407
408 if (t1->tv_usec < 0) {
409 t1->tv_sec--;
410 t1->tv_usec += 1000000;
411 }
412 if (t1->tv_usec >= 1000000) {
413 t1->tv_sec++;
414 t1->tv_usec -= 1000000;
415 }
416 }
417