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