Subversion Repositories HelenOS

Rev

Rev 1963 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1963 Rev 1968
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
/* Lock ordering
29
/* Lock ordering
30
 *
30
 *
31
 * First the answerbox, then the phone
31
 * First the answerbox, then the phone
32
 */
32
 */
33
 
33
 
34
#include <synch/spinlock.h>
34
#include <synch/spinlock.h>
35
#include <synch/waitq.h>
35
#include <synch/waitq.h>
36
#include <ipc/ipc.h>
36
#include <ipc/ipc.h>
37
#include <errno.h>
37
#include <errno.h>
38
#include <mm/slab.h>
38
#include <mm/slab.h>
39
#include <arch.h>
39
#include <arch.h>
40
#include <proc/task.h>
40
#include <proc/task.h>
41
#include <memstr.h>
41
#include <memstr.h>
42
#include <debug.h>
42
#include <debug.h>
43
 
43
 
44
#include <print.h>
44
#include <print.h>
45
#include <proc/thread.h>
45
#include <proc/thread.h>
46
#include <arch/interrupt.h>
46
#include <arch/interrupt.h>
47
#include <ipc/irq.h>
47
#include <ipc/irq.h>
48
 
48
 
49
/* Open channel that is assigned automatically to new tasks */
49
/* Open channel that is assigned automatically to new tasks */
50
answerbox_t *ipc_phone_0 = NULL;
50
answerbox_t *ipc_phone_0 = NULL;
51
 
51
 
52
static slab_cache_t *ipc_call_slab;
52
static slab_cache_t *ipc_call_slab;
53
 
53
 
54
/* Initialize new call */
54
/* Initialize new call */
55
static void _ipc_call_init(call_t *call)
55
static void _ipc_call_init(call_t *call)
56
{
56
{
57
    memsetb((__address)call, sizeof(*call), 0);
57
    memsetb((__address)call, sizeof(*call), 0);
58
    call->callerbox = &TASK->answerbox;
58
    call->callerbox = &TASK->answerbox;
59
    call->sender = TASK;
59
    call->sender = TASK;
60
}
60
}
61
 
61
 
62
/** Allocate & initialize call structure
62
/** Allocate & initialize call structure
63
 *
63
 *
64
 * The call is initialized, so that the reply will be directed
64
 * The call is initialized, so that the reply will be directed
65
 * to TASK->answerbox
65
 * to TASK->answerbox
66
 *
66
 *
67
 * @param flags Parameters for slab_alloc (ATOMIC, etc.)
67
 * @param flags Parameters for slab_alloc (ATOMIC, etc.)
68
 */
68
 */
69
call_t * ipc_call_alloc(int flags)
69
call_t * ipc_call_alloc(int flags)
70
{
70
{
71
    call_t *call;
71
    call_t *call;
72
 
72
 
73
    call = slab_alloc(ipc_call_slab, flags);
73
    call = slab_alloc(ipc_call_slab, flags);
74
    _ipc_call_init(call);
74
    _ipc_call_init(call);
75
 
75
 
76
    return call;
76
    return call;
77
}
77
}
78
 
78
 
79
/** Initialize allocated call */
79
/** Initialize allocated call */
80
void ipc_call_static_init(call_t *call)
80
void ipc_call_static_init(call_t *call)
81
{
81
{
82
    _ipc_call_init(call);
82
    _ipc_call_init(call);
83
    call->flags |= IPC_CALL_STATIC_ALLOC;
83
    call->flags |= IPC_CALL_STATIC_ALLOC;
84
}
84
}
85
 
85
 
86
/** Deallocate call stracuture */
86
/** Deallocate call stracuture */
87
void ipc_call_free(call_t *call)
87
void ipc_call_free(call_t *call)
88
{
88
{
89
    slab_free(ipc_call_slab, call);
89
    slab_free(ipc_call_slab, call);
90
}
90
}
91
 
91
 
92
/** Initialize answerbox structure
92
/** Initialize answerbox structure
93
 */
