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