Rev 1864 | Rev 2000 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1864 | Rev 1880 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
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 genericproc |
29 | /** @addtogroup genericproc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Task management. |
35 | * @brief Task management. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <main/uinit.h> |
38 | #include <main/uinit.h> |
39 | #include <proc/thread.h> |
39 | #include <proc/thread.h> |
40 | #include <proc/task.h> |
40 | #include <proc/task.h> |
41 | #include <proc/uarg.h> |
41 | #include <proc/uarg.h> |
42 | #include <mm/as.h> |
42 | #include <mm/as.h> |
43 | #include <mm/slab.h> |
43 | #include <mm/slab.h> |
44 | #include <synch/spinlock.h> |
44 | #include <synch/spinlock.h> |
45 | #include <arch.h> |
45 | #include <arch.h> |
46 | #include <panic.h> |
46 | #include <panic.h> |
47 | #include <adt/btree.h> |
47 | #include <adt/btree.h> |
48 | #include <adt/list.h> |
48 | #include <adt/list.h> |
49 | #include <ipc/ipc.h> |
49 | #include <ipc/ipc.h> |
50 | #include <security/cap.h> |
50 | #include <security/cap.h> |
51 | #include <memstr.h> |
51 | #include <memstr.h> |
52 | #include <print.h> |
52 | #include <print.h> |
53 | #include <elf.h> |
53 | #include <elf.h> |
54 | #include <errno.h> |
54 | #include <errno.h> |
55 | #include <syscall/copy.h> |
55 | #include <syscall/copy.h> |
56 | #include <console/klog.h> |
56 | #include <console/klog.h> |
57 | 57 | ||
58 | #ifndef LOADED_PROG_STACK_PAGES_NO |
58 | #ifndef LOADED_PROG_STACK_PAGES_NO |
59 | #define LOADED_PROG_STACK_PAGES_NO 1 |
59 | #define LOADED_PROG_STACK_PAGES_NO 1 |
60 | #endif |
60 | #endif |
61 | 61 | ||
62 | /** Spinlock protecting the tasks_btree B+tree. */ |
62 | /** Spinlock protecting the tasks_btree B+tree. */ |
63 | SPINLOCK_INITIALIZE(tasks_lock); |
63 | SPINLOCK_INITIALIZE(tasks_lock); |
64 | 64 | ||
65 | /** B+tree of active tasks. |
65 | /** B+tree of active tasks. |
66 | * |
66 | * |
67 | * The task is guaranteed to exist after it was found in the tasks_btree as long as: |
67 | * The task is guaranteed to exist after it was found in the tasks_btree as long as: |
68 | * @li the tasks_lock is held, |
68 | * @li the tasks_lock is held, |
69 | * @li the task's lock is held when task's lock is acquired before releasing tasks_lock or |
69 | * @li the task's lock is held when task's lock is acquired before releasing tasks_lock or |
70 | * @li the task's refcount is grater than 0 |
70 | * @li the task's refcount is greater than 0 |
71 | * |
71 | * |
72 | */ |
72 | */ |
73 | btree_t tasks_btree; |
73 | btree_t tasks_btree; |
74 | 74 | ||
75 | static task_id_t task_counter = 0; |
75 | static task_id_t task_counter = 0; |
76 | 76 | ||
77 | static void ktaskclnp(void *arg); |
77 | static void ktaskclnp(void *arg); |
78 | static void ktaskgc(void *arg); |
78 | static void ktaskgc(void *arg); |
79 | 79 | ||
80 | /** Initialize tasks |
80 | /** Initialize tasks |
81 | * |
81 | * |
82 | * Initialize kernel tasks support. |
82 | * Initialize kernel tasks support. |
83 | * |
83 | * |
84 | */ |
84 | */ |
85 | void task_init(void) |
85 | void task_init(void) |
86 | { |
86 | { |
87 | TASK = NULL; |
87 | TASK = NULL; |
88 | btree_create(&tasks_btree); |
88 | btree_create(&tasks_btree); |
89 | } |
89 | } |
90 | 90 | ||
91 | 91 | ||
92 | /** Create new task |
92 | /** Create new task |
93 | * |
93 | * |
94 | * Create new task with no threads. |
94 | * Create new task with no threads. |
95 | * |
95 | * |
96 | * @param as Task's address space. |
96 | * @param as Task's address space. |
97 | * @param name Symbolic name. |
97 | * @param name Symbolic name. |
98 | * |
98 | * |
99 | * @return New task's structure |
99 | * @return New task's structure |
100 | * |
100 | * |
101 | */ |
101 | */ |
102 | task_t *task_create(as_t *as, char *name) |
102 | task_t *task_create(as_t *as, char *name) |
103 | { |
103 | { |
104 | ipl_t ipl; |
104 | ipl_t ipl; |
105 | task_t *ta; |
105 | task_t *ta; |
106 | int i; |
106 | int i; |
107 | 107 | ||
108 | ta = (task_t *) malloc(sizeof(task_t), 0); |
108 | ta = (task_t *) malloc(sizeof(task_t), 0); |
109 | 109 | ||
110 | task_create_arch(ta); |
110 | task_create_arch(ta); |
111 | 111 | ||
112 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
112 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
113 | list_initialize(&ta->th_head); |
113 | list_initialize(&ta->th_head); |
114 | ta->as = as; |
114 | ta->as = as; |
115 | ta->name = name; |
115 | ta->name = name; |
116 | ta->main_thread = NULL; |
116 | ta->main_thread = NULL; |
117 | ta->refcount = 0; |
117 | ta->refcount = 0; |
118 | ta->context = CONTEXT; |
118 | ta->context = CONTEXT; |
119 | 119 | ||
120 | ta->capabilities = 0; |
120 | ta->capabilities = 0; |
121 | ta->accept_new_threads = true; |
121 | ta->accept_new_threads = true; |
122 | 122 | ||
123 | ipc_answerbox_init(&ta->answerbox); |
123 | ipc_answerbox_init(&ta->answerbox); |
124 | for (i = 0; i < IPC_MAX_PHONES; i++) |
124 | for (i = 0; i < IPC_MAX_PHONES; i++) |
125 | ipc_phone_init(&ta->phones[i]); |
125 | ipc_phone_init(&ta->phones[i]); |
126 | if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, ta->context))) |
126 | if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, ta->context))) |
127 | ipc_phone_connect(&ta->phones[0], ipc_phone_0); |
127 | ipc_phone_connect(&ta->phones[0], ipc_phone_0); |
128 | atomic_set(&ta->active_calls, 0); |
128 | atomic_set(&ta->active_calls, 0); |
129 | 129 | ||
130 | mutex_initialize(&ta->futexes_lock); |
130 | mutex_initialize(&ta->futexes_lock); |
131 | btree_create(&ta->futexes); |
131 | btree_create(&ta->futexes); |
132 | 132 | ||
133 | ipl = interrupts_disable(); |
133 | ipl = interrupts_disable(); |
134 | 134 | ||
135 | /* |
135 | /* |
136 | * Increment address space reference count. |
136 | * Increment address space reference count. |
137 | * TODO: Reconsider the locking scheme. |
137 | * TODO: Reconsider the locking scheme. |
138 | */ |
138 | */ |
139 | mutex_lock(&as->lock); |
139 | mutex_lock(&as->lock); |
140 | as->refcount++; |
140 | as->refcount++; |
141 | mutex_unlock(&as->lock); |
141 | mutex_unlock(&as->lock); |
142 | 142 | ||
143 | spinlock_lock(&tasks_lock); |
143 | spinlock_lock(&tasks_lock); |
144 | 144 | ||
145 | ta->taskid = ++task_counter; |
145 | ta->taskid = ++task_counter; |
146 | btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL); |
146 | btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL); |
147 | 147 | ||
148 | spinlock_unlock(&tasks_lock); |
148 | spinlock_unlock(&tasks_lock); |
149 | interrupts_restore(ipl); |
149 | interrupts_restore(ipl); |
150 | 150 | ||
151 | return ta; |
151 | return ta; |
152 | } |
152 | } |
153 | 153 | ||
154 | /** Destroy task. |
154 | /** Destroy task. |
155 | * |
155 | * |
156 | * @param t Task to be destroyed. |
156 | * @param t Task to be destroyed. |
157 | */ |
157 | */ |
158 | void task_destroy(task_t *t) |
158 | void task_destroy(task_t *t) |
159 | { |
159 | { |
160 | task_destroy_arch(t); |
160 | task_destroy_arch(t); |
161 | btree_destroy(&t->futexes); |
161 | btree_destroy(&t->futexes); |
162 | 162 | ||
163 | mutex_lock_active(&t->as->lock); |
163 | mutex_lock_active(&t->as->lock); |
164 | if (--t->as->refcount == 0) { |
164 | if (--t->as->refcount == 0) { |
165 | mutex_unlock(&t->as->lock); |
165 | mutex_unlock(&t->as->lock); |
166 | as_destroy(t->as); |
166 | as_destroy(t->as); |
167 | /* |
167 | /* |
168 | * t->as is destroyed. |
168 | * t->as is destroyed. |
169 | */ |
169 | */ |
170 | } else { |
170 | } else { |
171 | mutex_unlock(&t->as->lock); |
171 | mutex_unlock(&t->as->lock); |
172 | } |
172 | } |
173 | 173 | ||
174 | free(t); |
174 | free(t); |
175 | TASK = NULL; |
175 | TASK = NULL; |
176 | } |
176 | } |
177 | 177 | ||
178 | /** Create new task with 1 thread and run it |
178 | /** Create new task with 1 thread and run it |
179 | * |
179 | * |
180 | * @param program_addr Address of program executable image. |
180 | * @param program_addr Address of program executable image. |
181 | * @param name Program name. |
181 | * @param name Program name. |
182 | * |
182 | * |
183 | * @return Task of the running program or NULL on error. |
183 | * @return Task of the running program or NULL on error. |
184 | */ |
184 | */ |
185 | task_t * task_run_program(void *program_addr, char *name) |
185 | task_t * task_run_program(void *program_addr, char *name) |
186 | { |
186 | { |
187 | as_t *as; |
187 | as_t *as; |
188 | as_area_t *a; |
188 | as_area_t *a; |
189 | int rc; |
189 | int rc; |
190 | thread_t *t1, *t2; |
190 | thread_t *t1, *t2; |
191 | task_t *task; |
191 | task_t *task; |
192 | uspace_arg_t *kernel_uarg; |
192 | uspace_arg_t *kernel_uarg; |
193 | 193 | ||
194 | as = as_create(0); |
194 | as = as_create(0); |
195 | ASSERT(as); |
195 | ASSERT(as); |
196 | 196 | ||
197 | rc = elf_load((elf_header_t *) program_addr, as); |
197 | rc = elf_load((elf_header_t *) program_addr, as); |
198 | if (rc != EE_OK) { |
198 | if (rc != EE_OK) { |
199 | as_destroy(as); |
199 | as_destroy(as); |
200 | return NULL; |
200 | return NULL; |
201 | } |
201 | } |
202 | 202 | ||
203 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
203 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
204 | kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry; |
204 | kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry; |
205 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
205 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
206 | kernel_uarg->uspace_thread_function = NULL; |
206 | kernel_uarg->uspace_thread_function = NULL; |
207 | kernel_uarg->uspace_thread_arg = NULL; |
207 | kernel_uarg->uspace_thread_arg = NULL; |
208 | kernel_uarg->uspace_uarg = NULL; |
208 | kernel_uarg->uspace_uarg = NULL; |
209 | 209 | ||
210 | task = task_create(as, name); |
210 | task = task_create(as, name); |
211 | ASSERT(task); |
211 | ASSERT(task); |
212 | 212 | ||
213 | /* |
213 | /* |
214 | * Create the data as_area. |
214 | * Create the data as_area. |
215 | */ |
215 | */ |
216 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
216 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
217 | LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE, |
217 | LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE, |
218 | USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL); |
218 | USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL); |
219 | 219 | ||
220 | /* |
220 | /* |
221 | * Create the main thread. |
221 | * Create the main thread. |
222 | */ |
222 | */ |
223 | t1 = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE, "uinit"); |
223 | t1 = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE, "uinit"); |
224 | ASSERT(t1); |
224 | ASSERT(t1); |
225 | 225 | ||
226 | /* |
226 | /* |
227 | * Create killer thread for the new task. |
227 | * Create killer thread for the new task. |
228 | */ |
228 | */ |
229 | t2 = thread_create(ktaskgc, t1, task, 0, "ktaskgc"); |
229 | t2 = thread_create(ktaskgc, t1, task, 0, "ktaskgc"); |
230 | ASSERT(t2); |
230 | ASSERT(t2); |
231 | thread_ready(t2); |
231 | thread_ready(t2); |
232 | 232 | ||
233 | thread_ready(t1); |
233 | thread_ready(t1); |
234 | 234 | ||
235 | return task; |
235 | return task; |
236 | } |
236 | } |
237 | 237 | ||
238 | /** Syscall for reading task ID from userspace. |
238 | /** Syscall for reading task ID from userspace. |
239 | * |
239 | * |
240 | * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID. |
240 | * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID. |
241 | * |
241 | * |
242 | * @return 0 on success or an error code from @ref errno.h. |
242 | * @return 0 on success or an error code from @ref errno.h. |
243 | */ |
243 | */ |
244 | unative_t sys_task_get_id(task_id_t *uspace_task_id) |
244 | unative_t sys_task_get_id(task_id_t *uspace_task_id) |
245 | { |
245 | { |
246 | /* |
246 | /* |
247 | * No need to acquire lock on TASK because taskid |
247 | * No need to acquire lock on TASK because taskid |
248 | * remains constant for the lifespan of the task. |
248 | * remains constant for the lifespan of the task. |
249 | */ |
249 | */ |
250 | return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid)); |
250 | return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid)); |
251 | } |
251 | } |
252 | 252 | ||
253 | /** Find task structure corresponding to task ID. |
253 | /** Find task structure corresponding to task ID. |
254 | * |
254 | * |
255 | * The tasks_lock must be already held by the caller of this function |
255 | * The tasks_lock must be already held by the caller of this function |
256 | * and interrupts must be disabled. |
256 | * and interrupts must be disabled. |
257 | * |
257 | * |
258 | * @param id Task ID. |
258 | * @param id Task ID. |
259 | * |
259 | * |
260 | * @return Task structure address or NULL if there is no such task ID. |
260 | * @return Task structure address or NULL if there is no such task ID. |
261 | */ |
261 | */ |
262 | task_t *task_find_by_id(task_id_t id) |
262 | task_t *task_find_by_id(task_id_t id) |
263 | { |
263 | { |
264 | btree_node_t *leaf; |
264 | btree_node_t *leaf; |
265 | 265 | ||
266 | return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf); |
266 | return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf); |
267 | } |
267 | } |
268 | 268 | ||
269 | /** Kill task. |
269 | /** Kill task. |
270 | * |
270 | * |
271 | * @param id ID of the task to be killed. |
271 | * @param id ID of the task to be killed. |
272 | * |
272 | * |
273 | * @return 0 on success or an error code from errno.h |
273 | * @return 0 on success or an error code from errno.h |
274 | */ |
274 | */ |
275 | int task_kill(task_id_t id) |
275 | int task_kill(task_id_t id) |
276 | { |
276 | { |
277 | ipl_t ipl; |
277 | ipl_t ipl; |
278 | task_t *ta; |
278 | task_t *ta; |
279 | thread_t *t; |
279 | thread_t *t; |
280 | link_t *cur; |
280 | link_t *cur; |
281 | 281 | ||
282 | if (id == 1) |
282 | if (id == 1) |
283 | return EPERM; |
283 | return EPERM; |
284 | 284 | ||
285 | ipl = interrupts_disable(); |
285 | ipl = interrupts_disable(); |
286 | spinlock_lock(&tasks_lock); |
286 | spinlock_lock(&tasks_lock); |
287 | 287 | ||
288 | if (!(ta = task_find_by_id(id))) { |
288 | if (!(ta = task_find_by_id(id))) { |
289 | spinlock_unlock(&tasks_lock); |
289 | spinlock_unlock(&tasks_lock); |
290 | interrupts_restore(ipl); |
290 | interrupts_restore(ipl); |
291 | return ENOENT; |
291 | return ENOENT; |
292 | } |
292 | } |
293 | 293 | ||
294 | spinlock_lock(&ta->lock); |
294 | spinlock_lock(&ta->lock); |
295 | ta->refcount++; |
295 | ta->refcount++; |
296 | spinlock_unlock(&ta->lock); |
296 | spinlock_unlock(&ta->lock); |
297 | 297 | ||
298 | btree_remove(&tasks_btree, ta->taskid, NULL); |
298 | btree_remove(&tasks_btree, ta->taskid, NULL); |
299 | spinlock_unlock(&tasks_lock); |
299 | spinlock_unlock(&tasks_lock); |
300 | 300 | ||
301 | t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp"); |
301 | t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp"); |
302 | 302 | ||
303 | spinlock_lock(&ta->lock); |
303 | spinlock_lock(&ta->lock); |
304 | ta->accept_new_threads = false; |
304 | ta->accept_new_threads = false; |
305 | ta->refcount--; |
305 | ta->refcount--; |
306 | 306 | ||
307 | /* |
307 | /* |
308 | * Interrupt all threads except ktaskclnp. |
308 | * Interrupt all threads except ktaskclnp. |
309 | */ |
309 | */ |
310 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
310 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
311 | thread_t *thr; |
311 | thread_t *thr; |
312 | bool sleeping = false; |
312 | bool sleeping = false; |
313 | 313 | ||
314 | thr = list_get_instance(cur, thread_t, th_link); |
314 | thr = list_get_instance(cur, thread_t, th_link); |
315 | if (thr == t) |
315 | if (thr == t) |
316 | continue; |
316 | continue; |
317 | 317 | ||
318 | spinlock_lock(&thr->lock); |
318 | spinlock_lock(&thr->lock); |
319 | thr->interrupted = true; |
319 | thr->interrupted = true; |
320 | if (thr->state == Sleeping) |
320 | if (thr->state == Sleeping) |
321 | sleeping = true; |
321 | sleeping = true; |
322 | spinlock_unlock(&thr->lock); |
322 | spinlock_unlock(&thr->lock); |
323 | 323 | ||
324 | if (sleeping) |
324 | if (sleeping) |
325 | waitq_interrupt_sleep(thr); |
325 | waitq_interrupt_sleep(thr); |
326 | } |
326 | } |
327 | 327 | ||
328 | spinlock_unlock(&ta->lock); |
328 | spinlock_unlock(&ta->lock); |
329 | interrupts_restore(ipl); |
329 | interrupts_restore(ipl); |
330 | 330 | ||
331 | if (t) |
331 | if (t) |
332 | thread_ready(t); |
332 | thread_ready(t); |
333 | 333 | ||
334 | return 0; |
334 | return 0; |
335 | } |
335 | } |
336 | 336 | ||
337 | /** Print task list */ |
337 | /** Print task list */ |
338 | void task_print_list(void) |
338 | void task_print_list(void) |
339 | { |
339 | { |
340 | link_t *cur; |
340 | link_t *cur; |
341 | ipl_t ipl; |
341 | ipl_t ipl; |
342 | 342 | ||
343 | /* Messing with thread structures, avoid deadlock */ |
343 | /* Messing with thread structures, avoid deadlock */ |
344 | ipl = interrupts_disable(); |
344 | ipl = interrupts_disable(); |
345 | spinlock_lock(&tasks_lock); |
345 | spinlock_lock(&tasks_lock); |
346 | 346 | ||
347 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) { |
347 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) { |
348 | btree_node_t *node; |
348 | btree_node_t *node; |
349 | int i; |
349 | int i; |
350 | 350 | ||
351 | node = list_get_instance(cur, btree_node_t, leaf_link); |
351 | node = list_get_instance(cur, btree_node_t, leaf_link); |
352 | for (i = 0; i < node->keys; i++) { |
352 | for (i = 0; i < node->keys; i++) { |
353 | task_t *t; |
353 | task_t *t; |
354 | int j; |
354 | int j; |
355 | 355 | ||
356 | t = (task_t *) node->value[i]; |
356 | t = (task_t *) node->value[i]; |
357 | 357 | ||
358 | spinlock_lock(&t->lock); |
358 | spinlock_lock(&t->lock); |
359 | printf("%s(%lld): context=%ld, address=%#zx, as=%#zx, ActiveCalls: %zd", |
359 | printf("%s(%lld): context=%ld, address=%#zx, as=%#zx, ActiveCalls: %zd", |
360 | t->name, t->taskid, t->context, t, t->as, atomic_get(&t->active_calls)); |
360 | t->name, t->taskid, t->context, t, t->as, atomic_get(&t->active_calls)); |
361 | for (j=0; j < IPC_MAX_PHONES; j++) { |
361 | for (j=0; j < IPC_MAX_PHONES; j++) { |
362 | if (t->phones[j].callee) |
362 | if (t->phones[j].callee) |
363 | printf(" Ph(%zd): %#zx ", j, t->phones[j].callee); |
363 | printf(" Ph(%zd): %#zx ", j, t->phones[j].callee); |
364 | } |
364 | } |
365 | printf("\n"); |
365 | printf("\n"); |
366 | spinlock_unlock(&t->lock); |
366 | spinlock_unlock(&t->lock); |
367 | } |
367 | } |
368 | } |
368 | } |
369 | 369 | ||
370 | spinlock_unlock(&tasks_lock); |
370 | spinlock_unlock(&tasks_lock); |
371 | interrupts_restore(ipl); |
371 | interrupts_restore(ipl); |
372 | } |
372 | } |
373 | 373 | ||
374 | /** Kernel thread used to cleanup the task after it is killed. */ |
374 | /** Kernel thread used to cleanup the task after it is killed. */ |
375 | void ktaskclnp(void *arg) |
375 | void ktaskclnp(void *arg) |
376 | { |
376 | { |
377 | ipl_t ipl; |
377 | ipl_t ipl; |
378 | thread_t *t = NULL, *main_thread; |
378 | thread_t *t = NULL, *main_thread; |
379 | link_t *cur; |
379 | link_t *cur; |
380 | bool again; |
380 | bool again; |
381 | 381 | ||
382 | thread_detach(THREAD); |
382 | thread_detach(THREAD); |
383 | 383 | ||
384 | loop: |
384 | loop: |
385 | ipl = interrupts_disable(); |
385 | ipl = interrupts_disable(); |
386 | spinlock_lock(&TASK->lock); |
386 | spinlock_lock(&TASK->lock); |
387 | 387 | ||
388 | main_thread = TASK->main_thread; |
388 | main_thread = TASK->main_thread; |
389 | 389 | ||
390 | /* |
390 | /* |
391 | * Find a thread to join. |
391 | * Find a thread to join. |
392 | */ |
392 | */ |
393 | again = false; |
393 | again = false; |
394 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
394 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
395 | t = list_get_instance(cur, thread_t, th_link); |
395 | t = list_get_instance(cur, thread_t, th_link); |
396 | 396 | ||
397 | spinlock_lock(&t->lock); |
397 | spinlock_lock(&t->lock); |
398 | if (t == THREAD) { |
398 | if (t == THREAD) { |
399 | spinlock_unlock(&t->lock); |
399 | spinlock_unlock(&t->lock); |
400 | continue; |
400 | continue; |
401 | } else if (t == main_thread) { |
401 | } else if (t == main_thread) { |
402 | spinlock_unlock(&t->lock); |
402 | spinlock_unlock(&t->lock); |
403 | continue; |
403 | continue; |
404 | } else if (t->join_type != None) { |
404 | } else if (t->join_type != None) { |
405 | spinlock_unlock(&t->lock); |
405 | spinlock_unlock(&t->lock); |
406 | again = true; |
406 | again = true; |
407 | continue; |
407 | continue; |
408 | } else { |
408 | } else { |
409 | t->join_type = TaskClnp; |
409 | t->join_type = TaskClnp; |
410 | spinlock_unlock(&t->lock); |
410 | spinlock_unlock(&t->lock); |
411 | again = false; |
411 | again = false; |
412 | break; |
412 | break; |
413 | } |
413 | } |
414 | } |
414 | } |
415 | 415 | ||
416 | spinlock_unlock(&TASK->lock); |
416 | spinlock_unlock(&TASK->lock); |
417 | interrupts_restore(ipl); |
417 | interrupts_restore(ipl); |
418 | 418 | ||
419 | if (again) { |
419 | if (again) { |
420 | /* |
420 | /* |
421 | * Other cleanup (e.g. ktaskgc) is in progress. |
421 | * Other cleanup (e.g. ktaskgc) is in progress. |
422 | */ |
422 | */ |
423 | scheduler(); |
423 | scheduler(); |
424 | goto loop; |
424 | goto loop; |
425 | } |
425 | } |
426 | 426 | ||
427 | if (t != THREAD) { |
427 | if (t != THREAD) { |
428 | ASSERT(t != main_thread); /* uninit is joined and detached in ktaskgc */ |
428 | ASSERT(t != main_thread); /* uninit is joined and detached in ktaskgc */ |
429 | thread_join(t); |
429 | thread_join(t); |
430 | thread_detach(t); |
430 | thread_detach(t); |
431 | goto loop; /* go for another thread */ |
431 | goto loop; /* go for another thread */ |
432 | } |
432 | } |
433 | 433 | ||
434 | /* |
434 | /* |
435 | * Now there are no other threads in this task |
435 | * Now there are no other threads in this task |
436 | * and no new threads can be created. |
436 | * and no new threads can be created. |
437 | */ |
437 | */ |
438 | 438 | ||
439 | ipc_cleanup(); |
439 | ipc_cleanup(); |
440 | futex_cleanup(); |
440 | futex_cleanup(); |
441 | klog_printf("Cleanup of task %lld completed.", TASK->taskid); |
441 | klog_printf("Cleanup of task %lld completed.", TASK->taskid); |
442 | } |
442 | } |
443 | 443 | ||
444 | /** Kernel thread used to kill the userspace task when its main thread exits. |
444 | /** Kernel thread used to kill the userspace task when its main thread exits. |
445 | * |
445 | * |
446 | * This thread waits until the main userspace thread (i.e. uninit) exits. |
446 | * This thread waits until the main userspace thread (i.e. uninit) exits. |
447 | * When this happens, the task is killed. In the meantime, exited threads |
447 | * When this happens, the task is killed. In the meantime, exited threads |
448 | * are garbage collected. |
448 | * are garbage collected. |
449 | * |
449 | * |
450 | * @param arg Pointer to the thread structure of the task's main thread. |
450 | * @param arg Pointer to the thread structure of the task's main thread. |
451 | */ |
451 | */ |
452 | void ktaskgc(void *arg) |
452 | void ktaskgc(void *arg) |
453 | { |
453 | { |
454 | thread_t *t = (thread_t *) arg; |
454 | thread_t *t = (thread_t *) arg; |
455 | loop: |
455 | loop: |
456 | /* |
456 | /* |
457 | * Userspace threads cannot detach themselves, |
457 | * Userspace threads cannot detach themselves, |
458 | * therefore the thread pointer is guaranteed to be valid. |
458 | * therefore the thread pointer is guaranteed to be valid. |
459 | */ |
459 | */ |
460 | if (thread_join_timeout(t, 1000000, SYNCH_FLAGS_NONE) == ESYNCH_TIMEOUT) { /* sleep uninterruptibly here! */ |
460 | if (thread_join_timeout(t, 1000000, SYNCH_FLAGS_NONE) == ESYNCH_TIMEOUT) { /* sleep uninterruptibly here! */ |
461 | ipl_t ipl; |
461 | ipl_t ipl; |
462 | link_t *cur; |
462 | link_t *cur; |
463 | thread_t *thr = NULL; |
463 | thread_t *thr = NULL; |
464 | 464 | ||
465 | /* |
465 | /* |
466 | * The join timed out. Try to do some garbage collection of Undead threads. |
466 | * The join timed out. Try to do some garbage collection of Undead threads. |
467 | */ |
467 | */ |
468 | more_gc: |
468 | more_gc: |
469 | ipl = interrupts_disable(); |
469 | ipl = interrupts_disable(); |
470 | spinlock_lock(&TASK->lock); |
470 | spinlock_lock(&TASK->lock); |
471 | 471 | ||
472 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
472 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
473 | thr = list_get_instance(cur, thread_t, th_link); |
473 | thr = list_get_instance(cur, thread_t, th_link); |
474 | spinlock_lock(&thr->lock); |
474 | spinlock_lock(&thr->lock); |
475 | if (thr != t && thr->state == Undead && thr->join_type == None) { |
475 | if (thr != t && thr->state == Undead && thr->join_type == None) { |
476 | thr->join_type = TaskGC; |
476 | thr->join_type = TaskGC; |
477 | spinlock_unlock(&thr->lock); |
477 | spinlock_unlock(&thr->lock); |
478 | break; |
478 | break; |
479 | } |
479 | } |
480 | spinlock_unlock(&thr->lock); |
480 | spinlock_unlock(&thr->lock); |
481 | thr = NULL; |
481 | thr = NULL; |
482 | } |
482 | } |
483 | spinlock_unlock(&TASK->lock); |
483 | spinlock_unlock(&TASK->lock); |
484 | interrupts_restore(ipl); |
484 | interrupts_restore(ipl); |
485 | 485 | ||
486 | if (thr) { |
486 | if (thr) { |
487 | thread_join(thr); |
487 | thread_join(thr); |
488 | thread_detach(thr); |
488 | thread_detach(thr); |
489 | scheduler(); |
489 | scheduler(); |
490 | goto more_gc; |
490 | goto more_gc; |
491 | } |
491 | } |
492 | 492 | ||
493 | goto loop; |
493 | goto loop; |
494 | } |
494 | } |
495 | thread_detach(t); |
495 | thread_detach(t); |
496 | task_kill(TASK->taskid); |
496 | task_kill(TASK->taskid); |
497 | } |
497 | } |
498 | 498 | ||
499 | /** @} |
499 | /** @} |
500 | */ |
500 | */ |
501 | 501 |