2 * Copyright (c) 2006 Ondrej Palkovsky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup kernel_generic_ipc
38 #include <synch/spinlock.h>
39 #include <synch/mutex.h>
40 #include <synch/waitq.h>
41 #include <abi/ipc/ipc.h>
42 #include <abi/proc/task.h>
52 /** Phone is free and can be allocated */
54 /** Phone is connecting somewhere */
56 /** Phone is connected */
58 /** Phone is hung up, waiting for answers to come */
60 /** Phone was hungup from server */
64 /** Structure identifying phone (in TASK structure) */
65 typedef struct phone
{
69 struct answerbox
*callee
;
70 /* A call prepared for hangup ahead of time, so that it cannot fail. */
71 struct call
*hangup_call
;
72 ipc_phone_state_t state
;
73 atomic_size_t active_calls
;
74 /** User-defined label */
79 typedef struct answerbox
{
80 IRQ_SPINLOCK_DECLARE(lock
);
82 /** Answerbox is active until it enters cleanup. */
90 * Number of answers the answerbox is expecting to eventually arrive.
92 atomic_size_t active_calls
;
94 /** Phones connected to this answerbox. */
95 list_t connected_phones
;
96 /** Received calls. */
98 list_t dispatched_calls
; /* Should be hash table in the future */
100 /** Answered calls. */
103 IRQ_SPINLOCK_DECLARE(irq_lock
);
105 /** Notifications from IRQ handlers. */
109 typedef struct call
{
114 * Valid only when the call is not forgotten.
115 * Protected by the task's active_calls_lock.
119 /** Answerbox link. */
124 /** Protects the forget member. */
125 SPINLOCK_DECLARE(forget_lock
);
128 * True if the caller 'forgot' this call and donated it to the callee.
129 * Forgotten calls are discarded upon answering (the answer is not
130 * delivered) and answered calls cannot be forgotten. Forgotten calls
131 * also do not figure on the task's active call list.
133 * We keep this separate from the flags so that it is not necessary
134 * to take a lock when accessing them.
138 /** True if the call is in the active list. */
142 * Identification of the caller.
143 * Valid only when the call is not forgotten.
148 * Answerbox that will receive the answer.
149 * This will most of the times be the sender's answerbox,
150 * but we allow for useful exceptions.
152 answerbox_t
*callerbox
;
154 /** Phone which was used to send the call. */
155 phone_t
*caller_phone
;
157 /** Private data to internal IPC. */
160 /** Data passed from/to userspace. */
163 /** Method as it was sent in the request. */
164 sysarg_t request_method
;
166 /** Buffer for IPC_M_DATA_WRITE and IPC_M_DATA_READ. */
170 extern slab_cache_t
*phone_cache
;
172 extern answerbox_t
*ipc_box_0
;
174 extern kobject_ops_t call_kobject_ops
;
176 extern void ipc_init(void);
178 extern call_t
*ipc_call_alloc(void);
180 extern errno_t
ipc_call_sync(phone_t
*, call_t
*);
181 extern errno_t
ipc_call(phone_t
*, call_t
*);
182 extern errno_t
ipc_wait_for_call(answerbox_t
*, uint32_t, unsigned int, call_t
**);
183 extern errno_t
ipc_forward(call_t
*, phone_t
*, answerbox_t
*, unsigned int);
184 extern void ipc_answer(answerbox_t
*, call_t
*);
185 extern void _ipc_answer_free_call(call_t
*, bool);
187 extern void ipc_phone_init(phone_t
*, struct task
*);
188 extern bool ipc_phone_connect(phone_t
*, answerbox_t
*);
189 extern errno_t
ipc_phone_hangup(phone_t
*);
191 extern void ipc_answerbox_init(answerbox_t
*, struct task
*);
193 extern void ipc_cleanup(void);
194 extern void ipc_backsend_err(phone_t
*, call_t
*, errno_t
);
195 extern void ipc_answerbox_slam_phones(answerbox_t
*, bool);
196 extern void ipc_cleanup_call_list(answerbox_t
*, list_t
*);
198 extern void ipc_print_task(task_id_t
);