Rev 1757 | Rev 1787 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1757 | Rev 1780 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericipc |
29 | /** @addtogroup genericipc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | /* Lock ordering |
35 | /* Lock ordering |
36 | * |
36 | * |
37 | * First the answerbox, then the phone |
37 | * First the answerbox, then the phone |
38 | */ |
38 | */ |
39 | 39 | ||
40 | #include <synch/spinlock.h> |
40 | #include <synch/spinlock.h> |
41 | #include <synch/waitq.h> |
41 | #include <synch/waitq.h> |
42 | #include <synch/synch.h> |
42 | #include <synch/synch.h> |
43 | #include <ipc/ipc.h> |
43 | #include <ipc/ipc.h> |
44 | #include <errno.h> |
44 | #include <errno.h> |
45 | #include <mm/slab.h> |
45 | #include <mm/slab.h> |
46 | #include <arch.h> |
46 | #include <arch.h> |
47 | #include <proc/task.h> |
47 | #include <proc/task.h> |
48 | #include <memstr.h> |
48 | #include <memstr.h> |
49 | #include <debug.h> |
49 | #include <debug.h> |
50 | 50 | ||
51 | #include <print.h> |
51 | #include <print.h> |
52 | #include <proc/thread.h> |
52 | #include <proc/thread.h> |
53 | #include <arch/interrupt.h> |
53 | #include <arch/interrupt.h> |
54 | #include <ipc/irq.h> |
54 | #include <ipc/irq.h> |
55 | 55 | ||
56 | /* Open channel that is assigned automatically to new tasks */ |
56 | /* Open channel that is assigned automatically to new tasks */ |
57 | answerbox_t *ipc_phone_0 = NULL; |
57 | answerbox_t *ipc_phone_0 = NULL; |
58 | 58 | ||
59 | static slab_cache_t *ipc_call_slab; |
59 | static slab_cache_t *ipc_call_slab; |
60 | 60 | ||
61 | /* Initialize new call */ |
61 | /* Initialize new call */ |
62 | static void _ipc_call_init(call_t *call) |
62 | static void _ipc_call_init(call_t *call) |
63 | { |
63 | { |
64 | memsetb((__address)call, sizeof(*call), 0); |
64 | memsetb((uintptr_t)call, sizeof(*call), 0); |
65 | call->callerbox = &TASK->answerbox; |
65 | call->callerbox = &TASK->answerbox; |
66 | call->sender = TASK; |
66 | call->sender = TASK; |
67 | } |
67 | } |
68 | 68 | ||
69 | /** Allocate & initialize call structure |
69 | /** Allocate & initialize call structure |
70 | * |
70 | * |
71 | * The call is initialized, so that the reply will be directed |
71 | * The call is initialized, so that the reply will be directed |
72 | * to TASK->answerbox |
72 | * to TASK->answerbox |
73 | * |
73 | * |
74 | * @param flags Parameters for slab_alloc (ATOMIC, etc.) |
74 | * @param flags Parameters for slab_alloc (ATOMIC, etc.) |
75 | */ |
75 | */ |
76 | call_t * ipc_call_alloc(int flags) |
76 | call_t * ipc_call_alloc(int flags) |
77 | { |
77 | { |
78 | call_t *call; |
78 | call_t *call; |
79 | 79 | ||
80 | call = slab_alloc(ipc_call_slab, flags); |
80 | call = slab_alloc(ipc_call_slab, flags); |
81 | _ipc_call_init(call); |
81 | _ipc_call_init(call); |
82 | 82 | ||
83 | return call; |
83 | return call; |
84 | } |
84 | } |
85 | 85 | ||
86 | /** Initialize allocated call */ |
86 | /** Initialize allocated call */ |
87 | void ipc_call_static_init(call_t *call) |
87 | void ipc_call_static_init(call_t *call) |
88 | { |
88 | { |
89 | _ipc_call_init(call); |
89 | _ipc_call_init(call); |
90 | call->flags |= IPC_CALL_STATIC_ALLOC; |
90 | call->flags |= IPC_CALL_STATIC_ALLOC; |
91 | } |
91 | } |
92 | 92 | ||
93 | /** Deallocate call stracuture */ |
93 | /** Deallocate call stracuture */ |
94 | void ipc_call_free(call_t *call) |
94 | void ipc_call_free(call_t *call) |
95 | { |
95 | { |
96 | slab_free(ipc_call_slab, call); |
96 | slab_free(ipc_call_slab, call); |
97 | } |
97 | } |
98 | 98 | ||
99 | /** Initialize answerbox structure |
99 | /** Initialize answerbox structure |
100 | */ |
100 | */ |
101 | void ipc_answerbox_init(answerbox_t *box) |
101 | void ipc_answerbox_init(answerbox_t *box) |
102 | { |
102 | { |
103 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
103 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
104 | spinlock_initialize(&box->irq_lock, "ipc_box_irqlock"); |
104 | spinlock_initialize(&box->irq_lock, "ipc_box_irqlock"); |
105 | waitq_initialize(&box->wq); |
105 | waitq_initialize(&box->wq); |
106 | list_initialize(&box->connected_phones); |
106 | list_initialize(&box->connected_phones); |
107 | list_initialize(&box->calls); |
107 | list_initialize(&box->calls); |
108 | list_initialize(&box->dispatched_calls); |
108 | list_initialize(&box->dispatched_calls); |
109 | list_initialize(&box->answers); |
109 | list_initialize(&box->answers); |
110 | list_initialize(&box->irq_notifs); |
110 | list_initialize(&box->irq_notifs); |
111 | box->task = TASK; |
111 | box->task = TASK; |
112 | } |
112 | } |
113 | 113 | ||
114 | /** Connect phone to answerbox */ |
114 | /** Connect phone to answerbox */ |
115 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
115 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
116 | { |
116 | { |
117 | spinlock_lock(&phone->lock); |
117 | spinlock_lock(&phone->lock); |
118 | 118 | ||
119 | phone->state = IPC_PHONE_CONNECTED; |
119 | phone->state = IPC_PHONE_CONNECTED; |
120 | phone->callee = box; |
120 | phone->callee = box; |
121 | 121 | ||
122 | spinlock_lock(&box->lock); |
122 | spinlock_lock(&box->lock); |
123 | list_append(&phone->link, &box->connected_phones); |
123 | list_append(&phone->link, &box->connected_phones); |
124 | spinlock_unlock(&box->lock); |
124 | spinlock_unlock(&box->lock); |
125 | 125 | ||
126 | spinlock_unlock(&phone->lock); |
126 | spinlock_unlock(&phone->lock); |
127 | } |
127 | } |
128 | 128 | ||
129 | /** Initialize phone structure and connect phone to answerbox |
129 | /** Initialize phone structure and connect phone to answerbox |
130 | */ |
130 | */ |
131 | void ipc_phone_init(phone_t *phone) |
131 | void ipc_phone_init(phone_t *phone) |
132 | { |
132 | { |
133 | spinlock_initialize(&phone->lock, "phone_lock"); |
133 | spinlock_initialize(&phone->lock, "phone_lock"); |
134 | phone->callee = NULL; |
134 | phone->callee = NULL; |
135 | phone->state = IPC_PHONE_FREE; |
135 | phone->state = IPC_PHONE_FREE; |
136 | atomic_set(&phone->active_calls, 0); |
136 | atomic_set(&phone->active_calls, 0); |
137 | } |
137 | } |
138 | 138 | ||
139 | /** Helper function to facilitate synchronous calls */ |
139 | /** Helper function to facilitate synchronous calls */ |
140 | void ipc_call_sync(phone_t *phone, call_t *request) |
140 | void ipc_call_sync(phone_t *phone, call_t *request) |
141 | { |
141 | { |
142 | answerbox_t sync_box; |
142 | answerbox_t sync_box; |
143 | 143 | ||
144 | ipc_answerbox_init(&sync_box); |
144 | ipc_answerbox_init(&sync_box); |
145 | 145 | ||
146 | /* We will receive data on special box */ |
146 | /* We will receive data on special box */ |
147 | request->callerbox = &sync_box; |
147 | request->callerbox = &sync_box; |
148 | 148 | ||
149 | ipc_call(phone, request); |
149 | ipc_call(phone, request); |
150 | ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE); |
150 | ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE); |
151 | } |
151 | } |
152 | 152 | ||
153 | /** Answer message that was not dispatched and is not entered in |
153 | /** Answer message that was not dispatched and is not entered in |
154 | * any queue |
154 | * any queue |
155 | */ |
155 | */ |
156 | static void _ipc_answer_free_call(call_t *call) |
156 | static void _ipc_answer_free_call(call_t *call) |
157 | { |
157 | { |
158 | answerbox_t *callerbox = call->callerbox; |
158 | answerbox_t *callerbox = call->callerbox; |
159 | 159 | ||
160 | call->flags |= IPC_CALL_ANSWERED; |
160 | call->flags |= IPC_CALL_ANSWERED; |
161 | 161 | ||
162 | spinlock_lock(&callerbox->lock); |
162 | spinlock_lock(&callerbox->lock); |
163 | list_append(&call->link, &callerbox->answers); |
163 | list_append(&call->link, &callerbox->answers); |
164 | spinlock_unlock(&callerbox->lock); |
164 | spinlock_unlock(&callerbox->lock); |
165 | waitq_wakeup(&callerbox->wq, 0); |
165 | waitq_wakeup(&callerbox->wq, 0); |
166 | } |
166 | } |
167 | 167 | ||
168 | /** Answer message, that is in callee queue |
168 | /** Answer message, that is in callee queue |
169 | * |
169 | * |
170 | * @param box Answerbox that is answering the message |
170 | * @param box Answerbox that is answering the message |
171 | * @param call Modified request that is being sent back |
171 | * @param call Modified request that is being sent back |
172 | */ |
172 | */ |
173 | void ipc_answer(answerbox_t *box, call_t *call) |
173 | void ipc_answer(answerbox_t *box, call_t *call) |
174 | { |
174 | { |
175 | /* Remove from active box */ |
175 | /* Remove from active box */ |
176 | spinlock_lock(&box->lock); |
176 | spinlock_lock(&box->lock); |
177 | list_remove(&call->link); |
177 | list_remove(&call->link); |
178 | spinlock_unlock(&box->lock); |
178 | spinlock_unlock(&box->lock); |
179 | /* Send back answer */ |
179 | /* Send back answer */ |
180 | _ipc_answer_free_call(call); |
180 | _ipc_answer_free_call(call); |
181 | } |
181 | } |
182 | 182 | ||
183 | /** Simulate sending back a message |
183 | /** Simulate sending back a message |
184 | * |
184 | * |
185 | * Most errors are better handled by forming a normal backward |
185 | * Most errors are better handled by forming a normal backward |
186 | * message and sending it as a normal answer. |
186 | * message and sending it as a normal answer. |
187 | */ |
187 | */ |
188 | void ipc_backsend_err(phone_t *phone, call_t *call, __native err) |
188 | void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err) |
189 | { |
189 | { |
190 | call->data.phone = phone; |
190 | call->data.phone = phone; |
191 | atomic_inc(&phone->active_calls); |
191 | atomic_inc(&phone->active_calls); |
192 | IPC_SET_RETVAL(call->data, err); |
192 | IPC_SET_RETVAL(call->data, err); |
193 | _ipc_answer_free_call(call); |
193 | _ipc_answer_free_call(call); |
194 | } |
194 | } |
195 | 195 | ||
196 | /* Unsafe unchecking ipc_call */ |
196 | /* Unsafe unchecking ipc_call */ |
197 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call) |
197 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call) |
198 | { |
198 | { |
199 | if (! (call->flags & IPC_CALL_FORWARDED)) { |
199 | if (! (call->flags & IPC_CALL_FORWARDED)) { |
200 | atomic_inc(&phone->active_calls); |
200 | atomic_inc(&phone->active_calls); |
201 | call->data.phone = phone; |
201 | call->data.phone = phone; |
202 | } |
202 | } |
203 | 203 | ||
204 | spinlock_lock(&box->lock); |
204 | spinlock_lock(&box->lock); |
205 | list_append(&call->link, &box->calls); |
205 | list_append(&call->link, &box->calls); |
206 | spinlock_unlock(&box->lock); |
206 | spinlock_unlock(&box->lock); |
207 | waitq_wakeup(&box->wq, 0); |
207 | waitq_wakeup(&box->wq, 0); |
208 | } |
208 | } |
209 | 209 | ||
210 | /** Send a asynchronous request using phone to answerbox |
210 | /** Send a asynchronous request using phone to answerbox |
211 | * |
211 | * |
212 | * @param phone Phone connected to answerbox. |
212 | * @param phone Phone connected to answerbox. |
213 | * @param call Structure representing the call. |
213 | * @param call Structure representing the call. |
214 | */ |
214 | */ |
215 | int ipc_call(phone_t *phone, call_t *call) |
215 | int ipc_call(phone_t *phone, call_t *call) |
216 | { |
216 | { |
217 | answerbox_t *box; |
217 | answerbox_t *box; |
218 | 218 | ||
219 | spinlock_lock(&phone->lock); |
219 | spinlock_lock(&phone->lock); |
220 | if (phone->state != IPC_PHONE_CONNECTED) { |
220 | if (phone->state != IPC_PHONE_CONNECTED) { |
221 | spinlock_unlock(&phone->lock); |
221 | spinlock_unlock(&phone->lock); |
222 | if (call->flags & IPC_CALL_FORWARDED) { |
222 | if (call->flags & IPC_CALL_FORWARDED) { |
223 | IPC_SET_RETVAL(call->data, EFORWARD); |
223 | IPC_SET_RETVAL(call->data, EFORWARD); |
224 | _ipc_answer_free_call(call); |
224 | _ipc_answer_free_call(call); |
225 | } else { |
225 | } else { |
226 | if (phone->state == IPC_PHONE_HUNGUP) |
226 | if (phone->state == IPC_PHONE_HUNGUP) |
227 | ipc_backsend_err(phone, call, EHANGUP); |
227 | ipc_backsend_err(phone, call, EHANGUP); |
228 | else |
228 | else |
229 | ipc_backsend_err(phone, call, ENOENT); |
229 | ipc_backsend_err(phone, call, ENOENT); |
230 | } |
230 | } |
231 | return ENOENT; |
231 | return ENOENT; |
232 | } |
232 | } |
233 | box = phone->callee; |
233 | box = phone->callee; |
234 | _ipc_call(phone, box, call); |
234 | _ipc_call(phone, box, call); |
235 | 235 | ||
236 | spinlock_unlock(&phone->lock); |
236 | spinlock_unlock(&phone->lock); |
237 | return 0; |
237 | return 0; |
238 | } |
238 | } |
239 | 239 | ||
240 | /** Disconnect phone from answerbox |
240 | /** Disconnect phone from answerbox |
241 | * |
241 | * |
242 | * This call leaves the phone in HUNGUP state. The change to 'free' is done |
242 | * This call leaves the phone in HUNGUP state. The change to 'free' is done |
243 | * lazily later. |
243 | * lazily later. |
244 | * |
244 | * |
245 | * @param phone Phone to be hung up |
245 | * @param phone Phone to be hung up |
246 | * |
246 | * |
247 | * @return 0 - phone disconnected, -1 - the phone was already disconnected |
247 | * @return 0 - phone disconnected, -1 - the phone was already disconnected |
248 | */ |
248 | */ |
249 | int ipc_phone_hangup(phone_t *phone) |
249 | int ipc_phone_hangup(phone_t *phone) |
250 | { |
250 | { |
251 | answerbox_t *box; |
251 | answerbox_t *box; |
252 | call_t *call; |
252 | call_t *call; |
253 | 253 | ||
254 | spinlock_lock(&phone->lock); |
254 | spinlock_lock(&phone->lock); |
255 | if (phone->state == IPC_PHONE_FREE || phone->state ==IPC_PHONE_HUNGUP \ |
255 | if (phone->state == IPC_PHONE_FREE || phone->state ==IPC_PHONE_HUNGUP \ |
256 | || phone->state == IPC_PHONE_CONNECTING) { |
256 | || phone->state == IPC_PHONE_CONNECTING) { |
257 | spinlock_unlock(&phone->lock); |
257 | spinlock_unlock(&phone->lock); |
258 | return -1; |
258 | return -1; |
259 | } |
259 | } |
260 | box = phone->callee; |
260 | box = phone->callee; |
261 | if (phone->state != IPC_PHONE_SLAMMED) { |
261 | if (phone->state != IPC_PHONE_SLAMMED) { |
262 | /* Remove myself from answerbox */ |
262 | /* Remove myself from answerbox */ |
263 | spinlock_lock(&box->lock); |
263 | spinlock_lock(&box->lock); |
264 | list_remove(&phone->link); |
264 | list_remove(&phone->link); |
265 | spinlock_unlock(&box->lock); |
265 | spinlock_unlock(&box->lock); |
266 | 266 | ||
267 | if (phone->state != IPC_PHONE_SLAMMED) { |
267 | if (phone->state != IPC_PHONE_SLAMMED) { |
268 | call = ipc_call_alloc(0); |
268 | call = ipc_call_alloc(0); |
269 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
269 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
270 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
270 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
271 | _ipc_call(phone, box, call); |
271 | _ipc_call(phone, box, call); |
272 | } |
272 | } |
273 | } |
273 | } |
274 | 274 | ||
275 | phone->state = IPC_PHONE_HUNGUP; |
275 | phone->state = IPC_PHONE_HUNGUP; |
276 | spinlock_unlock(&phone->lock); |
276 | spinlock_unlock(&phone->lock); |
277 | 277 | ||
278 | return 0; |
278 | return 0; |
279 | } |
279 | } |
280 | 280 | ||
281 | /** Forwards call from one answerbox to a new one |
281 | /** Forwards call from one answerbox to a new one |
282 | * |
282 | * |
283 | * @param call Call to be redirected. |
283 | * @param call Call to be redirected. |
284 | * @param newphone Phone to target answerbox. |
284 | * @param newphone Phone to target answerbox. |
285 | * @param oldbox Old answerbox |
285 | * @param oldbox Old answerbox |
286 | * @return 0 on forward ok, error code, if there was error |
286 | * @return 0 on forward ok, error code, if there was error |
287 | * |
287 | * |
288 | * - the return value serves only as an information for the forwarder, |
288 | * - the return value serves only as an information for the forwarder, |
289 | * the original caller is notified automatically with EFORWARD |
289 | * the original caller is notified automatically with EFORWARD |
290 | */ |
290 | */ |
291 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox) |
291 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox) |
292 | { |
292 | { |
293 | spinlock_lock(&oldbox->lock); |
293 | spinlock_lock(&oldbox->lock); |
294 | list_remove(&call->link); |
294 | list_remove(&call->link); |
295 | spinlock_unlock(&oldbox->lock); |
295 | spinlock_unlock(&oldbox->lock); |
296 | 296 | ||
297 | return ipc_call(newphone, call); |
297 | return ipc_call(newphone, call); |
298 | } |
298 | } |
299 | 299 | ||
300 | 300 | ||
301 | /** Wait for phone call |
301 | /** Wait for phone call |
302 | * |
302 | * |
303 | * @param box Answerbox expecting the call. |
303 | * @param box Answerbox expecting the call. |
304 | * @param usec Timeout in microseconds. See documentation for waitq_sleep_timeout() for |
304 | * @param usec Timeout in microseconds. See documentation for waitq_sleep_timeout() for |
305 | * decription of its special meaning. |
305 | * decription of its special meaning. |
306 | * @param flags Select mode of sleep operation. See documentation for waitq_sleep_timeout()i |
306 | * @param flags Select mode of sleep operation. See documentation for waitq_sleep_timeout()i |
307 | * for description of its special meaning. |
307 | * for description of its special meaning. |
308 | * @return Recived message address |
308 | * @return Recived message address |
309 | * - to distinguish between call and answer, look at call->flags |
309 | * - to distinguish between call and answer, look at call->flags |
310 | */ |
310 | */ |
311 | call_t * ipc_wait_for_call(answerbox_t *box, __u32 usec, int flags) |
311 | call_t * ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags) |
312 | { |
312 | { |
313 | call_t *request; |
313 | call_t *request; |
314 | ipl_t ipl; |
314 | ipl_t ipl; |
315 | int rc; |
315 | int rc; |
316 | 316 | ||
317 | restart: |
317 | restart: |
318 | rc = waitq_sleep_timeout(&box->wq, usec, flags); |
318 | rc = waitq_sleep_timeout(&box->wq, usec, flags); |
319 | if (SYNCH_FAILED(rc)) |
319 | if (SYNCH_FAILED(rc)) |
320 | return NULL; |
320 | return NULL; |
321 | 321 | ||
322 | spinlock_lock(&box->lock); |
322 | spinlock_lock(&box->lock); |
323 | if (!list_empty(&box->irq_notifs)) { |
323 | if (!list_empty(&box->irq_notifs)) { |
324 | ipl = interrupts_disable(); |
324 | ipl = interrupts_disable(); |
325 | spinlock_lock(&box->irq_lock); |
325 | spinlock_lock(&box->irq_lock); |
326 | 326 | ||
327 | request = list_get_instance(box->irq_notifs.next, call_t, link); |
327 | request = list_get_instance(box->irq_notifs.next, call_t, link); |
328 | list_remove(&request->link); |
328 | list_remove(&request->link); |
329 | 329 | ||
330 | spinlock_unlock(&box->irq_lock); |
330 | spinlock_unlock(&box->irq_lock); |
331 | interrupts_restore(ipl); |
331 | interrupts_restore(ipl); |
332 | } else if (!list_empty(&box->answers)) { |
332 | } else if (!list_empty(&box->answers)) { |
333 | /* Handle asynchronous answers */ |
333 | /* Handle asynchronous answers */ |
334 | request = list_get_instance(box->answers.next, call_t, link); |
334 | request = list_get_instance(box->answers.next, call_t, link); |
335 | list_remove(&request->link); |
335 | list_remove(&request->link); |
336 | atomic_dec(&request->data.phone->active_calls); |
336 | atomic_dec(&request->data.phone->active_calls); |
337 | } else if (!list_empty(&box->calls)) { |
337 | } else if (!list_empty(&box->calls)) { |
338 | /* Handle requests */ |
338 | /* Handle requests */ |
339 | request = list_get_instance(box->calls.next, call_t, link); |
339 | request = list_get_instance(box->calls.next, call_t, link); |
340 | list_remove(&request->link); |
340 | list_remove(&request->link); |
341 | /* Append request to dispatch queue */ |
341 | /* Append request to dispatch queue */ |
342 | list_append(&request->link, &box->dispatched_calls); |
342 | list_append(&request->link, &box->dispatched_calls); |
343 | } else { |
343 | } else { |
344 | /* This can happen regularly after ipc_cleanup */ |
344 | /* This can happen regularly after ipc_cleanup */ |
345 | spinlock_unlock(&box->lock); |
345 | spinlock_unlock(&box->lock); |
346 | goto restart; |
346 | goto restart; |
347 | } |
347 | } |
348 | spinlock_unlock(&box->lock); |
348 | spinlock_unlock(&box->lock); |
349 | return request; |
349 | return request; |
350 | } |
350 | } |
351 | 351 | ||
352 | /** Answer all calls from list with EHANGUP msg */ |
352 | /** Answer all calls from list with EHANGUP msg */ |
353 | static void ipc_cleanup_call_list(link_t *lst) |
353 | static void ipc_cleanup_call_list(link_t *lst) |
354 | { |
354 | { |
355 | call_t *call; |
355 | call_t *call; |
356 | 356 | ||
357 | while (!list_empty(lst)) { |
357 | while (!list_empty(lst)) { |
358 | call = list_get_instance(lst->next, call_t, link); |
358 | call = list_get_instance(lst->next, call_t, link); |
359 | list_remove(&call->link); |
359 | list_remove(&call->link); |
360 | 360 | ||
361 | IPC_SET_RETVAL(call->data, EHANGUP); |
361 | IPC_SET_RETVAL(call->data, EHANGUP); |
362 | _ipc_answer_free_call(call); |
362 | _ipc_answer_free_call(call); |
363 | } |
363 | } |
364 | } |
364 | } |
365 | 365 | ||
366 | /** Cleans up all IPC communication of the current task |
366 | /** Cleans up all IPC communication of the current task |
367 | * |
367 | * |
368 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you |
368 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you |
369 | * have to change it as well if you want to cleanup other current then current. |
369 | * have to change it as well if you want to cleanup other current then current. |
370 | */ |
370 | */ |
371 | void ipc_cleanup(void) |
371 | void ipc_cleanup(void) |
372 | { |
372 | { |
373 | int i; |
373 | int i; |
374 | call_t *call; |
374 | call_t *call; |
375 | phone_t *phone; |
375 | phone_t *phone; |
376 | 376 | ||
377 | /* Disconnect all our phones ('ipc_phone_hangup') */ |
377 | /* Disconnect all our phones ('ipc_phone_hangup') */ |
378 | for (i=0;i < IPC_MAX_PHONES; i++) |
378 | for (i=0;i < IPC_MAX_PHONES; i++) |
379 | ipc_phone_hangup(&TASK->phones[i]); |
379 | ipc_phone_hangup(&TASK->phones[i]); |
380 | 380 | ||
381 | /* Disconnect all connected irqs */ |
381 | /* Disconnect all connected irqs */ |
382 | ipc_irq_cleanup(&TASK->answerbox); |
382 | ipc_irq_cleanup(&TASK->answerbox); |
383 | 383 | ||
384 | /* Disconnect all phones connected to our answerbox */ |
384 | /* Disconnect all phones connected to our answerbox */ |
385 | restart_phones: |
385 | restart_phones: |
386 | spinlock_lock(&TASK->answerbox.lock); |
386 | spinlock_lock(&TASK->answerbox.lock); |
387 | while (!list_empty(&TASK->answerbox.connected_phones)) { |
387 | while (!list_empty(&TASK->answerbox.connected_phones)) { |
388 | phone = list_get_instance(TASK->answerbox.connected_phones.next, |
388 | phone = list_get_instance(TASK->answerbox.connected_phones.next, |
389 | phone_t, link); |
389 | phone_t, link); |
390 | if (! spinlock_trylock(&phone->lock)) { |
390 | if (! spinlock_trylock(&phone->lock)) { |
391 | spinlock_unlock(&TASK->answerbox.lock); |
391 | spinlock_unlock(&TASK->answerbox.lock); |
392 | goto restart_phones; |
392 | goto restart_phones; |
393 | } |
393 | } |
394 | 394 | ||
395 | /* Disconnect phone */ |
395 | /* Disconnect phone */ |
396 | ASSERT(phone->state == IPC_PHONE_CONNECTED); |
396 | ASSERT(phone->state == IPC_PHONE_CONNECTED); |
397 | phone->state = IPC_PHONE_SLAMMED; |
397 | phone->state = IPC_PHONE_SLAMMED; |
398 | list_remove(&phone->link); |
398 | list_remove(&phone->link); |
399 | 399 | ||
400 | spinlock_unlock(&phone->lock); |
400 | spinlock_unlock(&phone->lock); |
401 | } |
401 | } |
402 | 402 | ||
403 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
403 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
404 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls); |
404 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls); |
405 | ipc_cleanup_call_list(&TASK->answerbox.calls); |
405 | ipc_cleanup_call_list(&TASK->answerbox.calls); |
406 | spinlock_unlock(&TASK->answerbox.lock); |
406 | spinlock_unlock(&TASK->answerbox.lock); |
407 | 407 | ||
408 | /* Wait for all async answers to arrive */ |
408 | /* Wait for all async answers to arrive */ |
409 | while (1) { |
409 | while (1) { |
410 | /* Go through all phones, until all are FREE... */ |
410 | /* Go through all phones, until all are FREE... */ |
411 | /* Locking not needed, no one else should modify |
411 | /* Locking not needed, no one else should modify |
412 | * it, when we are in cleanup */ |
412 | * it, when we are in cleanup */ |
413 | for (i=0;i < IPC_MAX_PHONES; i++) { |
413 | for (i=0;i < IPC_MAX_PHONES; i++) { |
414 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP && \ |
414 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP && \ |
415 | atomic_get(&TASK->phones[i].active_calls) == 0) |
415 | atomic_get(&TASK->phones[i].active_calls) == 0) |
416 | TASK->phones[i].state = IPC_PHONE_FREE; |
416 | TASK->phones[i].state = IPC_PHONE_FREE; |
417 | 417 | ||
418 | /* Just for sure, we might have had some |
418 | /* Just for sure, we might have had some |
419 | * IPC_PHONE_CONNECTING phones */ |
419 | * IPC_PHONE_CONNECTING phones */ |
420 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED) |
420 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED) |
421 | ipc_phone_hangup(&TASK->phones[i]); |
421 | ipc_phone_hangup(&TASK->phones[i]); |
422 | /* If the hangup succeeded, it has sent a HANGUP |
422 | /* If the hangup succeeded, it has sent a HANGUP |
423 | * message, the IPC is now in HUNGUP state, we |
423 | * message, the IPC is now in HUNGUP state, we |
424 | * wait for the reply to come */ |
424 | * wait for the reply to come */ |
425 | 425 | ||
426 | if (TASK->phones[i].state != IPC_PHONE_FREE) |
426 | if (TASK->phones[i].state != IPC_PHONE_FREE) |
427 | break; |
427 | break; |
428 | } |
428 | } |
429 | /* Voila, got into cleanup */ |
429 | /* Voila, got into cleanup */ |
430 | if (i == IPC_MAX_PHONES) |
430 | if (i == IPC_MAX_PHONES) |
431 | break; |
431 | break; |
432 | 432 | ||
433 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE); |
433 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE); |
434 | ASSERT((call->flags & IPC_CALL_ANSWERED) || (call->flags & IPC_CALL_NOTIF)); |
434 | ASSERT((call->flags & IPC_CALL_ANSWERED) || (call->flags & IPC_CALL_NOTIF)); |
435 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC)); |
435 | ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC)); |
436 | 436 | ||
437 | atomic_dec(&TASK->active_calls); |
437 | atomic_dec(&TASK->active_calls); |
438 | ipc_call_free(call); |
438 | ipc_call_free(call); |
439 | } |
439 | } |
440 | } |
440 | } |
441 | 441 | ||
442 | 442 | ||
443 | /** Initilize ipc subsystem */ |
443 | /** Initilize ipc subsystem */ |
444 | void ipc_init(void) |
444 | void ipc_init(void) |
445 | { |
445 | { |
446 | ipc_call_slab = slab_cache_create("ipc_call", |
446 | ipc_call_slab = slab_cache_create("ipc_call", |
447 | sizeof(call_t), |
447 | sizeof(call_t), |
448 | 0, |
448 | 0, |
449 | NULL, NULL, 0); |
449 | NULL, NULL, 0); |
450 | ipc_irq_make_table(IRQ_COUNT); |
450 | ipc_irq_make_table(IRQ_COUNT); |
451 | } |
451 | } |
452 | 452 | ||
453 | 453 | ||
454 | /** Kconsole - list answerbox contents */ |
454 | /** Kconsole - list answerbox contents */ |
455 | void ipc_print_task(task_id_t taskid) |
455 | void ipc_print_task(task_id_t taskid) |
456 | { |
456 | { |
457 | task_t *task; |
457 | task_t *task; |
458 | int i; |
458 | int i; |
459 | call_t *call; |
459 | call_t *call; |
460 | link_t *tmp; |
460 | link_t *tmp; |
461 | 461 | ||
462 | spinlock_lock(&tasks_lock); |
462 | spinlock_lock(&tasks_lock); |
463 | task = task_find_by_id(taskid); |
463 | task = task_find_by_id(taskid); |
464 | if (task) |
464 | if (task) |
465 | spinlock_lock(&task->lock); |
465 | spinlock_lock(&task->lock); |
466 | spinlock_unlock(&tasks_lock); |
466 | spinlock_unlock(&tasks_lock); |
467 | if (!task) |
467 | if (!task) |
468 | return; |
468 | return; |
469 | 469 | ||
470 | /* Print opened phones & details */ |
470 | /* Print opened phones & details */ |
471 | printf("PHONE:\n"); |
471 | printf("PHONE:\n"); |
472 | for (i=0; i < IPC_MAX_PHONES;i++) { |
472 | for (i=0; i < IPC_MAX_PHONES;i++) { |
473 | spinlock_lock(&task->phones[i].lock); |
473 | spinlock_lock(&task->phones[i].lock); |
474 | if (task->phones[i].state != IPC_PHONE_FREE) { |
474 | if (task->phones[i].state != IPC_PHONE_FREE) { |
475 | printf("%d: ",i); |
475 | printf("%d: ",i); |
476 | switch (task->phones[i].state) { |
476 | switch (task->phones[i].state) { |
477 | case IPC_PHONE_CONNECTING: |
477 | case IPC_PHONE_CONNECTING: |
478 | printf("connecting "); |
478 | printf("connecting "); |
479 | break; |
479 | break; |
480 | case IPC_PHONE_CONNECTED: |
480 | case IPC_PHONE_CONNECTED: |
481 | printf("connected to: %p ", |
481 | printf("connected to: %p ", |
482 | task->phones[i].callee); |
482 | task->phones[i].callee); |
483 | break; |
483 | break; |
484 | case IPC_PHONE_SLAMMED: |
484 | case IPC_PHONE_SLAMMED: |
485 | printf("slammed by: %p ", |
485 | printf("slammed by: %p ", |
486 | task->phones[i].callee); |
486 | task->phones[i].callee); |
487 | break; |
487 | break; |
488 | case IPC_PHONE_HUNGUP: |
488 | case IPC_PHONE_HUNGUP: |
489 | printf("hung up - was: %p ", |
489 | printf("hung up - was: %p ", |
490 | task->phones[i].callee); |
490 | task->phones[i].callee); |
491 | break; |
491 | break; |
492 | default: |
492 | default: |
493 | break; |
493 | break; |
494 | } |
494 | } |
495 | printf("active: %d\n", atomic_get(&task->phones[i].active_calls)); |
495 | printf("active: %d\n", atomic_get(&task->phones[i].active_calls)); |
496 | } |
496 | } |
497 | spinlock_unlock(&task->phones[i].lock); |
497 | spinlock_unlock(&task->phones[i].lock); |
498 | } |
498 | } |
499 | 499 | ||
500 | 500 | ||
501 | /* Print answerbox - calls */ |
501 | /* Print answerbox - calls */ |
502 | spinlock_lock(&task->answerbox.lock); |
502 | spinlock_lock(&task->answerbox.lock); |
503 | printf("ABOX - CALLS:\n"); |
503 | printf("ABOX - CALLS:\n"); |
504 | for (tmp=task->answerbox.calls.next; tmp != &task->answerbox.calls;tmp = tmp->next) { |
504 | for (tmp=task->answerbox.calls.next; tmp != &task->answerbox.calls;tmp = tmp->next) { |
505 | call = list_get_instance(tmp, call_t, link); |
505 | call = list_get_instance(tmp, call_t, link); |
506 | printf("Callid: %p Srctask:%lld M:%d A1:%d A2:%d A3:%d Flags:%x\n",call, |
506 | printf("Callid: %p Srctask:%lld M:%d A1:%d A2:%d A3:%d Flags:%x\n",call, |
507 | call->sender->taskid, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
507 | call->sender->taskid, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
508 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), call->flags); |
508 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), call->flags); |
509 | } |
509 | } |
510 | /* Print answerbox - calls */ |
510 | /* Print answerbox - calls */ |
511 | printf("ABOX - DISPATCHED CALLS:\n"); |
511 | printf("ABOX - DISPATCHED CALLS:\n"); |
512 | for (tmp=task->answerbox.dispatched_calls.next; |
512 | for (tmp=task->answerbox.dispatched_calls.next; |
513 | tmp != &task->answerbox.dispatched_calls; |
513 | tmp != &task->answerbox.dispatched_calls; |
514 | tmp = tmp->next) { |
514 | tmp = tmp->next) { |
515 | call = list_get_instance(tmp, call_t, link); |
515 | call = list_get_instance(tmp, call_t, link); |
516 | printf("Callid: %p Srctask:%lld M:%d A1:%d A2:%d A3:%d Flags:%x\n",call, |
516 | printf("Callid: %p Srctask:%lld M:%d A1:%d A2:%d A3:%d Flags:%x\n",call, |
517 | call->sender->taskid, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
517 | call->sender->taskid, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
518 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), call->flags); |
518 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), call->flags); |
519 | } |
519 | } |
520 | /* Print answerbox - calls */ |
520 | /* Print answerbox - calls */ |
521 | printf("ABOX - ANSWERS:\n"); |
521 | printf("ABOX - ANSWERS:\n"); |
522 | for (tmp=task->answerbox.answers.next; tmp != &task->answerbox.answers; tmp = tmp->next) { |
522 | for (tmp=task->answerbox.answers.next; tmp != &task->answerbox.answers; tmp = tmp->next) { |
523 | call = list_get_instance(tmp, call_t, link); |
523 | call = list_get_instance(tmp, call_t, link); |
524 | printf("Callid:%p M:%d A1:%d A2:%d A3:%d Flags:%x\n",call, |
524 | printf("Callid:%p M:%d A1:%d A2:%d A3:%d Flags:%x\n",call, |
525 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
525 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
526 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), call->flags); |
526 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), call->flags); |
527 | } |
527 | } |
528 | 528 | ||
529 | spinlock_unlock(&task->answerbox.lock); |
529 | spinlock_unlock(&task->answerbox.lock); |
530 | spinlock_unlock(&task->lock); |
530 | spinlock_unlock(&task->lock); |
531 | } |
531 | } |
532 | 532 | ||
533 | /** @} |
533 | /** @} |
534 | */ |
534 | */ |
535 | 535 |