source: mainline/kernel/generic/src/proc/task.c@ db4d6de

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since db4d6de was 59ee56f, checked in by Jakub Jermar <jakub@…>, 16 years ago

Introduce a task constructor.

  • Property mode set to 100644
File size: 11.1 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericproc
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Task management.
36 */
37
38#include <proc/thread.h>
39#include <proc/task.h>
40#include <mm/as.h>
41#include <mm/slab.h>
42#include <atomic.h>
43#include <synch/spinlock.h>
44#include <synch/waitq.h>
45#include <arch.h>
46#include <arch/barrier.h>
47#include <adt/avl.h>
48#include <adt/btree.h>
49#include <adt/list.h>
50#include <ipc/ipc.h>
51#include <ipc/ipcrsc.h>
52#include <print.h>
53#include <errno.h>
54#include <func.h>
55#include <string.h>
56#include <syscall/copy.h>
57#include <macros.h>
58#include <ipc/event.h>
59
60/** Spinlock protecting the tasks_tree AVL tree. */
61SPINLOCK_INITIALIZE(tasks_lock);
62
63/** AVL tree of active tasks.
64 *
65 * The task is guaranteed to exist after it was found in the tasks_tree as
66 * long as:
67 * @li the tasks_lock is held,
68 * @li the task's lock is held when task's lock is acquired before releasing
69 * tasks_lock or
70 * @li the task's refcount is greater than 0
71 *
72 */
73avltree_t tasks_tree;
74
75static task_id_t task_counter = 0;
76
77static slab_cache_t *task_slab;
78
79/* Forward declarations. */
80static void task_kill_internal(task_t *);
81static int tsk_constructor(void *, int);
82
83/** Initialize kernel tasks support. */
84void task_init(void)
85{
86 TASK = NULL;
87 avltree_create(&tasks_tree);
88 task_slab = slab_cache_create("task_slab", sizeof(task_t), 0,
89 tsk_constructor, NULL, 0);
90}
91
92/*
93 * The idea behind this walker is to kill and count all tasks different from
94 * TASK.
95 */
96static bool task_done_walker(avltree_node_t *node, void *arg)
97{
98 task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
99 unsigned *cnt = (unsigned *) arg;
100
101 if (t != TASK) {
102 (*cnt)++;
103#ifdef CONFIG_DEBUG
104 printf("[%"PRIu64"] ", t->taskid);
105#endif
106 task_kill_internal(t);
107 }
108
109 return true; /* continue the walk */
110}
111
112/** Kill all tasks except the current task. */
113void task_done(void)
114{
115 unsigned tasks_left;
116
117 do { /* Repeat until there are any tasks except TASK */
118 /* Messing with task structures, avoid deadlock */
119#ifdef CONFIG_DEBUG
120 printf("Killing tasks... ");
121#endif
122 ipl_t ipl = interrupts_disable();
123 spinlock_lock(&tasks_lock);
124 tasks_left = 0;
125 avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
126 spinlock_unlock(&tasks_lock);
127 interrupts_restore(ipl);
128 thread_sleep(1);
129#ifdef CONFIG_DEBUG
130 printf("\n");
131#endif
132 } while (tasks_left);
133}
134
135int tsk_constructor(void *obj, int kmflags)
136{
137 task_t *ta = obj;
138 int i;
139
140 atomic_set(&ta->refcount, 0);
141 atomic_set(&ta->lifecount, 0);
142 atomic_set(&ta->active_calls, 0);
143
144 spinlock_initialize(&ta->lock, "task_ta_lock");
145 mutex_initialize(&ta->futexes_lock, MUTEX_PASSIVE);
146
147 list_initialize(&ta->th_head);
148 list_initialize(&ta->sync_box_head);
149
150 ipc_answerbox_init(&ta->answerbox, ta);
151 for (i = 0; i < IPC_MAX_PHONES; i++)
152 ipc_phone_init(&ta->phones[i]);
153
154#ifdef CONFIG_UDEBUG
155 /* Init kbox stuff */
156 ta->kb.thread = NULL;
157 ipc_answerbox_init(&ta->kb.box, ta);
158 mutex_initialize(&ta->kb.cleanup_lock, MUTEX_PASSIVE);
159#endif
160
161 return 0;
162}
163
164/** Create new task with no threads.
165 *
166 * @param as Task's address space.
167 * @param name Symbolic name (a copy is made).
168 *
169 * @return New task's structure.
170 *
171 */
172task_t *task_create(as_t *as, char *name)
173{
174 ipl_t ipl;
175 task_t *ta;
176
177 ta = (task_t *) slab_alloc(task_slab, 0);
178 task_create_arch(ta);
179 ta->as = as;
180 memcpy(ta->name, name, TASK_NAME_BUFLEN);
181 ta->name[TASK_NAME_BUFLEN - 1] = 0;
182
183 ta->context = CONTEXT;
184 ta->capabilities = 0;
185 ta->cycles = 0;
186
187#ifdef CONFIG_UDEBUG
188 /* Init debugging stuff */
189 udebug_task_init(&ta->udebug);
190
191 /* Init kbox stuff */
192 ta->kb.finished = false;
193#endif
194
195 if ((ipc_phone_0) &&
196 (context_check(ipc_phone_0->task->context, ta->context)))
197 ipc_phone_connect(&ta->phones[0], ipc_phone_0);
198
199 btree_create(&ta->futexes);
200
201 ipl = interrupts_disable();
202 atomic_inc(&as->refcount);
203 spinlock_lock(&tasks_lock);
204 ta->taskid = ++task_counter;
205 avltree_node_initialize(&ta->tasks_tree_node);
206 ta->tasks_tree_node.key = ta->taskid;
207 avltree_insert(&tasks_tree, &ta->tasks_tree_node);
208 spinlock_unlock(&tasks_lock);
209 interrupts_restore(ipl);
210
211 return ta;
212}
213
214/** Destroy task.
215 *
216 * @param t Task to be destroyed.
217 */
218void task_destroy(task_t *t)
219{
220 /*
221 * Remove the task from the task B+tree.
222 */
223 spinlock_lock(&tasks_lock);
224 avltree_delete(&tasks_tree, &t->tasks_tree_node);
225 spinlock_unlock(&tasks_lock);
226
227 /*
228 * Perform architecture specific task destruction.
229 */
230 task_destroy_arch(t);
231
232 /*
233 * Free up dynamically allocated state.
234 */
235 btree_destroy(&t->futexes);
236
237 /*
238 * Drop our reference to the address space.
239 */
240 if (atomic_predec(&t->as->refcount) == 0)
241 as_destroy(t->as);
242
243 slab_free(task_slab, t);
244 TASK = NULL;
245}
246
247/** Syscall for reading task ID from userspace.
248 *
249 * @param uspace_task_id userspace address of 8-byte buffer
250 * where to store current task ID.
251 *
252 * @return Zero on success or an error code from @ref errno.h.
253 */
254unative_t sys_task_get_id(task_id_t *uspace_task_id)
255{
256 /*
257 * No need to acquire lock on TASK because taskid remains constant for
258 * the lifespan of the task.
259 */
260 return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
261 sizeof(TASK->taskid));
262}
263
264/** Syscall for setting the task name.
265 *
266 * The name simplifies identifying the task in the task list.
267 *
268 * @param name The new name for the task. (typically the same
269 * as the command used to execute it).
270 *
271 * @return 0 on success or an error code from @ref errno.h.
272 */
273unative_t sys_task_set_name(const char *uspace_name, size_t name_len)
274{
275 int rc;
276 char namebuf[TASK_NAME_BUFLEN];
277
278 /* Cap length of name and copy it from userspace. */
279
280 if (name_len > TASK_NAME_BUFLEN - 1)
281 name_len = TASK_NAME_BUFLEN - 1;
282
283 rc = copy_from_uspace(namebuf, uspace_name, name_len);
284 if (rc != 0)
285 return (unative_t) rc;
286
287 namebuf[name_len] = '\0';
288 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
289
290 return EOK;
291}
292
293/** Find task structure corresponding to task ID.
294 *
295 * The tasks_lock must be already held by the caller of this function and
296 * interrupts must be disabled.
297 *
298 * @param id Task ID.
299 *
300 * @return Task structure address or NULL if there is no such task
301 * ID.
302 */
303task_t *task_find_by_id(task_id_t id) { avltree_node_t *node;
304
305 node = avltree_search(&tasks_tree, (avltree_key_t) id);
306
307 if (node)
308 return avltree_get_instance(node, task_t, tasks_tree_node);
309 return NULL;
310}
311
312/** Get accounting data of given task.
313 *
314 * Note that task lock of 't' must be already held and interrupts must be
315 * already disabled.
316 *
317 * @param t Pointer to thread.
318 *
319 * @return Number of cycles used by the task and all its threads
320 * so far.
321 */
322uint64_t task_get_accounting(task_t *t)
323{
324 /* Accumulated value of task */
325 uint64_t ret = t->cycles;
326
327 /* Current values of threads */
328 link_t *cur;
329 for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
330 thread_t *thr = list_get_instance(cur, thread_t, th_link);
331
332 spinlock_lock(&thr->lock);
333 /* Process only counted threads */
334 if (!thr->uncounted) {
335 if (thr == THREAD) {
336 /* Update accounting of current thread */
337 thread_update_accounting();
338 }
339 ret += thr->cycles;
340 }
341 spinlock_unlock(&thr->lock);
342 }
343
344 return ret;
345}
346
347static void task_kill_internal(task_t *ta)
348{
349 link_t *cur;
350
351 /*
352 * Interrupt all threads.
353 */
354 spinlock_lock(&ta->lock);
355 for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
356 thread_t *thr;
357 bool sleeping = false;
358
359 thr = list_get_instance(cur, thread_t, th_link);
360
361 spinlock_lock(&thr->lock);
362 thr->interrupted = true;
363 if (thr->state == Sleeping)
364 sleeping = true;
365 spinlock_unlock(&thr->lock);
366
367 if (sleeping)
368 waitq_interrupt_sleep(thr);
369 }
370 spinlock_unlock(&ta->lock);
371}
372
373/** Kill task.
374 *
375 * This function is idempotent.
376 * It signals all the task's threads to bail it out.
377 *
378 * @param id ID of the task to be killed.
379 *
380 * @return Zero on success or an error code from errno.h.
381 */
382int task_kill(task_id_t id)
383{
384 ipl_t ipl;
385 task_t *ta;
386
387 if (id == 1)
388 return EPERM;
389
390 ipl = interrupts_disable();
391 spinlock_lock(&tasks_lock);
392 if (!(ta = task_find_by_id(id))) {
393 spinlock_unlock(&tasks_lock);
394 interrupts_restore(ipl);
395 return ENOENT;
396 }
397 task_kill_internal(ta);
398 spinlock_unlock(&tasks_lock);
399 interrupts_restore(ipl);
400 return 0;
401}
402
403static bool task_print_walker(avltree_node_t *node, void *arg)
404{
405 task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
406 int j;
407
408 spinlock_lock(&t->lock);
409
410 uint64_t cycles;
411 char suffix;
412 order(task_get_accounting(t), &cycles, &suffix);
413
414#ifdef __32_BITS__
415 printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %10p %10p %9" PRIu64
416 "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as, cycles,
417 suffix, atomic_get(&t->refcount), atomic_get(&t->active_calls));
418#endif
419
420#ifdef __64_BITS__
421 printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %18p %18p %9" PRIu64
422 "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as, cycles,
423 suffix, atomic_get(&t->refcount), atomic_get(&t->active_calls));
424#endif
425
426 for (j = 0; j < IPC_MAX_PHONES; j++) {
427 if (t->phones[j].callee)
428 printf(" %d:%p", j, t->phones[j].callee);
429 }
430 printf("\n");
431
432 spinlock_unlock(&t->lock);
433 return true;
434}
435
436/** Print task list */
437void task_print_list(void)
438{
439 ipl_t ipl;
440
441 /* Messing with task structures, avoid deadlock */
442 ipl = interrupts_disable();
443 spinlock_lock(&tasks_lock);
444
445#ifdef __32_BITS__
446 printf("taskid name ctx address as "
447 "cycles threads calls callee\n");
448 printf("------ ------------ --- ---------- ---------- "
449 "---------- ------- ------ ------>\n");
450#endif
451
452#ifdef __64_BITS__
453 printf("taskid name ctx address as "
454 "cycles threads calls callee\n");
455 printf("------ ------------ --- ------------------ ------------------ "
456 "---------- ------- ------ ------>\n");
457#endif
458
459 avltree_walk(&tasks_tree, task_print_walker, NULL);
460
461 spinlock_unlock(&tasks_lock);
462 interrupts_restore(ipl);
463}
464
465/** @}
466 */
Note: See TracBrowser for help on using the repository browser.