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