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