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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright 2022 Oxide Computer Company
26  */
27 
28 #ifndef	_SYS_PORT_KERNEL_H
29 #define	_SYS_PORT_KERNEL_H
30 
31 #include <sys/vnode.h>
32 #include <sys/list.h>
33 
34 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 /*
39  * Note:
40  * The contents of this file are private to the implementation of the
41  * Solaris system and event ports subsystem and are subject to change
42  * at any time without notice.
43  */
44 
45 #ifdef _KERNEL
46 
47 /*
48  * The port_kevent_t struct represents the kernel internal port event.
49  * Every event is associated to a port (portkev_port).
50  */
51 typedef	struct	port_kevent {
52 	kmutex_t	portkev_lock;	/* used by PORT_SOURCE_FD source */
53 	int	portkev_source;		/* event: source */
54 	int	portkev_events;		/* event: data */
55 	int	portkev_flags;		/* internal flags */
56 	pid_t	portkev_pid;		/* pid of process using this struct */
57 	long	portkev_object;		/* event: object */
58 	void	*portkev_user;		/* event: user-defined value */
59 	int	(*portkev_callback)(void *, int *, pid_t, int, void *);
60 	void	*portkev_arg;		/* event source callback arg */
61 	struct	port *portkev_port;	/* associated port */
62 	list_node_t portkev_node;	/* pointer to neighbor events */
63 } port_kevent_t;
64 
65 /* portkev_flags */
66 #define	PORT_KEV_PRIVATE	0x01	/* subsystem private, don't free */
67 #define	PORT_KEV_CACHED		0x02	/* port local cached, don't free */
68 #define	PORT_KEV_SCACHED	0x04	/* source local cached, don't free */
69 #define	PORT_KEV_VALID		0x08	/* event associated and enabled */
70 #define	PORT_KEV_DONEQ		0x10	/* event is in done queue */
71 #define	PORT_KEV_FREE		0x20	/* free event and don't copyout it */
72 #define	PORT_KEV_NOSHARE	0x40	/* non-shareable across processes */
73 
74 /* flags : port_alloc_event() */
75 #define	PORT_ALLOC_DEFAULT	0
76 #define	PORT_ALLOC_PRIVATE	PORT_KEV_PRIVATE
77 #define	PORT_ALLOC_CACHED	PORT_KEV_CACHED
78 #define	PORT_ALLOC_SCACHED	PORT_KEV_SCACHED
79 
80 /* flags : callback function */
81 #define	PORT_CALLBACK_DEFAULT	0	/* free resources, event delivery */
82 #define	PORT_CALLBACK_CLOSE	1	/* free resources, don't copyout */
83 #define	PORT_CALLBACK_DISSOCIATE 2	/* dissociate object */
84 
85 #define	PORT_DEFAULT_PORTS	0x02000
86 #define	PORT_MAX_PORTS		0x10000
87 #define	PORT_DEFAULT_EVENTS	0x10000	/* default # of events per port */
88 #define	PORT_MAX_EVENTS		UINT_MAX/2 /* max. # of events per port */
89 
90 /*
91  * port_source_t represents a source associated with a port.
92  * The portsrc_close() function is required to notify the source when
93  * a port is closed.
94  */
95 typedef struct port_source {
96 	int	portsrc_source;
97 	int	portsrc_cnt;		/* # of associations */
98 	void	(*portsrc_close)(void *, int, pid_t, int);
99 	void	*portsrc_closearg;	/* callback arg */
100 	void	*portsrc_data;		/* Private data of source */
101 	struct port_source *portsrc_next;
102 	struct port_source *portsrc_prev;
103 } port_source_t;
104 
105 
106 /*
107  * PORT_SOURCE_FILE cache structure.
108  */
109 #define	PORTFOP_HASHSIZE	256	/* cache space for fop events */
110 
111 /*
112  * One cache for each port that uses PORT_SOURCE_FILE.
113  */
114 typedef struct portfop_cache {
115 	kmutex_t	pfc_lock;	/* lock to protect cache */
116 	kcondvar_t	pfc_lclosecv;	/* last close cv */
117 	int		pfc_objcount;	/* track how many file obj are hashed */
118 	struct portfop	*pfc_hash[PORTFOP_HASHSIZE]; /* hash table */
119 } portfop_cache_t;
120 
121 /*
122  * PORT_SOURCE_FD cache per port.
123  * One cache for each port that uses PORT_SOURCE_FD.
124  *
125  * The types and offsets of pc_lock and pc_flag must exactly match their sibling
126  * fields in pollcache_t, as they are accessed as if the port_fdcache_t _was_ a
127  * pollcache via t_pollcache. (See: pollrelock() and fs_reject_epoll())
128  */
129 typedef struct port_fdcache {
130 	kmutex_t	pc_lock;	/* lock to protect portcache */
131 	kcondvar_t	pc_lclosecv;
132 	struct portfd	**pc_hash;	/* points to a hash table of ptrs */
133 	int		pc_hashsize;	/* the size of current hash table */
134 	int		pc_fdcount;	/* track how many fd's are hashed */
135 	uintptr_t	_pc_pad;	/* pad to properly offset pc_flag */
136 	int		pc_flag;	/* pollcache flags (compat) */
137 } port_fdcache_t;
138 
139 /*
140  * Structure of port_ksource_tab[] table.
141  * The port_ksource_tab[] is required to allow kernel sources to become
142  * associated with a port at the time of port creation. This feature is
143  * required to avoid performance degradation in sub-systems, specially when
144  * they should need to check the association on every event activity.
145  */
146 typedef	struct	port_ksource {
147 	int	pks_source;
148 	void	(*pks_close)(void *, int, pid_t, int);
149 	void	*pks_closearg;
150 	void	*pks_portsrc;
151 } port_ksource_t;
152 
153 /* event port and source management */
154 int	port_associate_ksource(int, int, struct port_source **,
155     void (*)(void *, int, pid_t, int), void *arg,
156     int (*)(port_kevent_t *, int, int, uintptr_t, void *));
157 int	port_dissociate_ksource(int, int, struct port_source *);
158 
159 /* event management */
160 int	port_alloc_event(int, int, int, port_kevent_t **);
161 int	port_pollwkup(struct port *);
162 void	port_pollwkdone(struct port *);
163 void	port_send_event(port_kevent_t *);
164 void	port_free_event(port_kevent_t *);
165 void	port_init_event(port_kevent_t *, uintptr_t, void *,
166     int (*)(void *, int *, pid_t, int, void *), void *);
167 int	port_dup_event(port_kevent_t *, port_kevent_t **, int);
168 int	port_associate_fd(struct port *, int, uintptr_t, int, void *);
169 int	port_dissociate_fd(struct port *, uintptr_t);
170 int	port_associate_fop(struct port *, int, uintptr_t, int, void *);
171 int	port_dissociate_fop(struct port *, uintptr_t);
172 
173 /* misc functions */
174 void	port_free_event_local(port_kevent_t *, int counter);
175 int	port_alloc_event_local(struct port *, int, int, port_kevent_t **);
176 void	port_close_pfd(struct portfd *);
177 
178 #endif	/* _KERNEL */
179 
180 #ifdef	__cplusplus
181 }
182 #endif
183 
184 #endif	/* _SYS_PORT_KERNEL_H */
185