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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9e889f6 was abf6c01, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Add FRAME_ATOMIC to some allocations

  • Property mode set to 100644
File size: 16.5 KB
Line 
1/*
2 * Copyright (c) 2010 Jakub Jermar
3 * Copyright (c) 2018 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup kernel_generic_proc
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Task management.
37 */
38
39#include <assert.h>
40#include <proc/thread.h>
41#include <proc/task.h>
42#include <mm/as.h>
43#include <mm/slab.h>
44#include <atomic.h>
45#include <synch/futex.h>
46#include <synch/spinlock.h>
47#include <synch/waitq.h>
48#include <arch.h>
49#include <barrier.h>
50#include <adt/list.h>
51#include <adt/odict.h>
52#include <cap/cap.h>
53#include <ipc/ipc.h>
54#include <ipc/ipcrsc.h>
55#include <ipc/event.h>
56#include <stdio.h>
57#include <errno.h>
58#include <halt.h>
59#include <str.h>
60#include <syscall/copy.h>
61#include <macros.h>
62
63/** Spinlock protecting the @c tasks ordered dictionary. */
64IRQ_SPINLOCK_INITIALIZE(tasks_lock);
65
66/** Ordered dictionary of active tasks by task ID.
67 *
68 * Members are task_t structures.
69 *
70 * The task is guaranteed to exist after it was found in the @c tasks
71 * dictionary as long as:
72 *
73 * @li the tasks_lock is held,
74 * @li the task's lock is held when task's lock is acquired before releasing
75 * tasks_lock or
76 * @li the task's refcount is greater than 0
77 *
78 */
79odict_t tasks;
80
81static task_id_t task_counter = 0;
82
83static slab_cache_t *task_cache;
84
85/* Forward declarations. */
86static void task_kill_internal(task_t *);
87static errno_t tsk_constructor(void *, unsigned int);
88static size_t tsk_destructor(void *);
89
90static void *tasks_getkey(odlink_t *);
91static int tasks_cmp(void *, void *);
92
93/** Initialize kernel tasks support.
94 *
95 */
96void task_init(void)
97{
98 TASK = NULL;
99 odict_initialize(&tasks, tasks_getkey, tasks_cmp);
100 task_cache = slab_cache_create("task_t", sizeof(task_t), 0,
101 tsk_constructor, tsk_destructor, 0);
102}
103
104/** Kill all tasks except the current task.
105 *
106 */
107void task_done(void)
108{
109 size_t tasks_left;
110 task_t *task;
111
112 if (ipc_box_0) {
113 task_t *task_0 = ipc_box_0->task;
114 ipc_box_0 = NULL;
115 /*
116 * The first task is held by kinit(), we need to release it or
117 * it will never finish cleanup.
118 */
119 task_release(task_0);
120 }
121
122 /* Repeat until there are any tasks except TASK */
123 do {
124#ifdef CONFIG_DEBUG
125 printf("Killing tasks... ");
126#endif
127 irq_spinlock_lock(&tasks_lock, true);
128 tasks_left = 0;
129
130 task = task_first();
131 while (task != NULL) {
132 if (task != TASK) {
133 tasks_left++;
134#ifdef CONFIG_DEBUG
135 printf("[%" PRIu64 "] ", task->taskid);
136#endif
137 task_kill_internal(task);
138 }
139
140 task = task_next(task);
141 }
142
143 irq_spinlock_unlock(&tasks_lock, true);
144
145 thread_sleep(1);
146
147#ifdef CONFIG_DEBUG
148 printf("\n");
149#endif
150 } while (tasks_left > 0);
151}
152
153errno_t tsk_constructor(void *obj, unsigned int kmflags)
154{
155 task_t *task = (task_t *) obj;
156
157 errno_t rc = caps_task_alloc(task);
158 if (rc != EOK)
159 return rc;
160
161 atomic_store(&task->refcount, 0);
162 atomic_store(&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 spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
171 list_initialize(&task->active_calls);
172
173#ifdef CONFIG_UDEBUG
174 /* Init kbox stuff */
175 task->kb.thread = NULL;
176 ipc_answerbox_init(&task->kb.box, task);
177 mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
178#endif
179
180 return EOK;
181}
182
183size_t tsk_destructor(void *obj)
184{
185 task_t *task = (task_t *) obj;
186
187 caps_task_free(task);
188 return 0;
189}
190
191/** Create new task with no threads.
192 *
193 * @param as Task's address space.
194 * @param name Symbolic name (a copy is made).
195 *
196 * @return New task's structure.
197 *
198 */
199task_t *task_create(as_t *as, const char *name)
200{
201 task_t *task = (task_t *) slab_alloc(task_cache, FRAME_ATOMIC);
202 if (!task)
203 return NULL;
204
205 task_create_arch(task);
206
207 task->as = as;
208 str_cpy(task->name, TASK_NAME_BUFLEN, name);
209
210 task->container = CONTAINER;
211 task->perms = 0;
212 task->ucycles = 0;
213 task->kcycles = 0;
214
215 caps_task_init(task);
216
217 task->ipc_info.call_sent = 0;
218 task->ipc_info.call_received = 0;
219 task->ipc_info.answer_sent = 0;
220 task->ipc_info.answer_received = 0;
221 task->ipc_info.irq_notif_received = 0;
222 task->ipc_info.forwarded = 0;
223
224 event_task_init(task);
225
226 task->answerbox.active = true;
227
228#ifdef CONFIG_UDEBUG
229 /* Init debugging stuff */
230 udebug_task_init(&task->udebug);
231
232 /* Init kbox stuff */
233 task->kb.box.active = true;
234 task->kb.finished = false;
235#endif
236
237 if ((ipc_box_0) &&
238 (container_check(ipc_box_0->task->container, task->container))) {
239 cap_phone_handle_t phone_handle;
240 errno_t rc = phone_alloc(task, true, &phone_handle, NULL);
241 if (rc != EOK) {
242 task->as = NULL;
243 task_destroy_arch(task);
244 slab_free(task_cache, task);
245 return NULL;
246 }
247
248 kobject_t *phone_obj = kobject_get(task, phone_handle,
249 KOBJECT_TYPE_PHONE);
250 (void) ipc_phone_connect(phone_obj->phone, ipc_box_0);
251 }
252
253 futex_task_init(task);
254
255 irq_spinlock_lock(&tasks_lock, true);
256
257 task->taskid = ++task_counter;
258 odlink_initialize(&task->ltasks);
259 odict_insert(&task->ltasks, &tasks, NULL);
260
261 irq_spinlock_unlock(&tasks_lock, true);
262
263 return task;
264}
265
266/** Destroy task.
267 *
268 * @param task Task to be destroyed.
269 *
270 */
271void task_destroy(task_t *task)
272{
273 /*
274 * Remove the task from the task B+tree.
275 */
276 irq_spinlock_lock(&tasks_lock, true);
277 odict_remove(&task->ltasks);
278 irq_spinlock_unlock(&tasks_lock, true);
279
280 /*
281 * Perform architecture specific task destruction.
282 */
283 task_destroy_arch(task);
284
285 /*
286 * Drop our reference to the address space.
287 */
288 as_release(task->as);
289
290 slab_free(task_cache, task);
291}
292
293/** Hold a reference to a task.
294 *
295 * Holding a reference to a task prevents destruction of that task.
296 *
297 * @param task Task to be held.
298 *
299 */
300void task_hold(task_t *task)
301{
302 atomic_inc(&task->refcount);
303}
304
305/** Release a reference to a task.
306 *
307 * The last one to release a reference to a task destroys the task.
308 *
309 * @param task Task to be released.
310 *
311 */
312void task_release(task_t *task)
313{
314 if ((atomic_predec(&task->refcount)) == 0)
315 task_destroy(task);
316}
317
318#ifdef __32_BITS__
319
320/** Syscall for reading task ID from userspace (32 bits)
321 *
322 * @param uspace_taskid Pointer to user-space buffer
323 * where to store current task ID.
324 *
325 * @return Zero on success or an error code from @ref errno.h.
326 *
327 */
328sys_errno_t sys_task_get_id(sysarg64_t *uspace_taskid)
329{
330 /*
331 * No need to acquire lock on TASK because taskid remains constant for
332 * the lifespan of the task.
333 */
334 return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
335 sizeof(TASK->taskid));
336}
337
338#endif /* __32_BITS__ */
339
340#ifdef __64_BITS__
341
342/** Syscall for reading task ID from userspace (64 bits)
343 *
344 * @return Current task ID.
345 *
346 */
347sysarg_t sys_task_get_id(void)
348{
349 /*
350 * No need to acquire lock on TASK because taskid remains constant for
351 * the lifespan of the task.
352 */
353 return TASK->taskid;
354}
355
356#endif /* __64_BITS__ */
357
358/** Syscall for setting the task name.
359 *
360 * The name simplifies identifying the task in the task list.
361 *
362 * @param name The new name for the task. (typically the same
363 * as the command used to execute it).
364 *
365 * @return 0 on success or an error code from @ref errno.h.
366 *
367 */
368sys_errno_t sys_task_set_name(const char *uspace_name, size_t name_len)
369{
370 char namebuf[TASK_NAME_BUFLEN];
371
372 /* Cap length of name and copy it from userspace. */
373 if (name_len > TASK_NAME_BUFLEN - 1)
374 name_len = TASK_NAME_BUFLEN - 1;
375
376 errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
377 if (rc != EOK)
378 return (sys_errno_t) rc;
379
380 namebuf[name_len] = '\0';
381
382 /*
383 * As the task name is referenced also from the
384 * threads, lock the threads' lock for the course
385 * of the update.
386 */
387
388 irq_spinlock_lock(&tasks_lock, true);
389 irq_spinlock_lock(&TASK->lock, false);
390 irq_spinlock_lock(&threads_lock, false);
391
392 /* Set task name */
393 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
394
395 irq_spinlock_unlock(&threads_lock, false);
396 irq_spinlock_unlock(&TASK->lock, false);
397 irq_spinlock_unlock(&tasks_lock, true);
398
399 return EOK;
400}
401
402/** Syscall to forcefully terminate a task
403 *
404 * @param uspace_taskid Pointer to task ID in user space.
405 *
406 * @return 0 on success or an error code from @ref errno.h.
407 *
408 */
409sys_errno_t sys_task_kill(task_id_t *uspace_taskid)
410{
411 task_id_t taskid;
412 errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
413 if (rc != EOK)
414 return (sys_errno_t) rc;
415
416 return (sys_errno_t) task_kill(taskid);
417}
418
419/** Find task structure corresponding to task ID.
420 *
421 * The tasks_lock must be already held by the caller of this function and
422 * interrupts must be disabled.
423 *
424 * @param id Task ID.
425 *
426 * @return Task structure address or NULL if there is no such task ID.
427 *
428 */
429task_t *task_find_by_id(task_id_t id)
430{
431 assert(interrupts_disabled());
432 assert(irq_spinlock_locked(&tasks_lock));
433
434 odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
435 if (odlink != NULL)
436 return odict_get_instance(odlink, task_t, ltasks);
437
438 return NULL;
439}
440
441/** Get count of tasks.
442 *
443 * @return Number of tasks in the system
444 */
445size_t task_count(void)
446{
447 assert(interrupts_disabled());
448 assert(irq_spinlock_locked(&tasks_lock));
449
450 return odict_count(&tasks);
451}
452
453/** Get first task (task with lowest ID).
454 *
455 * @return Pointer to first task or @c NULL if there are none.
456 */
457task_t *task_first(void)
458{
459 odlink_t *odlink;
460
461 assert(interrupts_disabled());
462 assert(irq_spinlock_locked(&tasks_lock));
463
464 odlink = odict_first(&tasks);
465 if (odlink == NULL)
466 return NULL;
467
468 return odict_get_instance(odlink, task_t, ltasks);
469}
470
471/** Get next task (with higher task ID).
472 *
473 * @param cur Current task
474 * @return Pointer to next task or @c NULL if there are no more tasks.
475 */
476task_t *task_next(task_t *cur)
477{
478 odlink_t *odlink;
479
480 assert(interrupts_disabled());
481 assert(irq_spinlock_locked(&tasks_lock));
482
483 odlink = odict_next(&cur->ltasks, &tasks);
484 if (odlink == NULL)
485 return NULL;
486
487 return odict_get_instance(odlink, task_t, ltasks);
488}
489
490/** Get accounting data of given task.
491 *
492 * Note that task lock of 'task' must be already held and interrupts must be
493 * already disabled.
494 *
495 * @param task Pointer to the task.
496 * @param ucycles Out pointer to sum of all user cycles.
497 * @param kcycles Out pointer to sum of all kernel cycles.
498 *
499 */
500void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
501{
502 assert(interrupts_disabled());
503 assert(irq_spinlock_locked(&task->lock));
504
505 /* Accumulated values of task */
506 uint64_t uret = task->ucycles;
507 uint64_t kret = task->kcycles;
508
509 /* Current values of threads */
510 list_foreach(task->threads, th_link, thread_t, thread) {
511 irq_spinlock_lock(&thread->lock, false);
512
513 /* Process only counted threads */
514 if (!thread->uncounted) {
515 if (thread == THREAD) {
516 /* Update accounting of current thread */
517 thread_update_accounting(false);
518 }
519
520 uret += thread->ucycles;
521 kret += thread->kcycles;
522 }
523
524 irq_spinlock_unlock(&thread->lock, false);
525 }
526
527 *ucycles = uret;
528 *kcycles = kret;
529}
530
531static void task_kill_internal(task_t *task)
532{
533 irq_spinlock_lock(&task->lock, false);
534 irq_spinlock_lock(&threads_lock, false);
535
536 /*
537 * Interrupt all threads.
538 */
539
540 list_foreach(task->threads, th_link, thread_t, thread) {
541 bool sleeping = false;
542
543 irq_spinlock_lock(&thread->lock, false);
544
545 thread->interrupted = true;
546 if (thread->state == Sleeping)
547 sleeping = true;
548
549 irq_spinlock_unlock(&thread->lock, false);
550
551 if (sleeping)
552 waitq_interrupt_sleep(thread);
553 }
554
555 irq_spinlock_unlock(&threads_lock, false);
556 irq_spinlock_unlock(&task->lock, false);
557}
558
559/** Kill task.
560 *
561 * This function is idempotent.
562 * It signals all the task's threads to bail it out.
563 *
564 * @param id ID of the task to be killed.
565 *
566 * @return Zero on success or an error code from errno.h.
567 *
568 */
569errno_t task_kill(task_id_t id)
570{
571 if (id == 1)
572 return EPERM;
573
574 irq_spinlock_lock(&tasks_lock, true);
575
576 task_t *task = task_find_by_id(id);
577 if (!task) {
578 irq_spinlock_unlock(&tasks_lock, true);
579 return ENOENT;
580 }
581
582 task_kill_internal(task);
583 irq_spinlock_unlock(&tasks_lock, true);
584
585 return EOK;
586}
587
588/** Kill the currently running task.
589 *
590 * @param notify Send out fault notifications.
591 *
592 * @return Zero on success or an error code from errno.h.
593 *
594 */
595void task_kill_self(bool notify)
596{
597 /*
598 * User space can subscribe for FAULT events to take action
599 * whenever a task faults (to take a dump, run a debugger, etc.).
600 * The notification is always available, but unless udebug is enabled,
601 * that's all you get.
602 */
603 if (notify) {
604 /* Notify the subscriber that a fault occurred. */
605 if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
606 UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
607#ifdef CONFIG_UDEBUG
608 /* Wait for a debugging session. */
609 udebug_thread_fault();
610#endif
611 }
612 }
613
614 irq_spinlock_lock(&tasks_lock, true);
615 task_kill_internal(TASK);
616 irq_spinlock_unlock(&tasks_lock, true);
617
618 thread_exit();
619}
620
621/** Process syscall to terminate the current task.
622 *
623 * @param notify Send out fault notifications.
624 *
625 */
626sys_errno_t sys_task_exit(sysarg_t notify)
627{
628 task_kill_self(notify);
629 unreachable();
630}
631
632static void task_print(task_t *task, bool additional)
633{
634 irq_spinlock_lock(&task->lock, false);
635
636 uint64_t ucycles;
637 uint64_t kcycles;
638 char usuffix, ksuffix;
639 task_get_accounting(task, &ucycles, &kcycles);
640 order_suffix(ucycles, &ucycles, &usuffix);
641 order_suffix(kcycles, &kcycles, &ksuffix);
642
643#ifdef __32_BITS__
644 if (additional)
645 printf("%-8" PRIu64 " %9zu", task->taskid,
646 atomic_load(&task->refcount));
647 else
648 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
649 " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
650 task->name, task->container, task, task->as,
651 ucycles, usuffix, kcycles, ksuffix);
652#endif
653
654#ifdef __64_BITS__
655 if (additional)
656 printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
657 "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
658 ksuffix, atomic_load(&task->refcount));
659 else
660 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
661 task->taskid, task->name, task->container, task, task->as);
662#endif
663
664 irq_spinlock_unlock(&task->lock, false);
665}
666
667/** Print task list
668 *
669 * @param additional Print additional information.
670 *
671 */
672void task_print_list(bool additional)
673{
674 /* Messing with task structures, avoid deadlock */
675 irq_spinlock_lock(&tasks_lock, true);
676
677#ifdef __32_BITS__
678 if (additional)
679 printf("[id ] [threads] [calls] [callee\n");
680 else
681 printf("[id ] [name ] [ctn] [address ] [as ]"
682 " [ucycles ] [kcycles ]\n");
683#endif
684
685#ifdef __64_BITS__
686 if (additional)
687 printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
688 " [callee\n");
689 else
690 printf("[id ] [name ] [ctn] [address ]"
691 " [as ]\n");
692#endif
693
694 task_t *task;
695
696 task = task_first();
697 while (task != NULL) {
698 task_print(task, additional);
699 task = task_next(task);
700 }
701
702 irq_spinlock_unlock(&tasks_lock, true);
703}
704
705/** Get key function for the @c tasks ordered dictionary.
706 *
707 * @param odlink Link
708 * @return Pointer to task ID cast as 'void *'
709 */
710static void *tasks_getkey(odlink_t *odlink)
711{
712 task_t *task = odict_get_instance(odlink, task_t, ltasks);
713 return (void *) &task->taskid;
714}
715
716/** Key comparison function for the @c tasks ordered dictionary.
717 *
718 * @param a Pointer to thread A ID
719 * @param b Pointer to thread B ID
720 * @return -1, 0, 1 iff ID A is less than, equal to, greater than B
721 */
722static int tasks_cmp(void *a, void *b)
723{
724 task_id_t ida = *(task_id_t *)a;
725 task_id_t idb = *(task_id_t *)b;
726
727 if (ida < idb)
728 return -1;
729 else if (ida == idb)
730 return 0;
731 else
732 return +1;
733}
734
735/** @}
736 */
Note: See TracBrowser for help on using the repository browser.