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@181 | 154 | /* A server main loop with no endpoint deletion detection. */ |
paul@181 | 155 | |
paul@181 | 156 | long ipc_server_loop(int expected_items, void *obj, |
paul@181 | 157 | ipc_server_handler_type handler) |
paul@181 | 158 | { |
paul@181 | 159 | return ipc_server_managed_loop(expected_items, obj, L4_INVALID_CAP, handler); |
paul@181 | 160 | } |
paul@181 | 161 | |
paul@181 | 162 | /* A server main loop handling endpoint deletion for an IPC gate dedicated to |
paul@181 | 163 | a particular object within its own thread. */ |
paul@181 | 164 | |
paul@181 | 165 | long ipc_server_managed_loop(int expected_items, void *obj, l4_cap_idx_t irq, |
paul@181 | 166 | ipc_server_handler_type handler) |
paul@181 | 167 | { |
paul@181 | 168 | ipc_message_t msg; |
paul@181 | 169 | l4_umword_t label, irq_label = (l4_umword_t) irq; |
paul@181 | 170 | |
paul@181 | 171 | /* Declare the extent to which capabilities are expected in messages. */ |
paul@181 | 172 | |
paul@181 | 173 | long err = ipc_message_expect(&msg, expected_items); |
paul@181 | 174 | |
paul@181 | 175 | if (err) |
paul@181 | 176 | return err; |
paul@181 | 177 | |
paul@181 | 178 | /* Unmask the interrupt. */ |
paul@181 | 179 | |
paul@181 | 180 | if (l4_is_valid_cap(irq)) |
paul@181 | 181 | ipc_init_irq(irq); |
paul@181 | 182 | |
paul@181 | 183 | /* Wait for an incoming message. */ |
paul@181 | 184 | |
paul@181 | 185 | while (1) |
paul@181 | 186 | { |
paul@181 | 187 | ipc_message_wait(&msg, &label); |
paul@181 | 188 | |
paul@181 | 189 | /* Clear lower label bits. */ |
paul@181 | 190 | |
paul@181 | 191 | label = label & ~3UL; |
paul@181 | 192 | |
paul@181 | 193 | /* Ignore erroneous messages. */ |
paul@181 | 194 | |
paul@181 | 195 | if (l4_ipc_error(msg.tag, l4_utcb())) |
paul@181 | 196 | continue; |
paul@181 | 197 | |
paul@181 | 198 | /* Message involves the IPC gate itself. */ |
paul@181 | 199 | |
paul@181 | 200 | if (label != irq_label) |
paul@181 | 201 | handler(&msg, obj); |
paul@181 | 202 | |
paul@181 | 203 | /* Message involves the IRQ or a termination condition occurred. */ |
paul@181 | 204 | |
paul@181 | 205 | if ((label == irq_label) || msg.terminating) |
paul@181 | 206 | break; |
paul@181 | 207 | } |
paul@181 | 208 | |
paul@181 | 209 | /* Free expected capabilities. */ |
paul@181 | 210 | |
paul@181 | 211 | ipc_message_free(&msg); |
paul@181 | 212 | |
paul@181 | 213 | return L4_EOK; |
paul@181 | 214 | } |
paul@181 | 215 | |
paul@181 | 216 | /* A pthread-compatible mainloop initiation function. */ |
paul@181 | 217 | |
paul@181 | 218 | void *ipc_server_start_mainloop(void *data) |
paul@181 | 219 | { |
paul@181 | 220 | ipc_server_config_type *config = (ipc_server_config_type *) data; |
paul@181 | 221 | |
paul@181 | 222 | long err = ipc_server_managed_loop(config->expected_items, config->handler_obj, |
paul@181 | 223 | config->irq, config->handler); |
paul@181 | 224 | |
paul@181 | 225 | if (config->finaliser != NULL) |
paul@181 | 226 | config->finaliser(config); |
paul@181 | 227 | |
paul@181 | 228 | return (void *) err; |
paul@181 | 229 | } |
paul@181 | 230 | |
paul@181 | 231 | /* Wait for an incoming message via an IPC gate dedicated to a particular |
paul@181 | 232 | object within its own thread. */ |
paul@181 | 233 | |
paul@181 | 234 | l4_msgtag_t ipc_server_wait(l4_umword_t id) |
paul@181 | 235 | { |
paul@181 | 236 | l4_umword_t label; |
paul@181 | 237 | l4_msgtag_t tag; |
paul@181 | 238 | |
paul@181 | 239 | /* Ignore errors and messages not intended for the identified gate. */ |
paul@181 | 240 | |
paul@181 | 241 | do tag = l4_ipc_wait(l4_utcb(), &label, L4_IPC_NEVER); |
paul@181 | 242 | while (l4_ipc_error(tag, l4_utcb()) || (label & ~3UL) != id); |
paul@181 | 243 | |
paul@181 | 244 | return tag; |
paul@181 | 245 | } |
paul@181 | 246 | |
paul@181 | 247 | |
paul@181 | 248 | |
paul@181 | 249 | /* Finalise a server configuration. */ |
paul@181 | 250 | |
paul@181 | 251 | void ipc_server_finalise_config(ipc_server_config_type *config) |
paul@181 | 252 | { |
paul@181 | 253 | if (l4_is_valid_cap(config->server)) |
paul@181 | 254 | { |
paul@181 | 255 | ipc_cap_free_um(config->server); |
paul@181 | 256 | config->server = L4_INVALID_CAP; |
paul@181 | 257 | } |
paul@181 | 258 | |
paul@181 | 259 | if (l4_is_valid_cap(config->irq)) |
paul@181 | 260 | { |
paul@181 | 261 | ipc_cap_free_um(config->irq); |
paul@181 | 262 | config->irq = L4_INVALID_CAP; |
paul@181 | 263 | } |
paul@181 | 264 | } |
paul@181 | 265 | |
paul@181 | 266 | /* Discard any thread created but not initiated. */ |
paul@181 | 267 | |
paul@181 | 268 | void ipc_server_discard_thread(ipc_server_config_type *config) |
paul@181 | 269 | { |
paul@181 | 270 | if (config->new_thread && l4_is_valid_cap(config->thread)) |
paul@181 | 271 | { |
paul@181 | 272 | ipc_cap_free_um(config->thread); |
paul@181 | 273 | config->thread = L4_INVALID_CAP; |
paul@181 | 274 | } |
paul@181 | 275 | } |
paul@181 | 276 | |
paul@181 | 277 | /* Initialise a server configuration. */ |
paul@181 | 278 | |
paul@181 | 279 | void ipc_server_init_config(ipc_server_config_type *config) |
paul@181 | 280 | { |
paul@181 | 281 | /* No object references defined. */ |
paul@181 | 282 | |
paul@181 | 283 | config->handler_obj = NULL; |
paul@181 | 284 | config->finaliser_obj = NULL; |
paul@181 | 285 | |
paul@181 | 286 | /* Expect no capability items by default. */ |
paul@181 | 287 | |
paul@181 | 288 | config->expected_items = 0; |
paul@181 | 289 | |
paul@181 | 290 | /* No handler or finaliser defined. */ |
paul@181 | 291 | |
paul@181 | 292 | config->handler = NULL; |
paul@181 | 293 | config->finaliser = NULL; |
paul@181 | 294 | |
paul@181 | 295 | /* No new thread for the mainloop. */ |
paul@181 | 296 | |
paul@181 | 297 | config->new_thread = 0; |
paul@181 | 298 | |
paul@181 | 299 | /* Main thread by default with IPC gate to be allocated. */ |
paul@181 | 300 | |
paul@181 | 301 | config->thread = l4re_env()->main_thread; |
paul@181 | 302 | config->server = L4_INVALID_CAP; |
paul@181 | 303 | |
paul@181 | 304 | /* No notifications and with IRQ to be potentially allocated. */ |
paul@181 | 305 | |
paul@181 | 306 | config->notifications = 0; |
paul@181 | 307 | config->irq = L4_INVALID_CAP; |
paul@181 | 308 | } |
paul@181 | 309 | |
paul@181 | 310 | /* Initialise and start a server using the given configuration. */ |
paul@181 | 311 | |
paul@181 | 312 | long ipc_server_start_config(ipc_server_config_type *config) |
paul@181 | 313 | { |
paul@181 | 314 | long err; |
paul@181 | 315 | |
paul@181 | 316 | /* Allocate a new IPC gate if one is not already defined. */ |
paul@181 | 317 | |
paul@181 | 318 | if (l4_is_invalid_cap(config->server)) |
paul@181 | 319 | { |
paul@181 | 320 | err = ipc_server_new_for_thread(&config->server, config->handler_obj, config->thread); |
paul@181 | 321 | |
paul@181 | 322 | if (err) |
paul@181 | 323 | return err; |
paul@181 | 324 | } |
paul@181 | 325 | |
paul@181 | 326 | /* Allocate an IRQ for notifications if requested. */ |
paul@181 | 327 | |
paul@181 | 328 | if (config->notifications) |
paul@181 | 329 | { |
paul@181 | 330 | err = ipc_server_apply_irq_for_thread(config->server, &config->irq, config->thread); |
paul@181 | 331 | |
paul@181 | 332 | if (err) |
paul@181 | 333 | return err; |
paul@181 | 334 | } |
paul@181 | 335 | |
paul@181 | 336 | /* With a new thread, return the last status value. Otherwise, invoke the main |
paul@181 | 337 | loop. */ |
paul@181 | 338 | |
paul@181 | 339 | if (config->new_thread) |
paul@181 | 340 | return L4_EOK; |
paul@181 | 341 | else |
paul@181 | 342 | return (long) ipc_server_start_mainloop(config); |
paul@181 | 343 | } |