xref: /illumos-gate/usr/src/uts/common/sys/timer.h (revision 440a8a36)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
534709573Sraf  * Common Development and Distribution License (the "License").
634709573Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2134709573Sraf 
227c478bd9Sstevel@tonic-gate /*
23e0cf54a5SRoger A. Faulkner  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
276a72db4aSBryan Cantrill /*
28*440a8a36SPatrick Mooney  * Copyright 2020 Joyent, Inc.
296a72db4aSBryan Cantrill  */
306a72db4aSBryan Cantrill 
317c478bd9Sstevel@tonic-gate #ifndef	_SYS_TIMER_H
327c478bd9Sstevel@tonic-gate #define	_SYS_TIMER_H
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/proc.h>
367c478bd9Sstevel@tonic-gate #include <sys/thread.h>
37*440a8a36SPatrick Mooney #include <sys/param.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
407c478bd9Sstevel@tonic-gate extern "C" {
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
447c478bd9Sstevel@tonic-gate 
4534709573Sraf #define	_TIMER_MAX	32
46*440a8a36SPatrick Mooney /*
47*440a8a36SPatrick Mooney  * Max timers per process.  This is patchable via /etc/system and can be
48*440a8a36SPatrick Mooney  * updated via kmdb.  Sticking to positive powers of 2 is recommended.
49*440a8a36SPatrick Mooney  * The default value is 4 * NCPU. Setting timer_max to a value below the
50*440a8a36SPatrick Mooney  * default via /etc/system is ignored.
51*440a8a36SPatrick Mooney  */
52*440a8a36SPatrick Mooney extern	int	timer_max;
53*440a8a36SPatrick Mooney 
54*440a8a36SPatrick Mooney #define	_TIMER_ALLOC_INIT	8	/* initial size for p_itimer array */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * Bit values for the it_lock field.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate #define	ITLK_LOCKED		0x01
607c478bd9Sstevel@tonic-gate #define	ITLK_WANTED		0x02
617c478bd9Sstevel@tonic-gate #define	ITLK_REMOVE		0x04
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Bit values for the it_flags field.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate #define	IT_SIGNAL		0x01
677c478bd9Sstevel@tonic-gate #define	IT_PORT			0x02	/* use event port notification */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate struct clock_backend;
707c478bd9Sstevel@tonic-gate 
716a72db4aSBryan Cantrill struct itimer;
726a72db4aSBryan Cantrill typedef struct itimer itimer_t;
736a72db4aSBryan Cantrill 
746a72db4aSBryan Cantrill struct itimer {
757c478bd9Sstevel@tonic-gate 	itimerspec_t	it_itime;
767c478bd9Sstevel@tonic-gate 	hrtime_t	it_hrtime;
777c478bd9Sstevel@tonic-gate 	ushort_t	it_flags;
787c478bd9Sstevel@tonic-gate 	ushort_t	it_lock;
796a72db4aSBryan Cantrill 	void		*it_arg;	/* clock backend-specific data */
807c478bd9Sstevel@tonic-gate 	struct proc	*it_proc;
816a72db4aSBryan Cantrill 	union {
826a72db4aSBryan Cantrill 		struct {
836a72db4aSBryan Cantrill 			sigqueue_t	*__it_sigq;
846a72db4aSBryan Cantrill 			klwp_t		*__it_lwp;
856a72db4aSBryan Cantrill 		} __proc;
866a72db4aSBryan Cantrill 		void *__it_frontend;
876a72db4aSBryan Cantrill 	} __data;			/* timer frontend-specific data */
887c478bd9Sstevel@tonic-gate 	kcondvar_t	it_cv;
897c478bd9Sstevel@tonic-gate 	int		it_blockers;
907c478bd9Sstevel@tonic-gate 	int		it_pending;
917c478bd9Sstevel@tonic-gate 	int		it_overrun;
927c478bd9Sstevel@tonic-gate 	struct clock_backend *it_backend;
936a72db4aSBryan Cantrill 	void		(*it_fire)(itimer_t *);
947c478bd9Sstevel@tonic-gate 	kmutex_t	it_mutex;
957c478bd9Sstevel@tonic-gate 	void		*it_portev;	/* port_kevent_t pointer */
967c478bd9Sstevel@tonic-gate 	void		*it_portsrc;	/* port_source_t pointer */
977c478bd9Sstevel@tonic-gate 	int		it_portfd;	/* port file descriptor */
986a72db4aSBryan Cantrill };
996a72db4aSBryan Cantrill 
1006a72db4aSBryan Cantrill #define	it_sigq		__data.__proc.__it_sigq
1016a72db4aSBryan Cantrill #define	it_lwp		__data.__proc.__it_lwp
1026a72db4aSBryan Cantrill #define	it_frontend	__data.__it_frontend
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate typedef struct clock_backend {
1057c478bd9Sstevel@tonic-gate 	struct sigevent clk_default;
1067c478bd9Sstevel@tonic-gate 	int (*clk_clock_settime)(timespec_t *);
1077c478bd9Sstevel@tonic-gate 	int (*clk_clock_gettime)(timespec_t *);
1087c478bd9Sstevel@tonic-gate 	int (*clk_clock_getres)(timespec_t *);
1096a72db4aSBryan Cantrill 	int (*clk_timer_create)(itimer_t *, void (*)(itimer_t *));
1107c478bd9Sstevel@tonic-gate 	int (*clk_timer_settime)(itimer_t *, int, const struct itimerspec *);
1117c478bd9Sstevel@tonic-gate 	int (*clk_timer_gettime)(itimer_t *, struct itimerspec *);
1127c478bd9Sstevel@tonic-gate 	int (*clk_timer_delete)(itimer_t *);
1137c478bd9Sstevel@tonic-gate 	void (*clk_timer_lwpbind)(itimer_t *);
1147c478bd9Sstevel@tonic-gate } clock_backend_t;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate extern void clock_add_backend(clockid_t clock, clock_backend_t *backend);
1176a72db4aSBryan Cantrill extern clock_backend_t *clock_get_backend(clockid_t clock);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate extern void timer_lwpbind();
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate extern	void	timer_func(sigqueue_t *);
1227c478bd9Sstevel@tonic-gate extern	void	timer_exit(void);
1237c478bd9Sstevel@tonic-gate extern	void	timer_lwpexit(void);
1247c478bd9Sstevel@tonic-gate extern	clock_t	hzto(struct timeval *);
1257c478bd9Sstevel@tonic-gate extern	clock_t	timespectohz(timespec_t *, timespec_t);
126f635d46aSqiao extern	int64_t	timespectohz64(timespec_t *);
1277c478bd9Sstevel@tonic-gate extern	int	itimerspecfix(timespec_t *);
1287c478bd9Sstevel@tonic-gate extern	void	timespecadd(timespec_t *, timespec_t *);
1297c478bd9Sstevel@tonic-gate extern	void	timespecsub(timespec_t *, timespec_t *);
1307c478bd9Sstevel@tonic-gate extern	void	timespecfix(timespec_t *);
1317c478bd9Sstevel@tonic-gate extern	int	xgetitimer(uint_t, struct itimerval *, int);
1327c478bd9Sstevel@tonic-gate extern	int	xsetitimer(uint_t, struct itimerval *, int);
133e0cf54a5SRoger A. Faulkner extern	void	delete_itimer_realprof(void);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	timerspecisset(tvp)		((tvp)->tv_sec || (tvp)->tv_nsec)
1367c478bd9Sstevel@tonic-gate #define	timerspeccmp(tvp, uvp)		(((tvp)->tv_sec - (uvp)->tv_sec) ? \
1377c478bd9Sstevel@tonic-gate 	((tvp)->tv_sec - (uvp)->tv_sec):((tvp)->tv_nsec - (uvp)->tv_nsec))
1387c478bd9Sstevel@tonic-gate #define	timerspecclear(tvp)		((tvp)->tv_sec = (tvp)->tv_nsec = 0)
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate struct oldsigevent {
1417c478bd9Sstevel@tonic-gate 	/* structure definition prior to notification attributes member */
1427c478bd9Sstevel@tonic-gate 	int		_notify;
1437c478bd9Sstevel@tonic-gate 	union {
1447c478bd9Sstevel@tonic-gate 		int		_signo;
1457c478bd9Sstevel@tonic-gate 		void		(*_notify_function)(union sigval);
1467c478bd9Sstevel@tonic-gate 	} _un;
1477c478bd9Sstevel@tonic-gate 	union sigval	_value;
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32)
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate struct oldsigevent32 {
1537c478bd9Sstevel@tonic-gate 	int32_t		_notify;
1547c478bd9Sstevel@tonic-gate 	union {
1557c478bd9Sstevel@tonic-gate 		int32_t		_signo;
1567c478bd9Sstevel@tonic-gate 		caddr32_t	_notify_function;
1577c478bd9Sstevel@tonic-gate 	} _un;
1587c478bd9Sstevel@tonic-gate 	union sigval32	_value;
1597c478bd9Sstevel@tonic-gate };
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32 */
1627c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #endif	/* _SYS_TIMER_H */
169