Subversion Repositories HelenOS

Rev

Rev 3363 | Rev 3394 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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