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