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