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