xref: /original-bsd/sys/kern/kern_time.c (revision 7eb91141)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)kern_time.c	7.16 (Berkeley) 01/14/92
8  */
9 
10 #include "param.h"
11 #include "resourcevar.h"
12 #include "kernel.h"
13 #include "proc.h"
14 #include "vnode.h"
15 
16 #include "machine/cpu.h"
17 
18 /*
19  * Time of day and interval timer support.
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.
26  */
27 
28 /* ARGSUSED */
29 gettimeofday(p, uap, retval)
30 	struct proc *p;
31 	register struct args {
32 		struct	timeval *tp;
33 		struct	timezone *tzp;
34 	} *uap;
35 	int *retval;
36 {
37 	struct timeval atv;
38 	int error = 0;
39 
40 	if (uap->tp) {
41 		microtime(&atv);
42 		if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
43 		    sizeof (atv)))
44 			return (error);
45 	}
46 	if (uap->tzp)
47 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
48 		    sizeof (tz));
49 	return (error);
50 }
51 
52 /* ARGSUSED */
53 settimeofday(p, uap, retval)
54 	struct proc *p;
55 	struct args {
56 		struct	timeval *tv;
57 		struct	timezone *tzp;
58 	} *uap;
59 	int *retval;
60 {
61 	struct timeval atv;
62 	struct timezone atz;
63 	int error, s;
64 
65 	if (error = suser(p->p_ucred, &p->p_acflag))
66 		return (error);
67 	if (uap->tv) {
68 		if (error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
69 		    sizeof (struct timeval)))
70 			return (error);
71 		/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
72 		boottime.tv_sec += atv.tv_sec - time.tv_sec;
73 		LEASE_UPDATETIME(atv.tv_sec - time.tv_sec);
74 		s = splhigh(); time = atv; splx(s);
75 		resettodr();
76 	}
77 	if (uap->tzp && (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz,
78 	    sizeof (atz))) == 0)
79 		tz = atz;
80 	return (error);
81 }
82 
83 extern	int tickadj;			/* "standard" clock skew, us./tick */
84 int	tickdelta;			/* current clock skew, us. per tick */
85 long	timedelta;			/* unapplied time correction, us. */
86 long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
87 
88 /* ARGSUSED */
89 adjtime(p, uap, retval)
90 	struct proc *p;
91 	register struct args {
92 		struct timeval *delta;
93 		struct timeval *olddelta;
94 	} *uap;
95 	int *retval;
96 {
97 	struct timeval atv, oatv;
98 	register long ndelta;
99 	int s, error;
100 
101 	if (error = suser(p->p_ucred, &p->p_acflag))
102 		return (error);
103 	if (error =
104 	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval)))
105 		return (error);
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 
115 	s = splclock();
116 	if (uap->olddelta) {
117 		oatv.tv_sec = timedelta / 1000000;
118 		oatv.tv_usec = timedelta % 1000000;
119 	}
120 	timedelta = ndelta;
121 	splx(s);
122 
123 	if (uap->olddelta)
124 		(void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
125 			sizeof (struct timeval));
126 	return (0);
127 }
128 
129 /*
130  * Get value of an interval timer.  The process virtual and
131  * profiling virtual time timers are kept in the p_stats 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  */
150 /* ARGSUSED */
151 getitimer(p, uap, retval)
152 	struct proc *p;
153 	register struct args {
154 		u_int	which;
155 		struct	itimerval *itv;
156 	} *uap;
157 	int *retval;
158 {
159 	struct itimerval aitv;
160 	int s;
161 
162 	if (uap->which > ITIMER_PROF)
163 		return (EINVAL);
164 	s = splclock();
165 	if (uap->which == ITIMER_REAL) {
166 		/*
167 		 * Convert from absoulte to relative time in .it_value
168 		 * part of real time timer.  If time for real time timer
169 		 * has passed return 0, else return difference between
170 		 * current time and time for the timer to go off.
171 		 */
172 		aitv = p->p_realtimer;
173 		if (timerisset(&aitv.it_value))
174 			if (timercmp(&aitv.it_value, &time, <))
175 				timerclear(&aitv.it_value);
176 			else
177 				timevalsub(&aitv.it_value, &time);
178 	} else
179 		aitv = p->p_stats->p_timer[uap->which];
180 	splx(s);
181 	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
182 	    sizeof (struct itimerval)));
183 }
184 
185 /* ARGSUSED */
186 setitimer(p, uap, retval)
187 	struct proc *p;
188 	register struct args {
189 		u_int	which;
190 		struct	itimerval *itv, *oitv;
191 	} *uap;
192 	int *retval;
193 {
194 	struct itimerval aitv;
195 	register struct itimerval *itvp;
196 	int s, error;
197 
198 	if (uap->which > ITIMER_PROF)
199 		return (EINVAL);
200 	itvp = uap->itv;
201 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
202 	    sizeof(struct itimerval))))
203 		return (error);
204 	if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
205 		return (error);
206 	if (itvp == 0)
207 		return (0);
208 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
209 		return (EINVAL);
210 	s = splclock();
211 	if (uap->which == ITIMER_REAL) {
212 		untimeout(realitexpire, (caddr_t)p);
213 		if (timerisset(&aitv.it_value)) {
214 			timevaladd(&aitv.it_value, &time);
215 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
216 		}
217 		p->p_realtimer = aitv;
218 	} else
219 		p->p_stats->p_timer[uap->which] = aitv;
220 	splx(s);
221 	return (0);
222 }
223 
224 /*
225  * Real interval timer expired:
226  * send process whose timer expired an alarm signal.
227  * If time is not set up to reload, then just return.
228  * Else compute next time timer should go off which is > current time.
229  * This is where delay in processing this timeout causes multiple
230  * SIGALRM calls to be compressed into one.
231  */
232 realitexpire(p)
233 	register struct proc *p;
234 {
235 	int s;
236 
237 	psignal(p, SIGALRM);
238 	if (!timerisset(&p->p_realtimer.it_interval)) {
239 		timerclear(&p->p_realtimer.it_value);
240 		return;
241 	}
242 	for (;;) {
243 		s = splclock();
244 		timevaladd(&p->p_realtimer.it_value,
245 		    &p->p_realtimer.it_interval);
246 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
247 			timeout(realitexpire, (caddr_t)p,
248 			    hzto(&p->p_realtimer.it_value));
249 			splx(s);
250 			return;
251 		}
252 		splx(s);
253 	}
254 }
255 
256 /*
257  * Check that a proposed value to load into the .it_value or
258  * .it_interval part of an interval timer is acceptable, and
259  * fix it to have at least minimal value (i.e. if it is less
260  * than the resolution of the clock, round it up.)
261  */
262 itimerfix(tv)
263 	struct timeval *tv;
264 {
265 
266 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
267 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
268 		return (EINVAL);
269 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
270 		tv->tv_usec = tick;
271 	return (0);
272 }
273 
274 /*
275  * Decrement an interval timer by a specified number
276  * of microseconds, which must be less than a second,
277  * i.e. < 1000000.  If the timer expires, then reload
278  * it.  In this case, carry over (usec - old value) to
279  * reducint the value reloaded into the timer so that
280  * the timer does not drift.  This routine assumes
281  * that it is called in a context where the timers
282  * on which it is operating cannot change in value.
283  */
284 itimerdecr(itp, usec)
285 	register struct itimerval *itp;
286 	int usec;
287 {
288 
289 	if (itp->it_value.tv_usec < usec) {
290 		if (itp->it_value.tv_sec == 0) {
291 			/* expired, and already in next interval */
292 			usec -= itp->it_value.tv_usec;
293 			goto expire;
294 		}
295 		itp->it_value.tv_usec += 1000000;
296 		itp->it_value.tv_sec--;
297 	}
298 	itp->it_value.tv_usec -= usec;
299 	usec = 0;
300 	if (timerisset(&itp->it_value))
301 		return (1);
302 	/* expired, exactly at end of interval */
303 expire:
304 	if (timerisset(&itp->it_interval)) {
305 		itp->it_value = itp->it_interval;
306 		itp->it_value.tv_usec -= usec;
307 		if (itp->it_value.tv_usec < 0) {
308 			itp->it_value.tv_usec += 1000000;
309 			itp->it_value.tv_sec--;
310 		}
311 	} else
312 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
313 	return (0);
314 }
315 
316 /*
317  * Add and subtract routines for timevals.
318  * N.B.: subtract routine doesn't deal with
319  * results which are before the beginning,
320  * it just gets very confused in this case.
321  * Caveat emptor.
322  */
323 timevaladd(t1, t2)
324 	struct timeval *t1, *t2;
325 {
326 
327 	t1->tv_sec += t2->tv_sec;
328 	t1->tv_usec += t2->tv_usec;
329 	timevalfix(t1);
330 }
331 
332 timevalsub(t1, t2)
333 	struct timeval *t1, *t2;
334 {
335 
336 	t1->tv_sec -= t2->tv_sec;
337 	t1->tv_usec -= t2->tv_usec;
338 	timevalfix(t1);
339 }
340 
341 timevalfix(t1)
342 	struct timeval *t1;
343 {
344 
345 	if (t1->tv_usec < 0) {
346 		t1->tv_sec--;
347 		t1->tv_usec += 1000000;
348 	}
349 	if (t1->tv_usec >= 1000000) {
350 		t1->tv_sec++;
351 		t1->tv_usec -= 1000000;
352 	}
353 }
354