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

Last change on this file since 40043e8 was 2cc569a3, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Removing inclusion of halt.h

Several files used to include halt.h even though
this was not necessary. The inclusion has therefore
been removed

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