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