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 handler function type. */ 38 39 typedef void (*ipc_server_handler_type)(ipc_message_t *, void *); 40 41 /* A finaliser function type. */ 42 43 struct ipc_server_config_type; 44 45 typedef void (*ipc_server_finaliser_type)(struct ipc_server_config_type *); 46 47 /* A server configuration type. */ 48 49 typedef struct ipc_server_config_type 50 { 51 /* Component object references. C++ objects may provide different references 52 for the handler (providing access to the exposed interface) and the 53 finaliser (providing access to a distinct control interface). */ 54 55 void *handler_obj; 56 void *finaliser_obj; 57 58 /* Expected capability item limit for incoming messages. */ 59 60 int expected_items; 61 62 /* Handler for incoming messages and finaliser upon deletion. */ 63 64 ipc_server_handler_type handler; 65 ipc_server_finaliser_type finaliser; 66 67 /* Create a new thread. */ 68 69 int new_thread; 70 71 /* Thread and IPC capabilities. */ 72 73 l4_cap_idx_t thread; 74 l4_cap_idx_t server; 75 76 /* Receive deletion notifications via an IRQ. */ 77 78 int notifications; 79 l4_cap_idx_t irq; 80 81 } ipc_server_config_type; 82 83 84 85 /* Associate a notification IRQ with an IPC gate in the main thread. */ 86 87 long ipc_server_apply_irq(l4_cap_idx_t cap, l4_cap_idx_t *irq); 88 89 /* Associate a notification IRQ with an IPC gate in the given thread. */ 90 91 long ipc_server_apply_irq_for_thread(l4_cap_idx_t cap, l4_cap_idx_t *irq, l4_cap_idx_t thread); 92 93 /* Bind the main thread to a named IPC gate capability. */ 94 95 long ipc_server_bind(const char *name, l4_umword_t id, l4_cap_idx_t *server); 96 97 /* Create a new IPC gate for the main thread. */ 98 99 long ipc_server_new(l4_cap_idx_t *cap, void *obj); 100 101 /* Create a new IPC gate for the given thread. */ 102 103 long ipc_server_new_for_thread(l4_cap_idx_t *cap, void *obj, l4_cap_idx_t thread); 104 105 /* Create an IPC gate for the main thread. */ 106 107 long ipc_server_new_gate(l4_cap_idx_t *ref, l4_umword_t id); 108 109 /* Create an IPC gate for the indicated thread. */ 110 111 long ipc_server_new_gate_for_thread(l4_cap_idx_t *ref, l4_cap_idx_t thread, l4_umword_t id); 112 113 114 115 /* Handle incoming messages for a server. */ 116 117 long _ipc_server_loop_for(int expected_items, void *handler_obj, 118 ipc_server_handler_type handler, const char *name); 119 120 /* Handle incoming messages and IRQ notifications for a server. */ 121 122 long ipc_server_managed_loop(int expected_items, ipc_server_config_type *config); 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