Subversion Repositories HelenOS-historic

Rev

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

Rev 1223 Rev 1258
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
 
47
 
47
/* Open channel that is assigned automatically to new tasks */
48
/* Open channel that is assigned automatically to new tasks */
48
answerbox_t *ipc_phone_0 = NULL;
49
answerbox_t *ipc_phone_0 = NULL;
49
 
50
 
50
static slab_cache_t *ipc_call_slab;
51
static slab_cache_t *ipc_call_slab;
51
 
52
 
-
 
53
typedef struct {
-
 
54
    SPINLOCK_DECLARE(lock);
-
 
55
    answerbox_t *box;
-
 
56
} ipc_irq_t;
-
 
57
 
-
 
58
static ipc_irq_t *irq_conns = NULL;
-
 
59
static int irq_conns_size;
-
 
60
 
52
/* Initialize new call */
61
/* Initialize new call */
53
static void _ipc_call_init(call_t *call)
62
static void _ipc_call_init(call_t *call)
54
{
63
{
55
    memsetb((__address)call, sizeof(*call), 0);
64
    memsetb((__address)call, sizeof(*call), 0);
56
    call->callerbox = &TASK->answerbox;
65
    call->callerbox = &TASK->answerbox;
57
    call->sender = TASK;
66
    call->sender = TASK;
58
}
67
}
59
 
68
 
60
/** Allocate & initialize call structure
69
/** Allocate & initialize call structure
61
 *
70
 *
62
 * The call is initialized, so that the reply will be directed
71
 * The call is initialized, so that the reply will be directed
63
 * to TASK->answerbox
72
 * to TASK->answerbox
-
 
73
 *
-
 
74
 * @param flags Parameters for slab_alloc (ATOMIC, etc.)
64
 */
75
 */
65
call_t * ipc_call_alloc(void)
76
call_t * ipc_call_alloc(int flags)
66
{
77
{
67
    call_t *call;
78
    call_t *call;
68
 
79
 
69
    call = slab_alloc(ipc_call_slab, 0);
80
    call = slab_alloc(ipc_call_slab, flags);
70
    _ipc_call_init(call);
81
    _ipc_call_init(call);
71
 
82
 
72
    return call;
83
    return call;
73
}
84
}
74
 
85
 
75
/** Initialize allocated call */
86
/** Initialize allocated call */
76
void ipc_call_static_init(call_t *call)
87
void ipc_call_static_init(call_t *call)
77
{
88
{
78
    _ipc_call_init(call);
89
    _ipc_call_init(call);
79
    call->flags |= IPC_CALL_STATIC_ALLOC;
90
    call->flags |= IPC_CALL_STATIC_ALLOC;
80
}
91
}
81
 
92
 
82
/** Deallocate call stracuture */
93
/** Deallocate call stracuture */
83
void ipc_call_free(call_t *call)
94
void ipc_call_free(call_t *call)
84
{
95
{
85
    slab_free(ipc_call_slab, call);
96
    slab_free(ipc_call_slab, call);
86
}
97
}
87
 
98
 
88
/** Initialize answerbox structure
99
/** Initialize answerbox structure
89
 */
100
 */
90
void ipc_answerbox_init(answerbox_t *box)
101
void ipc_answerbox_init(answerbox_t *box)
91
{
102
{
92
    spinlock_initialize(&box->lock, "ipc_box_lock");
103
    spinlock_initialize(&box->lock, "ipc_box_lock");
-
 
104
    spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
93
    waitq_initialize(&box->wq);
105
    waitq_initialize(&box->wq);
94
    list_initialize(&box->connected_phones);
106
    list_initialize(&box->connected_phones);
95
    list_initialize(&box->calls);
107
    list_initialize(&box->calls);
96
    list_initialize(&box->dispatched_calls);
108
    list_initialize(&box->dispatched_calls);
97
    list_initialize(&box->answers);
109
    list_initialize(&box->answers);
-
 
110
    list_initialize(&box->irq_notifs);
98
    box->task = TASK;
111
    box->task = TASK;
99
}
112
}
100
 
113
 
