Subversion Repositories HelenOS-historic

Rev

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

Rev 1083 Rev 1084
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
 *
39
 *
-
 
40
 * Locking strategy
-
 
41
 *
-
 
42
 * - To use a phone, disconnect a phone etc., the phone must be
-
 
43
 *   first locked and then checked that it is connected
-
 
44
 * - To connect an allocated phone it need not be locked (assigning
-
 
45
 *   pointer is atomic on all platforms)
-
 
46
 *
-
 
47
 * - To find an empty phone slot, the TASK must be locked
-
 
48
 * - To answer a message, the answerbox must be locked
-
 
49
 * - The locking of phone and answerbox is done at the ipc_ level.
-
 
50
 *   It is perfectly correct to pass unconnected phone to these functions
-
 
51
 *   and proper reply will be generated.
-
 
52
 *
-
 
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
-
 
62
 *
-
 
63
 * There are 2 possibilities
-
 
64
 * - first phone, then answerbox
-
 
65
 *   + Easy locking on calls
-
 
66
 *   - Very hard traversing list of phones when disconnecting because
-
 
67
 *     the phones may disconnect during traversal of list of connected phones.
-
 
68
 *     The only possibility is try_lock with restart of list traversal.
-
 
69
 *
-
 
70
 * - first answerbox, then phone(s)
-
 
71
 *   + Easy phone disconnect
-
 
72
 *   - Multiple checks needed when sending message
-
 
73
 *
-
 
74
 * Because the answerbox destroyal is much less frequent operation,
-
 
75
 * the first method is chosen.
-
 
76
 *
-
 
77
 * Cleanup strategy
-
 
78
 *
-
 
79
 * 1) Disconnect all phones.
-
 
80
 *    * Send message 'PHONE_DISCONNECTED' to the target application
-
 
81
 * - Once all phones are disconnected, no further calls can arrive
-
 
82
 *
-
 
83
 * 2) Answer all messages in 'calls' and 'dispatched_calls' queues with
-
 
84
 *    appropriate error code.
-
 
85
 *
-
 
86
 * 3) 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
 *
40
 *
92
 *
41
 */
93
 */
42
 
94
 
43
#include <synch/spinlock.h>
95
#include <synch/spinlock.h>
44
#include <ipc/ipc.h>
96
#include <ipc/ipc.h>
45
#include <arch.h>
97
#include <arch.h>
46
#include <proc/task.h>
98
#include <proc/task.h>
47
#include <ipc/ipcrsc.h>
99
#include <ipc/ipcrsc.h>
48
#include <debug.h>
100
#include <debug.h>
49
 
101
 
50
/** Find call_t * in call table according to callid
102
/** Find call_t * in call table according to callid
51
 *
103
 *
52
 * @return NULL on not found, otherwise pointer to call structure
104
 * @return NULL on not found, otherwise pointer to call structure
53
 */
105
 */
54
call_t * get_call(__native callid)
106
call_t * get_call(__native callid)
55
{
107
{
56
    /* TODO: Traverse list of dispatched calls and find one */
108
    /* TODO: Traverse list of dispatched calls and find one */
57
    /* TODO: locking of call, ripping it from dispatched calls etc.  */
109
    /* TODO: locking of call, ripping it from dispatched calls etc.  */
58
    return (call_t *) callid;
110
    return (call_t *) callid;
59
}
111
}
60
 
112
 
61
/** Return pointer to phone identified by phoneid or NULL if non-existent */
-
 
62
phone_t * get_phone_and_lock(__native phoneid)
-
 
63
{
-
 
64
    phone_t *phone;
-
 
65
 
-
 
66
    if (phoneid >= IPC_MAX_PHONES)
-
 
67
        return NULL;
-
 
68
 
-
 
69
    phone = &TASK->phones[phoneid];
-
 
70
    spinlock_lock(&phone->lock);
-
 
71
    if (!phone->callee) {
-
 
72
        spinlock_unlock(&phone->lock);
-
 
73
        return NULL;
-
 
74
    }
-
 
75
    /* TODO... */
-
 
76
    spinlock_unlock(&phone->lock);
-
 
77
    return phone;
-
 
78
}
-
 
79
 
-
 
80
/** Allocate new phone slot in current TASK structure */
113
/** Allocate new phone slot in current TASK structure */
81
int phone_alloc(void)
114
int phone_alloc(void)
82
{
115
{
83
    int i;
116
    int i;
84
 
117
 
85
    spinlock_lock(&TASK->lock);
118
    spinlock_lock(&TASK->lock);
86
   
119
   
87
    for (i=0; i < IPC_MAX_PHONES; i++) {
120
    for (i=0; i < IPC_MAX_PHONES; i++) {
88
        if (!TASK->phones[i].busy) {
121
        if (!TASK->phones[i].busy) {
89
            TASK->phones[i].busy = 1;
122
            TASK->phones[i].busy = 1;
90
            break;
123
            break;
91
        }
124
        }
92
    }
125
    }
93
    spinlock_unlock(&TASK->lock);
126
    spinlock_unlock(&TASK->lock);
94
 
127
 
95
    if (i >= IPC_MAX_PHONES)
128
    if (i >= IPC_MAX_PHONES)
96
        return -1;
129
        return -1;
97
    return i;
130
    return i;
98
}
131
}
99
 
132
 
100
/** Disconnect phone */
133
/** Disconnect phone */
101
void phone_dealloc(int phoneid)
134
void phone_dealloc(int phoneid)
102
{
135
{
103
    spinlock_lock(&TASK->lock);
136
    spinlock_lock(&TASK->lock);
104
 
137
 
105
    ASSERT(TASK->phones[phoneid].busy);
138
    ASSERT(TASK->phones[phoneid].busy);
106
 
139
 
107
    if (TASK->phones[phoneid].callee)
140
    if (TASK->phones[phoneid].callee)
108
        ipc_phone_destroy(&TASK->phones[phoneid]);
141
        ipc_phone_destroy(&TASK->phones[phoneid]);
109
 
142
 
110
    TASK->phones[phoneid].busy = 0;
143
    TASK->phones[phoneid].busy = 0;
111
    spinlock_unlock(&TASK->lock);
144
    spinlock_unlock(&TASK->lock);
112
}
145
}
113
 
146
 
-
 
147
/** Connect phone to a given answerbox
-
 
148
 *
-
 
149
 * @param phoneid The slot that will be connected
-
 
150
 *
-
 
151
 * The procedure _enforces_ that the user first marks the phone
-
 
152
 * busy (e.g. via phone_alloc) and then connects the phone, otherwise
-
 
153
 * race condition may appear.
-
 
154
 */
114
void phone_connect(int phoneid, answerbox_t *box)
155
void phone_connect(int phoneid, answerbox_t *box)
115
{
156
{
116
    phone_t *phone = &TASK->phones[phoneid];
157
    phone_t *phone = &TASK->phones[phoneid];
117
   
158
   
-
 
159
    ASSERT(phone->busy);
118
    ipc_phone_connect(phone, box);
160
    ipc_phone_connect(phone, box);
119
}
161
}
120
 
162