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