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