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 long err; 149 150 if (name != NULL) 151 config.server = l4re_env_get_cap(name); 152 153 _ipc_server_init_for(&config, default_config, handler_obj); 154 155 return ipc_server_start_config(&config); 156 } 157 158 /* Initialise a server for a given object. */ 159 160 long _ipc_server_init_for(ipc_server_config_type *config, 161 ipc_server_default_config_type *default_config, 162 void *handler_obj) 163 { 164 ipc_server_init_config(config); 165 166 config->expected_items = default_config->expected_items; 167 config->handler = default_config->handler; 168 config->handler_obj = handler_obj; 169 170 return L4_EOK; 171 } 172 173 /* Associate a new configuration with an existing server endpoint. */ 174 175 long _ipc_server_add_config(ipc_server_config_type *config, 176 ipc_server_default_config_type *default_config, 177 void *handler_obj, 178 l4_cap_idx_t thread) 179 { 180 _ipc_server_init_for(config, default_config, handler_obj); 181 return ipc_server_start_config_thread(config, thread); 182 } 183 184 /* Complete initialisation of a server in the given thread. */ 185 186 long ipc_server_start_config_thread(ipc_server_config_type *config, 187 l4_cap_idx_t thread) 188 { 189 config->thread = thread; 190 config->config_thread = 1; 191 192 return ipc_server_prepare_config(config); 193 } 194 195 196 197 /* A server main loop handling endpoint deletion for an IPC gate dedicated to 198 a particular object within its own thread. */ 199 200 long ipc_server_managed_loop(ipc_server_config_type *config) 201 { 202 ipc_message_t msg; 203 l4_umword_t label, irq_label; 204 205 /* Permit other endpoints by dynamically interpreting the label. */ 206 207 ipc_server_config_type *config_from_label; 208 209 /* Declare the extent to which capabilities are expected in messages. */ 210 211 long err = ipc_message_expect(&msg, config->expected_items); 212 213 if (err) 214 return err; 215 216 /* Wait for an incoming message. */ 217 218 while (1) 219 { 220 ipc_message_wait(&msg, &label); 221 222 /* Clear lower label bits. */ 223 224 label = label & ~3UL; 225 226 /* Since we interpret the label as a pointer, we should detect zero labels 227 and report them since they are likely to be interrupts inappropriately 228 bound to this thread. */ 229 230 if (!label) 231 continue; 232 233 /* Ignore erroneous messages. */ 234 235 if (l4_ipc_error(msg.tag, l4_utcb())) 236 continue; 237 238 /* Message involves the IPC gate itself. */ 239 240 irq_label = (l4_umword_t) config->irq; 241 242 if (label != irq_label) 243 { 244 config_from_label = (ipc_server_config_type *) label; 245 config_from_label->handler(&msg, config_from_label->handler_obj); 246 } 247 248 /* Message involves the IRQ or a termination condition occurred. */ 249 250 else if ((label == irq_label) || msg.terminating) 251 break; 252 } 253 254 /* Free expected capabilities. */ 255 256 ipc_message_free(&msg); 257 258 return L4_EOK; 259 } 260 261 /* A pthread-compatible mainloop initiation function. */ 262 263 void *ipc_server_start_mainloop(void *data) 264 { 265 ipc_server_config_type *config = (ipc_server_config_type *) data; 266 267 long err = ipc_server_managed_loop(config); 268 269 if (config->finaliser != NULL) 270 config->finaliser(config); 271 272 return (void *) err; 273 } 274 275 /* Wait for an incoming message via an IPC gate dedicated to a particular 276 object within its own thread. */ 277 278 l4_msgtag_t ipc_server_wait(l4_umword_t id) 279 { 280 l4_umword_t label; 281 l4_msgtag_t tag; 282 283 /* Ignore errors and messages not intended for the identified gate. */ 284 285 do tag = l4_ipc_wait(l4_utcb(), &label, L4_IPC_NEVER); 286 while (l4_ipc_error(tag, l4_utcb()) || (label & ~3UL) != id); 287 288 return tag; 289 } 290 291 292 293 /* Finalise a server configuration. */ 294 295 void ipc_server_finalise_config(ipc_server_config_type *config) 296 { 297 if (l4_is_valid_cap(config->server)) 298 { 299 ipc_cap_free_um(config->server); 300 config->server = L4_INVALID_CAP; 301 } 302 303 if (l4_is_valid_cap(config->irq)) 304 { 305 ipc_cap_free_um(config->irq); 306 config->irq = L4_INVALID_CAP; 307 } 308 } 309 310 /* Discard any uninitiated thread. */ 311 312 void ipc_server_discard_thread(ipc_server_config_type *config) 313 { 314 if (config->config_thread && l4_is_valid_cap(config->thread)) 315 { 316 ipc_cap_free_um(config->thread); 317 config->thread = L4_INVALID_CAP; 318 } 319 } 320 321 /* Initialise a server configuration. */ 322 323 void ipc_server_init_config(ipc_server_config_type *config) 324 { 325 /* No object references defined. */ 326 327 config->handler_obj = NULL; 328 config->finaliser_obj = NULL; 329 330 /* Expect no capability items by default. */ 331 332 config->expected_items = 0; 333 334 /* No handler or finaliser defined. */ 335 336 config->handler = NULL; 337 config->finaliser = NULL; 338 339 /* No separate thread for the main loop. */ 340 341 config->config_thread = 0; 342 343 /* Main thread by default with IPC gate to be allocated. */ 344 345 config->thread = l4re_env()->main_thread; 346 config->server = L4_INVALID_CAP; 347 348 /* No notifications and with IRQ to be potentially allocated. Auto-deletion is 349 also disabled by default. */ 350 351 config->notifications = 0; 352 config->irq = L4_INVALID_CAP; 353 config->auto_deletion = 0; 354 } 355 356 /* Initialise but do not start a server using the given configuration. */ 357 358 long ipc_server_prepare_config(ipc_server_config_type *config) 359 { 360 long err; 361 362 /* Allocate a new IPC gate if one is not already defined. */ 363 364 if (l4_is_invalid_cap(config->server)) 365 err = ipc_server_new_for_thread(&config->server, config, config->thread); 366 367 /* Otherwise, bind the existing IPC gate to the thread. */ 368 369 else 370 err = l4_error(l4_rcv_ep_bind_thread(config->server, config->thread, (l4_umword_t) config)); 371 372 if (err) 373 return err; 374 375 /* Allocate an IRQ for notifications if requested. */ 376 377 if (config->notifications) 378 { 379 err = ipc_server_apply_irq_for_thread(&config->irq, config->thread); 380 381 if (err) 382 return err; 383 384 /* Apply auto-deletion if appropriate. */ 385 386 if (config->auto_deletion) 387 { 388 err = ipc_server_apply_deletion_for_thread(config->server, config->irq, config->thread); 389 390 if (err) 391 return err; 392 } 393 394 /* Unmask the interrupt. */ 395 396 ipc_init_irq(config->irq); 397 } 398 399 return L4_EOK; 400 } 401 402 /* Initialise and start a server using the given configuration. */ 403 404 long ipc_server_start_config(ipc_server_config_type *config) 405 { 406 long err = ipc_server_prepare_config(config); 407 408 if (err) 409 return err; 410 411 /* With a separate thread, return the last status value. Otherwise, invoke the 412 main loop. */ 413 414 if (config->config_thread) 415 return L4_EOK; 416 else 417 return (long) ipc_server_start_mainloop(config); 418 } 419 420 /* vim: tabstop=2 expandtab shiftwidth=2 421 */