Subversion Repositories HelenOS-historic

Rev

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

Rev 1086 Rev 1088
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
#ifndef __IPC_H__
29
#ifndef __IPC_H__
30
#define __IPC_H__
30
#define __IPC_H__
31
 
31
 
32
/* Length of data being transfered with IPC call */
32
/* Length of data being transfered with IPC call */
33
/* - the uspace may not be able to utilize full length */
33
/* - the uspace may not be able to utilize full length */
34
#define IPC_CALL_LEN    4
34
#define IPC_CALL_LEN    4
35
 
35
 
36
/** Maximum active async calls per thread */
36
/** Maximum active async calls per thread */
37
#define IPC_MAX_ASYNC_CALLS  4
37
#define IPC_MAX_ASYNC_CALLS  4
38
 
38
 
39
/* Flags for calls */
39
/* Flags for calls */
40
#define IPC_CALL_ANSWERED     (1<<0) /**< This is answer to a call */
40
#define IPC_CALL_ANSWERED       (1<<0) /**< This is answer to a call */
41
#define IPC_CALL_STATIC_ALLOC (1<<1) /**< This call will not be freed on error */
41
#define IPC_CALL_STATIC_ALLOC   (1<<1) /**< This call will not be freed on error */
42
#define IPC_CALL_DISPATCHED   (1<<2) /**< Call is in dispatch queue */
42
#define IPC_CALL_DISPATCHED     (1<<2) /**< Call is in dispatch queue */
43
#define IPC_CALL_DISCARD_ANSWER (1<<3) /**< Answer will not be passed to
43
#define IPC_CALL_DISCARD_ANSWER (1<<3) /**< Answer will not be passed to
44
                    * userspace, will be discarded */
44
                    * userspace, will be discarded */
-
 
45
#define IPC_CALL_FORWARDED      (1<<4) /* Call was forwarded */
45
 
46
 
46
/* Flags for ipc_wait_for_call */
47
/* Flags for ipc_wait_for_call */
47
#define IPC_WAIT_NONBLOCKING   1
48
#define IPC_WAIT_NONBLOCKING   1
48
 
49
 
49
/* Flags of callid (the addresses are aligned at least to 4,
50
/* Flags of callid (the addresses are aligned at least to 4,
50
 * that is why we can use bottom 2 bits of the call address
51
 * that is why we can use bottom 2 bits of the call address
51
 */
52
 */
52
#define IPC_CALLID_ANSWERED       1 /**< Type of this msg is 'answer' */
53
#define IPC_CALLID_ANSWERED       1 /**< Type of this msg is 'answer' */
53
#define IPC_CALLID_NOTIFICATION   2 /**< Type of this msg is 'notification' */
54
#define IPC_CALLID_NOTIFICATION   2 /**< Type of this msg is 'notification' */
54
 
55
 
55
/* Return values from IPC_ASYNC */
56
/* Return values from IPC_ASYNC */
56
#define IPC_CALLRET_FATAL         -1
57
#define IPC_CALLRET_FATAL         -1
57
#define IPC_CALLRET_TEMPORARY     -2
58
#define IPC_CALLRET_TEMPORARY     -2
58
 
59
 
59
 
60
 
60
/* Macros for manipulating calling data */
61
/* Macros for manipulating calling data */
61
#define IPC_SET_RETVAL(data, retval)   ((data).args[0] = (retval))
62
#define IPC_SET_RETVAL(data, retval)   ((data).args[0] = (retval))
62
#define IPC_SET_METHOD(data, val)   ((data).args[0] = (val))
63
#define IPC_SET_METHOD(data, val)   ((data).args[0] = (val))
63
#define IPC_SET_ARG1(data, val)   ((data).args[1] = (val))
64
#define IPC_SET_ARG1(data, val)   ((data).args[1] = (val))
64
#define IPC_SET_ARG2(data, val)   ((data).args[2] = (val))
65
#define IPC_SET_ARG2(data, val)   ((data).args[2] = (val))
65
#define IPC_SET_ARG3(data, val)   ((data).args[3] = (val))
66
#define IPC_SET_ARG3(data, val)   ((data).args[3] = (val))
66
 
67
 
67
#define IPC_GET_METHOD(data)           ((data).args[0])
68
#define IPC_GET_METHOD(data)           ((data).args[0])
68
#define IPC_GET_RETVAL(data)           ((data).args[0])
69
#define IPC_GET_RETVAL(data)           ((data).args[0])
69
 
70
 
70
#define IPC_GET_ARG1(data)              ((data).args[1])
71
#define IPC_GET_ARG1(data)              ((data).args[1])
71
#define IPC_GET_ARG2(data)              ((data).args[2])
72
#define IPC_GET_ARG2(data)              ((data).args[2])
72
#define IPC_GET_ARG3(data)              ((data).args[3])
73
#define IPC_GET_ARG3(data)              ((data).args[3])
73
 
