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 90 } ipc_server_config_type; 91 92 /* Server configuration defaults. */ 93 94 typedef struct ipc_server_default_config_type 95 { 96 int expected_items; 97 ipc_server_handler_type handler; 98 99 } ipc_server_default_config_type; 100 101 102 103 /* Associate a notification IRQ with an IPC gate in the main thread. */ 104 105 long ipc_server_apply_irq(l4_cap_idx_t cap, l4_cap_idx_t *irq); 106 107 /* Associate a notification IRQ with an IPC gate in the given thread. */ 108 109 long ipc_server_apply_irq_for_thread(l4_cap_idx_t cap, l4_cap_idx_t *irq, l4_cap_idx_t thread); 110 111 /* Bind the main thread to a named IPC gate capability. */ 112 113 long ipc_server_bind(const char *name, l4_umword_t id, l4_cap_idx_t *server); 114 115 /* Create a new IPC gate for the main thread. */ 116 117 long ipc_server_new(l4_cap_idx_t *cap, void *obj); 118 119 /* Create a new IPC gate for the given thread. */ 120 121 long ipc_server_new_for_thread(l4_cap_idx_t *cap, void *obj, l4_cap_idx_t thread); 122 123 /* Create an IPC gate for the main thread. */ 124 125 long ipc_server_new_gate(l4_cap_idx_t *ref, l4_umword_t id); 126 127 /* Create an IPC gate for the indicated thread. */ 128 129 long ipc_server_new_gate_for_thread(l4_cap_idx_t *ref, l4_cap_idx_t thread, l4_umword_t id); 130 131 132 133 /* Handle incoming messages for a server. */ 134 135 long _ipc_server_loop_for(ipc_server_default_config_type *default_config, 136 void *handler_obj, const char *name); 137 138 /* Initialise a server. */ 139 140 long _ipc_server_init_for(ipc_server_config_type *config, 141 ipc_server_default_config_type *default_config, 142 void *handler_obj); 143 144 /* Add handling of incoming messages to an existing server. */ 145 146 long _ipc_server_add_config(ipc_server_config_type *config, 147 ipc_server_default_config_type *default_config, 148 void *handler_obj, 149 l4_cap_idx_t thread); 150 151 /* Handle incoming messages and IRQ notifications for a server. */ 152 153 long ipc_server_managed_loop(ipc_server_config_type *config); 154 155 /* A pthread-compatible mainloop initiation function. */ 156 157 void *ipc_server_start_mainloop(void *data); 158 159 /* Wait for an incoming message via a dedicated IPC gate for a thread. */ 160 161 l4_msgtag_t ipc_server_wait(l4_umword_t id); 162 163 164 165 /* Finalise a server configuration. */ 166 167 void ipc_server_finalise_config(ipc_server_config_type *config); 168 169 /* Discard any thread created but not initiated. */ 170 171 void ipc_server_discard_thread(ipc_server_config_type *config); 172 173 /* Initialise a server configuration. */ 174 175 void ipc_server_init_config(ipc_server_config_type *config); 176 177 /* Initialise but do not start a server using the given configuration. */ 178 179 long ipc_server_prepare_config(ipc_server_config_type *config); 180 181 /* Initialise and start a server using the given configuration. */ 182 183 long ipc_server_start_config(ipc_server_config_type *config); 184 185 /* Initialise and start a server using the given configuration and thread. */ 186 187 long ipc_server_start_config_thread(ipc_server_config_type *config, 188 l4_cap_idx_t thread); 189 190 EXTERN_C_END 191 192 /* vim: tabstop=2 expandtab shiftwidth=2 193 */