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