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

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

Add task_hold() and task_release().

  • Property mode set to 100644
File size: 12.2 KB
RevLine 
[f761f1eb]1/*
[278b4a30]2 * Copyright (c) 2010 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
[5ba201d]35 * @brief Task management.
[9179d0a]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>
[19f857a]55#include <str.h>
[41df2827]56#include <memstr.h>
[e3c762cd]57#include <syscall/copy.h>
[95ad426]58#include <macros.h>
59#include <ipc/event.h>
[8e5e78f]60
[b76a2217]61/** Spinlock protecting the tasks_tree AVL tree. */
[dc747e3]62SPINLOCK_INITIALIZE(tasks_lock);
[88169d9]63
[b76a2217]64/** AVL tree of active tasks.
[88169d9]65 *
[b76a2217]66 * The task is guaranteed to exist after it was found in the tasks_tree as
[6f4495f5]67 * long as:
[5ba201d]68 *
[88169d9]69 * @li the tasks_lock is held,
[6f4495f5]70 * @li the task's lock is held when task's lock is acquired before releasing
71 * tasks_lock or
[7bb6b06]72 * @li the task's refcount is greater than 0
[88169d9]73 *
74 */
[b76a2217]75avltree_t tasks_tree;
[88169d9]76
[286e03d]77static task_id_t task_counter = 0;
[70527f1]78
[103de761]79static slab_cache_t *task_slab;
80
[121966e]81/* Forward declarations. */
82static void task_kill_internal(task_t *);
[59ee56f]83static int tsk_constructor(void *, int);
[121966e]84
[814c4f5]85/** Initialize kernel tasks support. */
[f761f1eb]86void task_init(void)
87{
[43114c5]88 TASK = NULL;
[b76a2217]89 avltree_create(&tasks_tree);
[59ee56f]90 task_slab = slab_cache_create("task_slab", sizeof(task_t), 0,
91 tsk_constructor, NULL, 0);
[b76a2217]92}
93
94/*
[121966e]95 * The idea behind this walker is to kill and count all tasks different from
[814c4f5]96 * TASK.
[b76a2217]97 */
98static bool task_done_walker(avltree_node_t *node, void *arg)
99{
100 task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
[121966e]101 unsigned *cnt = (unsigned *) arg;
[5ba201d]102
[121966e]103 if (t != TASK) {
104 (*cnt)++;
105#ifdef CONFIG_DEBUG
106 printf("[%"PRIu64"] ", t->taskid);
107#endif
108 task_kill_internal(t);
[b76a2217]109 }
[5ba201d]110
111 /* Continue the walk */
112 return true;
[f761f1eb]113}
114
[814c4f5]115/** Kill all tasks except the current task. */
[f74bbaf]116void task_done(void)
117{
[121966e]118 unsigned tasks_left;
[5ba201d]119
[f74bbaf]120 do { /* Repeat until there are any tasks except TASK */
121 /* Messing with task structures, avoid deadlock */
[121966e]122#ifdef CONFIG_DEBUG
123 printf("Killing tasks... ");
124#endif
[f74bbaf]125 ipl_t ipl = interrupts_disable();
126 spinlock_lock(&tasks_lock);
[121966e]127 tasks_left = 0;
128 avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
129 spinlock_unlock(&tasks_lock);
130 interrupts_restore(ipl);
131 thread_sleep(1);
[f74bbaf]132#ifdef CONFIG_DEBUG
[121966e]133 printf("\n");
134#endif
135 } while (tasks_left);
[f74bbaf]136}
[70527f1]137
[59ee56f]138int tsk_constructor(void *obj, int kmflags)
139{
140 task_t *ta = obj;
141 int i;
[5ba201d]142
[59ee56f]143 atomic_set(&ta->refcount, 0);
144 atomic_set(&ta->lifecount, 0);
145 atomic_set(&ta->active_calls, 0);
[5ba201d]146
[59ee56f]147 spinlock_initialize(&ta->lock, "task_ta_lock");
148 mutex_initialize(&ta->futexes_lock, MUTEX_PASSIVE);
[5ba201d]149
[59ee56f]150 list_initialize(&ta->th_head);
151 list_initialize(&ta->sync_box_head);
[5ba201d]152
[59ee56f]153 ipc_answerbox_init(&ta->answerbox, ta);
154 for (i = 0; i < IPC_MAX_PHONES; i++)
155 ipc_phone_init(&ta->phones[i]);
[5ba201d]156
[59ee56f]157#ifdef CONFIG_UDEBUG
158 /* Init kbox stuff */
159 ta->kb.thread = NULL;
160 ipc_answerbox_init(&ta->kb.box, ta);
161 mutex_initialize(&ta->kb.cleanup_lock, MUTEX_PASSIVE);
162#endif
[5ba201d]163
[59ee56f]164 return 0;
165}
166
[814c4f5]167/** Create new task with no threads.
[70527f1]168 *
[5ba201d]169 * @param as Task's address space.
170 * @param name Symbolic name (a copy is made).
[70527f1]171 *
[5ba201d]172 * @return New task's structure.
[70527f1]173 *
174 */
[a000878c]175task_t *task_create(as_t *as, const char *name)
[f761f1eb]176{
[22f7769]177 ipl_t ipl;
[f761f1eb]178 task_t *ta;
179
[103de761]180 ta = (task_t *) slab_alloc(task_slab, 0);
[963074b3]181 task_create_arch(ta);
[bb68433]182 ta->as = as;
[24345a5]183 memcpy(ta->name, name, TASK_NAME_BUFLEN);
[b60c582]184 ta->name[TASK_NAME_BUFLEN - 1] = 0;
[5ba201d]185
[cfffb290]186 ta->context = CONTEXT;
[1077d91]187 ta->capabilities = 0;
[a2a00e8]188 ta->ucycles = 0;
189 ta->kcycles = 0;
[9a1b20c]190
[a307beb]191 ta->ipc_info.call_sent = 0;
192 ta->ipc_info.call_recieved = 0;
193 ta->ipc_info.answer_sent = 0;
194 ta->ipc_info.answer_recieved = 0;
195 ta->ipc_info.irq_notif_recieved = 0;
196 ta->ipc_info.forwarded = 0;
197
[9a1b20c]198#ifdef CONFIG_UDEBUG
199 /* Init debugging stuff */
200 udebug_task_init(&ta->udebug);
[5ba201d]201
[9a1b20c]202 /* Init kbox stuff */
[31696b4f]203 ta->kb.finished = false;
[9a1b20c]204#endif
[5ba201d]205
[59ee56f]206 if ((ipc_phone_0) &&
207 (context_check(ipc_phone_0->task->context, ta->context)))
[2ba7810]208 ipc_phone_connect(&ta->phones[0], ipc_phone_0);
[5ba201d]209
[4fded58]210 btree_create(&ta->futexes);
[bb68433]211
212 ipl = interrupts_disable();
[31d8e10]213 atomic_inc(&as->refcount);
[bb68433]214 spinlock_lock(&tasks_lock);
[286e03d]215 ta->taskid = ++task_counter;
[b76a2217]216 avltree_node_initialize(&ta->tasks_tree_node);
217 ta->tasks_tree_node.key = ta->taskid;
218 avltree_insert(&tasks_tree, &ta->tasks_tree_node);
[bb68433]219 spinlock_unlock(&tasks_lock);
220 interrupts_restore(ipl);
[8658f89]221
[f761f1eb]222 return ta;
223}
224
[7509ddc]225/** Destroy task.
226 *
[5ba201d]227 * @param t Task to be destroyed.
228 *
[7509ddc]229 */
230void task_destroy(task_t *t)
231{
[ea7890e7]232 /*
233 * Remove the task from the task B+tree.
234 */
235 spinlock_lock(&tasks_lock);
[b76a2217]236 avltree_delete(&tasks_tree, &t->tasks_tree_node);
[ea7890e7]237 spinlock_unlock(&tasks_lock);
[5ba201d]238
[ea7890e7]239 /*
240 * Perform architecture specific task destruction.
241 */
[31e8ddd]242 task_destroy_arch(t);
[5ba201d]243
[ea7890e7]244 /*
245 * Free up dynamically allocated state.
246 */
[31e8ddd]247 btree_destroy(&t->futexes);
[5ba201d]248
[ea7890e7]249 /*
250 * Drop our reference to the address space.
251 */
[31d8e10]252 if (atomic_predec(&t->as->refcount) == 0)
[31e8ddd]253 as_destroy(t->as);
254
[103de761]255 slab_free(task_slab, t);
[31e8ddd]256 TASK = NULL;
[7509ddc]257}
258
[278b4a30]259/** Hold a reference to a task.
260 *
261 * Holding a reference to a task prevents destruction of that task.
262 *
263 * @param t Task to be held.
264 */
265void task_hold(task_t *t)
266{
267 atomic_inc(&t->refcount);
268}
269
270/** Release a reference to a task.
271 *
272 * The last one to release a reference to a task destroys the task.
273 *
274 * @param t Task to be released.
275 */
276void task_release(task_t *t)
277{
278 if ((atomic_predec(&t->refcount)) == 0)
279 task_destroy(t);
280}
281
[ec55358]282/** Syscall for reading task ID from userspace.
283 *
[5ba201d]284 * @param uspace_task_id Userspace address of 8-byte buffer
285 * where to store current task ID.
286 *
287 * @return Zero on success or an error code from @ref errno.h.
[ec55358]288 *
289 */
[7f1c620]290unative_t sys_task_get_id(task_id_t *uspace_task_id)
[ec55358]291{
292 /*
[814c4f5]293 * No need to acquire lock on TASK because taskid remains constant for
294 * the lifespan of the task.
[ec55358]295 */
[6f4495f5]296 return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
297 sizeof(TASK->taskid));
[ec55358]298}
299
[bc18d63]300/** Syscall for setting the task name.
301 *
302 * The name simplifies identifying the task in the task list.
303 *
[5ba201d]304 * @param name The new name for the task. (typically the same
305 * as the command used to execute it).
[bc18d63]306 *
307 * @return 0 on success or an error code from @ref errno.h.
[5ba201d]308 *
[bc18d63]309 */
310unative_t sys_task_set_name(const char *uspace_name, size_t name_len)
311{
312 int rc;
313 char namebuf[TASK_NAME_BUFLEN];
[5ba201d]314
[bc18d63]315 /* Cap length of name and copy it from userspace. */
[5ba201d]316
[bc18d63]317 if (name_len > TASK_NAME_BUFLEN - 1)
318 name_len = TASK_NAME_BUFLEN - 1;
[5ba201d]319
[bc18d63]320 rc = copy_from_uspace(namebuf, uspace_name, name_len);
321 if (rc != 0)
322 return (unative_t) rc;
[5ba201d]323
[f4b1535]324 namebuf[name_len] = '\0';
325 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
[5ba201d]326
[bc18d63]327 return EOK;
328}
329
[9a8d91b]330/** Find task structure corresponding to task ID.
331 *
[814c4f5]332 * The tasks_lock must be already held by the caller of this function and
333 * interrupts must be disabled.
[9a8d91b]334 *
[5ba201d]335 * @param id Task ID.
336 *
[e1b6742]337 * @return Task structure address or NULL if there is no such task ID.
[9a8d91b]338 *
339 */
[5ba201d]340task_t *task_find_by_id(task_id_t id)
341{
[e1b6742]342 avltree_node_t *node =
343 avltree_search(&tasks_tree, (avltree_key_t) id);
[5ba201d]344
[b76a2217]345 if (node)
346 return avltree_get_instance(node, task_t, tasks_tree_node);
[5ba201d]347
[b76a2217]348 return NULL;
[9a8d91b]349}
350
[0313ff0]351/** Get accounting data of given task.
352 *
[814c4f5]353 * Note that task lock of 't' must be already held and interrupts must be
354 * already disabled.
[0313ff0]355 *
[88dea9d]356 * @param t Pointer to thread.
357 * @param ucycles Out pointer to sum of all user cycles.
358 * @param kcycles Out pointer to sum of all kernel cycles.
[0313ff0]359 *
360 */
[1ba37fa]361void task_get_accounting(task_t *t, uint64_t *ucycles, uint64_t *kcycles)
[0313ff0]362{
[a2a00e8]363 /* Accumulated values of task */
364 uint64_t uret = t->ucycles;
365 uint64_t kret = t->kcycles;
[0313ff0]366
367 /* Current values of threads */
368 link_t *cur;
369 for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
370 thread_t *thr = list_get_instance(cur, thread_t, th_link);
371
372 spinlock_lock(&thr->lock);
[62b6d17]373 /* Process only counted threads */
374 if (!thr->uncounted) {
[6f4495f5]375 if (thr == THREAD) {
376 /* Update accounting of current thread */
[a2a00e8]377 thread_update_accounting(false);
[6f4495f5]378 }
[a2a00e8]379 uret += thr->ucycles;
380 kret += thr->kcycles;
[62b6d17]381 }
[0313ff0]382 spinlock_unlock(&thr->lock);
383 }
384
[a2a00e8]385 *ucycles = uret;
386 *kcycles = kret;
[0313ff0]387}
388
[121966e]389static void task_kill_internal(task_t *ta)
390{
391 link_t *cur;
[5ba201d]392
[121966e]393 /*
394 * Interrupt all threads.
395 */
396 spinlock_lock(&ta->lock);
397 for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
398 thread_t *thr;
399 bool sleeping = false;
400
401 thr = list_get_instance(cur, thread_t, th_link);
402
403 spinlock_lock(&thr->lock);
404 thr->interrupted = true;
405 if (thr->state == Sleeping)
406 sleeping = true;
407 spinlock_unlock(&thr->lock);
408
409 if (sleeping)
410 waitq_interrupt_sleep(thr);
411 }
412 spinlock_unlock(&ta->lock);
413}
414
[7509ddc]415/** Kill task.
[ea7890e7]416 *
417 * This function is idempotent.
418 * It signals all the task's threads to bail it out.
[7509ddc]419 *
[5ba201d]420 * @param id ID of the task to be killed.
421 *
422 * @return Zero on success or an error code from errno.h.
[7509ddc]423 *
424 */
425int task_kill(task_id_t id)
426{
427 ipl_t ipl;
428 task_t *ta;
[9b6aae6]429
430 if (id == 1)
431 return EPERM;
[7509ddc]432
433 ipl = interrupts_disable();
434 spinlock_lock(&tasks_lock);
435 if (!(ta = task_find_by_id(id))) {
436 spinlock_unlock(&tasks_lock);
437 interrupts_restore(ipl);
438 return ENOENT;
439 }
[121966e]440 task_kill_internal(ta);
[31e8ddd]441 spinlock_unlock(&tasks_lock);
[34dcd3f]442 interrupts_restore(ipl);
[7509ddc]443 return 0;
444}
445
[b76a2217]446static bool task_print_walker(avltree_node_t *node, void *arg)
447{
448 task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
449 int j;
[5ba201d]450
[b76a2217]451 spinlock_lock(&t->lock);
[5ba201d]452
[a2a00e8]453 uint64_t ucycles;
454 uint64_t kcycles;
[1ba37fa]455 char usuffix, ksuffix;
456 task_get_accounting(t, &ucycles, &kcycles);
[e535eeb]457 order_suffix(ucycles, &ucycles, &usuffix);
458 order_suffix(kcycles, &kcycles, &ksuffix);
[88dea9d]459
[52755f1]460#ifdef __32_BITS__
[07640dfd]461 printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %10p %10p %9" PRIu64 "%c %9"
462 PRIu64 "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as,
[a2a00e8]463 ucycles, usuffix, kcycles, ksuffix, atomic_get(&t->refcount),
464 atomic_get(&t->active_calls));
[52755f1]465#endif
[5ba201d]466
[52755f1]467#ifdef __64_BITS__
[07640dfd]468 printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %18p %18p %9" PRIu64 "%c %9"
469 PRIu64 "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as,
[a2a00e8]470 ucycles, usuffix, kcycles, ksuffix, atomic_get(&t->refcount),
471 atomic_get(&t->active_calls));
[52755f1]472#endif
[5ba201d]473
[b76a2217]474 for (j = 0; j < IPC_MAX_PHONES; j++) {
475 if (t->phones[j].callee)
[52755f1]476 printf(" %d:%p", j, t->phones[j].callee);
[b76a2217]477 }
478 printf("\n");
[5ba201d]479
[b76a2217]480 spinlock_unlock(&t->lock);
481 return true;
482}
483
[37c57f2]484/** Print task list */
485void task_print_list(void)
486{
487 ipl_t ipl;
488
[f74bbaf]489 /* Messing with task structures, avoid deadlock */
[37c57f2]490 ipl = interrupts_disable();
491 spinlock_lock(&tasks_lock);
[5ba201d]492
493#ifdef __32_BITS__
[a2a00e8]494 printf("taskid name ctx address as "
[07640dfd]495 " ucycles kcycles threads calls callee\n");
[a2a00e8]496 printf("------ ------------ --- ---------- ----------"
[07640dfd]497 " ---------- ---------- ------- ------ ------>\n");
[52755f1]498#endif
[5ba201d]499
[52755f1]500#ifdef __64_BITS__
[a2a00e8]501 printf("taskid name ctx address as "
[07640dfd]502 " ucycles kcycles threads calls callee\n");
[a2a00e8]503 printf("------ ------------ --- ------------------ ------------------"
[07640dfd]504 " ---------- ---------- ---------- ------- ------ ------>\n");
[52755f1]505#endif
[5ba201d]506
[b76a2217]507 avltree_walk(&tasks_tree, task_print_walker, NULL);
[5ba201d]508
[37c57f2]509 spinlock_unlock(&tasks_lock);
510 interrupts_restore(ipl);
511}
[7509ddc]512
[cc73a8a1]513/** @}
[b45c443]514 */
Note: See TracBrowser for help on using the repository browser.