17c478bd9Sstevel@tonic-gate /* 2*f635d46aSqiao * CDDL HEADER START 3*f635d46aSqiao * 4*f635d46aSqiao * The contents of this file are subject to the terms of the 5*f635d46aSqiao * Common Development and Distribution License (the "License"). 6*f635d46aSqiao * You may not use this file except in compliance with the License. 7*f635d46aSqiao * 8*f635d46aSqiao * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*f635d46aSqiao * or http://www.opensolaris.org/os/licensing. 10*f635d46aSqiao * See the License for the specific language governing permissions 11*f635d46aSqiao * and limitations under the License. 12*f635d46aSqiao * 13*f635d46aSqiao * When distributing Covered Code, include this CDDL HEADER in each 14*f635d46aSqiao * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*f635d46aSqiao * If applicable, add the following below this CDDL HEADER, with the 16*f635d46aSqiao * fields enclosed by brackets "[]" replaced with your own identifying 17*f635d46aSqiao * information: Portions Copyright [yyyy] [name of copyright owner] 18*f635d46aSqiao * 19*f635d46aSqiao * CDDL HEADER END 20*f635d46aSqiao */ 21*f635d46aSqiao /* 22*f635d46aSqiao * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Copyright (c) 1982, 1986 Regents of the University of California. 307c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 317c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 327c478bd9Sstevel@tonic-gate */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include <sys/param.h> 357c478bd9Sstevel@tonic-gate #include <sys/user.h> 367c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 377c478bd9Sstevel@tonic-gate #include <sys/proc.h> 387c478bd9Sstevel@tonic-gate #include <sys/time.h> 397c478bd9Sstevel@tonic-gate #include <sys/systm.h> 407c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 417c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 427c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 437c478bd9Sstevel@tonic-gate #include <sys/timer.h> 447c478bd9Sstevel@tonic-gate #include <sys/debug.h> 457c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 467c478bd9Sstevel@tonic-gate #include <sys/cyclic.h> 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate static void realitexpire(void *); 497c478bd9Sstevel@tonic-gate static void realprofexpire(void *); 507c478bd9Sstevel@tonic-gate static void timeval_advance(struct timeval *, struct timeval *); 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate kmutex_t tod_lock; /* protects time-of-day stuff */ 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* 557c478bd9Sstevel@tonic-gate * Constant to define the minimum interval value of the ITIMER_REALPROF timer. 567c478bd9Sstevel@tonic-gate * Value is in microseconds; defaults to 500 usecs. Setting this value 577c478bd9Sstevel@tonic-gate * significantly lower may allow for denial-of-service attacks. 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate int itimer_realprof_minimum = 500; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* 627c478bd9Sstevel@tonic-gate * macro to compare a timeval to a timestruc 637c478bd9Sstevel@tonic-gate */ 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate #define TVTSCMP(tvp, tsp, cmp) \ 667c478bd9Sstevel@tonic-gate /* CSTYLED */ \ 677c478bd9Sstevel@tonic-gate ((tvp)->tv_sec cmp (tsp)->tv_sec || \ 687c478bd9Sstevel@tonic-gate ((tvp)->tv_sec == (tsp)->tv_sec && \ 697c478bd9Sstevel@tonic-gate /* CSTYLED */ \ 707c478bd9Sstevel@tonic-gate (tvp)->tv_usec * 1000 cmp (tsp)->tv_nsec)) 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate /* 737c478bd9Sstevel@tonic-gate * Time of day and interval timer support. 747c478bd9Sstevel@tonic-gate * 757c478bd9Sstevel@tonic-gate * These routines provide the kernel entry points to get and set 767c478bd9Sstevel@tonic-gate * the time-of-day and per-process interval timers. Subroutines 777c478bd9Sstevel@tonic-gate * here provide support for adding and subtracting timeval structures 787c478bd9Sstevel@tonic-gate * and decrementing interval timers, optionally reloading the interval 797c478bd9Sstevel@tonic-gate * timers when they expire. 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /* 837c478bd9Sstevel@tonic-gate * SunOS function to generate monotonically increasing time values. 847c478bd9Sstevel@tonic-gate */ 857c478bd9Sstevel@tonic-gate void 867c478bd9Sstevel@tonic-gate uniqtime(struct timeval *tv) 877c478bd9Sstevel@tonic-gate { 887c478bd9Sstevel@tonic-gate static struct timeval last; 897c478bd9Sstevel@tonic-gate timestruc_t ts; 907c478bd9Sstevel@tonic-gate time_t sec; 917c478bd9Sstevel@tonic-gate int usec, nsec; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * protect modification of last 957c478bd9Sstevel@tonic-gate */ 967c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 977c478bd9Sstevel@tonic-gate gethrestime(&ts); 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * Fast algorithm to convert nsec to usec -- see hrt2ts() 1017c478bd9Sstevel@tonic-gate * in common/os/timers.c for a full description. 1027c478bd9Sstevel@tonic-gate */ 1037c478bd9Sstevel@tonic-gate nsec = ts.tv_nsec; 1047c478bd9Sstevel@tonic-gate usec = nsec + (nsec >> 2); 1057c478bd9Sstevel@tonic-gate usec = nsec + (usec >> 1); 1067c478bd9Sstevel@tonic-gate usec = nsec + (usec >> 2); 1077c478bd9Sstevel@tonic-gate usec = nsec + (usec >> 4); 1087c478bd9Sstevel@tonic-gate usec = nsec - (usec >> 3); 1097c478bd9Sstevel@tonic-gate usec = nsec + (usec >> 2); 1107c478bd9Sstevel@tonic-gate usec = nsec + (usec >> 3); 1117c478bd9Sstevel@tonic-gate usec = nsec + (usec >> 4); 1127c478bd9Sstevel@tonic-gate usec = nsec + (usec >> 1); 1137c478bd9Sstevel@tonic-gate usec = nsec + (usec >> 6); 1147c478bd9Sstevel@tonic-gate usec = usec >> 10; 1157c478bd9Sstevel@tonic-gate sec = ts.tv_sec; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * Try to keep timestamps unique, but don't be obsessive about 1197c478bd9Sstevel@tonic-gate * it in the face of large differences. 1207c478bd9Sstevel@tonic-gate */ 1217c478bd9Sstevel@tonic-gate if ((sec <= last.tv_sec) && /* same or lower seconds, and */ 1227c478bd9Sstevel@tonic-gate ((sec != last.tv_sec) || /* either different second or */ 1237c478bd9Sstevel@tonic-gate (usec <= last.tv_usec)) && /* lower microsecond, and */ 1247c478bd9Sstevel@tonic-gate ((last.tv_sec - sec) <= 5)) { /* not way back in time */ 1257c478bd9Sstevel@tonic-gate sec = last.tv_sec; 1267c478bd9Sstevel@tonic-gate usec = last.tv_usec + 1; 1277c478bd9Sstevel@tonic-gate if (usec >= MICROSEC) { 1287c478bd9Sstevel@tonic-gate usec -= MICROSEC; 1297c478bd9Sstevel@tonic-gate sec++; 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate last.tv_sec = sec; 1337c478bd9Sstevel@tonic-gate last.tv_usec = usec; 1347c478bd9Sstevel@tonic-gate mutex_exit(&tod_lock); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate tv->tv_sec = sec; 1377c478bd9Sstevel@tonic-gate tv->tv_usec = usec; 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* 1417c478bd9Sstevel@tonic-gate * Timestamps are exported from the kernel in several places. 1427c478bd9Sstevel@tonic-gate * Such timestamps are commonly used for either uniqueness or for 1437c478bd9Sstevel@tonic-gate * sequencing - truncation to 32-bits is fine for uniqueness, 1447c478bd9Sstevel@tonic-gate * but sequencing is going to take more work as we get closer to 2038! 1457c478bd9Sstevel@tonic-gate */ 1467c478bd9Sstevel@tonic-gate void 1477c478bd9Sstevel@tonic-gate uniqtime32(struct timeval32 *tv32p) 1487c478bd9Sstevel@tonic-gate { 1497c478bd9Sstevel@tonic-gate struct timeval tv; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate uniqtime(&tv); 1527c478bd9Sstevel@tonic-gate TIMEVAL_TO_TIMEVAL32(tv32p, &tv); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate int 1567c478bd9Sstevel@tonic-gate gettimeofday(struct timeval *tp) 1577c478bd9Sstevel@tonic-gate { 1587c478bd9Sstevel@tonic-gate struct timeval atv; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate if (tp) { 1617c478bd9Sstevel@tonic-gate uniqtime(&atv); 1627c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 1637c478bd9Sstevel@tonic-gate if (copyout(&atv, tp, sizeof (atv))) 1647c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1657c478bd9Sstevel@tonic-gate } else { 1667c478bd9Sstevel@tonic-gate struct timeval32 tv32; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (TIMEVAL_OVERFLOW(&atv)) 1697c478bd9Sstevel@tonic-gate return (set_errno(EOVERFLOW)); 1707c478bd9Sstevel@tonic-gate TIMEVAL_TO_TIMEVAL32(&tv32, &atv); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate if (copyout(&tv32, tp, sizeof (tv32))) 1737c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate return (0); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate int 1807c478bd9Sstevel@tonic-gate getitimer(uint_t which, struct itimerval *itv) 1817c478bd9Sstevel@tonic-gate { 1827c478bd9Sstevel@tonic-gate int error; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) 1857c478bd9Sstevel@tonic-gate error = xgetitimer(which, itv, 0); 1867c478bd9Sstevel@tonic-gate else { 1877c478bd9Sstevel@tonic-gate struct itimerval kitv; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate if ((error = xgetitimer(which, &kitv, 1)) == 0) { 1907c478bd9Sstevel@tonic-gate if (ITIMERVAL_OVERFLOW(&kitv)) { 1917c478bd9Sstevel@tonic-gate error = EOVERFLOW; 1927c478bd9Sstevel@tonic-gate } else { 1937c478bd9Sstevel@tonic-gate struct itimerval32 itv32; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate ITIMERVAL_TO_ITIMERVAL32(&itv32, &kitv); 1967c478bd9Sstevel@tonic-gate if (copyout(&itv32, itv, sizeof (itv32)) != 0) 1977c478bd9Sstevel@tonic-gate error = EFAULT; 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate return (error ? (set_errno(error)) : 0); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate int 2067c478bd9Sstevel@tonic-gate xgetitimer(uint_t which, struct itimerval *itv, int iskaddr) 2077c478bd9Sstevel@tonic-gate { 2087c478bd9Sstevel@tonic-gate struct proc *p = curproc; 2097c478bd9Sstevel@tonic-gate struct timeval now; 2107c478bd9Sstevel@tonic-gate struct itimerval aitv; 2117c478bd9Sstevel@tonic-gate hrtime_t ts, first, interval, remain; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate switch (which) { 2167c478bd9Sstevel@tonic-gate case ITIMER_VIRTUAL: 2177c478bd9Sstevel@tonic-gate case ITIMER_PROF: 2187c478bd9Sstevel@tonic-gate aitv = ttolwp(curthread)->lwp_timer[which]; 2197c478bd9Sstevel@tonic-gate break; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate case ITIMER_REAL: 2227c478bd9Sstevel@tonic-gate uniqtime(&now); 2237c478bd9Sstevel@tonic-gate aitv = p->p_realitimer; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate if (timerisset(&aitv.it_value)) { 2267c478bd9Sstevel@tonic-gate /*CSTYLED*/ 2277c478bd9Sstevel@tonic-gate if (timercmp(&aitv.it_value, &now, <)) { 2287c478bd9Sstevel@tonic-gate timerclear(&aitv.it_value); 2297c478bd9Sstevel@tonic-gate } else { 2307c478bd9Sstevel@tonic-gate timevalsub(&aitv.it_value, &now); 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate break; 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate case ITIMER_REALPROF: 2367c478bd9Sstevel@tonic-gate if (curproc->p_rprof_cyclic == CYCLIC_NONE) { 2377c478bd9Sstevel@tonic-gate bzero(&aitv, sizeof (aitv)); 2387c478bd9Sstevel@tonic-gate break; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate aitv = curproc->p_rprof_timer; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate first = tv2hrt(&aitv.it_value); 2447c478bd9Sstevel@tonic-gate interval = tv2hrt(&aitv.it_interval); 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate if ((ts = gethrtime()) < first) { 2477c478bd9Sstevel@tonic-gate /* 2487c478bd9Sstevel@tonic-gate * We haven't gone off for the first time; the time 2497c478bd9Sstevel@tonic-gate * remaining is simply the first time we will go 2507c478bd9Sstevel@tonic-gate * off minus the current time. 2517c478bd9Sstevel@tonic-gate */ 2527c478bd9Sstevel@tonic-gate remain = first - ts; 2537c478bd9Sstevel@tonic-gate } else { 2547c478bd9Sstevel@tonic-gate if (interval == 0) { 2557c478bd9Sstevel@tonic-gate /* 2567c478bd9Sstevel@tonic-gate * This was set as a one-shot, and we've 2577c478bd9Sstevel@tonic-gate * already gone off; there is no time 2587c478bd9Sstevel@tonic-gate * remaining. 2597c478bd9Sstevel@tonic-gate */ 2607c478bd9Sstevel@tonic-gate remain = 0; 2617c478bd9Sstevel@tonic-gate } else { 2627c478bd9Sstevel@tonic-gate /* 2637c478bd9Sstevel@tonic-gate * We have a non-zero interval; we need to 2647c478bd9Sstevel@tonic-gate * determine how far we are into the current 2657c478bd9Sstevel@tonic-gate * interval, and subtract that from the 2667c478bd9Sstevel@tonic-gate * interval to determine the time remaining. 2677c478bd9Sstevel@tonic-gate */ 2687c478bd9Sstevel@tonic-gate remain = interval - ((ts - first) % interval); 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate hrt2tv(remain, &aitv.it_value); 2737c478bd9Sstevel@tonic-gate break; 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate default: 2767c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 2777c478bd9Sstevel@tonic-gate return (EINVAL); 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate if (iskaddr) { 2837c478bd9Sstevel@tonic-gate bcopy(&aitv, itv, sizeof (*itv)); 2847c478bd9Sstevel@tonic-gate } else { 2857c478bd9Sstevel@tonic-gate ASSERT(get_udatamodel() == DATAMODEL_NATIVE); 2867c478bd9Sstevel@tonic-gate if (copyout(&aitv, itv, sizeof (*itv))) 2877c478bd9Sstevel@tonic-gate return (EFAULT); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate return (0); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate int 2957c478bd9Sstevel@tonic-gate setitimer(uint_t which, struct itimerval *itv, struct itimerval *oitv) 2967c478bd9Sstevel@tonic-gate { 2977c478bd9Sstevel@tonic-gate int error; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate if (oitv != NULL) 3007c478bd9Sstevel@tonic-gate if ((error = getitimer(which, oitv)) != 0) 3017c478bd9Sstevel@tonic-gate return (error); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate if (itv == NULL) 3047c478bd9Sstevel@tonic-gate return (0); 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) 3077c478bd9Sstevel@tonic-gate error = xsetitimer(which, itv, 0); 3087c478bd9Sstevel@tonic-gate else { 3097c478bd9Sstevel@tonic-gate struct itimerval32 itv32; 3107c478bd9Sstevel@tonic-gate struct itimerval kitv; 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if (copyin(itv, &itv32, sizeof (itv32))) 3137c478bd9Sstevel@tonic-gate error = EFAULT; 3147c478bd9Sstevel@tonic-gate ITIMERVAL32_TO_ITIMERVAL(&kitv, &itv32); 3157c478bd9Sstevel@tonic-gate error = xsetitimer(which, &kitv, 1); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate return (error ? (set_errno(error)) : 0); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate int 3227c478bd9Sstevel@tonic-gate xsetitimer(uint_t which, struct itimerval *itv, int iskaddr) 3237c478bd9Sstevel@tonic-gate { 3247c478bd9Sstevel@tonic-gate struct itimerval aitv; 3257c478bd9Sstevel@tonic-gate struct timeval now; 3267c478bd9Sstevel@tonic-gate struct proc *p = curproc; 3277c478bd9Sstevel@tonic-gate kthread_t *t; 3287c478bd9Sstevel@tonic-gate timeout_id_t tmp_id; 3297c478bd9Sstevel@tonic-gate cyc_handler_t hdlr; 3307c478bd9Sstevel@tonic-gate cyc_time_t when; 3317c478bd9Sstevel@tonic-gate cyclic_id_t cyclic; 3327c478bd9Sstevel@tonic-gate hrtime_t ts; 3337c478bd9Sstevel@tonic-gate int min; 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate if (itv == NULL) 3367c478bd9Sstevel@tonic-gate return (0); 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate if (iskaddr) { 3397c478bd9Sstevel@tonic-gate bcopy(itv, &aitv, sizeof (aitv)); 3407c478bd9Sstevel@tonic-gate } else { 3417c478bd9Sstevel@tonic-gate ASSERT(get_udatamodel() == DATAMODEL_NATIVE); 3427c478bd9Sstevel@tonic-gate if (copyin(itv, &aitv, sizeof (aitv))) 3437c478bd9Sstevel@tonic-gate return (EFAULT); 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate if (which == ITIMER_REALPROF) { 3477c478bd9Sstevel@tonic-gate min = MAX((int)(cyclic_getres() / (NANOSEC / MICROSEC)), 3487c478bd9Sstevel@tonic-gate itimer_realprof_minimum); 3497c478bd9Sstevel@tonic-gate } else { 3507c478bd9Sstevel@tonic-gate min = usec_per_tick; 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate if (itimerfix(&aitv.it_value, min) || 3547c478bd9Sstevel@tonic-gate (itimerfix(&aitv.it_interval, min) && timerisset(&aitv.it_value))) 3557c478bd9Sstevel@tonic-gate return (EINVAL); 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 3587c478bd9Sstevel@tonic-gate switch (which) { 3597c478bd9Sstevel@tonic-gate case ITIMER_REAL: 3607c478bd9Sstevel@tonic-gate /* 3617c478bd9Sstevel@tonic-gate * The SITBUSY flag prevents conflicts with multiple 3627c478bd9Sstevel@tonic-gate * threads attempting to perform setitimer(ITIMER_REAL) 3637c478bd9Sstevel@tonic-gate * at the same time, even when we drop p->p_lock below. 3647c478bd9Sstevel@tonic-gate * Any blocked thread returns successfully because the 3657c478bd9Sstevel@tonic-gate * effect is the same as if it got here first, finished, 3667c478bd9Sstevel@tonic-gate * and the other thread then came through and destroyed 3677c478bd9Sstevel@tonic-gate * what it did. We are just protecting the system from 3687c478bd9Sstevel@tonic-gate * malfunctioning due to the race condition. 3697c478bd9Sstevel@tonic-gate */ 3707c478bd9Sstevel@tonic-gate if (p->p_flag & SITBUSY) { 3717c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 3727c478bd9Sstevel@tonic-gate return (0); 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate p->p_flag |= SITBUSY; 3757c478bd9Sstevel@tonic-gate while ((tmp_id = p->p_itimerid) != 0) { 3767c478bd9Sstevel@tonic-gate /* 3777c478bd9Sstevel@tonic-gate * Avoid deadlock in callout_delete (called from 3787c478bd9Sstevel@tonic-gate * untimeout) which may go to sleep (while holding 3797c478bd9Sstevel@tonic-gate * p_lock). Drop p_lock and re-acquire it after 3807c478bd9Sstevel@tonic-gate * untimeout returns. Need to clear p_itimerid 3817c478bd9Sstevel@tonic-gate * while holding p_lock. 3827c478bd9Sstevel@tonic-gate */ 3837c478bd9Sstevel@tonic-gate p->p_itimerid = 0; 3847c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 3857c478bd9Sstevel@tonic-gate (void) untimeout(tmp_id); 3867c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate if (timerisset(&aitv.it_value)) { 3897c478bd9Sstevel@tonic-gate uniqtime(&now); 3907c478bd9Sstevel@tonic-gate timevaladd(&aitv.it_value, &now); 3917c478bd9Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, 3927c478bd9Sstevel@tonic-gate p, hzto(&aitv.it_value)); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate p->p_realitimer = aitv; 3957c478bd9Sstevel@tonic-gate p->p_flag &= ~SITBUSY; 3967c478bd9Sstevel@tonic-gate break; 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate case ITIMER_REALPROF: 3997c478bd9Sstevel@tonic-gate cyclic = p->p_rprof_cyclic; 4007c478bd9Sstevel@tonic-gate p->p_rprof_cyclic = CYCLIC_NONE; 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate /* 4057c478bd9Sstevel@tonic-gate * We're now going to acquire cpu_lock, remove the old cyclic 4067c478bd9Sstevel@tonic-gate * if necessary, and add our new cyclic. 4077c478bd9Sstevel@tonic-gate */ 4087c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate if (cyclic != CYCLIC_NONE) 4117c478bd9Sstevel@tonic-gate cyclic_remove(cyclic); 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate if (!timerisset(&aitv.it_value)) { 4147c478bd9Sstevel@tonic-gate /* 4157c478bd9Sstevel@tonic-gate * If we were passed a value of 0, we're done. 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 4187c478bd9Sstevel@tonic-gate return (0); 4197c478bd9Sstevel@tonic-gate } 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate hdlr.cyh_func = realprofexpire; 4227c478bd9Sstevel@tonic-gate hdlr.cyh_arg = p; 4237c478bd9Sstevel@tonic-gate hdlr.cyh_level = CY_LOW_LEVEL; 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate when.cyt_when = (ts = gethrtime() + tv2hrt(&aitv.it_value)); 4267c478bd9Sstevel@tonic-gate when.cyt_interval = tv2hrt(&aitv.it_interval); 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate if (when.cyt_interval == 0) { 4297c478bd9Sstevel@tonic-gate /* 4307c478bd9Sstevel@tonic-gate * Using the same logic as for CLOCK_HIGHRES timers, we 4317c478bd9Sstevel@tonic-gate * set the interval to be INT64_MAX - when.cyt_when to 4327c478bd9Sstevel@tonic-gate * effect a one-shot; see the comment in clock_highres.c 4337c478bd9Sstevel@tonic-gate * for more details on why this works. 4347c478bd9Sstevel@tonic-gate */ 4357c478bd9Sstevel@tonic-gate when.cyt_interval = INT64_MAX - when.cyt_when; 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate cyclic = cyclic_add(&hdlr, &when); 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate /* 4437c478bd9Sstevel@tonic-gate * We have now successfully added the cyclic. Reacquire 4447c478bd9Sstevel@tonic-gate * p_lock, and see if anyone has snuck in. 4457c478bd9Sstevel@tonic-gate */ 4467c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate if (p->p_rprof_cyclic != CYCLIC_NONE) { 4497c478bd9Sstevel@tonic-gate /* 4507c478bd9Sstevel@tonic-gate * We're racing with another thread establishing an 4517c478bd9Sstevel@tonic-gate * ITIMER_REALPROF interval timer. We'll let the other 4527c478bd9Sstevel@tonic-gate * thread win (this is a race at the application level, 4537c478bd9Sstevel@tonic-gate * so letting the other thread win is acceptable). 4547c478bd9Sstevel@tonic-gate */ 4557c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 4567c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 4577c478bd9Sstevel@tonic-gate cyclic_remove(cyclic); 4587c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate return (0); 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate /* 4647c478bd9Sstevel@tonic-gate * Success. Set our tracking variables in the proc structure, 4657c478bd9Sstevel@tonic-gate * cancel any outstanding ITIMER_PROF, and allocate the 4667c478bd9Sstevel@tonic-gate * per-thread SIGPROF buffers, if possible. 4677c478bd9Sstevel@tonic-gate */ 4687c478bd9Sstevel@tonic-gate hrt2tv(ts, &aitv.it_value); 4697c478bd9Sstevel@tonic-gate p->p_rprof_timer = aitv; 4707c478bd9Sstevel@tonic-gate p->p_rprof_cyclic = cyclic; 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate t = p->p_tlist; 4737c478bd9Sstevel@tonic-gate do { 4747c478bd9Sstevel@tonic-gate struct itimerval *itvp; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate itvp = &ttolwp(t)->lwp_timer[ITIMER_PROF]; 4777c478bd9Sstevel@tonic-gate timerclear(&itvp->it_interval); 4787c478bd9Sstevel@tonic-gate timerclear(&itvp->it_value); 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate if (t->t_rprof != NULL) 4817c478bd9Sstevel@tonic-gate continue; 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate t->t_rprof = 4847c478bd9Sstevel@tonic-gate kmem_zalloc(sizeof (struct rprof), KM_NOSLEEP); 4857c478bd9Sstevel@tonic-gate aston(t); 4867c478bd9Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate break; 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate case ITIMER_VIRTUAL: 4917c478bd9Sstevel@tonic-gate ttolwp(curthread)->lwp_timer[ITIMER_VIRTUAL] = aitv; 4927c478bd9Sstevel@tonic-gate break; 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate case ITIMER_PROF: 4957c478bd9Sstevel@tonic-gate if (p->p_rprof_cyclic != CYCLIC_NONE) { 4967c478bd9Sstevel@tonic-gate /* 4977c478bd9Sstevel@tonic-gate * Silently ignore ITIMER_PROF if ITIMER_REALPROF 4987c478bd9Sstevel@tonic-gate * is in effect. 4997c478bd9Sstevel@tonic-gate */ 5007c478bd9Sstevel@tonic-gate break; 5017c478bd9Sstevel@tonic-gate } 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate ttolwp(curthread)->lwp_timer[ITIMER_PROF] = aitv; 5047c478bd9Sstevel@tonic-gate break; 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate default: 5077c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5087c478bd9Sstevel@tonic-gate return (EINVAL); 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5117c478bd9Sstevel@tonic-gate return (0); 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate /* 5157c478bd9Sstevel@tonic-gate * Real interval timer expired: 5167c478bd9Sstevel@tonic-gate * send process whose timer expired an alarm signal. 5177c478bd9Sstevel@tonic-gate * If time is not set up to reload, then just return. 5187c478bd9Sstevel@tonic-gate * Else compute next time timer should go off which is > current time. 5197c478bd9Sstevel@tonic-gate * This is where delay in processing this timeout causes multiple 5207c478bd9Sstevel@tonic-gate * SIGALRM calls to be compressed into one. 5217c478bd9Sstevel@tonic-gate */ 5227c478bd9Sstevel@tonic-gate static void 5237c478bd9Sstevel@tonic-gate realitexpire(void *arg) 5247c478bd9Sstevel@tonic-gate { 5257c478bd9Sstevel@tonic-gate struct proc *p = arg; 5267c478bd9Sstevel@tonic-gate struct timeval *valp = &p->p_realitimer.it_value; 5277c478bd9Sstevel@tonic-gate struct timeval *intervalp = &p->p_realitimer.it_interval; 5287c478bd9Sstevel@tonic-gate #if !defined(_LP64) 5297c478bd9Sstevel@tonic-gate clock_t ticks; 5307c478bd9Sstevel@tonic-gate #endif 5317c478bd9Sstevel@tonic-gate 5327c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 5337c478bd9Sstevel@tonic-gate #if !defined(_LP64) 5347c478bd9Sstevel@tonic-gate if ((ticks = hzto(valp)) > 1) { 5357c478bd9Sstevel@tonic-gate /* 5367c478bd9Sstevel@tonic-gate * If we are executing before we were meant to, it must be 5377c478bd9Sstevel@tonic-gate * because of an overflow in a prior hzto() calculation. 5387c478bd9Sstevel@tonic-gate * In this case, we want to go to sleep for the recalculated 5397c478bd9Sstevel@tonic-gate * number of ticks. For the special meaning of the value "1" 5407c478bd9Sstevel@tonic-gate * see comment in timespectohz(). 5417c478bd9Sstevel@tonic-gate */ 5427c478bd9Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, p, ticks); 5437c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5447c478bd9Sstevel@tonic-gate return; 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate #endif 5477c478bd9Sstevel@tonic-gate sigtoproc(p, NULL, SIGALRM); 5487c478bd9Sstevel@tonic-gate if (!timerisset(intervalp)) { 5497c478bd9Sstevel@tonic-gate timerclear(valp); 5507c478bd9Sstevel@tonic-gate p->p_itimerid = 0; 5517c478bd9Sstevel@tonic-gate } else { 5527c478bd9Sstevel@tonic-gate /* advance timer value past current time */ 5537c478bd9Sstevel@tonic-gate timeval_advance(valp, intervalp); 5547c478bd9Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, p, hzto(valp)); 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5577c478bd9Sstevel@tonic-gate } 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * Real time profiling interval timer expired: 5617c478bd9Sstevel@tonic-gate * Increment microstate counters for each lwp in the process 5627c478bd9Sstevel@tonic-gate * and ensure that running lwps are kicked into the kernel. 5637c478bd9Sstevel@tonic-gate * If time is not set up to reload, then just return. 5647c478bd9Sstevel@tonic-gate * Else compute next time timer should go off which is > current time, 5657c478bd9Sstevel@tonic-gate * as above. 5667c478bd9Sstevel@tonic-gate */ 5677c478bd9Sstevel@tonic-gate static void 5687c478bd9Sstevel@tonic-gate realprofexpire(void *arg) 5697c478bd9Sstevel@tonic-gate { 5707c478bd9Sstevel@tonic-gate struct proc *p = arg; 5717c478bd9Sstevel@tonic-gate kthread_t *t; 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 5747c478bd9Sstevel@tonic-gate if ((t = p->p_tlist) == NULL) { 5757c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5767c478bd9Sstevel@tonic-gate return; 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate do { 5797c478bd9Sstevel@tonic-gate int mstate; 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate /* 5827c478bd9Sstevel@tonic-gate * Attempt to allocate the SIGPROF buffer, but don't sleep. 5837c478bd9Sstevel@tonic-gate */ 5847c478bd9Sstevel@tonic-gate if (t->t_rprof == NULL) 5857c478bd9Sstevel@tonic-gate t->t_rprof = kmem_zalloc(sizeof (struct rprof), 5867c478bd9Sstevel@tonic-gate KM_NOSLEEP); 5877c478bd9Sstevel@tonic-gate if (t->t_rprof == NULL) 5887c478bd9Sstevel@tonic-gate continue; 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate thread_lock(t); 5917c478bd9Sstevel@tonic-gate switch (t->t_state) { 5927c478bd9Sstevel@tonic-gate case TS_SLEEP: 5937c478bd9Sstevel@tonic-gate /* 5947c478bd9Sstevel@tonic-gate * Don't touch the lwp is it is swapped out. 5957c478bd9Sstevel@tonic-gate */ 5967c478bd9Sstevel@tonic-gate if (!(t->t_schedflag & TS_LOAD)) { 5977c478bd9Sstevel@tonic-gate mstate = LMS_SLEEP; 5987c478bd9Sstevel@tonic-gate break; 5997c478bd9Sstevel@tonic-gate } 6007c478bd9Sstevel@tonic-gate switch (mstate = ttolwp(t)->lwp_mstate.ms_prev) { 6017c478bd9Sstevel@tonic-gate case LMS_TFAULT: 6027c478bd9Sstevel@tonic-gate case LMS_DFAULT: 6037c478bd9Sstevel@tonic-gate case LMS_KFAULT: 6047c478bd9Sstevel@tonic-gate case LMS_USER_LOCK: 6057c478bd9Sstevel@tonic-gate break; 6067c478bd9Sstevel@tonic-gate default: 6077c478bd9Sstevel@tonic-gate mstate = LMS_SLEEP; 6087c478bd9Sstevel@tonic-gate break; 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate break; 6117c478bd9Sstevel@tonic-gate case TS_RUN: 612c97ad5cdSakolb case TS_WAIT: 6137c478bd9Sstevel@tonic-gate mstate = LMS_WAIT_CPU; 6147c478bd9Sstevel@tonic-gate break; 6157c478bd9Sstevel@tonic-gate case TS_ONPROC: 6167c478bd9Sstevel@tonic-gate switch (mstate = t->t_mstate) { 6177c478bd9Sstevel@tonic-gate case LMS_USER: 6187c478bd9Sstevel@tonic-gate case LMS_SYSTEM: 6197c478bd9Sstevel@tonic-gate case LMS_TRAP: 6207c478bd9Sstevel@tonic-gate break; 6217c478bd9Sstevel@tonic-gate default: 6227c478bd9Sstevel@tonic-gate mstate = LMS_SYSTEM; 6237c478bd9Sstevel@tonic-gate break; 6247c478bd9Sstevel@tonic-gate } 6257c478bd9Sstevel@tonic-gate break; 6267c478bd9Sstevel@tonic-gate default: 6277c478bd9Sstevel@tonic-gate mstate = t->t_mstate; 6287c478bd9Sstevel@tonic-gate break; 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate t->t_rprof->rp_anystate = 1; 6317c478bd9Sstevel@tonic-gate t->t_rprof->rp_state[mstate]++; 6327c478bd9Sstevel@tonic-gate aston(t); 6337c478bd9Sstevel@tonic-gate /* 6347c478bd9Sstevel@tonic-gate * force the thread into the kernel 6357c478bd9Sstevel@tonic-gate * if it is not already there. 6367c478bd9Sstevel@tonic-gate */ 6377c478bd9Sstevel@tonic-gate if (t->t_state == TS_ONPROC && t->t_cpu != CPU) 6387c478bd9Sstevel@tonic-gate poke_cpu(t->t_cpu->cpu_id); 6397c478bd9Sstevel@tonic-gate thread_unlock(t); 6407c478bd9Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 6437c478bd9Sstevel@tonic-gate } 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate /* 6467c478bd9Sstevel@tonic-gate * Advances timer value past the current time of day. See the detailed 6477c478bd9Sstevel@tonic-gate * comment for this logic in realitsexpire(), above. 6487c478bd9Sstevel@tonic-gate */ 6497c478bd9Sstevel@tonic-gate static void 6507c478bd9Sstevel@tonic-gate timeval_advance(struct timeval *valp, struct timeval *intervalp) 6517c478bd9Sstevel@tonic-gate { 6527c478bd9Sstevel@tonic-gate int cnt2nth; 6537c478bd9Sstevel@tonic-gate struct timeval interval2nth; 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate for (;;) { 6567c478bd9Sstevel@tonic-gate interval2nth = *intervalp; 6577c478bd9Sstevel@tonic-gate for (cnt2nth = 0; ; cnt2nth++) { 6587c478bd9Sstevel@tonic-gate timevaladd(valp, &interval2nth); 6597c478bd9Sstevel@tonic-gate /*CSTYLED*/ 6607c478bd9Sstevel@tonic-gate if (TVTSCMP(valp, &hrestime, >)) 6617c478bd9Sstevel@tonic-gate break; 6627c478bd9Sstevel@tonic-gate timevaladd(&interval2nth, &interval2nth); 6637c478bd9Sstevel@tonic-gate } 6647c478bd9Sstevel@tonic-gate if (cnt2nth == 0) 6657c478bd9Sstevel@tonic-gate break; 6667c478bd9Sstevel@tonic-gate timevalsub(valp, &interval2nth); 6677c478bd9Sstevel@tonic-gate } 6687c478bd9Sstevel@tonic-gate } 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate /* 6717c478bd9Sstevel@tonic-gate * Check that a proposed value to load into the .it_value or .it_interval 6727c478bd9Sstevel@tonic-gate * part of an interval timer is acceptable, and set it to at least a 6737c478bd9Sstevel@tonic-gate * specified minimal value. 6747c478bd9Sstevel@tonic-gate */ 6757c478bd9Sstevel@tonic-gate int 6767c478bd9Sstevel@tonic-gate itimerfix(struct timeval *tv, int minimum) 6777c478bd9Sstevel@tonic-gate { 6787c478bd9Sstevel@tonic-gate if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 6797c478bd9Sstevel@tonic-gate tv->tv_usec < 0 || tv->tv_usec >= MICROSEC) 6807c478bd9Sstevel@tonic-gate return (EINVAL); 6817c478bd9Sstevel@tonic-gate if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < minimum) 6827c478bd9Sstevel@tonic-gate tv->tv_usec = minimum; 6837c478bd9Sstevel@tonic-gate return (0); 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate /* 6877c478bd9Sstevel@tonic-gate * Same as itimerfix, except a) it takes a timespec instead of a timeval and 6887c478bd9Sstevel@tonic-gate * b) it doesn't truncate based on timeout granularity; consumers of this 6897c478bd9Sstevel@tonic-gate * interface (e.g. timer_settime()) depend on the passed timespec not being 6907c478bd9Sstevel@tonic-gate * modified implicitly. 6917c478bd9Sstevel@tonic-gate */ 6927c478bd9Sstevel@tonic-gate int 6937c478bd9Sstevel@tonic-gate itimerspecfix(timespec_t *tv) 6947c478bd9Sstevel@tonic-gate { 6957c478bd9Sstevel@tonic-gate if (tv->tv_sec < 0 || tv->tv_nsec < 0 || tv->tv_nsec >= NANOSEC) 6967c478bd9Sstevel@tonic-gate return (EINVAL); 6977c478bd9Sstevel@tonic-gate return (0); 6987c478bd9Sstevel@tonic-gate } 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate /* 7017c478bd9Sstevel@tonic-gate * Decrement an interval timer by a specified number 7027c478bd9Sstevel@tonic-gate * of microseconds, which must be less than a second, 7037c478bd9Sstevel@tonic-gate * i.e. < 1000000. If the timer expires, then reload 7047c478bd9Sstevel@tonic-gate * it. In this case, carry over (usec - old value) to 7057c478bd9Sstevel@tonic-gate * reducint the value reloaded into the timer so that 7067c478bd9Sstevel@tonic-gate * the timer does not drift. This routine assumes 7077c478bd9Sstevel@tonic-gate * that it is called in a context where the timers 7087c478bd9Sstevel@tonic-gate * on which it is operating cannot change in value. 7097c478bd9Sstevel@tonic-gate */ 7107c478bd9Sstevel@tonic-gate int 7117c478bd9Sstevel@tonic-gate itimerdecr(struct itimerval *itp, int usec) 7127c478bd9Sstevel@tonic-gate { 7137c478bd9Sstevel@tonic-gate if (itp->it_value.tv_usec < usec) { 7147c478bd9Sstevel@tonic-gate if (itp->it_value.tv_sec == 0) { 7157c478bd9Sstevel@tonic-gate /* expired, and already in next interval */ 7167c478bd9Sstevel@tonic-gate usec -= itp->it_value.tv_usec; 7177c478bd9Sstevel@tonic-gate goto expire; 7187c478bd9Sstevel@tonic-gate } 7197c478bd9Sstevel@tonic-gate itp->it_value.tv_usec += MICROSEC; 7207c478bd9Sstevel@tonic-gate itp->it_value.tv_sec--; 7217c478bd9Sstevel@tonic-gate } 7227c478bd9Sstevel@tonic-gate itp->it_value.tv_usec -= usec; 7237c478bd9Sstevel@tonic-gate usec = 0; 7247c478bd9Sstevel@tonic-gate if (timerisset(&itp->it_value)) 7257c478bd9Sstevel@tonic-gate return (1); 7267c478bd9Sstevel@tonic-gate /* expired, exactly at end of interval */ 7277c478bd9Sstevel@tonic-gate expire: 7287c478bd9Sstevel@tonic-gate if (timerisset(&itp->it_interval)) { 7297c478bd9Sstevel@tonic-gate itp->it_value = itp->it_interval; 7307c478bd9Sstevel@tonic-gate itp->it_value.tv_usec -= usec; 7317c478bd9Sstevel@tonic-gate if (itp->it_value.tv_usec < 0) { 7327c478bd9Sstevel@tonic-gate itp->it_value.tv_usec += MICROSEC; 7337c478bd9Sstevel@tonic-gate itp->it_value.tv_sec--; 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate } else 7367c478bd9Sstevel@tonic-gate itp->it_value.tv_usec = 0; /* sec is already 0 */ 7377c478bd9Sstevel@tonic-gate return (0); 7387c478bd9Sstevel@tonic-gate } 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate /* 7417c478bd9Sstevel@tonic-gate * Add and subtract routines for timevals. 7427c478bd9Sstevel@tonic-gate * N.B.: subtract routine doesn't deal with 7437c478bd9Sstevel@tonic-gate * results which are before the beginning, 7447c478bd9Sstevel@tonic-gate * it just gets very confused in this case. 7457c478bd9Sstevel@tonic-gate * Caveat emptor. 7467c478bd9Sstevel@tonic-gate */ 7477c478bd9Sstevel@tonic-gate void 7487c478bd9Sstevel@tonic-gate timevaladd(struct timeval *t1, struct timeval *t2) 7497c478bd9Sstevel@tonic-gate { 7507c478bd9Sstevel@tonic-gate t1->tv_sec += t2->tv_sec; 7517c478bd9Sstevel@tonic-gate t1->tv_usec += t2->tv_usec; 7527c478bd9Sstevel@tonic-gate timevalfix(t1); 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate 7557c478bd9Sstevel@tonic-gate void 7567c478bd9Sstevel@tonic-gate timevalsub(struct timeval *t1, struct timeval *t2) 7577c478bd9Sstevel@tonic-gate { 7587c478bd9Sstevel@tonic-gate t1->tv_sec -= t2->tv_sec; 7597c478bd9Sstevel@tonic-gate t1->tv_usec -= t2->tv_usec; 7607c478bd9Sstevel@tonic-gate timevalfix(t1); 7617c478bd9Sstevel@tonic-gate } 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate void 7647c478bd9Sstevel@tonic-gate timevalfix(struct timeval *t1) 7657c478bd9Sstevel@tonic-gate { 7667c478bd9Sstevel@tonic-gate if (t1->tv_usec < 0) { 7677c478bd9Sstevel@tonic-gate t1->tv_sec--; 7687c478bd9Sstevel@tonic-gate t1->tv_usec += MICROSEC; 7697c478bd9Sstevel@tonic-gate } 7707c478bd9Sstevel@tonic-gate if (t1->tv_usec >= MICROSEC) { 7717c478bd9Sstevel@tonic-gate t1->tv_sec++; 7727c478bd9Sstevel@tonic-gate t1->tv_usec -= MICROSEC; 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate 7767c478bd9Sstevel@tonic-gate /* 7777c478bd9Sstevel@tonic-gate * Same as the routines above. These routines take a timespec instead 7787c478bd9Sstevel@tonic-gate * of a timeval. 7797c478bd9Sstevel@tonic-gate */ 7807c478bd9Sstevel@tonic-gate void 7817c478bd9Sstevel@tonic-gate timespecadd(timespec_t *t1, timespec_t *t2) 7827c478bd9Sstevel@tonic-gate { 7837c478bd9Sstevel@tonic-gate t1->tv_sec += t2->tv_sec; 7847c478bd9Sstevel@tonic-gate t1->tv_nsec += t2->tv_nsec; 7857c478bd9Sstevel@tonic-gate timespecfix(t1); 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate void 7897c478bd9Sstevel@tonic-gate timespecsub(timespec_t *t1, timespec_t *t2) 7907c478bd9Sstevel@tonic-gate { 7917c478bd9Sstevel@tonic-gate t1->tv_sec -= t2->tv_sec; 7927c478bd9Sstevel@tonic-gate t1->tv_nsec -= t2->tv_nsec; 7937c478bd9Sstevel@tonic-gate timespecfix(t1); 7947c478bd9Sstevel@tonic-gate } 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate void 7977c478bd9Sstevel@tonic-gate timespecfix(timespec_t *t1) 7987c478bd9Sstevel@tonic-gate { 7997c478bd9Sstevel@tonic-gate if (t1->tv_nsec < 0) { 8007c478bd9Sstevel@tonic-gate t1->tv_sec--; 8017c478bd9Sstevel@tonic-gate t1->tv_nsec += NANOSEC; 8027c478bd9Sstevel@tonic-gate } else { 8037c478bd9Sstevel@tonic-gate if (t1->tv_nsec >= NANOSEC) { 8047c478bd9Sstevel@tonic-gate t1->tv_sec++; 8057c478bd9Sstevel@tonic-gate t1->tv_nsec -= NANOSEC; 8067c478bd9Sstevel@tonic-gate } 8077c478bd9Sstevel@tonic-gate } 8087c478bd9Sstevel@tonic-gate } 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate /* 8117c478bd9Sstevel@tonic-gate * Compute number of hz until specified time. 8127c478bd9Sstevel@tonic-gate * Used to compute third argument to timeout() from an absolute time. 8137c478bd9Sstevel@tonic-gate */ 8147c478bd9Sstevel@tonic-gate clock_t 8157c478bd9Sstevel@tonic-gate hzto(struct timeval *tv) 8167c478bd9Sstevel@tonic-gate { 8177c478bd9Sstevel@tonic-gate timespec_t ts, now; 8187c478bd9Sstevel@tonic-gate 8197c478bd9Sstevel@tonic-gate ts.tv_sec = tv->tv_sec; 8207c478bd9Sstevel@tonic-gate ts.tv_nsec = tv->tv_usec * 1000; 8217c478bd9Sstevel@tonic-gate gethrestime_lasttick(&now); 8227c478bd9Sstevel@tonic-gate 8237c478bd9Sstevel@tonic-gate return (timespectohz(&ts, now)); 8247c478bd9Sstevel@tonic-gate } 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate /* 8277c478bd9Sstevel@tonic-gate * Compute number of hz until specified time for a given timespec value. 8287c478bd9Sstevel@tonic-gate * Used to compute third argument to timeout() from an absolute time. 8297c478bd9Sstevel@tonic-gate */ 8307c478bd9Sstevel@tonic-gate clock_t 8317c478bd9Sstevel@tonic-gate timespectohz(timespec_t *tv, timespec_t now) 8327c478bd9Sstevel@tonic-gate { 8337c478bd9Sstevel@tonic-gate clock_t ticks; 8347c478bd9Sstevel@tonic-gate time_t sec; 8357c478bd9Sstevel@tonic-gate int nsec; 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate /* 8387c478bd9Sstevel@tonic-gate * Compute number of ticks we will see between now and 8397c478bd9Sstevel@tonic-gate * the target time; returns "1" if the destination time 8407c478bd9Sstevel@tonic-gate * is before the next tick, so we always get some delay, 8417c478bd9Sstevel@tonic-gate * and returns LONG_MAX ticks if we would overflow. 8427c478bd9Sstevel@tonic-gate */ 8437c478bd9Sstevel@tonic-gate sec = tv->tv_sec - now.tv_sec; 8447c478bd9Sstevel@tonic-gate nsec = tv->tv_nsec - now.tv_nsec + nsec_per_tick - 1; 8457c478bd9Sstevel@tonic-gate 8467c478bd9Sstevel@tonic-gate if (nsec < 0) { 8477c478bd9Sstevel@tonic-gate sec--; 8487c478bd9Sstevel@tonic-gate nsec += NANOSEC; 8497c478bd9Sstevel@tonic-gate } else if (nsec >= NANOSEC) { 8507c478bd9Sstevel@tonic-gate sec++; 8517c478bd9Sstevel@tonic-gate nsec -= NANOSEC; 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate ticks = NSEC_TO_TICK(nsec); 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate /* 8577c478bd9Sstevel@tonic-gate * Compute ticks, accounting for negative and overflow as above. 8587c478bd9Sstevel@tonic-gate * Overflow protection kicks in at about 70 weeks for hz=50 8597c478bd9Sstevel@tonic-gate * and at about 35 weeks for hz=100. (Rather longer for the 64-bit 8607c478bd9Sstevel@tonic-gate * kernel :-) 8617c478bd9Sstevel@tonic-gate */ 8627c478bd9Sstevel@tonic-gate if (sec < 0 || (sec == 0 && ticks < 1)) 8637c478bd9Sstevel@tonic-gate ticks = 1; /* protect vs nonpositive */ 8647c478bd9Sstevel@tonic-gate else if (sec > (LONG_MAX - ticks) / hz) 8657c478bd9Sstevel@tonic-gate ticks = LONG_MAX; /* protect vs overflow */ 8667c478bd9Sstevel@tonic-gate else 8677c478bd9Sstevel@tonic-gate ticks += sec * hz; /* common case */ 8687c478bd9Sstevel@tonic-gate 8697c478bd9Sstevel@tonic-gate return (ticks); 8707c478bd9Sstevel@tonic-gate } 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate /* 873*f635d46aSqiao * Compute number of hz with the timespec tv specified. 874*f635d46aSqiao * The return type must be 64 bit integer. 8753348528fSdm120769 */ 876*f635d46aSqiao int64_t 877*f635d46aSqiao timespectohz64(timespec_t *tv) 8783348528fSdm120769 { 879*f635d46aSqiao int64_t ticks; 880*f635d46aSqiao int64_t sec; 881*f635d46aSqiao int64_t nsec; 8823348528fSdm120769 883*f635d46aSqiao sec = tv->tv_sec; 884*f635d46aSqiao nsec = tv->tv_nsec + nsec_per_tick - 1; 885*f635d46aSqiao 886*f635d46aSqiao if (nsec < 0) { 887*f635d46aSqiao sec--; 888*f635d46aSqiao nsec += NANOSEC; 889*f635d46aSqiao } else if (nsec >= NANOSEC) { 890*f635d46aSqiao sec++; 891*f635d46aSqiao nsec -= NANOSEC; 892*f635d46aSqiao } 893*f635d46aSqiao 894*f635d46aSqiao ticks = NSEC_TO_TICK(nsec); 895*f635d46aSqiao 896*f635d46aSqiao /* 897*f635d46aSqiao * Compute ticks, accounting for negative and overflow as above. 898*f635d46aSqiao * Overflow protection kicks in at about 70 weeks for hz=50 899*f635d46aSqiao * and at about 35 weeks for hz=100. (Rather longer for the 64-bit 900*f635d46aSqiao * kernel 901*f635d46aSqiao */ 902*f635d46aSqiao if (sec < 0 || (sec == 0 && ticks < 1)) 903*f635d46aSqiao ticks = 1; /* protect vs nonpositive */ 904*f635d46aSqiao else if (sec > (((~0ULL) >> 1) - ticks) / hz) 905*f635d46aSqiao ticks = (~0ULL) >> 1; /* protect vs overflow */ 906*f635d46aSqiao else 907*f635d46aSqiao ticks += sec * hz; /* common case */ 908*f635d46aSqiao 909*f635d46aSqiao return (ticks); 9103348528fSdm120769 } 9113348528fSdm120769 9123348528fSdm120769 /* 9137c478bd9Sstevel@tonic-gate * hrt2ts(): convert from hrtime_t to timestruc_t. 9147c478bd9Sstevel@tonic-gate * 9157c478bd9Sstevel@tonic-gate * All this routine really does is: 9167c478bd9Sstevel@tonic-gate * 9177c478bd9Sstevel@tonic-gate * tsp->sec = hrt / NANOSEC; 9187c478bd9Sstevel@tonic-gate * tsp->nsec = hrt % NANOSEC; 9197c478bd9Sstevel@tonic-gate * 9207c478bd9Sstevel@tonic-gate * The black magic below avoids doing a 64-bit by 32-bit integer divide, 9217c478bd9Sstevel@tonic-gate * which is quite expensive. There's actually much more going on here than 9227c478bd9Sstevel@tonic-gate * it might first appear -- don't try this at home. 9237c478bd9Sstevel@tonic-gate * 9247c478bd9Sstevel@tonic-gate * For the adventuresome, here's an explanation of how it works. 9257c478bd9Sstevel@tonic-gate * 9267c478bd9Sstevel@tonic-gate * Multiplication by a fixed constant is easy -- you just do the appropriate 9277c478bd9Sstevel@tonic-gate * shifts and adds. For example, to multiply by 10, we observe that 9287c478bd9Sstevel@tonic-gate * 9297c478bd9Sstevel@tonic-gate * x * 10 = x * (8 + 2) 9307c478bd9Sstevel@tonic-gate * = (x * 8) + (x * 2) 9317c478bd9Sstevel@tonic-gate * = (x << 3) + (x << 1). 9327c478bd9Sstevel@tonic-gate * 9337c478bd9Sstevel@tonic-gate * In general, you can read the algorithm right off the bits: the number 10 9347c478bd9Sstevel@tonic-gate * is 1010 in binary; bits 1 and 3 are ones, so x * 10 = (x << 1) + (x << 3). 9357c478bd9Sstevel@tonic-gate * 9367c478bd9Sstevel@tonic-gate * Sometimes you can do better. For example, 15 is 1111 binary, so the normal 9377c478bd9Sstevel@tonic-gate * shift/add computation is x * 15 = (x << 0) + (x << 1) + (x << 2) + (x << 3). 9387c478bd9Sstevel@tonic-gate * But, it's cheaper if you capitalize on the fact that you have a run of ones: 9397c478bd9Sstevel@tonic-gate * 1111 = 10000 - 1, hence x * 15 = (x << 4) - (x << 0). [You would never 9407c478bd9Sstevel@tonic-gate * actually perform the operation << 0, since it's a no-op; I'm just writing 9417c478bd9Sstevel@tonic-gate * it that way for clarity.] 9427c478bd9Sstevel@tonic-gate * 9437c478bd9Sstevel@tonic-gate * The other way you can win is if you get lucky with the prime factorization 9447c478bd9Sstevel@tonic-gate * of your constant. The number 1,000,000,000, which we have to multiply 9457c478bd9Sstevel@tonic-gate * by below, is a good example. One billion is 111011100110101100101000000000 9467c478bd9Sstevel@tonic-gate * in binary. If you apply the bit-grouping trick, it doesn't buy you very 9477c478bd9Sstevel@tonic-gate * much, because it's only a win for groups of three or more equal bits: 9487c478bd9Sstevel@tonic-gate * 9497c478bd9Sstevel@tonic-gate * 111011100110101100101000000000 = 1000000000000000000000000000000 9507c478bd9Sstevel@tonic-gate * - 000100011001010011011000000000 9517c478bd9Sstevel@tonic-gate * 9527c478bd9Sstevel@tonic-gate * Thus, instead of the 13 shift/add pairs (26 operations) implied by the LHS, 9537c478bd9Sstevel@tonic-gate * we have reduced this to 10 shift/add pairs (20 operations) on the RHS. 9547c478bd9Sstevel@tonic-gate * This is better, but not great. 9557c478bd9Sstevel@tonic-gate * 9567c478bd9Sstevel@tonic-gate * However, we can factor 1,000,000,000 = 2^9 * 5^9 = 2^9 * 125 * 125 * 125, 9577c478bd9Sstevel@tonic-gate * and multiply by each factor. Multiplication by 125 is particularly easy, 9587c478bd9Sstevel@tonic-gate * since 128 is nearby: x * 125 = (x << 7) - x - x - x, which is just four 9597c478bd9Sstevel@tonic-gate * operations. So, to multiply by 1,000,000,000, we perform three multipli- 9607c478bd9Sstevel@tonic-gate * cations by 125, then << 9, a total of only 3 * 4 + 1 = 13 operations. 9617c478bd9Sstevel@tonic-gate * This is the algorithm we actually use in both hrt2ts() and ts2hrt(). 9627c478bd9Sstevel@tonic-gate * 9637c478bd9Sstevel@tonic-gate * Division is harder; there is no equivalent of the simple shift-add algorithm 9647c478bd9Sstevel@tonic-gate * we used for multiplication. However, we can convert the division problem 9657c478bd9Sstevel@tonic-gate * into a multiplication problem by pre-computing the binary representation 9667c478bd9Sstevel@tonic-gate * of the reciprocal of the divisor. For the case of interest, we have 9677c478bd9Sstevel@tonic-gate * 9687c478bd9Sstevel@tonic-gate * 1 / 1,000,000,000 = 1.0001001011100000101111101000001B-30, 9697c478bd9Sstevel@tonic-gate * 9707c478bd9Sstevel@tonic-gate * to 32 bits of precision. (The notation B-30 means "* 2^-30", just like 9717c478bd9Sstevel@tonic-gate * E-18 means "* 10^-18".) 9727c478bd9Sstevel@tonic-gate * 9737c478bd9Sstevel@tonic-gate * So, to compute x / 1,000,000,000, we just multiply x by the 32-bit 9747c478bd9Sstevel@tonic-gate * integer 10001001011100000101111101000001, then normalize (shift) the 9757c478bd9Sstevel@tonic-gate * result. This constant has several large bits runs, so the multiply 9767c478bd9Sstevel@tonic-gate * is relatively cheap: 9777c478bd9Sstevel@tonic-gate * 9787c478bd9Sstevel@tonic-gate * 10001001011100000101111101000001 = 10001001100000000110000001000001 9797c478bd9Sstevel@tonic-gate * - 00000000000100000000000100000000 9807c478bd9Sstevel@tonic-gate * 9817c478bd9Sstevel@tonic-gate * Again, you can just read the algorithm right off the bits: 9827c478bd9Sstevel@tonic-gate * 9837c478bd9Sstevel@tonic-gate * sec = hrt; 9847c478bd9Sstevel@tonic-gate * sec += (hrt << 6); 9857c478bd9Sstevel@tonic-gate * sec -= (hrt << 8); 9867c478bd9Sstevel@tonic-gate * sec += (hrt << 13); 9877c478bd9Sstevel@tonic-gate * sec += (hrt << 14); 9887c478bd9Sstevel@tonic-gate * sec -= (hrt << 20); 9897c478bd9Sstevel@tonic-gate * sec += (hrt << 23); 9907c478bd9Sstevel@tonic-gate * sec += (hrt << 24); 9917c478bd9Sstevel@tonic-gate * sec += (hrt << 27); 9927c478bd9Sstevel@tonic-gate * sec += (hrt << 31); 9937c478bd9Sstevel@tonic-gate * sec >>= (32 + 30); 9947c478bd9Sstevel@tonic-gate * 9957c478bd9Sstevel@tonic-gate * Voila! The only problem is, since hrt is 64 bits, we need to use 96-bit 9967c478bd9Sstevel@tonic-gate * arithmetic to perform this calculation. That's a waste, because ultimately 9977c478bd9Sstevel@tonic-gate * we only need the highest 32 bits of the result. 9987c478bd9Sstevel@tonic-gate * 9997c478bd9Sstevel@tonic-gate * The first thing we do is to realize that we don't need to use all of hrt 10007c478bd9Sstevel@tonic-gate * in the calculation. The lowest 30 bits can contribute at most 1 to the 10017c478bd9Sstevel@tonic-gate * quotient (2^30 / 1,000,000,000 = 1.07...), so we'll deal with them later. 10027c478bd9Sstevel@tonic-gate * The highest 2 bits have to be zero, or hrt won't fit in a timestruc_t. 10037c478bd9Sstevel@tonic-gate * Thus, the only bits of hrt that matter for division are bits 30..61. 10047c478bd9Sstevel@tonic-gate * These 32 bits are just the lower-order word of (hrt >> 30). This brings 10057c478bd9Sstevel@tonic-gate * us down from 96-bit math to 64-bit math, and our algorithm becomes: 10067c478bd9Sstevel@tonic-gate * 10077c478bd9Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30); 10087c478bd9Sstevel@tonic-gate * sec = tmp; 10097c478bd9Sstevel@tonic-gate * sec += (tmp << 6); 10107c478bd9Sstevel@tonic-gate * sec -= (tmp << 8); 10117c478bd9Sstevel@tonic-gate * sec += (tmp << 13); 10127c478bd9Sstevel@tonic-gate * sec += (tmp << 14); 10137c478bd9Sstevel@tonic-gate * sec -= (tmp << 20); 10147c478bd9Sstevel@tonic-gate * sec += (tmp << 23); 10157c478bd9Sstevel@tonic-gate * sec += (tmp << 24); 10167c478bd9Sstevel@tonic-gate * sec += (tmp << 27); 10177c478bd9Sstevel@tonic-gate * sec += (tmp << 31); 10187c478bd9Sstevel@tonic-gate * sec >>= 32; 10197c478bd9Sstevel@tonic-gate * 10207c478bd9Sstevel@tonic-gate * Next, we're going to reduce this 64-bit computation to a 32-bit 10217c478bd9Sstevel@tonic-gate * computation. We begin by rewriting the above algorithm to use relative 10227c478bd9Sstevel@tonic-gate * shifts instead of absolute shifts. That is, instead of computing 10237c478bd9Sstevel@tonic-gate * tmp << 6, tmp << 8, tmp << 13, etc, we'll just shift incrementally: 10247c478bd9Sstevel@tonic-gate * tmp <<= 6, tmp <<= 2 (== 8 - 6), tmp <<= 5 (== 13 - 8), etc: 10257c478bd9Sstevel@tonic-gate * 10267c478bd9Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30); 10277c478bd9Sstevel@tonic-gate * sec = tmp; 10287c478bd9Sstevel@tonic-gate * tmp <<= 6; sec += tmp; 10297c478bd9Sstevel@tonic-gate * tmp <<= 2; sec -= tmp; 10307c478bd9Sstevel@tonic-gate * tmp <<= 5; sec += tmp; 10317c478bd9Sstevel@tonic-gate * tmp <<= 1; sec += tmp; 10327c478bd9Sstevel@tonic-gate * tmp <<= 6; sec -= tmp; 10337c478bd9Sstevel@tonic-gate * tmp <<= 3; sec += tmp; 10347c478bd9Sstevel@tonic-gate * tmp <<= 1; sec += tmp; 10357c478bd9Sstevel@tonic-gate * tmp <<= 3; sec += tmp; 10367c478bd9Sstevel@tonic-gate * tmp <<= 4; sec += tmp; 10377c478bd9Sstevel@tonic-gate * sec >>= 32; 10387c478bd9Sstevel@tonic-gate * 10397c478bd9Sstevel@tonic-gate * Now for the final step. Instead of throwing away the low 32 bits at 10407c478bd9Sstevel@tonic-gate * the end, we can throw them away as we go, only keeping the high 32 bits 10417c478bd9Sstevel@tonic-gate * of the product at each step. So, for example, where we now have 10427c478bd9Sstevel@tonic-gate * 10437c478bd9Sstevel@tonic-gate * tmp <<= 6; sec = sec + tmp; 10447c478bd9Sstevel@tonic-gate * we will instead have 10457c478bd9Sstevel@tonic-gate * tmp <<= 6; sec = (sec + tmp) >> 6; 10467c478bd9Sstevel@tonic-gate * which is equivalent to 10477c478bd9Sstevel@tonic-gate * sec = (sec >> 6) + tmp; 10487c478bd9Sstevel@tonic-gate * 10497c478bd9Sstevel@tonic-gate * The final shift ("sec >>= 32") goes away. 10507c478bd9Sstevel@tonic-gate * 10517c478bd9Sstevel@tonic-gate * All we're really doing here is long multiplication, just like we learned in 10527c478bd9Sstevel@tonic-gate * grade school, except that at each step, we only look at the leftmost 32 10537c478bd9Sstevel@tonic-gate * columns. The cumulative error is, at most, the sum of all the bits we 10547c478bd9Sstevel@tonic-gate * throw away, which is 2^-32 + 2^-31 + ... + 2^-2 + 2^-1 == 1 - 2^-32. 10557c478bd9Sstevel@tonic-gate * Thus, the final result ("sec") is correct to +/- 1. 10567c478bd9Sstevel@tonic-gate * 10577c478bd9Sstevel@tonic-gate * It turns out to be important to keep "sec" positive at each step, because 10587c478bd9Sstevel@tonic-gate * we don't want to have to explicitly extend the sign bit. Therefore, 10597c478bd9Sstevel@tonic-gate * starting with the last line of code above, each line that would have read 10607c478bd9Sstevel@tonic-gate * "sec = (sec >> n) - tmp" must be changed to "sec = tmp - (sec >> n)", and 10617c478bd9Sstevel@tonic-gate * the operators (+ or -) in all previous lines must be toggled accordingly. 10627c478bd9Sstevel@tonic-gate * Thus, we end up with: 10637c478bd9Sstevel@tonic-gate * 10647c478bd9Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30); 10657c478bd9Sstevel@tonic-gate * sec = tmp + (sec >> 6); 10667c478bd9Sstevel@tonic-gate * sec = tmp - (tmp >> 2); 10677c478bd9Sstevel@tonic-gate * sec = tmp - (sec >> 5); 10687c478bd9Sstevel@tonic-gate * sec = tmp + (sec >> 1); 10697c478bd9Sstevel@tonic-gate * sec = tmp - (sec >> 6); 10707c478bd9Sstevel@tonic-gate * sec = tmp - (sec >> 3); 10717c478bd9Sstevel@tonic-gate * sec = tmp + (sec >> 1); 10727c478bd9Sstevel@tonic-gate * sec = tmp + (sec >> 3); 10737c478bd9Sstevel@tonic-gate * sec = tmp + (sec >> 4); 10747c478bd9Sstevel@tonic-gate * 10757c478bd9Sstevel@tonic-gate * This yields a value for sec that is accurate to +1/-1, so we have two 10767c478bd9Sstevel@tonic-gate * cases to deal with. The mysterious-looking "+ 7" in the code below biases 10777c478bd9Sstevel@tonic-gate * the rounding toward zero, so that sec is always less than or equal to 10787c478bd9Sstevel@tonic-gate * the correct value. With this modified code, sec is accurate to +0/-2, with 10797c478bd9Sstevel@tonic-gate * the -2 case being very rare in practice. With this change, we only have to 10807c478bd9Sstevel@tonic-gate * deal with one case (sec too small) in the cleanup code. 10817c478bd9Sstevel@tonic-gate * 10827c478bd9Sstevel@tonic-gate * The other modification we make is to delete the second line above 10837c478bd9Sstevel@tonic-gate * ("sec = tmp + (sec >> 6);"), since it only has an effect when bit 31 is 10847c478bd9Sstevel@tonic-gate * set, and the cleanup code can handle that rare case. This reduces the 10857c478bd9Sstevel@tonic-gate * *guaranteed* accuracy of sec to +0/-3, but speeds up the common cases. 10867c478bd9Sstevel@tonic-gate * 10877c478bd9Sstevel@tonic-gate * Finally, we compute nsec = hrt - (sec * 1,000,000,000). nsec will always 10887c478bd9Sstevel@tonic-gate * be positive (since sec is never too large), and will at most be equal to 10897c478bd9Sstevel@tonic-gate * the error in sec (times 1,000,000,000) plus the low-order 30 bits of hrt. 10907c478bd9Sstevel@tonic-gate * Thus, nsec < 3 * 1,000,000,000 + 2^30, which is less than 2^32, so we can 10917c478bd9Sstevel@tonic-gate * safely assume that nsec fits in 32 bits. Consequently, when we compute 10927c478bd9Sstevel@tonic-gate * sec * 1,000,000,000, we only need the low 32 bits, so we can just do 32-bit 10937c478bd9Sstevel@tonic-gate * arithmetic and let the high-order bits fall off the end. 10947c478bd9Sstevel@tonic-gate * 10957c478bd9Sstevel@tonic-gate * Since nsec < 3 * 1,000,000,000 + 2^30 == 4,073,741,824, the cleanup loop: 10967c478bd9Sstevel@tonic-gate * 10977c478bd9Sstevel@tonic-gate * while (nsec >= NANOSEC) { 10987c478bd9Sstevel@tonic-gate * nsec -= NANOSEC; 10997c478bd9Sstevel@tonic-gate * sec++; 11007c478bd9Sstevel@tonic-gate * } 11017c478bd9Sstevel@tonic-gate * 11027c478bd9Sstevel@tonic-gate * is guaranteed to complete in at most 4 iterations. In practice, the loop 11037c478bd9Sstevel@tonic-gate * completes in 0 or 1 iteration over 95% of the time. 11047c478bd9Sstevel@tonic-gate * 11057c478bd9Sstevel@tonic-gate * On an SS2, this implementation of hrt2ts() takes 1.7 usec, versus about 11067c478bd9Sstevel@tonic-gate * 35 usec for software division -- about 20 times faster. 11077c478bd9Sstevel@tonic-gate */ 11087c478bd9Sstevel@tonic-gate void 11097c478bd9Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp) 11107c478bd9Sstevel@tonic-gate { 11117c478bd9Sstevel@tonic-gate uint32_t sec, nsec, tmp; 11127c478bd9Sstevel@tonic-gate 11137c478bd9Sstevel@tonic-gate tmp = (uint32_t)(hrt >> 30); 11147c478bd9Sstevel@tonic-gate sec = tmp - (tmp >> 2); 11157c478bd9Sstevel@tonic-gate sec = tmp - (sec >> 5); 11167c478bd9Sstevel@tonic-gate sec = tmp + (sec >> 1); 11177c478bd9Sstevel@tonic-gate sec = tmp - (sec >> 6) + 7; 11187c478bd9Sstevel@tonic-gate sec = tmp - (sec >> 3); 11197c478bd9Sstevel@tonic-gate sec = tmp + (sec >> 1); 11207c478bd9Sstevel@tonic-gate sec = tmp + (sec >> 3); 11217c478bd9Sstevel@tonic-gate sec = tmp + (sec >> 4); 11227c478bd9Sstevel@tonic-gate tmp = (sec << 7) - sec - sec - sec; 11237c478bd9Sstevel@tonic-gate tmp = (tmp << 7) - tmp - tmp - tmp; 11247c478bd9Sstevel@tonic-gate tmp = (tmp << 7) - tmp - tmp - tmp; 11257c478bd9Sstevel@tonic-gate nsec = (uint32_t)hrt - (tmp << 9); 11267c478bd9Sstevel@tonic-gate while (nsec >= NANOSEC) { 11277c478bd9Sstevel@tonic-gate nsec -= NANOSEC; 11287c478bd9Sstevel@tonic-gate sec++; 11297c478bd9Sstevel@tonic-gate } 11307c478bd9Sstevel@tonic-gate tsp->tv_sec = (time_t)sec; 11317c478bd9Sstevel@tonic-gate tsp->tv_nsec = nsec; 11327c478bd9Sstevel@tonic-gate } 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate /* 11357c478bd9Sstevel@tonic-gate * Convert from timestruc_t to hrtime_t. 11367c478bd9Sstevel@tonic-gate * 11377c478bd9Sstevel@tonic-gate * The code below is equivalent to: 11387c478bd9Sstevel@tonic-gate * 11397c478bd9Sstevel@tonic-gate * hrt = tsp->tv_sec * NANOSEC + tsp->tv_nsec; 11407c478bd9Sstevel@tonic-gate * 11417c478bd9Sstevel@tonic-gate * but requires no integer multiply. 11427c478bd9Sstevel@tonic-gate */ 11437c478bd9Sstevel@tonic-gate hrtime_t 11447c478bd9Sstevel@tonic-gate ts2hrt(const timestruc_t *tsp) 11457c478bd9Sstevel@tonic-gate { 11467c478bd9Sstevel@tonic-gate hrtime_t hrt; 11477c478bd9Sstevel@tonic-gate 11487c478bd9Sstevel@tonic-gate hrt = tsp->tv_sec; 11497c478bd9Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt; 11507c478bd9Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt; 11517c478bd9Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt; 11527c478bd9Sstevel@tonic-gate hrt = (hrt << 9) + tsp->tv_nsec; 11537c478bd9Sstevel@tonic-gate return (hrt); 11547c478bd9Sstevel@tonic-gate } 11557c478bd9Sstevel@tonic-gate 11567c478bd9Sstevel@tonic-gate /* 11577c478bd9Sstevel@tonic-gate * For the various 32-bit "compatibility" paths in the system. 11587c478bd9Sstevel@tonic-gate */ 11597c478bd9Sstevel@tonic-gate void 11607c478bd9Sstevel@tonic-gate hrt2ts32(hrtime_t hrt, timestruc32_t *ts32p) 11617c478bd9Sstevel@tonic-gate { 11627c478bd9Sstevel@tonic-gate timestruc_t ts; 11637c478bd9Sstevel@tonic-gate 11647c478bd9Sstevel@tonic-gate hrt2ts(hrt, &ts); 11657c478bd9Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(ts32p, &ts); 11667c478bd9Sstevel@tonic-gate } 11677c478bd9Sstevel@tonic-gate 11687c478bd9Sstevel@tonic-gate /* 11697c478bd9Sstevel@tonic-gate * If this ever becomes performance critical (ha!), we can borrow the 11707c478bd9Sstevel@tonic-gate * code from ts2hrt(), above, to multiply tv_sec by 1,000,000 and the 11717c478bd9Sstevel@tonic-gate * straightforward (x << 10) - (x << 5) + (x << 3) to multiply tv_usec by 11727c478bd9Sstevel@tonic-gate * 1,000. For now, we'll opt for readability (besides, the compiler does 11737c478bd9Sstevel@tonic-gate * a passable job of optimizing constant multiplication into shifts and adds). 11747c478bd9Sstevel@tonic-gate */ 11757c478bd9Sstevel@tonic-gate hrtime_t 11767c478bd9Sstevel@tonic-gate tv2hrt(struct timeval *tvp) 11777c478bd9Sstevel@tonic-gate { 11787c478bd9Sstevel@tonic-gate return ((hrtime_t)tvp->tv_sec * NANOSEC + 11797c478bd9Sstevel@tonic-gate (hrtime_t)tvp->tv_usec * (NANOSEC / MICROSEC)); 11807c478bd9Sstevel@tonic-gate } 11817c478bd9Sstevel@tonic-gate 11827c478bd9Sstevel@tonic-gate void 1183be2140a8Sandyb hrt2tv(hrtime_t hrt, struct timeval *tvp) 11847c478bd9Sstevel@tonic-gate { 1185be2140a8Sandyb uint32_t sec, nsec, tmp; 1186be2140a8Sandyb uint32_t q, r, t; 1187be2140a8Sandyb 1188be2140a8Sandyb tmp = (uint32_t)(hrt >> 30); 1189be2140a8Sandyb sec = tmp - (tmp >> 2); 1190be2140a8Sandyb sec = tmp - (sec >> 5); 1191be2140a8Sandyb sec = tmp + (sec >> 1); 1192be2140a8Sandyb sec = tmp - (sec >> 6) + 7; 1193be2140a8Sandyb sec = tmp - (sec >> 3); 1194be2140a8Sandyb sec = tmp + (sec >> 1); 1195be2140a8Sandyb sec = tmp + (sec >> 3); 1196be2140a8Sandyb sec = tmp + (sec >> 4); 1197be2140a8Sandyb tmp = (sec << 7) - sec - sec - sec; 1198be2140a8Sandyb tmp = (tmp << 7) - tmp - tmp - tmp; 1199be2140a8Sandyb tmp = (tmp << 7) - tmp - tmp - tmp; 1200be2140a8Sandyb nsec = (uint32_t)hrt - (tmp << 9); 1201be2140a8Sandyb while (nsec >= NANOSEC) { 1202be2140a8Sandyb nsec -= NANOSEC; 1203be2140a8Sandyb sec++; 1204be2140a8Sandyb } 1205be2140a8Sandyb tvp->tv_sec = (time_t)sec; 1206be2140a8Sandyb /* 1207be2140a8Sandyb * this routine is very similar to hr2ts, but requires microseconds 1208be2140a8Sandyb * instead of nanoseconds, so an interger divide by 1000 routine 1209be2140a8Sandyb * completes the conversion 1210be2140a8Sandyb */ 1211be2140a8Sandyb t = (nsec >> 7) + (nsec >> 8) + (nsec >> 12); 1212be2140a8Sandyb q = (nsec >> 1) + t + (nsec >> 15) + (t >> 11) + (t >> 14); 1213be2140a8Sandyb q = q >> 9; 1214be2140a8Sandyb r = nsec - q*1000; 1215be2140a8Sandyb tvp->tv_usec = q + ((r + 24) >> 10); 1216be2140a8Sandyb 12177c478bd9Sstevel@tonic-gate } 12187c478bd9Sstevel@tonic-gate 12197c478bd9Sstevel@tonic-gate int 12207c478bd9Sstevel@tonic-gate nanosleep(timespec_t *rqtp, timespec_t *rmtp) 12217c478bd9Sstevel@tonic-gate { 12227c478bd9Sstevel@tonic-gate timespec_t rqtime; 12237c478bd9Sstevel@tonic-gate timespec_t rmtime; 12247c478bd9Sstevel@tonic-gate timespec_t now; 12253348528fSdm120769 int timecheck; 12267c478bd9Sstevel@tonic-gate int ret = 1; 12277c478bd9Sstevel@tonic-gate model_t datamodel = get_udatamodel(); 12287c478bd9Sstevel@tonic-gate 12297c478bd9Sstevel@tonic-gate if (datamodel == DATAMODEL_NATIVE) { 12307c478bd9Sstevel@tonic-gate if (copyin(rqtp, &rqtime, sizeof (rqtime))) 12317c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 12327c478bd9Sstevel@tonic-gate } else { 12337c478bd9Sstevel@tonic-gate timespec32_t rqtime32; 12347c478bd9Sstevel@tonic-gate 12357c478bd9Sstevel@tonic-gate if (copyin(rqtp, &rqtime32, sizeof (rqtime32))) 12367c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 12377c478bd9Sstevel@tonic-gate TIMESPEC32_TO_TIMESPEC(&rqtime, &rqtime32); 12387c478bd9Sstevel@tonic-gate } 12397c478bd9Sstevel@tonic-gate 12407c478bd9Sstevel@tonic-gate if (rqtime.tv_sec < 0 || rqtime.tv_nsec < 0 || 12417c478bd9Sstevel@tonic-gate rqtime.tv_nsec >= NANOSEC) 12427c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 12437c478bd9Sstevel@tonic-gate 12447c478bd9Sstevel@tonic-gate if (timerspecisset(&rqtime)) { 12453348528fSdm120769 timecheck = timechanged; 12467c478bd9Sstevel@tonic-gate gethrestime(&now); 12477c478bd9Sstevel@tonic-gate timespecadd(&rqtime, &now); 12487c478bd9Sstevel@tonic-gate mutex_enter(&curthread->t_delay_lock); 12497c478bd9Sstevel@tonic-gate while ((ret = cv_waituntil_sig(&curthread->t_delay_cv, 12503348528fSdm120769 &curthread->t_delay_lock, &rqtime, timecheck)) > 0) 12517c478bd9Sstevel@tonic-gate continue; 12527c478bd9Sstevel@tonic-gate mutex_exit(&curthread->t_delay_lock); 12537c478bd9Sstevel@tonic-gate } 12547c478bd9Sstevel@tonic-gate 12557c478bd9Sstevel@tonic-gate if (rmtp) { 12567c478bd9Sstevel@tonic-gate /* 12577c478bd9Sstevel@tonic-gate * If cv_waituntil_sig() returned due to a signal, and 12587c478bd9Sstevel@tonic-gate * there is time remaining, then set the time remaining. 12597c478bd9Sstevel@tonic-gate * Else set time remaining to zero 12607c478bd9Sstevel@tonic-gate */ 12617c478bd9Sstevel@tonic-gate rmtime.tv_sec = rmtime.tv_nsec = 0; 12627c478bd9Sstevel@tonic-gate if (ret == 0) { 1263b2a1c443Svb160487 timespec_t delta = rqtime; 1264b2a1c443Svb160487 12657c478bd9Sstevel@tonic-gate gethrestime(&now); 1266b2a1c443Svb160487 timespecsub(&delta, &now); 1267b2a1c443Svb160487 if (delta.tv_sec > 0 || (delta.tv_sec == 0 && 1268b2a1c443Svb160487 delta.tv_nsec > 0)) 1269b2a1c443Svb160487 rmtime = delta; 12707c478bd9Sstevel@tonic-gate } 12717c478bd9Sstevel@tonic-gate 12727c478bd9Sstevel@tonic-gate if (datamodel == DATAMODEL_NATIVE) { 12737c478bd9Sstevel@tonic-gate if (copyout(&rmtime, rmtp, sizeof (rmtime))) 12747c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 12757c478bd9Sstevel@tonic-gate } else { 12767c478bd9Sstevel@tonic-gate timespec32_t rmtime32; 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime); 12797c478bd9Sstevel@tonic-gate if (copyout(&rmtime32, rmtp, sizeof (rmtime32))) 12807c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 12817c478bd9Sstevel@tonic-gate } 12827c478bd9Sstevel@tonic-gate } 12837c478bd9Sstevel@tonic-gate 12847c478bd9Sstevel@tonic-gate if (ret == 0) 12857c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 12867c478bd9Sstevel@tonic-gate return (0); 12877c478bd9Sstevel@tonic-gate } 12887c478bd9Sstevel@tonic-gate 12897c478bd9Sstevel@tonic-gate /* 12907c478bd9Sstevel@tonic-gate * Routines to convert standard UNIX time (seconds since Jan 1, 1970) 12917c478bd9Sstevel@tonic-gate * into year/month/day/hour/minute/second format, and back again. 12927c478bd9Sstevel@tonic-gate * Note: these routines require tod_lock held to protect cached state. 12937c478bd9Sstevel@tonic-gate */ 12947c478bd9Sstevel@tonic-gate static int days_thru_month[64] = { 12957c478bd9Sstevel@tonic-gate 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 0, 0, 12967c478bd9Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0, 12977c478bd9Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0, 12987c478bd9Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0, 12997c478bd9Sstevel@tonic-gate }; 13007c478bd9Sstevel@tonic-gate 13017c478bd9Sstevel@tonic-gate todinfo_t saved_tod; 13027c478bd9Sstevel@tonic-gate int saved_utc = -60; 13037c478bd9Sstevel@tonic-gate 13047c478bd9Sstevel@tonic-gate todinfo_t 13057c478bd9Sstevel@tonic-gate utc_to_tod(time_t utc) 13067c478bd9Sstevel@tonic-gate { 13077c478bd9Sstevel@tonic-gate long dse, day, month, year; 13087c478bd9Sstevel@tonic-gate todinfo_t tod; 13097c478bd9Sstevel@tonic-gate 13107c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 13117c478bd9Sstevel@tonic-gate 13127c478bd9Sstevel@tonic-gate if (utc < 0) /* should never happen */ 13137c478bd9Sstevel@tonic-gate utc = 0; 13147c478bd9Sstevel@tonic-gate 13157c478bd9Sstevel@tonic-gate saved_tod.tod_sec += utc - saved_utc; 13167c478bd9Sstevel@tonic-gate saved_utc = utc; 13177c478bd9Sstevel@tonic-gate if (saved_tod.tod_sec >= 0 && saved_tod.tod_sec < 60) 13187c478bd9Sstevel@tonic-gate return (saved_tod); /* only the seconds changed */ 13197c478bd9Sstevel@tonic-gate 13207c478bd9Sstevel@tonic-gate dse = utc / 86400; /* days since epoch */ 13217c478bd9Sstevel@tonic-gate 13227c478bd9Sstevel@tonic-gate tod.tod_sec = utc % 60; 13237c478bd9Sstevel@tonic-gate tod.tod_min = (utc % 3600) / 60; 13247c478bd9Sstevel@tonic-gate tod.tod_hour = (utc % 86400) / 3600; 13257c478bd9Sstevel@tonic-gate tod.tod_dow = (dse + 4) % 7 + 1; /* epoch was a Thursday */ 13267c478bd9Sstevel@tonic-gate 13277c478bd9Sstevel@tonic-gate year = dse / 365 + 72; /* first guess -- always a bit too large */ 13287c478bd9Sstevel@tonic-gate do { 13297c478bd9Sstevel@tonic-gate year--; 13307c478bd9Sstevel@tonic-gate day = dse - 365 * (year - 70) - ((year - 69) >> 2); 13317c478bd9Sstevel@tonic-gate } while (day < 0); 13327c478bd9Sstevel@tonic-gate 13337c478bd9Sstevel@tonic-gate month = ((year & 3) << 4) + 1; 13347c478bd9Sstevel@tonic-gate while (day >= days_thru_month[month + 1]) 13357c478bd9Sstevel@tonic-gate month++; 13367c478bd9Sstevel@tonic-gate 13377c478bd9Sstevel@tonic-gate tod.tod_day = day - days_thru_month[month] + 1; 13387c478bd9Sstevel@tonic-gate tod.tod_month = month & 15; 13397c478bd9Sstevel@tonic-gate tod.tod_year = year; 13407c478bd9Sstevel@tonic-gate 13417c478bd9Sstevel@tonic-gate saved_tod = tod; 13427c478bd9Sstevel@tonic-gate return (tod); 13437c478bd9Sstevel@tonic-gate } 13447c478bd9Sstevel@tonic-gate 13457c478bd9Sstevel@tonic-gate time_t 13467c478bd9Sstevel@tonic-gate tod_to_utc(todinfo_t tod) 13477c478bd9Sstevel@tonic-gate { 13487c478bd9Sstevel@tonic-gate time_t utc; 13497c478bd9Sstevel@tonic-gate int year = tod.tod_year; 13507c478bd9Sstevel@tonic-gate int month = tod.tod_month + ((year & 3) << 4); 13517c478bd9Sstevel@tonic-gate #ifdef DEBUG 13527c478bd9Sstevel@tonic-gate /* only warn once, not each time called */ 13537c478bd9Sstevel@tonic-gate static int year_warn = 1; 13547c478bd9Sstevel@tonic-gate static int month_warn = 1; 13557c478bd9Sstevel@tonic-gate static int day_warn = 1; 13567c478bd9Sstevel@tonic-gate static int hour_warn = 1; 13577c478bd9Sstevel@tonic-gate static int min_warn = 1; 13587c478bd9Sstevel@tonic-gate static int sec_warn = 1; 13597c478bd9Sstevel@tonic-gate int days_diff = days_thru_month[month + 1] - days_thru_month[month]; 13607c478bd9Sstevel@tonic-gate #endif 13617c478bd9Sstevel@tonic-gate 13627c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 13637c478bd9Sstevel@tonic-gate 13647c478bd9Sstevel@tonic-gate #ifdef DEBUG 13657c478bd9Sstevel@tonic-gate if (year_warn && (year < 70 || year > 8029)) { 13667c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 13677c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 13687c478bd9Sstevel@tonic-gate "wrong years value %d -- time needs to be reset\n", 13697c478bd9Sstevel@tonic-gate year); 13707c478bd9Sstevel@tonic-gate year_warn = 0; 13717c478bd9Sstevel@tonic-gate } 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate if (month_warn && (tod.tod_month < 1 || tod.tod_month > 12)) { 13747c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 13757c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 13767c478bd9Sstevel@tonic-gate "wrong months value %d -- time needs to be reset\n", 13777c478bd9Sstevel@tonic-gate tod.tod_month); 13787c478bd9Sstevel@tonic-gate month_warn = 0; 13797c478bd9Sstevel@tonic-gate } 13807c478bd9Sstevel@tonic-gate 13817c478bd9Sstevel@tonic-gate if (day_warn && (tod.tod_day < 1 || tod.tod_day > days_diff)) { 13827c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 13837c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 13847c478bd9Sstevel@tonic-gate "wrong days value %d -- time needs to be reset\n", 13857c478bd9Sstevel@tonic-gate tod.tod_day); 13867c478bd9Sstevel@tonic-gate day_warn = 0; 13877c478bd9Sstevel@tonic-gate } 13887c478bd9Sstevel@tonic-gate 13897c478bd9Sstevel@tonic-gate if (hour_warn && (tod.tod_hour < 0 || tod.tod_hour > 23)) { 13907c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 13917c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 13927c478bd9Sstevel@tonic-gate "wrong hours value %d -- time needs to be reset\n", 13937c478bd9Sstevel@tonic-gate tod.tod_hour); 13947c478bd9Sstevel@tonic-gate hour_warn = 0; 13957c478bd9Sstevel@tonic-gate } 13967c478bd9Sstevel@tonic-gate 13977c478bd9Sstevel@tonic-gate if (min_warn && (tod.tod_min < 0 || tod.tod_min > 59)) { 13987c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 13997c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 14007c478bd9Sstevel@tonic-gate "wrong minutes value %d -- time needs to be reset\n", 14017c478bd9Sstevel@tonic-gate tod.tod_min); 14027c478bd9Sstevel@tonic-gate min_warn = 0; 14037c478bd9Sstevel@tonic-gate } 14047c478bd9Sstevel@tonic-gate 14057c478bd9Sstevel@tonic-gate if (sec_warn && (tod.tod_sec < 0 || tod.tod_sec > 59)) { 14067c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 14077c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 14087c478bd9Sstevel@tonic-gate "wrong seconds value %d -- time needs to be reset\n", 14097c478bd9Sstevel@tonic-gate tod.tod_sec); 14107c478bd9Sstevel@tonic-gate sec_warn = 0; 14117c478bd9Sstevel@tonic-gate } 14127c478bd9Sstevel@tonic-gate #endif 14137c478bd9Sstevel@tonic-gate 14147c478bd9Sstevel@tonic-gate utc = (year - 70); /* next 3 lines: utc = 365y + y/4 */ 14157c478bd9Sstevel@tonic-gate utc += (utc << 3) + (utc << 6); 14167c478bd9Sstevel@tonic-gate utc += (utc << 2) + ((year - 69) >> 2); 14177c478bd9Sstevel@tonic-gate utc += days_thru_month[month] + tod.tod_day - 1; 14187c478bd9Sstevel@tonic-gate utc = (utc << 3) + (utc << 4) + tod.tod_hour; /* 24 * day + hour */ 14197c478bd9Sstevel@tonic-gate utc = (utc << 6) - (utc << 2) + tod.tod_min; /* 60 * hour + min */ 14207c478bd9Sstevel@tonic-gate utc = (utc << 6) - (utc << 2) + tod.tod_sec; /* 60 * min + sec */ 14217c478bd9Sstevel@tonic-gate 14227c478bd9Sstevel@tonic-gate return (utc); 14237c478bd9Sstevel@tonic-gate } 1424