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 /* Create a new IPC gate for the main thread. */ 117 118 long ipc_server_new(l4_cap_idx_t *cap, void *obj); 119 120 /* Create a new IPC gate for the given thread. */ 121 122 long ipc_server_new_for_thread(l4_cap_idx_t *cap, void *obj, l4_cap_idx_t thread); 123 124 /* Create an IPC gate for the main thread. */ 125 126 long ipc_server_new_gate(l4_cap_idx_t *ref, l4_umword_t id); 127 128 /* Create an IPC gate for the indicated thread. */ 129 130 long ipc_server_new_gate_for_thread(l4_cap_idx_t *ref, l4_cap_idx_t thread, l4_umword_t id); 131 132 133 134 /* Handle incoming messages for a server. */ 135 136 long _ipc_server_loop_for(ipc_server_default_config_type *default_config, 137 void *handler_obj, const char *name); 138 139 /* Initialise a server. */ 140 141 long _ipc_server_init_for(ipc_server_config_type *config, 142 ipc_server_default_config_type *default_config, 143 void *handler_obj); 144 145 /* Add handling of incoming messages to an existing server. */ 146 147 long _ipc_server_add_config(ipc_server_config_type *config, 148 ipc_server_default_config_type *default_config, 149 void *handler_obj, 150 l4_cap_idx_t thread); 151 152 /* Handle incoming messages and IRQ notifications for a server. */ 153 154 long ipc_server_managed_loop(ipc_server_config_type *config); 155 156 /* A pthread-compatible mainloop initiation function. */ 157 158 void *ipc_server_start_mainloop(void *data); 159 160 /* Wait for an incoming message via a dedicated IPC gate for a thread. */ 161 162 l4_msgtag_t ipc_server_wait(l4_umword_t id); 163 164 165 166 /* Finalise a server configuration. */ 167 168 void ipc_server_finalise_config(ipc_server_config_type *config); 169 170 /* Discard any thread created but not initiated. */ 171 172 void ipc_server_discard_thread(ipc_server_config_type *config); 173 174 /* Initialise a server configuration. */ 175 176 void ipc_server_init_config(ipc_server_config_type *config); 177 178 /* Initialise but do not start a server using the given configuration. */ 179 180 long ipc_server_prepare_config(ipc_server_config_type *config); 181 182 /* Initialise and start a server using the given configuration. */ 183 184 long ipc_server_start_config(ipc_server_config_type *config); 185 186 /* Initialise and start a server using the given configuration and thread. */ 187 188 long ipc_server_start_config_thread(ipc_server_config_type *config, 189 l4_cap_idx_t thread); 190 191 EXTERN_C_END 192 193 /* vim: tabstop=2 expandtab shiftwidth=2 194 */