xref: /dragonfly/sys/sys/msgport.h (revision cfd1aba3)
1 /*
2  * SYS/MSGPORT.H
3  *
4  *	Implements LWKT messages and ports.
5  */
6 
7 #ifndef _SYS_MSGPORT_H_
8 #define _SYS_MSGPORT_H_
9 
10 #ifndef _SYS_QUEUE_H_
11 #include <sys/queue.h>		/* TAILQ_* macros */
12 #endif
13 #ifndef _SYS_STDINT_H_
14 #include <sys/stdint.h>
15 #endif
16 #ifndef _SYS_SPINLOCK_H_
17 #include <sys/spinlock.h>
18 #endif
19 
20 #ifdef _KERNEL
21 
22 #ifndef _SYS_MALLOC_H_
23 #include <sys/malloc.h>
24 #endif
25 
26 #endif
27 
28 struct lwkt_msg;
29 struct lwkt_port;
30 struct lwkt_serialize;
31 struct thread;
32 
33 typedef struct lwkt_msg		*lwkt_msg_t;
34 typedef struct lwkt_port	*lwkt_port_t;
35 
36 typedef TAILQ_HEAD(lwkt_msg_queue, lwkt_msg) lwkt_msg_queue;
37 
38 /*
39  * The standard message and port structure for communications between
40  * threads.  See kern/lwkt_msgport.c for documentation on how messages and
41  * ports work.
42  *
43  * A message may only be manipulated by whomever currently owns it,
44  * which generally means the originating port if the message has
45  * not been sent yet or has been replied, and the target port if the message
46  * has been sent and/or is undergoing processing.
47  *
48  * NOTE! 64-bit-align this structure.
49  */
50 typedef struct lwkt_msg {
51     TAILQ_ENTRY(lwkt_msg) ms_node;	/* link node */
52     lwkt_port_t ms_target_port;		/* current target or relay port */
53     lwkt_port_t	ms_reply_port;		/* async replies returned here */
54     void	(*ms_abortfn)(struct lwkt_msg *);
55     int		ms_flags;		/* message flags */
56     int		ms_error;		/* positive error code or 0 */
57     union {
58 	void	*ms_resultp;		/* misc pointer data or result */
59 	int	ms_result;		/* standard 'int'eger result */
60 	long	ms_lresult;		/* long result */
61 	int	ms_fds[2];		/* two int bit results */
62 	__int32_t ms_result32;		/* 32 bit result */
63 	__int64_t ms_result64;		/* 64 bit result */
64 	__off_t	ms_offset;		/* off_t result */
65     } u;
66     int		ms_pad[2];		/* future use */
67 } lwkt_msg;
68 
69 /*
70  * Message state flags are manipulated by the current owner only.
71  *
72  * DONE		Indicates completion of the reply.  This flag is also set
73  *		for unsent messages.
74  *
75  * REPLY	Indicates message is being replied but may or may not
76  *		have been queued or returned yet.  This bit is left set
77  *		when a message is retrieved from a reply port so the caller
78  *		can distinguish between requests and replies.
79  *
80  * QUEUED	Indicates message is queued on reply or target port, or
81  *		some other port.
82  *
83  * SYNC		Indicates that the originator is blocked directly on the
84  *		message and that the message should be signaled on
85  *		completion instead of queued.
86  *
87  * INTRANSIT	Indicates that the message state is indeterminant (e.g.
88  *		being passed through an IPI).
89  *
90  * ABORTABLE	Static flag indicates that ms_abortfn is valid.
91  *
92  * High 16 bits are available to message handlers.
93  */
94 #define MSGF_DONE	0x0001		/* message is complete */
95 #define MSGF_REPLY	0x0002		/* asynch message has been returned */
96 #define MSGF_QUEUED	0x0004		/* message has been queued sanitychk */
97 #define MSGF_SYNC	0x0008		/* synchronous message operation */
98 #define MSGF_INTRANSIT	0x0010		/* in-transit (IPI) */
99 #define MSGF_WAITING	0x0020		/* MSGF_SYNC being waited upon */
100 #define MSGF_DROPABLE	0x0040		/* message supports drop */
101 #define MSGF_ABORTABLE	0x0080		/* message supports abort */
102 #define MSGF_PRIORITY	0x0100		/* priority message */
103 
104 #define MSGF_USER0	0x00010000
105 #define MSGF_USER1	0x00020000
106 #define MSGF_USER2	0x00040000
107 #define MSGF_USER3	0x00080000
108 
109 #define MSG_CMD_CDEV	0x00010000
110 #define MSG_CMD_VFS	0x00020000
111 #define MSG_CMD_SYSCALL	0x00030000
112 #define MSG_SUBCMD_MASK	0x0000FFFF
113 
114 #ifdef _KERNEL
115 MALLOC_DECLARE(M_LWKTMSG);
116 #endif
117 
118 /*
119  * Notes on port processing requirements:
120  *
121  * mp_putport():
122  *	- may return synchronous error code (error != EASYNC) directly and
123  *	  does not need to check or set MSGF_DONE if so, or set ms_target_port
124  *	- for asynch procesing should clear MSGF_DONE and set ms_target_port
125  *	  to port prior to initiation of the command.
126  *
127  * mp_waitmsg():
128  *	- wait for a particular message to be returned.
129  *
130  * mp_waitport():
131  *	- wait for a new message on the specified port.
132  *
133  * mp_replyport():
134  *	- reply a message (executed on the originating port to return a
135  *	  message to it).  This can be rather involved if abort is to be
136  *	  supported, see lwkt_default_replyport().  Generally speaking
137  *	  one sets MSGF_DONE and MSGF_REPLY.  If MSGF_SYNC is set the message
138  *	  is not queued to the port and the reply code wakes up the waiter
139  *	  directly.
140  *
141  * mp_dropmsg():
142  *	- drop a specific message from the specified port.  Currently only
143  *	  threads' embedded ports (thread ports or spin ports) support this
144  *        function and must be used in the port's owner thread.
145  *	  (returns 0 on success, ENOENT on error).
146  *
147  * The use of mpu_td and mp_u.spin is specific to the port callback function
148  * set.  Default ports are tied to specific threads and use cpu locality
149  * of reference and mpu_td (and not mp_u.spin at all).  Descriptor ports
150  * assume access via descriptors, signal interruption, etc.  Such ports use
151  * mp_u.spin (and not mpu_td at all) and may be accessed by multiple threads.
152  *
153  * Threads' embedded ports always have mpu_td back pointing to themselves.
154  */
155 typedef struct lwkt_port {
156     lwkt_msg_queue	mp_msgq;
157     lwkt_msg_queue	mp_msgq_prio;
158     int			mp_flags;
159     int			mp_cpuid;
160     union {
161 	struct spinlock	spin;
162 	struct lwkt_serialize *serialize;
163 	void		*data;
164     } mp_u;
165     struct thread	*mpu_td;
166     void *		(*mp_getport)(lwkt_port_t);
167     int			(*mp_putport)(lwkt_port_t, lwkt_msg_t);
168     int			(*mp_waitmsg)(lwkt_msg_t, int flags);
169     void *		(*mp_waitport)(lwkt_port_t, int flags);
170     void		(*mp_replyport)(lwkt_port_t, lwkt_msg_t);
171     int			(*mp_dropmsg)(lwkt_port_t, lwkt_msg_t);
172     int			(*mp_putport_oncpu)(lwkt_port_t, lwkt_msg_t);
173 } lwkt_port;
174 
175 #ifdef _KERNEL
176 
177 #define mpu_spin	mp_u.spin
178 #define mpu_serialize	mp_u.serialize
179 #define mpu_data	mp_u.data
180 
181 /*
182  * Port state flags.
183  *
184  * WAITING      The owner of the port is descheduled waiting for a message
185  *              to be replied.  In case this a spin port there can actually
186  *              be more than one thread waiting on the port.
187  */
188 #define MSGPORTF_WAITING	0x0001
189 
190 /*
191  * These functions are good for userland as well as the kernel.  The
192  * messaging function support for userland is provided by the kernel's
193  * kern/lwkt_msgport.c.  The port functions are provided by userland.
194  */
195 
196 void lwkt_initport_thread(lwkt_port_t, struct thread *);
197 void lwkt_initport_spin(lwkt_port_t, struct thread *, boolean_t);
198 void lwkt_initport_serialize(lwkt_port_t, struct lwkt_serialize *);
199 void lwkt_initport_panic(lwkt_port_t);
200 void lwkt_initport_replyonly_null(lwkt_port_t);
201 void lwkt_initport_replyonly(lwkt_port_t,
202 				void (*rportfn)(lwkt_port_t, lwkt_msg_t));
203 void lwkt_initport_putonly(lwkt_port_t,
204 				int (*pportfn)(lwkt_port_t, lwkt_msg_t));
205 
206 void lwkt_sendmsg(lwkt_port_t, lwkt_msg_t);
207 void lwkt_sendmsg_oncpu(lwkt_port_t, lwkt_msg_t);
208 void lwkt_sendmsg_prepare(lwkt_port_t, lwkt_msg_t);
209 void lwkt_sendmsg_start(lwkt_port_t, lwkt_msg_t);
210 int lwkt_domsg(lwkt_port_t, lwkt_msg_t, int);
211 int lwkt_forwardmsg(lwkt_port_t, lwkt_msg_t);
212 void lwkt_abortmsg(lwkt_msg_t);
213 
214 #endif /* _KERNEL */
215 
216 #endif
217