paul@181 | 1 | /* |
paul@181 | 2 | * Server binding/registration. |
paul@181 | 3 | * |
paul@181 | 4 | * Copyright (C) 2018, 2019, 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@181 | 5 | * |
paul@181 | 6 | * This program is free software; you can redistribute it and/or |
paul@181 | 7 | * modify it under the terms of the GNU General Public License as |
paul@181 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@181 | 9 | * the License, or (at your option) any later version. |
paul@181 | 10 | * |
paul@181 | 11 | * This program is distributed in the hope that it will be useful, |
paul@181 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@181 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@181 | 14 | * GNU General Public License for more details. |
paul@181 | 15 | * |
paul@181 | 16 | * You should have received a copy of the GNU General Public License |
paul@181 | 17 | * along with this program; if not, write to the Free Software |
paul@181 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@181 | 19 | * Boston, MA 02110-1301, USA |
paul@181 | 20 | */ |
paul@181 | 21 | |
paul@181 | 22 | #include <l4/re/env.h> |
paul@181 | 23 | #include <l4/sys/factory.h> |
paul@181 | 24 | #include <l4/sys/kernel_object.h> |
paul@181 | 25 | #include <l4/sys/rcv_endpoint.h> |
paul@181 | 26 | #include <l4/sys/thread.h> |
paul@181 | 27 | #include <l4/util/util.h> |
paul@181 | 28 | |
paul@181 | 29 | #include <stdlib.h> |
paul@181 | 30 | |
paul@181 | 31 | #include "cap_alloc.h" |
paul@181 | 32 | #include "irq.h" |
paul@181 | 33 | #include "server.h" |
paul@181 | 34 | |
paul@181 | 35 | |
paul@181 | 36 | |
paul@181 | 37 | /* Associate an IRQ with the IPC gate in the main thread to handle gate deletion |
paul@181 | 38 | notifications. */ |
paul@181 | 39 | |
paul@181 | 40 | long ipc_server_apply_irq(l4_cap_idx_t cap, l4_cap_idx_t *irq) |
paul@181 | 41 | { |
paul@181 | 42 | return ipc_server_apply_irq_for_thread(cap, irq, l4re_env()->main_thread); |
paul@181 | 43 | } |
paul@181 | 44 | |
paul@181 | 45 | /* Associate an IRQ with the IPC gate in the given thread to handle gate deletion |
paul@181 | 46 | notifications. */ |
paul@181 | 47 | |
paul@181 | 48 | long ipc_server_apply_irq_for_thread(l4_cap_idx_t cap, l4_cap_idx_t *irq, l4_cap_idx_t thread) |
paul@181 | 49 | { |
paul@181 | 50 | /* Create an IRQ for the gate. */ |
paul@181 | 51 | |
paul@181 | 52 | long err = ipc_create_irq(irq); |
paul@181 | 53 | |
paul@181 | 54 | if (err) |
paul@181 | 55 | return err; |
paul@181 | 56 | |
paul@181 | 57 | /* Bind the IRQ to the thread, presenting the label if it provides the |
paul@181 | 58 | incoming message. */ |
paul@181 | 59 | |
paul@181 | 60 | err = l4_error(l4_rcv_ep_bind_thread(*irq, thread, (l4_umword_t) *irq)); |
paul@181 | 61 | |
paul@181 | 62 | if (err) |
paul@181 | 63 | { |
paul@181 | 64 | ipc_cap_free_um(*irq); |
paul@181 | 65 | return err; |
paul@181 | 66 | } |
paul@181 | 67 | |
paul@181 | 68 | /* Register for notifications. The thread associated with the gate seems to be |
paul@181 | 69 | needed. */ |
paul@181 | 70 | |
paul@181 | 71 | err = l4_error(l4_thread_register_del_irq(thread, *irq)); |
paul@181 | 72 | |
paul@181 | 73 | if (err) |
paul@181 | 74 | { |
paul@181 | 75 | ipc_cap_free_um(*irq); |
paul@181 | 76 | return err; |
paul@181 | 77 | } |
paul@181 | 78 | |
paul@181 | 79 | /* Decrease the reference count on the server capability (IPC gate). */ |
paul@181 | 80 | |
paul@181 | 81 | return l4_error(l4_kobject_dec_refcnt(cap, 1)); |
paul@181 | 82 | } |
paul@181 | 83 | |
paul@181 | 84 | |
paul@181 | 85 | |
paul@181 | 86 | /* Bind the main thread to the named capability, employing an identifier for |
paul@181 | 87 | the IPC gate and returning the capability index. */ |
paul@181 | 88 | |
paul@181 | 89 | long ipc_server_bind(const char *name, l4_umword_t id, l4_cap_idx_t *server) |
paul@181 | 90 | { |
paul@181 | 91 | /* Obtain a reference to the device. */ |
paul@181 | 92 | |
paul@181 | 93 | *server = l4re_env_get_cap(name); |
paul@181 | 94 | |
paul@181 | 95 | if (l4_is_invalid_cap(*server)) |
paul@181 | 96 | return -L4_ENOENT; |
paul@181 | 97 | |
paul@181 | 98 | /* Bind to an IPC gate with the label identifying the server as message |
paul@181 | 99 | destination. */ |
paul@181 | 100 | |
paul@181 | 101 | return l4_error(l4_rcv_ep_bind_thread(*server, l4re_env()->main_thread, id)); |
paul@181 | 102 | } |
paul@181 | 103 | |
paul@181 | 104 | /* Initialise a new server capability, binding it to the main thread. */ |
paul@181 | 105 | |
paul@181 | 106 | long ipc_server_new(l4_cap_idx_t *cap, void *obj) |
paul@181 | 107 | { |
paul@181 | 108 | return ipc_server_new_for_thread(cap, obj, l4re_env()->main_thread); |
paul@181 | 109 | } |
paul@181 | 110 | |
paul@181 | 111 | /* Initialise a new server capability, binding it to the given thread. */ |
paul@181 | 112 | |
paul@181 | 113 | long ipc_server_new_for_thread(l4_cap_idx_t *cap, void *obj, l4_cap_idx_t thread) |
paul@181 | 114 | { |
paul@181 | 115 | l4_umword_t label = (l4_umword_t) obj; |
paul@181 | 116 | |
paul@181 | 117 | /* Create an IPC gate, presenting the resource's label if this gate provides |
paul@181 | 118 | the message. */ |
paul@181 | 119 | |
paul@181 | 120 | return ipc_server_new_gate_for_thread(cap, thread, label); |
paul@181 | 121 | } |
paul@181 | 122 | |
paul@181 | 123 | /* Create an IPC gate, exporting via a new capability an object with the given |
paul@181 | 124 | identifier. */ |
paul@181 | 125 | |
paul@181 | 126 | long ipc_server_new_gate(l4_cap_idx_t *ref, l4_umword_t id) |
paul@181 | 127 | { |
paul@181 | 128 | return ipc_server_new_gate_for_thread(ref, l4re_env()->main_thread, id); |
paul@181 | 129 | } |
paul@181 | 130 | |
paul@181 | 131 | /* Create an IPC gate associated with a specific thread. */ |
paul@181 | 132 | |
paul@181 | 133 | long ipc_server_new_gate_for_thread(l4_cap_idx_t *ref, l4_cap_idx_t thread, l4_umword_t id) |
paul@181 | 134 | { |
paul@181 | 135 | long err; |
paul@181 | 136 | |
paul@181 | 137 | *ref = ipc_cap_alloc(); |
paul@181 | 138 | |
paul@181 | 139 | if (l4_is_invalid_cap(*ref)) |
paul@181 | 140 | return -L4_ENOENT; |
paul@181 | 141 | |
paul@181 | 142 | /* Associate the object with an IPC gate. */ |
paul@181 | 143 | |
paul@181 | 144 | err = l4_error(l4_factory_create_gate(l4re_env()->factory, *ref, thread, id)); |
paul@181 | 145 | |
paul@181 | 146 | if (err) |
paul@181 | 147 | ipc_cap_free_um(*ref); |
paul@181 | 148 | |
paul@181 | 149 | return err; |
paul@181 | 150 | } |
paul@181 | 151 | |
paul@181 | 152 | |
paul@181 | 153 | |
paul@289 | 154 | /* Support for the convenience macro starting a server for a given object. */ |
paul@289 | 155 | |
paul@289 | 156 | long _ipc_server_loop_for(int expected_items, void *handler_obj, |
paul@289 | 157 | ipc_server_handler_type handler, const char *name) |
paul@289 | 158 | { |
paul@289 | 159 | ipc_server_config_type config; |
paul@289 | 160 | long err; |
paul@289 | 161 | |
paul@289 | 162 | if (name != NULL) |
paul@289 | 163 | { |
paul@289 | 164 | err = ipc_server_bind(name, (l4_umword_t) &config, &config.server); |
paul@289 | 165 | if (err) |
paul@289 | 166 | return err; |
paul@289 | 167 | } |
paul@289 | 168 | |
paul@289 | 169 | ipc_server_init_config(&config); |
paul@289 | 170 | config.expected_items = expected_items; |
paul@289 | 171 | config.handler = handler; |
paul@289 | 172 | config.handler_obj = handler_obj; |
paul@289 | 173 | |
paul@289 | 174 | return ipc_server_start_config(&config); |
paul@289 | 175 | } |
paul@289 | 176 | |
paul@181 | 177 | /* A server main loop handling endpoint deletion for an IPC gate dedicated to |
paul@181 | 178 | a particular object within its own thread. */ |
paul@181 | 179 | |
paul@291 | 180 | long ipc_server_managed_loop(int expected_items, ipc_server_config_type *config) |
paul@181 | 181 | { |
paul@181 | 182 | ipc_message_t msg; |
paul@291 | 183 | l4_umword_t label, irq_label = (l4_umword_t) config->irq; |
paul@181 | 184 | |
paul@286 | 185 | /* Permit other endpoints by dynamically interpreting the label. */ |
paul@286 | 186 | |
paul@286 | 187 | ipc_server_config_type *config_from_label; |
paul@286 | 188 | |
paul@181 | 189 | /* Declare the extent to which capabilities are expected in messages. */ |
paul@181 | 190 | |
paul@181 | 191 | long err = ipc_message_expect(&msg, expected_items); |
paul@181 | 192 | |
paul@181 | 193 | if (err) |
paul@181 | 194 | return err; |
paul@181 | 195 | |
paul@181 | 196 | /* Unmask the interrupt. */ |
paul@181 | 197 | |
paul@291 | 198 | if (l4_is_valid_cap(config->irq)) |
paul@291 | 199 | ipc_init_irq(config->irq); |
paul@181 | 200 | |
paul@181 | 201 | /* Wait for an incoming message. */ |
paul@181 | 202 | |
paul@181 | 203 | while (1) |
paul@181 | 204 | { |
paul@181 | 205 | ipc_message_wait(&msg, &label); |
paul@181 | 206 | |
paul@181 | 207 | /* Clear lower label bits. */ |
paul@181 | 208 | |
paul@181 | 209 | label = label & ~3UL; |
paul@181 | 210 | |
paul@181 | 211 | /* Ignore erroneous messages. */ |
paul@181 | 212 | |
paul@181 | 213 | if (l4_ipc_error(msg.tag, l4_utcb())) |
paul@181 | 214 | continue; |
paul@181 | 215 | |
paul@181 | 216 | /* Message involves the IPC gate itself. */ |
paul@181 | 217 | |
paul@181 | 218 | if (label != irq_label) |
paul@286 | 219 | { |
paul@286 | 220 | config_from_label = (ipc_server_config_type *) label; |
paul@286 | 221 | config_from_label->handler(&msg, config_from_label->handler_obj); |
paul@286 | 222 | } |
paul@181 | 223 | |
paul@181 | 224 | /* Message involves the IRQ or a termination condition occurred. */ |
paul@181 | 225 | |
paul@181 | 226 | if ((label == irq_label) || msg.terminating) |
paul@181 | 227 | break; |
paul@181 | 228 | } |
paul@181 | 229 | |
paul@181 | 230 | /* Free expected capabilities. */ |
paul@181 | 231 | |
paul@181 | 232 | ipc_message_free(&msg); |
paul@181 | 233 | |
paul@181 | 234 | return L4_EOK; |
paul@181 | 235 | } |
paul@181 | 236 | |
paul@181 | 237 | /* A pthread-compatible mainloop initiation function. */ |
paul@181 | 238 | |
paul@181 | 239 | void *ipc_server_start_mainloop(void *data) |
paul@181 | 240 | { |
paul@181 | 241 | ipc_server_config_type *config = (ipc_server_config_type *) data; |
paul@181 | 242 | |
paul@291 | 243 | long err = ipc_server_managed_loop(config->expected_items, config); |
paul@181 | 244 | |
paul@181 | 245 | if (config->finaliser != NULL) |
paul@181 | 246 | config->finaliser(config); |
paul@181 | 247 | |
paul@181 | 248 | return (void *) err; |
paul@181 | 249 | } |
paul@181 | 250 | |
paul@181 | 251 | /* Wait for an incoming message via an IPC gate dedicated to a particular |
paul@181 | 252 | object within its own thread. */ |
paul@181 | 253 | |
paul@181 | 254 | l4_msgtag_t ipc_server_wait(l4_umword_t id) |
paul@181 | 255 | { |
paul@181 | 256 | l4_umword_t label; |
paul@181 | 257 | l4_msgtag_t tag; |
paul@181 | 258 | |
paul@181 | 259 | /* Ignore errors and messages not intended for the identified gate. */ |
paul@181 | 260 | |
paul@181 | 261 | do tag = l4_ipc_wait(l4_utcb(), &label, L4_IPC_NEVER); |
paul@181 | 262 | while (l4_ipc_error(tag, l4_utcb()) || (label & ~3UL) != id); |
paul@181 | 263 | |
paul@181 | 264 | return tag; |
paul@181 | 265 | } |
paul@181 | 266 | |
paul@181 | 267 | |
paul@181 | 268 | |
paul@181 | 269 | /* Finalise a server configuration. */ |
paul@181 | 270 | |
paul@181 | 271 | void ipc_server_finalise_config(ipc_server_config_type *config) |
paul@181 | 272 | { |
paul@181 | 273 | if (l4_is_valid_cap(config->server)) |
paul@181 | 274 | { |
paul@181 | 275 | ipc_cap_free_um(config->server); |
paul@181 | 276 | config->server = L4_INVALID_CAP; |
paul@181 | 277 | } |
paul@181 | 278 | |
paul@181 | 279 | if (l4_is_valid_cap(config->irq)) |
paul@181 | 280 | { |
paul@181 | 281 | ipc_cap_free_um(config->irq); |
paul@181 | 282 | config->irq = L4_INVALID_CAP; |
paul@181 | 283 | } |
paul@181 | 284 | } |
paul@181 | 285 | |
paul@181 | 286 | /* Discard any thread created but not initiated. */ |
paul@181 | 287 | |
paul@181 | 288 | void ipc_server_discard_thread(ipc_server_config_type *config) |
paul@181 | 289 | { |
paul@181 | 290 | if (config->new_thread && l4_is_valid_cap(config->thread)) |
paul@181 | 291 | { |
paul@181 | 292 | ipc_cap_free_um(config->thread); |
paul@181 | 293 | config->thread = L4_INVALID_CAP; |
paul@181 | 294 | } |
paul@181 | 295 | } |
paul@181 | 296 | |
paul@181 | 297 | /* Initialise a server configuration. */ |
paul@181 | 298 | |
paul@181 | 299 | void ipc_server_init_config(ipc_server_config_type *config) |
paul@181 | 300 | { |
paul@181 | 301 | /* No object references defined. */ |
paul@181 | 302 | |
paul@181 | 303 | config->handler_obj = NULL; |
paul@181 | 304 | config->finaliser_obj = NULL; |
paul@181 | 305 | |
paul@181 | 306 | /* Expect no capability items by default. */ |
paul@181 | 307 | |
paul@181 | 308 | config->expected_items = 0; |
paul@181 | 309 | |
paul@181 | 310 | /* No handler or finaliser defined. */ |
paul@181 | 311 | |
paul@181 | 312 | config->handler = NULL; |
paul@181 | 313 | config->finaliser = NULL; |
paul@181 | 314 | |
paul@181 | 315 | /* No new thread for the mainloop. */ |
paul@181 | 316 | |
paul@181 | 317 | config->new_thread = 0; |
paul@181 | 318 | |
paul@181 | 319 | /* Main thread by default with IPC gate to be allocated. */ |
paul@181 | 320 | |
paul@181 | 321 | config->thread = l4re_env()->main_thread; |
paul@181 | 322 | config->server = L4_INVALID_CAP; |
paul@181 | 323 | |
paul@181 | 324 | /* No notifications and with IRQ to be potentially allocated. */ |
paul@181 | 325 | |
paul@181 | 326 | config->notifications = 0; |
paul@181 | 327 | config->irq = L4_INVALID_CAP; |
paul@181 | 328 | } |
paul@181 | 329 | |
paul@181 | 330 | /* Initialise and start a server using the given configuration. */ |
paul@181 | 331 | |
paul@181 | 332 | long ipc_server_start_config(ipc_server_config_type *config) |
paul@181 | 333 | { |
paul@181 | 334 | long err; |
paul@181 | 335 | |
paul@181 | 336 | /* Allocate a new IPC gate if one is not already defined. */ |
paul@181 | 337 | |
paul@181 | 338 | if (l4_is_invalid_cap(config->server)) |
paul@181 | 339 | { |
paul@286 | 340 | err = ipc_server_new_for_thread(&config->server, config, config->thread); |
paul@181 | 341 | |
paul@181 | 342 | if (err) |
paul@181 | 343 | return err; |
paul@181 | 344 | } |
paul@181 | 345 | |
paul@181 | 346 | /* Allocate an IRQ for notifications if requested. */ |
paul@181 | 347 | |
paul@181 | 348 | if (config->notifications) |
paul@181 | 349 | { |
paul@181 | 350 | err = ipc_server_apply_irq_for_thread(config->server, &config->irq, config->thread); |
paul@181 | 351 | |
paul@181 | 352 | if (err) |
paul@181 | 353 | return err; |
paul@181 | 354 | } |
paul@181 | 355 | |
paul@181 | 356 | /* With a new thread, return the last status value. Otherwise, invoke the main |
paul@181 | 357 | loop. */ |
paul@181 | 358 | |
paul@181 | 359 | if (config->new_thread) |
paul@181 | 360 | return L4_EOK; |
paul@181 | 361 | else |
paul@181 | 362 | return (long) ipc_server_start_mainloop(config); |
paul@181 | 363 | } |