74
 
74
/* Well known phone descriptors */
75
/* Well known phone descriptors */
75
#define PHONE_NS              0
76
#define PHONE_NS              0
76
 
77
 
77
/* System-specific methods - only through special syscalls
78
/* System-specific methods - only through special syscalls
78
 * These methods have special behaviour
79
 * These methods have special behaviour
79
 */
80
 */
80
/** Protocol for CONNECT - TO - ME
81
/** Protocol for CONNECT - TO - ME
81
 *
82
 *
82
 * Calling process asks the callee to create a callback connection,
83
 * Calling process asks the callee to create a callback connection,
83
 * so that it can start initiating new messages.
84
 * so that it can start initiating new messages.
84
 *
85
 *
85
 * The protocol for negotiating is:
86
 * The protocol for negotiating is:
86
 * - sys_connect_to_me - sends a message IPC_M_CONNECTTOME
87
 * - sys_connect_to_me - sends a message IPC_M_CONNECTTOME
87
 * - sys_wait_for_call - upon receipt tries to allocate new phone
88
 * - sys_wait_for_call - upon receipt tries to allocate new phone
88
 *                       - if it fails, responds with ELIMIT
89
 *                       - if it fails, responds with ELIMIT
89
 *                     - passes call to userspace. If userspace
90
 *                     - passes call to userspace. If userspace
90
 *                       responds with error, phone is deallocated and
91
 *                       responds with error, phone is deallocated and
91
 *                       error is sent back to caller. Otherwise
92
 *                       error is sent back to caller. Otherwise
92
 *                       the call is accepted and the response is sent back.
93
 *                       the call is accepted and the response is sent back.
93
 *                     - the allocated phoneid is passed to userspace
94
 *                     - the allocated phoneid is passed to userspace
94
 *                       (on the receiving sid) as ARG3 of the call.
95
 *                       (on the receiving sid) as ARG3 of the call.
95
 *                     - the caller obtains taskid of the called thread
96
 *                     - the caller obtains taskid of the called thread
96
 */
97
 */
97
#define IPC_M_CONNECT_TO_ME     1
98
#define IPC_M_CONNECT_TO_ME     1
98
/** Protocol for CONNECT - ME - TO
99
/** Protocol for CONNECT - ME - TO
99
 *
100
 *
100
 * Calling process asks the callee to create for him a new connection.
101
 * Calling process asks the callee to create for him a new connection.
101
 * E.g. the caller wants a name server to connect him to print server.
102
 * E.g. the caller wants a name server to connect him to print server.
102
 *
103
 *
103
 * The protocol for negotiating is:
104
 * The protocol for negotiating is:
104
 * - sys_connect_me_to - send a synchronous message to name server
105
 * - sys_connect_me_to - send a synchronous message to name server
105
 *                       indicating that it wants to be connected to some
106
 *                       indicating that it wants to be connected to some
106
 *                       service
107
 *                       service
107
 *                     - arg1/2 are user specified, arg3 contains
108
 *                     - arg1/2 are user specified, arg3 contains
108
 *                       address of the phone that should be connected
109
 *                       address of the phone that should be connected
109
 *                       (TODO: it leaks to userspace)
110
 *                       (TODO: it leaks to userspace)
110
 *   recepient         -  if ipc_answer == 0, then accept connection
111
 *   recepient         -  if ipc_answer == 0, then accept connection
111
 *                     -  otherwise connection refused
112
 *                     -  otherwise connection refused
112
 *                     -  recepient may forward message. Forwarding
113
 *                     -  recepient may forward message. Forwarding
113
 *                        system message
114
 *                        system message
114
 *
115
 *
115
 */
116
 */
116
#define IPC_M_CONNECT_ME_TO     2
117
#define IPC_M_CONNECT_ME_TO     2
117
/** This message is sent to answerbox when the phone
118
/** This message is sent to answerbox when the phone
118
 * is hung up
119
 * is hung up
119
 */
120
 */
120
#define IPC_M_PHONE_HUNGUP      3
121
#define IPC_M_PHONE_HUNGUP      3
121
 
122
 
122
 
123
 
123
/* Well-known methods */
124
/* Well-known methods */
124
#define IPC_M_LAST_SYSTEM     511
125
#define IPC_M_LAST_SYSTEM     511
125
#define IPC_M_PING            512
126
#define IPC_M_PING            512
126
/* User methods */
127
/* User methods */
127
#define FIRST_USER_METHOD     1024
128
#define FIRST_USER_METHOD     1024
128
 
129
 
129
#ifdef KERNEL
130
#ifdef KERNEL
130
 
131
 
131
#include <synch/mutex.h>
132
#include <synch/mutex.h>
132
#include <synch/condvar.h>
133
#include <synch/condvar.h>
133
#include <adt/list.h>
134
#include <adt/list.h>
134
 
135
 