101
/** Connect phone to answerbox */
114
/** Connect phone to answerbox */
102
void ipc_phone_connect(phone_t *phone, answerbox_t *box)
115
void ipc_phone_connect(phone_t *phone, answerbox_t *box)
103
{
116
{
104
    spinlock_lock(&phone->lock);
117
    spinlock_lock(&phone->lock);
105
 
118
 
106
    ASSERT(!phone->callee);
119
    ASSERT(!phone->callee);
107
    phone->busy = IPC_BUSY_CONNECTED;
120
    phone->busy = IPC_BUSY_CONNECTED;
108
    phone->callee = box;
121
    phone->callee = box;
109
 
122
 
110
    spinlock_lock(&box->lock);
123
    spinlock_lock(&box->lock);
111
    list_append(&phone->list, &box->connected_phones);
124
    list_append(&phone->list, &box->connected_phones);
112
    spinlock_unlock(&box->lock);
125
    spinlock_unlock(&box->lock);
113
 
126
 
114
    spinlock_unlock(&phone->lock);
127
    spinlock_unlock(&phone->lock);
115
}
128
}
116
 
129
 
117
/** Initialize phone structure and connect phone to answerbox
130
/** Initialize phone structure and connect phone to answerbox
118
 */
131
 */
119
void ipc_phone_init(phone_t *phone)
132
void ipc_phone_init(phone_t *phone)
120
{
133
{
121
    spinlock_initialize(&phone->lock, "phone_lock");
134
    spinlock_initialize(&phone->lock, "phone_lock");
122
    phone->callee = NULL;
135
    phone->callee = NULL;
123
    phone->busy = IPC_BUSY_FREE;
136
    phone->busy = IPC_BUSY_FREE;
124
    atomic_set(&phone->active_calls, 0);
137
    atomic_set(&phone->active_calls, 0);
125
}
138
}
126
 
139
 
127
/** Helper function to facilitate synchronous calls */
140
/** Helper function to facilitate synchronous calls */
128
void ipc_call_sync(phone_t *phone, call_t *request)
141
void ipc_call_sync(phone_t *phone, call_t *request)
129
{
142
{
130
    answerbox_t sync_box;
143
    answerbox_t sync_box;
131
 
144
 
132
    ipc_answerbox_init(&sync_box);
145
    ipc_answerbox_init(&sync_box);
133
 
146
 
134
    /* We will receive data on special box */
147
    /* We will receive data on special box */
135
    request->callerbox = &sync_box;
148
    request->callerbox = &sync_box;
136
 
149
 
137
    ipc_call(phone, request);
150
    ipc_call(phone, request);
138
    ipc_wait_for_call(&sync_box, 0);
151
    ipc_wait_for_call(&sync_box, 0);
139
}
152
}
140
 
153
 
141
/** Answer message that was not dispatched and is not entered in
154
/** Answer message that was not dispatched and is not entered in
142
 * any queue
155
 * any queue
143
 */
156
 */
144
static void _ipc_answer_free_call(call_t *call)
157
static void _ipc_answer_free_call(call_t *call)
145
{
158
{
146
    answerbox_t *callerbox = call->callerbox;
159
    answerbox_t *callerbox = call->callerbox;
147
 
160
 
148
    call->flags |= IPC_CALL_ANSWERED;
161
    call->flags |= IPC_CALL_ANSWERED;
149
 
162
 
150
    spinlock_lock(&callerbox->lock);
163
    spinlock_lock(&callerbox->lock);
151
    list_append(&call->list, &callerbox->answers);
164
    list_append(&call->list, &callerbox->answers);
152
    spinlock_unlock(&callerbox->lock);
165
    spinlock_unlock(&callerbox->lock);
153
    waitq_wakeup(&callerbox->wq, 0);
166
    waitq_wakeup(&callerbox->wq, 0);
154
}
167
}
155
 
168
 
156
/** Answer message, that is in callee queue
169
/** Answer message, that is in callee queue
157
 *
170
 *
158
 * @param box Answerbox that is answering the message
171
 * @param box Answerbox that is answering the message
159
 * @param call Modified request that is being sent back
172
 * @param call Modified request that is being sent back
160
 */
173
 */
161
void ipc_answer(answerbox_t *box, call_t *call)
174
void ipc_answer(answerbox_t *box, call_t *call)
162
{
175
{
163
    /* Remove from active box */
176
    /* Remove from active box */
164
    spinlock_lock(&box->lock);
177
    spinlock_lock(&box->lock);
165
    list_remove(&call->list);
178
    list_remove(&call->list);
166
    spinlock_unlock(&box->lock);
179
    spinlock_unlock(&box->lock);
167
    /* Send back answer */
180
    /* Send back answer */
168
    _ipc_answer_free_call(call);
181
    _ipc_answer_free_call(call);
169
}
182
}
170
 
