Subversion Repositories HelenOS

Rev

Rev 2870 | Rev 2913 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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