/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
*/
#include <sys/port_impl.h>
#include <sys/port_kernel.h>
static int timer_port_callback(void *, int *, pid_t, int, void *);
static void timer_close_port(void *, int, pid_t, int);
/*
* Tunable to increase the maximum number of POSIX timers per-process. This
* _cannot_ be tuned on a running system.
*/
/*
* timer_lock() locks the specified interval timer. It doesn't look at the
* ITLK_REMOVE bit; it's up to callers to look at this if they need to
* care. p_lock must be held on entry; it may be dropped and reaquired,
* but timer_lock() will always return with p_lock held.
*
* Note that timer_create() doesn't call timer_lock(); it creates timers
* with the ITLK_LOCKED bit explictly set.
*/
static void
{
it->it_blockers++;
it->it_blockers--;
}
}
/*
* timer_unlock() unlocks the specified interval timer, waking up any
* waiters. p_lock must be held on entry; it will not be dropped by
* timer_unlock().
*/
static void
{
}
/*
* timer_delete_locked() takes a proc pointer, timer ID and locked interval
* timer, and deletes the specified timer. It must be called with p_lock
* held, and cannot be called on a timer which already has ITLK_REMOVE set;
* the caller must check this. timer_delete_locked() will set the ITLK_REMOVE
* bit and will iteratively unlock and lock the interval timer until all
* blockers have seen the ITLK_REMOVE and cleared out. It will then zero
* out the specified entry in the p_itimer array, and call into the clock
* backend to complete the deletion.
*
* This function will always return with p_lock held.
*/
static void
{
/*
* If there are threads waiting to lock this timer, we'll unlock
* the timer, and block on the cv. Threads blocking our removal will
* have the opportunity to run; when they see the ITLK_REMOVE flag
* set, they will immediately unlock the timer.
*/
while (it->it_blockers) {
timer_unlock(p, it);
timer_lock(p, it);
}
/*
* No one is blocked on this timer, and no one will be (we've set
* p_itimer[tid] to be NULL; no one can find it). Now we call into
* the clock backend to delete the timer; it is up to the backend to
* guarantee that timer_fire() has completed (and will never again
* be called) for this timer.
*/
mutex_exit(&p->p_lock);
/* dissociate timer from the event port */
it->it_pending = 0;
(void) port_remove_done_event(pev);
} else {
}
}
mutex_enter(&p->p_lock);
/*
* We need to be careful freeing the sigqueue for this timer;
* if a signal is pending, the sigqueue needs to be freed
* synchronously in siginfofree(). The need to free the sigqueue
* in siginfofree() is indicated by setting sq_func to NULL.
*/
if (it->it_pending > 0) {
} else {
}
}
/*
* timer_grab() and its companion routine, timer_release(), are wrappers
* around timer_lock()/_unlock() which allow the timer_*(3R) routines to
* (a) share error handling code and (b) not grab p_lock themselves. Routines
* which are called with p_lock held (e.g. timer_lwpbind(), timer_lwpexit())
* must call timer_lock()/_unlock() explictly.
*
* timer_grab() takes a proc and a timer ID, and returns a pointer to a
* locked interval timer. p_lock must _not_ be held on entry; timer_grab()
* may acquire p_lock, but will always return with p_lock dropped.
*
* If timer_grab() fails, it will return NULL. timer_grab() will fail if
* one or more of the following is true:
*
* (a) The specified timer ID is out of range.
*
* (b) The specified timer ID does not correspond to a timer ID returned
* from timer_create(3R).
*
* (c) The specified timer ID is currently being removed.
*
*/
static itimer_t *
{
return (NULL);
mutex_enter(&p->p_lock);
mutex_exit(&p->p_lock);
return (NULL);
}
timer_lock(p, it);
/*
* Someone is removing this timer; it will soon be invalid.
*/
timer_unlock(p, it);
mutex_exit(&p->p_lock);
return (NULL);
}
mutex_exit(&p->p_lock);
return (it);
}
/*
* timer_release() releases a timer acquired with timer_grab(). p_lock
* should not be held on entry; timer_release() will acquire p_lock but
* will drop it before returning.
*/
static void
{
mutex_enter(&p->p_lock);
timer_unlock(p, it);
mutex_exit(&p->p_lock);
}
/*
* timer_delete_grabbed() deletes a timer acquired with timer_grab().
* p_lock should not be held on entry; timer_delete_grabbed() will acquire
* p_lock, but will drop it before returning.
*/
static void
{
mutex_enter(&p->p_lock);
mutex_exit(&p->p_lock);
}
void
{
}
void
{
}
{
return (NULL);
return (clock_backend[clock]);
}
int
{
timespec_t t;
int error;
if (secpolicy_settime(CRED()) != 0)
if (get_udatamodel() == DATAMODEL_NATIVE) {
} else {
TIMESPEC32_TO_TIMESPEC(&t, &t32);
}
if (itimerspecfix(&t))
if (error)
return (0);
}
int
{
timespec_t t;
int error;
if (error)
if (get_udatamodel() == DATAMODEL_NATIVE) {
} else {
if (TIMESPEC_OVERFLOW(&t))
TIMESPEC_TO_TIMESPEC32(&t32, &t);
}
return (0);
}
int
{
timespec_t t;
int error;
/*
* Strangely, the standard defines clock_getres() with a NULL tp
* to do nothing (regardless of the validity of the specified
* clock_id). Go figure.
*/
return (0);
if (error)
if (get_udatamodel() == DATAMODEL_NATIVE) {
} else {
if (TIMESPEC_OVERFLOW(&t))
TIMESPEC_TO_TIMESPEC32(&t32, &t);
}
return (0);
}
void
{
/*
* There are some conditions during a fork or an exit when we can
* call siginfofree() without p_lock held. To prevent a race
* between timer_signal() and timer_fire() with regard to it_pending,
* we therefore acquire it_mutex in both paths.
*/
it->it_pending = 0;
}
/*
* This routine is called from the clock backend.
*/
static void
{
proc_t *p;
int proc_lock_held;
/*
* See the comment in timer_signal() for why it is not
* sufficient to only grab p_lock here. Because p_lock can be
* held on entry to timer_signal(), the lock ordering is
* necessarily p_lock before it_mutex.
*/
proc_lock_held = 1;
mutex_enter(&p->p_lock);
} else {
/*
* IT_PORT:
* If a timer was ever programmed to send events to a port,
* the IT_PORT flag will remain set until:
* a) the timer is deleted (see timer_delete_locked()) or
* b) the port is being closed (see timer_close_port()).
* Both cases are synchronized with the it_mutex.
* We don't need to use the p_lock because it is only
* required in the IT_SIGNAL case.
* If IT_PORT was set and the port is being closed then
* the timer notification is set to NONE. In such a case
* the timer itself and the it_pending counter remain active
* until the application deletes the counter or the process
* exits.
*/
proc_lock_held = 0;
}
if (it->it_pending > 0) {
it->it_pending++;
} else {
} else {
}
}
if (proc_lock_held)
mutex_exit(&p->p_lock);
}
int
{
int error = 0;
timer_t i;
/*
* short copyin() for binary compatibility
* fetch oldsigevent to determine how much to copy in.
*/
if (get_udatamodel() == DATAMODEL_NATIVE) {
sizeof (port_notify_t)))
}
#ifdef _SYSCALL32_IMPL
} else {
/*
* See comment in sigqueue32() on handling of 32-bit
* sigvals in a 64-bit kernel.
*/
(void *)&tim_pnevp32,
sizeof (port_notify32_t)))
}
#endif
}
switch (ev.sigev_notify) {
case SIGEV_NONE:
break;
case SIGEV_SIGNAL:
break;
case SIGEV_THREAD:
case SIGEV_PORT:
break;
default:
}
} else {
/*
* Use the clock's default sigevent (this is a structure copy).
*/
}
/*
* We'll allocate our timer and sigqueue now, before we grab p_lock.
* If we can't find an empty slot, we'll free them before returning.
*/
mutex_enter(&p->p_lock);
/*
* If this is this process' first timer, we need to attempt to allocate
* an array of timerstr_t pointers. We drop p_lock to perform the
* allocation; if we return to discover that p_itimer is non-NULL,
* we will free our allocation and drive on.
*/
mutex_exit(&p->p_lock);
mutex_enter(&p->p_lock);
else {
}
}
continue;
if (i == timer_max) {
/*
* We couldn't find a slot. Drop p_lock, free the preallocated
* timer and sigqueue, and return an error.
*/
mutex_exit(&p->p_lock);
}
/*
* If we develop other notification mechanisms, this will need
* to call into (yet another) backend.
*/
else
int port;
/*
* This timer is programmed to use event port notification when
* the timer fires:
* - allocate a port event structure and prepare it to be sent
* to the port as soon as the timer fires.
* - when the timer fires :
* - if event structure was already sent to the port then this
* is a timer fire overflow => increment overflow counter.
* - otherwise send pre-allocated event structure to the port.
* - the events field of the port_event_t structure counts the
* number of timer fired events.
* - The event structured is allocated using the
* PORT_ALLOC_CACHED flag.
* This flag indicates that the timer itself will manage and
* free the event structure when required.
*/
/* associate timer as event source with the port */
if (error) {
mutex_exit(&p->p_lock);
}
if (error) {
mutex_exit(&p->p_lock);
}
/* initialize event data */
} else {
}
mutex_exit(&p->p_lock);
/*
* Call on the backend to verify the event argument (or return
* EINVAL if this clock type does not support timers).
*/
goto err;
goto err;
}
/*
* If we're here, then we have successfully created the timer; we
* just need to release the timer and return.
*/
timer_release(p, it);
return (0);
err:
/*
* If we're here, an error has occurred late in the timer creation
* process. We need to regrab p_lock, and delete the incipient timer.
* Since we never unlocked the timer (it was born locked), it's
* impossible for a removal to be pending.
*/
timer_delete_grabbed(p, i, it);
}
int
{
int error;
timer_release(p, it);
if (error == 0) {
if (get_udatamodel() == DATAMODEL_NATIVE) {
} else {
if (ITIMERSPEC_OVERFLOW(&when))
else {
}
}
}
}
int
{
int error;
return (error);
}
if (get_udatamodel() == DATAMODEL_NATIVE) {
} else {
}
}
timer_release(p, it);
}
int
{
return (0);
}
int
{
int overrun;
/*
* The it_overrun field is protected by p_lock; we need to acquire
* it before looking at the value.
*/
mutex_enter(&p->p_lock);
mutex_exit(&p->p_lock);
timer_release(p, it);
return (overrun);
}
/*
*/
void
timer_lwpexit(void)
{
timer_t i;
return;
for (i = 0; i < timer_max; i++) {
continue;
timer_lock(p, it);
/*
* This timer is either being removed or it isn't
* associated with this lwp.
*/
timer_unlock(p, it);
continue;
}
/*
* The LWP that created this timer is going away. To the user,
* our behavior here is explicitly undefined. We will simply
* null out the it_lwp field; if the LWP was bound to a CPU,
* the cyclic will stay bound to that CPU until the process
* exits.
*/
timer_unlock(p, it);
}
}
/*
* held, but will repeatedly drop and regrab p_lock.
*/
void
{
timer_t i;
return;
for (i = 0; i < timer_max; i++) {
continue;
timer_lock(p, it);
/*
* Drop p_lock and jump into the backend.
*/
mutex_exit(&p->p_lock);
mutex_enter(&p->p_lock);
}
timer_unlock(p, it);
}
}
/*
* This function should only be called if p_itimer is non-NULL.
*/
void
timer_exit(void)
{
timer_t i;
for (i = 0; i < timer_max; i++)
(void) timer_delete(i);
}
/*
* timer_port_callback() is a callback function which is associated with the
* timer event and is activated just before the event is delivered to the user.
* to reenable the use of the event structure.
*/
/* ARGSUSED */
static int
{
/* can not deliver timer events to another proc */
return (EACCES);
}
/*
* This function can also be activated when the port is being closed
* and a timer event is already submitted to the port.
* In such a case the event port framework will use the
* close-callback function to notify the events sources.
* The timer close-callback function is timer_close_port() which
* will free all allocated resources (including the allocated
* port event structure).
* For that reason we don't need to check the value of flag here.
*/
return (0);
}
/*
* port is being closed ... free all allocated port event structures
* The delivered arg currently correspond to the first timer associated with
* the port and it is not useable in this case.
* We have to scan the list of activated timers in the current proc and
* compare them with the delivered port id.
*/
/* ARGSUSED */
static void
{
continue;
(void) port_remove_done_event(pev);
} else {
}
}
timer_release(p, it);
}
}