[06502f7d] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[06502f7d] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[b2951e2] | 27 | */
|
---|
| 28 |
|
---|
[a46da63] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[c07544d3] | 33 | */
|
---|
[06502f7d] | 34 |
|
---|
[80649a91] | 35 | /**
|
---|
| 36 | * Asynchronous library
|
---|
| 37 | *
|
---|
[c07544d3] | 38 | * The aim of this library is to provide a facility for writing programs which
|
---|
| 39 | * utilize the asynchronous nature of HelenOS IPC, yet using a normal way of
|
---|
| 40 | * programming.
|
---|
[80649a91] | 41 | *
|
---|
[79ae36dd] | 42 | * You should be able to write very simple multithreaded programs. The async
|
---|
| 43 | * framework will automatically take care of most of the synchronization
|
---|
| 44 | * problems.
|
---|
[80649a91] | 45 | *
|
---|
[9591265] | 46 | * Example of use (pseudo C):
|
---|
[c07544d3] | 47 | *
|
---|
[80649a91] | 48 | * 1) Multithreaded client application
|
---|
[9591265] | 49 | *
|
---|
[c07544d3] | 50 | * fibril_create(fibril1, ...);
|
---|
| 51 | * fibril_create(fibril2, ...);
|
---|
| 52 | * ...
|
---|
| 53 | *
|
---|
| 54 | * int fibril1(void *arg)
|
---|
| 55 | * {
|
---|
[79ae36dd] | 56 | * conn = async_connect_me_to(...);
|
---|
| 57 | *
|
---|
| 58 | * exch = async_exchange_begin(conn);
|
---|
| 59 | * c1 = async_send(exch);
|
---|
| 60 | * async_exchange_end(exch);
|
---|
| 61 | *
|
---|
| 62 | * exch = async_exchange_begin(conn);
|
---|
| 63 | * c2 = async_send(exch);
|
---|
| 64 | * async_exchange_end(exch);
|
---|
| 65 | *
|
---|
[c07544d3] | 66 | * async_wait_for(c1);
|
---|
| 67 | * async_wait_for(c2);
|
---|
| 68 | * ...
|
---|
| 69 | * }
|
---|
[80649a91] | 70 | *
|
---|
| 71 | *
|
---|
| 72 | * 2) Multithreaded server application
|
---|
| 73 | *
|
---|
[c07544d3] | 74 | * main()
|
---|
| 75 | * {
|
---|
| 76 | * async_manager();
|
---|
| 77 | * }
|
---|
| 78 | *
|
---|
[984a9ba] | 79 | * port_handler(ipc_call_t *icall)
|
---|
[c07544d3] | 80 | * {
|
---|
| 81 | * if (want_refuse) {
|
---|
[984a9ba] | 82 | * async_answer_0(icall, ELIMIT);
|
---|
[c07544d3] | 83 | * return;
|
---|
| 84 | * }
|
---|
[80649a91] | 85 | *
|
---|
[984a9ba] | 86 | * async_answer_0(icall, EOK);
|
---|
[53ca318] | 87 | *
|
---|
[984a9ba] | 88 | * async_get_call(&call);
|
---|
| 89 | * somehow_handle_the_call(&call);
|
---|
| 90 | * async_answer_2(&call, 1, 2, 3);
|
---|
| 91 | *
|
---|
| 92 | * async_get_call(&call);
|
---|
[c07544d3] | 93 | * ...
|
---|
| 94 | * }
|
---|
[a2cd194] | 95 | *
|
---|
[80649a91] | 96 | */
|
---|
[9591265] | 97 |
|
---|
[4805495] | 98 | #define _LIBC_ASYNC_C_
|
---|
[64d2b10] | 99 | #include <ipc/ipc.h>
|
---|
[80649a91] | 100 | #include <async.h>
|
---|
[49a796f1] | 101 | #include "../private/async.h"
|
---|
[4805495] | 102 | #undef _LIBC_ASYNC_C_
|
---|
[64d2b10] | 103 |
|
---|
[8820544] | 104 | #include <ipc/irq.h>
|
---|
| 105 | #include <ipc/event.h>
|
---|
[bc1f1c2] | 106 | #include <fibril.h>
|
---|
[d9c8c81] | 107 | #include <adt/hash_table.h>
|
---|
[853802e] | 108 | #include <adt/hash.h>
|
---|
[d9c8c81] | 109 | #include <adt/list.h>
|
---|
[80649a91] | 110 | #include <assert.h>
|
---|
| 111 | #include <errno.h>
|
---|
[bd41ac52] | 112 | #include <time.h>
|
---|
[3e6a98c5] | 113 | #include <stdbool.h>
|
---|
[38d150e] | 114 | #include <stdlib.h>
|
---|
[79ae36dd] | 115 | #include <mem.h>
|
---|
| 116 | #include <stdlib.h>
|
---|
[e2ab36f1] | 117 | #include <macros.h>
|
---|
[514d561] | 118 | #include <str_error.h>
|
---|
[101516d] | 119 | #include <as.h>
|
---|
[ae6021d] | 120 | #include <abi/mm/as.h>
|
---|
[49a796f1] | 121 | #include "../private/libc.h"
|
---|
[d73d992] | 122 | #include "../private/fibril.h"
|
---|
[5da7199] | 123 |
|
---|
[8dab988] | 124 | #define DPRINTF(...) ((void) 0)
|
---|
| 125 |
|
---|
[79ae36dd] | 126 | /* Client connection data */
|
---|
[c80fdd0] | 127 | typedef struct {
|
---|
[062d900] | 128 | ht_link_t link;
|
---|
[a35b458] | 129 |
|
---|
[649f087] | 130 | task_id_t in_task_id;
|
---|
[498ced1] | 131 | int refcnt;
|
---|
[c80fdd0] | 132 | void *data;
|
---|
| 133 | } client_t;
|
---|
| 134 |
|
---|
[79ae36dd] | 135 | /* Server connection data */
|
---|
[80649a91] | 136 | typedef struct {
|
---|
[514d561] | 137 | /** Fibril handling the connection. */
|
---|
| 138 | fid_t fid;
|
---|
[a35b458] | 139 |
|
---|
[e70bfa5] | 140 | /** Hash table link. */
|
---|
[062d900] | 141 | ht_link_t link;
|
---|
[a35b458] | 142 |
|
---|
[e2ab36f1] | 143 | /** Incoming client task ID. */
|
---|
| 144 | task_id_t in_task_id;
|
---|
[a35b458] | 145 |
|
---|
[23882034] | 146 | /** Link to the client tracking structure. */
|
---|
| 147 | client_t *client;
|
---|
[a35b458] | 148 |
|
---|
[1de92fb0] | 149 | /** Channel for messages that should be delivered to this fibril. */
|
---|
| 150 | mpsc_t *msg_channel;
|
---|
[a35b458] | 151 |
|
---|
[e70bfa5] | 152 | /** Call data of the opening call. */
|
---|
[80649a91] | 153 | ipc_call_t call;
|
---|
[a35b458] | 154 |
|
---|
[e70bfa5] | 155 | /** Fibril function that will be used to handle the connection. */
|
---|
[b688fd8] | 156 | async_port_handler_t handler;
|
---|
[a35b458] | 157 |
|
---|
[b688fd8] | 158 | /** Client data */
|
---|
| 159 | void *data;
|
---|
[80649a91] | 160 | } connection_t;
|
---|
| 161 |
|
---|
[8dab988] | 162 | /* Member of notification_t::msg_list. */
|
---|
| 163 | typedef struct {
|
---|
| 164 | link_t link;
|
---|
| 165 | ipc_call_t calldata;
|
---|
| 166 | } notification_msg_t;
|
---|
| 167 |
|
---|
[8820544] | 168 | /* Notification data */
|
---|
| 169 | typedef struct {
|
---|
[3b1cc8d] | 170 | /** notification_hash_table link */
|
---|
| 171 | ht_link_t htlink;
|
---|
| 172 |
|
---|
| 173 | /** notification_queue link */
|
---|
| 174 | link_t qlink;
|
---|
[a35b458] | 175 |
|
---|
[8820544] | 176 | /** Notification method */
|
---|
| 177 | sysarg_t imethod;
|
---|
[a35b458] | 178 |
|
---|
[8820544] | 179 | /** Notification handler */
|
---|
| 180 | async_notification_handler_t handler;
|
---|
[a35b458] | 181 |
|
---|
[3b1cc8d] | 182 | /** Notification handler argument */
|
---|
| 183 | void *arg;
|
---|
| 184 |
|
---|
[8dab988] | 185 | /** List of arrived notifications. */
|
---|
| 186 | list_t msg_list;
|
---|
[8820544] | 187 | } notification_t;
|
---|
| 188 |
|
---|
[bc1f1c2] | 189 | /** Identifier of the incoming connection handled by the current fibril. */
|
---|
[79ae36dd] | 190 | static fibril_local connection_t *fibril_connection;
|
---|
[e70bfa5] | 191 |
|
---|
[46eec3b] | 192 | static void *default_client_data_constructor(void)
|
---|
| 193 | {
|
---|
| 194 | return NULL;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | static void default_client_data_destructor(void *data)
|
---|
| 198 | {
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | static async_client_data_ctor_t async_client_data_create =
|
---|
| 202 | default_client_data_constructor;
|
---|
| 203 | static async_client_data_dtor_t async_client_data_destroy =
|
---|
| 204 | default_client_data_destructor;
|
---|
| 205 |
|
---|
| 206 | void async_set_client_data_constructor(async_client_data_ctor_t ctor)
|
---|
| 207 | {
|
---|
[f302586] | 208 | assert(async_client_data_create == default_client_data_constructor);
|
---|
[46eec3b] | 209 | async_client_data_create = ctor;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | void async_set_client_data_destructor(async_client_data_dtor_t dtor)
|
---|
| 213 | {
|
---|
[f302586] | 214 | assert(async_client_data_destroy == default_client_data_destructor);
|
---|
[46eec3b] | 215 | async_client_data_destroy = dtor;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[241f1985] | 218 | static async_port_handler_t implicit_connection = NULL;
|
---|
| 219 |
|
---|
| 220 | /** Setter for implicit_connection function pointer.
|
---|
| 221 | *
|
---|
| 222 | * @param conn Function that will implement a new connection fibril for
|
---|
| 223 | * unrouted calls.
|
---|
| 224 | *
|
---|
| 225 | */
|
---|
| 226 | void async_set_implicit_connection(async_port_handler_t conn)
|
---|
| 227 | {
|
---|
[102f641] | 228 | assert(implicit_connection == NULL);
|
---|
| 229 | implicit_connection = conn;
|
---|
[241f1985] | 230 | }
|
---|
| 231 |
|
---|
[45c8eea] | 232 | static fibril_rmutex_t client_mutex;
|
---|
[c80fdd0] | 233 | static hash_table_t client_hash_table;
|
---|
[3b1cc8d] | 234 |
|
---|
| 235 | // TODO: lockfree notification_queue?
|
---|
[45c8eea] | 236 | static fibril_rmutex_t notification_mutex;
|
---|
[8820544] | 237 | static hash_table_t notification_hash_table;
|
---|
[3b1cc8d] | 238 | static LIST_INITIALIZE(notification_queue);
|
---|
| 239 | static FIBRIL_SEMAPHORE_INITIALIZE(notification_semaphore, 0);
|
---|
| 240 |
|
---|
[8dab988] | 241 | static LIST_INITIALIZE(notification_freelist);
|
---|
| 242 | static long notification_freelist_total = 0;
|
---|
| 243 | static long notification_freelist_used = 0;
|
---|
| 244 |
|
---|
[8820544] | 245 | static sysarg_t notification_avail = 0;
|
---|
| 246 |
|
---|
[5e801dc] | 247 | static size_t client_key_hash(const void *key)
|
---|
[c80fdd0] | 248 | {
|
---|
[5e801dc] | 249 | const task_id_t *in_task_id = key;
|
---|
| 250 | return *in_task_id;
|
---|
[c80fdd0] | 251 | }
|
---|
| 252 |
|
---|
[062d900] | 253 | static size_t client_hash(const ht_link_t *item)
|
---|
[c80fdd0] | 254 | {
|
---|
[062d900] | 255 | client_t *client = hash_table_get_inst(item, client_t, link);
|
---|
| 256 | return client_key_hash(&client->in_task_id);
|
---|
[c80fdd0] | 257 | }
|
---|
| 258 |
|
---|
[5e801dc] | 259 | static bool client_key_equal(const void *key, const ht_link_t *item)
|
---|
[c80fdd0] | 260 | {
|
---|
[5e801dc] | 261 | const task_id_t *in_task_id = key;
|
---|
[062d900] | 262 | client_t *client = hash_table_get_inst(item, client_t, link);
|
---|
[5e801dc] | 263 | return *in_task_id == client->in_task_id;
|
---|
[c80fdd0] | 264 | }
|
---|
| 265 |
|
---|
| 266 | /** Operations for the client hash table. */
|
---|
[062d900] | 267 | static hash_table_ops_t client_hash_table_ops = {
|
---|
[c80fdd0] | 268 | .hash = client_hash,
|
---|
[062d900] | 269 | .key_hash = client_key_hash,
|
---|
| 270 | .key_equal = client_key_equal,
|
---|
[4e00f87] | 271 | .equal = NULL,
|
---|
| 272 | .remove_callback = NULL
|
---|
[c80fdd0] | 273 | };
|
---|
[80649a91] | 274 |
|
---|
[9ef495f] | 275 | static client_t *async_client_get(task_id_t client_id, bool create)
|
---|
| 276 | {
|
---|
| 277 | client_t *client = NULL;
|
---|
[a35b458] | 278 |
|
---|
[9bde0d5] | 279 | fibril_rmutex_lock(&client_mutex);
|
---|
[9ef495f] | 280 | ht_link_t *link = hash_table_find(&client_hash_table, &client_id);
|
---|
| 281 | if (link) {
|
---|
| 282 | client = hash_table_get_inst(link, client_t, link);
|
---|
[498ced1] | 283 | client->refcnt++;
|
---|
[9ef495f] | 284 | } else if (create) {
|
---|
[3bd1d7d4] | 285 | // TODO: move the malloc out of critical section
|
---|
[9bde0d5] | 286 | /* malloc() is rmutex safe. */
|
---|
[9ef495f] | 287 | client = malloc(sizeof(client_t));
|
---|
| 288 | if (client) {
|
---|
| 289 | client->in_task_id = client_id;
|
---|
| 290 | client->data = async_client_data_create();
|
---|
[a35b458] | 291 |
|
---|
[498ced1] | 292 | client->refcnt = 1;
|
---|
[9ef495f] | 293 | hash_table_insert(&client_hash_table, &client->link);
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
[a35b458] | 296 |
|
---|
[9bde0d5] | 297 | fibril_rmutex_unlock(&client_mutex);
|
---|
[9ef495f] | 298 | return client;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | static void async_client_put(client_t *client)
|
---|
| 302 | {
|
---|
| 303 | bool destroy;
|
---|
[a35b458] | 304 |
|
---|
[9bde0d5] | 305 | fibril_rmutex_lock(&client_mutex);
|
---|
[a35b458] | 306 |
|
---|
[498ced1] | 307 | if (--client->refcnt == 0) {
|
---|
[9ef495f] | 308 | hash_table_remove(&client_hash_table, &client->in_task_id);
|
---|
| 309 | destroy = true;
|
---|
| 310 | } else
|
---|
| 311 | destroy = false;
|
---|
[a35b458] | 312 |
|
---|
[9bde0d5] | 313 | fibril_rmutex_unlock(&client_mutex);
|
---|
[a35b458] | 314 |
|
---|
[9ef495f] | 315 | if (destroy) {
|
---|
| 316 | if (client->data)
|
---|
| 317 | async_client_data_destroy(client->data);
|
---|
[a35b458] | 318 |
|
---|
[9ef495f] | 319 | free(client);
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | /** Wrapper for client connection fibril.
|
---|
| 324 | *
|
---|
| 325 | * When a new connection arrives, a fibril with this implementing
|
---|
| 326 | * function is created.
|
---|
| 327 | *
|
---|
| 328 | * @param arg Connection structure pointer.
|
---|
| 329 | *
|
---|
| 330 | * @return Always zero.
|
---|
| 331 | *
|
---|
| 332 | */
|
---|
[b7fd2a0] | 333 | static errno_t connection_fibril(void *arg)
|
---|
[9ef495f] | 334 | {
|
---|
| 335 | assert(arg);
|
---|
[a35b458] | 336 |
|
---|
[9ef495f] | 337 | /*
|
---|
| 338 | * Setup fibril-local connection pointer.
|
---|
| 339 | */
|
---|
| 340 | fibril_connection = (connection_t *) arg;
|
---|
[a35b458] | 341 |
|
---|
[6769005] | 342 | mpsc_t *c = fibril_connection->msg_channel;
|
---|
| 343 |
|
---|
[9ef495f] | 344 | /*
|
---|
| 345 | * Add our reference for the current connection in the client task
|
---|
| 346 | * tracking structure. If this is the first reference, create and
|
---|
| 347 | * hash in a new tracking structure.
|
---|
| 348 | */
|
---|
[a35b458] | 349 |
|
---|
[9ef495f] | 350 | client_t *client = async_client_get(fibril_connection->in_task_id, true);
|
---|
| 351 | if (!client) {
|
---|
[061274f] | 352 | ipc_answer_0(fibril_connection->call.cap_handle, ENOMEM);
|
---|
[6769005] | 353 | goto out;
|
---|
[9ef495f] | 354 | }
|
---|
[a35b458] | 355 |
|
---|
[9ef495f] | 356 | fibril_connection->client = client;
|
---|
[a35b458] | 357 |
|
---|
[9ef495f] | 358 | /*
|
---|
| 359 | * Call the connection handler function.
|
---|
| 360 | */
|
---|
[984a9ba] | 361 | fibril_connection->handler(&fibril_connection->call,
|
---|
| 362 | fibril_connection->data);
|
---|
[a35b458] | 363 |
|
---|
[9ef495f] | 364 | /*
|
---|
| 365 | * Remove the reference for this client task connection.
|
---|
| 366 | */
|
---|
| 367 | async_client_put(client);
|
---|
[a35b458] | 368 |
|
---|
[1de92fb0] | 369 | /*
|
---|
| 370 | * Close the channel, if it isn't closed already.
|
---|
| 371 | */
|
---|
| 372 | mpsc_close(c);
|
---|
| 373 |
|
---|
[9ef495f] | 374 | /*
|
---|
| 375 | * Answer all remaining messages with EHANGUP.
|
---|
| 376 | */
|
---|
[1de92fb0] | 377 | ipc_call_t call;
|
---|
| 378 | while (mpsc_receive(c, &call, NULL) == EOK)
|
---|
| 379 | ipc_answer_0(call.cap_handle, EHANGUP);
|
---|
[a35b458] | 380 |
|
---|
[9ef495f] | 381 | /*
|
---|
[1de92fb0] | 382 | * Clean up memory.
|
---|
[9ef495f] | 383 | */
|
---|
[6769005] | 384 | out:
|
---|
[1de92fb0] | 385 | mpsc_destroy(c);
|
---|
[9ef495f] | 386 | free(fibril_connection);
|
---|
[d5c1051] | 387 | return EOK;
|
---|
[9ef495f] | 388 | }
|
---|
| 389 |
|
---|
[6769005] | 390 | /** Return label usable during replies to IPC_M_CONNECT_ME_TO. */
|
---|
| 391 | sysarg_t async_get_label(void)
|
---|
| 392 | {
|
---|
[53ee7a0] | 393 | return (sysarg_t) fibril_connection;
|
---|
[6769005] | 394 | }
|
---|
| 395 |
|
---|
[9ef495f] | 396 | /** Create a new fibril for a new connection.
|
---|
| 397 | *
|
---|
[01c3bb4] | 398 | * Create new fibril for connection, fill in connection structures and insert it
|
---|
| 399 | * into the hash table, so that later we can easily do routing of messages to
|
---|
| 400 | * particular fibrils.
|
---|
[9ef495f] | 401 | *
|
---|
[6769005] | 402 | * @param conn Pointer to the connection structure. Will be used as the
|
---|
| 403 | * label of the connected phone and request_label of incoming
|
---|
| 404 | * calls routed through that phone.
|
---|
| 405 | * @param in_task_id Identification of the incoming connection.
|
---|
[012dd8e] | 406 | * @param call Call data of the opening call. If call is NULL, it's
|
---|
| 407 | * either a callback connection that was opened by
|
---|
| 408 | * accepting the IPC_M_CONNECT_TO_ME call.
|
---|
| 409 | * Alternatively, it is zero when we are opening
|
---|
| 410 | * implicit connection.
|
---|
[6769005] | 411 | * @param handler Connection handler.
|
---|
| 412 | * @param data Client argument to pass to the connection handler.
|
---|
[9ef495f] | 413 | *
|
---|
[01c3bb4] | 414 | * @return New fibril id or NULL on failure.
|
---|
[9ef495f] | 415 | *
|
---|
| 416 | */
|
---|
[6769005] | 417 | static fid_t async_new_connection(connection_t *conn, task_id_t in_task_id,
|
---|
[061274f] | 418 | ipc_call_t *call, async_port_handler_t handler, void *data)
|
---|
[9ef495f] | 419 | {
|
---|
| 420 | conn->in_task_id = in_task_id;
|
---|
[1de92fb0] | 421 | conn->msg_channel = mpsc_create(sizeof(ipc_call_t));
|
---|
[9ef495f] | 422 | conn->handler = handler;
|
---|
| 423 | conn->data = data;
|
---|
[a35b458] | 424 |
|
---|
[6769005] | 425 | if (!conn->msg_channel)
|
---|
| 426 | goto error;
|
---|
| 427 |
|
---|
[9ef495f] | 428 | if (call)
|
---|
| 429 | conn->call = *call;
|
---|
[061274f] | 430 | else
|
---|
| 431 | conn->call.cap_handle = CAP_NIL;
|
---|
[a35b458] | 432 |
|
---|
[9ef495f] | 433 | /* We will activate the fibril ASAP */
|
---|
[514d561] | 434 | conn->fid = fibril_create(connection_fibril, conn);
|
---|
[a35b458] | 435 |
|
---|
[6769005] | 436 | if (conn->fid == 0)
|
---|
| 437 | goto error;
|
---|
[a35b458] | 438 |
|
---|
[6769005] | 439 | fibril_start(conn->fid);
|
---|
[a35b458] | 440 |
|
---|
[6769005] | 441 | return conn->fid;
|
---|
[a35b458] | 442 |
|
---|
[6769005] | 443 | error:
|
---|
| 444 | if (conn->msg_channel)
|
---|
| 445 | mpsc_destroy(conn->msg_channel);
|
---|
| 446 | free(conn);
|
---|
[a35b458] | 447 |
|
---|
[6769005] | 448 | if (call)
|
---|
| 449 | ipc_answer_0(call->cap_handle, ENOMEM);
|
---|
[a35b458] | 450 |
|
---|
[6769005] | 451 | return (fid_t) NULL;
|
---|
[9ef495f] | 452 | }
|
---|
| 453 |
|
---|
[78bb04b] | 454 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
---|
| 455 | *
|
---|
| 456 | * Ask through phone for a new connection to some service.
|
---|
| 457 | *
|
---|
| 458 | * @param exch Exchange for sending the message.
|
---|
| 459 | * @param iface Callback interface.
|
---|
| 460 | * @param arg1 User defined argument.
|
---|
| 461 | * @param arg2 User defined argument.
|
---|
| 462 | * @param handler Callback handler.
|
---|
| 463 | * @param data Handler data.
|
---|
| 464 | * @param port_id ID of the newly created port.
|
---|
| 465 | *
|
---|
[cde999a] | 466 | * @return Zero on success or an error code.
|
---|
[78bb04b] | 467 | *
|
---|
| 468 | */
|
---|
[b7fd2a0] | 469 | errno_t async_create_callback_port(async_exch_t *exch, iface_t iface, sysarg_t arg1,
|
---|
[78bb04b] | 470 | sysarg_t arg2, async_port_handler_t handler, void *data, port_id_t *port_id)
|
---|
| 471 | {
|
---|
| 472 | if ((iface & IFACE_MOD_CALLBACK) != IFACE_MOD_CALLBACK)
|
---|
| 473 | return EINVAL;
|
---|
[a35b458] | 474 |
|
---|
[78bb04b] | 475 | if (exch == NULL)
|
---|
| 476 | return ENOENT;
|
---|
[a35b458] | 477 |
|
---|
[6769005] | 478 | connection_t *conn = calloc(1, sizeof(*conn));
|
---|
| 479 | if (!conn)
|
---|
| 480 | return ENOMEM;
|
---|
| 481 |
|
---|
[78bb04b] | 482 | ipc_call_t answer;
|
---|
[6769005] | 483 | aid_t req = async_send_5(exch, IPC_M_CONNECT_TO_ME, iface, arg1, arg2,
|
---|
| 484 | 0, (sysarg_t) conn, &answer);
|
---|
[a35b458] | 485 |
|
---|
[fda19b8] | 486 | errno_t rc;
|
---|
| 487 | async_wait_for(req, &rc);
|
---|
[6769005] | 488 | if (rc != EOK) {
|
---|
| 489 | free(conn);
|
---|
[fda19b8] | 490 | return rc;
|
---|
[6769005] | 491 | }
|
---|
[a35b458] | 492 |
|
---|
[fda19b8] | 493 | rc = async_create_port_internal(iface, handler, data, port_id);
|
---|
[6769005] | 494 | if (rc != EOK) {
|
---|
| 495 | free(conn);
|
---|
[fda19b8] | 496 | return rc;
|
---|
[6769005] | 497 | }
|
---|
[a35b458] | 498 |
|
---|
[6769005] | 499 | fid_t fid = async_new_connection(conn, answer.task_id, NULL, handler,
|
---|
| 500 | data);
|
---|
[514d561] | 501 | if (fid == (fid_t) NULL)
|
---|
[78bb04b] | 502 | return ENOMEM;
|
---|
[a35b458] | 503 |
|
---|
[78bb04b] | 504 | return EOK;
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[5e801dc] | 507 | static size_t notification_key_hash(const void *key)
|
---|
[8820544] | 508 | {
|
---|
[5e801dc] | 509 | const sysarg_t *id = key;
|
---|
| 510 | return *id;
|
---|
[8820544] | 511 | }
|
---|
| 512 |
|
---|
| 513 | static size_t notification_hash(const ht_link_t *item)
|
---|
| 514 | {
|
---|
| 515 | notification_t *notification =
|
---|
[3b1cc8d] | 516 | hash_table_get_inst(item, notification_t, htlink);
|
---|
[8820544] | 517 | return notification_key_hash(¬ification->imethod);
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[5e801dc] | 520 | static bool notification_key_equal(const void *key, const ht_link_t *item)
|
---|
[8820544] | 521 | {
|
---|
[5e801dc] | 522 | const sysarg_t *id = key;
|
---|
[8820544] | 523 | notification_t *notification =
|
---|
[3b1cc8d] | 524 | hash_table_get_inst(item, notification_t, htlink);
|
---|
[5e801dc] | 525 | return *id == notification->imethod;
|
---|
[8820544] | 526 | }
|
---|
| 527 |
|
---|
| 528 | /** Operations for the notification hash table. */
|
---|
| 529 | static hash_table_ops_t notification_hash_table_ops = {
|
---|
| 530 | .hash = notification_hash,
|
---|
| 531 | .key_hash = notification_key_hash,
|
---|
| 532 | .key_equal = notification_key_equal,
|
---|
| 533 | .equal = NULL,
|
---|
| 534 | .remove_callback = NULL
|
---|
| 535 | };
|
---|
| 536 |
|
---|
[e70bfa5] | 537 | /** Try to route a call to an appropriate connection fibril.
|
---|
[80649a91] | 538 | *
|
---|
[36c9234] | 539 | * If the proper connection fibril is found, a message with the call is added to
|
---|
| 540 | * its message queue. If the fibril was not active, it is activated and all
|
---|
| 541 | * timeouts are unregistered.
|
---|
| 542 | *
|
---|
[061274f] | 543 | * @param call Data of the incoming call.
|
---|
[c07544d3] | 544 | *
|
---|
[1de92fb0] | 545 | * @return EOK if the call was successfully passed to the respective fibril.
|
---|
| 546 | * @return ENOENT if the call doesn't match any connection.
|
---|
| 547 | * @return Other error code if routing failed for other reasons.
|
---|
[36c9234] | 548 | *
|
---|
[80649a91] | 549 | */
|
---|
[1de92fb0] | 550 | static errno_t route_call(ipc_call_t *call)
|
---|
[450cd3a] | 551 | {
|
---|
[79ae36dd] | 552 | assert(call);
|
---|
[a35b458] | 553 |
|
---|
[6769005] | 554 | connection_t *conn = (connection_t *) call->request_label;
|
---|
[a35b458] | 555 |
|
---|
[6769005] | 556 | if (!conn)
|
---|
[1de92fb0] | 557 | return ENOENT;
|
---|
[a35b458] | 558 |
|
---|
[6769005] | 559 | assert(conn->msg_channel);
|
---|
[a35b458] | 560 |
|
---|
[1de92fb0] | 561 | errno_t rc = mpsc_send(conn->msg_channel, call);
|
---|
[a35b458] | 562 |
|
---|
[fafb8e5] | 563 | if (ipc_get_imethod(call) == IPC_M_PHONE_HUNGUP) {
|
---|
[1de92fb0] | 564 | /* Close the channel, but let the connection fibril answer. */
|
---|
| 565 | mpsc_close(conn->msg_channel);
|
---|
| 566 | // FIXME: Ideally, we should be able to discard/answer the
|
---|
| 567 | // hungup message here and just close the channel without
|
---|
| 568 | // passing it out. Unfortunatelly, somehow that breaks
|
---|
| 569 | // handling of CPU exceptions.
|
---|
| 570 | }
|
---|
[a35b458] | 571 |
|
---|
[1de92fb0] | 572 | return rc;
|
---|
[c07544d3] | 573 | }
|
---|
[80649a91] | 574 |
|
---|
[3b1cc8d] | 575 | /** Function implementing the notification handler fibril. Never returns. */
|
---|
| 576 | static errno_t notification_fibril_func(void *arg)
|
---|
| 577 | {
|
---|
| 578 | (void) arg;
|
---|
| 579 |
|
---|
| 580 | while (true) {
|
---|
| 581 | fibril_semaphore_down(¬ification_semaphore);
|
---|
| 582 |
|
---|
[9bde0d5] | 583 | fibril_rmutex_lock(¬ification_mutex);
|
---|
[3b1cc8d] | 584 |
|
---|
| 585 | /*
|
---|
| 586 | * The semaphore ensures that if we get this far,
|
---|
| 587 | * the queue must be non-empty.
|
---|
| 588 | */
|
---|
| 589 | assert(!list_empty(¬ification_queue));
|
---|
| 590 |
|
---|
| 591 | notification_t *notification = list_get_instance(
|
---|
| 592 | list_first(¬ification_queue), notification_t, qlink);
|
---|
| 593 |
|
---|
| 594 | async_notification_handler_t handler = notification->handler;
|
---|
| 595 | void *arg = notification->arg;
|
---|
| 596 |
|
---|
[8dab988] | 597 | notification_msg_t *m = list_pop(¬ification->msg_list,
|
---|
| 598 | notification_msg_t, link);
|
---|
| 599 | assert(m);
|
---|
| 600 | ipc_call_t calldata = m->calldata;
|
---|
| 601 |
|
---|
| 602 | notification_freelist_used--;
|
---|
| 603 |
|
---|
| 604 | if (notification_freelist_total > 64 &&
|
---|
| 605 | notification_freelist_total > 2 * notification_freelist_used) {
|
---|
| 606 | /* Going to free the structure if we have too much. */
|
---|
| 607 | notification_freelist_total--;
|
---|
| 608 | } else {
|
---|
| 609 | /* Otherwise add to freelist. */
|
---|
| 610 | list_append(&m->link, ¬ification_freelist);
|
---|
| 611 | m = NULL;
|
---|
| 612 | }
|
---|
[3b1cc8d] | 613 |
|
---|
[8dab988] | 614 | if (list_empty(¬ification->msg_list))
|
---|
| 615 | list_remove(¬ification->qlink);
|
---|
[3b1cc8d] | 616 |
|
---|
[9bde0d5] | 617 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[3b1cc8d] | 618 |
|
---|
| 619 | if (handler)
|
---|
| 620 | handler(&calldata, arg);
|
---|
[8dab988] | 621 |
|
---|
| 622 | free(m);
|
---|
[3b1cc8d] | 623 | }
|
---|
| 624 |
|
---|
| 625 | /* Not reached. */
|
---|
| 626 | return EOK;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | /**
|
---|
| 630 | * Creates a new dedicated fibril for handling notifications.
|
---|
| 631 | * By default, there is one such fibril. This function can be used to
|
---|
| 632 | * create more in order to increase the number of notification that can
|
---|
| 633 | * be processed concurrently.
|
---|
| 634 | *
|
---|
| 635 | * Currently, there is no way to destroy those fibrils after they are created.
|
---|
| 636 | */
|
---|
| 637 | errno_t async_spawn_notification_handler(void)
|
---|
| 638 | {
|
---|
| 639 | fid_t f = fibril_create(notification_fibril_func, NULL);
|
---|
| 640 | if (f == 0)
|
---|
| 641 | return ENOMEM;
|
---|
| 642 |
|
---|
| 643 | fibril_add_ready(f);
|
---|
| 644 | return EOK;
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | /** Queue notification.
|
---|
[c07544d3] | 648 | *
|
---|
[c170438] | 649 | * @param call Data of the incoming call.
|
---|
[58563585] | 650 | *
|
---|
[c07544d3] | 651 | */
|
---|
[3b1cc8d] | 652 | static void queue_notification(ipc_call_t *call)
|
---|
[c07544d3] | 653 | {
|
---|
[c170438] | 654 | assert(call);
|
---|
[a35b458] | 655 |
|
---|
[9bde0d5] | 656 | fibril_rmutex_lock(¬ification_mutex);
|
---|
[a35b458] | 657 |
|
---|
[8dab988] | 658 | notification_msg_t *m = list_pop(¬ification_freelist,
|
---|
| 659 | notification_msg_t, link);
|
---|
| 660 |
|
---|
| 661 | if (!m) {
|
---|
[9bde0d5] | 662 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[8dab988] | 663 | m = malloc(sizeof(notification_msg_t));
|
---|
| 664 | if (!m) {
|
---|
| 665 | DPRINTF("Out of memory.\n");
|
---|
| 666 | abort();
|
---|
| 667 | }
|
---|
| 668 |
|
---|
[9bde0d5] | 669 | fibril_rmutex_lock(¬ification_mutex);
|
---|
[8dab988] | 670 | notification_freelist_total++;
|
---|
| 671 | }
|
---|
| 672 |
|
---|
[95a47b0] | 673 | sysarg_t imethod = ipc_get_imethod(call);
|
---|
| 674 | ht_link_t *link = hash_table_find(¬ification_hash_table, &imethod);
|
---|
[3b1cc8d] | 675 | if (!link) {
|
---|
| 676 | /* Invalid notification. */
|
---|
| 677 | // TODO: Make sure this can't happen and turn it into assert.
|
---|
[8dab988] | 678 | notification_freelist_total--;
|
---|
[9bde0d5] | 679 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[8dab988] | 680 | free(m);
|
---|
[3b1cc8d] | 681 | return;
|
---|
[8820544] | 682 | }
|
---|
[a35b458] | 683 |
|
---|
[3b1cc8d] | 684 | notification_t *notification =
|
---|
| 685 | hash_table_get_inst(link, notification_t, htlink);
|
---|
| 686 |
|
---|
[8dab988] | 687 | notification_freelist_used++;
|
---|
| 688 | m->calldata = *call;
|
---|
| 689 | list_append(&m->link, ¬ification->msg_list);
|
---|
[3b1cc8d] | 690 |
|
---|
[8dab988] | 691 | if (!link_in_use(¬ification->qlink))
|
---|
| 692 | list_append(¬ification->qlink, ¬ification_queue);
|
---|
[3b1cc8d] | 693 |
|
---|
[9bde0d5] | 694 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[3b1cc8d] | 695 |
|
---|
| 696 | fibril_semaphore_up(¬ification_semaphore);
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | /**
|
---|
| 700 | * Creates a new notification structure and inserts it into the hash table.
|
---|
| 701 | *
|
---|
| 702 | * @param handler Function to call when notification is received.
|
---|
| 703 | * @param arg Argument for the handler function.
|
---|
| 704 | * @return The newly created notification structure.
|
---|
| 705 | */
|
---|
| 706 | static notification_t *notification_create(async_notification_handler_t handler, void *arg)
|
---|
| 707 | {
|
---|
| 708 | notification_t *notification = calloc(1, sizeof(notification_t));
|
---|
| 709 | if (!notification)
|
---|
| 710 | return NULL;
|
---|
| 711 |
|
---|
| 712 | notification->handler = handler;
|
---|
| 713 | notification->arg = arg;
|
---|
| 714 |
|
---|
[8dab988] | 715 | list_initialize(¬ification->msg_list);
|
---|
| 716 |
|
---|
[3b1cc8d] | 717 | fid_t fib = 0;
|
---|
| 718 |
|
---|
[9bde0d5] | 719 | fibril_rmutex_lock(¬ification_mutex);
|
---|
[3b1cc8d] | 720 |
|
---|
| 721 | if (notification_avail == 0) {
|
---|
| 722 | /* Attempt to create the first handler fibril. */
|
---|
| 723 | fib = fibril_create(notification_fibril_func, NULL);
|
---|
| 724 | if (fib == 0) {
|
---|
[9bde0d5] | 725 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[3b1cc8d] | 726 | free(notification);
|
---|
| 727 | return NULL;
|
---|
| 728 | }
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | sysarg_t imethod = notification_avail;
|
---|
| 732 | notification_avail++;
|
---|
| 733 |
|
---|
| 734 | notification->imethod = imethod;
|
---|
| 735 | hash_table_insert(¬ification_hash_table, ¬ification->htlink);
|
---|
| 736 |
|
---|
[9bde0d5] | 737 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[3b1cc8d] | 738 |
|
---|
| 739 | if (imethod == 0) {
|
---|
| 740 | assert(fib);
|
---|
| 741 | fibril_add_ready(fib);
|
---|
| 742 | }
|
---|
[a35b458] | 743 |
|
---|
[3b1cc8d] | 744 | return notification;
|
---|
[80649a91] | 745 | }
|
---|
| 746 |
|
---|
[8820544] | 747 | /** Subscribe to IRQ notification.
|
---|
| 748 | *
|
---|
| 749 | * @param inr IRQ number.
|
---|
| 750 | * @param handler Notification handler.
|
---|
| 751 | * @param data Notification handler client data.
|
---|
| 752 | * @param ucode Top-half pseudocode handler.
|
---|
| 753 | *
|
---|
[071a1ddb] | 754 | * @param[out] handle IRQ capability handle on success.
|
---|
| 755 | *
|
---|
[cde999a] | 756 | * @return An error code.
|
---|
[8820544] | 757 | *
|
---|
| 758 | */
|
---|
[b7fd2a0] | 759 | errno_t async_irq_subscribe(int inr, async_notification_handler_t handler,
|
---|
[eadaeae8] | 760 | void *data, const irq_code_t *ucode, cap_irq_handle_t *handle)
|
---|
[8820544] | 761 | {
|
---|
[3b1cc8d] | 762 | notification_t *notification = notification_create(handler, data);
|
---|
[8820544] | 763 | if (!notification)
|
---|
| 764 | return ENOMEM;
|
---|
[a35b458] | 765 |
|
---|
[eadaeae8] | 766 | cap_irq_handle_t ihandle;
|
---|
[3b1cc8d] | 767 | errno_t rc = ipc_irq_subscribe(inr, notification->imethod, ucode,
|
---|
| 768 | &ihandle);
|
---|
[071a1ddb] | 769 | if (rc == EOK && handle != NULL) {
|
---|
[eadaeae8] | 770 | *handle = ihandle;
|
---|
[9233e9d] | 771 | }
|
---|
[071a1ddb] | 772 | return rc;
|
---|
[8820544] | 773 | }
|
---|
| 774 |
|
---|
| 775 | /** Unsubscribe from IRQ notification.
|
---|
| 776 | *
|
---|
[eadaeae8] | 777 | * @param handle IRQ capability handle.
|
---|
[8820544] | 778 | *
|
---|
[cde999a] | 779 | * @return Zero on success or an error code.
|
---|
[8820544] | 780 | *
|
---|
| 781 | */
|
---|
[eadaeae8] | 782 | errno_t async_irq_unsubscribe(cap_irq_handle_t ihandle)
|
---|
[8820544] | 783 | {
|
---|
| 784 | // TODO: Remove entry from hash table
|
---|
| 785 | // to avoid memory leak
|
---|
[a35b458] | 786 |
|
---|
[eadaeae8] | 787 | return ipc_irq_unsubscribe(ihandle);
|
---|
[8820544] | 788 | }
|
---|
| 789 |
|
---|
| 790 | /** Subscribe to event notifications.
|
---|
| 791 | *
|
---|
| 792 | * @param evno Event type to subscribe.
|
---|
| 793 | * @param handler Notification handler.
|
---|
| 794 | * @param data Notification handler client data.
|
---|
| 795 | *
|
---|
[cde999a] | 796 | * @return Zero on success or an error code.
|
---|
[8820544] | 797 | *
|
---|
| 798 | */
|
---|
[b7fd2a0] | 799 | errno_t async_event_subscribe(event_type_t evno,
|
---|
[8820544] | 800 | async_notification_handler_t handler, void *data)
|
---|
| 801 | {
|
---|
[3b1cc8d] | 802 | notification_t *notification = notification_create(handler, data);
|
---|
[8820544] | 803 | if (!notification)
|
---|
| 804 | return ENOMEM;
|
---|
[a35b458] | 805 |
|
---|
[3b1cc8d] | 806 | return ipc_event_subscribe(evno, notification->imethod);
|
---|
[8820544] | 807 | }
|
---|
| 808 |
|
---|
| 809 | /** Subscribe to task event notifications.
|
---|
| 810 | *
|
---|
| 811 | * @param evno Event type to subscribe.
|
---|
| 812 | * @param handler Notification handler.
|
---|
| 813 | * @param data Notification handler client data.
|
---|
| 814 | *
|
---|
[cde999a] | 815 | * @return Zero on success or an error code.
|
---|
[8820544] | 816 | *
|
---|
| 817 | */
|
---|
[b7fd2a0] | 818 | errno_t async_event_task_subscribe(event_task_type_t evno,
|
---|
[8820544] | 819 | async_notification_handler_t handler, void *data)
|
---|
| 820 | {
|
---|
[3b1cc8d] | 821 | notification_t *notification = notification_create(handler, data);
|
---|
[8820544] | 822 | if (!notification)
|
---|
| 823 | return ENOMEM;
|
---|
[a35b458] | 824 |
|
---|
[3b1cc8d] | 825 | return ipc_event_task_subscribe(evno, notification->imethod);
|
---|
[8820544] | 826 | }
|
---|
| 827 |
|
---|
| 828 | /** Unmask event notifications.
|
---|
| 829 | *
|
---|
| 830 | * @param evno Event type to unmask.
|
---|
| 831 | *
|
---|
| 832 | * @return Value returned by the kernel.
|
---|
| 833 | *
|
---|
| 834 | */
|
---|
[b7fd2a0] | 835 | errno_t async_event_unmask(event_type_t evno)
|
---|
[8820544] | 836 | {
|
---|
| 837 | return ipc_event_unmask(evno);
|
---|
| 838 | }
|
---|
| 839 |
|
---|
| 840 | /** Unmask task event notifications.
|
---|
| 841 | *
|
---|
| 842 | * @param evno Event type to unmask.
|
---|
| 843 | *
|
---|
| 844 | * @return Value returned by the kernel.
|
---|
| 845 | *
|
---|
| 846 | */
|
---|
[b7fd2a0] | 847 | errno_t async_event_task_unmask(event_task_type_t evno)
|
---|
[8820544] | 848 | {
|
---|
| 849 | return ipc_event_task_unmask(evno);
|
---|
| 850 | }
|
---|
| 851 |
|
---|
[e70bfa5] | 852 | /** Return new incoming message for the current (fibril-local) connection.
|
---|
| 853 | *
|
---|
[984a9ba] | 854 | * @param call Storage where the incoming call data will be stored.
|
---|
| 855 | * @param usecs Timeout in microseconds. Zero denotes no timeout.
|
---|
| 856 | *
|
---|
| 857 | * @return If no timeout was specified, then true is returned.
|
---|
| 858 | * @return If a timeout is specified, then true is returned unless
|
---|
| 859 | * the timeout expires prior to receiving a message.
|
---|
[e70bfa5] | 860 | *
|
---|
| 861 | */
|
---|
[bd41ac52] | 862 | bool async_get_call_timeout(ipc_call_t *call, usec_t usecs)
|
---|
[80649a91] | 863 | {
|
---|
[79ae36dd] | 864 | assert(call);
|
---|
| 865 | assert(fibril_connection);
|
---|
[a35b458] | 866 |
|
---|
[bd41ac52] | 867 | struct timespec ts;
|
---|
| 868 | struct timespec *expires = NULL;
|
---|
[49d072e] | 869 | if (usecs) {
|
---|
[bd41ac52] | 870 | getuptime(&ts);
|
---|
| 871 | ts_add_diff(&ts, USEC2NSEC(usecs));
|
---|
| 872 | expires = &ts;
|
---|
[514d561] | 873 | }
|
---|
| 874 |
|
---|
[1de92fb0] | 875 | errno_t rc = mpsc_receive(fibril_connection->msg_channel,
|
---|
| 876 | call, expires);
|
---|
[a35b458] | 877 |
|
---|
[1de92fb0] | 878 | if (rc == ETIMEOUT)
|
---|
| 879 | return false;
|
---|
[a35b458] | 880 |
|
---|
[1de92fb0] | 881 | if (rc != EOK) {
|
---|
| 882 | /*
|
---|
| 883 | * The async_get_call_timeout() interface doesn't support
|
---|
| 884 | * propagating errors. Return a null call instead.
|
---|
| 885 | */
|
---|
[514d561] | 886 |
|
---|
[1de92fb0] | 887 | memset(call, 0, sizeof(ipc_call_t));
|
---|
[fafb8e5] | 888 | ipc_set_imethod(call, IPC_M_PHONE_HUNGUP);
|
---|
[de9a18e] | 889 | call->cap_handle = CAP_NIL;
|
---|
[450cd3a] | 890 | }
|
---|
[a35b458] | 891 |
|
---|
[984a9ba] | 892 | return true;
|
---|
[80649a91] | 893 | }
|
---|
| 894 |
|
---|
[4f13e19] | 895 | bool async_get_call(ipc_call_t *call)
|
---|
| 896 | {
|
---|
| 897 | return async_get_call_timeout(call, 0);
|
---|
| 898 | }
|
---|
| 899 |
|
---|
[455f190] | 900 | void *async_get_client_data(void)
|
---|
| 901 | {
|
---|
| 902 | assert(fibril_connection);
|
---|
| 903 | return fibril_connection->client->data;
|
---|
| 904 | }
|
---|
| 905 |
|
---|
[e2ab36f1] | 906 | void *async_get_client_data_by_id(task_id_t client_id)
|
---|
[455f190] | 907 | {
|
---|
[e2ab36f1] | 908 | client_t *client = async_client_get(client_id, false);
|
---|
[455f190] | 909 | if (!client)
|
---|
| 910 | return NULL;
|
---|
[a35b458] | 911 |
|
---|
[455f190] | 912 | if (!client->data) {
|
---|
| 913 | async_client_put(client);
|
---|
| 914 | return NULL;
|
---|
| 915 | }
|
---|
[a35b458] | 916 |
|
---|
[455f190] | 917 | return client->data;
|
---|
| 918 | }
|
---|
| 919 |
|
---|
[e2ab36f1] | 920 | void async_put_client_data_by_id(task_id_t client_id)
|
---|
[455f190] | 921 | {
|
---|
[e2ab36f1] | 922 | client_t *client = async_client_get(client_id, false);
|
---|
[a35b458] | 923 |
|
---|
[455f190] | 924 | assert(client);
|
---|
| 925 | assert(client->data);
|
---|
[a35b458] | 926 |
|
---|
[cdc8ee2d] | 927 | /* Drop the reference we got in async_get_client_data_by_hash(). */
|
---|
| 928 | async_client_put(client);
|
---|
[a35b458] | 929 |
|
---|
[cdc8ee2d] | 930 | /* Drop our own reference we got at the beginning of this function. */
|
---|
[455f190] | 931 | async_client_put(client);
|
---|
| 932 | }
|
---|
| 933 |
|
---|
[36c9234] | 934 | /** Handle a call that was received.
|
---|
| 935 | *
|
---|
| 936 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
|
---|
| 937 | * Otherwise the call is routed to its connection fibril.
|
---|
| 938 | *
|
---|
[061274f] | 939 | * @param call Data of the incoming call.
|
---|
[6b21292] | 940 | *
|
---|
[36c9234] | 941 | */
|
---|
[061274f] | 942 | static void handle_call(ipc_call_t *call)
|
---|
[80649a91] | 943 | {
|
---|
[79ae36dd] | 944 | assert(call);
|
---|
[a35b458] | 945 |
|
---|
[d054ad3] | 946 | if (call->flags & IPC_CALL_ANSWERED) {
|
---|
| 947 | /* Answer to a call made by us. */
|
---|
| 948 | async_reply_received(call);
|
---|
[e768aea] | 949 | return;
|
---|
[d054ad3] | 950 | }
|
---|
[e768aea] | 951 |
|
---|
[061274f] | 952 | if (call->cap_handle == CAP_NIL) {
|
---|
[e768aea] | 953 | if (call->flags & IPC_CALL_NOTIF) {
|
---|
| 954 | /* Kernel notification */
|
---|
| 955 | queue_notification(call);
|
---|
| 956 | }
|
---|
[47b7006] | 957 | return;
|
---|
[6b21292] | 958 | }
|
---|
[a35b458] | 959 |
|
---|
[566992e1] | 960 | /* New connection */
|
---|
[fafb8e5] | 961 | if (ipc_get_imethod(call) == IPC_M_CONNECT_ME_TO) {
|
---|
[6769005] | 962 | connection_t *conn = calloc(1, sizeof(*conn));
|
---|
| 963 | if (!conn) {
|
---|
| 964 | ipc_answer_0(call->cap_handle, ENOMEM);
|
---|
| 965 | return;
|
---|
| 966 | }
|
---|
| 967 |
|
---|
[fafb8e5] | 968 | iface_t iface = (iface_t) ipc_get_arg1(call);
|
---|
[a35b458] | 969 |
|
---|
[49a796f1] | 970 | // TODO: Currently ignores all ports but the first one.
|
---|
| 971 | void *data;
|
---|
| 972 | async_port_handler_t handler =
|
---|
| 973 | async_get_port_handler(iface, 0, &data);
|
---|
[a35b458] | 974 |
|
---|
[6769005] | 975 | async_new_connection(conn, call->task_id, call, handler, data);
|
---|
[566992e1] | 976 | return;
|
---|
| 977 | }
|
---|
[a35b458] | 978 |
|
---|
[6769005] | 979 | /* Route the call according to its request label */
|
---|
[1de92fb0] | 980 | errno_t rc = route_call(call);
|
---|
[0a8f070] | 981 | if (rc == EOK) {
|
---|
[47b7006] | 982 | return;
|
---|
[0a8f070] | 983 | } else if (implicit_connection != NULL) {
|
---|
[241f1985] | 984 | connection_t *conn = calloc(1, sizeof(connection_t));
|
---|
| 985 | if (!conn) {
|
---|
| 986 | ipc_answer_0(call->cap_handle, ENOMEM);
|
---|
| 987 | return;
|
---|
| 988 | }
|
---|
| 989 |
|
---|
| 990 | async_new_connection(conn, call->task_id, call, implicit_connection, NULL);
|
---|
[0a8f070] | 991 | return;
|
---|
| 992 | }
|
---|
[a35b458] | 993 |
|
---|
[1de92fb0] | 994 | // TODO: Log the error.
|
---|
| 995 |
|
---|
| 996 | if (call->cap_handle != CAP_NIL)
|
---|
| 997 | /* Unknown call from unknown phone - hang it up */
|
---|
| 998 | ipc_answer_0(call->cap_handle, EHANGUP);
|
---|
[450cd3a] | 999 | }
|
---|
| 1000 |
|
---|
[36c9234] | 1001 | /** Endless loop dispatching incoming calls and answers.
|
---|
| 1002 | *
|
---|
[c07544d3] | 1003 | * @return Never returns.
|
---|
| 1004 | *
|
---|
[36c9234] | 1005 | */
|
---|
[b7fd2a0] | 1006 | static errno_t async_manager_worker(void)
|
---|
[80649a91] | 1007 | {
|
---|
[514d561] | 1008 | ipc_call_t call;
|
---|
| 1009 | errno_t rc;
|
---|
[a35b458] | 1010 |
|
---|
[514d561] | 1011 | while (true) {
|
---|
| 1012 | rc = fibril_ipc_wait(&call, NULL);
|
---|
[acf6b55] | 1013 | if (rc == EOK)
|
---|
| 1014 | handle_call(&call);
|
---|
[80649a91] | 1015 | }
|
---|
[01c3bb4] | 1016 |
|
---|
[a46da63] | 1017 | return 0;
|
---|
[80649a91] | 1018 | }
|
---|
| 1019 |
|
---|
[36c9234] | 1020 | /** Function to start async_manager as a standalone fibril.
|
---|
[c07544d3] | 1021 | *
|
---|
[36c9234] | 1022 | * When more kernel threads are used, one async manager should exist per thread.
|
---|
| 1023 | *
|
---|
[c07544d3] | 1024 | * @param arg Unused.
|
---|
| 1025 | * @return Never returns.
|
---|
[36c9234] | 1026 | *
|
---|
[a2cd194] | 1027 | */
|
---|
[b7fd2a0] | 1028 | static errno_t async_manager_fibril(void *arg)
|
---|
[80649a91] | 1029 | {
|
---|
[085bd54] | 1030 | async_manager_worker();
|
---|
[a46da63] | 1031 | return 0;
|
---|
[80649a91] | 1032 | }
|
---|
[450cd3a] | 1033 |
|
---|
[36c9234] | 1034 | /** Add one manager to manager list. */
|
---|
[fec7ba0] | 1035 | static fid_t async_create_manager(void)
|
---|
[450cd3a] | 1036 | {
|
---|
[c170438] | 1037 | fid_t fid = fibril_create_generic(async_manager_fibril, NULL, PAGE_SIZE);
|
---|
[514d561] | 1038 | fibril_start(fid);
|
---|
| 1039 | return fid;
|
---|
[80649a91] | 1040 | }
|
---|
| 1041 |
|
---|
[36c9234] | 1042 | /** Initialize the async framework.
|
---|
| 1043 | *
|
---|
| 1044 | */
|
---|
[49a796f1] | 1045 | void __async_server_init(void)
|
---|
[80649a91] | 1046 | {
|
---|
[45c8eea] | 1047 | if (fibril_rmutex_initialize(&client_mutex) != EOK)
|
---|
| 1048 | abort();
|
---|
| 1049 | if (fibril_rmutex_initialize(¬ification_mutex) != EOK)
|
---|
| 1050 | abort();
|
---|
| 1051 |
|
---|
[062d900] | 1052 | if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
|
---|
[47b7006] | 1053 | abort();
|
---|
[a35b458] | 1054 |
|
---|
[8820544] | 1055 | if (!hash_table_create(¬ification_hash_table, 0, 0,
|
---|
| 1056 | ¬ification_hash_table_ops))
|
---|
| 1057 | abort();
|
---|
[514d561] | 1058 |
|
---|
| 1059 | async_create_manager();
|
---|
[49a796f1] | 1060 | }
|
---|
[a35b458] | 1061 |
|
---|
[25f6bddb] | 1062 | void __async_server_fini(void)
|
---|
| 1063 | {
|
---|
| 1064 | fibril_rmutex_destroy(&client_mutex);
|
---|
| 1065 | fibril_rmutex_destroy(¬ification_mutex);
|
---|
| 1066 | }
|
---|
| 1067 |
|
---|
[beb83c1] | 1068 | errno_t async_accept_0(ipc_call_t *call)
|
---|
| 1069 | {
|
---|
[d57c7c2] | 1070 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1071 | assert(chandle != CAP_NIL);
|
---|
| 1072 | call->cap_handle = CAP_NIL;
|
---|
| 1073 | return ipc_answer_5(chandle, EOK, 0, 0, 0, 0, async_get_label());
|
---|
[beb83c1] | 1074 | }
|
---|
| 1075 |
|
---|
[984a9ba] | 1076 | errno_t async_answer_0(ipc_call_t *call, errno_t retval)
|
---|
[49a796f1] | 1077 | {
|
---|
[d57c7c2] | 1078 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1079 | assert(chandle != CAP_NIL);
|
---|
| 1080 | call->cap_handle = CAP_NIL;
|
---|
| 1081 | return ipc_answer_0(chandle, retval);
|
---|
[450cd3a] | 1082 | }
|
---|
[01ff41c] | 1083 |
|
---|
[984a9ba] | 1084 | errno_t async_answer_1(ipc_call_t *call, errno_t retval, sysarg_t arg1)
|
---|
[01ff41c] | 1085 | {
|
---|
[d57c7c2] | 1086 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1087 | assert(chandle != CAP_NIL);
|
---|
| 1088 | call->cap_handle = CAP_NIL;
|
---|
| 1089 | return ipc_answer_1(chandle, retval, arg1);
|
---|
[49a796f1] | 1090 | }
|
---|
[a35b458] | 1091 |
|
---|
[984a9ba] | 1092 | errno_t async_answer_2(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
[49a796f1] | 1093 | sysarg_t arg2)
|
---|
| 1094 | {
|
---|
[d57c7c2] | 1095 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1096 | assert(chandle != CAP_NIL);
|
---|
| 1097 | call->cap_handle = CAP_NIL;
|
---|
| 1098 | return ipc_answer_2(chandle, retval, arg1, arg2);
|
---|
[49a796f1] | 1099 | }
|
---|
[a35b458] | 1100 |
|
---|
[984a9ba] | 1101 | errno_t async_answer_3(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
[49a796f1] | 1102 | sysarg_t arg2, sysarg_t arg3)
|
---|
| 1103 | {
|
---|
[d57c7c2] | 1104 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1105 | assert(chandle != CAP_NIL);
|
---|
| 1106 | call->cap_handle = CAP_NIL;
|
---|
| 1107 | return ipc_answer_3(chandle, retval, arg1, arg2, arg3);
|
---|
[49a796f1] | 1108 | }
|
---|
[a35b458] | 1109 |
|
---|
[984a9ba] | 1110 | errno_t async_answer_4(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
[49a796f1] | 1111 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
| 1112 | {
|
---|
[d57c7c2] | 1113 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1114 | assert(chandle != CAP_NIL);
|
---|
| 1115 | call->cap_handle = CAP_NIL;
|
---|
| 1116 | return ipc_answer_4(chandle, retval, arg1, arg2, arg3, arg4);
|
---|
[49a796f1] | 1117 | }
|
---|
[a35b458] | 1118 |
|
---|
[984a9ba] | 1119 | errno_t async_answer_5(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
[49a796f1] | 1120 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
| 1121 | {
|
---|
[d57c7c2] | 1122 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1123 | assert(chandle != CAP_NIL);
|
---|
| 1124 | call->cap_handle = CAP_NIL;
|
---|
| 1125 | return ipc_answer_5(chandle, retval, arg1, arg2, arg3, arg4, arg5);
|
---|
[49a796f1] | 1126 | }
|
---|
[a35b458] | 1127 |
|
---|
[4f13e19] | 1128 | static errno_t async_forward_fast(ipc_call_t *call, async_exch_t *exch,
|
---|
[49a796f1] | 1129 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
| 1130 | {
|
---|
[984a9ba] | 1131 | assert(call);
|
---|
| 1132 |
|
---|
[d57c7c2] | 1133 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1134 | assert(chandle != CAP_NIL);
|
---|
| 1135 | call->cap_handle = CAP_NIL;
|
---|
| 1136 |
|
---|
[49a796f1] | 1137 | if (exch == NULL)
|
---|
| 1138 | return ENOENT;
|
---|
[a35b458] | 1139 |
|
---|
[d57c7c2] | 1140 | return ipc_forward_fast(chandle, exch->phone, imethod, arg1, arg2,
|
---|
| 1141 | mode);
|
---|
[49a796f1] | 1142 | }
|
---|
[a35b458] | 1143 |
|
---|
[4f13e19] | 1144 | static errno_t async_forward_slow(ipc_call_t *call, async_exch_t *exch,
|
---|
[49a796f1] | 1145 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
| 1146 | sysarg_t arg4, sysarg_t arg5, unsigned int mode)
|
---|
| 1147 | {
|
---|
[984a9ba] | 1148 | assert(call);
|
---|
| 1149 |
|
---|
[d57c7c2] | 1150 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1151 | assert(chandle != CAP_NIL);
|
---|
| 1152 | call->cap_handle = CAP_NIL;
|
---|
| 1153 |
|
---|
[49a796f1] | 1154 | if (exch == NULL)
|
---|
| 1155 | return ENOENT;
|
---|
[a35b458] | 1156 |
|
---|
[d57c7c2] | 1157 | return ipc_forward_slow(chandle, exch->phone, imethod, arg1, arg2, arg3,
|
---|
| 1158 | arg4, arg5, mode);
|
---|
[01ff41c] | 1159 | }
|
---|
| 1160 |
|
---|
[4f13e19] | 1161 | errno_t async_forward_0(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
| 1162 | unsigned int mode)
|
---|
| 1163 | {
|
---|
| 1164 | return async_forward_fast(call, exch, imethod, 0, 0, mode);
|
---|
| 1165 | }
|
---|
| 1166 |
|
---|
| 1167 | errno_t async_forward_1(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
| 1168 | sysarg_t arg1, unsigned int mode)
|
---|
| 1169 | {
|
---|
| 1170 | return async_forward_fast(call, exch, imethod, arg1, 0, mode);
|
---|
| 1171 | }
|
---|
| 1172 |
|
---|
| 1173 | errno_t async_forward_2(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
| 1174 | sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
| 1175 | {
|
---|
| 1176 | return async_forward_fast(call, exch, imethod, arg1, arg2, mode);
|
---|
| 1177 | }
|
---|
| 1178 |
|
---|
| 1179 | errno_t async_forward_3(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
| 1180 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, unsigned int mode)
|
---|
| 1181 | {
|
---|
| 1182 | return async_forward_slow(call, exch, imethod, arg1, arg2, arg3, 0, 0,
|
---|
| 1183 | mode);
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
| 1186 | errno_t async_forward_4(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
| 1187 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
| 1188 | unsigned int mode)
|
---|
| 1189 | {
|
---|
| 1190 | return async_forward_slow(call, exch, imethod, arg1, arg2, arg3, arg4,
|
---|
| 1191 | 0, mode);
|
---|
| 1192 | }
|
---|
| 1193 |
|
---|
| 1194 | errno_t async_forward_5(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
| 1195 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
---|
| 1196 | unsigned int mode)
|
---|
| 1197 | {
|
---|
| 1198 | return async_forward_slow(call, exch, imethod, arg1, arg2, arg3, arg4,
|
---|
| 1199 | arg5, mode);
|
---|
| 1200 | }
|
---|
| 1201 |
|
---|
[49a796f1] | 1202 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
---|
[36c9234] | 1203 | *
|
---|
[49a796f1] | 1204 | * Ask through phone for a new connection to some service.
|
---|
[01ff41c] | 1205 | *
|
---|
[9b1baac] | 1206 | * @param exch Exchange for sending the message.
|
---|
| 1207 | * @param iface Callback interface.
|
---|
| 1208 | * @param arg2 User defined argument.
|
---|
| 1209 | * @param arg3 User defined argument.
|
---|
[c07544d3] | 1210 | *
|
---|
[49a796f1] | 1211 | * @return Zero on success or an error code.
|
---|
[36c9234] | 1212 | *
|
---|
[01ff41c] | 1213 | */
|
---|
[9b1baac] | 1214 | errno_t async_connect_to_me(async_exch_t *exch, iface_t iface, sysarg_t arg2,
|
---|
[49a796f1] | 1215 | sysarg_t arg3)
|
---|
[01ff41c] | 1216 | {
|
---|
[79ae36dd] | 1217 | if (exch == NULL)
|
---|
[49a796f1] | 1218 | return ENOENT;
|
---|
[a35b458] | 1219 |
|
---|
[6769005] | 1220 | sysarg_t label = 0;
|
---|
| 1221 | errno_t rc = async_req_5_0(exch, IPC_M_CONNECT_TO_ME, iface, arg2, arg3,
|
---|
| 1222 | 0, label);
|
---|
[a35b458] | 1223 |
|
---|
[6769005] | 1224 | return rc;
|
---|
[49a796f1] | 1225 | }
|
---|
[a35b458] | 1226 |
|
---|
[49a796f1] | 1227 | /** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
|
---|
[0da4e41] | 1228 | *
|
---|
[47b7006] | 1229 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
|
---|
| 1230 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1231 | * argument.
|
---|
[0da4e41] | 1232 | *
|
---|
| 1233 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1234 | *
|
---|
[984a9ba] | 1235 | * @param call Storage for the data of the IPC_M_SHARE_IN call.
|
---|
| 1236 | * @param size Destination address space area size.
|
---|
[47b7006] | 1237 | *
|
---|
| 1238 | * @return True on success, false on failure.
|
---|
[0da4e41] | 1239 | *
|
---|
| 1240 | */
|
---|
[984a9ba] | 1241 | bool async_share_in_receive(ipc_call_t *call, size_t *size)
|
---|
[0da4e41] | 1242 | {
|
---|
[984a9ba] | 1243 | assert(call);
|
---|
[0da4e41] | 1244 | assert(size);
|
---|
[a35b458] | 1245 |
|
---|
[984a9ba] | 1246 | async_get_call(call);
|
---|
[a35b458] | 1247 |
|
---|
[fafb8e5] | 1248 | if (ipc_get_imethod(call) != IPC_M_SHARE_IN)
|
---|
[47b7006] | 1249 | return false;
|
---|
[a35b458] | 1250 |
|
---|
[fafb8e5] | 1251 | *size = (size_t) ipc_get_arg1(call);
|
---|
[47b7006] | 1252 | return true;
|
---|
[0da4e41] | 1253 | }
|
---|
| 1254 |
|
---|
| 1255 | /** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
|
---|
| 1256 | *
|
---|
[fbcdeb8] | 1257 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
|
---|
[47b7006] | 1258 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1259 | * argument.
|
---|
[0da4e41] | 1260 | *
|
---|
[f8048d1] | 1261 | * @param call IPC_M_SHARE_IN call to answer.
|
---|
[984a9ba] | 1262 | * @param src Source address space base.
|
---|
| 1263 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
[47b7006] | 1264 | *
|
---|
| 1265 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1266 | *
|
---|
| 1267 | */
|
---|
[984a9ba] | 1268 | errno_t async_share_in_finalize(ipc_call_t *call, void *src, unsigned int flags)
|
---|
[0da4e41] | 1269 | {
|
---|
[984a9ba] | 1270 | assert(call);
|
---|
| 1271 |
|
---|
[d57c7c2] | 1272 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1273 | assert(chandle != CAP_NIL);
|
---|
| 1274 | call->cap_handle = CAP_NIL;
|
---|
| 1275 |
|
---|
| 1276 | return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) flags);
|
---|
[0da4e41] | 1277 | }
|
---|
| 1278 |
|
---|
| 1279 | /** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
|
---|
| 1280 | *
|
---|
[47b7006] | 1281 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
|
---|
| 1282 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1283 | * argument.
|
---|
[0da4e41] | 1284 | *
|
---|
| 1285 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1286 | *
|
---|
[984a9ba] | 1287 | * @param call Storage for the data of the IPC_M_SHARE_OUT call.
|
---|
| 1288 | * @param size Storage for the source address space area size.
|
---|
| 1289 | * @param flags Storage for the sharing flags.
|
---|
[47b7006] | 1290 | *
|
---|
| 1291 | * @return True on success, false on failure.
|
---|
[0da4e41] | 1292 | *
|
---|
| 1293 | */
|
---|
[984a9ba] | 1294 | bool async_share_out_receive(ipc_call_t *call, size_t *size,
|
---|
[01c3bb4] | 1295 | unsigned int *flags)
|
---|
[0da4e41] | 1296 | {
|
---|
[984a9ba] | 1297 | assert(call);
|
---|
[0da4e41] | 1298 | assert(size);
|
---|
| 1299 | assert(flags);
|
---|
[a35b458] | 1300 |
|
---|
[984a9ba] | 1301 | async_get_call(call);
|
---|
[a35b458] | 1302 |
|
---|
[fafb8e5] | 1303 | if (ipc_get_imethod(call) != IPC_M_SHARE_OUT)
|
---|
[47b7006] | 1304 | return false;
|
---|
[a35b458] | 1305 |
|
---|
[fafb8e5] | 1306 | *size = (size_t) ipc_get_arg2(call);
|
---|
| 1307 | *flags = (unsigned int) ipc_get_arg3(call);
|
---|
[47b7006] | 1308 | return true;
|
---|
[0da4e41] | 1309 | }
|
---|
| 1310 |
|
---|
| 1311 | /** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
|
---|
| 1312 | *
|
---|
[47b7006] | 1313 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
|
---|
| 1314 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1315 | * argument.
|
---|
[0da4e41] | 1316 | *
|
---|
[f8048d1] | 1317 | * @param call IPC_M_SHARE_OUT call to answer.
|
---|
[984a9ba] | 1318 | * @param dst Address of the storage for the destination address space area
|
---|
| 1319 | * base address.
|
---|
[47b7006] | 1320 | *
|
---|
[01c3bb4] | 1321 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1322 | *
|
---|
| 1323 | */
|
---|
[984a9ba] | 1324 | errno_t async_share_out_finalize(ipc_call_t *call, void **dst)
|
---|
[0da4e41] | 1325 | {
|
---|
[984a9ba] | 1326 | assert(call);
|
---|
[0da4e41] | 1327 |
|
---|
[d57c7c2] | 1328 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1329 | assert(chandle != CAP_NIL);
|
---|
| 1330 | call->cap_handle = CAP_NIL;
|
---|
| 1331 |
|
---|
| 1332 | return ipc_answer_2(chandle, EOK, (sysarg_t) __progsymbols.end,
|
---|
[984a9ba] | 1333 | (sysarg_t) dst);
|
---|
[d768d4c8] | 1334 | }
|
---|
| 1335 |
|
---|
| 1336 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
---|
| 1337 | *
|
---|
| 1338 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
|
---|
| 1339 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1340 | * argument.
|
---|
| 1341 | *
|
---|
| 1342 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1343 | *
|
---|
[984a9ba] | 1344 | * @param call Storage for the data of the IPC_M_DATA_READ.
|
---|
| 1345 | * @param size Storage for the maximum size. Can be NULL.
|
---|
[d768d4c8] | 1346 | *
|
---|
| 1347 | * @return True on success, false on failure.
|
---|
| 1348 | *
|
---|
| 1349 | */
|
---|
[984a9ba] | 1350 | bool async_data_read_receive(ipc_call_t *call, size_t *size)
|
---|
[0da4e41] | 1351 | {
|
---|
[984a9ba] | 1352 | assert(call);
|
---|
[a35b458] | 1353 |
|
---|
[984a9ba] | 1354 | async_get_call(call);
|
---|
[a35b458] | 1355 |
|
---|
[fafb8e5] | 1356 | if (ipc_get_imethod(call) != IPC_M_DATA_READ)
|
---|
[47b7006] | 1357 | return false;
|
---|
[a35b458] | 1358 |
|
---|
[0da4e41] | 1359 | if (size)
|
---|
[fafb8e5] | 1360 | *size = (size_t) ipc_get_arg2(call);
|
---|
[a35b458] | 1361 |
|
---|
[47b7006] | 1362 | return true;
|
---|
[0da4e41] | 1363 | }
|
---|
| 1364 |
|
---|
| 1365 | /** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
|
---|
| 1366 | *
|
---|
[47b7006] | 1367 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
|
---|
| 1368 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1369 | * argument.
|
---|
[0da4e41] | 1370 | *
|
---|
[984a9ba] | 1371 | * @param call IPC_M_DATA_READ call to answer.
|
---|
| 1372 | * @param src Source address for the IPC_M_DATA_READ call.
|
---|
| 1373 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
---|
| 1374 | * the maximum size announced by the sender.
|
---|
[47b7006] | 1375 | *
|
---|
[01c3bb4] | 1376 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1377 | *
|
---|
| 1378 | */
|
---|
[984a9ba] | 1379 | errno_t async_data_read_finalize(ipc_call_t *call, const void *src, size_t size)
|
---|
[0da4e41] | 1380 | {
|
---|
[984a9ba] | 1381 | assert(call);
|
---|
| 1382 |
|
---|
[d57c7c2] | 1383 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1384 | assert(chandle != CAP_NIL);
|
---|
| 1385 | call->cap_handle = CAP_NIL;
|
---|
| 1386 |
|
---|
| 1387 | return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) size);
|
---|
[0da4e41] | 1388 | }
|
---|
| 1389 |
|
---|
[b4cbef1] | 1390 | /** Wrapper for forwarding any read request
|
---|
| 1391 | *
|
---|
| 1392 | */
|
---|
[4f13e19] | 1393 | static errno_t async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
|
---|
[79ae36dd] | 1394 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
| 1395 | ipc_call_t *dataptr)
|
---|
[b4cbef1] | 1396 | {
|
---|
[79ae36dd] | 1397 | if (exch == NULL)
|
---|
| 1398 | return ENOENT;
|
---|
[a35b458] | 1399 |
|
---|
[984a9ba] | 1400 | ipc_call_t call;
|
---|
| 1401 | if (!async_data_read_receive(&call, NULL)) {
|
---|
| 1402 | async_answer_0(&call, EINVAL);
|
---|
[b4cbef1] | 1403 | return EINVAL;
|
---|
| 1404 | }
|
---|
[a35b458] | 1405 |
|
---|
[4f13e19] | 1406 | aid_t msg = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
|
---|
[b4cbef1] | 1407 | dataptr);
|
---|
| 1408 | if (msg == 0) {
|
---|
[984a9ba] | 1409 | async_answer_0(&call, EINVAL);
|
---|
[b4cbef1] | 1410 | return EINVAL;
|
---|
| 1411 | }
|
---|
[a35b458] | 1412 |
|
---|
[984a9ba] | 1413 | errno_t retval = ipc_forward_fast(call.cap_handle, exch->phone, 0, 0, 0,
|
---|
[b4cbef1] | 1414 | IPC_FF_ROUTE_FROM_ME);
|
---|
| 1415 | if (retval != EOK) {
|
---|
[ab9f443] | 1416 | async_forget(msg);
|
---|
[984a9ba] | 1417 | async_answer_0(&call, retval);
|
---|
[b4cbef1] | 1418 | return retval;
|
---|
| 1419 | }
|
---|
[a35b458] | 1420 |
|
---|
[b7fd2a0] | 1421 | errno_t rc;
|
---|
[b4cbef1] | 1422 | async_wait_for(msg, &rc);
|
---|
[a35b458] | 1423 |
|
---|
[b7fd2a0] | 1424 | return (errno_t) rc;
|
---|
[b4cbef1] | 1425 | }
|
---|
| 1426 |
|
---|
[4f13e19] | 1427 | errno_t async_data_read_forward_0_0(async_exch_t *exch, sysarg_t imethod)
|
---|
| 1428 | {
|
---|
| 1429 | return async_data_read_forward_fast(exch, imethod, 0, 0, 0, 0, NULL);
|
---|
| 1430 | }
|
---|
| 1431 |
|
---|
| 1432 | errno_t async_data_read_forward_1_0(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1433 | sysarg_t arg1)
|
---|
| 1434 | {
|
---|
| 1435 | return async_data_read_forward_fast(exch, imethod, arg1, 0, 0, 0, NULL);
|
---|
| 1436 | }
|
---|
| 1437 |
|
---|
| 1438 | errno_t async_data_read_forward_2_0(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1439 | sysarg_t arg1, sysarg_t arg2)
|
---|
| 1440 | {
|
---|
| 1441 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, 0,
|
---|
| 1442 | 0, NULL);
|
---|
| 1443 | }
|
---|
| 1444 |
|
---|
| 1445 | errno_t async_data_read_forward_3_0(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1446 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
|
---|
| 1447 | {
|
---|
| 1448 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
| 1449 | 0, NULL);
|
---|
| 1450 | }
|
---|
| 1451 |
|
---|
| 1452 | errno_t async_data_read_forward_4_0(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1453 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
| 1454 | {
|
---|
| 1455 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
| 1456 | arg4, NULL);
|
---|
| 1457 | }
|
---|
| 1458 |
|
---|
| 1459 | errno_t async_data_read_forward_0_1(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1460 | ipc_call_t *dataptr)
|
---|
| 1461 | {
|
---|
| 1462 | return async_data_read_forward_fast(exch, imethod, 0, 0, 0,
|
---|
| 1463 | 0, dataptr);
|
---|
| 1464 | }
|
---|
| 1465 |
|
---|
| 1466 | errno_t async_data_read_forward_1_1(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1467 | sysarg_t arg1, ipc_call_t *dataptr)
|
---|
| 1468 | {
|
---|
| 1469 | return async_data_read_forward_fast(exch, imethod, arg1, 0, 0,
|
---|
| 1470 | 0, dataptr);
|
---|
| 1471 | }
|
---|
| 1472 |
|
---|
| 1473 | errno_t async_data_read_forward_2_1(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1474 | sysarg_t arg1, sysarg_t arg2, ipc_call_t *dataptr)
|
---|
| 1475 | {
|
---|
| 1476 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, 0,
|
---|
| 1477 | 0, dataptr);
|
---|
| 1478 | }
|
---|
| 1479 |
|
---|
| 1480 | errno_t async_data_read_forward_3_1(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1481 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, ipc_call_t *dataptr)
|
---|
| 1482 | {
|
---|
| 1483 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
| 1484 | 0, dataptr);
|
---|
| 1485 | }
|
---|
| 1486 |
|
---|
| 1487 | errno_t async_data_read_forward_4_1(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1488 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
| 1489 | ipc_call_t *dataptr)
|
---|
| 1490 | {
|
---|
| 1491 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
| 1492 | arg4, dataptr);
|
---|
| 1493 | }
|
---|
| 1494 |
|
---|
[0da4e41] | 1495 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
---|
| 1496 | *
|
---|
[47b7006] | 1497 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
|
---|
| 1498 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1499 | * argument.
|
---|
[0da4e41] | 1500 | *
|
---|
| 1501 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1502 | *
|
---|
[984a9ba] | 1503 | * @param call Storage for the data of the IPC_M_DATA_WRITE.
|
---|
| 1504 | * @param size Storage for the suggested size. May be NULL.
|
---|
[5ae1c51] | 1505 | *
|
---|
| 1506 | * @return True on success, false on failure.
|
---|
| 1507 | *
|
---|
| 1508 | */
|
---|
[984a9ba] | 1509 | bool async_data_write_receive(ipc_call_t *call, size_t *size)
|
---|
[0da4e41] | 1510 | {
|
---|
[984a9ba] | 1511 | assert(call);
|
---|
[a35b458] | 1512 |
|
---|
[984a9ba] | 1513 | async_get_call(call);
|
---|
[a35b458] | 1514 |
|
---|
[fafb8e5] | 1515 | if (ipc_get_imethod(call) != IPC_M_DATA_WRITE)
|
---|
[47b7006] | 1516 | return false;
|
---|
[a35b458] | 1517 |
|
---|
[0da4e41] | 1518 | if (size)
|
---|
[fafb8e5] | 1519 | *size = (size_t) ipc_get_arg2(call);
|
---|
[a35b458] | 1520 |
|
---|
[47b7006] | 1521 | return true;
|
---|
[0da4e41] | 1522 | }
|
---|
| 1523 |
|
---|
| 1524 | /** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
|
---|
| 1525 | *
|
---|
[47b7006] | 1526 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
|
---|
| 1527 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1528 | * argument.
|
---|
[0da4e41] | 1529 | *
|
---|
[984a9ba] | 1530 | * @param call IPC_M_DATA_WRITE call to answer.
|
---|
| 1531 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
---|
| 1532 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
---|
[b4cbef1] | 1533 | *
|
---|
[01c3bb4] | 1534 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1535 | *
|
---|
| 1536 | */
|
---|
[984a9ba] | 1537 | errno_t async_data_write_finalize(ipc_call_t *call, void *dst, size_t size)
|
---|
[0da4e41] | 1538 | {
|
---|
[984a9ba] | 1539 | assert(call);
|
---|
| 1540 |
|
---|
| 1541 | return async_answer_2(call, EOK, (sysarg_t) dst, (sysarg_t) size);
|
---|
[0da4e41] | 1542 | }
|
---|
| 1543 |
|
---|
[eda925a] | 1544 | /** Wrapper for receiving binary data or strings
|
---|
[8aa42e3] | 1545 | *
|
---|
| 1546 | * This wrapper only makes it more comfortable to use async_data_write_*
|
---|
[eda925a] | 1547 | * functions to receive binary data or strings.
|
---|
[8aa42e3] | 1548 | *
|
---|
[472c09d] | 1549 | * @param data Pointer to data pointer (which should be later disposed
|
---|
| 1550 | * by free()). If the operation fails, the pointer is not
|
---|
| 1551 | * touched.
|
---|
[eda925a] | 1552 | * @param nullterm If true then the received data is always zero terminated.
|
---|
| 1553 | * This also causes to allocate one extra byte beyond the
|
---|
| 1554 | * raw transmitted data.
|
---|
[b4cbef1] | 1555 | * @param min_size Minimum size (in bytes) of the data to receive.
|
---|
[472c09d] | 1556 | * @param max_size Maximum size (in bytes) of the data to receive. 0 means
|
---|
| 1557 | * no limit.
|
---|
[eda925a] | 1558 | * @param granulariy If non-zero then the size of the received data has to
|
---|
[472c09d] | 1559 | * be divisible by this value.
|
---|
| 1560 | * @param received If not NULL, the size of the received data is stored here.
|
---|
[8aa42e3] | 1561 | *
|
---|
| 1562 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
| 1563 | *
|
---|
| 1564 | */
|
---|
[b7fd2a0] | 1565 | errno_t async_data_write_accept(void **data, const bool nullterm,
|
---|
[eda925a] | 1566 | const size_t min_size, const size_t max_size, const size_t granularity,
|
---|
| 1567 | size_t *received)
|
---|
[8aa42e3] | 1568 | {
|
---|
[79ae36dd] | 1569 | assert(data);
|
---|
[a35b458] | 1570 |
|
---|
[984a9ba] | 1571 | ipc_call_t call;
|
---|
[8aa42e3] | 1572 | size_t size;
|
---|
[984a9ba] | 1573 | if (!async_data_write_receive(&call, &size)) {
|
---|
| 1574 | async_answer_0(&call, EINVAL);
|
---|
[8aa42e3] | 1575 | return EINVAL;
|
---|
| 1576 | }
|
---|
[a35b458] | 1577 |
|
---|
[b4cbef1] | 1578 | if (size < min_size) {
|
---|
[984a9ba] | 1579 | async_answer_0(&call, EINVAL);
|
---|
[b4cbef1] | 1580 | return EINVAL;
|
---|
| 1581 | }
|
---|
[a35b458] | 1582 |
|
---|
[8aa42e3] | 1583 | if ((max_size > 0) && (size > max_size)) {
|
---|
[984a9ba] | 1584 | async_answer_0(&call, EINVAL);
|
---|
[8aa42e3] | 1585 | return EINVAL;
|
---|
| 1586 | }
|
---|
[a35b458] | 1587 |
|
---|
[472c09d] | 1588 | if ((granularity > 0) && ((size % granularity) != 0)) {
|
---|
[984a9ba] | 1589 | async_answer_0(&call, EINVAL);
|
---|
[472c09d] | 1590 | return EINVAL;
|
---|
| 1591 | }
|
---|
[a35b458] | 1592 |
|
---|
[57dea62] | 1593 | void *arg_data;
|
---|
[a35b458] | 1594 |
|
---|
[eda925a] | 1595 | if (nullterm)
|
---|
[57dea62] | 1596 | arg_data = malloc(size + 1);
|
---|
[eda925a] | 1597 | else
|
---|
[57dea62] | 1598 | arg_data = malloc(size);
|
---|
[a35b458] | 1599 |
|
---|
[57dea62] | 1600 | if (arg_data == NULL) {
|
---|
[984a9ba] | 1601 | async_answer_0(&call, ENOMEM);
|
---|
[8aa42e3] | 1602 | return ENOMEM;
|
---|
| 1603 | }
|
---|
[a35b458] | 1604 |
|
---|
[984a9ba] | 1605 | errno_t rc = async_data_write_finalize(&call, arg_data, size);
|
---|
[8aa42e3] | 1606 | if (rc != EOK) {
|
---|
[57dea62] | 1607 | free(arg_data);
|
---|
[8aa42e3] | 1608 | return rc;
|
---|
| 1609 | }
|
---|
[a35b458] | 1610 |
|
---|
[eda925a] | 1611 | if (nullterm)
|
---|
[57dea62] | 1612 | ((char *) arg_data)[size] = 0;
|
---|
[a35b458] | 1613 |
|
---|
[57dea62] | 1614 | *data = arg_data;
|
---|
[472c09d] | 1615 | if (received != NULL)
|
---|
| 1616 | *received = size;
|
---|
[a35b458] | 1617 |
|
---|
[8aa42e3] | 1618 | return EOK;
|
---|
| 1619 | }
|
---|
| 1620 |
|
---|
[b4cbef1] | 1621 | /** Wrapper for voiding any data that is about to be received
|
---|
| 1622 | *
|
---|
| 1623 | * This wrapper can be used to void any pending data
|
---|
| 1624 | *
|
---|
| 1625 | * @param retval Error value from @ref errno.h to be returned to the caller.
|
---|
| 1626 | *
|
---|
| 1627 | */
|
---|
[b7fd2a0] | 1628 | void async_data_write_void(errno_t retval)
|
---|
[b4cbef1] | 1629 | {
|
---|
[984a9ba] | 1630 | ipc_call_t call;
|
---|
| 1631 | async_data_write_receive(&call, NULL);
|
---|
| 1632 | async_answer_0(&call, retval);
|
---|
[b4cbef1] | 1633 | }
|
---|
| 1634 |
|
---|
| 1635 | /** Wrapper for forwarding any data that is about to be received
|
---|
| 1636 | *
|
---|
| 1637 | */
|
---|
[4f13e19] | 1638 | static errno_t async_data_write_forward_fast(async_exch_t *exch,
|
---|
| 1639 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
| 1640 | sysarg_t arg4, ipc_call_t *dataptr)
|
---|
[b4cbef1] | 1641 | {
|
---|
[79ae36dd] | 1642 | if (exch == NULL)
|
---|
| 1643 | return ENOENT;
|
---|
[a35b458] | 1644 |
|
---|
[984a9ba] | 1645 | ipc_call_t call;
|
---|
| 1646 | if (!async_data_write_receive(&call, NULL)) {
|
---|
| 1647 | async_answer_0(&call, EINVAL);
|
---|
[b4cbef1] | 1648 | return EINVAL;
|
---|
| 1649 | }
|
---|
[a35b458] | 1650 |
|
---|
[4f13e19] | 1651 | aid_t msg = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
|
---|
[b4cbef1] | 1652 | dataptr);
|
---|
| 1653 | if (msg == 0) {
|
---|
[984a9ba] | 1654 | async_answer_0(&call, EINVAL);
|
---|
[b4cbef1] | 1655 | return EINVAL;
|
---|
| 1656 | }
|
---|
[a35b458] | 1657 |
|
---|
[984a9ba] | 1658 | errno_t retval = ipc_forward_fast(call.cap_handle, exch->phone, 0, 0, 0,
|
---|
[b4cbef1] | 1659 | IPC_FF_ROUTE_FROM_ME);
|
---|
| 1660 | if (retval != EOK) {
|
---|
[ab9f443] | 1661 | async_forget(msg);
|
---|
[984a9ba] | 1662 | async_answer_0(&call, retval);
|
---|
[b4cbef1] | 1663 | return retval;
|
---|
| 1664 | }
|
---|
[a35b458] | 1665 |
|
---|
[b7fd2a0] | 1666 | errno_t rc;
|
---|
[b4cbef1] | 1667 | async_wait_for(msg, &rc);
|
---|
[a35b458] | 1668 |
|
---|
[b7fd2a0] | 1669 | return (errno_t) rc;
|
---|
[b4cbef1] | 1670 | }
|
---|
| 1671 |
|
---|
[4f13e19] | 1672 | errno_t async_data_write_forward_0_0(async_exch_t *exch, sysarg_t imethod)
|
---|
| 1673 | {
|
---|
| 1674 | return async_data_write_forward_fast(exch, imethod, 0, 0, 0,
|
---|
| 1675 | 0, NULL);
|
---|
| 1676 | }
|
---|
| 1677 |
|
---|
| 1678 | errno_t async_data_write_forward_1_0(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1679 | sysarg_t arg1)
|
---|
| 1680 | {
|
---|
| 1681 | return async_data_write_forward_fast(exch, imethod, arg1, 0, 0,
|
---|
| 1682 | 0, NULL);
|
---|
| 1683 | }
|
---|
| 1684 |
|
---|
| 1685 | errno_t async_data_write_forward_2_0(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1686 | sysarg_t arg1, sysarg_t arg2)
|
---|
| 1687 | {
|
---|
| 1688 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, 0,
|
---|
| 1689 | 0, NULL);
|
---|
| 1690 | }
|
---|
| 1691 |
|
---|
| 1692 | errno_t async_data_write_forward_3_0(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1693 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
|
---|
| 1694 | {
|
---|
| 1695 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
| 1696 | 0, NULL);
|
---|
| 1697 | }
|
---|
| 1698 |
|
---|
| 1699 | errno_t async_data_write_forward_4_0(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1700 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
| 1701 | {
|
---|
| 1702 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
| 1703 | arg4, NULL);
|
---|
| 1704 | }
|
---|
| 1705 |
|
---|
| 1706 | errno_t async_data_write_forward_0_1(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1707 | ipc_call_t *dataptr)
|
---|
| 1708 | {
|
---|
| 1709 | return async_data_write_forward_fast(exch, imethod, 0, 0, 0,
|
---|
| 1710 | 0, dataptr);
|
---|
| 1711 | }
|
---|
| 1712 |
|
---|
| 1713 | errno_t async_data_write_forward_1_1(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1714 | sysarg_t arg1, ipc_call_t *dataptr)
|
---|
| 1715 | {
|
---|
| 1716 | return async_data_write_forward_fast(exch, imethod, arg1, 0, 0,
|
---|
| 1717 | 0, dataptr);
|
---|
| 1718 | }
|
---|
| 1719 |
|
---|
| 1720 | errno_t async_data_write_forward_2_1(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1721 | sysarg_t arg1, sysarg_t arg2, ipc_call_t *dataptr)
|
---|
| 1722 | {
|
---|
| 1723 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, 0,
|
---|
| 1724 | 0, dataptr);
|
---|
| 1725 | }
|
---|
| 1726 |
|
---|
| 1727 | errno_t async_data_write_forward_3_1(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1728 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, ipc_call_t *dataptr)
|
---|
| 1729 | {
|
---|
| 1730 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
| 1731 | 0, dataptr);
|
---|
| 1732 | }
|
---|
| 1733 |
|
---|
| 1734 | errno_t async_data_write_forward_4_1(async_exch_t *exch, sysarg_t imethod,
|
---|
| 1735 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
| 1736 | ipc_call_t *dataptr)
|
---|
| 1737 | {
|
---|
| 1738 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
| 1739 | arg4, dataptr);
|
---|
| 1740 | }
|
---|
| 1741 |
|
---|
[79ae36dd] | 1742 | /** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
|
---|
| 1743 | *
|
---|
| 1744 | * If the current call is IPC_M_CONNECT_TO_ME then a new
|
---|
| 1745 | * async session is created for the accepted phone.
|
---|
| 1746 | *
|
---|
| 1747 | * @param mgmt Exchange management style.
|
---|
| 1748 | *
|
---|
[8869f7b] | 1749 | * @return New async session.
|
---|
| 1750 | * @return NULL on failure.
|
---|
[79ae36dd] | 1751 | *
|
---|
| 1752 | */
|
---|
| 1753 | async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
|
---|
| 1754 | {
|
---|
| 1755 | /* Accept the phone */
|
---|
| 1756 | ipc_call_t call;
|
---|
[984a9ba] | 1757 | async_get_call(&call);
|
---|
| 1758 |
|
---|
[fafb8e5] | 1759 | cap_phone_handle_t phandle = (cap_handle_t) ipc_get_arg5(&call);
|
---|
[a35b458] | 1760 |
|
---|
[fafb8e5] | 1761 | if ((ipc_get_imethod(&call) != IPC_M_CONNECT_TO_ME) ||
|
---|
[bb97118] | 1762 | !cap_handle_valid((phandle))) {
|
---|
[984a9ba] | 1763 | async_answer_0(&call, EINVAL);
|
---|
[79ae36dd] | 1764 | return NULL;
|
---|
| 1765 | }
|
---|
[a35b458] | 1766 |
|
---|
[012dd8e] | 1767 | async_sess_t *sess = create_session(phandle, mgmt, 0, 0, 0);
|
---|
[79ae36dd] | 1768 | if (sess == NULL) {
|
---|
[241f1985] | 1769 | ipc_hangup(phandle);
|
---|
[012dd8e] | 1770 | async_answer_0(&call, errno);
|
---|
| 1771 | } else {
|
---|
| 1772 | /* Acknowledge the connected phone */
|
---|
| 1773 | async_answer_0(&call, EOK);
|
---|
[79ae36dd] | 1774 | }
|
---|
[a35b458] | 1775 |
|
---|
[79ae36dd] | 1776 | return sess;
|
---|
| 1777 | }
|
---|
| 1778 |
|
---|
[8869f7b] | 1779 | /** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
|
---|
| 1780 | *
|
---|
| 1781 | * If the call is IPC_M_CONNECT_TO_ME then a new
|
---|
| 1782 | * async session is created. However, the phone is
|
---|
| 1783 | * not accepted automatically.
|
---|
| 1784 | *
|
---|
| 1785 | * @param mgmt Exchange management style.
|
---|
| 1786 | * @param call Call data.
|
---|
| 1787 | *
|
---|
| 1788 | * @return New async session.
|
---|
| 1789 | * @return NULL on failure.
|
---|
| 1790 | * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
|
---|
| 1791 | *
|
---|
| 1792 | */
|
---|
| 1793 | async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
|
---|
| 1794 | {
|
---|
[fafb8e5] | 1795 | cap_phone_handle_t phandle = (cap_handle_t) ipc_get_arg5(call);
|
---|
[a35b458] | 1796 |
|
---|
[fafb8e5] | 1797 | if ((ipc_get_imethod(call) != IPC_M_CONNECT_TO_ME) ||
|
---|
[bb97118] | 1798 | !cap_handle_valid((phandle)))
|
---|
[8869f7b] | 1799 | return NULL;
|
---|
[a35b458] | 1800 |
|
---|
[012dd8e] | 1801 | return create_session(phandle, mgmt, 0, 0, 0);
|
---|
[8869f7b] | 1802 | }
|
---|
| 1803 |
|
---|
[984a9ba] | 1804 | bool async_state_change_receive(ipc_call_t *call)
|
---|
[2c4aa39] | 1805 | {
|
---|
[984a9ba] | 1806 | assert(call);
|
---|
[a35b458] | 1807 |
|
---|
[984a9ba] | 1808 | async_get_call(call);
|
---|
[a35b458] | 1809 |
|
---|
[fafb8e5] | 1810 | if (ipc_get_imethod(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
|
---|
[2c4aa39] | 1811 | return false;
|
---|
[a35b458] | 1812 |
|
---|
[2c4aa39] | 1813 | return true;
|
---|
| 1814 | }
|
---|
| 1815 |
|
---|
[984a9ba] | 1816 | errno_t async_state_change_finalize(ipc_call_t *call, async_exch_t *other_exch)
|
---|
[2c4aa39] | 1817 | {
|
---|
[984a9ba] | 1818 | assert(call);
|
---|
| 1819 |
|
---|
[bb97118] | 1820 | return async_answer_1(call, EOK, cap_handle_raw(other_exch->phone));
|
---|
[2c4aa39] | 1821 | }
|
---|
| 1822 |
|
---|
[6b96dc06] | 1823 | __noreturn void async_manager(void)
|
---|
[d73d992] | 1824 | {
|
---|
[514d561] | 1825 | fibril_event_t ever = FIBRIL_EVENT_INIT;
|
---|
| 1826 | fibril_wait_for(&ever);
|
---|
[d73d992] | 1827 | __builtin_unreachable();
|
---|
| 1828 | }
|
---|
| 1829 |
|
---|
[a46da63] | 1830 | /** @}
|
---|
[b2951e2] | 1831 | */
|
---|