93
 */
94
void ipc_answerbox_init(answerbox_t *box)
94
void ipc_answerbox_init(answerbox_t *box)
95
{
95
{
96
    spinlock_initialize(&box->lock, "ipc_box_lock");
96
    spinlock_initialize(&box->lock, "ipc_box_lock");
97
    spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
97
    spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
98
    waitq_initialize(&box->wq);
98
    waitq_initialize(&box->wq);
99
    list_initialize(&box->connected_phones);
99
    list_initialize(&box->connected_phones);
100
    list_initialize(&box->calls);
100
    list_initialize(&box->calls);
101
    list_initialize(&box->dispatched_calls);
101
    list_initialize(&box->dispatched_calls);
102
    list_initialize(&box->answers);
102
    list_initialize(&box->answers);
103
    list_initialize(&box->irq_notifs);
103
    list_initialize(&box->irq_notifs);
104
    box->task = TASK;
104
    box->task = TASK;
105
}
105
}
106
 
106
 
107
/** Connect phone to answerbox */
107
/** Connect phone to answerbox */
108
void ipc_phone_connect(phone_t *phone, answerbox_t *box)
108
void ipc_phone_connect(phone_t *phone, answerbox_t *box)
109
{
109
{
110
    spinlock_lock(&phone->lock);
110
    spinlock_lock(&phone->lock);
111
 
111
 
112
    ASSERT(!phone->callee);
112
    ASSERT(!phone->callee);
113
    phone->busy = IPC_BUSY_CONNECTED;
113
    phone->busy = IPC_BUSY_CONNECTED;
114
    phone->callee = box;
114
    phone->callee = box;
115
 
115
 
116
    spinlock_lock(&box->lock);
116
    spinlock_lock(&box->lock);
117
    list_append(&phone->list, &box->connected_phones);
117
    list_append(&phone->list, &box->connected_phones);
118
    spinlock_unlock(&box->lock);
118
    spinlock_unlock(&box->lock);
119
 
119
 
120
    spinlock_unlock(&phone->lock);
120
    spinlock_unlock(&phone->lock);
121
}
121
}
122
 
122
 
123
/** Initialize phone structure and connect phone to answerbox
123
/** Initialize phone structure and connect phone to answerbox
124
 */
124
 */
125
void ipc_phone_init(phone_t *phone)
125
void ipc_phone_init(phone_t *phone)
126
{
126
{
127
    spinlock_initialize(&phone->lock, "phone_lock");
127
    spinlock_initialize(&phone->lock, "phone_lock");
128
    phone->callee = NULL;
128
    phone->callee = NULL;
129
    phone->busy = IPC_BUSY_FREE;
129
    phone->busy = IPC_BUSY_FREE;
130
    atomic_set(&phone->active_calls, 0);
130
    atomic_set(&phone->active_calls, 0);
131
}
131
}
132
 
132
 
133
/** Helper function to facilitate synchronous calls */
133
/** Helper function to facilitate synchronous calls */
134
void ipc_call_sync(phone_t *phone, call_t *request)
134
void ipc_call_sync(phone_t *phone, call_t *request)
135
{
135
{
136
    answerbox_t sync_box;
136
    answerbox_t sync_box;
137
 
137
 
138
    ipc_answerbox_init(&sync_box);
138
    ipc_answerbox_init(&sync_box);
139
 
139
 
140
    /* We will receive data on special box */
140
    /* We will receive data on special box */
141
    request->callerbox = &sync_box;
141
    request->callerbox = &sync_box;
142
 
142
 
143
    ipc_call(phone, request);
143
    ipc_call(phone, request);
144
    ipc_wait_for_call(&sync_box, 0);
144
    ipc_wait_for_call(&sync_box, 0);
145
}
145
}
146
 
146
 
147
/** Answer message that was not dispatched and is not entered in
147
/** Answer message that was not dispatched and is not entered in
148
 * any queue
148
 * any queue
149
 */
149
 */
