1 /* 2 * Server binding/registration. 3 * 4 * Copyright (C) 2018, 2019, 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #pragma once 23 24 #include <l4/sys/types.h> 25 26 #include <ipc/message.h> 27 28 EXTERN_C_BEGIN 29 30 /* A convenience macro for invoking the server mainloop. */ 31 32 #define ipc_server_loop_for(TYPE, POINTER, NAME) \ 33 _ipc_server_loop_for(TYPE##_expected_items, (TYPE *) POINTER, \ 34 (ipc_server_handler_type) handle_##TYPE, \ 35 NAME) 36 37 /* A convenience macro for adding a configuration to an existing server. */ 38 39 #define ipc_server_add_config(CONFIG, TYPE, POINTER, THREAD) \ 40 _ipc_server_add_config(CONFIG, TYPE##_expected_items, (TYPE *) POINTER, \ 41 (ipc_server_handler_type) handle_##TYPE, \ 42 THREAD) 43 44 /* A handler function type. */ 45 46 typedef void (*ipc_server_handler_type)(ipc_message_t *, void *); 47 48 /* A finaliser function type. */ 49 50 struct ipc_server_config_type; 51 52 typedef void (*ipc_server_finaliser_type)(struct ipc_server_config_type *); 53 54 /* A server configuration type. */ 55 56 typedef struct ipc_server_config_type 57 { 58 /* Component object references. C++ objects may provide different references 59 for the handler (providing access to the exposed interface) and the 60 finaliser (providing access to a distinct control interface). */ 61 62 void *handler_obj; 63 void *finaliser_obj; 64 65 /* Expected capability item limit for incoming messages. */ 66 67 int expected_items; 68 69 /* Handler for incoming messages and finaliser upon deletion. */ 70 71 ipc_server_handler_type handler; 72 ipc_server_finaliser_type finaliser; 73 74 /* Configure a thread instead of using the current thread. */ 75 76 int config_thread; 77 78 /* Thread and IPC capabilities. */ 79 80 l4_cap_idx_t thread; 81 l4_cap_idx_t server; 82 83 /* Receive deletion notifications via an IRQ. */ 84 85 int notifications; 86 l4_cap_idx_t irq; 87 88 } ipc_server_config_type; 89 90 91 92 /* Associate a notification IRQ with an IPC gate in the main thread. */ 93 94 long ipc_server_apply_irq(l4_cap_idx_t cap, l4_cap_idx_t *irq); 95 96 /* Associate a notification IRQ with an IPC gate in the given thread. */ 97 98 long ipc_server_apply_irq_for_thread(l4_cap_idx_t cap, l4_cap_idx_t *irq, l4_cap_idx_t thread); 99 100 /* Bind the main thread to a named IPC gate capability. */ 101 102 long ipc_server_bind(const char *name, l4_umword_t id, l4_cap_idx_t *server); 103 104 /* Create a new IPC gate for the main thread. */ 105 106 long ipc_server_new(l4_cap_idx_t *cap, void *obj); 107 108 /* Create a new IPC gate for the given thread. */ 109 110 long ipc_server_new_for_thread(l4_cap_idx_t *cap, void *obj, l4_cap_idx_t thread); 111 112 /* Create an IPC gate for the main thread. */ 113 114 long ipc_server_new_gate(l4_cap_idx_t *ref, l4_umword_t id); 115 116 /* Create an IPC gate for the indicated thread. */ 117 118 long ipc_server_new_gate_for_thread(l4_cap_idx_t *ref, l4_cap_idx_t thread, l4_umword_t id); 119 120 121 122 /* Handle incoming messages for a server. */ 123 124 long _ipc_server_loop_for(int expected_items, void *handler_obj, 125 ipc_server_handler_type handler, const char *name); 126 127 /* Add handling of incoming messages to an existing server. */ 128 129 long _ipc_server_add_config(ipc_server_config_type *config, int expected_items, 130 void *handler_obj, ipc_server_handler_type handler, 131 l4_cap_idx_t thread); 132 133 /* Handle incoming messages and IRQ notifications for a server. */ 134 135 long ipc_server_managed_loop(int expected_items, ipc_server_config_type *config); 136 137 /* A pthread-compatible mainloop initiation function. */ 138 139 void *ipc_server_start_mainloop(void *data); 140 141 /* Wait for an incoming message via a dedicated IPC gate for a thread. */ 142 143 l4_msgtag_t ipc_server_wait(l4_umword_t id); 144 145 146 147 /* Finalise a server configuration. */ 148 149 void ipc_server_finalise_config(ipc_server_config_type *config); 150 151 /* Discard any thread created but not initiated. */ 152 153 void ipc_server_discard_thread(ipc_server_config_type *config); 154 155 /* Initialise a server configuration. */ 156 157 void ipc_server_init_config(ipc_server_config_type *config); 158 159 /* Initialise and start a server using the given configuration. */ 160 161 long ipc_server_start_config(ipc_server_config_type *config); 162 163 EXTERN_C_END 164 165 /* vim: tabstop=2 expandtab shiftwidth=2 166 */