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

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

Add tsk_destructor() to free task's kobject

Freeing up kobject in task_destroy() leads to a kernel panic the next time the
task object is reused from the slab cache.

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