183
 
171
/** Simulate sending back a message
184
/** Simulate sending back a message
172
 *
185
 *
173
 * Most errors are better handled by forming a normal backward
186
 * Most errors are better handled by forming a normal backward
174
 * message and sending it as a normal answer.
187
 * message and sending it as a normal answer.
175
 */
188
 */
176
void ipc_backsend_err(phone_t *phone, call_t *call, __native err)
189
void ipc_backsend_err(phone_t *phone, call_t *call, __native err)
177
{
190
{
178
    call->data.phone = phone;
191
    call->data.phone = phone;
179
    atomic_inc(&phone->active_calls);
192
    atomic_inc(&phone->active_calls);
180
    if (phone->busy == IPC_BUSY_CONNECTED)
193
    if (phone->busy == IPC_BUSY_CONNECTED)
181
        IPC_SET_RETVAL(call->data, EHANGUP);
194
        IPC_SET_RETVAL(call->data, EHANGUP);
182
    else
195
    else
183
        IPC_SET_RETVAL(call->data, ENOENT);
196
        IPC_SET_RETVAL(call->data, ENOENT);
184
 
197
 
185
    _ipc_answer_free_call(call);
198
    _ipc_answer_free_call(call);
186
}
199
}
187
 
200
 
188
/* Unsafe unchecking ipc_call */
201
/* Unsafe unchecking ipc_call */
189
static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
202
static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
190
{
203
{
191
    if (! (call->flags & IPC_CALL_FORWARDED)) {
204
    if (! (call->flags & IPC_CALL_FORWARDED)) {
192
        atomic_inc(&phone->active_calls);
205
        atomic_inc(&phone->active_calls);
193
        call->data.phone = phone;
206
        call->data.phone = phone;
194
    }
207
    }
195
 
208
 
196
    spinlock_lock(&box->lock);
209
    spinlock_lock(&box->lock);
197
    list_append(&call->list, &box->calls);
210
    list_append(&call->list, &box->calls);
198
    spinlock_unlock(&box->lock);
211
    spinlock_unlock(&box->lock);
199
    waitq_wakeup(&box->wq, 0);
212
    waitq_wakeup(&box->wq, 0);
200
}
213
}
201
 
214
 
202
/** Send a asynchronous request using phone to answerbox
215
/** Send a asynchronous request using phone to answerbox
203
 *
216
 *
204
 * @param phone Phone connected to answerbox
217
 * @param phone Phone connected to answerbox
205
 * @param request Request to be sent
218
 * @param request Request to be sent
206
 */
219
 */
207
int ipc_call(phone_t *phone, call_t *call)
220
int ipc_call(phone_t *phone, call_t *call)
208
{
221
{
209
    answerbox_t *box;
222
    answerbox_t *box;
210
 
223
 
211
    spinlock_lock(&phone->lock);
224
    spinlock_lock(&phone->lock);
212
 
225
 
213
    box = phone->callee;
226
    box = phone->callee;
214
    if (!box) {
227
    if (!box) {
215
        /* Trying to send over disconnected phone */
228
        /* Trying to send over disconnected phone */
216
        spinlock_unlock(&phone->lock);
229
        spinlock_unlock(&phone->lock);
217
        if (call->flags & IPC_CALL_FORWARDED) {
230
        if (call->flags & IPC_CALL_FORWARDED) {
218
            IPC_SET_RETVAL(call->data, EFORWARD);
231
            IPC_SET_RETVAL(call->data, EFORWARD);
219
            _ipc_answer_free_call(call);
232
            _ipc_answer_free_call(call);
220
        } else { /* Simulate sending back a message */
233
        } else { /* Simulate sending back a message */
221
            if (phone->busy == IPC_BUSY_CONNECTED)
234
            if (phone->busy == IPC_BUSY_CONNECTED)
222
                ipc_backsend_err(phone, call, EHANGUP);
235
                ipc_backsend_err(phone, call, EHANGUP);
223
            else
236
            else
224
                ipc_backsend_err(phone, call, ENOENT);
237
                ipc_backsend_err(phone, call, ENOENT);
225
        }
238
        }
226
 
239
 
227
        return ENOENT;
240
        return ENOENT;
228
    }
241
    }
229
    _ipc_call(phone, box, call);
242
    _ipc_call(phone, box, call);
230
   
243
   
231
    spinlock_unlock(&phone->lock);
244
    spinlock_unlock(&phone->lock);
232
    return 0;
245
    return 0;
233
}
246
}
234
 
