Rev 3438 | Rev 3445 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3438 | Rev 3441 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jiri Svoboda |
2 | * Copyright (c) 2008 Jiri Svoboda |
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 | /** @addtogroup genericipc |
29 | /** @addtogroup genericipc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <synch/synch.h> |
35 | #include <synch/synch.h> |
36 | #include <synch/spinlock.h> |
36 | #include <synch/spinlock.h> |
37 | #include <synch/mutex.h> |
37 | #include <synch/mutex.h> |
38 | #include <ipc/ipc.h> |
38 | #include <ipc/ipc.h> |
39 | #include <ipc/ipcrsc.h> |
39 | #include <ipc/ipcrsc.h> |
40 | #include <arch.h> |
40 | #include <arch.h> |
41 | #include <errno.h> |
41 | #include <errno.h> |
42 | #include <print.h> |
42 | #include <debug.h> |
43 | #include <udebug/udebug_ipc.h> |
43 | #include <udebug/udebug_ipc.h> |
44 | #include <ipc/ipc_kbox.h> |
44 | #include <ipc/ipc_kbox.h> |
45 | 45 | ||
46 | void ipc_kbox_cleanup(void) |
46 | void ipc_kbox_cleanup(void) |
47 | { |
47 | { |
48 | bool have_kb_thread; |
48 | bool have_kb_thread; |
49 | 49 | ||
50 | /* Only hold kb_cleanup_lock while setting kb_finished - this is enough */ |
50 | /* Only hold kb_cleanup_lock while setting kb_finished - this is enough */ |
51 | mutex_lock(&TASK->kb_cleanup_lock); |
51 | mutex_lock(&TASK->kb_cleanup_lock); |
52 | TASK->kb_finished = true; |
52 | TASK->kb_finished = true; |
53 | mutex_unlock(&TASK->kb_cleanup_lock); |
53 | mutex_unlock(&TASK->kb_cleanup_lock); |
54 | 54 | ||
55 | have_kb_thread = (TASK->kb_thread != NULL); |
55 | have_kb_thread = (TASK->kb_thread != NULL); |
56 | 56 | ||
57 | /* From now on nobody will try to connect phones or attach kbox threads */ |
57 | /* From now on nobody will try to connect phones or attach kbox threads */ |
58 | 58 | ||
59 | /* |
59 | /* |
60 | * Disconnect all phones connected to our kbox. Passing true for |
60 | * Disconnect all phones connected to our kbox. Passing true for |
61 | * notify_box causes a HANGUP message to be inserted for each |
61 | * notify_box causes a HANGUP message to be inserted for each |
62 | * disconnected phone. This ensures the kbox thread is going to |
62 | * disconnected phone. This ensures the kbox thread is going to |
63 | * wake up and terminate. |
63 | * wake up and terminate. |
64 | */ |
64 | */ |
65 | ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread); |
65 | ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread); |
66 | 66 | ||
67 | if (have_kb_thread) { |
67 | if (have_kb_thread) { |
68 | printf("join kb_thread..\n"); |
68 | LOG("join kb_thread..\n"); |
69 | thread_join(TASK->kb_thread); |
69 | thread_join(TASK->kb_thread); |
70 | thread_detach(TASK->kb_thread); |
70 | thread_detach(TASK->kb_thread); |
71 | printf("join done\n"); |
71 | LOG("join done\n"); |
72 | TASK->kb_thread = NULL; |
72 | TASK->kb_thread = NULL; |
73 | } |
73 | } |
74 | 74 | ||
75 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
75 | /* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
76 | spinlock_lock(&TASK->kernel_box.lock); |
76 | spinlock_lock(&TASK->kernel_box.lock); |
77 | ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls); |
77 | ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls); |
78 | ipc_cleanup_call_list(&TASK->kernel_box.calls); |
78 | ipc_cleanup_call_list(&TASK->kernel_box.calls); |
79 | spinlock_unlock(&TASK->kernel_box.lock); |
79 | spinlock_unlock(&TASK->kernel_box.lock); |
80 | } |
80 | } |
81 | 81 | ||
82 | 82 | ||
83 | static void kbox_thread_proc(void *arg) |
83 | static void kbox_thread_proc(void *arg) |
84 | { |
84 | { |
85 | call_t *call; |
85 | call_t *call; |
86 | int method; |
86 | int method; |
87 | bool done; |
87 | bool done; |
88 | ipl_t ipl; |
88 | ipl_t ipl; |
89 | 89 | ||
90 | (void)arg; |
90 | (void)arg; |
91 | printf("kbox_thread_proc()\n"); |
91 | LOG("kbox_thread_proc()\n"); |
92 | done = false; |
92 | done = false; |
93 | 93 | ||
94 | while (!done) { |
94 | while (!done) { |
95 | //printf("kbox: wait for call\n"); |
- | |
96 | call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT, |
95 | call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT, |
97 | SYNCH_FLAGS_NONE); |
96 | SYNCH_FLAGS_NONE); |
98 | 97 | ||
99 | if (call != NULL) { |
98 | if (call != NULL) { |
100 | method = IPC_GET_METHOD(call->data); |
99 | method = IPC_GET_METHOD(call->data); |
101 | 100 | ||
102 | if (method == IPC_M_DEBUG_ALL) { |
101 | if (method == IPC_M_DEBUG_ALL) { |
103 | udebug_call_receive(call); |
102 | udebug_call_receive(call); |
104 | } |
103 | } |
105 | 104 | ||
106 | if (method == IPC_M_PHONE_HUNGUP) { |
105 | if (method == IPC_M_PHONE_HUNGUP) { |
107 | printf("kbox: handle hangup message\n"); |
106 | LOG("kbox: handle hangup message\n"); |
108 | 107 | ||
109 | /* Was it our debugger, who hung up? */ |
108 | /* Was it our debugger, who hung up? */ |
110 | if (call->sender == TASK->udebug.debugger) { |
109 | if (call->sender == TASK->udebug.debugger) { |
111 | /* Terminate debugging session (if any) */ |
110 | /* Terminate debugging session (if any) */ |
112 | printf("kbox: terminate debug session\n"); |
111 | LOG("kbox: terminate debug session\n"); |
113 | ipl = interrupts_disable(); |
112 | ipl = interrupts_disable(); |
114 | spinlock_lock(&TASK->lock); |
113 | spinlock_lock(&TASK->lock); |
115 | udebug_task_cleanup(TASK); |
114 | udebug_task_cleanup(TASK); |
116 | spinlock_unlock(&TASK->lock); |
115 | spinlock_unlock(&TASK->lock); |
117 | interrupts_restore(ipl); |
116 | interrupts_restore(ipl); |
118 | } else { |
117 | } else { |
119 | printf("kbox: was not debugger\n"); |
118 | LOG("kbox: was not debugger\n"); |
120 | } |
119 | } |
121 | 120 | ||
122 | printf("kbox: continue with hangup message\n"); |
121 | LOG("kbox: continue with hangup message\n"); |
123 | IPC_SET_RETVAL(call->data, 0); |
122 | IPC_SET_RETVAL(call->data, 0); |
124 | ipc_answer(&TASK->kernel_box, call); |
123 | ipc_answer(&TASK->kernel_box, call); |
125 | 124 | ||
126 | ipl = interrupts_disable(); |
125 | ipl = interrupts_disable(); |
127 | spinlock_lock(&TASK->lock); |
126 | spinlock_lock(&TASK->lock); |
128 | spinlock_lock(&TASK->answerbox.lock); |
127 | spinlock_lock(&TASK->answerbox.lock); |
129 | if (list_empty(&TASK->answerbox.connected_phones)) { |
128 | if (list_empty(&TASK->answerbox.connected_phones)) { |
130 | /* Last phone has been disconnected */ |
129 | /* Last phone has been disconnected */ |
131 | TASK->kb_thread = NULL; |
130 | TASK->kb_thread = NULL; |
132 | done = true; |
131 | done = true; |
133 | printf("phone list is empty\n"); |
132 | LOG("phone list is empty\n"); |
134 | } |
133 | } |
135 | spinlock_unlock(&TASK->answerbox.lock); |
134 | spinlock_unlock(&TASK->answerbox.lock); |
136 | spinlock_unlock(&TASK->lock); |
135 | spinlock_unlock(&TASK->lock); |
137 | interrupts_restore(ipl); |
136 | interrupts_restore(ipl); |
138 | } |
137 | } |
139 | } |
138 | } |
140 | } |
139 | } |
141 | 140 | ||
142 | printf("kbox: finished\n"); |
141 | LOG("kbox: finished\n"); |
143 | } |
142 | } |
144 | 143 | ||
145 | 144 | ||
146 | /** |
145 | /** |
147 | * Connect phone to a task kernel-box specified by id. |
146 | * Connect phone to a task kernel-box specified by id. |
148 | * |
147 | * |
149 | * Note that this is not completely atomic. For optimisation reasons, |
148 | * Note that this is not completely atomic. For optimisation reasons, |
150 | * The task might start cleaning up kbox after the phone has been connected |
149 | * The task might start cleaning up kbox after the phone has been connected |
151 | * and before a kbox thread has been created. This must be taken into account |
150 | * and before a kbox thread has been created. This must be taken into account |
152 | * in the cleanup code. |
151 | * in the cleanup code. |
153 | * |
152 | * |
154 | * @return Phone id on success, or negative error code. |
153 | * @return Phone id on success, or negative error code. |
155 | */ |
154 | */ |
156 | int ipc_connect_kbox(task_id_t taskid) |
155 | int ipc_connect_kbox(task_id_t taskid) |
157 | { |
156 | { |
158 | int newphid; |
157 | int newphid; |
159 | task_t *ta; |
158 | task_t *ta; |
160 | thread_t *kb_thread; |
159 | thread_t *kb_thread; |
161 | ipl_t ipl; |
160 | ipl_t ipl; |
162 | 161 | ||
163 | ipl = interrupts_disable(); |
162 | ipl = interrupts_disable(); |
164 | spinlock_lock(&tasks_lock); |
163 | spinlock_lock(&tasks_lock); |
165 | 164 | ||
166 | ta = task_find_by_id(taskid); |
165 | ta = task_find_by_id(taskid); |
167 | if (ta == NULL) { |
166 | if (ta == NULL) { |
168 | spinlock_unlock(&tasks_lock); |
167 | spinlock_unlock(&tasks_lock); |
169 | interrupts_restore(ipl); |
168 | interrupts_restore(ipl); |
170 | return ENOENT; |
169 | return ENOENT; |
171 | } |
170 | } |
172 | 171 | ||
173 | atomic_inc(&ta->refcount); |
172 | atomic_inc(&ta->refcount); |
174 | 173 | ||
175 | spinlock_unlock(&tasks_lock); |
174 | spinlock_unlock(&tasks_lock); |
176 | interrupts_restore(ipl); |
175 | interrupts_restore(ipl); |
177 | 176 | ||
178 | mutex_lock(&ta->kb_cleanup_lock); |
177 | mutex_lock(&ta->kb_cleanup_lock); |
179 | 178 | ||
180 | if (atomic_predec(&ta->refcount) == 0) { |
179 | if (atomic_predec(&ta->refcount) == 0) { |
181 | mutex_unlock(&ta->kb_cleanup_lock); |
180 | mutex_unlock(&ta->kb_cleanup_lock); |
182 | task_destroy(ta); |
181 | task_destroy(ta); |
183 | return ENOENT; |
182 | return ENOENT; |
184 | } |
183 | } |
185 | 184 | ||
186 | if (ta->kb_finished != false) { |
185 | if (ta->kb_finished != false) { |
187 | mutex_unlock(&ta->kb_cleanup_lock); |
186 | mutex_unlock(&ta->kb_cleanup_lock); |
188 | return EINVAL; |
187 | return EINVAL; |
189 | } |
188 | } |
190 | 189 | ||
191 | newphid = phone_alloc(); |
190 | newphid = phone_alloc(); |
192 | if (newphid < 0) { |
191 | if (newphid < 0) { |
193 | mutex_unlock(&ta->kb_cleanup_lock); |
192 | mutex_unlock(&ta->kb_cleanup_lock); |
194 | return ELIMIT; |
193 | return ELIMIT; |
195 | } |
194 | } |
196 | 195 | ||
197 | /* Connect the newly allocated phone to the kbox */ |
196 | /* Connect the newly allocated phone to the kbox */ |
198 | ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box); |
197 | ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box); |
199 | 198 | ||
200 | if (ta->kb_thread != NULL) { |
199 | if (ta->kb_thread != NULL) { |
201 | mutex_unlock(&ta->kb_cleanup_lock); |
200 | mutex_unlock(&ta->kb_cleanup_lock); |
202 | return newphid; |
201 | return newphid; |
203 | } |
202 | } |
204 | 203 | ||
205 | /* Create a kbox thread */ |
204 | /* Create a kbox thread */ |
206 | kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false); |
205 | kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false); |
207 | if (!kb_thread) { |
206 | if (!kb_thread) { |
208 | mutex_unlock(&ta->kb_cleanup_lock); |
207 | mutex_unlock(&ta->kb_cleanup_lock); |
209 | return ENOMEM; |
208 | return ENOMEM; |
210 | } |
209 | } |
211 | 210 | ||
212 | ta->kb_thread = kb_thread; |
211 | ta->kb_thread = kb_thread; |
213 | thread_ready(kb_thread); |
212 | thread_ready(kb_thread); |
214 | 213 | ||
215 | mutex_unlock(&ta->kb_cleanup_lock); |
214 | mutex_unlock(&ta->kb_cleanup_lock); |
216 | 215 | ||
217 | return newphid; |
216 | return newphid; |
218 | } |
217 | } |
219 | 218 | ||
220 | /** @} |
219 | /** @} |
221 | */ |
220 | */ |
222 | 221 |