Subversion Repositories HelenOS

Rev

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

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