Rev 2216 | Rev 2436 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2216 | Rev 2227 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericproc |
29 | /** @addtogroup genericproc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Task management. |
35 | * @brief Task management. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <main/uinit.h> |
38 | #include <main/uinit.h> |
39 | #include <proc/thread.h> |
39 | #include <proc/thread.h> |
40 | #include <proc/task.h> |
40 | #include <proc/task.h> |
41 | #include <proc/uarg.h> |
41 | #include <proc/uarg.h> |
42 | #include <mm/as.h> |
42 | #include <mm/as.h> |
43 | #include <mm/slab.h> |
43 | #include <mm/slab.h> |
44 | #include <atomic.h> |
44 | #include <atomic.h> |
45 | #include <synch/spinlock.h> |
45 | #include <synch/spinlock.h> |
46 | #include <synch/waitq.h> |
46 | #include <synch/waitq.h> |
47 | #include <arch.h> |
47 | #include <arch.h> |
48 | #include <panic.h> |
48 | #include <panic.h> |
49 | #include <adt/btree.h> |
49 | #include <adt/btree.h> |
50 | #include <adt/list.h> |
50 | #include <adt/list.h> |
51 | #include <ipc/ipc.h> |
51 | #include <ipc/ipc.h> |
52 | #include <security/cap.h> |
52 | #include <security/cap.h> |
53 | #include <memstr.h> |
53 | #include <memstr.h> |
54 | #include <print.h> |
54 | #include <print.h> |
55 | #include <lib/elf.h> |
55 | #include <lib/elf.h> |
56 | #include <errno.h> |
56 | #include <errno.h> |
57 | #include <func.h> |
57 | #include <func.h> |
58 | #include <syscall/copy.h> |
58 | #include <syscall/copy.h> |
59 | #include <console/klog.h> |
59 | #include <console/klog.h> |
60 | 60 | ||
61 | #ifndef LOADED_PROG_STACK_PAGES_NO |
61 | #ifndef LOADED_PROG_STACK_PAGES_NO |
62 | #define LOADED_PROG_STACK_PAGES_NO 1 |
62 | #define LOADED_PROG_STACK_PAGES_NO 1 |
63 | #endif |
63 | #endif |
64 | 64 | ||
65 | /** Spinlock protecting the tasks_btree B+tree. */ |
65 | /** Spinlock protecting the tasks_btree B+tree. */ |
66 | SPINLOCK_INITIALIZE(tasks_lock); |
66 | SPINLOCK_INITIALIZE(tasks_lock); |
67 | 67 | ||
68 | /** B+tree of active tasks. |
68 | /** B+tree of active tasks. |
69 | * |
69 | * |
70 | * The task is guaranteed to exist after it was found in the tasks_btree as |
70 | * The task is guaranteed to exist after it was found in the tasks_btree as |
71 | * long as: |
71 | * long as: |
72 | * @li the tasks_lock is held, |
72 | * @li the tasks_lock is held, |
73 | * @li the task's lock is held when task's lock is acquired before releasing |
73 | * @li the task's lock is held when task's lock is acquired before releasing |
74 | * tasks_lock or |
74 | * tasks_lock or |
75 | * @li the task's refcount is greater than 0 |
75 | * @li the task's refcount is greater than 0 |
76 | * |
76 | * |
77 | */ |
77 | */ |
78 | btree_t tasks_btree; |
78 | btree_t tasks_btree; |
79 | 79 | ||
80 | static task_id_t task_counter = 0; |
80 | static task_id_t task_counter = 0; |
81 | 81 | ||
82 | static void ktaskclnp(void *arg); |
82 | static void ktaskclnp(void *arg); |
83 | static void ktaskgc(void *arg); |
83 | static void ktaskgc(void *arg); |
84 | 84 | ||
85 | /** Initialize tasks |
85 | /** Initialize tasks |
86 | * |
86 | * |
87 | * Initialize kernel tasks support. |
87 | * Initialize kernel tasks support. |
88 | * |
88 | * |
89 | */ |
89 | */ |
90 | void task_init(void) |
90 | void task_init(void) |
91 | { |
91 | { |
92 | TASK = NULL; |
92 | TASK = NULL; |
93 | btree_create(&tasks_btree); |
93 | btree_create(&tasks_btree); |
94 | } |
94 | } |
95 | 95 | ||
- | 96 | /** Kill all tasks except the current task. |
|
- | 97 | * |
|
- | 98 | */ |
|
- | 99 | void task_done(void) |
|
- | 100 | { |
|
- | 101 | task_t *t; |
|
- | 102 | do { /* Repeat until there are any tasks except TASK */ |
|
- | 103 | ||
- | 104 | /* Messing with task structures, avoid deadlock */ |
|
- | 105 | ipl_t ipl = interrupts_disable(); |
|
- | 106 | spinlock_lock(&tasks_lock); |
|
- | 107 | ||
- | 108 | t = NULL; |
|
- | 109 | link_t *cur; |
|
- | 110 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) { |
|
- | 111 | btree_node_t *node = list_get_instance(cur, btree_node_t, leaf_link); |
|
- | 112 | ||
- | 113 | unsigned int i; |
|
- | 114 | for (i = 0; i < node->keys; i++) { |
|
- | 115 | if ((task_t *) node->value[i] != TASK) { |
|
- | 116 | t = (task_t *) node->value[i]; |
|
- | 117 | break; |
|
- | 118 | } |
|
- | 119 | } |
|
- | 120 | } |
|
- | 121 | ||
- | 122 | if (t != NULL) { |
|
- | 123 | task_id_t id = t->taskid; |
|
- | 124 | ||
- | 125 | spinlock_unlock(&tasks_lock); |
|
- | 126 | interrupts_restore(ipl); |
|
- | 127 | ||
- | 128 | #ifdef CONFIG_DEBUG |
|
- | 129 | printf("Killing task %llu\n", id); |
|
- | 130 | #endif |
|
- | 131 | task_kill(id); |
|
- | 132 | } else { |
|
- | 133 | spinlock_unlock(&tasks_lock); |
|
- | 134 | interrupts_restore(ipl); |
|
- | 135 | } |
|
- | 136 | ||
- | 137 | } while (t != NULL); |
|
- | 138 | } |
|
96 | 139 | ||
97 | /** Create new task |
140 | /** Create new task |
98 | * |
141 | * |
99 | * Create new task with no threads. |
142 | * Create new task with no threads. |
100 | * |
143 | * |
101 | * @param as Task's address space. |
144 | * @param as Task's address space. |
102 | * @param name Symbolic name. |
145 | * @param name Symbolic name. |
103 | * |
146 | * |
104 | * @return New task's structure |
147 | * @return New task's structure |
105 | * |
148 | * |
106 | */ |
149 | */ |
107 | task_t *task_create(as_t *as, char *name) |
150 | task_t *task_create(as_t *as, char *name) |
108 | { |
151 | { |
109 | ipl_t ipl; |
152 | ipl_t ipl; |
110 | task_t *ta; |
153 | task_t *ta; |
111 | int i; |
154 | int i; |
112 | 155 | ||
113 | ta = (task_t *) malloc(sizeof(task_t), 0); |
156 | ta = (task_t *) malloc(sizeof(task_t), 0); |
114 | 157 | ||
115 | task_create_arch(ta); |
158 | task_create_arch(ta); |
116 | 159 | ||
117 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
160 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
118 | list_initialize(&ta->th_head); |
161 | list_initialize(&ta->th_head); |
119 | ta->as = as; |
162 | ta->as = as; |
120 | ta->name = name; |
163 | ta->name = name; |
121 | ta->main_thread = NULL; |
164 | ta->main_thread = NULL; |
122 | ta->refcount = 0; |
165 | ta->refcount = 0; |
123 | ta->context = CONTEXT; |
166 | ta->context = CONTEXT; |
124 | 167 | ||
125 | ta->capabilities = 0; |
168 | ta->capabilities = 0; |
126 | ta->accept_new_threads = true; |
169 | ta->accept_new_threads = true; |
127 | ta->cycles = 0; |
170 | ta->cycles = 0; |
128 | 171 | ||
129 | ipc_answerbox_init(&ta->answerbox); |
172 | ipc_answerbox_init(&ta->answerbox); |
130 | for (i = 0; i < IPC_MAX_PHONES; i++) |
173 | for (i = 0; i < IPC_MAX_PHONES; i++) |
131 | ipc_phone_init(&ta->phones[i]); |
174 | ipc_phone_init(&ta->phones[i]); |
132 | if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, |
175 | if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, |
133 | ta->context))) |
176 | ta->context))) |
134 | ipc_phone_connect(&ta->phones[0], ipc_phone_0); |
177 | ipc_phone_connect(&ta->phones[0], ipc_phone_0); |
135 | atomic_set(&ta->active_calls, 0); |
178 | atomic_set(&ta->active_calls, 0); |
136 | 179 | ||
137 | mutex_initialize(&ta->futexes_lock); |
180 | mutex_initialize(&ta->futexes_lock); |
138 | btree_create(&ta->futexes); |
181 | btree_create(&ta->futexes); |
139 | 182 | ||
140 | ipl = interrupts_disable(); |
183 | ipl = interrupts_disable(); |
141 | 184 | ||
142 | /* |
185 | /* |
143 | * Increment address space reference count. |
186 | * Increment address space reference count. |
144 | */ |
187 | */ |
145 | atomic_inc(&as->refcount); |
188 | atomic_inc(&as->refcount); |
146 | 189 | ||
147 | spinlock_lock(&tasks_lock); |
190 | spinlock_lock(&tasks_lock); |
148 | 191 | ||
149 | ta->taskid = ++task_counter; |
192 | ta->taskid = ++task_counter; |
150 | btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL); |
193 | btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL); |
151 | 194 | ||
152 | spinlock_unlock(&tasks_lock); |
195 | spinlock_unlock(&tasks_lock); |
153 | interrupts_restore(ipl); |
196 | interrupts_restore(ipl); |
154 | 197 | ||
155 | return ta; |
198 | return ta; |
156 | } |
199 | } |
157 | 200 | ||
158 | /** Destroy task. |
201 | /** Destroy task. |
159 | * |
202 | * |
160 | * @param t Task to be destroyed. |
203 | * @param t Task to be destroyed. |
161 | */ |
204 | */ |
162 | void task_destroy(task_t *t) |
205 | void task_destroy(task_t *t) |
163 | { |
206 | { |
164 | task_destroy_arch(t); |
207 | task_destroy_arch(t); |
165 | btree_destroy(&t->futexes); |
208 | btree_destroy(&t->futexes); |
166 | 209 | ||
167 | if (atomic_predec(&t->as->refcount) == 0) |
210 | if (atomic_predec(&t->as->refcount) == 0) |
168 | as_destroy(t->as); |
211 | as_destroy(t->as); |
169 | 212 | ||
170 | free(t); |
213 | free(t); |
171 | TASK = NULL; |
214 | TASK = NULL; |
172 | } |
215 | } |
173 | 216 | ||
174 | /** Create new task with 1 thread and run it |
217 | /** Create new task with 1 thread and run it |
175 | * |
218 | * |
176 | * @param program_addr Address of program executable image. |
219 | * @param program_addr Address of program executable image. |
177 | * @param name Program name. |
220 | * @param name Program name. |
178 | * |
221 | * |
179 | * @return Task of the running program or NULL on error. |
222 | * @return Task of the running program or NULL on error. |
180 | */ |
223 | */ |
181 | task_t * task_run_program(void *program_addr, char *name) |
224 | task_t * task_run_program(void *program_addr, char *name) |
182 | { |
225 | { |
183 | as_t *as; |
226 | as_t *as; |
184 | as_area_t *a; |
227 | as_area_t *a; |
185 | int rc; |
228 | int rc; |
186 | thread_t *t1, *t2; |
229 | thread_t *t1, *t2; |
187 | task_t *task; |
230 | task_t *task; |
188 | uspace_arg_t *kernel_uarg; |
231 | uspace_arg_t *kernel_uarg; |
189 | 232 | ||
190 | as = as_create(0); |
233 | as = as_create(0); |
191 | ASSERT(as); |
234 | ASSERT(as); |
192 | 235 | ||
193 | rc = elf_load((elf_header_t *) program_addr, as); |
236 | rc = elf_load((elf_header_t *) program_addr, as); |
194 | if (rc != EE_OK) { |
237 | if (rc != EE_OK) { |
195 | as_destroy(as); |
238 | as_destroy(as); |
196 | return NULL; |
239 | return NULL; |
197 | } |
240 | } |
198 | 241 | ||
199 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
242 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
200 | kernel_uarg->uspace_entry = |
243 | kernel_uarg->uspace_entry = |
201 | (void *) ((elf_header_t *) program_addr)->e_entry; |
244 | (void *) ((elf_header_t *) program_addr)->e_entry; |
202 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
245 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
203 | kernel_uarg->uspace_thread_function = NULL; |
246 | kernel_uarg->uspace_thread_function = NULL; |
204 | kernel_uarg->uspace_thread_arg = NULL; |
247 | kernel_uarg->uspace_thread_arg = NULL; |
205 | kernel_uarg->uspace_uarg = NULL; |
248 | kernel_uarg->uspace_uarg = NULL; |
206 | 249 | ||
207 | task = task_create(as, name); |
250 | task = task_create(as, name); |
208 | ASSERT(task); |
251 | ASSERT(task); |
209 | 252 | ||
210 | /* |
253 | /* |
211 | * Create the data as_area. |
254 | * Create the data as_area. |
212 | */ |
255 | */ |
213 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
256 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
214 | LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS, |
257 | LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS, |
215 | AS_AREA_ATTR_NONE, &anon_backend, NULL); |
258 | AS_AREA_ATTR_NONE, &anon_backend, NULL); |
216 | 259 | ||
217 | /* |
260 | /* |
218 | * Create the main thread. |
261 | * Create the main thread. |
219 | */ |
262 | */ |
220 | t1 = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE, |
263 | t1 = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE, |
221 | "uinit", false); |
264 | "uinit", false); |
222 | ASSERT(t1); |
265 | ASSERT(t1); |
223 | 266 | ||
224 | /* |
267 | /* |
225 | * Create killer thread for the new task. |
268 | * Create killer thread for the new task. |
226 | */ |
269 | */ |
227 | t2 = thread_create(ktaskgc, t1, task, 0, "ktaskgc", true); |
270 | t2 = thread_create(ktaskgc, t1, task, 0, "ktaskgc", true); |
228 | ASSERT(t2); |
271 | ASSERT(t2); |
229 | thread_ready(t2); |
272 | thread_ready(t2); |
230 | 273 | ||
231 | thread_ready(t1); |
274 | thread_ready(t1); |
232 | 275 | ||
233 | return task; |
276 | return task; |
234 | } |
277 | } |
235 | 278 | ||
236 | /** Syscall for reading task ID from userspace. |
279 | /** Syscall for reading task ID from userspace. |
237 | * |
280 | * |
238 | * @param uspace_task_id Userspace address of 8-byte buffer where to store |
281 | * @param uspace_task_id Userspace address of 8-byte buffer where to store |
239 | * current task ID. |
282 | * current task ID. |
240 | * |
283 | * |
241 | * @return 0 on success or an error code from @ref errno.h. |
284 | * @return 0 on success or an error code from @ref errno.h. |
242 | */ |
285 | */ |
243 | unative_t sys_task_get_id(task_id_t *uspace_task_id) |
286 | unative_t sys_task_get_id(task_id_t *uspace_task_id) |
244 | { |
287 | { |
245 | /* |
288 | /* |
246 | * No need to acquire lock on TASK because taskid |
289 | * No need to acquire lock on TASK because taskid |
247 | * remains constant for the lifespan of the task. |
290 | * remains constant for the lifespan of the task. |
248 | */ |
291 | */ |
249 | return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, |
292 | return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, |
250 | sizeof(TASK->taskid)); |
293 | sizeof(TASK->taskid)); |
251 | } |
294 | } |
252 | 295 | ||
253 | /** Find task structure corresponding to task ID. |
296 | /** Find task structure corresponding to task ID. |
254 | * |
297 | * |
255 | * The tasks_lock must be already held by the caller of this function |
298 | * The tasks_lock must be already held by the caller of this function |
256 | * and interrupts must be disabled. |
299 | * and interrupts must be disabled. |
257 | * |
300 | * |
258 | * @param id Task ID. |
301 | * @param id Task ID. |
259 | * |
302 | * |
260 | * @return Task structure address or NULL if there is no such task ID. |
303 | * @return Task structure address or NULL if there is no such task ID. |
261 | */ |
304 | */ |
262 | task_t *task_find_by_id(task_id_t id) |
305 | task_t *task_find_by_id(task_id_t id) |
263 | { |
306 | { |
264 | btree_node_t *leaf; |
307 | btree_node_t *leaf; |
265 | 308 | ||
266 | return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf); |
309 | return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf); |
267 | } |
310 | } |
268 | 311 | ||
269 | /** Get accounting data of given task. |
312 | /** Get accounting data of given task. |
270 | * |
313 | * |
271 | * Note that task lock of 't' must be already held and |
314 | * Note that task lock of 't' must be already held and |
272 | * interrupts must be already disabled. |
315 | * interrupts must be already disabled. |
273 | * |
316 | * |
274 | * @param t Pointer to thread. |
317 | * @param t Pointer to thread. |
275 | * |
318 | * |
276 | */ |
319 | */ |
277 | uint64_t task_get_accounting(task_t *t) |
320 | uint64_t task_get_accounting(task_t *t) |
278 | { |
321 | { |
279 | /* Accumulated value of task */ |
322 | /* Accumulated value of task */ |
280 | uint64_t ret = t->cycles; |
323 | uint64_t ret = t->cycles; |
281 | 324 | ||
282 | /* Current values of threads */ |
325 | /* Current values of threads */ |
283 | link_t *cur; |
326 | link_t *cur; |
284 | for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) { |
327 | for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) { |
285 | thread_t *thr = list_get_instance(cur, thread_t, th_link); |
328 | thread_t *thr = list_get_instance(cur, thread_t, th_link); |
286 | 329 | ||
287 | spinlock_lock(&thr->lock); |
330 | spinlock_lock(&thr->lock); |
288 | /* Process only counted threads */ |
331 | /* Process only counted threads */ |
289 | if (!thr->uncounted) { |
332 | if (!thr->uncounted) { |
290 | if (thr == THREAD) { |
333 | if (thr == THREAD) { |
291 | /* Update accounting of current thread */ |
334 | /* Update accounting of current thread */ |
292 | thread_update_accounting(); |
335 | thread_update_accounting(); |
293 | } |
336 | } |
294 | ret += thr->cycles; |
337 | ret += thr->cycles; |
295 | } |
338 | } |
296 | spinlock_unlock(&thr->lock); |
339 | spinlock_unlock(&thr->lock); |
297 | } |
340 | } |
298 | 341 | ||
299 | return ret; |
342 | return ret; |
300 | } |
343 | } |
301 | 344 | ||
302 | /** Kill task. |
345 | /** Kill task. |
303 | * |
346 | * |
304 | * @param id ID of the task to be killed. |
347 | * @param id ID of the task to be killed. |
305 | * |
348 | * |
306 | * @return 0 on success or an error code from errno.h |
349 | * @return 0 on success or an error code from errno.h |
307 | */ |
350 | */ |
308 | int task_kill(task_id_t id) |
351 | int task_kill(task_id_t id) |
309 | { |
352 | { |
310 | ipl_t ipl; |
353 | ipl_t ipl; |
311 | task_t *ta; |
354 | task_t *ta; |
312 | thread_t *t; |
355 | thread_t *t; |
313 | link_t *cur; |
356 | link_t *cur; |
314 | 357 | ||
315 | if (id == 1) |
358 | if (id == 1) |
316 | return EPERM; |
359 | return EPERM; |
317 | 360 | ||
318 | ipl = interrupts_disable(); |
361 | ipl = interrupts_disable(); |
319 | spinlock_lock(&tasks_lock); |
362 | spinlock_lock(&tasks_lock); |
320 | 363 | ||
321 | if (!(ta = task_find_by_id(id))) { |
364 | if (!(ta = task_find_by_id(id))) { |
322 | spinlock_unlock(&tasks_lock); |
365 | spinlock_unlock(&tasks_lock); |
323 | interrupts_restore(ipl); |
366 | interrupts_restore(ipl); |
324 | return ENOENT; |
367 | return ENOENT; |
325 | } |
368 | } |
326 | 369 | ||
327 | spinlock_lock(&ta->lock); |
370 | spinlock_lock(&ta->lock); |
328 | ta->refcount++; |
371 | ta->refcount++; |
329 | spinlock_unlock(&ta->lock); |
372 | spinlock_unlock(&ta->lock); |
330 | 373 | ||
331 | btree_remove(&tasks_btree, ta->taskid, NULL); |
374 | btree_remove(&tasks_btree, ta->taskid, NULL); |
332 | spinlock_unlock(&tasks_lock); |
375 | spinlock_unlock(&tasks_lock); |
333 | 376 | ||
334 | t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp", true); |
377 | t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp", true); |
335 | 378 | ||
336 | spinlock_lock(&ta->lock); |
379 | spinlock_lock(&ta->lock); |
337 | ta->accept_new_threads = false; |
380 | ta->accept_new_threads = false; |
338 | ta->refcount--; |
381 | ta->refcount--; |
339 | 382 | ||
340 | /* |
383 | /* |
341 | * Interrupt all threads except ktaskclnp. |
384 | * Interrupt all threads except ktaskclnp. |
342 | */ |
385 | */ |
343 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
386 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
344 | thread_t *thr; |
387 | thread_t *thr; |
345 | bool sleeping = false; |
388 | bool sleeping = false; |
346 | 389 | ||
347 | thr = list_get_instance(cur, thread_t, th_link); |
390 | thr = list_get_instance(cur, thread_t, th_link); |
348 | if (thr == t) |
391 | if (thr == t) |
349 | continue; |
392 | continue; |
350 | 393 | ||
351 | spinlock_lock(&thr->lock); |
394 | spinlock_lock(&thr->lock); |
352 | thr->interrupted = true; |
395 | thr->interrupted = true; |
353 | if (thr->state == Sleeping) |
396 | if (thr->state == Sleeping) |
354 | sleeping = true; |
397 | sleeping = true; |
355 | spinlock_unlock(&thr->lock); |
398 | spinlock_unlock(&thr->lock); |
356 | 399 | ||
357 | if (sleeping) |
400 | if (sleeping) |
358 | waitq_interrupt_sleep(thr); |
401 | waitq_interrupt_sleep(thr); |
359 | } |
402 | } |
360 | 403 | ||
361 | spinlock_unlock(&ta->lock); |
404 | spinlock_unlock(&ta->lock); |
362 | interrupts_restore(ipl); |
405 | interrupts_restore(ipl); |
363 | 406 | ||
364 | if (t) |
407 | if (t) |
365 | thread_ready(t); |
408 | thread_ready(t); |
366 | 409 | ||
367 | return 0; |
410 | return 0; |
368 | } |
411 | } |
369 | 412 | ||
370 | /** Print task list */ |
413 | /** Print task list */ |
371 | void task_print_list(void) |
414 | void task_print_list(void) |
372 | { |
415 | { |
373 | link_t *cur; |
416 | link_t *cur; |
374 | ipl_t ipl; |
417 | ipl_t ipl; |
375 | 418 | ||
376 | /* Messing with thread structures, avoid deadlock */ |
419 | /* Messing with task structures, avoid deadlock */ |
377 | ipl = interrupts_disable(); |
420 | ipl = interrupts_disable(); |
378 | spinlock_lock(&tasks_lock); |
421 | spinlock_lock(&tasks_lock); |
379 | 422 | ||
380 | printf("taskid name ctx address as cycles threads " |
423 | printf("taskid name ctx address as cycles threads " |
381 | "calls callee\n"); |
424 | "calls callee\n"); |
382 | printf("------ ---------- --- ---------- ---------- ---------- ------- " "------ ------>\n"); |
425 | printf("------ ---------- --- ---------- ---------- ---------- ------- " "------ ------>\n"); |
383 | 426 | ||
384 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; |
427 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; |
385 | cur = cur->next) { |
428 | cur = cur->next) { |
386 | btree_node_t *node; |
429 | btree_node_t *node; |
387 | unsigned int i; |
430 | unsigned int i; |
388 | 431 | ||
389 | node = list_get_instance(cur, btree_node_t, leaf_link); |
432 | node = list_get_instance(cur, btree_node_t, leaf_link); |
390 | for (i = 0; i < node->keys; i++) { |
433 | for (i = 0; i < node->keys; i++) { |
391 | task_t *t; |
434 | task_t *t; |
392 | int j; |
435 | int j; |
393 | 436 | ||
394 | t = (task_t *) node->value[i]; |
437 | t = (task_t *) node->value[i]; |
395 | 438 | ||
396 | spinlock_lock(&t->lock); |
439 | spinlock_lock(&t->lock); |
397 | 440 | ||
398 | uint64_t cycles; |
441 | uint64_t cycles; |
399 | char suffix; |
442 | char suffix; |
400 | order(task_get_accounting(t), &cycles, &suffix); |
443 | order(task_get_accounting(t), &cycles, &suffix); |
401 | 444 | ||
402 | printf("%-6llu %-10s %-3ld %#10zx %#10zx %9llu%c %7zd " |
445 | printf("%-6llu %-10s %-3ld %#10zx %#10zx %9llu%c %7zd " |
403 | "%6zd", t->taskid, t->name, t->context, t, t->as, |
446 | "%6zd", t->taskid, t->name, t->context, t, t->as, |
404 | cycles, suffix, t->refcount, |
447 | cycles, suffix, t->refcount, |
405 | atomic_get(&t->active_calls)); |
448 | atomic_get(&t->active_calls)); |
406 | for (j = 0; j < IPC_MAX_PHONES; j++) { |
449 | for (j = 0; j < IPC_MAX_PHONES; j++) { |
407 | if (t->phones[j].callee) |
450 | if (t->phones[j].callee) |
408 | printf(" %zd:%#zx", j, |
451 | printf(" %zd:%#zx", j, |
409 | t->phones[j].callee); |
452 | t->phones[j].callee); |
410 | } |
453 | } |
411 | printf("\n"); |
454 | printf("\n"); |
412 | 455 | ||
413 | spinlock_unlock(&t->lock); |
456 | spinlock_unlock(&t->lock); |
414 | } |
457 | } |
415 | } |
458 | } |
416 | 459 | ||
417 | spinlock_unlock(&tasks_lock); |
460 | spinlock_unlock(&tasks_lock); |
418 | interrupts_restore(ipl); |
461 | interrupts_restore(ipl); |
419 | } |
462 | } |
420 | 463 | ||
421 | /** Kernel thread used to cleanup the task after it is killed. */ |
464 | /** Kernel thread used to cleanup the task after it is killed. */ |
422 | void ktaskclnp(void *arg) |
465 | void ktaskclnp(void *arg) |
423 | { |
466 | { |
424 | ipl_t ipl; |
467 | ipl_t ipl; |
425 | thread_t *t = NULL, *main_thread; |
468 | thread_t *t = NULL, *main_thread; |
426 | link_t *cur; |
469 | link_t *cur; |
427 | bool again; |
470 | bool again; |
428 | 471 | ||
429 | thread_detach(THREAD); |
472 | thread_detach(THREAD); |
430 | 473 | ||
431 | loop: |
474 | loop: |
432 | ipl = interrupts_disable(); |
475 | ipl = interrupts_disable(); |
433 | spinlock_lock(&TASK->lock); |
476 | spinlock_lock(&TASK->lock); |
434 | 477 | ||
435 | main_thread = TASK->main_thread; |
478 | main_thread = TASK->main_thread; |
436 | 479 | ||
437 | /* |
480 | /* |
438 | * Find a thread to join. |
481 | * Find a thread to join. |
439 | */ |
482 | */ |
440 | again = false; |
483 | again = false; |
441 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
484 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
442 | t = list_get_instance(cur, thread_t, th_link); |
485 | t = list_get_instance(cur, thread_t, th_link); |
443 | 486 | ||
444 | spinlock_lock(&t->lock); |
487 | spinlock_lock(&t->lock); |
445 | if (t == THREAD) { |
488 | if (t == THREAD) { |
446 | spinlock_unlock(&t->lock); |
489 | spinlock_unlock(&t->lock); |
447 | continue; |
490 | continue; |
448 | } else if (t == main_thread) { |
491 | } else if (t == main_thread) { |
449 | spinlock_unlock(&t->lock); |
492 | spinlock_unlock(&t->lock); |
450 | continue; |
493 | continue; |
451 | } else if (t->join_type != None) { |
494 | } else if (t->join_type != None) { |
452 | spinlock_unlock(&t->lock); |
495 | spinlock_unlock(&t->lock); |
453 | again = true; |
496 | again = true; |
454 | continue; |
497 | continue; |
455 | } else { |
498 | } else { |
456 | t->join_type = TaskClnp; |
499 | t->join_type = TaskClnp; |
457 | spinlock_unlock(&t->lock); |
500 | spinlock_unlock(&t->lock); |
458 | again = false; |
501 | again = false; |
459 | break; |
502 | break; |
460 | } |
503 | } |
461 | } |
504 | } |
462 | 505 | ||
463 | spinlock_unlock(&TASK->lock); |
506 | spinlock_unlock(&TASK->lock); |
464 | interrupts_restore(ipl); |
507 | interrupts_restore(ipl); |
465 | 508 | ||
466 | if (again) { |
509 | if (again) { |
467 | /* |
510 | /* |
468 | * Other cleanup (e.g. ktaskgc) is in progress. |
511 | * Other cleanup (e.g. ktaskgc) is in progress. |
469 | */ |
512 | */ |
470 | scheduler(); |
513 | scheduler(); |
471 | goto loop; |
514 | goto loop; |
472 | } |
515 | } |
473 | 516 | ||
474 | if (t != THREAD) { |
517 | if (t != THREAD) { |
475 | ASSERT(t != main_thread); /* uninit is joined and detached |
518 | ASSERT(t != main_thread); /* uninit is joined and detached |
476 | * in ktaskgc */ |
519 | * in ktaskgc */ |
477 | thread_join(t); |
520 | thread_join(t); |
478 | thread_detach(t); |
521 | thread_detach(t); |
479 | goto loop; /* go for another thread */ |
522 | goto loop; /* go for another thread */ |
480 | } |
523 | } |
481 | 524 | ||
482 | /* |
525 | /* |
483 | * Now there are no other threads in this task |
526 | * Now there are no other threads in this task |
484 | * and no new threads can be created. |
527 | * and no new threads can be created. |
485 | */ |
528 | */ |
486 | 529 | ||
487 | ipc_cleanup(); |
530 | ipc_cleanup(); |
488 | futex_cleanup(); |
531 | futex_cleanup(); |
489 | klog_printf("Cleanup of task %llu completed.", TASK->taskid); |
532 | klog_printf("Cleanup of task %llu completed.", TASK->taskid); |
490 | } |
533 | } |
491 | 534 | ||
492 | /** Kernel thread used to kill the userspace task when its main thread exits. |
535 | /** Kernel thread used to kill the userspace task when its main thread exits. |
493 | * |
536 | * |
494 | * This thread waits until the main userspace thread (i.e. uninit) exits. |
537 | * This thread waits until the main userspace thread (i.e. uninit) exits. |
495 | * When this happens, the task is killed. In the meantime, exited threads |
538 | * When this happens, the task is killed. In the meantime, exited threads |
496 | * are garbage collected. |
539 | * are garbage collected. |
497 | * |
540 | * |
498 | * @param arg Pointer to the thread structure of the task's main thread. |
541 | * @param arg Pointer to the thread structure of the task's main thread. |
499 | */ |
542 | */ |
500 | void ktaskgc(void *arg) |
543 | void ktaskgc(void *arg) |
501 | { |
544 | { |
502 | thread_t *t = (thread_t *) arg; |
545 | thread_t *t = (thread_t *) arg; |
503 | loop: |
546 | loop: |
504 | /* |
547 | /* |
505 | * Userspace threads cannot detach themselves, |
548 | * Userspace threads cannot detach themselves, |
506 | * therefore the thread pointer is guaranteed to be valid. |
549 | * therefore the thread pointer is guaranteed to be valid. |
507 | */ |
550 | */ |
508 | if (thread_join_timeout(t, 1000000, SYNCH_FLAGS_NONE) == |
551 | if (thread_join_timeout(t, 1000000, SYNCH_FLAGS_NONE) == |
509 | ESYNCH_TIMEOUT) { /* sleep uninterruptibly here! */ |
552 | ESYNCH_TIMEOUT) { /* sleep uninterruptibly here! */ |
510 | ipl_t ipl; |
553 | ipl_t ipl; |
511 | link_t *cur; |
554 | link_t *cur; |
512 | thread_t *thr = NULL; |
555 | thread_t *thr = NULL; |
513 | 556 | ||
514 | /* |
557 | /* |
515 | * The join timed out. Try to do some garbage collection of |
558 | * The join timed out. Try to do some garbage collection of |
516 | * Undead threads. |
559 | * Undead threads. |
517 | */ |
560 | */ |
518 | more_gc: |
561 | more_gc: |
519 | ipl = interrupts_disable(); |
562 | ipl = interrupts_disable(); |
520 | spinlock_lock(&TASK->lock); |
563 | spinlock_lock(&TASK->lock); |
521 | 564 | ||
522 | for (cur = TASK->th_head.next; cur != &TASK->th_head; |
565 | for (cur = TASK->th_head.next; cur != &TASK->th_head; |
523 | cur = cur->next) { |
566 | cur = cur->next) { |
524 | thr = list_get_instance(cur, thread_t, th_link); |
567 | thr = list_get_instance(cur, thread_t, th_link); |
525 | spinlock_lock(&thr->lock); |
568 | spinlock_lock(&thr->lock); |
526 | if (thr != t && thr->state == Undead && |
569 | if (thr != t && thr->state == Undead && |
527 | thr->join_type == None) { |
570 | thr->join_type == None) { |
528 | thr->join_type = TaskGC; |
571 | thr->join_type = TaskGC; |
529 | spinlock_unlock(&thr->lock); |
572 | spinlock_unlock(&thr->lock); |
530 | break; |
573 | break; |
531 | } |
574 | } |
532 | spinlock_unlock(&thr->lock); |
575 | spinlock_unlock(&thr->lock); |
533 | thr = NULL; |
576 | thr = NULL; |
534 | } |
577 | } |
535 | spinlock_unlock(&TASK->lock); |
578 | spinlock_unlock(&TASK->lock); |
536 | interrupts_restore(ipl); |
579 | interrupts_restore(ipl); |
537 | 580 | ||
538 | if (thr) { |
581 | if (thr) { |
539 | thread_join(thr); |
582 | thread_join(thr); |
540 | thread_detach(thr); |
583 | thread_detach(thr); |
541 | scheduler(); |
584 | scheduler(); |
542 | goto more_gc; |
585 | goto more_gc; |
543 | } |
586 | } |
544 | 587 | ||
545 | goto loop; |
588 | goto loop; |
546 | } |
589 | } |
547 | thread_detach(t); |
590 | thread_detach(t); |
548 | task_kill(TASK->taskid); |
591 | task_kill(TASK->taskid); |
549 | } |
592 | } |
550 | 593 | ||
551 | /** @} |
594 | /** @} |
552 | */ |
595 | */ |
553 | 596 |