Subversion Repositories HelenOS

Rev

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

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