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