Subversion Repositories HelenOS

Rev

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

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