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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c28413a9 was 669f3d32, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Adapted the kernel futex subsystem to use CHT.

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