xref: /titanic_51/usr/src/uts/common/syscall/lwp_timer.c (revision 51b32bdd07bc63a6e416c5759f0f445147703107)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f635d46aSqiao  * Common Development and Distribution License (the "License").
6f635d46aSqiao  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21fd6545c7Sraf 
227c478bd9Sstevel@tonic-gate /*
23*51b32bddSMadhavan Venkataraman  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/proc.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/debug.h>
307c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
31fd6545c7Sraf #include <sys/atomic.h>
327c478bd9Sstevel@tonic-gate #include <sys/timer.h>
337c478bd9Sstevel@tonic-gate #include <sys/lwp_timer_impl.h>
3487a18d3fSMadhavan Venkataraman #include <sys/callo.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * lwp_timer_timeout() is called from a timeout set up in lwp_cond_wait(),
387c478bd9Sstevel@tonic-gate  * lwp_mutex_timedlock(), lwp_sema_timedwait() or lwp_rwlock_lock().
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * It recomputes the time remaining until the absolute time when the
417c478bd9Sstevel@tonic-gate  * wait is supposed to timeout and either calls realtime_timeout()
427c478bd9Sstevel@tonic-gate  * to reschedule itself or calls setrun() on the sleeping thread.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * This is done to ensure that the waiting thread does not wake up
457c478bd9Sstevel@tonic-gate  * due to timer expiration until the absolute future time of the
467c478bd9Sstevel@tonic-gate  * timeout has been reached.  Until that time, the thread must
477c478bd9Sstevel@tonic-gate  * remain on its sleep queue.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * An lwp_timer_t structure is used to pass information
507c478bd9Sstevel@tonic-gate  * about the sleeping thread to the timeout function.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static void
547c478bd9Sstevel@tonic-gate lwp_timer_timeout(void *arg)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	lwp_timer_t *lwptp = arg;
577c478bd9Sstevel@tonic-gate 	kthread_t *t = lwptp->lwpt_thread;
58*51b32bddSMadhavan Venkataraman 	timespec_t now, delta;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	mutex_enter(&t->t_delay_lock);
617c478bd9Sstevel@tonic-gate 	gethrestime(&now);
627c478bd9Sstevel@tonic-gate 	/*
633348528fSdm120769 	 * Requeue the timeout if no one has reset the system time
643348528fSdm120769 	 * and if the absolute future time has not been reached.
657c478bd9Sstevel@tonic-gate 	 */
663348528fSdm120769 	if (lwptp->lwpt_timecheck == timechanged &&
677c478bd9Sstevel@tonic-gate 	    (lwptp->lwpt_rqtime.tv_sec > now.tv_sec ||
687c478bd9Sstevel@tonic-gate 	    (lwptp->lwpt_rqtime.tv_sec == now.tv_sec &&
697c478bd9Sstevel@tonic-gate 	    lwptp->lwpt_rqtime.tv_nsec > now.tv_nsec))) {
70fd6545c7Sraf 		lwptp->lwpt_imm_timeout = 0;
71*51b32bddSMadhavan Venkataraman 		delta = lwptp->lwpt_rqtime;
72*51b32bddSMadhavan Venkataraman 		timespecsub(&delta, &now);
7387a18d3fSMadhavan Venkataraman 		lwptp->lwpt_id = timeout_generic(CALLOUT_REALTIME,
74*51b32bddSMadhavan Venkataraman 		    lwp_timer_timeout, lwptp, ts2hrt(&delta), nsec_per_tick,
75*51b32bddSMadhavan Venkataraman 		    (CALLOUT_FLAG_HRESTIME | CALLOUT_FLAG_ROUNDUP));
767c478bd9Sstevel@tonic-gate 	} else {
777c478bd9Sstevel@tonic-gate 		/*
787c478bd9Sstevel@tonic-gate 		 * Set the thread running only if it is asleep on
797c478bd9Sstevel@tonic-gate 		 * its lwpchan sleep queue (not if it is asleep on
807c478bd9Sstevel@tonic-gate 		 * the t_delay_lock mutex).
817c478bd9Sstevel@tonic-gate 		 */
827c478bd9Sstevel@tonic-gate 		thread_lock(t);
83fd6545c7Sraf 		/* do this for the benefit of upi mutexes */
84fd6545c7Sraf 		(void) atomic_cas_uint(&lwptp->lwpt_imm_timeout, 0, 1);
857c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_SLEEP &&
867c478bd9Sstevel@tonic-gate 		    (t->t_flag & T_WAKEABLE) &&
877c478bd9Sstevel@tonic-gate 		    t->t_wchan0 != NULL)
887c478bd9Sstevel@tonic-gate 			setrun_locked(t);
897c478bd9Sstevel@tonic-gate 		thread_unlock(t);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	mutex_exit(&t->t_delay_lock);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate int
957c478bd9Sstevel@tonic-gate lwp_timer_copyin(lwp_timer_t *lwptp, timespec_t *tsp)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	timespec_t now;
987c478bd9Sstevel@tonic-gate 	int error = 0;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (tsp == NULL)	/* not really an error, just need to bzero() */
1017c478bd9Sstevel@tonic-gate 		goto err;
1023348528fSdm120769 	lwptp->lwpt_timecheck = timechanged; /* do this before gethrestime() */
1037c478bd9Sstevel@tonic-gate 	gethrestime(&now);		/* do this before copyin() */
1047c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
1057c478bd9Sstevel@tonic-gate 		if (copyin(tsp, &lwptp->lwpt_rqtime, sizeof (timespec_t))) {
1067c478bd9Sstevel@tonic-gate 			error = EFAULT;
1077c478bd9Sstevel@tonic-gate 			goto err;
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 	} else {
1107c478bd9Sstevel@tonic-gate 		timespec32_t ts32;
1117c478bd9Sstevel@tonic-gate 		if (copyin(tsp, &ts32, sizeof (timespec32_t))) {
1127c478bd9Sstevel@tonic-gate 			error = EFAULT;
1137c478bd9Sstevel@tonic-gate 			goto err;
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 		TIMESPEC32_TO_TIMESPEC(&lwptp->lwpt_rqtime, &ts32);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	if (itimerspecfix(&lwptp->lwpt_rqtime)) {
1187c478bd9Sstevel@tonic-gate 		error = EINVAL;
1197c478bd9Sstevel@tonic-gate 		goto err;
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * Unless the requested timeout is zero,
1237c478bd9Sstevel@tonic-gate 	 * get the precise future (absolute) time at
1247c478bd9Sstevel@tonic-gate 	 * which we are to time out and return ETIME.
1257c478bd9Sstevel@tonic-gate 	 * We must not return ETIME before that time.
1267c478bd9Sstevel@tonic-gate 	 */
1277c478bd9Sstevel@tonic-gate 	if (lwptp->lwpt_rqtime.tv_sec == 0 && lwptp->lwpt_rqtime.tv_nsec == 0) {
1287c478bd9Sstevel@tonic-gate 		bzero(lwptp, sizeof (lwp_timer_t));
1297c478bd9Sstevel@tonic-gate 		lwptp->lwpt_imm_timeout = 1;
1307c478bd9Sstevel@tonic-gate 	} else {
1317c478bd9Sstevel@tonic-gate 		lwptp->lwpt_thread = curthread;
1327c478bd9Sstevel@tonic-gate 		lwptp->lwpt_tsp = tsp;
1337c478bd9Sstevel@tonic-gate 		lwptp->lwpt_time_error = 0;
1347c478bd9Sstevel@tonic-gate 		lwptp->lwpt_id = 0;
1357c478bd9Sstevel@tonic-gate 		lwptp->lwpt_imm_timeout = 0;
1367c478bd9Sstevel@tonic-gate 		timespecadd(&lwptp->lwpt_rqtime, &now);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	return (0);
1397c478bd9Sstevel@tonic-gate err:
1407c478bd9Sstevel@tonic-gate 	bzero(lwptp, sizeof (lwp_timer_t));
1417c478bd9Sstevel@tonic-gate 	lwptp->lwpt_time_error = error;
1427c478bd9Sstevel@tonic-gate 	return (error);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate int
1467c478bd9Sstevel@tonic-gate lwp_timer_enqueue(lwp_timer_t *lwptp)
1477c478bd9Sstevel@tonic-gate {
148*51b32bddSMadhavan Venkataraman 	timespec_t now, delta;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	ASSERT(lwptp->lwpt_thread == curthread);
1517c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&curthread->t_delay_lock));
1527c478bd9Sstevel@tonic-gate 	gethrestime(&now);
1533348528fSdm120769 	if (lwptp->lwpt_timecheck == timechanged &&
1547c478bd9Sstevel@tonic-gate 	    (lwptp->lwpt_rqtime.tv_sec > now.tv_sec ||
1557c478bd9Sstevel@tonic-gate 	    (lwptp->lwpt_rqtime.tv_sec == now.tv_sec &&
1567c478bd9Sstevel@tonic-gate 	    lwptp->lwpt_rqtime.tv_nsec > now.tv_nsec))) {
1577c478bd9Sstevel@tonic-gate 		/*
1587c478bd9Sstevel@tonic-gate 		 * Queue the timeout.
1597c478bd9Sstevel@tonic-gate 		 */
160fd6545c7Sraf 		lwptp->lwpt_imm_timeout = 0;
161*51b32bddSMadhavan Venkataraman 		delta = lwptp->lwpt_rqtime;
162*51b32bddSMadhavan Venkataraman 		timespecsub(&delta, &now);
16387a18d3fSMadhavan Venkataraman 		lwptp->lwpt_id = timeout_generic(CALLOUT_REALTIME,
164*51b32bddSMadhavan Venkataraman 		    lwp_timer_timeout, lwptp, ts2hrt(&delta), nsec_per_tick,
165*51b32bddSMadhavan Venkataraman 		    (CALLOUT_FLAG_HRESTIME | CALLOUT_FLAG_ROUNDUP));
1667c478bd9Sstevel@tonic-gate 		return (0);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Time has already run out or someone reset the system time;
1717c478bd9Sstevel@tonic-gate 	 * just cause an immediate timeout.
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate 	lwptp->lwpt_imm_timeout = 1;
1747c478bd9Sstevel@tonic-gate 	return (1);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate clock_t
1787c478bd9Sstevel@tonic-gate lwp_timer_dequeue(lwp_timer_t *lwptp)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1817c478bd9Sstevel@tonic-gate 	clock_t tim = -1;
18287a18d3fSMadhavan Venkataraman 	callout_id_t tmp_id;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	mutex_enter(&t->t_delay_lock);
1857c478bd9Sstevel@tonic-gate 	while ((tmp_id = lwptp->lwpt_id) != 0) {
1867c478bd9Sstevel@tonic-gate 		lwptp->lwpt_id = 0;
1877c478bd9Sstevel@tonic-gate 		mutex_exit(&t->t_delay_lock);
18887a18d3fSMadhavan Venkataraman 		tim = untimeout_default(tmp_id, 0);
1897c478bd9Sstevel@tonic-gate 		mutex_enter(&t->t_delay_lock);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	mutex_exit(&t->t_delay_lock);
1927c478bd9Sstevel@tonic-gate 	return (tim);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate int
1967c478bd9Sstevel@tonic-gate lwp_timer_copyout(lwp_timer_t *lwptp, int error)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	timespec_t rmtime;
1997c478bd9Sstevel@tonic-gate 	timespec_t now;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (lwptp->lwpt_tsp == NULL)	/* nothing to do */
2027c478bd9Sstevel@tonic-gate 		return (error);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	rmtime.tv_sec = rmtime.tv_nsec = 0;
2057c478bd9Sstevel@tonic-gate 	if (error != ETIME) {
2067c478bd9Sstevel@tonic-gate 		gethrestime(&now);
2077c478bd9Sstevel@tonic-gate 		if ((now.tv_sec < lwptp->lwpt_rqtime.tv_sec) ||
2087c478bd9Sstevel@tonic-gate 		    ((now.tv_sec == lwptp->lwpt_rqtime.tv_sec) &&
2097c478bd9Sstevel@tonic-gate 		    (now.tv_nsec < lwptp->lwpt_rqtime.tv_nsec))) {
2107c478bd9Sstevel@tonic-gate 			rmtime = lwptp->lwpt_rqtime;
2117c478bd9Sstevel@tonic-gate 			timespecsub(&rmtime, &now);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
2157c478bd9Sstevel@tonic-gate 		if (copyout(&rmtime, lwptp->lwpt_tsp, sizeof (timespec_t)))
2167c478bd9Sstevel@tonic-gate 			error = EFAULT;
2177c478bd9Sstevel@tonic-gate 	} else {
2187c478bd9Sstevel@tonic-gate 		timespec32_t rmtime32;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime);
2217c478bd9Sstevel@tonic-gate 		if (copyout(&rmtime32, lwptp->lwpt_tsp, sizeof (timespec32_t)))
2227c478bd9Sstevel@tonic-gate 			error = EFAULT;
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	return (error);
2267c478bd9Sstevel@tonic-gate }
227