Subversion Repositories HelenOS

Rev

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

Rev 3362 Rev 3363
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
 */
174
 */
175
void ipc_call_sync(phone_t *phone, call_t *request)
175
void ipc_call_sync(phone_t *phone, call_t *request)
176
{
176
{
177
    answerbox_t sync_box;
177
    answerbox_t sync_box;
178
 
178
 
179
    ipc_answerbox_init(&sync_box, TASK);
179
    ipc_answerbox_init(&sync_box, TASK);
180
 
180
 
181
    /* We will receive data in a special box. */
181
    /* We will receive data in a special box. */
182
    request->callerbox = &sync_box;
182
    request->callerbox = &sync_box;
183
 
183
 
184
    ipc_call(phone, request);
184
    ipc_call(phone, request);
185
    ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
185
    ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
186
}
186
}
187
 
187
 
188
/** Answer a message which was not dispatched and is not listed in any queue.
188
/** Answer a message which was not dispatched and is not listed in any queue.
189
 *
189
 *
190
 * @param call      Call structure to be answered.
190
 * @param call      Call structure to be answered.
191
 */
191
 */
192
static void _ipc_answer_free_call(call_t *call)
192
static void _ipc_answer_free_call(call_t *call)
193
{
193
{
194
    answerbox_t *callerbox = call->callerbox;
194
    answerbox_t *callerbox = call->callerbox;
195
 
195
 
196
    call->flags |= IPC_CALL_ANSWERED;
196
    call->flags |= IPC_CALL_ANSWERED;
197
 
197
 
-
 
198
    if (call->flags & IPC_CALL_FORWARDED) {
-
 
199
        if (call->data.caller_phone) {
-
 
200
            /* Demasquerade the caller phone. */
-
 
201
            call->data.phone = call->data.caller_phone;
-
 
202
        }
-
 
203
    }
-
 
204
 
198
    spinlock_lock(&callerbox->lock);
205
    spinlock_lock(&callerbox->lock);
199
    list_append(&call->link, &callerbox->answers);
206
    list_append(&call->link, &callerbox->answers);
200
    spinlock_unlock(&callerbox->lock);
207
    spinlock_unlock(&callerbox->lock);
201
    waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
208
    waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
202
}
209
}
203
 
210
 
204
/** Answer a message which is in a callee queue.
211
/** Answer a message which is in a callee queue.
205
 *
212
 *
206
 * @param box       Answerbox that is answering the message.
213
 * @param box       Answerbox that is answering the message.
207
 * @param call      Modified request that is being sent back.
214
 * @param call      Modified request that is being sent back.
208
 */
215
 */
209
void ipc_answer(answerbox_t *box, call_t *call)
216
void ipc_answer(answerbox_t *box, call_t *call)
210
{
217
{
211
    /* Remove from active box */
218
    /* Remove from active box */
212
    spinlock_lock(&box->lock);
219
    spinlock_lock(&box->lock);
213
    list_remove(&call->link);
220
    list_remove(&call->link);
214
    spinlock_unlock(&box->lock);
221
    spinlock_unlock(&box->lock);
215
    /* Send back answer */
222
    /* Send back answer */
216
    _ipc_answer_free_call(call);
223
    _ipc_answer_free_call(call);
217
}
224
}
218
 
225
 
219
/** Simulate sending back a message.
226
/** Simulate sending back a message.
220
 *
227
 *
221
 * Most errors are better handled by forming a normal backward
228
 * Most errors are better handled by forming a normal backward
222
 * message and sending it as a normal answer.
229
 * message and sending it as a normal answer.
223
 *
230
 *
224
 * @param phone     Phone structure the call should appear to come from.
231
 * @param phone     Phone structure the call should appear to come from.
225
 * @param call      Call structure to be answered.
232
 * @param call      Call structure to be answered.
226
 * @param err       Return value to be used for the answer.
233
 * @param err       Return value to be used for the answer.
227
 */
234
 */
