10Sstevel@tonic-gate /* 26422Sqiao * CDDL HEADER START 36422Sqiao * 46422Sqiao * The contents of this file are subject to the terms of the 56422Sqiao * Common Development and Distribution License (the "License"). 66422Sqiao * You may not use this file except in compliance with the License. 76422Sqiao * 86422Sqiao * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 96422Sqiao * or http://www.opensolaris.org/os/licensing. 106422Sqiao * See the License for the specific language governing permissions 116422Sqiao * and limitations under the License. 126422Sqiao * 136422Sqiao * When distributing Covered Code, include this CDDL HEADER in each 146422Sqiao * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 156422Sqiao * If applicable, add the following below this CDDL HEADER, with the 166422Sqiao * fields enclosed by brackets "[]" replaced with your own identifying 176422Sqiao * information: Portions Copyright [yyyy] [name of copyright owner] 186422Sqiao * 196422Sqiao * CDDL HEADER END 206422Sqiao */ 216422Sqiao /* 226422Sqiao * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* 270Sstevel@tonic-gate * Copyright (c) 1982, 1986 Regents of the University of California. 280Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 290Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/param.h> 330Sstevel@tonic-gate #include <sys/user.h> 340Sstevel@tonic-gate #include <sys/vnode.h> 350Sstevel@tonic-gate #include <sys/proc.h> 360Sstevel@tonic-gate #include <sys/time.h> 370Sstevel@tonic-gate #include <sys/systm.h> 380Sstevel@tonic-gate #include <sys/kmem.h> 390Sstevel@tonic-gate #include <sys/cmn_err.h> 400Sstevel@tonic-gate #include <sys/cpuvar.h> 410Sstevel@tonic-gate #include <sys/timer.h> 420Sstevel@tonic-gate #include <sys/debug.h> 430Sstevel@tonic-gate #include <sys/sysmacros.h> 440Sstevel@tonic-gate #include <sys/cyclic.h> 450Sstevel@tonic-gate 460Sstevel@tonic-gate static void realitexpire(void *); 470Sstevel@tonic-gate static void realprofexpire(void *); 480Sstevel@tonic-gate static void timeval_advance(struct timeval *, struct timeval *); 490Sstevel@tonic-gate 500Sstevel@tonic-gate kmutex_t tod_lock; /* protects time-of-day stuff */ 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * Constant to define the minimum interval value of the ITIMER_REALPROF timer. 540Sstevel@tonic-gate * Value is in microseconds; defaults to 500 usecs. Setting this value 550Sstevel@tonic-gate * significantly lower may allow for denial-of-service attacks. 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate int itimer_realprof_minimum = 500; 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* 600Sstevel@tonic-gate * macro to compare a timeval to a timestruc 610Sstevel@tonic-gate */ 620Sstevel@tonic-gate 630Sstevel@tonic-gate #define TVTSCMP(tvp, tsp, cmp) \ 640Sstevel@tonic-gate /* CSTYLED */ \ 650Sstevel@tonic-gate ((tvp)->tv_sec cmp (tsp)->tv_sec || \ 660Sstevel@tonic-gate ((tvp)->tv_sec == (tsp)->tv_sec && \ 670Sstevel@tonic-gate /* CSTYLED */ \ 680Sstevel@tonic-gate (tvp)->tv_usec * 1000 cmp (tsp)->tv_nsec)) 690Sstevel@tonic-gate 700Sstevel@tonic-gate /* 710Sstevel@tonic-gate * Time of day and interval timer support. 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * These routines provide the kernel entry points to get and set 740Sstevel@tonic-gate * the time-of-day and per-process interval timers. Subroutines 750Sstevel@tonic-gate * here provide support for adding and subtracting timeval structures 760Sstevel@tonic-gate * and decrementing interval timers, optionally reloading the interval 770Sstevel@tonic-gate * timers when they expire. 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* 810Sstevel@tonic-gate * SunOS function to generate monotonically increasing time values. 820Sstevel@tonic-gate */ 830Sstevel@tonic-gate void 840Sstevel@tonic-gate uniqtime(struct timeval *tv) 850Sstevel@tonic-gate { 860Sstevel@tonic-gate static struct timeval last; 877900SBirva.Shah@Sun.COM static int last_timechanged; 880Sstevel@tonic-gate timestruc_t ts; 890Sstevel@tonic-gate time_t sec; 900Sstevel@tonic-gate int usec, nsec; 910Sstevel@tonic-gate 920Sstevel@tonic-gate /* 930Sstevel@tonic-gate * protect modification of last 940Sstevel@tonic-gate */ 950Sstevel@tonic-gate mutex_enter(&tod_lock); 960Sstevel@tonic-gate gethrestime(&ts); 970Sstevel@tonic-gate 980Sstevel@tonic-gate /* 990Sstevel@tonic-gate * Fast algorithm to convert nsec to usec -- see hrt2ts() 1000Sstevel@tonic-gate * in common/os/timers.c for a full description. 1010Sstevel@tonic-gate */ 1020Sstevel@tonic-gate nsec = ts.tv_nsec; 1030Sstevel@tonic-gate usec = nsec + (nsec >> 2); 1040Sstevel@tonic-gate usec = nsec + (usec >> 1); 1050Sstevel@tonic-gate usec = nsec + (usec >> 2); 1060Sstevel@tonic-gate usec = nsec + (usec >> 4); 1070Sstevel@tonic-gate usec = nsec - (usec >> 3); 1080Sstevel@tonic-gate usec = nsec + (usec >> 2); 1090Sstevel@tonic-gate usec = nsec + (usec >> 3); 1100Sstevel@tonic-gate usec = nsec + (usec >> 4); 1110Sstevel@tonic-gate usec = nsec + (usec >> 1); 1120Sstevel@tonic-gate usec = nsec + (usec >> 6); 1130Sstevel@tonic-gate usec = usec >> 10; 1140Sstevel@tonic-gate sec = ts.tv_sec; 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate /* 1177900SBirva.Shah@Sun.COM * If the system hres time has been changed since the last time 1187900SBirva.Shah@Sun.COM * we are called. then all bets are off; just update our 1197900SBirva.Shah@Sun.COM * local copy of timechanged and accept the reported time as is. 1207900SBirva.Shah@Sun.COM */ 1217900SBirva.Shah@Sun.COM if (last_timechanged != timechanged) { 1227900SBirva.Shah@Sun.COM last_timechanged = timechanged; 1237900SBirva.Shah@Sun.COM } 1247900SBirva.Shah@Sun.COM /* 1250Sstevel@tonic-gate * Try to keep timestamps unique, but don't be obsessive about 1260Sstevel@tonic-gate * it in the face of large differences. 1270Sstevel@tonic-gate */ 1287900SBirva.Shah@Sun.COM else if ((sec <= last.tv_sec) && /* same or lower seconds, and */ 1290Sstevel@tonic-gate ((sec != last.tv_sec) || /* either different second or */ 1300Sstevel@tonic-gate (usec <= last.tv_usec)) && /* lower microsecond, and */ 1310Sstevel@tonic-gate ((last.tv_sec - sec) <= 5)) { /* not way back in time */ 1320Sstevel@tonic-gate sec = last.tv_sec; 1330Sstevel@tonic-gate usec = last.tv_usec + 1; 1340Sstevel@tonic-gate if (usec >= MICROSEC) { 1350Sstevel@tonic-gate usec -= MICROSEC; 1360Sstevel@tonic-gate sec++; 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate last.tv_sec = sec; 1400Sstevel@tonic-gate last.tv_usec = usec; 1410Sstevel@tonic-gate mutex_exit(&tod_lock); 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate tv->tv_sec = sec; 1440Sstevel@tonic-gate tv->tv_usec = usec; 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate /* 1480Sstevel@tonic-gate * Timestamps are exported from the kernel in several places. 1490Sstevel@tonic-gate * Such timestamps are commonly used for either uniqueness or for 1500Sstevel@tonic-gate * sequencing - truncation to 32-bits is fine for uniqueness, 1510Sstevel@tonic-gate * but sequencing is going to take more work as we get closer to 2038! 1520Sstevel@tonic-gate */ 1530Sstevel@tonic-gate void 1540Sstevel@tonic-gate uniqtime32(struct timeval32 *tv32p) 1550Sstevel@tonic-gate { 1560Sstevel@tonic-gate struct timeval tv; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate uniqtime(&tv); 1590Sstevel@tonic-gate TIMEVAL_TO_TIMEVAL32(tv32p, &tv); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate int 1630Sstevel@tonic-gate gettimeofday(struct timeval *tp) 1640Sstevel@tonic-gate { 1650Sstevel@tonic-gate struct timeval atv; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate if (tp) { 1680Sstevel@tonic-gate uniqtime(&atv); 1690Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 1700Sstevel@tonic-gate if (copyout(&atv, tp, sizeof (atv))) 1710Sstevel@tonic-gate return (set_errno(EFAULT)); 1720Sstevel@tonic-gate } else { 1730Sstevel@tonic-gate struct timeval32 tv32; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate if (TIMEVAL_OVERFLOW(&atv)) 1760Sstevel@tonic-gate return (set_errno(EOVERFLOW)); 1770Sstevel@tonic-gate TIMEVAL_TO_TIMEVAL32(&tv32, &atv); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate if (copyout(&tv32, tp, sizeof (tv32))) 1800Sstevel@tonic-gate return (set_errno(EFAULT)); 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate return (0); 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate int 1870Sstevel@tonic-gate getitimer(uint_t which, struct itimerval *itv) 1880Sstevel@tonic-gate { 1890Sstevel@tonic-gate int error; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) 1920Sstevel@tonic-gate error = xgetitimer(which, itv, 0); 1930Sstevel@tonic-gate else { 1940Sstevel@tonic-gate struct itimerval kitv; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate if ((error = xgetitimer(which, &kitv, 1)) == 0) { 1970Sstevel@tonic-gate if (ITIMERVAL_OVERFLOW(&kitv)) { 1980Sstevel@tonic-gate error = EOVERFLOW; 1990Sstevel@tonic-gate } else { 2000Sstevel@tonic-gate struct itimerval32 itv32; 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate ITIMERVAL_TO_ITIMERVAL32(&itv32, &kitv); 2030Sstevel@tonic-gate if (copyout(&itv32, itv, sizeof (itv32)) != 0) 2040Sstevel@tonic-gate error = EFAULT; 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate return (error ? (set_errno(error)) : 0); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate int 2130Sstevel@tonic-gate xgetitimer(uint_t which, struct itimerval *itv, int iskaddr) 2140Sstevel@tonic-gate { 2150Sstevel@tonic-gate struct proc *p = curproc; 2160Sstevel@tonic-gate struct timeval now; 2170Sstevel@tonic-gate struct itimerval aitv; 2180Sstevel@tonic-gate hrtime_t ts, first, interval, remain; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate mutex_enter(&p->p_lock); 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate switch (which) { 2230Sstevel@tonic-gate case ITIMER_VIRTUAL: 2240Sstevel@tonic-gate case ITIMER_PROF: 2250Sstevel@tonic-gate aitv = ttolwp(curthread)->lwp_timer[which]; 2260Sstevel@tonic-gate break; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate case ITIMER_REAL: 2290Sstevel@tonic-gate uniqtime(&now); 2300Sstevel@tonic-gate aitv = p->p_realitimer; 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate if (timerisset(&aitv.it_value)) { 2330Sstevel@tonic-gate /*CSTYLED*/ 2340Sstevel@tonic-gate if (timercmp(&aitv.it_value, &now, <)) { 2350Sstevel@tonic-gate timerclear(&aitv.it_value); 2360Sstevel@tonic-gate } else { 2370Sstevel@tonic-gate timevalsub(&aitv.it_value, &now); 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate break; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate case ITIMER_REALPROF: 2430Sstevel@tonic-gate if (curproc->p_rprof_cyclic == CYCLIC_NONE) { 2440Sstevel@tonic-gate bzero(&aitv, sizeof (aitv)); 2450Sstevel@tonic-gate break; 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate aitv = curproc->p_rprof_timer; 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate first = tv2hrt(&aitv.it_value); 2510Sstevel@tonic-gate interval = tv2hrt(&aitv.it_interval); 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate if ((ts = gethrtime()) < first) { 2540Sstevel@tonic-gate /* 2550Sstevel@tonic-gate * We haven't gone off for the first time; the time 2560Sstevel@tonic-gate * remaining is simply the first time we will go 2570Sstevel@tonic-gate * off minus the current time. 2580Sstevel@tonic-gate */ 2590Sstevel@tonic-gate remain = first - ts; 2600Sstevel@tonic-gate } else { 2610Sstevel@tonic-gate if (interval == 0) { 2620Sstevel@tonic-gate /* 2630Sstevel@tonic-gate * This was set as a one-shot, and we've 2640Sstevel@tonic-gate * already gone off; there is no time 2650Sstevel@tonic-gate * remaining. 2660Sstevel@tonic-gate */ 2670Sstevel@tonic-gate remain = 0; 2680Sstevel@tonic-gate } else { 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * We have a non-zero interval; we need to 2710Sstevel@tonic-gate * determine how far we are into the current 2720Sstevel@tonic-gate * interval, and subtract that from the 2730Sstevel@tonic-gate * interval to determine the time remaining. 2740Sstevel@tonic-gate */ 2750Sstevel@tonic-gate remain = interval - ((ts - first) % interval); 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate hrt2tv(remain, &aitv.it_value); 2800Sstevel@tonic-gate break; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate default: 2830Sstevel@tonic-gate mutex_exit(&p->p_lock); 2840Sstevel@tonic-gate return (EINVAL); 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate mutex_exit(&p->p_lock); 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate if (iskaddr) { 2900Sstevel@tonic-gate bcopy(&aitv, itv, sizeof (*itv)); 2910Sstevel@tonic-gate } else { 2920Sstevel@tonic-gate ASSERT(get_udatamodel() == DATAMODEL_NATIVE); 2930Sstevel@tonic-gate if (copyout(&aitv, itv, sizeof (*itv))) 2940Sstevel@tonic-gate return (EFAULT); 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate return (0); 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate int 3020Sstevel@tonic-gate setitimer(uint_t which, struct itimerval *itv, struct itimerval *oitv) 3030Sstevel@tonic-gate { 3040Sstevel@tonic-gate int error; 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate if (oitv != NULL) 3070Sstevel@tonic-gate if ((error = getitimer(which, oitv)) != 0) 3080Sstevel@tonic-gate return (error); 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate if (itv == NULL) 3110Sstevel@tonic-gate return (0); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) 3140Sstevel@tonic-gate error = xsetitimer(which, itv, 0); 3150Sstevel@tonic-gate else { 3160Sstevel@tonic-gate struct itimerval32 itv32; 3170Sstevel@tonic-gate struct itimerval kitv; 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate if (copyin(itv, &itv32, sizeof (itv32))) 3200Sstevel@tonic-gate error = EFAULT; 3210Sstevel@tonic-gate ITIMERVAL32_TO_ITIMERVAL(&kitv, &itv32); 3220Sstevel@tonic-gate error = xsetitimer(which, &kitv, 1); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate return (error ? (set_errno(error)) : 0); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate int 3290Sstevel@tonic-gate xsetitimer(uint_t which, struct itimerval *itv, int iskaddr) 3300Sstevel@tonic-gate { 3310Sstevel@tonic-gate struct itimerval aitv; 3320Sstevel@tonic-gate struct timeval now; 3330Sstevel@tonic-gate struct proc *p = curproc; 3340Sstevel@tonic-gate kthread_t *t; 3350Sstevel@tonic-gate timeout_id_t tmp_id; 3360Sstevel@tonic-gate cyc_handler_t hdlr; 3370Sstevel@tonic-gate cyc_time_t when; 3380Sstevel@tonic-gate cyclic_id_t cyclic; 3390Sstevel@tonic-gate hrtime_t ts; 3400Sstevel@tonic-gate int min; 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate if (itv == NULL) 3430Sstevel@tonic-gate return (0); 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate if (iskaddr) { 3460Sstevel@tonic-gate bcopy(itv, &aitv, sizeof (aitv)); 3470Sstevel@tonic-gate } else { 3480Sstevel@tonic-gate ASSERT(get_udatamodel() == DATAMODEL_NATIVE); 3490Sstevel@tonic-gate if (copyin(itv, &aitv, sizeof (aitv))) 3500Sstevel@tonic-gate return (EFAULT); 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate if (which == ITIMER_REALPROF) { 3540Sstevel@tonic-gate min = MAX((int)(cyclic_getres() / (NANOSEC / MICROSEC)), 3550Sstevel@tonic-gate itimer_realprof_minimum); 3560Sstevel@tonic-gate } else { 3570Sstevel@tonic-gate min = usec_per_tick; 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate if (itimerfix(&aitv.it_value, min) || 3610Sstevel@tonic-gate (itimerfix(&aitv.it_interval, min) && timerisset(&aitv.it_value))) 3620Sstevel@tonic-gate return (EINVAL); 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate mutex_enter(&p->p_lock); 3650Sstevel@tonic-gate switch (which) { 3660Sstevel@tonic-gate case ITIMER_REAL: 3670Sstevel@tonic-gate /* 3680Sstevel@tonic-gate * The SITBUSY flag prevents conflicts with multiple 3690Sstevel@tonic-gate * threads attempting to perform setitimer(ITIMER_REAL) 3700Sstevel@tonic-gate * at the same time, even when we drop p->p_lock below. 3710Sstevel@tonic-gate * Any blocked thread returns successfully because the 3720Sstevel@tonic-gate * effect is the same as if it got here first, finished, 3730Sstevel@tonic-gate * and the other thread then came through and destroyed 3740Sstevel@tonic-gate * what it did. We are just protecting the system from 3750Sstevel@tonic-gate * malfunctioning due to the race condition. 3760Sstevel@tonic-gate */ 3770Sstevel@tonic-gate if (p->p_flag & SITBUSY) { 3780Sstevel@tonic-gate mutex_exit(&p->p_lock); 3790Sstevel@tonic-gate return (0); 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate p->p_flag |= SITBUSY; 3820Sstevel@tonic-gate while ((tmp_id = p->p_itimerid) != 0) { 3830Sstevel@tonic-gate /* 3840Sstevel@tonic-gate * Avoid deadlock in callout_delete (called from 3850Sstevel@tonic-gate * untimeout) which may go to sleep (while holding 3860Sstevel@tonic-gate * p_lock). Drop p_lock and re-acquire it after 3870Sstevel@tonic-gate * untimeout returns. Need to clear p_itimerid 3880Sstevel@tonic-gate * while holding p_lock. 3890Sstevel@tonic-gate */ 3900Sstevel@tonic-gate p->p_itimerid = 0; 3910Sstevel@tonic-gate mutex_exit(&p->p_lock); 3920Sstevel@tonic-gate (void) untimeout(tmp_id); 3930Sstevel@tonic-gate mutex_enter(&p->p_lock); 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate if (timerisset(&aitv.it_value)) { 3960Sstevel@tonic-gate uniqtime(&now); 3970Sstevel@tonic-gate timevaladd(&aitv.it_value, &now); 3980Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, 3990Sstevel@tonic-gate p, hzto(&aitv.it_value)); 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate p->p_realitimer = aitv; 4020Sstevel@tonic-gate p->p_flag &= ~SITBUSY; 4030Sstevel@tonic-gate break; 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate case ITIMER_REALPROF: 4060Sstevel@tonic-gate cyclic = p->p_rprof_cyclic; 4070Sstevel@tonic-gate p->p_rprof_cyclic = CYCLIC_NONE; 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate mutex_exit(&p->p_lock); 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate /* 4120Sstevel@tonic-gate * We're now going to acquire cpu_lock, remove the old cyclic 4130Sstevel@tonic-gate * if necessary, and add our new cyclic. 4140Sstevel@tonic-gate */ 4150Sstevel@tonic-gate mutex_enter(&cpu_lock); 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate if (cyclic != CYCLIC_NONE) 4180Sstevel@tonic-gate cyclic_remove(cyclic); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate if (!timerisset(&aitv.it_value)) { 4210Sstevel@tonic-gate /* 4220Sstevel@tonic-gate * If we were passed a value of 0, we're done. 4230Sstevel@tonic-gate */ 4240Sstevel@tonic-gate mutex_exit(&cpu_lock); 4250Sstevel@tonic-gate return (0); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate hdlr.cyh_func = realprofexpire; 4290Sstevel@tonic-gate hdlr.cyh_arg = p; 4300Sstevel@tonic-gate hdlr.cyh_level = CY_LOW_LEVEL; 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate when.cyt_when = (ts = gethrtime() + tv2hrt(&aitv.it_value)); 4330Sstevel@tonic-gate when.cyt_interval = tv2hrt(&aitv.it_interval); 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate if (when.cyt_interval == 0) { 4360Sstevel@tonic-gate /* 4370Sstevel@tonic-gate * Using the same logic as for CLOCK_HIGHRES timers, we 4380Sstevel@tonic-gate * set the interval to be INT64_MAX - when.cyt_when to 4390Sstevel@tonic-gate * effect a one-shot; see the comment in clock_highres.c 4400Sstevel@tonic-gate * for more details on why this works. 4410Sstevel@tonic-gate */ 4420Sstevel@tonic-gate when.cyt_interval = INT64_MAX - when.cyt_when; 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate cyclic = cyclic_add(&hdlr, &when); 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate mutex_exit(&cpu_lock); 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate /* 4500Sstevel@tonic-gate * We have now successfully added the cyclic. Reacquire 4510Sstevel@tonic-gate * p_lock, and see if anyone has snuck in. 4520Sstevel@tonic-gate */ 4530Sstevel@tonic-gate mutex_enter(&p->p_lock); 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate if (p->p_rprof_cyclic != CYCLIC_NONE) { 4560Sstevel@tonic-gate /* 4570Sstevel@tonic-gate * We're racing with another thread establishing an 4580Sstevel@tonic-gate * ITIMER_REALPROF interval timer. We'll let the other 4590Sstevel@tonic-gate * thread win (this is a race at the application level, 4600Sstevel@tonic-gate * so letting the other thread win is acceptable). 4610Sstevel@tonic-gate */ 4620Sstevel@tonic-gate mutex_exit(&p->p_lock); 4630Sstevel@tonic-gate mutex_enter(&cpu_lock); 4640Sstevel@tonic-gate cyclic_remove(cyclic); 4650Sstevel@tonic-gate mutex_exit(&cpu_lock); 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate return (0); 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate /* 4710Sstevel@tonic-gate * Success. Set our tracking variables in the proc structure, 4720Sstevel@tonic-gate * cancel any outstanding ITIMER_PROF, and allocate the 4730Sstevel@tonic-gate * per-thread SIGPROF buffers, if possible. 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate hrt2tv(ts, &aitv.it_value); 4760Sstevel@tonic-gate p->p_rprof_timer = aitv; 4770Sstevel@tonic-gate p->p_rprof_cyclic = cyclic; 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate t = p->p_tlist; 4800Sstevel@tonic-gate do { 4810Sstevel@tonic-gate struct itimerval *itvp; 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate itvp = &ttolwp(t)->lwp_timer[ITIMER_PROF]; 4840Sstevel@tonic-gate timerclear(&itvp->it_interval); 4850Sstevel@tonic-gate timerclear(&itvp->it_value); 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate if (t->t_rprof != NULL) 4880Sstevel@tonic-gate continue; 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate t->t_rprof = 4910Sstevel@tonic-gate kmem_zalloc(sizeof (struct rprof), KM_NOSLEEP); 4920Sstevel@tonic-gate aston(t); 4930Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate break; 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate case ITIMER_VIRTUAL: 4980Sstevel@tonic-gate ttolwp(curthread)->lwp_timer[ITIMER_VIRTUAL] = aitv; 4990Sstevel@tonic-gate break; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate case ITIMER_PROF: 5020Sstevel@tonic-gate if (p->p_rprof_cyclic != CYCLIC_NONE) { 5030Sstevel@tonic-gate /* 5040Sstevel@tonic-gate * Silently ignore ITIMER_PROF if ITIMER_REALPROF 5050Sstevel@tonic-gate * is in effect. 5060Sstevel@tonic-gate */ 5070Sstevel@tonic-gate break; 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate ttolwp(curthread)->lwp_timer[ITIMER_PROF] = aitv; 5110Sstevel@tonic-gate break; 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate default: 5140Sstevel@tonic-gate mutex_exit(&p->p_lock); 5150Sstevel@tonic-gate return (EINVAL); 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate mutex_exit(&p->p_lock); 5180Sstevel@tonic-gate return (0); 5190Sstevel@tonic-gate } 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate /* 5220Sstevel@tonic-gate * Real interval timer expired: 5230Sstevel@tonic-gate * send process whose timer expired an alarm signal. 5240Sstevel@tonic-gate * If time is not set up to reload, then just return. 5250Sstevel@tonic-gate * Else compute next time timer should go off which is > current time. 5260Sstevel@tonic-gate * This is where delay in processing this timeout causes multiple 5270Sstevel@tonic-gate * SIGALRM calls to be compressed into one. 5280Sstevel@tonic-gate */ 5290Sstevel@tonic-gate static void 5300Sstevel@tonic-gate realitexpire(void *arg) 5310Sstevel@tonic-gate { 5320Sstevel@tonic-gate struct proc *p = arg; 5330Sstevel@tonic-gate struct timeval *valp = &p->p_realitimer.it_value; 5340Sstevel@tonic-gate struct timeval *intervalp = &p->p_realitimer.it_interval; 5350Sstevel@tonic-gate #if !defined(_LP64) 5360Sstevel@tonic-gate clock_t ticks; 5370Sstevel@tonic-gate #endif 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate mutex_enter(&p->p_lock); 5400Sstevel@tonic-gate #if !defined(_LP64) 5410Sstevel@tonic-gate if ((ticks = hzto(valp)) > 1) { 5420Sstevel@tonic-gate /* 5430Sstevel@tonic-gate * If we are executing before we were meant to, it must be 5440Sstevel@tonic-gate * because of an overflow in a prior hzto() calculation. 5450Sstevel@tonic-gate * In this case, we want to go to sleep for the recalculated 5460Sstevel@tonic-gate * number of ticks. For the special meaning of the value "1" 5470Sstevel@tonic-gate * see comment in timespectohz(). 5480Sstevel@tonic-gate */ 5490Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, p, ticks); 5500Sstevel@tonic-gate mutex_exit(&p->p_lock); 5510Sstevel@tonic-gate return; 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate #endif 5540Sstevel@tonic-gate sigtoproc(p, NULL, SIGALRM); 5550Sstevel@tonic-gate if (!timerisset(intervalp)) { 5560Sstevel@tonic-gate timerclear(valp); 5570Sstevel@tonic-gate p->p_itimerid = 0; 5580Sstevel@tonic-gate } else { 5590Sstevel@tonic-gate /* advance timer value past current time */ 5600Sstevel@tonic-gate timeval_advance(valp, intervalp); 5610Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, p, hzto(valp)); 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate mutex_exit(&p->p_lock); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate /* 5670Sstevel@tonic-gate * Real time profiling interval timer expired: 5680Sstevel@tonic-gate * Increment microstate counters for each lwp in the process 5690Sstevel@tonic-gate * and ensure that running lwps are kicked into the kernel. 5700Sstevel@tonic-gate * If time is not set up to reload, then just return. 5710Sstevel@tonic-gate * Else compute next time timer should go off which is > current time, 5720Sstevel@tonic-gate * as above. 5730Sstevel@tonic-gate */ 5740Sstevel@tonic-gate static void 5750Sstevel@tonic-gate realprofexpire(void *arg) 5760Sstevel@tonic-gate { 5770Sstevel@tonic-gate struct proc *p = arg; 5780Sstevel@tonic-gate kthread_t *t; 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate mutex_enter(&p->p_lock); 5810Sstevel@tonic-gate if ((t = p->p_tlist) == NULL) { 5820Sstevel@tonic-gate mutex_exit(&p->p_lock); 5830Sstevel@tonic-gate return; 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate do { 5860Sstevel@tonic-gate int mstate; 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate /* 5890Sstevel@tonic-gate * Attempt to allocate the SIGPROF buffer, but don't sleep. 5900Sstevel@tonic-gate */ 5910Sstevel@tonic-gate if (t->t_rprof == NULL) 5920Sstevel@tonic-gate t->t_rprof = kmem_zalloc(sizeof (struct rprof), 5930Sstevel@tonic-gate KM_NOSLEEP); 5940Sstevel@tonic-gate if (t->t_rprof == NULL) 5950Sstevel@tonic-gate continue; 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate thread_lock(t); 5980Sstevel@tonic-gate switch (t->t_state) { 5990Sstevel@tonic-gate case TS_SLEEP: 6000Sstevel@tonic-gate /* 6010Sstevel@tonic-gate * Don't touch the lwp is it is swapped out. 6020Sstevel@tonic-gate */ 6030Sstevel@tonic-gate if (!(t->t_schedflag & TS_LOAD)) { 6040Sstevel@tonic-gate mstate = LMS_SLEEP; 6050Sstevel@tonic-gate break; 6060Sstevel@tonic-gate } 6070Sstevel@tonic-gate switch (mstate = ttolwp(t)->lwp_mstate.ms_prev) { 6080Sstevel@tonic-gate case LMS_TFAULT: 6090Sstevel@tonic-gate case LMS_DFAULT: 6100Sstevel@tonic-gate case LMS_KFAULT: 6110Sstevel@tonic-gate case LMS_USER_LOCK: 6120Sstevel@tonic-gate break; 6130Sstevel@tonic-gate default: 6140Sstevel@tonic-gate mstate = LMS_SLEEP; 6150Sstevel@tonic-gate break; 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate break; 6180Sstevel@tonic-gate case TS_RUN: 6193792Sakolb case TS_WAIT: 6200Sstevel@tonic-gate mstate = LMS_WAIT_CPU; 6210Sstevel@tonic-gate break; 6220Sstevel@tonic-gate case TS_ONPROC: 6230Sstevel@tonic-gate switch (mstate = t->t_mstate) { 6240Sstevel@tonic-gate case LMS_USER: 6250Sstevel@tonic-gate case LMS_SYSTEM: 6260Sstevel@tonic-gate case LMS_TRAP: 6270Sstevel@tonic-gate break; 6280Sstevel@tonic-gate default: 6290Sstevel@tonic-gate mstate = LMS_SYSTEM; 6300Sstevel@tonic-gate break; 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate break; 6330Sstevel@tonic-gate default: 6340Sstevel@tonic-gate mstate = t->t_mstate; 6350Sstevel@tonic-gate break; 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate t->t_rprof->rp_anystate = 1; 6380Sstevel@tonic-gate t->t_rprof->rp_state[mstate]++; 6390Sstevel@tonic-gate aston(t); 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * force the thread into the kernel 6420Sstevel@tonic-gate * if it is not already there. 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate if (t->t_state == TS_ONPROC && t->t_cpu != CPU) 6450Sstevel@tonic-gate poke_cpu(t->t_cpu->cpu_id); 6460Sstevel@tonic-gate thread_unlock(t); 6470Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate mutex_exit(&p->p_lock); 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate /* 6530Sstevel@tonic-gate * Advances timer value past the current time of day. See the detailed 6540Sstevel@tonic-gate * comment for this logic in realitsexpire(), above. 6550Sstevel@tonic-gate */ 6560Sstevel@tonic-gate static void 6570Sstevel@tonic-gate timeval_advance(struct timeval *valp, struct timeval *intervalp) 6580Sstevel@tonic-gate { 6590Sstevel@tonic-gate int cnt2nth; 6600Sstevel@tonic-gate struct timeval interval2nth; 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate for (;;) { 6630Sstevel@tonic-gate interval2nth = *intervalp; 6640Sstevel@tonic-gate for (cnt2nth = 0; ; cnt2nth++) { 6650Sstevel@tonic-gate timevaladd(valp, &interval2nth); 6660Sstevel@tonic-gate /*CSTYLED*/ 6670Sstevel@tonic-gate if (TVTSCMP(valp, &hrestime, >)) 6680Sstevel@tonic-gate break; 6690Sstevel@tonic-gate timevaladd(&interval2nth, &interval2nth); 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate if (cnt2nth == 0) 6720Sstevel@tonic-gate break; 6730Sstevel@tonic-gate timevalsub(valp, &interval2nth); 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate /* 6780Sstevel@tonic-gate * Check that a proposed value to load into the .it_value or .it_interval 6790Sstevel@tonic-gate * part of an interval timer is acceptable, and set it to at least a 6800Sstevel@tonic-gate * specified minimal value. 6810Sstevel@tonic-gate */ 6820Sstevel@tonic-gate int 6830Sstevel@tonic-gate itimerfix(struct timeval *tv, int minimum) 6840Sstevel@tonic-gate { 6850Sstevel@tonic-gate if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 6860Sstevel@tonic-gate tv->tv_usec < 0 || tv->tv_usec >= MICROSEC) 6870Sstevel@tonic-gate return (EINVAL); 6880Sstevel@tonic-gate if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < minimum) 6890Sstevel@tonic-gate tv->tv_usec = minimum; 6900Sstevel@tonic-gate return (0); 6910Sstevel@tonic-gate } 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate /* 6940Sstevel@tonic-gate * Same as itimerfix, except a) it takes a timespec instead of a timeval and 6950Sstevel@tonic-gate * b) it doesn't truncate based on timeout granularity; consumers of this 6960Sstevel@tonic-gate * interface (e.g. timer_settime()) depend on the passed timespec not being 6970Sstevel@tonic-gate * modified implicitly. 6980Sstevel@tonic-gate */ 6990Sstevel@tonic-gate int 7000Sstevel@tonic-gate itimerspecfix(timespec_t *tv) 7010Sstevel@tonic-gate { 7020Sstevel@tonic-gate if (tv->tv_sec < 0 || tv->tv_nsec < 0 || tv->tv_nsec >= NANOSEC) 7030Sstevel@tonic-gate return (EINVAL); 7040Sstevel@tonic-gate return (0); 7050Sstevel@tonic-gate } 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate /* 7080Sstevel@tonic-gate * Decrement an interval timer by a specified number 7090Sstevel@tonic-gate * of microseconds, which must be less than a second, 7100Sstevel@tonic-gate * i.e. < 1000000. If the timer expires, then reload 7110Sstevel@tonic-gate * it. In this case, carry over (usec - old value) to 7120Sstevel@tonic-gate * reducint the value reloaded into the timer so that 7130Sstevel@tonic-gate * the timer does not drift. This routine assumes 7140Sstevel@tonic-gate * that it is called in a context where the timers 7150Sstevel@tonic-gate * on which it is operating cannot change in value. 7160Sstevel@tonic-gate */ 7170Sstevel@tonic-gate int 7180Sstevel@tonic-gate itimerdecr(struct itimerval *itp, int usec) 7190Sstevel@tonic-gate { 7200Sstevel@tonic-gate if (itp->it_value.tv_usec < usec) { 7210Sstevel@tonic-gate if (itp->it_value.tv_sec == 0) { 7220Sstevel@tonic-gate /* expired, and already in next interval */ 7230Sstevel@tonic-gate usec -= itp->it_value.tv_usec; 7240Sstevel@tonic-gate goto expire; 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate itp->it_value.tv_usec += MICROSEC; 7270Sstevel@tonic-gate itp->it_value.tv_sec--; 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate itp->it_value.tv_usec -= usec; 7300Sstevel@tonic-gate usec = 0; 7310Sstevel@tonic-gate if (timerisset(&itp->it_value)) 7320Sstevel@tonic-gate return (1); 7330Sstevel@tonic-gate /* expired, exactly at end of interval */ 7340Sstevel@tonic-gate expire: 7350Sstevel@tonic-gate if (timerisset(&itp->it_interval)) { 7360Sstevel@tonic-gate itp->it_value = itp->it_interval; 7370Sstevel@tonic-gate itp->it_value.tv_usec -= usec; 7380Sstevel@tonic-gate if (itp->it_value.tv_usec < 0) { 7390Sstevel@tonic-gate itp->it_value.tv_usec += MICROSEC; 7400Sstevel@tonic-gate itp->it_value.tv_sec--; 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate } else 7430Sstevel@tonic-gate itp->it_value.tv_usec = 0; /* sec is already 0 */ 7440Sstevel@tonic-gate return (0); 7450Sstevel@tonic-gate } 7460Sstevel@tonic-gate 7470Sstevel@tonic-gate /* 7480Sstevel@tonic-gate * Add and subtract routines for timevals. 7490Sstevel@tonic-gate * N.B.: subtract routine doesn't deal with 7500Sstevel@tonic-gate * results which are before the beginning, 7510Sstevel@tonic-gate * it just gets very confused in this case. 7520Sstevel@tonic-gate * Caveat emptor. 7530Sstevel@tonic-gate */ 7540Sstevel@tonic-gate void 7550Sstevel@tonic-gate timevaladd(struct timeval *t1, struct timeval *t2) 7560Sstevel@tonic-gate { 7570Sstevel@tonic-gate t1->tv_sec += t2->tv_sec; 7580Sstevel@tonic-gate t1->tv_usec += t2->tv_usec; 7590Sstevel@tonic-gate timevalfix(t1); 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate void 7630Sstevel@tonic-gate timevalsub(struct timeval *t1, struct timeval *t2) 7640Sstevel@tonic-gate { 7650Sstevel@tonic-gate t1->tv_sec -= t2->tv_sec; 7660Sstevel@tonic-gate t1->tv_usec -= t2->tv_usec; 7670Sstevel@tonic-gate timevalfix(t1); 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate void 7710Sstevel@tonic-gate timevalfix(struct timeval *t1) 7720Sstevel@tonic-gate { 7730Sstevel@tonic-gate if (t1->tv_usec < 0) { 7740Sstevel@tonic-gate t1->tv_sec--; 7750Sstevel@tonic-gate t1->tv_usec += MICROSEC; 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate if (t1->tv_usec >= MICROSEC) { 7780Sstevel@tonic-gate t1->tv_sec++; 7790Sstevel@tonic-gate t1->tv_usec -= MICROSEC; 7800Sstevel@tonic-gate } 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate /* 7840Sstevel@tonic-gate * Same as the routines above. These routines take a timespec instead 7850Sstevel@tonic-gate * of a timeval. 7860Sstevel@tonic-gate */ 7870Sstevel@tonic-gate void 7880Sstevel@tonic-gate timespecadd(timespec_t *t1, timespec_t *t2) 7890Sstevel@tonic-gate { 7900Sstevel@tonic-gate t1->tv_sec += t2->tv_sec; 7910Sstevel@tonic-gate t1->tv_nsec += t2->tv_nsec; 7920Sstevel@tonic-gate timespecfix(t1); 7930Sstevel@tonic-gate } 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate void 7960Sstevel@tonic-gate timespecsub(timespec_t *t1, timespec_t *t2) 7970Sstevel@tonic-gate { 7980Sstevel@tonic-gate t1->tv_sec -= t2->tv_sec; 7990Sstevel@tonic-gate t1->tv_nsec -= t2->tv_nsec; 8000Sstevel@tonic-gate timespecfix(t1); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate void 8040Sstevel@tonic-gate timespecfix(timespec_t *t1) 8050Sstevel@tonic-gate { 8060Sstevel@tonic-gate if (t1->tv_nsec < 0) { 8070Sstevel@tonic-gate t1->tv_sec--; 8080Sstevel@tonic-gate t1->tv_nsec += NANOSEC; 8090Sstevel@tonic-gate } else { 8100Sstevel@tonic-gate if (t1->tv_nsec >= NANOSEC) { 8110Sstevel@tonic-gate t1->tv_sec++; 8120Sstevel@tonic-gate t1->tv_nsec -= NANOSEC; 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate } 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate /* 8180Sstevel@tonic-gate * Compute number of hz until specified time. 8190Sstevel@tonic-gate * Used to compute third argument to timeout() from an absolute time. 8200Sstevel@tonic-gate */ 8210Sstevel@tonic-gate clock_t 8220Sstevel@tonic-gate hzto(struct timeval *tv) 8230Sstevel@tonic-gate { 8240Sstevel@tonic-gate timespec_t ts, now; 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate ts.tv_sec = tv->tv_sec; 8270Sstevel@tonic-gate ts.tv_nsec = tv->tv_usec * 1000; 8280Sstevel@tonic-gate gethrestime_lasttick(&now); 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate return (timespectohz(&ts, now)); 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate /* 8340Sstevel@tonic-gate * Compute number of hz until specified time for a given timespec value. 8350Sstevel@tonic-gate * Used to compute third argument to timeout() from an absolute time. 8360Sstevel@tonic-gate */ 8370Sstevel@tonic-gate clock_t 8380Sstevel@tonic-gate timespectohz(timespec_t *tv, timespec_t now) 8390Sstevel@tonic-gate { 8400Sstevel@tonic-gate clock_t ticks; 8410Sstevel@tonic-gate time_t sec; 8420Sstevel@tonic-gate int nsec; 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate /* 8450Sstevel@tonic-gate * Compute number of ticks we will see between now and 8460Sstevel@tonic-gate * the target time; returns "1" if the destination time 8470Sstevel@tonic-gate * is before the next tick, so we always get some delay, 8480Sstevel@tonic-gate * and returns LONG_MAX ticks if we would overflow. 8490Sstevel@tonic-gate */ 8500Sstevel@tonic-gate sec = tv->tv_sec - now.tv_sec; 8510Sstevel@tonic-gate nsec = tv->tv_nsec - now.tv_nsec + nsec_per_tick - 1; 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate if (nsec < 0) { 8540Sstevel@tonic-gate sec--; 8550Sstevel@tonic-gate nsec += NANOSEC; 8560Sstevel@tonic-gate } else if (nsec >= NANOSEC) { 8570Sstevel@tonic-gate sec++; 8580Sstevel@tonic-gate nsec -= NANOSEC; 8590Sstevel@tonic-gate } 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate ticks = NSEC_TO_TICK(nsec); 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate /* 8640Sstevel@tonic-gate * Compute ticks, accounting for negative and overflow as above. 8650Sstevel@tonic-gate * Overflow protection kicks in at about 70 weeks for hz=50 8660Sstevel@tonic-gate * and at about 35 weeks for hz=100. (Rather longer for the 64-bit 8670Sstevel@tonic-gate * kernel :-) 8680Sstevel@tonic-gate */ 8690Sstevel@tonic-gate if (sec < 0 || (sec == 0 && ticks < 1)) 8700Sstevel@tonic-gate ticks = 1; /* protect vs nonpositive */ 8710Sstevel@tonic-gate else if (sec > (LONG_MAX - ticks) / hz) 8720Sstevel@tonic-gate ticks = LONG_MAX; /* protect vs overflow */ 8730Sstevel@tonic-gate else 8740Sstevel@tonic-gate ticks += sec * hz; /* common case */ 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate return (ticks); 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate /* 8806422Sqiao * Compute number of hz with the timespec tv specified. 8816422Sqiao * The return type must be 64 bit integer. 8824123Sdm120769 */ 8836422Sqiao int64_t 8846422Sqiao timespectohz64(timespec_t *tv) 8854123Sdm120769 { 8866422Sqiao int64_t ticks; 8876422Sqiao int64_t sec; 8886422Sqiao int64_t nsec; 8896422Sqiao 8906422Sqiao sec = tv->tv_sec; 8916422Sqiao nsec = tv->tv_nsec + nsec_per_tick - 1; 8924123Sdm120769 8936422Sqiao if (nsec < 0) { 8946422Sqiao sec--; 8956422Sqiao nsec += NANOSEC; 8966422Sqiao } else if (nsec >= NANOSEC) { 8976422Sqiao sec++; 8986422Sqiao nsec -= NANOSEC; 8996422Sqiao } 9006422Sqiao 9016422Sqiao ticks = NSEC_TO_TICK(nsec); 9026422Sqiao 9036422Sqiao /* 9046422Sqiao * Compute ticks, accounting for negative and overflow as above. 9056422Sqiao * Overflow protection kicks in at about 70 weeks for hz=50 9066422Sqiao * and at about 35 weeks for hz=100. (Rather longer for the 64-bit 9076422Sqiao * kernel 9086422Sqiao */ 9096422Sqiao if (sec < 0 || (sec == 0 && ticks < 1)) 9106422Sqiao ticks = 1; /* protect vs nonpositive */ 9116422Sqiao else if (sec > (((~0ULL) >> 1) - ticks) / hz) 9126422Sqiao ticks = (~0ULL) >> 1; /* protect vs overflow */ 9136422Sqiao else 9146422Sqiao ticks += sec * hz; /* common case */ 9156422Sqiao 9166422Sqiao return (ticks); 9174123Sdm120769 } 9184123Sdm120769 9194123Sdm120769 /* 9200Sstevel@tonic-gate * hrt2ts(): convert from hrtime_t to timestruc_t. 9210Sstevel@tonic-gate * 9220Sstevel@tonic-gate * All this routine really does is: 9230Sstevel@tonic-gate * 9240Sstevel@tonic-gate * tsp->sec = hrt / NANOSEC; 9250Sstevel@tonic-gate * tsp->nsec = hrt % NANOSEC; 9260Sstevel@tonic-gate * 9270Sstevel@tonic-gate * The black magic below avoids doing a 64-bit by 32-bit integer divide, 9280Sstevel@tonic-gate * which is quite expensive. There's actually much more going on here than 9290Sstevel@tonic-gate * it might first appear -- don't try this at home. 9300Sstevel@tonic-gate * 9310Sstevel@tonic-gate * For the adventuresome, here's an explanation of how it works. 9320Sstevel@tonic-gate * 9330Sstevel@tonic-gate * Multiplication by a fixed constant is easy -- you just do the appropriate 9340Sstevel@tonic-gate * shifts and adds. For example, to multiply by 10, we observe that 9350Sstevel@tonic-gate * 9360Sstevel@tonic-gate * x * 10 = x * (8 + 2) 9370Sstevel@tonic-gate * = (x * 8) + (x * 2) 9380Sstevel@tonic-gate * = (x << 3) + (x << 1). 9390Sstevel@tonic-gate * 9400Sstevel@tonic-gate * In general, you can read the algorithm right off the bits: the number 10 9410Sstevel@tonic-gate * is 1010 in binary; bits 1 and 3 are ones, so x * 10 = (x << 1) + (x << 3). 9420Sstevel@tonic-gate * 9430Sstevel@tonic-gate * Sometimes you can do better. For example, 15 is 1111 binary, so the normal 9440Sstevel@tonic-gate * shift/add computation is x * 15 = (x << 0) + (x << 1) + (x << 2) + (x << 3). 9450Sstevel@tonic-gate * But, it's cheaper if you capitalize on the fact that you have a run of ones: 9460Sstevel@tonic-gate * 1111 = 10000 - 1, hence x * 15 = (x << 4) - (x << 0). [You would never 9470Sstevel@tonic-gate * actually perform the operation << 0, since it's a no-op; I'm just writing 9480Sstevel@tonic-gate * it that way for clarity.] 9490Sstevel@tonic-gate * 9500Sstevel@tonic-gate * The other way you can win is if you get lucky with the prime factorization 9510Sstevel@tonic-gate * of your constant. The number 1,000,000,000, which we have to multiply 9520Sstevel@tonic-gate * by below, is a good example. One billion is 111011100110101100101000000000 9530Sstevel@tonic-gate * in binary. If you apply the bit-grouping trick, it doesn't buy you very 9540Sstevel@tonic-gate * much, because it's only a win for groups of three or more equal bits: 9550Sstevel@tonic-gate * 9560Sstevel@tonic-gate * 111011100110101100101000000000 = 1000000000000000000000000000000 9570Sstevel@tonic-gate * - 000100011001010011011000000000 9580Sstevel@tonic-gate * 9590Sstevel@tonic-gate * Thus, instead of the 13 shift/add pairs (26 operations) implied by the LHS, 9600Sstevel@tonic-gate * we have reduced this to 10 shift/add pairs (20 operations) on the RHS. 9610Sstevel@tonic-gate * This is better, but not great. 9620Sstevel@tonic-gate * 9630Sstevel@tonic-gate * However, we can factor 1,000,000,000 = 2^9 * 5^9 = 2^9 * 125 * 125 * 125, 9640Sstevel@tonic-gate * and multiply by each factor. Multiplication by 125 is particularly easy, 9650Sstevel@tonic-gate * since 128 is nearby: x * 125 = (x << 7) - x - x - x, which is just four 9660Sstevel@tonic-gate * operations. So, to multiply by 1,000,000,000, we perform three multipli- 9670Sstevel@tonic-gate * cations by 125, then << 9, a total of only 3 * 4 + 1 = 13 operations. 9680Sstevel@tonic-gate * This is the algorithm we actually use in both hrt2ts() and ts2hrt(). 9690Sstevel@tonic-gate * 9700Sstevel@tonic-gate * Division is harder; there is no equivalent of the simple shift-add algorithm 9710Sstevel@tonic-gate * we used for multiplication. However, we can convert the division problem 9720Sstevel@tonic-gate * into a multiplication problem by pre-computing the binary representation 9730Sstevel@tonic-gate * of the reciprocal of the divisor. For the case of interest, we have 9740Sstevel@tonic-gate * 9750Sstevel@tonic-gate * 1 / 1,000,000,000 = 1.0001001011100000101111101000001B-30, 9760Sstevel@tonic-gate * 9770Sstevel@tonic-gate * to 32 bits of precision. (The notation B-30 means "* 2^-30", just like 9780Sstevel@tonic-gate * E-18 means "* 10^-18".) 9790Sstevel@tonic-gate * 9800Sstevel@tonic-gate * So, to compute x / 1,000,000,000, we just multiply x by the 32-bit 9810Sstevel@tonic-gate * integer 10001001011100000101111101000001, then normalize (shift) the 9820Sstevel@tonic-gate * result. This constant has several large bits runs, so the multiply 9830Sstevel@tonic-gate * is relatively cheap: 9840Sstevel@tonic-gate * 9850Sstevel@tonic-gate * 10001001011100000101111101000001 = 10001001100000000110000001000001 9860Sstevel@tonic-gate * - 00000000000100000000000100000000 9870Sstevel@tonic-gate * 9880Sstevel@tonic-gate * Again, you can just read the algorithm right off the bits: 9890Sstevel@tonic-gate * 9900Sstevel@tonic-gate * sec = hrt; 9910Sstevel@tonic-gate * sec += (hrt << 6); 9920Sstevel@tonic-gate * sec -= (hrt << 8); 9930Sstevel@tonic-gate * sec += (hrt << 13); 9940Sstevel@tonic-gate * sec += (hrt << 14); 9950Sstevel@tonic-gate * sec -= (hrt << 20); 9960Sstevel@tonic-gate * sec += (hrt << 23); 9970Sstevel@tonic-gate * sec += (hrt << 24); 9980Sstevel@tonic-gate * sec += (hrt << 27); 9990Sstevel@tonic-gate * sec += (hrt << 31); 10000Sstevel@tonic-gate * sec >>= (32 + 30); 10010Sstevel@tonic-gate * 10020Sstevel@tonic-gate * Voila! The only problem is, since hrt is 64 bits, we need to use 96-bit 10030Sstevel@tonic-gate * arithmetic to perform this calculation. That's a waste, because ultimately 10040Sstevel@tonic-gate * we only need the highest 32 bits of the result. 10050Sstevel@tonic-gate * 10060Sstevel@tonic-gate * The first thing we do is to realize that we don't need to use all of hrt 10070Sstevel@tonic-gate * in the calculation. The lowest 30 bits can contribute at most 1 to the 10080Sstevel@tonic-gate * quotient (2^30 / 1,000,000,000 = 1.07...), so we'll deal with them later. 10090Sstevel@tonic-gate * The highest 2 bits have to be zero, or hrt won't fit in a timestruc_t. 10100Sstevel@tonic-gate * Thus, the only bits of hrt that matter for division are bits 30..61. 10110Sstevel@tonic-gate * These 32 bits are just the lower-order word of (hrt >> 30). This brings 10120Sstevel@tonic-gate * us down from 96-bit math to 64-bit math, and our algorithm becomes: 10130Sstevel@tonic-gate * 10140Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30); 10150Sstevel@tonic-gate * sec = tmp; 10160Sstevel@tonic-gate * sec += (tmp << 6); 10170Sstevel@tonic-gate * sec -= (tmp << 8); 10180Sstevel@tonic-gate * sec += (tmp << 13); 10190Sstevel@tonic-gate * sec += (tmp << 14); 10200Sstevel@tonic-gate * sec -= (tmp << 20); 10210Sstevel@tonic-gate * sec += (tmp << 23); 10220Sstevel@tonic-gate * sec += (tmp << 24); 10230Sstevel@tonic-gate * sec += (tmp << 27); 10240Sstevel@tonic-gate * sec += (tmp << 31); 10250Sstevel@tonic-gate * sec >>= 32; 10260Sstevel@tonic-gate * 10270Sstevel@tonic-gate * Next, we're going to reduce this 64-bit computation to a 32-bit 10280Sstevel@tonic-gate * computation. We begin by rewriting the above algorithm to use relative 10290Sstevel@tonic-gate * shifts instead of absolute shifts. That is, instead of computing 10300Sstevel@tonic-gate * tmp << 6, tmp << 8, tmp << 13, etc, we'll just shift incrementally: 10310Sstevel@tonic-gate * tmp <<= 6, tmp <<= 2 (== 8 - 6), tmp <<= 5 (== 13 - 8), etc: 10320Sstevel@tonic-gate * 10330Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30); 10340Sstevel@tonic-gate * sec = tmp; 10350Sstevel@tonic-gate * tmp <<= 6; sec += tmp; 10360Sstevel@tonic-gate * tmp <<= 2; sec -= tmp; 10370Sstevel@tonic-gate * tmp <<= 5; sec += tmp; 10380Sstevel@tonic-gate * tmp <<= 1; sec += tmp; 10390Sstevel@tonic-gate * tmp <<= 6; sec -= tmp; 10400Sstevel@tonic-gate * tmp <<= 3; sec += tmp; 10410Sstevel@tonic-gate * tmp <<= 1; sec += tmp; 10420Sstevel@tonic-gate * tmp <<= 3; sec += tmp; 10430Sstevel@tonic-gate * tmp <<= 4; sec += tmp; 10440Sstevel@tonic-gate * sec >>= 32; 10450Sstevel@tonic-gate * 10460Sstevel@tonic-gate * Now for the final step. Instead of throwing away the low 32 bits at 10470Sstevel@tonic-gate * the end, we can throw them away as we go, only keeping the high 32 bits 10480Sstevel@tonic-gate * of the product at each step. So, for example, where we now have 10490Sstevel@tonic-gate * 10500Sstevel@tonic-gate * tmp <<= 6; sec = sec + tmp; 10510Sstevel@tonic-gate * we will instead have 10520Sstevel@tonic-gate * tmp <<= 6; sec = (sec + tmp) >> 6; 10530Sstevel@tonic-gate * which is equivalent to 10540Sstevel@tonic-gate * sec = (sec >> 6) + tmp; 10550Sstevel@tonic-gate * 10560Sstevel@tonic-gate * The final shift ("sec >>= 32") goes away. 10570Sstevel@tonic-gate * 10580Sstevel@tonic-gate * All we're really doing here is long multiplication, just like we learned in 10590Sstevel@tonic-gate * grade school, except that at each step, we only look at the leftmost 32 10600Sstevel@tonic-gate * columns. The cumulative error is, at most, the sum of all the bits we 10610Sstevel@tonic-gate * throw away, which is 2^-32 + 2^-31 + ... + 2^-2 + 2^-1 == 1 - 2^-32. 10620Sstevel@tonic-gate * Thus, the final result ("sec") is correct to +/- 1. 10630Sstevel@tonic-gate * 10640Sstevel@tonic-gate * It turns out to be important to keep "sec" positive at each step, because 10650Sstevel@tonic-gate * we don't want to have to explicitly extend the sign bit. Therefore, 10660Sstevel@tonic-gate * starting with the last line of code above, each line that would have read 10670Sstevel@tonic-gate * "sec = (sec >> n) - tmp" must be changed to "sec = tmp - (sec >> n)", and 10680Sstevel@tonic-gate * the operators (+ or -) in all previous lines must be toggled accordingly. 10690Sstevel@tonic-gate * Thus, we end up with: 10700Sstevel@tonic-gate * 10710Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30); 10720Sstevel@tonic-gate * sec = tmp + (sec >> 6); 10730Sstevel@tonic-gate * sec = tmp - (tmp >> 2); 10740Sstevel@tonic-gate * sec = tmp - (sec >> 5); 10750Sstevel@tonic-gate * sec = tmp + (sec >> 1); 10760Sstevel@tonic-gate * sec = tmp - (sec >> 6); 10770Sstevel@tonic-gate * sec = tmp - (sec >> 3); 10780Sstevel@tonic-gate * sec = tmp + (sec >> 1); 10790Sstevel@tonic-gate * sec = tmp + (sec >> 3); 10800Sstevel@tonic-gate * sec = tmp + (sec >> 4); 10810Sstevel@tonic-gate * 10820Sstevel@tonic-gate * This yields a value for sec that is accurate to +1/-1, so we have two 10830Sstevel@tonic-gate * cases to deal with. The mysterious-looking "+ 7" in the code below biases 10840Sstevel@tonic-gate * the rounding toward zero, so that sec is always less than or equal to 10850Sstevel@tonic-gate * the correct value. With this modified code, sec is accurate to +0/-2, with 10860Sstevel@tonic-gate * the -2 case being very rare in practice. With this change, we only have to 10870Sstevel@tonic-gate * deal with one case (sec too small) in the cleanup code. 10880Sstevel@tonic-gate * 10890Sstevel@tonic-gate * The other modification we make is to delete the second line above 10900Sstevel@tonic-gate * ("sec = tmp + (sec >> 6);"), since it only has an effect when bit 31 is 10910Sstevel@tonic-gate * set, and the cleanup code can handle that rare case. This reduces the 10920Sstevel@tonic-gate * *guaranteed* accuracy of sec to +0/-3, but speeds up the common cases. 10930Sstevel@tonic-gate * 10940Sstevel@tonic-gate * Finally, we compute nsec = hrt - (sec * 1,000,000,000). nsec will always 10950Sstevel@tonic-gate * be positive (since sec is never too large), and will at most be equal to 10960Sstevel@tonic-gate * the error in sec (times 1,000,000,000) plus the low-order 30 bits of hrt. 10970Sstevel@tonic-gate * Thus, nsec < 3 * 1,000,000,000 + 2^30, which is less than 2^32, so we can 10980Sstevel@tonic-gate * safely assume that nsec fits in 32 bits. Consequently, when we compute 10990Sstevel@tonic-gate * sec * 1,000,000,000, we only need the low 32 bits, so we can just do 32-bit 11000Sstevel@tonic-gate * arithmetic and let the high-order bits fall off the end. 11010Sstevel@tonic-gate * 11020Sstevel@tonic-gate * Since nsec < 3 * 1,000,000,000 + 2^30 == 4,073,741,824, the cleanup loop: 11030Sstevel@tonic-gate * 11040Sstevel@tonic-gate * while (nsec >= NANOSEC) { 11050Sstevel@tonic-gate * nsec -= NANOSEC; 11060Sstevel@tonic-gate * sec++; 11070Sstevel@tonic-gate * } 11080Sstevel@tonic-gate * 11090Sstevel@tonic-gate * is guaranteed to complete in at most 4 iterations. In practice, the loop 11100Sstevel@tonic-gate * completes in 0 or 1 iteration over 95% of the time. 11110Sstevel@tonic-gate * 11120Sstevel@tonic-gate * On an SS2, this implementation of hrt2ts() takes 1.7 usec, versus about 11130Sstevel@tonic-gate * 35 usec for software division -- about 20 times faster. 11140Sstevel@tonic-gate */ 11150Sstevel@tonic-gate void 11160Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp) 11170Sstevel@tonic-gate { 11180Sstevel@tonic-gate uint32_t sec, nsec, tmp; 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate tmp = (uint32_t)(hrt >> 30); 11210Sstevel@tonic-gate sec = tmp - (tmp >> 2); 11220Sstevel@tonic-gate sec = tmp - (sec >> 5); 11230Sstevel@tonic-gate sec = tmp + (sec >> 1); 11240Sstevel@tonic-gate sec = tmp - (sec >> 6) + 7; 11250Sstevel@tonic-gate sec = tmp - (sec >> 3); 11260Sstevel@tonic-gate sec = tmp + (sec >> 1); 11270Sstevel@tonic-gate sec = tmp + (sec >> 3); 11280Sstevel@tonic-gate sec = tmp + (sec >> 4); 11290Sstevel@tonic-gate tmp = (sec << 7) - sec - sec - sec; 11300Sstevel@tonic-gate tmp = (tmp << 7) - tmp - tmp - tmp; 11310Sstevel@tonic-gate tmp = (tmp << 7) - tmp - tmp - tmp; 11320Sstevel@tonic-gate nsec = (uint32_t)hrt - (tmp << 9); 11330Sstevel@tonic-gate while (nsec >= NANOSEC) { 11340Sstevel@tonic-gate nsec -= NANOSEC; 11350Sstevel@tonic-gate sec++; 11360Sstevel@tonic-gate } 11370Sstevel@tonic-gate tsp->tv_sec = (time_t)sec; 11380Sstevel@tonic-gate tsp->tv_nsec = nsec; 11390Sstevel@tonic-gate } 11400Sstevel@tonic-gate 11410Sstevel@tonic-gate /* 11420Sstevel@tonic-gate * Convert from timestruc_t to hrtime_t. 11430Sstevel@tonic-gate * 11440Sstevel@tonic-gate * The code below is equivalent to: 11450Sstevel@tonic-gate * 11460Sstevel@tonic-gate * hrt = tsp->tv_sec * NANOSEC + tsp->tv_nsec; 11470Sstevel@tonic-gate * 11480Sstevel@tonic-gate * but requires no integer multiply. 11490Sstevel@tonic-gate */ 11500Sstevel@tonic-gate hrtime_t 11510Sstevel@tonic-gate ts2hrt(const timestruc_t *tsp) 11520Sstevel@tonic-gate { 11530Sstevel@tonic-gate hrtime_t hrt; 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate hrt = tsp->tv_sec; 11560Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt; 11570Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt; 11580Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt; 11590Sstevel@tonic-gate hrt = (hrt << 9) + tsp->tv_nsec; 11600Sstevel@tonic-gate return (hrt); 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate /* 11640Sstevel@tonic-gate * For the various 32-bit "compatibility" paths in the system. 11650Sstevel@tonic-gate */ 11660Sstevel@tonic-gate void 11670Sstevel@tonic-gate hrt2ts32(hrtime_t hrt, timestruc32_t *ts32p) 11680Sstevel@tonic-gate { 11690Sstevel@tonic-gate timestruc_t ts; 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate hrt2ts(hrt, &ts); 11720Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(ts32p, &ts); 11730Sstevel@tonic-gate } 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate /* 11760Sstevel@tonic-gate * If this ever becomes performance critical (ha!), we can borrow the 11770Sstevel@tonic-gate * code from ts2hrt(), above, to multiply tv_sec by 1,000,000 and the 11780Sstevel@tonic-gate * straightforward (x << 10) - (x << 5) + (x << 3) to multiply tv_usec by 11790Sstevel@tonic-gate * 1,000. For now, we'll opt for readability (besides, the compiler does 11800Sstevel@tonic-gate * a passable job of optimizing constant multiplication into shifts and adds). 11810Sstevel@tonic-gate */ 11820Sstevel@tonic-gate hrtime_t 11830Sstevel@tonic-gate tv2hrt(struct timeval *tvp) 11840Sstevel@tonic-gate { 11850Sstevel@tonic-gate return ((hrtime_t)tvp->tv_sec * NANOSEC + 11860Sstevel@tonic-gate (hrtime_t)tvp->tv_usec * (NANOSEC / MICROSEC)); 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate void 11901432Sandyb hrt2tv(hrtime_t hrt, struct timeval *tvp) 11910Sstevel@tonic-gate { 11921432Sandyb uint32_t sec, nsec, tmp; 11931432Sandyb uint32_t q, r, t; 11941432Sandyb 11951432Sandyb tmp = (uint32_t)(hrt >> 30); 11961432Sandyb sec = tmp - (tmp >> 2); 11971432Sandyb sec = tmp - (sec >> 5); 11981432Sandyb sec = tmp + (sec >> 1); 11991432Sandyb sec = tmp - (sec >> 6) + 7; 12001432Sandyb sec = tmp - (sec >> 3); 12011432Sandyb sec = tmp + (sec >> 1); 12021432Sandyb sec = tmp + (sec >> 3); 12031432Sandyb sec = tmp + (sec >> 4); 12041432Sandyb tmp = (sec << 7) - sec - sec - sec; 12051432Sandyb tmp = (tmp << 7) - tmp - tmp - tmp; 12061432Sandyb tmp = (tmp << 7) - tmp - tmp - tmp; 12071432Sandyb nsec = (uint32_t)hrt - (tmp << 9); 12081432Sandyb while (nsec >= NANOSEC) { 12091432Sandyb nsec -= NANOSEC; 12101432Sandyb sec++; 12111432Sandyb } 12121432Sandyb tvp->tv_sec = (time_t)sec; 12131432Sandyb /* 12141432Sandyb * this routine is very similar to hr2ts, but requires microseconds 12151432Sandyb * instead of nanoseconds, so an interger divide by 1000 routine 12161432Sandyb * completes the conversion 12171432Sandyb */ 12181432Sandyb t = (nsec >> 7) + (nsec >> 8) + (nsec >> 12); 12191432Sandyb q = (nsec >> 1) + t + (nsec >> 15) + (t >> 11) + (t >> 14); 12201432Sandyb q = q >> 9; 12211432Sandyb r = nsec - q*1000; 12221432Sandyb tvp->tv_usec = q + ((r + 24) >> 10); 12231432Sandyb 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate int 12270Sstevel@tonic-gate nanosleep(timespec_t *rqtp, timespec_t *rmtp) 12280Sstevel@tonic-gate { 12290Sstevel@tonic-gate timespec_t rqtime; 12300Sstevel@tonic-gate timespec_t rmtime; 12310Sstevel@tonic-gate timespec_t now; 12324123Sdm120769 int timecheck; 12330Sstevel@tonic-gate int ret = 1; 12340Sstevel@tonic-gate model_t datamodel = get_udatamodel(); 12350Sstevel@tonic-gate 1236*7982SDonghai.Qiao@Sun.COM timecheck = timechanged; 1237*7982SDonghai.Qiao@Sun.COM gethrestime(&now); 1238*7982SDonghai.Qiao@Sun.COM 12390Sstevel@tonic-gate if (datamodel == DATAMODEL_NATIVE) { 12400Sstevel@tonic-gate if (copyin(rqtp, &rqtime, sizeof (rqtime))) 12410Sstevel@tonic-gate return (set_errno(EFAULT)); 12420Sstevel@tonic-gate } else { 12430Sstevel@tonic-gate timespec32_t rqtime32; 12440Sstevel@tonic-gate 12450Sstevel@tonic-gate if (copyin(rqtp, &rqtime32, sizeof (rqtime32))) 12460Sstevel@tonic-gate return (set_errno(EFAULT)); 12470Sstevel@tonic-gate TIMESPEC32_TO_TIMESPEC(&rqtime, &rqtime32); 12480Sstevel@tonic-gate } 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate if (rqtime.tv_sec < 0 || rqtime.tv_nsec < 0 || 12510Sstevel@tonic-gate rqtime.tv_nsec >= NANOSEC) 12520Sstevel@tonic-gate return (set_errno(EINVAL)); 12530Sstevel@tonic-gate 12540Sstevel@tonic-gate if (timerspecisset(&rqtime)) { 12550Sstevel@tonic-gate timespecadd(&rqtime, &now); 12560Sstevel@tonic-gate mutex_enter(&curthread->t_delay_lock); 12570Sstevel@tonic-gate while ((ret = cv_waituntil_sig(&curthread->t_delay_cv, 12584123Sdm120769 &curthread->t_delay_lock, &rqtime, timecheck)) > 0) 12590Sstevel@tonic-gate continue; 12600Sstevel@tonic-gate mutex_exit(&curthread->t_delay_lock); 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate 12630Sstevel@tonic-gate if (rmtp) { 12640Sstevel@tonic-gate /* 12650Sstevel@tonic-gate * If cv_waituntil_sig() returned due to a signal, and 12660Sstevel@tonic-gate * there is time remaining, then set the time remaining. 12670Sstevel@tonic-gate * Else set time remaining to zero 12680Sstevel@tonic-gate */ 12690Sstevel@tonic-gate rmtime.tv_sec = rmtime.tv_nsec = 0; 12700Sstevel@tonic-gate if (ret == 0) { 12713346Svb160487 timespec_t delta = rqtime; 12723346Svb160487 12730Sstevel@tonic-gate gethrestime(&now); 12743346Svb160487 timespecsub(&delta, &now); 12753346Svb160487 if (delta.tv_sec > 0 || (delta.tv_sec == 0 && 12763346Svb160487 delta.tv_nsec > 0)) 12773346Svb160487 rmtime = delta; 12780Sstevel@tonic-gate } 12790Sstevel@tonic-gate 12800Sstevel@tonic-gate if (datamodel == DATAMODEL_NATIVE) { 12810Sstevel@tonic-gate if (copyout(&rmtime, rmtp, sizeof (rmtime))) 12820Sstevel@tonic-gate return (set_errno(EFAULT)); 12830Sstevel@tonic-gate } else { 12840Sstevel@tonic-gate timespec32_t rmtime32; 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime); 12870Sstevel@tonic-gate if (copyout(&rmtime32, rmtp, sizeof (rmtime32))) 12880Sstevel@tonic-gate return (set_errno(EFAULT)); 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate } 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate if (ret == 0) 12930Sstevel@tonic-gate return (set_errno(EINTR)); 12940Sstevel@tonic-gate return (0); 12950Sstevel@tonic-gate } 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate /* 12980Sstevel@tonic-gate * Routines to convert standard UNIX time (seconds since Jan 1, 1970) 12990Sstevel@tonic-gate * into year/month/day/hour/minute/second format, and back again. 13000Sstevel@tonic-gate * Note: these routines require tod_lock held to protect cached state. 13010Sstevel@tonic-gate */ 13020Sstevel@tonic-gate static int days_thru_month[64] = { 13030Sstevel@tonic-gate 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 0, 0, 13040Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0, 13050Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0, 13060Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0, 13070Sstevel@tonic-gate }; 13080Sstevel@tonic-gate 13090Sstevel@tonic-gate todinfo_t saved_tod; 13100Sstevel@tonic-gate int saved_utc = -60; 13110Sstevel@tonic-gate 13120Sstevel@tonic-gate todinfo_t 13130Sstevel@tonic-gate utc_to_tod(time_t utc) 13140Sstevel@tonic-gate { 13150Sstevel@tonic-gate long dse, day, month, year; 13160Sstevel@tonic-gate todinfo_t tod; 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate if (utc < 0) /* should never happen */ 13210Sstevel@tonic-gate utc = 0; 13220Sstevel@tonic-gate 13230Sstevel@tonic-gate saved_tod.tod_sec += utc - saved_utc; 13240Sstevel@tonic-gate saved_utc = utc; 13250Sstevel@tonic-gate if (saved_tod.tod_sec >= 0 && saved_tod.tod_sec < 60) 13260Sstevel@tonic-gate return (saved_tod); /* only the seconds changed */ 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate dse = utc / 86400; /* days since epoch */ 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate tod.tod_sec = utc % 60; 13310Sstevel@tonic-gate tod.tod_min = (utc % 3600) / 60; 13320Sstevel@tonic-gate tod.tod_hour = (utc % 86400) / 3600; 13330Sstevel@tonic-gate tod.tod_dow = (dse + 4) % 7 + 1; /* epoch was a Thursday */ 13340Sstevel@tonic-gate 13350Sstevel@tonic-gate year = dse / 365 + 72; /* first guess -- always a bit too large */ 13360Sstevel@tonic-gate do { 13370Sstevel@tonic-gate year--; 13380Sstevel@tonic-gate day = dse - 365 * (year - 70) - ((year - 69) >> 2); 13390Sstevel@tonic-gate } while (day < 0); 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate month = ((year & 3) << 4) + 1; 13420Sstevel@tonic-gate while (day >= days_thru_month[month + 1]) 13430Sstevel@tonic-gate month++; 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate tod.tod_day = day - days_thru_month[month] + 1; 13460Sstevel@tonic-gate tod.tod_month = month & 15; 13470Sstevel@tonic-gate tod.tod_year = year; 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate saved_tod = tod; 13500Sstevel@tonic-gate return (tod); 13510Sstevel@tonic-gate } 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate time_t 13540Sstevel@tonic-gate tod_to_utc(todinfo_t tod) 13550Sstevel@tonic-gate { 13560Sstevel@tonic-gate time_t utc; 13570Sstevel@tonic-gate int year = tod.tod_year; 13580Sstevel@tonic-gate int month = tod.tod_month + ((year & 3) << 4); 13590Sstevel@tonic-gate #ifdef DEBUG 13600Sstevel@tonic-gate /* only warn once, not each time called */ 13610Sstevel@tonic-gate static int year_warn = 1; 13620Sstevel@tonic-gate static int month_warn = 1; 13630Sstevel@tonic-gate static int day_warn = 1; 13640Sstevel@tonic-gate static int hour_warn = 1; 13650Sstevel@tonic-gate static int min_warn = 1; 13660Sstevel@tonic-gate static int sec_warn = 1; 13670Sstevel@tonic-gate int days_diff = days_thru_month[month + 1] - days_thru_month[month]; 13680Sstevel@tonic-gate #endif 13690Sstevel@tonic-gate 13700Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate #ifdef DEBUG 13730Sstevel@tonic-gate if (year_warn && (year < 70 || year > 8029)) { 13740Sstevel@tonic-gate cmn_err(CE_WARN, 13756422Sqiao "The hardware real-time clock appears to have the " 13766422Sqiao "wrong years value %d -- time needs to be reset\n", 13776422Sqiao year); 13780Sstevel@tonic-gate year_warn = 0; 13790Sstevel@tonic-gate } 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate if (month_warn && (tod.tod_month < 1 || tod.tod_month > 12)) { 13820Sstevel@tonic-gate cmn_err(CE_WARN, 13836422Sqiao "The hardware real-time clock appears to have the " 13846422Sqiao "wrong months value %d -- time needs to be reset\n", 13856422Sqiao tod.tod_month); 13860Sstevel@tonic-gate month_warn = 0; 13870Sstevel@tonic-gate } 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate if (day_warn && (tod.tod_day < 1 || tod.tod_day > days_diff)) { 13900Sstevel@tonic-gate cmn_err(CE_WARN, 13916422Sqiao "The hardware real-time clock appears to have the " 13926422Sqiao "wrong days value %d -- time needs to be reset\n", 13936422Sqiao tod.tod_day); 13940Sstevel@tonic-gate day_warn = 0; 13950Sstevel@tonic-gate } 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate if (hour_warn && (tod.tod_hour < 0 || tod.tod_hour > 23)) { 13980Sstevel@tonic-gate cmn_err(CE_WARN, 13996422Sqiao "The hardware real-time clock appears to have the " 14006422Sqiao "wrong hours value %d -- time needs to be reset\n", 14016422Sqiao tod.tod_hour); 14020Sstevel@tonic-gate hour_warn = 0; 14030Sstevel@tonic-gate } 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate if (min_warn && (tod.tod_min < 0 || tod.tod_min > 59)) { 14060Sstevel@tonic-gate cmn_err(CE_WARN, 14076422Sqiao "The hardware real-time clock appears to have the " 14086422Sqiao "wrong minutes value %d -- time needs to be reset\n", 14096422Sqiao tod.tod_min); 14100Sstevel@tonic-gate min_warn = 0; 14110Sstevel@tonic-gate } 14120Sstevel@tonic-gate 14130Sstevel@tonic-gate if (sec_warn && (tod.tod_sec < 0 || tod.tod_sec > 59)) { 14140Sstevel@tonic-gate cmn_err(CE_WARN, 14156422Sqiao "The hardware real-time clock appears to have the " 14166422Sqiao "wrong seconds value %d -- time needs to be reset\n", 14176422Sqiao tod.tod_sec); 14180Sstevel@tonic-gate sec_warn = 0; 14190Sstevel@tonic-gate } 14200Sstevel@tonic-gate #endif 14210Sstevel@tonic-gate 14220Sstevel@tonic-gate utc = (year - 70); /* next 3 lines: utc = 365y + y/4 */ 14230Sstevel@tonic-gate utc += (utc << 3) + (utc << 6); 14240Sstevel@tonic-gate utc += (utc << 2) + ((year - 69) >> 2); 14250Sstevel@tonic-gate utc += days_thru_month[month] + tod.tod_day - 1; 14260Sstevel@tonic-gate utc = (utc << 3) + (utc << 4) + tod.tod_hour; /* 24 * day + hour */ 14270Sstevel@tonic-gate utc = (utc << 6) - (utc << 2) + tod.tod_min; /* 60 * hour + min */ 14280Sstevel@tonic-gate utc = (utc << 6) - (utc << 2) + tod.tod_sec; /* 60 * min + sec */ 14290Sstevel@tonic-gate 14300Sstevel@tonic-gate return (utc); 14310Sstevel@tonic-gate } 1432