150
static void _ipc_answer_free_call(call_t *call)
150
static void _ipc_answer_free_call(call_t *call)
151
{
151
{
152
    answerbox_t *callerbox = call->callerbox;
152
    answerbox_t *callerbox = call->callerbox;
153
 
153
 
154
    call->flags |= IPC_CALL_ANSWERED;
154
    call->flags |= IPC_CALL_ANSWERED;
155
 
155
 
156
    spinlock_lock(&callerbox->lock);
156
    spinlock_lock(&callerbox->lock);
157
    list_append(&call->list, &callerbox->answers);
157
    list_append(&call->list, &callerbox->answers);
158
    spinlock_unlock(&callerbox->lock);
158
    spinlock_unlock(&callerbox->lock);
159
    waitq_wakeup(&callerbox->wq, 0);
159
    waitq_wakeup(&callerbox->wq, 0);
160
}
160
}
161
 
161
 
162
/** Answer message, that is in callee queue
162
/** Answer message, that is in callee queue
163
 *
163
 *
164
 * @param box Answerbox that is answering the message
164
 * @param box Answerbox that is answering the message
165
 * @param call Modified request that is being sent back
165
 * @param call Modified request that is being sent back
166
 */
166
 */
167
void ipc_answer(answerbox_t *box, call_t *call)
167
void ipc_answer(answerbox_t *box, call_t *call)
168
{
168
{
169
    /* Remove from active box */
169
    /* Remove from active box */
170
    spinlock_lock(&box->lock);
170
    spinlock_lock(&box->lock);
171
    list_remove(&call->list);
171
    list_remove(&call->list);
172
    spinlock_unlock(&box->lock);
172
    spinlock_unlock(&box->lock);
173
    /* Send back answer */
173
    /* Send back answer */
174
    _ipc_answer_free_call(call);
174
    _ipc_answer_free_call(call);
175
}
175
}
176
 
176
 
177
/** Simulate sending back a message
177
/** Simulate sending back a message
178
 *
178
 *
179
 * Most errors are better handled by forming a normal backward
179
 * Most errors are better handled by forming a normal backward
180
 * message and sending it as a normal answer.
180
 * message and sending it as a normal answer.
181
 */
181
 */
182
void ipc_backsend_err(phone_t *phone, call_t *call, __native err)
182
void ipc_backsend_err(phone_t *phone, call_t *call, __native err)
183
{
183
{
184
    call->data.phone = phone;
184
    call->data.phone = phone;
185
    atomic_inc(&phone->active_calls);
185
    atomic_inc(&phone->active_calls);
186
    if (phone->busy == IPC_BUSY_CONNECTED)
186
    if (phone->busy == IPC_BUSY_CONNECTED)
187
        IPC_SET_RETVAL(call->data, EHANGUP);
187
        IPC_SET_RETVAL(call->data, EHANGUP);
188
    else
188
    else
189
        IPC_SET_RETVAL(call->data, ENOENT);
189
        IPC_SET_RETVAL(call->data, ENOENT);
190
 
190
 
191
    _ipc_answer_free_call(call);
191
    _ipc_answer_free_call(call);
192
}
192
}
193
 
193
 
194
/* Unsafe unchecking ipc_call */
194
/* Unsafe unchecking ipc_call */
195
static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
195
static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
196
{
196
{
197
    if (! (call->flags & IPC_CALL_FORWARDED)) {
197
    if (! (call->flags & IPC_CALL_FORWARDED)) {
198
        atomic_inc(&phone->active_calls);
198
        atomic_inc(&phone->active_calls);
199
        call->data.phone = phone;
199
        call->data.phone = phone;
200
    }
200
    }
201
 
201
 
202
    spinlock_lock(&box->lock);
202
    spinlock_lock(&box->lock);
203
    list_append(&call->list, &box->calls);
203
    list_append(&call->list, &box->calls);
204
    spinlock_unlock(&box->lock);
204
    spinlock_unlock(&box->lock);
205
    waitq_wakeup(&box->wq, 0);
205
    waitq_wakeup(&box->wq, 0);
206
}
206
}
207
 
