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

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

Turn IRQ structures into kernel objects

ipc_irq_subscribe() now returns a capability for the underlying IRQ kernel
object. ipc_irq_unsubscribe() can now be done only with a valid IRQ capability.

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