Rev 1040 | Rev 1072 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1040 | Rev 1050 | ||
---|---|---|---|
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 | /** Allocate & initialize call structure |
52 | /** Allocate & initialize call structure |
53 | * |
53 | * |
54 | * The call is initialized, so that the reply will be directed |
54 | * The call is initialized, so that the reply will be directed |
55 | * to TASK->answerbox |
55 | * to TASK->answerbox |
56 | */ |
56 | */ |
57 | call_t * ipc_call_alloc(void) |
57 | call_t * ipc_call_alloc(void) |
58 | { |
58 | { |
59 | call_t *call; |
59 | call_t *call; |
60 | 60 | ||
61 | call = slab_alloc(ipc_call_slab, 0); |
61 | call = slab_alloc(ipc_call_slab, 0); |
62 | memsetb((__address)call, sizeof(*call), 0); |
62 | memsetb((__address)call, sizeof(*call), 0); |
63 | call->callerbox = &TASK->answerbox; |
63 | call->callerbox = &TASK->answerbox; |
64 | call->sender = TASK; |
64 | call->sender = TASK; |
65 | 65 | ||
66 | return call; |
66 | return call; |
67 | } |
67 | } |
68 | 68 | ||
69 | /** Initialize allocated call */ |
69 | /** Initialize allocated call */ |
70 | void ipc_call_init(call_t *call) |
70 | void ipc_call_init(call_t *call) |
71 | { |
71 | { |
72 | call->callerbox = &TASK->answerbox; |
72 | call->callerbox = &TASK->answerbox; |
73 | call->flags = IPC_CALL_STATIC_ALLOC; |
73 | call->flags = IPC_CALL_STATIC_ALLOC; |
74 | call->sender = TASK; |
74 | call->sender = TASK; |
75 | } |
75 | } |
76 | 76 | ||
77 | /** Deallocate call stracuture */ |
77 | /** Deallocate call stracuture */ |
78 | void ipc_call_free(call_t *call) |
78 | void ipc_call_free(call_t *call) |
79 | { |
79 | { |
80 | slab_free(ipc_call_slab, call); |
80 | slab_free(ipc_call_slab, call); |
81 | } |
81 | } |
82 | 82 | ||
83 | /** Initialize answerbox structure |
83 | /** Initialize answerbox structure |
84 | */ |
84 | */ |
85 | void ipc_answerbox_init(answerbox_t *box) |
85 | void ipc_answerbox_init(answerbox_t *box) |
86 | { |
86 | { |
87 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
87 | spinlock_initialize(&box->lock, "ipc_box_lock"); |
88 | waitq_initialize(&box->wq); |
88 | waitq_initialize(&box->wq); |
89 | list_initialize(&box->connected_phones); |
89 | list_initialize(&box->connected_phones); |
90 | list_initialize(&box->calls); |
90 | list_initialize(&box->calls); |
91 | list_initialize(&box->dispatched_calls); |
91 | list_initialize(&box->dispatched_calls); |
92 | list_initialize(&box->answers); |
92 | list_initialize(&box->answers); |
93 | box->task = TASK; |
93 | box->task = TASK; |
94 | } |
94 | } |
95 | 95 | ||
96 | /** Connect phone to answerbox */ |
96 | /** Connect phone to answerbox */ |
97 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
97 | void ipc_phone_connect(phone_t *phone, answerbox_t *box) |
98 | { |
98 | { |
99 | ASSERT(!phone->callee); |
99 | ASSERT(!phone->callee); |
100 | phone->busy = 1; |
100 | phone->busy = 1; |
101 | phone->callee = box; |
101 | phone->callee = box; |
102 | 102 | ||
103 | spinlock_lock(&box->lock); |
103 | spinlock_lock(&box->lock); |
104 | list_append(&phone->list, &box->connected_phones); |
104 | list_append(&phone->list, &box->connected_phones); |
105 | spinlock_unlock(&box->lock); |
105 | spinlock_unlock(&box->lock); |
106 | } |
106 | } |
107 | 107 | ||
108 | /** Initialize phone structure and connect phone to naswerbox |
108 | /** Initialize phone structure and connect phone to naswerbox |
109 | */ |
109 | */ |
110 | void ipc_phone_init(phone_t *phone) |
110 | void ipc_phone_init(phone_t *phone) |
111 | { |
111 | { |
112 | spinlock_initialize(&phone->lock, "phone_lock"); |
112 | spinlock_initialize(&phone->lock, "phone_lock"); |
113 | phone->callee = NULL; |
113 | phone->callee = NULL; |
114 | phone->busy = 0; |
114 | phone->busy = 0; |
115 | } |
115 | } |
116 | 116 | ||
117 | /** Disconnect phone from answerbox */ |
117 | /** Disconnect phone from answerbox */ |
118 | void ipc_phone_destroy(phone_t *phone) |
118 | void ipc_phone_destroy(phone_t *phone) |
119 | { |
119 | { |
120 | answerbox_t *box = phone->callee; |
120 | answerbox_t *box = phone->callee; |
121 | 121 | ||
122 | ASSERT(box); |
122 | ASSERT(box); |
123 | 123 | ||
124 | spinlock_lock(&box->lock); |
124 | spinlock_lock(&box->lock); |
125 | list_remove(&phone->list); |
125 | list_remove(&phone->list); |
126 | spinlock_unlock(&box->lock); |
126 | spinlock_unlock(&box->lock); |
127 | } |
127 | } |
128 | 128 | ||
129 | /** Helper function to facilitate synchronous calls */ |
129 | /** Helper function to facilitate synchronous calls */ |
130 | void ipc_call_sync(phone_t *phone, call_t *request) |
130 | void ipc_call_sync(phone_t *phone, call_t *request) |
131 | { |
131 | { |
132 | answerbox_t sync_box; |
132 | answerbox_t sync_box; |
133 | 133 | ||
134 | ipc_answerbox_init(&sync_box); |
134 | ipc_answerbox_init(&sync_box); |
135 | 135 | ||
136 | /* We will receive data on special box */ |
136 | /* We will receive data on special box */ |
137 | request->callerbox = &sync_box; |
137 | request->callerbox = &sync_box; |
138 | 138 | ||
139 | ipc_call(phone, request); |
139 | ipc_call(phone, request); |
140 | ipc_wait_for_call(&sync_box, 0); |
140 | ipc_wait_for_call(&sync_box, 0); |
141 | } |
141 | } |
142 | 142 | ||
143 | /** Send a asynchronous request using phone to answerbox |
143 | /** Send a asynchronous request using phone to answerbox |
144 | * |
144 | * |
145 | * @param phone Phone connected to answerbox |
145 | * @param phone Phone connected to answerbox |
146 | * @param request Request to be sent |
146 | * @param request Request to be sent |
147 | */ |
147 | */ |
148 | void ipc_call(phone_t *phone, call_t *call) |
148 | void ipc_call(phone_t *phone, call_t *call) |
149 | { |
149 | { |
150 | answerbox_t *box = phone->callee; |
150 | answerbox_t *box = phone->callee; |
151 | 151 | ||
152 | ASSERT(box); |
152 | ASSERT(box); |
153 | 153 | ||
154 | spinlock_lock(&box->lock); |
154 | spinlock_lock(&box->lock); |
155 | list_append(&call->list, &box->calls); |
155 | list_append(&call->list, &box->calls); |
156 | spinlock_unlock(&box->lock); |
156 | spinlock_unlock(&box->lock); |
157 | waitq_wakeup(&box->wq, 0); |
157 | waitq_wakeup(&box->wq, 0); |
158 | } |
158 | } |
159 | 159 | ||
160 | /** Forwards call from one answerbox to a new one |
160 | /** Forwards call from one answerbox to a new one |
161 | * |
161 | * |
162 | * @param request Request to be forwarded |
162 | * @param request Request to be forwarded |
163 | * @param newbox Target answerbox |
163 | * @param newbox Target answerbox |
164 | * @param oldbox Old answerbox |
164 | * @param oldbox Old answerbox |
165 | */ |
165 | */ |
166 | void ipc_forward(call_t *call, answerbox_t *newbox, answerbox_t *oldbox) |
166 | void ipc_forward(call_t *call, answerbox_t *newbox, answerbox_t *oldbox) |
167 | { |
167 | { |
168 | spinlock_lock(&oldbox->lock); |
168 | spinlock_lock(&oldbox->lock); |
169 | list_remove(&call->list); |
169 | list_remove(&call->list); |
170 | spinlock_unlock(&oldbox->lock); |
170 | spinlock_unlock(&oldbox->lock); |
171 | 171 | ||
172 | spinlock_lock(&newbox->lock); |
172 | spinlock_lock(&newbox->lock); |
173 | list_append(&call->list, &newbox->calls); |
173 | list_append(&call->list, &newbox->calls); |
174 | spinlock_lock(&newbox->lock); |
174 | spinlock_lock(&newbox->lock); |
175 | waitq_wakeup(&newbox->wq, 0); |
175 | waitq_wakeup(&newbox->wq, 0); |
176 | } |
176 | } |
177 | 177 | ||
178 | /** Answer message back to phone |
178 | /** Answer message back to phone |
179 | * |
179 | * |
180 | * @param box Answerbox that is answering the message |
180 | * @param box Answerbox that is answering the message |
181 | * @param request Modified request that is being sent back |
181 | * @param request Modified request that is being sent back |
182 | */ |
182 | */ |
183 | void ipc_answer(answerbox_t *box, call_t *request) |
183 | void ipc_answer(answerbox_t *box, call_t *request) |
184 | { |
184 | { |
185 | answerbox_t *callerbox = request->callerbox; |
185 | answerbox_t *callerbox = request->callerbox; |
186 | 186 | ||
- | 187 | request->flags &= ~IPC_CALL_DISPATCHED; |
|
187 | request->flags |= IPC_CALL_ANSWERED; |
188 | request->flags |= IPC_CALL_ANSWERED; |
188 | 189 | ||
189 | spinlock_lock(&box->lock); |
190 | spinlock_lock(&box->lock); |
190 | list_remove(&request->list); |
191 | list_remove(&request->list); |
191 | spinlock_unlock(&box->lock); |
192 | spinlock_unlock(&box->lock); |
192 | 193 | ||
193 | spinlock_lock(&callerbox->lock); |
194 | spinlock_lock(&callerbox->lock); |
194 | list_append(&request->list, &callerbox->answers); |
195 | list_append(&request->list, &callerbox->answers); |
195 | spinlock_unlock(&callerbox->lock); |
196 | spinlock_unlock(&callerbox->lock); |
196 | waitq_wakeup(&callerbox->wq, 0); |
197 | waitq_wakeup(&callerbox->wq, 0); |
197 | } |
198 | } |
198 | 199 | ||
199 | /** Wait for phone call |
200 | /** Wait for phone call |
200 | * |
201 | * |
201 | * @return Recived message address |
202 | * @return Recived message address |
202 | * - to distinguish between call and answer, look at call->flags |
203 | * - to distinguish between call and answer, look at call->flags |
203 | */ |
204 | */ |
204 | call_t * ipc_wait_for_call(answerbox_t *box, int flags) |
205 | call_t * ipc_wait_for_call(answerbox_t *box, int flags) |
205 | { |
206 | { |
206 | call_t *request; |
207 | call_t *request; |
207 | 208 | ||
208 | spinlock_lock(&box->lock); |
209 | spinlock_lock(&box->lock); |
209 | while (1) { |
210 | while (1) { |
210 | if (!list_empty(&box->answers)) { |
211 | if (!list_empty(&box->answers)) { |
211 | /* Handle asynchronous answers */ |
212 | /* Handle asynchronous answers */ |
212 | request = list_get_instance(box->answers.next, call_t, list); |
213 | request = list_get_instance(box->answers.next, call_t, list); |
213 | list_remove(&request->list); |
214 | list_remove(&request->list); |
214 | } else if (!list_empty(&box->calls)) { |
215 | } else if (!list_empty(&box->calls)) { |
215 | /* Handle requests */ |
216 | /* Handle requests */ |
216 | request = list_get_instance(box->calls.next, call_t, list); |
217 | request = list_get_instance(box->calls.next, call_t, list); |
217 | list_remove(&request->list); |
218 | list_remove(&request->list); |
218 | /* Append request to dispatch queue */ |
219 | /* Append request to dispatch queue */ |
219 | list_append(&request->list, &box->dispatched_calls); |
220 | list_append(&request->list, &box->dispatched_calls); |
- | 221 | request->flags |= IPC_CALL_DISPATCHED; |
|
220 | } else { |
222 | } else { |
221 | if (!(flags & IPC_WAIT_NONBLOCKING)) { |
223 | if (!(flags & IPC_WAIT_NONBLOCKING)) { |
222 | /* Wait for event to appear */ |
224 | /* Wait for event to appear */ |
223 | spinlock_unlock(&box->lock); |
225 | spinlock_unlock(&box->lock); |
224 | waitq_sleep(&box->wq); |
226 | waitq_sleep(&box->wq); |
225 | spinlock_lock(&box->lock); |
227 | spinlock_lock(&box->lock); |
226 | continue; |
228 | continue; |
227 | } |
229 | } |
228 | request = NULL; |
230 | request = NULL; |
229 | } |
231 | } |
230 | break; |
232 | break; |
231 | } |
233 | } |
232 | spinlock_unlock(&box->lock); |
234 | spinlock_unlock(&box->lock); |
233 | return request; |
235 | return request; |
234 | } |
236 | } |
235 | 237 | ||
236 | /** Initilize ipc subsystem */ |
238 | /** Initilize ipc subsystem */ |
237 | void ipc_init(void) |
239 | void ipc_init(void) |
238 | { |
240 | { |
239 | ipc_call_slab = slab_cache_create("ipc_call", |
241 | ipc_call_slab = slab_cache_create("ipc_call", |
240 | sizeof(call_t), |
242 | sizeof(call_t), |
241 | 0, |
243 | 0, |
242 | NULL, NULL, 0); |
244 | NULL, NULL, 0); |
243 | } |
245 | } |
244 | 246 |