228
void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
235
void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
229
{
236
{
230
    call->data.phone = phone;
237
    call->data.phone = phone;
231
    atomic_inc(&phone->active_calls);
238
    atomic_inc(&phone->active_calls);
232
    IPC_SET_RETVAL(call->data, err);
239
    IPC_SET_RETVAL(call->data, err);
233
    _ipc_answer_free_call(call);
240
    _ipc_answer_free_call(call);
234
}
241
}
235
 
242
 
236
/** Unsafe unchecking version of ipc_call.
243
/** Unsafe unchecking version of ipc_call.
237
 *
244
 *
238
 * @param phone     Phone structure the call comes from.
245
 * @param phone     Phone structure the call comes from.
239
 * @param box       Destination answerbox structure.
246
 * @param box       Destination answerbox structure.
240
 * @param call      Call structure with request.
247
 * @param call      Call structure with request.
241
 */
248
 */
242
static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
249
static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
243
{
250
{
244
    if (!(call->flags & IPC_CALL_FORWARDED)) {
251
    if (!(call->flags & IPC_CALL_FORWARDED)) {
245
        atomic_inc(&phone->active_calls);
252
        atomic_inc(&phone->active_calls);
246
        call->data.phone = phone;
253
        call->data.phone = phone;
247
    }
254
    }
248
 
255
 
249
    spinlock_lock(&box->lock);
256
    spinlock_lock(&box->lock);
250
    list_append(&call->link, &box->calls);
257
    list_append(&call->link, &box->calls);
251
    spinlock_unlock(&box->lock);
258
    spinlock_unlock(&box->lock);
252
    waitq_wakeup(&box->wq, WAKEUP_FIRST);
259
    waitq_wakeup(&box->wq, WAKEUP_FIRST);
253
}
260
}
254
 
261
 
255
/** Send an asynchronous request using a phone to an answerbox.
262
/** Send an asynchronous request using a phone to an answerbox.
256
 *
263
 *
257
 * @param phone     Phone structure the call comes from and which is
264
 * @param phone     Phone structure the call comes from and which is
258
 *          connected to the destination answerbox.
265
 *          connected to the destination answerbox.
259
 * @param call      Call structure with request.
266
 * @param call      Call structure with request.
260
 *
267
 *
261
 * @return      Return 0 on success, ENOENT on error.
268
 * @return      Return 0 on success, ENOENT on error.
262
 */
269
 */
263
int ipc_call(phone_t *phone, call_t *call)
270
int ipc_call(phone_t *phone, call_t *call)
264
{
271
{
265
    answerbox_t *box;
272
    answerbox_t *box;
266
 
273
 
267
    mutex_lock(&phone->lock);
274
    mutex_lock(&phone->lock);
268
    if (phone->state != IPC_PHONE_CONNECTED) {
275
    if (phone->state != IPC_PHONE_CONNECTED) {
269
        mutex_unlock(&phone->lock);
276
        mutex_unlock(&phone->lock);
270
        if (call->flags & IPC_CALL_FORWARDED) {
277
        if (call->flags & IPC_CALL_FORWARDED) {
271
            IPC_SET_RETVAL(call->data, EFORWARD);
278
            IPC_SET_RETVAL(call->data, EFORWARD);
272
            _ipc_answer_free_call(call);
279
            _ipc_answer_free_call(call);
273
        } else {
280
        } else {
274
            if (phone->state == IPC_PHONE_HUNGUP)
281
            if (phone->state == IPC_PHONE_HUNGUP)
275
                ipc_backsend_err(phone, call, EHANGUP);
282
                ipc_backsend_err(phone, call, EHANGUP);
276
            else
283
            else
277
                ipc_backsend_err(phone, call, ENOENT);
284
                ipc_backsend_err(phone, call, ENOENT);
278
        }
285
        }
279
        return ENOENT;
286
        return ENOENT;
280
    }
287
    }
281
    box = phone->callee;
288
    box = phone->callee;
282
    _ipc_call(phone, box, call);
289
    _ipc_call(phone, box, call);
283
   
290
   
284
    mutex_unlock(&phone->lock);
291
    mutex_unlock(&phone->lock);
285
    return 0;
292
    return 0;
286
}
293
}
287
 
