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