Subversion Repositories HelenOS-historic

Rev

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

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