source: mainline/kernel/generic/src/proc/task.c@ 44a7ee5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 44a7ee5 was 44a7ee5, checked in by Jiri Svoboda <jiri@…>, 8 years ago

memxxx functions should be provided in the kernel via the same header as in userspace (mem.h).

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