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