207
 
208
/** Send a asynchronous request using phone to answerbox
208
/** Send a asynchronous request using phone to answerbox
209
 *
209
 *
210
 * @param phone Phone connected to answerbox
210
 * @param phone Phone connected to answerbox
211
 * @param request Request to be sent
211
 * @param request Request to be sent
212
 */
212
 */
213
int ipc_call(phone_t *phone, call_t *call)
213
int ipc_call(phone_t *phone, call_t *call)
214
{
214
{
215
    answerbox_t *box;
215
    answerbox_t *box;
216
 
216
 
217
    spinlock_lock(&phone->lock);
217
    spinlock_lock(&phone->lock);
218
 
218
 
219
    box = phone->callee;
219
    box = phone->callee;
220
    if (!box) {
220
    if (!box) {
221
        /* Trying to send over disconnected phone */
221
        /* Trying to send over disconnected phone */
222
        spinlock_unlock(&phone->lock);
222
        spinlock_unlock(&phone->lock);
223
        if (call->flags & IPC_CALL_FORWARDED) {
223
        if (call->flags & IPC_CALL_FORWARDED) {
224
            IPC_SET_RETVAL(call->data, EFORWARD);
224
            IPC_SET_RETVAL(call->data, EFORWARD);
225
            _ipc_answer_free_call(call);
225
            _ipc_answer_free_call(call);
226
        } else { /* Simulate sending back a message */
226
        } else { /* Simulate sending back a message */
227
            if (phone->busy == IPC_BUSY_CONNECTED)
227
            if (phone->busy == IPC_BUSY_CONNECTED)
228
                ipc_backsend_err(phone, call, EHANGUP);
228
                ipc_backsend_err(phone, call, EHANGUP);
229
            else
229
            else
230
                ipc_backsend_err(phone, call, ENOENT);
230
                ipc_backsend_err(phone, call, ENOENT);
231
        }
231
        }
232
 
232
 
233
        return ENOENT;
233
        return ENOENT;
234
    }
234
    }
235
    _ipc_call(phone, box, call);
235
    _ipc_call(phone, box, call);
236
   
236
   
237
    spinlock_unlock(&phone->lock);
237
    spinlock_unlock(&phone->lock);
238
    return 0;
238
    return 0;
239
}
239
}
240
 
240
 
241
/** Disconnect phone from answerbox
241
/** Disconnect phone from answerbox
242
 *
242
 *
243
 * It is allowed to call disconnect on already disconnected phone
243
 * It is allowed to call disconnect on already disconnected phone
244
 *
244
 *
245
 * @return 0 - phone disconnected, -1 - the phone was already disconnected
245
 * @return 0 - phone disconnected, -1 - the phone was already disconnected
246
 */
246
 */
247
int ipc_phone_hangup(phone_t *phone)
247
int ipc_phone_hangup(phone_t *phone)
248
{
248
{
249
    answerbox_t *box;
249
    answerbox_t *box;
250
    call_t *call;
250
    call_t *call;
251
   
251
   
252
    spinlock_lock(&phone->lock);
252
    spinlock_lock(&phone->lock);
253
    box = phone->callee;
253
    box = phone->callee;
254
    if (!box) {
254
    if (!box) {
255
        if (phone->busy == IPC_BUSY_CONNECTING) {
255
        if (phone->busy == IPC_BUSY_CONNECTING) {
256
            spinlock_unlock(&phone->lock);
256
            spinlock_unlock(&phone->lock);
257
            return -1;
257
            return -1;
258
        }
258
        }
259
        /* Already disconnected phone */
259
        /* Already disconnected phone */
260
        phone->busy = IPC_BUSY_FREE;
260
        phone->busy = IPC_BUSY_FREE;
261
        spinlock_unlock(&phone->lock);
261
        spinlock_unlock(&phone->lock);
262
        return 0;
262
        return 0;
263
    }
263
    }
264
 
264
 
265
    spinlock_lock(&box->lock);
265
    spinlock_lock(&box->lock);
266
    list_remove(&phone->list);
266
    list_remove(&phone->list);
267
    phone->callee = NULL;
267
    phone->callee = NULL;
268
    spinlock_unlock(&box->lock);
268
    spinlock_unlock(&box->lock);
269
 
269
 
270
    call = ipc_call_alloc(0);
270
    call = ipc_call_alloc(0);
271
    IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
271
    IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
272
    call->flags |= IPC_CALL_DISCARD_ANSWER;
272
    call->flags |= IPC_CALL_DISCARD_ANSWER;
273
    _ipc_call(phone, box, call);
273
    _ipc_call(phone, box, call);
274
 
274
 
275
    phone->busy = IPC_BUSY_FREE;
275
    phone->busy = IPC_BUSY_FREE;
276
 
276
 
277
    spinlock_unlock(&phone->lock);
277
    spinlock_unlock(&phone->lock);
278
 
278
 
279
    return 0;
279
    return 0;
280
}
280
}
281
 
