Subversion Repositories HelenOS

Rev

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

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