Subversion Repositories HelenOS

Rev

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

Rev 1084 Rev 1086
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
/* IPC resources management
29
/* IPC resources management
30
 *
30
 *
31
 * The goal of this source code is to properly manage IPC resources
31
 * The goal of this source code is to properly manage IPC resources
32
 * and allow straight and clean clean-up procedure upon task termination.
32
 * and allow straight and clean clean-up procedure upon task termination.
33
 *
33
 *
34
 * The pattern of usage of the resources is:
34
 * The pattern of usage of the resources is:
35
 * - allocate empty phone slot, connect | deallocate slot
35
 * - allocate empty phone slot, connect | deallocate slot
36
 * - disconnect connected phone (some messages might be on the fly)
36
 * - disconnect connected phone (some messages might be on the fly)
37
 * - find phone in slot and send a message using phone
37
 * - find phone in slot and send a message using phone
38
 * - answer message to phone
38
 * - answer message to phone
-
 
39
 * - hangup phone (the caller has hung up)
-
 
40
 * - hangup phone (the answerbox is exiting)
39
 *
41
 *
40
 * Locking strategy
42
 * Locking strategy
41
 *
43
 *
42
 * - To use a phone, disconnect a phone etc., the phone must be
44
 * - To use a phone, disconnect a phone etc., the phone must be
43
 *   first locked and then checked that it is connected
45
 *   first locked and then checked that it is connected
44
 * - To connect an allocated phone it need not be locked (assigning
46
 * - To connect an allocated phone it need not be locked (assigning
45
 *   pointer is atomic on all platforms)
47
 *   pointer is atomic on all platforms)
46
 *
48
 *
47
 * - To find an empty phone slot, the TASK must be locked
49
 * - To find an empty phone slot, the TASK must be locked
48
 * - To answer a message, the answerbox must be locked
50
 * - To answer a message, the answerbox must be locked
49
 * - The locking of phone and answerbox is done at the ipc_ level.
51
 * - The locking of phone and answerbox is done at the ipc_ level.
50
 *   It is perfectly correct to pass unconnected phone to these functions
52
 *   It is perfectly correct to pass unconnected phone to these functions
51
 *   and proper reply will be generated.
53
 *   and proper reply will be generated.
52
 *
54
 *
53
 * - There may be objection that a race may occur when the syscall finds
-
 
54
 *   an appropriate call and before executing ipc_send, the phone call might
-
 
55
 *   be disconnected and connected elsewhere. As there is no easy solution,
-
 
56
 *   the application will be notified by an  'PHONE_DISCONNECTED' message
-
 
57
 *   and the phone will not be allocated before the application notifies
-
 
58
 *   the kernel subsystem that it does not have any pending calls regarding
-
 
59
 *   this phone call.
-
 
60
 *
-
 
61
 * Locking order
55
 * Locking order
62
 *
56
 *
63
 * There are 2 possibilities
-
 
64
 * - first phone, then answerbox
57
 * - first phone, then answerbox
65
 *   + Easy locking on calls
58
 *   + Easy locking on calls
66
 *   - Very hard traversing list of phones when disconnecting because
59
 *   - Very hard traversing list of phones when disconnecting because
67
 *     the phones may disconnect during traversal of list of connected phones.
60
 *     the phones may disconnect during traversal of list of connected phones.
68
 *     The only possibility is try_lock with restart of list traversal.
61
 *     The only possibility is try_lock with restart of list traversal.
69
 *
62
 *
70
 * - first answerbox, then phone(s)
-
 
71
 *   + Easy phone disconnect
-
 
72
 *   - Multiple checks needed when sending message
63
 * Destroying is less frequent, this approach is taken.
73
 *
64
 *
-
 
65
 * Phone hangup
-
 
66
 *
-
 
67
 * *** The caller hangs up (sys_ipc_hangup) ***
74
 * Because the answerbox destroyal is much less frequent operation,
68
 * - The phone is disconnected (no more messages can be sent over this phone),
-
 
69
 *   all in-progress messages are correctly handled. The anwerbox receives
-
 
70
 *   IPC_M_PHONE_HUNGUP call from the phone that hung up. When all async
75
 * the first method is chosen.
71
 *   calls are answered, the phone is deallocated.
-
 
72
 *
-
 
73
 * *** The answerbox hangs up (ipc_answer(ESLAM))
-
 
74
 * - The phone is disconnected. IPC_M_ANSWERBOX_HUNGUP notification
-
 
75
 *   is sent to source task, the calling process is expected to
-
 
76
 *   send an sys_ipc_hangup after cleaning up it's internal structures.
76
 *
77
 *
77
 * Cleanup strategy
78
 * Cleanup strategy
78
 *
79
 *
79
 * 1) Disconnect all phones.
80
 * 1) Disconnect all our phones ('sys_ipc_hangup')
-
 
81
 *
-
 
82
 * 2) Disconnect all phones connected to answerbox.
80
 *    * Send message 'PHONE_DISCONNECTED' to the target application
83
 *    * Send message 'PHONE_DISCONNECTED' to the target application
81
 * - Once all phones are disconnected, no further calls can arrive
84
 * - Once all phones are disconnected, no further calls can arrive
82
 *
85
 *
83
 * 2) Answer all messages in 'calls' and 'dispatched_calls' queues with
86
 * 3) Answer all messages in 'calls' and 'dispatched_calls' queues with
84
 *    appropriate error code.
87
 *    appropriate error code.
85
 *
88
 *
86
 * 3) Wait for all async answers to arrive
89
 * 4) Wait for all async answers to arrive
87
 * Alternatively - we might try to invalidate all messages by setting some
-
 
88
 * flag, that would dispose of the message once it is answered. This
-
 
89
 * would need to link all calls in one big list, which we don't currently
-
 
90
 * do.
-
 
91
 *
-
 
92
 *
90
 *
93
 */
