Rev 3425 | Rev 3433 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3425 | Rev 3431 | ||
---|---|---|---|
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/synch.h> |
40 | #include <synch/synch.h> |
41 | #include <synch/spinlock.h> |
41 | #include <synch/spinlock.h> |
42 | #include <synch/mutex.h> |
42 | #include <synch/mutex.h> |
43 | #include <synch/waitq.h> |
43 | #include <synch/waitq.h> |
44 | #include <synch/synch.h> |
44 | #include <synch/synch.h> |
45 | #include <ipc/ipc.h> |
45 | #include <ipc/ipc.h> |
46 | #include <errno.h> |
46 | #include <errno.h> |
47 | #include <mm/slab.h> |
47 | #include <mm/slab.h> |
48 | #include <arch.h> |
48 | #include <arch.h> |
49 | #include <proc/task.h> |
49 | #include <proc/task.h> |
50 | #include <memstr.h> |
50 | #include <memstr.h> |
51 | #include <debug.h> |
51 | #include <debug.h> |
52 | 52 | ||
53 | #include <print.h> |
53 | #include <print.h> |
54 | #include <console/console.h> |
54 | #include <console/console.h> |
55 | #include <proc/thread.h> |
55 | #include <proc/thread.h> |
56 | #include <arch/interrupt.h> |
56 | #include <arch/interrupt.h> |
57 | #include <ipc/irq.h> |
57 | #include <ipc/irq.h> |
58 | 58 | ||
59 | /** Open channel that is assigned automatically to new tasks */ |
59 | /** Open channel that is assigned automatically to new tasks */ |
60 | answerbox_t *ipc_phone_0 = NULL; |
60 | answerbox_t *ipc_phone_0 = NULL; |
61 | 61 | ||
62 | static slab_cache_t *ipc_call_slab; |
62 | static slab_cache_t *ipc_call_slab; |
63 | 63 | ||
64 | /** Initialize a call structure. |
64 | /** Initialize a call structure. |
65 | * |
65 | * |
66 | * @param call Call structure to be initialized. |
66 | * @param call Call structure to be initialized. |
67 | */ |
67 | */ |
68 | static void _ipc_call_init(call_t *call) |
68 | static void _ipc_call_init(call_t *call) |
69 | { |
69 | { |
70 | memsetb(call, sizeof(*call), 0); |
70 | memsetb(call, sizeof(*call), 0); |
71 | call->callerbox = &TASK->answerbox; |
71 | call->callerbox = &TASK->answerbox; |
72 | call->sender = TASK; |
72 | call->sender = TASK; |
73 | call->buffer = NULL; |
73 | call->buffer = NULL; |
74 | } |
74 | } |
75 | 75 | ||
76 | /** Allocate and initialize a call structure. |
76 | /** Allocate and initialize a call structure. |
77 | * |
77 | * |
78 | * The call is initialized, so that the reply will be directed to |
78 | * The call is initialized, so that the reply will be directed to |
79 | * TASK->answerbox. |
79 | * TASK->answerbox. |
80 | * |
80 | * |
81 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC). |
81 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC). |
82 | * |
82 | * |
83 | * @return If flags permit it, return NULL, or initialized kernel |
83 | * @return If flags permit it, return NULL, or initialized kernel |
84 | * call structure. |
84 | * call structure. |
85 | */ |
85 | */ |
86 | call_t *ipc_call_alloc(int flags) |
86 | call_t *ipc_call_alloc(int flags) |
87 | { |
87 | { |
88 | call_t *call; |
88 | call_t *call; |
89 | 89 | ||
90 | call = slab_alloc(ipc_call_slab, flags); |
90 | call = slab_alloc(ipc_call_slab, flags); |
91 | if (call) |
91 | if (call) |
92 | _ipc_call_init(call); |
92 | _ipc_call_init(call); |
93 | 93 | ||
94 | return call; |
94 | return call; |
95 | } |
95 | } |
96 | 96 | ||
97 | /** Initialize a statically allocated call structure. |
97 | /** Initialize a statically allocated call structure. |
98 | * |
98 | * |
99 | * @param call Statically allocated kernel call structure to be |
99 | * @param call Statically allocated kernel call structure to be |
100 | * initialized. |
100 | * initialized. |
101 | */ |
101 | */ |
102 | void ipc_call_static_init(call_t *call) |
102 | void ipc_call_static_init(call_t *call) |
103 | { |
103 | { |
104 | _ipc_call_init(call); |
104 | _ipc_call_init(call); |
105 | call->flags |= IPC_CALL_STATIC_ALLOC; |
105 | call->flags |= IPC_CALL_STATIC_ALLOC; |
106 | } |
106 | } |
107 | 107 | ||
108 | /** Deallocate a call structure. |
108 | /** Deallocate a call structure. |
109 | * |
109 | * |
110 | * @param call Call structure to be freed. |
110 | * @param call Call structure to be freed. |
111 | */ |
111 | */ |
112 | void ipc_call_free(call_t *call) |
112 | void ipc_call_free(call_t *call) |
113 | { |
113 | { |
114 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
114 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
115 | /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */ |
115 | /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */ |
116 | if (call->buffer) |
116 | if (call->buffer) |
117 | free(call->buffer); |
117 | free(call->buffer); |
118 | slab_free(ipc_call_slab, call); |
118 | slab_free(ipc_call_slab, call); |
119 | } |
119 | } |
120 | 120 | ||
121 | /** Initialize an answerbox structure. |
121 | /** Initialize an answerbox structure. |
122 | * |
122 | * |
123 | * @param box Answerbox structure to be initialized. |
123 | * @param box Answerbox structure to be initialized. |
124 | * @param task Task to which the answerbox belongs. |
124 | * @param task Task to which the answerbox belongs. |
125 | */ |
125 | */ |
126 | void ipc_answerbox_init(answerbox_t *box, task_t *task) |
126 | void ipc_answerbox_init(answerbox_t *box, task_t *task) |
127 | { |
127 | { |
128 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
128 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
129 | spinlock_initialize(&box->irq_lock, "ipc_box_irqlock"); |
129 | spinlock_initialize(&box->irq_lock, "ipc_box_irqlock"); |
130 | waitq_initialize(&box->wq); |
130 | waitq_initialize(&box->wq); |
131 | list_initialize(&box->connected_phones); |
131 | list_initialize(&box->connected_phones); |
132 | list_initialize(&box->calls); |
132 | list_initialize(&box->calls); |
133 | list_initialize(&box->dispatched_calls); |
133 | list_initialize(&box->dispatched_calls); |
134 | list_initialize(&box->answers); |
134 | list_initialize(&box->answers); |
135 | list_initialize(&box->irq_notifs); |
135 | list_initialize(&box->irq_notifs); |
136 | list_initialize(&box->irq_head); |
136 | list_initialize(&box->irq_head); |
137 | box->task = task; |
137 | box->task = task; |
138 | } |
138 | } |
139 | 139 | ||
140 | /** Connect a phone to an answerbox. |
140 | /** Connect a phone to an answerbox. |
141 | * |
141 | * |
142 | * @param phone Initialized phone structure. |
142 | * @param phone Initialized phone structure. |
143 | * @param box Initialized answerbox structure. |
143 | * @param box Initialized answerbox structure. |
144 | */ |
144 | */ |
145 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
145 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
146 | { |
146 | { |
147 | mutex_lock(&phone->lock); |
147 | mutex_lock(&phone->lock); |
148 | 148 | ||
149 | phone->state = IPC_PHONE_CONNECTED; |
149 | phone->state = IPC_PHONE_CONNECTED; |
150 | phone->callee = box; |
150 | phone->callee = box; |
151 | 151 | ||
152 | spinlock_lock(&box->lock); |
152 | spinlock_lock(&box->lock); |
153 | list_append(&phone->link, &box->connected_phones); |
153 | list_append(&phone->link, &box->connected_phones); |
154 | spinlock_unlock(&box->lock); |
154 | spinlock_unlock(&box->lock); |
155 | 155 | ||
156 | mutex_unlock(&phone->lock); |
156 | mutex_unlock(&phone->lock); |
157 | } |
157 | } |
158 | 158 | ||
159 | /** Initialize a phone structure. |
159 | /** Initialize a phone structure. |
160 | * |
160 | * |
161 | * @param phone Phone structure to be initialized. |
161 | * @param phone Phone structure to be initialized. |
162 | */ |
162 | */ |
163 | void ipc_phone_init(phone_t *phone) |
163 | void ipc_phone_init(phone_t *phone) |
164 | { |
164 | { |
165 | mutex_initialize(&phone->lock, MUTEX_PASSIVE); |
165 | mutex_initialize(&phone->lock, MUTEX_PASSIVE); |
166 | phone->callee = NULL; |
166 | phone->callee = NULL; |
167 | phone->state = IPC_PHONE_FREE; |
167 | phone->state = IPC_PHONE_FREE; |
168 | atomic_set(&phone->active_calls, 0); |
168 | atomic_set(&phone->active_calls, 0); |
169 | } |
169 | } |
170 | 170 | ||
171 | /** Helper function to facilitate synchronous calls. |
171 | /** Helper function to facilitate synchronous calls. |
172 | * |
172 | * |
173 | * @param phone Destination kernel phone structure. |
173 | * @param phone Destination kernel phone structure. |
174 | * @param request Call structure with request. |
174 | * @param request Call structure with request. |
175 | * |
175 | * |
176 | * @return EOK on success or EINTR if the sleep was interrupted. |
176 | * @return EOK on success or EINTR if the sleep was interrupted. |
177 | */ |
177 | */ |
178 | int ipc_call_sync(phone_t *phone, call_t *request) |
178 | int ipc_call_sync(phone_t *phone, call_t *request) |
179 | { |
179 | { |
180 | answerbox_t sync_box; |
180 | answerbox_t sync_box; |
181 | 181 | ||
182 | ipc_answerbox_init(&sync_box, TASK); |
182 | ipc_answerbox_init(&sync_box, TASK); |
183 | 183 | ||
184 | /* We will receive data in a special box. */ |
184 | /* We will receive data in a special box. */ |
185 | request->callerbox = &sync_box; |
185 | request->callerbox = &sync_box; |
186 | 186 | ||
187 | ipc_call(phone, request); |
187 | ipc_call(phone, request); |
188 | if (!ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, |
188 | if (!ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, |
189 | SYNCH_FLAGS_INTERRUPTIBLE)) |
189 | SYNCH_FLAGS_INTERRUPTIBLE)) |
190 | return EINTR; |
190 | return EINTR; |
191 | return EOK; |
191 | return EOK; |
192 | } |
192 | } |
193 | 193 | ||
194 | /** Answer a message which was not dispatched and is not listed in any queue. |
194 | /** Answer a message which was not dispatched and is not listed in any queue. |
195 | * |
195 | * |
196 | * @param call Call structure to be answered. |
196 | * @param call Call structure to be answered. |
197 | */ |
197 | */ |
198 | static void _ipc_answer_free_call(call_t *call) |
198 | static void _ipc_answer_free_call(call_t *call) |
199 | { |
199 | { |
200 | answerbox_t *callerbox = call->callerbox; |
200 | answerbox_t *callerbox = call->callerbox; |
201 | 201 | ||
202 | call->flags |= IPC_CALL_ANSWERED; |
202 | call->flags |= IPC_CALL_ANSWERED; |
203 | 203 | ||
204 | if (call->flags & IPC_CALL_FORWARDED) { |
204 | if (call->flags & IPC_CALL_FORWARDED) { |
205 | if (call->caller_phone) { |
205 | if (call->caller_phone) { |
206 | /* Demasquerade the caller phone. */ |
206 | /* Demasquerade the caller phone. */ |
207 | call->data.phone = call->caller_phone; |
207 | call->data.phone = call->caller_phone; |
208 | } |
208 | } |
209 | } |
209 | } |
210 | 210 | ||
211 | spinlock_lock(&callerbox->lock); |
211 | spinlock_lock(&callerbox->lock); |
212 | list_append(&call->link, &callerbox->answers); |
212 | list_append(&call->link, &callerbox->answers); |
213 | spinlock_unlock(&callerbox->lock); |
213 | spinlock_unlock(&callerbox->lock); |
214 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST); |
214 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST); |
215 | } |
215 | } |
216 | 216 | ||
217 | /** Answer a message which is in a callee queue. |
217 | /** Answer a message which is in a callee queue. |
218 | * |
218 | * |
219 | * @param box Answerbox that is answering the message. |
219 | * @param box Answerbox that is answering the message. |
220 | * @param call Modified request that is being sent back. |
220 | * @param call Modified request that is being sent back. |
221 | */ |
221 | */ |
222 | void ipc_answer(answerbox_t *box, call_t *call) |
222 | void ipc_answer(answerbox_t *box, call_t *call) |
223 | { |
223 | { |
224 | /* Remove from active box */ |
224 | /* Remove from active box */ |
225 | spinlock_lock(&box->lock); |
225 | spinlock_lock(&box->lock); |
226 | list_remove(&call->link); |
226 | list_remove(&call->link); |
227 | spinlock_unlock(&box->lock); |
227 | spinlock_unlock(&box->lock); |
228 | /* Send back answer */ |
228 | /* Send back answer */ |
229 | _ipc_answer_free_call(call); |
229 | _ipc_answer_free_call(call); |
230 | } |
230 | } |
231 | 231 | ||
232 | /** Simulate sending back a message. |
232 | /** Simulate sending back a message. |
233 | * |
233 | * |
234 | * Most errors are better handled by forming a normal backward |
234 | * Most errors are better handled by forming a normal backward |
235 | * message and sending it as a normal answer. |
235 | * message and sending it as a normal answer. |
236 | * |
236 | * |
237 | * @param phone Phone structure the call should appear to come from. |
237 | * @param phone Phone structure the call should appear to come from. |
238 | * @param call Call structure to be answered. |
238 | * @param call Call structure to be answered. |
239 | * @param err Return value to be used for the answer. |
239 | * @param err Return value to be used for the answer. |
240 | */ |
240 | */ |
241 | void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err) |
241 | void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err) |
242 | { |
242 | { |
243 | call->data.phone = phone; |
243 | call->data.phone = phone; |
244 | atomic_inc(&phone->active_calls); |
244 | atomic_inc(&phone->active_calls); |
245 | IPC_SET_RETVAL(call->data, err); |
245 | IPC_SET_RETVAL(call->data, err); |
246 | _ipc_answer_free_call(call); |
246 | _ipc_answer_free_call(call); |
247 | } |
247 | } |
248 | 248 | ||
249 | /** Unsafe unchecking version of ipc_call. |
249 | /** Unsafe unchecking version of ipc_call. |
250 | * |
250 | * |
251 | * @param phone Phone structure the call comes from. |
251 | * @param phone Phone structure the call comes from. |
252 | * @param box Destination answerbox structure. |
252 | * @param box Destination answerbox structure. |
253 | * @param call Call structure with request. |
253 | * @param call Call structure with request. |
254 | */ |
254 | */ |
255 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call) |
255 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call) |
256 | { |
256 | { |
257 | if (!(call->flags & IPC_CALL_FORWARDED)) { |
257 | if (!(call->flags & IPC_CALL_FORWARDED)) { |
258 | atomic_inc(&phone->active_calls); |
258 | atomic_inc(&phone->active_calls); |
259 | call->data.phone = phone; |
259 | call->data.phone = phone; |
260 | } |
260 | } |
261 | 261 | ||
262 | spinlock_lock(&box->lock); |
262 | spinlock_lock(&box->lock); |
263 | list_append(&call->link, &box->calls); |
263 | list_append(&call->link, &box->calls); |
264 | spinlock_unlock(&box->lock); |
264 | spinlock_unlock(&box->lock); |
265 | waitq_wakeup(&box->wq, WAKEUP_FIRST); |
265 | waitq_wakeup(&box->wq, WAKEUP_FIRST); |
266 | } |
266 | } |
267 | 267 | ||
268 | /** Send an asynchronous request using a phone to an answerbox. |
268 | /** Send an asynchronous request using a phone to an answerbox. |
269 | * |
269 | * |
270 | * @param phone Phone structure the call comes from and which is |
270 | * @param phone Phone structure the call comes from and which is |
271 | * connected to the destination answerbox. |
271 | * connected to the destination answerbox. |
272 | * @param call Call structure with request. |
272 | * @param call Call structure with request. |
273 | * |
273 | * |
274 | * @return Return 0 on success, ENOENT on error. |
274 | * @return Return 0 on success, ENOENT on error. |
275 | */ |
275 | */ |
276 | int ipc_call(phone_t *phone, call_t *call) |
276 | int ipc_call(phone_t *phone, call_t *call) |
277 | { |
277 | { |
278 | answerbox_t *box; |
278 | answerbox_t *box; |
279 | 279 | ||
280 | mutex_lock(&phone->lock); |
280 | mutex_lock(&phone->lock); |
281 | if (phone->state != IPC_PHONE_CONNECTED) { |
281 | if (phone->state != IPC_PHONE_CONNECTED) { |
282 | mutex_unlock(&phone->lock); |
282 | mutex_unlock(&phone->lock); |
283 | if (call->flags & IPC_CALL_FORWARDED) { |
283 | if (call->flags & IPC_CALL_FORWARDED) { |
284 | IPC_SET_RETVAL(call->data, EFORWARD); |
284 | IPC_SET_RETVAL(call->data, EFORWARD); |
285 | _ipc_answer_free_call(call); |
285 | _ipc_answer_free_call(call); |
286 | } else { |
286 | } else { |
287 | if (phone->state == IPC_PHONE_HUNGUP) |
287 | if (phone->state == IPC_PHONE_HUNGUP) |
288 | ipc_backsend_err(phone, call, EHANGUP); |
288 | ipc_backsend_err(phone, call, EHANGUP); |
289 | else |
289 | else |
290 | ipc_backsend_err(phone, call, ENOENT); |
290 | ipc_backsend_err(phone, call, ENOENT); |
291 | } |
291 | } |
292 | return ENOENT; |
292 | return ENOENT; |
293 | } |
293 | } |
294 | box = phone->callee; |
294 | box = phone->callee; |
295 | _ipc_call(phone, box, call); |
295 | _ipc_call(phone, box, call); |
296 | 296 | ||
297 | mutex_unlock(&phone->lock); |
297 | mutex_unlock(&phone->lock); |
298 | return 0; |
298 | return 0; |
299 | } |
299 | } |
300 | 300 | ||
301 | /** Disconnect phone from answerbox. |
301 | /** Disconnect phone from answerbox. |
302 | * |
302 | * |
303 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done |
303 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done |
304 | * lazily later. |
304 | * lazily later. |
305 | * |
305 | * |
306 | * @param phone Phone structure to be hung up. |
306 | * @param phone Phone structure to be hung up. |
307 | * |
307 | * |
308 | * @return Return 0 if the phone is disconnected. |
308 | * @return Return 0 if the phone is disconnected. |
309 | * Return -1 if the phone was already disconnected. |
309 | * Return -1 if the phone was already disconnected. |
310 | */ |
310 | */ |
311 | int ipc_phone_hangup(phone_t *phone) |
311 | int ipc_phone_hangup(phone_t *phone) |
312 | { |
312 | { |
313 | answerbox_t *box; |
313 | answerbox_t *box; |
314 | call_t *call; |
314 | call_t *call; |
315 | 315 | ||
316 | mutex_lock(&phone->lock); |
316 | mutex_lock(&phone->lock); |
317 | if (phone->state == IPC_PHONE_FREE || |
317 | if (phone->state == IPC_PHONE_FREE || |
318 | phone->state == IPC_PHONE_HUNGUP || |
318 | phone->state == IPC_PHONE_HUNGUP || |
319 | phone->state == IPC_PHONE_CONNECTING) { |
319 | phone->state == IPC_PHONE_CONNECTING) { |
320 | mutex_unlock(&phone->lock); |
320 | mutex_unlock(&phone->lock); |
321 | return -1; |
321 | return -1; |
322 | } |
322 | } |
323 | box = phone->callee; |
323 | box = phone->callee; |
324 | if (phone->state != IPC_PHONE_SLAMMED) { |
324 | if (phone->state != IPC_PHONE_SLAMMED) { |
325 | /* Remove myself from answerbox */ |
325 | /* Remove myself from answerbox */ |
326 | spinlock_lock(&box->lock); |
326 | spinlock_lock(&box->lock); |
327 | list_remove(&phone->link); |
327 | list_remove(&phone->link); |
328 | spinlock_unlock(&box->lock); |
328 | spinlock_unlock(&box->lock); |
329 | 329 | ||
330 | if (phone->state != IPC_PHONE_SLAMMED) { |
330 | if (phone->state != IPC_PHONE_SLAMMED) { |
331 | call = ipc_call_alloc(0); |
331 | call = ipc_call_alloc(0); |
332 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
332 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
333 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
333 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
334 | _ipc_call(phone, box, call); |
334 | _ipc_call(phone, box, call); |
335 | } |
335 | } |
336 | } |
336 | } |
337 | 337 | ||
338 | phone->state = IPC_PHONE_HUNGUP; |
338 | phone->state = IPC_PHONE_HUNGUP; |
339 | mutex_unlock(&phone->lock); |
339 | mutex_unlock(&phone->lock); |
340 | 340 | ||
341 | return 0; |
341 | return 0; |
342 | } |
342 | } |
343 | 343 | ||
344 | /** Forwards call from one answerbox to another one. |
344 | /** Forwards call from one answerbox to another one. |
345 | * |
345 | * |
346 | * @param call Call structure to be redirected. |
346 | * @param call Call structure to be redirected. |
347 | * @param newphone Phone structure to target answerbox. |
347 | * @param newphone Phone structure to target answerbox. |
348 | * @param oldbox Old answerbox structure. |
348 | * @param oldbox Old answerbox structure. |
349 | * @param mode Flags that specify mode of the forward operation. |
349 | * @param mode Flags that specify mode of the forward operation. |
350 | * |
350 | * |
351 | * @return Return 0 if forwarding succeeded or an error code if |
351 | * @return Return 0 if forwarding succeeded or an error code if |
352 | * there was error. |
352 | * there was error. |
353 | * |
353 | * |
354 | * The return value serves only as an information for the forwarder, |
354 | * The return value serves only as an information for the forwarder, |
355 | * the original caller is notified automatically with EFORWARD. |
355 | * the original caller is notified automatically with EFORWARD. |
356 | */ |
356 | */ |
357 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode) |
357 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode) |
358 | { |
358 | { |
359 | spinlock_lock(&oldbox->lock); |
359 | spinlock_lock(&oldbox->lock); |
360 | list_remove(&call->link); |
360 | list_remove(&call->link); |
361 | spinlock_unlock(&oldbox->lock); |
361 | spinlock_unlock(&oldbox->lock); |
362 | 362 | ||
363 | if (mode & IPC_FF_ROUTE_FROM_ME) { |
363 | if (mode & IPC_FF_ROUTE_FROM_ME) { |
364 | if (!call->caller_phone) |
364 | if (!call->caller_phone) |
365 | call->caller_phone = call->data.phone; |
365 | call->caller_phone = call->data.phone; |
366 | call->data.phone = newphone; |
366 | call->data.phone = newphone; |
367 | } |
367 | } |
368 | 368 | ||
369 | return ipc_call(newphone, call); |
369 | return ipc_call(newphone, call); |
370 | } |
370 | } |
371 | 371 | ||
372 | 372 | ||
373 | /** Wait for a phone call. |
373 | /** Wait for a phone call. |
374 | * |
374 | * |
375 | * @param box Answerbox expecting the call. |
375 | * @param box Answerbox expecting the call. |
376 | * @param usec Timeout in microseconds. See documentation for |
376 | * @param usec Timeout in microseconds. See documentation for |
377 | * waitq_sleep_timeout() for decription of its special |
377 | * waitq_sleep_timeout() for decription of its special |
378 | * meaning. |
378 | * meaning. |
379 | * @param flags Select mode of sleep operation. See documentation for |
379 | * @param flags Select mode of sleep operation. See documentation for |
380 | * waitq_sleep_timeout() for description of its special |
380 | * waitq_sleep_timeout() for description of its special |
381 | * meaning. |
381 | * meaning. |
382 | * @return Recived call structure or NULL. |
382 | * @return Recived call structure or NULL. |
383 | * |
383 | * |
384 | * To distinguish between a call and an answer, have a look at call->flags. |
384 | * To distinguish between a call and an answer, have a look at call->flags. |
385 | */ |
385 | */ |
386 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags) |
386 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags) |
387 | { |
387 | { |
388 | call_t *request; |
388 | call_t *request; |
389 | ipl_t ipl; |
389 | ipl_t ipl; |
390 | int rc; |
390 | int rc; |
391 | 391 | ||
392 | restart: |
392 | restart: |
393 | rc = waitq_sleep_timeout(&box->wq, usec, flags); |
393 | rc = waitq_sleep_timeout(&box->wq, usec, flags); |
394 | if (SYNCH_FAILED(rc)) |
394 | if (SYNCH_FAILED(rc)) |
395 | return NULL; |
395 | return NULL; |
396 | 396 | ||
397 | spinlock_lock(&box->lock); |
397 | spinlock_lock(&box->lock); |
398 | if (!list_empty(&box->irq_notifs)) { |
398 | if (!list_empty(&box->irq_notifs)) { |
399 | ipl = interrupts_disable(); |
399 | ipl = interrupts_disable(); |
400 | spinlock_lock(&box->irq_lock); |
400 | spinlock_lock(&box->irq_lock); |
401 | 401 | ||
402 | request = list_get_instance(box->irq_notifs.next, call_t, link); |
402 | request = list_get_instance(box->irq_notifs.next, call_t, link); |
403 | list_remove(&request->link); |
403 | list_remove(&request->link); |
404 | 404 | ||
405 | spinlock_unlock(&box->irq_lock); |
405 | spinlock_unlock(&box->irq_lock); |
406 | interrupts_restore(ipl); |
406 | interrupts_restore(ipl); |
407 | } else if (!list_empty(&box->answers)) { |
407 | } else if (!list_empty(&box->answers)) { |
408 | /* Handle asynchronous answers */ |
408 | /* Handle asynchronous answers */ |
409 | request = list_get_instance(box->answers.next, call_t, link); |
409 | request = list_get_instance(box->answers.next, call_t, link); |
410 | list_remove(&request->link); |
410 | list_remove(&request->link); |
411 | atomic_dec(&request->data.phone->active_calls); |
411 | atomic_dec(&request->data.phone->active_calls); |
412 | } else if (!list_empty(&box->calls)) { |
412 | } else if (!list_empty(&box->calls)) { |
413 | /* Handle requests */ |
413 | /* Handle requests */ |
414 | request = list_get_instance(box->calls.next, call_t, link); |
414 | request = list_get_instance(box->calls.next, call_t, link); |
415 | list_remove(&request->link); |
415 | list_remove(&request->link); |
416 | /* Append request to dispatch queue */ |
416 | /* Append request to dispatch queue */ |
417 | list_append(&request->link, &box->dispatched_calls); |
417 | list_append(&request->link, &box->dispatched_calls); |
418 | } else { |
418 | } else { |
419 | /* This can happen regularly after ipc_cleanup */ |
419 | /* This can happen regularly after ipc_cleanup */ |
420 | spinlock_unlock(&box->lock); |
420 | spinlock_unlock(&box->lock); |
421 | goto restart; |
421 | goto restart; |
422 | } |
422 | } |
423 | spinlock_unlock(&box->lock); |
423 | spinlock_unlock(&box->lock); |
424 | return request; |
424 | return request; |
425 | } |
425 | } |
426 | 426 | ||
427 | /** Answer all calls from list with EHANGUP answer. |
427 | /** Answer all calls from list with EHANGUP answer. |
428 | * |
428 | * |
429 | * @param lst Head of the list to be cleaned up. |
429 | * @param lst Head of the list to be cleaned up. |
430 | */ |
430 | */ |
431 | static void ipc_cleanup_call_list(link_t *lst) |
431 | static void ipc_cleanup_call_list(link_t *lst) |
432 | { |
432 | { |
433 | call_t *call; |
433 | call_t *call; |
434 | 434 | ||
435 | while (!list_empty(lst)) { |
435 | while (!list_empty(lst)) { |
436 | call = list_get_instance(lst->next, call_t, link); |
436 | call = list_get_instance(lst->next, call_t, link); |
437 | if (call->buffer) |
437 | if (call->buffer) |
438 | free(call->buffer); |
438 | free(call->buffer); |
439 | list_remove(&call->link); |
439 | list_remove(&call->link); |
440 | 440 | ||
441 | IPC_SET_RETVAL(call->data, EHANGUP); |
441 | IPC_SET_RETVAL(call->data, EHANGUP); |
442 | _ipc_answer_free_call(call); |
442 | _ipc_answer_free_call(call); |
443 | } |
443 | } |
444 | } |
444 | } |
445 | 445 | ||
446 | /** Disconnects all phones connected to an answerbox. |
446 | /** Disconnects all phones connected to an answerbox. |
447 | * |
447 | * |
448 | * @param box Answerbox to disconnect phones from. |
448 | * @param box Answerbox to disconnect phones from. |
449 | * @param notify_box If true, the answerbox will get a hangup message for |
449 | * @param notify_box If true, the answerbox will get a hangup message for |
450 | * each disconnected phone. |
450 | * each disconnected phone. |
451 | */ |
451 | */ |
452 | static void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box) |
452 | static void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box) |
453 | { |
453 | { |
454 | phone_t *phone; |
454 | phone_t *phone; |
455 | DEADLOCK_PROBE_INIT(p_phonelck); |
455 | DEADLOCK_PROBE_INIT(p_phonelck); |
456 | ipl_t ipl; |
456 | ipl_t ipl; |
457 | call_t *call; |
457 | call_t *call; |
458 | 458 | ||
459 | call = ipc_call_alloc(0); |
459 | call = notify_box ? ipc_call_alloc(0) : NULL; |
460 | 460 | ||
461 | /* Disconnect all phones connected to our answerbox */ |
461 | /* Disconnect all phones connected to our answerbox */ |
462 | restart_phones: |
462 | restart_phones: |
463 | ipl = interrupts_disable(); |
463 | ipl = interrupts_disable(); |
464 | spinlock_lock(&box->lock); |
464 | spinlock_lock(&box->lock); |
465 | while (!list_empty(&box->connected_phones)) { |
465 | while (!list_empty(&box->connected_phones)) { |
466 | phone = list_get_instance(box->connected_phones.next, |
466 | phone = list_get_instance(box->connected_phones.next, |
467 | phone_t, link); |
467 | phone_t, link); |
468 | if (SYNCH_FAILED(mutex_trylock(&phone->lock))) { |
468 | if (SYNCH_FAILED(mutex_trylock(&phone->lock))) { |
469 | spinlock_unlock(&box->lock); |
469 | spinlock_unlock(&box->lock); |
470 | interrupts_restore(ipl); |
470 | interrupts_restore(ipl); |
471 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD); |
471 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD); |
472 | goto restart_phones; |
472 | goto restart_phones; |
473 | } |
473 | } |
474 | 474 | ||
475 | /* Disconnect phone */ |
475 | /* Disconnect phone */ |
476 | ASSERT(phone->state == IPC_PHONE_CONNECTED); |
476 | ASSERT(phone->state == IPC_PHONE_CONNECTED); |
477 | 477 | ||
478 | list_remove(&phone->link); |
478 | list_remove(&phone->link); |
479 | phone->state = IPC_PHONE_SLAMMED; |
479 | phone->state = IPC_PHONE_SLAMMED; |
480 | 480 | ||
481 | if (notify_box) { |
481 | if (notify_box) { |
482 | mutex_unlock(&phone->lock); |
482 | mutex_unlock(&phone->lock); |
483 | spinlock_unlock(&box->lock); |
483 | spinlock_unlock(&box->lock); |
484 | interrupts_restore(ipl); |
484 | interrupts_restore(ipl); |
485 | 485 | ||
486 | /* |
486 | /* |
487 | * Send one message to the answerbox for each |
487 | * Send one message to the answerbox for each |
488 | * phone. Used to make sure the kbox thread |
488 | * phone. Used to make sure the kbox thread |
489 | * wakes up after the last phone has been |
489 | * wakes up after the last phone has been |
490 | * disconnected. |
490 | * disconnected. |
491 | */ |
491 | */ |
492 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
492 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
493 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
493 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
494 | _ipc_call(phone, box, call); |
494 | _ipc_call(phone, box, call); |
495 | 495 | ||
496 | /* Allocate another call in advance */ |
496 | /* Allocate another call in advance */ |
497 | call = ipc_call_alloc(0); |
497 | call = ipc_call_alloc(0); |
498 | 498 | ||
499 | /* Must start again */ |
499 | /* Must start again */ |
500 | goto restart_phones; |
500 | goto restart_phones; |
501 | } |
501 | } |
502 | 502 | ||
503 | mutex_unlock(&phone->lock); |
503 | mutex_unlock(&phone->lock); |
504 | } |
504 | } |
505 | 505 | ||
506 | spinlock_unlock(&box->lock); |
506 | spinlock_unlock(&box->lock); |
507 | interrupts_restore(ipl); |
507 | interrupts_restore(ipl); |
508 | 508 | ||
509 | /* Free unused call */ |
509 | /* Free unused call */ |
510 | if (call) ipc_call_free(call); |
510 | if (call) ipc_call_free(call); |
511 | } |
511 | } |
512 | 512 | ||
- | 513 | #ifdef CONFIG_UDEBUG |
|
- | 514 | ||
513 | static void ipc_kbox_cleanup() |
515 | static void ipc_kbox_cleanup() |
514 | { |
516 | { |
515 | bool have_kb_thread; |
517 | bool have_kb_thread; |
516 | 518 | ||
517 | /* Only hold kb_cleanup_lock while setting kb_finished - this is enough */ |
519 | /* Only hold kb_cleanup_lock while setting kb_finished - this is enough */ |
518 | mutex_lock(&TASK->kb_cleanup_lock); |
520 | mutex_lock(&TASK->kb_cleanup_lock); |
519 | TASK->kb_finished = true; |
521 | TASK->kb_finished = true; |
520 | mutex_unlock(&TASK->kb_cleanup_lock); |
522 | mutex_unlock(&TASK->kb_cleanup_lock); |
521 | 523 | ||
522 | have_kb_thread = (TASK->kb_thread != NULL); |
524 | have_kb_thread = (TASK->kb_thread != NULL); |
523 | 525 | ||
524 | /* From now on nobody will try to connect phones or attach kbox threads */ |
526 | /* From now on nobody will try to connect phones or attach kbox threads */ |
525 | 527 | ||
526 | /* |
528 | /* |
527 | * Disconnect all phones connected to our kbox. Passing true for |
529 | * Disconnect all phones connected to our kbox. Passing true for |
528 | * notify_box causes a HANGUP message to be inserted for each |
530 | * notify_box causes a HANGUP message to be inserted for each |
529 | * disconnected phone. This ensures the kbox thread is going to |
531 | * disconnected phone. This ensures the kbox thread is going to |
530 | * wake up and terminate. |
532 | * wake up and terminate. |
531 | */ |
533 | */ |
532 | ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread); |
534 | ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread); |
533 | 535 | ||
534 | if (have_kb_thread) { |
536 | if (have_kb_thread) { |
535 | printf("join kb_thread..\n"); |
537 | printf("join kb_thread..\n"); |
536 | thread_join(TASK->kb_thread); |
538 | thread_join(TASK->kb_thread); |
537 | thread_detach(TASK->kb_thread); |
539 | thread_detach(TASK->kb_thread); |
538 | printf("join done\n"); |
540 | printf("join done\n"); |
539 | TASK->kb_thread = NULL; |
541 | TASK->kb_thread = NULL; |
540 | } |
542 | } |
541 | 543 | ||
542 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
544 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
543 | spinlock_lock(&TASK->kernel_box.lock); |
545 | spinlock_lock(&TASK->kernel_box.lock); |
544 | ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls); |
546 | ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls); |
545 | ipc_cleanup_call_list(&TASK->kernel_box.calls); |
547 | ipc_cleanup_call_list(&TASK->kernel_box.calls); |
546 | spinlock_unlock(&TASK->kernel_box.lock); |
548 | spinlock_unlock(&TASK->kernel_box.lock); |
547 | } |
549 | } |
548 | 550 | ||
- | 551 | #endif |
|
549 | 552 | ||
550 | /** Cleans up all IPC communication of the current task. |
553 | /** Cleans up all IPC communication of the current task. |
551 | * |
554 | * |
552 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you |
555 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you |
553 | * have to change it as well if you want to cleanup other tasks than TASK. |
556 | * have to change it as well if you want to cleanup other tasks than TASK. |
554 | */ |
557 | */ |
555 | void ipc_cleanup(void) |
558 | void ipc_cleanup(void) |
556 | { |
559 | { |
557 | int i; |
560 | int i; |
558 | call_t *call; |
561 | call_t *call; |
559 | 562 | ||
560 | /* Disconnect all our phones ('ipc_phone_hangup') */ |
563 | /* Disconnect all our phones ('ipc_phone_hangup') */ |
561 | for (i = 0; i < IPC_MAX_PHONES; i++) |
564 | for (i = 0; i < IPC_MAX_PHONES; i++) |
562 | ipc_phone_hangup(&TASK->phones[i]); |
565 | ipc_phone_hangup(&TASK->phones[i]); |
563 | 566 | ||
564 | /* Disconnect all connected irqs */ |
567 | /* Disconnect all connected irqs */ |
565 | ipc_irq_cleanup(&TASK->answerbox); |
568 | ipc_irq_cleanup(&TASK->answerbox); |
566 | 569 | ||
567 | /* Disconnect all phones connected to our regular answerbox */ |
570 | /* Disconnect all phones connected to our regular answerbox */ |
568 | ipc_answerbox_slam_phones(&TASK->answerbox, false); |
571 | ipc_answerbox_slam_phones(&TASK->answerbox, false); |
569 | 572 | ||
- | 573 | #ifdef CONFIG_UDEBUG |
|
570 | /* Clean up kbox thread and communications */ |
574 | /* Clean up kbox thread and communications */ |
571 | ipc_kbox_cleanup(); |
575 | ipc_kbox_cleanup(); |
- | 576 | #endif |
|
572 | 577 | ||
573 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
578 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
574 | spinlock_lock(&TASK->answerbox.lock); |
579 | spinlock_lock(&TASK->answerbox.lock); |
575 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls); |
580 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls); |
576 | ipc_cleanup_call_list(&TASK->answerbox.calls); |
581 | ipc_cleanup_call_list(&TASK->answerbox.calls); |
577 | spinlock_unlock(&TASK->answerbox.lock); |
582 | spinlock_unlock(&TASK->answerbox.lock); |
578 | 583 | ||
579 | /* Wait for all async answers to arrive */ |
584 | /* Wait for all async answers to arrive */ |
580 | while (1) { |
585 | while (1) { |
581 | /* Go through all phones, until all are FREE... */ |
586 | /* Go through all phones, until all are FREE... */ |
582 | /* Locking not needed, no one else should modify |
587 | /* Locking not needed, no one else should modify |
583 | * it, when we are in cleanup */ |
588 | * it, when we are in cleanup */ |
584 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
589 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
585 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP && |
590 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP && |
586 | atomic_get(&TASK->phones[i].active_calls) == 0) |
591 | atomic_get(&TASK->phones[i].active_calls) == 0) |
587 | TASK->phones[i].state = IPC_PHONE_FREE; |
592 | TASK->phones[i].state = IPC_PHONE_FREE; |
588 | 593 | ||
589 | /* Just for sure, we might have had some |
594 | /* Just for sure, we might have had some |
590 | * IPC_PHONE_CONNECTING phones */ |
595 | * IPC_PHONE_CONNECTING phones */ |
591 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED) |
596 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED) |
592 | ipc_phone_hangup(&TASK->phones[i]); |
597 | ipc_phone_hangup(&TASK->phones[i]); |
593 | /* If the hangup succeeded, it has sent a HANGUP |
598 | /* If the hangup succeeded, it has sent a HANGUP |
594 | * message, the IPC is now in HUNGUP state, we |
599 | * message, the IPC is now in HUNGUP state, we |
595 | * wait for the reply to come */ |
600 | * wait for the reply to come */ |
596 | 601 | ||
597 | if (TASK->phones[i].state != IPC_PHONE_FREE) |
602 | if (TASK->phones[i].state != IPC_PHONE_FREE) |
598 | break; |
603 | break; |
599 | } |
604 | } |
600 | /* Voila, got into cleanup */ |
605 | /* Voila, got into cleanup */ |
601 | if (i == IPC_MAX_PHONES) |
606 | if (i == IPC_MAX_PHONES) |
602 | break; |
607 | break; |
603 | 608 | ||
604 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT, |
609 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT, |
605 | SYNCH_FLAGS_NONE); |
610 | SYNCH_FLAGS_NONE); |
606 | ASSERT((call->flags & IPC_CALL_ANSWERED) || |
611 | ASSERT((call->flags & IPC_CALL_ANSWERED) || |
607 | (call->flags & IPC_CALL_NOTIF)); |
612 | (call->flags & IPC_CALL_NOTIF)); |
608 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
613 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
609 | 614 | ||
610 | atomic_dec(&TASK->active_calls); |
615 | atomic_dec(&TASK->active_calls); |
611 | ipc_call_free(call); |
616 | ipc_call_free(call); |
612 | } |
617 | } |
613 | } |
618 | } |
614 | 619 | ||
615 | 620 | ||
616 | /** Initilize IPC subsystem */ |
621 | /** Initilize IPC subsystem */ |
617 | void ipc_init(void) |
622 | void ipc_init(void) |
618 | { |
623 | { |
619 | ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL, |
624 | ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL, |
620 | NULL, 0); |
625 | NULL, 0); |
621 | } |
626 | } |
622 | 627 | ||
623 | 628 | ||
624 | /** List answerbox contents. |
629 | /** List answerbox contents. |
625 | * |
630 | * |
626 | * @param taskid Task ID. |
631 | * @param taskid Task ID. |
627 | */ |
632 | */ |
628 | void ipc_print_task(task_id_t taskid) |
633 | void ipc_print_task(task_id_t taskid) |
629 | { |
634 | { |
630 | task_t *task; |
635 | task_t *task; |
631 | int i; |
636 | int i; |
632 | call_t *call; |
637 | call_t *call; |
633 | link_t *tmp; |
638 | link_t *tmp; |
634 | 639 | ||
635 | spinlock_lock(&tasks_lock); |
640 | spinlock_lock(&tasks_lock); |
636 | task = task_find_by_id(taskid); |
641 | task = task_find_by_id(taskid); |
637 | if (task) |
642 | if (task) |
638 | spinlock_lock(&task->lock); |
643 | spinlock_lock(&task->lock); |
639 | spinlock_unlock(&tasks_lock); |
644 | spinlock_unlock(&tasks_lock); |
640 | if (!task) |
645 | if (!task) |
641 | return; |
646 | return; |
642 | 647 | ||
643 | /* Print opened phones & details */ |
648 | /* Print opened phones & details */ |
644 | printf("PHONE:\n"); |
649 | printf("PHONE:\n"); |
645 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
650 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
646 | if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) { |
651 | if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) { |
647 | printf("%d: mutex busy\n", i); |
652 | printf("%d: mutex busy\n", i); |
648 | continue; |
653 | continue; |
649 | } |
654 | } |
650 | if (task->phones[i].state != IPC_PHONE_FREE) { |
655 | if (task->phones[i].state != IPC_PHONE_FREE) { |
651 | printf("%d: ", i); |
656 | printf("%d: ", i); |
652 | switch (task->phones[i].state) { |
657 | switch (task->phones[i].state) { |
653 | case IPC_PHONE_CONNECTING: |
658 | case IPC_PHONE_CONNECTING: |
654 | printf("connecting "); |
659 | printf("connecting "); |
655 | break; |
660 | break; |
656 | case IPC_PHONE_CONNECTED: |
661 | case IPC_PHONE_CONNECTED: |
657 | printf("connected to: %p ", |
662 | printf("connected to: %p ", |
658 | task->phones[i].callee); |
663 | task->phones[i].callee); |
659 | break; |
664 | break; |
660 | case IPC_PHONE_SLAMMED: |
665 | case IPC_PHONE_SLAMMED: |
661 | printf("slammed by: %p ", |
666 | printf("slammed by: %p ", |
662 | task->phones[i].callee); |
667 | task->phones[i].callee); |
663 | break; |
668 | break; |
664 | case IPC_PHONE_HUNGUP: |
669 | case IPC_PHONE_HUNGUP: |
665 | printf("hung up - was: %p ", |
670 | printf("hung up - was: %p ", |
666 | task->phones[i].callee); |
671 | task->phones[i].callee); |
667 | break; |
672 | break; |
668 | default: |
673 | default: |
669 | break; |
674 | break; |
670 | } |
675 | } |
671 | printf("active: %ld\n", |
676 | printf("active: %ld\n", |
672 | atomic_get(&task->phones[i].active_calls)); |
677 | atomic_get(&task->phones[i].active_calls)); |
673 | } |
678 | } |
674 | mutex_unlock(&task->phones[i].lock); |
679 | mutex_unlock(&task->phones[i].lock); |
675 | } |
680 | } |
676 | 681 | ||
677 | 682 | ||
678 | /* Print answerbox - calls */ |
683 | /* Print answerbox - calls */ |
679 | spinlock_lock(&task->answerbox.lock); |
684 | spinlock_lock(&task->answerbox.lock); |
680 | printf("ABOX - CALLS:\n"); |
685 | printf("ABOX - CALLS:\n"); |
681 | for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls; |
686 | for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls; |
682 | tmp = tmp->next) { |
687 | tmp = tmp->next) { |
683 | call = list_get_instance(tmp, call_t, link); |
688 | call = list_get_instance(tmp, call_t, link); |
684 | printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun |
689 | printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun |
685 | " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun |
690 | " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun |
686 | " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call, |
691 | " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call, |
687 | call->sender->taskid, |
692 | call->sender->taskid, |
688 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
693 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
689 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
694 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
690 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
695 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
691 | call->flags); |
696 | call->flags); |
692 | } |
697 | } |
693 | /* Print answerbox - calls */ |
698 | /* Print answerbox - calls */ |
694 | printf("ABOX - DISPATCHED CALLS:\n"); |
699 | printf("ABOX - DISPATCHED CALLS:\n"); |
695 | for (tmp = task->answerbox.dispatched_calls.next; |
700 | for (tmp = task->answerbox.dispatched_calls.next; |
696 | tmp != &task->answerbox.dispatched_calls; |
701 | tmp != &task->answerbox.dispatched_calls; |
697 | tmp = tmp->next) { |
702 | tmp = tmp->next) { |
698 | call = list_get_instance(tmp, call_t, link); |
703 | call = list_get_instance(tmp, call_t, link); |
699 | printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun |
704 | printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun |
700 | " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun |
705 | " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun |
701 | " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call, |
706 | " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call, |
702 | call->sender->taskid, |
707 | call->sender->taskid, |
703 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
708 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
704 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
709 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
705 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
710 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
706 | call->flags); |
711 | call->flags); |
707 | } |
712 | } |
708 | /* Print answerbox - calls */ |
713 | /* Print answerbox - calls */ |
709 | printf("ABOX - ANSWERS:\n"); |
714 | printf("ABOX - ANSWERS:\n"); |
710 | for (tmp = task->answerbox.answers.next; |
715 | for (tmp = task->answerbox.answers.next; |
711 | tmp != &task->answerbox.answers; |
716 | tmp != &task->answerbox.answers; |
712 | tmp = tmp->next) { |
717 | tmp = tmp->next) { |
713 | call = list_get_instance(tmp, call_t, link); |
718 | call = list_get_instance(tmp, call_t, link); |
714 | printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun |
719 | printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun |
715 | " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", |
720 | " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", |
716 | call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
721 | call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
717 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
722 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
718 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
723 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
719 | call->flags); |
724 | call->flags); |
720 | } |
725 | } |
721 | 726 | ||
722 | spinlock_unlock(&task->answerbox.lock); |
727 | spinlock_unlock(&task->answerbox.lock); |
723 | spinlock_unlock(&task->lock); |
728 | spinlock_unlock(&task->lock); |
724 | } |
729 | } |
725 | 730 | ||
- | 731 | #ifdef CONFIG_UDEBUG |
|
- | 732 | ||
726 | #include <ipc/ipcrsc.h> |
733 | #include <ipc/ipcrsc.h> |
727 | #include <print.h> |
734 | #include <print.h> |
728 | #include <udebug/udebug_ipc.h> |
735 | #include <udebug/udebug_ipc.h> |
729 | 736 | ||
730 | static void kbox_thread_proc(void *arg) |
737 | static void kbox_thread_proc(void *arg) |
731 | { |
738 | { |
732 | call_t *call; |
739 | call_t *call; |
733 | int method; |
740 | int method; |
734 | bool done; |
741 | bool done; |
735 | ipl_t ipl; |
742 | ipl_t ipl; |
736 | 743 | ||
737 | (void)arg; |
744 | (void)arg; |
738 | printf("kbox_thread_proc()\n"); |
745 | printf("kbox_thread_proc()\n"); |
739 | done = false; |
746 | done = false; |
740 | 747 | ||
741 | while (!done) { |
748 | while (!done) { |
742 | //printf("kbox: wait for call\n"); |
749 | //printf("kbox: wait for call\n"); |
743 | call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT, |
750 | call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT, |
744 | SYNCH_FLAGS_NONE); |
751 | SYNCH_FLAGS_NONE); |
745 | 752 | ||
746 | if (call != NULL) { |
753 | if (call != NULL) { |
747 | method = IPC_GET_METHOD(call->data); |
754 | method = IPC_GET_METHOD(call->data); |
748 | 755 | ||
749 | if (method == IPC_M_DEBUG_ALL) { |
756 | if (method == IPC_M_DEBUG_ALL) { |
750 | udebug_call_receive(call); |
757 | udebug_call_receive(call); |
751 | } |
758 | } |
752 | 759 | ||
753 | if (method == IPC_M_PHONE_HUNGUP) { |
760 | if (method == IPC_M_PHONE_HUNGUP) { |
754 | printf("kbox: handle hangup message\n"); |
761 | printf("kbox: handle hangup message\n"); |
755 | 762 | ||
756 | /* Was it our debugger, who hung up? */ |
763 | /* Was it our debugger, who hung up? */ |
757 | if (call->sender == TASK->udebug.debugger) { |
764 | if (call->sender == TASK->udebug.debugger) { |
758 | /* Terminate debugging session (if any) */ |
765 | /* Terminate debugging session (if any) */ |
759 | printf("kbox: terminate debug session\n"); |
766 | printf("kbox: terminate debug session\n"); |
760 | ipl = interrupts_disable(); |
767 | ipl = interrupts_disable(); |
761 | spinlock_lock(&TASK->lock); |
768 | spinlock_lock(&TASK->lock); |
762 | udebug_task_cleanup(TASK); |
769 | udebug_task_cleanup(TASK); |
763 | spinlock_unlock(&TASK->lock); |
770 | spinlock_unlock(&TASK->lock); |
764 | interrupts_restore(ipl); |
771 | interrupts_restore(ipl); |
765 | } else { |
772 | } else { |
766 | printf("kbox: was not debugger\n"); |
773 | printf("kbox: was not debugger\n"); |
767 | } |
774 | } |
768 | 775 | ||
769 | printf("kbox: continue with hangup message\n"); |
776 | printf("kbox: continue with hangup message\n"); |
770 | IPC_SET_RETVAL(call->data, 0); |
777 | IPC_SET_RETVAL(call->data, 0); |
771 | ipc_answer(&TASK->kernel_box, call); |
778 | ipc_answer(&TASK->kernel_box, call); |
772 | 779 | ||
773 | ipl = interrupts_disable(); |
780 | ipl = interrupts_disable(); |
774 | spinlock_lock(&TASK->lock); |
781 | spinlock_lock(&TASK->lock); |
775 | spinlock_lock(&TASK->answerbox.lock); |
782 | spinlock_lock(&TASK->answerbox.lock); |
776 | if (list_empty(&TASK->answerbox.connected_phones)) { |
783 | if (list_empty(&TASK->answerbox.connected_phones)) { |
777 | /* Last phone has been disconnected */ |
784 | /* Last phone has been disconnected */ |
778 | TASK->kb_thread = NULL; |
785 | TASK->kb_thread = NULL; |
779 | done = true; |
786 | done = true; |
780 | printf("phone list is empty\n"); |
787 | printf("phone list is empty\n"); |
781 | } |
788 | } |
782 | spinlock_unlock(&TASK->answerbox.lock); |
789 | spinlock_unlock(&TASK->answerbox.lock); |
783 | spinlock_unlock(&TASK->lock); |
790 | spinlock_unlock(&TASK->lock); |
784 | interrupts_restore(ipl); |
791 | interrupts_restore(ipl); |
785 | } |
792 | } |
786 | } |
793 | } |
787 | } |
794 | } |
788 | 795 | ||
789 | printf("kbox: finished\n"); |
796 | printf("kbox: finished\n"); |
790 | } |
797 | } |
791 | 798 | ||
792 | 799 | ||
793 | /** |
800 | /** |
794 | * Connect phone to a task kernel-box specified by id. |
801 | * Connect phone to a task kernel-box specified by id. |
795 | * |
802 | * |
796 | * Note that this is not completely atomic. For optimisation reasons, |
803 | * Note that this is not completely atomic. For optimisation reasons, |
797 | * The task might start cleaning up kbox after the phone has been connected |
804 | * The task might start cleaning up kbox after the phone has been connected |
798 | * and before a kbox thread has been created. This must be taken into account |
805 | * and before a kbox thread has been created. This must be taken into account |
799 | * in the cleanup code. |
806 | * in the cleanup code. |
800 | * |
807 | * |
801 | * @return Phone id on success, or negative error code. |
808 | * @return Phone id on success, or negative error code. |
802 | */ |
809 | */ |
803 | int ipc_connect_kbox(task_id_t taskid) |
810 | int ipc_connect_kbox(task_id_t taskid) |
804 | { |
811 | { |
805 | int newphid; |
812 | int newphid; |
806 | task_t *ta; |
813 | task_t *ta; |
807 | thread_t *kb_thread; |
814 | thread_t *kb_thread; |
808 | ipl_t ipl; |
815 | ipl_t ipl; |
809 | 816 | ||
810 | ipl = interrupts_disable(); |
817 | ipl = interrupts_disable(); |
811 | spinlock_lock(&tasks_lock); |
818 | spinlock_lock(&tasks_lock); |
812 | 819 | ||
813 | ta = task_find_by_id(taskid); |
820 | ta = task_find_by_id(taskid); |
814 | if (ta == NULL) { |
821 | if (ta == NULL) { |
815 | spinlock_unlock(&tasks_lock); |
822 | spinlock_unlock(&tasks_lock); |
816 | interrupts_restore(ipl); |
823 | interrupts_restore(ipl); |
817 | return ENOENT; |
824 | return ENOENT; |
818 | } |
825 | } |
819 | 826 | ||
820 | atomic_inc(&ta->refcount); |
827 | atomic_inc(&ta->refcount); |
821 | 828 | ||
822 | spinlock_unlock(&tasks_lock); |
829 | spinlock_unlock(&tasks_lock); |
823 | interrupts_restore(ipl); |
830 | interrupts_restore(ipl); |
824 | 831 | ||
825 | mutex_lock(&ta->kb_cleanup_lock); |
832 | mutex_lock(&ta->kb_cleanup_lock); |
826 | 833 | ||
827 | if (atomic_predec(&ta->refcount) == 0) { |
834 | if (atomic_predec(&ta->refcount) == 0) { |
828 | mutex_unlock(&ta->kb_cleanup_lock); |
835 | mutex_unlock(&ta->kb_cleanup_lock); |
829 | task_destroy(ta); |
836 | task_destroy(ta); |
830 | return ENOENT; |
837 | return ENOENT; |
831 | } |
838 | } |
832 | 839 | ||
833 | if (ta->kb_finished != false) { |
840 | if (ta->kb_finished != false) { |
834 | mutex_unlock(&ta->kb_cleanup_lock); |
841 | mutex_unlock(&ta->kb_cleanup_lock); |
835 | return EINVAL; |
842 | return EINVAL; |
836 | } |
843 | } |
837 | 844 | ||
838 | newphid = phone_alloc(); |
845 | newphid = phone_alloc(); |
839 | if (newphid < 0) { |
846 | if (newphid < 0) { |
840 | mutex_unlock(&ta->kb_cleanup_lock); |
847 | mutex_unlock(&ta->kb_cleanup_lock); |
841 | return ELIMIT; |
848 | return ELIMIT; |
842 | } |
849 | } |
843 | 850 | ||
844 | /* Connect the newly allocated phone to the kbox */ |
851 | /* Connect the newly allocated phone to the kbox */ |
845 | ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box); |
852 | ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box); |
846 | 853 | ||
847 | if (ta->kb_thread != NULL) { |
854 | if (ta->kb_thread != NULL) { |
848 | mutex_unlock(&ta->kb_cleanup_lock); |
855 | mutex_unlock(&ta->kb_cleanup_lock); |
849 | return newphid; |
856 | return newphid; |
850 | } |
857 | } |
851 | 858 | ||
852 | /* Create a kbox thread */ |
859 | /* Create a kbox thread */ |
853 | kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false); |
860 | kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false); |
854 | if (!kb_thread) { |
861 | if (!kb_thread) { |
855 | mutex_unlock(&ta->kb_cleanup_lock); |
862 | mutex_unlock(&ta->kb_cleanup_lock); |
856 | return ENOMEM; |
863 | return ENOMEM; |
857 | } |
864 | } |
858 | 865 | ||
859 | ta->kb_thread = kb_thread; |
866 | ta->kb_thread = kb_thread; |
860 | thread_ready(kb_thread); |
867 | thread_ready(kb_thread); |
861 | 868 | ||
862 | mutex_unlock(&ta->kb_cleanup_lock); |
869 | mutex_unlock(&ta->kb_cleanup_lock); |
863 | 870 | ||
864 | return newphid; |
871 | return newphid; |
865 | } |
872 | } |
866 | 873 | ||
- | 874 | #endif /* defined(CONFIG_UDEBUG) */ |
|
- | 875 | ||
867 | /** @} |
876 | /** @} |
868 | */ |
877 | */ |
869 | 878 |