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