kern_time.c revision 1.137
1/*	$NetBSD: kern_time.c,v 1.137 2007/12/22 01:14:55 yamt Exp $	*/
2
3/*-
4 * Copyright (c) 2000, 2004, 2005, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christopher G. Demetriou.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the NetBSD
21 *	Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39/*
40 * Copyright (c) 1982, 1986, 1989, 1993
41 *	The Regents of the University of California.  All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 *    notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 *    notice, this list of conditions and the following disclaimer in the
50 *    documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 *    may be used to endorse or promote products derived from this software
53 *    without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
68 */
69
70#include <sys/cdefs.h>
71__KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.137 2007/12/22 01:14:55 yamt Exp $");
72
73#include <sys/param.h>
74#include <sys/resourcevar.h>
75#include <sys/kernel.h>
76#include <sys/systm.h>
77#include <sys/proc.h>
78#include <sys/vnode.h>
79#include <sys/signalvar.h>
80#include <sys/syslog.h>
81#include <sys/timetc.h>
82#include <sys/kauth.h>
83
84#include <sys/mount.h>
85#include <sys/syscallargs.h>
86
87#include <uvm/uvm_extern.h>
88
89#include <sys/cpu.h>
90
91kmutex_t	time_lock;
92
93POOL_INIT(ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
94    &pool_allocator_nointr, IPL_NONE);
95POOL_INIT(ptimers_pool, sizeof(struct ptimers), 0, 0, 0, "ptimerspl",
96    &pool_allocator_nointr, IPL_NONE);
97
98/*
99 * Initialize timekeeping.
100 */
101void
102time_init(void)
103{
104
105	mutex_init(&time_lock, MUTEX_DEFAULT, IPL_NONE);
106}
107
108/* Time of day and interval timer support.
109 *
110 * These routines provide the kernel entry points to get and set
111 * the time-of-day and per-process interval timers.  Subroutines
112 * here provide support for adding and subtracting timeval structures
113 * and decrementing interval timers, optionally reloading the interval
114 * timers when they expire.
115 */
116
117/* This function is used by clock_settime and settimeofday */
118static int
119settime1(struct proc *p, struct timespec *ts, bool check_kauth)
120{
121	struct timeval delta, tv;
122	struct timeval now;
123	struct timespec ts1;
124	struct bintime btdelta;
125	lwp_t *l;
126	int s;
127
128	TIMESPEC_TO_TIMEVAL(&tv, ts);
129
130	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
131	s = splclock();
132	microtime(&now);
133	timersub(&tv, &now, &delta);
134
135	if (check_kauth && kauth_authorize_system(kauth_cred_get(),
136	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, ts, &delta,
137	    KAUTH_ARG(check_kauth ? false : true)) != 0) {
138		splx(s);
139		return (EPERM);
140	}
141
142#ifdef notyet
143	if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
144		splx(s);
145		return (EPERM);
146	}
147#endif
148
149	TIMEVAL_TO_TIMESPEC(&tv, &ts1);
150	tc_setclock(&ts1);
151
152	timeradd(&boottime, &delta, &boottime);
153
154	/*
155	 * XXXSMP: There is a short race between setting the time above
156	 * and adjusting LWP's run times.  Fixing this properly means
157	 * pausing all CPUs while we adjust the clock.
158	 */
159	timeval2bintime(&delta, &btdelta);
160	mutex_enter(&proclist_lock);
161	LIST_FOREACH(l, &alllwp, l_list) {
162		lwp_lock(l);
163		bintime_add(&l->l_stime, &btdelta);
164		lwp_unlock(l);
165	}
166	mutex_exit(&proclist_lock);
167	resettodr();
168	splx(s);
169
170	return (0);
171}
172
173int
174settime(struct proc *p, struct timespec *ts)
175{
176	return (settime1(p, ts, true));
177}
178
179/* ARGSUSED */
180int
181sys_clock_gettime(struct lwp *l, const struct sys_clock_gettime_args *uap, register_t *retval)
182{
183	/* {
184		syscallarg(clockid_t) clock_id;
185		syscallarg(struct timespec *) tp;
186	} */
187	clockid_t clock_id;
188	struct timespec ats;
189
190	clock_id = SCARG(uap, clock_id);
191	switch (clock_id) {
192	case CLOCK_REALTIME:
193		nanotime(&ats);
194		break;
195	case CLOCK_MONOTONIC:
196		nanouptime(&ats);
197		break;
198	default:
199		return (EINVAL);
200	}
201
202	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
203}
204
205/* ARGSUSED */
206int
207sys_clock_settime(struct lwp *l, const struct sys_clock_settime_args *uap, register_t *retval)
208{
209	/* {
210		syscallarg(clockid_t) clock_id;
211		syscallarg(const struct timespec *) tp;
212	} */
213
214	return clock_settime1(l->l_proc, SCARG(uap, clock_id), SCARG(uap, tp),
215	    true);
216}
217
218
219int
220clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
221    bool check_kauth)
222{
223	struct timespec ats;
224	int error;
225
226	if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
227		return (error);
228
229	switch (clock_id) {
230	case CLOCK_REALTIME:
231		if ((error = settime1(p, &ats, check_kauth)) != 0)
232			return (error);
233		break;
234	case CLOCK_MONOTONIC:
235		return (EINVAL);	/* read-only clock */
236	default:
237		return (EINVAL);
238	}
239
240	return 0;
241}
242
243int
244sys_clock_getres(struct lwp *l, const struct sys_clock_getres_args *uap, register_t *retval)
245{
246	/* {
247		syscallarg(clockid_t) clock_id;
248		syscallarg(struct timespec *) tp;
249	} */
250	clockid_t clock_id;
251	struct timespec ts;
252	int error = 0;
253
254	clock_id = SCARG(uap, clock_id);
255	switch (clock_id) {
256	case CLOCK_REALTIME:
257	case CLOCK_MONOTONIC:
258		ts.tv_sec = 0;
259		if (tc_getfrequency() > 1000000000)
260			ts.tv_nsec = 1;
261		else
262			ts.tv_nsec = 1000000000 / tc_getfrequency();
263		break;
264	default:
265		return (EINVAL);
266	}
267
268	if (SCARG(uap, tp))
269		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
270
271	return error;
272}
273
274/* ARGSUSED */
275int
276sys_nanosleep(struct lwp *l, const struct sys_nanosleep_args *uap, register_t *retval)
277{
278	/* {
279		syscallarg(struct timespec *) rqtp;
280		syscallarg(struct timespec *) rmtp;
281	} */
282	struct timespec rmt, rqt;
283	int error, error1;
284
285	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
286	if (error)
287		return (error);
288
289	error = nanosleep1(l, &rqt, SCARG(uap, rmtp) ? &rmt : NULL);
290	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
291		return error;
292
293	error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
294	return error1 ? error1 : error;
295}
296
297int
298nanosleep1(struct lwp *l, struct timespec *rqt, struct timespec *rmt)
299{
300	int error, timo;
301
302	if (itimespecfix(rqt))
303		return (EINVAL);
304
305	timo = tstohz(rqt);
306	/*
307	 * Avoid inadvertantly sleeping forever
308	 */
309	if (timo == 0)
310		timo = 1;
311
312	if (rmt != NULL)
313		getnanouptime(rmt);
314
315	error = kpause("nanoslp", true, timo, NULL);
316	if (error == ERESTART)
317		error = EINTR;
318	if (error == EWOULDBLOCK)
319		error = 0;
320
321	if (rmt!= NULL) {
322		struct timespec rmtend;
323
324		getnanouptime(&rmtend);
325
326		timespecsub(&rmtend, rmt, rmt);
327		timespecsub(rqt, rmt, rmt);
328		if (rmt->tv_sec < 0)
329			timespecclear(rmt);
330	}
331
332	return error;
333}
334
335/* ARGSUSED */
336int
337sys_gettimeofday(struct lwp *l, const struct sys_gettimeofday_args *uap, register_t *retval)
338{
339	/* {
340		syscallarg(struct timeval *) tp;
341		syscallarg(void *) tzp;		really "struct timezone *";
342	} */
343	struct timeval atv;
344	int error = 0;
345	struct timezone tzfake;
346
347	if (SCARG(uap, tp)) {
348		microtime(&atv);
349		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
350		if (error)
351			return (error);
352	}
353	if (SCARG(uap, tzp)) {
354		/*
355		 * NetBSD has no kernel notion of time zone, so we just
356		 * fake up a timezone struct and return it if demanded.
357		 */
358		tzfake.tz_minuteswest = 0;
359		tzfake.tz_dsttime = 0;
360		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
361	}
362	return (error);
363}
364
365/* ARGSUSED */
366int
367sys_settimeofday(struct lwp *l, const struct sys_settimeofday_args *uap, register_t *retval)
368{
369	/* {
370		syscallarg(const struct timeval *) tv;
371		syscallarg(const void *) tzp;	really "const struct timezone *";
372	} */
373
374	return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
375}
376
377int
378settimeofday1(const struct timeval *utv, bool userspace,
379    const void *utzp, struct lwp *l, bool check_kauth)
380{
381	struct timeval atv;
382	struct timespec ts;
383	int error;
384
385	/* Verify all parameters before changing time. */
386
387	/*
388	 * NetBSD has no kernel notion of time zone, and only an
389	 * obsolete program would try to set it, so we log a warning.
390	 */
391	if (utzp)
392		log(LOG_WARNING, "pid %d attempted to set the "
393		    "(obsolete) kernel time zone\n", l->l_proc->p_pid);
394
395	if (utv == NULL)
396		return 0;
397
398	if (userspace) {
399		if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
400			return error;
401		utv = &atv;
402	}
403
404	TIMEVAL_TO_TIMESPEC(utv, &ts);
405	return settime1(l->l_proc, &ts, check_kauth);
406}
407
408#ifndef __HAVE_TIMECOUNTER
409int	tickdelta;			/* current clock skew, us. per tick */
410long	timedelta;			/* unapplied time correction, us. */
411long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
412#endif
413
414int	time_adjusted;			/* set if an adjustment is made */
415
416/* ARGSUSED */
417int
418sys_adjtime(struct lwp *l, const struct sys_adjtime_args *uap, register_t *retval)
419{
420	/* {
421		syscallarg(const struct timeval *) delta;
422		syscallarg(struct timeval *) olddelta;
423	} */
424	int error;
425
426	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
427	    KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
428		return (error);
429
430	return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), l->l_proc);
431}
432
433int
434adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
435{
436	struct timeval atv;
437	int error = 0;
438
439#ifdef __HAVE_TIMECOUNTER
440	extern int64_t time_adjtime;  /* in kern_ntptime.c */
441#else /* !__HAVE_TIMECOUNTER */
442	long ndelta, ntickdelta, odelta;
443	int s;
444#endif /* !__HAVE_TIMECOUNTER */
445
446#ifdef __HAVE_TIMECOUNTER
447	if (olddelta) {
448		atv.tv_sec = time_adjtime / 1000000;
449		atv.tv_usec = time_adjtime % 1000000;
450		if (atv.tv_usec < 0) {
451			atv.tv_usec += 1000000;
452			atv.tv_sec--;
453		}
454		error = copyout(&atv, olddelta, sizeof(struct timeval));
455		if (error)
456			return (error);
457	}
458
459	if (delta) {
460		error = copyin(delta, &atv, sizeof(struct timeval));
461		if (error)
462			return (error);
463
464		time_adjtime = (int64_t)atv.tv_sec * 1000000 +
465			atv.tv_usec;
466
467		if (time_adjtime)
468			/* We need to save the system time during shutdown */
469			time_adjusted |= 1;
470	}
471#else /* !__HAVE_TIMECOUNTER */
472	error = copyin(delta, &atv, sizeof(struct timeval));
473	if (error)
474		return (error);
475
476	/*
477	 * Compute the total correction and the rate at which to apply it.
478	 * Round the adjustment down to a whole multiple of the per-tick
479	 * delta, so that after some number of incremental changes in
480	 * hardclock(), tickdelta will become zero, lest the correction
481	 * overshoot and start taking us away from the desired final time.
482	 */
483	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
484	if (ndelta > bigadj || ndelta < -bigadj)
485		ntickdelta = 10 * tickadj;
486	else
487		ntickdelta = tickadj;
488	if (ndelta % ntickdelta)
489		ndelta = ndelta / ntickdelta * ntickdelta;
490
491	/*
492	 * To make hardclock()'s job easier, make the per-tick delta negative
493	 * if we want time to run slower; then hardclock can simply compute
494	 * tick + tickdelta, and subtract tickdelta from timedelta.
495	 */
496	if (ndelta < 0)
497		ntickdelta = -ntickdelta;
498	if (ndelta != 0)
499		/* We need to save the system clock time during shutdown */
500		time_adjusted |= 1;
501	s = splclock();
502	odelta = timedelta;
503	timedelta = ndelta;
504	tickdelta = ntickdelta;
505	splx(s);
506
507	if (olddelta) {
508		atv.tv_sec = odelta / 1000000;
509		atv.tv_usec = odelta % 1000000;
510		error = copyout(&atv, olddelta, sizeof(struct timeval));
511	}
512#endif /* __HAVE_TIMECOUNTER */
513
514	return error;
515}
516
517/*
518 * Interval timer support. Both the BSD getitimer() family and the POSIX
519 * timer_*() family of routines are supported.
520 *
521 * All timers are kept in an array pointed to by p_timers, which is
522 * allocated on demand - many processes don't use timers at all. The
523 * first three elements in this array are reserved for the BSD timers:
524 * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, and element
525 * 2 is ITIMER_PROF. The rest may be allocated by the timer_create()
526 * syscall.
527 *
528 * Realtime timers are kept in the ptimer structure as an absolute
529 * time; virtual time timers are kept as a linked list of deltas.
530 * Virtual time timers are processed in the hardclock() routine of
531 * kern_clock.c.  The real time timer is processed by a callout
532 * routine, called from the softclock() routine.  Since a callout may
533 * be delayed in real time due to interrupt processing in the system,
534 * it is possible for the real time timeout routine (realtimeexpire,
535 * given below), to be delayed in real time past when it is supposed
536 * to occur.  It does not suffice, therefore, to reload the real timer
537 * .it_value from the real time timers .it_interval.  Rather, we
538 * compute the next time in absolute time the timer should go off.  */
539
540/* Allocate a POSIX realtime timer. */
541int
542sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap, register_t *retval)
543{
544	/* {
545		syscallarg(clockid_t) clock_id;
546		syscallarg(struct sigevent *) evp;
547		syscallarg(timer_t *) timerid;
548	} */
549
550	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
551	    SCARG(uap, evp), copyin, l);
552}
553
554int
555timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
556    copyin_t fetch_event, struct lwp *l)
557{
558	int error;
559	timer_t timerid;
560	struct ptimer *pt;
561	struct proc *p;
562
563	p = l->l_proc;
564
565	if (id < CLOCK_REALTIME ||
566	    id > CLOCK_PROF)
567		return (EINVAL);
568
569	if (p->p_timers == NULL)
570		timers_alloc(p);
571
572	/* Find a free timer slot, skipping those reserved for setitimer(). */
573	for (timerid = 3; timerid < TIMER_MAX; timerid++)
574		if (p->p_timers->pts_timers[timerid] == NULL)
575			break;
576
577	if (timerid == TIMER_MAX)
578		return EAGAIN;
579
580	pt = pool_get(&ptimer_pool, PR_WAITOK);
581	if (evp) {
582		if (((error =
583		    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
584		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
585			(pt->pt_ev.sigev_notify > SIGEV_SA))) {
586			pool_put(&ptimer_pool, pt);
587			return (error ? error : EINVAL);
588		}
589	} else {
590		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
591		switch (id) {
592		case CLOCK_REALTIME:
593			pt->pt_ev.sigev_signo = SIGALRM;
594			break;
595		case CLOCK_VIRTUAL:
596			pt->pt_ev.sigev_signo = SIGVTALRM;
597			break;
598		case CLOCK_PROF:
599			pt->pt_ev.sigev_signo = SIGPROF;
600			break;
601		}
602		pt->pt_ev.sigev_value.sival_int = timerid;
603	}
604	pt->pt_info.ksi_signo = pt->pt_ev.sigev_signo;
605	pt->pt_info.ksi_errno = 0;
606	pt->pt_info.ksi_code = 0;
607	pt->pt_info.ksi_pid = p->p_pid;
608	pt->pt_info.ksi_uid = kauth_cred_getuid(l->l_cred);
609	pt->pt_info.ksi_value = pt->pt_ev.sigev_value;
610
611	pt->pt_type = id;
612	pt->pt_proc = p;
613	pt->pt_overruns = 0;
614	pt->pt_poverruns = 0;
615	pt->pt_entry = timerid;
616	timerclear(&pt->pt_time.it_value);
617	if (id == CLOCK_REALTIME)
618		callout_init(&pt->pt_ch, 0);
619	else
620		pt->pt_active = 0;
621
622	p->p_timers->pts_timers[timerid] = pt;
623
624	return copyout(&timerid, tid, sizeof(timerid));
625}
626
627/* Delete a POSIX realtime timer */
628int
629sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap, register_t *retval)
630{
631	/* {
632		syscallarg(timer_t) timerid;
633	} */
634	struct proc *p = l->l_proc;
635	timer_t timerid;
636	struct ptimer *pt, *ptn;
637	int s;
638
639	timerid = SCARG(uap, timerid);
640
641	if ((p->p_timers == NULL) ||
642	    (timerid < 2) || (timerid >= TIMER_MAX) ||
643	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
644		return (EINVAL);
645
646	if (pt->pt_type == CLOCK_REALTIME) {
647		callout_stop(&pt->pt_ch);
648		callout_destroy(&pt->pt_ch);
649	} else if (pt->pt_active) {
650		s = splclock();
651		ptn = LIST_NEXT(pt, pt_list);
652		LIST_REMOVE(pt, pt_list);
653		for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
654			timeradd(&pt->pt_time.it_value, &ptn->pt_time.it_value,
655			    &ptn->pt_time.it_value);
656		splx(s);
657	}
658
659	p->p_timers->pts_timers[timerid] = NULL;
660	pool_put(&ptimer_pool, pt);
661
662	return (0);
663}
664
665/*
666 * Set up the given timer. The value in pt->pt_time.it_value is taken
667 * to be an absolute time for CLOCK_REALTIME timers and a relative
668 * time for virtual timers.
669 * Must be called at splclock().
670 */
671void
672timer_settime(struct ptimer *pt)
673{
674	struct ptimer *ptn, *pptn;
675	struct ptlist *ptl;
676
677	if (pt->pt_type == CLOCK_REALTIME) {
678		callout_stop(&pt->pt_ch);
679		if (timerisset(&pt->pt_time.it_value)) {
680			/*
681			 * Don't need to check hzto() return value, here.
682			 * callout_reset() does it for us.
683			 */
684			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
685			    realtimerexpire, pt);
686		}
687	} else {
688		if (pt->pt_active) {
689			ptn = LIST_NEXT(pt, pt_list);
690			LIST_REMOVE(pt, pt_list);
691			for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list))
692				timeradd(&pt->pt_time.it_value,
693				    &ptn->pt_time.it_value,
694				    &ptn->pt_time.it_value);
695		}
696		if (timerisset(&pt->pt_time.it_value)) {
697			if (pt->pt_type == CLOCK_VIRTUAL)
698				ptl = &pt->pt_proc->p_timers->pts_virtual;
699			else
700				ptl = &pt->pt_proc->p_timers->pts_prof;
701
702			for (ptn = LIST_FIRST(ptl), pptn = NULL;
703			     ptn && timercmp(&pt->pt_time.it_value,
704				 &ptn->pt_time.it_value, >);
705			     pptn = ptn, ptn = LIST_NEXT(ptn, pt_list))
706				timersub(&pt->pt_time.it_value,
707				    &ptn->pt_time.it_value,
708				    &pt->pt_time.it_value);
709
710			if (pptn)
711				LIST_INSERT_AFTER(pptn, pt, pt_list);
712			else
713				LIST_INSERT_HEAD(ptl, pt, pt_list);
714
715			for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list))
716				timersub(&ptn->pt_time.it_value,
717				    &pt->pt_time.it_value,
718				    &ptn->pt_time.it_value);
719
720			pt->pt_active = 1;
721		} else
722			pt->pt_active = 0;
723	}
724}
725
726void
727timer_gettime(struct ptimer *pt, struct itimerval *aitv)
728{
729	struct timeval now;
730	struct ptimer *ptn;
731
732	*aitv = pt->pt_time;
733	if (pt->pt_type == CLOCK_REALTIME) {
734		/*
735		 * Convert from absolute to relative time in .it_value
736		 * part of real time timer.  If time for real time
737		 * timer has passed return 0, else return difference
738		 * between current time and time for the timer to go
739		 * off.
740		 */
741		if (timerisset(&aitv->it_value)) {
742			getmicrotime(&now);
743			if (timercmp(&aitv->it_value, &now, <))
744				timerclear(&aitv->it_value);
745			else
746				timersub(&aitv->it_value, &now,
747				    &aitv->it_value);
748		}
749	} else if (pt->pt_active) {
750		if (pt->pt_type == CLOCK_VIRTUAL)
751			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_virtual);
752		else
753			ptn = LIST_FIRST(&pt->pt_proc->p_timers->pts_prof);
754		for ( ; ptn && ptn != pt; ptn = LIST_NEXT(ptn, pt_list))
755			timeradd(&aitv->it_value,
756			    &ptn->pt_time.it_value, &aitv->it_value);
757		KASSERT(ptn != NULL); /* pt should be findable on the list */
758	} else
759		timerclear(&aitv->it_value);
760}
761
762
763
764/* Set and arm a POSIX realtime timer */
765int
766sys_timer_settime(struct lwp *l, const struct sys_timer_settime_args *uap, register_t *retval)
767{
768	/* {
769		syscallarg(timer_t) timerid;
770		syscallarg(int) flags;
771		syscallarg(const struct itimerspec *) value;
772		syscallarg(struct itimerspec *) ovalue;
773	} */
774	int error;
775	struct itimerspec value, ovalue, *ovp = NULL;
776
777	if ((error = copyin(SCARG(uap, value), &value,
778	    sizeof(struct itimerspec))) != 0)
779		return (error);
780
781	if (SCARG(uap, ovalue))
782		ovp = &ovalue;
783
784	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
785	    SCARG(uap, flags), l->l_proc)) != 0)
786		return error;
787
788	if (ovp)
789		return copyout(&ovalue, SCARG(uap, ovalue),
790		    sizeof(struct itimerspec));
791	return 0;
792}
793
794int
795dotimer_settime(int timerid, struct itimerspec *value,
796    struct itimerspec *ovalue, int flags, struct proc *p)
797{
798	struct timeval now;
799	struct itimerval val, oval;
800	struct ptimer *pt;
801	int s;
802
803	if ((p->p_timers == NULL) ||
804	    (timerid < 2) || (timerid >= TIMER_MAX) ||
805	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
806		return (EINVAL);
807
808	TIMESPEC_TO_TIMEVAL(&val.it_value, &value->it_value);
809	TIMESPEC_TO_TIMEVAL(&val.it_interval, &value->it_interval);
810	if (itimerfix(&val.it_value) || itimerfix(&val.it_interval))
811		return (EINVAL);
812
813	oval = pt->pt_time;
814	pt->pt_time = val;
815
816	s = splclock();
817	/*
818	 * If we've been passed a relative time for a realtime timer,
819	 * convert it to absolute; if an absolute time for a virtual
820	 * timer, convert it to relative and make sure we don't set it
821	 * to zero, which would cancel the timer, or let it go
822	 * negative, which would confuse the comparison tests.
823	 */
824	if (timerisset(&pt->pt_time.it_value)) {
825		if (pt->pt_type == CLOCK_REALTIME) {
826			if ((flags & TIMER_ABSTIME) == 0) {
827				getmicrotime(&now);
828				timeradd(&pt->pt_time.it_value, &now,
829				    &pt->pt_time.it_value);
830			}
831		} else {
832			if ((flags & TIMER_ABSTIME) != 0) {
833				getmicrotime(&now);
834				timersub(&pt->pt_time.it_value, &now,
835				    &pt->pt_time.it_value);
836				if (!timerisset(&pt->pt_time.it_value) ||
837				    pt->pt_time.it_value.tv_sec < 0) {
838					pt->pt_time.it_value.tv_sec = 0;
839					pt->pt_time.it_value.tv_usec = 1;
840				}
841			}
842		}
843	}
844
845	timer_settime(pt);
846	splx(s);
847
848	if (ovalue) {
849		TIMEVAL_TO_TIMESPEC(&oval.it_value, &ovalue->it_value);
850		TIMEVAL_TO_TIMESPEC(&oval.it_interval, &ovalue->it_interval);
851	}
852
853	return (0);
854}
855
856/* Return the time remaining until a POSIX timer fires. */
857int
858sys_timer_gettime(struct lwp *l, const struct sys_timer_gettime_args *uap, register_t *retval)
859{
860	/* {
861		syscallarg(timer_t) timerid;
862		syscallarg(struct itimerspec *) value;
863	} */
864	struct itimerspec its;
865	int error;
866
867	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
868	    &its)) != 0)
869		return error;
870
871	return copyout(&its, SCARG(uap, value), sizeof(its));
872}
873
874int
875dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
876{
877	int s;
878	struct ptimer *pt;
879	struct itimerval aitv;
880
881	if ((p->p_timers == NULL) ||
882	    (timerid < 2) || (timerid >= TIMER_MAX) ||
883	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
884		return (EINVAL);
885
886	s = splclock();
887	timer_gettime(pt, &aitv);
888	splx(s);
889
890	TIMEVAL_TO_TIMESPEC(&aitv.it_interval, &its->it_interval);
891	TIMEVAL_TO_TIMESPEC(&aitv.it_value, &its->it_value);
892
893	return 0;
894}
895
896/*
897 * Return the count of the number of times a periodic timer expired
898 * while a notification was already pending. The counter is reset when
899 * a timer expires and a notification can be posted.
900 */
901int
902sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap, register_t *retval)
903{
904	/* {
905		syscallarg(timer_t) timerid;
906	} */
907	struct proc *p = l->l_proc;
908	int timerid;
909	struct ptimer *pt;
910
911	timerid = SCARG(uap, timerid);
912
913	if ((p->p_timers == NULL) ||
914	    (timerid < 2) || (timerid >= TIMER_MAX) ||
915	    ((pt = p->p_timers->pts_timers[timerid]) == NULL))
916		return (EINVAL);
917
918	*retval = pt->pt_poverruns;
919
920	return (0);
921}
922
923/*
924 * Real interval timer expired:
925 * send process whose timer expired an alarm signal.
926 * If time is not set up to reload, then just return.
927 * Else compute next time timer should go off which is > current time.
928 * This is where delay in processing this timeout causes multiple
929 * SIGALRM calls to be compressed into one.
930 */
931void
932realtimerexpire(void *arg)
933{
934	struct timeval now;
935	struct ptimer *pt;
936	int s;
937
938	pt = (struct ptimer *)arg;
939
940	itimerfire(pt);
941
942	if (!timerisset(&pt->pt_time.it_interval)) {
943		timerclear(&pt->pt_time.it_value);
944		return;
945	}
946	for (;;) {
947		s = splclock();	/* XXX need spl now? */
948		timeradd(&pt->pt_time.it_value,
949		    &pt->pt_time.it_interval, &pt->pt_time.it_value);
950		getmicrotime(&now);
951		if (timercmp(&pt->pt_time.it_value, &now, >)) {
952			/*
953			 * Don't need to check hzto() return value, here.
954			 * callout_reset() does it for us.
955			 */
956			callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
957			    realtimerexpire, pt);
958			splx(s);
959			return;
960		}
961		splx(s);
962		pt->pt_overruns++;
963	}
964}
965
966/* BSD routine to get the value of an interval timer. */
967/* ARGSUSED */
968int
969sys_getitimer(struct lwp *l, const struct sys_getitimer_args *uap, register_t *retval)
970{
971	/* {
972		syscallarg(int) which;
973		syscallarg(struct itimerval *) itv;
974	} */
975	struct proc *p = l->l_proc;
976	struct itimerval aitv;
977	int error;
978
979	error = dogetitimer(p, SCARG(uap, which), &aitv);
980	if (error)
981		return error;
982	return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
983}
984
985int
986dogetitimer(struct proc *p, int which, struct itimerval *itvp)
987{
988	int s;
989
990	if ((u_int)which > ITIMER_PROF)
991		return (EINVAL);
992
993	if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)){
994		timerclear(&itvp->it_value);
995		timerclear(&itvp->it_interval);
996	} else {
997		s = splclock();
998		timer_gettime(p->p_timers->pts_timers[which], itvp);
999		splx(s);
1000	}
1001
1002	return 0;
1003}
1004
1005/* BSD routine to set/arm an interval timer. */
1006/* ARGSUSED */
1007int
1008sys_setitimer(struct lwp *l, const struct sys_setitimer_args *uap, register_t *retval)
1009{
1010	/* {
1011		syscallarg(int) which;
1012		syscallarg(const struct itimerval *) itv;
1013		syscallarg(struct itimerval *) oitv;
1014	} */
1015	struct proc *p = l->l_proc;
1016	int which = SCARG(uap, which);
1017	struct sys_getitimer_args getargs;
1018	const struct itimerval *itvp;
1019	struct itimerval aitv;
1020	int error;
1021
1022	if ((u_int)which > ITIMER_PROF)
1023		return (EINVAL);
1024	itvp = SCARG(uap, itv);
1025	if (itvp &&
1026	    (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
1027		return (error);
1028	if (SCARG(uap, oitv) != NULL) {
1029		SCARG(&getargs, which) = which;
1030		SCARG(&getargs, itv) = SCARG(uap, oitv);
1031		if ((error = sys_getitimer(l, &getargs, retval)) != 0)
1032			return (error);
1033	}
1034	if (itvp == 0)
1035		return (0);
1036
1037	return dosetitimer(p, which, &aitv);
1038}
1039
1040int
1041dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1042{
1043	struct timeval now;
1044	struct ptimer *pt;
1045	int s;
1046
1047	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1048		return (EINVAL);
1049
1050	/*
1051	 * Don't bother allocating data structures if the process just
1052	 * wants to clear the timer.
1053	 */
1054	if (!timerisset(&itvp->it_value) &&
1055	    ((p->p_timers == NULL) ||(p->p_timers->pts_timers[which] == NULL)))
1056		return (0);
1057
1058	if (p->p_timers == NULL)
1059		timers_alloc(p);
1060	if (p->p_timers->pts_timers[which] == NULL) {
1061		pt = pool_get(&ptimer_pool, PR_WAITOK);
1062		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1063		pt->pt_ev.sigev_value.sival_int = which;
1064		pt->pt_overruns = 0;
1065		pt->pt_proc = p;
1066		pt->pt_type = which;
1067		pt->pt_entry = which;
1068		switch (which) {
1069		case ITIMER_REAL:
1070			callout_init(&pt->pt_ch, 0);
1071			pt->pt_ev.sigev_signo = SIGALRM;
1072			break;
1073		case ITIMER_VIRTUAL:
1074			pt->pt_active = 0;
1075			pt->pt_ev.sigev_signo = SIGVTALRM;
1076			break;
1077		case ITIMER_PROF:
1078			pt->pt_active = 0;
1079			pt->pt_ev.sigev_signo = SIGPROF;
1080			break;
1081		}
1082	} else
1083		pt = p->p_timers->pts_timers[which];
1084
1085	pt->pt_time = *itvp;
1086	p->p_timers->pts_timers[which] = pt;
1087
1088	s = splclock();
1089	if ((which == ITIMER_REAL) && timerisset(&pt->pt_time.it_value)) {
1090		/* Convert to absolute time */
1091		/* XXX need to wrap in splclock for timecounters case? */
1092		getmicrotime(&now);
1093		timeradd(&pt->pt_time.it_value, &now, &pt->pt_time.it_value);
1094	}
1095	timer_settime(pt);
1096	splx(s);
1097
1098	return (0);
1099}
1100
1101/* Utility routines to manage the array of pointers to timers. */
1102void
1103timers_alloc(struct proc *p)
1104{
1105	int i;
1106	struct ptimers *pts;
1107
1108	pts = pool_get(&ptimers_pool, PR_WAITOK);
1109	LIST_INIT(&pts->pts_virtual);
1110	LIST_INIT(&pts->pts_prof);
1111	for (i = 0; i < TIMER_MAX; i++)
1112		pts->pts_timers[i] = NULL;
1113	pts->pts_fired = 0;
1114	p->p_timers = pts;
1115}
1116
1117/*
1118 * Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1119 * then clean up all timers and free all the data structures. If
1120 * "which" is set to TIMERS_POSIX, only clean up the timers allocated
1121 * by timer_create(), not the BSD setitimer() timers, and only free the
1122 * structure if none of those remain.
1123 */
1124void
1125timers_free(struct proc *p, int which)
1126{
1127	int i, s;
1128	struct ptimers *pts;
1129	struct ptimer *pt, *ptn;
1130	struct timeval tv;
1131
1132	if (p->p_timers) {
1133		pts = p->p_timers;
1134		if (which == TIMERS_ALL)
1135			i = 0;
1136		else {
1137			s = splclock();
1138			timerclear(&tv);
1139			for (ptn = LIST_FIRST(&p->p_timers->pts_virtual);
1140			     ptn && ptn != pts->pts_timers[ITIMER_VIRTUAL];
1141			     ptn = LIST_NEXT(ptn, pt_list))
1142				timeradd(&tv, &ptn->pt_time.it_value, &tv);
1143			LIST_FIRST(&p->p_timers->pts_virtual) = NULL;
1144			if (ptn) {
1145				timeradd(&tv, &ptn->pt_time.it_value,
1146				    &ptn->pt_time.it_value);
1147				LIST_INSERT_HEAD(&p->p_timers->pts_virtual,
1148				    ptn, pt_list);
1149			}
1150
1151			timerclear(&tv);
1152			for (ptn = LIST_FIRST(&p->p_timers->pts_prof);
1153			     ptn && ptn != pts->pts_timers[ITIMER_PROF];
1154			     ptn = LIST_NEXT(ptn, pt_list))
1155				timeradd(&tv, &ptn->pt_time.it_value, &tv);
1156			LIST_FIRST(&p->p_timers->pts_prof) = NULL;
1157			if (ptn) {
1158				timeradd(&tv, &ptn->pt_time.it_value,
1159				    &ptn->pt_time.it_value);
1160				LIST_INSERT_HEAD(&p->p_timers->pts_prof, ptn,
1161				    pt_list);
1162			}
1163			splx(s);
1164			i = 3;
1165		}
1166		for ( ; i < TIMER_MAX; i++)
1167			if ((pt = pts->pts_timers[i]) != NULL) {
1168				if (pt->pt_type == CLOCK_REALTIME) {
1169					callout_stop(&pt->pt_ch);
1170					callout_destroy(&pt->pt_ch);
1171				}
1172				pts->pts_timers[i] = NULL;
1173				pool_put(&ptimer_pool, pt);
1174			}
1175		if ((pts->pts_timers[0] == NULL) &&
1176		    (pts->pts_timers[1] == NULL) &&
1177		    (pts->pts_timers[2] == NULL)) {
1178			p->p_timers = NULL;
1179			pool_put(&ptimers_pool, pts);
1180		}
1181	}
1182}
1183
1184/*
1185 * Decrement an interval timer by a specified number
1186 * of microseconds, which must be less than a second,
1187 * i.e. < 1000000.  If the timer expires, then reload
1188 * it.  In this case, carry over (usec - old value) to
1189 * reduce the value reloaded into the timer so that
1190 * the timer does not drift.  This routine assumes
1191 * that it is called in a context where the timers
1192 * on which it is operating cannot change in value.
1193 */
1194int
1195itimerdecr(struct ptimer *pt, int usec)
1196{
1197	struct itimerval *itp;
1198
1199	itp = &pt->pt_time;
1200	if (itp->it_value.tv_usec < usec) {
1201		if (itp->it_value.tv_sec == 0) {
1202			/* expired, and already in next interval */
1203			usec -= itp->it_value.tv_usec;
1204			goto expire;
1205		}
1206		itp->it_value.tv_usec += 1000000;
1207		itp->it_value.tv_sec--;
1208	}
1209	itp->it_value.tv_usec -= usec;
1210	usec = 0;
1211	if (timerisset(&itp->it_value))
1212		return (1);
1213	/* expired, exactly at end of interval */
1214expire:
1215	if (timerisset(&itp->it_interval)) {
1216		itp->it_value = itp->it_interval;
1217		itp->it_value.tv_usec -= usec;
1218		if (itp->it_value.tv_usec < 0) {
1219			itp->it_value.tv_usec += 1000000;
1220			itp->it_value.tv_sec--;
1221		}
1222		timer_settime(pt);
1223	} else
1224		itp->it_value.tv_usec = 0;		/* sec is already 0 */
1225	return (0);
1226}
1227
1228void
1229itimerfire(struct ptimer *pt)
1230{
1231	struct proc *p = pt->pt_proc;
1232
1233	if (pt->pt_ev.sigev_notify == SIGEV_SIGNAL) {
1234		/*
1235		 * No RT signal infrastructure exists at this time;
1236		 * just post the signal number and throw away the
1237		 * value.
1238		 */
1239		if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo))
1240			pt->pt_overruns++;
1241		else {
1242			ksiginfo_t ksi;
1243			KSI_INIT(&ksi);
1244			ksi.ksi_signo = pt->pt_ev.sigev_signo;
1245			ksi.ksi_code = SI_TIMER;
1246			ksi.ksi_value = pt->pt_ev.sigev_value;
1247			pt->pt_poverruns = pt->pt_overruns;
1248			pt->pt_overruns = 0;
1249			mutex_enter(&proclist_mutex);
1250			kpsignal(p, &ksi, NULL);
1251			mutex_exit(&proclist_mutex);
1252		}
1253	}
1254}
1255
1256/*
1257 * ratecheck(): simple time-based rate-limit checking.  see ratecheck(9)
1258 * for usage and rationale.
1259 */
1260int
1261ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1262{
1263	struct timeval tv, delta;
1264	int rv = 0;
1265
1266	getmicrouptime(&tv);
1267	timersub(&tv, lasttime, &delta);
1268
1269	/*
1270	 * check for 0,0 is so that the message will be seen at least once,
1271	 * even if interval is huge.
1272	 */
1273	if (timercmp(&delta, mininterval, >=) ||
1274	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1275		*lasttime = tv;
1276		rv = 1;
1277	}
1278
1279	return (rv);
1280}
1281
1282/*
1283 * ppsratecheck(): packets (or events) per second limitation.
1284 */
1285int
1286ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1287{
1288	struct timeval tv, delta;
1289	int rv;
1290
1291	getmicrouptime(&tv);
1292	timersub(&tv, lasttime, &delta);
1293
1294	/*
1295	 * check for 0,0 is so that the message will be seen at least once.
1296	 * if more than one second have passed since the last update of
1297	 * lasttime, reset the counter.
1298	 *
1299	 * we do increment *curpps even in *curpps < maxpps case, as some may
1300	 * try to use *curpps for stat purposes as well.
1301	 */
1302	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
1303	    delta.tv_sec >= 1) {
1304		*lasttime = tv;
1305		*curpps = 0;
1306	}
1307	if (maxpps < 0)
1308		rv = 1;
1309	else if (*curpps < maxpps)
1310		rv = 1;
1311	else
1312		rv = 0;
1313
1314#if 1 /*DIAGNOSTIC?*/
1315	/* be careful about wrap-around */
1316	if (*curpps + 1 > *curpps)
1317		*curpps = *curpps + 1;
1318#else
1319	/*
1320	 * assume that there's not too many calls to this function.
1321	 * not sure if the assumption holds, as it depends on *caller's*
1322	 * behavior, not the behavior of this function.
1323	 * IMHO it is wrong to make assumption on the caller's behavior,
1324	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
1325	 */
1326	*curpps = *curpps + 1;
1327#endif
1328
1329	return (rv);
1330}
1331