135
#define IPC_MAX_PHONES  16
136
#define IPC_MAX_PHONES  16
136
 
137
 
137
typedef struct answerbox_s answerbox_t;
138
typedef struct answerbox_s answerbox_t;
138
typedef struct phone_s phone_t;
139
typedef struct phone_s phone_t;
139
typedef struct {
140
typedef struct {
140
    __native args[IPC_CALL_LEN];
141
    __native args[IPC_CALL_LEN];
141
    phone_t *phone;
142
    phone_t *phone;
142
}ipc_data_t;
143
}ipc_data_t;
143
 
144
 
144
struct answerbox_s {
145
struct answerbox_s {
145
    SPINLOCK_DECLARE(lock);
146
    SPINLOCK_DECLARE(lock);
146
 
147
 
147
    task_t *task;
148
    task_t *task;
148
 
149
 
149
    waitq_t wq;
150
    waitq_t wq;
150
 
151
 
151
    link_t connected_phones; /**< Phones connected to this answerbox */
152
    link_t connected_phones; /**< Phones connected to this answerbox */
152
    link_t calls;            /**< Received calls */
153
    link_t calls;            /**< Received calls */
153
    link_t dispatched_calls; /* Should be hash table in the future */
154
    link_t dispatched_calls; /* Should be hash table in the future */
154
 
155
 
155
    link_t answers;          /**< Answered calls */
156
    link_t answers;          /**< Answered calls */
156
};
157
};
157
 
158
 
-
 
159
typedef enum {
-
 
160
    IPC_BUSY_FREE = 0,
-
 
161
    IPC_BUSY_CONNECTING,
-
 
162
    IPC_BUSY_CONNECTED
-
 
163
} ipc_busy_t;
-
 
164
 
158
struct phone_s {
165
struct phone_s {
159
    SPINLOCK_DECLARE(lock);
166
    SPINLOCK_DECLARE(lock);
160
    link_t list;
167
    link_t list;
161
    answerbox_t *callee;
168
    answerbox_t *callee;
162
    int busy;
169
    ipc_busy_t busy;
163
    atomic_t active_calls;
170
    atomic_t active_calls;
164
};
171
};
165
 
172
 
166
typedef struct {
173
typedef struct {
167
    link_t list;
174
    link_t list;
168
 
175
 
169
    int flags;
176
    int flags;
170
 
177
 
171
    /* Identification of the caller */
178
    /* Identification of the caller */
172
    task_t *sender;
179
    task_t *sender;
173
    /* The caller box is different from sender->answerbox
180
    /* The caller box is different from sender->answerbox
174
     * for synchronous calls
181
     * for synchronous calls
175
     */
182
     */
176
    answerbox_t *callerbox;
183
    answerbox_t *callerbox;
177
 
184
 
178
    ipc_data_t data;
185
    ipc_data_t data;
179
}call_t;
186
}call_t;
180
 
187
 
181
extern void ipc_init(void);
188
extern void ipc_init(void);
182
extern call_t * ipc_wait_for_call(answerbox_t *box, int flags);
189
extern call_t * ipc_wait_for_call(answerbox_t *box, int flags);
183
extern void ipc_answer(answerbox_t *box, call_t *request);
190
extern void ipc_answer(answerbox_t *box, call_t *request);
184
extern void ipc_call(phone_t *phone, call_t *request);
191
extern int ipc_call(phone_t *phone, call_t *call);
185
extern void ipc_call_sync(phone_t *phone, call_t *request);
192
extern void ipc_call_sync(phone_t *phone, call_t *request);
186
extern void ipc_phone_init(phone_t *phone);
193
extern void ipc_phone_init(phone_t *phone);
187
extern void ipc_phone_connect(phone_t *phone, answerbox_t *box);
194
extern void ipc_phone_connect(phone_t *phone, answerbox_t *box);
188
extern void ipc_call_free(call_t *call);
195
extern void ipc_call_free(call_t *call);
189
extern call_t * ipc_call_alloc(void);
196
extern call_t * ipc_call_alloc(void);
190
extern void ipc_answerbox_init(answerbox_t *box);
197
extern void ipc_answerbox_init(answerbox_t *box);
191
extern void ipc_call_static_init(call_t *call);
198
extern void ipc_call_static_init(call_t *call);
192
extern void task_print_list(void);
199
extern void task_print_list(void);
193
extern void ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox);
200
extern int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox);
194
 
201
 
195
extern answerbox_t *ipc_phone_0;
202
extern answerbox_t *ipc_phone_0;
196
extern void ipc_cleanup(task_t *task);
203
extern void ipc_cleanup(task_t *task);
197
extern int ipc_phone_hangup(phone_t *phone);
204
extern int ipc_phone_hangup(phone_t *phone);
198
 
205
 
199
#endif
206
#endif
200
 
207
 
201
#endif
208
#endif
202
 
209