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