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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 577f042a was 577f042a, checked in by Martin Decky <martin@…>, 14 years ago
  • improve synchronization in sys_task_set_name()
  • improve printouts of 'threads' kernel console command
  • Property mode set to 100644
File size: 13.8 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. */
[da1bafb]62IRQ_SPINLOCK_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 *);
[da1bafb]83static int tsk_constructor(void *, unsigned int);
[121966e]84
[da1bafb]85/** Initialize kernel tasks support.
86 *
87 */
[f761f1eb]88void task_init(void)
89{
[43114c5]90 TASK = NULL;
[b76a2217]91 avltree_create(&tasks_tree);
[59ee56f]92 task_slab = slab_cache_create("task_slab", sizeof(task_t), 0,
93 tsk_constructor, NULL, 0);
[b76a2217]94}
95
[da1bafb]96/** Task finish walker.
97 *
[121966e]98 * The idea behind this walker is to kill and count all tasks different from
[814c4f5]99 * TASK.
[da1bafb]100 *
[b76a2217]101 */
102static bool task_done_walker(avltree_node_t *node, void *arg)
103{
[da1bafb]104 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
105 size_t *cnt = (size_t *) arg;
[5ba201d]106
[da1bafb]107 if (task != TASK) {
[121966e]108 (*cnt)++;
[da1bafb]109
[121966e]110#ifdef CONFIG_DEBUG
[da1bafb]111 printf("[%"PRIu64"] ", task->taskid);
[121966e]112#endif
[da1bafb]113
114 task_kill_internal(task);
[b76a2217]115 }
[5ba201d]116
117 /* Continue the walk */
118 return true;
[f761f1eb]119}
120
[da1bafb]121/** Kill all tasks except the current task.
122 *
123 */
[f74bbaf]124void task_done(void)
125{
[da1bafb]126 size_t tasks_left;
[5ba201d]127
[da1bafb]128 /* Repeat until there are any tasks except TASK */
129 do {
[121966e]130#ifdef CONFIG_DEBUG
131 printf("Killing tasks... ");
132#endif
[da1bafb]133
134 irq_spinlock_lock(&tasks_lock, true);
[121966e]135 tasks_left = 0;
136 avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
[da1bafb]137 irq_spinlock_unlock(&tasks_lock, true);
138
[121966e]139 thread_sleep(1);
[da1bafb]140
[f74bbaf]141#ifdef CONFIG_DEBUG
[121966e]142 printf("\n");
143#endif
[da1bafb]144 } while (tasks_left > 0);
[f74bbaf]145}
[70527f1]146
[da1bafb]147int tsk_constructor(void *obj, unsigned int kmflags)
[59ee56f]148{
[da1bafb]149 task_t *task = (task_t *) obj;
150
151 atomic_set(&task->refcount, 0);
152 atomic_set(&task->lifecount, 0);
[5ba201d]153
[da1bafb]154 irq_spinlock_initialize(&task->lock, "task_t_lock");
155 mutex_initialize(&task->futexes_lock, MUTEX_PASSIVE);
[5ba201d]156
[da1bafb]157 list_initialize(&task->th_head);
158 list_initialize(&task->sync_box_head);
[5ba201d]159
[da1bafb]160 ipc_answerbox_init(&task->answerbox, task);
[5ba201d]161
[da1bafb]162 size_t i;
[59ee56f]163 for (i = 0; i < IPC_MAX_PHONES; i++)
[da1bafb]164 ipc_phone_init(&task->phones[i]);
[5ba201d]165
[59ee56f]166#ifdef CONFIG_UDEBUG
167 /* Init kbox stuff */
[da1bafb]168 task->kb.thread = NULL;
169 ipc_answerbox_init(&task->kb.box, task);
170 mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
[59ee56f]171#endif
[5ba201d]172
[59ee56f]173 return 0;
174}
175
[814c4f5]176/** Create new task with no threads.
[70527f1]177 *
[5ba201d]178 * @param as Task's address space.
179 * @param name Symbolic name (a copy is made).
[70527f1]180 *
[5ba201d]181 * @return New task's structure.
[70527f1]182 *
183 */
[a000878c]184task_t *task_create(as_t *as, const char *name)
[f761f1eb]185{
[da1bafb]186 task_t *task = (task_t *) slab_alloc(task_slab, 0);
187 task_create_arch(task);
188
189 task->as = as;
190 str_cpy(task->name, TASK_NAME_BUFLEN, name);
191
192 task->context = CONTEXT;
193 task->capabilities = 0;
194 task->ucycles = 0;
195 task->kcycles = 0;
196
197 task->ipc_info.call_sent = 0;
[be06914]198 task->ipc_info.call_received = 0;
[da1bafb]199 task->ipc_info.answer_sent = 0;
[be06914]200 task->ipc_info.answer_received = 0;
201 task->ipc_info.irq_notif_received = 0;
[da1bafb]202 task->ipc_info.forwarded = 0;
203
[9a1b20c]204#ifdef CONFIG_UDEBUG
205 /* Init debugging stuff */
[da1bafb]206 udebug_task_init(&task->udebug);
[5ba201d]207
[9a1b20c]208 /* Init kbox stuff */
[da1bafb]209 task->kb.finished = false;
[9a1b20c]210#endif
[5ba201d]211
[59ee56f]212 if ((ipc_phone_0) &&
[da1bafb]213 (context_check(ipc_phone_0->task->context, task->context)))
214 ipc_phone_connect(&task->phones[0], ipc_phone_0);
[5ba201d]215
[da1bafb]216 btree_create(&task->futexes);
[bb68433]217
[6193351]218 /*
219 * Get a reference to the address space.
220 */
[da1bafb]221 as_hold(task->as);
222
223 irq_spinlock_lock(&tasks_lock, true);
224
225 task->taskid = ++task_counter;
226 avltree_node_initialize(&task->tasks_tree_node);
227 task->tasks_tree_node.key = task->taskid;
228 avltree_insert(&tasks_tree, &task->tasks_tree_node);
229
230 irq_spinlock_unlock(&tasks_lock, true);
231
232 return task;
[f761f1eb]233}
234
[7509ddc]235/** Destroy task.
236 *
[da1bafb]237 * @param task Task to be destroyed.
[5ba201d]238 *
[7509ddc]239 */
[da1bafb]240void task_destroy(task_t *task)
[7509ddc]241{
[ea7890e7]242 /*
243 * Remove the task from the task B+tree.
244 */
[da1bafb]245 irq_spinlock_lock(&tasks_lock, true);
246 avltree_delete(&tasks_tree, &task->tasks_tree_node);
247 irq_spinlock_unlock(&tasks_lock, true);
[5ba201d]248
[ea7890e7]249 /*
250 * Perform architecture specific task destruction.
251 */
[da1bafb]252 task_destroy_arch(task);
[5ba201d]253
[ea7890e7]254 /*
255 * Free up dynamically allocated state.
256 */
[da1bafb]257 btree_destroy(&task->futexes);
[5ba201d]258
[ea7890e7]259 /*
260 * Drop our reference to the address space.
261 */
[da1bafb]262 as_release(task->as);
[31e8ddd]263
[da1bafb]264 slab_free(task_slab, task);
[7509ddc]265}
266
[278b4a30]267/** Hold a reference to a task.
268 *
269 * Holding a reference to a task prevents destruction of that task.
270 *
[da1bafb]271 * @param task Task to be held.
272 *
[278b4a30]273 */
[da1bafb]274void task_hold(task_t *task)
[278b4a30]275{
[da1bafb]276 atomic_inc(&task->refcount);
[278b4a30]277}
278
279/** Release a reference to a task.
280 *
281 * The last one to release a reference to a task destroys the task.
282 *
[da1bafb]283 * @param task Task to be released.
284 *
[278b4a30]285 */
[da1bafb]286void task_release(task_t *task)
[278b4a30]287{
[da1bafb]288 if ((atomic_predec(&task->refcount)) == 0)
289 task_destroy(task);
[278b4a30]290}
291
[dd8d5a7]292#ifdef __32_BITS__
293
294/** Syscall for reading task ID from userspace (32 bits)
[ec55358]295 *
[dd8d5a7]296 * @param uspace_taskid Pointer to user-space buffer
297 * where to store current task ID.
[5ba201d]298 *
299 * @return Zero on success or an error code from @ref errno.h.
[ec55358]300 *
301 */
[dd8d5a7]302sysarg_t sys_task_get_id(sysarg64_t *uspace_taskid)
[ec55358]303{
304 /*
[814c4f5]305 * No need to acquire lock on TASK because taskid remains constant for
306 * the lifespan of the task.
[ec55358]307 */
[dd8d5a7]308 return (sysarg_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
[6f4495f5]309 sizeof(TASK->taskid));
[ec55358]310}
311
[dd8d5a7]312#endif /* __32_BITS__ */
313
314#ifdef __64_BITS__
315
316/** Syscall for reading task ID from userspace (64 bits)
317 *
318 * @return Current task ID.
319 *
320 */
321sysarg_t sys_task_get_id(void)
322{
323 /*
324 * No need to acquire lock on TASK because taskid remains constant for
325 * the lifespan of the task.
326 */
327 return TASK->taskid;
328}
329
330#endif /* __64_BITS__ */
331
[bc18d63]332/** Syscall for setting the task name.
333 *
334 * The name simplifies identifying the task in the task list.
335 *
[5ba201d]336 * @param name The new name for the task. (typically the same
337 * as the command used to execute it).
[bc18d63]338 *
339 * @return 0 on success or an error code from @ref errno.h.
[5ba201d]340 *
[bc18d63]341 */
[96b02eb9]342sysarg_t sys_task_set_name(const char *uspace_name, size_t name_len)
[bc18d63]343{
344 char namebuf[TASK_NAME_BUFLEN];
[5ba201d]345
[bc18d63]346 /* Cap length of name and copy it from userspace. */
347 if (name_len > TASK_NAME_BUFLEN - 1)
348 name_len = TASK_NAME_BUFLEN - 1;
[5ba201d]349
[577f042a]350 int rc = copy_from_uspace(namebuf, uspace_name, name_len);
[bc18d63]351 if (rc != 0)
[96b02eb9]352 return (sysarg_t) rc;
[5ba201d]353
[f4b1535]354 namebuf[name_len] = '\0';
[577f042a]355
356 /*
357 * As the task name is referenced also from the
358 * threads, lock the threads' lock for the course
359 * of the update.
360 */
361
362 irq_spinlock_lock(&tasks_lock, true);
363 irq_spinlock_lock(&TASK->lock, false);
364 irq_spinlock_lock(&threads_lock, false);
365
366 /* Set task name */
[f4b1535]367 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
[5ba201d]368
[577f042a]369 irq_spinlock_unlock(&threads_lock, false);
370 irq_spinlock_unlock(&TASK->lock, false);
371 irq_spinlock_unlock(&tasks_lock, true);
372
[bc18d63]373 return EOK;
374}
375
[1e9f8ab]376/** Syscall to forcefully terminate a task
377 *
378 * @param uspace_taskid Pointer to task ID in user space.
379 *
380 * @return 0 on success or an error code from @ref errno.h.
381 *
382 */
383sysarg_t sys_task_kill(task_id_t *uspace_taskid)
384{
385 task_id_t taskid;
386 int rc;
387
388 rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
389 if (rc != 0)
390 return (sysarg_t) rc;
391
392 return (sysarg_t) task_kill(taskid);
393}
394
[9a8d91b]395/** Find task structure corresponding to task ID.
396 *
[814c4f5]397 * The tasks_lock must be already held by the caller of this function and
398 * interrupts must be disabled.
[9a8d91b]399 *
[5ba201d]400 * @param id Task ID.
401 *
[e1b6742]402 * @return Task structure address or NULL if there is no such task ID.
[9a8d91b]403 *
404 */
[5ba201d]405task_t *task_find_by_id(task_id_t id)
406{
[1d432f9]407 ASSERT(interrupts_disabled());
408 ASSERT(irq_spinlock_locked(&tasks_lock));
409
[e1b6742]410 avltree_node_t *node =
411 avltree_search(&tasks_tree, (avltree_key_t) id);
[5ba201d]412
[b76a2217]413 if (node)
[da1bafb]414 return avltree_get_instance(node, task_t, tasks_tree_node);
[5ba201d]415
[b76a2217]416 return NULL;
[9a8d91b]417}
418
[0313ff0]419/** Get accounting data of given task.
420 *
[1d432f9]421 * Note that task lock of 'task' must be already held and interrupts must be
[814c4f5]422 * already disabled.
[0313ff0]423 *
[da1bafb]424 * @param task Pointer to the task.
[88dea9d]425 * @param ucycles Out pointer to sum of all user cycles.
426 * @param kcycles Out pointer to sum of all kernel cycles.
[0313ff0]427 *
428 */
[da1bafb]429void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
[0313ff0]430{
[1d432f9]431 ASSERT(interrupts_disabled());
432 ASSERT(irq_spinlock_locked(&task->lock));
433
[a2a00e8]434 /* Accumulated values of task */
[da1bafb]435 uint64_t uret = task->ucycles;
436 uint64_t kret = task->kcycles;
[0313ff0]437
438 /* Current values of threads */
439 link_t *cur;
[da1bafb]440 for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
441 thread_t *thread = list_get_instance(cur, thread_t, th_link);
442
443 irq_spinlock_lock(&thread->lock, false);
[0313ff0]444
[62b6d17]445 /* Process only counted threads */
[da1bafb]446 if (!thread->uncounted) {
447 if (thread == THREAD) {
[6f4495f5]448 /* Update accounting of current thread */
[a2a00e8]449 thread_update_accounting(false);
[da1bafb]450 }
451
452 uret += thread->ucycles;
453 kret += thread->kcycles;
[62b6d17]454 }
[da1bafb]455
456 irq_spinlock_unlock(&thread->lock, false);
[0313ff0]457 }
458
[a2a00e8]459 *ucycles = uret;
460 *kcycles = kret;
[0313ff0]461}
462
[da1bafb]463static void task_kill_internal(task_t *task)
[121966e]464{
[df58e44]465 irq_spinlock_lock(&task->lock, false);
466 irq_spinlock_lock(&threads_lock, false);
[5ba201d]467
[121966e]468 /*
469 * Interrupt all threads.
470 */
[df58e44]471
472 link_t *cur;
[da1bafb]473 for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
474 thread_t *thread = list_get_instance(cur, thread_t, th_link);
[121966e]475 bool sleeping = false;
476
[da1bafb]477 irq_spinlock_lock(&thread->lock, false);
[121966e]478
[da1bafb]479 thread->interrupted = true;
480 if (thread->state == Sleeping)
[121966e]481 sleeping = true;
[da1bafb]482
483 irq_spinlock_unlock(&thread->lock, false);
[121966e]484
485 if (sleeping)
[da1bafb]486 waitq_interrupt_sleep(thread);
[121966e]487 }
[da1bafb]488
[df58e44]489 irq_spinlock_unlock(&threads_lock, false);
[da1bafb]490 irq_spinlock_unlock(&task->lock, false);
[121966e]491}
492
[7509ddc]493/** Kill task.
[ea7890e7]494 *
495 * This function is idempotent.
496 * It signals all the task's threads to bail it out.
[7509ddc]497 *
[5ba201d]498 * @param id ID of the task to be killed.
499 *
500 * @return Zero on success or an error code from errno.h.
[7509ddc]501 *
502 */
503int task_kill(task_id_t id)
504{
[9b6aae6]505 if (id == 1)
506 return EPERM;
[7509ddc]507
[da1bafb]508 irq_spinlock_lock(&tasks_lock, true);
509
510 task_t *task = task_find_by_id(id);
511 if (!task) {
512 irq_spinlock_unlock(&tasks_lock, true);
[7509ddc]513 return ENOENT;
514 }
[da1bafb]515
516 task_kill_internal(task);
517 irq_spinlock_unlock(&tasks_lock, true);
518
519 return EOK;
[7509ddc]520}
521
[b76a2217]522static bool task_print_walker(avltree_node_t *node, void *arg)
523{
[c0f13d2]524 bool *additional = (bool *) arg;
[da1bafb]525 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
526 irq_spinlock_lock(&task->lock, false);
[5ba201d]527
[a2a00e8]528 uint64_t ucycles;
529 uint64_t kcycles;
[1ba37fa]530 char usuffix, ksuffix;
[da1bafb]531 task_get_accounting(task, &ucycles, &kcycles);
[e535eeb]532 order_suffix(ucycles, &ucycles, &usuffix);
533 order_suffix(kcycles, &kcycles, &ksuffix);
[88dea9d]534
[da1bafb]535#ifdef __32_BITS__
[c0f13d2]536 if (*additional)
[97d17fe]537 printf("%-8" PRIu64 " %9" PRIua, task->taskid,
538 atomic_get(&task->refcount));
[c0f13d2]539 else
540 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
541 " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
542 task->name, task->context, task, task->as,
543 ucycles, usuffix, kcycles, ksuffix);
[52755f1]544#endif
[5ba201d]545
[52755f1]546#ifdef __64_BITS__
[c0f13d2]547 if (*additional)
[7e752b2]548 printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
[97d17fe]549 "%9" PRIua, task->taskid, ucycles, usuffix, kcycles,
550 ksuffix, atomic_get(&task->refcount));
[c0f13d2]551 else
552 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
553 task->taskid, task->name, task->context, task, task->as);
[52755f1]554#endif
[5ba201d]555
[c0f13d2]556 if (*additional) {
557 size_t i;
558 for (i = 0; i < IPC_MAX_PHONES; i++) {
559 if (task->phones[i].callee)
[7e752b2]560 printf(" %zu:%p", i, task->phones[i].callee);
[c0f13d2]561 }
562 printf("\n");
[b76a2217]563 }
[5ba201d]564
[da1bafb]565 irq_spinlock_unlock(&task->lock, false);
[b76a2217]566 return true;
567}
568
[c0f13d2]569/** Print task list
570 *
571 * @param additional Print additional information.
572 *
573 */
574void task_print_list(bool additional)
[37c57f2]575{
[f74bbaf]576 /* Messing with task structures, avoid deadlock */
[da1bafb]577 irq_spinlock_lock(&tasks_lock, true);
[5ba201d]578
579#ifdef __32_BITS__
[c0f13d2]580 if (additional)
[48dcc69]581 printf("[id ] [threads] [calls] [callee\n");
[c0f13d2]582 else
[48dcc69]583 printf("[id ] [name ] [ctx] [address ] [as ]"
[c0f13d2]584 " [ucycles ] [kcycles ]\n");
[52755f1]585#endif
[5ba201d]586
[52755f1]587#ifdef __64_BITS__
[c0f13d2]588 if (additional)
[be06914]589 printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
[c0f13d2]590 " [callee\n");
591 else
[be06914]592 printf("[id ] [name ] [ctx] [address ]"
[c0f13d2]593 " [as ]\n");
[52755f1]594#endif
[5ba201d]595
[c0f13d2]596 avltree_walk(&tasks_tree, task_print_walker, &additional);
[5ba201d]597
[da1bafb]598 irq_spinlock_unlock(&tasks_lock, true);
[37c57f2]599}
[7509ddc]600
[cc73a8a1]601/** @}
[b45c443]602 */
Note: See TracBrowser for help on using the repository browser.