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@289 | 33 | _ipc_server_loop_for(TYPE##_expected_items, (TYPE *) POINTER, \ |
paul@289 | 34 | (ipc_server_handler_type) handle_##TYPE, \ |
paul@289 | 35 | NAME) |
paul@289 | 36 | |
paul@181 | 37 | /* A handler function type. */ |
paul@181 | 38 | |
paul@181 | 39 | typedef void (*ipc_server_handler_type)(ipc_message_t *, void *); |
paul@181 | 40 | |
paul@181 | 41 | /* A finaliser function type. */ |
paul@181 | 42 | |
paul@181 | 43 | struct ipc_server_config_type; |
paul@181 | 44 | |
paul@181 | 45 | typedef void (*ipc_server_finaliser_type)(struct ipc_server_config_type *); |
paul@181 | 46 | |
paul@181 | 47 | /* A server configuration type. */ |
paul@181 | 48 | |
paul@181 | 49 | typedef struct ipc_server_config_type |
paul@181 | 50 | { |
paul@181 | 51 | /* Component object references. C++ objects may provide different references |
paul@181 | 52 | for the handler (providing access to the exposed interface) and the |
paul@181 | 53 | finaliser (providing access to a distinct control interface). */ |
paul@181 | 54 | |
paul@181 | 55 | void *handler_obj; |
paul@181 | 56 | void *finaliser_obj; |
paul@181 | 57 | |
paul@181 | 58 | /* Expected capability item limit for incoming messages. */ |
paul@181 | 59 | |
paul@181 | 60 | int expected_items; |
paul@181 | 61 | |
paul@181 | 62 | /* Handler for incoming messages and finaliser upon deletion. */ |
paul@181 | 63 | |
paul@181 | 64 | ipc_server_handler_type handler; |
paul@181 | 65 | ipc_server_finaliser_type finaliser; |
paul@181 | 66 | |
paul@181 | 67 | /* Create a new thread. */ |
paul@181 | 68 | |
paul@181 | 69 | int new_thread; |
paul@181 | 70 | |
paul@181 | 71 | /* Thread and IPC capabilities. */ |
paul@181 | 72 | |
paul@181 | 73 | l4_cap_idx_t thread; |
paul@181 | 74 | l4_cap_idx_t server; |
paul@181 | 75 | |
paul@181 | 76 | /* Receive deletion notifications via an IRQ. */ |
paul@181 | 77 | |
paul@181 | 78 | int notifications; |
paul@181 | 79 | l4_cap_idx_t irq; |
paul@181 | 80 | |
paul@181 | 81 | } ipc_server_config_type; |
paul@181 | 82 | |
paul@181 | 83 | |
paul@181 | 84 | |
paul@181 | 85 | /* Associate a notification IRQ with an IPC gate in the main thread. */ |
paul@181 | 86 | |
paul@181 | 87 | long ipc_server_apply_irq(l4_cap_idx_t cap, l4_cap_idx_t *irq); |
paul@181 | 88 | |
paul@181 | 89 | /* Associate a notification IRQ with an IPC gate in the given thread. */ |
paul@181 | 90 | |
paul@181 | 91 | long ipc_server_apply_irq_for_thread(l4_cap_idx_t cap, l4_cap_idx_t *irq, l4_cap_idx_t thread); |
paul@181 | 92 | |
paul@181 | 93 | /* Bind the main thread to a named IPC gate capability. */ |
paul@181 | 94 | |
paul@181 | 95 | long ipc_server_bind(const char *name, l4_umword_t id, l4_cap_idx_t *server); |
paul@181 | 96 | |
paul@181 | 97 | /* Create a new IPC gate for the main thread. */ |
paul@181 | 98 | |
paul@181 | 99 | long ipc_server_new(l4_cap_idx_t *cap, void *obj); |
paul@181 | 100 | |
paul@181 | 101 | /* Create a new IPC gate for the given thread. */ |
paul@181 | 102 | |
paul@181 | 103 | long ipc_server_new_for_thread(l4_cap_idx_t *cap, void *obj, l4_cap_idx_t thread); |
paul@181 | 104 | |
paul@181 | 105 | /* Create an IPC gate for the main thread. */ |
paul@181 | 106 | |
paul@181 | 107 | long ipc_server_new_gate(l4_cap_idx_t *ref, l4_umword_t id); |
paul@181 | 108 | |
paul@181 | 109 | /* Create an IPC gate for the indicated thread. */ |
paul@181 | 110 | |
paul@181 | 111 | long ipc_server_new_gate_for_thread(l4_cap_idx_t *ref, l4_cap_idx_t thread, l4_umword_t id); |
paul@181 | 112 | |
paul@181 | 113 | |
paul@181 | 114 | |
paul@181 | 115 | /* Handle incoming messages for a server. */ |
paul@181 | 116 | |
paul@289 | 117 | long _ipc_server_loop_for(int expected_items, void *handler_obj, |
paul@289 | 118 | ipc_server_handler_type handler, const char *name); |
paul@289 | 119 | |
paul@289 | 120 | /* Handle incoming messages for a server. */ |
paul@289 | 121 | |
paul@286 | 122 | long ipc_server_loop(int expected_items, ipc_server_config_type *config); |
paul@181 | 123 | |
paul@181 | 124 | /* Handle incoming messages and IRQ notifications for a server. */ |
paul@181 | 125 | |
paul@286 | 126 | long ipc_server_managed_loop(int expected_items, ipc_server_config_type *config, |
paul@286 | 127 | l4_cap_idx_t irq); |
paul@181 | 128 | |
paul@181 | 129 | /* A pthread-compatible mainloop initiation function. */ |
paul@181 | 130 | |
paul@181 | 131 | void *ipc_server_start_mainloop(void *data); |
paul@181 | 132 | |
paul@181 | 133 | /* Wait for an incoming message via a dedicated IPC gate for a thread. */ |
paul@181 | 134 | |
paul@181 | 135 | l4_msgtag_t ipc_server_wait(l4_umword_t id); |
paul@181 | 136 | |
paul@181 | 137 | |
paul@181 | 138 | |
paul@181 | 139 | /* Finalise a server configuration. */ |
paul@181 | 140 | |
paul@181 | 141 | void ipc_server_finalise_config(ipc_server_config_type *config); |
paul@181 | 142 | |
paul@181 | 143 | /* Discard any thread created but not initiated. */ |
paul@181 | 144 | |
paul@181 | 145 | void ipc_server_discard_thread(ipc_server_config_type *config); |
paul@181 | 146 | |
paul@181 | 147 | /* Initialise a server configuration. */ |
paul@181 | 148 | |
paul@181 | 149 | void ipc_server_init_config(ipc_server_config_type *config); |
paul@181 | 150 | |
paul@181 | 151 | /* Initialise and start a server using the given configuration. */ |
paul@181 | 152 | |
paul@181 | 153 | long ipc_server_start_config(ipc_server_config_type *config); |
paul@181 | 154 | |
paul@181 | 155 | EXTERN_C_END |