247
 
235
/** Disconnect phone from answerbox
248
/** Disconnect phone from answerbox
236
 *
249
 *
237
 * It is allowed to call disconnect on already disconnected phone
250
 * It is allowed to call disconnect on already disconnected phone
238
 *
251
 *
239
 * @return 0 - phone disconnected, -1 - the phone was already disconnected
252
 * @return 0 - phone disconnected, -1 - the phone was already disconnected
240
 */
253
 */
241
int ipc_phone_hangup(phone_t *phone)
254
int ipc_phone_hangup(phone_t *phone)
242
{
255
{
243
    answerbox_t *box;
256
    answerbox_t *box;
244
    call_t *call;
257
    call_t *call;
245
   
258
   
246
    spinlock_lock(&phone->lock);
259
    spinlock_lock(&phone->lock);
247
    box = phone->callee;
260
    box = phone->callee;
248
    if (!box) {
261
    if (!box) {
249
        if (phone->busy == IPC_BUSY_CONNECTING) {
262
        if (phone->busy == IPC_BUSY_CONNECTING) {
250
            spinlock_unlock(&phone->lock);
263
            spinlock_unlock(&phone->lock);
251
            return -1;
264
            return -1;
252
        }
265
        }
253
        /* Already disconnected phone */
266
        /* Already disconnected phone */
254
        phone->busy = IPC_BUSY_FREE;
267
        phone->busy = IPC_BUSY_FREE;
255
        spinlock_unlock(&phone->lock);
268
        spinlock_unlock(&phone->lock);
256
        return 0;
269
        return 0;
257
    }
270
    }
258
 
271
 
259
    spinlock_lock(&box->lock);
272
    spinlock_lock(&box->lock);
260
    list_remove(&phone->list);
273
    list_remove(&phone->list);
261
    phone->callee = NULL;
274
    phone->callee = NULL;
262
    spinlock_unlock(&box->lock);
275
    spinlock_unlock(&box->lock);
263
 
276
 
264
    call = ipc_call_alloc();
277
    call = ipc_call_alloc(0);
265
    IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
278
    IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
266
    call->flags |= IPC_CALL_DISCARD_ANSWER;
279
    call->flags |= IPC_CALL_DISCARD_ANSWER;
267
    _ipc_call(phone, box, call);
280
    _ipc_call(phone, box, call);
268
 
281
 
269
    phone->busy = IPC_BUSY_FREE;
282
    phone->busy = IPC_BUSY_FREE;
270
 
283
 
271
    spinlock_unlock(&phone->lock);
284
    spinlock_unlock(&phone->lock);
272
 
285
 
273
    return 0;
286
    return 0;
274
}
287
}
275
 
288
 
276
/** Forwards call from one answerbox to a new one
289
/** Forwards call from one answerbox to a new one
277
 *
290
 *
278
 * @param request Request to be forwarded
291
 * @param request Request to be forwarded
279
 * @param newbox Target answerbox
292
 * @param newbox Target answerbox
280
 * @param oldbox Old answerbox
293
 * @param oldbox Old answerbox
281
 * @return 0 on forward ok, error code, if there was error
294
 * @return 0 on forward ok, error code, if there was error
282
 *
295
 *
283
 * - the return value serves only as an information for the forwarder,
296
 * - the return value serves only as an information for the forwarder,
284
 *   the original caller is notified automatically with EFORWARD
297
 *   the original caller is notified automatically with EFORWARD
285
 */
298
 */
286
int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox)
299
int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox)
287
{
300
{
288
    spinlock_lock(&oldbox->lock);
301
    spinlock_lock(&oldbox->lock);
289
    list_remove(&call->list);
302
    list_remove(&call->list);
290
    spinlock_unlock(&oldbox->lock);
303
    spinlock_unlock(&oldbox->lock);
291
 
304
 
292
    return ipc_call(newphone, call);
305
    return ipc_call(newphone, call);
293
}
306
}
294
 
307
 
295
 
308
 
296
/** Wait for phone call
309
/** Wait for phone call
297
 *
310
 *
298
 * @return Recived message address
311
 * @return Recived message address
299
 * - to distinguish between call and answer, look at call->flags
312
 * - to distinguish between call and answer, look at call->flags
300
 */
313
 */
