/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1982, 1986 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include <sys/sysmacros.h>
static void realitexpire(void *);
static void realprofexpire(void *);
/*
* Constant to define the minimum interval value of the ITIMER_REALPROF timer.
* Value is in microseconds; defaults to 500 usecs. Setting this value
* significantly lower may allow for denial-of-service attacks.
*/
/*
* macro to compare a timeval to a timestruc
*/
/* CSTYLED */ \
/* CSTYLED */ \
/*
* Time of day and interval timer support.
*
* These routines provide the kernel entry points to get and set
* the time-of-day and per-process interval timers. Subroutines
* here provide support for adding and subtracting timeval structures
* and decrementing interval timers, optionally reloading the interval
* timers when they expire.
*/
/*
* SunOS function to generate monotonically increasing time values.
*/
void
{
static int last_timechanged;
/*
* protect modification of last
*/
gethrestime(&ts);
/*
* Fast algorithm to convert nsec to usec -- see hrt2ts()
*/
/*
* If the system hres time has been changed since the last time
* we are called. then all bets are off; just update our
* local copy of timechanged and accept the reported time as is.
*/
if (last_timechanged != timechanged) {
}
/*
* Try to keep timestamps unique, but don't be obsessive about
* it in the face of large differences.
*/
sec++;
}
}
}
/*
* Timestamps are exported from the kernel in several places.
* Such timestamps are commonly used for either uniqueness or for
* sequencing - truncation to 32-bits is fine for uniqueness,
* but sequencing is going to take more work as we get closer to 2038!
*/
void
{
}
int
{
if (tp) {
if (get_udatamodel() == DATAMODEL_NATIVE) {
} else {
if (TIMEVAL_OVERFLOW(&atv))
}
}
return (0);
}
int
{
int error;
if (get_udatamodel() == DATAMODEL_NATIVE)
else {
if (ITIMERVAL_OVERFLOW(&kitv)) {
} else {
}
}
}
}
int
{
mutex_enter(&p->p_lock);
switch (which) {
case ITIMER_VIRTUAL:
case ITIMER_PROF:
break;
case ITIMER_REAL:
aitv = p->p_realitimer;
/*CSTYLED*/
} else {
}
}
break;
case ITIMER_REALPROF:
break;
}
/*
* We haven't gone off for the first time; the time
* remaining is simply the first time we will go
* off minus the current time.
*/
} else {
if (interval == 0) {
/*
* This was set as a one-shot, and we've
* already gone off; there is no time
* remaining.
*/
remain = 0;
} else {
/*
* We have a non-zero interval; we need to
* determine how far we are into the current
* interval, and subtract that from the
* interval to determine the time remaining.
*/
}
}
break;
default:
mutex_exit(&p->p_lock);
return (EINVAL);
}
mutex_exit(&p->p_lock);
if (iskaddr) {
} else {
return (EFAULT);
}
return (0);
}
int
{
int error;
return (error);
return (0);
if (get_udatamodel() == DATAMODEL_NATIVE)
else {
}
}
int
{
kthread_t *t;
int min;
return (0);
if (iskaddr) {
} else {
return (EFAULT);
}
if (which == ITIMER_REALPROF) {
} else {
min = usec_per_tick;
}
return (EINVAL);
mutex_enter(&p->p_lock);
switch (which) {
case ITIMER_REAL:
/*
* The SITBUSY flag prevents conflicts with multiple
* threads attempting to perform setitimer(ITIMER_REAL)
* at the same time, even when we drop p->p_lock below.
* Any blocked thread returns successfully because the
* effect is the same as if it got here first, finished,
* and the other thread then came through and destroyed
* what it did. We are just protecting the system from
* malfunctioning due to the race condition.
*/
mutex_exit(&p->p_lock);
return (0);
}
while ((tmp_id = p->p_itimerid) != 0) {
/*
* Avoid deadlock in callout_delete (called from
* untimeout) which may go to sleep (while holding
* p_lock). Drop p_lock and re-acquire it after
* untimeout returns. Need to clear p_itimerid
* while holding p_lock.
*/
p->p_itimerid = 0;
mutex_exit(&p->p_lock);
mutex_enter(&p->p_lock);
}
}
p->p_realitimer = aitv;
break;
case ITIMER_REALPROF:
cyclic = p->p_rprof_cyclic;
p->p_rprof_cyclic = CYCLIC_NONE;
mutex_exit(&p->p_lock);
/*
* We're now going to acquire cpu_lock, remove the old cyclic
* if necessary, and add our new cyclic.
*/
if (cyclic != CYCLIC_NONE)
/*
* If we were passed a value of 0, we're done.
*/
return (0);
}
if (when.cyt_interval == 0) {
/*
* Using the same logic as for CLOCK_HIGHRES timers, we
* set the interval to be INT64_MAX - when.cyt_when to
* effect a one-shot; see the comment in clock_highres.c
* for more details on why this works.
*/
}
/*
* We have now successfully added the cyclic. Reacquire
* p_lock, and see if anyone has snuck in.
*/
mutex_enter(&p->p_lock);
if (p->p_rprof_cyclic != CYCLIC_NONE) {
/*
* We're racing with another thread establishing an
* ITIMER_REALPROF interval timer. We'll let the other
* thread win (this is a race at the application level,
* so letting the other thread win is acceptable).
*/
mutex_exit(&p->p_lock);
return (0);
}
/*
* Success. Set our tracking variables in the proc structure,
* cancel any outstanding ITIMER_PROF, and allocate the
* per-thread SIGPROF buffers, if possible.
*/
p->p_rprof_timer = aitv;
p->p_rprof_cyclic = cyclic;
t = p->p_tlist;
do {
continue;
t->t_rprof =
aston(t);
break;
case ITIMER_VIRTUAL:
break;
case ITIMER_PROF:
if (p->p_rprof_cyclic != CYCLIC_NONE) {
/*
* Silently ignore ITIMER_PROF if ITIMER_REALPROF
* is in effect.
*/
break;
}
break;
default:
mutex_exit(&p->p_lock);
return (EINVAL);
}
mutex_exit(&p->p_lock);
return (0);
}
/*
* Delete the ITIMER_REALPROF interval timer.
* Called only from exec_args() when exec occurs.
* The other ITIMER_* interval timers are specified
* to be inherited across exec(), so leave them alone.
*/
void
delete_itimer_realprof(void)
{
mutex_enter(&p->p_lock);
/* we are performing execve(); assert we are single-threaded */
mutex_exit(&p->p_lock);
} else {
p->p_rprof_cyclic = CYCLIC_NONE;
/*
* Delete any current instance of SIGPROF.
*/
lwp->lwp_cursig = 0;
lwp->lwp_extsig = 0;
if (lwp->lwp_curinfo) {
}
}
/*
* Delete any pending instances of SIGPROF.
*/
mutex_exit(&p->p_lock);
/*
* Remove the ITIMER_REALPROF cyclic.
*/
}
}
/*
* Real interval timer expired:
* send process whose timer expired an alarm signal.
* If time is not set up to reload, then just return.
* Else compute next time timer should go off which is > current time.
* This is where delay in processing this timeout causes multiple
* SIGALRM calls to be compressed into one.
*/
static void
{
#if !defined(_LP64)
#endif
mutex_enter(&p->p_lock);
#if !defined(_LP64)
/*
* If we are executing before we were meant to, it must be
* because of an overflow in a prior hzto() calculation.
* In this case, we want to go to sleep for the recalculated
* number of ticks. For the special meaning of the value "1"
* see comment in timespectohz().
*/
mutex_exit(&p->p_lock);
return;
}
#endif
if (!timerisset(intervalp)) {
p->p_itimerid = 0;
} else {
/* advance timer value past current time */
}
mutex_exit(&p->p_lock);
}
/*
* Real time profiling interval timer expired:
* Increment microstate counters for each lwp in the process
* and ensure that running lwps are kicked into the kernel.
* If time is not set up to reload, then just return.
* Else compute next time timer should go off which is > current time,
* as above.
*/
static void
{
kthread_t *t;
mutex_enter(&p->p_lock);
if (p->p_rprof_cyclic == CYCLIC_NONE ||
mutex_exit(&p->p_lock);
return;
}
do {
int mstate;
/*
* Attempt to allocate the SIGPROF buffer, but don't sleep.
*/
continue;
thread_lock(t);
switch (t->t_state) {
case TS_SLEEP:
/*
* Don't touch the lwp is it is swapped out.
*/
if (!(t->t_schedflag & TS_LOAD)) {
break;
}
case LMS_TFAULT:
case LMS_DFAULT:
case LMS_KFAULT:
case LMS_USER_LOCK:
break;
default:
break;
}
break;
case TS_RUN:
case TS_WAIT:
break;
case TS_ONPROC:
case LMS_USER:
case LMS_SYSTEM:
case LMS_TRAP:
break;
default:
mstate = LMS_SYSTEM;
break;
}
break;
default:
break;
}
aston(t);
/*
* force the thread into the kernel
* if it is not already there.
*/
thread_unlock(t);
mutex_exit(&p->p_lock);
}
/*
* Advances timer value past the current time of day. See the detailed
* comment for this logic in realitsexpire(), above.
*/
static void
{
int cnt2nth;
for (;;) {
/*CSTYLED*/
break;
}
if (cnt2nth == 0)
break;
}
}
/*
* Check that a proposed value to load into the .it_value or .it_interval
* part of an interval timer is acceptable, and set it to at least a
* specified minimal value.
*/
int
{
return (EINVAL);
return (0);
}
/*
* Same as itimerfix, except a) it takes a timespec instead of a timeval and
* b) it doesn't truncate based on timeout granularity; consumers of this
* interface (e.g. timer_settime()) depend on the passed timespec not being
* modified implicitly.
*/
int
{
return (EINVAL);
return (0);
}
/*
* Decrement an interval timer by a specified number
* of microseconds, which must be less than a second,
* i.e. < 1000000. If the timer expires, then reload
* it. In this case, carry over (usec - old value) to
* reducint the value reloaded into the timer so that
* the timer does not drift. This routine assumes
* that it is called in a context where the timers
* on which it is operating cannot change in value.
*/
int
{
/* expired, and already in next interval */
goto expire;
}
}
usec = 0;
return (1);
/* expired, exactly at end of interval */
}
} else
return (0);
}
/*
* Add and subtract routines for timevals.
* N.B.: subtract routine doesn't deal with
* results which are before the beginning,
* it just gets very confused in this case.
* Caveat emptor.
*/
void
{
timevalfix(t1);
}
void
{
timevalfix(t1);
}
void
{
}
}
}
/*
* Same as the routines above. These routines take a timespec instead
* of a timeval.
*/
void
{
}
void
{
}
void
{
} else {
}
}
}
/*
* Compute number of hz until specified time.
* Used to compute third argument to timeout() from an absolute time.
*/
{
}
/*
* Compute number of hz until specified time for a given timespec value.
* Used to compute third argument to timeout() from an absolute time.
*/
{
int nsec;
/*
* Compute number of ticks we will see between now and
* the target time; returns "1" if the destination time
* is before the next tick, so we always get some delay,
* and returns LONG_MAX ticks if we would overflow.
*/
if (nsec < 0) {
sec--;
sec++;
}
/*
* Compute ticks, accounting for negative and overflow as above.
* Overflow protection kicks in at about 70 weeks for hz=50
* and at about 35 weeks for hz=100. (Rather longer for the 64-bit
* kernel :-)
*/
else
return (ticks);
}
/*
* Compute number of hz with the timespec tv specified.
* The return type must be 64 bit integer.
*/
{
if (nsec < 0) {
sec--;
sec++;
}
/*
* Compute ticks, accounting for negative and overflow as above.
* Overflow protection kicks in at about 70 weeks for hz=50
* and at about 35 weeks for hz=100. (Rather longer for the 64-bit
* kernel
*/
else
return (ticks);
}
/*
* hrt2ts(): convert from hrtime_t to timestruc_t.
*
* All this routine really does is:
*
* tsp->sec = hrt / NANOSEC;
* tsp->nsec = hrt % NANOSEC;
*
* The black magic below avoids doing a 64-bit by 32-bit integer divide,
* which is quite expensive. There's actually much more going on here than
* it might first appear -- don't try this at home.
*
* For the adventuresome, here's an explanation of how it works.
*
* Multiplication by a fixed constant is easy -- you just do the appropriate
* shifts and adds. For example, to multiply by 10, we observe that
*
* x * 10 = x * (8 + 2)
* = (x * 8) + (x * 2)
* = (x << 3) + (x << 1).
*
* In general, you can read the algorithm right off the bits: the number 10
* is 1010 in binary; bits 1 and 3 are ones, so x * 10 = (x << 1) + (x << 3).
*
* Sometimes you can do better. For example, 15 is 1111 binary, so the normal
* But, it's cheaper if you capitalize on the fact that you have a run of ones:
* 1111 = 10000 - 1, hence x * 15 = (x << 4) - (x << 0). [You would never
* actually perform the operation << 0, since it's a no-op; I'm just writing
* it that way for clarity.]
*
* The other way you can win is if you get lucky with the prime factorization
* of your constant. The number 1,000,000,000, which we have to multiply
* by below, is a good example. One billion is 111011100110101100101000000000
* in binary. If you apply the bit-grouping trick, it doesn't buy you very
* much, because it's only a win for groups of three or more equal bits:
*
* 111011100110101100101000000000 = 1000000000000000000000000000000
* - 000100011001010011011000000000
*
* This is better, but not great.
*
* However, we can factor 1,000,000,000 = 2^9 * 5^9 = 2^9 * 125 * 125 * 125,
* and multiply by each factor. Multiplication by 125 is particularly easy,
* since 128 is nearby: x * 125 = (x << 7) - x - x - x, which is just four
* operations. So, to multiply by 1,000,000,000, we perform three multipli-
* cations by 125, then << 9, a total of only 3 * 4 + 1 = 13 operations.
* This is the algorithm we actually use in both hrt2ts() and ts2hrt().
*
* Division is harder; there is no equivalent of the simple shift-add algorithm
* we used for multiplication. However, we can convert the division problem
* into a multiplication problem by pre-computing the binary representation
* of the reciprocal of the divisor. For the case of interest, we have
*
* 1 / 1,000,000,000 = 1.0001001011100000101111101000001B-30,
*
* to 32 bits of precision. (The notation B-30 means "* 2^-30", just like
* E-18 means "* 10^-18".)
*
* So, to compute x / 1,000,000,000, we just multiply x by the 32-bit
* integer 10001001011100000101111101000001, then normalize (shift) the
* result. This constant has several large bits runs, so the multiply
* is relatively cheap:
*
* 10001001011100000101111101000001 = 10001001100000000110000001000001
* - 00000000000100000000000100000000
*
* Again, you can just read the algorithm right off the bits:
*
* sec = hrt;
* sec += (hrt << 6);
* sec -= (hrt << 8);
* sec += (hrt << 13);
* sec += (hrt << 14);
* sec -= (hrt << 20);
* sec += (hrt << 23);
* sec += (hrt << 24);
* sec += (hrt << 27);
* sec += (hrt << 31);
* sec >>= (32 + 30);
*
* Voila! The only problem is, since hrt is 64 bits, we need to use 96-bit
* arithmetic to perform this calculation. That's a waste, because ultimately
* we only need the highest 32 bits of the result.
*
* The first thing we do is to realize that we don't need to use all of hrt
* in the calculation. The lowest 30 bits can contribute at most 1 to the
* quotient (2^30 / 1,000,000,000 = 1.07...), so we'll deal with them later.
* The highest 2 bits have to be zero, or hrt won't fit in a timestruc_t.
* Thus, the only bits of hrt that matter for division are bits 30..61.
* These 32 bits are just the lower-order word of (hrt >> 30). This brings
* us down from 96-bit math to 64-bit math, and our algorithm becomes:
*
* tmp = (uint32_t) (hrt >> 30);
* sec = tmp;
* sec += (tmp << 6);
* sec -= (tmp << 8);
* sec += (tmp << 13);
* sec += (tmp << 14);
* sec -= (tmp << 20);
* sec += (tmp << 23);
* sec += (tmp << 24);
* sec += (tmp << 27);
* sec += (tmp << 31);
* sec >>= 32;
*
* Next, we're going to reduce this 64-bit computation to a 32-bit
* computation. We begin by rewriting the above algorithm to use relative
* shifts instead of absolute shifts. That is, instead of computing
* tmp << 6, tmp << 8, tmp << 13, etc, we'll just shift incrementally:
* tmp <<= 6, tmp <<= 2 (== 8 - 6), tmp <<= 5 (== 13 - 8), etc:
*
* tmp = (uint32_t) (hrt >> 30);
* sec = tmp;
* tmp <<= 6; sec += tmp;
* tmp <<= 2; sec -= tmp;
* tmp <<= 5; sec += tmp;
* tmp <<= 1; sec += tmp;
* tmp <<= 6; sec -= tmp;
* tmp <<= 3; sec += tmp;
* tmp <<= 1; sec += tmp;
* tmp <<= 3; sec += tmp;
* tmp <<= 4; sec += tmp;
* sec >>= 32;
*
* Now for the final step. Instead of throwing away the low 32 bits at
* the end, we can throw them away as we go, only keeping the high 32 bits
* of the product at each step. So, for example, where we now have
*
* tmp <<= 6; sec = sec + tmp;
* we will instead have
* tmp <<= 6; sec = (sec + tmp) >> 6;
* which is equivalent to
* sec = (sec >> 6) + tmp;
*
* The final shift ("sec >>= 32") goes away.
*
* All we're really doing here is long multiplication, just like we learned in
* grade school, except that at each step, we only look at the leftmost 32
* columns. The cumulative error is, at most, the sum of all the bits we
* throw away, which is 2^-32 + 2^-31 + ... + 2^-2 + 2^-1 == 1 - 2^-32.
* Thus, the final result ("sec") is correct to +/- 1.
*
* It turns out to be important to keep "sec" positive at each step, because
* we don't want to have to explicitly extend the sign bit. Therefore,
* starting with the last line of code above, each line that would have read
* "sec = (sec >> n) - tmp" must be changed to "sec = tmp - (sec >> n)", and
* the operators (+ or -) in all previous lines must be toggled accordingly.
* Thus, we end up with:
*
* tmp = (uint32_t) (hrt >> 30);
* sec = tmp + (sec >> 6);
* sec = tmp - (tmp >> 2);
* sec = tmp - (sec >> 5);
* sec = tmp + (sec >> 1);
* sec = tmp - (sec >> 6);
* sec = tmp - (sec >> 3);
* sec = tmp + (sec >> 1);
* sec = tmp + (sec >> 3);
* sec = tmp + (sec >> 4);
*
* This yields a value for sec that is accurate to +1/-1, so we have two
* cases to deal with. The mysterious-looking "+ 7" in the code below biases
* the rounding toward zero, so that sec is always less than or equal to
* the correct value. With this modified code, sec is accurate to +0/-2, with
* the -2 case being very rare in practice. With this change, we only have to
* deal with one case (sec too small) in the cleanup code.
*
* The other modification we make is to delete the second line above
* ("sec = tmp + (sec >> 6);"), since it only has an effect when bit 31 is
* set, and the cleanup code can handle that rare case. This reduces the
* *guaranteed* accuracy of sec to +0/-3, but speeds up the common cases.
*
* Finally, we compute nsec = hrt - (sec * 1,000,000,000). nsec will always
* be positive (since sec is never too large), and will at most be equal to
* the error in sec (times 1,000,000,000) plus the low-order 30 bits of hrt.
* Thus, nsec < 3 * 1,000,000,000 + 2^30, which is less than 2^32, so we can
* safely assume that nsec fits in 32 bits. Consequently, when we compute
* sec * 1,000,000,000, we only need the low 32 bits, so we can just do 32-bit
* arithmetic and let the high-order bits fall off the end.
*
* Since nsec < 3 * 1,000,000,000 + 2^30 == 4,073,741,824, the cleanup loop:
*
* while (nsec >= NANOSEC) {
* nsec -= NANOSEC;
* sec++;
* }
*
* is guaranteed to complete in at most 4 iterations. In practice, the loop
* completes in 0 or 1 iteration over 95% of the time.
*
* On an SS2, this implementation of hrt2ts() takes 1.7 usec, versus about
* 35 usec for software division -- about 20 times faster.
*/
void
{
sec++;
}
}
/*
* Convert from timestruc_t to hrtime_t.
*
* The code below is equivalent to:
*
* hrt = tsp->tv_sec * NANOSEC + tsp->tv_nsec;
*
* but requires no integer multiply.
*/
{
return (hrt);
}
/*
* For the various 32-bit "compatibility" paths in the system.
*/
void
{
}
/*
* If this ever becomes performance critical (ha!), we can borrow the
* code from ts2hrt(), above, to multiply tv_sec by 1,000,000 and the
* straightforward (x << 10) - (x << 5) + (x << 3) to multiply tv_usec by
* 1,000. For now, we'll opt for readability (besides, the compiler does
* a passable job of optimizing constant multiplication into shifts and adds).
*/
{
}
void
{
uint32_t q, r, t;
sec++;
}
/*
* this routine is very similar to hr2ts, but requires microseconds
* instead of nanoseconds, so an interger divide by 1000 routine
* completes the conversion
*/
q = q >> 9;
r = nsec - q*1000;
}
int
{
int timecheck;
gethrestime(&now);
if (datamodel == DATAMODEL_NATIVE) {
} else {
}
if (timerspecisset(&rqtime)) {
continue;
}
if (rmtp) {
/*
* If cv_waituntil_sig() returned due to a signal, and
* there is time remaining, then set the time remaining.
* Else set time remaining to zero
*/
if (ret == 0) {
gethrestime(&now);
}
if (datamodel == DATAMODEL_NATIVE) {
} else {
}
}
if (ret == 0)
return (0);
}
/*
* Routines to convert standard UNIX time (seconds since Jan 1, 1970)
* Note: these routines require tod_lock held to protect cached state.
*/
0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 0, 0,
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
};
{
/*
* Note that tod_set_prev() assumes utc will be set to zero in
* the case of it being negative. Consequently, any change made
* to this behavior would have to be reflected in that function
* as well.
*/
if (utc < 0) /* should never happen */
utc = 0;
return (saved_tod); /* only the seconds changed */
do {
year--;
} while (day < 0);
month++;
return (tod);
}
{
#ifdef DEBUG
/* only warn once, not each time called */
#endif
#ifdef DEBUG
"The hardware real-time clock appears to have the "
"wrong years value %d -- time needs to be reset\n",
year);
year_warn = 0;
}
"The hardware real-time clock appears to have the "
"wrong months value %d -- time needs to be reset\n",
month_warn = 0;
}
"The hardware real-time clock appears to have the "
"wrong days value %d -- time needs to be reset\n",
day_warn = 0;
}
"The hardware real-time clock appears to have the "
"wrong hours value %d -- time needs to be reset\n",
hour_warn = 0;
}
"The hardware real-time clock appears to have the "
"wrong minutes value %d -- time needs to be reset\n",
min_warn = 0;
}
"The hardware real-time clock appears to have the "
"wrong seconds value %d -- time needs to be reset\n",
sec_warn = 0;
}
#endif
return (utc);
}