281
 
282
/** Forwards call from one answerbox to a new one
282
/** Forwards call from one answerbox to a new one
283
 *
283
 *
284
 * @param call Call to be redirected.
284
 * @param call Call to be redirected.
285
 * @param newphone Phone to target answerbox.
285
 * @param newphone Phone to target answerbox.
286
 * @param oldbox Old answerbox
286
 * @param oldbox Old answerbox
287
 * @return 0 on forward ok, error code, if there was error
287
 * @return 0 on forward ok, error code, if there was error
288
 *
288
 *
289
 * - the return value serves only as an information for the forwarder,
289
 * - the return value serves only as an information for the forwarder,
290
 *   the original caller is notified automatically with EFORWARD
290
 *   the original caller is notified automatically with EFORWARD
291
 */
291
 */
292
int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox)
292
int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox)
293
{
293
{
294
    spinlock_lock(&oldbox->lock);
294
    spinlock_lock(&oldbox->lock);
295
    list_remove(&call->list);
295
    list_remove(&call->list);
296
    spinlock_unlock(&oldbox->lock);
296
    spinlock_unlock(&oldbox->lock);
297
 
297
 
298
    return ipc_call(newphone, call);
298
    return ipc_call(newphone, call);
299
}
299
}
300
 
300
 
301
 
301
 
302
/** Wait for phone call
302
/** Wait for phone call
303
 *
303
 *
304
 * @return Recived message address
304
 * @return Recived message address
305
 * - to distinguish between call and answer, look at call->flags
305
 * - to distinguish between call and answer, look at call->flags
306
 */
306
 */
