Subversion Repositories HelenOS-historic

Rev

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

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