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