xref: /illumos-gate/usr/src/uts/common/sys/timer.h (revision b8767451d156f585534afac0bf22721810d0dc63)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright 2020 Joyent, Inc.
29  */
30 
31 #ifndef	_SYS_TIMER_H
32 #define	_SYS_TIMER_H
33 
34 #include <sys/types.h>
35 #include <sys/proc.h>
36 #include <sys/thread.h>
37 #include <sys/param.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 #ifdef	_KERNEL
44 
45 #define	_TIMER_MAX	32
46 /*
47  * Max timers per process.  This is patchable via /etc/system and can be
48  * updated via kmdb.  Sticking to positive powers of 2 is recommended.
49  * The default value is 4 * NCPU. Setting timer_max to a value below the
50  * default via /etc/system is ignored.
51  */
52 extern	int	timer_max;
53 
54 #define	_TIMER_ALLOC_INIT	8	/* initial size for p_itimer array */
55 
56 /*
57  * Bit values for the it_lock field.
58  */
59 #define	ITLK_LOCKED		0x01
60 #define	ITLK_WANTED		0x02
61 #define	ITLK_REMOVE		0x04
62 
63 /*
64  * Bit values for the it_flags field.
65  */
66 #define	IT_SIGNAL		0x01
67 #define	IT_PORT			0x02	/* use event port notification */
68 
69 struct clock_backend;
70 
71 struct itimer;
72 typedef struct itimer itimer_t;
73 
74 struct itimer {
75 	itimerspec_t	it_itime;
76 	hrtime_t	it_hrtime;
77 	ushort_t	it_flags;
78 	ushort_t	it_lock;
79 	void		*it_arg;	/* clock backend-specific data */
80 	struct proc	*it_proc;
81 	union {
82 		struct {
83 			sigqueue_t	*__it_sigq;
84 			klwp_t		*__it_lwp;
85 		} __proc;
86 		void *__it_frontend;
87 	} __data;			/* timer frontend-specific data */
88 	kcondvar_t	it_cv;
89 	int		it_blockers;
90 	int		it_pending;
91 	int		it_overrun;
92 	struct clock_backend *it_backend;
93 	void		(*it_fire)(itimer_t *);
94 	kmutex_t	it_mutex;
95 	void		*it_portev;	/* port_kevent_t pointer */
96 	void		*it_portsrc;	/* port_source_t pointer */
97 	int		it_portfd;	/* port file descriptor */
98 };
99 
100 #define	it_sigq		__data.__proc.__it_sigq
101 #define	it_lwp		__data.__proc.__it_lwp
102 #define	it_frontend	__data.__it_frontend
103 
104 typedef struct clock_backend {
105 	struct sigevent clk_default;
106 	int (*clk_clock_settime)(timespec_t *);
107 	int (*clk_clock_gettime)(timespec_t *);
108 	int (*clk_clock_getres)(timespec_t *);
109 	int (*clk_timer_create)(itimer_t *, void (*)(itimer_t *));
110 	int (*clk_timer_settime)(itimer_t *, int, const struct itimerspec *);
111 	int (*clk_timer_gettime)(itimer_t *, struct itimerspec *);
112 	int (*clk_timer_delete)(itimer_t *);
113 	void (*clk_timer_lwpbind)(itimer_t *);
114 } clock_backend_t;
115 
116 extern void clock_add_backend(clockid_t clock, clock_backend_t *backend);
117 extern clock_backend_t *clock_get_backend(clockid_t clock);
118 
119 extern void timer_lwpbind();
120 
121 extern	void	timer_func(sigqueue_t *);
122 extern	void	timer_exit(void);
123 extern	void	timer_lwpexit(void);
124 extern	clock_t	hzto(struct timeval *);
125 extern	clock_t	timespectohz(timespec_t *, timespec_t);
126 extern	int64_t	timespectohz64(timespec_t *);
127 extern	int	itimerspecfix(timespec_t *);
128 extern	void	timespecadd(timespec_t *, timespec_t *);
129 extern	void	timespecsub(timespec_t *, timespec_t *);
130 extern	void	timespecfix(timespec_t *);
131 extern	int	xgetitimer(uint_t, struct itimerval *, int);
132 extern	int	xsetitimer(uint_t, struct itimerval *, int);
133 extern	void	delete_itimer_realprof(void);
134 
135 #define	timerspecisset(tvp)		((tvp)->tv_sec || (tvp)->tv_nsec)
136 #define	timerspeccmp(tvp, uvp)		(((tvp)->tv_sec - (uvp)->tv_sec) ? \
137 	((tvp)->tv_sec - (uvp)->tv_sec):((tvp)->tv_nsec - (uvp)->tv_nsec))
138 #define	timerspecclear(tvp)		((tvp)->tv_sec = (tvp)->tv_nsec = 0)
139 
140 struct oldsigevent {
141 	/* structure definition prior to notification attributes member */
142 	int		_notify;
143 	union {
144 		int		_signo;
145 		void		(*_notify_function)(union sigval);
146 	} _un;
147 	union sigval	_value;
148 };
149 
150 #if defined(_SYSCALL32)
151 
152 struct oldsigevent32 {
153 	int32_t		_notify;
154 	union {
155 		int32_t		_signo;
156 		caddr32_t	_notify_function;
157 	} _un;
158 	union sigval32	_value;
159 };
160 
161 #endif	/* _SYSCALL32 */
162 #endif	/* _KERNEL */
163 
164 #ifdef	__cplusplus
165 }
166 #endif
167 
168 #endif	/* _SYS_TIMER_H */
169