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 /* A handler function type. */ 47 48 typedef void (*ipc_server_handler_type)(ipc_message_t *, void *); 49 50 /* A finaliser function type. */ 51 52 struct ipc_server_config_type; 53 54 typedef void (*ipc_server_finaliser_type)(struct ipc_server_config_type *); 55 56 /* A server configuration type. */ 57 58 typedef struct ipc_server_config_type 59 { 60 /* Component object references. C++ objects may provide different references 61 for the handler (providing access to the exposed interface) and the 62 finaliser (providing access to a distinct control interface). */ 63 64 void *handler_obj; 65 void *finaliser_obj; 66 67 /* Expected capability item limit for incoming messages. */ 68 69 int expected_items; 70 71 /* Handler for incoming messages and finaliser upon deletion. */ 72 73 ipc_server_handler_type handler; 74 ipc_server_finaliser_type finaliser; 75 76 /* Configure a thread instead of using the current thread. */ 77 78 int config_thread; 79 80 /* Thread and IPC capabilities. */ 81 82 l4_cap_idx_t thread; 83 l4_cap_idx_t server; 84 85 /* Receive deletion notifications via an IRQ. */ 86 87 int notifications; 88 l4_cap_idx_t irq; 89 int auto_deletion; 90 91 } ipc_server_config_type; 92 93 /* Server configuration defaults. */ 94 95 typedef struct ipc_server_default_config_type 96 { 97 int expected_items; 98 ipc_server_handler_type handler; 99 100 } ipc_server_default_config_type; 101 102 103 104 /* Associate a notification IRQ with the main thread. */ 105 106 long ipc_server_apply_irq(l4_cap_idx_t *irq); 107 108 /* Associate a notification IRQ with the given thread. */ 109 110 long ipc_server_apply_irq_for_thread(l4_cap_idx_t *irq, l4_cap_idx_t thread); 111 112 /* Apply auto-deletion to an IRQ associated with the given thread. */ 113 114 long ipc_server_apply_deletion_for_thread(l4_cap_idx_t cap, l4_cap_idx_t irq, l4_cap_idx_t thread); 115 116 /* Bind the main thread to a named IPC gate capability. */ 117 118 long ipc_server_bind(const char *name, l4_umword_t id, l4_cap_idx_t *server); 119 120 /* Create a new IPC gate for the main thread. */ 121 122 long ipc_server_new(l4_cap_idx_t *cap, void *obj); 123 124 /* Create a new IPC gate for the given thread. */ 125 126 long ipc_server_new_for_thread(l4_cap_idx_t *cap, void *obj, l4_cap_idx_t thread); 127 128 /* Create an IPC gate for the main thread. */ 129 130 long ipc_server_new_gate(l4_cap_idx_t *ref, l4_umword_t id); 131 132 /* Create an IPC gate for the indicated thread. */ 133 134 long ipc_server_new_gate_for_thread(l4_cap_idx_t *ref, l4_cap_idx_t thread, l4_umword_t id); 135 136 137 138 /* Handle incoming messages for a server. */ 139 140 long _ipc_server_loop_for(ipc_server_default_config_type *default_config, 141 void *handler_obj, const char *name); 142 143 /* Initialise a server. */ 144 145 long _ipc_server_init_for(ipc_server_config_type *config, 146 ipc_server_default_config_type *default_config, 147 void *handler_obj); 148 149 /* Add handling of incoming messages to an existing server. */ 150 151 long _ipc_server_add_config(ipc_server_config_type *config, 152 ipc_server_default_config_type *default_config, 153 void *handler_obj, 154 l4_cap_idx_t thread); 155 156 /* Handle incoming messages and IRQ notifications for a server. */ 157 158 long ipc_server_managed_loop(ipc_server_config_type *config); 159 160 /* A pthread-compatible mainloop initiation function. */ 161 162 void *ipc_server_start_mainloop(void *data); 163 164 /* Wait for an incoming message via a dedicated IPC gate for a thread. */ 165 166 l4_msgtag_t ipc_server_wait(l4_umword_t id); 167 168 169 170 /* Finalise a server configuration. */ 171 172 void ipc_server_finalise_config(ipc_server_config_type *config); 173 174 /* Discard any thread created but not initiated. */ 175 176 void ipc_server_discard_thread(ipc_server_config_type *config); 177 178 /* Initialise a server configuration. */ 179 180 void ipc_server_init_config(ipc_server_config_type *config); 181 182 /* Initialise but do not start a server using the given configuration. */ 183 184 long ipc_server_prepare_config(ipc_server_config_type *config); 185 186 /* Initialise and start a server using the given configuration. */ 187 188 long ipc_server_start_config(ipc_server_config_type *config); 189 190 /* Initialise and start a server using the given configuration and thread. */ 191 192 long ipc_server_start_config_thread(ipc_server_config_type *config, 193 l4_cap_idx_t thread); 194 195 EXTERN_C_END 196 197 /* vim: tabstop=2 expandtab shiftwidth=2 198 */