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