Rev 1392 | Rev 1463 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 954 | palkovsky | 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 | |||
| 1352 | palkovsky | 29 | #include <ipc/ipc.h> |
| 954 | palkovsky | 30 | #include <libc.h> |
| 999 | palkovsky | 31 | #include <malloc.h> |
| 32 | #include <errno.h> |
||
| 1352 | palkovsky | 33 | #include <libadt/list.h> |
| 999 | palkovsky | 34 | #include <stdio.h> |
| 35 | #include <unistd.h> |
||
| 1350 | palkovsky | 36 | #include <futex.h> |
| 1365 | jermar | 37 | #include <kernel/synch/synch.h> |
| 954 | palkovsky | 38 | |
| 999 | palkovsky | 39 | /** Structure used for keeping track of sent async msgs |
| 40 | * and queing unsent msgs |
||
| 41 | * |
||
| 42 | */ |
||
| 43 | typedef struct { |
||
| 44 | link_t list; |
||
| 45 | |||
| 46 | ipc_async_callback_t callback; |
||
| 47 | void *private; |
||
| 48 | union { |
||
| 49 | ipc_callid_t callid; |
||
| 50 | struct { |
||
| 1091 | palkovsky | 51 | ipc_call_t data; |
| 999 | palkovsky | 52 | int phoneid; |
| 53 | } msg; |
||
| 54 | }u; |
||
| 55 | } async_call_t; |
||
| 56 | |||
| 57 | LIST_INITIALIZE(dispatched_calls); |
||
| 58 | LIST_INITIALIZE(queued_calls); |
||
| 59 | |||
| 1392 | palkovsky | 60 | static atomic_t ipc_futex = FUTEX_INITIALIZER; |
| 1350 | palkovsky | 61 | |
| 999 | palkovsky | 62 | int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1, |
| 63 | ipcarg_t *result) |
||
| 954 | palkovsky | 64 | { |
| 1091 | palkovsky | 65 | ipc_call_t resdata; |
| 966 | palkovsky | 66 | int callres; |
| 67 | |||
| 999 | palkovsky | 68 | callres = __SYSCALL4(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1, |
| 966 | palkovsky | 69 | (sysarg_t)&resdata); |
| 70 | if (callres) |
||
| 71 | return callres; |
||
| 72 | if (result) |
||
| 73 | *result = IPC_GET_ARG1(resdata); |
||
| 74 | return IPC_GET_RETVAL(resdata); |
||
| 954 | palkovsky | 75 | } |
| 76 | |||
| 999 | palkovsky | 77 | int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1, |
| 78 | ipcarg_t arg2, ipcarg_t arg3, |
||
| 79 | ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3) |
||
| 954 | palkovsky | 80 | { |
| 1091 | palkovsky | 81 | ipc_call_t data; |
| 966 | palkovsky | 82 | int callres; |
| 83 | |||
| 84 | IPC_SET_METHOD(data, method); |
||
| 85 | IPC_SET_ARG1(data, arg1); |
||
| 86 | IPC_SET_ARG2(data, arg2); |
||
| 87 | IPC_SET_ARG3(data, arg3); |
||
| 88 | |||
| 1006 | palkovsky | 89 | callres = __SYSCALL3(SYS_IPC_CALL_SYNC, phoneid, (sysarg_t)&data, |
| 90 | (sysarg_t)&data); |
||
| 966 | palkovsky | 91 | if (callres) |
| 92 | return callres; |
||
| 93 | |||
| 94 | if (result1) |
||
| 95 | *result1 = IPC_GET_ARG1(data); |
||
| 96 | if (result2) |
||
| 97 | *result2 = IPC_GET_ARG2(data); |
||
| 98 | if (result3) |
||
| 99 | *result3 = IPC_GET_ARG3(data); |
||
| 100 | return IPC_GET_RETVAL(data); |
||
| 101 | } |
||
| 102 | |||
| 999 | palkovsky | 103 | /** Syscall to send asynchronous message */ |
| 1091 | palkovsky | 104 | static ipc_callid_t _ipc_call_async(int phoneid, ipc_call_t *data) |
| 999 | palkovsky | 105 | { |
| 106 | return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t)data); |
||
| 107 | } |
||
| 108 | |||
| 966 | palkovsky | 109 | /** Send asynchronous message |
| 110 | * |
||
| 111 | * - if fatal error, call callback handler with proper error code |
||
| 112 | * - if message cannot be temporarily sent, add to queue |
||
| 113 | */ |
||
| 999 | palkovsky | 114 | void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1, |
| 115 | ipcarg_t arg2, void *private, |
||
| 966 | palkovsky | 116 | ipc_async_callback_t callback) |
| 117 | { |
||
| 999 | palkovsky | 118 | async_call_t *call; |
| 966 | palkovsky | 119 | ipc_callid_t callid; |
| 120 | |||
| 999 | palkovsky | 121 | call = malloc(sizeof(*call)); |
| 122 | if (!call) { |
||
| 1443 | palkovsky | 123 | if (callback) |
| 124 | callback(private, ENOMEM, NULL); |
||
| 1028 | palkovsky | 125 | return; |
| 999 | palkovsky | 126 | } |
| 127 | |||
| 128 | callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2); |
||
| 966 | palkovsky | 129 | if (callid == IPC_CALLRET_FATAL) { |
| 130 | /* Call asynchronous handler with error code */ |
||
| 1443 | palkovsky | 131 | if (callback) |
| 132 | callback(private, ENOENT, NULL); |
||
| 999 | palkovsky | 133 | free(call); |
| 966 | palkovsky | 134 | return; |
| 135 | } |
||
| 999 | palkovsky | 136 | |
| 137 | call->callback = callback; |
||
| 138 | call->private = private; |
||
| 139 | |||
| 966 | palkovsky | 140 | if (callid == IPC_CALLRET_TEMPORARY) { |
| 141 | /* Add asynchronous call to queue of non-dispatched async calls */ |
||
| 999 | palkovsky | 142 | call->u.msg.phoneid = phoneid; |
| 143 | IPC_SET_METHOD(call->u.msg.data, method); |
||
| 144 | IPC_SET_ARG1(call->u.msg.data, arg1); |
||
| 145 | IPC_SET_ARG2(call->u.msg.data, arg2); |
||
| 1350 | palkovsky | 146 | |
| 147 | futex_down(&ipc_futex); |
||
| 999 | palkovsky | 148 | list_append(&call->list, &queued_calls); |
| 1350 | palkovsky | 149 | futex_up(&ipc_futex); |
| 966 | palkovsky | 150 | return; |
| 151 | } |
||
| 999 | palkovsky | 152 | call->u.callid = callid; |
| 153 | /* Add call to list of dispatched calls */ |
||
| 1350 | palkovsky | 154 | futex_down(&ipc_futex); |
| 999 | palkovsky | 155 | list_append(&call->list, &dispatched_calls); |
| 1350 | palkovsky | 156 | futex_up(&ipc_futex); |
| 954 | palkovsky | 157 | } |
| 158 | |||
| 966 | palkovsky | 159 | |
| 1343 | jermar | 160 | /** Send a fast answer to a received call. |
| 161 | * |
||
| 162 | * The fast answer makes use of passing retval and first two arguments in registers. |
||
| 163 | * If you need to return more, use the ipc_answer() instead. |
||
| 164 | * |
||
| 165 | * @param callid ID of the call being answered. |
||
| 166 | * @param retval Return value. |
||
| 167 | * @param arg1 First return argument. |
||
| 168 | * @param arg2 Second return argument. |
||
| 169 | * |
||
| 170 | * @return Zero on success or a value from @ref errno.h on failure. |
||
| 171 | */ |
||
| 172 | ipcarg_t ipc_answer_fast(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1, |
||
| 999 | palkovsky | 173 | ipcarg_t arg2) |
| 954 | palkovsky | 174 | { |
| 1330 | palkovsky | 175 | return __SYSCALL4(SYS_IPC_ANSWER_FAST, callid, retval, arg1, arg2); |
| 954 | palkovsky | 176 | } |
| 177 | |||
| 1343 | jermar | 178 | /** Send a full answer to a received call. |
| 179 | * |
||
| 180 | * @param callid ID of the call being answered. |
||
| 181 | * @param call Call data. Must be already initialized by the responder. |
||
| 182 | * |
||
| 183 | * @return Zero on success or a value from @ref errno.h on failure. |
||
| 184 | */ |
||
| 185 | ipcarg_t ipc_answer(ipc_callid_t callid, ipc_call_t *call) |
||
| 186 | { |
||
| 187 | return __SYSCALL2(SYS_IPC_ANSWER, callid, (sysarg_t) call); |
||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 999 | palkovsky | 191 | /** Try to dispatch queed calls from async queue */ |
| 192 | static void try_dispatch_queued_calls(void) |
||
| 193 | { |
||
| 194 | async_call_t *call; |
||
| 195 | ipc_callid_t callid; |
||
| 196 | |||
| 1350 | palkovsky | 197 | futex_down(&ipc_futex); |
| 999 | palkovsky | 198 | while (!list_empty(&queued_calls)) { |
| 199 | call = list_get_instance(queued_calls.next, async_call_t, |
||
| 200 | list); |
||
| 201 | |||
| 202 | callid = _ipc_call_async(call->u.msg.phoneid, |
||
| 203 | &call->u.msg.data); |
||
| 204 | if (callid == IPC_CALLRET_TEMPORARY) |
||
| 205 | break; |
||
| 206 | list_remove(&call->list); |
||
| 1350 | palkovsky | 207 | |
| 999 | palkovsky | 208 | if (callid == IPC_CALLRET_FATAL) { |
| 1350 | palkovsky | 209 | futex_up(&ipc_futex); |
| 1443 | palkovsky | 210 | if (call->callback) |
| 211 | call->callback(call->private, ENOENT, NULL); |
||
| 999 | palkovsky | 212 | free(call); |
| 1350 | palkovsky | 213 | futex_down(&ipc_futex); |
| 999 | palkovsky | 214 | } else { |
| 215 | call->u.callid = callid; |
||
| 216 | list_append(&call->list, &dispatched_calls); |
||
| 217 | } |
||
| 218 | } |
||
| 1350 | palkovsky | 219 | futex_up(&ipc_futex); |
| 999 | palkovsky | 220 | } |
| 221 | |||
| 222 | /** Handle received answer |
||
| 223 | * |
||
| 224 | * TODO: Make it use hash table |
||
| 225 | * |
||
| 226 | * @param callid Callid (with first bit set) of the answered call |
||
| 227 | */ |
||
| 1091 | palkovsky | 228 | static void handle_answer(ipc_callid_t callid, ipc_call_t *data) |
| 999 | palkovsky | 229 | { |
| 230 | link_t *item; |
||
| 231 | async_call_t *call; |
||
| 232 | |||
| 233 | callid &= ~IPC_CALLID_ANSWERED; |
||
| 234 | |||
| 1350 | palkovsky | 235 | futex_down(&ipc_futex); |
| 999 | palkovsky | 236 | for (item = dispatched_calls.next; item != &dispatched_calls; |
| 237 | item = item->next) { |
||
| 238 | call = list_get_instance(item, async_call_t, list); |
||
| 239 | if (call->u.callid == callid) { |
||
| 240 | list_remove(&call->list); |
||
| 1350 | palkovsky | 241 | futex_up(&ipc_futex); |
| 1443 | palkovsky | 242 | if (call->callback) |
| 243 | call->callback(call->private, |
||
| 244 | IPC_GET_RETVAL(*data), |
||
| 245 | data); |
||
| 246 | free(call); |
||
| 999 | palkovsky | 247 | return; |
| 248 | } |
||
| 249 | } |
||
| 1350 | palkovsky | 250 | futex_up(&ipc_futex); |
| 999 | palkovsky | 251 | printf("Received unidentified answer: %P!!!\n", callid); |
| 252 | } |
||
| 253 | |||
| 254 | |||
| 1392 | palkovsky | 255 | /** One cycle of ipc wait for call call |
| 954 | palkovsky | 256 | * |
| 257 | * - dispatch ASYNC reoutines in the background |
||
| 1365 | jermar | 258 | * @param call Space where the message is stored |
| 1392 | palkovsky | 259 | * @param usec Timeout in microseconds |
| 260 | * @param flags Flags passed to SYS_IPC_WAIT (blocking, nonblocking) |
||
| 1365 | jermar | 261 | * @return Callid of the answer. |
| 954 | palkovsky | 262 | */ |
| 1392 | palkovsky | 263 | ipc_callid_t ipc_wait_cycle(ipc_call_t *call, uint32_t usec, int flags) |
| 954 | palkovsky | 264 | { |
| 265 | ipc_callid_t callid; |
||
| 266 | |||
| 1392 | palkovsky | 267 | try_dispatch_queued_calls(); |
| 268 | |||
| 269 | callid = __SYSCALL3(SYS_IPC_WAIT, (sysarg_t) call, usec, flags); |
||
| 270 | /* Handle received answers */ |
||
| 271 | if (callid & IPC_CALLID_ANSWERED) |
||
| 272 | handle_answer(callid, call); |
||
| 999 | palkovsky | 273 | |
| 954 | palkovsky | 274 | return callid; |
| 275 | } |
||
| 1028 | palkovsky | 276 | |
| 1365 | jermar | 277 | /** Wait some time for an IPC call. |
| 278 | * |
||
| 279 | * - dispatch ASYNC reoutines in the background |
||
| 280 | * @param call Space where the message is stored |
||
| 281 | * @param usec Timeout in microseconds. |
||
| 282 | * @return Callid of the answer. |
||
| 283 | */ |
||
| 284 | ipc_callid_t ipc_wait_for_call_timeout(ipc_call_t *call, uint32_t usec) |
||
| 285 | { |
||
| 286 | ipc_callid_t callid; |
||
| 287 | |||
| 288 | do { |
||
| 1392 | palkovsky | 289 | callid = ipc_wait_cycle(call, usec, SYNCH_BLOCKING); |
| 1365 | jermar | 290 | } while (callid & IPC_CALLID_ANSWERED); |
| 291 | |||
| 292 | return callid; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** Check if there is an IPC call waiting to be picked up. |
||
| 296 | * |
||
| 297 | * - dispatch ASYNC reoutines in the background |
||
| 298 | * @param call Space where the message is stored |
||
| 299 | * @return Callid of the answer. |
||
| 300 | */ |
||
| 301 | ipc_callid_t ipc_trywait_for_call(ipc_call_t *call) |
||
| 302 | { |
||
| 303 | ipc_callid_t callid; |
||
| 304 | |||
| 305 | do { |
||
| 1392 | palkovsky | 306 | callid = ipc_wait_cycle(call, SYNCH_NO_TIMEOUT, SYNCH_NON_BLOCKING); |
| 1365 | jermar | 307 | } while (callid & IPC_CALLID_ANSWERED); |
| 308 | |||
| 309 | return callid; |
||
| 310 | } |
||
| 311 | |||
| 1091 | palkovsky | 312 | /** Ask destination to do a callback connection |
| 313 | * |
||
| 314 | * @return 0 - OK, error code |
||
| 315 | */ |
||
| 316 | int ipc_connect_to_me(int phoneid, int arg1, int arg2, ipcarg_t *phone) |
||
| 1028 | palkovsky | 317 | { |
| 1091 | palkovsky | 318 | return ipc_call_sync_3(phoneid, IPC_M_CONNECT_TO_ME, arg1, |
| 319 | arg2, 0, 0, 0, phone); |
||
| 1028 | palkovsky | 320 | } |
| 1061 | palkovsky | 321 | |
| 1091 | palkovsky | 322 | /** Ask through phone for a new connection to some service |
| 323 | * |
||
| 324 | * @return new phoneid - OK, error code |
||
| 325 | */ |
||
| 1061 | palkovsky | 326 | int ipc_connect_me_to(int phoneid, int arg1, int arg2) |
| 327 | { |
||
| 1092 | palkovsky | 328 | ipcarg_t newphid; |
| 1091 | palkovsky | 329 | int res; |
| 330 | |||
| 331 | res = ipc_call_sync_3(phoneid, IPC_M_CONNECT_ME_TO, arg1, |
||
| 332 | arg2, 0, 0, 0, &newphid); |
||
| 333 | if (res) |
||
| 334 | return res; |
||
| 335 | return newphid; |
||
| 1061 | palkovsky | 336 | } |
| 337 | |||
| 1089 | palkovsky | 338 | /* Hang up specified phone */ |
| 339 | int ipc_hangup(int phoneid) |
||
| 340 | { |
||
| 341 | return __SYSCALL1(SYS_IPC_HANGUP, phoneid); |
||
| 342 | } |
||
| 1259 | palkovsky | 343 | |
| 1282 | palkovsky | 344 | int ipc_register_irq(int irq, irq_code_t *ucode) |
| 1259 | palkovsky | 345 | { |
| 1282 | palkovsky | 346 | return __SYSCALL2(SYS_IPC_REGISTER_IRQ, irq, (sysarg_t) ucode); |
| 1259 | palkovsky | 347 | } |
| 348 | |||
| 349 | int ipc_unregister_irq(int irq) |
||
| 350 | { |
||
| 351 | return __SYSCALL1(SYS_IPC_UNREGISTER_IRQ, irq); |
||
| 352 | } |
||
| 1330 | palkovsky | 353 | |
| 1336 | jermar | 354 | int ipc_forward_fast(ipc_callid_t callid, int phoneid, int method, ipcarg_t arg1) |
| 355 | { |
||
| 356 | return __SYSCALL4(SYS_IPC_FORWARD_FAST, callid, phoneid, method, arg1); |
||
| 357 | } |
||
| 358 | |||
| 1350 | palkovsky | 359 | |
| 360 | /** Open shared memory connection over specified phoneid |
||
| 361 | * |
||
| 362 | * |
||
| 1360 | jermar | 363 | * Allocate as_area, notify the other side about our intention |
| 1350 | palkovsky | 364 | * to open the connection |
| 365 | * |
||
| 366 | * @return Connection id identifying this connection |
||
| 367 | */ |
||
| 368 | //int ipc_dgr_open(int pohoneid, size_t bufsize) |
||
| 369 | //{ |
||
| 370 | /* Find new file descriptor in local descriptor table */ |
||
| 371 | /* Create AS_area, initialize structures */ |
||
| 372 | /* Send AS to other side, handle error states */ |
||
| 373 | |||
| 374 | //} |
||
| 1330 | palkovsky | 375 | /* |
| 1350 | palkovsky | 376 | void ipc_dgr_close(int cid) |
| 1330 | palkovsky | 377 | { |
| 378 | } |
||
| 379 | |||
| 1350 | palkovsky | 380 | void * ipc_dgr_alloc(int cid, size_t size) |
| 381 | { |
||
| 382 | } |
||
| 1330 | palkovsky | 383 | |
| 1350 | palkovsky | 384 | void ipc_dgr_free(int cid, void *area) |
| 385 | { |
||
| 386 | |||
| 387 | } |
||
| 388 | |||
| 389 | int ipc_dgr_send(int cid, void *area) |
||
| 390 | { |
||
| 391 | } |
||
| 392 | |||
| 393 | |||
| 394 | int ipc_dgr_send_data(int cid, void *data, size_t size) |
||
| 395 | { |
||
| 396 | } |
||
| 397 | |||
| 1330 | palkovsky | 398 | */ |