| 1 | /* | 
|---|
| 2 | * Copyright (C) 2006 Ondrej Palkovsky | 
|---|
| 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. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | #ifndef __IPC_H__ | 
|---|
| 30 | #define __IPC_H__ | 
|---|
| 31 |  | 
|---|
| 32 | /* Length of data being transfered with IPC call */ | 
|---|
| 33 | /* - the uspace may not be able to utilize full length */ | 
|---|
| 34 | #define IPC_CALL_LEN    4 | 
|---|
| 35 |  | 
|---|
| 36 | /** Maximum active async calls per thread */ | 
|---|
| 37 | #define IPC_MAX_ASYNC_CALLS  4 | 
|---|
| 38 |  | 
|---|
| 39 | /* Flags for calls */ | 
|---|
| 40 | #define IPC_CALL_ANSWERED      0x1 /**< This is answer to a call */ | 
|---|
| 41 | #define IPC_CALL_STATIC_ALLOC  0x2 /**< This call will not be freed on error */ | 
|---|
| 42 | #define IPC_CALL_DISPATCHED    0x4 /**< Call is in dispatch queue */ | 
|---|
| 43 |  | 
|---|
| 44 | /* Flags for ipc_wait_for_call */ | 
|---|
| 45 | #define IPC_WAIT_NONBLOCKING   1 | 
|---|
| 46 |  | 
|---|
| 47 | /* Flags of callid */ | 
|---|
| 48 | #define IPC_CALLID_ANSWERED  1 | 
|---|
| 49 |  | 
|---|
| 50 | /* Return values from IPC_ASYNC */ | 
|---|
| 51 | #define IPC_CALLRET_FATAL         -1 | 
|---|
| 52 | #define IPC_CALLRET_TEMPORARY     -2 | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | /* Macros for manipulating calling data */ | 
|---|
| 56 | #define IPC_SET_RETVAL(data, retval)   ((data)[0] = (retval)) | 
|---|
| 57 | #define IPC_SET_METHOD(data, val)   ((data)[0] = (val)) | 
|---|
| 58 | #define IPC_SET_ARG1(data, val)   ((data)[1] = (val)) | 
|---|
| 59 | #define IPC_SET_ARG2(data, val)   ((data)[2] = (val)) | 
|---|
| 60 | #define IPC_SET_ARG3(data, val)   ((data)[3] = (val)) | 
|---|
| 61 |  | 
|---|
| 62 | #define IPC_GET_METHOD(data)           ((data)[0]) | 
|---|
| 63 | #define IPC_GET_RETVAL(data)           ((data)[0]) | 
|---|
| 64 |  | 
|---|
| 65 | #define IPC_GET_ARG1(data)              ((data)[1]) | 
|---|
| 66 | #define IPC_GET_ARG2(data)              ((data)[2]) | 
|---|
| 67 | #define IPC_GET_ARG3(data)              ((data)[3]) | 
|---|
| 68 |  | 
|---|
| 69 | /* Well known phone descriptors */ | 
|---|
| 70 | #define PHONE_NS              0 | 
|---|
| 71 |  | 
|---|
| 72 | /* System-specific methods - only through special syscalls | 
|---|
| 73 | * These methods have special behaviour | 
|---|
| 74 | */ | 
|---|
| 75 | /** Protocol for CONNECT - TO - ME | 
|---|
| 76 | * | 
|---|
| 77 | * Calling process asks the callee to create a callback connection, | 
|---|
| 78 | * so that it can start initiating new messages. | 
|---|
| 79 | * | 
|---|
| 80 | * The protocol for negotiating is: | 
|---|
| 81 | * - sys_connecttome - sends a message IPC_M_CONNECTTOME | 
|---|
| 82 | * - sys_wait_for_call - upon receipt tries to allocate new phone | 
|---|
| 83 | *                       - if it fails, responds with ELIMIT | 
|---|
| 84 | *                     - passes call to userspace. If userspace | 
|---|
| 85 | *                       responds with error, phone is deallocated and | 
|---|
| 86 | *                       error is sent back to caller. Otherwise | 
|---|
| 87 | *                       the call is accepted and the response is sent back. | 
|---|
| 88 | *                     - the allocated phoneid is passed to userspace | 
|---|
| 89 | *                       (on the receiving sid) as ARG3 of the call. | 
|---|
| 90 | *                     - the caller obtains taskid of the called thread | 
|---|
| 91 | */ | 
|---|
| 92 | #define IPC_M_CONNECTTOME     1 | 
|---|
| 93 | /** Protocol for CONNECT - ME - TO | 
|---|
| 94 | * | 
|---|
| 95 | * Calling process asks the callee to create for him a new connection. | 
|---|
| 96 | * E.g. the caller wants a name server to connect him to print server. | 
|---|
| 97 | * | 
|---|
| 98 | * The protocol for negotiating is: | 
|---|
| 99 | * - sys_connect_me_to - send a synchronous message to name server | 
|---|
| 100 | *                       indicating that it wants to be connected to some | 
|---|
| 101 | *                       service | 
|---|
| 102 | *   recepient         -  if ipc_answer == 0, then accept connection | 
|---|
| 103 | *                     -  otherwise connection refused | 
|---|
| 104 | *                     -  recepient may forward message. Forwarding | 
|---|
| 105 | *                        system message | 
|---|
| 106 | * | 
|---|
| 107 | */ | 
|---|
| 108 | #define IPC_M_CONNECTMETO     2 | 
|---|
| 109 | /* Control messages that the server sends to the processes | 
|---|
| 110 | * about their connections. | 
|---|
| 111 | */ | 
|---|
| 112 |  | 
|---|
| 113 |  | 
|---|
| 114 | /* Well-known methods */ | 
|---|
| 115 | #define IPC_M_LAST_SYSTEM     511 | 
|---|
| 116 | #define IPC_M_PING            512 | 
|---|
| 117 | /* User methods */ | 
|---|
| 118 | #define FIRST_USER_METHOD     1024 | 
|---|
| 119 |  | 
|---|
| 120 | #ifdef KERNEL | 
|---|
| 121 |  | 
|---|
| 122 | #include <synch/mutex.h> | 
|---|
| 123 | #include <synch/condvar.h> | 
|---|
| 124 | #include <adt/list.h> | 
|---|
| 125 |  | 
|---|
| 126 | #define IPC_MAX_PHONES  16 | 
|---|
| 127 |  | 
|---|
| 128 | typedef struct answerbox answerbox_t; | 
|---|
| 129 | typedef __native ipc_data_t[IPC_CALL_LEN]; | 
|---|
| 130 |  | 
|---|
| 131 | typedef struct { | 
|---|
| 132 | link_t list; | 
|---|
| 133 | answerbox_t *callerbox; | 
|---|
| 134 | int flags; | 
|---|
| 135 | task_t *sender; | 
|---|
| 136 | ipc_data_t data; | 
|---|
| 137 | } call_t; | 
|---|
| 138 |  | 
|---|
| 139 | struct answerbox { | 
|---|
| 140 | SPINLOCK_DECLARE(lock); | 
|---|
| 141 |  | 
|---|
| 142 | task_t *task; | 
|---|
| 143 |  | 
|---|
| 144 | waitq_t wq; | 
|---|
| 145 |  | 
|---|
| 146 | link_t connected_phones; /**< Phones connected to this answerbox */ | 
|---|
| 147 | link_t calls;            /**< Received calls */ | 
|---|
| 148 | link_t dispatched_calls; /* Should be hash table in the future */ | 
|---|
| 149 |  | 
|---|
| 150 | link_t answers;          /**< Answered calls */ | 
|---|
| 151 | }; | 
|---|
| 152 |  | 
|---|
| 153 | typedef struct { | 
|---|
| 154 | SPINLOCK_DECLARE(lock); | 
|---|
| 155 | link_t list; | 
|---|
| 156 | answerbox_t *callee; | 
|---|
| 157 | int busy; | 
|---|
| 158 | } phone_t; | 
|---|
| 159 |  | 
|---|
| 160 | extern void ipc_init(void); | 
|---|
| 161 | extern call_t * ipc_wait_for_call(answerbox_t *box, int flags); | 
|---|
| 162 | extern void ipc_answer(answerbox_t *box, call_t *request); | 
|---|
| 163 | extern void ipc_call(phone_t *phone, call_t *request); | 
|---|
| 164 | extern void ipc_call_sync(phone_t *phone, call_t *request); | 
|---|
| 165 | extern void ipc_phone_destroy(phone_t *phone); | 
|---|
| 166 | extern void ipc_phone_init(phone_t *phone); | 
|---|
| 167 | extern void ipc_phone_connect(phone_t *phone, answerbox_t *box); | 
|---|
| 168 | extern void ipc_call_free(call_t *call); | 
|---|
| 169 | extern call_t * ipc_call_alloc(void); | 
|---|
| 170 | extern void ipc_answerbox_init(answerbox_t *box); | 
|---|
| 171 | extern void ipc_call_init(call_t *call); | 
|---|
| 172 | extern void ipc_forward(call_t *call, answerbox_t *newbox,answerbox_t *oldbox); | 
|---|
| 173 |  | 
|---|
| 174 | extern answerbox_t *ipc_phone_0; | 
|---|
| 175 |  | 
|---|
| 176 | #endif | 
|---|
| 177 |  | 
|---|
| 178 | #endif | 
|---|