Subversion Repositories HelenOS-historic

Rev

Rev 1084 | Rev 1088 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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     (1<<0) /**< This is answer to a call */
  41. #define IPC_CALL_STATIC_ALLOC (1<<1) /**< This call will not be freed on error */
  42. #define IPC_CALL_DISPATCHED   (1<<2) /**< Call is in dispatch queue */
  43. #define IPC_CALL_DISCARD_ANSWER (1<<3) /**< Answer will not be passed to
  44.                     * userspace, will be discarded */
  45.  
  46. /* Flags for ipc_wait_for_call */
  47. #define IPC_WAIT_NONBLOCKING   1
  48.  
  49. /* Flags of callid (the addresses are aligned at least to 4,
  50.  * that is why we can use bottom 2 bits of the call address
  51.  */
  52. #define IPC_CALLID_ANSWERED       1 /**< Type of this msg is 'answer' */
  53. #define IPC_CALLID_NOTIFICATION   2 /**< Type of this msg is 'notification' */
  54.  
  55. /* Return values from IPC_ASYNC */
  56. #define IPC_CALLRET_FATAL         -1
  57. #define IPC_CALLRET_TEMPORARY     -2
  58.  
  59.  
  60. /* Macros for manipulating calling data */
  61. #define IPC_SET_RETVAL(data, retval)   ((data).args[0] = (retval))
  62. #define IPC_SET_METHOD(data, val)   ((data).args[0] = (val))
  63. #define IPC_SET_ARG1(data, val)   ((data).args[1] = (val))
  64. #define IPC_SET_ARG2(data, val)   ((data).args[2] = (val))
  65. #define IPC_SET_ARG3(data, val)   ((data).args[3] = (val))
  66.  
  67. #define IPC_GET_METHOD(data)           ((data).args[0])
  68. #define IPC_GET_RETVAL(data)           ((data).args[0])
  69.  
  70. #define IPC_GET_ARG1(data)              ((data).args[1])
  71. #define IPC_GET_ARG2(data)              ((data).args[2])
  72. #define IPC_GET_ARG3(data)              ((data).args[3])
  73.  
  74. /* Well known phone descriptors */
  75. #define PHONE_NS              0
  76.  
  77. /* System-specific methods - only through special syscalls
  78.  * These methods have special behaviour
  79.  */
  80. /** Protocol for CONNECT - TO - ME
  81.  *
  82.  * Calling process asks the callee to create a callback connection,
  83.  * so that it can start initiating new messages.
  84.  *
  85.  * The protocol for negotiating is:
  86.  * - sys_connect_to_me - sends a message IPC_M_CONNECTTOME
  87.  * - sys_wait_for_call - upon receipt tries to allocate new phone
  88.  *                       - if it fails, responds with ELIMIT
  89.  *                     - passes call to userspace. If userspace
  90.  *                       responds with error, phone is deallocated and
  91.  *                       error is sent back to caller. Otherwise
  92.  *                       the call is accepted and the response is sent back.
  93.  *                     - the allocated phoneid is passed to userspace
  94.  *                       (on the receiving sid) as ARG3 of the call.
  95.  *                     - the caller obtains taskid of the called thread
  96.  */
  97. #define IPC_M_CONNECT_TO_ME     1
  98. /** Protocol for CONNECT - ME - TO
  99.  *
  100.  * Calling process asks the callee to create for him a new connection.
  101.  * E.g. the caller wants a name server to connect him to print server.
  102.  *
  103.  * The protocol for negotiating is:
  104.  * - sys_connect_me_to - send a synchronous message to name server
  105.  *                       indicating that it wants to be connected to some
  106.  *                       service
  107.  *                     - arg1/2 are user specified, arg3 contains
  108.  *                       address of the phone that should be connected
  109.  *                       (TODO: it leaks to userspace)
  110.  *   recepient         -  if ipc_answer == 0, then accept connection
  111.  *                     -  otherwise connection refused
  112.  *                     -  recepient may forward message. Forwarding
  113.  *                        system message
  114.  *
  115.  */
  116. #define IPC_M_CONNECT_ME_TO     2
  117. /** This message is sent to answerbox when the phone
  118.  * is hung up
  119.  */
  120. #define IPC_M_PHONE_HUNGUP      3
  121.  
  122.  
  123. /* Well-known methods */
  124. #define IPC_M_LAST_SYSTEM     511
  125. #define IPC_M_PING            512
  126. /* User methods */
  127. #define FIRST_USER_METHOD     1024
  128.  
  129. #ifdef KERNEL
  130.  
  131. #include <synch/mutex.h>
  132. #include <synch/condvar.h>
  133. #include <adt/list.h>
  134.  
  135. #define IPC_MAX_PHONES  16
  136.  
  137. typedef struct answerbox_s answerbox_t;
  138. typedef struct phone_s phone_t;
  139. typedef struct {
  140.     __native args[IPC_CALL_LEN];
  141.     phone_t *phone;
  142. }ipc_data_t;
  143.  
  144. struct answerbox_s {
  145.     SPINLOCK_DECLARE(lock);
  146.  
  147.     task_t *task;
  148.  
  149.     waitq_t wq;
  150.  
  151.     link_t connected_phones; /**< Phones connected to this answerbox */
  152.     link_t calls;            /**< Received calls */
  153.     link_t dispatched_calls; /* Should be hash table in the future */
  154.  
  155.     link_t answers;          /**< Answered calls */
  156. };
  157.  
  158. struct phone_s {
  159.     SPINLOCK_DECLARE(lock);
  160.     link_t list;
  161.     answerbox_t *callee;
  162.     int busy;
  163.     atomic_t active_calls;
  164. };
  165.  
  166. typedef struct {
  167.     link_t list;
  168.  
  169.     int flags;
  170.  
  171.     /* Identification of the caller */
  172.     task_t *sender;
  173.     /* The caller box is different from sender->answerbox
  174.      * for synchronous calls
  175.      */
  176.     answerbox_t *callerbox;
  177.  
  178.     ipc_data_t data;
  179. }call_t;
  180.  
  181. extern void ipc_init(void);
  182. extern call_t * ipc_wait_for_call(answerbox_t *box, int flags);
  183. extern void ipc_answer(answerbox_t *box, call_t *request);
  184. extern void ipc_call(phone_t *phone, call_t *request);
  185. extern void ipc_call_sync(phone_t *phone, call_t *request);
  186. extern void ipc_phone_init(phone_t *phone);
  187. extern void ipc_phone_connect(phone_t *phone, answerbox_t *box);
  188. extern void ipc_call_free(call_t *call);
  189. extern call_t * ipc_call_alloc(void);
  190. extern void ipc_answerbox_init(answerbox_t *box);
  191. extern void ipc_call_static_init(call_t *call);
  192. extern void task_print_list(void);
  193. extern void ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox);
  194.  
  195. extern answerbox_t *ipc_phone_0;
  196. extern void ipc_cleanup(task_t *task);
  197. extern int ipc_phone_hangup(phone_t *phone);
  198.  
  199. #endif
  200.  
  201. #endif
  202.