Subversion Repositories HelenOS

Rev

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

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