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
534709573Sraf * Common Development and Distribution License (the "License").
634709573Sraf * 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 */
2134709573Sraf
227c478bd9Sstevel@tonic-gate /*
238b79e857Spraks * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*13a42676SBryan Cantrill /*
28*13a42676SBryan Cantrill * Copyright (c) 2012, Joyent, Inc. All rights reserved.
29*13a42676SBryan Cantrill */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <sys/timer.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/policy.h>
378b79e857Spraks #include <sys/port_impl.h>
387c478bd9Sstevel@tonic-gate #include <sys/port_kernel.h>
397c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate static kmem_cache_t *clock_timer_cache;
427c478bd9Sstevel@tonic-gate static clock_backend_t *clock_backend[CLOCK_MAX];
437c478bd9Sstevel@tonic-gate static int timer_port_callback(void *, int *, pid_t, int, void *);
447c478bd9Sstevel@tonic-gate static void timer_close_port(void *, int, pid_t, int);
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #define CLOCK_BACKEND(clk) \
477c478bd9Sstevel@tonic-gate ((clk) < CLOCK_MAX && (clk) >= 0 ? clock_backend[(clk)] : NULL)
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate * Tunable to increase the maximum number of POSIX timers per-process. This
517c478bd9Sstevel@tonic-gate * may _only_ be tuned in /etc/system or by patching the kernel binary; it
527c478bd9Sstevel@tonic-gate * _cannot_ be tuned on a running system.
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate int timer_max = _TIMER_MAX;
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate * timer_lock() locks the specified interval timer. It doesn't look at the
587c478bd9Sstevel@tonic-gate * ITLK_REMOVE bit; it's up to callers to look at this if they need to
597c478bd9Sstevel@tonic-gate * care. p_lock must be held on entry; it may be dropped and reaquired,
607c478bd9Sstevel@tonic-gate * but timer_lock() will always return with p_lock held.
617c478bd9Sstevel@tonic-gate *
627c478bd9Sstevel@tonic-gate * Note that timer_create() doesn't call timer_lock(); it creates timers
637c478bd9Sstevel@tonic-gate * with the ITLK_LOCKED bit explictly set.
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate static void
timer_lock(proc_t * p,itimer_t * it)667c478bd9Sstevel@tonic-gate timer_lock(proc_t *p, itimer_t *it)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate while (it->it_lock & ITLK_LOCKED) {
717c478bd9Sstevel@tonic-gate it->it_blockers++;
727c478bd9Sstevel@tonic-gate cv_wait(&it->it_cv, &p->p_lock);
737c478bd9Sstevel@tonic-gate it->it_blockers--;
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate it->it_lock |= ITLK_LOCKED;
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * timer_unlock() unlocks the specified interval timer, waking up any
817c478bd9Sstevel@tonic-gate * waiters. p_lock must be held on entry; it will not be dropped by
827c478bd9Sstevel@tonic-gate * timer_unlock().
837c478bd9Sstevel@tonic-gate */
847c478bd9Sstevel@tonic-gate static void
timer_unlock(proc_t * p,itimer_t * it)857c478bd9Sstevel@tonic-gate timer_unlock(proc_t *p, itimer_t *it)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
887c478bd9Sstevel@tonic-gate ASSERT(it->it_lock & ITLK_LOCKED);
897c478bd9Sstevel@tonic-gate it->it_lock &= ~ITLK_LOCKED;
907c478bd9Sstevel@tonic-gate cv_signal(&it->it_cv);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate * timer_delete_locked() takes a proc pointer, timer ID and locked interval
957c478bd9Sstevel@tonic-gate * timer, and deletes the specified timer. It must be called with p_lock
967c478bd9Sstevel@tonic-gate * held, and cannot be called on a timer which already has ITLK_REMOVE set;
977c478bd9Sstevel@tonic-gate * the caller must check this. timer_delete_locked() will set the ITLK_REMOVE
987c478bd9Sstevel@tonic-gate * bit and will iteratively unlock and lock the interval timer until all
997c478bd9Sstevel@tonic-gate * blockers have seen the ITLK_REMOVE and cleared out. It will then zero
1007c478bd9Sstevel@tonic-gate * out the specified entry in the p_itimer array, and call into the clock
1017c478bd9Sstevel@tonic-gate * backend to complete the deletion.
1027c478bd9Sstevel@tonic-gate *
1037c478bd9Sstevel@tonic-gate * This function will always return with p_lock held.
1047c478bd9Sstevel@tonic-gate */
1057c478bd9Sstevel@tonic-gate static void
timer_delete_locked(proc_t * p,timer_t tid,itimer_t * it)1067c478bd9Sstevel@tonic-gate timer_delete_locked(proc_t *p, timer_t tid, itimer_t *it)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
1097c478bd9Sstevel@tonic-gate ASSERT(!(it->it_lock & ITLK_REMOVE));
1107c478bd9Sstevel@tonic-gate ASSERT(it->it_lock & ITLK_LOCKED);
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate it->it_lock |= ITLK_REMOVE;
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate * If there are threads waiting to lock this timer, we'll unlock
1167c478bd9Sstevel@tonic-gate * the timer, and block on the cv. Threads blocking our removal will
1177c478bd9Sstevel@tonic-gate * have the opportunity to run; when they see the ITLK_REMOVE flag
1187c478bd9Sstevel@tonic-gate * set, they will immediately unlock the timer.
1197c478bd9Sstevel@tonic-gate */
1207c478bd9Sstevel@tonic-gate while (it->it_blockers) {
1217c478bd9Sstevel@tonic-gate timer_unlock(p, it);
1227c478bd9Sstevel@tonic-gate cv_wait(&it->it_cv, &p->p_lock);
1237c478bd9Sstevel@tonic-gate timer_lock(p, it);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate ASSERT(p->p_itimer[tid] == it);
1277c478bd9Sstevel@tonic-gate p->p_itimer[tid] = NULL;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate * No one is blocked on this timer, and no one will be (we've set
1317c478bd9Sstevel@tonic-gate * p_itimer[tid] to be NULL; no one can find it). Now we call into
1327c478bd9Sstevel@tonic-gate * the clock backend to delete the timer; it is up to the backend to
1337c478bd9Sstevel@tonic-gate * guarantee that timer_fire() has completed (and will never again
1347c478bd9Sstevel@tonic-gate * be called) for this timer.
1357c478bd9Sstevel@tonic-gate */
1367c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate it->it_backend->clk_timer_delete(it);
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate if (it->it_portev) {
1417c478bd9Sstevel@tonic-gate mutex_enter(&it->it_mutex);
1427c478bd9Sstevel@tonic-gate if (it->it_portev) {
1438b79e857Spraks port_kevent_t *pev;
1447c478bd9Sstevel@tonic-gate /* dissociate timer from the event port */
1457c478bd9Sstevel@tonic-gate (void) port_dissociate_ksource(it->it_portfd,
1467c478bd9Sstevel@tonic-gate PORT_SOURCE_TIMER, (port_source_t *)it->it_portsrc);
1478b79e857Spraks pev = (port_kevent_t *)it->it_portev;
1487c478bd9Sstevel@tonic-gate it->it_portev = NULL;
1497c478bd9Sstevel@tonic-gate it->it_flags &= ~IT_PORT;
1507c478bd9Sstevel@tonic-gate it->it_pending = 0;
1517c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex);
1528b79e857Spraks (void) port_remove_done_event(pev);
1538b79e857Spraks port_free_event(pev);
1548b79e857Spraks } else {
1558b79e857Spraks mutex_exit(&it->it_mutex);
1568b79e857Spraks }
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate * We need to be careful freeing the sigqueue for this timer;
1637c478bd9Sstevel@tonic-gate * if a signal is pending, the sigqueue needs to be freed
1647c478bd9Sstevel@tonic-gate * synchronously in siginfofree(). The need to free the sigqueue
1657c478bd9Sstevel@tonic-gate * in siginfofree() is indicated by setting sq_func to NULL.
1667c478bd9Sstevel@tonic-gate */
1677c478bd9Sstevel@tonic-gate if (it->it_pending > 0) {
1687c478bd9Sstevel@tonic-gate it->it_sigq->sq_func = NULL;
1697c478bd9Sstevel@tonic-gate } else {
1707c478bd9Sstevel@tonic-gate kmem_free(it->it_sigq, sizeof (sigqueue_t));
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate ASSERT(it->it_blockers == 0);
1747c478bd9Sstevel@tonic-gate kmem_cache_free(clock_timer_cache, it);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate * timer_grab() and its companion routine, timer_release(), are wrappers
1797c478bd9Sstevel@tonic-gate * around timer_lock()/_unlock() which allow the timer_*(3R) routines to
1807c478bd9Sstevel@tonic-gate * (a) share error handling code and (b) not grab p_lock themselves. Routines
1817c478bd9Sstevel@tonic-gate * which are called with p_lock held (e.g. timer_lwpbind(), timer_lwpexit())
1827c478bd9Sstevel@tonic-gate * must call timer_lock()/_unlock() explictly.
1837c478bd9Sstevel@tonic-gate *
1847c478bd9Sstevel@tonic-gate * timer_grab() takes a proc and a timer ID, and returns a pointer to a
1857c478bd9Sstevel@tonic-gate * locked interval timer. p_lock must _not_ be held on entry; timer_grab()
1867c478bd9Sstevel@tonic-gate * may acquire p_lock, but will always return with p_lock dropped.
1877c478bd9Sstevel@tonic-gate *
1887c478bd9Sstevel@tonic-gate * If timer_grab() fails, it will return NULL. timer_grab() will fail if
1897c478bd9Sstevel@tonic-gate * one or more of the following is true:
1907c478bd9Sstevel@tonic-gate *
1917c478bd9Sstevel@tonic-gate * (a) The specified timer ID is out of range.
1927c478bd9Sstevel@tonic-gate *
1937c478bd9Sstevel@tonic-gate * (b) The specified timer ID does not correspond to a timer ID returned
1947c478bd9Sstevel@tonic-gate * from timer_create(3R).
1957c478bd9Sstevel@tonic-gate *
1967c478bd9Sstevel@tonic-gate * (c) The specified timer ID is currently being removed.
1977c478bd9Sstevel@tonic-gate *
1987c478bd9Sstevel@tonic-gate */
1997c478bd9Sstevel@tonic-gate static itimer_t *
timer_grab(proc_t * p,timer_t tid)2007c478bd9Sstevel@tonic-gate timer_grab(proc_t *p, timer_t tid)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate itimer_t **itp, *it;
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate if (tid >= timer_max || tid < 0)
2057c478bd9Sstevel@tonic-gate return (NULL);
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate if ((itp = p->p_itimer) == NULL || (it = itp[tid]) == NULL) {
2107c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2117c478bd9Sstevel@tonic-gate return (NULL);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate timer_lock(p, it);
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate if (it->it_lock & ITLK_REMOVE) {
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate * Someone is removing this timer; it will soon be invalid.
2197c478bd9Sstevel@tonic-gate */
2207c478bd9Sstevel@tonic-gate timer_unlock(p, it);
2217c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2227c478bd9Sstevel@tonic-gate return (NULL);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate return (it);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate * timer_release() releases a timer acquired with timer_grab(). p_lock
2327c478bd9Sstevel@tonic-gate * should not be held on entry; timer_release() will acquire p_lock but
2337c478bd9Sstevel@tonic-gate * will drop it before returning.
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate static void
timer_release(proc_t * p,itimer_t * it)2367c478bd9Sstevel@tonic-gate timer_release(proc_t *p, itimer_t *it)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
2397c478bd9Sstevel@tonic-gate timer_unlock(p, it);
2407c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate * timer_delete_grabbed() deletes a timer acquired with timer_grab().
2457c478bd9Sstevel@tonic-gate * p_lock should not be held on entry; timer_delete_grabbed() will acquire
2467c478bd9Sstevel@tonic-gate * p_lock, but will drop it before returning.
2477c478bd9Sstevel@tonic-gate */
2487c478bd9Sstevel@tonic-gate static void
timer_delete_grabbed(proc_t * p,timer_t tid,itimer_t * it)2497c478bd9Sstevel@tonic-gate timer_delete_grabbed(proc_t *p, timer_t tid, itimer_t *it)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
2527c478bd9Sstevel@tonic-gate timer_delete_locked(p, tid, it);
2537c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate void
clock_timer_init()2577c478bd9Sstevel@tonic-gate clock_timer_init()
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate clock_timer_cache = kmem_cache_create("timer_cache",
2607c478bd9Sstevel@tonic-gate sizeof (itimer_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate void
clock_add_backend(clockid_t clock,clock_backend_t * backend)2647c478bd9Sstevel@tonic-gate clock_add_backend(clockid_t clock, clock_backend_t *backend)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate ASSERT(clock >= 0 && clock < CLOCK_MAX);
2677c478bd9Sstevel@tonic-gate ASSERT(clock_backend[clock] == NULL);
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate clock_backend[clock] = backend;
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate int
clock_settime(clockid_t clock,timespec_t * tp)2737c478bd9Sstevel@tonic-gate clock_settime(clockid_t clock, timespec_t *tp)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate timespec_t t;
2767c478bd9Sstevel@tonic-gate clock_backend_t *backend;
2777c478bd9Sstevel@tonic-gate int error;
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate if ((backend = CLOCK_BACKEND(clock)) == NULL)
2807c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate if (secpolicy_settime(CRED()) != 0)
2837c478bd9Sstevel@tonic-gate return (set_errno(EPERM));
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
2867c478bd9Sstevel@tonic-gate if (copyin(tp, &t, sizeof (timespec_t)) != 0)
2877c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
2887c478bd9Sstevel@tonic-gate } else {
2897c478bd9Sstevel@tonic-gate timespec32_t t32;
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate if (copyin(tp, &t32, sizeof (timespec32_t)) != 0)
2927c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate TIMESPEC32_TO_TIMESPEC(&t, &t32);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate if (itimerspecfix(&t))
2987c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate error = backend->clk_clock_settime(&t);
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate if (error)
3037c478bd9Sstevel@tonic-gate return (set_errno(error));
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate return (0);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate int
clock_gettime(clockid_t clock,timespec_t * tp)3097c478bd9Sstevel@tonic-gate clock_gettime(clockid_t clock, timespec_t *tp)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate timespec_t t;
3127c478bd9Sstevel@tonic-gate clock_backend_t *backend;
3137c478bd9Sstevel@tonic-gate int error;
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate if ((backend = CLOCK_BACKEND(clock)) == NULL)
3167c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate error = backend->clk_clock_gettime(&t);
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate if (error)
3217c478bd9Sstevel@tonic-gate return (set_errno(error));
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
3247c478bd9Sstevel@tonic-gate if (copyout(&t, tp, sizeof (timespec_t)) != 0)
3257c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
3267c478bd9Sstevel@tonic-gate } else {
3277c478bd9Sstevel@tonic-gate timespec32_t t32;
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate if (TIMESPEC_OVERFLOW(&t))
3307c478bd9Sstevel@tonic-gate return (set_errno(EOVERFLOW));
3317c478bd9Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(&t32, &t);
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate if (copyout(&t32, tp, sizeof (timespec32_t)) != 0)
3347c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate return (0);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate int
clock_getres(clockid_t clock,timespec_t * tp)3417c478bd9Sstevel@tonic-gate clock_getres(clockid_t clock, timespec_t *tp)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate timespec_t t;
3447c478bd9Sstevel@tonic-gate clock_backend_t *backend;
3457c478bd9Sstevel@tonic-gate int error;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate * Strangely, the standard defines clock_getres() with a NULL tp
3497c478bd9Sstevel@tonic-gate * to do nothing (regardless of the validity of the specified
3507c478bd9Sstevel@tonic-gate * clock_id). Go figure.
3517c478bd9Sstevel@tonic-gate */
3527c478bd9Sstevel@tonic-gate if (tp == NULL)
3537c478bd9Sstevel@tonic-gate return (0);
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate if ((backend = CLOCK_BACKEND(clock)) == NULL)
3567c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate error = backend->clk_clock_getres(&t);
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate if (error)
3617c478bd9Sstevel@tonic-gate return (set_errno(error));
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
3647c478bd9Sstevel@tonic-gate if (copyout(&t, tp, sizeof (timespec_t)) != 0)
3657c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
3667c478bd9Sstevel@tonic-gate } else {
3677c478bd9Sstevel@tonic-gate timespec32_t t32;
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate if (TIMESPEC_OVERFLOW(&t))
3707c478bd9Sstevel@tonic-gate return (set_errno(EOVERFLOW));
3717c478bd9Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(&t32, &t);
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate if (copyout(&t32, tp, sizeof (timespec32_t)) != 0)
3747c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate return (0);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate void
timer_signal(sigqueue_t * sigq)3817c478bd9Sstevel@tonic-gate timer_signal(sigqueue_t *sigq)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate itimer_t *it = (itimer_t *)sigq->sq_backptr;
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate * There are some conditions during a fork or an exit when we can
3877c478bd9Sstevel@tonic-gate * call siginfofree() without p_lock held. To prevent a race
3887c478bd9Sstevel@tonic-gate * between timer_signal() and timer_fire() with regard to it_pending,
3897c478bd9Sstevel@tonic-gate * we therefore acquire it_mutex in both paths.
3907c478bd9Sstevel@tonic-gate */
3917c478bd9Sstevel@tonic-gate mutex_enter(&it->it_mutex);
3927c478bd9Sstevel@tonic-gate ASSERT(it->it_pending > 0);
3937c478bd9Sstevel@tonic-gate it->it_overrun = it->it_pending - 1;
3947c478bd9Sstevel@tonic-gate it->it_pending = 0;
3957c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate * This routine is called from the clock backend.
4007c478bd9Sstevel@tonic-gate */
4017c478bd9Sstevel@tonic-gate void
timer_fire(itimer_t * it)4027c478bd9Sstevel@tonic-gate timer_fire(itimer_t *it)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate proc_t *p;
4057c478bd9Sstevel@tonic-gate int proc_lock_held;
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate if (it->it_flags & IT_SIGNAL) {
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate * See the comment in timer_signal() for why it is not
4107c478bd9Sstevel@tonic-gate * sufficient to only grab p_lock here. Because p_lock can be
4117c478bd9Sstevel@tonic-gate * held on entry to timer_signal(), the lock ordering is
4127c478bd9Sstevel@tonic-gate * necessarily p_lock before it_mutex.
4137c478bd9Sstevel@tonic-gate */
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate p = it->it_proc;
4167c478bd9Sstevel@tonic-gate proc_lock_held = 1;
4177c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
4187c478bd9Sstevel@tonic-gate } else {
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate * IT_PORT:
4217c478bd9Sstevel@tonic-gate * If a timer was ever programmed to send events to a port,
4227c478bd9Sstevel@tonic-gate * the IT_PORT flag will remain set until:
4237c478bd9Sstevel@tonic-gate * a) the timer is deleted (see timer_delete_locked()) or
4247c478bd9Sstevel@tonic-gate * b) the port is being closed (see timer_close_port()).
4257c478bd9Sstevel@tonic-gate * Both cases are synchronized with the it_mutex.
4267c478bd9Sstevel@tonic-gate * We don't need to use the p_lock because it is only
4277c478bd9Sstevel@tonic-gate * required in the IT_SIGNAL case.
4287c478bd9Sstevel@tonic-gate * If IT_PORT was set and the port is being closed then
4297c478bd9Sstevel@tonic-gate * the timer notification is set to NONE. In such a case
4307c478bd9Sstevel@tonic-gate * the timer itself and the it_pending counter remain active
4317c478bd9Sstevel@tonic-gate * until the application deletes the counter or the process
4327c478bd9Sstevel@tonic-gate * exits.
4337c478bd9Sstevel@tonic-gate */
4347c478bd9Sstevel@tonic-gate proc_lock_held = 0;
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate mutex_enter(&it->it_mutex);
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate if (it->it_pending > 0) {
4397c478bd9Sstevel@tonic-gate if (it->it_pending < INT_MAX)
4407c478bd9Sstevel@tonic-gate it->it_pending++;
4417c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex);
4427c478bd9Sstevel@tonic-gate } else {
4437c478bd9Sstevel@tonic-gate if (it->it_flags & IT_PORT) {
4447c478bd9Sstevel@tonic-gate it->it_pending = 1;
44534709573Sraf port_send_event((port_kevent_t *)it->it_portev);
4467c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex);
4477c478bd9Sstevel@tonic-gate } else if (it->it_flags & IT_SIGNAL) {
4487c478bd9Sstevel@tonic-gate it->it_pending = 1;
4497c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex);
4507c478bd9Sstevel@tonic-gate sigaddqa(p, NULL, it->it_sigq);
4517c478bd9Sstevel@tonic-gate } else {
4527c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate if (proc_lock_held)
4577c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate int
timer_create(clockid_t clock,struct sigevent * evp,timer_t * tid)4617c478bd9Sstevel@tonic-gate timer_create(clockid_t clock, struct sigevent *evp, timer_t *tid)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate struct sigevent ev;
4647c478bd9Sstevel@tonic-gate proc_t *p = curproc;
4657c478bd9Sstevel@tonic-gate clock_backend_t *backend;
4667c478bd9Sstevel@tonic-gate itimer_t *it, **itp;
4677c478bd9Sstevel@tonic-gate sigqueue_t *sigq;
4687c478bd9Sstevel@tonic-gate cred_t *cr = CRED();
4697c478bd9Sstevel@tonic-gate int error = 0;
4707c478bd9Sstevel@tonic-gate timer_t i;
4717c478bd9Sstevel@tonic-gate port_notify_t tim_pnevp;
4727c478bd9Sstevel@tonic-gate port_kevent_t *pkevp = NULL;
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate if ((backend = CLOCK_BACKEND(clock)) == NULL)
4757c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate if (evp != NULL) {
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate * short copyin() for binary compatibility
4807c478bd9Sstevel@tonic-gate * fetch oldsigevent to determine how much to copy in.
4817c478bd9Sstevel@tonic-gate */
4827c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
4837c478bd9Sstevel@tonic-gate if (copyin(evp, &ev, sizeof (struct oldsigevent)))
4847c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
4857c478bd9Sstevel@tonic-gate
48634709573Sraf if (ev.sigev_notify == SIGEV_PORT ||
48734709573Sraf ev.sigev_notify == SIGEV_THREAD) {
4887c478bd9Sstevel@tonic-gate if (copyin(ev.sigev_value.sival_ptr, &tim_pnevp,
4897c478bd9Sstevel@tonic-gate sizeof (port_notify_t)))
4907c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4937c478bd9Sstevel@tonic-gate } else {
4947c478bd9Sstevel@tonic-gate struct sigevent32 ev32;
4957c478bd9Sstevel@tonic-gate port_notify32_t tim_pnevp32;
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate if (copyin(evp, &ev32, sizeof (struct oldsigevent32)))
4987c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
4997c478bd9Sstevel@tonic-gate ev.sigev_notify = ev32.sigev_notify;
5007c478bd9Sstevel@tonic-gate ev.sigev_signo = ev32.sigev_signo;
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate * See comment in sigqueue32() on handling of 32-bit
5037c478bd9Sstevel@tonic-gate * sigvals in a 64-bit kernel.
5047c478bd9Sstevel@tonic-gate */
5057c478bd9Sstevel@tonic-gate ev.sigev_value.sival_int = ev32.sigev_value.sival_int;
50634709573Sraf if (ev.sigev_notify == SIGEV_PORT ||
50734709573Sraf ev.sigev_notify == SIGEV_THREAD) {
5087c478bd9Sstevel@tonic-gate if (copyin((void *)(uintptr_t)
5097c478bd9Sstevel@tonic-gate ev32.sigev_value.sival_ptr,
5107c478bd9Sstevel@tonic-gate (void *)&tim_pnevp32,
5117c478bd9Sstevel@tonic-gate sizeof (port_notify32_t)))
5127c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
5137c478bd9Sstevel@tonic-gate tim_pnevp.portnfy_port =
5147c478bd9Sstevel@tonic-gate tim_pnevp32.portnfy_port;
5157c478bd9Sstevel@tonic-gate tim_pnevp.portnfy_user =
5167c478bd9Sstevel@tonic-gate (void *)(uintptr_t)tim_pnevp32.portnfy_user;
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate #endif
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate switch (ev.sigev_notify) {
5217c478bd9Sstevel@tonic-gate case SIGEV_NONE:
5227c478bd9Sstevel@tonic-gate break;
5237c478bd9Sstevel@tonic-gate case SIGEV_SIGNAL:
5247c478bd9Sstevel@tonic-gate if (ev.sigev_signo < 1 || ev.sigev_signo >= NSIG)
5257c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
5267c478bd9Sstevel@tonic-gate break;
52734709573Sraf case SIGEV_THREAD:
5287c478bd9Sstevel@tonic-gate case SIGEV_PORT:
5297c478bd9Sstevel@tonic-gate break;
5307c478bd9Sstevel@tonic-gate default:
5317c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate } else {
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate * Use the clock's default sigevent (this is a structure copy).
5367c478bd9Sstevel@tonic-gate */
5377c478bd9Sstevel@tonic-gate ev = backend->clk_default;
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate * We'll allocate our timer and sigqueue now, before we grab p_lock.
5427c478bd9Sstevel@tonic-gate * If we can't find an empty slot, we'll free them before returning.
5437c478bd9Sstevel@tonic-gate */
5447c478bd9Sstevel@tonic-gate it = kmem_cache_alloc(clock_timer_cache, KM_SLEEP);
5457c478bd9Sstevel@tonic-gate bzero(it, sizeof (itimer_t));
5467c478bd9Sstevel@tonic-gate mutex_init(&it->it_mutex, NULL, MUTEX_DEFAULT, NULL);
5477c478bd9Sstevel@tonic-gate sigq = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
5487c478bd9Sstevel@tonic-gate
5497c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate /*
5527c478bd9Sstevel@tonic-gate * If this is this process' first timer, we need to attempt to allocate
5537c478bd9Sstevel@tonic-gate * an array of timerstr_t pointers. We drop p_lock to perform the
5547c478bd9Sstevel@tonic-gate * allocation; if we return to discover that p_itimer is non-NULL,
5557c478bd9Sstevel@tonic-gate * we will free our allocation and drive on.
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate if ((itp = p->p_itimer) == NULL) {
5587c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
5597c478bd9Sstevel@tonic-gate itp = kmem_zalloc(timer_max * sizeof (itimer_t *), KM_SLEEP);
5607c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate if (p->p_itimer == NULL)
5637c478bd9Sstevel@tonic-gate p->p_itimer = itp;
5647c478bd9Sstevel@tonic-gate else {
5657c478bd9Sstevel@tonic-gate kmem_free(itp, timer_max * sizeof (itimer_t *));
5667c478bd9Sstevel@tonic-gate itp = p->p_itimer;
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate for (i = 0; i < timer_max && itp[i] != NULL; i++)
5717c478bd9Sstevel@tonic-gate continue;
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate if (i == timer_max) {
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate * We couldn't find a slot. Drop p_lock, free the preallocated
5767c478bd9Sstevel@tonic-gate * timer and sigqueue, and return an error.
5777c478bd9Sstevel@tonic-gate */
5787c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
5797c478bd9Sstevel@tonic-gate kmem_cache_free(clock_timer_cache, it);
5807c478bd9Sstevel@tonic-gate kmem_free(sigq, sizeof (sigqueue_t));
5817c478bd9Sstevel@tonic-gate
5827c478bd9Sstevel@tonic-gate return (set_errno(EAGAIN));
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate ASSERT(i < timer_max && itp[i] == NULL);
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate * If we develop other notification mechanisms, this will need
5897c478bd9Sstevel@tonic-gate * to call into (yet another) backend.
5907c478bd9Sstevel@tonic-gate */
5917c478bd9Sstevel@tonic-gate sigq->sq_info.si_signo = ev.sigev_signo;
59234709573Sraf if (evp == NULL)
59334709573Sraf sigq->sq_info.si_value.sival_int = i;
59434709573Sraf else
5957c478bd9Sstevel@tonic-gate sigq->sq_info.si_value = ev.sigev_value;
5967c478bd9Sstevel@tonic-gate sigq->sq_info.si_code = SI_TIMER;
5977c478bd9Sstevel@tonic-gate sigq->sq_info.si_pid = p->p_pid;
5987c478bd9Sstevel@tonic-gate sigq->sq_info.si_ctid = PRCTID(p);
5997c478bd9Sstevel@tonic-gate sigq->sq_info.si_zoneid = getzoneid();
6007c478bd9Sstevel@tonic-gate sigq->sq_info.si_uid = crgetruid(cr);
6017c478bd9Sstevel@tonic-gate sigq->sq_func = timer_signal;
6027c478bd9Sstevel@tonic-gate sigq->sq_next = NULL;
6037c478bd9Sstevel@tonic-gate sigq->sq_backptr = it;
6047c478bd9Sstevel@tonic-gate it->it_sigq = sigq;
6057c478bd9Sstevel@tonic-gate it->it_backend = backend;
6067c478bd9Sstevel@tonic-gate it->it_lock = ITLK_LOCKED;
6077c478bd9Sstevel@tonic-gate itp[i] = it;
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate
61034709573Sraf if (ev.sigev_notify == SIGEV_THREAD ||
61134709573Sraf ev.sigev_notify == SIGEV_PORT) {
6127c478bd9Sstevel@tonic-gate int port;
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate * This timer is programmed to use event port notification when
6167c478bd9Sstevel@tonic-gate * the timer fires:
6177c478bd9Sstevel@tonic-gate * - allocate a port event structure and prepare it to be sent
6187c478bd9Sstevel@tonic-gate * to the port as soon as the timer fires.
6197c478bd9Sstevel@tonic-gate * - when the timer fires :
6207c478bd9Sstevel@tonic-gate * - if event structure was already sent to the port then this
6217c478bd9Sstevel@tonic-gate * is a timer fire overflow => increment overflow counter.
6227c478bd9Sstevel@tonic-gate * - otherwise send pre-allocated event structure to the port.
6237c478bd9Sstevel@tonic-gate * - the events field of the port_event_t structure counts the
6247c478bd9Sstevel@tonic-gate * number of timer fired events.
6257c478bd9Sstevel@tonic-gate * - The event structured is allocated using the
6267c478bd9Sstevel@tonic-gate * PORT_ALLOC_CACHED flag.
6277c478bd9Sstevel@tonic-gate * This flag indicates that the timer itself will manage and
6287c478bd9Sstevel@tonic-gate * free the event structure when required.
6297c478bd9Sstevel@tonic-gate */
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate it->it_flags |= IT_PORT;
6327c478bd9Sstevel@tonic-gate port = tim_pnevp.portnfy_port;
6337c478bd9Sstevel@tonic-gate
6347c478bd9Sstevel@tonic-gate /* associate timer as event source with the port */
6357c478bd9Sstevel@tonic-gate error = port_associate_ksource(port, PORT_SOURCE_TIMER,
6367c478bd9Sstevel@tonic-gate (port_source_t **)&it->it_portsrc, timer_close_port,
6377c478bd9Sstevel@tonic-gate (void *)it, NULL);
6387c478bd9Sstevel@tonic-gate if (error) {
6397c478bd9Sstevel@tonic-gate itp[i] = NULL; /* clear slot */
6407c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
6417c478bd9Sstevel@tonic-gate kmem_cache_free(clock_timer_cache, it);
6427c478bd9Sstevel@tonic-gate kmem_free(sigq, sizeof (sigqueue_t));
6437c478bd9Sstevel@tonic-gate return (set_errno(error));
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate /* allocate an event structure/slot */
6477c478bd9Sstevel@tonic-gate error = port_alloc_event(port, PORT_ALLOC_SCACHED,
6487c478bd9Sstevel@tonic-gate PORT_SOURCE_TIMER, &pkevp);
6497c478bd9Sstevel@tonic-gate if (error) {
6507c478bd9Sstevel@tonic-gate (void) port_dissociate_ksource(port, PORT_SOURCE_TIMER,
6517c478bd9Sstevel@tonic-gate (port_source_t *)it->it_portsrc);
6527c478bd9Sstevel@tonic-gate itp[i] = NULL; /* clear slot */
6537c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
6547c478bd9Sstevel@tonic-gate kmem_cache_free(clock_timer_cache, it);
6557c478bd9Sstevel@tonic-gate kmem_free(sigq, sizeof (sigqueue_t));
6567c478bd9Sstevel@tonic-gate return (set_errno(error));
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate /* initialize event data */
6607c478bd9Sstevel@tonic-gate port_init_event(pkevp, i, tim_pnevp.portnfy_user,
6617c478bd9Sstevel@tonic-gate timer_port_callback, it);
6627c478bd9Sstevel@tonic-gate it->it_portev = pkevp;
6637c478bd9Sstevel@tonic-gate it->it_portfd = port;
6647c478bd9Sstevel@tonic-gate } else {
6657c478bd9Sstevel@tonic-gate if (ev.sigev_notify == SIGEV_SIGNAL)
6667c478bd9Sstevel@tonic-gate it->it_flags |= IT_SIGNAL;
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate
6697c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate * Call on the backend to verify the event argument (or return
6737c478bd9Sstevel@tonic-gate * EINVAL if this clock type does not support timers).
6747c478bd9Sstevel@tonic-gate */
6757c478bd9Sstevel@tonic-gate if ((error = backend->clk_timer_create(it, &ev)) != 0)
6767c478bd9Sstevel@tonic-gate goto err;
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate it->it_lwp = ttolwp(curthread);
6797c478bd9Sstevel@tonic-gate it->it_proc = p;
6807c478bd9Sstevel@tonic-gate
6817c478bd9Sstevel@tonic-gate if (copyout(&i, tid, sizeof (timer_t)) != 0) {
6827c478bd9Sstevel@tonic-gate error = EFAULT;
6837c478bd9Sstevel@tonic-gate goto err;
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate * If we're here, then we have successfully created the timer; we
6887c478bd9Sstevel@tonic-gate * just need to release the timer and return.
6897c478bd9Sstevel@tonic-gate */
6907c478bd9Sstevel@tonic-gate timer_release(p, it);
6917c478bd9Sstevel@tonic-gate
6927c478bd9Sstevel@tonic-gate return (0);
6937c478bd9Sstevel@tonic-gate
6947c478bd9Sstevel@tonic-gate err:
6957c478bd9Sstevel@tonic-gate /*
6967c478bd9Sstevel@tonic-gate * If we're here, an error has occurred late in the timer creation
6977c478bd9Sstevel@tonic-gate * process. We need to regrab p_lock, and delete the incipient timer.
6987c478bd9Sstevel@tonic-gate * Since we never unlocked the timer (it was born locked), it's
6997c478bd9Sstevel@tonic-gate * impossible for a removal to be pending.
7007c478bd9Sstevel@tonic-gate */
7017c478bd9Sstevel@tonic-gate ASSERT(!(it->it_lock & ITLK_REMOVE));
7027c478bd9Sstevel@tonic-gate timer_delete_grabbed(p, i, it);
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate return (set_errno(error));
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate
7077c478bd9Sstevel@tonic-gate int
timer_gettime(timer_t tid,itimerspec_t * val)7087c478bd9Sstevel@tonic-gate timer_gettime(timer_t tid, itimerspec_t *val)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate proc_t *p = curproc;
7117c478bd9Sstevel@tonic-gate itimer_t *it;
7127c478bd9Sstevel@tonic-gate itimerspec_t when;
7137c478bd9Sstevel@tonic-gate int error;
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate if ((it = timer_grab(p, tid)) == NULL)
7167c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
7177c478bd9Sstevel@tonic-gate
7187c478bd9Sstevel@tonic-gate error = it->it_backend->clk_timer_gettime(it, &when);
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate timer_release(p, it);
7217c478bd9Sstevel@tonic-gate
7227c478bd9Sstevel@tonic-gate if (error == 0) {
7237c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
7247c478bd9Sstevel@tonic-gate if (copyout(&when, val, sizeof (itimerspec_t)))
7257c478bd9Sstevel@tonic-gate error = EFAULT;
7267c478bd9Sstevel@tonic-gate } else {
7277c478bd9Sstevel@tonic-gate if (ITIMERSPEC_OVERFLOW(&when))
7287c478bd9Sstevel@tonic-gate error = EOVERFLOW;
7297c478bd9Sstevel@tonic-gate else {
7307c478bd9Sstevel@tonic-gate itimerspec32_t w32;
7317c478bd9Sstevel@tonic-gate
7327c478bd9Sstevel@tonic-gate ITIMERSPEC_TO_ITIMERSPEC32(&w32, &when)
7337c478bd9Sstevel@tonic-gate if (copyout(&w32, val, sizeof (itimerspec32_t)))
7347c478bd9Sstevel@tonic-gate error = EFAULT;
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate
7397c478bd9Sstevel@tonic-gate return (error ? set_errno(error) : 0);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate
7427c478bd9Sstevel@tonic-gate int
timer_settime(timer_t tid,int flags,itimerspec_t * val,itimerspec_t * oval)7437c478bd9Sstevel@tonic-gate timer_settime(timer_t tid, int flags, itimerspec_t *val, itimerspec_t *oval)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate itimerspec_t when;
7467c478bd9Sstevel@tonic-gate itimer_t *it;
7477c478bd9Sstevel@tonic-gate proc_t *p = curproc;
7487c478bd9Sstevel@tonic-gate int error;
7497c478bd9Sstevel@tonic-gate
7507c478bd9Sstevel@tonic-gate if (oval != NULL) {
7517c478bd9Sstevel@tonic-gate if ((error = timer_gettime(tid, oval)) != 0)
7527c478bd9Sstevel@tonic-gate return (error);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate
7557c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
7567c478bd9Sstevel@tonic-gate if (copyin(val, &when, sizeof (itimerspec_t)))
7577c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
7587c478bd9Sstevel@tonic-gate } else {
7597c478bd9Sstevel@tonic-gate itimerspec32_t w32;
7607c478bd9Sstevel@tonic-gate
7617c478bd9Sstevel@tonic-gate if (copyin(val, &w32, sizeof (itimerspec32_t)))
7627c478bd9Sstevel@tonic-gate return (set_errno(EFAULT));
7637c478bd9Sstevel@tonic-gate
7647c478bd9Sstevel@tonic-gate ITIMERSPEC32_TO_ITIMERSPEC(&when, &w32);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate
7677c478bd9Sstevel@tonic-gate if (itimerspecfix(&when.it_value) ||
7687c478bd9Sstevel@tonic-gate (itimerspecfix(&when.it_interval) &&
7697c478bd9Sstevel@tonic-gate timerspecisset(&when.it_value))) {
7707c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate
7737c478bd9Sstevel@tonic-gate if ((it = timer_grab(p, tid)) == NULL)
7747c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate error = it->it_backend->clk_timer_settime(it, flags, &when);
7777c478bd9Sstevel@tonic-gate
7787c478bd9Sstevel@tonic-gate timer_release(p, it);
7797c478bd9Sstevel@tonic-gate
7807c478bd9Sstevel@tonic-gate return (error ? set_errno(error) : 0);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate int
timer_delete(timer_t tid)7847c478bd9Sstevel@tonic-gate timer_delete(timer_t tid)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate proc_t *p = curproc;
7877c478bd9Sstevel@tonic-gate itimer_t *it;
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate if ((it = timer_grab(p, tid)) == NULL)
7907c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
7917c478bd9Sstevel@tonic-gate
7927c478bd9Sstevel@tonic-gate timer_delete_grabbed(p, tid, it);
7937c478bd9Sstevel@tonic-gate
7947c478bd9Sstevel@tonic-gate return (0);
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate
7977c478bd9Sstevel@tonic-gate int
timer_getoverrun(timer_t tid)7987c478bd9Sstevel@tonic-gate timer_getoverrun(timer_t tid)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate int overrun;
8017c478bd9Sstevel@tonic-gate proc_t *p = curproc;
8027c478bd9Sstevel@tonic-gate itimer_t *it;
8037c478bd9Sstevel@tonic-gate
8047c478bd9Sstevel@tonic-gate if ((it = timer_grab(p, tid)) == NULL)
8057c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate /*
8087c478bd9Sstevel@tonic-gate * The it_overrun field is protected by p_lock; we need to acquire
8097c478bd9Sstevel@tonic-gate * it before looking at the value.
8107c478bd9Sstevel@tonic-gate */
8117c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
8127c478bd9Sstevel@tonic-gate overrun = it->it_overrun;
8137c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
8147c478bd9Sstevel@tonic-gate
8157c478bd9Sstevel@tonic-gate timer_release(p, it);
8167c478bd9Sstevel@tonic-gate
8177c478bd9Sstevel@tonic-gate return (overrun);
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate
8207c478bd9Sstevel@tonic-gate /*
8217c478bd9Sstevel@tonic-gate * Entered/exited with p_lock held, but will repeatedly drop and regrab p_lock.
8227c478bd9Sstevel@tonic-gate */
8237c478bd9Sstevel@tonic-gate void
timer_lwpexit(void)8247c478bd9Sstevel@tonic-gate timer_lwpexit(void)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate timer_t i;
8277c478bd9Sstevel@tonic-gate proc_t *p = curproc;
8287c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
8297c478bd9Sstevel@tonic-gate itimer_t *it, **itp;
8307c478bd9Sstevel@tonic-gate
8317c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate if ((itp = p->p_itimer) == NULL)
8347c478bd9Sstevel@tonic-gate return;
8357c478bd9Sstevel@tonic-gate
8367c478bd9Sstevel@tonic-gate for (i = 0; i < timer_max; i++) {
8377c478bd9Sstevel@tonic-gate if ((it = itp[i]) == NULL)
8387c478bd9Sstevel@tonic-gate continue;
8397c478bd9Sstevel@tonic-gate
8407c478bd9Sstevel@tonic-gate timer_lock(p, it);
8417c478bd9Sstevel@tonic-gate
8427c478bd9Sstevel@tonic-gate if ((it->it_lock & ITLK_REMOVE) || it->it_lwp != lwp) {
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate * This timer is either being removed or it isn't
8457c478bd9Sstevel@tonic-gate * associated with this lwp.
8467c478bd9Sstevel@tonic-gate */
8477c478bd9Sstevel@tonic-gate timer_unlock(p, it);
8487c478bd9Sstevel@tonic-gate continue;
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate
8517c478bd9Sstevel@tonic-gate /*
8527c478bd9Sstevel@tonic-gate * The LWP that created this timer is going away. To the user,
8537c478bd9Sstevel@tonic-gate * our behavior here is explicitly undefined. We will simply
8547c478bd9Sstevel@tonic-gate * null out the it_lwp field; if the LWP was bound to a CPU,
8557c478bd9Sstevel@tonic-gate * the cyclic will stay bound to that CPU until the process
8567c478bd9Sstevel@tonic-gate * exits.
8577c478bd9Sstevel@tonic-gate */
8587c478bd9Sstevel@tonic-gate it->it_lwp = NULL;
8597c478bd9Sstevel@tonic-gate timer_unlock(p, it);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate
8637c478bd9Sstevel@tonic-gate /*
8647c478bd9Sstevel@tonic-gate * Called to notify of an LWP binding change. Entered/exited with p_lock
8657c478bd9Sstevel@tonic-gate * held, but will repeatedly drop and regrab p_lock.
8667c478bd9Sstevel@tonic-gate */
8677c478bd9Sstevel@tonic-gate void
timer_lwpbind()8687c478bd9Sstevel@tonic-gate timer_lwpbind()
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate timer_t i;
8717c478bd9Sstevel@tonic-gate proc_t *p = curproc;
8727c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
8737c478bd9Sstevel@tonic-gate itimer_t *it, **itp;
8747c478bd9Sstevel@tonic-gate
8757c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
8767c478bd9Sstevel@tonic-gate
8777c478bd9Sstevel@tonic-gate if ((itp = p->p_itimer) == NULL)
8787c478bd9Sstevel@tonic-gate return;
8797c478bd9Sstevel@tonic-gate
8807c478bd9Sstevel@tonic-gate for (i = 0; i < timer_max; i++) {
8817c478bd9Sstevel@tonic-gate if ((it = itp[i]) == NULL)
8827c478bd9Sstevel@tonic-gate continue;
8837c478bd9Sstevel@tonic-gate
8847c478bd9Sstevel@tonic-gate timer_lock(p, it);
8857c478bd9Sstevel@tonic-gate
8867c478bd9Sstevel@tonic-gate if (!(it->it_lock & ITLK_REMOVE) && it->it_lwp == lwp) {
8877c478bd9Sstevel@tonic-gate /*
8887c478bd9Sstevel@tonic-gate * Drop p_lock and jump into the backend.
8897c478bd9Sstevel@tonic-gate */
8907c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
8917c478bd9Sstevel@tonic-gate it->it_backend->clk_timer_lwpbind(it);
8927c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate
8957c478bd9Sstevel@tonic-gate timer_unlock(p, it);
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate /*
9007c478bd9Sstevel@tonic-gate * This function should only be called if p_itimer is non-NULL.
9017c478bd9Sstevel@tonic-gate */
9027c478bd9Sstevel@tonic-gate void
timer_exit(void)9037c478bd9Sstevel@tonic-gate timer_exit(void)
9047c478bd9Sstevel@tonic-gate {
9057c478bd9Sstevel@tonic-gate timer_t i;
9067c478bd9Sstevel@tonic-gate proc_t *p = curproc;
9077c478bd9Sstevel@tonic-gate
9087c478bd9Sstevel@tonic-gate ASSERT(p->p_itimer != NULL);
9097c478bd9Sstevel@tonic-gate
9107c478bd9Sstevel@tonic-gate for (i = 0; i < timer_max; i++)
9117c478bd9Sstevel@tonic-gate (void) timer_delete(i);
9127c478bd9Sstevel@tonic-gate
9137c478bd9Sstevel@tonic-gate kmem_free(p->p_itimer, timer_max * sizeof (itimer_t *));
9147c478bd9Sstevel@tonic-gate p->p_itimer = NULL;
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate
9177c478bd9Sstevel@tonic-gate /*
9187c478bd9Sstevel@tonic-gate * timer_port_callback() is a callback function which is associated with the
9197c478bd9Sstevel@tonic-gate * timer event and is activated just before the event is delivered to the user.
9207c478bd9Sstevel@tonic-gate * The timer uses this function to update/set the overflow counter and
9217c478bd9Sstevel@tonic-gate * to reenable the use of the event structure.
9227c478bd9Sstevel@tonic-gate */
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate /* ARGSUSED */
9257c478bd9Sstevel@tonic-gate static int
timer_port_callback(void * arg,int * events,pid_t pid,int flag,void * evp)9267c478bd9Sstevel@tonic-gate timer_port_callback(void *arg, int *events, pid_t pid, int flag, void *evp)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate itimer_t *it = arg;
9297c478bd9Sstevel@tonic-gate
9307c478bd9Sstevel@tonic-gate mutex_enter(&it->it_mutex);
9317c478bd9Sstevel@tonic-gate if (curproc != it->it_proc) {
9327c478bd9Sstevel@tonic-gate /* can not deliver timer events to another proc */
9337c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex);
9347c478bd9Sstevel@tonic-gate return (EACCES);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate *events = it->it_pending; /* 1 = 1 event, >1 # of overflows */
9377c478bd9Sstevel@tonic-gate it->it_pending = 0; /* reinit overflow counter */
9387c478bd9Sstevel@tonic-gate /*
9397c478bd9Sstevel@tonic-gate * This function can also be activated when the port is being closed
9407c478bd9Sstevel@tonic-gate * and a timer event is already submitted to the port.
9417c478bd9Sstevel@tonic-gate * In such a case the event port framework will use the
9427c478bd9Sstevel@tonic-gate * close-callback function to notify the events sources.
9437c478bd9Sstevel@tonic-gate * The timer close-callback function is timer_close_port() which
9447c478bd9Sstevel@tonic-gate * will free all allocated resources (including the allocated
9457c478bd9Sstevel@tonic-gate * port event structure).
9467c478bd9Sstevel@tonic-gate * For that reason we don't need to check the value of flag here.
9477c478bd9Sstevel@tonic-gate */
9487c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex);
9497c478bd9Sstevel@tonic-gate return (0);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate
9527c478bd9Sstevel@tonic-gate /*
9537c478bd9Sstevel@tonic-gate * port is being closed ... free all allocated port event structures
9547c478bd9Sstevel@tonic-gate * The delivered arg currently correspond to the first timer associated with
9557c478bd9Sstevel@tonic-gate * the port and it is not useable in this case.
9567c478bd9Sstevel@tonic-gate * We have to scan the list of activated timers in the current proc and
9577c478bd9Sstevel@tonic-gate * compare them with the delivered port id.
9587c478bd9Sstevel@tonic-gate */
9597c478bd9Sstevel@tonic-gate
9607c478bd9Sstevel@tonic-gate /* ARGSUSED */
9617c478bd9Sstevel@tonic-gate static void
timer_close_port(void * arg,int port,pid_t pid,int lastclose)9627c478bd9Sstevel@tonic-gate timer_close_port(void *arg, int port, pid_t pid, int lastclose)
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate proc_t *p = curproc;
9657c478bd9Sstevel@tonic-gate timer_t tid;
9667c478bd9Sstevel@tonic-gate itimer_t *it;
9677c478bd9Sstevel@tonic-gate
9687c478bd9Sstevel@tonic-gate for (tid = 0; tid < timer_max; tid++) {
9697c478bd9Sstevel@tonic-gate if ((it = timer_grab(p, tid)) == NULL)
9707c478bd9Sstevel@tonic-gate continue;
9717c478bd9Sstevel@tonic-gate if (it->it_portev) {
9727c478bd9Sstevel@tonic-gate mutex_enter(&it->it_mutex);
9737c478bd9Sstevel@tonic-gate if (it->it_portfd == port) {
9748b79e857Spraks port_kevent_t *pev;
9758b79e857Spraks pev = (port_kevent_t *)it->it_portev;
9767c478bd9Sstevel@tonic-gate it->it_portev = NULL;
9777c478bd9Sstevel@tonic-gate it->it_flags &= ~IT_PORT;
9787c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex);
9798b79e857Spraks (void) port_remove_done_event(pev);
9808b79e857Spraks port_free_event(pev);
9818b79e857Spraks } else {
9828b79e857Spraks mutex_exit(&it->it_mutex);
9838b79e857Spraks }
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate timer_release(p, it);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate }
988