Rev 2840 | Rev 2913 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2840 | Rev 2870 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Ondrej Palkovsky |
2 | * Copyright (c) 2006 Ondrej Palkovsky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericipc |
29 | /** @addtogroup genericipc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | /* Lock ordering |
35 | /* Lock ordering |
36 | * |
36 | * |
37 | * First the answerbox, then the phone. |
37 | * First the answerbox, then the phone. |
38 | */ |
38 | */ |
39 | 39 | ||
40 | #include <synch/spinlock.h> |
40 | #include <synch/spinlock.h> |
41 | #include <synch/waitq.h> |
41 | #include <synch/waitq.h> |
42 | #include <synch/synch.h> |
42 | #include <synch/synch.h> |
43 | #include <ipc/ipc.h> |
43 | #include <ipc/ipc.h> |
44 | #include <errno.h> |
44 | #include <errno.h> |
45 | #include <mm/slab.h> |
45 | #include <mm/slab.h> |
46 | #include <arch.h> |
46 | #include <arch.h> |
47 | #include <proc/task.h> |
47 | #include <proc/task.h> |
48 | #include <memstr.h> |
48 | #include <memstr.h> |
49 | #include <debug.h> |
49 | #include <debug.h> |
50 | 50 | ||
51 | #include <print.h> |
51 | #include <print.h> |
52 | #include <proc/thread.h> |
52 | #include <proc/thread.h> |
53 | #include <arch/interrupt.h> |
53 | #include <arch/interrupt.h> |
54 | #include <ipc/irq.h> |
54 | #include <ipc/irq.h> |
55 | 55 | ||
56 | /** Open channel that is assigned automatically to new tasks */ |
56 | /** Open channel that is assigned automatically to new tasks */ |
57 | answerbox_t *ipc_phone_0 = NULL; |
57 | answerbox_t *ipc_phone_0 = NULL; |
58 | 58 | ||
59 | static slab_cache_t *ipc_call_slab; |
59 | static slab_cache_t *ipc_call_slab; |
60 | 60 | ||
61 | /** Initialize a call structure. |
61 | /** Initialize a call structure. |
62 | * |
62 | * |
63 | * @param call Call structure to be initialized. |
63 | * @param call Call structure to be initialized. |
64 | */ |
64 | */ |
65 | static void _ipc_call_init(call_t *call) |
65 | static void _ipc_call_init(call_t *call) |
66 | { |
66 | { |
67 | memsetb((uintptr_t) call, sizeof(*call), 0); |
67 | memsetb((uintptr_t) call, sizeof(*call), 0); |
68 | call->callerbox = &TASK->answerbox; |
68 | call->callerbox = &TASK->answerbox; |
69 | call->sender = TASK; |
69 | call->sender = TASK; |
70 | call->buffer = NULL; |
70 | call->buffer = NULL; |
71 | } |
71 | } |
72 | 72 | ||
73 | /** Allocate and initialize a call structure. |
73 | /** Allocate and initialize a call structure. |
74 | * |
74 | * |
75 | * The call is initialized, so that the reply will be directed to |
75 | * The call is initialized, so that the reply will be directed to |
76 | * TASK->answerbox. |
76 | * TASK->answerbox. |
77 | * |
77 | * |
78 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC). |
78 | * @param flags Parameters for slab_alloc (e.g FRAME_ATOMIC). |
79 | * |
79 | * |
80 | * @return If flags permit it, return NULL, or initialized kernel |
80 | * @return If flags permit it, return NULL, or initialized kernel |
81 | * call structure. |
81 | * call structure. |
82 | */ |
82 | */ |
83 | call_t *ipc_call_alloc(int flags) |
83 | call_t *ipc_call_alloc(int flags) |
84 | { |
84 | { |
85 | call_t *call; |
85 | call_t *call; |
86 | 86 | ||
87 | call = slab_alloc(ipc_call_slab, flags); |
87 | call = slab_alloc(ipc_call_slab, flags); |
88 | _ipc_call_init(call); |
88 | _ipc_call_init(call); |
89 | 89 | ||
90 | return call; |
90 | return call; |
91 | } |
91 | } |
92 | 92 | ||
93 | /** Initialize a statically allocated call structure. |
93 | /** Initialize a statically allocated call structure. |
94 | * |
94 | * |
95 | * @param call Statically allocated kernel call structure to be |
95 | * @param call Statically allocated kernel call structure to be |
96 | * initialized. |
96 | * initialized. |
97 | */ |
97 | */ |
98 | void ipc_call_static_init(call_t *call) |
98 | void ipc_call_static_init(call_t *call) |
99 | { |
99 | { |
100 | _ipc_call_init(call); |
100 | _ipc_call_init(call); |
101 | call->flags |= IPC_CALL_STATIC_ALLOC; |
101 | call->flags |= IPC_CALL_STATIC_ALLOC; |
102 | } |
102 | } |
103 | 103 | ||
104 | /** Deallocate a call structure. |
104 | /** Deallocate a call structure. |
105 | * |
105 | * |
106 | * @param call Call structure to be freed. |
106 | * @param call Call structure to be freed. |
107 | */ |
107 | */ |
108 | void ipc_call_free(call_t *call) |
108 | void ipc_call_free(call_t *call) |
109 | { |
109 | { |
110 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
110 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
111 | /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */ |
111 | /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */ |
112 | if (call->buffer) |
112 | if (call->buffer) |
113 | free(call->buffer); |
113 | free(call->buffer); |
114 | slab_free(ipc_call_slab, call); |
114 | slab_free(ipc_call_slab, call); |
115 | } |
115 | } |
116 | 116 | ||
117 | /** Initialize an answerbox structure. |
117 | /** Initialize an answerbox structure. |
118 | * |
118 | * |
119 | * @param box Answerbox structure to be initialized. |
119 | * @param box Answerbox structure to be initialized. |
120 | * @param task Task to which the answerbox belongs. |
120 | * @param task Task to which the answerbox belongs. |
121 | */ |
121 | */ |
122 | void ipc_answerbox_init(answerbox_t *box, task_t *task) |
122 | void ipc_answerbox_init(answerbox_t *box, task_t *task) |
123 | { |
123 | { |
124 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
124 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
125 | spinlock_initialize(&box->irq_lock, "ipc_box_irqlock"); |
125 | spinlock_initialize(&box->irq_lock, "ipc_box_irqlock"); |
126 | waitq_initialize(&box->wq); |
126 | waitq_initialize(&box->wq); |
127 | list_initialize(&box->connected_phones); |
127 | list_initialize(&box->connected_phones); |
128 | list_initialize(&box->calls); |
128 | list_initialize(&box->calls); |
129 | list_initialize(&box->dispatched_calls); |
129 | list_initialize(&box->dispatched_calls); |
130 | list_initialize(&box->answers); |
130 | list_initialize(&box->answers); |
131 | list_initialize(&box->irq_notifs); |
131 | list_initialize(&box->irq_notifs); |
132 | list_initialize(&box->irq_head); |
132 | list_initialize(&box->irq_head); |
133 | box->task = task; |
133 | box->task = task; |
134 | } |
134 | } |
135 | 135 | ||
136 | /** Connect a phone to an answerbox. |
136 | /** Connect a phone to an answerbox. |
137 | * |
137 | * |
138 | * @param phone Initialized phone structure. |
138 | * @param phone Initialized phone structure. |
139 | * @param box Initialized answerbox structure. |
139 | * @param box Initialized answerbox structure. |
140 | */ |
140 | */ |
141 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
141 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
142 | { |
142 | { |
143 | spinlock_lock(&phone->lock); |
143 | spinlock_lock(&phone->lock); |
144 | 144 | ||
145 | phone->state = IPC_PHONE_CONNECTED; |
145 | phone->state = IPC_PHONE_CONNECTED; |
146 | phone->callee = box; |
146 | phone->callee = box; |
147 | 147 | ||
148 | spinlock_lock(&box->lock); |
148 | spinlock_lock(&box->lock); |
149 | list_append(&phone->link, &box->connected_phones); |
149 | list_append(&phone->link, &box->connected_phones); |
150 | spinlock_unlock(&box->lock); |
150 | spinlock_unlock(&box->lock); |
151 | 151 | ||
152 | spinlock_unlock(&phone->lock); |
152 | spinlock_unlock(&phone->lock); |
153 | } |
153 | } |
154 | 154 | ||
155 | /** Initialize a phone structure. |
155 | /** Initialize a phone structure. |
156 | * |
156 | * |
157 | * @param phone Phone structure to be initialized. |
157 | * @param phone Phone structure to be initialized. |
158 | */ |
158 | */ |
159 | void ipc_phone_init(phone_t *phone) |
159 | void ipc_phone_init(phone_t *phone) |
160 | { |
160 | { |
161 | spinlock_initialize(&phone->lock, "phone_lock"); |
161 | spinlock_initialize(&phone->lock, "phone_lock"); |
162 | phone->callee = NULL; |
162 | phone->callee = NULL; |
163 | phone->state = IPC_PHONE_FREE; |
163 | phone->state = IPC_PHONE_FREE; |
164 | atomic_set(&phone->active_calls, 0); |
164 | atomic_set(&phone->active_calls, 0); |
165 | } |
165 | } |
166 | 166 | ||
167 | /** Helper function to facilitate synchronous calls. |
167 | /** Helper function to facilitate synchronous calls. |
168 | * |
168 | * |
169 | * @param phone Destination kernel phone structure. |
169 | * @param phone Destination kernel phone structure. |
170 | * @param request Call structure with request. |
170 | * @param request Call structure with request. |
171 | */ |
171 | */ |
172 | void ipc_call_sync(phone_t *phone, call_t *request) |
172 | void ipc_call_sync(phone_t *phone, call_t *request) |
173 | { |
173 | { |
174 | answerbox_t sync_box; |
174 | answerbox_t sync_box; |
175 | 175 | ||
176 | ipc_answerbox_init(&sync_box, TASK); |
176 | ipc_answerbox_init(&sync_box, TASK); |
177 | 177 | ||
178 | /* We will receive data in a special box. */ |
178 | /* We will receive data in a special box. */ |
179 | request->callerbox = &sync_box; |
179 | request->callerbox = &sync_box; |
180 | 180 | ||
181 | ipc_call(phone, request); |
181 | ipc_call(phone, request); |
182 | ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE); |
182 | ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE); |
183 | } |
183 | } |
184 | 184 | ||
185 | /** Answer a message which was not dispatched and is not listed in any queue. |
185 | /** Answer a message which was not dispatched and is not listed in any queue. |
186 | * |
186 | * |
187 | * @param call Call structure to be answered. |
187 | * @param call Call structure to be answered. |
188 | */ |
188 | */ |
189 | static void _ipc_answer_free_call(call_t *call) |
189 | static void _ipc_answer_free_call(call_t *call) |
190 | { |
190 | { |
191 | answerbox_t *callerbox = call->callerbox; |
191 | answerbox_t *callerbox = call->callerbox; |
192 | 192 | ||
193 | call->flags |= IPC_CALL_ANSWERED; |
193 | call->flags |= IPC_CALL_ANSWERED; |
194 | 194 | ||
195 | spinlock_lock(&callerbox->lock); |
195 | spinlock_lock(&callerbox->lock); |
196 | list_append(&call->link, &callerbox->answers); |
196 | list_append(&call->link, &callerbox->answers); |
197 | spinlock_unlock(&callerbox->lock); |
197 | spinlock_unlock(&callerbox->lock); |
198 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST); |
198 | waitq_wakeup(&callerbox->wq, WAKEUP_FIRST); |
199 | } |
199 | } |
200 | 200 | ||
201 | /** Answer a message which is in a callee queue. |
201 | /** Answer a message which is in a callee queue. |
202 | * |
202 | * |
203 | * @param box Answerbox that is answering the message. |
203 | * @param box Answerbox that is answering the message. |
204 | * @param call Modified request that is being sent back. |
204 | * @param call Modified request that is being sent back. |
205 | */ |
205 | */ |
206 | void ipc_answer(answerbox_t *box, call_t *call) |
206 | void ipc_answer(answerbox_t *box, call_t *call) |
207 | { |
207 | { |
208 | /* Remove from active box */ |
208 | /* Remove from active box */ |
209 | spinlock_lock(&box->lock); |
209 | spinlock_lock(&box->lock); |
210 | list_remove(&call->link); |
210 | list_remove(&call->link); |
211 | spinlock_unlock(&box->lock); |
211 | spinlock_unlock(&box->lock); |
212 | /* Send back answer */ |
212 | /* Send back answer */ |
213 | _ipc_answer_free_call(call); |
213 | _ipc_answer_free_call(call); |
214 | } |
214 | } |
215 | 215 | ||
216 | /** Simulate sending back a message. |
216 | /** Simulate sending back a message. |
217 | * |
217 | * |
218 | * Most errors are better handled by forming a normal backward |
218 | * Most errors are better handled by forming a normal backward |
219 | * message and sending it as a normal answer. |
219 | * message and sending it as a normal answer. |
220 | * |
220 | * |
221 | * @param phone Phone structure the call should appear to come from. |
221 | * @param phone Phone structure the call should appear to come from. |
222 | * @param call Call structure to be answered. |
222 | * @param call Call structure to be answered. |
223 | * @param err Return value to be used for the answer. |
223 | * @param err Return value to be used for the answer. |
224 | */ |
224 | */ |
225 | void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err) |
225 | void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err) |
226 | { |
226 | { |
227 | call->data.phone = phone; |
227 | call->data.phone = phone; |
228 | atomic_inc(&phone->active_calls); |
228 | atomic_inc(&phone->active_calls); |
229 | IPC_SET_RETVAL(call->data, err); |
229 | IPC_SET_RETVAL(call->data, err); |
230 | _ipc_answer_free_call(call); |
230 | _ipc_answer_free_call(call); |
231 | } |
231 | } |
232 | 232 | ||
233 | /** Unsafe unchecking version of ipc_call. |
233 | /** Unsafe unchecking version of ipc_call. |
234 | * |
234 | * |
235 | * @param phone Phone structure the call comes from. |
235 | * @param phone Phone structure the call comes from. |
236 | * @param box Destination answerbox structure. |
236 | * @param box Destination answerbox structure. |
237 | * @param call Call structure with request. |
237 | * @param call Call structure with request. |
238 | */ |
238 | */ |
239 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call) |
239 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call) |
240 | { |
240 | { |
241 | if (!(call->flags & IPC_CALL_FORWARDED)) { |
241 | if (!(call->flags & IPC_CALL_FORWARDED)) { |
242 | atomic_inc(&phone->active_calls); |
242 | atomic_inc(&phone->active_calls); |
243 | call->data.phone = phone; |
243 | call->data.phone = phone; |
244 | } |
244 | } |
245 | 245 | ||
246 | spinlock_lock(&box->lock); |
246 | spinlock_lock(&box->lock); |
247 | list_append(&call->link, &box->calls); |
247 | list_append(&call->link, &box->calls); |
248 | spinlock_unlock(&box->lock); |
248 | spinlock_unlock(&box->lock); |
249 | waitq_wakeup(&box->wq, WAKEUP_FIRST); |
249 | waitq_wakeup(&box->wq, WAKEUP_FIRST); |
250 | } |
250 | } |
251 | 251 | ||
252 | /** Send an asynchronous request using a phone to an answerbox. |
252 | /** Send an asynchronous request using a phone to an answerbox. |
253 | * |
253 | * |
254 | * @param phone Phone structure the call comes from and which is |
254 | * @param phone Phone structure the call comes from and which is |
255 | * connected to the destination answerbox. |
255 | * connected to the destination answerbox. |
256 | * @param call Call structure with request. |
256 | * @param call Call structure with request. |
257 | * |
257 | * |
258 | * @return Return 0 on success, ENOENT on error. |
258 | * @return Return 0 on success, ENOENT on error. |
259 | */ |
259 | */ |
260 | int ipc_call(phone_t *phone, call_t *call) |
260 | int ipc_call(phone_t *phone, call_t *call) |
261 | { |
261 | { |
262 | answerbox_t *box; |
262 | answerbox_t *box; |
263 | 263 | ||
264 | spinlock_lock(&phone->lock); |
264 | spinlock_lock(&phone->lock); |
265 | if (phone->state != IPC_PHONE_CONNECTED) { |
265 | if (phone->state != IPC_PHONE_CONNECTED) { |
266 | spinlock_unlock(&phone->lock); |
266 | spinlock_unlock(&phone->lock); |
267 | if (call->flags & IPC_CALL_FORWARDED) { |
267 | if (call->flags & IPC_CALL_FORWARDED) { |
268 | IPC_SET_RETVAL(call->data, EFORWARD); |
268 | IPC_SET_RETVAL(call->data, EFORWARD); |
269 | _ipc_answer_free_call(call); |
269 | _ipc_answer_free_call(call); |
270 | } else { |
270 | } else { |
271 | if (phone->state == IPC_PHONE_HUNGUP) |
271 | if (phone->state == IPC_PHONE_HUNGUP) |
272 | ipc_backsend_err(phone, call, EHANGUP); |
272 | ipc_backsend_err(phone, call, EHANGUP); |
273 | else |
273 | else |
274 | ipc_backsend_err(phone, call, ENOENT); |
274 | ipc_backsend_err(phone, call, ENOENT); |
275 | } |
275 | } |
276 | return ENOENT; |
276 | return ENOENT; |
277 | } |
277 | } |
278 | box = phone->callee; |
278 | box = phone->callee; |
279 | _ipc_call(phone, box, call); |
279 | _ipc_call(phone, box, call); |
280 | 280 | ||
281 | spinlock_unlock(&phone->lock); |
281 | spinlock_unlock(&phone->lock); |
282 | return 0; |
282 | return 0; |
283 | } |
283 | } |
284 | 284 | ||
285 | /** Disconnect phone from answerbox. |
285 | /** Disconnect phone from answerbox. |
286 | * |
286 | * |
287 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done |
287 | * This call leaves the phone in the HUNGUP state. The change to 'free' is done |
288 | * lazily later. |
288 | * lazily later. |
289 | * |
289 | * |
290 | * @param phone Phone structure to be hung up. |
290 | * @param phone Phone structure to be hung up. |
291 | * |
291 | * |
292 | * @return Return 0 if the phone is disconnected. |
292 | * @return Return 0 if the phone is disconnected. |
293 | * Return -1 if the phone was already disconnected. |
293 | * Return -1 if the phone was already disconnected. |
294 | */ |
294 | */ |
295 | int ipc_phone_hangup(phone_t *phone) |
295 | int ipc_phone_hangup(phone_t *phone) |
296 | { |
296 | { |
297 | answerbox_t *box; |
297 | answerbox_t *box; |
298 | call_t *call; |
298 | call_t *call; |
299 | 299 | ||
300 | spinlock_lock(&phone->lock); |
300 | spinlock_lock(&phone->lock); |
301 | if (phone->state == IPC_PHONE_FREE || |
301 | if (phone->state == IPC_PHONE_FREE || |
302 | phone->state == IPC_PHONE_HUNGUP || |
302 | phone->state == IPC_PHONE_HUNGUP || |
303 | phone->state == IPC_PHONE_CONNECTING) { |
303 | phone->state == IPC_PHONE_CONNECTING) { |
304 | spinlock_unlock(&phone->lock); |
304 | spinlock_unlock(&phone->lock); |
305 | return -1; |
305 | return -1; |
306 | } |
306 | } |
307 | box = phone->callee; |
307 | box = phone->callee; |
308 | if (phone->state != IPC_PHONE_SLAMMED) { |
308 | if (phone->state != IPC_PHONE_SLAMMED) { |
309 | /* Remove myself from answerbox */ |
309 | /* Remove myself from answerbox */ |
310 | spinlock_lock(&box->lock); |
310 | spinlock_lock(&box->lock); |
311 | list_remove(&phone->link); |
311 | list_remove(&phone->link); |
312 | spinlock_unlock(&box->lock); |
312 | spinlock_unlock(&box->lock); |
313 | 313 | ||
314 | if (phone->state != IPC_PHONE_SLAMMED) { |
314 | if (phone->state != IPC_PHONE_SLAMMED) { |
315 | call = ipc_call_alloc(0); |
315 | call = ipc_call_alloc(0); |
316 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
316 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
317 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
317 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
318 | _ipc_call(phone, box, call); |
318 | _ipc_call(phone, box, call); |
319 | } |
319 | } |
320 | } |
320 | } |
321 | 321 | ||
322 | phone->state = IPC_PHONE_HUNGUP; |
322 | phone->state = IPC_PHONE_HUNGUP; |
323 | spinlock_unlock(&phone->lock); |
323 | spinlock_unlock(&phone->lock); |
324 | 324 | ||
325 | return 0; |
325 | return 0; |
326 | } |
326 | } |
327 | 327 | ||
328 | /** Forwards call from one answerbox to another one. |
328 | /** Forwards call from one answerbox to another one. |
329 | * |
329 | * |
330 | * @param call Call structure to be redirected. |
330 | * @param call Call structure to be redirected. |
331 | * @param newphone Phone structure to target answerbox. |
331 | * @param newphone Phone structure to target answerbox. |
332 | * @param oldbox Old answerbox structure. |
332 | * @param oldbox Old answerbox structure. |
333 | * @param mode Flags that specify mode of the forward operation. |
333 | * @param mode Flags that specify mode of the forward operation. |
334 | * |
334 | * |
335 | * @return Return 0 if forwarding succeeded or an error code if |
335 | * @return Return 0 if forwarding succeeded or an error code if |
336 | * there was error. |
336 | * there was error. |
337 | * |
337 | * |
338 | * The return value serves only as an information for the forwarder, |
338 | * The return value serves only as an information for the forwarder, |
339 | * the original caller is notified automatically with EFORWARD. |
339 | * the original caller is notified automatically with EFORWARD. |
340 | */ |
340 | */ |
341 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode) |
341 | int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode) |
342 | { |
342 | { |
343 | spinlock_lock(&oldbox->lock); |
343 | spinlock_lock(&oldbox->lock); |
344 | list_remove(&call->link); |
344 | list_remove(&call->link); |
345 | spinlock_unlock(&oldbox->lock); |
345 | spinlock_unlock(&oldbox->lock); |
346 | 346 | ||
347 | if (mode & IPC_FF_ROUTE_FROM_ME) |
347 | if (mode & IPC_FF_ROUTE_FROM_ME) |
348 | call->data.phone = newphone; |
348 | call->data.phone = newphone; |
349 | 349 | ||
350 | return ipc_call(newphone, call); |
350 | return ipc_call(newphone, call); |
351 | } |
351 | } |
352 | 352 | ||
353 | 353 | ||
354 | /** Wait for a phone call. |
354 | /** Wait for a phone call. |
355 | * |
355 | * |
356 | * @param box Answerbox expecting the call. |
356 | * @param box Answerbox expecting the call. |
357 | * @param usec Timeout in microseconds. See documentation for |
357 | * @param usec Timeout in microseconds. See documentation for |
358 | * waitq_sleep_timeout() for decription of its special |
358 | * waitq_sleep_timeout() for decription of its special |
359 | * meaning. |
359 | * meaning. |
360 | * @param flags Select mode of sleep operation. See documentation for |
360 | * @param flags Select mode of sleep operation. See documentation for |
361 | * waitq_sleep_timeout() for description of its special |
361 | * waitq_sleep_timeout() for description of its special |
362 | * meaning. |
362 | * meaning. |
363 | * @return Recived call structure or NULL. |
363 | * @return Recived call structure or NULL. |
364 | * |
364 | * |
365 | * To distinguish between a call and an answer, have a look at call->flags. |
365 | * To distinguish between a call and an answer, have a look at call->flags. |
366 | */ |
366 | */ |
367 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags) |
367 | call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags) |
368 | { |
368 | { |
369 | call_t *request; |
369 | call_t *request; |
370 | ipl_t ipl; |
370 | ipl_t ipl; |
371 | int rc; |
371 | int rc; |
372 | 372 | ||
373 | restart: |
373 | restart: |
374 | rc = waitq_sleep_timeout(&box->wq, usec, flags); |
374 | rc = waitq_sleep_timeout(&box->wq, usec, flags); |
375 | if (SYNCH_FAILED(rc)) |
375 | if (SYNCH_FAILED(rc)) |
376 | return NULL; |
376 | return NULL; |
377 | 377 | ||
378 | spinlock_lock(&box->lock); |
378 | spinlock_lock(&box->lock); |
379 | if (!list_empty(&box->irq_notifs)) { |
379 | if (!list_empty(&box->irq_notifs)) { |
380 | ipl = interrupts_disable(); |
380 | ipl = interrupts_disable(); |
381 | spinlock_lock(&box->irq_lock); |
381 | spinlock_lock(&box->irq_lock); |
382 | 382 | ||
383 | request = list_get_instance(box->irq_notifs.next, call_t, link); |
383 | request = list_get_instance(box->irq_notifs.next, call_t, link); |
384 | list_remove(&request->link); |
384 | list_remove(&request->link); |
385 | 385 | ||
386 | spinlock_unlock(&box->irq_lock); |
386 | spinlock_unlock(&box->irq_lock); |
387 | interrupts_restore(ipl); |
387 | interrupts_restore(ipl); |
388 | } else if (!list_empty(&box->answers)) { |
388 | } else if (!list_empty(&box->answers)) { |
389 | /* Handle asynchronous answers */ |
389 | /* Handle asynchronous answers */ |
390 | request = list_get_instance(box->answers.next, call_t, link); |
390 | request = list_get_instance(box->answers.next, call_t, link); |
391 | list_remove(&request->link); |
391 | list_remove(&request->link); |
392 | atomic_dec(&request->data.phone->active_calls); |
392 | atomic_dec(&request->data.phone->active_calls); |
393 | } else if (!list_empty(&box->calls)) { |
393 | } else if (!list_empty(&box->calls)) { |
394 | /* Handle requests */ |
394 | /* Handle requests */ |
395 | request = list_get_instance(box->calls.next, call_t, link); |
395 | request = list_get_instance(box->calls.next, call_t, link); |
396 | list_remove(&request->link); |
396 | list_remove(&request->link); |
397 | /* Append request to dispatch queue */ |
397 | /* Append request to dispatch queue */ |
398 | list_append(&request->link, &box->dispatched_calls); |
398 | list_append(&request->link, &box->dispatched_calls); |
399 | } else { |
399 | } else { |
400 | /* This can happen regularly after ipc_cleanup */ |
400 | /* This can happen regularly after ipc_cleanup */ |
401 | spinlock_unlock(&box->lock); |
401 | spinlock_unlock(&box->lock); |
402 | goto restart; |
402 | goto restart; |
403 | } |
403 | } |
404 | spinlock_unlock(&box->lock); |
404 | spinlock_unlock(&box->lock); |
405 | return request; |
405 | return request; |
406 | } |
406 | } |
407 | 407 | ||
408 | /** Answer all calls from list with EHANGUP answer. |
408 | /** Answer all calls from list with EHANGUP answer. |
409 | * |
409 | * |
410 | * @param lst Head of the list to be cleaned up. |
410 | * @param lst Head of the list to be cleaned up. |
411 | */ |
411 | */ |
412 | static void ipc_cleanup_call_list(link_t *lst) |
412 | static void ipc_cleanup_call_list(link_t *lst) |
413 | { |
413 | { |
414 | call_t *call; |
414 | call_t *call; |
415 | 415 | ||
416 | while (!list_empty(lst)) { |
416 | while (!list_empty(lst)) { |
417 | call = list_get_instance(lst->next, call_t, link); |
417 | call = list_get_instance(lst->next, call_t, link); |
418 | if (call->buffer) |
418 | if (call->buffer) |
419 | free(call->buffer); |
419 | free(call->buffer); |
420 | list_remove(&call->link); |
420 | list_remove(&call->link); |
421 | 421 | ||
422 | IPC_SET_RETVAL(call->data, EHANGUP); |
422 | IPC_SET_RETVAL(call->data, EHANGUP); |
423 | _ipc_answer_free_call(call); |
423 | _ipc_answer_free_call(call); |
424 | } |
424 | } |
425 | } |
425 | } |
426 | 426 | ||
427 | /** Cleans up all IPC communication of the current task. |
427 | /** Cleans up all IPC communication of the current task. |
428 | * |
428 | * |
429 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you |
429 | * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you |
430 | * have to change it as well if you want to cleanup other tasks than TASK. |
430 | * have to change it as well if you want to cleanup other tasks than TASK. |
431 | */ |
431 | */ |
432 | void ipc_cleanup(void) |
432 | void ipc_cleanup(void) |
433 | { |
433 | { |
434 | int i; |
434 | int i; |
435 | call_t *call; |
435 | call_t *call; |
436 | phone_t *phone; |
436 | phone_t *phone; |
437 | DEADLOCK_PROBE_INIT(p_phonelck); |
437 | DEADLOCK_PROBE_INIT(p_phonelck); |
438 | 438 | ||
439 | /* Disconnect all our phones ('ipc_phone_hangup') */ |
439 | /* Disconnect all our phones ('ipc_phone_hangup') */ |
440 | for (i = 0; i < IPC_MAX_PHONES; i++) |
440 | for (i = 0; i < IPC_MAX_PHONES; i++) |
441 | ipc_phone_hangup(&TASK->phones[i]); |
441 | ipc_phone_hangup(&TASK->phones[i]); |
442 | 442 | ||
443 | /* Disconnect all connected irqs */ |
443 | /* Disconnect all connected irqs */ |
444 | ipc_irq_cleanup(&TASK->answerbox); |
444 | ipc_irq_cleanup(&TASK->answerbox); |
445 | 445 | ||
446 | /* Disconnect all phones connected to our answerbox */ |
446 | /* Disconnect all phones connected to our answerbox */ |
447 | restart_phones: |
447 | restart_phones: |
448 | spinlock_lock(&TASK->answerbox.lock); |
448 | spinlock_lock(&TASK->answerbox.lock); |
449 | while (!list_empty(&TASK->answerbox.connected_phones)) { |
449 | while (!list_empty(&TASK->answerbox.connected_phones)) { |
450 | phone = list_get_instance(TASK->answerbox.connected_phones.next, |
450 | phone = list_get_instance(TASK->answerbox.connected_phones.next, |
451 | phone_t, link); |
451 | phone_t, link); |
452 | if (!spinlock_trylock(&phone->lock)) { |
452 | if (!spinlock_trylock(&phone->lock)) { |
453 | spinlock_unlock(&TASK->answerbox.lock); |
453 | spinlock_unlock(&TASK->answerbox.lock); |
454 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD); |
454 | DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD); |
455 | goto restart_phones; |
455 | goto restart_phones; |
456 | } |
456 | } |
457 | 457 | ||
458 | /* Disconnect phone */ |
458 | /* Disconnect phone */ |
459 | ASSERT(phone->state == IPC_PHONE_CONNECTED); |
459 | ASSERT(phone->state == IPC_PHONE_CONNECTED); |
460 | phone->state = IPC_PHONE_SLAMMED; |
460 | phone->state = IPC_PHONE_SLAMMED; |
461 | list_remove(&phone->link); |
461 | list_remove(&phone->link); |
462 | 462 | ||
463 | spinlock_unlock(&phone->lock); |
463 | spinlock_unlock(&phone->lock); |
464 | } |
464 | } |
465 | 465 | ||
466 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
466 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
467 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls); |
467 | ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls); |
468 | ipc_cleanup_call_list(&TASK->answerbox.calls); |
468 | ipc_cleanup_call_list(&TASK->answerbox.calls); |
469 | spinlock_unlock(&TASK->answerbox.lock); |
469 | spinlock_unlock(&TASK->answerbox.lock); |
470 | 470 | ||
471 | /* Wait for all async answers to arrive */ |
471 | /* Wait for all async answers to arrive */ |
472 | while (1) { |
472 | while (1) { |
473 | /* Go through all phones, until all are FREE... */ |
473 | /* Go through all phones, until all are FREE... */ |
474 | /* Locking not needed, no one else should modify |
474 | /* Locking not needed, no one else should modify |
475 | * it, when we are in cleanup */ |
475 | * it, when we are in cleanup */ |
476 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
476 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
477 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP && |
477 | if (TASK->phones[i].state == IPC_PHONE_HUNGUP && |
478 | atomic_get(&TASK->phones[i].active_calls) == 0) |
478 | atomic_get(&TASK->phones[i].active_calls) == 0) |
479 | TASK->phones[i].state = IPC_PHONE_FREE; |
479 | TASK->phones[i].state = IPC_PHONE_FREE; |
480 | 480 | ||
481 | /* Just for sure, we might have had some |
481 | /* Just for sure, we might have had some |
482 | * IPC_PHONE_CONNECTING phones */ |
482 | * IPC_PHONE_CONNECTING phones */ |
483 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED) |
483 | if (TASK->phones[i].state == IPC_PHONE_CONNECTED) |
484 | ipc_phone_hangup(&TASK->phones[i]); |
484 | ipc_phone_hangup(&TASK->phones[i]); |
485 | /* If the hangup succeeded, it has sent a HANGUP |
485 | /* If the hangup succeeded, it has sent a HANGUP |
486 | * message, the IPC is now in HUNGUP state, we |
486 | * message, the IPC is now in HUNGUP state, we |
487 | * wait for the reply to come */ |
487 | * wait for the reply to come */ |
488 | 488 | ||
489 | if (TASK->phones[i].state != IPC_PHONE_FREE) |
489 | if (TASK->phones[i].state != IPC_PHONE_FREE) |
490 | break; |
490 | break; |
491 | } |
491 | } |
492 | /* Voila, got into cleanup */ |
492 | /* Voila, got into cleanup */ |
493 | if (i == IPC_MAX_PHONES) |
493 | if (i == IPC_MAX_PHONES) |
494 | break; |
494 | break; |
495 | 495 | ||
496 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT, |
496 | call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT, |
497 | SYNCH_FLAGS_NONE); |
497 | SYNCH_FLAGS_NONE); |
498 | ASSERT((call->flags & IPC_CALL_ANSWERED) || |
498 | ASSERT((call->flags & IPC_CALL_ANSWERED) || |
499 | (call->flags & IPC_CALL_NOTIF)); |
499 | (call->flags & IPC_CALL_NOTIF)); |
500 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
500 | ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC)); |
501 | 501 | ||
502 | atomic_dec(&TASK->active_calls); |
502 | atomic_dec(&TASK->active_calls); |
503 | ipc_call_free(call); |
503 | ipc_call_free(call); |
504 | } |
504 | } |
505 | } |
505 | } |
506 | 506 | ||
507 | 507 | ||
508 | /** Initilize IPC subsystem */ |
508 | /** Initilize IPC subsystem */ |
509 | void ipc_init(void) |
509 | void ipc_init(void) |
510 | { |
510 | { |
511 | ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL, |
511 | ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL, |
512 | NULL, 0); |
512 | NULL, 0); |
513 | } |
513 | } |
514 | 514 | ||
515 | 515 | ||
516 | /** List answerbox contents. |
516 | /** List answerbox contents. |
517 | * |
517 | * |
518 | * @param taskid Task ID. |
518 | * @param taskid Task ID. |
519 | */ |
519 | */ |
520 | void ipc_print_task(task_id_t taskid) |
520 | void ipc_print_task(task_id_t taskid) |
521 | { |
521 | { |
522 | task_t *task; |
522 | task_t *task; |
523 | int i; |
523 | int i; |
524 | call_t *call; |
524 | call_t *call; |
525 | link_t *tmp; |
525 | link_t *tmp; |
526 | 526 | ||
527 | spinlock_lock(&tasks_lock); |
527 | spinlock_lock(&tasks_lock); |
528 | task = task_find_by_id(taskid); |
528 | task = task_find_by_id(taskid); |
529 | if (task) |
529 | if (task) |
530 | spinlock_lock(&task->lock); |
530 | spinlock_lock(&task->lock); |
531 | spinlock_unlock(&tasks_lock); |
531 | spinlock_unlock(&tasks_lock); |
532 | if (!task) |
532 | if (!task) |
533 | return; |
533 | return; |
534 | 534 | ||
535 | /* Print opened phones & details */ |
535 | /* Print opened phones & details */ |
536 | printf("PHONE:\n"); |
536 | printf("PHONE:\n"); |
537 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
537 | for (i = 0; i < IPC_MAX_PHONES; i++) { |
538 | spinlock_lock(&task->phones[i].lock); |
538 | spinlock_lock(&task->phones[i].lock); |
539 | if (task->phones[i].state != IPC_PHONE_FREE) { |
539 | if (task->phones[i].state != IPC_PHONE_FREE) { |
540 | printf("%d: ", i); |
540 | printf("%d: ", i); |
541 | switch (task->phones[i].state) { |
541 | switch (task->phones[i].state) { |
542 | case IPC_PHONE_CONNECTING: |
542 | case IPC_PHONE_CONNECTING: |
543 | printf("connecting "); |
543 | printf("connecting "); |
544 | break; |
544 | break; |
545 | case IPC_PHONE_CONNECTED: |
545 | case IPC_PHONE_CONNECTED: |
546 | printf("connected to: %p ", |
546 | printf("connected to: %p ", |
547 | task->phones[i].callee); |
547 | task->phones[i].callee); |
548 | break; |
548 | break; |
549 | case IPC_PHONE_SLAMMED: |
549 | case IPC_PHONE_SLAMMED: |
550 | printf("slammed by: %p ", |
550 | printf("slammed by: %p ", |
551 | task->phones[i].callee); |
551 | task->phones[i].callee); |
552 | break; |
552 | break; |
553 | case IPC_PHONE_HUNGUP: |
553 | case IPC_PHONE_HUNGUP: |
554 | printf("hung up - was: %p ", |
554 | printf("hung up - was: %p ", |
555 | task->phones[i].callee); |
555 | task->phones[i].callee); |
556 | break; |
556 | break; |
557 | default: |
557 | default: |
558 | break; |
558 | break; |
559 | } |
559 | } |
560 | printf("active: %d\n", |
560 | printf("active: %d\n", |
561 | atomic_get(&task->phones[i].active_calls)); |
561 | atomic_get(&task->phones[i].active_calls)); |
562 | } |
562 | } |
563 | spinlock_unlock(&task->phones[i].lock); |
563 | spinlock_unlock(&task->phones[i].lock); |
564 | } |
564 | } |
565 | 565 | ||
566 | 566 | ||
567 | /* Print answerbox - calls */ |
567 | /* Print answerbox - calls */ |
568 | spinlock_lock(&task->answerbox.lock); |
568 | spinlock_lock(&task->answerbox.lock); |
569 | printf("ABOX - CALLS:\n"); |
569 | printf("ABOX - CALLS:\n"); |
570 | for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls; |
570 | for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls; |
571 | tmp = tmp->next) { |
571 | tmp = tmp->next) { |
572 | call = list_get_instance(tmp, call_t, link); |
572 | call = list_get_instance(tmp, call_t, link); |
573 | printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d " |
573 | printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d " |
574 | "A4:%d A5:%d Flags:%x\n", call, call->sender->taskid, |
574 | "A4:%d A5:%d Flags:%x\n", call, call->sender->taskid, |
575 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
575 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
576 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
576 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
577 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
577 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
578 | call->flags); |
578 | call->flags); |
579 | } |
579 | } |
580 | /* Print answerbox - calls */ |
580 | /* Print answerbox - calls */ |
581 | printf("ABOX - DISPATCHED CALLS:\n"); |
581 | printf("ABOX - DISPATCHED CALLS:\n"); |
582 | for (tmp=task->answerbox.dispatched_calls.next; |
582 | for (tmp=task->answerbox.dispatched_calls.next; |
583 | tmp != &task->answerbox.dispatched_calls; |
583 | tmp != &task->answerbox.dispatched_calls; |
584 | tmp = tmp->next) { |
584 | tmp = tmp->next) { |
585 | call = list_get_instance(tmp, call_t, link); |
585 | call = list_get_instance(tmp, call_t, link); |
586 | printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d " |
586 | printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d " |
587 | "A4:%d A5:%d Flags:%x\n", call, call->sender->taskid, |
587 | "A4:%d A5:%d Flags:%x\n", call, call->sender->taskid, |
588 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
588 | IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
589 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
589 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
590 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
590 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
591 | call->flags); |
591 | call->flags); |
592 | } |
592 | } |
593 | /* Print answerbox - calls */ |
593 | /* Print answerbox - calls */ |
594 | printf("ABOX - ANSWERS:\n"); |
594 | printf("ABOX - ANSWERS:\n"); |
595 | for (tmp = task->answerbox.answers.next; tmp != &task->answerbox.answers; |
595 | for (tmp = task->answerbox.answers.next; tmp != &task->answerbox.answers; |
596 | tmp = tmp->next) { |
596 | tmp = tmp->next) { |
597 | call = list_get_instance(tmp, call_t, link); |
597 | call = list_get_instance(tmp, call_t, link); |
598 | printf("Callid:%p M:%d A1:%d A2:%d A3:%d A4:%d A5:%d Flags:%x\n", |
598 | printf("Callid:%p M:%d A1:%d A2:%d A3:%d A4:%d A5:%d Flags:%x\n", |
599 | call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
599 | call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data), |
600 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
600 | IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data), |
601 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
601 | IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data), |
602 | call->flags); |
602 | call->flags); |
603 | } |
603 | } |
604 | 604 | ||
605 | spinlock_unlock(&task->answerbox.lock); |
605 | spinlock_unlock(&task->answerbox.lock); |
606 | spinlock_unlock(&task->lock); |
606 | spinlock_unlock(&task->lock); |
607 | } |
607 | } |
608 | 608 | ||
609 | #include <ipc/ipcrsc.h> |
609 | #include <ipc/ipcrsc.h> |
610 | #include <console/klog.h> |
610 | #include <console/klog.h> |
611 | #include <udebug/udebug_ipc.h> |
611 | #include <udebug/udebug_ipc.h> |
612 | 612 | ||
613 | static void kbox_thread_proc(void *arg) |
613 | static void kbox_thread_proc(void *arg) |
614 | { |
614 | { |
615 | call_t *call; |
615 | call_t *call; |
616 | int method; |
616 | int method; |
617 | bool done; |
617 | bool done; |
618 | ipl_t ipl; |
618 | ipl_t ipl; |
619 | 619 | ||
620 | (void)arg; |
620 | (void)arg; |
621 | klog_printf("kbox_thread_proc()"); |
621 | klog_printf("kbox_thread_proc()"); |
622 | done = false; |
622 | done = false; |
623 | 623 | ||
624 | while (!done) { |
624 | while (!done) { |
625 | klog_printf("kbox: wait for call"); |
625 | klog_printf("kbox: wait for call"); |
626 | call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT, |
626 | call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT, |
627 | SYNCH_FLAGS_NONE); |
627 | SYNCH_FLAGS_NONE); |
628 | 628 | ||
629 | if (call != NULL) { |
629 | if (call != NULL) { |
630 | method = IPC_GET_METHOD(call->data); |
630 | method = IPC_GET_METHOD(call->data); |
631 | 631 | ||
632 | if (method == IPC_M_DEBUG_ALL) { |
632 | if (method == IPC_M_DEBUG_ALL) { |
633 | udebug_call_receive(call); |
633 | udebug_call_receive(call); |
634 | } |
634 | } |
635 | 635 | ||
636 | if (method == IPC_M_PHONE_HUNGUP) { |
636 | if (method == IPC_M_PHONE_HUNGUP) { |
637 | klog_printf("kbox: handle hangup message\n"); |
637 | klog_printf("kbox: handle hangup message\n"); |
- | 638 | ||
- | 639 | /* Was it our debugger, who hung up? */ |
|
- | 640 | if (call->sender == TASK->debugger) { |
|
- | 641 | /* Terminate debugging session (if any) */ |
|
- | 642 | klog_printf("kbox: terminate debug session\n"); |
|
- | 643 | ipl = interrupts_disable(); |
|
- | 644 | spinlock_lock(&TASK->lock); |
|
- | 645 | udebug_task_cleanup(TASK); |
|
- | 646 | spinlock_unlock(&TASK->lock); |
|
- | 647 | interrupts_restore(ipl); |
|
- | 648 | } else { |
|
- | 649 | klog_printf("kbox: was not debugger\n"); |
|
- | 650 | } |
|
- | 651 | ||
- | 652 | klog_printf("kbox: continue with hangup message\n"); |
|
638 | IPC_SET_RETVAL(call->data, 0); |
653 | IPC_SET_RETVAL(call->data, 0); |
639 | ipc_answer(&TASK->kernel_box, call); |
654 | ipc_answer(&TASK->kernel_box, call); |
640 | 655 | ||
641 | ipl = interrupts_disable(); |
656 | ipl = interrupts_disable(); |
642 | spinlock_lock(&TASK->lock); |
657 | spinlock_lock(&TASK->lock); |
643 | spinlock_lock(&TASK->answerbox.lock); |
658 | spinlock_lock(&TASK->answerbox.lock); |
644 | if (list_empty(&TASK->answerbox.connected_phones)) { |
659 | if (list_empty(&TASK->answerbox.connected_phones)) { |
645 | /* Last phone has been disconnected */ |
660 | /* Last phone has been disconnected */ |
646 | TASK->kb_thread_at_hand = false; |
661 | TASK->kb_thread_at_hand = false; |
647 | TASK->kb_thread = NULL; |
662 | TASK->kb_thread = NULL; |
648 | done = true; |
663 | done = true; |
649 | printf("phone list is empty\n"); |
664 | printf("phone list is empty\n"); |
650 | } |
665 | } |
651 | spinlock_unlock(&TASK->answerbox.lock); |
666 | spinlock_unlock(&TASK->answerbox.lock); |
652 | spinlock_unlock(&TASK->lock); |
667 | spinlock_unlock(&TASK->lock); |
653 | interrupts_restore(ipl); |
668 | interrupts_restore(ipl); |
654 | } |
669 | } |
655 | } |
670 | } |
656 | } |
671 | } |
657 | 672 | ||
658 | klog_printf("kbox: finished"); |
673 | klog_printf("kbox: finished"); |
659 | } |
674 | } |
660 | 675 | ||
661 | 676 | ||
662 | /** |
677 | /** |
663 | * Connect phone to a task kernel-box specified by id. |
678 | * Connect phone to a task kernel-box specified by id. |
664 | * |
679 | * |
665 | * @return Phone id on success, or negative error code. |
680 | * @return Phone id on success, or negative error code. |
666 | */ |
681 | */ |
667 | int ipc_connect_kbox(task_id_t taskid) |
682 | int ipc_connect_kbox(task_id_t taskid) |
668 | { |
683 | { |
669 | int newphid; |
684 | int newphid; |
670 | task_t *ta; |
685 | task_t *ta; |
671 | thread_t *kb_thread; |
686 | thread_t *kb_thread; |
672 | int rc; |
687 | int rc; |
673 | ipl_t ipl; |
688 | ipl_t ipl; |
674 | 689 | ||
675 | newphid = phone_alloc(); |
690 | newphid = phone_alloc(); |
676 | if (newphid < 0) |
691 | if (newphid < 0) |
677 | return ELIMIT; |
692 | return ELIMIT; |
678 | 693 | ||
679 | ipl = interrupts_disable(); |
694 | ipl = interrupts_disable(); |
680 | spinlock_lock(&tasks_lock); |
695 | spinlock_lock(&tasks_lock); |
681 | 696 | ||
682 | ta = task_find_by_id(taskid); |
697 | ta = task_find_by_id(taskid); |
683 | if (ta == NULL) { |
698 | if (ta == NULL) { |
684 | spinlock_unlock(&tasks_lock); |
699 | spinlock_unlock(&tasks_lock); |
685 | return ENOENT; |
700 | return ENOENT; |
686 | } |
701 | } |
687 | 702 | ||
688 | spinlock_lock(&ta->lock); |
703 | spinlock_lock(&ta->lock); |
689 | spinlock_unlock(&tasks_lock); |
704 | spinlock_unlock(&tasks_lock); |
690 | 705 | ||
691 | ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box); |
706 | ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box); |
692 | 707 | ||
693 | if (ta->kb_thread_at_hand == false) { |
708 | if (ta->kb_thread_at_hand == false) { |
694 | ta->kb_thread_at_hand = true; |
709 | ta->kb_thread_at_hand = true; |
695 | spinlock_unlock(&ta->lock); |
710 | spinlock_unlock(&ta->lock); |
696 | interrupts_restore(ipl); |
711 | interrupts_restore(ipl); |
697 | 712 | ||
698 | kb_thread = thread_create(kbox_thread_proc, |
713 | kb_thread = thread_create(kbox_thread_proc, |
699 | NULL, ta, THREAD_FLAG_NOATTACH, "kbox", false); |
714 | NULL, ta, THREAD_FLAG_NOATTACH, "kbox", false); |
700 | if (!kb_thread) |
715 | if (!kb_thread) |
701 | return ENOMEM; |
716 | return ENOMEM; |
702 | 717 | ||
703 | rc = thread_attach_by_id(kb_thread, taskid); |
718 | rc = thread_attach_by_id(kb_thread, taskid); |
704 | 719 | ||
705 | if (rc == EOK) { |
720 | if (rc == EOK) { |
706 | ipl = interrupts_disable(); |
721 | ipl = interrupts_disable(); |
707 | spinlock_lock(&ta->lock); |
722 | spinlock_lock(&ta->lock); |
708 | ta->kb_thread = kb_thread; |
723 | ta->kb_thread = kb_thread; |
709 | spinlock_unlock(&ta->lock); |
724 | spinlock_unlock(&ta->lock); |
710 | interrupts_restore(ipl); |
725 | interrupts_restore(ipl); |
711 | 726 | ||
712 | thread_detach(kb_thread); |
727 | thread_detach(kb_thread); |
713 | thread_ready(kb_thread); |
728 | thread_ready(kb_thread); |
714 | } else { |
729 | } else { |
715 | /* Return the allocated thread struct */ |
730 | /* Return the allocated thread struct */ |
716 | thread_unattached_free(kb_thread); |
731 | thread_unattached_free(kb_thread); |
717 | } |
732 | } |
718 | } else { |
733 | } else { |
719 | spinlock_unlock(&ta->lock); |
734 | spinlock_unlock(&ta->lock); |
720 | interrupts_restore(ipl); |
735 | interrupts_restore(ipl); |
721 | } |
736 | } |
722 | 737 | ||
723 | return newphid; |
738 | return newphid; |
724 | } |
739 | } |
725 | 740 | ||
726 | /** @} |
741 | /** @} |
727 | */ |
742 | */ |
728 | 743 |