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