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