307
call_t * ipc_wait_for_call(answerbox_t *box, int flags)
307
call_t * ipc_wait_for_call(answerbox_t *box, int flags)
308
{
308
{
309
    call_t *request;
309
    call_t *request;
310
    ipl_t ipl;
310
    ipl_t ipl;
311
 
311
 
312
restart:      
312
restart:      
313
    if (flags & IPC_WAIT_NONBLOCKING) {
313
    if (flags & IPC_WAIT_NONBLOCKING) {
314
        if (waitq_sleep_timeout(&box->wq,0,1) == ESYNCH_WOULD_BLOCK)
314
        if (waitq_sleep_timeout(&box->wq,0,1) == ESYNCH_WOULD_BLOCK)
315
            return NULL;
315
            return NULL;
316
    } else
316
    } else
317
        waitq_sleep(&box->wq);
317
        waitq_sleep(&box->wq);
318
   
318
   
319
    spinlock_lock(&box->lock);
319
    spinlock_lock(&box->lock);
320
    if (!list_empty(&box->irq_notifs)) {
320
    if (!list_empty(&box->irq_notifs)) {
321
        ipl = interrupts_disable();
321
        ipl = interrupts_disable();
322
        spinlock_lock(&box->irq_lock);
322
        spinlock_lock(&box->irq_lock);
323
 
323
 
324
        request = list_get_instance(box->irq_notifs.next, call_t, list);
324
        request = list_get_instance(box->irq_notifs.next, call_t, list);
325
        list_remove(&request->list);
325
        list_remove(&request->list);
326
 
326
 
327
        spinlock_unlock(&box->irq_lock);
327
        spinlock_unlock(&box->irq_lock);
328
        interrupts_restore(ipl);
328
        interrupts_restore(ipl);
329
    } else if (!list_empty(&box->answers)) {
329
    } else if (!list_empty(&box->answers)) {
330
        /* Handle asynchronous answers */
330
        /* Handle asynchronous answers */
331
        request = list_get_instance(box->answers.next, call_t, list);
331
        request = list_get_instance(box->answers.next, call_t, list);
332
        list_remove(&request->list);
332
        list_remove(&request->list);
333
        atomic_dec(&request->data.phone->active_calls);
333
        atomic_dec(&request->data.phone->active_calls);
334
    } else if (!list_empty(&box->calls)) {
334
    } else if (!list_empty(&box->calls)) {
335
        /* Handle requests */
335
        /* Handle requests */
336
        request = list_get_instance(box->calls.next, call_t, list);
336
        request = list_get_instance(box->calls.next, call_t, list);
337
        list_remove(&request->list);
337
        list_remove(&request->list);
338
        /* Append request to dispatch queue */
338
        /* Append request to dispatch queue */
339
        list_append(&request->list, &box->dispatched_calls);
339
        list_append(&request->list, &box->dispatched_calls);
340
    } else {
340
    } else {
341
        /* This can happen regularly after ipc_cleanup, remove
341
        /* This can happen regularly after ipc_cleanup, remove
342
         * the warning in the future when the IPC is
342
         * the warning in the future when the IPC is
343
         * more debugged */
343
         * more debugged */
344
        printf("WARNING: Spurious IPC wakeup.\n");
344
        printf("WARNING: Spurious IPC wakeup.\n");
345
        spinlock_unlock(&box->lock);
345
        spinlock_unlock(&box->lock);
346
        goto restart;
346
        goto restart;
347
    }
347
    }
348
    spinlock_unlock(&box->lock);
348
    spinlock_unlock(&box->lock);
349
    return request;
349
    return request;
350
}
350
}
351
 
351
 
352
/** Answer all calls from list with EHANGUP msg */
352
/** Answer all calls from list with EHANGUP msg */
353
static void ipc_cleanup_call_list(link_t *lst)
353
static void ipc_cleanup_call_list(link_t *lst)
354
{
354
{
355
    call_t *call;
355
    call_t *call;
356
 
356
 
357
    while (!list_empty(lst)) {
357
    while (!list_empty(lst)) {
358
        call = list_get_instance(lst->next, call_t, list);
358
        call = list_get_instance(lst->next, call_t, list);
359
        list_remove(&call->list);
359
        list_remove(&call->list);
360
 
360
 
361
        IPC_SET_RETVAL(call->data, EHANGUP);
361
        IPC_SET_RETVAL(call->data, EHANGUP);
362
        _ipc_answer_free_call(call);
362
        _ipc_answer_free_call(call);
363
    }
363
    }
364
}
364
}
365
 
365
 
366
/** Cleans up all IPC communication of the given task
366
/** Cleans up all IPC communication of the given task
367
 *
367
 *
368
 *
368
 *
369
 */
369
 */
