17c478bd9Sstevel@tonic-gate /* 2f635d46aSqiao * CDDL HEADER START 3f635d46aSqiao * 4f635d46aSqiao * The contents of this file are subject to the terms of the 5f635d46aSqiao * Common Development and Distribution License (the "License"). 6f635d46aSqiao * You may not use this file except in compliance with the License. 7f635d46aSqiao * 8f635d46aSqiao * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9f635d46aSqiao * or http://www.opensolaris.org/os/licensing. 10f635d46aSqiao * See the License for the specific language governing permissions 11f635d46aSqiao * and limitations under the License. 12f635d46aSqiao * 13f635d46aSqiao * When distributing Covered Code, include this CDDL HEADER in each 14f635d46aSqiao * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15f635d46aSqiao * If applicable, add the following below this CDDL HEADER, with the 16f635d46aSqiao * fields enclosed by brackets "[]" replaced with your own identifying 17f635d46aSqiao * information: Portions Copyright [yyyy] [name of copyright owner] 18f635d46aSqiao * 19f635d46aSqiao * CDDL HEADER END 20f635d46aSqiao */ 21e0cf54a5SRoger A. Faulkner 22f635d46aSqiao /* 23*8fc99e42STrevor Thompson * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * Copyright (c) 1982, 1986 Regents of the University of California. 297c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 307c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <sys/param.h> 347c478bd9Sstevel@tonic-gate #include <sys/user.h> 357c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 367c478bd9Sstevel@tonic-gate #include <sys/proc.h> 377c478bd9Sstevel@tonic-gate #include <sys/time.h> 387c478bd9Sstevel@tonic-gate #include <sys/systm.h> 397c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 407c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 417c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 427c478bd9Sstevel@tonic-gate #include <sys/timer.h> 437c478bd9Sstevel@tonic-gate #include <sys/debug.h> 447c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 457c478bd9Sstevel@tonic-gate #include <sys/cyclic.h> 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate static void realitexpire(void *); 487c478bd9Sstevel@tonic-gate static void realprofexpire(void *); 497c478bd9Sstevel@tonic-gate static void timeval_advance(struct timeval *, struct timeval *); 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate kmutex_t tod_lock; /* protects time-of-day stuff */ 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* 547c478bd9Sstevel@tonic-gate * Constant to define the minimum interval value of the ITIMER_REALPROF timer. 557c478bd9Sstevel@tonic-gate * Value is in microseconds; defaults to 500 usecs. Setting this value 567c478bd9Sstevel@tonic-gate * significantly lower may allow for denial-of-service attacks. 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate int itimer_realprof_minimum = 500; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * macro to compare a timeval to a timestruc 627c478bd9Sstevel@tonic-gate */ 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate #define TVTSCMP(tvp, tsp, cmp) \ 657c478bd9Sstevel@tonic-gate /* CSTYLED */ \ 667c478bd9Sstevel@tonic-gate ((tvp)->tv_sec cmp (tsp)->tv_sec || \ 677c478bd9Sstevel@tonic-gate ((tvp)->tv_sec == (tsp)->tv_sec && \ 687c478bd9Sstevel@tonic-gate /* CSTYLED */ \ 697c478bd9Sstevel@tonic-gate (tvp)->tv_usec * 1000 cmp (tsp)->tv_nsec)) 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* 727c478bd9Sstevel@tonic-gate * Time of day and interval timer support. 737c478bd9Sstevel@tonic-gate * 747c478bd9Sstevel@tonic-gate * These routines provide the kernel entry points to get and set 757c478bd9Sstevel@tonic-gate * the time-of-day and per-process interval timers. Subroutines 767c478bd9Sstevel@tonic-gate * here provide support for adding and subtracting timeval structures 777c478bd9Sstevel@tonic-gate * and decrementing interval timers, optionally reloading the interval 787c478bd9Sstevel@tonic-gate * timers when they expire. 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* 827c478bd9Sstevel@tonic-gate * SunOS function to generate monotonically increasing time values. 837c478bd9Sstevel@tonic-gate */ 847c478bd9Sstevel@tonic-gate void 857c478bd9Sstevel@tonic-gate uniqtime(struct timeval *tv) 867c478bd9Sstevel@tonic-gate { 877c478bd9Sstevel@tonic-gate static struct timeval last; 889226afcbSBirva Shah static int last_timechanged; 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 /* 1189226afcbSBirva Shah * If the system hres time has been changed since the last time 1199226afcbSBirva Shah * we are called. then all bets are off; just update our 1209226afcbSBirva Shah * local copy of timechanged and accept the reported time as is. 1219226afcbSBirva Shah */ 1229226afcbSBirva Shah if (last_timechanged != timechanged) { 1239226afcbSBirva Shah last_timechanged = timechanged; 1249226afcbSBirva Shah } 1259226afcbSBirva Shah /* 1267c478bd9Sstevel@tonic-gate * Try to keep timestamps unique, but don't be obsessive about 1277c478bd9Sstevel@tonic-gate * it in the face of large differences. 1287c478bd9Sstevel@tonic-gate */ 1299226afcbSBirva Shah else if ((sec <= last.tv_sec) && /* same or lower seconds, and */ 1307c478bd9Sstevel@tonic-gate ((sec != last.tv_sec) || /* either different second or */ 1317c478bd9Sstevel@tonic-gate (usec <= last.tv_usec)) && /* lower microsecond, and */ 1327c478bd9Sstevel@tonic-gate ((last.tv_sec - sec) <= 5)) { /* not way back in time */ 1337c478bd9Sstevel@tonic-gate sec = last.tv_sec; 1347c478bd9Sstevel@tonic-gate usec = last.tv_usec + 1; 1357c478bd9Sstevel@tonic-gate if (usec >= MICROSEC) { 1367c478bd9Sstevel@tonic-gate usec -= MICROSEC; 1377c478bd9Sstevel@tonic-gate sec++; 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate last.tv_sec = sec; 1417c478bd9Sstevel@tonic-gate last.tv_usec = usec; 1427c478bd9Sstevel@tonic-gate mutex_exit(&tod_lock); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate tv->tv_sec = sec; 1457c478bd9Sstevel@tonic-gate tv->tv_usec = usec; 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* 1497c478bd9Sstevel@tonic-gate * Timestamps are exported from the kernel in several places. 1507c478bd9Sstevel@tonic-gate * Such timestamps are commonly used for either uniqueness or for 1517c478bd9Sstevel@tonic-gate * sequencing - truncation to 32-bits is fine for uniqueness, 1527c478bd9Sstevel@tonic-gate * but sequencing is going to take more work as we get closer to 2038! 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate void 1557c478bd9Sstevel@tonic-gate uniqtime32(struct timeval32 *tv32p) 1567c478bd9Sstevel@tonic-gate { 1577c478bd9Sstevel@tonic-gate struct timeval tv; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate uniqtime(&tv); 1607c478bd9Sstevel@tonic-gate TIMEVAL_TO_TIMEVAL32(tv32p, &tv); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate int 1647c478bd9Sstevel@tonic-gate gettimeofday(struct timeval *tp) 1657c478bd9Sstevel@tonic-gate { 1667c478bd9Sstevel@tonic-gate struct timeval atv; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (tp) { 1697c478bd9Sstevel@tonic-gate uniqtime(&atv); 1707c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 1717c478bd9Sstevel@tonic-gate if (copyout(&atv, tp, sizeof (atv))) 1727c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1737c478bd9Sstevel@tonic-gate } else { 1747c478bd9Sstevel@tonic-gate struct timeval32 tv32; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate if (TIMEVAL_OVERFLOW(&atv)) 1777c478bd9Sstevel@tonic-gate return (set_errno(EOVERFLOW)); 1787c478bd9Sstevel@tonic-gate TIMEVAL_TO_TIMEVAL32(&tv32, &atv); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate if (copyout(&tv32, tp, sizeof (tv32))) 1817c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate return (0); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate int 1887c478bd9Sstevel@tonic-gate getitimer(uint_t which, struct itimerval *itv) 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate int error; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) 1937c478bd9Sstevel@tonic-gate error = xgetitimer(which, itv, 0); 1947c478bd9Sstevel@tonic-gate else { 1957c478bd9Sstevel@tonic-gate struct itimerval kitv; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate if ((error = xgetitimer(which, &kitv, 1)) == 0) { 1987c478bd9Sstevel@tonic-gate if (ITIMERVAL_OVERFLOW(&kitv)) { 1997c478bd9Sstevel@tonic-gate error = EOVERFLOW; 2007c478bd9Sstevel@tonic-gate } else { 2017c478bd9Sstevel@tonic-gate struct itimerval32 itv32; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate ITIMERVAL_TO_ITIMERVAL32(&itv32, &kitv); 2047c478bd9Sstevel@tonic-gate if (copyout(&itv32, itv, sizeof (itv32)) != 0) 2057c478bd9Sstevel@tonic-gate error = EFAULT; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate return (error ? (set_errno(error)) : 0); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate int 2147c478bd9Sstevel@tonic-gate xgetitimer(uint_t which, struct itimerval *itv, int iskaddr) 2157c478bd9Sstevel@tonic-gate { 2167c478bd9Sstevel@tonic-gate struct proc *p = curproc; 2177c478bd9Sstevel@tonic-gate struct timeval now; 2187c478bd9Sstevel@tonic-gate struct itimerval aitv; 2197c478bd9Sstevel@tonic-gate hrtime_t ts, first, interval, remain; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate switch (which) { 2247c478bd9Sstevel@tonic-gate case ITIMER_VIRTUAL: 2257c478bd9Sstevel@tonic-gate case ITIMER_PROF: 2267c478bd9Sstevel@tonic-gate aitv = ttolwp(curthread)->lwp_timer[which]; 2277c478bd9Sstevel@tonic-gate break; 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate case ITIMER_REAL: 2307c478bd9Sstevel@tonic-gate uniqtime(&now); 2317c478bd9Sstevel@tonic-gate aitv = p->p_realitimer; 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate if (timerisset(&aitv.it_value)) { 2347c478bd9Sstevel@tonic-gate /*CSTYLED*/ 2357c478bd9Sstevel@tonic-gate if (timercmp(&aitv.it_value, &now, <)) { 2367c478bd9Sstevel@tonic-gate timerclear(&aitv.it_value); 2377c478bd9Sstevel@tonic-gate } else { 2387c478bd9Sstevel@tonic-gate timevalsub(&aitv.it_value, &now); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate break; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate case ITIMER_REALPROF: 2447c478bd9Sstevel@tonic-gate if (curproc->p_rprof_cyclic == CYCLIC_NONE) { 2457c478bd9Sstevel@tonic-gate bzero(&aitv, sizeof (aitv)); 2467c478bd9Sstevel@tonic-gate break; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate aitv = curproc->p_rprof_timer; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate first = tv2hrt(&aitv.it_value); 2527c478bd9Sstevel@tonic-gate interval = tv2hrt(&aitv.it_interval); 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate if ((ts = gethrtime()) < first) { 2557c478bd9Sstevel@tonic-gate /* 2567c478bd9Sstevel@tonic-gate * We haven't gone off for the first time; the time 2577c478bd9Sstevel@tonic-gate * remaining is simply the first time we will go 2587c478bd9Sstevel@tonic-gate * off minus the current time. 2597c478bd9Sstevel@tonic-gate */ 2607c478bd9Sstevel@tonic-gate remain = first - ts; 2617c478bd9Sstevel@tonic-gate } else { 2627c478bd9Sstevel@tonic-gate if (interval == 0) { 2637c478bd9Sstevel@tonic-gate /* 2647c478bd9Sstevel@tonic-gate * This was set as a one-shot, and we've 2657c478bd9Sstevel@tonic-gate * already gone off; there is no time 2667c478bd9Sstevel@tonic-gate * remaining. 2677c478bd9Sstevel@tonic-gate */ 2687c478bd9Sstevel@tonic-gate remain = 0; 2697c478bd9Sstevel@tonic-gate } else { 2707c478bd9Sstevel@tonic-gate /* 2717c478bd9Sstevel@tonic-gate * We have a non-zero interval; we need to 2727c478bd9Sstevel@tonic-gate * determine how far we are into the current 2737c478bd9Sstevel@tonic-gate * interval, and subtract that from the 2747c478bd9Sstevel@tonic-gate * interval to determine the time remaining. 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate remain = interval - ((ts - first) % interval); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate hrt2tv(remain, &aitv.it_value); 2817c478bd9Sstevel@tonic-gate break; 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate default: 2847c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 2857c478bd9Sstevel@tonic-gate return (EINVAL); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate if (iskaddr) { 2917c478bd9Sstevel@tonic-gate bcopy(&aitv, itv, sizeof (*itv)); 2927c478bd9Sstevel@tonic-gate } else { 2937c478bd9Sstevel@tonic-gate ASSERT(get_udatamodel() == DATAMODEL_NATIVE); 2947c478bd9Sstevel@tonic-gate if (copyout(&aitv, itv, sizeof (*itv))) 2957c478bd9Sstevel@tonic-gate return (EFAULT); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate return (0); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate int 3037c478bd9Sstevel@tonic-gate setitimer(uint_t which, struct itimerval *itv, struct itimerval *oitv) 3047c478bd9Sstevel@tonic-gate { 3057c478bd9Sstevel@tonic-gate int error; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate if (oitv != NULL) 3087c478bd9Sstevel@tonic-gate if ((error = getitimer(which, oitv)) != 0) 3097c478bd9Sstevel@tonic-gate return (error); 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate if (itv == NULL) 3127c478bd9Sstevel@tonic-gate return (0); 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) 3157c478bd9Sstevel@tonic-gate error = xsetitimer(which, itv, 0); 3167c478bd9Sstevel@tonic-gate else { 3177c478bd9Sstevel@tonic-gate struct itimerval32 itv32; 3187c478bd9Sstevel@tonic-gate struct itimerval kitv; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate if (copyin(itv, &itv32, sizeof (itv32))) 3217c478bd9Sstevel@tonic-gate error = EFAULT; 3227c478bd9Sstevel@tonic-gate ITIMERVAL32_TO_ITIMERVAL(&kitv, &itv32); 3237c478bd9Sstevel@tonic-gate error = xsetitimer(which, &kitv, 1); 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate return (error ? (set_errno(error)) : 0); 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate int 3307c478bd9Sstevel@tonic-gate xsetitimer(uint_t which, struct itimerval *itv, int iskaddr) 3317c478bd9Sstevel@tonic-gate { 3327c478bd9Sstevel@tonic-gate struct itimerval aitv; 3337c478bd9Sstevel@tonic-gate struct timeval now; 3347c478bd9Sstevel@tonic-gate struct proc *p = curproc; 3357c478bd9Sstevel@tonic-gate kthread_t *t; 3367c478bd9Sstevel@tonic-gate timeout_id_t tmp_id; 3377c478bd9Sstevel@tonic-gate cyc_handler_t hdlr; 3387c478bd9Sstevel@tonic-gate cyc_time_t when; 3397c478bd9Sstevel@tonic-gate cyclic_id_t cyclic; 3407c478bd9Sstevel@tonic-gate hrtime_t ts; 3417c478bd9Sstevel@tonic-gate int min; 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate if (itv == NULL) 3447c478bd9Sstevel@tonic-gate return (0); 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate if (iskaddr) { 3477c478bd9Sstevel@tonic-gate bcopy(itv, &aitv, sizeof (aitv)); 3487c478bd9Sstevel@tonic-gate } else { 3497c478bd9Sstevel@tonic-gate ASSERT(get_udatamodel() == DATAMODEL_NATIVE); 3507c478bd9Sstevel@tonic-gate if (copyin(itv, &aitv, sizeof (aitv))) 3517c478bd9Sstevel@tonic-gate return (EFAULT); 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate if (which == ITIMER_REALPROF) { 3557c478bd9Sstevel@tonic-gate min = MAX((int)(cyclic_getres() / (NANOSEC / MICROSEC)), 3567c478bd9Sstevel@tonic-gate itimer_realprof_minimum); 3577c478bd9Sstevel@tonic-gate } else { 3587c478bd9Sstevel@tonic-gate min = usec_per_tick; 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate if (itimerfix(&aitv.it_value, min) || 3627c478bd9Sstevel@tonic-gate (itimerfix(&aitv.it_interval, min) && timerisset(&aitv.it_value))) 3637c478bd9Sstevel@tonic-gate return (EINVAL); 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 3667c478bd9Sstevel@tonic-gate switch (which) { 3677c478bd9Sstevel@tonic-gate case ITIMER_REAL: 3687c478bd9Sstevel@tonic-gate /* 3697c478bd9Sstevel@tonic-gate * The SITBUSY flag prevents conflicts with multiple 3707c478bd9Sstevel@tonic-gate * threads attempting to perform setitimer(ITIMER_REAL) 3717c478bd9Sstevel@tonic-gate * at the same time, even when we drop p->p_lock below. 3727c478bd9Sstevel@tonic-gate * Any blocked thread returns successfully because the 3737c478bd9Sstevel@tonic-gate * effect is the same as if it got here first, finished, 3747c478bd9Sstevel@tonic-gate * and the other thread then came through and destroyed 3757c478bd9Sstevel@tonic-gate * what it did. We are just protecting the system from 3767c478bd9Sstevel@tonic-gate * malfunctioning due to the race condition. 3777c478bd9Sstevel@tonic-gate */ 3787c478bd9Sstevel@tonic-gate if (p->p_flag & SITBUSY) { 3797c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 3807c478bd9Sstevel@tonic-gate return (0); 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate p->p_flag |= SITBUSY; 3837c478bd9Sstevel@tonic-gate while ((tmp_id = p->p_itimerid) != 0) { 3847c478bd9Sstevel@tonic-gate /* 3857c478bd9Sstevel@tonic-gate * Avoid deadlock in callout_delete (called from 3867c478bd9Sstevel@tonic-gate * untimeout) which may go to sleep (while holding 3877c478bd9Sstevel@tonic-gate * p_lock). Drop p_lock and re-acquire it after 3887c478bd9Sstevel@tonic-gate * untimeout returns. Need to clear p_itimerid 3897c478bd9Sstevel@tonic-gate * while holding p_lock. 3907c478bd9Sstevel@tonic-gate */ 3917c478bd9Sstevel@tonic-gate p->p_itimerid = 0; 3927c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 3937c478bd9Sstevel@tonic-gate (void) untimeout(tmp_id); 3947c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate if (timerisset(&aitv.it_value)) { 3977c478bd9Sstevel@tonic-gate uniqtime(&now); 3987c478bd9Sstevel@tonic-gate timevaladd(&aitv.it_value, &now); 3997c478bd9Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, 4007c478bd9Sstevel@tonic-gate p, hzto(&aitv.it_value)); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate p->p_realitimer = aitv; 4037c478bd9Sstevel@tonic-gate p->p_flag &= ~SITBUSY; 4047c478bd9Sstevel@tonic-gate break; 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate case ITIMER_REALPROF: 4077c478bd9Sstevel@tonic-gate cyclic = p->p_rprof_cyclic; 4087c478bd9Sstevel@tonic-gate p->p_rprof_cyclic = CYCLIC_NONE; 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate /* 4137c478bd9Sstevel@tonic-gate * We're now going to acquire cpu_lock, remove the old cyclic 4147c478bd9Sstevel@tonic-gate * if necessary, and add our new cyclic. 4157c478bd9Sstevel@tonic-gate */ 4167c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate if (cyclic != CYCLIC_NONE) 4197c478bd9Sstevel@tonic-gate cyclic_remove(cyclic); 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate if (!timerisset(&aitv.it_value)) { 4227c478bd9Sstevel@tonic-gate /* 4237c478bd9Sstevel@tonic-gate * If we were passed a value of 0, we're done. 4247c478bd9Sstevel@tonic-gate */ 4257c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 4267c478bd9Sstevel@tonic-gate return (0); 4277c478bd9Sstevel@tonic-gate } 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate hdlr.cyh_func = realprofexpire; 4307c478bd9Sstevel@tonic-gate hdlr.cyh_arg = p; 4317c478bd9Sstevel@tonic-gate hdlr.cyh_level = CY_LOW_LEVEL; 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate when.cyt_when = (ts = gethrtime() + tv2hrt(&aitv.it_value)); 4347c478bd9Sstevel@tonic-gate when.cyt_interval = tv2hrt(&aitv.it_interval); 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate if (when.cyt_interval == 0) { 4377c478bd9Sstevel@tonic-gate /* 4387c478bd9Sstevel@tonic-gate * Using the same logic as for CLOCK_HIGHRES timers, we 4397c478bd9Sstevel@tonic-gate * set the interval to be INT64_MAX - when.cyt_when to 4407c478bd9Sstevel@tonic-gate * effect a one-shot; see the comment in clock_highres.c 4417c478bd9Sstevel@tonic-gate * for more details on why this works. 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate when.cyt_interval = INT64_MAX - when.cyt_when; 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate cyclic = cyclic_add(&hdlr, &when); 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate /* 4517c478bd9Sstevel@tonic-gate * We have now successfully added the cyclic. Reacquire 4527c478bd9Sstevel@tonic-gate * p_lock, and see if anyone has snuck in. 4537c478bd9Sstevel@tonic-gate */ 4547c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate if (p->p_rprof_cyclic != CYCLIC_NONE) { 4577c478bd9Sstevel@tonic-gate /* 4587c478bd9Sstevel@tonic-gate * We're racing with another thread establishing an 4597c478bd9Sstevel@tonic-gate * ITIMER_REALPROF interval timer. We'll let the other 4607c478bd9Sstevel@tonic-gate * thread win (this is a race at the application level, 4617c478bd9Sstevel@tonic-gate * so letting the other thread win is acceptable). 4627c478bd9Sstevel@tonic-gate */ 4637c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 4647c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 4657c478bd9Sstevel@tonic-gate cyclic_remove(cyclic); 4667c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate return (0); 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate /* 4727c478bd9Sstevel@tonic-gate * Success. Set our tracking variables in the proc structure, 4737c478bd9Sstevel@tonic-gate * cancel any outstanding ITIMER_PROF, and allocate the 4747c478bd9Sstevel@tonic-gate * per-thread SIGPROF buffers, if possible. 4757c478bd9Sstevel@tonic-gate */ 4767c478bd9Sstevel@tonic-gate hrt2tv(ts, &aitv.it_value); 4777c478bd9Sstevel@tonic-gate p->p_rprof_timer = aitv; 4787c478bd9Sstevel@tonic-gate p->p_rprof_cyclic = cyclic; 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate t = p->p_tlist; 4817c478bd9Sstevel@tonic-gate do { 4827c478bd9Sstevel@tonic-gate struct itimerval *itvp; 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate itvp = &ttolwp(t)->lwp_timer[ITIMER_PROF]; 4857c478bd9Sstevel@tonic-gate timerclear(&itvp->it_interval); 4867c478bd9Sstevel@tonic-gate timerclear(&itvp->it_value); 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate if (t->t_rprof != NULL) 4897c478bd9Sstevel@tonic-gate continue; 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate t->t_rprof = 4927c478bd9Sstevel@tonic-gate kmem_zalloc(sizeof (struct rprof), KM_NOSLEEP); 4937c478bd9Sstevel@tonic-gate aston(t); 4947c478bd9Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate break; 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate case ITIMER_VIRTUAL: 4997c478bd9Sstevel@tonic-gate ttolwp(curthread)->lwp_timer[ITIMER_VIRTUAL] = aitv; 5007c478bd9Sstevel@tonic-gate break; 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate case ITIMER_PROF: 5037c478bd9Sstevel@tonic-gate if (p->p_rprof_cyclic != CYCLIC_NONE) { 5047c478bd9Sstevel@tonic-gate /* 5057c478bd9Sstevel@tonic-gate * Silently ignore ITIMER_PROF if ITIMER_REALPROF 5067c478bd9Sstevel@tonic-gate * is in effect. 5077c478bd9Sstevel@tonic-gate */ 5087c478bd9Sstevel@tonic-gate break; 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate ttolwp(curthread)->lwp_timer[ITIMER_PROF] = aitv; 5127c478bd9Sstevel@tonic-gate break; 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate default: 5157c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5167c478bd9Sstevel@tonic-gate return (EINVAL); 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5197c478bd9Sstevel@tonic-gate return (0); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate /* 523e0cf54a5SRoger A. Faulkner * Delete the ITIMER_REALPROF interval timer. 524e0cf54a5SRoger A. Faulkner * Called only from exec_args() when exec occurs. 525e0cf54a5SRoger A. Faulkner * The other ITIMER_* interval timers are specified 526e0cf54a5SRoger A. Faulkner * to be inherited across exec(), so leave them alone. 527e0cf54a5SRoger A. Faulkner */ 528e0cf54a5SRoger A. Faulkner void 529e0cf54a5SRoger A. Faulkner delete_itimer_realprof(void) 530e0cf54a5SRoger A. Faulkner { 531e0cf54a5SRoger A. Faulkner kthread_t *t = curthread; 532e0cf54a5SRoger A. Faulkner struct proc *p = ttoproc(t); 533e0cf54a5SRoger A. Faulkner klwp_t *lwp = ttolwp(t); 534e0cf54a5SRoger A. Faulkner cyclic_id_t cyclic; 535e0cf54a5SRoger A. Faulkner 536e0cf54a5SRoger A. Faulkner mutex_enter(&p->p_lock); 537e0cf54a5SRoger A. Faulkner 538e0cf54a5SRoger A. Faulkner /* we are performing execve(); assert we are single-threaded */ 539e0cf54a5SRoger A. Faulkner ASSERT(t == p->p_tlist && t == t->t_forw); 540e0cf54a5SRoger A. Faulkner 541e0cf54a5SRoger A. Faulkner if ((cyclic = p->p_rprof_cyclic) == CYCLIC_NONE) { 542e0cf54a5SRoger A. Faulkner mutex_exit(&p->p_lock); 543e0cf54a5SRoger A. Faulkner } else { 544e0cf54a5SRoger A. Faulkner p->p_rprof_cyclic = CYCLIC_NONE; 545e0cf54a5SRoger A. Faulkner /* 546e0cf54a5SRoger A. Faulkner * Delete any current instance of SIGPROF. 547e0cf54a5SRoger A. Faulkner */ 548e0cf54a5SRoger A. Faulkner if (lwp->lwp_cursig == SIGPROF) { 549e0cf54a5SRoger A. Faulkner lwp->lwp_cursig = 0; 550e0cf54a5SRoger A. Faulkner lwp->lwp_extsig = 0; 551e0cf54a5SRoger A. Faulkner if (lwp->lwp_curinfo) { 552e0cf54a5SRoger A. Faulkner siginfofree(lwp->lwp_curinfo); 553e0cf54a5SRoger A. Faulkner lwp->lwp_curinfo = NULL; 554e0cf54a5SRoger A. Faulkner } 555e0cf54a5SRoger A. Faulkner } 556e0cf54a5SRoger A. Faulkner /* 557e0cf54a5SRoger A. Faulkner * Delete any pending instances of SIGPROF. 558e0cf54a5SRoger A. Faulkner */ 559e0cf54a5SRoger A. Faulkner sigdelset(&p->p_sig, SIGPROF); 560e0cf54a5SRoger A. Faulkner sigdelset(&p->p_extsig, SIGPROF); 561e0cf54a5SRoger A. Faulkner sigdelq(p, NULL, SIGPROF); 562e0cf54a5SRoger A. Faulkner sigdelset(&t->t_sig, SIGPROF); 563e0cf54a5SRoger A. Faulkner sigdelset(&t->t_extsig, SIGPROF); 564e0cf54a5SRoger A. Faulkner sigdelq(p, t, SIGPROF); 565e0cf54a5SRoger A. Faulkner 566e0cf54a5SRoger A. Faulkner mutex_exit(&p->p_lock); 567e0cf54a5SRoger A. Faulkner 568e0cf54a5SRoger A. Faulkner /* 569e0cf54a5SRoger A. Faulkner * Remove the ITIMER_REALPROF cyclic. 570e0cf54a5SRoger A. Faulkner */ 571e0cf54a5SRoger A. Faulkner mutex_enter(&cpu_lock); 572e0cf54a5SRoger A. Faulkner cyclic_remove(cyclic); 573e0cf54a5SRoger A. Faulkner mutex_exit(&cpu_lock); 574e0cf54a5SRoger A. Faulkner } 575e0cf54a5SRoger A. Faulkner } 576e0cf54a5SRoger A. Faulkner 577e0cf54a5SRoger A. Faulkner /* 5787c478bd9Sstevel@tonic-gate * Real interval timer expired: 5797c478bd9Sstevel@tonic-gate * send process whose timer expired an alarm signal. 5807c478bd9Sstevel@tonic-gate * If time is not set up to reload, then just return. 5817c478bd9Sstevel@tonic-gate * Else compute next time timer should go off which is > current time. 5827c478bd9Sstevel@tonic-gate * This is where delay in processing this timeout causes multiple 5837c478bd9Sstevel@tonic-gate * SIGALRM calls to be compressed into one. 5847c478bd9Sstevel@tonic-gate */ 5857c478bd9Sstevel@tonic-gate static void 5867c478bd9Sstevel@tonic-gate realitexpire(void *arg) 5877c478bd9Sstevel@tonic-gate { 5887c478bd9Sstevel@tonic-gate struct proc *p = arg; 5897c478bd9Sstevel@tonic-gate struct timeval *valp = &p->p_realitimer.it_value; 5907c478bd9Sstevel@tonic-gate struct timeval *intervalp = &p->p_realitimer.it_interval; 5917c478bd9Sstevel@tonic-gate #if !defined(_LP64) 5927c478bd9Sstevel@tonic-gate clock_t ticks; 5937c478bd9Sstevel@tonic-gate #endif 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 5967c478bd9Sstevel@tonic-gate #if !defined(_LP64) 5977c478bd9Sstevel@tonic-gate if ((ticks = hzto(valp)) > 1) { 5987c478bd9Sstevel@tonic-gate /* 5997c478bd9Sstevel@tonic-gate * If we are executing before we were meant to, it must be 6007c478bd9Sstevel@tonic-gate * because of an overflow in a prior hzto() calculation. 6017c478bd9Sstevel@tonic-gate * In this case, we want to go to sleep for the recalculated 6027c478bd9Sstevel@tonic-gate * number of ticks. For the special meaning of the value "1" 6037c478bd9Sstevel@tonic-gate * see comment in timespectohz(). 6047c478bd9Sstevel@tonic-gate */ 6057c478bd9Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, p, ticks); 6067c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 6077c478bd9Sstevel@tonic-gate return; 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate #endif 6107c478bd9Sstevel@tonic-gate sigtoproc(p, NULL, SIGALRM); 6117c478bd9Sstevel@tonic-gate if (!timerisset(intervalp)) { 6127c478bd9Sstevel@tonic-gate timerclear(valp); 6137c478bd9Sstevel@tonic-gate p->p_itimerid = 0; 6147c478bd9Sstevel@tonic-gate } else { 6157c478bd9Sstevel@tonic-gate /* advance timer value past current time */ 6167c478bd9Sstevel@tonic-gate timeval_advance(valp, intervalp); 6177c478bd9Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, p, hzto(valp)); 6187c478bd9Sstevel@tonic-gate } 6197c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate /* 6237c478bd9Sstevel@tonic-gate * Real time profiling interval timer expired: 6247c478bd9Sstevel@tonic-gate * Increment microstate counters for each lwp in the process 6257c478bd9Sstevel@tonic-gate * and ensure that running lwps are kicked into the kernel. 6267c478bd9Sstevel@tonic-gate * If time is not set up to reload, then just return. 6277c478bd9Sstevel@tonic-gate * Else compute next time timer should go off which is > current time, 6287c478bd9Sstevel@tonic-gate * as above. 6297c478bd9Sstevel@tonic-gate */ 6307c478bd9Sstevel@tonic-gate static void 6317c478bd9Sstevel@tonic-gate realprofexpire(void *arg) 6327c478bd9Sstevel@tonic-gate { 6337c478bd9Sstevel@tonic-gate struct proc *p = arg; 6347c478bd9Sstevel@tonic-gate kthread_t *t; 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 637e0cf54a5SRoger A. Faulkner if (p->p_rprof_cyclic == CYCLIC_NONE || 638e0cf54a5SRoger A. Faulkner (t = p->p_tlist) == NULL) { 6397c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 6407c478bd9Sstevel@tonic-gate return; 6417c478bd9Sstevel@tonic-gate } 6427c478bd9Sstevel@tonic-gate do { 6437c478bd9Sstevel@tonic-gate int mstate; 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate /* 6467c478bd9Sstevel@tonic-gate * Attempt to allocate the SIGPROF buffer, but don't sleep. 6477c478bd9Sstevel@tonic-gate */ 6487c478bd9Sstevel@tonic-gate if (t->t_rprof == NULL) 6497c478bd9Sstevel@tonic-gate t->t_rprof = kmem_zalloc(sizeof (struct rprof), 6507c478bd9Sstevel@tonic-gate KM_NOSLEEP); 6517c478bd9Sstevel@tonic-gate if (t->t_rprof == NULL) 6527c478bd9Sstevel@tonic-gate continue; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate thread_lock(t); 6557c478bd9Sstevel@tonic-gate switch (t->t_state) { 6567c478bd9Sstevel@tonic-gate case TS_SLEEP: 6577c478bd9Sstevel@tonic-gate /* 6587c478bd9Sstevel@tonic-gate * Don't touch the lwp is it is swapped out. 6597c478bd9Sstevel@tonic-gate */ 6607c478bd9Sstevel@tonic-gate if (!(t->t_schedflag & TS_LOAD)) { 6617c478bd9Sstevel@tonic-gate mstate = LMS_SLEEP; 6627c478bd9Sstevel@tonic-gate break; 6637c478bd9Sstevel@tonic-gate } 6647c478bd9Sstevel@tonic-gate switch (mstate = ttolwp(t)->lwp_mstate.ms_prev) { 6657c478bd9Sstevel@tonic-gate case LMS_TFAULT: 6667c478bd9Sstevel@tonic-gate case LMS_DFAULT: 6677c478bd9Sstevel@tonic-gate case LMS_KFAULT: 6687c478bd9Sstevel@tonic-gate case LMS_USER_LOCK: 6697c478bd9Sstevel@tonic-gate break; 6707c478bd9Sstevel@tonic-gate default: 6717c478bd9Sstevel@tonic-gate mstate = LMS_SLEEP; 6727c478bd9Sstevel@tonic-gate break; 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate break; 6757c478bd9Sstevel@tonic-gate case TS_RUN: 676c97ad5cdSakolb case TS_WAIT: 6777c478bd9Sstevel@tonic-gate mstate = LMS_WAIT_CPU; 6787c478bd9Sstevel@tonic-gate break; 6797c478bd9Sstevel@tonic-gate case TS_ONPROC: 6807c478bd9Sstevel@tonic-gate switch (mstate = t->t_mstate) { 6817c478bd9Sstevel@tonic-gate case LMS_USER: 6827c478bd9Sstevel@tonic-gate case LMS_SYSTEM: 6837c478bd9Sstevel@tonic-gate case LMS_TRAP: 6847c478bd9Sstevel@tonic-gate break; 6857c478bd9Sstevel@tonic-gate default: 6867c478bd9Sstevel@tonic-gate mstate = LMS_SYSTEM; 6877c478bd9Sstevel@tonic-gate break; 6887c478bd9Sstevel@tonic-gate } 6897c478bd9Sstevel@tonic-gate break; 6907c478bd9Sstevel@tonic-gate default: 6917c478bd9Sstevel@tonic-gate mstate = t->t_mstate; 6927c478bd9Sstevel@tonic-gate break; 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate t->t_rprof->rp_anystate = 1; 6957c478bd9Sstevel@tonic-gate t->t_rprof->rp_state[mstate]++; 6967c478bd9Sstevel@tonic-gate aston(t); 6977c478bd9Sstevel@tonic-gate /* 6987c478bd9Sstevel@tonic-gate * force the thread into the kernel 6997c478bd9Sstevel@tonic-gate * if it is not already there. 7007c478bd9Sstevel@tonic-gate */ 7017c478bd9Sstevel@tonic-gate if (t->t_state == TS_ONPROC && t->t_cpu != CPU) 7027c478bd9Sstevel@tonic-gate poke_cpu(t->t_cpu->cpu_id); 7037c478bd9Sstevel@tonic-gate thread_unlock(t); 7047c478bd9Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate /* 7107c478bd9Sstevel@tonic-gate * Advances timer value past the current time of day. See the detailed 7117c478bd9Sstevel@tonic-gate * comment for this logic in realitsexpire(), above. 7127c478bd9Sstevel@tonic-gate */ 7137c478bd9Sstevel@tonic-gate static void 7147c478bd9Sstevel@tonic-gate timeval_advance(struct timeval *valp, struct timeval *intervalp) 7157c478bd9Sstevel@tonic-gate { 7167c478bd9Sstevel@tonic-gate int cnt2nth; 7177c478bd9Sstevel@tonic-gate struct timeval interval2nth; 7187c478bd9Sstevel@tonic-gate 7197c478bd9Sstevel@tonic-gate for (;;) { 7207c478bd9Sstevel@tonic-gate interval2nth = *intervalp; 7217c478bd9Sstevel@tonic-gate for (cnt2nth = 0; ; cnt2nth++) { 7227c478bd9Sstevel@tonic-gate timevaladd(valp, &interval2nth); 7237c478bd9Sstevel@tonic-gate /*CSTYLED*/ 7247c478bd9Sstevel@tonic-gate if (TVTSCMP(valp, &hrestime, >)) 7257c478bd9Sstevel@tonic-gate break; 7267c478bd9Sstevel@tonic-gate timevaladd(&interval2nth, &interval2nth); 7277c478bd9Sstevel@tonic-gate } 7287c478bd9Sstevel@tonic-gate if (cnt2nth == 0) 7297c478bd9Sstevel@tonic-gate break; 7307c478bd9Sstevel@tonic-gate timevalsub(valp, &interval2nth); 7317c478bd9Sstevel@tonic-gate } 7327c478bd9Sstevel@tonic-gate } 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate /* 7357c478bd9Sstevel@tonic-gate * Check that a proposed value to load into the .it_value or .it_interval 7367c478bd9Sstevel@tonic-gate * part of an interval timer is acceptable, and set it to at least a 7377c478bd9Sstevel@tonic-gate * specified minimal value. 7387c478bd9Sstevel@tonic-gate */ 7397c478bd9Sstevel@tonic-gate int 7407c478bd9Sstevel@tonic-gate itimerfix(struct timeval *tv, int minimum) 7417c478bd9Sstevel@tonic-gate { 7427c478bd9Sstevel@tonic-gate if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || 7437c478bd9Sstevel@tonic-gate tv->tv_usec < 0 || tv->tv_usec >= MICROSEC) 7447c478bd9Sstevel@tonic-gate return (EINVAL); 7457c478bd9Sstevel@tonic-gate if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < minimum) 7467c478bd9Sstevel@tonic-gate tv->tv_usec = minimum; 7477c478bd9Sstevel@tonic-gate return (0); 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate 7507c478bd9Sstevel@tonic-gate /* 7517c478bd9Sstevel@tonic-gate * Same as itimerfix, except a) it takes a timespec instead of a timeval and 7527c478bd9Sstevel@tonic-gate * b) it doesn't truncate based on timeout granularity; consumers of this 7537c478bd9Sstevel@tonic-gate * interface (e.g. timer_settime()) depend on the passed timespec not being 7547c478bd9Sstevel@tonic-gate * modified implicitly. 7557c478bd9Sstevel@tonic-gate */ 7567c478bd9Sstevel@tonic-gate int 7577c478bd9Sstevel@tonic-gate itimerspecfix(timespec_t *tv) 7587c478bd9Sstevel@tonic-gate { 7597c478bd9Sstevel@tonic-gate if (tv->tv_sec < 0 || tv->tv_nsec < 0 || tv->tv_nsec >= NANOSEC) 7607c478bd9Sstevel@tonic-gate return (EINVAL); 7617c478bd9Sstevel@tonic-gate return (0); 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate 7647c478bd9Sstevel@tonic-gate /* 7657c478bd9Sstevel@tonic-gate * Decrement an interval timer by a specified number 7667c478bd9Sstevel@tonic-gate * of microseconds, which must be less than a second, 7677c478bd9Sstevel@tonic-gate * i.e. < 1000000. If the timer expires, then reload 7687c478bd9Sstevel@tonic-gate * it. In this case, carry over (usec - old value) to 7697c478bd9Sstevel@tonic-gate * reducint the value reloaded into the timer so that 7707c478bd9Sstevel@tonic-gate * the timer does not drift. This routine assumes 7717c478bd9Sstevel@tonic-gate * that it is called in a context where the timers 7727c478bd9Sstevel@tonic-gate * on which it is operating cannot change in value. 7737c478bd9Sstevel@tonic-gate */ 7747c478bd9Sstevel@tonic-gate int 7757c478bd9Sstevel@tonic-gate itimerdecr(struct itimerval *itp, int usec) 7767c478bd9Sstevel@tonic-gate { 7777c478bd9Sstevel@tonic-gate if (itp->it_value.tv_usec < usec) { 7787c478bd9Sstevel@tonic-gate if (itp->it_value.tv_sec == 0) { 7797c478bd9Sstevel@tonic-gate /* expired, and already in next interval */ 7807c478bd9Sstevel@tonic-gate usec -= itp->it_value.tv_usec; 7817c478bd9Sstevel@tonic-gate goto expire; 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate itp->it_value.tv_usec += MICROSEC; 7847c478bd9Sstevel@tonic-gate itp->it_value.tv_sec--; 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate itp->it_value.tv_usec -= usec; 7877c478bd9Sstevel@tonic-gate usec = 0; 7887c478bd9Sstevel@tonic-gate if (timerisset(&itp->it_value)) 7897c478bd9Sstevel@tonic-gate return (1); 7907c478bd9Sstevel@tonic-gate /* expired, exactly at end of interval */ 7917c478bd9Sstevel@tonic-gate expire: 7927c478bd9Sstevel@tonic-gate if (timerisset(&itp->it_interval)) { 7937c478bd9Sstevel@tonic-gate itp->it_value = itp->it_interval; 7947c478bd9Sstevel@tonic-gate itp->it_value.tv_usec -= usec; 7957c478bd9Sstevel@tonic-gate if (itp->it_value.tv_usec < 0) { 7967c478bd9Sstevel@tonic-gate itp->it_value.tv_usec += MICROSEC; 7977c478bd9Sstevel@tonic-gate itp->it_value.tv_sec--; 7987c478bd9Sstevel@tonic-gate } 7997c478bd9Sstevel@tonic-gate } else 8007c478bd9Sstevel@tonic-gate itp->it_value.tv_usec = 0; /* sec is already 0 */ 8017c478bd9Sstevel@tonic-gate return (0); 8027c478bd9Sstevel@tonic-gate } 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate /* 8057c478bd9Sstevel@tonic-gate * Add and subtract routines for timevals. 8067c478bd9Sstevel@tonic-gate * N.B.: subtract routine doesn't deal with 8077c478bd9Sstevel@tonic-gate * results which are before the beginning, 8087c478bd9Sstevel@tonic-gate * it just gets very confused in this case. 8097c478bd9Sstevel@tonic-gate * Caveat emptor. 8107c478bd9Sstevel@tonic-gate */ 8117c478bd9Sstevel@tonic-gate void 8127c478bd9Sstevel@tonic-gate timevaladd(struct timeval *t1, struct timeval *t2) 8137c478bd9Sstevel@tonic-gate { 8147c478bd9Sstevel@tonic-gate t1->tv_sec += t2->tv_sec; 8157c478bd9Sstevel@tonic-gate t1->tv_usec += t2->tv_usec; 8167c478bd9Sstevel@tonic-gate timevalfix(t1); 8177c478bd9Sstevel@tonic-gate } 8187c478bd9Sstevel@tonic-gate 8197c478bd9Sstevel@tonic-gate void 8207c478bd9Sstevel@tonic-gate timevalsub(struct timeval *t1, struct timeval *t2) 8217c478bd9Sstevel@tonic-gate { 8227c478bd9Sstevel@tonic-gate t1->tv_sec -= t2->tv_sec; 8237c478bd9Sstevel@tonic-gate t1->tv_usec -= t2->tv_usec; 8247c478bd9Sstevel@tonic-gate timevalfix(t1); 8257c478bd9Sstevel@tonic-gate } 8267c478bd9Sstevel@tonic-gate 8277c478bd9Sstevel@tonic-gate void 8287c478bd9Sstevel@tonic-gate timevalfix(struct timeval *t1) 8297c478bd9Sstevel@tonic-gate { 8307c478bd9Sstevel@tonic-gate if (t1->tv_usec < 0) { 8317c478bd9Sstevel@tonic-gate t1->tv_sec--; 8327c478bd9Sstevel@tonic-gate t1->tv_usec += MICROSEC; 8337c478bd9Sstevel@tonic-gate } 8347c478bd9Sstevel@tonic-gate if (t1->tv_usec >= MICROSEC) { 8357c478bd9Sstevel@tonic-gate t1->tv_sec++; 8367c478bd9Sstevel@tonic-gate t1->tv_usec -= MICROSEC; 8377c478bd9Sstevel@tonic-gate } 8387c478bd9Sstevel@tonic-gate } 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate /* 8417c478bd9Sstevel@tonic-gate * Same as the routines above. These routines take a timespec instead 8427c478bd9Sstevel@tonic-gate * of a timeval. 8437c478bd9Sstevel@tonic-gate */ 8447c478bd9Sstevel@tonic-gate void 8457c478bd9Sstevel@tonic-gate timespecadd(timespec_t *t1, timespec_t *t2) 8467c478bd9Sstevel@tonic-gate { 8477c478bd9Sstevel@tonic-gate t1->tv_sec += t2->tv_sec; 8487c478bd9Sstevel@tonic-gate t1->tv_nsec += t2->tv_nsec; 8497c478bd9Sstevel@tonic-gate timespecfix(t1); 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate 8527c478bd9Sstevel@tonic-gate void 8537c478bd9Sstevel@tonic-gate timespecsub(timespec_t *t1, timespec_t *t2) 8547c478bd9Sstevel@tonic-gate { 8557c478bd9Sstevel@tonic-gate t1->tv_sec -= t2->tv_sec; 8567c478bd9Sstevel@tonic-gate t1->tv_nsec -= t2->tv_nsec; 8577c478bd9Sstevel@tonic-gate timespecfix(t1); 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate void 8617c478bd9Sstevel@tonic-gate timespecfix(timespec_t *t1) 8627c478bd9Sstevel@tonic-gate { 8637c478bd9Sstevel@tonic-gate if (t1->tv_nsec < 0) { 8647c478bd9Sstevel@tonic-gate t1->tv_sec--; 8657c478bd9Sstevel@tonic-gate t1->tv_nsec += NANOSEC; 8667c478bd9Sstevel@tonic-gate } else { 8677c478bd9Sstevel@tonic-gate if (t1->tv_nsec >= NANOSEC) { 8687c478bd9Sstevel@tonic-gate t1->tv_sec++; 8697c478bd9Sstevel@tonic-gate t1->tv_nsec -= NANOSEC; 8707c478bd9Sstevel@tonic-gate } 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate 8747c478bd9Sstevel@tonic-gate /* 8757c478bd9Sstevel@tonic-gate * Compute number of hz until specified time. 8767c478bd9Sstevel@tonic-gate * Used to compute third argument to timeout() from an absolute time. 8777c478bd9Sstevel@tonic-gate */ 8787c478bd9Sstevel@tonic-gate clock_t 8797c478bd9Sstevel@tonic-gate hzto(struct timeval *tv) 8807c478bd9Sstevel@tonic-gate { 8817c478bd9Sstevel@tonic-gate timespec_t ts, now; 8827c478bd9Sstevel@tonic-gate 8837c478bd9Sstevel@tonic-gate ts.tv_sec = tv->tv_sec; 8847c478bd9Sstevel@tonic-gate ts.tv_nsec = tv->tv_usec * 1000; 8857c478bd9Sstevel@tonic-gate gethrestime_lasttick(&now); 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate return (timespectohz(&ts, now)); 8887c478bd9Sstevel@tonic-gate } 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate /* 8917c478bd9Sstevel@tonic-gate * Compute number of hz until specified time for a given timespec value. 8927c478bd9Sstevel@tonic-gate * Used to compute third argument to timeout() from an absolute time. 8937c478bd9Sstevel@tonic-gate */ 8947c478bd9Sstevel@tonic-gate clock_t 8957c478bd9Sstevel@tonic-gate timespectohz(timespec_t *tv, timespec_t now) 8967c478bd9Sstevel@tonic-gate { 8977c478bd9Sstevel@tonic-gate clock_t ticks; 8987c478bd9Sstevel@tonic-gate time_t sec; 8997c478bd9Sstevel@tonic-gate int nsec; 9007c478bd9Sstevel@tonic-gate 9017c478bd9Sstevel@tonic-gate /* 9027c478bd9Sstevel@tonic-gate * Compute number of ticks we will see between now and 9037c478bd9Sstevel@tonic-gate * the target time; returns "1" if the destination time 9047c478bd9Sstevel@tonic-gate * is before the next tick, so we always get some delay, 9057c478bd9Sstevel@tonic-gate * and returns LONG_MAX ticks if we would overflow. 9067c478bd9Sstevel@tonic-gate */ 9077c478bd9Sstevel@tonic-gate sec = tv->tv_sec - now.tv_sec; 9087c478bd9Sstevel@tonic-gate nsec = tv->tv_nsec - now.tv_nsec + nsec_per_tick - 1; 9097c478bd9Sstevel@tonic-gate 9107c478bd9Sstevel@tonic-gate if (nsec < 0) { 9117c478bd9Sstevel@tonic-gate sec--; 9127c478bd9Sstevel@tonic-gate nsec += NANOSEC; 9137c478bd9Sstevel@tonic-gate } else if (nsec >= NANOSEC) { 9147c478bd9Sstevel@tonic-gate sec++; 9157c478bd9Sstevel@tonic-gate nsec -= NANOSEC; 9167c478bd9Sstevel@tonic-gate } 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate ticks = NSEC_TO_TICK(nsec); 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate /* 9217c478bd9Sstevel@tonic-gate * Compute ticks, accounting for negative and overflow as above. 9227c478bd9Sstevel@tonic-gate * Overflow protection kicks in at about 70 weeks for hz=50 9237c478bd9Sstevel@tonic-gate * and at about 35 weeks for hz=100. (Rather longer for the 64-bit 9247c478bd9Sstevel@tonic-gate * kernel :-) 9257c478bd9Sstevel@tonic-gate */ 9267c478bd9Sstevel@tonic-gate if (sec < 0 || (sec == 0 && ticks < 1)) 9277c478bd9Sstevel@tonic-gate ticks = 1; /* protect vs nonpositive */ 9287c478bd9Sstevel@tonic-gate else if (sec > (LONG_MAX - ticks) / hz) 9297c478bd9Sstevel@tonic-gate ticks = LONG_MAX; /* protect vs overflow */ 9307c478bd9Sstevel@tonic-gate else 9317c478bd9Sstevel@tonic-gate ticks += sec * hz; /* common case */ 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate return (ticks); 9347c478bd9Sstevel@tonic-gate } 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate /* 937f635d46aSqiao * Compute number of hz with the timespec tv specified. 938f635d46aSqiao * The return type must be 64 bit integer. 9393348528fSdm120769 */ 940f635d46aSqiao int64_t 941f635d46aSqiao timespectohz64(timespec_t *tv) 9423348528fSdm120769 { 943f635d46aSqiao int64_t ticks; 944f635d46aSqiao int64_t sec; 945f635d46aSqiao int64_t nsec; 9463348528fSdm120769 947f635d46aSqiao sec = tv->tv_sec; 948f635d46aSqiao nsec = tv->tv_nsec + nsec_per_tick - 1; 949f635d46aSqiao 950f635d46aSqiao if (nsec < 0) { 951f635d46aSqiao sec--; 952f635d46aSqiao nsec += NANOSEC; 953f635d46aSqiao } else if (nsec >= NANOSEC) { 954f635d46aSqiao sec++; 955f635d46aSqiao nsec -= NANOSEC; 956f635d46aSqiao } 957f635d46aSqiao 958f635d46aSqiao ticks = NSEC_TO_TICK(nsec); 959f635d46aSqiao 960f635d46aSqiao /* 961f635d46aSqiao * Compute ticks, accounting for negative and overflow as above. 962f635d46aSqiao * Overflow protection kicks in at about 70 weeks for hz=50 963f635d46aSqiao * and at about 35 weeks for hz=100. (Rather longer for the 64-bit 964f635d46aSqiao * kernel 965f635d46aSqiao */ 966f635d46aSqiao if (sec < 0 || (sec == 0 && ticks < 1)) 967f635d46aSqiao ticks = 1; /* protect vs nonpositive */ 968f635d46aSqiao else if (sec > (((~0ULL) >> 1) - ticks) / hz) 969f635d46aSqiao ticks = (~0ULL) >> 1; /* protect vs overflow */ 970f635d46aSqiao else 971f635d46aSqiao ticks += sec * hz; /* common case */ 972f635d46aSqiao 973f635d46aSqiao return (ticks); 9743348528fSdm120769 } 9753348528fSdm120769 9763348528fSdm120769 /* 9777c478bd9Sstevel@tonic-gate * hrt2ts(): convert from hrtime_t to timestruc_t. 9787c478bd9Sstevel@tonic-gate * 9797c478bd9Sstevel@tonic-gate * All this routine really does is: 9807c478bd9Sstevel@tonic-gate * 9817c478bd9Sstevel@tonic-gate * tsp->sec = hrt / NANOSEC; 9827c478bd9Sstevel@tonic-gate * tsp->nsec = hrt % NANOSEC; 9837c478bd9Sstevel@tonic-gate * 9847c478bd9Sstevel@tonic-gate * The black magic below avoids doing a 64-bit by 32-bit integer divide, 9857c478bd9Sstevel@tonic-gate * which is quite expensive. There's actually much more going on here than 9867c478bd9Sstevel@tonic-gate * it might first appear -- don't try this at home. 9877c478bd9Sstevel@tonic-gate * 9887c478bd9Sstevel@tonic-gate * For the adventuresome, here's an explanation of how it works. 9897c478bd9Sstevel@tonic-gate * 9907c478bd9Sstevel@tonic-gate * Multiplication by a fixed constant is easy -- you just do the appropriate 9917c478bd9Sstevel@tonic-gate * shifts and adds. For example, to multiply by 10, we observe that 9927c478bd9Sstevel@tonic-gate * 9937c478bd9Sstevel@tonic-gate * x * 10 = x * (8 + 2) 9947c478bd9Sstevel@tonic-gate * = (x * 8) + (x * 2) 9957c478bd9Sstevel@tonic-gate * = (x << 3) + (x << 1). 9967c478bd9Sstevel@tonic-gate * 9977c478bd9Sstevel@tonic-gate * In general, you can read the algorithm right off the bits: the number 10 9987c478bd9Sstevel@tonic-gate * is 1010 in binary; bits 1 and 3 are ones, so x * 10 = (x << 1) + (x << 3). 9997c478bd9Sstevel@tonic-gate * 10007c478bd9Sstevel@tonic-gate * Sometimes you can do better. For example, 15 is 1111 binary, so the normal 10017c478bd9Sstevel@tonic-gate * shift/add computation is x * 15 = (x << 0) + (x << 1) + (x << 2) + (x << 3). 10027c478bd9Sstevel@tonic-gate * But, it's cheaper if you capitalize on the fact that you have a run of ones: 10037c478bd9Sstevel@tonic-gate * 1111 = 10000 - 1, hence x * 15 = (x << 4) - (x << 0). [You would never 10047c478bd9Sstevel@tonic-gate * actually perform the operation << 0, since it's a no-op; I'm just writing 10057c478bd9Sstevel@tonic-gate * it that way for clarity.] 10067c478bd9Sstevel@tonic-gate * 10077c478bd9Sstevel@tonic-gate * The other way you can win is if you get lucky with the prime factorization 10087c478bd9Sstevel@tonic-gate * of your constant. The number 1,000,000,000, which we have to multiply 10097c478bd9Sstevel@tonic-gate * by below, is a good example. One billion is 111011100110101100101000000000 10107c478bd9Sstevel@tonic-gate * in binary. If you apply the bit-grouping trick, it doesn't buy you very 10117c478bd9Sstevel@tonic-gate * much, because it's only a win for groups of three or more equal bits: 10127c478bd9Sstevel@tonic-gate * 10137c478bd9Sstevel@tonic-gate * 111011100110101100101000000000 = 1000000000000000000000000000000 10147c478bd9Sstevel@tonic-gate * - 000100011001010011011000000000 10157c478bd9Sstevel@tonic-gate * 10167c478bd9Sstevel@tonic-gate * Thus, instead of the 13 shift/add pairs (26 operations) implied by the LHS, 10177c478bd9Sstevel@tonic-gate * we have reduced this to 10 shift/add pairs (20 operations) on the RHS. 10187c478bd9Sstevel@tonic-gate * This is better, but not great. 10197c478bd9Sstevel@tonic-gate * 10207c478bd9Sstevel@tonic-gate * However, we can factor 1,000,000,000 = 2^9 * 5^9 = 2^9 * 125 * 125 * 125, 10217c478bd9Sstevel@tonic-gate * and multiply by each factor. Multiplication by 125 is particularly easy, 10227c478bd9Sstevel@tonic-gate * since 128 is nearby: x * 125 = (x << 7) - x - x - x, which is just four 10237c478bd9Sstevel@tonic-gate * operations. So, to multiply by 1,000,000,000, we perform three multipli- 10247c478bd9Sstevel@tonic-gate * cations by 125, then << 9, a total of only 3 * 4 + 1 = 13 operations. 10257c478bd9Sstevel@tonic-gate * This is the algorithm we actually use in both hrt2ts() and ts2hrt(). 10267c478bd9Sstevel@tonic-gate * 10277c478bd9Sstevel@tonic-gate * Division is harder; there is no equivalent of the simple shift-add algorithm 10287c478bd9Sstevel@tonic-gate * we used for multiplication. However, we can convert the division problem 10297c478bd9Sstevel@tonic-gate * into a multiplication problem by pre-computing the binary representation 10307c478bd9Sstevel@tonic-gate * of the reciprocal of the divisor. For the case of interest, we have 10317c478bd9Sstevel@tonic-gate * 10327c478bd9Sstevel@tonic-gate * 1 / 1,000,000,000 = 1.0001001011100000101111101000001B-30, 10337c478bd9Sstevel@tonic-gate * 10347c478bd9Sstevel@tonic-gate * to 32 bits of precision. (The notation B-30 means "* 2^-30", just like 10357c478bd9Sstevel@tonic-gate * E-18 means "* 10^-18".) 10367c478bd9Sstevel@tonic-gate * 10377c478bd9Sstevel@tonic-gate * So, to compute x / 1,000,000,000, we just multiply x by the 32-bit 10387c478bd9Sstevel@tonic-gate * integer 10001001011100000101111101000001, then normalize (shift) the 10397c478bd9Sstevel@tonic-gate * result. This constant has several large bits runs, so the multiply 10407c478bd9Sstevel@tonic-gate * is relatively cheap: 10417c478bd9Sstevel@tonic-gate * 10427c478bd9Sstevel@tonic-gate * 10001001011100000101111101000001 = 10001001100000000110000001000001 10437c478bd9Sstevel@tonic-gate * - 00000000000100000000000100000000 10447c478bd9Sstevel@tonic-gate * 10457c478bd9Sstevel@tonic-gate * Again, you can just read the algorithm right off the bits: 10467c478bd9Sstevel@tonic-gate * 10477c478bd9Sstevel@tonic-gate * sec = hrt; 10487c478bd9Sstevel@tonic-gate * sec += (hrt << 6); 10497c478bd9Sstevel@tonic-gate * sec -= (hrt << 8); 10507c478bd9Sstevel@tonic-gate * sec += (hrt << 13); 10517c478bd9Sstevel@tonic-gate * sec += (hrt << 14); 10527c478bd9Sstevel@tonic-gate * sec -= (hrt << 20); 10537c478bd9Sstevel@tonic-gate * sec += (hrt << 23); 10547c478bd9Sstevel@tonic-gate * sec += (hrt << 24); 10557c478bd9Sstevel@tonic-gate * sec += (hrt << 27); 10567c478bd9Sstevel@tonic-gate * sec += (hrt << 31); 10577c478bd9Sstevel@tonic-gate * sec >>= (32 + 30); 10587c478bd9Sstevel@tonic-gate * 10597c478bd9Sstevel@tonic-gate * Voila! The only problem is, since hrt is 64 bits, we need to use 96-bit 10607c478bd9Sstevel@tonic-gate * arithmetic to perform this calculation. That's a waste, because ultimately 10617c478bd9Sstevel@tonic-gate * we only need the highest 32 bits of the result. 10627c478bd9Sstevel@tonic-gate * 10637c478bd9Sstevel@tonic-gate * The first thing we do is to realize that we don't need to use all of hrt 10647c478bd9Sstevel@tonic-gate * in the calculation. The lowest 30 bits can contribute at most 1 to the 10657c478bd9Sstevel@tonic-gate * quotient (2^30 / 1,000,000,000 = 1.07...), so we'll deal with them later. 10667c478bd9Sstevel@tonic-gate * The highest 2 bits have to be zero, or hrt won't fit in a timestruc_t. 10677c478bd9Sstevel@tonic-gate * Thus, the only bits of hrt that matter for division are bits 30..61. 10687c478bd9Sstevel@tonic-gate * These 32 bits are just the lower-order word of (hrt >> 30). This brings 10697c478bd9Sstevel@tonic-gate * us down from 96-bit math to 64-bit math, and our algorithm becomes: 10707c478bd9Sstevel@tonic-gate * 10717c478bd9Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30); 10727c478bd9Sstevel@tonic-gate * sec = tmp; 10737c478bd9Sstevel@tonic-gate * sec += (tmp << 6); 10747c478bd9Sstevel@tonic-gate * sec -= (tmp << 8); 10757c478bd9Sstevel@tonic-gate * sec += (tmp << 13); 10767c478bd9Sstevel@tonic-gate * sec += (tmp << 14); 10777c478bd9Sstevel@tonic-gate * sec -= (tmp << 20); 10787c478bd9Sstevel@tonic-gate * sec += (tmp << 23); 10797c478bd9Sstevel@tonic-gate * sec += (tmp << 24); 10807c478bd9Sstevel@tonic-gate * sec += (tmp << 27); 10817c478bd9Sstevel@tonic-gate * sec += (tmp << 31); 10827c478bd9Sstevel@tonic-gate * sec >>= 32; 10837c478bd9Sstevel@tonic-gate * 10847c478bd9Sstevel@tonic-gate * Next, we're going to reduce this 64-bit computation to a 32-bit 10857c478bd9Sstevel@tonic-gate * computation. We begin by rewriting the above algorithm to use relative 10867c478bd9Sstevel@tonic-gate * shifts instead of absolute shifts. That is, instead of computing 10877c478bd9Sstevel@tonic-gate * tmp << 6, tmp << 8, tmp << 13, etc, we'll just shift incrementally: 10887c478bd9Sstevel@tonic-gate * tmp <<= 6, tmp <<= 2 (== 8 - 6), tmp <<= 5 (== 13 - 8), etc: 10897c478bd9Sstevel@tonic-gate * 10907c478bd9Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30); 10917c478bd9Sstevel@tonic-gate * sec = tmp; 10927c478bd9Sstevel@tonic-gate * tmp <<= 6; sec += tmp; 10937c478bd9Sstevel@tonic-gate * tmp <<= 2; sec -= tmp; 10947c478bd9Sstevel@tonic-gate * tmp <<= 5; sec += tmp; 10957c478bd9Sstevel@tonic-gate * tmp <<= 1; sec += tmp; 10967c478bd9Sstevel@tonic-gate * tmp <<= 6; sec -= tmp; 10977c478bd9Sstevel@tonic-gate * tmp <<= 3; sec += tmp; 10987c478bd9Sstevel@tonic-gate * tmp <<= 1; sec += tmp; 10997c478bd9Sstevel@tonic-gate * tmp <<= 3; sec += tmp; 11007c478bd9Sstevel@tonic-gate * tmp <<= 4; sec += tmp; 11017c478bd9Sstevel@tonic-gate * sec >>= 32; 11027c478bd9Sstevel@tonic-gate * 11037c478bd9Sstevel@tonic-gate * Now for the final step. Instead of throwing away the low 32 bits at 11047c478bd9Sstevel@tonic-gate * the end, we can throw them away as we go, only keeping the high 32 bits 11057c478bd9Sstevel@tonic-gate * of the product at each step. So, for example, where we now have 11067c478bd9Sstevel@tonic-gate * 11077c478bd9Sstevel@tonic-gate * tmp <<= 6; sec = sec + tmp; 11087c478bd9Sstevel@tonic-gate * we will instead have 11097c478bd9Sstevel@tonic-gate * tmp <<= 6; sec = (sec + tmp) >> 6; 11107c478bd9Sstevel@tonic-gate * which is equivalent to 11117c478bd9Sstevel@tonic-gate * sec = (sec >> 6) + tmp; 11127c478bd9Sstevel@tonic-gate * 11137c478bd9Sstevel@tonic-gate * The final shift ("sec >>= 32") goes away. 11147c478bd9Sstevel@tonic-gate * 11157c478bd9Sstevel@tonic-gate * All we're really doing here is long multiplication, just like we learned in 11167c478bd9Sstevel@tonic-gate * grade school, except that at each step, we only look at the leftmost 32 11177c478bd9Sstevel@tonic-gate * columns. The cumulative error is, at most, the sum of all the bits we 11187c478bd9Sstevel@tonic-gate * throw away, which is 2^-32 + 2^-31 + ... + 2^-2 + 2^-1 == 1 - 2^-32. 11197c478bd9Sstevel@tonic-gate * Thus, the final result ("sec") is correct to +/- 1. 11207c478bd9Sstevel@tonic-gate * 11217c478bd9Sstevel@tonic-gate * It turns out to be important to keep "sec" positive at each step, because 11227c478bd9Sstevel@tonic-gate * we don't want to have to explicitly extend the sign bit. Therefore, 11237c478bd9Sstevel@tonic-gate * starting with the last line of code above, each line that would have read 11247c478bd9Sstevel@tonic-gate * "sec = (sec >> n) - tmp" must be changed to "sec = tmp - (sec >> n)", and 11257c478bd9Sstevel@tonic-gate * the operators (+ or -) in all previous lines must be toggled accordingly. 11267c478bd9Sstevel@tonic-gate * Thus, we end up with: 11277c478bd9Sstevel@tonic-gate * 11287c478bd9Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30); 11297c478bd9Sstevel@tonic-gate * sec = tmp + (sec >> 6); 11307c478bd9Sstevel@tonic-gate * sec = tmp - (tmp >> 2); 11317c478bd9Sstevel@tonic-gate * sec = tmp - (sec >> 5); 11327c478bd9Sstevel@tonic-gate * sec = tmp + (sec >> 1); 11337c478bd9Sstevel@tonic-gate * sec = tmp - (sec >> 6); 11347c478bd9Sstevel@tonic-gate * sec = tmp - (sec >> 3); 11357c478bd9Sstevel@tonic-gate * sec = tmp + (sec >> 1); 11367c478bd9Sstevel@tonic-gate * sec = tmp + (sec >> 3); 11377c478bd9Sstevel@tonic-gate * sec = tmp + (sec >> 4); 11387c478bd9Sstevel@tonic-gate * 11397c478bd9Sstevel@tonic-gate * This yields a value for sec that is accurate to +1/-1, so we have two 11407c478bd9Sstevel@tonic-gate * cases to deal with. The mysterious-looking "+ 7" in the code below biases 11417c478bd9Sstevel@tonic-gate * the rounding toward zero, so that sec is always less than or equal to 11427c478bd9Sstevel@tonic-gate * the correct value. With this modified code, sec is accurate to +0/-2, with 11437c478bd9Sstevel@tonic-gate * the -2 case being very rare in practice. With this change, we only have to 11447c478bd9Sstevel@tonic-gate * deal with one case (sec too small) in the cleanup code. 11457c478bd9Sstevel@tonic-gate * 11467c478bd9Sstevel@tonic-gate * The other modification we make is to delete the second line above 11477c478bd9Sstevel@tonic-gate * ("sec = tmp + (sec >> 6);"), since it only has an effect when bit 31 is 11487c478bd9Sstevel@tonic-gate * set, and the cleanup code can handle that rare case. This reduces the 11497c478bd9Sstevel@tonic-gate * *guaranteed* accuracy of sec to +0/-3, but speeds up the common cases. 11507c478bd9Sstevel@tonic-gate * 11517c478bd9Sstevel@tonic-gate * Finally, we compute nsec = hrt - (sec * 1,000,000,000). nsec will always 11527c478bd9Sstevel@tonic-gate * be positive (since sec is never too large), and will at most be equal to 11537c478bd9Sstevel@tonic-gate * the error in sec (times 1,000,000,000) plus the low-order 30 bits of hrt. 11547c478bd9Sstevel@tonic-gate * Thus, nsec < 3 * 1,000,000,000 + 2^30, which is less than 2^32, so we can 11557c478bd9Sstevel@tonic-gate * safely assume that nsec fits in 32 bits. Consequently, when we compute 11567c478bd9Sstevel@tonic-gate * sec * 1,000,000,000, we only need the low 32 bits, so we can just do 32-bit 11577c478bd9Sstevel@tonic-gate * arithmetic and let the high-order bits fall off the end. 11587c478bd9Sstevel@tonic-gate * 11597c478bd9Sstevel@tonic-gate * Since nsec < 3 * 1,000,000,000 + 2^30 == 4,073,741,824, the cleanup loop: 11607c478bd9Sstevel@tonic-gate * 11617c478bd9Sstevel@tonic-gate * while (nsec >= NANOSEC) { 11627c478bd9Sstevel@tonic-gate * nsec -= NANOSEC; 11637c478bd9Sstevel@tonic-gate * sec++; 11647c478bd9Sstevel@tonic-gate * } 11657c478bd9Sstevel@tonic-gate * 11667c478bd9Sstevel@tonic-gate * is guaranteed to complete in at most 4 iterations. In practice, the loop 11677c478bd9Sstevel@tonic-gate * completes in 0 or 1 iteration over 95% of the time. 11687c478bd9Sstevel@tonic-gate * 11697c478bd9Sstevel@tonic-gate * On an SS2, this implementation of hrt2ts() takes 1.7 usec, versus about 11707c478bd9Sstevel@tonic-gate * 35 usec for software division -- about 20 times faster. 11717c478bd9Sstevel@tonic-gate */ 11727c478bd9Sstevel@tonic-gate void 11737c478bd9Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp) 11747c478bd9Sstevel@tonic-gate { 11757c478bd9Sstevel@tonic-gate uint32_t sec, nsec, tmp; 11767c478bd9Sstevel@tonic-gate 11777c478bd9Sstevel@tonic-gate tmp = (uint32_t)(hrt >> 30); 11787c478bd9Sstevel@tonic-gate sec = tmp - (tmp >> 2); 11797c478bd9Sstevel@tonic-gate sec = tmp - (sec >> 5); 11807c478bd9Sstevel@tonic-gate sec = tmp + (sec >> 1); 11817c478bd9Sstevel@tonic-gate sec = tmp - (sec >> 6) + 7; 11827c478bd9Sstevel@tonic-gate sec = tmp - (sec >> 3); 11837c478bd9Sstevel@tonic-gate sec = tmp + (sec >> 1); 11847c478bd9Sstevel@tonic-gate sec = tmp + (sec >> 3); 11857c478bd9Sstevel@tonic-gate sec = tmp + (sec >> 4); 11867c478bd9Sstevel@tonic-gate tmp = (sec << 7) - sec - sec - sec; 11877c478bd9Sstevel@tonic-gate tmp = (tmp << 7) - tmp - tmp - tmp; 11887c478bd9Sstevel@tonic-gate tmp = (tmp << 7) - tmp - tmp - tmp; 11897c478bd9Sstevel@tonic-gate nsec = (uint32_t)hrt - (tmp << 9); 11907c478bd9Sstevel@tonic-gate while (nsec >= NANOSEC) { 11917c478bd9Sstevel@tonic-gate nsec -= NANOSEC; 11927c478bd9Sstevel@tonic-gate sec++; 11937c478bd9Sstevel@tonic-gate } 11947c478bd9Sstevel@tonic-gate tsp->tv_sec = (time_t)sec; 11957c478bd9Sstevel@tonic-gate tsp->tv_nsec = nsec; 11967c478bd9Sstevel@tonic-gate } 11977c478bd9Sstevel@tonic-gate 11987c478bd9Sstevel@tonic-gate /* 11997c478bd9Sstevel@tonic-gate * Convert from timestruc_t to hrtime_t. 12007c478bd9Sstevel@tonic-gate * 12017c478bd9Sstevel@tonic-gate * The code below is equivalent to: 12027c478bd9Sstevel@tonic-gate * 12037c478bd9Sstevel@tonic-gate * hrt = tsp->tv_sec * NANOSEC + tsp->tv_nsec; 12047c478bd9Sstevel@tonic-gate * 12057c478bd9Sstevel@tonic-gate * but requires no integer multiply. 12067c478bd9Sstevel@tonic-gate */ 12077c478bd9Sstevel@tonic-gate hrtime_t 12087c478bd9Sstevel@tonic-gate ts2hrt(const timestruc_t *tsp) 12097c478bd9Sstevel@tonic-gate { 12107c478bd9Sstevel@tonic-gate hrtime_t hrt; 12117c478bd9Sstevel@tonic-gate 12127c478bd9Sstevel@tonic-gate hrt = tsp->tv_sec; 12137c478bd9Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt; 12147c478bd9Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt; 12157c478bd9Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt; 12167c478bd9Sstevel@tonic-gate hrt = (hrt << 9) + tsp->tv_nsec; 12177c478bd9Sstevel@tonic-gate return (hrt); 12187c478bd9Sstevel@tonic-gate } 12197c478bd9Sstevel@tonic-gate 12207c478bd9Sstevel@tonic-gate /* 12217c478bd9Sstevel@tonic-gate * For the various 32-bit "compatibility" paths in the system. 12227c478bd9Sstevel@tonic-gate */ 12237c478bd9Sstevel@tonic-gate void 12247c478bd9Sstevel@tonic-gate hrt2ts32(hrtime_t hrt, timestruc32_t *ts32p) 12257c478bd9Sstevel@tonic-gate { 12267c478bd9Sstevel@tonic-gate timestruc_t ts; 12277c478bd9Sstevel@tonic-gate 12287c478bd9Sstevel@tonic-gate hrt2ts(hrt, &ts); 12297c478bd9Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(ts32p, &ts); 12307c478bd9Sstevel@tonic-gate } 12317c478bd9Sstevel@tonic-gate 12327c478bd9Sstevel@tonic-gate /* 12337c478bd9Sstevel@tonic-gate * If this ever becomes performance critical (ha!), we can borrow the 12347c478bd9Sstevel@tonic-gate * code from ts2hrt(), above, to multiply tv_sec by 1,000,000 and the 12357c478bd9Sstevel@tonic-gate * straightforward (x << 10) - (x << 5) + (x << 3) to multiply tv_usec by 12367c478bd9Sstevel@tonic-gate * 1,000. For now, we'll opt for readability (besides, the compiler does 12377c478bd9Sstevel@tonic-gate * a passable job of optimizing constant multiplication into shifts and adds). 12387c478bd9Sstevel@tonic-gate */ 12397c478bd9Sstevel@tonic-gate hrtime_t 12407c478bd9Sstevel@tonic-gate tv2hrt(struct timeval *tvp) 12417c478bd9Sstevel@tonic-gate { 12427c478bd9Sstevel@tonic-gate return ((hrtime_t)tvp->tv_sec * NANOSEC + 12437c478bd9Sstevel@tonic-gate (hrtime_t)tvp->tv_usec * (NANOSEC / MICROSEC)); 12447c478bd9Sstevel@tonic-gate } 12457c478bd9Sstevel@tonic-gate 12467c478bd9Sstevel@tonic-gate void 1247be2140a8Sandyb hrt2tv(hrtime_t hrt, struct timeval *tvp) 12487c478bd9Sstevel@tonic-gate { 1249be2140a8Sandyb uint32_t sec, nsec, tmp; 1250be2140a8Sandyb uint32_t q, r, t; 1251be2140a8Sandyb 1252be2140a8Sandyb tmp = (uint32_t)(hrt >> 30); 1253be2140a8Sandyb sec = tmp - (tmp >> 2); 1254be2140a8Sandyb sec = tmp - (sec >> 5); 1255be2140a8Sandyb sec = tmp + (sec >> 1); 1256be2140a8Sandyb sec = tmp - (sec >> 6) + 7; 1257be2140a8Sandyb sec = tmp - (sec >> 3); 1258be2140a8Sandyb sec = tmp + (sec >> 1); 1259be2140a8Sandyb sec = tmp + (sec >> 3); 1260be2140a8Sandyb sec = tmp + (sec >> 4); 1261be2140a8Sandyb tmp = (sec << 7) - sec - sec - sec; 1262be2140a8Sandyb tmp = (tmp << 7) - tmp - tmp - tmp; 1263be2140a8Sandyb tmp = (tmp << 7) - tmp - tmp - tmp; 1264be2140a8Sandyb nsec = (uint32_t)hrt - (tmp << 9); 1265be2140a8Sandyb while (nsec >= NANOSEC) { 1266be2140a8Sandyb nsec -= NANOSEC; 1267be2140a8Sandyb sec++; 1268be2140a8Sandyb } 1269be2140a8Sandyb tvp->tv_sec = (time_t)sec; 1270be2140a8Sandyb /* 1271be2140a8Sandyb * this routine is very similar to hr2ts, but requires microseconds 1272be2140a8Sandyb * instead of nanoseconds, so an interger divide by 1000 routine 1273be2140a8Sandyb * completes the conversion 1274be2140a8Sandyb */ 1275be2140a8Sandyb t = (nsec >> 7) + (nsec >> 8) + (nsec >> 12); 1276be2140a8Sandyb q = (nsec >> 1) + t + (nsec >> 15) + (t >> 11) + (t >> 14); 1277be2140a8Sandyb q = q >> 9; 1278be2140a8Sandyb r = nsec - q*1000; 1279be2140a8Sandyb tvp->tv_usec = q + ((r + 24) >> 10); 1280be2140a8Sandyb 12817c478bd9Sstevel@tonic-gate } 12827c478bd9Sstevel@tonic-gate 12837c478bd9Sstevel@tonic-gate int 12847c478bd9Sstevel@tonic-gate nanosleep(timespec_t *rqtp, timespec_t *rmtp) 12857c478bd9Sstevel@tonic-gate { 12867c478bd9Sstevel@tonic-gate timespec_t rqtime; 12877c478bd9Sstevel@tonic-gate timespec_t rmtime; 12887c478bd9Sstevel@tonic-gate timespec_t now; 12893348528fSdm120769 int timecheck; 12907c478bd9Sstevel@tonic-gate int ret = 1; 12917c478bd9Sstevel@tonic-gate model_t datamodel = get_udatamodel(); 12927c478bd9Sstevel@tonic-gate 129344e59b5cSDonghai Qiao timecheck = timechanged; 129444e59b5cSDonghai Qiao gethrestime(&now); 129544e59b5cSDonghai Qiao 12967c478bd9Sstevel@tonic-gate if (datamodel == DATAMODEL_NATIVE) { 12977c478bd9Sstevel@tonic-gate if (copyin(rqtp, &rqtime, sizeof (rqtime))) 12987c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 12997c478bd9Sstevel@tonic-gate } else { 13007c478bd9Sstevel@tonic-gate timespec32_t rqtime32; 13017c478bd9Sstevel@tonic-gate 13027c478bd9Sstevel@tonic-gate if (copyin(rqtp, &rqtime32, sizeof (rqtime32))) 13037c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 13047c478bd9Sstevel@tonic-gate TIMESPEC32_TO_TIMESPEC(&rqtime, &rqtime32); 13057c478bd9Sstevel@tonic-gate } 13067c478bd9Sstevel@tonic-gate 13077c478bd9Sstevel@tonic-gate if (rqtime.tv_sec < 0 || rqtime.tv_nsec < 0 || 13087c478bd9Sstevel@tonic-gate rqtime.tv_nsec >= NANOSEC) 13097c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 13107c478bd9Sstevel@tonic-gate 13117c478bd9Sstevel@tonic-gate if (timerspecisset(&rqtime)) { 13127c478bd9Sstevel@tonic-gate timespecadd(&rqtime, &now); 13137c478bd9Sstevel@tonic-gate mutex_enter(&curthread->t_delay_lock); 13147c478bd9Sstevel@tonic-gate while ((ret = cv_waituntil_sig(&curthread->t_delay_cv, 13153348528fSdm120769 &curthread->t_delay_lock, &rqtime, timecheck)) > 0) 13167c478bd9Sstevel@tonic-gate continue; 13177c478bd9Sstevel@tonic-gate mutex_exit(&curthread->t_delay_lock); 13187c478bd9Sstevel@tonic-gate } 13197c478bd9Sstevel@tonic-gate 13207c478bd9Sstevel@tonic-gate if (rmtp) { 13217c478bd9Sstevel@tonic-gate /* 13227c478bd9Sstevel@tonic-gate * If cv_waituntil_sig() returned due to a signal, and 13237c478bd9Sstevel@tonic-gate * there is time remaining, then set the time remaining. 13247c478bd9Sstevel@tonic-gate * Else set time remaining to zero 13257c478bd9Sstevel@tonic-gate */ 13267c478bd9Sstevel@tonic-gate rmtime.tv_sec = rmtime.tv_nsec = 0; 13277c478bd9Sstevel@tonic-gate if (ret == 0) { 1328b2a1c443Svb160487 timespec_t delta = rqtime; 1329b2a1c443Svb160487 13307c478bd9Sstevel@tonic-gate gethrestime(&now); 1331b2a1c443Svb160487 timespecsub(&delta, &now); 1332b2a1c443Svb160487 if (delta.tv_sec > 0 || (delta.tv_sec == 0 && 1333b2a1c443Svb160487 delta.tv_nsec > 0)) 1334b2a1c443Svb160487 rmtime = delta; 13357c478bd9Sstevel@tonic-gate } 13367c478bd9Sstevel@tonic-gate 13377c478bd9Sstevel@tonic-gate if (datamodel == DATAMODEL_NATIVE) { 13387c478bd9Sstevel@tonic-gate if (copyout(&rmtime, rmtp, sizeof (rmtime))) 13397c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 13407c478bd9Sstevel@tonic-gate } else { 13417c478bd9Sstevel@tonic-gate timespec32_t rmtime32; 13427c478bd9Sstevel@tonic-gate 13437c478bd9Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime); 13447c478bd9Sstevel@tonic-gate if (copyout(&rmtime32, rmtp, sizeof (rmtime32))) 13457c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 13467c478bd9Sstevel@tonic-gate } 13477c478bd9Sstevel@tonic-gate } 13487c478bd9Sstevel@tonic-gate 13497c478bd9Sstevel@tonic-gate if (ret == 0) 13507c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 13517c478bd9Sstevel@tonic-gate return (0); 13527c478bd9Sstevel@tonic-gate } 13537c478bd9Sstevel@tonic-gate 13547c478bd9Sstevel@tonic-gate /* 13557c478bd9Sstevel@tonic-gate * Routines to convert standard UNIX time (seconds since Jan 1, 1970) 13567c478bd9Sstevel@tonic-gate * into year/month/day/hour/minute/second format, and back again. 13577c478bd9Sstevel@tonic-gate * Note: these routines require tod_lock held to protect cached state. 13587c478bd9Sstevel@tonic-gate */ 13597c478bd9Sstevel@tonic-gate static int days_thru_month[64] = { 13607c478bd9Sstevel@tonic-gate 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 0, 0, 13617c478bd9Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0, 13627c478bd9Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0, 13637c478bd9Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0, 13647c478bd9Sstevel@tonic-gate }; 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate todinfo_t saved_tod; 13677c478bd9Sstevel@tonic-gate int saved_utc = -60; 13687c478bd9Sstevel@tonic-gate 13697c478bd9Sstevel@tonic-gate todinfo_t 13707c478bd9Sstevel@tonic-gate utc_to_tod(time_t utc) 13717c478bd9Sstevel@tonic-gate { 13727c478bd9Sstevel@tonic-gate long dse, day, month, year; 13737c478bd9Sstevel@tonic-gate todinfo_t tod; 13747c478bd9Sstevel@tonic-gate 13757c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 13767c478bd9Sstevel@tonic-gate 1377*8fc99e42STrevor Thompson /* 1378*8fc99e42STrevor Thompson * Note that tod_set_prev() assumes utc will be set to zero in 1379*8fc99e42STrevor Thompson * the case of it being negative. Consequently, any change made 1380*8fc99e42STrevor Thompson * to this behavior would have to be reflected in that function 1381*8fc99e42STrevor Thompson * as well. 1382*8fc99e42STrevor Thompson */ 13837c478bd9Sstevel@tonic-gate if (utc < 0) /* should never happen */ 13847c478bd9Sstevel@tonic-gate utc = 0; 13857c478bd9Sstevel@tonic-gate 13867c478bd9Sstevel@tonic-gate saved_tod.tod_sec += utc - saved_utc; 13877c478bd9Sstevel@tonic-gate saved_utc = utc; 13887c478bd9Sstevel@tonic-gate if (saved_tod.tod_sec >= 0 && saved_tod.tod_sec < 60) 13897c478bd9Sstevel@tonic-gate return (saved_tod); /* only the seconds changed */ 13907c478bd9Sstevel@tonic-gate 13917c478bd9Sstevel@tonic-gate dse = utc / 86400; /* days since epoch */ 13927c478bd9Sstevel@tonic-gate 13937c478bd9Sstevel@tonic-gate tod.tod_sec = utc % 60; 13947c478bd9Sstevel@tonic-gate tod.tod_min = (utc % 3600) / 60; 13957c478bd9Sstevel@tonic-gate tod.tod_hour = (utc % 86400) / 3600; 13967c478bd9Sstevel@tonic-gate tod.tod_dow = (dse + 4) % 7 + 1; /* epoch was a Thursday */ 13977c478bd9Sstevel@tonic-gate 13987c478bd9Sstevel@tonic-gate year = dse / 365 + 72; /* first guess -- always a bit too large */ 13997c478bd9Sstevel@tonic-gate do { 14007c478bd9Sstevel@tonic-gate year--; 14017c478bd9Sstevel@tonic-gate day = dse - 365 * (year - 70) - ((year - 69) >> 2); 14027c478bd9Sstevel@tonic-gate } while (day < 0); 14037c478bd9Sstevel@tonic-gate 14047c478bd9Sstevel@tonic-gate month = ((year & 3) << 4) + 1; 14057c478bd9Sstevel@tonic-gate while (day >= days_thru_month[month + 1]) 14067c478bd9Sstevel@tonic-gate month++; 14077c478bd9Sstevel@tonic-gate 14087c478bd9Sstevel@tonic-gate tod.tod_day = day - days_thru_month[month] + 1; 14097c478bd9Sstevel@tonic-gate tod.tod_month = month & 15; 14107c478bd9Sstevel@tonic-gate tod.tod_year = year; 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate saved_tod = tod; 14137c478bd9Sstevel@tonic-gate return (tod); 14147c478bd9Sstevel@tonic-gate } 14157c478bd9Sstevel@tonic-gate 14167c478bd9Sstevel@tonic-gate time_t 14177c478bd9Sstevel@tonic-gate tod_to_utc(todinfo_t tod) 14187c478bd9Sstevel@tonic-gate { 14197c478bd9Sstevel@tonic-gate time_t utc; 14207c478bd9Sstevel@tonic-gate int year = tod.tod_year; 14217c478bd9Sstevel@tonic-gate int month = tod.tod_month + ((year & 3) << 4); 14227c478bd9Sstevel@tonic-gate #ifdef DEBUG 14237c478bd9Sstevel@tonic-gate /* only warn once, not each time called */ 14247c478bd9Sstevel@tonic-gate static int year_warn = 1; 14257c478bd9Sstevel@tonic-gate static int month_warn = 1; 14267c478bd9Sstevel@tonic-gate static int day_warn = 1; 14277c478bd9Sstevel@tonic-gate static int hour_warn = 1; 14287c478bd9Sstevel@tonic-gate static int min_warn = 1; 14297c478bd9Sstevel@tonic-gate static int sec_warn = 1; 14307c478bd9Sstevel@tonic-gate int days_diff = days_thru_month[month + 1] - days_thru_month[month]; 14317c478bd9Sstevel@tonic-gate #endif 14327c478bd9Sstevel@tonic-gate 14337c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 14347c478bd9Sstevel@tonic-gate 14357c478bd9Sstevel@tonic-gate #ifdef DEBUG 14367c478bd9Sstevel@tonic-gate if (year_warn && (year < 70 || year > 8029)) { 14377c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 14387c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 14397c478bd9Sstevel@tonic-gate "wrong years value %d -- time needs to be reset\n", 14407c478bd9Sstevel@tonic-gate year); 14417c478bd9Sstevel@tonic-gate year_warn = 0; 14427c478bd9Sstevel@tonic-gate } 14437c478bd9Sstevel@tonic-gate 14447c478bd9Sstevel@tonic-gate if (month_warn && (tod.tod_month < 1 || tod.tod_month > 12)) { 14457c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 14467c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 14477c478bd9Sstevel@tonic-gate "wrong months value %d -- time needs to be reset\n", 14487c478bd9Sstevel@tonic-gate tod.tod_month); 14497c478bd9Sstevel@tonic-gate month_warn = 0; 14507c478bd9Sstevel@tonic-gate } 14517c478bd9Sstevel@tonic-gate 14527c478bd9Sstevel@tonic-gate if (day_warn && (tod.tod_day < 1 || tod.tod_day > days_diff)) { 14537c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 14547c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 14557c478bd9Sstevel@tonic-gate "wrong days value %d -- time needs to be reset\n", 14567c478bd9Sstevel@tonic-gate tod.tod_day); 14577c478bd9Sstevel@tonic-gate day_warn = 0; 14587c478bd9Sstevel@tonic-gate } 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate if (hour_warn && (tod.tod_hour < 0 || tod.tod_hour > 23)) { 14617c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 14627c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 14637c478bd9Sstevel@tonic-gate "wrong hours value %d -- time needs to be reset\n", 14647c478bd9Sstevel@tonic-gate tod.tod_hour); 14657c478bd9Sstevel@tonic-gate hour_warn = 0; 14667c478bd9Sstevel@tonic-gate } 14677c478bd9Sstevel@tonic-gate 14687c478bd9Sstevel@tonic-gate if (min_warn && (tod.tod_min < 0 || tod.tod_min > 59)) { 14697c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 14707c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 14717c478bd9Sstevel@tonic-gate "wrong minutes value %d -- time needs to be reset\n", 14727c478bd9Sstevel@tonic-gate tod.tod_min); 14737c478bd9Sstevel@tonic-gate min_warn = 0; 14747c478bd9Sstevel@tonic-gate } 14757c478bd9Sstevel@tonic-gate 14767c478bd9Sstevel@tonic-gate if (sec_warn && (tod.tod_sec < 0 || tod.tod_sec > 59)) { 14777c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 14787c478bd9Sstevel@tonic-gate "The hardware real-time clock appears to have the " 14797c478bd9Sstevel@tonic-gate "wrong seconds value %d -- time needs to be reset\n", 14807c478bd9Sstevel@tonic-gate tod.tod_sec); 14817c478bd9Sstevel@tonic-gate sec_warn = 0; 14827c478bd9Sstevel@tonic-gate } 14837c478bd9Sstevel@tonic-gate #endif 14847c478bd9Sstevel@tonic-gate 14857c478bd9Sstevel@tonic-gate utc = (year - 70); /* next 3 lines: utc = 365y + y/4 */ 14867c478bd9Sstevel@tonic-gate utc += (utc << 3) + (utc << 6); 14877c478bd9Sstevel@tonic-gate utc += (utc << 2) + ((year - 69) >> 2); 14887c478bd9Sstevel@tonic-gate utc += days_thru_month[month] + tod.tod_day - 1; 14897c478bd9Sstevel@tonic-gate utc = (utc << 3) + (utc << 4) + tod.tod_hour; /* 24 * day + hour */ 14907c478bd9Sstevel@tonic-gate utc = (utc << 6) - (utc << 2) + tod.tod_min; /* 60 * hour + min */ 14917c478bd9Sstevel@tonic-gate utc = (utc << 6) - (utc << 2) + tod.tod_sec; /* 60 * min + sec */ 14927c478bd9Sstevel@tonic-gate 14937c478bd9Sstevel@tonic-gate return (utc); 14947c478bd9Sstevel@tonic-gate } 1495