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

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

Turn IPC phones into kobjects

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