370
void ipc_cleanup(task_t *task)
370
void ipc_cleanup(task_t *task)
371
{
371
{
372
    int i;
372
    int i;
373
    call_t *call;
373
    call_t *call;
374
    phone_t *phone;
374
    phone_t *phone;
375
   
375
   
376
    /* Disconnect all our phones ('ipc_phone_hangup') */
376
    /* Disconnect all our phones ('ipc_phone_hangup') */
377
    for (i=0;i < IPC_MAX_PHONES; i++)
377
    for (i=0;i < IPC_MAX_PHONES; i++)
378
        ipc_phone_hangup(&task->phones[i]);
378
        ipc_phone_hangup(&task->phones[i]);
379
 
379
 
380
    /* Disconnect all connected irqs */
380
    /* Disconnect all connected irqs */
381
    ipc_irq_cleanup(&task->answerbox);
381
    ipc_irq_cleanup(&task->answerbox);
382
 
382
 
383
    /* Disconnect all phones connected to our answerbox */
383
    /* Disconnect all phones connected to our answerbox */
384
restart_phones:
384
restart_phones:
385
    spinlock_lock(&task->answerbox.lock);
385
    spinlock_lock(&task->answerbox.lock);
386
    while (!list_empty(&task->answerbox.connected_phones)) {
386
    while (!list_empty(&task->answerbox.connected_phones)) {
387
        phone = list_get_instance(task->answerbox.connected_phones.next,
387
        phone = list_get_instance(task->answerbox.connected_phones.next,
388
                      phone_t,
388
                      phone_t,
389
                      list);
389
                      list);
390
        if (! spinlock_trylock(&phone->lock)) {
390
        if (! spinlock_trylock(&phone->lock)) {
391
            spinlock_unlock(&task->answerbox.lock);
391
            spinlock_unlock(&task->answerbox.lock);
392
            goto restart_phones;
392
            goto restart_phones;
393
        }
393
        }
394
       
394
       
395
        /* Disconnect phone */
395
        /* Disconnect phone */
396
        phone->callee = NULL;
396
        phone->callee = NULL;
397
        list_remove(&phone->list);
397
        list_remove(&phone->list);
398
 
398
 
399
        spinlock_unlock(&phone->lock);
399
        spinlock_unlock(&phone->lock);
400
    }
400
    }
401
 
401
 
402
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
402
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
403
    spinlock_lock(&task->answerbox.lock);
403
    spinlock_lock(&task->answerbox.lock);
404
    ipc_cleanup_call_list(&task->answerbox.dispatched_calls);
404
    ipc_cleanup_call_list(&task->answerbox.dispatched_calls);
405
    ipc_cleanup_call_list(&task->answerbox.calls);
405
    ipc_cleanup_call_list(&task->answerbox.calls);
406
    spinlock_unlock(&task->answerbox.lock);
406
    spinlock_unlock(&task->answerbox.lock);
407
   
407
   
408
    /* Wait for all async answers to arrive */
408
    /* Wait for all async answers to arrive */
409
    while (atomic_get(&task->active_calls)) {
409
    while (atomic_get(&task->active_calls)) {
410
        call = ipc_wait_for_call(&task->answerbox, 0);
410
        call = ipc_wait_for_call(&task->answerbox, 0);
411
        ASSERT((call->flags & IPC_CALL_ANSWERED) || (call->flags & IPC_CALL_NOTIF));
411
        ASSERT((call->flags & IPC_CALL_ANSWERED) || (call->flags & IPC_CALL_NOTIF));
412
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
412
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
413
       
413
       
414
        atomic_dec(&task->active_calls);
414
        atomic_dec(&task->active_calls);
415
        ipc_call_free(call);
415
        ipc_call_free(call);
416
    }
416
    }
417
}
417
}
418
 
418
 
419
 
419
 
420
/** Initilize ipc subsystem */
420
/** Initilize ipc subsystem */
421
void ipc_init(void)
421
void ipc_init(void)
422
{
422
{
423
    ipc_call_slab = slab_cache_create("ipc_call",
423
    ipc_call_slab = slab_cache_create("ipc_call",
424
                      sizeof(call_t),
424
                      sizeof(call_t),
425
                      0,
425
                      0,
426
                      NULL, NULL, 0);
426
                      NULL, NULL, 0);
427
    ipc_irq_make_table(IRQ_COUNT);
427
    ipc_irq_make_table(IRQ_COUNT);
428
}
428
}
429
 
429
 
430
 
430