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