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

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

Instead of printing the standard kill message, only inform the user when a task
is killed because of its failure to late-reserve memory. This happens, for
example, when there is not enough memory to grow thread stack.

  • Property mode set to 100644
File size: 15.0 KB
Line 
1/*
2 * Copyright (c) 2010 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 <ipc/event.h>
53#include <print.h>
54#include <errno.h>
55#include <func.h>
56#include <str.h>
57#include <memstr.h>
58#include <syscall/copy.h>
59#include <macros.h>
60
61/** Spinlock protecting the tasks_tree AVL tree. */
62IRQ_SPINLOCK_INITIALIZE(tasks_lock);
63
64/** AVL tree of active tasks.
65 *
66 * The task is guaranteed to exist after it was found in the tasks_tree as
67 * long as:
68 *
69 * @li the tasks_lock is held,
70 * @li the task's lock is held when task's lock is acquired before releasing
71 * tasks_lock or
72 * @li the task's refcount is greater than 0
73 *
74 */
75avltree_t tasks_tree;
76
77static task_id_t task_counter = 0;
78
79static slab_cache_t *task_slab;
80
81/* Forward declarations. */
82static void task_kill_internal(task_t *);
83static int tsk_constructor(void *, unsigned int);
84
85/** Initialize kernel tasks support.
86 *
87 */
88void task_init(void)
89{
90 TASK = NULL;
91 avltree_create(&tasks_tree);
92 task_slab = slab_cache_create("task_t", sizeof(task_t), 0,
93 tsk_constructor, NULL, 0);
94}
95
96/** Task finish walker.
97 *
98 * The idea behind this walker is to kill and count all tasks different from
99 * TASK.
100 *
101 */
102static bool task_done_walker(avltree_node_t *node, void *arg)
103{
104 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
105 size_t *cnt = (size_t *) arg;
106
107 if (task != TASK) {
108 (*cnt)++;
109
110#ifdef CONFIG_DEBUG
111 printf("[%"PRIu64"] ", task->taskid);
112#endif
113
114 task_kill_internal(task);
115 }
116
117 /* Continue the walk */
118 return true;
119}
120
121/** Kill all tasks except the current task.
122 *
123 */
124void task_done(void)
125{
126 size_t tasks_left;
127
128 /* Repeat until there are any tasks except TASK */
129 do {
130#ifdef CONFIG_DEBUG
131 printf("Killing tasks... ");
132#endif
133
134 irq_spinlock_lock(&tasks_lock, true);
135 tasks_left = 0;
136 avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
137 irq_spinlock_unlock(&tasks_lock, true);
138
139 thread_sleep(1);
140
141#ifdef CONFIG_DEBUG
142 printf("\n");
143#endif
144 } while (tasks_left > 0);
145}
146
147int tsk_constructor(void *obj, unsigned int kmflags)
148{
149 task_t *task = (task_t *) obj;
150
151 atomic_set(&task->refcount, 0);
152 atomic_set(&task->lifecount, 0);
153
154 irq_spinlock_initialize(&task->lock, "task_t_lock");
155 mutex_initialize(&task->futexes_lock, MUTEX_PASSIVE);
156
157 list_initialize(&task->threads);
158
159 ipc_answerbox_init(&task->answerbox, task);
160
161 size_t i;
162 for (i = 0; i < IPC_MAX_PHONES; i++)
163 ipc_phone_init(&task->phones[i], task);
164
165 spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
166 list_initialize(&task->active_calls);
167
168#ifdef CONFIG_UDEBUG
169 /* Init kbox stuff */
170 task->kb.thread = NULL;
171 ipc_answerbox_init(&task->kb.box, task);
172 mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
173#endif
174
175 return 0;
176}
177
178/** Create new task with no threads.
179 *
180 * @param as Task's address space.
181 * @param name Symbolic name (a copy is made).
182 *
183 * @return New task's structure.
184 *
185 */
186task_t *task_create(as_t *as, const char *name)
187{
188 task_t *task = (task_t *) slab_alloc(task_slab, 0);
189 task_create_arch(task);
190
191 task->as = as;
192 str_cpy(task->name, TASK_NAME_BUFLEN, name);
193
194 task->container = CONTAINER;
195 task->capabilities = 0;
196 task->ucycles = 0;
197 task->kcycles = 0;
198
199 task->silent_kill = false;
200
201 task->ipc_info.call_sent = 0;
202 task->ipc_info.call_received = 0;
203 task->ipc_info.answer_sent = 0;
204 task->ipc_info.answer_received = 0;
205 task->ipc_info.irq_notif_received = 0;
206 task->ipc_info.forwarded = 0;
207
208 event_task_init(task);
209
210 task->answerbox.active = true;
211
212#ifdef CONFIG_UDEBUG
213 /* Init debugging stuff */
214 udebug_task_init(&task->udebug);
215
216 /* Init kbox stuff */
217 task->kb.box.active = true;
218 task->kb.finished = false;
219#endif
220
221 if ((ipc_phone_0) &&
222 (container_check(ipc_phone_0->task->container, task->container)))
223 (void) ipc_phone_connect(&task->phones[0], ipc_phone_0);
224
225 btree_create(&task->futexes);
226
227 /*
228 * Get a reference to the address space.
229 */
230 as_hold(task->as);
231
232 irq_spinlock_lock(&tasks_lock, true);
233
234 task->taskid = ++task_counter;
235 avltree_node_initialize(&task->tasks_tree_node);
236 task->tasks_tree_node.key = task->taskid;
237 avltree_insert(&tasks_tree, &task->tasks_tree_node);
238
239 irq_spinlock_unlock(&tasks_lock, true);
240
241 return task;
242}
243
244/** Destroy task.
245 *
246 * @param task Task to be destroyed.
247 *
248 */
249void task_destroy(task_t *task)
250{
251 /*
252 * Remove the task from the task B+tree.
253 */
254 irq_spinlock_lock(&tasks_lock, true);
255 avltree_delete(&tasks_tree, &task->tasks_tree_node);
256 irq_spinlock_unlock(&tasks_lock, true);
257
258 /*
259 * Perform architecture specific task destruction.
260 */
261 task_destroy_arch(task);
262
263 /*
264 * Free up dynamically allocated state.
265 */
266 btree_destroy(&task->futexes);
267
268 /*
269 * Drop our reference to the address space.
270 */
271 as_release(task->as);
272
273 slab_free(task_slab, task);
274}
275
276/** Hold a reference to a task.
277 *
278 * Holding a reference to a task prevents destruction of that task.
279 *
280 * @param task Task to be held.
281 *
282 */
283void task_hold(task_t *task)
284{
285 atomic_inc(&task->refcount);
286}
287
288/** Release a reference to a task.
289 *
290 * The last one to release a reference to a task destroys the task.
291 *
292 * @param task Task to be released.
293 *
294 */
295void task_release(task_t *task)
296{
297 if ((atomic_predec(&task->refcount)) == 0)
298 task_destroy(task);
299}
300
301#ifdef __32_BITS__
302
303/** Syscall for reading task ID from userspace (32 bits)
304 *
305 * @param uspace_taskid Pointer to user-space buffer
306 * where to store current task ID.
307 *
308 * @return Zero on success or an error code from @ref errno.h.
309 *
310 */
311sysarg_t sys_task_get_id(sysarg64_t *uspace_taskid)
312{
313 /*
314 * No need to acquire lock on TASK because taskid remains constant for
315 * the lifespan of the task.
316 */
317 return (sysarg_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
318 sizeof(TASK->taskid));
319}
320
321#endif /* __32_BITS__ */
322
323#ifdef __64_BITS__
324
325/** Syscall for reading task ID from userspace (64 bits)
326 *
327 * @return Current task ID.
328 *
329 */
330sysarg_t sys_task_get_id(void)
331{
332 /*
333 * No need to acquire lock on TASK because taskid remains constant for
334 * the lifespan of the task.
335 */
336 return TASK->taskid;
337}
338
339#endif /* __64_BITS__ */
340
341/** Syscall for setting the task name.
342 *
343 * The name simplifies identifying the task in the task list.
344 *
345 * @param name The new name for the task. (typically the same
346 * as the command used to execute it).
347 *
348 * @return 0 on success or an error code from @ref errno.h.
349 *
350 */
351sysarg_t sys_task_set_name(const char *uspace_name, size_t name_len)
352{
353 char namebuf[TASK_NAME_BUFLEN];
354
355 /* Cap length of name and copy it from userspace. */
356 if (name_len > TASK_NAME_BUFLEN - 1)
357 name_len = TASK_NAME_BUFLEN - 1;
358
359 int rc = copy_from_uspace(namebuf, uspace_name, name_len);
360 if (rc != 0)
361 return (sysarg_t) rc;
362
363 namebuf[name_len] = '\0';
364
365 /*
366 * As the task name is referenced also from the
367 * threads, lock the threads' lock for the course
368 * of the update.
369 */
370
371 irq_spinlock_lock(&tasks_lock, true);
372 irq_spinlock_lock(&TASK->lock, false);
373 irq_spinlock_lock(&threads_lock, false);
374
375 /* Set task name */
376 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
377
378 irq_spinlock_unlock(&threads_lock, false);
379 irq_spinlock_unlock(&TASK->lock, false);
380 irq_spinlock_unlock(&tasks_lock, true);
381
382 return EOK;
383}
384
385/** Syscall to forcefully terminate a task
386 *
387 * @param uspace_taskid Pointer to task ID in user space.
388 *
389 * @return 0 on success or an error code from @ref errno.h.
390 *
391 */
392sysarg_t sys_task_kill(task_id_t *uspace_taskid)
393{
394 task_id_t taskid;
395 int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
396 if (rc != 0)
397 return (sysarg_t) rc;
398
399 return (sysarg_t) task_kill(taskid);
400}
401
402/** Find task structure corresponding to task ID.
403 *
404 * The tasks_lock must be already held by the caller of this function and
405 * interrupts must be disabled.
406 *
407 * @param id Task ID.
408 *
409 * @return Task structure address or NULL if there is no such task ID.
410 *
411 */
412task_t *task_find_by_id(task_id_t id)
413{
414 ASSERT(interrupts_disabled());
415 ASSERT(irq_spinlock_locked(&tasks_lock));
416
417 avltree_node_t *node =
418 avltree_search(&tasks_tree, (avltree_key_t) id);
419
420 if (node)
421 return avltree_get_instance(node, task_t, tasks_tree_node);
422
423 return NULL;
424}
425
426/** Get accounting data of given task.
427 *
428 * Note that task lock of 'task' must be already held and interrupts must be
429 * already disabled.
430 *
431 * @param task Pointer to the task.
432 * @param ucycles Out pointer to sum of all user cycles.
433 * @param kcycles Out pointer to sum of all kernel cycles.
434 *
435 */
436void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
437{
438 ASSERT(interrupts_disabled());
439 ASSERT(irq_spinlock_locked(&task->lock));
440
441 /* Accumulated values of task */
442 uint64_t uret = task->ucycles;
443 uint64_t kret = task->kcycles;
444
445 /* Current values of threads */
446 list_foreach(task->threads, cur) {
447 thread_t *thread = list_get_instance(cur, thread_t, th_link);
448
449 irq_spinlock_lock(&thread->lock, false);
450
451 /* Process only counted threads */
452 if (!thread->uncounted) {
453 if (thread == THREAD) {
454 /* Update accounting of current thread */
455 thread_update_accounting(false);
456 }
457
458 uret += thread->ucycles;
459 kret += thread->kcycles;
460 }
461
462 irq_spinlock_unlock(&thread->lock, false);
463 }
464
465 *ucycles = uret;
466 *kcycles = kret;
467}
468
469static void task_kill_internal(task_t *task)
470{
471 irq_spinlock_lock(&task->lock, false);
472 irq_spinlock_lock(&threads_lock, false);
473
474 /*
475 * Interrupt all threads.
476 */
477
478 list_foreach(task->threads, cur) {
479 thread_t *thread = list_get_instance(cur, thread_t, th_link);
480 bool sleeping = false;
481
482 irq_spinlock_lock(&thread->lock, false);
483
484 thread->interrupted = true;
485 if (thread->state == Sleeping)
486 sleeping = true;
487
488 irq_spinlock_unlock(&thread->lock, false);
489
490 if (sleeping)
491 waitq_interrupt_sleep(thread);
492 }
493
494 irq_spinlock_unlock(&threads_lock, false);
495 irq_spinlock_unlock(&task->lock, false);
496}
497
498/** Kill task.
499 *
500 * This function is idempotent.
501 * It signals all the task's threads to bail it out.
502 *
503 * @param id ID of the task to be killed.
504 *
505 * @return Zero on success or an error code from errno.h.
506 *
507 */
508int task_kill(task_id_t id)
509{
510 if (id == 1)
511 return EPERM;
512
513 irq_spinlock_lock(&tasks_lock, true);
514
515 task_t *task = task_find_by_id(id);
516 if (!task) {
517 irq_spinlock_unlock(&tasks_lock, true);
518 return ENOENT;
519 }
520
521 task_kill_internal(task);
522 irq_spinlock_unlock(&tasks_lock, true);
523
524 return EOK;
525}
526
527/** Kill the currently running task.
528 *
529 * @param notify Send out fault notifications.
530 *
531 * @return Zero on success or an error code from errno.h.
532 *
533 */
534void task_kill_self(bool notify)
535{
536 /*
537 * User space can subscribe for FAULT events to take action
538 * whenever a task faults (to take a dump, run a debugger, etc.).
539 * The notification is always available, but unless udebug is enabled,
540 * that's all you get.
541 */
542 if (notify) {
543 /* Notify the subscriber that a fault occurred. */
544 if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
545 UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
546#ifdef CONFIG_UDEBUG
547 /* Wait for a debugging session. */
548 udebug_thread_fault();
549#endif
550 }
551 }
552
553 irq_spinlock_lock(&tasks_lock, true);
554 task_kill_internal(TASK);
555 irq_spinlock_unlock(&tasks_lock, true);
556
557 thread_exit();
558}
559
560/** Process syscall to terminate the current task.
561 *
562 * @param notify Send out fault notifications.
563 *
564 */
565sysarg_t sys_task_exit(sysarg_t notify)
566{
567 task_kill_self(notify);
568
569 /* Unreachable */
570 return EOK;
571}
572
573static bool task_print_walker(avltree_node_t *node, void *arg)
574{
575 bool *additional = (bool *) arg;
576 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
577 irq_spinlock_lock(&task->lock, false);
578
579 uint64_t ucycles;
580 uint64_t kcycles;
581 char usuffix, ksuffix;
582 task_get_accounting(task, &ucycles, &kcycles);
583 order_suffix(ucycles, &ucycles, &usuffix);
584 order_suffix(kcycles, &kcycles, &ksuffix);
585
586#ifdef __32_BITS__
587 if (*additional)
588 printf("%-8" PRIu64 " %9" PRIua, task->taskid,
589 atomic_get(&task->refcount));
590 else
591 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
592 " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
593 task->name, task->container, task, task->as,
594 ucycles, usuffix, kcycles, ksuffix);
595#endif
596
597#ifdef __64_BITS__
598 if (*additional)
599 printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
600 "%9" PRIua, task->taskid, ucycles, usuffix, kcycles,
601 ksuffix, atomic_get(&task->refcount));
602 else
603 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
604 task->taskid, task->name, task->container, task, task->as);
605#endif
606
607 if (*additional) {
608 size_t i;
609 for (i = 0; i < IPC_MAX_PHONES; i++) {
610 if (task->phones[i].callee)
611 printf(" %zu:%p", i, task->phones[i].callee);
612 }
613 printf("\n");
614 }
615
616 irq_spinlock_unlock(&task->lock, false);
617 return true;
618}
619
620/** Print task list
621 *
622 * @param additional Print additional information.
623 *
624 */
625void task_print_list(bool additional)
626{
627 /* Messing with task structures, avoid deadlock */
628 irq_spinlock_lock(&tasks_lock, true);
629
630#ifdef __32_BITS__
631 if (additional)
632 printf("[id ] [threads] [calls] [callee\n");
633 else
634 printf("[id ] [name ] [ctn] [address ] [as ]"
635 " [ucycles ] [kcycles ]\n");
636#endif
637
638#ifdef __64_BITS__
639 if (additional)
640 printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
641 " [callee\n");
642 else
643 printf("[id ] [name ] [ctn] [address ]"
644 " [as ]\n");
645#endif
646
647 avltree_walk(&tasks_tree, task_print_walker, &additional);
648
649 irq_spinlock_unlock(&tasks_lock, true);
650}
651
652/** @}
653 */
Note: See TracBrowser for help on using the repository browser.