301
call_t * ipc_wait_for_call(answerbox_t *box, int flags)
314
call_t * ipc_wait_for_call(answerbox_t *box, int flags)
302
{
315
{
303
    call_t *request;
316
    call_t *request;
-
 
317
    ipl_t ipl;
304
 
318
 
305
restart:      
319
restart:      
306
    if (flags & IPC_WAIT_NONBLOCKING) {
320
    if (flags & IPC_WAIT_NONBLOCKING) {
307
        if (waitq_sleep_timeout(&box->wq,0,1) == ESYNCH_WOULD_BLOCK)
321
        if (waitq_sleep_timeout(&box->wq,0,1) == ESYNCH_WOULD_BLOCK)
308
            return NULL;
322
            return NULL;
309
    } else
323
    } else
310
        waitq_sleep(&box->wq);
324
        waitq_sleep(&box->wq);
311
   
325
   
312
    spinlock_lock(&box->lock);
326
    spinlock_lock(&box->lock);
-
 
327
    if (!list_empty(&box->irq_notifs)) {
-
 
328
        ipl = interrupts_disable();
-
 
329
        spinlock_lock(&box->irq_lock);
-
 
330
 
-
 
331
        request = list_get_instance(box->answers.next, call_t, list);
-
 
332
        list_remove(&request->list);
-
 
333
 
-
 
334
        spinlock_unlock(&box->irq_lock);
-
 
335
        interrupts_restore(ipl);
313
    if (!list_empty(&box->answers)) {
336
    } else if (!list_empty(&box->answers)) {
314
        /* Handle asynchronous answers */
337
        /* Handle asynchronous answers */
315
        request = list_get_instance(box->answers.next, call_t, list);
338
        request = list_get_instance(box->answers.next, call_t, list);
316
        list_remove(&request->list);
339
        list_remove(&request->list);
317
        atomic_dec(&request->data.phone->active_calls);
340
        atomic_dec(&request->data.phone->active_calls);
318
    } else if (!list_empty(&box->calls)) {
341
    } else if (!list_empty(&box->calls)) {
319
        /* Handle requests */
342
        /* Handle requests */
320
        request = list_get_instance(box->calls.next, call_t, list);
343
        request = list_get_instance(box->calls.next, call_t, list);
321
        list_remove(&request->list);
344
        list_remove(&request->list);
322
        /* Append request to dispatch queue */
345
        /* Append request to dispatch queue */
323
        list_append(&request->list, &box->dispatched_calls);
346
        list_append(&request->list, &box->dispatched_calls);
324
    } else {
347
    } else {
325
        /* This can happen regularly after ipc_cleanup, remove
348
        /* This can happen regularly after ipc_cleanup, remove
326
         * the warning in the future when the IPC is
349
         * the warning in the future when the IPC is
327
         * more debugged */
350
         * more debugged */
328
        printf("WARNING: Spurious IPC wakeup.\n");
351
        printf("WARNING: Spurious IPC wakeup.\n");
329
        spinlock_unlock(&box->lock);
352
        spinlock_unlock(&box->lock);
330
        goto restart;
353
        goto restart;
331
    }
354
    }
332
    spinlock_unlock(&box->lock);
355
    spinlock_unlock(&box->lock);
333
    return request;
356
    return request;
334
}
357
}
335
 
358
 
336
/** Initilize ipc subsystem */
-
 
337
void ipc_init(void)
-
 
338
{
-
 
339
    ipc_call_slab = slab_cache_create("ipc_call",
-
 
340
                      sizeof(call_t),
-
 
341
                      0,
-
 
342
                      NULL, NULL, 0);
-
 
343
}
-
 
344
 
-
 
345
/** Answer all calls from list with EHANGUP msg */
359
/** Answer all calls from list with EHANGUP msg */
346
static void ipc_cleanup_call_list(link_t *lst)
360
static void ipc_cleanup_call_list(link_t *lst)
347
{
361
{
348
    call_t *call;
362
    call_t *call;
349
 
363
 
350
    while (!list_empty(lst)) {
364
    while (!list_empty(lst)) {
351
        call = list_get_instance(lst->next, call_t, list);
365
        call = list_get_instance(lst->next, call_t, list);
352
        list_remove(&call->list);
366
        list_remove(&call->list);
353
 
367
 
354
        IPC_SET_RETVAL(call->data, EHANGUP);
368
        IPC_SET_RETVAL(call->data, EHANGUP);
355
        _ipc_answer_free_call(call);
369
        _ipc_answer_free_call(call);
356
    }
370
    }
357
}
371
}
358
 
