Subversion Repositories HelenOS

Rev

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

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