source: mainline/uspace/lib/c/generic/async/server.c@ 16d748ee

Last change on this file since 16d748ee was 16d748ee, checked in by Matthieu Riolo <matthieu.riolo@…>, 5 years ago

Removing implicit handler from async/server and taskman.
Correcting taskman methods and it's fallback handler
Simplifying connecting to taskman

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