372
 
-
 
373
/** Disconnect all irq's notifications
-
 
374
 *
-
 
375
 * TODO: It may be better to do some linked list, so that
-
 
376
 *       we wouldn't need to go through whole array every cleanup
-
 
377
 */
-
 
378
static void ipc_irq_cleanup(answerbox_t *box)
-
 
379
{
-
 
380
    int i;
-
 
381
    ipl_t ipl;
-
 
382
   
-
 
383
    for (i=0; i < irq_conns_size; i++) {
-
 
384
        ipl = interrupts_disable();
-
 
385
        spinlock_lock(&irq_conns[i].lock);
-
 
386
        if (irq_conns[i].box == box)
-
 
387
            irq_conns[i].box = NULL;
-
 
388
        spinlock_unlock(&irq_conns[i].lock);
-
 
389
        interrupts_restore(ipl);
-
 
390
    }
-
 
391
}
-
 
392
 
359
/** Cleans up all IPC communication of the given task
393
/** Cleans up all IPC communication of the given task
360
 *
394
 *
361
 *
395
 *
362
 */
396
 */
363
void ipc_cleanup(task_t *task)
397
void ipc_cleanup(task_t *task)
364
{
398
{
365
    int i;
399
    int i;
366
    call_t *call;
400
    call_t *call;
367
    phone_t *phone;
401
    phone_t *phone;
368
   
402
   
369
    /* Disconnect all our phones ('ipc_phone_hangup') */
403
    /* Disconnect all our phones ('ipc_phone_hangup') */
370
    for (i=0;i < IPC_MAX_PHONES; i++)
404
    for (i=0;i < IPC_MAX_PHONES; i++)
371
        ipc_phone_hangup(&task->phones[i]);
405
        ipc_phone_hangup(&task->phones[i]);
372
 
406
 
-
 
407
    /* Disconnect all connected irqs */
-
 
408
    ipc_irq_cleanup(&task->answerbox);
-
 
409
 
373
    /* Disconnect all phones connected to our answerbox */
410
    /* Disconnect all phones connected to our answerbox */
374
restart_phones:
411
restart_phones:
375
    spinlock_lock(&task->answerbox.lock);
412
    spinlock_lock(&task->answerbox.lock);
376
    while (!list_empty(&task->answerbox.connected_phones)) {
413
    while (!list_empty(&task->answerbox.connected_phones)) {
377
        phone = list_get_instance(task->answerbox.connected_phones.next,
414
        phone = list_get_instance(task->answerbox.connected_phones.next,
378
                      phone_t,
415
                      phone_t,
379
                      list);
416
                      list);
380
        if (! spinlock_trylock(&phone->lock)) {
417
        if (! spinlock_trylock(&phone->lock)) {
381
            spinlock_unlock(&task->answerbox.lock);
418
            spinlock_unlock(&task->answerbox.lock);
382
            goto restart_phones;
419
            goto restart_phones;
383
        }
420
        }
384
       
421
       
385
        /* Disconnect phone */
422
        /* Disconnect phone */
386
        phone->callee = NULL;
423
        phone->callee = NULL;
387
        list_remove(&phone->list);
424
        list_remove(&phone->list);
388
 
425
 
389
        spinlock_unlock(&phone->lock);
426
        spinlock_unlock(&phone->lock);
390
    }
427
    }
391
 
428
 
392
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
429
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
393
    spinlock_lock(&task->answerbox.lock);
430
    spinlock_lock(&task->answerbox.lock);
394
    ipc_cleanup_call_list(&task->answerbox.dispatched_calls);
431
    ipc_cleanup_call_list(&task->answerbox.dispatched_calls);
395
    ipc_cleanup_call_list(&task->answerbox.calls);
432
    ipc_cleanup_call_list(&task->answerbox.calls);
396
    spinlock_unlock(&task->answerbox.lock);
433
    spinlock_unlock(&task->answerbox.lock);
397
   
434
   
398
    /* Wait for all async answers to arrive */
435
    /* Wait for all async answers to arrive */
399
    while (atomic_get(&task->active_calls)) {
436
    while (atomic_get(&task->active_calls)) {
400
        call = ipc_wait_for_call(&task->answerbox, 0);
437
        call = ipc_wait_for_call(&task->answerbox, 0);
401
        ASSERT(call->flags & IPC_CALL_ANSWERED);
438
        ASSERT(call->flags & IPC_CALL_ANSWERED);
402
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
439
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
403
       
440
       
404
        atomic_dec(&task->active_calls);
441
        atomic_dec(&task->active_calls);
405
        ipc_call_free(call);
442
        ipc_call_free(call);
406
    }
443
    }
407
}
444
}
-
 