294
 
288
/** Disconnect phone from answerbox.
295
/** Disconnect phone from answerbox.
289
 *
296
 *
290
 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
297
 * This call leaves the phone in the HUNGUP state. The change to 'free' is done
291
 * lazily later.
298
 * lazily later.
292
 *
299
 *
293
 * @param phone     Phone structure to be hung up.
300
 * @param phone     Phone structure to be hung up.
294
 *              
301
 *              
295
 * @return      Return 0 if the phone is disconnected.
302
 * @return      Return 0 if the phone is disconnected.
296
 *          Return -1 if the phone was already disconnected.
303
 *          Return -1 if the phone was already disconnected.
297
 */
304
 */
298
int ipc_phone_hangup(phone_t *phone)
305
int ipc_phone_hangup(phone_t *phone)
299
{
306
{
300
    answerbox_t *box;
307
    answerbox_t *box;
301
    call_t *call;
308
    call_t *call;
302
   
309
   
303
    mutex_lock(&phone->lock);
310
    mutex_lock(&phone->lock);
304
    if (phone->state == IPC_PHONE_FREE ||
311
    if (phone->state == IPC_PHONE_FREE ||
305
        phone->state == IPC_PHONE_HUNGUP ||
312
        phone->state == IPC_PHONE_HUNGUP ||
306
        phone->state == IPC_PHONE_CONNECTING) {
313
        phone->state == IPC_PHONE_CONNECTING) {
307
        mutex_unlock(&phone->lock);
314
        mutex_unlock(&phone->lock);
308
        return -1;
315
        return -1;
309
    }
316
    }
310
    box = phone->callee;
317
    box = phone->callee;
311
    if (phone->state != IPC_PHONE_SLAMMED) {
318
    if (phone->state != IPC_PHONE_SLAMMED) {
312
        /* Remove myself from answerbox */
319
        /* Remove myself from answerbox */
313
        spinlock_lock(&box->lock);
320
        spinlock_lock(&box->lock);
314
        list_remove(&phone->link);
321
        list_remove(&phone->link);
315
        spinlock_unlock(&box->lock);
322
        spinlock_unlock(&box->lock);
316
 
323
 
317
        if (phone->state != IPC_PHONE_SLAMMED) {
324
        if (phone->state != IPC_PHONE_SLAMMED) {
318
            call = ipc_call_alloc(0);
325
            call = ipc_call_alloc(0);
319
            IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
326
            IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
320
            call->flags |= IPC_CALL_DISCARD_ANSWER;
327
            call->flags |= IPC_CALL_DISCARD_ANSWER;
321
            _ipc_call(phone, box, call);
328
            _ipc_call(phone, box, call);
322
        }
329
        }
323
    }
330
    }
324
 
331
 
325
    phone->state = IPC_PHONE_HUNGUP;
332
    phone->state = IPC_PHONE_HUNGUP;
326
    mutex_unlock(&phone->lock);
333
    mutex_unlock(&phone->lock);
327
 
334
 
328
    return 0;
335
    return 0;
329
}
336
}
330
 
337
 
331
/** Forwards call from one answerbox to another one.
338
/** Forwards call from one answerbox to another one.
332
 *
339
 *
333
 * @param call      Call structure to be redirected.
340
 * @param call      Call structure to be redirected.
334
 * @param newphone  Phone structure to target answerbox.
341
 * @param newphone  Phone structure to target answerbox.
335
 * @param oldbox    Old answerbox structure.
342
 * @param oldbox    Old answerbox structure.
336
 * @param mode      Flags that specify mode of the forward operation.
343
 * @param mode      Flags that specify mode of the forward operation.
337
 *
344
 *
338
 * @return      Return 0 if forwarding succeeded or an error code if
345
 * @return      Return 0 if forwarding succeeded or an error code if
339
 *          there was error.
346
 *          there was error.
340
 *
347
 *
341
 * The return value serves only as an information for the forwarder,
348
 * The return value serves only as an information for the forwarder,
342
 * the original caller is notified automatically with EFORWARD.
349
 * the original caller is notified automatically with EFORWARD.
343
 */
