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