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