350
 */
344
int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode)
351
int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode)
345
{
352
{
346
    spinlock_lock(&oldbox->lock);
353
    spinlock_lock(&oldbox->lock);
347
    list_remove(&call->link);
354
    list_remove(&call->link);
348
    spinlock_unlock(&oldbox->lock);
355
    spinlock_unlock(&oldbox->lock);
349
 
356
 
350
    if (mode & IPC_FF_ROUTE_FROM_ME) {
357
    if (mode & IPC_FF_ROUTE_FROM_ME) {
351
        if (!call->data.caller_phone)
358
        if (!call->data.caller_phone)
352
            call->data.caller_phone = call->data.phone;
359
            call->data.caller_phone = call->data.phone;
353
        call->data.phone = newphone;
360
        call->data.phone = newphone;
354
    }
361
    }
355
 
362
 
356
    return ipc_call(newphone, call);
363
    return ipc_call(newphone, call);
357
}
364
}
358
 
365
 
359
 
366
 
360
/** Wait for a phone call.
367
/** Wait for a phone call.
361
 *
368
 *
362
 * @param box       Answerbox expecting the call.
369
 * @param box       Answerbox expecting the call.
363
 * @param usec      Timeout in microseconds. See documentation for
370
 * @param usec      Timeout in microseconds. See documentation for
364
 *          waitq_sleep_timeout() for decription of its special
371
 *          waitq_sleep_timeout() for decription of its special
365
 *          meaning.
372
 *          meaning.
366
 * @param flags     Select mode of sleep operation. See documentation for
373
 * @param flags     Select mode of sleep operation. See documentation for
367
 *          waitq_sleep_timeout() for description of its special
374
 *          waitq_sleep_timeout() for description of its special
368
 *          meaning.
375
 *          meaning.
369
 * @return      Recived call structure or NULL.
376
 * @return      Recived call structure or NULL.
370
 *
377
 *
371
 * To distinguish between a call and an answer, have a look at call->flags.
378
 * To distinguish between a call and an answer, have a look at call->flags.
372
 */
379
 */
