1 /* 2 * Server binding/registration. 3 * 4 * Copyright (C) 2018, 2019, 2020, 2021, 2022, 5 * 2023 Paul Boddie <paul@boddie.org.uk> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation; either version 2 of 10 * the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 20 * Boston, MA 02110-1301, USA 21 */ 22 23 #pragma once 24 25 #include <l4/sys/types.h> 26 27 #include <ipc/message.h> 28 29 EXTERN_C_BEGIN 30 31 /* A convenience macro for invoking the server mainloop. */ 32 33 #define ipc_server_loop_for(TYPE, POINTER, NAME) \ 34 _ipc_server_loop_for(&config_##TYPE, (TYPE *) POINTER, NAME) 35 36 /* A convenience macro for initialising a server. */ 37 38 #define ipc_server_init_for(CONFIG, TYPE, POINTER) \ 39 _ipc_server_init_for(CONFIG, &config_##TYPE, (TYPE *) POINTER) 40 41 /* A convenience macro for adding a configuration to an existing server. */ 42 43 #define ipc_server_add_config(CONFIG, TYPE, POINTER, THREAD) \ 44 _ipc_server_add_config(CONFIG, &config_##TYPE, (TYPE *) POINTER, THREAD) 45 46 47 48 /* A handler function type. */ 49 50 typedef void (*ipc_server_handler_type)(ipc_message_t *, void *); 51 52 53 54 /* A finaliser function type. */ 55 56 struct ipc_server_config_type; 57 58 typedef void (*ipc_server_finaliser_type)(struct ipc_server_config_type *); 59 60 61 62 /* A generic type for server endpoints. */ 63 64 typedef struct ipc_server_endpoint_type 65 { 66 struct ipc_server_config_type *config; 67 68 /* Receive deletion notifications via an IRQ for the associated 69 configuration. This is only defined for IRQ endpoints. */ 70 71 l4_cap_idx_t irq; 72 73 } ipc_server_endpoint_type; 74 75 76 77 /* A server configuration type. */ 78 79 typedef struct ipc_server_config_type 80 { 81 /* Component object references. C++ objects may provide different references 82 for the handler (providing access to the exposed interface) and the 83 finaliser (providing access to a distinct control interface). */ 84 85 void *handler_obj; 86 void *finaliser_obj; 87 88 /* Expected capability item limit for incoming messages. */ 89 90 int expected_items; 91 92 /* Handler for incoming messages and finaliser upon deletion. */ 93 94 ipc_server_handler_type handler; 95 ipc_server_finaliser_type finaliser; 96 97 /* Configure a thread instead of using the current thread. */ 98 99 int config_thread; 100 101 /* Thread and IPC gate capabilities. */ 102 103 l4_cap_idx_t thread, server; 104 105 /* The generic endpoint object and IRQ endpoint. */ 106 107 ipc_server_endpoint_type endpoint, irq_endpoint; 108 109 } ipc_server_config_type; 110 111 112 113 /* Server configuration defaults. */ 114 115 typedef struct ipc_server_default_config_type 116 { 117 int expected_items; 118 ipc_server_handler_type handler; 119 120 } ipc_server_default_config_type; 121 122 123 124 /* Associate a notification IRQ with an IPC gate in the main thread. */ 125 126 long ipc_server_apply_irq(l4_cap_idx_t cap, l4_cap_idx_t *irq, l4_umword_t id); 127 128 /* Associate a notification IRQ with an IPC gate in the given thread. */ 129 130 long ipc_server_apply_irq_for_thread(l4_cap_idx_t cap, l4_cap_idx_t *irq, 131 l4_umword_t id, l4_cap_idx_t thread); 132 133 /* Bind the main thread to a named IPC gate capability. */ 134 135 long ipc_server_bind(const char *name, l4_umword_t id, l4_cap_idx_t *server); 136 137 /* Create a new IPC gate for the main thread. */ 138 139 long ipc_server_new(l4_cap_idx_t *cap, void *obj); 140 141 /* Create a new IPC gate for the given thread. */ 142 143 long ipc_server_new_for_thread(l4_cap_idx_t *cap, void *obj, l4_cap_idx_t thread); 144 145 /* Create an IPC gate for the main thread. */ 146 147 long ipc_server_new_gate(l4_cap_idx_t *ref, l4_umword_t id); 148 149 /* Create an IPC gate for the indicated thread. */ 150 151 long ipc_server_new_gate_for_thread(l4_cap_idx_t *ref, l4_cap_idx_t thread, l4_umword_t id); 152 153 154 155 /* Handle incoming messages for a server. */ 156 157 long _ipc_server_loop_for(ipc_server_default_config_type *default_config, 158 void *handler_obj, const char *name); 159 160 /* Initialise a server. */ 161 162 long _ipc_server_init_for(ipc_server_config_type *config, 163 ipc_server_default_config_type *default_config, 164 void *handler_obj); 165 166 /* Add handling of incoming messages to an existing server. */ 167 168 long _ipc_server_add_config(ipc_server_config_type *config, 169 ipc_server_default_config_type *default_config, 170 void *handler_obj, 171 l4_cap_idx_t thread); 172 173 /* Handle incoming messages and IRQ notifications for a server. */ 174 175 long ipc_server_managed_loop(ipc_server_config_type *config); 176 177 /* A pthread-compatible mainloop initiation function. */ 178 179 void *ipc_server_start_mainloop(void *data); 180 181 /* Wait for an incoming message via a dedicated IPC gate for a thread. */ 182 183 l4_msgtag_t ipc_server_wait(l4_umword_t id); 184 185 186 187 /* Finalise a server configuration. */ 188 189 void ipc_server_finalise_config(ipc_server_config_type *config); 190 191 /* Discard any thread created but not initiated. */ 192 193 void ipc_server_discard_thread(ipc_server_config_type *config); 194 195 /* Initialise a server configuration. */ 196 197 void ipc_server_init_config(ipc_server_config_type *config); 198 199 /* Initialise but do not start a server using the given configuration. */ 200 201 long ipc_server_prepare_config(ipc_server_config_type *config); 202 203 /* Initialise and start a server using the given configuration. */ 204 205 long ipc_server_start_config(ipc_server_config_type *config); 206 207 /* Initialise and start a server using the given configuration and thread. */ 208 209 long ipc_server_start_config_thread(ipc_server_config_type *config, 210 l4_cap_idx_t thread); 211 212 EXTERN_C_END 213 214 /* vim: tabstop=2 expandtab shiftwidth=2 215 */