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