373
call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
380
call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
374
{
381
{
375
    call_t *request;
382
    call_t *request;
376
    ipl_t ipl;
383
    ipl_t ipl;
377
    int rc;
384
    int rc;
378
 
385
 
379
restart:
386
restart:
380
    rc = waitq_sleep_timeout(&box->wq, usec, flags);
387
    rc = waitq_sleep_timeout(&box->wq, usec, flags);
381
    if (SYNCH_FAILED(rc))
388
    if (SYNCH_FAILED(rc))
382
        return NULL;
389
        return NULL;
383
   
390
   
384
    spinlock_lock(&box->lock);
391
    spinlock_lock(&box->lock);
385
    if (!list_empty(&box->irq_notifs)) {
392
    if (!list_empty(&box->irq_notifs)) {
386
        ipl = interrupts_disable();
393
        ipl = interrupts_disable();
387
        spinlock_lock(&box->irq_lock);
394
        spinlock_lock(&box->irq_lock);
388
 
395
 
389
        request = list_get_instance(box->irq_notifs.next, call_t, link);
396
        request = list_get_instance(box->irq_notifs.next, call_t, link);
390
        list_remove(&request->link);
397
        list_remove(&request->link);
391
 
398
 
392
        spinlock_unlock(&box->irq_lock);
399
        spinlock_unlock(&box->irq_lock);
393
        interrupts_restore(ipl);
400
        interrupts_restore(ipl);
394
    } else if (!list_empty(&box->answers)) {
401
    } else if (!list_empty(&box->answers)) {
395
        /* Handle asynchronous answers */
402
        /* Handle asynchronous answers */
396
        request = list_get_instance(box->answers.next, call_t, link);
403
        request = list_get_instance(box->answers.next, call_t, link);
397
        list_remove(&request->link);
404
        list_remove(&request->link);
398
        if (request->data.caller_phone)
-
 
399
            atomic_dec(&request->data.caller_phone->active_calls);
-
 
400
        else
-
 
401
            atomic_dec(&request->data.phone->active_calls);
405
        atomic_dec(&request->data.phone->active_calls);
402
    } else if (!list_empty(&box->calls)) {
406
    } else if (!list_empty(&box->calls)) {
403
        /* Handle requests */
407
        /* Handle requests */
404
        request = list_get_instance(box->calls.next, call_t, link);
408
        request = list_get_instance(box->calls.next, call_t, link);
405
        list_remove(&request->link);
409
        list_remove(&request->link);
406
        /* Append request to dispatch queue */
410
        /* Append request to dispatch queue */
407
        list_append(&request->link, &box->dispatched_calls);
411
        list_append(&request->link, &box->dispatched_calls);
408
    } else {
412
    } else {
409
        /* This can happen regularly after ipc_cleanup */
413
        /* This can happen regularly after ipc_cleanup */
410
        spinlock_unlock(&box->lock);
414
        spinlock_unlock(&box->lock);
411
        goto restart;
415
        goto restart;
412
    }
416
    }
413
    spinlock_unlock(&box->lock);
417
    spinlock_unlock(&box->lock);
414
    return request;
418
    return request;
415
}
419
}
416
 
420
 
417
/** Answer all calls from list with EHANGUP answer.
421
/** Answer all calls from list with EHANGUP answer.
418
 *
422
 *
419
 * @param lst       Head of the list to be cleaned up.
423
 * @param lst       Head of the list to be cleaned up.
420
 */
424
 */
421
static void ipc_cleanup_call_list(link_t *lst)
425
static void ipc_cleanup_call_list(link_t *lst)
422
{
426
{
423
    call_t *call;
427
    call_t *call;
424
 
428
 
425
    while (!list_empty(lst)) {
429
    while (!list_empty(lst)) {
426
        call = list_get_instance(lst->next, call_t, link);
430
        call = list_get_instance(lst->next, call_t, link);
427
        if (call->buffer)
431
        if (call->buffer)
428
            free(call->buffer);
432
            free(call->buffer);
429
        list_remove(&call->link);
433
        list_remove(&call->link);
430
 
434
 
431
        IPC_SET_RETVAL(call->data, EHANGUP);
435
        IPC_SET_RETVAL(call->data, EHANGUP);
432
        _ipc_answer_free_call(call);
436
        _ipc_answer_free_call(call);
433
    }
437
    }
434
}
438
}
435
 
439
 
436
/** Cleans up all IPC communication of the current task.
440
/** Cleans up all IPC communication of the current task.
437
 *
441
 *
438
 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
442
 * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
439
 * have to change it as well if you want to cleanup other tasks than TASK.
443
 * have to change it as well if you want to cleanup other tasks than TASK.
440
 */
444
 */
