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 | /* Lock ordering |
29 | /* Lock ordering |
30 | * |
30 | * |
31 | * First the answerbox, then the phone |
31 | * First the answerbox, then the phone |
32 | */ |
32 | */ |
33 | 33 | ||
34 | #include <synch/spinlock.h> |
34 | #include <synch/spinlock.h> |
35 | #include <synch/waitq.h> |
35 | #include <synch/waitq.h> |
36 | #include <ipc/ipc.h> |
36 | #include <ipc/ipc.h> |
37 | #include <errno.h> |
37 | #include <errno.h> |
38 | #include <mm/slab.h> |
38 | #include <mm/slab.h> |
39 | #include <arch.h> |
39 | #include <arch.h> |
40 | #include <proc/task.h> |
40 | #include <proc/task.h> |
41 | #include <memstr.h> |
41 | #include <memstr.h> |
42 | #include <debug.h> |
42 | #include <debug.h> |
43 | 43 | ||
44 | #include <print.h> |
44 | #include <print.h> |
45 | #include <proc/thread.h> |
45 | #include <proc/thread.h> |
46 | 46 | ||
47 | /* Open channel that is assigned automatically to new tasks */ |
47 | /* Open channel that is assigned automatically to new tasks */ |
48 | answerbox_t *ipc_phone_0 = NULL; |
48 | answerbox_t *ipc_phone_0 = NULL; |
49 | 49 | ||
50 | static slab_cache_t *ipc_call_slab; |
50 | static slab_cache_t *ipc_call_slab; |
51 | 51 | ||
52 | /* Initialize new call */ |
52 | /* Initialize new call */ |
53 | static void _ipc_call_init(call_t *call) |
53 | static void _ipc_call_init(call_t *call) |
54 | { |
54 | { |
55 | memsetb((__address)call, sizeof(*call), 0); |
55 | memsetb((__address)call, sizeof(*call), 0); |
56 | call->callerbox = &TASK->answerbox; |
56 | call->callerbox = &TASK->answerbox; |
57 | call->sender = TASK; |
57 | call->sender = TASK; |
58 | } |
58 | } |
59 | 59 | ||
60 | /** Allocate & initialize call structure |
60 | /** Allocate & initialize call structure |
61 | * |
61 | * |
62 | * The call is initialized, so that the reply will be directed |
62 | * The call is initialized, so that the reply will be directed |
63 | * to TASK->answerbox |
63 | * to TASK->answerbox |
64 | */ |
64 | */ |
65 | call_t * ipc_call_alloc(void) |
65 | call_t * ipc_call_alloc(void) |
66 | { |
66 | { |
67 | call_t *call; |
67 | call_t *call; |
68 | 68 | ||
69 | call = slab_alloc(ipc_call_slab, 0); |
69 | call = slab_alloc(ipc_call_slab, 0); |
70 | _ipc_call_init(call); |
70 | _ipc_call_init(call); |
71 | 71 | ||
72 | return call; |
72 | return call; |
73 | } |
73 | } |
74 | 74 | ||
75 | /** Initialize allocated call */ |
75 | /** Initialize allocated call */ |
76 | void ipc_call_static_init(call_t *call) |
76 | void ipc_call_static_init(call_t *call) |
77 | { |
77 | { |
78 | _ipc_call_init(call); |
78 | _ipc_call_init(call); |
79 | call->flags |= IPC_CALL_STATIC_ALLOC; |
79 | call->flags |= IPC_CALL_STATIC_ALLOC; |
80 | } |
80 | } |
81 | 81 | ||
82 | /** Deallocate call stracuture */ |
82 | /** Deallocate call stracuture */ |
83 | void ipc_call_free(call_t *call) |
83 | void ipc_call_free(call_t *call) |
84 | { |
84 | { |
85 | slab_free(ipc_call_slab, call); |
85 | slab_free(ipc_call_slab, call); |
86 | } |
86 | } |
87 | 87 | ||
88 | /** Initialize answerbox structure |
88 | /** Initialize answerbox structure |
89 | */ |
89 | */ |
90 | void ipc_answerbox_init(answerbox_t *box) |
90 | void ipc_answerbox_init(answerbox_t *box) |
91 | { |
91 | { |
92 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
92 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
93 | waitq_initialize(&box->wq); |
93 | waitq_initialize(&box->wq); |
94 | list_initialize(&box->connected_phones); |
94 | list_initialize(&box->connected_phones); |
95 | list_initialize(&box->calls); |
95 | list_initialize(&box->calls); |
96 | list_initialize(&box->dispatched_calls); |
96 | list_initialize(&box->dispatched_calls); |
97 | list_initialize(&box->answers); |
97 | list_initialize(&box->answers); |
98 | box->task = TASK; |
98 | box->task = TASK; |
99 | } |
99 | } |
100 | 100 | ||
101 | /** Connect phone to answerbox */ |
101 | /** Connect phone to answerbox */ |
102 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
102 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
103 | { |
103 | { |
104 | spinlock_lock(&phone->lock); |
104 | spinlock_lock(&phone->lock); |
105 | 105 | ||
106 | ASSERT(!phone->callee); |
106 | ASSERT(!phone->callee); |
107 | phone->busy = 1; |
107 | phone->busy = 1; |
108 | phone->callee = box; |
108 | phone->callee = box; |
109 | 109 | ||
110 | spinlock_lock(&box->lock); |
110 | spinlock_lock(&box->lock); |
111 | list_append(&phone->list, &box->connected_phones); |
111 | list_append(&phone->list, &box->connected_phones); |
112 | spinlock_unlock(&box->lock); |
112 | spinlock_unlock(&box->lock); |
113 | 113 | ||
114 | spinlock_unlock(&phone->lock); |
114 | spinlock_unlock(&phone->lock); |
115 | } |
115 | } |
116 | 116 | ||
117 | /** Initialize phone structure and connect phone to naswerbox |
117 | /** Initialize phone structure and connect phone to naswerbox |
118 | */ |
118 | */ |
119 | void ipc_phone_init(phone_t *phone) |
119 | void ipc_phone_init(phone_t *phone) |
120 | { |
120 | { |
121 | spinlock_initialize(&phone->lock, "phone_lock"); |
121 | spinlock_initialize(&phone->lock, "phone_lock"); |
122 | phone->callee = NULL; |
122 | phone->callee = NULL; |
123 | phone->busy = 0; |
123 | phone->busy = 0; |
124 | } |
124 | } |
125 | 125 | ||
126 | /** Disconnect phone from answerbox |
- | |
127 | * |
- | |
128 | * It is allowed to call disconnect on already disconnected phone\ |
- | |
129 | */ |
- | |
130 | void ipc_phone_destroy(phone_t *phone) |
- | |
131 | { |
- | |
132 | answerbox_t *box = phone->callee; |
- | |
133 | - | ||
134 | ASSERT(box); |
- | |
135 | - | ||
136 | spinlock_lock(&phone->lock); |
- | |
137 | spinlock_lock(&box->lock); |
- | |
138 | - | ||
139 | if (phone->callee) { |
- | |
140 | list_remove(&phone->list); |
- | |
141 | phone->callee = NULL; |
- | |
142 | } |
- | |
143 | - | ||
144 | spinlock_unlock(&box->lock); |
- | |
145 | spinlock_unlock(&phone->lock); |
- | |
146 | } |
- | |
147 | - | ||
148 | /** Helper function to facilitate synchronous calls */ |
126 | /** Helper function to facilitate synchronous calls */ |
149 | void ipc_call_sync(phone_t *phone, call_t *request) |
127 | void ipc_call_sync(phone_t *phone, call_t *request) |
150 | { |
128 | { |
151 | answerbox_t sync_box; |
129 | answerbox_t sync_box; |
152 | 130 | ||
153 | ipc_answerbox_init(&sync_box); |
131 | ipc_answerbox_init(&sync_box); |
154 | 132 | ||
155 | /* We will receive data on special box */ |
133 | /* We will receive data on special box */ |
156 | request->callerbox = &sync_box; |
134 | request->callerbox = &sync_box; |
157 | 135 | ||
158 | ipc_call(phone, request); |
136 | ipc_call(phone, request); |
159 | ipc_wait_for_call(&sync_box, 0); |
137 | ipc_wait_for_call(&sync_box, 0); |
160 | } |
138 | } |
161 | 139 | ||
162 | /** Answer message that was not dispatched and is not entered in |
140 | /** Answer message that was not dispatched and is not entered in |
163 | * any queue |
141 | * any queue |
164 | */ |
142 | */ |
165 | static void _ipc_answer_free_call(call_t *call) |
143 | static void _ipc_answer_free_call(call_t *call) |
166 | { |
144 | { |
167 | answerbox_t *callerbox = call->callerbox; |
145 | answerbox_t *callerbox = call->callerbox; |
168 | 146 | ||
169 | call->flags &= ~IPC_CALL_DISPATCHED; |
147 | call->flags &= ~IPC_CALL_DISPATCHED; |
170 | call->flags |= IPC_CALL_ANSWERED; |
148 | call->flags |= IPC_CALL_ANSWERED; |
171 | 149 | ||
172 | spinlock_lock(&callerbox->lock); |
150 | spinlock_lock(&callerbox->lock); |
173 | list_append(&call->list, &callerbox->answers); |
151 | list_append(&call->list, &callerbox->answers); |
174 | spinlock_unlock(&callerbox->lock); |
152 | spinlock_unlock(&callerbox->lock); |
175 | waitq_wakeup(&callerbox->wq, 0); |
153 | waitq_wakeup(&callerbox->wq, 0); |
176 | } |
154 | } |
177 | 155 | ||
178 | /** Answer message, that is in callee queue |
156 | /** Answer message, that is in callee queue |
179 | * |
157 | * |
180 | * @param box Answerbox that is answering the message |
158 | * @param box Answerbox that is answering the message |
181 | * @param call Modified request that is being sent back |
159 | * @param call Modified request that is being sent back |
182 | */ |
160 | */ |
183 | void ipc_answer(answerbox_t *box, call_t *call) |
161 | void ipc_answer(answerbox_t *box, call_t *call) |
184 | { |
162 | { |
185 | /* Remove from active box */ |
163 | /* Remove from active box */ |
186 | spinlock_lock(&box->lock); |
164 | spinlock_lock(&box->lock); |
187 | list_remove(&call->list); |
165 | list_remove(&call->list); |
188 | spinlock_unlock(&box->lock); |
166 | spinlock_unlock(&box->lock); |
189 | /* Send back answer */ |
167 | /* Send back answer */ |
190 | _ipc_answer_free_call(call); |
168 | _ipc_answer_free_call(call); |
191 | } |
169 | } |
192 | 170 | ||
- | 171 | /* Unsafe unchecking ipc_call */ |
|
- | 172 | static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call) |
|
- | 173 | { |
|
- | 174 | atomic_inc(&phone->active_calls); |
|
- | 175 | call->data.phone = phone; |
|
- | 176 | ||
- | 177 | spinlock_lock(&box->lock); |
|
- | 178 | list_append(&call->list, &box->calls); |
|
- | 179 | spinlock_unlock(&box->lock); |
|
- | 180 | waitq_wakeup(&box->wq, 0); |
|
- | 181 | } |
|
- | 182 | ||
193 | /** Send a asynchronous request using phone to answerbox |
183 | /** Send a asynchronous request using phone to answerbox |
194 | * |
184 | * |
195 | * @param phone Phone connected to answerbox |
185 | * @param phone Phone connected to answerbox |
196 | * @param request Request to be sent |
186 | * @param request Request to be sent |
197 | */ |
187 | */ |
198 | void ipc_call(phone_t *phone, call_t *call) |
188 | void ipc_call(phone_t *phone, call_t *call) |
199 | { |
189 | { |
200 | answerbox_t *box; |
190 | answerbox_t *box; |
201 | 191 | ||
202 | spinlock_lock(&phone->lock); |
192 | spinlock_lock(&phone->lock); |
- | 193 | ||
203 | box = phone->callee; |
194 | box = phone->callee; |
204 | if (!box) { |
195 | if (!box) { |
205 | /* Trying to send over disconnected phone */ |
196 | /* Trying to send over disconnected phone */ |
- | 197 | spinlock_unlock(&phone->lock); |
|
- | 198 | ||
- | 199 | call->data.phone = phone; |
|
206 | IPC_SET_RETVAL(call->data, ENOENT); |
200 | IPC_SET_RETVAL(call->data, ENOENT); |
207 | _ipc_answer_free_call(call); |
201 | _ipc_answer_free_call(call); |
208 | return; |
202 | return; |
209 | } |
203 | } |
- | 204 | _ipc_call(phone, box, call); |
|
- | 205 | ||
- | 206 | spinlock_unlock(&phone->lock); |
|
- | 207 | } |
|
- | 208 | ||
- | 209 | /** Disconnect phone from answerbox |
|
- | 210 | * |
|
- | 211 | * It is allowed to call disconnect on already disconnected phone |
|
- | 212 | * |
|
- | 213 | * @return 0 - phone disconnected, -1 - the phone was already disconnected |
|
- | 214 | */ |
|
- | 215 | int ipc_phone_hangup(phone_t *phone) |
|
- | 216 | { |
|
- | 217 | answerbox_t *box; |
|
- | 218 | call_t *call; |
|
- | 219 | ||
- | 220 | spinlock_lock(&phone->lock); |
|
- | 221 | box = phone->callee; |
|
- | 222 | if (!box) { |
|
- | 223 | spinlock_unlock(&phone->lock); |
|
- | 224 | return -1; |
|
- | 225 | } |
|
210 | 226 | ||
211 | spinlock_lock(&box->lock); |
227 | spinlock_lock(&box->lock); |
212 | list_append(&call->list, &box->calls); |
228 | list_remove(&phone->list); |
- | 229 | phone->callee = NULL; |
|
213 | spinlock_unlock(&box->lock); |
230 | spinlock_unlock(&box->lock); |
- | 231 | ||
- | 232 | call = ipc_call_alloc(); |
|
- | 233 | IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP); |
|
- | 234 | call->flags |= IPC_CALL_DISCARD_ANSWER; |
|
214 | waitq_wakeup(&box->wq, 0); |
235 | _ipc_call(phone, box, call); |
- | 236 | ||
- | 237 | phone->busy = 0; |
|
215 | 238 | ||
216 | spinlock_unlock(&phone->lock); |
239 | spinlock_unlock(&phone->lock); |
- | 240 | ||
- | 241 | return 0; |
|
217 | } |
242 | } |
218 | 243 | ||
219 | /** Forwards call from one answerbox to a new one |
244 | /** Forwards call from one answerbox to a new one |
220 | * |
245 | * |
221 | * @param request Request to be forwarded |
246 | * @param request Request to be forwarded |
222 | * @param newbox Target answerbox |
247 | * @param newbox Target answerbox |
223 | * @param oldbox Old answerbox |
248 | * @param oldbox Old answerbox |
224 | */ |
249 | */ |
225 | void ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox) |
250 | void ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox) |
226 | { |
251 | { |
227 | spinlock_lock(&oldbox->lock); |
252 | spinlock_lock(&oldbox->lock); |
- | 253 | atomic_dec(&call->data.phone->active_calls); |
|
228 | list_remove(&call->list); |
254 | list_remove(&call->list); |
229 | spinlock_unlock(&oldbox->lock); |
255 | spinlock_unlock(&oldbox->lock); |
230 | 256 | ||
231 | ipc_call(newphone, call); |
257 | ipc_call(newphone, call); |
232 | } |
258 | } |
233 | 259 | ||
234 | 260 | ||
235 | /** Wait for phone call |
261 | /** Wait for phone call |
236 | * |
262 | * |
237 | * @return Recived message address |
263 | * @return Recived message address |
238 | * - to distinguish between call and answer, look at call->flags |
264 | * - to distinguish between call and answer, look at call->flags |
239 | */ |
265 | */ |
240 | call_t * ipc_wait_for_call(answerbox_t *box, int flags) |
266 | call_t * ipc_wait_for_call(answerbox_t *box, int flags) |
241 | { |
267 | { |
242 | call_t *request; |
268 | call_t *request; |
243 | 269 | ||
- | 270 | restart: |
|
- | 271 | if (flags & IPC_WAIT_NONBLOCKING) { |
|
- | 272 | if (waitq_sleep_timeout(&box->wq,0,1) == ESYNCH_WOULD_BLOCK) |
|
- | 273 | return NULL; |
|
- | 274 | } else |
|
- | 275 | waitq_sleep(&box->wq); |
|
- | 276 | ||
244 | spinlock_lock(&box->lock); |
277 | spinlock_lock(&box->lock); |
245 | while (1) { |
- | |
246 | if (!list_empty(&box->answers)) { |
278 | if (!list_empty(&box->answers)) { |
247 | /* Handle asynchronous answers */ |
279 | /* Handle asynchronous answers */ |
248 | request = list_get_instance(box->answers.next, call_t, list); |
280 | request = list_get_instance(box->answers.next, call_t, list); |
249 | list_remove(&request->list); |
281 | list_remove(&request->list); |
- | 282 | printf("%d %P\n", IPC_GET_METHOD(request->data), |
|
- | 283 | request->data.phone); |
|
- | 284 | atomic_dec(&request->data.phone->active_calls); |
|
250 | } else if (!list_empty(&box->calls)) { |
285 | } else if (!list_empty(&box->calls)) { |
251 | /* Handle requests */ |
286 | /* Handle requests */ |
252 | request = list_get_instance(box->calls.next, call_t, list); |
287 | request = list_get_instance(box->calls.next, call_t, list); |
253 | list_remove(&request->list); |
288 | list_remove(&request->list); |
254 | /* Append request to dispatch queue */ |
289 | /* Append request to dispatch queue */ |
255 | list_append(&request->list, &box->dispatched_calls); |
290 | list_append(&request->list, &box->dispatched_calls); |
256 | request->flags |= IPC_CALL_DISPATCHED; |
291 | request->flags |= IPC_CALL_DISPATCHED; |
257 | } else { |
292 | } else { |
258 | if (!(flags & IPC_WAIT_NONBLOCKING)) { |
293 | printf("WARNING: Spurious IPC wakeup.\n"); |
259 | /* Wait for event to appear */ |
- | |
260 | spinlock_unlock(&box->lock); |
294 | spinlock_unlock(&box->lock); |
261 | waitq_sleep(&box->wq); |
- | |
262 | spinlock_lock(&box->lock); |
- | |
263 | continue; |
- | |
264 | } |
- | |
265 | request = NULL; |
- | |
266 | } |
- | |
267 | break; |
295 | goto restart; |
268 | } |
296 | } |
269 | spinlock_unlock(&box->lock); |
297 | spinlock_unlock(&box->lock); |
270 | return request; |
298 | return request; |
271 | } |
299 | } |
272 | 300 | ||
273 | /** Initilize ipc subsystem */ |
301 | /** Initilize ipc subsystem */ |
274 | void ipc_init(void) |
302 | void ipc_init(void) |
275 | { |
303 | { |
276 | ipc_call_slab = slab_cache_create("ipc_call", |
304 | ipc_call_slab = slab_cache_create("ipc_call", |
277 | sizeof(call_t), |
305 | sizeof(call_t), |
278 | 0, |
306 | 0, |
279 | NULL, NULL, 0); |
307 | NULL, NULL, 0); |
280 | } |
308 | } |
281 | 309 | ||
282 | /** Cleans up all IPC communication of the given task |
310 | /** Cleans up all IPC communication of the given task |
283 | * |
311 | * |
284 | * |
312 | * |
285 | */ |
313 | */ |
286 | void ipc_cleanup(task_t *task) |
314 | void ipc_cleanup(task_t *task) |
287 | { |
315 | { |
288 | /* Cancel all calls in my dispatch queue */ |
316 | /* Cancel all calls in my dispatch queue */ |
289 | 317 | ||
290 | } |
318 | } |
291 | 319 |