kern_time.c revision 1.17 1 1.17 christos /* $NetBSD: kern_time.c,v 1.17 1996/02/04 02:16:26 christos Exp $ */
2 1.9 cgd
3 1.1 cgd /*
4 1.8 cgd * Copyright (c) 1982, 1986, 1989, 1993
5 1.8 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.9 cgd * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
36 1.1 cgd */
37 1.1 cgd
38 1.5 mycroft #include <sys/param.h>
39 1.5 mycroft #include <sys/resourcevar.h>
40 1.5 mycroft #include <sys/kernel.h>
41 1.8 cgd #include <sys/systm.h>
42 1.5 mycroft #include <sys/proc.h>
43 1.8 cgd #include <sys/vnode.h>
44 1.17 christos #include <sys/signalvar.h>
45 1.1 cgd
46 1.11 cgd #include <sys/mount.h>
47 1.11 cgd #include <sys/syscallargs.h>
48 1.11 cgd
49 1.17 christos #include <kern/kern_extern.h>
50 1.17 christos
51 1.5 mycroft #include <machine/cpu.h>
52 1.1 cgd
53 1.1 cgd /*
54 1.1 cgd * Time of day and interval timer support.
55 1.1 cgd *
56 1.1 cgd * These routines provide the kernel entry points to get and set
57 1.1 cgd * the time-of-day and per-process interval timers. Subroutines
58 1.1 cgd * here provide support for adding and subtracting timeval structures
59 1.1 cgd * and decrementing interval timers, optionally reloading the interval
60 1.1 cgd * timers when they expire.
61 1.1 cgd */
62 1.1 cgd
63 1.1 cgd /* ARGSUSED */
64 1.3 andrew int
65 1.16 mycroft sys_gettimeofday(p, v, retval)
66 1.1 cgd struct proc *p;
67 1.15 thorpej void *v;
68 1.15 thorpej register_t *retval;
69 1.15 thorpej {
70 1.16 mycroft register struct sys_gettimeofday_args /* {
71 1.11 cgd syscallarg(struct timeval *) tp;
72 1.11 cgd syscallarg(struct timezone *) tzp;
73 1.15 thorpej } */ *uap = v;
74 1.1 cgd struct timeval atv;
75 1.1 cgd int error = 0;
76 1.1 cgd
77 1.11 cgd if (SCARG(uap, tp)) {
78 1.1 cgd microtime(&atv);
79 1.17 christos error = copyout((caddr_t)&atv, (caddr_t)SCARG(uap, tp),
80 1.17 christos sizeof (atv));
81 1.17 christos if (error)
82 1.1 cgd return (error);
83 1.1 cgd }
84 1.11 cgd if (SCARG(uap, tzp))
85 1.11 cgd error = copyout((caddr_t)&tz, (caddr_t)SCARG(uap, tzp),
86 1.1 cgd sizeof (tz));
87 1.1 cgd return (error);
88 1.1 cgd }
89 1.1 cgd
90 1.1 cgd /* ARGSUSED */
91 1.3 andrew int
92 1.16 mycroft sys_settimeofday(p, v, retval)
93 1.1 cgd struct proc *p;
94 1.15 thorpej void *v;
95 1.15 thorpej register_t *retval;
96 1.15 thorpej {
97 1.16 mycroft struct sys_settimeofday_args /* {
98 1.11 cgd syscallarg(struct timeval *) tv;
99 1.11 cgd syscallarg(struct timezone *) tzp;
100 1.15 thorpej } */ *uap = v;
101 1.8 cgd struct timeval atv, delta;
102 1.1 cgd struct timezone atz;
103 1.1 cgd int error, s;
104 1.1 cgd
105 1.17 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
106 1.1 cgd return (error);
107 1.8 cgd /* Verify all parameters before changing time. */
108 1.11 cgd if (SCARG(uap, tv) && (error = copyin((caddr_t)SCARG(uap, tv),
109 1.11 cgd (caddr_t)&atv, sizeof(atv))))
110 1.8 cgd return (error);
111 1.11 cgd if (SCARG(uap, tzp) && (error = copyin((caddr_t)SCARG(uap, tzp),
112 1.11 cgd (caddr_t)&atz, sizeof(atz))))
113 1.8 cgd return (error);
114 1.11 cgd if (SCARG(uap, tv)) {
115 1.1 cgd /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
116 1.8 cgd s = splclock();
117 1.14 mycroft timersub(&atv, &time, &delta);
118 1.8 cgd time = atv;
119 1.8 cgd (void) splsoftclock();
120 1.14 mycroft timeradd(&boottime, &delta, &boottime);
121 1.14 mycroft timeradd(&runtime, &delta, &runtime);
122 1.13 mycroft # if defined(NFSCLIENT) || defined(NFSSERVER)
123 1.13 mycroft lease_updatetime(delta.tv_sec);
124 1.13 mycroft # endif
125 1.8 cgd splx(s);
126 1.1 cgd resettodr();
127 1.1 cgd }
128 1.11 cgd if (SCARG(uap, tzp))
129 1.1 cgd tz = atz;
130 1.8 cgd return (0);
131 1.1 cgd }
132 1.1 cgd
133 1.1 cgd int tickdelta; /* current clock skew, us. per tick */
134 1.1 cgd long timedelta; /* unapplied time correction, us. */
135 1.1 cgd long bigadj = 1000000; /* use 10x skew above bigadj us. */
136 1.1 cgd
137 1.1 cgd /* ARGSUSED */
138 1.3 andrew int
139 1.16 mycroft sys_adjtime(p, v, retval)
140 1.1 cgd struct proc *p;
141 1.15 thorpej void *v;
142 1.15 thorpej register_t *retval;
143 1.15 thorpej {
144 1.16 mycroft register struct sys_adjtime_args /* {
145 1.11 cgd syscallarg(struct timeval *) delta;
146 1.11 cgd syscallarg(struct timeval *) olddelta;
147 1.15 thorpej } */ *uap = v;
148 1.8 cgd struct timeval atv;
149 1.8 cgd register long ndelta, ntickdelta, odelta;
150 1.1 cgd int s, error;
151 1.1 cgd
152 1.17 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
153 1.1 cgd return (error);
154 1.17 christos
155 1.17 christos error = copyin((caddr_t)SCARG(uap, delta), (caddr_t)&atv,
156 1.17 christos sizeof(struct timeval));
157 1.17 christos if (error)
158 1.1 cgd return (error);
159 1.8 cgd
160 1.8 cgd /*
161 1.8 cgd * Compute the total correction and the rate at which to apply it.
162 1.8 cgd * Round the adjustment down to a whole multiple of the per-tick
163 1.8 cgd * delta, so that after some number of incremental changes in
164 1.8 cgd * hardclock(), tickdelta will become zero, lest the correction
165 1.8 cgd * overshoot and start taking us away from the desired final time.
166 1.8 cgd */
167 1.1 cgd ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
168 1.8 cgd if (ndelta > bigadj)
169 1.8 cgd ntickdelta = 10 * tickadj;
170 1.8 cgd else
171 1.8 cgd ntickdelta = tickadj;
172 1.8 cgd if (ndelta % ntickdelta)
173 1.8 cgd ndelta = ndelta / ntickdelta * ntickdelta;
174 1.8 cgd
175 1.8 cgd /*
176 1.8 cgd * To make hardclock()'s job easier, make the per-tick delta negative
177 1.8 cgd * if we want time to run slower; then hardclock can simply compute
178 1.8 cgd * tick + tickdelta, and subtract tickdelta from timedelta.
179 1.8 cgd */
180 1.8 cgd if (ndelta < 0)
181 1.8 cgd ntickdelta = -ntickdelta;
182 1.1 cgd s = splclock();
183 1.8 cgd odelta = timedelta;
184 1.1 cgd timedelta = ndelta;
185 1.8 cgd tickdelta = ntickdelta;
186 1.1 cgd splx(s);
187 1.1 cgd
188 1.11 cgd if (SCARG(uap, olddelta)) {
189 1.8 cgd atv.tv_sec = odelta / 1000000;
190 1.8 cgd atv.tv_usec = odelta % 1000000;
191 1.11 cgd (void) copyout((caddr_t)&atv, (caddr_t)SCARG(uap, olddelta),
192 1.8 cgd sizeof(struct timeval));
193 1.8 cgd }
194 1.1 cgd return (0);
195 1.1 cgd }
196 1.1 cgd
197 1.1 cgd /*
198 1.1 cgd * Get value of an interval timer. The process virtual and
199 1.1 cgd * profiling virtual time timers are kept in the p_stats area, since
200 1.1 cgd * they can be swapped out. These are kept internally in the
201 1.1 cgd * way they are specified externally: in time until they expire.
202 1.1 cgd *
203 1.1 cgd * The real time interval timer is kept in the process table slot
204 1.1 cgd * for the process, and its value (it_value) is kept as an
205 1.1 cgd * absolute time rather than as a delta, so that it is easy to keep
206 1.1 cgd * periodic real-time signals from drifting.
207 1.1 cgd *
208 1.1 cgd * Virtual time timers are processed in the hardclock() routine of
209 1.1 cgd * kern_clock.c. The real time timer is processed by a timeout
210 1.1 cgd * routine, called from the softclock() routine. Since a callout
211 1.1 cgd * may be delayed in real time due to interrupt processing in the system,
212 1.1 cgd * it is possible for the real time timeout routine (realitexpire, given below),
213 1.1 cgd * to be delayed in real time past when it is supposed to occur. It
214 1.1 cgd * does not suffice, therefore, to reload the real timer .it_value from the
215 1.1 cgd * real time timers .it_interval. Rather, we compute the next time in
216 1.1 cgd * absolute time the timer should go off.
217 1.1 cgd */
218 1.1 cgd /* ARGSUSED */
219 1.3 andrew int
220 1.16 mycroft sys_getitimer(p, v, retval)
221 1.1 cgd struct proc *p;
222 1.15 thorpej void *v;
223 1.15 thorpej register_t *retval;
224 1.15 thorpej {
225 1.16 mycroft register struct sys_getitimer_args /* {
226 1.11 cgd syscallarg(u_int) which;
227 1.11 cgd syscallarg(struct itimerval *) itv;
228 1.15 thorpej } */ *uap = v;
229 1.1 cgd struct itimerval aitv;
230 1.1 cgd int s;
231 1.1 cgd
232 1.11 cgd if (SCARG(uap, which) > ITIMER_PROF)
233 1.1 cgd return (EINVAL);
234 1.1 cgd s = splclock();
235 1.11 cgd if (SCARG(uap, which) == ITIMER_REAL) {
236 1.1 cgd /*
237 1.12 mycroft * Convert from absolute to relative time in .it_value
238 1.1 cgd * part of real time timer. If time for real time timer
239 1.1 cgd * has passed return 0, else return difference between
240 1.1 cgd * current time and time for the timer to go off.
241 1.1 cgd */
242 1.1 cgd aitv = p->p_realtimer;
243 1.1 cgd if (timerisset(&aitv.it_value))
244 1.1 cgd if (timercmp(&aitv.it_value, &time, <))
245 1.1 cgd timerclear(&aitv.it_value);
246 1.1 cgd else
247 1.14 mycroft timersub(&aitv.it_value, &time, &aitv.it_value);
248 1.1 cgd } else
249 1.11 cgd aitv = p->p_stats->p_timer[SCARG(uap, which)];
250 1.1 cgd splx(s);
251 1.11 cgd return (copyout((caddr_t)&aitv, (caddr_t)SCARG(uap, itv),
252 1.1 cgd sizeof (struct itimerval)));
253 1.1 cgd }
254 1.1 cgd
255 1.1 cgd /* ARGSUSED */
256 1.3 andrew int
257 1.16 mycroft sys_setitimer(p, v, retval)
258 1.1 cgd struct proc *p;
259 1.17 christos register void *v;
260 1.15 thorpej register_t *retval;
261 1.15 thorpej {
262 1.16 mycroft register struct sys_setitimer_args /* {
263 1.11 cgd syscallarg(u_int) which;
264 1.11 cgd syscallarg(struct itimerval *) itv;
265 1.11 cgd syscallarg(struct itimerval *) oitv;
266 1.15 thorpej } */ *uap = v;
267 1.1 cgd struct itimerval aitv;
268 1.1 cgd register struct itimerval *itvp;
269 1.1 cgd int s, error;
270 1.1 cgd
271 1.11 cgd if (SCARG(uap, which) > ITIMER_PROF)
272 1.1 cgd return (EINVAL);
273 1.11 cgd itvp = SCARG(uap, itv);
274 1.1 cgd if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
275 1.1 cgd sizeof(struct itimerval))))
276 1.1 cgd return (error);
277 1.11 cgd if ((SCARG(uap, itv) = SCARG(uap, oitv)) &&
278 1.16 mycroft (error = sys_getitimer(p, uap, retval)))
279 1.1 cgd return (error);
280 1.1 cgd if (itvp == 0)
281 1.1 cgd return (0);
282 1.1 cgd if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
283 1.1 cgd return (EINVAL);
284 1.1 cgd s = splclock();
285 1.11 cgd if (SCARG(uap, which) == ITIMER_REAL) {
286 1.7 mycroft untimeout(realitexpire, p);
287 1.1 cgd if (timerisset(&aitv.it_value)) {
288 1.14 mycroft timeradd(&aitv.it_value, &time, &aitv.it_value);
289 1.7 mycroft timeout(realitexpire, p, hzto(&aitv.it_value));
290 1.1 cgd }
291 1.1 cgd p->p_realtimer = aitv;
292 1.1 cgd } else
293 1.11 cgd p->p_stats->p_timer[SCARG(uap, which)] = aitv;
294 1.1 cgd splx(s);
295 1.1 cgd return (0);
296 1.1 cgd }
297 1.1 cgd
298 1.1 cgd /*
299 1.1 cgd * Real interval timer expired:
300 1.1 cgd * send process whose timer expired an alarm signal.
301 1.1 cgd * If time is not set up to reload, then just return.
302 1.1 cgd * Else compute next time timer should go off which is > current time.
303 1.1 cgd * This is where delay in processing this timeout causes multiple
304 1.1 cgd * SIGALRM calls to be compressed into one.
305 1.1 cgd */
306 1.3 andrew void
307 1.6 cgd realitexpire(arg)
308 1.6 cgd void *arg;
309 1.6 cgd {
310 1.1 cgd register struct proc *p;
311 1.1 cgd int s;
312 1.1 cgd
313 1.6 cgd p = (struct proc *)arg;
314 1.1 cgd psignal(p, SIGALRM);
315 1.1 cgd if (!timerisset(&p->p_realtimer.it_interval)) {
316 1.1 cgd timerclear(&p->p_realtimer.it_value);
317 1.1 cgd return;
318 1.1 cgd }
319 1.1 cgd for (;;) {
320 1.1 cgd s = splclock();
321 1.14 mycroft timeradd(&p->p_realtimer.it_value,
322 1.14 mycroft &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
323 1.1 cgd if (timercmp(&p->p_realtimer.it_value, &time, >)) {
324 1.7 mycroft timeout(realitexpire, p,
325 1.1 cgd hzto(&p->p_realtimer.it_value));
326 1.1 cgd splx(s);
327 1.1 cgd return;
328 1.1 cgd }
329 1.1 cgd splx(s);
330 1.1 cgd }
331 1.1 cgd }
332 1.1 cgd
333 1.1 cgd /*
334 1.1 cgd * Check that a proposed value to load into the .it_value or
335 1.1 cgd * .it_interval part of an interval timer is acceptable, and
336 1.1 cgd * fix it to have at least minimal value (i.e. if it is less
337 1.1 cgd * than the resolution of the clock, round it up.)
338 1.1 cgd */
339 1.3 andrew int
340 1.1 cgd itimerfix(tv)
341 1.1 cgd struct timeval *tv;
342 1.1 cgd {
343 1.1 cgd
344 1.1 cgd if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
345 1.1 cgd tv->tv_usec < 0 || tv->tv_usec >= 1000000)
346 1.1 cgd return (EINVAL);
347 1.1 cgd if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
348 1.1 cgd tv->tv_usec = tick;
349 1.1 cgd return (0);
350 1.1 cgd }
351 1.1 cgd
352 1.1 cgd /*
353 1.1 cgd * Decrement an interval timer by a specified number
354 1.1 cgd * of microseconds, which must be less than a second,
355 1.1 cgd * i.e. < 1000000. If the timer expires, then reload
356 1.1 cgd * it. In this case, carry over (usec - old value) to
357 1.8 cgd * reduce the value reloaded into the timer so that
358 1.1 cgd * the timer does not drift. This routine assumes
359 1.1 cgd * that it is called in a context where the timers
360 1.1 cgd * on which it is operating cannot change in value.
361 1.1 cgd */
362 1.3 andrew int
363 1.1 cgd itimerdecr(itp, usec)
364 1.1 cgd register struct itimerval *itp;
365 1.1 cgd int usec;
366 1.1 cgd {
367 1.1 cgd
368 1.1 cgd if (itp->it_value.tv_usec < usec) {
369 1.1 cgd if (itp->it_value.tv_sec == 0) {
370 1.1 cgd /* expired, and already in next interval */
371 1.1 cgd usec -= itp->it_value.tv_usec;
372 1.1 cgd goto expire;
373 1.1 cgd }
374 1.1 cgd itp->it_value.tv_usec += 1000000;
375 1.1 cgd itp->it_value.tv_sec--;
376 1.1 cgd }
377 1.1 cgd itp->it_value.tv_usec -= usec;
378 1.1 cgd usec = 0;
379 1.1 cgd if (timerisset(&itp->it_value))
380 1.1 cgd return (1);
381 1.1 cgd /* expired, exactly at end of interval */
382 1.1 cgd expire:
383 1.1 cgd if (timerisset(&itp->it_interval)) {
384 1.1 cgd itp->it_value = itp->it_interval;
385 1.1 cgd itp->it_value.tv_usec -= usec;
386 1.1 cgd if (itp->it_value.tv_usec < 0) {
387 1.1 cgd itp->it_value.tv_usec += 1000000;
388 1.1 cgd itp->it_value.tv_sec--;
389 1.1 cgd }
390 1.1 cgd } else
391 1.1 cgd itp->it_value.tv_usec = 0; /* sec is already 0 */
392 1.1 cgd return (0);
393 1.1 cgd }
394