441
void ipc_cleanup(void)
445
void ipc_cleanup(void)
442
{
446
{
443
    int i;
447
    int i;
444
    call_t *call;
448
    call_t *call;
445
    phone_t *phone;
449
    phone_t *phone;
446
    DEADLOCK_PROBE_INIT(p_phonelck);
450
    DEADLOCK_PROBE_INIT(p_phonelck);
447
 
451
 
448
    /* Disconnect all our phones ('ipc_phone_hangup') */
452
    /* Disconnect all our phones ('ipc_phone_hangup') */
449
    for (i = 0; i < IPC_MAX_PHONES; i++)
453
    for (i = 0; i < IPC_MAX_PHONES; i++)
450
        ipc_phone_hangup(&TASK->phones[i]);
454
        ipc_phone_hangup(&TASK->phones[i]);
451
 
455
 
452
    /* Disconnect all connected irqs */
456
    /* Disconnect all connected irqs */
453
    ipc_irq_cleanup(&TASK->answerbox);
457
    ipc_irq_cleanup(&TASK->answerbox);
454
 
458
 
455
    /* Disconnect all phones connected to our answerbox */
459
    /* Disconnect all phones connected to our answerbox */
456
restart_phones:
460
restart_phones:
457
    spinlock_lock(&TASK->answerbox.lock);
461
    spinlock_lock(&TASK->answerbox.lock);
458
    while (!list_empty(&TASK->answerbox.connected_phones)) {
462
    while (!list_empty(&TASK->answerbox.connected_phones)) {
459
        phone = list_get_instance(TASK->answerbox.connected_phones.next,
463
        phone = list_get_instance(TASK->answerbox.connected_phones.next,
460
            phone_t, link);
464
            phone_t, link);
461
        if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
465
        if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
462
            spinlock_unlock(&TASK->answerbox.lock);
466
            spinlock_unlock(&TASK->answerbox.lock);
463
            DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
467
            DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
464
            goto restart_phones;
468
            goto restart_phones;
465
        }
469
        }
466
       
470
       
467
        /* Disconnect phone */
471
        /* Disconnect phone */
468
        ASSERT(phone->state == IPC_PHONE_CONNECTED);
472
        ASSERT(phone->state == IPC_PHONE_CONNECTED);
469
        phone->state = IPC_PHONE_SLAMMED;
473
        phone->state = IPC_PHONE_SLAMMED;
470
        list_remove(&phone->link);
474
        list_remove(&phone->link);
471
 
475
 
472
        mutex_unlock(&phone->lock);
476
        mutex_unlock(&phone->lock);
473
    }
477
    }
474
 
478
 
475
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
479
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
476
    ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
480
    ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
477
    ipc_cleanup_call_list(&TASK->answerbox.calls);
481
    ipc_cleanup_call_list(&TASK->answerbox.calls);
478
    spinlock_unlock(&TASK->answerbox.lock);
482
    spinlock_unlock(&TASK->answerbox.lock);
479
   
483
   
480
    /* Wait for all async answers to arrive */
484
    /* Wait for all async answers to arrive */
481
    while (1) {
485
    while (1) {
482
        /* Go through all phones, until all are FREE... */
486
        /* Go through all phones, until all are FREE... */
483
        /* Locking not needed, no one else should modify
487
        /* Locking not needed, no one else should modify
484
         * it, when we are in cleanup */
488
         * it, when we are in cleanup */
485
        for (i = 0; i < IPC_MAX_PHONES; i++) {
489
        for (i = 0; i < IPC_MAX_PHONES; i++) {
486
            if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
490
            if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
487
                atomic_get(&TASK->phones[i].active_calls) == 0)
491
                atomic_get(&TASK->phones[i].active_calls) == 0)
488
                TASK->phones[i].state = IPC_PHONE_FREE;
492
                TASK->phones[i].state = IPC_PHONE_FREE;
489
           
493
           
490
            /* Just for sure, we might have had some
494
            /* Just for sure, we might have had some
491
             * IPC_PHONE_CONNECTING phones */
495
             * IPC_PHONE_CONNECTING phones */
492
            if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
496
            if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
493
                ipc_phone_hangup(&TASK->phones[i]);
497
                ipc_phone_hangup(&TASK->phones[i]);
494
            /* If the hangup succeeded, it has sent a HANGUP
498
            /* If the hangup succeeded, it has sent a HANGUP
495
             * message, the IPC is now in HUNGUP state, we
499
             * message, the IPC is now in HUNGUP state, we
496
             * wait for the reply to come */
500
             * wait for the reply to come */
497
           
501
           
498
            if (TASK->phones[i].state != IPC_PHONE_FREE)
502
            if (TASK->phones[i].state != IPC_PHONE_FREE)
499
                break;
503
                break;
500
        }
504
        }