445
 
-
 
446
/** Initialize table of interrupt handlers */
-
 
447
static void ipc_irq_make_table(int irqcount)
-
 
448
{
-
 
449
    int i;
-
 
450
 
-
 
451
    irq_conns_size = irqcount;
-
 
452
    irq_conns = malloc(irqcount * (sizeof(*irq_conns)), 0);
-
 
453
    for (i=0; i < irqcount; i++) {
-
 
454
        spinlock_initialize(&irq_conns[i].lock, "irq_ipc_lock");
-
 
455
        irq_conns[i].box = NULL;
-
 
456
    }
-
 
457
}
-
 
458
 
-
 
459
void ipc_irq_unregister(answerbox_t *box, int irq)
-
 
460
{
-
 
461
    ipl_t ipl;
-
 
462
 
-
 
463
    ipl = interrupts_disable();
-
 
464
    spinlock_lock(&irq_conns[irq].lock);
-
 
465
    if (irq_conns[irq].box == box)
-
 
466
        irq_conns[irq].box = NULL;
-
 
467
 
-
 
468
    spinlock_unlock(&irq_conns[irq].lock);
-
 
469
    interrupts_restore(ipl);
-
 
470
}
-
 
471
 
-
 
472
/** Register an answerbox as a receiving end of interrupts notifications */
-
 
473
int ipc_irq_register(answerbox_t *box, int irq)
-
 
474
{
-
 
475
    ipl_t ipl;
-
 
476
 
-
 
477
    ASSERT(irq_conns);
-
 
478
 
-
 
479
    ipl = interrupts_disable();
-
 
480
    spinlock_lock(&irq_conns[irq].lock);
-
 
481
 
-
 
482
    if (irq_conns[irq].box) {
-
 
483
        spinlock_unlock(&irq_conns[irq].lock);
-
 
484
        interrupts_restore(ipl);
-
 
485
        return EEXISTS;
-
 
486
    }
-
 
487
    irq_conns[irq].box = box;
-
 
488
    spinlock_unlock(&irq_conns[irq].lock);
-
 
489
    interrupts_restore(ipl);
-
 
490
 
-
 
491
    return 0;
-
 
492
}
-
 
493
 
-
 
494
/** Notify process that an irq had happend
-
 
495
 *
-
 
496
 * We expect interrupts to be disabled
-
 
497
 */
-
 
498
void ipc_irq_send_notif(int irq)
-
 
499
{
-
 
500
    call_t *call;
-
 
501
 
-
 
502
    ASSERT(irq_conns);
-
 
503
    spinlock_lock(&irq_conns[irq].lock);
-
 
504
 
-
 
505
    if (irq_conns[irq].box) {
-
 
506
        call = ipc_call_alloc(FRAME_ATOMIC);
-
 
507
        call->flags |= IPC_CALL_NOTIF;
-
 
508
        IPC_SET_METHOD(call->data, IPC_M_INTERRUPT);
-
 
509
        IPC_SET_ARG1(call->data, irq);
-
 
510
 
-
 
511
        spinlock_lock(&irq_conns[irq].box->irq_lock);
-
 
512
        list_append(&call->list, &irq_conns[irq].box->irq_notifs);
-
 
513
        spinlock_unlock(&irq_conns[irq].box->irq_lock);
-
 
514
 
-
 
515
        waitq_wakeup(&irq_conns[irq].box->wq, 0);
-
 
516
    }
-
 
517
       
-
 
518
    spinlock_unlock(&irq_conns[irq].lock);
-
 
519
}
-
 
520
 
-
 
521
/** Initilize ipc subsystem */
-
 
522
void ipc_init(void)
-
 
523
{
-
 
524
    ipc_call_slab = slab_cache_create("ipc_call",
-
 
525
                      sizeof(call_t),
-
 
526
                      0,
-
 
527
                      NULL, NULL, 0);
-
 
528
    ipc_irq_make_table(IRQ_COUNT);
-
 
529
}
-
 
530
 
408
 
531