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