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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d359e227 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
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]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
[cc73a8a1]29/** @addtogroup genericproc
[b45c443]30 * @{
31 */
32
[9179d0a]33/**
[b45c443]34 * @file
[9179d0a]35 * @brief Task management.
36 */
37
[f761f1eb]38#include <proc/thread.h>
39#include <proc/task.h>
[20d50a1]40#include <mm/as.h>
[085d973]41#include <mm/slab.h>
[31d8e10]42#include <atomic.h>
[f761f1eb]43#include <synch/spinlock.h>
[5573942]44#include <synch/waitq.h>
[f761f1eb]45#include <arch.h>
[8605b24]46#include <arch/barrier.h>
[b76a2217]47#include <adt/avl.h>
[7f6e755]48#include <adt/btree.h>
[5c9a08b]49#include <adt/list.h>
[6d9c49a]50#include <ipc/ipc.h>
[c98e6ee]51#include <ipc/ipcrsc.h>
[37c57f2]52#include <print.h>
[7509ddc]53#include <errno.h>
[95155b0c]54#include <func.h>
[16da5f8e]55#include <string.h>
[e3c762cd]56#include <syscall/copy.h>
[95ad426]57#include <macros.h>
58#include <ipc/event.h>
[8e5e78f]59
[b76a2217]60/** Spinlock protecting the tasks_tree AVL tree. */
[dc747e3]61SPINLOCK_INITIALIZE(tasks_lock);
[88169d9]62
[b76a2217]63/** AVL tree of active tasks.
[88169d9]64 *
[b76a2217]65 * The task is guaranteed to exist after it was found in the tasks_tree as
[6f4495f5]66 * long as:
[88169d9]67 * @li the tasks_lock is held,
[6f4495f5]68 * @li the task's lock is held when task's lock is acquired before releasing
69 * tasks_lock or
[7bb6b06]70 * @li the task's refcount is greater than 0
[88169d9]71 *
72 */
[b76a2217]73avltree_t tasks_tree;
[88169d9]74
[286e03d]75static task_id_t task_counter = 0;
[70527f1]76
[103de761]77static slab_cache_t *task_slab;
78
[121966e]79/* Forward declarations. */
80static void task_kill_internal(task_t *);
[59ee56f]81static int tsk_constructor(void *, int);
[121966e]82
[814c4f5]83/** Initialize kernel tasks support. */
[f761f1eb]84void task_init(void)
85{
[43114c5]86 TASK = NULL;
[b76a2217]87 avltree_create(&tasks_tree);
[59ee56f]88 task_slab = slab_cache_create("task_slab", sizeof(task_t), 0,
89 tsk_constructor, NULL, 0);
[b76a2217]90}
91
92/*
[121966e]93 * The idea behind this walker is to kill and count all tasks different from
[814c4f5]94 * TASK.
[b76a2217]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);
[121966e]99 unsigned *cnt = (unsigned *) arg;
[b76a2217]100
[121966e]101 if (t != TASK) {
102 (*cnt)++;
103#ifdef CONFIG_DEBUG
104 printf("[%"PRIu64"] ", t->taskid);
105#endif
106 task_kill_internal(t);
[b76a2217]107 }
108
109 return true; /* continue the walk */
[f761f1eb]110}
111
[814c4f5]112/** Kill all tasks except the current task. */
[f74bbaf]113void task_done(void)
114{
[121966e]115 unsigned tasks_left;
116
[f74bbaf]117 do { /* Repeat until there are any tasks except TASK */
118 /* Messing with task structures, avoid deadlock */
[121966e]119#ifdef CONFIG_DEBUG
120 printf("Killing tasks... ");
121#endif
[f74bbaf]122 ipl_t ipl = interrupts_disable();
123 spinlock_lock(&tasks_lock);
[121966e]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);
[f74bbaf]129#ifdef CONFIG_DEBUG
[121966e]130 printf("\n");
131#endif
132 } while (tasks_left);
[f74bbaf]133}
[70527f1]134
[59ee56f]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
[814c4f5]164/** Create new task with no threads.
[70527f1]165 *
[814c4f5]166 * @param as Task's address space.
[24345a5]167 * @param name Symbolic name (a copy is made).
[70527f1]168 *
[814c4f5]169 * @return New task's structure.
[70527f1]170 *
171 */
[ff14c520]172task_t *task_create(as_t *as, char *name)
[f761f1eb]173{
[22f7769]174 ipl_t ipl;
[f761f1eb]175 task_t *ta;
176
[103de761]177 ta = (task_t *) slab_alloc(task_slab, 0);
[963074b3]178 task_create_arch(ta);
[bb68433]179 ta->as = as;
[24345a5]180 memcpy(ta->name, name, TASK_NAME_BUFLEN);
[b60c582]181 ta->name[TASK_NAME_BUFLEN - 1] = 0;
[24345a5]182
[cfffb290]183 ta->context = CONTEXT;
[1077d91]184 ta->capabilities = 0;
[0313ff0]185 ta->cycles = 0;
[9a1b20c]186
187#ifdef CONFIG_UDEBUG
188 /* Init debugging stuff */
189 udebug_task_init(&ta->udebug);
190
191 /* Init kbox stuff */
[31696b4f]192 ta->kb.finished = false;
[9a1b20c]193#endif
194
[59ee56f]195 if ((ipc_phone_0) &&
196 (context_check(ipc_phone_0->task->context, ta->context)))
[2ba7810]197 ipc_phone_connect(&ta->phones[0], ipc_phone_0);
[4fded58]198
199 btree_create(&ta->futexes);
[bb68433]200
201 ipl = interrupts_disable();
[31d8e10]202 atomic_inc(&as->refcount);
[bb68433]203 spinlock_lock(&tasks_lock);
[286e03d]204 ta->taskid = ++task_counter;
[b76a2217]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);
[bb68433]208 spinlock_unlock(&tasks_lock);
209 interrupts_restore(ipl);
[8658f89]210
[f761f1eb]211 return ta;
212}
213
[7509ddc]214/** Destroy task.
215 *
[814c4f5]216 * @param t Task to be destroyed.
[7509ddc]217 */
218void task_destroy(task_t *t)
219{
[ea7890e7]220 /*
221 * Remove the task from the task B+tree.
222 */
223 spinlock_lock(&tasks_lock);
[b76a2217]224 avltree_delete(&tasks_tree, &t->tasks_tree_node);
[ea7890e7]225 spinlock_unlock(&tasks_lock);
226
227 /*
228 * Perform architecture specific task destruction.
229 */
[31e8ddd]230 task_destroy_arch(t);
[ea7890e7]231
232 /*
233 * Free up dynamically allocated state.
234 */
[31e8ddd]235 btree_destroy(&t->futexes);
236
[ea7890e7]237 /*
238 * Drop our reference to the address space.
239 */
[31d8e10]240 if (atomic_predec(&t->as->refcount) == 0)
[31e8ddd]241 as_destroy(t->as);
242
[103de761]243 slab_free(task_slab, t);
[31e8ddd]244 TASK = NULL;
[7509ddc]245}
246
[ec55358]247/** Syscall for reading task ID from userspace.
248 *
[814c4f5]249 * @param uspace_task_id userspace address of 8-byte buffer
250 * where to store current task ID.
[ec55358]251 *
[814c4f5]252 * @return Zero on success or an error code from @ref errno.h.
[ec55358]253 */
[7f1c620]254unative_t sys_task_get_id(task_id_t *uspace_task_id)
[ec55358]255{
256 /*
[814c4f5]257 * No need to acquire lock on TASK because taskid remains constant for
258 * the lifespan of the task.
[ec55358]259 */
[6f4495f5]260 return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
261 sizeof(TASK->taskid));
[ec55358]262}
263
[bc18d63]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
[f4b1535]287 namebuf[name_len] = '\0';
288 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
[bc18d63]289
290 return EOK;
291}
292
[9a8d91b]293/** Find task structure corresponding to task ID.
294 *
[814c4f5]295 * The tasks_lock must be already held by the caller of this function and
296 * interrupts must be disabled.
[9a8d91b]297 *
[814c4f5]298 * @param id Task ID.
[9a8d91b]299 *
[814c4f5]300 * @return Task structure address or NULL if there is no such task
301 * ID.
[9a8d91b]302 */
[814c4f5]303task_t *task_find_by_id(task_id_t id) { avltree_node_t *node;
[9a8d91b]304
[b76a2217]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;
[9a8d91b]310}
311
[0313ff0]312/** Get accounting data of given task.
313 *
[814c4f5]314 * Note that task lock of 't' must be already held and interrupts must be
315 * already disabled.
[0313ff0]316 *
[814c4f5]317 * @param t Pointer to thread.
[0313ff0]318 *
[814c4f5]319 * @return Number of cycles used by the task and all its threads
320 * so far.
[0313ff0]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);
[62b6d17]333 /* Process only counted threads */
334 if (!thr->uncounted) {
[6f4495f5]335 if (thr == THREAD) {
336 /* Update accounting of current thread */
337 thread_update_accounting();
338 }
[62b6d17]339 ret += thr->cycles;
340 }
[0313ff0]341 spinlock_unlock(&thr->lock);
342 }
343
344 return ret;
345}
346
[121966e]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
[7509ddc]373/** Kill task.
[ea7890e7]374 *
375 * This function is idempotent.
376 * It signals all the task's threads to bail it out.
[7509ddc]377 *
[814c4f5]378 * @param id ID of the task to be killed.
[7509ddc]379 *
[814c4f5]380 * @return Zero on success or an error code from errno.h.
[7509ddc]381 */
382int task_kill(task_id_t id)
383{
384 ipl_t ipl;
385 task_t *ta;
[9b6aae6]386
387 if (id == 1)
388 return EPERM;
[7509ddc]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 }
[121966e]397 task_kill_internal(ta);
[31e8ddd]398 spinlock_unlock(&tasks_lock);
[34dcd3f]399 interrupts_restore(ipl);
[7509ddc]400 return 0;
401}
402
[b76a2217]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);
[52755f1]413
414#ifdef __32_BITS__
[0f94c3d]415 printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %10p %10p %9" PRIu64
[8605b24]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));
[52755f1]418#endif
419
420#ifdef __64_BITS__
[0f94c3d]421 printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %18p %18p %9" PRIu64
[8605b24]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));
[52755f1]424#endif
425
[b76a2217]426 for (j = 0; j < IPC_MAX_PHONES; j++) {
427 if (t->phones[j].callee)
[52755f1]428 printf(" %d:%p", j, t->phones[j].callee);
[b76a2217]429 }
430 printf("\n");
431
432 spinlock_unlock(&t->lock);
433 return true;
434}
435
[37c57f2]436/** Print task list */
437void task_print_list(void)
438{
439 ipl_t ipl;
440
[f74bbaf]441 /* Messing with task structures, avoid deadlock */
[37c57f2]442 ipl = interrupts_disable();
443 spinlock_lock(&tasks_lock);
[52755f1]444
445#ifdef __32_BITS__
[2e079b70]446 printf("taskid name ctx address as "
[8605b24]447 "cycles threads calls callee\n");
[0f94c3d]448 printf("------ ------------ --- ---------- ---------- "
[8605b24]449 "---------- ------- ------ ------>\n");
[52755f1]450#endif
451
452#ifdef __64_BITS__
[2e079b70]453 printf("taskid name ctx address as "
[8605b24]454 "cycles threads calls callee\n");
[0f94c3d]455 printf("------ ------------ --- ------------------ ------------------ "
[8605b24]456 "---------- ------- ------ ------>\n");
[52755f1]457#endif
[37c57f2]458
[b76a2217]459 avltree_walk(&tasks_tree, task_print_walker, NULL);
[37c57f2]460
461 spinlock_unlock(&tasks_lock);
462 interrupts_restore(ipl);
463}
[7509ddc]464
[cc73a8a1]465/** @}
[b45c443]466 */
Note: See TracBrowser for help on using the repository browser.