Merge from vendor branch BZIP:
[dragonfly.git] / sys / sys / msgport.h
1 /*
2  * SYS/MSGPORT.H
3  *
4  *      Implements LWKT messages and ports.
5  * 
6  * $DragonFly: src/sys/sys/msgport.h,v 1.27 2008/03/05 13:03:29 sephe Exp $
7  */
8
9 #ifndef _SYS_MSGPORT_H_
10 #define _SYS_MSGPORT_H_
11
12 #ifndef _SYS_QUEUE_H_
13 #include <sys/queue.h>          /* TAILQ_* macros */
14 #endif
15 #ifndef _SYS_STDINT_H_
16 #include <sys/stdint.h>
17 #endif
18 #ifndef _SYS_SPINLOCK_H_
19 #include <sys/spinlock.h>
20 #endif
21
22 #ifdef _KERNEL
23
24 #ifndef _SYS_MALLOC_H_
25 #include <sys/malloc.h>
26 #endif
27
28 #endif
29
30 struct lwkt_msg;
31 struct lwkt_port;
32 struct lwkt_serialize;
33 struct thread;
34
35 typedef struct lwkt_msg         *lwkt_msg_t;
36 typedef struct lwkt_port        *lwkt_port_t;
37
38 typedef TAILQ_HEAD(lwkt_msg_queue, lwkt_msg) lwkt_msg_queue;
39
40 /*
41  * The standard message and port structure for communications between
42  * threads.  See kern/lwkt_msgport.c for documentation on how messages and
43  * ports work.
44  *
45  * A message may only be manipulated by whomever currently owns it,
46  * which generally means the originating port if the message has
47  * not been sent yet or has been replied, and the target port if the message
48  * has been sent and/or is undergoing processing.
49  *
50  * NOTE! 64-bit-align this structure.
51  */
52 typedef struct lwkt_msg {
53     TAILQ_ENTRY(lwkt_msg) ms_node;      /* link node */
54     lwkt_port_t ms_target_port;         /* current target or relay port */
55     lwkt_port_t ms_reply_port;          /* async replies returned here */
56     void        (*ms_abortfn)(struct lwkt_msg *);
57     int         ms_flags;               /* message flags */
58     int         ms_error;               /* positive error code or 0 */
59     union {
60         void    *ms_resultp;            /* misc pointer data or result */
61         int     ms_result;              /* standard 'int'eger result */
62         long    ms_lresult;             /* long result */
63         int     ms_fds[2];              /* two int bit results */
64         __int32_t ms_result32;          /* 32 bit result */
65         __int64_t ms_result64;          /* 64 bit result */
66         __off_t ms_offset;              /* off_t result */
67     } u;
68     int         ms_pad[2];              /* future use */
69 } lwkt_msg;
70
71 /*
72  * Message state flags are manipulated by the current owner only.
73  *
74  * DONE         Indicates completion of the reply.  This flag is also set
75  *              for unsent messages.
76  *
77  * REPLY        Indicates message is being replied but may or may not
78  *              have been queued or returned yet.  This bit is left set
79  *              when a message is retrieved from a reply port so the caller
80  *              can distinguish between requests and replies.
81  *
82  * QUEUED       Indicates message is queued on reply or target port, or
83  *              some other port.
84  *
85  * SYNC         Indicates that the originator is blocked directly on the
86  *              message and that the message should be signaled on
87  *              completion instead of queued.
88  *
89  * INTRANSIT    Indicates that the message state is indeterminant (e.g.
90  *              being passed through an IPI).
91  *
92  * ABORTABLE    Static flag indicates that ms_abortfn is valid.
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_ABORTABLE  0x0080          /* message supports abort */
100
101 #define MSG_CMD_CDEV    0x00010000
102 #define MSG_CMD_VFS     0x00020000
103 #define MSG_CMD_SYSCALL 0x00030000
104 #define MSG_SUBCMD_MASK 0x0000FFFF
105
106 #ifdef _KERNEL
107 MALLOC_DECLARE(M_LWKTMSG);
108 #endif
109
110 /*
111  * Notes on port processing requirements:
112  *
113  * mp_putport():
114  *      - may return synchronous error code (error != EASYNC) directly and
115  *        does not need to check or set MSGF_DONE if so, or set ms_target_port
116  *      - for asynch procesing should clear MSGF_DONE and set ms_target_port
117  *        to port prior to initiation of the command.
118  *
119  * mp_waitmsg():
120  *      - wait for a particular message to be returned.
121  *
122  * mp_waitport():
123  *      - wait for a new message on the specified port.
124  *
125  * mp_replyport():
126  *      - reply a message (executed on the originating port to return a
127  *        message to it).  This can be rather involved if abort is to be
128  *        supported, see lwkt_default_replyport().  Generally speaking
129  *        one sets MSGF_DONE.  If MSGF_SYNC is set the message is not
130  *        queued to the port and the reply code wakes up the waiter
131  *        directly.
132  *
133  * The use of mp_u.td and mp_u.spin is specific to the port callback function
134  * set.  Default ports are tied to specific threads and use cpu locality
135  * of reference and mp_u.td (and not mp_u.spin at all).  Descriptor ports
136  * assume access via descriptors, signal interruption, etc.  Such ports use
137  * mp_u.spin (and not mp_u.td at all) and may be accessed by multiple threads.
138  */
139 typedef struct lwkt_port {
140     lwkt_msg_queue      mp_msgq;
141     int                 mp_flags;
142     union {
143         struct spinlock spin;
144         struct thread   *td;
145         struct lwkt_serialize *serialize;
146         void            *data;
147     } mp_u;
148     void *              (*mp_getport)(lwkt_port_t);
149     int                 (*mp_putport)(lwkt_port_t, lwkt_msg_t);
150     int                 (*mp_waitmsg)(lwkt_msg_t, int flags);
151     void *              (*mp_waitport)(lwkt_port_t, int flags);
152     void                (*mp_replyport)(lwkt_port_t, lwkt_msg_t);
153 } lwkt_port;
154
155 #ifdef _KERNEL
156
157 #define mpu_td          mp_u.td
158 #define mpu_spin        mp_u.spin
159 #define mpu_serialize   mp_u.serialize
160 #define mpu_data        mp_u.data
161
162 #endif
163
164 #define MSGPORTF_WAITING        0x0001
165
166 /*
167  * These functions are good for userland as well as the kernel.  The 
168  * messaging function support for userland is provided by the kernel's
169  * kern/lwkt_msgport.c.  The port functions are provided by userland.
170  */
171
172 void lwkt_initport_thread(lwkt_port_t, struct thread *);
173 void lwkt_initport_spin(lwkt_port_t);
174 void lwkt_initport_serialize(lwkt_port_t, struct lwkt_serialize *);
175 void lwkt_initport_panic(lwkt_port_t);
176 void lwkt_initport_replyonly_null(lwkt_port_t);
177 void lwkt_initport_replyonly(lwkt_port_t,
178                                 void (*rportfn)(lwkt_port_t, lwkt_msg_t));
179 void lwkt_initport_putonly(lwkt_port_t,
180                                 int (*pportfn)(lwkt_port_t, lwkt_msg_t));
181
182 void lwkt_sendmsg(lwkt_port_t, lwkt_msg_t);
183 int lwkt_domsg(lwkt_port_t, lwkt_msg_t, int);
184 int lwkt_forwardmsg(lwkt_port_t, lwkt_msg_t);
185 void lwkt_abortmsg(lwkt_msg_t);
186
187 #endif