501
        /* Voila, got into cleanup */
505
        /* Voila, got into cleanup */
502
        if (i == IPC_MAX_PHONES)
506
        if (i == IPC_MAX_PHONES)
503
            break;
507
            break;
504
       
508
       
505
        call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
509
        call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
506
            SYNCH_FLAGS_NONE);
510
            SYNCH_FLAGS_NONE);
507
        ASSERT((call->flags & IPC_CALL_ANSWERED) ||
511
        ASSERT((call->flags & IPC_CALL_ANSWERED) ||
508
            (call->flags & IPC_CALL_NOTIF));
512
            (call->flags & IPC_CALL_NOTIF));
509
        ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
513
        ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
510
       
514
       
511
        atomic_dec(&TASK->active_calls);
515
        atomic_dec(&TASK->active_calls);
512
        ipc_call_free(call);
516
        ipc_call_free(call);
513
    }
517
    }
514
}
518
}
515
 
519
 
516
 
520
 
517
/** Initilize IPC subsystem */
521
/** Initilize IPC subsystem */
518
void ipc_init(void)
522
void ipc_init(void)
519
{
523
{
520
    ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
524
    ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
521
        NULL, 0);
525
        NULL, 0);
522
}
526
}
523
 
527
 
524
 
528
 
525
/** List answerbox contents.
529
/** List answerbox contents.
526
 *
530
 *
527
 * @param taskid    Task ID.
531
 * @param taskid    Task ID.
528
 */
532
 */
529
void ipc_print_task(task_id_t taskid)
533
void ipc_print_task(task_id_t taskid)
530
{
534
{
531
    task_t *task;
535
    task_t *task;
532
    int i;
536
    int i;
533
    call_t *call;
537
    call_t *call;
534
    link_t *tmp;
538
    link_t *tmp;
535
   
539
   
536
    spinlock_lock(&tasks_lock);
540
    spinlock_lock(&tasks_lock);
537
    task = task_find_by_id(taskid);
541
    task = task_find_by_id(taskid);
538
    if (task)
542
    if (task)
539
        spinlock_lock(&task->lock);
543
        spinlock_lock(&task->lock);
540
    spinlock_unlock(&tasks_lock);
544
    spinlock_unlock(&tasks_lock);
541
    if (!task)
545
    if (!task)
542
        return;
546
        return;
543
 
547
 
544
    /* Print opened phones & details */
548
    /* Print opened phones & details */
545
    printf("PHONE:\n");
549
    printf("PHONE:\n");
546
    for (i = 0; i < IPC_MAX_PHONES; i++) {
550
    for (i = 0; i < IPC_MAX_PHONES; i++) {
547
        if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
551
        if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
548
            printf("%d: mutex busy\n", i);
552
            printf("%d: mutex busy\n", i);
549
            continue;
553
            continue;
550
        }
554
        }
551
        if (task->phones[i].state != IPC_PHONE_FREE) {
555
        if (task->phones[i].state != IPC_PHONE_FREE) {
552
            printf("%d: ", i);
556
            printf("%d: ", i);
553
            switch (task->phones[i].state) {
557
            switch (task->phones[i].state) {
554
            case IPC_PHONE_CONNECTING:
558
            case IPC_PHONE_CONNECTING:
555
                printf("connecting ");
559
                printf("connecting ");
556
                break;
560
                break;
557
            case IPC_PHONE_CONNECTED:
561
            case IPC_PHONE_CONNECTED:
558
                printf("connected to: %p ",
562
                printf("connected to: %p ",
559
                       task->phones[i].callee);
563
                       task->phones[i].callee);
560
                break;
564
                break;
561
            case IPC_PHONE_SLAMMED:
565
            case IPC_PHONE_SLAMMED:
562
                printf("slammed by: %p ",
566
                printf("slammed by: %p ",
563
                       task->phones[i].callee);
567
                       task->phones[i].callee);
564
                break;
568
                break;
565
            case IPC_PHONE_HUNGUP:
569
            case IPC_PHONE_HUNGUP:
566
                printf("hung up - was: %p ",
570
                printf("hung up - was: %p ",
567
                       task->phones[i].callee);
571
                       task->phones[i].callee);
568
                break;
572
                break;
569
            default:
573
            default:
570
                break;
574
                break;
571
            }
575
            }
572
            printf("active: %ld\n",
576
            printf("active: %ld\n",
573
                atomic_get(&task->phones[i].active_calls));
577
                atomic_get(&task->phones[i].active_calls));
574
        }
578
        }
575
        mutex_unlock(&task->phones[i].lock);
579
        mutex_unlock(&task->phones[i].lock);
576
    }
580
    }