91
 */
94
 
92
 
95
#include <synch/spinlock.h>
93
#include <synch/spinlock.h>
96
#include <ipc/ipc.h>
94
#include <ipc/ipc.h>
97
#include <arch.h>
95
#include <arch.h>
98
#include <proc/task.h>
96
#include <proc/task.h>
99
#include <ipc/ipcrsc.h>
97
#include <ipc/ipcrsc.h>
100
#include <debug.h>
98
#include <debug.h>
101
 
99
 
102
/** Find call_t * in call table according to callid
100
/** Find call_t * in call table according to callid
103
 *
101
 *
104
 * @return NULL on not found, otherwise pointer to call structure
102
 * @return NULL on not found, otherwise pointer to call structure
105
 */
103
 */
106
call_t * get_call(__native callid)
104
call_t * get_call(__native callid)
107
{
105
{
108
    /* TODO: Traverse list of dispatched calls and find one */
106
    /* TODO: Traverse list of dispatched calls and find one */
109
    /* TODO: locking of call, ripping it from dispatched calls etc.  */
107
    /* TODO: locking of call, ripping it from dispatched calls etc.  */
110
    return (call_t *) callid;
108
    return (call_t *) callid;
111
}
109
}
112
 
110
 
113
/** Allocate new phone slot in current TASK structure */
111
/** Allocate new phone slot in current TASK structure */
114
int phone_alloc(void)
112
int phone_alloc(void)
115
{
113
{
116
    int i;
114
    int i;
117
 
115
 
118
    spinlock_lock(&TASK->lock);
116
    spinlock_lock(&TASK->lock);
119
   
117
   
120
    for (i=0; i < IPC_MAX_PHONES; i++) {
118
    for (i=0; i < IPC_MAX_PHONES; i++) {
121
        if (!TASK->phones[i].busy) {
119
        if (!TASK->phones[i].busy && !atomic_get(&TASK->phones[i].active_calls)) {
122
            TASK->phones[i].busy = 1;
120
            TASK->phones[i].busy = 1;
123
            break;
121
            break;
124
        }
122
        }
125
    }
123
    }
126
    spinlock_unlock(&TASK->lock);
124
    spinlock_unlock(&TASK->lock);
127
 
125
 
128
    if (i >= IPC_MAX_PHONES)
126
    if (i >= IPC_MAX_PHONES)
129
        return -1;
127
        return -1;
130
    return i;
128
    return i;
131
}
129
}
132
 
130
 
133
/** Disconnect phone */
131
/** Disconnect phone a free the slot
-
 
132
 *
-
 
133
 * All already sent messages will be correctly processed
-
 
134
 */
134
void phone_dealloc(int phoneid)
135
void phone_dealloc(int phoneid)
135
{
136
{
136
    spinlock_lock(&TASK->lock);
137
    spinlock_lock(&TASK->lock);
137
 
138
 
138
    ASSERT(TASK->phones[phoneid].busy);
139
    ASSERT(TASK->phones[phoneid].busy);
139
 
-
 
140
    if (TASK->phones[phoneid].callee)
140
    ASSERT(! TASK->phones[phoneid].callee);
141
        ipc_phone_destroy(&TASK->phones[phoneid]);
-
 
142
 
141
 
143
    TASK->phones[phoneid].busy = 0;
142
    TASK->phones[phoneid].busy = 0;
144
    spinlock_unlock(&TASK->lock);
143
    spinlock_unlock(&TASK->lock);
145
}
144
}
146
 
145
 
147
/** Connect phone to a given answerbox
146
/** Connect phone to a given answerbox
148
 *
147
 *
149
 * @param phoneid The slot that will be connected
148
 * @param phoneid The slot that will be connected
150
 *
149
 *
151
 * The procedure _enforces_ that the user first marks the phone
150
 * The procedure _enforces_ that the user first marks the phone
152
 * busy (e.g. via phone_alloc) and then connects the phone, otherwise
151
 * busy (e.g. via phone_alloc) and then connects the phone, otherwise
153
 * race condition may appear.
152
 * race condition may appear.
154
 */
153
 */
155
void phone_connect(int phoneid, answerbox_t *box)
154
void phone_connect(int phoneid, answerbox_t *box)
156
{
155
{
157
    phone_t *phone = &TASK->phones[phoneid];
156
    phone_t *phone = &TASK->phones[phoneid];
158
   
157
   
159
    ASSERT(phone->busy);
158
    ASSERT(phone->busy);
160
    ipc_phone_connect(phone, box);
159
    ipc_phone_connect(phone, box);
161
}
160
}
162
 
161