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 2713a42676SBryan Cantrill /* 2813a42676SBryan Cantrill * Copyright (c) 2012, Joyent, Inc. All rights reserved. 2913a42676SBryan 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 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 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 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 * 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 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 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 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 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 272*6a72db4aSBryan Cantrill clock_backend_t * 273*6a72db4aSBryan Cantrill clock_get_backend(clockid_t clock) 274*6a72db4aSBryan Cantrill { 275*6a72db4aSBryan Cantrill if (clock < 0 || clock >= CLOCK_MAX) 276*6a72db4aSBryan Cantrill return (NULL); 277*6a72db4aSBryan Cantrill 278*6a72db4aSBryan Cantrill return (clock_backend[clock]); 279*6a72db4aSBryan Cantrill } 280*6a72db4aSBryan Cantrill 2817c478bd9Sstevel@tonic-gate int 2827c478bd9Sstevel@tonic-gate clock_settime(clockid_t clock, timespec_t *tp) 2837c478bd9Sstevel@tonic-gate { 2847c478bd9Sstevel@tonic-gate timespec_t t; 2857c478bd9Sstevel@tonic-gate clock_backend_t *backend; 2867c478bd9Sstevel@tonic-gate int error; 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate if ((backend = CLOCK_BACKEND(clock)) == NULL) 2897c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate if (secpolicy_settime(CRED()) != 0) 2927c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 2957c478bd9Sstevel@tonic-gate if (copyin(tp, &t, sizeof (timespec_t)) != 0) 2967c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 2977c478bd9Sstevel@tonic-gate } else { 2987c478bd9Sstevel@tonic-gate timespec32_t t32; 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate if (copyin(tp, &t32, sizeof (timespec32_t)) != 0) 3017c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate TIMESPEC32_TO_TIMESPEC(&t, &t32); 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate if (itimerspecfix(&t)) 3077c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate error = backend->clk_clock_settime(&t); 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate if (error) 3127c478bd9Sstevel@tonic-gate return (set_errno(error)); 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate return (0); 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate int 3187c478bd9Sstevel@tonic-gate clock_gettime(clockid_t clock, timespec_t *tp) 3197c478bd9Sstevel@tonic-gate { 3207c478bd9Sstevel@tonic-gate timespec_t t; 3217c478bd9Sstevel@tonic-gate clock_backend_t *backend; 3227c478bd9Sstevel@tonic-gate int error; 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate if ((backend = CLOCK_BACKEND(clock)) == NULL) 3257c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate error = backend->clk_clock_gettime(&t); 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate if (error) 3307c478bd9Sstevel@tonic-gate return (set_errno(error)); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 3337c478bd9Sstevel@tonic-gate if (copyout(&t, tp, sizeof (timespec_t)) != 0) 3347c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 3357c478bd9Sstevel@tonic-gate } else { 3367c478bd9Sstevel@tonic-gate timespec32_t t32; 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate if (TIMESPEC_OVERFLOW(&t)) 3397c478bd9Sstevel@tonic-gate return (set_errno(EOVERFLOW)); 3407c478bd9Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(&t32, &t); 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate if (copyout(&t32, tp, sizeof (timespec32_t)) != 0) 3437c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate return (0); 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate int 3507c478bd9Sstevel@tonic-gate clock_getres(clockid_t clock, timespec_t *tp) 3517c478bd9Sstevel@tonic-gate { 3527c478bd9Sstevel@tonic-gate timespec_t t; 3537c478bd9Sstevel@tonic-gate clock_backend_t *backend; 3547c478bd9Sstevel@tonic-gate int error; 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate /* 3577c478bd9Sstevel@tonic-gate * Strangely, the standard defines clock_getres() with a NULL tp 3587c478bd9Sstevel@tonic-gate * to do nothing (regardless of the validity of the specified 3597c478bd9Sstevel@tonic-gate * clock_id). Go figure. 3607c478bd9Sstevel@tonic-gate */ 3617c478bd9Sstevel@tonic-gate if (tp == NULL) 3627c478bd9Sstevel@tonic-gate return (0); 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate if ((backend = CLOCK_BACKEND(clock)) == NULL) 3657c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate error = backend->clk_clock_getres(&t); 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate if (error) 3707c478bd9Sstevel@tonic-gate return (set_errno(error)); 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 3737c478bd9Sstevel@tonic-gate if (copyout(&t, tp, sizeof (timespec_t)) != 0) 3747c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 3757c478bd9Sstevel@tonic-gate } else { 3767c478bd9Sstevel@tonic-gate timespec32_t t32; 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate if (TIMESPEC_OVERFLOW(&t)) 3797c478bd9Sstevel@tonic-gate return (set_errno(EOVERFLOW)); 3807c478bd9Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(&t32, &t); 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate if (copyout(&t32, tp, sizeof (timespec32_t)) != 0) 3837c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate 3867c478bd9Sstevel@tonic-gate return (0); 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate void 3907c478bd9Sstevel@tonic-gate timer_signal(sigqueue_t *sigq) 3917c478bd9Sstevel@tonic-gate { 3927c478bd9Sstevel@tonic-gate itimer_t *it = (itimer_t *)sigq->sq_backptr; 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate /* 3957c478bd9Sstevel@tonic-gate * There are some conditions during a fork or an exit when we can 3967c478bd9Sstevel@tonic-gate * call siginfofree() without p_lock held. To prevent a race 3977c478bd9Sstevel@tonic-gate * between timer_signal() and timer_fire() with regard to it_pending, 3987c478bd9Sstevel@tonic-gate * we therefore acquire it_mutex in both paths. 3997c478bd9Sstevel@tonic-gate */ 4007c478bd9Sstevel@tonic-gate mutex_enter(&it->it_mutex); 4017c478bd9Sstevel@tonic-gate ASSERT(it->it_pending > 0); 4027c478bd9Sstevel@tonic-gate it->it_overrun = it->it_pending - 1; 4037c478bd9Sstevel@tonic-gate it->it_pending = 0; 4047c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex); 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate /* 4087c478bd9Sstevel@tonic-gate * This routine is called from the clock backend. 4097c478bd9Sstevel@tonic-gate */ 410*6a72db4aSBryan Cantrill static void 4117c478bd9Sstevel@tonic-gate timer_fire(itimer_t *it) 4127c478bd9Sstevel@tonic-gate { 4137c478bd9Sstevel@tonic-gate proc_t *p; 4147c478bd9Sstevel@tonic-gate int proc_lock_held; 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate if (it->it_flags & IT_SIGNAL) { 4177c478bd9Sstevel@tonic-gate /* 4187c478bd9Sstevel@tonic-gate * See the comment in timer_signal() for why it is not 4197c478bd9Sstevel@tonic-gate * sufficient to only grab p_lock here. Because p_lock can be 4207c478bd9Sstevel@tonic-gate * held on entry to timer_signal(), the lock ordering is 4217c478bd9Sstevel@tonic-gate * necessarily p_lock before it_mutex. 4227c478bd9Sstevel@tonic-gate */ 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate p = it->it_proc; 4257c478bd9Sstevel@tonic-gate proc_lock_held = 1; 4267c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 4277c478bd9Sstevel@tonic-gate } else { 4287c478bd9Sstevel@tonic-gate /* 4297c478bd9Sstevel@tonic-gate * IT_PORT: 4307c478bd9Sstevel@tonic-gate * If a timer was ever programmed to send events to a port, 4317c478bd9Sstevel@tonic-gate * the IT_PORT flag will remain set until: 4327c478bd9Sstevel@tonic-gate * a) the timer is deleted (see timer_delete_locked()) or 4337c478bd9Sstevel@tonic-gate * b) the port is being closed (see timer_close_port()). 4347c478bd9Sstevel@tonic-gate * Both cases are synchronized with the it_mutex. 4357c478bd9Sstevel@tonic-gate * We don't need to use the p_lock because it is only 4367c478bd9Sstevel@tonic-gate * required in the IT_SIGNAL case. 4377c478bd9Sstevel@tonic-gate * If IT_PORT was set and the port is being closed then 4387c478bd9Sstevel@tonic-gate * the timer notification is set to NONE. In such a case 4397c478bd9Sstevel@tonic-gate * the timer itself and the it_pending counter remain active 4407c478bd9Sstevel@tonic-gate * until the application deletes the counter or the process 4417c478bd9Sstevel@tonic-gate * exits. 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate proc_lock_held = 0; 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate mutex_enter(&it->it_mutex); 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate if (it->it_pending > 0) { 4487c478bd9Sstevel@tonic-gate if (it->it_pending < INT_MAX) 4497c478bd9Sstevel@tonic-gate it->it_pending++; 4507c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex); 4517c478bd9Sstevel@tonic-gate } else { 4527c478bd9Sstevel@tonic-gate if (it->it_flags & IT_PORT) { 4537c478bd9Sstevel@tonic-gate it->it_pending = 1; 45434709573Sraf port_send_event((port_kevent_t *)it->it_portev); 4557c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex); 4567c478bd9Sstevel@tonic-gate } else if (it->it_flags & IT_SIGNAL) { 4577c478bd9Sstevel@tonic-gate it->it_pending = 1; 4587c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex); 4597c478bd9Sstevel@tonic-gate sigaddqa(p, NULL, it->it_sigq); 4607c478bd9Sstevel@tonic-gate } else { 4617c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex); 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate if (proc_lock_held) 4667c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate int 4707c478bd9Sstevel@tonic-gate timer_create(clockid_t clock, struct sigevent *evp, timer_t *tid) 4717c478bd9Sstevel@tonic-gate { 4727c478bd9Sstevel@tonic-gate struct sigevent ev; 4737c478bd9Sstevel@tonic-gate proc_t *p = curproc; 4747c478bd9Sstevel@tonic-gate clock_backend_t *backend; 4757c478bd9Sstevel@tonic-gate itimer_t *it, **itp; 4767c478bd9Sstevel@tonic-gate sigqueue_t *sigq; 4777c478bd9Sstevel@tonic-gate cred_t *cr = CRED(); 4787c478bd9Sstevel@tonic-gate int error = 0; 4797c478bd9Sstevel@tonic-gate timer_t i; 4807c478bd9Sstevel@tonic-gate port_notify_t tim_pnevp; 4817c478bd9Sstevel@tonic-gate port_kevent_t *pkevp = NULL; 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate if ((backend = CLOCK_BACKEND(clock)) == NULL) 4847c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate if (evp != NULL) { 4877c478bd9Sstevel@tonic-gate /* 4887c478bd9Sstevel@tonic-gate * short copyin() for binary compatibility 4897c478bd9Sstevel@tonic-gate * fetch oldsigevent to determine how much to copy in. 4907c478bd9Sstevel@tonic-gate */ 4917c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 4927c478bd9Sstevel@tonic-gate if (copyin(evp, &ev, sizeof (struct oldsigevent))) 4937c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 4947c478bd9Sstevel@tonic-gate 49534709573Sraf if (ev.sigev_notify == SIGEV_PORT || 49634709573Sraf ev.sigev_notify == SIGEV_THREAD) { 4977c478bd9Sstevel@tonic-gate if (copyin(ev.sigev_value.sival_ptr, &tim_pnevp, 4987c478bd9Sstevel@tonic-gate sizeof (port_notify_t))) 4997c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 5027c478bd9Sstevel@tonic-gate } else { 5037c478bd9Sstevel@tonic-gate struct sigevent32 ev32; 5047c478bd9Sstevel@tonic-gate port_notify32_t tim_pnevp32; 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate if (copyin(evp, &ev32, sizeof (struct oldsigevent32))) 5077c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 5087c478bd9Sstevel@tonic-gate ev.sigev_notify = ev32.sigev_notify; 5097c478bd9Sstevel@tonic-gate ev.sigev_signo = ev32.sigev_signo; 5107c478bd9Sstevel@tonic-gate /* 5117c478bd9Sstevel@tonic-gate * See comment in sigqueue32() on handling of 32-bit 5127c478bd9Sstevel@tonic-gate * sigvals in a 64-bit kernel. 5137c478bd9Sstevel@tonic-gate */ 5147c478bd9Sstevel@tonic-gate ev.sigev_value.sival_int = ev32.sigev_value.sival_int; 51534709573Sraf if (ev.sigev_notify == SIGEV_PORT || 51634709573Sraf ev.sigev_notify == SIGEV_THREAD) { 5177c478bd9Sstevel@tonic-gate if (copyin((void *)(uintptr_t) 5187c478bd9Sstevel@tonic-gate ev32.sigev_value.sival_ptr, 5197c478bd9Sstevel@tonic-gate (void *)&tim_pnevp32, 5207c478bd9Sstevel@tonic-gate sizeof (port_notify32_t))) 5217c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 5227c478bd9Sstevel@tonic-gate tim_pnevp.portnfy_port = 5237c478bd9Sstevel@tonic-gate tim_pnevp32.portnfy_port; 5247c478bd9Sstevel@tonic-gate tim_pnevp.portnfy_user = 5257c478bd9Sstevel@tonic-gate (void *)(uintptr_t)tim_pnevp32.portnfy_user; 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate #endif 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate switch (ev.sigev_notify) { 5307c478bd9Sstevel@tonic-gate case SIGEV_NONE: 5317c478bd9Sstevel@tonic-gate break; 5327c478bd9Sstevel@tonic-gate case SIGEV_SIGNAL: 5337c478bd9Sstevel@tonic-gate if (ev.sigev_signo < 1 || ev.sigev_signo >= NSIG) 5347c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 5357c478bd9Sstevel@tonic-gate break; 53634709573Sraf case SIGEV_THREAD: 5377c478bd9Sstevel@tonic-gate case SIGEV_PORT: 5387c478bd9Sstevel@tonic-gate break; 5397c478bd9Sstevel@tonic-gate default: 5407c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate } else { 5437c478bd9Sstevel@tonic-gate /* 5447c478bd9Sstevel@tonic-gate * Use the clock's default sigevent (this is a structure copy). 5457c478bd9Sstevel@tonic-gate */ 5467c478bd9Sstevel@tonic-gate ev = backend->clk_default; 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate /* 5507c478bd9Sstevel@tonic-gate * We'll allocate our timer and sigqueue now, before we grab p_lock. 5517c478bd9Sstevel@tonic-gate * If we can't find an empty slot, we'll free them before returning. 5527c478bd9Sstevel@tonic-gate */ 5537c478bd9Sstevel@tonic-gate it = kmem_cache_alloc(clock_timer_cache, KM_SLEEP); 5547c478bd9Sstevel@tonic-gate bzero(it, sizeof (itimer_t)); 5557c478bd9Sstevel@tonic-gate mutex_init(&it->it_mutex, NULL, MUTEX_DEFAULT, NULL); 5567c478bd9Sstevel@tonic-gate sigq = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP); 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate /* 5617c478bd9Sstevel@tonic-gate * If this is this process' first timer, we need to attempt to allocate 5627c478bd9Sstevel@tonic-gate * an array of timerstr_t pointers. We drop p_lock to perform the 5637c478bd9Sstevel@tonic-gate * allocation; if we return to discover that p_itimer is non-NULL, 5647c478bd9Sstevel@tonic-gate * we will free our allocation and drive on. 5657c478bd9Sstevel@tonic-gate */ 5667c478bd9Sstevel@tonic-gate if ((itp = p->p_itimer) == NULL) { 5677c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5687c478bd9Sstevel@tonic-gate itp = kmem_zalloc(timer_max * sizeof (itimer_t *), KM_SLEEP); 5697c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate if (p->p_itimer == NULL) 5727c478bd9Sstevel@tonic-gate p->p_itimer = itp; 5737c478bd9Sstevel@tonic-gate else { 5747c478bd9Sstevel@tonic-gate kmem_free(itp, timer_max * sizeof (itimer_t *)); 5757c478bd9Sstevel@tonic-gate itp = p->p_itimer; 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate for (i = 0; i < timer_max && itp[i] != NULL; i++) 5807c478bd9Sstevel@tonic-gate continue; 5817c478bd9Sstevel@tonic-gate 5827c478bd9Sstevel@tonic-gate if (i == timer_max) { 5837c478bd9Sstevel@tonic-gate /* 5847c478bd9Sstevel@tonic-gate * We couldn't find a slot. Drop p_lock, free the preallocated 5857c478bd9Sstevel@tonic-gate * timer and sigqueue, and return an error. 5867c478bd9Sstevel@tonic-gate */ 5877c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5887c478bd9Sstevel@tonic-gate kmem_cache_free(clock_timer_cache, it); 5897c478bd9Sstevel@tonic-gate kmem_free(sigq, sizeof (sigqueue_t)); 5907c478bd9Sstevel@tonic-gate 5917c478bd9Sstevel@tonic-gate return (set_errno(EAGAIN)); 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate ASSERT(i < timer_max && itp[i] == NULL); 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate /* 5977c478bd9Sstevel@tonic-gate * If we develop other notification mechanisms, this will need 5987c478bd9Sstevel@tonic-gate * to call into (yet another) backend. 5997c478bd9Sstevel@tonic-gate */ 6007c478bd9Sstevel@tonic-gate sigq->sq_info.si_signo = ev.sigev_signo; 60134709573Sraf if (evp == NULL) 60234709573Sraf sigq->sq_info.si_value.sival_int = i; 60334709573Sraf else 6047c478bd9Sstevel@tonic-gate sigq->sq_info.si_value = ev.sigev_value; 6057c478bd9Sstevel@tonic-gate sigq->sq_info.si_code = SI_TIMER; 6067c478bd9Sstevel@tonic-gate sigq->sq_info.si_pid = p->p_pid; 6077c478bd9Sstevel@tonic-gate sigq->sq_info.si_ctid = PRCTID(p); 6087c478bd9Sstevel@tonic-gate sigq->sq_info.si_zoneid = getzoneid(); 6097c478bd9Sstevel@tonic-gate sigq->sq_info.si_uid = crgetruid(cr); 6107c478bd9Sstevel@tonic-gate sigq->sq_func = timer_signal; 6117c478bd9Sstevel@tonic-gate sigq->sq_next = NULL; 6127c478bd9Sstevel@tonic-gate sigq->sq_backptr = it; 6137c478bd9Sstevel@tonic-gate it->it_sigq = sigq; 6147c478bd9Sstevel@tonic-gate it->it_backend = backend; 6157c478bd9Sstevel@tonic-gate it->it_lock = ITLK_LOCKED; 6167c478bd9Sstevel@tonic-gate itp[i] = it; 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate 61934709573Sraf if (ev.sigev_notify == SIGEV_THREAD || 62034709573Sraf ev.sigev_notify == SIGEV_PORT) { 6217c478bd9Sstevel@tonic-gate int port; 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate /* 6247c478bd9Sstevel@tonic-gate * This timer is programmed to use event port notification when 6257c478bd9Sstevel@tonic-gate * the timer fires: 6267c478bd9Sstevel@tonic-gate * - allocate a port event structure and prepare it to be sent 6277c478bd9Sstevel@tonic-gate * to the port as soon as the timer fires. 6287c478bd9Sstevel@tonic-gate * - when the timer fires : 6297c478bd9Sstevel@tonic-gate * - if event structure was already sent to the port then this 6307c478bd9Sstevel@tonic-gate * is a timer fire overflow => increment overflow counter. 6317c478bd9Sstevel@tonic-gate * - otherwise send pre-allocated event structure to the port. 6327c478bd9Sstevel@tonic-gate * - the events field of the port_event_t structure counts the 6337c478bd9Sstevel@tonic-gate * number of timer fired events. 6347c478bd9Sstevel@tonic-gate * - The event structured is allocated using the 6357c478bd9Sstevel@tonic-gate * PORT_ALLOC_CACHED flag. 6367c478bd9Sstevel@tonic-gate * This flag indicates that the timer itself will manage and 6377c478bd9Sstevel@tonic-gate * free the event structure when required. 6387c478bd9Sstevel@tonic-gate */ 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate it->it_flags |= IT_PORT; 6417c478bd9Sstevel@tonic-gate port = tim_pnevp.portnfy_port; 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate /* associate timer as event source with the port */ 6447c478bd9Sstevel@tonic-gate error = port_associate_ksource(port, PORT_SOURCE_TIMER, 6457c478bd9Sstevel@tonic-gate (port_source_t **)&it->it_portsrc, timer_close_port, 6467c478bd9Sstevel@tonic-gate (void *)it, NULL); 6477c478bd9Sstevel@tonic-gate if (error) { 6487c478bd9Sstevel@tonic-gate itp[i] = NULL; /* clear slot */ 6497c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 6507c478bd9Sstevel@tonic-gate kmem_cache_free(clock_timer_cache, it); 6517c478bd9Sstevel@tonic-gate kmem_free(sigq, sizeof (sigqueue_t)); 6527c478bd9Sstevel@tonic-gate return (set_errno(error)); 6537c478bd9Sstevel@tonic-gate } 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate /* allocate an event structure/slot */ 6567c478bd9Sstevel@tonic-gate error = port_alloc_event(port, PORT_ALLOC_SCACHED, 6577c478bd9Sstevel@tonic-gate PORT_SOURCE_TIMER, &pkevp); 6587c478bd9Sstevel@tonic-gate if (error) { 6597c478bd9Sstevel@tonic-gate (void) port_dissociate_ksource(port, PORT_SOURCE_TIMER, 6607c478bd9Sstevel@tonic-gate (port_source_t *)it->it_portsrc); 6617c478bd9Sstevel@tonic-gate itp[i] = NULL; /* clear slot */ 6627c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 6637c478bd9Sstevel@tonic-gate kmem_cache_free(clock_timer_cache, it); 6647c478bd9Sstevel@tonic-gate kmem_free(sigq, sizeof (sigqueue_t)); 6657c478bd9Sstevel@tonic-gate return (set_errno(error)); 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate /* initialize event data */ 6697c478bd9Sstevel@tonic-gate port_init_event(pkevp, i, tim_pnevp.portnfy_user, 6707c478bd9Sstevel@tonic-gate timer_port_callback, it); 6717c478bd9Sstevel@tonic-gate it->it_portev = pkevp; 6727c478bd9Sstevel@tonic-gate it->it_portfd = port; 6737c478bd9Sstevel@tonic-gate } else { 6747c478bd9Sstevel@tonic-gate if (ev.sigev_notify == SIGEV_SIGNAL) 6757c478bd9Sstevel@tonic-gate it->it_flags |= IT_SIGNAL; 6767c478bd9Sstevel@tonic-gate } 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate /* 6817c478bd9Sstevel@tonic-gate * Call on the backend to verify the event argument (or return 6827c478bd9Sstevel@tonic-gate * EINVAL if this clock type does not support timers). 6837c478bd9Sstevel@tonic-gate */ 684*6a72db4aSBryan Cantrill if ((error = backend->clk_timer_create(it, timer_fire)) != 0) 6857c478bd9Sstevel@tonic-gate goto err; 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate it->it_lwp = ttolwp(curthread); 6887c478bd9Sstevel@tonic-gate it->it_proc = p; 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate if (copyout(&i, tid, sizeof (timer_t)) != 0) { 6917c478bd9Sstevel@tonic-gate error = EFAULT; 6927c478bd9Sstevel@tonic-gate goto err; 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate /* 6967c478bd9Sstevel@tonic-gate * If we're here, then we have successfully created the timer; we 6977c478bd9Sstevel@tonic-gate * just need to release the timer and return. 6987c478bd9Sstevel@tonic-gate */ 6997c478bd9Sstevel@tonic-gate timer_release(p, it); 7007c478bd9Sstevel@tonic-gate 7017c478bd9Sstevel@tonic-gate return (0); 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate err: 7047c478bd9Sstevel@tonic-gate /* 7057c478bd9Sstevel@tonic-gate * If we're here, an error has occurred late in the timer creation 7067c478bd9Sstevel@tonic-gate * process. We need to regrab p_lock, and delete the incipient timer. 7077c478bd9Sstevel@tonic-gate * Since we never unlocked the timer (it was born locked), it's 7087c478bd9Sstevel@tonic-gate * impossible for a removal to be pending. 7097c478bd9Sstevel@tonic-gate */ 7107c478bd9Sstevel@tonic-gate ASSERT(!(it->it_lock & ITLK_REMOVE)); 7117c478bd9Sstevel@tonic-gate timer_delete_grabbed(p, i, it); 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate return (set_errno(error)); 7147c478bd9Sstevel@tonic-gate } 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate int 7177c478bd9Sstevel@tonic-gate timer_gettime(timer_t tid, itimerspec_t *val) 7187c478bd9Sstevel@tonic-gate { 7197c478bd9Sstevel@tonic-gate proc_t *p = curproc; 7207c478bd9Sstevel@tonic-gate itimer_t *it; 7217c478bd9Sstevel@tonic-gate itimerspec_t when; 7227c478bd9Sstevel@tonic-gate int error; 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate if ((it = timer_grab(p, tid)) == NULL) 7257c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate error = it->it_backend->clk_timer_gettime(it, &when); 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate timer_release(p, it); 7307c478bd9Sstevel@tonic-gate 7317c478bd9Sstevel@tonic-gate if (error == 0) { 7327c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 7337c478bd9Sstevel@tonic-gate if (copyout(&when, val, sizeof (itimerspec_t))) 7347c478bd9Sstevel@tonic-gate error = EFAULT; 7357c478bd9Sstevel@tonic-gate } else { 7367c478bd9Sstevel@tonic-gate if (ITIMERSPEC_OVERFLOW(&when)) 7377c478bd9Sstevel@tonic-gate error = EOVERFLOW; 7387c478bd9Sstevel@tonic-gate else { 7397c478bd9Sstevel@tonic-gate itimerspec32_t w32; 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate ITIMERSPEC_TO_ITIMERSPEC32(&w32, &when) 7427c478bd9Sstevel@tonic-gate if (copyout(&w32, val, sizeof (itimerspec32_t))) 7437c478bd9Sstevel@tonic-gate error = EFAULT; 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate } 7477c478bd9Sstevel@tonic-gate 7487c478bd9Sstevel@tonic-gate return (error ? set_errno(error) : 0); 7497c478bd9Sstevel@tonic-gate } 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate int 7527c478bd9Sstevel@tonic-gate timer_settime(timer_t tid, int flags, itimerspec_t *val, itimerspec_t *oval) 7537c478bd9Sstevel@tonic-gate { 7547c478bd9Sstevel@tonic-gate itimerspec_t when; 7557c478bd9Sstevel@tonic-gate itimer_t *it; 7567c478bd9Sstevel@tonic-gate proc_t *p = curproc; 7577c478bd9Sstevel@tonic-gate int error; 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate if (oval != NULL) { 7607c478bd9Sstevel@tonic-gate if ((error = timer_gettime(tid, oval)) != 0) 7617c478bd9Sstevel@tonic-gate return (error); 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate 7647c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 7657c478bd9Sstevel@tonic-gate if (copyin(val, &when, sizeof (itimerspec_t))) 7667c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 7677c478bd9Sstevel@tonic-gate } else { 7687c478bd9Sstevel@tonic-gate itimerspec32_t w32; 7697c478bd9Sstevel@tonic-gate 7707c478bd9Sstevel@tonic-gate if (copyin(val, &w32, sizeof (itimerspec32_t))) 7717c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate ITIMERSPEC32_TO_ITIMERSPEC(&when, &w32); 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate 7767c478bd9Sstevel@tonic-gate if (itimerspecfix(&when.it_value) || 7777c478bd9Sstevel@tonic-gate (itimerspecfix(&when.it_interval) && 7787c478bd9Sstevel@tonic-gate timerspecisset(&when.it_value))) { 7797c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 7807c478bd9Sstevel@tonic-gate } 7817c478bd9Sstevel@tonic-gate 7827c478bd9Sstevel@tonic-gate if ((it = timer_grab(p, tid)) == NULL) 7837c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate error = it->it_backend->clk_timer_settime(it, flags, &when); 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate timer_release(p, it); 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate return (error ? set_errno(error) : 0); 7907c478bd9Sstevel@tonic-gate } 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate int 7937c478bd9Sstevel@tonic-gate timer_delete(timer_t tid) 7947c478bd9Sstevel@tonic-gate { 7957c478bd9Sstevel@tonic-gate proc_t *p = curproc; 7967c478bd9Sstevel@tonic-gate itimer_t *it; 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate if ((it = timer_grab(p, tid)) == NULL) 7997c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate timer_delete_grabbed(p, tid, it); 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate return (0); 8047c478bd9Sstevel@tonic-gate } 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate int 8077c478bd9Sstevel@tonic-gate timer_getoverrun(timer_t tid) 8087c478bd9Sstevel@tonic-gate { 8097c478bd9Sstevel@tonic-gate int overrun; 8107c478bd9Sstevel@tonic-gate proc_t *p = curproc; 8117c478bd9Sstevel@tonic-gate itimer_t *it; 8127c478bd9Sstevel@tonic-gate 8137c478bd9Sstevel@tonic-gate if ((it = timer_grab(p, tid)) == NULL) 8147c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 8157c478bd9Sstevel@tonic-gate 8167c478bd9Sstevel@tonic-gate /* 8177c478bd9Sstevel@tonic-gate * The it_overrun field is protected by p_lock; we need to acquire 8187c478bd9Sstevel@tonic-gate * it before looking at the value. 8197c478bd9Sstevel@tonic-gate */ 8207c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 8217c478bd9Sstevel@tonic-gate overrun = it->it_overrun; 8227c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 8237c478bd9Sstevel@tonic-gate 8247c478bd9Sstevel@tonic-gate timer_release(p, it); 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate return (overrun); 8277c478bd9Sstevel@tonic-gate } 8287c478bd9Sstevel@tonic-gate 8297c478bd9Sstevel@tonic-gate /* 8307c478bd9Sstevel@tonic-gate * Entered/exited with p_lock held, but will repeatedly drop and regrab p_lock. 8317c478bd9Sstevel@tonic-gate */ 8327c478bd9Sstevel@tonic-gate void 8337c478bd9Sstevel@tonic-gate timer_lwpexit(void) 8347c478bd9Sstevel@tonic-gate { 8357c478bd9Sstevel@tonic-gate timer_t i; 8367c478bd9Sstevel@tonic-gate proc_t *p = curproc; 8377c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 8387c478bd9Sstevel@tonic-gate itimer_t *it, **itp; 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate if ((itp = p->p_itimer) == NULL) 8437c478bd9Sstevel@tonic-gate return; 8447c478bd9Sstevel@tonic-gate 8457c478bd9Sstevel@tonic-gate for (i = 0; i < timer_max; i++) { 8467c478bd9Sstevel@tonic-gate if ((it = itp[i]) == NULL) 8477c478bd9Sstevel@tonic-gate continue; 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate timer_lock(p, it); 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate if ((it->it_lock & ITLK_REMOVE) || it->it_lwp != lwp) { 8527c478bd9Sstevel@tonic-gate /* 8537c478bd9Sstevel@tonic-gate * This timer is either being removed or it isn't 8547c478bd9Sstevel@tonic-gate * associated with this lwp. 8557c478bd9Sstevel@tonic-gate */ 8567c478bd9Sstevel@tonic-gate timer_unlock(p, it); 8577c478bd9Sstevel@tonic-gate continue; 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate /* 8617c478bd9Sstevel@tonic-gate * The LWP that created this timer is going away. To the user, 8627c478bd9Sstevel@tonic-gate * our behavior here is explicitly undefined. We will simply 8637c478bd9Sstevel@tonic-gate * null out the it_lwp field; if the LWP was bound to a CPU, 8647c478bd9Sstevel@tonic-gate * the cyclic will stay bound to that CPU until the process 8657c478bd9Sstevel@tonic-gate * exits. 8667c478bd9Sstevel@tonic-gate */ 8677c478bd9Sstevel@tonic-gate it->it_lwp = NULL; 8687c478bd9Sstevel@tonic-gate timer_unlock(p, it); 8697c478bd9Sstevel@tonic-gate } 8707c478bd9Sstevel@tonic-gate } 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate /* 8737c478bd9Sstevel@tonic-gate * Called to notify of an LWP binding change. Entered/exited with p_lock 8747c478bd9Sstevel@tonic-gate * held, but will repeatedly drop and regrab p_lock. 8757c478bd9Sstevel@tonic-gate */ 8767c478bd9Sstevel@tonic-gate void 8777c478bd9Sstevel@tonic-gate timer_lwpbind() 8787c478bd9Sstevel@tonic-gate { 8797c478bd9Sstevel@tonic-gate timer_t i; 8807c478bd9Sstevel@tonic-gate proc_t *p = curproc; 8817c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 8827c478bd9Sstevel@tonic-gate itimer_t *it, **itp; 8837c478bd9Sstevel@tonic-gate 8847c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8857c478bd9Sstevel@tonic-gate 8867c478bd9Sstevel@tonic-gate if ((itp = p->p_itimer) == NULL) 8877c478bd9Sstevel@tonic-gate return; 8887c478bd9Sstevel@tonic-gate 8897c478bd9Sstevel@tonic-gate for (i = 0; i < timer_max; i++) { 8907c478bd9Sstevel@tonic-gate if ((it = itp[i]) == NULL) 8917c478bd9Sstevel@tonic-gate continue; 8927c478bd9Sstevel@tonic-gate 8937c478bd9Sstevel@tonic-gate timer_lock(p, it); 8947c478bd9Sstevel@tonic-gate 8957c478bd9Sstevel@tonic-gate if (!(it->it_lock & ITLK_REMOVE) && it->it_lwp == lwp) { 8967c478bd9Sstevel@tonic-gate /* 8977c478bd9Sstevel@tonic-gate * Drop p_lock and jump into the backend. 8987c478bd9Sstevel@tonic-gate */ 8997c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 9007c478bd9Sstevel@tonic-gate it->it_backend->clk_timer_lwpbind(it); 9017c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 9027c478bd9Sstevel@tonic-gate } 9037c478bd9Sstevel@tonic-gate 9047c478bd9Sstevel@tonic-gate timer_unlock(p, it); 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate } 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate /* 9097c478bd9Sstevel@tonic-gate * This function should only be called if p_itimer is non-NULL. 9107c478bd9Sstevel@tonic-gate */ 9117c478bd9Sstevel@tonic-gate void 9127c478bd9Sstevel@tonic-gate timer_exit(void) 9137c478bd9Sstevel@tonic-gate { 9147c478bd9Sstevel@tonic-gate timer_t i; 9157c478bd9Sstevel@tonic-gate proc_t *p = curproc; 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate ASSERT(p->p_itimer != NULL); 9187c478bd9Sstevel@tonic-gate 9197c478bd9Sstevel@tonic-gate for (i = 0; i < timer_max; i++) 9207c478bd9Sstevel@tonic-gate (void) timer_delete(i); 9217c478bd9Sstevel@tonic-gate 9227c478bd9Sstevel@tonic-gate kmem_free(p->p_itimer, timer_max * sizeof (itimer_t *)); 9237c478bd9Sstevel@tonic-gate p->p_itimer = NULL; 9247c478bd9Sstevel@tonic-gate } 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate /* 9277c478bd9Sstevel@tonic-gate * timer_port_callback() is a callback function which is associated with the 9287c478bd9Sstevel@tonic-gate * timer event and is activated just before the event is delivered to the user. 9297c478bd9Sstevel@tonic-gate * The timer uses this function to update/set the overflow counter and 9307c478bd9Sstevel@tonic-gate * to reenable the use of the event structure. 9317c478bd9Sstevel@tonic-gate */ 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate /* ARGSUSED */ 9347c478bd9Sstevel@tonic-gate static int 9357c478bd9Sstevel@tonic-gate timer_port_callback(void *arg, int *events, pid_t pid, int flag, void *evp) 9367c478bd9Sstevel@tonic-gate { 9377c478bd9Sstevel@tonic-gate itimer_t *it = arg; 9387c478bd9Sstevel@tonic-gate 9397c478bd9Sstevel@tonic-gate mutex_enter(&it->it_mutex); 9407c478bd9Sstevel@tonic-gate if (curproc != it->it_proc) { 9417c478bd9Sstevel@tonic-gate /* can not deliver timer events to another proc */ 9427c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex); 9437c478bd9Sstevel@tonic-gate return (EACCES); 9447c478bd9Sstevel@tonic-gate } 9457c478bd9Sstevel@tonic-gate *events = it->it_pending; /* 1 = 1 event, >1 # of overflows */ 9467c478bd9Sstevel@tonic-gate it->it_pending = 0; /* reinit overflow counter */ 9477c478bd9Sstevel@tonic-gate /* 9487c478bd9Sstevel@tonic-gate * This function can also be activated when the port is being closed 9497c478bd9Sstevel@tonic-gate * and a timer event is already submitted to the port. 9507c478bd9Sstevel@tonic-gate * In such a case the event port framework will use the 9517c478bd9Sstevel@tonic-gate * close-callback function to notify the events sources. 9527c478bd9Sstevel@tonic-gate * The timer close-callback function is timer_close_port() which 9537c478bd9Sstevel@tonic-gate * will free all allocated resources (including the allocated 9547c478bd9Sstevel@tonic-gate * port event structure). 9557c478bd9Sstevel@tonic-gate * For that reason we don't need to check the value of flag here. 9567c478bd9Sstevel@tonic-gate */ 9577c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex); 9587c478bd9Sstevel@tonic-gate return (0); 9597c478bd9Sstevel@tonic-gate } 9607c478bd9Sstevel@tonic-gate 9617c478bd9Sstevel@tonic-gate /* 9627c478bd9Sstevel@tonic-gate * port is being closed ... free all allocated port event structures 9637c478bd9Sstevel@tonic-gate * The delivered arg currently correspond to the first timer associated with 9647c478bd9Sstevel@tonic-gate * the port and it is not useable in this case. 9657c478bd9Sstevel@tonic-gate * We have to scan the list of activated timers in the current proc and 9667c478bd9Sstevel@tonic-gate * compare them with the delivered port id. 9677c478bd9Sstevel@tonic-gate */ 9687c478bd9Sstevel@tonic-gate 9697c478bd9Sstevel@tonic-gate /* ARGSUSED */ 9707c478bd9Sstevel@tonic-gate static void 9717c478bd9Sstevel@tonic-gate timer_close_port(void *arg, int port, pid_t pid, int lastclose) 9727c478bd9Sstevel@tonic-gate { 9737c478bd9Sstevel@tonic-gate proc_t *p = curproc; 9747c478bd9Sstevel@tonic-gate timer_t tid; 9757c478bd9Sstevel@tonic-gate itimer_t *it; 9767c478bd9Sstevel@tonic-gate 9777c478bd9Sstevel@tonic-gate for (tid = 0; tid < timer_max; tid++) { 9787c478bd9Sstevel@tonic-gate if ((it = timer_grab(p, tid)) == NULL) 9797c478bd9Sstevel@tonic-gate continue; 9807c478bd9Sstevel@tonic-gate if (it->it_portev) { 9817c478bd9Sstevel@tonic-gate mutex_enter(&it->it_mutex); 9827c478bd9Sstevel@tonic-gate if (it->it_portfd == port) { 9838b79e857Spraks port_kevent_t *pev; 9848b79e857Spraks pev = (port_kevent_t *)it->it_portev; 9857c478bd9Sstevel@tonic-gate it->it_portev = NULL; 9867c478bd9Sstevel@tonic-gate it->it_flags &= ~IT_PORT; 9877c478bd9Sstevel@tonic-gate mutex_exit(&it->it_mutex); 9888b79e857Spraks (void) port_remove_done_event(pev); 9898b79e857Spraks port_free_event(pev); 9908b79e857Spraks } else { 9918b79e857Spraks mutex_exit(&it->it_mutex); 9928b79e857Spraks } 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate timer_release(p, it); 9957c478bd9Sstevel@tonic-gate } 9967c478bd9Sstevel@tonic-gate } 997