577
 
581
 
578
 
582
 
579
    /* Print answerbox - calls */
583
    /* Print answerbox - calls */
580
    spinlock_lock(&task->answerbox.lock);
584
    spinlock_lock(&task->answerbox.lock);
581
    printf("ABOX - CALLS:\n");
585
    printf("ABOX - CALLS:\n");
582
    for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls;
586
    for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls;
583
        tmp = tmp->next) {
587
        tmp = tmp->next) {
584
        call = list_get_instance(tmp, call_t, link);
588
        call = list_get_instance(tmp, call_t, link);
585
        printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
589
        printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
586
            " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
590
            " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
587
            " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call, call->sender->taskid,
591
            " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call, call->sender->taskid,
588
            IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
592
            IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
589
            IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
593
            IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
590
            IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
594
            IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
591
            call->flags);
595
            call->flags);
592
    }
596
    }
593
    /* Print answerbox - calls */
597
    /* Print answerbox - calls */
594
    printf("ABOX - DISPATCHED CALLS:\n");
598
    printf("ABOX - DISPATCHED CALLS:\n");
595
    for (tmp = task->answerbox.dispatched_calls.next;
599
    for (tmp = task->answerbox.dispatched_calls.next;
596
        tmp != &task->answerbox.dispatched_calls;
600
        tmp != &task->answerbox.dispatched_calls;
597
        tmp = tmp->next) {
601
        tmp = tmp->next) {
598
        call = list_get_instance(tmp, call_t, link);
602
        call = list_get_instance(tmp, call_t, link);
599
        printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
603
        printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
600
            " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
604
            " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
601
            " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call, call->sender->taskid,
605
            " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call, call->sender->taskid,
602
            IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
606
            IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
603
            IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
607
            IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
604
            IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
608
            IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
605
            call->flags);
609
            call->flags);
606
    }
610
    }
607
    /* Print answerbox - calls */
611
    /* Print answerbox - calls */
608
    printf("ABOX - ANSWERS:\n");
612
    printf("ABOX - ANSWERS:\n");
609
    for (tmp = task->answerbox.answers.next; tmp != &task->answerbox.answers;
613
    for (tmp = task->answerbox.answers.next; tmp != &task->answerbox.answers;
610
        tmp = tmp->next) {
614
        tmp = tmp->next) {
611
        call = list_get_instance(tmp, call_t, link);
615
        call = list_get_instance(tmp, call_t, link);
612
        printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun
616
        printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun
613
            " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n",
617
            " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n",
614
            call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
618
            call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
615
            IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
619
            IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
616
            IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
620
            IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
617
            call->flags);
621
            call->flags);
618
    }
622
    }
619
 
623
 
620
    spinlock_unlock(&task->answerbox.lock);
624
    spinlock_unlock(&task->answerbox.lock);
621
    spinlock_unlock(&task->lock);
625
    spinlock_unlock(&task->lock);
622
}
626
}
623
 
627
 
624
/** @}
628
/** @}
625
 */
629
 */
626
 
630