kern_time.c revision 58377
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *	The Regents of the University of California.  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 *	@(#)kern_time.c	8.1 (Berkeley) 6/10/93
34 * $FreeBSD: head/sys/kern/kern_time.c 58377 2000-03-20 14:09:06Z phk $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/buf.h>
40#include <sys/sysproto.h>
41#include <sys/resourcevar.h>
42#include <sys/signalvar.h>
43#include <sys/kernel.h>
44#include <sys/systm.h>
45#include <sys/sysent.h>
46#include <sys/proc.h>
47#include <sys/time.h>
48#include <sys/timetc.h>
49#include <sys/vnode.h>
50#include <vm/vm.h>
51#include <vm/vm_extern.h>
52
53struct timezone tz;
54
55/*
56 * Time of day and interval timer support.
57 *
58 * These routines provide the kernel entry points to get and set
59 * the time-of-day and per-process interval timers.  Subroutines
60 * here provide support for adding and subtracting timeval structures
61 * and decrementing interval timers, optionally reloading the interval
62 * timers when they expire.
63 */
64
65static int	nanosleep1 __P((struct proc *p, struct timespec *rqt,
66		    struct timespec *rmt));
67static int	settime __P((struct timeval *));
68static void	timevalfix __P((struct timeval *));
69static void	no_lease_updatetime __P((int));
70
71static void
72no_lease_updatetime(deltat)
73	int deltat;
74{
75}
76
77void (*lease_updatetime) __P((int))  = no_lease_updatetime;
78
79static int
80settime(tv)
81	struct timeval *tv;
82{
83	struct timeval delta, tv1, tv2;
84	static struct timeval maxtime, laststep;
85	struct timespec ts;
86	int s;
87
88	s = splclock();
89	microtime(&tv1);
90	delta = *tv;
91	timevalsub(&delta, &tv1);
92
93	/*
94	 * If the system is secure, we do not allow the time to be
95	 * set to a value earlier than 1 second less than the highest
96	 * time we have yet seen. The worst a miscreant can do in
97	 * this circumstance is "freeze" time. He couldn't go
98	 * back to the past.
99	 *
100	 * We similarly do not allow the clock to be stepped more
101	 * than one second, nor more than once per second. This allows
102	 * a miscreant to make the clock march double-time, but no worse.
103	 */
104	if (securelevel > 1) {
105		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
106			/*
107			 * Update maxtime to latest time we've seen.
108			 */
109			if (tv1.tv_sec > maxtime.tv_sec)
110				maxtime = tv1;
111			tv2 = *tv;
112			timevalsub(&tv2, &maxtime);
113			if (tv2.tv_sec < -1) {
114				tv->tv_sec = maxtime.tv_sec - 1;
115				printf("Time adjustment clamped to -1 second\n");
116			}
117		} else {
118			if (tv1.tv_sec == laststep.tv_sec) {
119				splx(s);
120				return (EPERM);
121			}
122			if (delta.tv_sec > 1) {
123				tv->tv_sec = tv1.tv_sec + 1;
124				printf("Time adjustment clamped to +1 second\n");
125			}
126			laststep = *tv;
127		}
128	}
129
130	ts.tv_sec = tv->tv_sec;
131	ts.tv_nsec = tv->tv_usec * 1000;
132	tc_setclock(&ts);
133	(void) splsoftclock();
134	lease_updatetime(delta.tv_sec);
135	splx(s);
136	resettodr();
137	return (0);
138}
139
140#ifndef _SYS_SYSPROTO_H_
141struct clock_gettime_args {
142	clockid_t clock_id;
143	struct	timespec *tp;
144};
145#endif
146
147/* ARGSUSED */
148int
149clock_gettime(p, uap)
150	struct proc *p;
151	struct clock_gettime_args *uap;
152{
153	struct timespec ats;
154
155	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
156		return (EINVAL);
157	nanotime(&ats);
158	return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
159}
160
161#ifndef _SYS_SYSPROTO_H_
162struct clock_settime_args {
163	clockid_t clock_id;
164	const struct	timespec *tp;
165};
166#endif
167
168/* ARGSUSED */
169int
170clock_settime(p, uap)
171	struct proc *p;
172	struct clock_settime_args *uap;
173{
174	struct timeval atv;
175	struct timespec ats;
176	int error;
177
178	if ((error = suser(p)) != 0)
179		return (error);
180	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
181		return (EINVAL);
182	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
183		return (error);
184	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
185		return (EINVAL);
186	/* XXX Don't convert nsec->usec and back */
187	TIMESPEC_TO_TIMEVAL(&atv, &ats);
188	if ((error = settime(&atv)))
189		return (error);
190	return (0);
191}
192
193#ifndef _SYS_SYSPROTO_H_
194struct clock_getres_args {
195	clockid_t clock_id;
196	struct	timespec *tp;
197};
198#endif
199
200int
201clock_getres(p, uap)
202	struct proc *p;
203	struct clock_getres_args *uap;
204{
205	struct timespec ts;
206	int error;
207
208	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
209		return (EINVAL);
210	error = 0;
211	if (SCARG(uap, tp)) {
212		ts.tv_sec = 0;
213		ts.tv_nsec = 1000000000 / timecounter->tc_frequency;
214		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
215	}
216	return (error);
217}
218
219static int nanowait;
220
221static int
222nanosleep1(p, rqt, rmt)
223	struct proc *p;
224	struct timespec *rqt, *rmt;
225{
226	struct timespec ts, ts2, ts3;
227	struct timeval tv;
228	int error;
229
230	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
231		return (EINVAL);
232	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
233		return (0);
234	getnanouptime(&ts);
235	timespecadd(&ts, rqt);
236	TIMESPEC_TO_TIMEVAL(&tv, rqt);
237	for (;;) {
238		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
239		    tvtohz(&tv));
240		getnanouptime(&ts2);
241		if (error != EWOULDBLOCK) {
242			if (error == ERESTART)
243				error = EINTR;
244			if (rmt != NULL) {
245				timespecsub(&ts, &ts2);
246				if (ts.tv_sec < 0)
247					timespecclear(&ts);
248				*rmt = ts;
249			}
250			return (error);
251		}
252		if (timespeccmp(&ts2, &ts, >=))
253			return (0);
254		ts3 = ts;
255		timespecsub(&ts3, &ts2);
256		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
257	}
258}
259
260#ifndef _SYS_SYSPROTO_H_
261struct nanosleep_args {
262	struct	timespec *rqtp;
263	struct	timespec *rmtp;
264};
265#endif
266
267/* ARGSUSED */
268int
269nanosleep(p, uap)
270	struct proc *p;
271	struct nanosleep_args *uap;
272{
273	struct timespec rmt, rqt;
274	int error, error2;
275
276	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
277	if (error)
278		return (error);
279	if (SCARG(uap, rmtp))
280		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
281		    VM_PROT_WRITE))
282			return (EFAULT);
283	error = nanosleep1(p, &rqt, &rmt);
284	if (error && SCARG(uap, rmtp)) {
285		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
286		if (error2)	/* XXX shouldn't happen, did useracc() above */
287			return (error2);
288	}
289	return (error);
290}
291
292#ifndef _SYS_SYSPROTO_H_
293struct gettimeofday_args {
294	struct	timeval *tp;
295	struct	timezone *tzp;
296};
297#endif
298/* ARGSUSED */
299int
300gettimeofday(p, uap)
301	struct proc *p;
302	register struct gettimeofday_args *uap;
303{
304	struct timeval atv;
305	int error = 0;
306
307	if (uap->tp) {
308		microtime(&atv);
309		if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
310		    sizeof (atv))))
311			return (error);
312	}
313	if (uap->tzp)
314		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
315		    sizeof (tz));
316	return (error);
317}
318
319#ifndef _SYS_SYSPROTO_H_
320struct settimeofday_args {
321	struct	timeval *tv;
322	struct	timezone *tzp;
323};
324#endif
325/* ARGSUSED */
326int
327settimeofday(p, uap)
328	struct proc *p;
329	struct settimeofday_args *uap;
330{
331	struct timeval atv;
332	struct timezone atz;
333	int error;
334
335	if ((error = suser(p)))
336		return (error);
337	/* Verify all parameters before changing time. */
338	if (uap->tv) {
339		if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
340		    sizeof(atv))))
341			return (error);
342		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
343			return (EINVAL);
344	}
345	if (uap->tzp &&
346	    (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
347		return (error);
348	if (uap->tv && (error = settime(&atv)))
349		return (error);
350	if (uap->tzp)
351		tz = atz;
352	return (0);
353}
354
355int	tickdelta;			/* current clock skew, us. per tick */
356long	timedelta;			/* unapplied time correction, us. */
357static long	bigadj = 1000000;	/* use 10x skew above bigadj us. */
358
359#ifndef _SYS_SYSPROTO_H_
360struct adjtime_args {
361	struct timeval *delta;
362	struct timeval *olddelta;
363};
364#endif
365/* ARGSUSED */
366int
367adjtime(p, uap)
368	struct proc *p;
369	register struct adjtime_args *uap;
370{
371	struct timeval atv;
372	register long ndelta, ntickdelta, odelta;
373	int s, error;
374
375	if ((error = suser(p)))
376		return (error);
377	if ((error =
378	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
379		return (error);
380
381	/*
382	 * Compute the total correction and the rate at which to apply it.
383	 * Round the adjustment down to a whole multiple of the per-tick
384	 * delta, so that after some number of incremental changes in
385	 * hardclock(), tickdelta will become zero, lest the correction
386	 * overshoot and start taking us away from the desired final time.
387	 */
388	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
389	if (ndelta > bigadj || ndelta < -bigadj)
390		ntickdelta = 10 * tickadj;
391	else
392		ntickdelta = tickadj;
393	if (ndelta % ntickdelta)
394		ndelta = ndelta / ntickdelta * ntickdelta;
395
396	/*
397	 * To make hardclock()'s job easier, make the per-tick delta negative
398	 * if we want time to run slower; then hardclock can simply compute
399	 * tick + tickdelta, and subtract tickdelta from timedelta.
400	 */
401	if (ndelta < 0)
402		ntickdelta = -ntickdelta;
403	s = splclock();
404	odelta = timedelta;
405	timedelta = ndelta;
406	tickdelta = ntickdelta;
407	splx(s);
408
409	if (uap->olddelta) {
410		atv.tv_sec = odelta / 1000000;
411		atv.tv_usec = odelta % 1000000;
412		(void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
413		    sizeof(struct timeval));
414	}
415	return (0);
416}
417
418/*
419 * Get value of an interval timer.  The process virtual and
420 * profiling virtual time timers are kept in the p_stats area, since
421 * they can be swapped out.  These are kept internally in the
422 * way they are specified externally: in time until they expire.
423 *
424 * The real time interval timer is kept in the process table slot
425 * for the process, and its value (it_value) is kept as an
426 * absolute time rather than as a delta, so that it is easy to keep
427 * periodic real-time signals from drifting.
428 *
429 * Virtual time timers are processed in the hardclock() routine of
430 * kern_clock.c.  The real time timer is processed by a timeout
431 * routine, called from the softclock() routine.  Since a callout
432 * may be delayed in real time due to interrupt processing in the system,
433 * it is possible for the real time timeout routine (realitexpire, given below),
434 * to be delayed in real time past when it is supposed to occur.  It
435 * does not suffice, therefore, to reload the real timer .it_value from the
436 * real time timers .it_interval.  Rather, we compute the next time in
437 * absolute time the timer should go off.
438 */
439#ifndef _SYS_SYSPROTO_H_
440struct getitimer_args {
441	u_int	which;
442	struct	itimerval *itv;
443};
444#endif
445/* ARGSUSED */
446int
447getitimer(p, uap)
448	struct proc *p;
449	register struct getitimer_args *uap;
450{
451	struct timeval ctv;
452	struct itimerval aitv;
453	int s;
454
455	if (uap->which > ITIMER_PROF)
456		return (EINVAL);
457	s = splclock(); /* XXX still needed ? */
458	if (uap->which == ITIMER_REAL) {
459		/*
460		 * Convert from absolute to relative time in .it_value
461		 * part of real time timer.  If time for real time timer
462		 * has passed return 0, else return difference between
463		 * current time and time for the timer to go off.
464		 */
465		aitv = p->p_realtimer;
466		if (timevalisset(&aitv.it_value)) {
467			getmicrouptime(&ctv);
468			if (timevalcmp(&aitv.it_value, &ctv, <))
469				timevalclear(&aitv.it_value);
470			else
471				timevalsub(&aitv.it_value, &ctv);
472		}
473	} else
474		aitv = p->p_stats->p_timer[uap->which];
475	splx(s);
476	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
477	    sizeof (struct itimerval)));
478}
479
480#ifndef _SYS_SYSPROTO_H_
481struct setitimer_args {
482	u_int	which;
483	struct	itimerval *itv, *oitv;
484};
485#endif
486/* ARGSUSED */
487int
488setitimer(p, uap)
489	struct proc *p;
490	register struct setitimer_args *uap;
491{
492	struct itimerval aitv;
493	struct timeval ctv;
494	register struct itimerval *itvp;
495	int s, error;
496
497	if (uap->which > ITIMER_PROF)
498		return (EINVAL);
499	itvp = uap->itv;
500	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
501	    sizeof(struct itimerval))))
502		return (error);
503	if ((uap->itv = uap->oitv) &&
504	    (error = getitimer(p, (struct getitimer_args *)uap)))
505		return (error);
506	if (itvp == 0)
507		return (0);
508	if (itimerfix(&aitv.it_value))
509		return (EINVAL);
510	if (!timevalisset(&aitv.it_value))
511		timevalclear(&aitv.it_interval);
512	else if (itimerfix(&aitv.it_interval))
513		return (EINVAL);
514	s = splclock(); /* XXX: still needed ? */
515	if (uap->which == ITIMER_REAL) {
516		if (timevalisset(&p->p_realtimer.it_value))
517			untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
518		if (timevalisset(&aitv.it_value))
519			p->p_ithandle = timeout(realitexpire, (caddr_t)p,
520						tvtohz(&aitv.it_value));
521		getmicrouptime(&ctv);
522		timevaladd(&aitv.it_value, &ctv);
523		p->p_realtimer = aitv;
524	} else
525		p->p_stats->p_timer[uap->which] = aitv;
526	splx(s);
527	return (0);
528}
529
530/*
531 * Real interval timer expired:
532 * send process whose timer expired an alarm signal.
533 * If time is not set up to reload, then just return.
534 * Else compute next time timer should go off which is > current time.
535 * This is where delay in processing this timeout causes multiple
536 * SIGALRM calls to be compressed into one.
537 * tvtohz() always adds 1 to allow for the time until the next clock
538 * interrupt being strictly less than 1 clock tick, but we don't want
539 * that here since we want to appear to be in sync with the clock
540 * interrupt even when we're delayed.
541 */
542void
543realitexpire(arg)
544	void *arg;
545{
546	register struct proc *p;
547	struct timeval ctv, ntv;
548	int s;
549
550	p = (struct proc *)arg;
551	psignal(p, SIGALRM);
552	if (!timevalisset(&p->p_realtimer.it_interval)) {
553		timevalclear(&p->p_realtimer.it_value);
554		return;
555	}
556	for (;;) {
557		s = splclock(); /* XXX: still neeeded ? */
558		timevaladd(&p->p_realtimer.it_value,
559		    &p->p_realtimer.it_interval);
560		getmicrouptime(&ctv);
561		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
562			ntv = p->p_realtimer.it_value;
563			timevalsub(&ntv, &ctv);
564			p->p_ithandle = timeout(realitexpire, (caddr_t)p,
565			    tvtohz(&ntv) - 1);
566			splx(s);
567			return;
568		}
569		splx(s);
570	}
571}
572
573/*
574 * Check that a proposed value to load into the .it_value or
575 * .it_interval part of an interval timer is acceptable, and
576 * fix it to have at least minimal value (i.e. if it is less
577 * than the resolution of the clock, round it up.)
578 */
579int
580itimerfix(tv)
581	struct timeval *tv;
582{
583
584	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
585	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
586		return (EINVAL);
587	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
588		tv->tv_usec = tick;
589	return (0);
590}
591
592/*
593 * Decrement an interval timer by a specified number
594 * of microseconds, which must be less than a second,
595 * i.e. < 1000000.  If the timer expires, then reload
596 * it.  In this case, carry over (usec - old value) to
597 * reduce the value reloaded into the timer so that
598 * the timer does not drift.  This routine assumes
599 * that it is called in a context where the timers
600 * on which it is operating cannot change in value.
601 */
602int
603itimerdecr(itp, usec)
604	register struct itimerval *itp;
605	int usec;
606{
607
608	if (itp->it_value.tv_usec < usec) {
609		if (itp->it_value.tv_sec == 0) {
610			/* expired, and already in next interval */
611			usec -= itp->it_value.tv_usec;
612			goto expire;
613		}
614		itp->it_value.tv_usec += 1000000;
615		itp->it_value.tv_sec--;
616	}
617	itp->it_value.tv_usec -= usec;
618	usec = 0;
619	if (timevalisset(&itp->it_value))
620		return (1);
621	/* expired, exactly at end of interval */
622expire:
623	if (timevalisset(&itp->it_interval)) {
624		itp->it_value = itp->it_interval;
625		itp->it_value.tv_usec -= usec;
626		if (itp->it_value.tv_usec < 0) {
627			itp->it_value.tv_usec += 1000000;
628			itp->it_value.tv_sec--;
629		}
630	} else
631		itp->it_value.tv_usec = 0;		/* sec is already 0 */
632	return (0);
633}
634
635/*
636 * Add and subtract routines for timevals.
637 * N.B.: subtract routine doesn't deal with
638 * results which are before the beginning,
639 * it just gets very confused in this case.
640 * Caveat emptor.
641 */
642void
643timevaladd(t1, t2)
644	struct timeval *t1, *t2;
645{
646
647	t1->tv_sec += t2->tv_sec;
648	t1->tv_usec += t2->tv_usec;
649	timevalfix(t1);
650}
651
652void
653timevalsub(t1, t2)
654	struct timeval *t1, *t2;
655{
656
657	t1->tv_sec -= t2->tv_sec;
658	t1->tv_usec -= t2->tv_usec;
659	timevalfix(t1);
660}
661
662static void
663timevalfix(t1)
664	struct timeval *t1;
665{
666
667	if (t1->tv_usec < 0) {
668		t1->tv_sec--;
669		t1->tv_usec += 1000000;
670	}
671	if (t1->tv_usec >= 1000000) {
672		t1->tv_sec++;
673		t1->tv_usec -= 1000000;
674	}
675}
676