| 1 | /* |
| 2 | * SYS/MSGPORT.H |
| 3 | * |
| 4 | * Implements LWKT messages and ports. |
| 5 | * |
| 6 | * $DragonFly: src/sys/sys/msgport.h,v 1.23 2007/05/23 02:09:41 dillon 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 | |
| 19 | #ifdef _KERNEL |
| 20 | |
| 21 | #ifndef _SYS_MALLOC_H_ |
| 22 | #include <sys/malloc.h> |
| 23 | #endif |
| 24 | |
| 25 | #endif |
| 26 | |
| 27 | struct lwkt_msg; |
| 28 | struct lwkt_port; |
| 29 | struct thread; |
| 30 | |
| 31 | typedef struct lwkt_msg *lwkt_msg_t; |
| 32 | typedef struct lwkt_port *lwkt_port_t; |
| 33 | |
| 34 | typedef TAILQ_HEAD(lwkt_msg_queue, lwkt_msg) lwkt_msg_queue; |
| 35 | |
| 36 | /* |
| 37 | * LWKT command message operator type. This type holds a message's |
| 38 | * 'command'. The command format is opaque to the LWKT messaging system, |
| 39 | * meaning that it is specific to whatever convention the API chooses. |
| 40 | * By convention lwkt_cmd_t is passed by value and is expected to |
| 41 | * efficiently fit into a machine register. |
| 42 | */ |
| 43 | typedef union lwkt_cmd { |
| 44 | int cm_op; |
| 45 | int (*cm_func)(lwkt_msg_t msg); |
| 46 | } lwkt_cmd_t; |
| 47 | |
| 48 | /* |
| 49 | * The standard message and port structure for communications between |
| 50 | * threads. See kern/lwkt_msgport.c for documentation on how messages and |
| 51 | * ports work. |
| 52 | * |
| 53 | * For the most part a message may only be manipulated by whomever currently |
| 54 | * owns it, which generally means the originating port if the message has |
| 55 | * not been sent yet or has been replied, and the target port if the message |
| 56 | * has been sent and/or is undergoing processing. |
| 57 | * |
| 58 | * The one exception to this rule is an abort. Aborts must be initiated |
| 59 | * by the originator and may 'chase' the target (especially if a message |
| 60 | * is being forwarded), potentially even 'chase' the message all the way |
| 61 | * back to the originator if it races against the target replying the |
| 62 | * message. The ms_abort_port field is the only field that may be modified |
| 63 | * by the originator or intermediate target (when the abort is chasing |
| 64 | * a forwarding or reply op). An abort may cause a reply to be delayed |
| 65 | * until the abort catches up to it. |
| 66 | * |
| 67 | * Messages which support an abort will have MSGF_ABORTABLE set, indicating |
| 68 | * that the ms_abort field has been initialized. An abort will cause a |
| 69 | * message to be requeued to the target port so the target sees the same |
| 70 | * message twice: once during initial processing of the message, and a |
| 71 | * second time to process the abort request. lwkt_getport() will detect |
| 72 | * the requeued abort and will copy ms_abort into ms_cmd before returning |
| 73 | * the requeued message the second time. This makes target processing a |
| 74 | * whole lot less complex. |
| 75 | * |
| 76 | * NOTE! 64-bit-align this structure. |
| 77 | */ |
| 78 | typedef struct lwkt_msg { |
| 79 | TAILQ_ENTRY(lwkt_msg) ms_node; /* link node */ |
| 80 | lwkt_port_t ms_target_port; /* current target or relay port */ |
| 81 | lwkt_port_t ms_reply_port; /* async replies returned here */ |
| 82 | lwkt_port_t ms_abort_port; /* abort chasing port */ |
| 83 | lwkt_cmd_t ms_cmd; /* message command operator */ |
| 84 | lwkt_cmd_t ms_abort; /* message abort operator */ |
| 85 | int ms_flags; /* message flags */ |
| 86 | int ms_error; /* positive error code or 0 */ |
| 87 | union { |
| 88 | void *ms_resultp; /* misc pointer data or result */ |
| 89 | int ms_result; /* standard 'int'eger result */ |
| 90 | long ms_lresult; /* long result */ |
| 91 | int ms_fds[2]; /* two int bit results */ |
| 92 | __int32_t ms_result32; /* 32 bit result */ |
| 93 | __int64_t ms_result64; /* 64 bit result */ |
| 94 | __off_t ms_offset; /* off_t result */ |
| 95 | } u; |
| 96 | int ms_pad[2]; /* future use */ |
| 97 | } lwkt_msg; |
| 98 | |
| 99 | #define MSGF_DONE 0x0001 /* asynch message is complete */ |
| 100 | #define MSGF_REPLY1 0x0002 /* asynch message has been returned */ |
| 101 | #define MSGF_QUEUED 0x0004 /* message has been queued sanitychk */ |
| 102 | #define MSGF_ASYNC 0x0008 /* sync/async hint */ |
| 103 | #define MSGF_ABORTED 0x0010 /* indicate pending abort */ |
| 104 | #define MSGF_PCATCH 0x0020 /* catch proc signal while waiting */ |
| 105 | #define MSGF_REPLY2 0x0040 /* reply processed by rport cpu */ |
| 106 | #define MSGF_ABORTABLE 0x0080 /* message supports abort */ |
| 107 | #define MSGF_RETRIEVED 0x0100 /* message retrieved on target */ |
| 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_waitport(): |
| 128 | * - if the passed msg is NULL we wait for, remove, and return the |
| 129 | * next pending message on the port. |
| 130 | * - if the passed msg is non-NULL we wait for that particular message, |
| 131 | * which typically involves waiting until MSGF_DONE is set then |
| 132 | * pulling the message off the port if MSGF_QUEUED is set and |
| 133 | * returning it. If MSGF_PCATCH is set in the message we allow |
| 134 | * a signal to interrupt and abort the message. |
| 135 | * |
| 136 | * mp_replyport(): |
| 137 | * - reply a message (executed on the originating port to return a |
| 138 | * message to it). This can be rather involved if abort is to be |
| 139 | * supported, see lwkt_default_replyport(). Generally speaking |
| 140 | * one sets MSGF_DONE. If MSGF_ASYNC is set the message is queued |
| 141 | * to the port, else the port's thread is scheduled. |
| 142 | * |
| 143 | * mp_abortport(): |
| 144 | * - abort a message. The mp_abortport function for the message's |
| 145 | * ms_target_port is called. ms_target_port is basically where |
| 146 | * the message was sent to or last forwarded to. Aborting a message |
| 147 | * can be rather involved. Note that the lwkt_getmsg() code ensures |
| 148 | * that a message is returned non-abort before it is returned again |
| 149 | * with its ms_cmd set to ms_abort, even if the abort occurs before |
| 150 | * the initial retrieval of the message. The setting of ms_cmd to |
| 151 | * ms_abort is NOT handled by mp_abortport(). mp_abortport() is |
| 152 | * basically responsible for requeueing the message to the target |
| 153 | * port and setting the MSGF_ABORTED flag. |
| 154 | * |
| 155 | */ |
| 156 | typedef struct lwkt_port { |
| 157 | lwkt_msg_queue mp_msgq; |
| 158 | int mp_flags; |
| 159 | int mp_unused01; |
| 160 | struct thread *mp_td; |
| 161 | int (*mp_putport)(lwkt_port_t, lwkt_msg_t); |
| 162 | void * (*mp_waitport)(lwkt_port_t, lwkt_msg_t); |
| 163 | void (*mp_replyport)(lwkt_port_t, lwkt_msg_t); |
| 164 | void (*mp_abortport)(lwkt_port_t, lwkt_msg_t); |
| 165 | } lwkt_port; |
| 166 | |
| 167 | #define MSGPORTF_WAITING 0x0001 |
| 168 | |
| 169 | /* |
| 170 | * These functions are good for userland as well as the kernel. The |
| 171 | * messaging function support for userland is provided by the kernel's |
| 172 | * kern/lwkt_msgport.c. The port functions are provided by userland. |
| 173 | */ |
| 174 | void lwkt_initport(lwkt_port_t, struct thread *); |
| 175 | void lwkt_initport_null_rport(lwkt_port_t, struct thread *); |
| 176 | void lwkt_sendmsg(lwkt_port_t, lwkt_msg_t); |
| 177 | int lwkt_domsg(lwkt_port_t, lwkt_msg_t); |
| 178 | int lwkt_forwardmsg(lwkt_port_t, lwkt_msg_t); |
| 179 | void lwkt_abortmsg(lwkt_msg_t); |
| 180 | void *lwkt_getport(lwkt_port_t); |
| 181 | |
| 182 | int lwkt_default_putport(lwkt_port_t port, lwkt_msg_t msg); |
| 183 | void *lwkt_default_waitport(lwkt_port_t port, lwkt_msg_t msg); |
| 184 | void lwkt_default_replyport(lwkt_port_t port, lwkt_msg_t msg); |
| 185 | void lwkt_default_abortport(lwkt_port_t port, lwkt_msg_t msg); |
| 186 | void lwkt_null_replyport(lwkt_port_t port, lwkt_msg_t msg); |
| 187 | |
| 188 | #endif |