Subversion Repositories HelenOS-historic

Rev

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

Rev 1698 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
-
 
30
 * @{
-
 
31
 */
-
 
32
/** @file
-
 
33
 */
-
 
34
 
29
/* IPC resources management
35
/* IPC resources management
30
 *
36
 *
31
 * The goal of this source code is to properly manage IPC resources
37
 * The goal of this source code is to properly manage IPC resources
32
 * and allow straight and clean clean-up procedure upon task termination.
38
 * and allow straight and clean clean-up procedure upon task termination.
33
 *
39
 *
34
 * The pattern of usage of the resources is:
40
 * The pattern of usage of the resources is:
35
 * - allocate empty phone slot, connect | deallocate slot
41
 * - allocate empty phone slot, connect | deallocate slot
36
 * - disconnect connected phone (some messages might be on the fly)
42
 * - disconnect connected phone (some messages might be on the fly)
37
 * - find phone in slot and send a message using phone
43
 * - find phone in slot and send a message using phone
38
 * - answer message to phone
44
 * - answer message to phone
39
 * - hangup phone (the caller has hung up)
45
 * - hangup phone (the caller has hung up)
40
 * - hangup phone (the answerbox is exiting)
46
 * - hangup phone (the answerbox is exiting)
41
 *
47
 *
42
 * Locking strategy
48
 * Locking strategy
43
 *
49
 *
44
 * - To use a phone, disconnect a phone etc., the phone must be
50
 * - To use a phone, disconnect a phone etc., the phone must be
45
 *   first locked and then checked that it is connected
51
 *   first locked and then checked that it is connected
46
 * - To connect an allocated phone it need not be locked (assigning
52
 * - To connect an allocated phone it need not be locked (assigning
47
 *   pointer is atomic on all platforms)
53
 *   pointer is atomic on all platforms)
48
 *
54
 *
49
 * - To find an empty phone slot, the TASK must be locked
55
 * - To find an empty phone slot, the TASK must be locked
50
 * - To answer a message, the answerbox must be locked
56
 * - To answer a message, the answerbox must be locked
51
 * - The locking of phone and answerbox is done at the ipc_ level.
57
 * - The locking of phone and answerbox is done at the ipc_ level.
52
 *   It is perfectly correct to pass unconnected phone to these functions
58
 *   It is perfectly correct to pass unconnected phone to these functions
53
 *   and proper reply will be generated.
59
 *   and proper reply will be generated.
54
 *
60
 *
55
 * Locking order
61
 * Locking order
56
 *
62
 *
57
 * - first phone, then answerbox
63
 * - first phone, then answerbox
58
 *   + Easy locking on calls
64
 *   + Easy locking on calls
59
 *   - Very hard traversing list of phones when disconnecting because
65
 *   - Very hard traversing list of phones when disconnecting because
60
 *     the phones may disconnect during traversal of list of connected phones.
66
 *     the phones may disconnect during traversal of list of connected phones.
61
 *     The only possibility is try_lock with restart of list traversal.
67
 *     The only possibility is try_lock with restart of list traversal.
62
 *
68
 *
63
 * Destroying is less frequent, this approach is taken.
69
 * Destroying is less frequent, this approach is taken.
64
 *
70
 *
65
 * Phone call
71
 * Phone call
66
 *
72
 *
67
 * *** Connect_me_to ***
73
 * *** Connect_me_to ***
68
 * The caller sends IPC_M_CONNECT_ME_TO to an answerbox. The server
74
 * The caller sends IPC_M_CONNECT_ME_TO to an answerbox. The server
69
 * receives 'phoneid' of the connecting phone as an ARG3. If it answers
75
 * receives 'phoneid' of the connecting phone as an ARG3. If it answers
70
 * with RETVAL=0, the phonecall is accepted, otherwise it is refused.
76
 * with RETVAL=0, the phonecall is accepted, otherwise it is refused.
71
 *
77
 *
72
 * *** Connect_to_me ***
78
 * *** Connect_to_me ***
73
 * The caller sends IPC_M_CONNECT_TO_ME, with special
79
 * The caller sends IPC_M_CONNECT_TO_ME, with special
74
 * The server receives an automatically
80
 * The server receives an automatically
75
 * opened phoneid. If it accepts (RETVAL=0), it can use the phoneid
81
 * opened phoneid. If it accepts (RETVAL=0), it can use the phoneid
76
 * immediately.
82
 * immediately.
77
 * Possible race condition can arise, when the client receives messages
83
 * Possible race condition can arise, when the client receives messages
78
 * from new connection before getting response for connect_to_me message.
84
 * from new connection before getting response for connect_to_me message.
79
 * Userspace should implement handshake protocol that would control it.
85
 * Userspace should implement handshake protocol that would control it.
80
 *
86
 *
81
 * Phone hangup
87
 * Phone hangup
82
 *
88
 *
83
 * *** The caller hangs up (sys_ipc_hangup) ***
89
 * *** The caller hangs up (sys_ipc_hangup) ***
84
 * - The phone is disconnected (no more messages can be sent over this phone),
90
 * - The phone is disconnected (no more messages can be sent over this phone),
85
 *   all in-progress messages are correctly handled. The anwerbox receives
91
 *   all in-progress messages are correctly handled. The anwerbox receives
86
 *   IPC_M_PHONE_HUNGUP call from the phone that hung up. When all async
92
 *   IPC_M_PHONE_HUNGUP call from the phone that hung up. When all async
87
 *   calls are answered, the phone is deallocated.
93
 *   calls are answered, the phone is deallocated.
88
 *
94
 *
89
 * *** The answerbox hangs up (ipc_answer(EHANGUP))
95
 * *** The answerbox hangs up (ipc_answer(EHANGUP))
90
 * - The phone is disconnected. EHANGUP response code is sent
96
 * - The phone is disconnected. EHANGUP response code is sent
91
 *   to the calling task. All new calls through this phone
97
 *   to the calling task. All new calls through this phone
92
 *   get a EHUNGUP error code, the task is expected to
98
 *   get a EHUNGUP error code, the task is expected to
93
 *   send an sys_ipc_hangup after cleaning up it's internal structures.
99
 *   send an sys_ipc_hangup after cleaning up it's internal structures.
94
 *
100
 *
95
 * Call forwarding
101
 * Call forwarding
96
 *
102
 *
97
 * The call can be forwarded, so that the answer to call is passed directly
103
 * The call can be forwarded, so that the answer to call is passed directly
98
 * to the original sender. However, this poses special problems regarding
104
 * to the original sender. However, this poses special problems regarding
99
 * routing of hangup messages.
105
 * routing of hangup messages.
100
 *
106
 *
101
 * sys_ipc_hangup -> IPC_M_PHONE_HUNGUP
107
 * sys_ipc_hangup -> IPC_M_PHONE_HUNGUP
102
 * - this message CANNOT be forwarded
108
 * - this message CANNOT be forwarded
103
 *
109
 *
104
 * EHANGUP during forward
110
 * EHANGUP during forward
105
 * - The *forwarding* phone will be closed, EFORWARD is sent to receiver.
111
 * - The *forwarding* phone will be closed, EFORWARD is sent to receiver.
106
 *
112
 *
107
 * EHANGUP, ENOENT during forward
113
 * EHANGUP, ENOENT during forward
108
 * - EFORWARD is sent to the receiver, ipc_forward returns error code EFORWARD
114
 * - EFORWARD is sent to the receiver, ipc_forward returns error code EFORWARD
109
 *
115
 *
110
 * Cleanup strategy
116
 * Cleanup strategy
111
 *
117
 *
112
 * 1) Disconnect all our phones ('ipc_phone_hangup').
118
 * 1) Disconnect all our phones ('ipc_phone_hangup').
113
 *
119
 *
114
 * 2) Disconnect all phones connected to answerbox.
120
 * 2) Disconnect all phones connected to answerbox.
115
 *
121
 *
116
 * 3) Answer all messages in 'calls' and 'dispatched_calls' queues with
122
 * 3) Answer all messages in 'calls' and 'dispatched_calls' queues with
117
 *    appropriate error code (EHANGUP, EFORWARD).
123
 *    appropriate error code (EHANGUP, EFORWARD).
118
 *
124
 *
119
 * 4) Wait for all async answers to arrive and dispose of them.
125
 * 4) Wait for all async answers to arrive and dispose of them.
120
 *
126
 *
121
 */
127
 */
122
 
128
 
123
#include <synch/spinlock.h>
129
#include <synch/spinlock.h>
124
#include <ipc/ipc.h>
130
#include <ipc/ipc.h>
125
#include <arch.h>
131
#include <arch.h>
126
#include <proc/task.h>
132
#include <proc/task.h>
127
#include <ipc/ipcrsc.h>
133
#include <ipc/ipcrsc.h>
128
#include <debug.h>
134
#include <debug.h>
129
 
135
 
130
/** Find call_t * in call table according to callid
136
/** Find call_t * in call table according to callid
131
 *
137
 *
132
 * TODO: Some speedup (hash table?)
138
 * TODO: Some speedup (hash table?)
133
 * @return NULL on not found, otherwise pointer to call structure
139
 * @return NULL on not found, otherwise pointer to call structure
134
 */
140
 */
135
call_t * get_call(__native callid)
141
call_t * get_call(__native callid)
136
{
142
{
137
    link_t *lst;
143
    link_t *lst;
138
    call_t *call, *result = NULL;
144
    call_t *call, *result = NULL;
139
 
145
 
140
    spinlock_lock(&TASK->answerbox.lock);
146
    spinlock_lock(&TASK->answerbox.lock);
141
    for (lst = TASK->answerbox.dispatched_calls.next;
147
    for (lst = TASK->answerbox.dispatched_calls.next;
142
         lst != &TASK->answerbox.dispatched_calls; lst = lst->next) {
148
         lst != &TASK->answerbox.dispatched_calls; lst = lst->next) {
143
        call = list_get_instance(lst, call_t, link);
149
        call = list_get_instance(lst, call_t, link);
144
        if ((__native)call == callid) {
150
        if ((__native)call == callid) {
145
            result = call;
151
            result = call;
146
            break;
152
            break;
147
        }
153
        }
148
    }
154
    }
149
    spinlock_unlock(&TASK->answerbox.lock);
155
    spinlock_unlock(&TASK->answerbox.lock);
150
    return result;
156
    return result;
151
}
157
}
152
 
158
 
153
/** Allocate new phone slot in current TASK structure */
159
/** Allocate new phone slot in current TASK structure */
154
int phone_alloc(void)
160
int phone_alloc(void)
155
{
161
{
156
    int i;
162
    int i;
157
 
163
 
158
    spinlock_lock(&TASK->lock);
164
    spinlock_lock(&TASK->lock);
159
   
165
   
160
    for (i=0; i < IPC_MAX_PHONES; i++) {
166
    for (i=0; i < IPC_MAX_PHONES; i++) {
161
        if (TASK->phones[i].state == IPC_PHONE_HUNGUP && \
167
        if (TASK->phones[i].state == IPC_PHONE_HUNGUP && \
162
            atomic_get(&TASK->phones[i].active_calls) == 0)
168
            atomic_get(&TASK->phones[i].active_calls) == 0)
163
            TASK->phones[i].state = IPC_PHONE_FREE;
169
            TASK->phones[i].state = IPC_PHONE_FREE;
164
 
170
 
165
        if (TASK->phones[i].state == IPC_PHONE_FREE) {
171
        if (TASK->phones[i].state == IPC_PHONE_FREE) {
166
            TASK->phones[i].state = IPC_PHONE_CONNECTING;
172
            TASK->phones[i].state = IPC_PHONE_CONNECTING;
167
            break;
173
            break;
168
        }
174
        }
169
    }
175
    }
170
    spinlock_unlock(&TASK->lock);
176
    spinlock_unlock(&TASK->lock);
171
 
177
 
172
    if (i >= IPC_MAX_PHONES)
178
    if (i >= IPC_MAX_PHONES)
173
        return -1;
179
        return -1;
174
    return i;
180
    return i;
175
}
181
}
176
 
182
 
177
static void phone_deallocp(phone_t *phone)
183
static void phone_deallocp(phone_t *phone)
178
{
184
{
179
    ASSERT(phone->state == IPC_PHONE_CONNECTING);
185
    ASSERT(phone->state == IPC_PHONE_CONNECTING);
180
   
186
   
181
    /* atomic operation */
187
    /* atomic operation */
182
    phone->state = IPC_PHONE_FREE;
188
    phone->state = IPC_PHONE_FREE;
183
}
189
}
184
 
190
 
185
/** Free slot from a disconnected phone
191
/** Free slot from a disconnected phone
186
 *
192
 *
187
 * All already sent messages will be correctly processed
193
 * All already sent messages will be correctly processed
188
 */
194
 */
189
void phone_dealloc(int phoneid)
195
void phone_dealloc(int phoneid)
190
{
196
{
191
    phone_deallocp(&TASK->phones[phoneid]);
197
    phone_deallocp(&TASK->phones[phoneid]);
192
}
198
}
193
 
199
 
194
/** Connect phone to a given answerbox
200
/** Connect phone to a given answerbox
195
 *
201
 *
196
 * @param phoneid The slot that will be connected
202
 * @param phoneid The slot that will be connected
197
 *
203
 *
198
 * The procedure _enforces_ that the user first marks the phone
204
 * The procedure _enforces_ that the user first marks the phone
199
 * busy (e.g. via phone_alloc) and then connects the phone, otherwise
205
 * busy (e.g. via phone_alloc) and then connects the phone, otherwise
200
 * race condition may appear.
206
 * race condition may appear.
201
 */
207
 */
202
void phone_connect(int phoneid, answerbox_t *box)
208
void phone_connect(int phoneid, answerbox_t *box)
203
{
209
{
204
    phone_t *phone = &TASK->phones[phoneid];
210
    phone_t *phone = &TASK->phones[phoneid];
205
   
211
   
206
    ASSERT(phone->state == IPC_PHONE_CONNECTING);
212
    ASSERT(phone->state == IPC_PHONE_CONNECTING);
207
    ipc_phone_connect(phone, box);
213
    ipc_phone_connect(phone, box);
208
}
214
}
-
 
215
 
-
 
216
 /** @}
-
 
217
 */
-
 
218
 
209
 
219