paul@181 | 1 | /* |
paul@181 | 2 | * Server binding/registration. |
paul@181 | 3 | * |
paul@621 | 4 | * Copyright (C) 2018-2022, 2024 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 | #include <l4/re/env.h> |
paul@181 | 23 | #include <l4/sys/factory.h> |
paul@181 | 24 | #include <l4/sys/kernel_object.h> |
paul@181 | 25 | #include <l4/sys/rcv_endpoint.h> |
paul@181 | 26 | #include <l4/sys/thread.h> |
paul@181 | 27 | #include <l4/util/util.h> |
paul@181 | 28 | |
paul@181 | 29 | #include <stdlib.h> |
paul@181 | 30 | |
paul@181 | 31 | #include "cap_alloc.h" |
paul@181 | 32 | #include "irq.h" |
paul@181 | 33 | #include "server.h" |
paul@181 | 34 | |
paul@181 | 35 | |
paul@181 | 36 | |
paul@538 | 37 | /* Associate an IRQ with the main thread. */ |
paul@181 | 38 | |
paul@538 | 39 | long ipc_server_apply_irq(l4_cap_idx_t *irq) |
paul@181 | 40 | { |
paul@538 | 41 | return ipc_server_apply_irq_for_thread(irq, l4re_env()->main_thread); |
paul@181 | 42 | } |
paul@181 | 43 | |
paul@538 | 44 | /* Associate an IRQ with the given thread. */ |
paul@181 | 45 | |
paul@538 | 46 | long ipc_server_apply_irq_for_thread(l4_cap_idx_t *irq, l4_cap_idx_t thread) |
paul@181 | 47 | { |
paul@181 | 48 | /* Create an IRQ for the gate. */ |
paul@181 | 49 | |
paul@181 | 50 | long err = ipc_create_irq(irq); |
paul@181 | 51 | |
paul@181 | 52 | if (err) |
paul@181 | 53 | return err; |
paul@181 | 54 | |
paul@181 | 55 | /* Bind the IRQ to the thread, presenting the label if it provides the |
paul@181 | 56 | incoming message. */ |
paul@181 | 57 | |
paul@512 | 58 | err = ipc_bind_irq(*irq, (l4_umword_t) *irq, thread); |
paul@181 | 59 | |
paul@181 | 60 | if (err) |
paul@181 | 61 | { |
paul@181 | 62 | ipc_cap_free_um(*irq); |
paul@181 | 63 | return err; |
paul@181 | 64 | } |
paul@181 | 65 | |
paul@538 | 66 | return L4_EOK; |
paul@538 | 67 | } |
paul@538 | 68 | |
paul@538 | 69 | /* Associate the given IRQ with gate deletion notifications, also decreasing the |
paul@538 | 70 | IPC gate reference count so that such a notification will be delivered when |
paul@538 | 71 | the final client of the IPC gate releases its reference to it. */ |
paul@538 | 72 | |
paul@538 | 73 | long ipc_server_apply_deletion_for_thread(l4_cap_idx_t cap, l4_cap_idx_t irq, l4_cap_idx_t thread) |
paul@538 | 74 | { |
paul@181 | 75 | /* Register for notifications. The thread associated with the gate seems to be |
paul@181 | 76 | needed. */ |
paul@181 | 77 | |
paul@538 | 78 | long err = l4_error(l4_thread_register_del_irq(thread, irq)); |
paul@181 | 79 | |
paul@181 | 80 | if (err) |
paul@181 | 81 | { |
paul@538 | 82 | ipc_cap_free_um(irq); |
paul@181 | 83 | return err; |
paul@181 | 84 | } |
paul@181 | 85 | |
paul@181 | 86 | /* Decrease the reference count on the server capability (IPC gate). */ |
paul@181 | 87 | |
paul@181 | 88 | return l4_error(l4_kobject_dec_refcnt(cap, 1)); |
paul@181 | 89 | } |
paul@181 | 90 | |
paul@181 | 91 | /* Initialise a new server capability, binding it to the main thread. */ |
paul@181 | 92 | |
paul@181 | 93 | long ipc_server_new(l4_cap_idx_t *cap, void *obj) |
paul@181 | 94 | { |
paul@181 | 95 | return ipc_server_new_for_thread(cap, obj, l4re_env()->main_thread); |
paul@181 | 96 | } |
paul@181 | 97 | |
paul@181 | 98 | /* Initialise a new server capability, binding it to the given thread. */ |
paul@181 | 99 | |
paul@181 | 100 | long ipc_server_new_for_thread(l4_cap_idx_t *cap, void *obj, l4_cap_idx_t thread) |
paul@181 | 101 | { |
paul@181 | 102 | l4_umword_t label = (l4_umword_t) obj; |
paul@181 | 103 | |
paul@181 | 104 | /* Create an IPC gate, presenting the resource's label if this gate provides |
paul@181 | 105 | the message. */ |
paul@181 | 106 | |
paul@181 | 107 | return ipc_server_new_gate_for_thread(cap, thread, label); |
paul@181 | 108 | } |
paul@181 | 109 | |
paul@181 | 110 | /* Create an IPC gate, exporting via a new capability an object with the given |
paul@181 | 111 | identifier. */ |
paul@181 | 112 | |
paul@181 | 113 | long ipc_server_new_gate(l4_cap_idx_t *ref, l4_umword_t id) |
paul@181 | 114 | { |
paul@181 | 115 | return ipc_server_new_gate_for_thread(ref, l4re_env()->main_thread, id); |
paul@181 | 116 | } |
paul@181 | 117 | |
paul@181 | 118 | /* Create an IPC gate associated with a specific thread. */ |
paul@181 | 119 | |
paul@181 | 120 | long ipc_server_new_gate_for_thread(l4_cap_idx_t *ref, l4_cap_idx_t thread, l4_umword_t id) |
paul@181 | 121 | { |
paul@181 | 122 | long err; |
paul@181 | 123 | |
paul@181 | 124 | *ref = ipc_cap_alloc(); |
paul@181 | 125 | |
paul@181 | 126 | if (l4_is_invalid_cap(*ref)) |
paul@181 | 127 | return -L4_ENOENT; |
paul@181 | 128 | |
paul@181 | 129 | /* Associate the object with an IPC gate. */ |
paul@181 | 130 | |
paul@181 | 131 | err = l4_error(l4_factory_create_gate(l4re_env()->factory, *ref, thread, id)); |
paul@181 | 132 | |
paul@181 | 133 | if (err) |
paul@181 | 134 | ipc_cap_free_um(*ref); |
paul@181 | 135 | |
paul@181 | 136 | return err; |
paul@181 | 137 | } |
paul@181 | 138 | |
paul@181 | 139 | |
paul@181 | 140 | |
paul@289 | 141 | /* Support for the convenience macro starting a server for a given object. */ |
paul@289 | 142 | |
paul@459 | 143 | long _ipc_server_loop_for(ipc_server_default_config_type *default_config, |
paul@459 | 144 | void *handler_obj, |
paul@459 | 145 | const char *name) |
paul@289 | 146 | { |
paul@289 | 147 | ipc_server_config_type config; |
paul@289 | 148 | |
paul@289 | 149 | if (name != NULL) |
paul@621 | 150 | config.server = l4re_env_get_cap(name); |
paul@289 | 151 | |
paul@459 | 152 | _ipc_server_init_for(&config, default_config, handler_obj); |
paul@289 | 153 | |
paul@289 | 154 | return ipc_server_start_config(&config); |
paul@289 | 155 | } |
paul@289 | 156 | |
paul@354 | 157 | /* Initialise a server for a given object. */ |
paul@354 | 158 | |
paul@459 | 159 | long _ipc_server_init_for(ipc_server_config_type *config, |
paul@459 | 160 | ipc_server_default_config_type *default_config, |
paul@459 | 161 | void *handler_obj) |
paul@354 | 162 | { |
paul@354 | 163 | ipc_server_init_config(config); |
paul@354 | 164 | |
paul@459 | 165 | config->expected_items = default_config->expected_items; |
paul@459 | 166 | config->handler = default_config->handler; |
paul@354 | 167 | config->handler_obj = handler_obj; |
paul@413 | 168 | |
paul@413 | 169 | return L4_EOK; |
paul@354 | 170 | } |
paul@354 | 171 | |
paul@292 | 172 | /* Associate a new configuration with an existing server endpoint. */ |
paul@292 | 173 | |
paul@459 | 174 | long _ipc_server_add_config(ipc_server_config_type *config, |
paul@459 | 175 | ipc_server_default_config_type *default_config, |
paul@459 | 176 | void *handler_obj, |
paul@292 | 177 | l4_cap_idx_t thread) |
paul@292 | 178 | { |
paul@459 | 179 | _ipc_server_init_for(config, default_config, handler_obj); |
paul@354 | 180 | return ipc_server_start_config_thread(config, thread); |
paul@354 | 181 | } |
paul@354 | 182 | |
paul@354 | 183 | /* Complete initialisation of a server in the given thread. */ |
paul@354 | 184 | |
paul@354 | 185 | long ipc_server_start_config_thread(ipc_server_config_type *config, |
paul@354 | 186 | l4_cap_idx_t thread) |
paul@354 | 187 | { |
paul@292 | 188 | config->thread = thread; |
paul@292 | 189 | config->config_thread = 1; |
paul@292 | 190 | |
paul@529 | 191 | return ipc_server_prepare_config(config); |
paul@292 | 192 | } |
paul@292 | 193 | |
paul@292 | 194 | |
paul@292 | 195 | |
paul@181 | 196 | /* A server main loop handling endpoint deletion for an IPC gate dedicated to |
paul@181 | 197 | a particular object within its own thread. */ |
paul@181 | 198 | |
paul@459 | 199 | long ipc_server_managed_loop(ipc_server_config_type *config) |
paul@181 | 200 | { |
paul@181 | 201 | ipc_message_t msg; |
paul@379 | 202 | l4_umword_t label, irq_label; |
paul@181 | 203 | |
paul@286 | 204 | /* Permit other endpoints by dynamically interpreting the label. */ |
paul@286 | 205 | |
paul@286 | 206 | ipc_server_config_type *config_from_label; |
paul@286 | 207 | |
paul@181 | 208 | /* Declare the extent to which capabilities are expected in messages. */ |
paul@181 | 209 | |
paul@459 | 210 | long err = ipc_message_expect(&msg, config->expected_items); |
paul@181 | 211 | |
paul@181 | 212 | if (err) |
paul@181 | 213 | return err; |
paul@181 | 214 | |
paul@181 | 215 | /* Wait for an incoming message. */ |
paul@181 | 216 | |
paul@181 | 217 | while (1) |
paul@181 | 218 | { |
paul@181 | 219 | ipc_message_wait(&msg, &label); |
paul@181 | 220 | |
paul@181 | 221 | /* Clear lower label bits. */ |
paul@181 | 222 | |
paul@181 | 223 | label = label & ~3UL; |
paul@181 | 224 | |
paul@621 | 225 | /* Since we interpret the label as a pointer, we should detect zero labels |
paul@621 | 226 | and report them since they are likely to be interrupts inappropriately |
paul@621 | 227 | bound to this thread. */ |
paul@621 | 228 | |
paul@621 | 229 | if (!label) |
paul@621 | 230 | continue; |
paul@621 | 231 | |
paul@181 | 232 | /* Ignore erroneous messages. */ |
paul@181 | 233 | |
paul@181 | 234 | if (l4_ipc_error(msg.tag, l4_utcb())) |
paul@181 | 235 | continue; |
paul@181 | 236 | |
paul@181 | 237 | /* Message involves the IPC gate itself. */ |
paul@181 | 238 | |
paul@379 | 239 | irq_label = (l4_umword_t) config->irq; |
paul@379 | 240 | |
paul@538 | 241 | if (label != irq_label) |
paul@286 | 242 | { |
paul@286 | 243 | config_from_label = (ipc_server_config_type *) label; |
paul@286 | 244 | config_from_label->handler(&msg, config_from_label->handler_obj); |
paul@286 | 245 | } |
paul@181 | 246 | |
paul@181 | 247 | /* Message involves the IRQ or a termination condition occurred. */ |
paul@181 | 248 | |
paul@538 | 249 | else if ((label == irq_label) || msg.terminating) |
paul@181 | 250 | break; |
paul@181 | 251 | } |
paul@181 | 252 | |
paul@181 | 253 | /* Free expected capabilities. */ |
paul@181 | 254 | |
paul@181 | 255 | ipc_message_free(&msg); |
paul@181 | 256 | |
paul@181 | 257 | return L4_EOK; |
paul@181 | 258 | } |
paul@181 | 259 | |
paul@181 | 260 | /* A pthread-compatible mainloop initiation function. */ |
paul@181 | 261 | |
paul@181 | 262 | void *ipc_server_start_mainloop(void *data) |
paul@181 | 263 | { |
paul@181 | 264 | ipc_server_config_type *config = (ipc_server_config_type *) data; |
paul@181 | 265 | |
paul@459 | 266 | long err = ipc_server_managed_loop(config); |
paul@181 | 267 | |
paul@181 | 268 | if (config->finaliser != NULL) |
paul@181 | 269 | config->finaliser(config); |
paul@181 | 270 | |
paul@181 | 271 | return (void *) err; |
paul@181 | 272 | } |
paul@181 | 273 | |
paul@181 | 274 | /* Wait for an incoming message via an IPC gate dedicated to a particular |
paul@181 | 275 | object within its own thread. */ |
paul@181 | 276 | |
paul@181 | 277 | l4_msgtag_t ipc_server_wait(l4_umword_t id) |
paul@181 | 278 | { |
paul@181 | 279 | l4_umword_t label; |
paul@181 | 280 | l4_msgtag_t tag; |
paul@181 | 281 | |
paul@181 | 282 | /* Ignore errors and messages not intended for the identified gate. */ |
paul@181 | 283 | |
paul@181 | 284 | do tag = l4_ipc_wait(l4_utcb(), &label, L4_IPC_NEVER); |
paul@181 | 285 | while (l4_ipc_error(tag, l4_utcb()) || (label & ~3UL) != id); |
paul@181 | 286 | |
paul@181 | 287 | return tag; |
paul@181 | 288 | } |
paul@181 | 289 | |
paul@181 | 290 | |
paul@181 | 291 | |
paul@181 | 292 | /* Finalise a server configuration. */ |
paul@181 | 293 | |
paul@181 | 294 | void ipc_server_finalise_config(ipc_server_config_type *config) |
paul@181 | 295 | { |
paul@181 | 296 | if (l4_is_valid_cap(config->server)) |
paul@181 | 297 | { |
paul@181 | 298 | ipc_cap_free_um(config->server); |
paul@181 | 299 | config->server = L4_INVALID_CAP; |
paul@181 | 300 | } |
paul@181 | 301 | |
paul@181 | 302 | if (l4_is_valid_cap(config->irq)) |
paul@181 | 303 | { |
paul@181 | 304 | ipc_cap_free_um(config->irq); |
paul@181 | 305 | config->irq = L4_INVALID_CAP; |
paul@181 | 306 | } |
paul@181 | 307 | } |
paul@181 | 308 | |
paul@292 | 309 | /* Discard any uninitiated thread. */ |
paul@181 | 310 | |
paul@181 | 311 | void ipc_server_discard_thread(ipc_server_config_type *config) |
paul@181 | 312 | { |
paul@292 | 313 | if (config->config_thread && l4_is_valid_cap(config->thread)) |
paul@181 | 314 | { |
paul@181 | 315 | ipc_cap_free_um(config->thread); |
paul@181 | 316 | config->thread = L4_INVALID_CAP; |
paul@181 | 317 | } |
paul@181 | 318 | } |
paul@181 | 319 | |
paul@181 | 320 | /* Initialise a server configuration. */ |
paul@181 | 321 | |
paul@181 | 322 | void ipc_server_init_config(ipc_server_config_type *config) |
paul@181 | 323 | { |
paul@181 | 324 | /* No object references defined. */ |
paul@181 | 325 | |
paul@181 | 326 | config->handler_obj = NULL; |
paul@181 | 327 | config->finaliser_obj = NULL; |
paul@181 | 328 | |
paul@181 | 329 | /* Expect no capability items by default. */ |
paul@181 | 330 | |
paul@181 | 331 | config->expected_items = 0; |
paul@181 | 332 | |
paul@181 | 333 | /* No handler or finaliser defined. */ |
paul@181 | 334 | |
paul@181 | 335 | config->handler = NULL; |
paul@181 | 336 | config->finaliser = NULL; |
paul@181 | 337 | |
paul@292 | 338 | /* No separate thread for the main loop. */ |
paul@181 | 339 | |
paul@292 | 340 | config->config_thread = 0; |
paul@181 | 341 | |
paul@181 | 342 | /* Main thread by default with IPC gate to be allocated. */ |
paul@181 | 343 | |
paul@181 | 344 | config->thread = l4re_env()->main_thread; |
paul@181 | 345 | config->server = L4_INVALID_CAP; |
paul@181 | 346 | |
paul@538 | 347 | /* No notifications and with IRQ to be potentially allocated. Auto-deletion is |
paul@538 | 348 | also disabled by default. */ |
paul@181 | 349 | |
paul@181 | 350 | config->notifications = 0; |
paul@181 | 351 | config->irq = L4_INVALID_CAP; |
paul@538 | 352 | config->auto_deletion = 0; |
paul@181 | 353 | } |
paul@181 | 354 | |
paul@529 | 355 | /* Initialise but do not start a server using the given configuration. */ |
paul@181 | 356 | |
paul@529 | 357 | long ipc_server_prepare_config(ipc_server_config_type *config) |
paul@181 | 358 | { |
paul@181 | 359 | long err; |
paul@181 | 360 | |
paul@181 | 361 | /* Allocate a new IPC gate if one is not already defined. */ |
paul@181 | 362 | |
paul@181 | 363 | if (l4_is_invalid_cap(config->server)) |
paul@286 | 364 | err = ipc_server_new_for_thread(&config->server, config, config->thread); |
paul@181 | 365 | |
paul@621 | 366 | /* Otherwise, bind the existing IPC gate to the thread. */ |
paul@621 | 367 | |
paul@621 | 368 | else |
paul@621 | 369 | err = l4_error(l4_rcv_ep_bind_thread(config->server, config->thread, (l4_umword_t) config)); |
paul@621 | 370 | |
paul@621 | 371 | if (err) |
paul@621 | 372 | return err; |
paul@181 | 373 | |
paul@181 | 374 | /* Allocate an IRQ for notifications if requested. */ |
paul@181 | 375 | |
paul@181 | 376 | if (config->notifications) |
paul@181 | 377 | { |
paul@538 | 378 | err = ipc_server_apply_irq_for_thread(&config->irq, config->thread); |
paul@181 | 379 | |
paul@181 | 380 | if (err) |
paul@181 | 381 | return err; |
paul@379 | 382 | |
paul@538 | 383 | /* Apply auto-deletion if appropriate. */ |
paul@538 | 384 | |
paul@538 | 385 | if (config->auto_deletion) |
paul@538 | 386 | { |
paul@538 | 387 | err = ipc_server_apply_deletion_for_thread(config->server, config->irq, config->thread); |
paul@538 | 388 | |
paul@538 | 389 | if (err) |
paul@538 | 390 | return err; |
paul@538 | 391 | } |
paul@538 | 392 | |
paul@379 | 393 | /* Unmask the interrupt. */ |
paul@379 | 394 | |
paul@379 | 395 | ipc_init_irq(config->irq); |
paul@181 | 396 | } |
paul@181 | 397 | |
paul@529 | 398 | return L4_EOK; |
paul@529 | 399 | } |
paul@529 | 400 | |
paul@529 | 401 | /* Initialise and start a server using the given configuration. */ |
paul@529 | 402 | |
paul@529 | 403 | long ipc_server_start_config(ipc_server_config_type *config) |
paul@529 | 404 | { |
paul@529 | 405 | long err = ipc_server_prepare_config(config); |
paul@529 | 406 | |
paul@529 | 407 | if (err) |
paul@529 | 408 | return err; |
paul@529 | 409 | |
paul@292 | 410 | /* With a separate thread, return the last status value. Otherwise, invoke the |
paul@292 | 411 | main loop. */ |
paul@181 | 412 | |
paul@292 | 413 | if (config->config_thread) |
paul@181 | 414 | return L4_EOK; |
paul@181 | 415 | else |
paul@181 | 416 | return (long) ipc_server_start_mainloop(config); |
paul@181 | 417 | } |
paul@292 | 418 | |
paul@292 | 419 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@292 | 420 | */ |